3084350767dad16d6652d3d208ebcfae24240aa1
[platform/core/api/peripheral-io.git] / src / interface / peripheral_interface_gpio.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "peripheral_interface_gpio.h"
18
19 int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
20 {
21         static predefined_type_s types[3] = {
22                 {"in",   2},
23                 {"high", 4},
24                 {"low",  3}
25         };
26
27         int ret = write(gpio->fd_direction, types[direction].type, types[direction].len);
28         CHECK_ERROR(ret != types[direction].len);
29
30         return 0;
31 }
32
33 int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
34 {
35         static predefined_type_s types[4] = {
36                 {"none",    4},
37                 {"rising",  6},
38                 {"falling", 7},
39                 {"both",    4}
40         };
41
42         int ret = write(gpio->fd_edge, types[edge].type, types[edge].len);
43         CHECK_ERROR(ret != types[edge].len);
44
45         return 0;
46 }
47
48 int peripheral_interface_gpio_write(peripheral_gpio_h gpio, uint32_t value)
49 {
50         static predefined_type_s types[2] = {
51                 {"0", 1},
52                 {"1", 1}
53         };
54
55         int ret = write(gpio->fd_value, types[value].type, types[value].len);
56         CHECK_ERROR(ret != types[value].len);
57
58         return 0;
59 }
60
61 int peripheral_interface_gpio_read(peripheral_gpio_h gpio, uint32_t *value)
62 {
63         int ret;
64         int length = 1;
65         char gpio_buf[GPIO_BUFFER_MAX] = {0, };
66
67         ret = read(gpio->fd_value, &gpio_buf, length);
68         CHECK_ERROR(ret != length);
69
70         if (gpio_buf[0] == '0')
71                 *value = 0;
72         else if (gpio_buf[0] == '1')
73                 *value = 1;
74         else
75                 _E("Error: gpio value is error \n");
76                 return -EIO;
77
78         return 0;
79 }
80
81 int peripheral_interface_gpio_close(peripheral_gpio_h gpio)
82 {
83         int ret;
84
85         ret = close(gpio->fd_direction);
86         CHECK_ERROR(ret != 0);
87
88         ret = close(gpio->fd_edge);
89         CHECK_ERROR(ret != 0);
90
91         ret = close(gpio->fd_value);
92         CHECK_ERROR(ret != 0);
93
94         return 0;
95 }
96
97 int peripheral_interface_gpio_open_isr(peripheral_gpio_h gpio)
98 {
99         // TODO: set interrupted callback function
100
101         return 0;
102 }
103
104 int peripheral_interface_gpio_close_isr(peripheral_gpio_h gpio)
105 {
106         // TODO: unset interrupted callback function
107
108         return 0;
109 }