Debian Codegraph @

tools/gpioset.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 <ctype.h>
6 #include <gpiod.h>
7 #include <getopt.h>
8 #include <limits.h>
9 #include <poll.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #ifdef GPIOSET_INTERACTIVE
15 #include <editline/readline.h>
16 #endif
17
18 #include "tools-common.h"
19
20 struct config {
21 bool active_low;
22 bool banner;
23 bool by_name;
24 bool daemonize;
25 bool interactive;
26 bool strict;
27 bool unquoted;
28 enum gpiod_line_bias bias;
29 enum gpiod_line_drive drive;
30 int toggles;
31 unsigned long long *toggle_periods;
32 unsigned long long hold_period_us;
33 const char *chip_id;
34 const char *consumer;
35 };
36
37 static void print_help(void)
38 {
39 printf("Usage: %s [OPTIONS] <line=value>...\n", get_prog_name());
40 printf("\n");
41 printf("Set values of GPIO lines.\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("Values may be '1' or '0', or equivalently 'active'/'inactive' or 'on'/'off'.\n");
46 printf("\n");
47 printf("The line output state is maintained until the process exits, but after that\n");
48 printf("is not guaranteed.\n");
49 printf("\n");
50 printf("Options:\n");
51 printf(" --banner\t\tdisplay a banner on successful startup\n");
52 print_bias_help();
53 printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n");
54 printf(" -c, --chip <chip>\trestrict scope to a particular chip\n");
55 printf(" -C, --consumer <name>\tconsumer name applied to requested lines (default is 'gpioset')\n");
56 printf(" -d, --drive <drive>\tspecify the line drive mode\n");
57 printf("\t\t\tPossible values: 'push-pull', 'open-drain', 'open-source'.\n");
58 printf("\t\t\t(default is 'push-pull')\n");
59 printf(" -h, --help\t\tdisplay this help and exit\n");
60 #ifdef GPIOSET_INTERACTIVE
61 printf(" -i, --interactive\tset the lines then wait for additional set commands\n");
62 printf("\t\t\tUse the 'help' command at the interactive prompt to get help\n");
63 printf("\t\t\tfor the supported commands.\n");
64 #endif
65 printf(" -l, --active-low\ttreat the line as active low\n");
66 printf(" -p, --hold-period <period>\n");
67 printf("\t\t\tthe minimum time period to hold lines at the requested values\n");
68 printf(" -s, --strict\t\tabort if requested line names are not unique\n");
69 printf(" -t, --toggle <period>[,period]...\n");
70 printf("\t\t\ttoggle the line(s) after the specified period(s)\n");
71 printf("\t\t\tIf the last period is 0 then gpioset exits else the sequence repeats.\n");
72 printf(" --unquoted\tdon't quote line names\n");
73 printf(" -v, --version\t\toutput version information and exit\n");
74 printf(" -z, --daemonize\tset values then detach from the controlling terminal\n");
75 print_chip_help();
76 print_period_help();
77 printf("\n");
78 printf("*Note*\n");
79 printf(" It should not be assumed that a line will retain its state after gpioset exits.\n");
80 printf(" When a process exits, any GPIO lines it has requested are automatically released.\n");
81 printf(" Once released, the state of a line may be modified by the kernel or another process.\n");
82 printf(" To guarantee the requested value, by default gpioset does not exit.\n");
83 }
84
85 static int parse_drive_or_die(const char *option)
86 {
87 if (strcmp(option, "open-drain") == 0)
88 return GPIOD_LINE_DRIVE_OPEN_DRAIN;
89 if (strcmp(option, "open-source") == 0)
90 return GPIOD_LINE_DRIVE_OPEN_SOURCE;
91 if (strcmp(option, "push-pull") != 0)
92 die("invalid drive: %s", option);
93
94 return 0;
95 }
96
97 static int parse_periods_or_die(char *option, unsigned long long **periods)
98 {
99 int i, num_periods = 1;
100 unsigned long long *pp;
101 char *end;
102
103 for (i = 0; option[i] != '\0'; i++)
104 if (option[i] == ',')
105 num_periods++;
106
107 pp = calloc(num_periods, sizeof(*pp));
108 if (pp == NULL)
109 die("out of memory");
110
111 for (i = 0; i < num_periods - 1; i++) {
112 for (end = option; *end != ','; end++)
113 ;
114
115 *end = '\0';
116 pp[i] = parse_period_or_die(option);
117 option = end + 1;
118 }
119 pp[i] = parse_period_or_die(option);
120 *periods = pp;
121
122 return num_periods;
123 }
124
125 static int parse_config(int argc, char **argv, struct config *cfg)
126 {
127 static const struct option longopts[] = {
128 { "active-low", no_argument, NULL, 'l' },
129 { "banner", no_argument, NULL, '-'},
130 { "bias", required_argument, NULL, 'b' },
131 { "by-name", no_argument, NULL, 'B' },
132 { "chip", required_argument, NULL, 'c' },
133 { "consumer", required_argument, NULL, 'C' },
134 { "daemonize", no_argument, NULL, 'z' },
135 { "drive", required_argument, NULL, 'd' },
136 { "help", no_argument, NULL, 'h' },
137 { "hold-period", required_argument, NULL, 'p' },
138 #ifdef GPIOSET_INTERACTIVE
139 { "interactive", no_argument, NULL, 'i' },
140 #endif
141 { "strict", no_argument, NULL, 's' },
142 { "toggle", required_argument, NULL, 't' },
143 { "unquoted", no_argument, NULL, 'Q' },
144 { "version", no_argument, NULL, 'v' },
145 { GETOPT_NULL_LONGOPT },
146 };
147
148 #ifdef GPIOSET_INTERACTIVE
149 static const char *const shortopts = "+b:c:C:d:hilp:st:vz";
150 #else
151 static const char *const shortopts = "+b:c:C:d:hlp:st:vz";
152 #endif
153
154 int opti, optc;
155
156 memset(cfg, 0, sizeof(*cfg));
157 cfg->consumer = "gpioset";
158
159 for (;;) {
160 optc = getopt_long(argc, argv, shortopts, longopts, &opti);
161 if (optc < 0)
162 break;
163
164 switch (optc) {
165 case '-':
166 cfg->banner = true;
167 break;
168 case 'b':
169 cfg->bias = parse_bias_or_die(optarg);
170 break;
171 case 'B':
172 cfg->by_name = true;
173 break;
174 case 'c':
175 cfg->chip_id = optarg;
176 break;
177 case 'C':
178 cfg->consumer = optarg;
179 break;
180 case 'd':
181 cfg->drive = parse_drive_or_die(optarg);
182 break;
183 #ifdef GPIOSET_INTERACTIVE
184 case 'i':
185 cfg->interactive = true;
186 break;
187 #endif
188 case 'l':
189 cfg->active_low = true;
190 break;
191 case 'p':
192 cfg->hold_period_us = parse_period_or_die(optarg);
193 break;
194 case 'Q':
195 cfg->unquoted = true;
196 break;
197 case 's':
198 cfg->strict = true;
199 break;
200 case 't':
201 cfg->toggles = parse_periods_or_die(optarg,
202 &cfg->toggle_periods);
203 break;
204 case 'z':
205 cfg->daemonize = true;
206 break;
207 case 'h':
208 print_help();
209 exit(EXIT_SUCCESS);
210 case 'v':
211 print_version();
212 exit(EXIT_SUCCESS);
213 case '?':
214 die("try %s --help", get_prog_name());
215 case 0:
216 break;
217 default:
218 abort();
219 }
220 }
221
222 #ifdef GPIOSET_INTERACTIVE
223 if (cfg->toggles && cfg->interactive)
224 die("can't combine interactive with toggle");
225 #endif
226
227 return optind;
228 }
229
230 static enum gpiod_line_value parse_value(const char *option)
231 {
232 if (strcmp(option, "0") == 0)
233 return GPIOD_LINE_VALUE_INACTIVE;
234 if (strcmp(option, "1") == 0)
235 return GPIOD_LINE_VALUE_ACTIVE;
236 if (strcmp(option, "inactive") == 0)
237 return GPIOD_LINE_VALUE_INACTIVE;
238 if (strcmp(option, "active") == 0)
239 return GPIOD_LINE_VALUE_ACTIVE;
240 if (strcmp(option, "off") == 0)
241 return GPIOD_LINE_VALUE_INACTIVE;
242 if (strcmp(option, "on") == 0)
243 return GPIOD_LINE_VALUE_ACTIVE;
244 if (strcmp(option, "false") == 0)
245 return GPIOD_LINE_VALUE_INACTIVE;
246 if (strcmp(option, "true") == 0)
247 return GPIOD_LINE_VALUE_ACTIVE;
248
249 return GPIOD_LINE_VALUE_ERROR;
250 }
251
252 /*
253 * Parse line id and values from lvs into lines and values.
254 *
255 * Accepted forms:
256 * 'line=value'
257 * '"line"=value'
258 *
259 * If line id is quoted then it is returned unquoted.
260 */
261 static bool parse_line_values(int num_lines, char **lvs, char **lines,
262 enum gpiod_line_value *values, bool interactive)
263 {
264 char *value, *line;
265 int i;
266
267 for (i = 0; i < num_lines; i++) {
268 line = lvs[i];
269
270 if (*line != '"') {
271 value = strchr(line, '=');
272 } else {
273 line++;
274 value = strstr(line, "\"=");
275 if (value) {
276 *value = '\0';
277 value++;
278 }
279 }
280
281 if (!value) {
282 if (interactive)
283 printf("invalid line value: '%s'\n", lvs[i]);
284 else
285 print_error("invalid line value: '%s'", lvs[i]);
286
287 return false;
288 }
289
290 *value = '\0';
291 value++;
292 values[i] = parse_value(value);
293
294 if (values[i] == GPIOD_LINE_VALUE_ERROR) {
295 if (interactive)
296 printf("invalid line value: '%s'\n", value);
297 else
298 print_error("invalid line value: '%s'", value);
299
300 return false;
301 }
302
303 lines[i] = line;
304 }
305
306 return true;
307 }
308
309 /*
310 * Parse line id and values from lvs into lines and values, or die trying.
311 */
312 static void parse_line_values_or_die(int num_lines, char **lvs, char **lines,
313 enum gpiod_line_value *values)
314 {
315 if (!parse_line_values(num_lines, lvs, lines, values, false))
316 exit(EXIT_FAILURE);
317 }
318
319 static void print_banner(int num_lines, char **lines)
320 {
321 int i;
322
323 if (num_lines > 1) {
324 printf("Setting lines ");
325
326 for (i = 0; i < num_lines - 1; i++)
327 printf("'%s', ", lines[i]);
328
329 printf("and '%s'...\n", lines[i]);
330 } else {
331 printf("Setting line '%s'...\n", lines[0]);
332 }
333 fflush(stdout);
334 }
335
336 static void wait_fd(int fd)
337 {
338 struct pollfd pfd;
339
340 pfd.fd = fd;
341 pfd.events = POLLERR;
342
343 if (poll(&pfd, 1, -1) < 0)
344 die_perror("error waiting on request");
345 }
346
347 /*
348 * Apply values from the resolver to the requests.
349 * offset and values are scratch pads for working.
350 */
351 static void apply_values(struct gpiod_line_request **requests,
352 struct line_resolver *resolver, unsigned int *offsets,
353 enum gpiod_line_value *values)
354 {
355 int i;
356
357 for (i = 0; i < resolver->num_chips; i++) {
358 get_line_offsets_and_values(resolver, i, offsets, values);
359 if (gpiod_line_request_set_values(requests[i], values))
360 print_perror("unable to set values on '%s'",
361 get_chip_name(resolver, i));
362 }
363 }
364
365 /* Toggle the values of all lines in the resolver */
366 static void toggle_all_lines(struct line_resolver *resolver)
367 {
368 int i;
369
370 for (i = 0; i < resolver->num_lines; i++)
371 resolver->lines[i].value = !resolver->lines[i].value;
372 }
373
374 /*
375 * Toggle the resolved lines as specified by the toggle_periods,
376 * and apply the values to the requests.
377 * offset and values are scratch pads for working.
378 */
379 static void toggle_sequence(int toggles, unsigned long long *toggle_periods,
380 struct gpiod_line_request **requests,
381 struct line_resolver *resolver,
382 unsigned int *offsets,
383 enum gpiod_line_value *values)
384 {
385 int i = 0;
386
387 if ((toggles == 1) && (toggle_periods[0] == 0))
388 return;
389
390 for (;;) {
391 sleep_us(toggle_periods[i]);
392 toggle_all_lines(resolver);
393 apply_values(requests, resolver, offsets, values);
394
395 i++;
396 if ((i == toggles - 1) && (toggle_periods[i] == 0))
397 return;
398
399 if (i == toggles)
400 i = 0;
401 }
402 }
403
404 #ifdef GPIOSET_INTERACTIVE
405
406 /*
407 * Parse line id from words into lines.
408 *
409 * If line id is quoted then it is returned unquoted.
410 */
411 static bool parse_line_ids(int num_lines, char **words, char **lines)
412 {
413 int i, len;
414 char *line;
415
416 for (i = 0; i < num_lines; i++) {
417 line = words[i];
418 if (*line == '"') {
419 line++;
420 len = strlen(line);
421 if ((len == 0) || line[len - 1] != '"') {
422 printf("invalid line id: '%s'\n", words[i]);
423 return false;
424 }
425 line[len - 1] = '\0';
426 }
427 lines[i] = line;
428 }
429
430 return true;
431 }
432
433 /*
434 * Set the values in the resolver for the line values specified by
435 * the remaining parameters.
436 */
437 static void set_line_values_subset(struct line_resolver *resolver,
438 int num_lines, char **lines,
439 enum gpiod_line_value *values)
440 {
441 int l, i;
442
443 for (l = 0; l < num_lines; l++) {
444 for (i = 0; i < resolver->num_lines; i++) {
445 if (strcmp(lines[l], resolver->lines[i].id) == 0) {
446 resolver->lines[i].value = values[l];
447 break;
448 }
449 }
450 }
451 }
452
453 static void print_all_line_values(struct line_resolver *resolver, bool unquoted)
454 {
455 char *fmt = unquoted ? "%s=%s " : "\"%s\"=%s ";
456 int i;
457
458 for (i = 0; i < resolver->num_lines; i++) {
459 if (i == resolver->num_lines - 1)
460 fmt = unquoted ? "%s=%s\n" : "\"%s\"=%s\n";
461
462 printf(fmt, resolver->lines[i].id,
463 resolver->lines[i].value ? "active" : "inactive");
464 }
465 }
466
467 /*
468 * Print the resovler line values for a subset of lines, specified by
469 * num_lines and lines.
470 */
471 static void print_line_values(struct line_resolver *resolver, int num_lines,
472 char **lines, bool unquoted)
473 {
474 char *fmt = unquoted ? "%s=%s " : "\"%s\"=%s ";
475 struct resolved_line *line;
476 int i, j;
477
478 for (i = 0; i < num_lines; i++) {
479 if (i == num_lines - 1)
480 fmt = unquoted ? "%s=%s\n" : "\"%s\"=%s\n";
481
482 for (j = 0; j < resolver->num_lines; j++) {
483 line = &resolver->lines[j];
484 if (strcmp(lines[i], line->id) == 0) {
485 printf(fmt, line->id,
486 line->value ? "active" : "inactive");
487 break;
488 }
489 }
490 }
491 }
492
493 /*
494 * Toggle a subset of lines, specified by num_lines and lines, in the resolver.
495 */
496 static void toggle_lines(struct line_resolver *resolver, int num_lines,
497 char **lines)
498 {
499 struct resolved_line *line;
500 int i, j;
501
502 for (i = 0; i < num_lines; i++)
503 for (j = 0; j < resolver->num_lines; j++) {
504 line = &resolver->lines[j];
505 if (strcmp(lines[i], line->id) == 0) {
506 line->value = !line->value;
507 break;
508 }
509 }
510 }
511
512 /*
513 * Check that a set of lines, specified by num_lines and lines, are all
514 * resolved lines.
515 */
516 static bool valid_lines(struct line_resolver *resolver, int num_lines,
517 char **lines)
518 {
519 bool ret = true, found;
520 int i, l;
521
522 for (l = 0; l < num_lines; l++) {
523 found = false;
524
525 for (i = 0; i < resolver->num_lines; i++) {
526 if (strcmp(lines[l], resolver->lines[i].id) == 0) {
527 found = true;
528 break;
529 }
530 }
531
532 if (!found) {
533 printf("unknown line: '%s'\n", lines[l]);
534 ret = false;
535 }
536 }
537
538 return ret;
539 }
540
541 static void print_interactive_help(void)
542 {
543 printf("COMMANDS:\n\n");
544 printf(" exit\n");
545 printf(" Exit the program\n");
546 printf(" get [line]...\n");
547 printf(" Display the output values of the given requested lines\n\n");
548 printf(" If no lines are specified then all requested lines are displayed\n\n");
549 printf(" help\n");
550 printf(" Print this help\n\n");
551 printf(" set <line=value>...\n");
552 printf(" Update the output values of the given requested lines\n\n");
553 printf(" sleep <period>\n");
554 printf(" Sleep for the specified period\n\n");
555 printf(" toggle [line]...\n");
556 printf(" Toggle the output values of the given requested lines\n\n");
557 printf(" If no lines are specified then all requested lines are toggled\n\n");
558 }
559
560 /*
561 * Split a line into words, returning the each of the words and the count.
562 *
563 * max_words specifies the max number of words that may be returned in words.
564 *
565 * Any escaping is ignored, on the assumption that the only escaped
566 * character of consequence is '"', and that names won't include quotes.
567 */
568 static int split_words(char *line, int max_words, char **words)
569 {
570 bool in_quotes = false, in_word = false;
571 int num_words = 0;
572
573 for (; (*line != '\0'); line++) {
574 if (!in_word) {
575 if (isspace(*line))
576 continue;
577
578 in_word = true;
579 in_quotes = (*line == '"');
580
581 /* count all words, but only store max_words */
582 if (num_words < max_words)
583 words[num_words] = line;
584 } else {
585 if (in_quotes) {
586 if (*line == '"')
587 in_quotes = false;
588 continue;
589 }
590 if (isspace(*line)) {
591 num_words++;
592 in_word = false;
593 *line = '\0';
594 }
595 }
596 }
597
598 if (in_word)
599 num_words++;
600
601 return num_words;
602 }
603
604 /* check if a line is specified somewhere in the rl_line_buffer */
605 static bool in_line_buffer(const char *id)
606 {
607 char *match = rl_line_buffer;
608 int len = strlen(id);
609
610 while ((match = strstr(match, id))) {
611 if ((match > rl_line_buffer && isspace(match[-1])) &&
612 ((match[len] == '=') || isspace(match[len])))
613 return true;
614
615 match += len;
616 }
617
618 return false;
619 }
620
621 /* context for complete_line_id, so it can provide valid line ids */
622 static struct line_resolver *completion_context;
623
624 /* tab completion helper for line ids */
625 static char *complete_line_id(const char *text, int state)
626 {
627 static int idx, len;
628 const char *id;
629
630 if (!state) {
631 idx = 0;
632 len = strlen(text);
633 }
634
635 while (idx < completion_context->num_lines) {
636 id = completion_context->lines[idx].id;
637 idx++;
638
639 if ((strncmp(id, text, len) == 0) && (!in_line_buffer(id)))
640 return strdup(id);
641 }
642
643 return NULL;
644 }
645
646 /* tab completion helper for line values (just the value component) */
647 static char *complete_value(const char *text, int state)
648 {
649 static const char * const values[] = {
650 "1", "0", "active", "inactive", "on", "off", "true", "false",
651 NULL
652 };
653
654 static int idx, len;
655
656 const char *value;
657
658 if (!state) {
659 idx = 0;
660 len = strlen(text);
661 }
662
663 while ((value = values[idx])) {
664 idx++;
665 if (strncmp(value, text, len) == 0)
666 return strdup(value);
667 }
668
669 return NULL;
670 }
671
672 /* tab completion help for interactive commands */
673 static char *complete_command(const char *text, int state)
674 {
675 static const char * const commands[] = {
676 "get", "set", "toggle", "sleep", "help", "exit", NULL
677 };
678
679 static int idx, len;
680
681 const char *cmd;
682
683 if (!state) {
684 idx = 0;
685 len = strlen(text);
686 }
687
688 while ((cmd = commands[idx])) {
689 idx++;
690 if (strncmp(cmd, text, len) == 0)
691 return strdup(cmd);
692 }
693 return NULL;
694 }
695
696 /* tab completion for interactive command lines */
697 static char **tab_completion(const char *text, int start, int end)
698 {
699 int cmd_start, cmd_end, len;
700 char **matches = NULL;
701
702 rl_attempted_completion_over = true;
703 rl_completion_type = '@';
704 rl_completion_append_character = ' ';
705
706 for (cmd_start = 0;
707 isspace(rl_line_buffer[cmd_start]) && cmd_start < end; cmd_start++)
708 ;
709
710 if (cmd_start == start)
711 matches = rl_completion_matches(text, complete_command);
712
713 for (cmd_end = cmd_start + 1;
714 !isspace(rl_line_buffer[cmd_end]) && cmd_end < end; cmd_end++)
715 ;
716
717 len = cmd_end - cmd_start;
718 if (len == 3 && strncmp("set ", &rl_line_buffer[cmd_start], 4) == 0) {
719 if (rl_line_buffer[start - 1] == '=') {
720 matches = rl_completion_matches(text, complete_value);
721 } else {
722 rl_completion_append_character = '=';
723 matches = rl_completion_matches(text, complete_line_id);
724 }
725 }
726
727 if ((len == 3 && strncmp("get ", &rl_line_buffer[cmd_start], 4) == 0) ||
728 (len == 6 &&
729 strncmp("toggle ", &rl_line_buffer[cmd_start], 7) == 0))
730 matches = rl_completion_matches(text, complete_line_id);
731
732 return matches;
733 }
734
735 #define PROMPT "gpioset> "
736
737 static void interact(struct gpiod_line_request **requests,
738 struct line_resolver *resolver, char **lines,
739 unsigned int *offsets, enum gpiod_line_value *values,
740 bool unquoted)
741 {
742 int num_words, num_lines, max_words, i;
743 char *line, **words, *line_buf;
744 bool done, stdout_is_tty;
745 long long period_us;
746
747 stifle_history(20);
748 rl_attempted_completion_function = tab_completion;
749 rl_basic_word_break_characters = " =\"";
750 completion_context = resolver;
751 stdout_is_tty = isatty(1);
752
753 max_words = resolver->num_lines + 1;
754 words = calloc(max_words, sizeof(*words));
755 if (!words)
756 die("out of memory");
757
758 for (done = false; !done;) {
759 /*
760 * manually print the prompt, as libedit doesn't if stdout
761 * is not a tty. And fflush to ensure the prompt and any
762 * output buffered from the previous command is sent.
763 */
764 if (!stdout_is_tty)
765 printf(PROMPT);
766 fflush(stdout);
767
768 line = readline(PROMPT);
769 if (!line || line[0] == '\0') {
770 free(line);
771 continue;
772 }
773
774 for (i = strlen(line) - 1; (i > 0) && isspace(line[i]); i--)
775 line[i] = '\0';
776
777 line_buf = strdup(line);
778 num_words = split_words(line_buf, max_words, words);
779 if (num_words > max_words) {
780 printf("too many command parameters provided\n");
781 goto cmd_done;
782 }
783 num_lines = num_words - 1;
784 if (strcmp(words[0], "get") == 0) {
785 if (num_lines == 0)
786 print_all_line_values(resolver, unquoted);
787 else if (parse_line_ids(num_lines, &words[1], lines) &&
788 valid_lines(resolver, num_lines, lines))
789 print_line_values(resolver, num_lines, lines,
790 unquoted);
791 goto cmd_ok;
792 }
793 if (strcmp(words[0], "set") == 0) {
794 if (num_lines == 0)
795 printf("at least one GPIO line value must be specified\n");
796 else if (parse_line_values(num_lines, &words[1], lines,
797 values, true) &&
798 valid_lines(resolver, num_lines, lines)) {
799 set_line_values_subset(resolver, num_lines,
800 lines, values);
801 apply_values(requests, resolver, offsets,
802 values);
803 }
804 goto cmd_ok;
805 }
806 if (strcmp(words[0], "toggle") == 0) {
807 if (num_lines == 0)
808 toggle_all_lines(resolver);
809 else if (parse_line_ids(num_lines, &words[1], lines) &&
810 valid_lines(resolver, num_lines, lines))
811 toggle_lines(resolver, num_lines, lines);
812
813 apply_values(requests, resolver, offsets, values);
814 goto cmd_ok;
815 }
816 if (strcmp(words[0], "sleep") == 0) {
817 if (num_lines == 0) {
818 printf("a period must be specified\n");
819 goto cmd_ok;
820 }
821 if (num_lines > 1) {
822 printf("only one period can be specified\n");
823 goto cmd_ok;
824 }
825 period_us = parse_period(words[1]);
826 if (period_us < 0) {
827 printf("invalid period: '%s'\n", words[1]);
828 goto cmd_ok;
829 }
830 sleep_us(period_us);
831 goto cmd_ok;
832 }
833
834 if (strcmp(words[0], "exit") == 0) {
835 done = true;
836 goto cmd_done;
837 }
838
839 if (strcmp(words[0], "help") == 0) {
840 print_interactive_help();
841 goto cmd_done;
842 }
843
844 printf("unknown command: '%s'\n", words[0]);
845 printf("Try the 'help' command\n");
846
847 cmd_ok:
848 for (i = 0; isspace(line[i]); i++)
849 ;
850
851 if ((history_length) == 0 ||
852 (strcmp(history_list()[history_length - 1]->line,
853 &line[i]) != 0))
854 add_history(&line[i]);
855
856 cmd_done:
857 free(line);
858 free(line_buf);
859 }
860 free(words);
861 }
862
863 #endif /* GPIOSET_INTERACTIVE */
864
865 int main(int argc, char **argv)
866 {
867 struct gpiod_line_settings *settings;
868 struct gpiod_request_config *req_cfg;
869 struct gpiod_line_request **requests;
870 struct gpiod_line_config *line_cfg;
871 struct line_resolver *resolver;
872 enum gpiod_line_value *values;
873 struct gpiod_chip *chip;
874 unsigned int *offsets;
875 int i, num_lines, ret;
876 struct config cfg;
877 char **lines;
878
879 set_prog_name(argv[0]);
880 i = parse_config(argc, argv, &cfg);
881 argc -= i;
882 argv += i;
883
884 if (argc < 1)
885 die("at least one GPIO line value must be specified");
886
887 num_lines = argc;
888
889 lines = calloc(num_lines, sizeof(*lines));
890 values = calloc(num_lines, sizeof(*values));
891 if (!lines || !values)
892 die("out of memory");
893
894 parse_line_values_or_die(argc, argv, lines, values);
895
896 settings = gpiod_line_settings_new();
897 if (!settings)
898 die_perror("unable to allocate line settings");
899
900 if (cfg.bias)
901 gpiod_line_settings_set_bias(settings, cfg.bias);
902
903 if (cfg.drive)
904 gpiod_line_settings_set_drive(settings, cfg.drive);
905
906 if (cfg.active_low)
907 gpiod_line_settings_set_active_low(settings, true);
908
909 gpiod_line_settings_set_direction(settings,
910 GPIOD_LINE_DIRECTION_OUTPUT);
911
912 req_cfg = gpiod_request_config_new();
913 if (!req_cfg)
914 die_perror("unable to allocate the request config structure");
915
916 gpiod_request_config_set_consumer(req_cfg, cfg.consumer);
917 resolver = resolve_lines(num_lines, lines, cfg.chip_id, cfg.strict,
918 cfg.by_name);
919 validate_resolution(resolver, cfg.chip_id);
920 for (i = 0; i < num_lines; i++)
921 resolver->lines[i].value = values[i];
922
923 requests = calloc(resolver->num_chips, sizeof(*requests));
924 offsets = calloc(num_lines, sizeof(*offsets));
925 if (!requests || !offsets)
926 die("out of memory");
927
928 line_cfg = gpiod_line_config_new();
929 if (!line_cfg)
930 die_perror("unable to allocate the line config structure");
931
932 for (i = 0; i < resolver->num_chips; i++) {
933 num_lines = get_line_offsets_and_values(resolver, i, offsets,
934 values);
935
936 gpiod_line_config_reset(line_cfg);
937
938 ret = gpiod_line_config_add_line_settings(line_cfg, offsets,
939 num_lines, settings);
940 if (ret)
941 die_perror("unable to add line settings");
942
943 ret = gpiod_line_config_set_output_values(line_cfg,
944 values, num_lines);
945 if (ret)
946 die_perror("unable to set output values");
947
948 chip = gpiod_chip_open(resolver->chips[i].path);
949 if (!chip)
950 die_perror("unable to open chip '%s'",
951 resolver->chips[i].path);
952
953 requests[i] = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
954 if (!requests[i])
955 die_perror("unable to request lines on chip '%s'",
956 resolver->chips[i].path);
957
958 gpiod_chip_close(chip);
959 }
960
961 gpiod_request_config_free(req_cfg);
962 gpiod_line_config_free(line_cfg);
963 gpiod_line_settings_free(settings);
964
965 if (cfg.banner)
966 print_banner(argc, lines);
967
968 if (cfg.daemonize)
969 if (daemon(0, cfg.interactive) < 0)
970 die_perror("unable to daemonize");
971
972 if (cfg.toggles) {
973 for (i = 0; i < cfg.toggles; i++)
974 if ((cfg.hold_period_us > cfg.toggle_periods[i]) &&
975 ((i != cfg.toggles - 1) ||
976 cfg.toggle_periods[i] != 0))
977 cfg.toggle_periods[i] = cfg.hold_period_us;
978
979 toggle_sequence(cfg.toggles, cfg.toggle_periods, requests,
980 resolver, offsets, values);
981 free(cfg.toggle_periods);
982 }
983
984 if (cfg.hold_period_us)
985 sleep_us(cfg.hold_period_us);
986
987 #ifdef GPIOSET_INTERACTIVE
988 if (cfg.interactive)
989 interact(requests, resolver, lines, offsets, values,
990 cfg.unquoted);
991 else if (!cfg.toggles)
992 wait_fd(gpiod_line_request_get_fd(requests[0]));
993 #else
994 if (!cfg.toggles)
995 wait_fd(gpiod_line_request_get_fd(requests[0]));
996 #endif
997
998 for (i = 0; i < resolver->num_chips; i++)
999 gpiod_line_request_release(requests[i]);
1000
1001 free(requests);
1002 free_line_resolver(resolver);
1003 free(lines);
1004 free(values);
1005 free(offsets);
1006
1007 return EXIT_SUCCESS;
1008 }