Skip to content

Commit 62757c3

Browse files
warthog618brgl
authored andcommitted
tools: gpio: add multi-line monitoring to gpio-event-mon
Extend gpio-event-mon to support monitoring multiple lines. This would require multiple lineevent requests to implement using uAPI v1, but can be performed with a single line request using uAPI v2. Signed-off-by: Kent Gibson <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 0acda97 commit 62757c3

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

tools/gpio/gpio-event-mon.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
#include "gpio-utils.h"
2727

2828
int monitor_device(const char *device_name,
29-
unsigned int line,
29+
unsigned int *lines,
30+
unsigned int num_lines,
3031
struct gpio_v2_line_config *config,
3132
unsigned int loops)
3233
{
@@ -47,16 +48,18 @@ int monitor_device(const char *device_name,
4748
goto exit_free_name;
4849
}
4950

50-
ret = gpiotools_request_line(device_name, &line, 1, config,
51+
ret = gpiotools_request_line(device_name, lines, num_lines, config,
5152
"gpio-event-mon");
5253
if (ret < 0)
5354
goto exit_device_close;
5455
else
5556
lfd = ret;
5657

5758
/* Read initial states */
58-
values.mask = 1;
59+
values.mask = 0;
5960
values.bits = 0;
61+
for (i = 0; i < num_lines; i++)
62+
gpiotools_set_bit(&values.mask, i);
6063
ret = gpiotools_get_values(lfd, &values);
6164
if (ret < 0) {
6265
fprintf(stderr,
@@ -65,9 +68,23 @@ int monitor_device(const char *device_name,
6568
goto exit_line_close;
6669
}
6770

68-
fprintf(stdout, "Monitoring line %d on %s\n", line, device_name);
69-
fprintf(stdout, "Initial line value: %d\n",
70-
gpiotools_test_bit(values.bits, 0));
71+
if (num_lines == 1) {
72+
fprintf(stdout, "Monitoring line %d on %s\n", lines[0], device_name);
73+
fprintf(stdout, "Initial line value: %d\n",
74+
gpiotools_test_bit(values.bits, 0));
75+
} else {
76+
fprintf(stdout, "Monitoring lines %d", lines[0]);
77+
for (i = 1; i < num_lines - 1; i++)
78+
fprintf(stdout, ", %d", lines[i]);
79+
fprintf(stdout, " and %d on %s\n", lines[i], device_name);
80+
fprintf(stdout, "Initial line values: %d",
81+
gpiotools_test_bit(values.bits, 0));
82+
for (i = 1; i < num_lines - 1; i++)
83+
fprintf(stdout, ", %d",
84+
gpiotools_test_bit(values.bits, i));
85+
fprintf(stdout, " and %d\n",
86+
gpiotools_test_bit(values.bits, i));
87+
}
7188

7289
while (1) {
7390
struct gpio_v2_line_event event;
@@ -126,7 +143,7 @@ void print_usage(void)
126143
fprintf(stderr, "Usage: gpio-event-mon [options]...\n"
127144
"Listen to events on GPIO lines, 0->1 1->0\n"
128145
" -n <name> Listen on GPIOs on a named device (must be stated)\n"
129-
" -o <n> Offset to monitor\n"
146+
" -o <n> Offset of line to monitor (may be repeated)\n"
130147
" -d Set line as open drain\n"
131148
" -s Set line as open source\n"
132149
" -r Listen for rising edges\n"
@@ -146,7 +163,8 @@ void print_usage(void)
146163
int main(int argc, char **argv)
147164
{
148165
const char *device_name = NULL;
149-
unsigned int line = -1;
166+
unsigned int lines[GPIO_V2_LINES_MAX];
167+
unsigned int num_lines = 0;
150168
unsigned int loops = 0;
151169
struct gpio_v2_line_config config;
152170
int c;
@@ -162,7 +180,12 @@ int main(int argc, char **argv)
162180
device_name = optarg;
163181
break;
164182
case 'o':
165-
line = strtoul(optarg, NULL, 10);
183+
if (num_lines >= GPIO_V2_LINES_MAX) {
184+
print_usage();
185+
return -1;
186+
}
187+
lines[num_lines] = strtoul(optarg, NULL, 10);
188+
num_lines++;
166189
break;
167190
case 'd':
168191
config.flags |= GPIO_V2_LINE_FLAG_OPEN_DRAIN;
@@ -182,7 +205,7 @@ int main(int argc, char **argv)
182205
}
183206
}
184207

185-
if (!device_name || line == -1) {
208+
if (!device_name || num_lines == 0) {
186209
print_usage();
187210
return -1;
188211
}
@@ -191,5 +214,5 @@ int main(int argc, char **argv)
191214
"falling edges\n");
192215
config.flags |= EDGE_FLAGS;
193216
}
194-
return monitor_device(device_name, line, &config, loops);
217+
return monitor_device(device_name, lines, num_lines, &config, loops);
195218
}

0 commit comments

Comments
 (0)