2 * gpio-event-mon - monitor GPIO line events from userspace
4 * Copyright (C) 2016 Linus Walleij
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.
11 * gpio-event-mon -n <device-name> -o <offset>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <linux/gpio.h>
29 int monitor_device(const char *device_name
,
31 u_int32_t handleflags
,
35 struct gpioevent_request req
;
36 struct gpiohandle_data data
;
42 ret
= asprintf(&chrdev_name
, "/dev/%s", device_name
);
46 fd
= open(chrdev_name
, 0);
49 fprintf(stderr
, "Failed to open %s\n", chrdev_name
);
50 goto exit_close_error
;
53 req
.lineoffset
= line
;
54 req
.handleflags
= handleflags
;
55 req
.eventflags
= eventflags
;
56 strcpy(req
.consumer_label
, "gpio-event-mon");
58 ret
= ioctl(fd
, GPIO_GET_LINEEVENT_IOCTL
, &req
);
61 fprintf(stderr
, "Failed to issue GET EVENT "
64 goto exit_close_error
;
67 /* Read initial states */
68 ret
= ioctl(req
.fd
, GPIOHANDLE_GET_LINE_VALUES_IOCTL
, &data
);
71 fprintf(stderr
, "Failed to issue GPIOHANDLE GET LINE "
72 "VALUES IOCTL (%d)\n",
74 goto exit_close_error
;
77 fprintf(stdout
, "Monitoring line %d on %s\n", line
, device_name
);
78 fprintf(stdout
, "Initial line value: %d\n", data
.values
[0]);
81 struct gpioevent_data event
;
83 ret
= read(req
.fd
, &event
, sizeof(event
));
85 if (errno
== -EAGAIN
) {
86 fprintf(stderr
, "nothing available\n");
90 fprintf(stderr
, "Failed to read event (%d)\n",
96 if (ret
!= sizeof(event
)) {
97 fprintf(stderr
, "Reading event failed\n");
101 fprintf(stdout
, "GPIO EVENT %" PRIu64
": ", event
.timestamp
);
103 case GPIOEVENT_EVENT_RISING_EDGE
:
104 fprintf(stdout
, "rising edge");
106 case GPIOEVENT_EVENT_FALLING_EDGE
:
107 fprintf(stdout
, "falling edge");
110 fprintf(stdout
, "unknown event");
112 fprintf(stdout
, "\n");
121 perror("Failed to close GPIO character device file");
126 void print_usage(void)
128 fprintf(stderr
, "Usage: gpio-event-mon [options]...\n"
129 "Listen to events on GPIO lines, 0->1 1->0\n"
130 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
131 " -o <n> Offset to monitor\n"
132 " -d Set line as open drain\n"
133 " -s Set line as open source\n"
134 " -r Listen for rising edges\n"
135 " -f Listen for falling edges\n"
136 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
137 " -? This helptext\n"
140 "gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
144 int main(int argc
, char **argv
)
146 const char *device_name
= NULL
;
147 unsigned int line
= -1;
148 unsigned int loops
= 0;
149 u_int32_t handleflags
= GPIOHANDLE_REQUEST_INPUT
;
150 u_int32_t eventflags
= 0;
153 while ((c
= getopt(argc
, argv
, "c:n:o:dsrf?")) != -1) {
156 loops
= strtoul(optarg
, NULL
, 10);
159 device_name
= optarg
;
162 line
= strtoul(optarg
, NULL
, 10);
165 handleflags
|= GPIOHANDLE_REQUEST_OPEN_DRAIN
;
168 handleflags
|= GPIOHANDLE_REQUEST_OPEN_SOURCE
;
171 eventflags
|= GPIOEVENT_REQUEST_RISING_EDGE
;
174 eventflags
|= GPIOEVENT_REQUEST_FALLING_EDGE
;
182 if (!device_name
|| line
== -1) {
187 printf("No flags specified, listening on both rising and "
189 eventflags
= GPIOEVENT_REQUEST_BOTH_EDGES
;
191 return monitor_device(device_name
, line
, handleflags
,