Debian Codegraph @

tools/gpioinfo.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 #include <getopt.h>
6 #include <gpiod.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "tools-common.h"
13
14 struct config {
15 bool by_name;
16 bool strict;
17 bool unquoted_strings;
18 const char *chip_id;
19 };
20
21 static void print_help(void)
22 {
23 printf("Usage: %s [OPTIONS] [line]...\n", get_prog_name());
24 printf("\n");
25 printf("Print information about GPIO lines.\n");
26 printf("\n");
27 printf("Lines are specified by name, or optionally by offset if the chip option\n");
28 printf("is provided.\n");
29 printf("\n");
30 printf("If no lines are specified then all lines are displayed.\n");
31 printf("\n");
32 printf("Options:\n");
33 printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n");
34 printf(" -c, --chip <chip>\trestrict scope to a particular chip\n");
35 printf(" -h, --help\t\tdisplay this help and exit\n");
36 printf(" -s, --strict\t\tcheck all lines - don't assume line names are unique\n");
37 printf(" --unquoted\tdon't quote line or consumer names\n");
38 printf(" -v, --version\t\toutput version information and exit\n");
39 print_chip_help();
40 }
41
42 static int parse_config(int argc, char **argv, struct config *cfg)
43 {
44 static const struct option longopts[] = {
45 { "by-name", no_argument, NULL, 'B' },
46 { "chip", required_argument, NULL, 'c' },
47 { "help", no_argument, NULL, 'h' },
48 { "strict", no_argument, NULL, 's' },
49 { "unquoted", no_argument, NULL, 'Q' },
50 { "version", no_argument, NULL, 'v' },
51 { GETOPT_NULL_LONGOPT },
52 };
53
54
55 static const char *const shortopts = "+c:hsv";
56
57 int opti, optc;
58
59 memset(cfg, 0, sizeof(*cfg));
60
61 for (;;) {
62 optc = getopt_long(argc, argv, shortopts, longopts, &opti);
63 if (optc < 0)
64 break;
65
66 switch (optc) {
67 case 'B':
68 cfg->by_name = true;
69 break;
70 case 'c':
71 cfg->chip_id = optarg;
72 break;
73 case 's':
74 cfg->strict = true;
75 break;
76 case 'h':
77 print_help();
78 exit(EXIT_SUCCESS);
79 case 'Q':
80 cfg->unquoted_strings = true;
81 break;
82 case 'v':
83 print_version();
84 exit(EXIT_SUCCESS);
85 case '?':
86 die("try %s --help", get_prog_name());
87 case 0:
88 break;
89 default:
90 abort();
91 }
92 }
93
94 return optind;
95 }
96
97 /*
98 * Minimal version similar to tools-common that indicates if a line should be
99 * printed rather than storing details into the resolver.
100 * Does not die on non-unique lines.
101 */
102 static bool resolve_line(struct line_resolver *resolver,
103 struct gpiod_line_info *info, int chip_num)
104 {
105 struct resolved_line *line;
106 bool resolved = false;
107 unsigned int offset;
108 const char *name;
109 int i;
110
111 offset = gpiod_line_info_get_offset(info);
112
113 for (i = 0; i < resolver->num_lines; i++) {
114 line = &resolver->lines[i];
115
116 /* already resolved by offset? */
117 if (line->resolved && (line->offset == offset) &&
118 (line->chip_num == chip_num)) {
119 resolved = true;
120 }
121
122 if (line->resolved && !resolver->strict)
123 continue;
124
125 /* else resolve by name */
126 name = gpiod_line_info_get_name(info);
127 if (name && (strcmp(line->id, name) == 0)) {
128 line->resolved = true;
129 line->offset = offset;
130 line->chip_num = chip_num;
131 resolved = true;
132 }
133 }
134
135 return resolved;
136 }
137
138 static void print_line_info(struct gpiod_line_info *info, bool unquoted_strings)
139 {
140 bool unquoted_name = unquoted_strings;
141 char quoted_name[17];
142 const char *name;
143 int len;
144
145 name = gpiod_line_info_get_name(info);
146 if (!name) {
147 name = "unnamed";
148 unquoted_name = true;
149 }
150
151 if (unquoted_name) {
152 printf("%-16s\t", name);
153 } else {
154 len = strlen(name);
155 if (len <= 14) {
156 quoted_name[0] = '"';
157 memcpy(&quoted_name[1], name, len);
158 quoted_name[len + 1] = '"';
159 quoted_name[len + 2] = '\0';
160 printf("%-16s\t", quoted_name);
161 } else {
162 printf("\"%s\"\t", name);
163 }
164 }
165
166 print_line_attributes(info, unquoted_strings);
167 }
168
169 /*
170 * based on resolve_lines, but prints lines immediately rather than collecting
171 * details in the resolver.
172 */
173 static void list_lines(struct line_resolver *resolver, struct gpiod_chip *chip,
174 int chip_num, struct config *cfg)
175 {
176 struct gpiod_chip_info *chip_info;
177 struct gpiod_line_info *info;
178 int offset, num_lines;
179
180 chip_info = gpiod_chip_get_info(chip);
181 if (!chip_info)
182 die_perror("unable to read info from chip %s",
183 gpiod_chip_get_path(chip));
184
185 num_lines = gpiod_chip_info_get_num_lines(chip_info);
186
187 if ((chip_num == 0) && (cfg->chip_id && !cfg->by_name))
188 resolve_lines_by_offset(resolver, num_lines);
189
190 for (offset = 0; ((offset < num_lines) &&
191 !(resolver->num_lines && resolve_done(resolver)));
192 offset++) {
193 info = gpiod_chip_get_line_info(chip, offset);
194 if (!info)
195 die_perror("unable to read info for line %d from %s",
196 offset, gpiod_chip_info_get_name(chip_info));
197
198 if (resolver->num_lines &&
199 !resolve_line(resolver, info, chip_num)) {
200 gpiod_line_info_free(info);
201 continue;
202 }
203
204 if (resolver->num_lines) {
205 printf("%s %u", gpiod_chip_info_get_name(chip_info),
206 offset);
207 } else {
208 if (offset == 0)
209 printf("%s - %u lines:\n",
210 gpiod_chip_info_get_name(chip_info),
211 num_lines);
212
213 printf("\tline %3u:", offset);
214 }
215
216 fputc('\t', stdout);
217 print_line_info(info, cfg->unquoted_strings);
218 fputc('\n', stdout);
219 gpiod_line_info_free(info);
220 resolver->num_found++;
221 }
222
223 gpiod_chip_info_free(chip_info);
224 }
225
226 int main(int argc, char **argv)
227 {
228 struct line_resolver *resolver = NULL;
229 int num_chips, i, ret = EXIT_SUCCESS;
230 struct gpiod_chip *chip;
231 struct config cfg;
232 char **paths;
233
234 set_prog_name(argv[0]);
235 i = parse_config(argc, argv, &cfg);
236 argc -= i;
237 argv += i;
238
239 if (!cfg.chip_id)
240 cfg.by_name = true;
241
242 num_chips = chip_paths(cfg.chip_id, &paths);
243 if (cfg.chip_id && (num_chips == 0))
244 die("cannot find GPIO chip character device '%s'", cfg.chip_id);
245
246 resolver = resolver_init(argc, argv, num_chips, cfg.strict,
247 cfg.by_name);
248
249 for (i = 0; i < num_chips; i++) {
250 chip = gpiod_chip_open(paths[i]);
251 if (chip) {
252 list_lines(resolver, chip, i, &cfg);
253 gpiod_chip_close(chip);
254 } else {
255 print_perror("unable to open chip '%s'", paths[i]);
256
257 if (cfg.chip_id)
258 return EXIT_FAILURE;
259
260 ret = EXIT_FAILURE;
261 }
262 free(paths[i]);
263 }
264 free(paths);
265
266 validate_resolution(resolver, cfg.chip_id);
267 if (argc && resolver->num_found != argc)
268 ret = EXIT_FAILURE;
269 free_line_resolver(resolver);
270
271 return ret;
272 }