| 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 |
#include <unistd.h> |
| 11 |
|
| 12 |
#include "tools-common.h" |
| 13 |
|
| 14 |
struct config { |
| 15 |
bool active_low; |
| 16 |
bool by_name; |
| 17 |
bool numeric; |
| 18 |
bool strict; |
| 19 |
bool unquoted; |
| 20 |
enum gpiod_line_bias bias; |
| 21 |
enum gpiod_line_direction direction; |
| 22 |
unsigned long long hold_period_us; |
| 23 |
const char *chip_id; |
| 24 |
const char *consumer; |
| 25 |
}; |
| 26 |
|
| 27 |
static void print_help(void) |
| 28 |
{ |
| 29 |
printf("Usage: %s [OPTIONS] <line>...\n", get_prog_name()); |
| 30 |
printf("\n"); |
| 31 |
printf("Read values of GPIO lines.\n"); |
| 32 |
printf("\n"); |
| 33 |
printf("Lines are specified by name, or optionally by offset if the chip option\n"); |
| 34 |
printf("is provided.\n"); |
| 35 |
printf("\n"); |
| 36 |
printf("Options:\n"); |
| 37 |
printf(" -a, --as-is\t\tleave the line direction unchanged, not forced to input\n"); |
| 38 |
print_bias_help(); |
| 39 |
printf(" --by-name\t\ttreat lines as names even if they would parse as an offset\n"); |
| 40 |
printf(" -c, --chip <chip>\trestrict scope to a particular chip\n"); |
| 41 |
printf(" -C, --consumer <name>\tconsumer name applied to requested lines (default is 'gpioget')\n"); |
| 42 |
printf(" -h, --help\t\tdisplay this help and exit\n"); |
| 43 |
printf(" -l, --active-low\ttreat the line as active low\n"); |
| 44 |
printf(" -p, --hold-period <period>\n"); |
| 45 |
printf("\t\t\twait between requesting the lines and reading the values\n"); |
| 46 |
printf(" --numeric\t\tdisplay line values as '0' (inactive) or '1' (active)\n"); |
| 47 |
printf(" -s, --strict\t\tabort if requested line names are not unique\n"); |
| 48 |
printf(" --unquoted\tdon't quote line names\n"); |
| 49 |
printf(" -v, --version\t\toutput version information and exit\n"); |
| 50 |
print_chip_help(); |
| 51 |
print_period_help(); |
| 52 |
} |
| 53 |
|
| 54 |
static int parse_config(int argc, char **argv, struct config *cfg) |
| 55 |
{ |
| 56 |
static const struct option longopts[] = { |
| 57 |
{ "active-low", no_argument, NULL, 'l' }, |
| 58 |
{ "as-is", no_argument, NULL, 'a' }, |
| 59 |
{ "bias", required_argument, NULL, 'b' }, |
| 60 |
{ "by-name", no_argument, NULL, 'B' }, |
| 61 |
{ "chip", required_argument, NULL, 'c' }, |
| 62 |
{ "consumer", required_argument, NULL, 'C' }, |
| 63 |
{ "help", no_argument, NULL, 'h' }, |
| 64 |
{ "hold-period", required_argument, NULL, 'p' }, |
| 65 |
{ "numeric", no_argument, NULL, 'N' }, |
| 66 |
{ "strict", no_argument, NULL, 's' }, |
| 67 |
{ "unquoted", no_argument, NULL, 'Q' }, |
| 68 |
{ "version", no_argument, NULL, 'v' }, |
| 69 |
{ GETOPT_NULL_LONGOPT }, |
| 70 |
}; |
| 71 |
|
| 72 |
static const char *const shortopts = "+ab:c:C:hlp:sv"; |
| 73 |
|
| 74 |
int opti, optc; |
| 75 |
|
| 76 |
memset(cfg, 0, sizeof(*cfg)); |
| 77 |
cfg->direction = GPIOD_LINE_DIRECTION_INPUT; |
| 78 |
cfg->consumer = "gpioget"; |
| 79 |
|
| 80 |
for (;;) { |
| 81 |
optc = getopt_long(argc, argv, shortopts, longopts, &opti); |
| 82 |
if (optc < 0) |
| 83 |
break; |
| 84 |
|
| 85 |
switch (optc) { |
| 86 |
case 'a': |
| 87 |
cfg->direction = GPIOD_LINE_DIRECTION_AS_IS; |
| 88 |
break; |
| 89 |
case 'b': |
| 90 |
cfg->bias = parse_bias_or_die(optarg); |
| 91 |
break; |
| 92 |
case 'B': |
| 93 |
cfg->by_name = true; |
| 94 |
break; |
| 95 |
case 'c': |
| 96 |
cfg->chip_id = optarg; |
| 97 |
break; |
| 98 |
case 'C': |
| 99 |
cfg->consumer = optarg; |
| 100 |
break; |
| 101 |
case 'l': |
| 102 |
cfg->active_low = true; |
| 103 |
break; |
| 104 |
case 'N': |
| 105 |
cfg->numeric = true; |
| 106 |
break; |
| 107 |
case 'p': |
| 108 |
cfg->hold_period_us = parse_period_or_die(optarg); |
| 109 |
break; |
| 110 |
case 'Q': |
| 111 |
cfg->unquoted = true; |
| 112 |
break; |
| 113 |
case 's': |
| 114 |
cfg->strict = true; |
| 115 |
break; |
| 116 |
case 'h': |
| 117 |
print_help(); |
| 118 |
exit(EXIT_SUCCESS); |
| 119 |
case 'v': |
| 120 |
print_version(); |
| 121 |
exit(EXIT_SUCCESS); |
| 122 |
case '?': |
| 123 |
die("try %s --help", get_prog_name()); |
| 124 |
case 0: |
| 125 |
break; |
| 126 |
default: |
| 127 |
abort(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return optind; |
| 132 |
} |
| 133 |
|
| 134 |
int main(int argc, char **argv) |
| 135 |
{ |
| 136 |
struct gpiod_line_settings *settings; |
| 137 |
struct gpiod_request_config *req_cfg; |
| 138 |
struct gpiod_line_request *request; |
| 139 |
struct gpiod_line_config *line_cfg; |
| 140 |
struct line_resolver *resolver; |
| 141 |
enum gpiod_line_value *values; |
| 142 |
struct resolved_line *line; |
| 143 |
struct gpiod_chip *chip; |
| 144 |
unsigned int *offsets; |
| 145 |
int i, num_lines, ret; |
| 146 |
struct config cfg; |
| 147 |
const char *fmt; |
| 148 |
|
| 149 |
set_prog_name(argv[0]); |
| 150 |
i = parse_config(argc, argv, &cfg); |
| 151 |
argc -= i; |
| 152 |
argv += i; |
| 153 |
|
| 154 |
if (argc < 1) |
| 155 |
die("at least one GPIO line must be specified"); |
| 156 |
|
| 157 |
resolver = resolve_lines(argc, argv, cfg.chip_id, cfg.strict, |
| 158 |
cfg.by_name); |
| 159 |
validate_resolution(resolver, cfg.chip_id); |
| 160 |
|
| 161 |
offsets = calloc(resolver->num_lines, sizeof(*offsets)); |
| 162 |
values = calloc(resolver->num_lines, sizeof(*values)); |
| 163 |
if (!offsets || !values) |
| 164 |
die("out of memory"); |
| 165 |
|
| 166 |
settings = gpiod_line_settings_new(); |
| 167 |
if (!settings) |
| 168 |
die_perror("unable to allocate line settings"); |
| 169 |
|
| 170 |
gpiod_line_settings_set_direction(settings, cfg.direction); |
| 171 |
|
| 172 |
if (cfg.bias) |
| 173 |
gpiod_line_settings_set_bias(settings, cfg.bias); |
| 174 |
|
| 175 |
if (cfg.active_low) |
| 176 |
gpiod_line_settings_set_active_low(settings, true); |
| 177 |
|
| 178 |
req_cfg = gpiod_request_config_new(); |
| 179 |
if (!req_cfg) |
| 180 |
die_perror("unable to allocate the request config structure"); |
| 181 |
|
| 182 |
line_cfg = gpiod_line_config_new(); |
| 183 |
if (!line_cfg) |
| 184 |
die_perror("unable to allocate the line config structure"); |
| 185 |
|
| 186 |
gpiod_request_config_set_consumer(req_cfg, cfg.consumer); |
| 187 |
|
| 188 |
for (i = 0; i < resolver->num_chips; i++) { |
| 189 |
chip = gpiod_chip_open(resolver->chips[i].path); |
| 190 |
if (!chip) |
| 191 |
die_perror("unable to open chip '%s'", |
| 192 |
resolver->chips[i].path); |
| 193 |
|
| 194 |
num_lines = get_line_offsets_and_values(resolver, i, offsets, |
| 195 |
NULL); |
| 196 |
|
| 197 |
gpiod_line_config_reset(line_cfg); |
| 198 |
ret = gpiod_line_config_add_line_settings(line_cfg, offsets, |
| 199 |
num_lines, settings); |
| 200 |
if (ret) |
| 201 |
die_perror("unable to add line settings"); |
| 202 |
|
| 203 |
request = gpiod_chip_request_lines(chip, req_cfg, line_cfg); |
| 204 |
if (!request) |
| 205 |
die_perror("unable to request lines"); |
| 206 |
|
| 207 |
if (cfg.hold_period_us) |
| 208 |
sleep_us(cfg.hold_period_us); |
| 209 |
|
| 210 |
ret = gpiod_line_request_get_values(request, values); |
| 211 |
if (ret) |
| 212 |
die_perror("unable to read GPIO line values"); |
| 213 |
|
| 214 |
set_line_values(resolver, i, values); |
| 215 |
|
| 216 |
gpiod_line_request_release(request); |
| 217 |
gpiod_chip_close(chip); |
| 218 |
} |
| 219 |
|
| 220 |
fmt = cfg.unquoted ? "%s=%s" : "\"%s\"=%s"; |
| 221 |
|
| 222 |
for (i = 0; i < resolver->num_lines; i++) { |
| 223 |
line = &resolver->lines[i]; |
| 224 |
if (cfg.numeric) |
| 225 |
printf("%d", line->value); |
| 226 |
else |
| 227 |
printf(fmt, line->id, |
| 228 |
line->value ? "active" : "inactive"); |
| 229 |
|
| 230 |
if (i != resolver->num_lines - 1) |
| 231 |
printf(" "); |
| 232 |
} |
| 233 |
printf("\n"); |
| 234 |
|
| 235 |
free_line_resolver(resolver); |
| 236 |
gpiod_request_config_free(req_cfg); |
| 237 |
gpiod_line_config_free(line_cfg); |
| 238 |
gpiod_line_settings_free(settings); |
| 239 |
free(offsets); |
| 240 |
free(values); |
| 241 |
|
| 242 |
return EXIT_SUCCESS; |
| 243 |
} |