aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>2024-04-23 12:04:50 +0200
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>2024-04-24 09:55:36 +0200
commitbb5a2bbf5d254830502c3ef40ea22c49f557782c (patch)
treedeb17689139ea7dd8eb6188b136f884200e34960
parent9df101d6d6bac5a9ef42692034f3c2cfe9f2f521 (diff)
downloadlibgpiod-bb5a2bbf5d254830502c3ef40ea22c49f557782c.tar.gz
tools: use ppoll() where higher timeout resolution makes sense
We allow timeout units to be specified in microseconds but for poll() we need to round them up to milliseconds. Switch to ppoll() to avoid losing precision. Reviewed-by: Kent Gibson <warthog618@gmail.com> Link: https://lore.kernel.org/r/20240423100452.32958-3-brgl@bgdev.pl Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
-rw-r--r--tools/gpiomon.c12
-rw-r--r--tools/gpionotify.c12
2 files changed, 20 insertions, 4 deletions
diff --git a/tools/gpiomon.c b/tools/gpiomon.c
index 40e6ac2f..71358433 100644
--- a/tools/gpiomon.c
+++ b/tools/gpiomon.c
@@ -176,7 +176,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
cfg->fmt = optarg;
break;
case 'i':
- cfg->idle_timeout = parse_period_or_die(optarg) / 1000;
+ cfg->idle_timeout = parse_period_or_die(optarg);
break;
case 'l':
cfg->active_low = true;
@@ -362,6 +362,7 @@ int main(int argc, char **argv)
int num_lines, events_done = 0;
struct gpiod_edge_event *event;
struct line_resolver *resolver;
+ struct timespec idle_timeout;
struct gpiod_chip *chip;
struct pollfd *pollfds;
unsigned int *offsets;
@@ -450,10 +451,17 @@ int main(int argc, char **argv)
if (cfg.banner)
print_banner(argc, argv);
+ if (cfg.idle_timeout > 0) {
+ idle_timeout.tv_sec = cfg.idle_timeout / 1000000;
+ idle_timeout.tv_nsec =
+ (cfg.idle_timeout % 1000000) * 1000;
+ }
+
for (;;) {
fflush(stdout);
- ret = poll(pollfds, resolver->num_chips, cfg.idle_timeout);
+ ret = ppoll(pollfds, resolver->num_chips,
+ cfg.idle_timeout > 0 ? &idle_timeout : NULL, NULL);
if (ret < 0)
die_perror("error polling for events");
diff --git a/tools/gpionotify.c b/tools/gpionotify.c
index d2aee152..08f5da93 100644
--- a/tools/gpionotify.c
+++ b/tools/gpionotify.c
@@ -132,7 +132,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
cfg->fmt = optarg;
break;
case 'i':
- cfg->idle_timeout = parse_period_or_die(optarg) / 1000;
+ cfg->idle_timeout = parse_period_or_die(optarg);
break;
case 'n':
cfg->events_wanted = parse_uint_or_die(optarg);
@@ -374,6 +374,7 @@ int main(int argc, char **argv)
int i, j, ret, events_done = 0, evtype;
struct line_resolver *resolver;
struct gpiod_info_event *event;
+ struct timespec idle_timeout;
struct gpiod_chip **chips;
struct gpiod_chip *chip;
struct pollfd *pollfds;
@@ -419,10 +420,17 @@ int main(int argc, char **argv)
if (cfg.banner)
print_banner(argc, argv);
+ if (cfg.idle_timeout > 0) {
+ idle_timeout.tv_sec = cfg.idle_timeout / 1000000;
+ idle_timeout.tv_nsec =
+ (cfg.idle_timeout % 1000000) * 1000;
+ }
+
for (;;) {
fflush(stdout);
- ret = poll(pollfds, resolver->num_chips, cfg.idle_timeout);
+ ret = ppoll(pollfds, resolver->num_chips,
+ cfg.idle_timeout > 0 ? &idle_timeout : NULL, NULL);
if (ret < 0)
die_perror("error polling for events");