f0bf6f282bd68154cc8dcd877e357d37ada84fb6
[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 #define MAX_ERR_LEN 255
20
21 int peripheral_interface_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
22 {
23         static predefined_type_s types[3] = {
24                 {"in",   2},
25                 {"high", 4},
26                 {"low",  3}
27         };
28
29         int ret = write(gpio->fd_direction, types[direction].type, types[direction].len);
30         CHECK_ERROR(ret != types[direction].len);
31
32         return 0;
33 }
34
35 int peripheral_interface_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
36 {
37         static predefined_type_s types[4] = {
38                 {"none",    4},
39                 {"rising",  6},
40                 {"falling", 7},
41                 {"both",    4}
42         };
43
44         int ret = write(gpio->fd_edge, types[edge].type, types[edge].len);
45         CHECK_ERROR(ret != types[edge].len);
46
47         return 0;
48 }
49
50 int peripheral_interface_gpio_write(peripheral_gpio_h gpio, uint32_t value)
51 {
52         static predefined_type_s types[2] = {
53                 {"0", 1},
54                 {"1", 1}
55         };
56
57         int ret = write(gpio->fd_value, types[value].type, types[value].len);
58         CHECK_ERROR(ret != types[value].len);
59
60         return 0;
61 }
62
63 int peripheral_interface_gpio_read(peripheral_gpio_h gpio, uint32_t *value)
64 {
65         int ret;
66         int length = 1;
67         char gpio_buf[GPIO_BUFFER_MAX] = {0, };
68
69         ret = read(gpio->fd_value, &gpio_buf, length);
70         CHECK_ERROR(ret != length);
71
72         if (gpio_buf[0] == '0')
73                 *value = 0;
74         else if (gpio_buf[0] == '1')
75                 *value = 1;
76         else
77                 _E("Error: gpio value is error \n");
78                 return -EIO;
79
80         return 0;
81 }
82
83 int peripheral_interface_gpio_close(peripheral_gpio_h gpio)
84 {
85         int ret;
86
87         ret = close(gpio->fd_direction);
88         CHECK_ERROR(ret != 0);
89
90         ret = close(gpio->fd_edge);
91         CHECK_ERROR(ret != 0);
92
93         ret = close(gpio->fd_value);
94         CHECK_ERROR(ret != 0);
95
96         return 0;
97 }
98
99 int peripheral_interface_gpio_open_isr(peripheral_gpio_h gpio)
100 {
101         // TODO: set interrupted callback function
102
103         return 0;
104 }
105
106 int peripheral_interface_gpio_close_isr(peripheral_gpio_h gpio)
107 {
108         // TODO: unset interrupted callback function
109
110         return 0;
111 }