Debian Codegraph @

source/event_gpio.c

raw symbols SCIP
1 /*
2 Copyright (c) 2013-2021 Ben Croston
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include <pthread.h>
24 #include <sys/epoll.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/time.h>
32 #include "event_gpio.h"
33
34 const char *stredge[4] = {"none", "rising", "falling", "both"};
35
36 struct gpios
37 {
38 unsigned int gpio;
39 int value_fd;
40 int exported;
41 int edge;
42 int initial_thread;
43 int initial_wait;
44 int thread_added;
45 int bouncetime;
46 unsigned long long lastcall;
47 struct gpios *next;
48 };
49 struct gpios *gpio_list = NULL;
50
51 // event callbacks
52 struct callback
53 {
54 unsigned int gpio;
55 void (*func)(unsigned int gpio);
56 struct callback *next;
57 };
58 struct callback *callbacks = NULL;
59
60 int event_occurred[54] = { 0 };
61 int thread_running = 0;
62 int epfd_thread = -1;
63 int epfd_blocking = -1;
64
65 /************* /sys/class/gpio functions ************/
66 #define x_write(fd, buf, len) do { \
67 size_t x_write_len = (len); \
68 \
69 if ((size_t)write((fd), (buf), x_write_len) != x_write_len) { \
70 close(fd); \
71 return (-1); \
72 } \
73 } while (/* CONSTCOND */ 0)
74
75 int gpio_export(unsigned int gpio)
76 {
77 int fd, len;
78 char str_gpio[3];
79 char filename[33];
80
81 snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d", gpio);
82
83 /* return if gpio already exported */
84 if (access(filename, F_OK) != -1) {
85 return 0;
86 }
87
88 if ((fd = open("/sys/class/gpio/export", O_WRONLY)) < 0) {
89 return -1;
90 }
91
92 len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
93 x_write(fd, str_gpio, len);
94 close(fd);
95
96 return 0;
97 }
98
99 int gpio_unexport(unsigned int gpio)
100 {
101 int fd, len;
102 char str_gpio[3];
103
104 if ((fd = open("/sys/class/gpio/unexport", O_WRONLY)) < 0)
105 return -1;
106
107 len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
108 x_write(fd, str_gpio, len);
109 close(fd);
110
111 return 0;
112 }
113
114 int gpio_set_direction(unsigned int gpio, unsigned int in_flag)
115 {
116 int retry;
117 struct timespec delay;
118 int fd;
119 char filename[33];
120
121 snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/direction", gpio);
122
123 // retry waiting for udev to set correct file permissions
124 delay.tv_sec = 0;
125 delay.tv_nsec = 10000000L; // 10ms
126 for (retry=0; retry<100; retry++) {
127 if ((fd = open(filename, O_WRONLY)) >= 0)
128 break;
129 nanosleep(&delay, NULL);
130 }
131 if (retry >= 100)
132 return -1;
133
134 if (in_flag)
135 x_write(fd, "in", 3);
136 else
137 x_write(fd, "out", 4);
138
139 close(fd);
140 return 0;
141 }
142
143 int gpio_set_edge(unsigned int gpio, unsigned int edge)
144 {
145 int fd;
146 char filename[28];
147
148 snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", gpio);
149
150 if ((fd = open(filename, O_WRONLY)) < 0)
151 return -1;
152
153 x_write(fd, stredge[edge], strlen(stredge[edge]) + 1);
154 close(fd);
155 return 0;
156 }
157
158 int open_value_file(unsigned int gpio)
159 {
160 int fd;
161 char filename[29];
162
163 // create file descriptor of value file
164 snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/value", gpio);
165 if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0)
166 return -1;
167 return fd;
168 }
169
170 /********* gpio list functions **********/
171 struct gpios *get_gpio(unsigned int gpio)
172 {
173 struct gpios *g = gpio_list;
174 while (g != NULL) {
175 if (g->gpio == gpio)
176 return g;
177 g = g->next;
178 }
179 return NULL;
180 }
181
182 struct gpios *get_gpio_from_value_fd(int fd)
183 {
184 struct gpios *g = gpio_list;
185 while (g != NULL) {
186 if (g->value_fd == fd)
187 return g;
188 g = g->next;
189 }
190 return NULL;
191 }
192
193 struct gpios *new_gpio(unsigned int gpio)
194 {
195 struct gpios *new_gpio;
196
197 new_gpio = malloc(sizeof(struct gpios));
198 if (new_gpio == 0) {
199 return NULL; // out of memory
200 }
201
202 new_gpio->gpio = gpio;
203 if (gpio_export(gpio) != 0) {
204 free(new_gpio);
205 return NULL;
206 }
207 new_gpio->exported = 1;
208
209 if (gpio_set_direction(gpio,1) != 0) { // 1==input
210 free(new_gpio);
211 return NULL;
212 }
213
214 if ((new_gpio->value_fd = open_value_file(gpio)) == -1) {
215 gpio_unexport(gpio);
216 free(new_gpio);
217 return NULL;
218 }
219
220 new_gpio->initial_thread = 1;
221 new_gpio->initial_wait = 1;
222 new_gpio->bouncetime = -666;
223 new_gpio->lastcall = 0;
224 new_gpio->thread_added = 0;
225
226 if (gpio_list == NULL) {
227 new_gpio->next = NULL;
228 } else {
229 new_gpio->next = gpio_list;
230 }
231 gpio_list = new_gpio;
232 return new_gpio;
233 }
234
235 void delete_gpio(unsigned int gpio)
236 {
237 struct gpios *g = gpio_list;
238 struct gpios *prev = NULL;
239
240 while (g != NULL) {
241 if (g->gpio == gpio) {
242 if (prev == NULL)
243 gpio_list = g->next;
244 else
245 prev->next = g->next;
246 free(g);
247 return;
248 } else {
249 prev = g;
250 g = g->next;
251 }
252 }
253 }
254
255 int gpio_event_added(unsigned int gpio)
256 {
257 struct gpios *g = gpio_list;
258 while (g != NULL) {
259 if (g->gpio == gpio)
260 return g->edge;
261 g = g->next;
262 }
263 return 0;
264 }
265
266 /******* callback list functions ********/
267 int add_edge_callback(unsigned int gpio, void (*func)(unsigned int gpio))
268 {
269 struct callback *cb = callbacks;
270 struct callback *new_cb;
271
272 new_cb = malloc(sizeof(struct callback));
273 if (new_cb == 0)
274 return -1; // out of memory
275
276 new_cb->gpio = gpio;
277 new_cb->func = func;
278 new_cb->next = NULL;
279
280 if (callbacks == NULL) {
281 // start new list
282 callbacks = new_cb;
283 } else {
284 // add to end of list
285 while (cb->next != NULL)
286 cb = cb->next;
287 cb->next = new_cb;
288 }
289 return 0;
290 }
291
292 int callback_exists(unsigned int gpio)
293 {
294 struct callback *cb = callbacks;
295 while (cb != NULL) {
296 if (cb->gpio == gpio)
297 return 1;
298 cb = cb->next;
299 }
300 return 0;
301 }
302
303 void run_callbacks(unsigned int gpio)
304 {
305 struct callback *cb = callbacks;
306 while (cb != NULL)
307 {
308 if (cb->gpio == gpio)
309 cb->func(cb->gpio);
310 cb = cb->next;
311 }
312 }
313
314 void remove_callbacks(unsigned int gpio)
315 {
316 struct callback *cb = callbacks;
317 struct callback *temp;
318 struct callback *prev = NULL;
319
320 while (cb != NULL)
321 {
322 if (cb->gpio == gpio)
323 {
324 if (prev == NULL)
325 callbacks = cb->next;
326 else
327 prev->next = cb->next;
328 temp = cb;
329 cb = cb->next;
330 free(temp);
331 } else {
332 prev = cb;
333 cb = cb->next;
334 }
335 }
336 }
337
338 void *poll_thread(void *threadarg)
339 {
340 struct epoll_event events;
341 char buf;
342 struct timeval tv_timenow;
343 unsigned long long timenow;
344 struct gpios *g;
345 int n;
346
347 thread_running = 1;
348 while (thread_running) {
349 n = epoll_wait(epfd_thread, &events, 1, -1);
350 if (n > 0) {
351 lseek(events.data.fd, 0, SEEK_SET);
352 if (read(events.data.fd, &buf, 1) != 1) {
353 thread_running = 0;
354 pthread_exit(NULL);
355 }
356 g = get_gpio_from_value_fd(events.data.fd);
357 if (g->initial_thread) { // ignore first epoll trigger
358 g->initial_thread = 0;
359 } else {
360 gettimeofday(&tv_timenow, NULL);
361 timenow = tv_timenow.tv_sec*1E6 + tv_timenow.tv_usec;
362 if (g->bouncetime == -666 || timenow - g->lastcall > (unsigned int)g->bouncetime*1000 || g->lastcall == 0 || g->lastcall > timenow) {
363 g->lastcall = timenow;
364 event_occurred[g->gpio] = 1;
365 run_callbacks(g->gpio);
366 }
367 }
368 } else if (n == -1) {
369 /* If a signal is received while we are waiting,
370 epoll_wait will return with an EINTR error.
371 Just try again in that case. */
372 if (errno == EINTR) {
373 continue;
374 }
375 thread_running = 0;
376 pthread_exit(NULL);
377 }
378 }
379 thread_running = 0;
380 pthread_exit(NULL);
381 }
382
383 void remove_edge_detect(unsigned int gpio)
384 {
385 struct epoll_event ev;
386 struct gpios *g = get_gpio(gpio);
387
388 if (g == NULL)
389 return;
390
391 // delete epoll of fd
392
393 ev.events = EPOLLIN | EPOLLET | EPOLLPRI;
394 ev.data.fd = g->value_fd;
395 epoll_ctl(epfd_thread, EPOLL_CTL_DEL, g->value_fd, &ev);
396
397 // delete callbacks for gpio
398 remove_callbacks(gpio);
399
400 // btc fixme - check return result??
401 gpio_set_edge(gpio, NO_EDGE);
402 g->edge = NO_EDGE;
403
404 if (g->value_fd != -1)
405 close(g->value_fd);
406
407 // btc fixme - check return result??
408 gpio_unexport(gpio);
409 event_occurred[gpio] = 0;
410
411 delete_gpio(gpio);
412 }
413
414 int event_detected(unsigned int gpio)
415 {
416 if (event_occurred[gpio]) {
417 event_occurred[gpio] = 0;
418 return 1;
419 } else {
420 return 0;
421 }
422 }
423
424 void event_cleanup(int gpio)
425 // gpio of -666 means clean every channel used
426 {
427 struct gpios *g = gpio_list;
428 struct gpios *next_gpio = NULL;
429
430 while (g != NULL) {
431 next_gpio = g->next;
432 if ((gpio == -666) || ((int)g->gpio == gpio))
433 remove_edge_detect(g->gpio);
434 g = next_gpio;
435 }
436 if (gpio_list == NULL) {
437 if (epfd_blocking != -1) {
438 close(epfd_blocking);
439 epfd_blocking = -1;
440 }
441 if (epfd_thread != -1) {
442 close(epfd_thread);
443 epfd_thread = -1;
444 }
445 thread_running = 0;
446 }
447 }
448
449 void event_cleanup_all(void)
450 {
451 event_cleanup(-666);
452 }
453
454 int add_edge_detect(unsigned int gpio, unsigned int edge, int bouncetime)
455 // return values:
456 // 0 - Success
457 // 1 - Edge detection already added
458 // 2 - Other error
459 {
460 pthread_t threads;
461 struct epoll_event ev;
462 long t = 0;
463 struct gpios *g;
464 int i = -1;
465
466 i = gpio_event_added(gpio);
467 if (i == 0) { // event not already added
468 if ((g = new_gpio(gpio)) == NULL) {
469 return 2;
470 }
471
472 gpio_set_edge(gpio, edge);
473 g->edge = edge;
474 g->bouncetime = bouncetime;
475 } else if (i == (int)edge) { // get existing event
476 g = get_gpio(gpio);
477 if ((bouncetime != -666 && g->bouncetime != bouncetime) || // different event bouncetime used
478 (g->thread_added)) // event already added
479 return 1;
480 } else {
481 return 1;
482 }
483
484 // create epfd_thread if not already open
485 if ((epfd_thread == -1) && ((epfd_thread = epoll_create(1)) == -1))
486 return 2;
487
488 // add to epoll fd
489 ev.events = EPOLLIN | EPOLLET | EPOLLPRI;
490 ev.data.fd = g->value_fd;
491 if (epoll_ctl(epfd_thread, EPOLL_CTL_ADD, g->value_fd, &ev) == -1) {
492 remove_edge_detect(gpio);
493 return 2;
494 }
495 g->thread_added = 1;
496
497 // start poll thread if it is not already running
498 if (!thread_running) {
499 if (pthread_create(&threads, NULL, poll_thread, (void *)t) != 0) {
500 remove_edge_detect(gpio);
501 return 2;
502 }
503 }
504 return 0;
505 }
506
507 int blocking_wait_for_edge(unsigned int gpio, unsigned int edge, int bouncetime, int timeout)
508 // return values:
509 // 1 - Success (edge detected)
510 // 0 - Timeout
511 // -1 - Edge detection already added
512 // -2 - Other error
513 {
514 int n, ed;
515 struct epoll_event events, ev;
516 char buf;
517 struct gpios *g = NULL;
518 struct timeval tv_timenow;
519 unsigned long long timenow;
520 int finished = 0;
521 int initial_edge = 1;
522
523 if (callback_exists(gpio))
524 return -1;
525
526 // add gpio if it has not been added already
527 ed = gpio_event_added(gpio);
528 if (ed == (int)edge) { // get existing record
529 g = get_gpio(gpio);
530 if (g->bouncetime != -666 && g->bouncetime != bouncetime) {
531 return -1;
532 }
533 } else if (ed == NO_EDGE) { // not found so add event
534 if ((g = new_gpio(gpio)) == NULL) {
535 return -2;
536 }
537 gpio_set_edge(gpio, edge);
538 g->edge = edge;
539 g->bouncetime = bouncetime;
540 } else { // ed != edge - event for a different edge
541 g = get_gpio(gpio);
542 gpio_set_edge(gpio, edge);
543 g->edge = edge;
544 g->bouncetime = bouncetime;
545 g->initial_wait = 1;
546 }
547
548 // create epfd_blocking if not already open
549 if ((epfd_blocking == -1) && ((epfd_blocking = epoll_create(1)) == -1)) {
550 return -2;
551 }
552
553 // add to epoll fd
554 ev.events = EPOLLIN | EPOLLET | EPOLLPRI;
555 ev.data.fd = g->value_fd;
556 if (epoll_ctl(epfd_blocking, EPOLL_CTL_ADD, g->value_fd, &ev) == -1) {
557 return -2;
558 }
559
560 // wait for edge
561 while (!finished) {
562 n = epoll_wait(epfd_blocking, &events, 1, timeout);
563 if (n == -1) {
564 /* If a signal is received while we are waiting,
565 epoll_wait will return with an EINTR error.
566 Just try again in that case. */
567 if (errno == EINTR) {
568 continue;
569 }
570 epoll_ctl(epfd_blocking, EPOLL_CTL_DEL, g->value_fd, &ev);
571 return -2;
572 }
573 if (initial_edge) { // first time triggers with current state, so ignore
574 initial_edge = 0;
575 } else {
576 gettimeofday(&tv_timenow, NULL);
577 timenow = tv_timenow.tv_sec*1E6 + tv_timenow.tv_usec;
578 if (g->bouncetime == -666 || timenow - g->lastcall > (unsigned int)g->bouncetime*1000 || g->lastcall == 0 || g->lastcall > timenow) {
579 g->lastcall = timenow;
580 finished = 1;
581 }
582 }
583 }
584
585 // check event was valid
586 if (n > 0) {
587 lseek(events.data.fd, 0, SEEK_SET);
588 if ((read(events.data.fd, &buf, 1) != 1) || (events.data.fd != g->value_fd)) {
589 epoll_ctl(epfd_blocking, EPOLL_CTL_DEL, g->value_fd, &ev);
590 return -2;
591 }
592 }
593
594 epoll_ctl(epfd_blocking, EPOLL_CTL_DEL, g->value_fd, &ev);
595 if (n == 0) {
596 return 0; // timeout
597 } else {
598 return 1; // edge found
599 }
600 }