gpio-event-mon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * gpio-event-mon - monitor GPIO line events from userspace
  3. *
  4. * Copyright (C) 2016 Linus Walleij
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * Usage:
  11. * gpio-event-mon -n <device-name> -o <offset>
  12. */
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <dirent.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <poll.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <inttypes.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/types.h>
  27. #include <linux/gpio.h>
  28. int monitor_device(const char *device_name,
  29. unsigned int line,
  30. uint32_t handleflags,
  31. uint32_t eventflags,
  32. unsigned int loops)
  33. {
  34. struct gpioevent_request req;
  35. struct gpiohandle_data data;
  36. char *chrdev_name;
  37. int fd;
  38. int ret;
  39. int i = 0;
  40. ret = asprintf(&chrdev_name, "/dev/%s", device_name);
  41. if (ret < 0)
  42. return -ENOMEM;
  43. fd = open(chrdev_name, 0);
  44. if (fd == -1) {
  45. ret = -errno;
  46. fprintf(stderr, "Failed to open %s\n", chrdev_name);
  47. goto exit_close_error;
  48. }
  49. req.lineoffset = line;
  50. req.handleflags = handleflags;
  51. req.eventflags = eventflags;
  52. strcpy(req.consumer_label, "gpio-event-mon");
  53. ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
  54. if (ret == -1) {
  55. ret = -errno;
  56. fprintf(stderr, "Failed to issue GET EVENT "
  57. "IOCTL (%d)\n",
  58. ret);
  59. goto exit_close_error;
  60. }
  61. /* Read initial states */
  62. ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
  63. if (ret == -1) {
  64. ret = -errno;
  65. fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
  66. "VALUES IOCTL (%d)\n",
  67. ret);
  68. goto exit_close_error;
  69. }
  70. fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
  71. fprintf(stdout, "Initial line value: %d\n", data.values[0]);
  72. while (1) {
  73. struct gpioevent_data event;
  74. ret = read(req.fd, &event, sizeof(event));
  75. if (ret == -1) {
  76. if (errno == -EAGAIN) {
  77. fprintf(stderr, "nothing available\n");
  78. continue;
  79. } else {
  80. ret = -errno;
  81. fprintf(stderr, "Failed to read event (%d)\n",
  82. ret);
  83. break;
  84. }
  85. }
  86. if (ret != sizeof(event)) {
  87. fprintf(stderr, "Reading event failed\n");
  88. ret = -EIO;
  89. break;
  90. }
  91. fprintf(stdout, "GPIO EVENT %llu: ", event.timestamp);
  92. switch (event.id) {
  93. case GPIOEVENT_EVENT_RISING_EDGE:
  94. fprintf(stdout, "rising edge");
  95. break;
  96. case GPIOEVENT_EVENT_FALLING_EDGE:
  97. fprintf(stdout, "falling edge");
  98. break;
  99. default:
  100. fprintf(stdout, "unknown event");
  101. }
  102. fprintf(stdout, "\n");
  103. i++;
  104. if (i == loops)
  105. break;
  106. }
  107. exit_close_error:
  108. if (close(fd) == -1)
  109. perror("Failed to close GPIO character device file");
  110. free(chrdev_name);
  111. return ret;
  112. }
  113. void print_usage(void)
  114. {
  115. fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
  116. "Listen to events on GPIO lines, 0->1 1->0\n"
  117. " -n <name> Listen on GPIOs on a named device (must be stated)\n"
  118. " -o <n> Offset to monitor\n"
  119. " -d Set line as open drain\n"
  120. " -s Set line as open source\n"
  121. " -r Listen for rising edges\n"
  122. " -f Listen for falling edges\n"
  123. " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
  124. " -? This helptext\n"
  125. "\n"
  126. "Example:\n"
  127. "gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
  128. );
  129. }
  130. int main(int argc, char **argv)
  131. {
  132. const char *device_name = NULL;
  133. unsigned int line = -1;
  134. unsigned int loops = 0;
  135. uint32_t handleflags = GPIOHANDLE_REQUEST_INPUT;
  136. uint32_t eventflags = 0;
  137. int c;
  138. while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) {
  139. switch (c) {
  140. case 'c':
  141. loops = strtoul(optarg, NULL, 10);
  142. break;
  143. case 'n':
  144. device_name = optarg;
  145. break;
  146. case 'o':
  147. line = strtoul(optarg, NULL, 10);
  148. break;
  149. case 'd':
  150. handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
  151. break;
  152. case 's':
  153. handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;
  154. break;
  155. case 'r':
  156. eventflags |= GPIOEVENT_REQUEST_RISING_EDGE;
  157. break;
  158. case 'f':
  159. eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE;
  160. break;
  161. case '?':
  162. print_usage();
  163. return -1;
  164. }
  165. }
  166. if (!device_name || line == -1) {
  167. print_usage();
  168. return -1;
  169. }
  170. if (!eventflags) {
  171. printf("No flags specified, listening on both rising and "
  172. "falling edges\n");
  173. eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
  174. }
  175. return monitor_device(device_name, line, handleflags,
  176. eventflags, loops);
  177. }