core: add the offset argument to the simple event loop callback
authorBartosz Golaszewski <bartekgola@gmail.com>
Thu, 13 Jul 2017 16:09:17 +0000 (18:09 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Thu, 13 Jul 2017 16:09:17 +0000 (18:09 +0200)
In preparation for introducing a multiple lines variant of the simple
event loop routine, add the offset argument to the callback prototype.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
include/gpiod.h
src/lib/simple.c
tests/tests-simple-api.c

index fb0010664bfc9026ec982ccaa977887fdda0e2a6..c2c6e7c0309cf9d6f711d97da1cd3653d1c5903f 100644 (file)
@@ -151,8 +151,13 @@ enum {
 
 /**
  * @brief Simple event callack signature.
+ *
+ * The callback function takes the following arguments: event type (int),
+ * GPIO line offset (unsigned int), event timestamp (const struct timespec *)
+ * and a pointer to user data.
  */
-typedef int (*gpiod_event_cb)(int, const struct timespec *, void *);
+typedef int (*gpiod_event_cb)(int, unsigned int,
+                             const struct timespec *, void *);
 
 /**
  * @brief Wait for events on a single GPIO line.
index 90ea6ed8952c6613b04a03f9a8a58fcec09af918..c141f7b76363e750954fb0f0360fef57f7637887 100644 (file)
@@ -182,7 +182,7 @@ int gpiod_simple_event_loop(const char *consumer, const char *device,
                                                : GPIOD_EVENT_CB_FALLING_EDGE;
                }
 
-               status = callback(evtype, &event.ts, cbdata);
+               status = callback(evtype, offset, &event.ts, cbdata);
                if (status == GPIOD_EVENT_CB_STOP) {
                        status = 0;
                        goto out;
index bf262c960f73425ba2e780d8f8d90a9686be6cbe..ab894154443c51748879b6357dd6a08b53dbe340 100644 (file)
@@ -122,23 +122,30 @@ TEST_DEFINE(simple_set_value_multiple_max_lines,
            0, { 128 });
 
 struct simple_event_data {
-       bool got_event;
+       bool got_rising_edge;
+       bool got_falling_edge;
+       unsigned int offset;
+       unsigned int count;
 };
 
-static int simple_event_cb(int evtype TEST_UNUSED,
-                          const struct timespec *ts TEST_UNUSED,
-                          void *data)
+static int simple_event_cb(int evtype, unsigned int offset,
+                          const struct timespec *ts TEST_UNUSED, void *data)
 {
        struct simple_event_data *evdata = data;
 
-       evdata->got_event = true;
+       if (evtype == GPIOD_EVENT_CB_RISING_EDGE)
+               evdata->got_rising_edge = true;
+       else if (evtype == GPIOD_EVENT_CB_FALLING_EDGE)
+               evdata->got_falling_edge = true;
 
-       return GPIOD_EVENT_CB_STOP;
+       evdata->offset = offset;
+
+       return ++evdata->count == 2 ? GPIOD_EVENT_CB_STOP : GPIOD_EVENT_CB_OK;
 }
 
 static void simple_event_loop(void)
 {
-       struct simple_event_data evdata = { false };
+       struct simple_event_data evdata = { false, false, 0, 0 };
        struct timespec ts = { 1, 0 };
        int status;
 
@@ -148,7 +155,10 @@ static void simple_event_loop(void)
                                         false, &ts, simple_event_cb, &evdata);
 
        TEST_ASSERT_RET_OK(status);
-       TEST_ASSERT(evdata.got_event);
+       TEST_ASSERT(evdata.got_rising_edge);
+       TEST_ASSERT(evdata.got_falling_edge);
+       TEST_ASSERT_EQ(evdata.count, 2);
+       TEST_ASSERT_EQ(evdata.offset, 3);
 }
 TEST_DEFINE(simple_event_loop,
            "gpiod_simple_event_loop() - single event",