Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * gpio-event-mon - monitor GPIO line events from userspace
4 *
5 * Copyright (C) 2016 Linus Walleij
6 *
7 * Usage:
8 * gpio-event-mon -n <device-name> -o <offset>
9 */
10
11#include <unistd.h>
12#include <stdlib.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include <stdio.h>
16#include <dirent.h>
17#include <errno.h>
18#include <string.h>
19#include <poll.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <inttypes.h>
23#include <sys/ioctl.h>
24#include <sys/types.h>
25#include <linux/gpio.h>
26#include "gpio-utils.h"
27
28int monitor_device(const char *device_name,
29 unsigned int *lines,
30 unsigned int num_lines,
31 struct gpio_v2_line_config *config,
32 unsigned int loops)
33{
34 struct gpio_v2_line_values values;
35 char *chrdev_name;
36 int cfd, lfd;
37 int ret;
38 int i = 0;
39
40 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
41 if (ret < 0)
42 return -ENOMEM;
43
44 cfd = open(chrdev_name, 0);
45 if (cfd == -1) {
46 ret = -errno;
47 fprintf(stderr, "Failed to open %s\n", chrdev_name);
48 goto exit_free_name;
49 }
50
51 ret = gpiotools_request_line(device_name, lines, num_lines, config,
52 "gpio-event-mon");
53 if (ret < 0)
54 goto exit_device_close;
55 else
56 lfd = ret;
57
58 /* Read initial states */
59 values.mask = 0;
60 values.bits = 0;
61 for (i = 0; i < num_lines; i++)
62 gpiotools_set_bit(&values.mask, i);
63 ret = gpiotools_get_values(lfd, &values);
64 if (ret < 0) {
65 fprintf(stderr,
66 "Failed to issue GPIO LINE GET VALUES IOCTL (%d)\n",
67 ret);
68 goto exit_line_close;
69 }
70
71 if (num_lines == 1) {
72 fprintf(stdout, "Monitoring line %u on %s\n", lines[0], device_name);
73 fprintf(stdout, "Initial line value: %d\n",
74 gpiotools_test_bit(values.bits, 0));
75 } else {
76 fprintf(stdout, "Monitoring lines %u", lines[0]);
77 for (i = 1; i < num_lines - 1; i++)
78 fprintf(stdout, ", %u", lines[i]);
79 fprintf(stdout, " and %u on %s\n", lines[i], device_name);
80 fprintf(stdout, "Initial line values: %d",
81 gpiotools_test_bit(values.bits, 0));
82 for (i = 1; i < num_lines - 1; i++)
83 fprintf(stdout, ", %d",
84 gpiotools_test_bit(values.bits, i));
85 fprintf(stdout, " and %d\n",
86 gpiotools_test_bit(values.bits, i));
87 }
88
89 i = 0;
90 while (1) {
91 struct gpio_v2_line_event event;
92
93 ret = read(lfd, &event, sizeof(event));
94 if (ret == -1) {
95 if (errno == -EAGAIN) {
96 fprintf(stderr, "nothing available\n");
97 continue;
98 } else {
99 ret = -errno;
100 fprintf(stderr, "Failed to read event (%d)\n",
101 ret);
102 break;
103 }
104 }
105
106 if (ret != sizeof(event)) {
107 fprintf(stderr, "Reading event failed\n");
108 ret = -EIO;
109 break;
110 }
111 fprintf(stdout, "GPIO EVENT at %" PRIu64 " on line %d (%d|%d) ",
112 (uint64_t)event.timestamp_ns, event.offset, event.line_seqno,
113 event.seqno);
114 switch (event.id) {
115 case GPIO_V2_LINE_EVENT_RISING_EDGE:
116 fprintf(stdout, "rising edge");
117 break;
118 case GPIO_V2_LINE_EVENT_FALLING_EDGE:
119 fprintf(stdout, "falling edge");
120 break;
121 default:
122 fprintf(stdout, "unknown event");
123 }
124 fprintf(stdout, "\n");
125
126 i++;
127 if (i == loops)
128 break;
129 }
130
131exit_line_close:
132 if (close(lfd) == -1)
133 perror("Failed to close line file");
134exit_device_close:
135 if (close(cfd) == -1)
136 perror("Failed to close GPIO character device file");
137exit_free_name:
138 free(chrdev_name);
139 return ret;
140}
141
142void print_usage(void)
143{
144 fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
145 "Listen to events on GPIO lines, 0->1 1->0\n"
146 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
147 " -o <n> Offset of line to monitor (may be repeated)\n"
148 " -d Set line as open drain\n"
149 " -s Set line as open source\n"
150 " -r Listen for rising edges\n"
151 " -f Listen for falling edges\n"
152 " -w Report the wall-clock time for events\n"
153 " -t Report the hardware timestamp for events\n"
154 " -b <n> Debounce the line with period n microseconds\n"
155 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
156 " -? This helptext\n"
157 "\n"
158 "Example:\n"
159 "gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000\n"
160 );
161}
162
163#define EDGE_FLAGS \
164 (GPIO_V2_LINE_FLAG_EDGE_RISING | \
165 GPIO_V2_LINE_FLAG_EDGE_FALLING)
166
167int main(int argc, char **argv)
168{
169 const char *device_name = NULL;
170 unsigned int lines[GPIO_V2_LINES_MAX];
171 unsigned int num_lines = 0;
172 unsigned int loops = 0;
173 struct gpio_v2_line_config config;
174 int c, attr, i;
175 unsigned long debounce_period_us = 0;
176
177 memset(&config, 0, sizeof(config));
178 config.flags = GPIO_V2_LINE_FLAG_INPUT;
179 while ((c = getopt(argc, argv, "c:n:o:b:dsrfwt?")) != -1) {
180 switch (c) {
181 case 'c':
182 loops = strtoul(optarg, NULL, 10);
183 break;
184 case 'n':
185 device_name = optarg;
186 break;
187 case 'o':
188 if (num_lines >= GPIO_V2_LINES_MAX) {
189 print_usage();
190 return -1;
191 }
192 lines[num_lines] = strtoul(optarg, NULL, 10);
193 num_lines++;
194 break;
195 case 'b':
196 debounce_period_us = strtoul(optarg, NULL, 10);
197 break;
198 case 'd':
199 config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
200 break;
201 case 's':
202 config.flags |= GPIO_V2_LINE_FLAG_OPEN_SOURCE;
203 break;
204 case 'r':
205 config.flags |= GPIO_V2_LINE_FLAG_EDGE_RISING;
206 break;
207 case 'f':
208 config.flags |= GPIO_V2_LINE_FLAG_EDGE_FALLING;
209 break;
210 case 'w':
211 config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME;
212 break;
213 case 't':
214 config.flags |= GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE;
215 break;
216 case '?':
217 print_usage();
218 return -1;
219 }
220 }
221
222 if (debounce_period_us) {
223 attr = config.num_attrs;
224 config.num_attrs++;
225 for (i = 0; i < num_lines; i++)
226 gpiotools_set_bit(&config.attrs[attr].mask, i);
227 config.attrs[attr].attr.id = GPIO_V2_LINE_ATTR_ID_DEBOUNCE;
228 config.attrs[attr].attr.debounce_period_us = debounce_period_us;
229 }
230
231 if (!device_name || num_lines == 0) {
232 print_usage();
233 return -1;
234 }
235 if (!(config.flags & EDGE_FLAGS)) {
236 printf("No flags specified, listening on both rising and "
237 "falling edges\n");
238 config.flags |= EDGE_FLAGS;
239 }
240 return monitor_device(device_name, lines, num_lines, &config, loops);
241}
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
14#include <unistd.h>
15#include <stdlib.h>
16#include <stdbool.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 <linux/gpio.h>
27
28int monitor_device(const char *device_name,
29 unsigned int line,
30 u_int32_t handleflags,
31 u_int32_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
41 ret = asprintf(&chrdev_name, "/dev/%s", device_name);
42 if (ret < 0)
43 return -ENOMEM;
44
45 fd = open(chrdev_name, 0);
46 if (fd == -1) {
47 ret = -errno;
48 fprintf(stderr, "Failed to open %s\n", chrdev_name);
49 goto exit_close_error;
50 }
51
52 req.lineoffset = line;
53 req.handleflags = handleflags;
54 req.eventflags = eventflags;
55 strcpy(req.consumer_label, "gpio-event-mon");
56
57 ret = ioctl(fd, GPIO_GET_LINEEVENT_IOCTL, &req);
58 if (ret == -1) {
59 ret = -errno;
60 fprintf(stderr, "Failed to issue GET EVENT "
61 "IOCTL (%d)\n",
62 ret);
63 goto exit_close_error;
64 }
65
66 /* Read initial states */
67 ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
68 if (ret == -1) {
69 ret = -errno;
70 fprintf(stderr, "Failed to issue GPIOHANDLE GET LINE "
71 "VALUES IOCTL (%d)\n",
72 ret);
73 goto exit_close_error;
74 }
75
76 fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
77 fprintf(stdout, "Initial line value: %d\n", data.values[0]);
78
79 while (1) {
80 struct gpioevent_data event;
81
82 ret = read(req.fd, &event, sizeof(event));
83 if (ret == -1) {
84 if (errno == -EAGAIN) {
85 fprintf(stderr, "nothing available\n");
86 continue;
87 } else {
88 ret = -errno;
89 fprintf(stderr, "Failed to read event (%d)\n",
90 ret);
91 break;
92 }
93 }
94
95 if (ret != sizeof(event)) {
96 fprintf(stderr, "Reading event failed\n");
97 ret = -EIO;
98 break;
99 }
100 fprintf(stdout, "GPIO EVENT %" PRIu64 ": ", event.timestamp);
101 switch (event.id) {
102 case GPIOEVENT_EVENT_RISING_EDGE:
103 fprintf(stdout, "rising edge");
104 break;
105 case GPIOEVENT_EVENT_FALLING_EDGE:
106 fprintf(stdout, "falling edge");
107 break;
108 default:
109 fprintf(stdout, "unknown event");
110 }
111 fprintf(stdout, "\n");
112
113 i++;
114 if (i == loops)
115 break;
116 }
117
118exit_close_error:
119 if (close(fd) == -1)
120 perror("Failed to close GPIO character device file");
121 free(chrdev_name);
122 return ret;
123}
124
125void print_usage(void)
126{
127 fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
128 "Listen to events on GPIO lines, 0->1 1->0\n"
129 " -n <name> Listen on GPIOs on a named device (must be stated)\n"
130 " -o <n> Offset to monitor\n"
131 " -d Set line as open drain\n"
132 " -s Set line as open source\n"
133 " -r Listen for rising edges\n"
134 " -f Listen for falling edges\n"
135 " [-c <n>] Do <n> loops (optional, infinite loop if not stated)\n"
136 " -? This helptext\n"
137 "\n"
138 "Example:\n"
139 "gpio-event-mon -n gpiochip0 -o 4 -r -f\n"
140 );
141}
142
143int main(int argc, char **argv)
144{
145 const char *device_name = NULL;
146 unsigned int line = -1;
147 unsigned int loops = 0;
148 u_int32_t handleflags = GPIOHANDLE_REQUEST_INPUT;
149 u_int32_t eventflags = 0;
150 int c;
151
152 while ((c = getopt(argc, argv, "c:n:o:dsrf?")) != -1) {
153 switch (c) {
154 case 'c':
155 loops = strtoul(optarg, NULL, 10);
156 break;
157 case 'n':
158 device_name = optarg;
159 break;
160 case 'o':
161 line = strtoul(optarg, NULL, 10);
162 break;
163 case 'd':
164 handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
165 break;
166 case 's':
167 handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;
168 break;
169 case 'r':
170 eventflags |= GPIOEVENT_REQUEST_RISING_EDGE;
171 break;
172 case 'f':
173 eventflags |= GPIOEVENT_REQUEST_FALLING_EDGE;
174 break;
175 case '?':
176 print_usage();
177 return -1;
178 }
179 }
180
181 if (!device_name || line == -1) {
182 print_usage();
183 return -1;
184 }
185 if (!eventflags) {
186 printf("No flags specified, listening on both rising and "
187 "falling edges\n");
188 eventflags = GPIOEVENT_REQUEST_BOTH_EDGES;
189 }
190 return monitor_device(device_name, line, handleflags,
191 eventflags, loops);
192}