Debian Codegraph @

tools/gpionotify.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2022 Kent Gibson <[email protected]>
3
4 #include <getopt.h>
5 #include <gpiod.h>
6 #include <inttypes.h>
7 #include <poll.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12
13 #include "tools-common.h"
14
15 struct config {
16 bool banner;
17 bool by_name;
18 bool quiet;
19 bool strict;
20 bool unquoted;
21 int event_type;
22 int events_wanted;
23 const char *chip_id;
24 const char *fmt;
25 int timestamp_fmt;
26 long long idle_timeout;
27 };
28
29 static void print_help(void)
30 {
31 printf("Usage: %s [OPTIONS] <line>...\n", get_prog_name());
32 printf("\n");
33 printf("Wait for changes to info on GPIO lines and print them to standard output.\n");
34 printf("\n");
35 printf("Lines are specified by name, or optionally by offset if the chip option\n");
36 printf("is provided.\n");
37 printf("\n");
38 printf("Options:\n");
39 printf(" --banner\t\tdisplay a banner on successful startup\n");
40 printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n");
41 printf(" -c, --chip <chip>\trestrict scope to a particular chip\n");
42 printf(" -e, --event <event>\tspecify the events to monitor\n");
43 printf("\t\t\tPossible values: 'requested', 'released', 'reconfigured'.\n");
44 printf("\t\t\t(default is all events)\n");
45 printf(" -h, --help\t\tdisplay this help and exit\n");
46 printf(" -F, --format <fmt>\tspecify a custom output format\n");
47 printf(" --idle-timeout <period>\n");
48 printf("\t\t\texit gracefully if no events occur for the period specified\n");
49 printf(" --localtime\tconvert event timestamps to local time\n");
50 printf(" -n, --num-events <num>\n");
51 printf("\t\t\texit after processing num events\n");
52 printf(" -q, --quiet\t\tdon't generate any output\n");
53 printf(" -s, --strict\t\tabort if requested line names are not unique\n");
54 printf(" --unquoted\tdon't quote line or consumer names\n");
55 printf(" --utc\t\tconvert event timestamps to UTC\n");
56 printf(" -v, --version\t\toutput version information and exit\n");
57 print_chip_help();
58 print_period_help();
59 printf("\n");
60 printf("Format specifiers:\n");
61 printf(" %%o GPIO line offset\n");
62 printf(" %%l GPIO line name\n");
63 printf(" %%c GPIO chip name\n");
64 printf(" %%e numeric info event type ('1' - requested, '2' - released or '3' - reconfigured)\n");
65 printf(" %%E info event type ('requested', 'released' or 'reconfigured')\n");
66 printf(" %%a line attributes\n");
67 printf(" %%C consumer\n");
68 printf(" %%S event timestamp as seconds\n");
69 printf(" %%U event timestamp as UTC\n");
70 printf(" %%L event timestamp as local time\n");
71 }
72
73 static int parse_event_type_or_die(const char *option)
74 {
75 if (strcmp(option, "requested") == 0)
76 return GPIOD_INFO_EVENT_LINE_REQUESTED;
77 if (strcmp(option, "released") == 0)
78 return GPIOD_INFO_EVENT_LINE_RELEASED;
79 if (strcmp(option, "reconfigured") != 0)
80 die("invalid edge: %s", option);
81
82 return GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED;
83 }
84
85 static int parse_config(int argc, char **argv, struct config *cfg)
86 {
87 static const char *const shortopts = "+c:e:hF:n:qshv";
88
89 const struct option longopts[] = {
90 { "banner", no_argument, NULL, '-'},
91 { "by-name", no_argument, NULL, 'B'},
92 { "chip", required_argument, NULL, 'c' },
93 { "event", required_argument, NULL, 'e' },
94 { "format", required_argument, NULL, 'F' },
95 { "help", no_argument, NULL, 'h' },
96 { "idle-timeout", required_argument, NULL, 'i' },
97 { "localtime", no_argument, &cfg->timestamp_fmt, 2 },
98 { "num-events", required_argument, NULL, 'n' },
99 { "quiet", no_argument, NULL, 'q' },
100 { "silent", no_argument, NULL, 'q' },
101 { "strict", no_argument, NULL, 's' },
102 { "unquoted", no_argument, NULL, 'Q' },
103 { "utc", no_argument, &cfg->timestamp_fmt, 1 },
104 { "version", no_argument, NULL, 'v' },
105 { GETOPT_NULL_LONGOPT },
106 };
107
108 int opti, optc;
109
110 memset(cfg, 0, sizeof(*cfg));
111 cfg->idle_timeout = -1;
112
113 for (;;) {
114 optc = getopt_long(argc, argv, shortopts, longopts, &opti);
115 if (optc < 0)
116 break;
117
118 switch (optc) {
119 case '-':
120 cfg->banner = true;
121 break;
122 case 'B':
123 cfg->by_name = true;
124 break;
125 case 'c':
126 cfg->chip_id = optarg;
127 break;
128 case 'e':
129 cfg->event_type = parse_event_type_or_die(optarg);
130 break;
131 case 'F':
132 cfg->fmt = optarg;
133 break;
134 case 'i':
135 cfg->idle_timeout = parse_period_or_die(optarg);
136 break;
137 case 'n':
138 cfg->events_wanted = parse_uint_or_die(optarg);
139 break;
140 case 'q':
141 cfg->quiet = true;
142 break;
143 case 'Q':
144 cfg->unquoted = true;
145 break;
146 case 's':
147 cfg->strict = true;
148 break;
149 case 'h':
150 print_help();
151 exit(EXIT_SUCCESS);
152 case 'v':
153 print_version();
154 exit(EXIT_SUCCESS);
155 case '?':
156 die("try %s --help", get_prog_name());
157 case 0:
158 break;
159 default:
160 abort();
161 }
162 }
163
164 return optind;
165 }
166
167 static void print_banner(int num_lines, char **lines)
168 {
169 int i;
170
171 if (num_lines > 1) {
172 printf("Watching lines ");
173
174 for (i = 0; i < num_lines - 1; i++)
175 printf("'%s', ", lines[i]);
176
177 printf("and '%s'...\n", lines[i]);
178 } else {
179 printf("Watching line '%s'...\n", lines[0]);
180 }
181 }
182
183 static void print_event_type(int evtype)
184 {
185 switch (evtype) {
186 case GPIOD_INFO_EVENT_LINE_REQUESTED:
187 fputs("requested", stdout);
188 break;
189 case GPIOD_INFO_EVENT_LINE_RELEASED:
190 fputs("released", stdout);
191 break;
192 case GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED:
193 fputs("reconfigured", stdout);
194 break;
195 default:
196 fputs("unknown", stdout);
197 break;
198 }
199 }
200
201 /*
202 * A convenience function to map clock monotonic to realtime, as uAPI only
203 * supports CLOCK_MONOTONIC.
204 *
205 * Samples the realtime clock on either side of a monotonic sample and averages
206 * the realtime samples to estimate the offset between the two clocks.
207 * Any time shifts between the two realtime samples will result in the
208 * monotonic time being mapped to the average of the before and after, so
209 * half way between the old and new times.
210 *
211 * Any CPU suspension between the event being generated and converted will
212 * result in the returned time being shifted by the period of suspension.
213 */
214 static uint64_t monotonic_to_realtime(uint64_t evtime)
215 {
216 uint64_t before, after, mono;
217 struct timespec ts;
218
219 clock_gettime(CLOCK_REALTIME, &ts);
220 before = ts.tv_nsec + ((uint64_t)ts.tv_sec) * 1000000000;
221
222 clock_gettime(CLOCK_MONOTONIC, &ts);
223 mono = ts.tv_nsec + ((uint64_t)ts.tv_sec) * 1000000000;
224
225 clock_gettime(CLOCK_REALTIME, &ts);
226 after = ts.tv_nsec + ((uint64_t)ts.tv_sec) * 1000000000;
227
228 evtime += (after / 2 - mono + before / 2);
229
230 return evtime;
231 }
232
233 static void event_print_formatted(struct gpiod_info_event *event,
234 struct line_resolver *resolver, int chip_num,
235 struct config *cfg)
236 {
237 const char *lname, *prev, *curr, *consumer;
238 struct gpiod_line_info *info;
239 unsigned int offset;
240 uint64_t evtime;
241 int evtype;
242 char fmt;
243
244 info = gpiod_info_event_get_line_info(event);
245 evtime = gpiod_info_event_get_timestamp_ns(event);
246 evtype = gpiod_info_event_get_event_type(event);
247 offset = gpiod_line_info_get_offset(info);
248
249 for (prev = curr = cfg->fmt;;) {
250 curr = strchr(curr, '%');
251 if (!curr) {
252 fputs(prev, stdout);
253 break;
254 }
255
256 if (prev != curr)
257 fwrite(prev, curr - prev, 1, stdout);
258
259 fmt = *(curr + 1);
260
261 switch (fmt) {
262 case 'a':
263 print_line_attributes(info, cfg->unquoted);
264 break;
265 case 'c':
266 fputs(get_chip_name(resolver, chip_num), stdout);
267 break;
268 case 'C':
269 if (!gpiod_line_info_is_used(info)) {
270 consumer = "unused";
271 } else {
272 consumer = gpiod_line_info_get_consumer(info);
273 if (!consumer)
274 consumer = "kernel";
275 }
276 fputs(consumer, stdout);
277 break;
278 case 'e':
279 printf("%d", evtype);
280 break;
281 case 'E':
282 print_event_type(evtype);
283 break;
284 case 'l':
285 lname = gpiod_line_info_get_name(info);
286 if (!lname)
287 lname = "unnamed";
288 fputs(lname, stdout);
289 break;
290 case 'L':
291 print_event_time(monotonic_to_realtime(evtime), 2);
292 break;
293 case 'o':
294 printf("%u", offset);
295 break;
296 case 'S':
297 print_event_time(evtime, 0);
298 break;
299 case 'U':
300 print_event_time(monotonic_to_realtime(evtime), 1);
301 break;
302 case '%':
303 fputc('%', stdout);
304 break;
305 case '\0':
306 fputc('%', stdout);
307 goto end;
308 default:
309 fwrite(curr, 2, 1, stdout);
310 break;
311 }
312
313 curr += 2;
314 prev = curr;
315 }
316
317 end:
318 fputc('\n', stdout);
319 }
320
321 static void event_print_human_readable(struct gpiod_info_event *event,
322 struct line_resolver *resolver,
323 int chip_num, struct config *cfg)
324 {
325 struct gpiod_line_info *info;
326 unsigned int offset;
327 uint64_t evtime;
328 char *evname;
329 int evtype;
330
331 info = gpiod_info_event_get_line_info(event);
332 evtime = gpiod_info_event_get_timestamp_ns(event);
333 evtype = gpiod_info_event_get_event_type(event);
334 offset = gpiod_line_info_get_offset(info);
335
336 switch (evtype) {
337 case GPIOD_INFO_EVENT_LINE_REQUESTED:
338 evname = "requested";
339 break;
340 case GPIOD_INFO_EVENT_LINE_RELEASED:
341 evname = "released";
342 break;
343 case GPIOD_INFO_EVENT_LINE_CONFIG_CHANGED:
344 evname = "reconfigured";
345 break;
346 default:
347 evname = "unknown";
348 }
349
350 if (cfg->timestamp_fmt)
351 evtime = monotonic_to_realtime(evtime);
352
353 print_event_time(evtime, cfg->timestamp_fmt);
354 printf("\t%s\t", evname);
355 print_line_id(resolver, chip_num, offset, cfg->chip_id, cfg->unquoted);
356 fputc('\n', stdout);
357 }
358
359 static void event_print(struct gpiod_info_event *event,
360 struct line_resolver *resolver, int chip_num,
361 struct config *cfg)
362 {
363 if (cfg->quiet)
364 return;
365
366 if (cfg->fmt)
367 event_print_formatted(event, resolver, chip_num, cfg);
368 else
369 event_print_human_readable(event, resolver, chip_num, cfg);
370 }
371
372 int main(int argc, char **argv)
373 {
374 int i, j, ret, events_done = 0, evtype;
375 struct line_resolver *resolver;
376 struct gpiod_info_event *event;
377 struct gpiod_line_info *info;
378 struct timespec idle_timeout;
379 struct gpiod_chip **chips;
380 struct gpiod_chip *chip;
381 struct pollfd *pollfds;
382 struct config cfg;
383
384 set_prog_name(argv[0]);
385 i = parse_config(argc, argv, &cfg);
386 argc -= optind;
387 argv += optind;
388
389 if (argc < 1)
390 die("at least one GPIO line must be specified");
391
392 if (argc > 64)
393 die("too many lines given");
394
395 resolver = resolve_lines(argc, argv, cfg.chip_id, cfg.strict,
396 cfg.by_name);
397 validate_resolution(resolver, cfg.chip_id);
398 chips = calloc(resolver->num_chips, sizeof(*chips));
399 pollfds = calloc(resolver->num_chips, sizeof(*pollfds));
400 if (!pollfds || !chips)
401 die("out of memory");
402
403 for (i = 0; i < resolver->num_chips; i++) {
404 chip = gpiod_chip_open(resolver->chips[i].path);
405 if (!chip)
406 die_perror("unable to open chip '%s'",
407 resolver->chips[i].path);
408
409 for (j = 0; j < resolver->num_lines; j++) {
410 if (resolver->lines[j].chip_num != i)
411 continue;
412
413 info = gpiod_chip_watch_line_info(chip,
414 resolver->lines[j].offset);
415 if (!info)
416 die_perror("unable to watch line on chip '%s'",
417 resolver->chips[i].path);
418
419 gpiod_line_info_free(info);
420 }
421
422 chips[i] = chip;
423 pollfds[i].fd = gpiod_chip_get_fd(chip);
424 pollfds[i].events = POLLIN;
425 }
426
427 if (cfg.banner)
428 print_banner(argc, argv);
429
430 if (cfg.idle_timeout > 0) {
431 idle_timeout.tv_sec = cfg.idle_timeout / 1000000;
432 idle_timeout.tv_nsec =
433 (cfg.idle_timeout % 1000000) * 1000;
434 }
435
436 for (;;) {
437 fflush(stdout);
438
439 ret = ppoll(pollfds, resolver->num_chips,
440 cfg.idle_timeout > 0 ? &idle_timeout : NULL, NULL);
441 if (ret < 0)
442 die_perror("error polling for events");
443
444 if (ret == 0)
445 goto done;
446
447 for (i = 0; i < resolver->num_chips; i++) {
448 if (pollfds[i].revents == 0)
449 continue;
450
451 event = gpiod_chip_read_info_event(chips[i]);
452 if (!event)
453 die_perror("unable to retrieve chip event");
454
455 if (cfg.event_type) {
456 evtype = gpiod_info_event_get_event_type(event);
457 if (evtype != cfg.event_type) {
458 gpiod_info_event_free(event);
459 continue;
460 }
461 }
462
463 event_print(event, resolver, i, &cfg);
464 gpiod_info_event_free(event);
465
466 events_done++;
467
468 if (cfg.events_wanted &&
469 events_done >= cfg.events_wanted)
470 goto done;
471 }
472 }
473 done:
474 for (i = 0; i < resolver->num_chips; i++)
475 gpiod_chip_close(chips[i]);
476
477 free(pollfds);
478 free(chips);
479 free_line_resolver(resolver);
480
481 return EXIT_SUCCESS;
482 }