From 64c778c847f341db0a4d2ba7a603e65dbadfd4c8 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Wed, 11 Jan 2017 16:07:27 +0100 Subject: [PATCH] core: add a helper for requesting all types of events Implement gpiod_line_event_request_all() which requests all types of events on a single line with less arguments. While we're at it: use it in gpiod_simple_event_loop(). Signed-off-by: Bartosz Golaszewski --- core.c | 10 ++-------- gpiod.h | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/core.c b/core.c index 0e87648..6b4772b 100644 --- a/core.c +++ b/core.c @@ -226,18 +226,11 @@ int gpiod_simple_event_loop(const char *device, unsigned int offset, bool active_low, struct timespec *timeout, gpiod_event_cb callback, void *cbdata) { - struct gpiod_line_evreq_config config; struct gpiod_line_event event; struct gpiod_chip *chip; struct gpiod_line *line; int status, evtype; - memset(&config, 0, sizeof(config)); - config.consumer = libgpiod_consumer; - config.event_type = GPIOD_EVENT_BOTH_EDGES; - config.active_state = active_low ? GPIOD_ACTIVE_STATE_LOW - : GPIOD_ACTIVE_STATE_HIGH; - chip = gpiod_chip_open_lookup(device); if (!chip) return -1; @@ -248,7 +241,8 @@ int gpiod_simple_event_loop(const char *device, unsigned int offset, return -1; } - status = gpiod_line_event_request(line, &config); + status = gpiod_line_event_request_all(line, + libgpiod_consumer, active_low); if (status < 0) { gpiod_chip_close(chip); return -1; diff --git a/gpiod.h b/gpiod.h index bbebbd3..875708f 100644 --- a/gpiod.h +++ b/gpiod.h @@ -635,6 +635,27 @@ struct gpiod_line_event { int gpiod_line_event_request(struct gpiod_line *line, struct gpiod_line_evreq_config *config) GPIOD_API; +/** + * @brief Request all event type notifications on a single line. + * @param line GPIO line object. + * @param consumer Name of the consumer. + * @param active_low Active state of the line - true if low. + * @return 0 if the operation succeeds, -1 on failure. + */ +static inline int gpiod_line_event_request_all(struct gpiod_line *line, + const char *consumer, + bool active_low) +{ + struct gpiod_line_evreq_config config = { + .consumer = consumer, + .event_type = GPIOD_EVENT_BOTH_EDGES, + .active_state = active_low ? GPIOD_ACTIVE_STATE_LOW + : GPIOD_ACTIVE_STATE_HIGH, + }; + + return gpiod_line_event_request(line, &config); +} + /** * @brief Stop listening for events and release the line. * @param line GPIO line object. -- 2.30.2