/
gpio.c
1720 lines (1429 loc) · 50.3 KB
/
gpio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014-2018 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "gpio.h"
#include "gpio/gpio_chardev.h"
#include "linux/gpio.h"
#include "mraa_internal.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#define SYSFS_CLASS_GPIO "/sys/class/gpio"
#define MAX_SIZE 64
#define POLL_TIMEOUT
static mraa_result_t
_mraa_gpio_get_valfp(mraa_gpio_context dev)
{
char bu[MAX_SIZE];
snprintf(bu, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
dev->value_fp = open(bu, O_RDWR);
if (dev->value_fp == -1) {
syslog(LOG_ERR, "gpio%i: Failed to open 'value': %s", dev->pin, strerror(errno));
return MRAA_ERROR_INVALID_RESOURCE;
}
return MRAA_SUCCESS;
}
void
mraa_gpio_close_event_handles_sysfs(int fds[], int num_fds)
{
if ((fds == NULL) || (num_fds <= 0)) {
syslog(LOG_CRIT, "failed to close and free sysfs event handles");
return;
}
for (int i = 0; i < num_fds; ++i) {
// Check required to avoid closing stdin and of an uninitialized fd
if (fds[i] != 0) {
close(fds[i]);
}
}
free(fds);
}
static mraa_gpio_context
mraa_gpio_init_internal(mraa_adv_func_t* func_table, int pin)
{
if (pin < 0) {
return NULL;
}
mraa_result_t status = MRAA_SUCCESS;
mraa_gpio_context dev = (mraa_gpio_context) calloc(1, sizeof(struct _gpio));
if (dev == NULL) {
syslog(LOG_CRIT, "gpio%i: Failed to allocate memory for context", pin);
return NULL;
}
dev->advance_func = func_table;
dev->pin = pin;
if (IS_FUNC_DEFINED(dev, gpio_init_internal_replace)) {
status = dev->advance_func->gpio_init_internal_replace(dev, pin);
if (status == MRAA_SUCCESS)
return dev;
else
goto init_internal_cleanup;
}
if (IS_FUNC_DEFINED(dev, gpio_init_pre)) {
status = dev->advance_func->gpio_init_pre(pin);
if (status != MRAA_SUCCESS)
goto init_internal_cleanup;
}
dev->value_fp = -1;
dev->isr_value_fp = -1;
#ifndef HAVE_PTHREAD_CANCEL
dev->isr_control_pipe[0] = dev->isr_control_pipe[1] = -1;
#endif
dev->isr_thread_terminating = 0;
dev->phy_pin = -1;
if ((plat != NULL) && (!plat->chardev_capable)) {
char bu[MAX_SIZE];
int length;
// then check to make sure the pin is exported.
char directory[MAX_SIZE];
snprintf(directory, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/", dev->pin);
struct stat dir;
if (stat(directory, &dir) == 0 && S_ISDIR(dir.st_mode)) {
dev->owner = 0; // Not Owner
} else {
int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
if (export == -1) {
syslog(LOG_ERR, "gpio%i: init: Failed to open 'export' for writing: %s", pin, strerror(errno));
status = MRAA_ERROR_INVALID_RESOURCE;
goto init_internal_cleanup;
}
length = snprintf(bu, sizeof(bu), "%d", dev->pin);
if (write(export, bu, length * sizeof(char)) == -1) {
syslog(LOG_ERR, "gpio%i: init: Failed to write to 'export': %s", pin, strerror(errno));
close(export);
status = MRAA_ERROR_INVALID_RESOURCE;
goto init_internal_cleanup;
}
dev->owner = 1;
close(export);
}
}
/* We only have one pin. No need for multiple pin legacy support. */
dev->num_pins = 1;
dev->next = NULL;
/* Initialize events array. */
dev->events = NULL;
init_internal_cleanup:
if (status != MRAA_SUCCESS) {
if (dev != NULL)
free(dev);
return NULL;
}
return dev;
}
mraa_gpio_context
mraa_gpio_init(int pin)
{
mraa_board_t* board = plat;
if (board == NULL) {
syslog(LOG_ERR, "gpio%i: init: platform not initialised", pin);
return NULL;
}
/* If this pin belongs to a subplatform, then the pin member will contain
* the mraa pin index and the phy_pin will contain the subplatform's
* pin index.
* example: pin 515, dev->pin = 515, dev->phy_pin = 3
*/
if (mraa_is_sub_platform_id(pin) && (board->sub_platform != NULL)) {
syslog(LOG_NOTICE, "gpio%i: initialised on sub platform '%s' physical pin: %i", pin,
board->sub_platform->platform_name != NULL ? board->sub_platform->platform_name : "",
mraa_get_sub_platform_index(pin));
board = board->sub_platform;
if (board == NULL) {
syslog(LOG_ERR, "gpio%i: init: Sub platform not initialised", pin);
return NULL;
}
pin = mraa_get_sub_platform_index(pin);
}
if (board->chardev_capable) {
int pins[1] = { pin };
return mraa_gpio_init_multi(pins, 1);
}
if (pin < 0 || pin >= board->phy_pin_count) {
syslog(LOG_ERR, "gpio: init: pin %i beyond platform pin count (%i)", pin, board->phy_pin_count);
return NULL;
}
if (board->pins[pin].capabilities.gpio != 1) {
syslog(LOG_ERR, "gpio: init: pin %i not capable of gpio", pin);
return NULL;
}
if (board->pins[pin].gpio.mux_total > 0) {
if (mraa_setup_mux_mapped(board->pins[pin].gpio) != MRAA_SUCCESS) {
syslog(LOG_ERR, "gpio%i: init: unable to setup muxes", pin);
return NULL;
}
}
mraa_gpio_context r = mraa_gpio_init_internal(board->adv_func, board->pins[pin].gpio.pinmap);
if (r == NULL) {
return NULL;
}
if (r->phy_pin == -1)
r->phy_pin = pin;
if (IS_FUNC_DEFINED(r, gpio_init_post)) {
mraa_result_t ret = r->advance_func->gpio_init_post(r);
if (ret != MRAA_SUCCESS) {
free(r);
return NULL;
}
}
return r;
}
mraa_gpio_context
mraa_gpio_chardev_init(int pins[], int num_pins)
{
int chip_id, line_offset;
mraa_gpio_context dev;
mraa_gpiod_group_t gpio_group;
mraa_board_t* board = plat;
dev = (mraa_gpio_context) calloc(1, sizeof(struct _gpio));
if (dev == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for context");
return NULL;
}
dev->pin_to_gpio_table = malloc(num_pins * sizeof(int));
if (dev->pin_to_gpio_table == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
mraa_gpio_close(dev);
return NULL;
}
dev->num_chips = mraa_get_number_of_gpio_chips();
if (dev->num_chips <= 0) {
mraa_gpio_close(dev);
return NULL;
}
dev->num_pins = num_pins;
gpio_group = calloc(dev->num_chips, sizeof(struct _gpio_group));
if (gpio_group == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
mraa_gpio_close(dev);
return NULL;
}
dev->gpio_group = gpio_group;
for (int i = 0; i < dev->num_chips; ++i) {
gpio_group[i].gpio_chip = i;
/* Just to be sure realloc has the desired behaviour. */
gpio_group[i].gpio_lines = NULL;
}
for (int i = 0; i < num_pins; ++i) {
if (mraa_is_sub_platform_id(pins[i])) {
syslog(LOG_NOTICE, "[GPIOD_INTERFACE]: init: Using sub platform for %d", pins[i]);
board = board->sub_platform;
if (board == NULL) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: init: Sub platform not initialised for pin %d", pins[i]);
mraa_gpio_close(dev);
return NULL;
}
pins[i] = mraa_get_sub_platform_index(pins[i]);
}
if (pins[i] < 0 || pins[i] >= board->phy_pin_count) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: init: pin %d beyond platform pin count (%d)",
pins[i], board->phy_pin_count);
mraa_gpio_close(dev);
return NULL;
}
if (board->pins[pins[i]].capabilities.gpio != 1) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: init: pin %d not capable of gpio", pins[i]);
mraa_gpio_close(dev);
return NULL;
}
if (board->pins[pins[i]].gpio.mux_total > 0) {
if (mraa_setup_mux_mapped(board->pins[pins[i]].gpio) != MRAA_SUCCESS) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: init: unable to setup muxes for pin %d", pins[i]);
mraa_gpio_close(dev);
return NULL;
}
}
chip_id = board->pins[pins[i]].gpio.gpio_chip;
line_offset = board->pins[pins[i]].gpio.gpio_line;
/* Map pin to _gpio_group structure. */
dev->pin_to_gpio_table[i] = chip_id;
if (!gpio_group[chip_id].is_required) {
mraa_gpiod_chip_info* cinfo = mraa_get_chip_info_by_number(chip_id);
if (!cinfo) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting gpio_chip_info for chip %d", chip_id);
mraa_gpio_close(dev);
return NULL;
}
gpio_group[chip_id].dev_fd = cinfo->chip_fd;
gpio_group[chip_id].is_required = 1;
gpio_group[chip_id].gpiod_handle = -1;
free(cinfo);
}
int line_in_group;
line_in_group = gpio_group[chip_id].num_gpio_lines;
gpio_group[chip_id].gpio_lines =
realloc(gpio_group[chip_id].gpio_lines, (line_in_group + 1) * sizeof(unsigned int));
if (gpio_group[chip_id].gpio_lines == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
mraa_gpio_close(dev);
return NULL;
}
gpio_group[chip_id].gpio_lines[line_in_group] = line_offset;
gpio_group[chip_id].num_gpio_lines++;
}
/* Initialize rw_values for read / write multiple functions.
* Also, allocate memory for inverse map: */
for (int i = 0; i < dev->num_chips; ++i) {
gpio_group[i].rw_values = calloc(gpio_group[i].num_gpio_lines, sizeof(unsigned char));
if (gpio_group[i].rw_values == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
mraa_gpio_close(dev);
return NULL;
}
gpio_group[i].gpio_group_to_pins_table = calloc(gpio_group[i].num_gpio_lines, sizeof(int));
if (gpio_group[i].gpio_group_to_pins_table == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
mraa_gpio_close(dev);
return NULL;
}
/* Set event handle arrays for all lines contained on a chip to NULL. */
gpio_group[i].event_handles = NULL;
}
/* Finally map the inverse relation between a gpio group and its original pin numbers
* provided by user. */
int* counters = calloc(dev->num_chips, sizeof(int));
if (counters == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for local variable");
mraa_gpio_close(dev);
return NULL;
}
for (int i = 0; i < num_pins; ++i) {
int chip = dev->pin_to_gpio_table[i];
gpio_group[chip].gpio_group_to_pins_table[counters[chip]] = i;
counters[chip]++;
}
free(counters);
/* Save the provided array from the user to our internal structure. */
dev->provided_pins = malloc(dev->num_pins * sizeof(int));
if (dev->provided_pins == NULL) {
syslog(LOG_CRIT, "[GPIOD_INTERFACE]: Failed to allocate memory for internal member");
mraa_gpio_close(dev);
return NULL;
}
memcpy(dev->provided_pins, pins, dev->num_pins * sizeof(int));
/* Initialize events array. */
dev->events = NULL;
return dev;
}
mraa_gpio_context
mraa_gpio_init_multi(int pins[], int num_pins)
{
mraa_board_t* board = plat;
if (board == NULL) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: init: platform not initialised");
return NULL;
}
if (board->chardev_capable)
return mraa_gpio_chardev_init(pins, num_pins);
/* Fallback to legacy interface. */
mraa_gpio_context head = NULL, current, tmp;
for (int i = 0; i < num_pins; ++i) {
tmp = mraa_gpio_init(pins[i]);
if (!tmp) {
syslog(LOG_ERR, "mraa_gpio_init_multi() error initializing pin %i", pins[i]);
continue;
}
if (head == NULL) {
head = tmp;
current = tmp;
} else {
current->next = tmp;
current = tmp;
}
current->next = NULL;
}
if (head != NULL) {
head->num_pins = num_pins;
}
return head;
}
mraa_gpio_context
mraa_gpio_init_raw(int pin)
{
return mraa_gpio_init_internal(plat == NULL ? NULL : plat->adv_func, pin);
}
mraa_timestamp_t
_mraa_gpio_get_timestamp_sysfs()
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1e6 + time.tv_usec);
}
static mraa_result_t
mraa_gpio_wait_interrupt(int fds[],
int num_fds
#ifndef HAVE_PTHREAD_CANCEL
,
int control_fd
#endif
,
mraa_gpio_events_t events)
{
unsigned char c;
#ifdef HAVE_PTHREAD_CANCEL
struct pollfd pfd[num_fds];
#else
struct pollfd pfd[num_fds + 1];
if (control_fd < 0) {
return MRAA_ERROR_INVALID_PARAMETER;
}
#endif
if (!fds) {
return MRAA_ERROR_INVALID_PARAMETER;
}
for (int i = 0; i < num_fds; ++i) {
pfd[i].fd = fds[i];
// setup poll on POLLPRI
pfd[i].events = POLLPRI;
// do an initial read to clear interrupt
lseek(fds[i], 0, SEEK_SET);
read(fds[i], &c, 1);
}
#ifdef HAVE_PTHREAD_CANCEL
// Wait for it forever or until pthread_cancel
// poll is a cancelable point like sleep()
poll(pfd, num_fds, -1);
#else
// setup poll on the controling fd
pfd[num_fds].fd = control_fd;
pfd[num_fds].events = 0; // POLLHUP, POLLERR, and POLLNVAL
// Wait for it forever or until control fd is closed
poll(pfd, num_fds + 1, -1);
#endif
for (int i = 0; i < num_fds; ++i) {
if (pfd[i].revents & POLLPRI) {
read(fds[i], &c, 1);
events[i].id = i;
events[i].timestamp = _mraa_gpio_get_timestamp_sysfs();
} else
events[i].id = -1;
}
return MRAA_SUCCESS;
}
static mraa_result_t
mraa_gpio_chardev_wait_interrupt(int fds[], int num_fds, mraa_gpio_events_t events)
{
struct pollfd pfd[num_fds];
struct gpioevent_data event_data;
if (!fds) {
return MRAA_ERROR_INVALID_PARAMETER;
}
for (int i = 0; i < num_fds; ++i) {
pfd[i].fd = fds[i];
pfd[i].events = POLLIN;
lseek(fds[i], 0, SEEK_SET);
}
poll(pfd, num_fds, -1);
for (int i = 0; i < num_fds; ++i) {
if (pfd[i].revents & POLLIN) {
read(fds[i], &event_data, sizeof(event_data));
events[i].id = i;
events[i].timestamp = event_data.timestamp;
} else
events[i].id = -1;
}
return MRAA_SUCCESS;
}
mraa_gpio_events_t
mraa_gpio_get_events(mraa_gpio_context dev)
{
if (dev == NULL) {
syslog(LOG_ERR, "gpio: mraa_gpio_get_events(): context is invalid");
return NULL;
}
unsigned int event_idx = 0;
if (plat->chardev_capable) {
unsigned int pin_idx;
mraa_gpiod_group_t gpio_iter;
for_each_gpio_group(gpio_iter, dev)
{
for (int i = 0; i < gpio_iter->num_gpio_lines; ++i) {
if (dev->events[event_idx].id != -1) {
pin_idx = gpio_iter->gpio_group_to_pins_table[i];
dev->events[event_idx].id = dev->provided_pins[pin_idx];
}
event_idx++;
}
}
} else {
mraa_gpio_context it = dev;
while (it) {
if (dev->events[event_idx].id != -1) {
dev->events[event_idx].id = it->phy_pin;
}
event_idx++;
it = it->next;
}
}
return dev->events;
}
static void*
mraa_gpio_interrupt_handler(void* arg)
{
if (arg == NULL) {
syslog(LOG_ERR, "gpio: interrupt_handler: context is invalid");
return NULL;
}
mraa_result_t ret;
mraa_gpio_context dev = (mraa_gpio_context) arg;
int idx = 0;
if (IS_FUNC_DEFINED(dev, gpio_interrupt_handler_init_replace)) {
if (dev->advance_func->gpio_interrupt_handler_init_replace(dev) != MRAA_SUCCESS)
return NULL;
}
int* fps = calloc(dev->num_pins, sizeof(int));
if (!fps) {
syslog(LOG_ERR, "mraa_gpio_interrupt_handler_multiple() malloc error");
return NULL;
}
/* Is this pin on a subplatform? Do nothing... */
if (mraa_is_sub_platform_id(dev->pin)) {
}
/* Is the platform chardev_capable? */
else if (plat->chardev_capable) {
mraa_gpiod_group_t gpio_group;
for_each_gpio_group(gpio_group, dev)
{
for (int i = 0; i < gpio_group->num_gpio_lines; ++i) {
fps[idx++] = gpio_group->event_handles[i];
}
}
}
/* Else, attempt fs access */
else {
mraa_gpio_context it = dev;
while (it) {
// open gpio value with open(3)
char bu[MAX_SIZE];
snprintf(bu, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/value", it->pin);
fps[idx] = open(bu, O_RDONLY);
if (fps[idx] < 0) {
syslog(LOG_ERR, "gpio%i: interrupt_handler: failed to open 'value' : %s", it->pin,
strerror(errno));
mraa_gpio_close_event_handles_sysfs(fps, idx);
return NULL;
}
idx++;
it = it->next;
}
}
#ifndef HAVE_PTHREAD_CANCEL
if (pipe(dev->isr_control_pipe)) {
syslog(LOG_ERR, "gpio%i: interrupt_handler: failed to create isr control pipe: %s",
dev->pin, strerror(errno));
mraa_gpio_close_event_handles_sysfs(fps, dev->num_pins);
return NULL;
}
#endif
if (lang_func->java_attach_thread != NULL) {
if (dev->isr == lang_func->java_isr_callback) {
if (lang_func->java_attach_thread() != MRAA_SUCCESS) {
mraa_gpio_close_event_handles_sysfs(fps, dev->num_pins);
return NULL;
}
}
}
for (;;) {
if (IS_FUNC_DEFINED(dev, gpio_wait_interrupt_replace)) {
ret = dev->advance_func->gpio_wait_interrupt_replace(dev);
} else {
if (plat->chardev_capable) {
ret = mraa_gpio_chardev_wait_interrupt(fps, idx, dev->events);
} else {
ret = mraa_gpio_wait_interrupt(fps, idx
#ifndef HAVE_PTHREAD_CANCEL
,
dev->isr_control_pipe[0]
#endif
,
dev->events);
}
}
if (ret == MRAA_SUCCESS && !dev->isr_thread_terminating) {
#ifdef HAVE_PTHREAD_CANCEL
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
#endif
if (lang_func->python_isr != NULL) {
lang_func->python_isr(dev->isr, dev->isr_args);
} else {
dev->isr(dev->isr_args);
}
#ifdef HAVE_PTHREAD_CANCEL
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
#endif
} else {
// we must have got an error code or exit request so die nicely
#ifdef HAVE_PTHREAD_CANCEL
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
#endif
mraa_gpio_close_event_handles_sysfs(fps, dev->num_pins);
if (lang_func->java_detach_thread != NULL && lang_func->java_delete_global_ref != NULL) {
if (dev->isr == lang_func->java_isr_callback) {
lang_func->java_delete_global_ref(dev->isr_args);
lang_func->java_detach_thread();
}
}
return NULL;
}
}
}
mraa_result_t
mraa_gpio_chardev_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
{
if (dev == NULL) {
syslog(LOG_ERR, "gpio: edge_mode: context is invalid");
return MRAA_ERROR_INVALID_HANDLE;
}
if (!plat->chardev_capable) {
syslog(LOG_ERR, "mraa_gpio_chardev_edge_mode() not supported for old sysfs interface");
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
}
int status;
mraa_gpiod_group_t gpio_group;
struct gpioevent_request req;
switch (mode) {
case MRAA_GPIO_EDGE_BOTH:
req.eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
break;
case MRAA_GPIO_EDGE_RISING:
req.eventflags = GPIOEVENT_REQUEST_RISING_EDGE;
break;
case MRAA_GPIO_EDGE_FALLING:
req.eventflags = GPIOEVENT_REQUEST_FALLING_EDGE;
break;
/* Chardev interface doesn't handle EDGE_NONE. */
case MRAA_GPIO_EDGE_NONE:
default:
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
}
for_each_gpio_group(gpio_group, dev)
{
if (gpio_group->gpiod_handle != -1) {
close(gpio_group->gpiod_handle);
gpio_group->gpiod_handle = -1;
}
gpio_group->event_handles = malloc(gpio_group->num_gpio_lines * sizeof(int));
if (!gpio_group->event_handles) {
syslog(LOG_ERR, "mraa_gpio_chardev_edge_mode(): malloc error!");
return MRAA_ERROR_NO_RESOURCES;
}
for (int i = 0; i < gpio_group->num_gpio_lines; ++i) {
req.lineoffset = gpio_group->gpio_lines[i];
req.handleflags = GPIOHANDLE_REQUEST_INPUT;
status = _mraa_gpiod_ioctl(gpio_group->dev_fd, GPIO_GET_LINEEVENT_IOCTL, &req);
if (status < 0) {
syslog(LOG_ERR, "error getting line event handle for line %i", gpio_group->gpio_lines[i]);
return MRAA_ERROR_INVALID_RESOURCE;
}
gpio_group->event_handles[i] = req.fd;
}
}
return MRAA_SUCCESS;
}
mraa_result_t
mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
{
if (dev == NULL) {
syslog(LOG_ERR, "gpio: edge_mode: context is invalid");
return MRAA_ERROR_INVALID_HANDLE;
}
if (IS_FUNC_DEFINED(dev, gpio_edge_mode_replace))
return dev->advance_func->gpio_edge_mode_replace(dev, mode);
/* Initialize events array. */
if (dev->events == NULL && mode != MRAA_GPIO_EDGE_NONE) {
dev->events = malloc(dev->num_pins * sizeof(mraa_gpio_event));
if (dev->events == NULL) {
syslog(LOG_ERR, "mraa_gpio_edge_mode() malloc error");
return MRAA_ERROR_NO_RESOURCES;
}
for (int i = 0; i < dev->num_pins; ++i) {
dev->events[i].id = -1;
}
}
if (plat->chardev_capable)
return mraa_gpio_chardev_edge_mode(dev, mode);
mraa_gpio_context it = dev;
while (it) {
if (it->value_fp != -1) {
close(it->value_fp);
it->value_fp = -1;
}
char filepath[MAX_SIZE];
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", it->pin);
int edge = open(filepath, O_RDWR);
if (edge == -1) {
syslog(LOG_ERR, "gpio%i: edge_mode: Failed to open 'edge' for writing: %s", it->pin,
strerror(errno));
return MRAA_ERROR_INVALID_RESOURCE;
}
char bu[MAX_SIZE];
int length;
switch (mode) {
case MRAA_GPIO_EDGE_NONE:
length = snprintf(bu, sizeof(bu), "none");
break;
case MRAA_GPIO_EDGE_BOTH:
length = snprintf(bu, sizeof(bu), "both");
break;
case MRAA_GPIO_EDGE_RISING:
length = snprintf(bu, sizeof(bu), "rising");
break;
case MRAA_GPIO_EDGE_FALLING:
length = snprintf(bu, sizeof(bu), "falling");
break;
default:
close(edge);
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
}
if (write(edge, bu, length * sizeof(char)) == -1) {
syslog(LOG_ERR, "gpio%i: edge_mode: Failed to write to 'edge': %s", it->pin, strerror(errno));
close(edge);
return MRAA_ERROR_UNSPECIFIED;
}
close(edge);
it = it->next;
}
return MRAA_SUCCESS;
}
mraa_result_t
mraa_gpio_isr(mraa_gpio_context dev, mraa_gpio_edge_t mode, void (*fptr)(void*), void* args)
{
if (dev == NULL) {
syslog(LOG_ERR, "gpio: isr: context is invalid");
return MRAA_ERROR_INVALID_HANDLE;
}
if (IS_FUNC_DEFINED(dev, gpio_isr_replace)) {
return dev->advance_func->gpio_isr_replace(dev, mode, fptr, args);
}
// we only allow one isr per mraa_gpio_context
if (dev->thread_id != 0) {
return MRAA_ERROR_NO_RESOURCES;
}
mraa_result_t ret;
ret = mraa_gpio_edge_mode(dev, mode);
if (ret != MRAA_SUCCESS) {
return ret;
}
dev->isr = fptr;
/* Most UPM sensors use the C API, the Java global ref must be created here. */
/* The reason for checking the callback function is internal callbacks. */
if (lang_func->java_create_global_ref != NULL) {
if (dev->isr == lang_func->java_isr_callback) {
args = lang_func->java_create_global_ref(args);
}
}
dev->isr_args = args;
pthread_create(&dev->thread_id, NULL, mraa_gpio_interrupt_handler, (void*) dev);
return MRAA_SUCCESS;
}
mraa_result_t
mraa_gpio_isr_exit(mraa_gpio_context dev)
{
mraa_result_t ret = MRAA_SUCCESS;
if (dev == NULL) {
return MRAA_ERROR_INVALID_HANDLE;
}
if (IS_FUNC_DEFINED(dev, gpio_isr_exit_replace)) {
return dev->advance_func->gpio_isr_exit_replace(dev);
}
// wasting our time, there is no isr to exit from
if (dev->thread_id == 0) {
return ret;
}
// mark the beginning of the thread termination process for interested parties
dev->isr_thread_terminating = 1;
// stop isr being useful
if (plat && (plat->chardev_capable))
_mraa_close_gpio_event_handles(dev);
else
ret = mraa_gpio_edge_mode(dev, MRAA_GPIO_EDGE_NONE);
if ((dev->thread_id != 0)) {
#ifdef HAVE_PTHREAD_CANCEL
if ((pthread_cancel(dev->thread_id) != 0) || (pthread_join(dev->thread_id, NULL) != 0)) {
ret = MRAA_ERROR_INVALID_RESOURCE;
}
#else
close(dev->isr_control_pipe[1]);
if (pthread_join(dev->thread_id, NULL) != 0)
ret = MRAA_ERROR_INVALID_RESOURCE;
close(dev->isr_control_pipe[0]);
dev->isr_control_pipe[0] = dev->isr_control_pipe[1] = -1;
#endif
}
// assume our thread will exit either way we just lost it's handle
dev->thread_id = 0;
dev->isr_value_fp = -1;
dev->isr_thread_terminating = 0;
if (dev->events) {
free(dev->events);
dev->events = NULL;
}
return ret;
}
mraa_result_t
mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
{
if (dev == NULL) {
syslog(LOG_ERR, "gpio: mode: context is invalid");
return MRAA_ERROR_INVALID_HANDLE;
}
if (IS_FUNC_DEFINED(dev, gpio_mode_replace))
return dev->advance_func->gpio_mode_replace(dev, mode);
if (IS_FUNC_DEFINED(dev, gpio_mode_pre)) {
mraa_result_t pre_ret = (dev->advance_func->gpio_mode_pre(dev, mode));
if (pre_ret != MRAA_SUCCESS)
return pre_ret;
}
if (plat->chardev_capable) {
unsigned flags = 0;
int line_handle;
mraa_gpiod_group_t gpio_iter;
_mraa_close_gpio_desc(dev);
/* We save flag values from the first valid line. */
for_each_gpio_group(gpio_iter, dev)
{
mraa_gpiod_line_info* linfo =
mraa_get_line_info_by_chip_number(gpio_iter->gpio_chip, gpio_iter->gpio_lines[0]);
if (!linfo) {
syslog(LOG_ERR, "[GPIOD_INTERFACE]: error getting line info");
return MRAA_ERROR_UNSPECIFIED;
}
flags = linfo->flags;
free(linfo);
/* We don't need to iterate any further. */
break;
}
/* Without changing the API, for now, we can request only one mode per call. */
switch (mode) {
case MRAA_GPIOD_ACTIVE_LOW:
flags |= GPIOHANDLE_REQUEST_ACTIVE_LOW;
break;
case MRAA_GPIOD_OPEN_DRAIN:
flags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
break;
case MRAA_GPIOD_OPEN_SOURCE:
flags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;