| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <getopt.h> |
| 6 |
#include <gpiod.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "tools-common.h" |
| 12 |
|
| 13 |
static void print_help(void) |
| 14 |
{ |
| 15 |
printf("Usage: %s [OPTIONS] [chip]...\n", get_prog_name()); |
| 16 |
printf("\n"); |
| 17 |
printf("List GPIO chips, print their labels and number of GPIO lines.\n"); |
| 18 |
printf("\n"); |
| 19 |
printf("Chips may be identified by number, name, or path.\n"); |
| 20 |
printf("e.g. '0', 'gpiochip0', and '/dev/gpiochip0' all refer to the same chip.\n"); |
| 21 |
printf("\n"); |
| 22 |
printf("If no chips are specified then all chips are listed.\n"); |
| 23 |
printf("\n"); |
| 24 |
printf("Options:\n"); |
| 25 |
printf(" -h, --help\t\tdisplay this help and exit\n"); |
| 26 |
printf(" -v, --version\t\toutput version information and exit\n"); |
| 27 |
} |
| 28 |
|
| 29 |
static int parse_config(int argc, char **argv) |
| 30 |
{ |
| 31 |
static const struct option longopts[] = { |
| 32 |
{ "help", no_argument, NULL, 'h' }, |
| 33 |
{ "version", no_argument, NULL, 'v' }, |
| 34 |
{ GETOPT_NULL_LONGOPT }, |
| 35 |
}; |
| 36 |
|
| 37 |
static const char *const shortopts = "+hv"; |
| 38 |
|
| 39 |
int optc, opti; |
| 40 |
|
| 41 |
for (;;) { |
| 42 |
optc = getopt_long(argc, argv, shortopts, longopts, &opti); |
| 43 |
if (optc < 0) |
| 44 |
break; |
| 45 |
|
| 46 |
switch (optc) { |
| 47 |
case 'h': |
| 48 |
print_help(); |
| 49 |
exit(EXIT_SUCCESS); |
| 50 |
case 'v': |
| 51 |
print_version(); |
| 52 |
exit(EXIT_SUCCESS); |
| 53 |
case '?': |
| 54 |
die("try %s --help", get_prog_name()); |
| 55 |
default: |
| 56 |
abort(); |
| 57 |
} |
| 58 |
} |
| 59 |
return optind; |
| 60 |
} |
| 61 |
|
| 62 |
static int print_chip_info(const char *path) |
| 63 |
{ |
| 64 |
struct gpiod_chip_info *info; |
| 65 |
struct gpiod_chip *chip; |
| 66 |
|
| 67 |
chip = gpiod_chip_open(path); |
| 68 |
if (!chip) { |
| 69 |
print_perror("unable to open chip '%s'", path); |
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
info = gpiod_chip_get_info(chip); |
| 74 |
if (!info) |
| 75 |
die_perror("unable to read info for '%s'", path); |
| 76 |
|
| 77 |
printf("%s [%s] (%zu lines)\n", gpiod_chip_info_get_name(info), |
| 78 |
gpiod_chip_info_get_label(info), |
| 79 |
gpiod_chip_info_get_num_lines(info)); |
| 80 |
|
| 81 |
gpiod_chip_info_free(info); |
| 82 |
gpiod_chip_close(chip); |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
int main(int argc, char **argv) |
| 88 |
{ |
| 89 |
int num_chips, i, ret = EXIT_SUCCESS; |
| 90 |
char **paths, *path; |
| 91 |
|
| 92 |
set_prog_name(argv[0]); |
| 93 |
i = parse_config(argc, argv); |
| 94 |
argc -= i; |
| 95 |
argv += i; |
| 96 |
|
| 97 |
if (argc == 0) { |
| 98 |
num_chips = all_chip_paths(&paths); |
| 99 |
for (i = 0; i < num_chips; i++) { |
| 100 |
if (print_chip_info(paths[i])) |
| 101 |
ret = EXIT_FAILURE; |
| 102 |
|
| 103 |
free(paths[i]); |
| 104 |
} |
| 105 |
|
| 106 |
free(paths); |
| 107 |
} |
| 108 |
|
| 109 |
for (i = 0; i < argc; i++) { |
| 110 |
if (chip_path_lookup(argv[i], &path)) { |
| 111 |
if (print_chip_info(path)) |
| 112 |
ret = EXIT_FAILURE; |
| 113 |
|
| 114 |
free(path); |
| 115 |
} else { |
| 116 |
print_error( |
| 117 |
"cannot find GPIO chip character device '%s'", |
| 118 |
argv[i]); |
| 119 |
ret = EXIT_FAILURE; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return ret; |
| 124 |
} |