| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <getopt.h> |
| 6 |
#include <gpiod.h> |
| 7 |
#include <inttypes.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <poll.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "tools-common.h" |
| 15 |
|
| 16 |
#define EVENT_BUF_SIZE 32 |
| 17 |
|
| 18 |
struct config { |
| 19 |
bool active_low; |
| 20 |
bool banner; |
| 21 |
bool by_name; |
| 22 |
bool quiet; |
| 23 |
bool strict; |
| 24 |
bool unquoted; |
| 25 |
enum gpiod_line_bias bias; |
| 26 |
enum gpiod_line_edge edges; |
| 27 |
int events_wanted; |
| 28 |
unsigned long long debounce_period_us; |
| 29 |
const char *chip_id; |
| 30 |
const char *consumer; |
| 31 |
const char *fmt; |
| 32 |
enum gpiod_line_clock event_clock; |
| 33 |
int timestamp_fmt; |
| 34 |
long long idle_timeout; |
| 35 |
}; |
| 36 |
|
| 37 |
static void print_help(void) |
| 38 |
{ |
| 39 |
printf("Usage: %s [OPTIONS] <line>...\n", get_prog_name()); |
| 40 |
printf("\n"); |
| 41 |
printf("Wait for events on GPIO lines and print them to standard output.\n"); |
| 42 |
printf("\n"); |
| 43 |
printf("Lines are specified by name, or optionally by offset if the chip option\n"); |
| 44 |
printf("is provided.\n"); |
| 45 |
printf("\n"); |
| 46 |
printf("Options:\n"); |
| 47 |
printf(" --banner\t\tdisplay a banner on successful startup\n"); |
| 48 |
print_bias_help(); |
| 49 |
printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n"); |
| 50 |
printf(" -c, --chip <chip>\trestrict scope to a particular chip\n"); |
| 51 |
printf(" -C, --consumer <name>\tconsumer name applied to requested lines (default is 'gpiomon')\n"); |
| 52 |
printf(" -e, --edges <edges>\tspecify the edges to monitor\n"); |
| 53 |
printf("\t\t\tPossible values: 'falling', 'rising', 'both'.\n"); |
| 54 |
printf("\t\t\t(default is 'both')\n"); |
| 55 |
printf(" -E, --event-clock <clock>\n"); |
| 56 |
printf("\t\t\tspecify the source clock for event timestamps\n"); |
| 57 |
printf("\t\t\tPossible values: 'monotonic', 'realtime', 'hte'.\n"); |
| 58 |
printf("\t\t\t(default is 'monotonic')\n"); |
| 59 |
printf("\t\t\tBy default 'realtime' is formatted as UTC, others as raw u64.\n"); |
| 60 |
printf(" -h, --help\t\tdisplay this help and exit\n"); |
| 61 |
printf(" -F, --format <fmt>\tspecify a custom output format\n"); |
| 62 |
printf(" --idle-timeout <period>\n"); |
| 63 |
printf("\t\t\texit gracefully if no events occur for the period specified\n"); |
| 64 |
printf(" -l, --active-low\ttreat the line as active low, flipping the sense of\n"); |
| 65 |
printf("\t\t\trising and falling edges\n"); |
| 66 |
printf(" --localtime\tformat event timestamps as local time\n"); |
| 67 |
printf(" -n, --num-events <num>\n"); |
| 68 |
printf("\t\t\texit after processing num events\n"); |
| 69 |
printf(" -p, --debounce-period <period>\n"); |
| 70 |
printf("\t\t\tdebounce the line(s) with the specified period\n"); |
| 71 |
printf(" -q, --quiet\t\tdon't generate any output\n"); |
| 72 |
printf(" -s, --strict\t\tabort if requested line names are not unique\n"); |
| 73 |
printf(" --unquoted\tdon't quote line or consumer names\n"); |
| 74 |
printf(" --utc\t\tformat event timestamps as UTC (default for 'realtime')\n"); |
| 75 |
printf(" -v, --version\t\toutput version information and exit\n"); |
| 76 |
print_chip_help(); |
| 77 |
print_period_help(); |
| 78 |
printf("\n"); |
| 79 |
printf("Format specifiers:\n"); |
| 80 |
printf(" %%o GPIO line offset\n"); |
| 81 |
printf(" %%l GPIO line name\n"); |
| 82 |
printf(" %%c GPIO chip name\n"); |
| 83 |
printf(" %%e numeric edge event type ('1' - rising or '2' - falling)\n"); |
| 84 |
printf(" %%E edge event type ('rising' or 'falling')\n"); |
| 85 |
printf(" %%S event timestamp as seconds\n"); |
| 86 |
printf(" %%U event timestamp as UTC\n"); |
| 87 |
printf(" %%L event timestamp as local time\n"); |
| 88 |
} |
| 89 |
|
| 90 |
static int parse_edges_or_die(const char *option) |
| 91 |
{ |
| 92 |
if (strcmp(option, "rising") == 0) |
| 93 |
return GPIOD_LINE_EDGE_RISING; |
| 94 |
if (strcmp(option, "falling") == 0) |
| 95 |
return GPIOD_LINE_EDGE_FALLING; |
| 96 |
if (strcmp(option, "both") != 0) |
| 97 |
die("invalid edges: %s", option); |
| 98 |
|
| 99 |
return GPIOD_LINE_EDGE_BOTH; |
| 100 |
} |
| 101 |
|
| 102 |
static int parse_event_clock_or_die(const char *option) |
| 103 |
{ |
| 104 |
if (strcmp(option, "realtime") == 0) |
| 105 |
return GPIOD_LINE_CLOCK_REALTIME; |
| 106 |
if (strcmp(option, "hte") == 0) |
| 107 |
return GPIOD_LINE_CLOCK_HTE; |
| 108 |
if (strcmp(option, "monotonic") != 0) |
| 109 |
die("invalid event clock: %s", option); |
| 110 |
|
| 111 |
return GPIOD_LINE_CLOCK_MONOTONIC; |
| 112 |
} |
| 113 |
|
| 114 |
static int parse_config(int argc, char **argv, struct config *cfg) |
| 115 |
{ |
| 116 |
static const char *const shortopts = "+b:c:C:e:E:hF:ln:p:qshv"; |
| 117 |
|
| 118 |
const struct option longopts[] = { |
| 119 |
{ "active-low", no_argument, NULL, 'l' }, |
| 120 |
{ "banner", no_argument, NULL, '-'}, |
| 121 |
{ "bias", required_argument, NULL, 'b' }, |
| 122 |
{ "by-name", no_argument, NULL, 'B'}, |
| 123 |
{ "chip", required_argument, NULL, 'c' }, |
| 124 |
{ "consumer", required_argument, NULL, 'C' }, |
| 125 |
{ "debounce-period", required_argument, NULL, 'p' }, |
| 126 |
{ "edges", required_argument, NULL, 'e' }, |
| 127 |
{ "event-clock", required_argument, NULL, 'E' }, |
| 128 |
{ "format", required_argument, NULL, 'F' }, |
| 129 |
{ "help", no_argument, NULL, 'h' }, |
| 130 |
{ "idle-timeout", required_argument, NULL, 'i' }, |
| 131 |
{ "localtime", no_argument, &cfg->timestamp_fmt, 2 }, |
| 132 |
{ "num-events", required_argument, NULL, 'n' }, |
| 133 |
{ "quiet", no_argument, NULL, 'q' }, |
| 134 |
{ "silent", no_argument, NULL, 'q' }, |
| 135 |
{ "strict", no_argument, NULL, 's' }, |
| 136 |
{ "unquoted", no_argument, NULL, 'Q' }, |
| 137 |
{ "utc", no_argument, &cfg->timestamp_fmt, 1 }, |
| 138 |
{ "version", no_argument, NULL, 'v' }, |
| 139 |
{ GETOPT_NULL_LONGOPT }, |
| 140 |
}; |
| 141 |
|
| 142 |
int opti, optc; |
| 143 |
|
| 144 |
memset(cfg, 0, sizeof(*cfg)); |
| 145 |
cfg->edges = GPIOD_LINE_EDGE_BOTH; |
| 146 |
cfg->consumer = "gpiomon"; |
| 147 |
cfg->idle_timeout = -1; |
| 148 |
|
| 149 |
for (;;) { |
| 150 |
optc = getopt_long(argc, argv, shortopts, longopts, &opti); |
| 151 |
if (optc < 0) |
| 152 |
break; |
| 153 |
|
| 154 |
switch (optc) { |
| 155 |
case '-': |
| 156 |
cfg->banner = true; |
| 157 |
break; |
| 158 |
case 'b': |
| 159 |
cfg->bias = parse_bias_or_die(optarg); |
| 160 |
break; |
| 161 |
case 'B': |
| 162 |
cfg->by_name = true; |
| 163 |
break; |
| 164 |
case 'c': |
| 165 |
cfg->chip_id = optarg; |
| 166 |
break; |
| 167 |
case 'C': |
| 168 |
cfg->consumer = optarg; |
| 169 |
break; |
| 170 |
case 'e': |
| 171 |
cfg->edges = parse_edges_or_die(optarg); |
| 172 |
break; |
| 173 |
case 'E': |
| 174 |
cfg->event_clock = parse_event_clock_or_die(optarg); |
| 175 |
break; |
| 176 |
case 'F': |
| 177 |
cfg->fmt = optarg; |
| 178 |
break; |
| 179 |
case 'i': |
| 180 |
cfg->idle_timeout = parse_period_or_die(optarg); |
| 181 |
break; |
| 182 |
case 'l': |
| 183 |
cfg->active_low = true; |
| 184 |
break; |
| 185 |
case 'n': |
| 186 |
cfg->events_wanted = parse_uint_or_die(optarg); |
| 187 |
break; |
| 188 |
case 'p': |
| 189 |
cfg->debounce_period_us = parse_period_or_die(optarg); |
| 190 |
break; |
| 191 |
case 'q': |
| 192 |
cfg->quiet = true; |
| 193 |
break; |
| 194 |
case 'Q': |
| 195 |
cfg->unquoted = true; |
| 196 |
break; |
| 197 |
case 's': |
| 198 |
cfg->strict = true; |
| 199 |
break; |
| 200 |
case 'h': |
| 201 |
print_help(); |
| 202 |
exit(EXIT_SUCCESS); |
| 203 |
case 'v': |
| 204 |
print_version(); |
| 205 |
exit(EXIT_SUCCESS); |
| 206 |
case '?': |
| 207 |
die("try %s --help", get_prog_name()); |
| 208 |
case 0: |
| 209 |
break; |
| 210 |
default: |
| 211 |
abort(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
if (cfg->event_clock == 0) { |
| 217 |
if (cfg->timestamp_fmt) |
| 218 |
cfg->event_clock = GPIOD_LINE_CLOCK_REALTIME; |
| 219 |
else |
| 220 |
cfg->event_clock = GPIOD_LINE_CLOCK_MONOTONIC; |
| 221 |
} else if ((cfg->event_clock == GPIOD_LINE_CLOCK_REALTIME) && |
| 222 |
(cfg->timestamp_fmt == 0)) { |
| 223 |
cfg->timestamp_fmt = 1; |
| 224 |
} |
| 225 |
|
| 226 |
return optind; |
| 227 |
} |
| 228 |
|
| 229 |
static void print_banner(int num_lines, char **lines) |
| 230 |
{ |
| 231 |
int i; |
| 232 |
|
| 233 |
if (num_lines > 1) { |
| 234 |
printf("Monitoring lines "); |
| 235 |
|
| 236 |
for (i = 0; i < num_lines - 1; i++) |
| 237 |
printf("'%s', ", lines[i]); |
| 238 |
|
| 239 |
printf("and '%s'...\n", lines[i]); |
| 240 |
} else { |
| 241 |
printf("Monitoring line '%s'...\n", lines[0]); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
static void event_print_formatted(struct gpiod_edge_event *event, |
| 246 |
struct line_resolver *resolver, int chip_num, |
| 247 |
struct config *cfg) |
| 248 |
{ |
| 249 |
const char *lname, *prev, *curr; |
| 250 |
unsigned int offset; |
| 251 |
uint64_t evtime; |
| 252 |
int evtype; |
| 253 |
char fmt; |
| 254 |
|
| 255 |
offset = gpiod_edge_event_get_line_offset(event); |
| 256 |
evtime = gpiod_edge_event_get_timestamp_ns(event); |
| 257 |
evtype = gpiod_edge_event_get_event_type(event); |
| 258 |
|
| 259 |
for (prev = curr = cfg->fmt;;) { |
| 260 |
curr = strchr(curr, '%'); |
| 261 |
if (!curr) { |
| 262 |
fputs(prev, stdout); |
| 263 |
break; |
| 264 |
} |
| 265 |
|
| 266 |
if (prev != curr) |
| 267 |
fwrite(prev, curr - prev, 1, stdout); |
| 268 |
|
| 269 |
fmt = *(curr + 1); |
| 270 |
|
| 271 |
switch (fmt) { |
| 272 |
case 'c': |
| 273 |
fputs(get_chip_name(resolver, chip_num), stdout); |
| 274 |
break; |
| 275 |
case 'e': |
| 276 |
printf("%d", evtype); |
| 277 |
break; |
| 278 |
case 'E': |
| 279 |
if (evtype == GPIOD_EDGE_EVENT_RISING_EDGE) |
| 280 |
fputs("rising", stdout); |
| 281 |
else |
| 282 |
fputs("falling", stdout); |
| 283 |
break; |
| 284 |
case 'l': |
| 285 |
lname = get_line_name(resolver, chip_num, offset); |
| 286 |
if (!lname) |
| 287 |
lname = "unnamed"; |
| 288 |
fputs(lname, stdout); |
| 289 |
break; |
| 290 |
case 'L': |
| 291 |
print_event_time(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(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_edge_event *event, |
| 322 |
struct line_resolver *resolver, |
| 323 |
int chip_num, struct config *cfg) |
| 324 |
{ |
| 325 |
unsigned int offset; |
| 326 |
uint64_t evtime; |
| 327 |
|
| 328 |
offset = gpiod_edge_event_get_line_offset(event); |
| 329 |
evtime = gpiod_edge_event_get_timestamp_ns(event); |
| 330 |
|
| 331 |
print_event_time(evtime, cfg->timestamp_fmt); |
| 332 |
|
| 333 |
if (gpiod_edge_event_get_event_type(event) == |
| 334 |
GPIOD_EDGE_EVENT_RISING_EDGE) |
| 335 |
fputs("\trising\t", stdout); |
| 336 |
else |
| 337 |
fputs("\tfalling\t", stdout); |
| 338 |
|
| 339 |
print_line_id(resolver, chip_num, offset, cfg->chip_id, cfg->unquoted); |
| 340 |
fputc('\n', stdout); |
| 341 |
} |
| 342 |
|
| 343 |
static void event_print(struct gpiod_edge_event *event, |
| 344 |
struct line_resolver *resolver, int chip_num, |
| 345 |
struct config *cfg) |
| 346 |
{ |
| 347 |
if (cfg->quiet) |
| 348 |
return; |
| 349 |
|
| 350 |
if (cfg->fmt) |
| 351 |
event_print_formatted(event, resolver, chip_num, cfg); |
| 352 |
else |
| 353 |
event_print_human_readable(event, resolver, chip_num, cfg); |
| 354 |
} |
| 355 |
|
| 356 |
int main(int argc, char **argv) |
| 357 |
{ |
| 358 |
struct gpiod_edge_event_buffer *event_buffer; |
| 359 |
struct gpiod_line_settings *settings; |
| 360 |
struct gpiod_request_config *req_cfg; |
| 361 |
struct gpiod_line_request **requests; |
| 362 |
struct gpiod_line_config *line_cfg; |
| 363 |
int num_lines, events_done = 0; |
| 364 |
struct gpiod_edge_event *event; |
| 365 |
struct line_resolver *resolver; |
| 366 |
struct timespec idle_timeout; |
| 367 |
struct gpiod_chip *chip; |
| 368 |
struct pollfd *pollfds; |
| 369 |
unsigned int *offsets; |
| 370 |
struct config cfg; |
| 371 |
int ret, i, j; |
| 372 |
|
| 373 |
set_prog_name(argv[0]); |
| 374 |
i = parse_config(argc, argv, &cfg); |
| 375 |
argc -= i; |
| 376 |
argv += i; |
| 377 |
|
| 378 |
if (argc < 1) |
| 379 |
die("at least one GPIO line must be specified"); |
| 380 |
|
| 381 |
if (argc > 64) |
| 382 |
die("too many lines given"); |
| 383 |
|
| 384 |
settings = gpiod_line_settings_new(); |
| 385 |
if (!settings) |
| 386 |
die_perror("unable to allocate line settings"); |
| 387 |
|
| 388 |
if (cfg.bias) |
| 389 |
gpiod_line_settings_set_bias(settings, cfg.bias); |
| 390 |
|
| 391 |
if (cfg.active_low) |
| 392 |
gpiod_line_settings_set_active_low(settings, true); |
| 393 |
|
| 394 |
if (cfg.debounce_period_us) { |
| 395 |
if (cfg.debounce_period_us > UINT_MAX) |
| 396 |
die("maximum debounce period is %uus, got %lluus", |
| 397 |
UINT_MAX, cfg.debounce_period_us); |
| 398 |
|
| 399 |
gpiod_line_settings_set_debounce_period_us( |
| 400 |
settings, (unsigned long)cfg.debounce_period_us); |
| 401 |
} |
| 402 |
|
| 403 |
gpiod_line_settings_set_event_clock(settings, cfg.event_clock); |
| 404 |
gpiod_line_settings_set_edge_detection(settings, cfg.edges); |
| 405 |
|
| 406 |
line_cfg = gpiod_line_config_new(); |
| 407 |
if (!line_cfg) |
| 408 |
die_perror("unable to allocate the line config structure"); |
| 409 |
|
| 410 |
req_cfg = gpiod_request_config_new(); |
| 411 |
if (!req_cfg) |
| 412 |
die_perror("unable to allocate the request config structure"); |
| 413 |
|
| 414 |
gpiod_request_config_set_consumer(req_cfg, cfg.consumer); |
| 415 |
|
| 416 |
event_buffer = gpiod_edge_event_buffer_new(EVENT_BUF_SIZE); |
| 417 |
if (!event_buffer) |
| 418 |
die_perror("unable to allocate the line event buffer"); |
| 419 |
|
| 420 |
resolver = resolve_lines(argc, argv, cfg.chip_id, cfg.strict, |
| 421 |
cfg.by_name); |
| 422 |
validate_resolution(resolver, cfg.chip_id); |
| 423 |
requests = calloc(resolver->num_chips, sizeof(*requests)); |
| 424 |
pollfds = calloc(resolver->num_chips, sizeof(*pollfds)); |
| 425 |
offsets = calloc(resolver->num_lines, sizeof(*offsets)); |
| 426 |
if (!requests || !pollfds || !offsets) |
| 427 |
die("out of memory"); |
| 428 |
|
| 429 |
for (i = 0; i < resolver->num_chips; i++) { |
| 430 |
num_lines = get_line_offsets_and_values(resolver, i, offsets, |
| 431 |
NULL); |
| 432 |
gpiod_line_config_reset(line_cfg); |
| 433 |
ret = gpiod_line_config_add_line_settings(line_cfg, offsets, |
| 434 |
num_lines, settings); |
| 435 |
if (ret) |
| 436 |
die_perror("unable to add line settings"); |
| 437 |
|
| 438 |
chip = gpiod_chip_open(resolver->chips[i].path); |
| 439 |
if (!chip) |
| 440 |
die_perror("unable to open chip '%s'", |
| 441 |
resolver->chips[i].path); |
| 442 |
|
| 443 |
requests[i] = gpiod_chip_request_lines(chip, req_cfg, line_cfg); |
| 444 |
if (!requests[i]) |
| 445 |
die_perror("unable to request lines on chip %s", |
| 446 |
resolver->chips[i].path); |
| 447 |
|
| 448 |
pollfds[i].fd = gpiod_line_request_get_fd(requests[i]); |
| 449 |
pollfds[i].events = POLLIN; |
| 450 |
gpiod_chip_close(chip); |
| 451 |
} |
| 452 |
|
| 453 |
gpiod_request_config_free(req_cfg); |
| 454 |
gpiod_line_config_free(line_cfg); |
| 455 |
gpiod_line_settings_free(settings); |
| 456 |
|
| 457 |
if (cfg.banner) |
| 458 |
print_banner(argc, argv); |
| 459 |
|
| 460 |
if (cfg.idle_timeout > 0) { |
| 461 |
idle_timeout.tv_sec = cfg.idle_timeout / 1000000; |
| 462 |
idle_timeout.tv_nsec = |
| 463 |
(cfg.idle_timeout % 1000000) * 1000; |
| 464 |
} |
| 465 |
|
| 466 |
for (;;) { |
| 467 |
fflush(stdout); |
| 468 |
|
| 469 |
ret = ppoll(pollfds, resolver->num_chips, |
| 470 |
cfg.idle_timeout > 0 ? &idle_timeout : NULL, NULL); |
| 471 |
if (ret < 0) |
| 472 |
die_perror("error polling for events"); |
| 473 |
|
| 474 |
if (ret == 0) |
| 475 |
goto done; |
| 476 |
|
| 477 |
for (i = 0; i < resolver->num_chips; i++) { |
| 478 |
if (pollfds[i].revents == 0) |
| 479 |
continue; |
| 480 |
|
| 481 |
ret = gpiod_line_request_read_edge_events(requests[i], |
| 482 |
event_buffer, EVENT_BUF_SIZE); |
| 483 |
if (ret < 0) |
| 484 |
die_perror("error reading line events"); |
| 485 |
|
| 486 |
for (j = 0; j < ret; j++) { |
| 487 |
event = gpiod_edge_event_buffer_get_event( |
| 488 |
event_buffer, j); |
| 489 |
if (!event) |
| 490 |
die_perror("unable to retrieve event from buffer"); |
| 491 |
|
| 492 |
event_print(event, resolver, i, &cfg); |
| 493 |
|
| 494 |
events_done++; |
| 495 |
|
| 496 |
if (cfg.events_wanted && |
| 497 |
events_done >= cfg.events_wanted) |
| 498 |
goto done; |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
done: |
| 504 |
for (i = 0; i < resolver->num_chips; i++) |
| 505 |
gpiod_line_request_release(requests[i]); |
| 506 |
|
| 507 |
free(requests); |
| 508 |
free_line_resolver(resolver); |
| 509 |
gpiod_edge_event_buffer_free(event_buffer); |
| 510 |
free(offsets); |
| 511 |
|
| 512 |
return EXIT_SUCCESS; |
| 513 |
} |