Debian Codegraph @

tools/tools-common.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <[email protected]>
3 // SPDX-FileCopyrightText: 2022 Kent Gibson <[email protected]>
4
5 /* Common code for GPIO tools. */
6
7 #include <ctype.h>
8 #include <dirent.h>
9 #include <errno.h>
10 #include <gpiod.h>
11 #include <inttypes.h>
12 #include <limits.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <time.h>
19
20 #include "tools-common.h"
21
22 static const char *prog_name = NULL;
23 static const char *prog_short_name = NULL;
24
25 void set_prog_name(const char *name)
26 {
27 prog_name = name;
28 prog_short_name = name;
29 while (*name) {
30 if (*name++ == '/') {
31 prog_short_name = name;
32 }
33 }
34 }
35
36 const char *get_prog_name(void)
37 {
38 return prog_name;
39 }
40
41 const char *get_prog_short_name(void)
42 {
43 return prog_short_name;
44 }
45
46 void print_error(const char *fmt, ...)
47 {
48 va_list va;
49
50 va_start(va, fmt);
51 fprintf(stderr, "%s: ", get_prog_name());
52 vfprintf(stderr, fmt, va);
53 fprintf(stderr, "\n");
54 va_end(va);
55 }
56
57 void print_perror(const char *fmt, ...)
58 {
59 va_list va;
60
61 va_start(va, fmt);
62 fprintf(stderr, "%s: ", get_prog_name());
63 vfprintf(stderr, fmt, va);
64 fprintf(stderr, ": %s\n", strerror(errno));
65 va_end(va);
66 }
67
68 void die(const char *fmt, ...)
69 {
70 va_list va;
71
72 va_start(va, fmt);
73 fprintf(stderr, "%s: ", get_prog_name());
74 vfprintf(stderr, fmt, va);
75 fprintf(stderr, "\n");
76 va_end(va);
77
78 exit(EXIT_FAILURE);
79 }
80
81 void die_perror(const char *fmt, ...)
82 {
83 va_list va;
84
85 va_start(va, fmt);
86 fprintf(stderr, "%s: ", get_prog_name());
87 vfprintf(stderr, fmt, va);
88 fprintf(stderr, ": %s\n", strerror(errno));
89 va_end(va);
90
91 exit(EXIT_FAILURE);
92 }
93
94 void print_version(void)
95 {
96 printf("%s (libgpiod) v%s\n", get_prog_short_name(), gpiod_api_version());
97 printf("Copyright (C) 2017-2023 Bartosz Golaszewski\n");
98 printf("License: GPL-2.0-or-later\n");
99 printf("This is free software: you are free to change and redistribute it.\n");
100 printf("There is NO WARRANTY, to the extent permitted by law.\n");
101 }
102
103 int parse_bias_or_die(const char *option)
104 {
105 if (strcmp(option, "pull-down") == 0)
106 return GPIOD_LINE_BIAS_PULL_DOWN;
107 if (strcmp(option, "pull-up") == 0)
108 return GPIOD_LINE_BIAS_PULL_UP;
109 if (strcmp(option, "disabled") != 0)
110 die("invalid bias: %s", option);
111
112 return GPIOD_LINE_BIAS_DISABLED;
113 }
114
115 long long parse_period(const char *option)
116 {
117 unsigned long long p, m = 0;
118 char *end;
119
120 p = strtoull(option, &end, 10);
121
122 switch (*end) {
123 case 'u':
124 if (*(end + 1) != 's')
125 return -1;
126 m = 1;
127 end++;
128 break;
129 case 'm':
130 m = 1000;
131 end++;
132 break;
133 case 's':
134 m = 1000000;
135 break;
136 case '\0':
137 break;
138 default:
139 return -1;
140 }
141
142 if (m) {
143 if (*end == '\0')
144 m = 60000000;
145 else if (*end == 's')
146 end++;
147 else
148 return -1;
149 } else {
150 m = 1000;
151 }
152
153 if (*end != '\0')
154 return -1;
155
156 if (m != 0 && p > ULLONG_MAX / m)
157 return -1;
158 p *= m;
159
160 return p;
161 }
162
163 unsigned long long parse_period_or_die(const char *option)
164 {
165 long long period = parse_period(option);
166
167 if (period < 0)
168 die("invalid period: %s", option);
169
170 return period;
171 }
172
173 void sleep_us(unsigned long long period)
174 {
175 struct timespec spec;
176
177 spec.tv_sec = period / 1000000;
178 spec.tv_nsec = (period % 1000000) * 1000;
179
180 nanosleep(&spec, NULL);
181 }
182
183 int parse_uint(const char *option)
184 {
185 unsigned long o;
186 char *end;
187
188 o = strtoul(option, &end, 10);
189 if (*end == '\0' && o <= INT_MAX)
190 return o;
191
192 return -1;
193 }
194
195 unsigned int parse_uint_or_die(const char *option)
196 {
197 int i = parse_uint(option);
198
199 if (i < 0)
200 die("invalid number: '%s'", option);
201
202 return i;
203 }
204
205 void print_bias_help(void)
206 {
207 printf(" -b, --bias <bias>\tspecify the line bias\n");
208 printf("\t\t\tPossible values: 'pull-down', 'pull-up', 'disabled'.\n");
209 printf("\t\t\t(default is to leave bias unchanged)\n");
210 }
211
212 void print_chip_help(void)
213 {
214 printf("\nChips:\n");
215 printf(" A GPIO chip may be identified by number, name, or path.\n");
216 printf(" e.g. '0', 'gpiochip0', and '/dev/gpiochip0' all refer to the same chip.\n");
217 }
218
219 void print_period_help(void)
220 {
221 printf("\nPeriods:\n");
222 printf(" Periods are taken as milliseconds unless units are specified. e.g. 10us.\n");
223 printf(" Supported units are 'm', 's', 'ms', and 'us' for minutes, seconds, milliseconds and\n");
224 printf(" microseconds respectively.\n");
225 }
226
227 #define TIME_BUFFER_SIZE 20
228
229 /*
230 * format:
231 * 0: raw seconds
232 * 1: utc time
233 * 2: local time
234 */
235 void print_event_time(uint64_t evtime, int format)
236 {
237 char tbuf[TIME_BUFFER_SIZE];
238 time_t evtsec;
239 struct tm t;
240 char *tz;
241
242 if (format) {
243 evtsec = evtime / 1000000000;
244 if (format == 2) {
245 localtime_r(&evtsec, &t);
246 tz = "";
247 } else {
248 gmtime_r(&evtsec, &t);
249 tz = "Z";
250 }
251 strftime(tbuf, TIME_BUFFER_SIZE, "%FT%T", &t);
252 printf("%s.%09" PRIu64 "%s", tbuf, evtime % 1000000000, tz);
253 } else {
254 printf("%" PRIu64 ".%09" PRIu64, evtime / 1000000000,
255 evtime % 1000000000);
256 }
257 }
258
259 static void print_bias(struct gpiod_line_info *info)
260 {
261 const char *name;
262
263 switch (gpiod_line_info_get_bias(info)) {
264 case GPIOD_LINE_BIAS_PULL_UP:
265 name = "pull-up";
266 break;
267 case GPIOD_LINE_BIAS_PULL_DOWN:
268 name = "pull-down";
269 break;
270 case GPIOD_LINE_BIAS_DISABLED:
271 name = "disabled";
272 break;
273 default:
274 return;
275 }
276
277 printf(" bias=%s", name);
278 }
279
280 static void print_drive(struct gpiod_line_info *info)
281 {
282 const char *name;
283
284 switch (gpiod_line_info_get_drive(info)) {
285 case GPIOD_LINE_DRIVE_OPEN_DRAIN:
286 name = "open-drain";
287 break;
288 case GPIOD_LINE_DRIVE_OPEN_SOURCE:
289 name = "open-source";
290 break;
291 default:
292 return;
293 }
294
295 printf(" drive=%s", name);
296 }
297
298 static void print_edge_detection(struct gpiod_line_info *info)
299 {
300 const char *name;
301
302 switch (gpiod_line_info_get_edge_detection(info)) {
303 case GPIOD_LINE_EDGE_BOTH:
304 name = "both";
305 break;
306 case GPIOD_LINE_EDGE_RISING:
307 name = "rising";
308 break;
309 case GPIOD_LINE_EDGE_FALLING:
310 name = "falling";
311 break;
312 default:
313 return;
314 }
315
316 printf(" edges=%s", name);
317 }
318
319 static void print_event_clock(struct gpiod_line_info *info)
320 {
321 const char *name;
322
323 switch (gpiod_line_info_get_event_clock(info)) {
324 case GPIOD_LINE_CLOCK_REALTIME:
325 name = "realtime";
326 break;
327 case GPIOD_LINE_CLOCK_HTE:
328 name = "hte";
329 break;
330 default:
331 return;
332 }
333
334 printf(" event-clock=%s", name);
335 }
336
337 static void print_debounce(struct gpiod_line_info *info)
338 {
339 const char *units = "us";
340 unsigned long debounce;
341
342 debounce = gpiod_line_info_get_debounce_period_us(info);
343 if (!debounce)
344 return;
345 if (debounce % 1000000 == 0) {
346 debounce /= 1000000;
347 units = "s";
348 } else if (debounce % 1000 == 0) {
349 debounce /= 1000;
350 units = "ms";
351 }
352 printf(" debounce-period=%lu%s", debounce, units);
353 }
354
355 static void print_consumer(struct gpiod_line_info *info, bool unquoted)
356 {
357 const char *consumer;
358 const char *fmt;
359
360 if (!gpiod_line_info_is_used(info))
361 return;
362
363 consumer = gpiod_line_info_get_consumer(info);
364 if (!consumer)
365 consumer = "kernel";
366
367 fmt = unquoted ? " consumer=%s" : " consumer=\"%s\"";
368
369 printf(fmt, consumer);
370 }
371
372 void print_line_attributes(struct gpiod_line_info *info, bool unquoted_strings)
373 {
374 enum gpiod_line_direction direction;
375
376 direction = gpiod_line_info_get_direction(info);
377
378 printf("%s", direction == GPIOD_LINE_DIRECTION_INPUT ?
379 "input" : "output");
380
381 if (gpiod_line_info_is_active_low(info))
382 printf(" active-low");
383
384 print_drive(info);
385 print_bias(info);
386 print_edge_detection(info);
387 print_event_clock(info);
388 print_debounce(info);
389 print_consumer(info, unquoted_strings);
390 }
391
392 void print_line_id(struct line_resolver *resolver, int chip_num,
393 unsigned int offset, const char *chip_id, bool unquoted)
394 {
395 const char *lname, *fmt;
396
397 lname = get_line_name(resolver, chip_num, offset);
398 if (!lname) {
399 printf("%s %u", get_chip_name(resolver, chip_num), offset);
400 return;
401 }
402 if (chip_id)
403 printf("%s %u ", get_chip_name(resolver, chip_num), offset);
404
405 fmt = unquoted ? "%s" : "\"%s\"";
406 printf(fmt, lname);
407 }
408
409 static int chip_dir_filter(const struct dirent *entry)
410 {
411 struct stat sb;
412 int ret = 0;
413 char *path;
414
415 if (asprintf(&path, "/dev/%s", entry->d_name) < 0)
416 return 0;
417
418 if ((lstat(path, &sb) == 0) && (!S_ISLNK(sb.st_mode)) &&
419 gpiod_is_gpiochip_device(path))
420 ret = 1;
421
422 free(path);
423
424 return ret;
425 }
426
427 static bool isuint(const char *str)
428 {
429 for (; *str && isdigit(*str); str++)
430 ;
431
432 return *str == '\0';
433 }
434
435 bool chip_path_lookup(const char *id, char **path_ptr)
436 {
437 char *path;
438
439 if (isuint(id)) {
440 /* by number */
441 if (asprintf(&path, "/dev/gpiochip%s", id) < 0)
442 return false;
443 } else if (strchr(id, '/')) {
444 /* by path */
445 if (asprintf(&path, "%s", id) < 0)
446 return false;
447 } else {
448 /* by device name */
449 if (asprintf(&path, "/dev/%s", id) < 0)
450 return false;
451 }
452
453 if (!gpiod_is_gpiochip_device(path)) {
454 free(path);
455 return false;
456 }
457
458 *path_ptr = path;
459
460 return true;
461 }
462
463 int chip_paths(const char *id, char ***paths_ptr)
464 {
465 char **paths;
466 char *path;
467
468 if (id == NULL)
469 return all_chip_paths(paths_ptr);
470
471 if (!chip_path_lookup(id, &path))
472 return 0;
473
474 paths = malloc(sizeof(*paths));
475 if (paths == NULL)
476 die("out of memory");
477
478 paths[0] = path;
479 *paths_ptr = paths;
480
481 return 1;
482 }
483
484 int all_chip_paths(char ***paths_ptr)
485 {
486 int i, j, num_chips, ret = 0;
487 struct dirent **entries;
488 char **paths;
489
490 num_chips = scandir("/dev/", &entries, chip_dir_filter, versionsort);
491 if (num_chips < 0)
492 die_perror("unable to scan /dev");
493
494 paths = calloc(num_chips, sizeof(*paths));
495 if (paths == NULL)
496 die("out of memory");
497
498 for (i = 0; i < num_chips; i++) {
499 if (asprintf(&paths[i], "/dev/%s", entries[i]->d_name) < 0) {
500 for (j = 0; j < i; j++)
501 free(paths[j]);
502
503 free(paths);
504 return 0;
505 }
506 }
507
508 *paths_ptr = paths;
509 ret = num_chips;
510
511 for (i = 0; i < num_chips; i++)
512 free(entries[i]);
513
514 free(entries);
515 return ret;
516 }
517
518 static bool resolve_line(struct line_resolver *resolver,
519 struct gpiod_line_info *info, int chip_num)
520 {
521 struct resolved_line *line;
522 bool resolved = false;
523 unsigned int offset;
524 const char *name;
525 int i;
526
527 offset = gpiod_line_info_get_offset(info);
528 for (i = 0; i < resolver->num_lines; i++) {
529 line = &resolver->lines[i];
530 /* already resolved by offset? */
531 if (line->resolved && (line->offset == offset) &&
532 (line->chip_num == chip_num)) {
533 line->info = info;
534 resolver->num_found++;
535 resolved = true;
536 }
537 if (line->resolved && !resolver->strict)
538 continue;
539
540 /* else resolve by name */
541 name = gpiod_line_info_get_name(info);
542 if (name && (strcmp(line->id, name) == 0)) {
543 if (resolver->strict && line->resolved)
544 die("line '%s' is not unique", line->id);
545 line->offset = offset;
546 line->info = info;
547 line->chip_num = resolver->num_chips;
548 line->resolved = true;
549 resolver->num_found++;
550 resolved = true;
551 }
552 }
553
554 return resolved;
555 }
556
557 /*
558 * check for lines that can be identified by offset
559 *
560 * This only applies to the first chip, as otherwise the lines must be
561 * identified by name.
562 */
563 bool resolve_lines_by_offset(struct line_resolver *resolver,
564 unsigned int num_lines)
565 {
566 struct resolved_line *line;
567 bool used = false;
568 int i;
569
570 for (i = 0; i < resolver->num_lines; i++) {
571 line = &resolver->lines[i];
572 if ((line->id_as_offset != -1) &&
573 (line->id_as_offset < (int)num_lines)) {
574 line->chip_num = 0;
575 line->offset = line->id_as_offset;
576 line->resolved = true;
577 used = true;
578 }
579 }
580 return used;
581 }
582
583 bool resolve_done(struct line_resolver *resolver)
584 {
585 return (!resolver->strict &&
586 resolver->num_found >= resolver->num_lines);
587 }
588
589 struct line_resolver *resolver_init(int num_lines, char **lines, int num_chips,
590 bool strict, bool by_name)
591 {
592 struct line_resolver *resolver;
593 struct resolved_line *line;
594 size_t resolver_size;
595 int i;
596
597 resolver_size = sizeof(*resolver) + num_lines * sizeof(*line);
598 resolver = malloc(resolver_size);
599 if (resolver == NULL)
600 die("out of memory");
601
602 memset(resolver, 0, resolver_size);
603
604 resolver->chips = calloc(num_chips, sizeof(struct resolved_chip));
605 if (resolver->chips == NULL)
606 die("out of memory");
607 memset(resolver->chips, 0, num_chips * sizeof(struct resolved_chip));
608
609 resolver->num_lines = num_lines;
610 resolver->strict = strict;
611 for (i = 0; i < num_lines; i++) {
612 line = &resolver->lines[i];
613 line->id = lines[i];
614 line->id_as_offset = by_name ? -1 : parse_uint(lines[i]);
615 line->chip_num = -1;
616 }
617
618 return resolver;
619 }
620
621 struct line_resolver *resolve_lines(int num_lines, char **lines,
622 const char *chip_id, bool strict,
623 bool by_name)
624 {
625 struct gpiod_chip_info *chip_info;
626 struct gpiod_line_info *line_info;
627 struct line_resolver *resolver;
628 int num_chips, i, offset;
629 struct gpiod_chip *chip;
630 bool chip_used;
631 char **paths;
632
633 if (chip_id == NULL)
634 by_name = true;
635
636 num_chips = chip_paths(chip_id, &paths);
637 if (chip_id && (num_chips == 0))
638 die("cannot find GPIO chip character device '%s'", chip_id);
639
640 resolver = resolver_init(num_lines, lines, num_chips, strict, by_name);
641
642 for (i = 0; (i < num_chips) && !resolve_done(resolver); i++) {
643 chip_used = false;
644 chip = gpiod_chip_open(paths[i]);
645 if (!chip) {
646 if ((errno == EACCES) && (chip_id == NULL)) {
647 free(paths[i]);
648 continue;
649 }
650
651 die_perror("unable to open chip '%s'", paths[i]);
652 }
653
654 chip_info = gpiod_chip_get_info(chip);
655 if (!chip_info)
656 die_perror("unable to get info for '%s'", paths[i]);
657
658 num_lines = gpiod_chip_info_get_num_lines(chip_info);
659
660 if (i == 0 && chip_id && !by_name)
661 chip_used = resolve_lines_by_offset(resolver, num_lines);
662
663 for (offset = 0;
664 (offset < num_lines) && !resolve_done(resolver);
665 offset++) {
666 line_info = gpiod_chip_get_line_info(chip, offset);
667 if (!line_info)
668 die_perror("unable to read the info for line %d from %s",
669 offset,
670 gpiod_chip_info_get_name(chip_info));
671
672 if (resolve_line(resolver, line_info, i))
673 chip_used = true;
674 else
675 gpiod_line_info_free(line_info);
676
677 }
678
679 gpiod_chip_close(chip);
680
681 if (chip_used) {
682 resolver->chips[resolver->num_chips].info = chip_info;
683 resolver->chips[resolver->num_chips].path = paths[i];
684 resolver->num_chips++;
685 } else {
686 gpiod_chip_info_free(chip_info);
687 free(paths[i]);
688 }
689 }
690 free(paths);
691
692 return resolver;
693 }
694
695 void validate_resolution(struct line_resolver *resolver, const char *chip_id)
696 {
697 struct resolved_line *line, *prev;
698 bool valid = true;
699 int i, j;
700
701 for (i = 0; i < resolver->num_lines; i++) {
702 line = &resolver->lines[i];
703 if (line->resolved) {
704 for (j = 0; j < i; j++) {
705 prev = &resolver->lines[j];
706 if (prev->resolved &&
707 (prev->chip_num == line->chip_num) &&
708 (prev->offset == line->offset)) {
709 print_error("lines '%s' and '%s' are the same line",
710 prev->id, line->id);
711 valid = false;
712 break;
713 }
714 }
715 continue;
716 }
717 valid = false;
718 if (chip_id && line->id_as_offset != -1)
719 print_error("offset %s is out of range on chip '%s'",
720 line->id, chip_id);
721 else
722 print_error("cannot find line '%s'", line->id);
723 }
724 if (!valid)
725 exit(EXIT_FAILURE);
726 }
727
728 void free_line_resolver(struct line_resolver *resolver)
729 {
730 int i;
731
732 if (!resolver)
733 return;
734
735 for (i = 0; i < resolver->num_lines; i++)
736 gpiod_line_info_free(resolver->lines[i].info);
737
738 for (i = 0; i < resolver->num_chips; i++) {
739 gpiod_chip_info_free(resolver->chips[i].info);
740 free(resolver->chips[i].path);
741 }
742
743 free(resolver->chips);
744 free(resolver);
745 }
746
747 int get_line_offsets_and_values(struct line_resolver *resolver, int chip_num,
748 unsigned int *offsets,
749 enum gpiod_line_value *values)
750 {
751 struct resolved_line *line;
752 int i, num_lines = 0;
753
754 for (i = 0; i < resolver->num_lines; i++) {
755 line = &resolver->lines[i];
756 if (line->chip_num == chip_num) {
757 offsets[num_lines] = line->offset;
758 if (values)
759 values[num_lines] = line->value;
760
761 num_lines++;
762 }
763 }
764
765 return num_lines;
766 }
767
768 const char *get_chip_name(struct line_resolver *resolver, int chip_num)
769 {
770 return gpiod_chip_info_get_name(resolver->chips[chip_num].info);
771 }
772
773 const char *get_line_name(struct line_resolver *resolver, int chip_num,
774 unsigned int offset)
775 {
776 struct resolved_line *line;
777 int i;
778
779 for (i = 0; i < resolver->num_lines; i++) {
780 line = &resolver->lines[i];
781 if (line->info && (line->offset == offset) &&
782 (line->chip_num == chip_num))
783 return gpiod_line_info_get_name(
784 resolver->lines[i].info);
785 }
786
787 return 0;
788 }
789
790 void set_line_values(struct line_resolver *resolver, int chip_num,
791 enum gpiod_line_value *values)
792 {
793 int i, j;
794
795 for (i = 0, j = 0; i < resolver->num_lines; i++) {
796 if (resolver->lines[i].chip_num == chip_num) {
797 resolver->lines[i].value = values[j];
798 j++;
799 }
800 }
801 }