2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "peripheral_common.h"
28 #define MAX_ERR_LEN 255
30 int gpio_get_edge_mode(int gpiopin, gpio_edge_e *edge)
33 char gpio_dev[GPIO_BUFFER_MAX] = {0, };
34 char gpio_buf[GPIO_BUFFER_MAX] = {0, };
36 snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/edge", gpiopin);
37 fd = open(gpio_dev, O_RDONLY);
40 char errmsg[MAX_ERR_LEN];
41 strerror_r(errno, errmsg, MAX_ERR_LEN);
42 _E("Can't Open /sys/class/gpio/gpio%d/edge: %s\n", gpiopin, errmsg);
46 len = read(fd, &gpio_buf, GPIO_BUFFER_MAX);
49 _E("Error: gpio edge read error\n");
53 if (0 == strncmp(gpio_buf, "none", strlen("none")))
54 *edge = GPIO_EDGE_NONE;
55 else if (0 == strncmp(gpio_buf, "both", strlen("both")))
56 *edge = GPIO_EDGE_BOTH;
57 else if (0 == strncmp(gpio_buf, "rising", strlen("rising")))
58 *edge = GPIO_EDGE_RISING;
59 else if (0 == strncmp(gpio_buf, "falling", strlen("falling")))
60 *edge = GPIO_EDGE_FALLING;
63 _E("Error: gpio edge is wrong\n");
72 int gpio_get_direction(int gpiopin, gpio_direction_e *dir)
75 char gpio_dev[GPIO_BUFFER_MAX] = {0, };
76 char gpio_buf[GPIO_BUFFER_MAX] = {0, };
78 snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/direction", gpiopin);
79 fd = open(gpio_dev, O_RDONLY);
82 char errmsg[MAX_ERR_LEN];
83 strerror_r(errno, errmsg, MAX_ERR_LEN);
84 _E("Can't Open /sys/class/gpio/gpio%d/direction: %s\n", gpiopin, errmsg);
88 len = read(fd, &gpio_buf, GPIO_BUFFER_MAX);
91 _E("Error: gpio direction read error\n");
95 if (0 == strncmp(gpio_buf, "in", strlen("in")))
96 *dir = GPIO_DIRECTION_IN;
97 else if (0 == strncmp(gpio_buf, "out", strlen("out")))
98 *dir = GPIO_DIRECTION_OUT;
101 _E("Error: gpio direction is wrong\n");
110 int gpio_read(int gpiopin, int *value)
113 char gpio_dev[GPIO_BUFFER_MAX] = {0, };
114 char gpio_buf[GPIO_BUFFER_MAX] = {0, };
116 snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/value", gpiopin);
117 fd = open(gpio_dev, O_RDONLY);
119 char errmsg[MAX_ERR_LEN];
120 strerror_r(errno, errmsg, MAX_ERR_LEN);
121 _E("Can't Open /sys/class/gpio/gpio%d pin value: %s\n", gpiopin, errmsg);
125 len = read(fd, &gpio_buf, 1);
129 _E("Error: gpio read error \n");
133 if (0 == strncmp(gpio_buf, "1", strlen("1")))
135 else if (0 == strncmp(gpio_buf, "0", strlen("0")))
138 _E("Error: gpio value is error \n");
146 int gpio_open(int gpiopin)
149 char gpio_export[GPIO_BUFFER_MAX] = {0, };
153 fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
156 char errmsg[MAX_ERR_LEN];
157 strerror_r(errno, errmsg, MAX_ERR_LEN);
158 _E("Can't Open /sys/class/gpio/export :%s\n", errmsg);
162 len = snprintf(gpio_export, GPIO_BUFFER_MAX, "%d", gpiopin);
163 status = write(fd, gpio_export, len);
167 _E("Error: gpio open error \n");
176 int gpio_close(int gpiopin)
179 char gpio_unexport[GPIO_BUFFER_MAX] = {0, };
181 fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
183 char errmsg[MAX_ERR_LEN];
184 strerror_r(errno, errmsg, MAX_ERR_LEN);
185 _E("Can't Open /sys/class/gpio/unexport %s\n", errmsg);
189 len = snprintf(gpio_unexport, GPIO_BUFFER_MAX, "%d", gpiopin);
190 status = write(fd, gpio_unexport, len);
194 _E("Error: gpio open error \n");
204 int gpio_set_edge_mode(int gpiopin, gpio_edge_e edge)
207 char gpio_dev[GPIO_BUFFER_MAX] = {0, };
209 snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/edge", gpiopin);
210 fd = open(gpio_dev, O_WRONLY);
212 char errmsg[MAX_ERR_LEN];
213 strerror_r(errno, errmsg, MAX_ERR_LEN);
214 _E("Can't Open /sys/class/gpio/gpio%d/edge: %s\n", gpiopin, errmsg);
218 if (edge == GPIO_EDGE_NONE)
219 status = write(fd, "none", strlen("none")+1);
220 else if (edge == GPIO_EDGE_RISING)
221 status = write(fd, "rising", strlen("rising")+1);
222 else if (edge == GPIO_EDGE_FALLING)
223 status = write(fd, "falling", strlen("falling")+1);
224 else if (edge == GPIO_EDGE_BOTH)
225 status = write(fd, "both", strlen("both")+1);
228 _E("Error: gpio edge is wrong\n");
234 _E("Error: gpio edge set error\n");
243 int gpio_set_direction(int gpiopin, gpio_direction_e dir)
246 char gpio_dev[GPIO_BUFFER_MAX] = {0, };
248 snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/direction", gpiopin);
249 fd = open(gpio_dev, O_WRONLY);
251 char errmsg[MAX_ERR_LEN];
252 strerror_r(errno, errmsg, MAX_ERR_LEN);
253 _E("Can't Open /sys/class/gpio/gpio%d/direction: %s\n", gpiopin, errmsg);
257 if (dir == GPIO_DIRECTION_OUT)
258 status = write(fd, "out", strlen("out")+1);
259 else if (dir == GPIO_DIRECTION_IN)
260 status = write(fd, "in", strlen("in")+1);
263 _E("Error: gpio direction is wrong\n");
269 _E("Error: gpio direction set error\n");
278 int gpio_write(int gpiopin, int value)
281 char gpio_dev[GPIO_BUFFER_MAX] = {0, };
283 snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/value", gpiopin);
284 fd = open(gpio_dev, O_WRONLY);
287 char errmsg[MAX_ERR_LEN];
288 strerror_r(errno, errmsg, MAX_ERR_LEN);
289 _E("Can't Open /sys/class/gpio/gpio%d/value: %s\n", gpiopin, errmsg);
294 status = write(fd, "1", strlen("1")+1);
296 status = write(fd, "0", strlen("0")+1);
299 _E("Error: gpio write value error \n");
305 _E("Error: gpio write error\n");
314 int gpio_open_isr(int gpiopin)
317 char gpio_dev[GPIO_BUFFER_MAX] = {0, };
319 snprintf(gpio_dev, sizeof(gpio_dev)-1, SYSFS_GPIO_DIR"/gpio%d/value", gpiopin);
321 _D("open isr string [%s]", gpio_dev);
323 fd = open(gpio_dev, O_RDONLY);
325 char errmsg[MAX_ERR_LEN];
326 strerror_r(errno, errmsg, MAX_ERR_LEN);
327 _E("Can't Open /sys/class/gpio/gpio%d pin value: %s\n", gpiopin, errmsg);
334 int gpio_close_isr(int file_hndl)
336 if (file_hndl == (int)NULL)
344 int gpio_read_isr(void *fdset, char *rev_buf, int length)
348 struct pollfd poll_events;
350 poll_events.fd = ((struct pollfd*)fdset)->fd;
351 poll_events.events = POLLPRI;
352 poll_events.revents = ((struct pollfd*)fdset)->revents;
354 poll_state = poll((struct pollfd*)&poll_events, 1, -1); // 0 is going to return directly.
356 if (poll_state < 0) {
357 _E("poll() failed!\n");
361 if (poll_events.revents & POLLPRI) {
362 lseek(poll_events.fd, 0, SEEK_SET);
363 len = read(poll_events.fd, rev_buf, length);