core: add a helper for requesting all types of events
authorBartosz Golaszewski <bartekgola@gmail.com>
Wed, 11 Jan 2017 15:07:27 +0000 (16:07 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Wed, 11 Jan 2017 15:11:26 +0000 (16:11 +0100)
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 <bartekgola@gmail.com>
core.c
gpiod.h

diff --git a/core.c b/core.c
index 0e876487de5b7d052290c871c30f731086fffe95..6b4772bcbbb8d29972fbf8a281fac015c02e2317 100644 (file)
--- 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 bbebbd332bb8a826216afae758151a0b552c5be0..875708f682142f775f16a2170ea56b57b9840f5b 100644 (file)
--- 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.