Debian Codegraph @

tools/tools-common.h

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 #ifndef __GPIOD_TOOLS_COMMON_H__
6 #define __GPIOD_TOOLS_COMMON_H__
7
8 #include <gpiod.h>
9
10 /*
11 * Various helpers for the GPIO tools.
12 *
13 * NOTE: This is not a stable interface - it's only to avoid duplicating
14 * common code.
15 */
16
17 #define NORETURN __attribute__((noreturn))
18 #define PRINTF(fmt, arg) __attribute__((format(printf, fmt, arg)))
19
20 #define GETOPT_NULL_LONGOPT NULL, 0, NULL, 0
21
22 struct resolved_line {
23 /* from the command line */
24 const char *id;
25
26 /*
27 * id parsed as int, if that is an option, or -1 if line must be
28 * resolved by name
29 */
30 int id_as_offset;
31
32 /* line has been located on a chip */
33 bool resolved;
34
35 /* remaining fields only valid once resolved... */
36
37 /* info for the line */
38 struct gpiod_line_info *info;
39
40 /* num of relevant chip in line_resolver */
41 int chip_num;
42
43 /* offset of line on chip */
44 unsigned int offset;
45
46 /* line value for gpioget/set */
47 int value;
48 };
49
50 struct resolved_chip {
51 /* info of the relevant chips */
52 struct gpiod_chip_info *info;
53
54 /* path to the chip */
55 char *path;
56 };
57
58 /* a resolver from requested line names/offsets to lines on the system */
59 struct line_resolver {
60 /*
61 * number of chips the lines span, and number of entries in chips
62 */
63 int num_chips;
64
65 /* number of lines in lines */
66 int num_lines;
67
68 /* number of lines found */
69 int num_found;
70
71 /* perform exhaustive search to check line names are unique */
72 bool strict;
73
74 /* details of the relevant chips */
75 struct resolved_chip *chips;
76
77 /* descriptors for the requested lines */
78 struct resolved_line lines[];
79 };
80
81 void set_prog_name(const char *name);
82 const char *get_prog_name(void);
83 const char *get_prog_short_name(void);
84 void print_error(const char *fmt, ...) PRINTF(1, 2);
85 void print_perror(const char *fmt, ...) PRINTF(1, 2);
86 void die(const char *fmt, ...) NORETURN PRINTF(1, 2);
87 void die_perror(const char *fmt, ...) NORETURN PRINTF(1, 2);
88 void print_version(void);
89 int parse_bias_or_die(const char *option);
90 long long parse_period(const char *option);
91 unsigned long long parse_period_or_die(const char *option);
92 void sleep_us(unsigned long long period);
93 int parse_uint(const char *option);
94 unsigned int parse_uint_or_die(const char *option);
95 void print_bias_help(void);
96 void print_chip_help(void);
97 void print_period_help(void);
98 void print_event_time(uint64_t evtime, int format);
99 void print_line_attributes(struct gpiod_line_info *info, bool unquoted_strings);
100 void print_line_id(struct line_resolver *resolver, int chip_num,
101 unsigned int offset, const char *chip_id, bool unquoted);
102 bool chip_path_lookup(const char *id, char **path_ptr);
103 int chip_paths(const char *id, char ***paths_ptr);
104 int all_chip_paths(char ***paths_ptr);
105 struct line_resolver *resolve_lines(int num_lines, char **lines,
106 const char *chip_id, bool strict,
107 bool by_name);
108 struct line_resolver *resolver_init(int num_lines, char **lines, int num_chips,
109 bool strict, bool by_name);
110 bool resolve_lines_by_offset(struct line_resolver *resolver,
111 unsigned int num_lines);
112 bool resolve_done(struct line_resolver *resolver);
113 void validate_resolution(struct line_resolver *resolver, const char *chip_id);
114 void free_line_resolver(struct line_resolver *resolver);
115 int get_line_offsets_and_values(struct line_resolver *resolver, int chip_num,
116 unsigned int *offsets,
117 enum gpiod_line_value *values);
118 const char *get_chip_name(struct line_resolver *resolver, int chip_num);
119 const char *get_line_name(struct line_resolver *resolver, int chip_num,
120 unsigned int offset);
121 void set_line_values(struct line_resolver *resolver, int chip_num,
122 enum gpiod_line_value *values);
123
124 #endif /* __GPIOD_TOOLS_COMMON_H__ */