From: Bartosz Golaszewski <bartekgola@gmail.com> Date: Thu, 13 Jul 2017 16:09:17 +0000 (+0200) Subject: core: add the offset argument to the simple event loop callback X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=5571ba17ce5a978025edf94ef7a68e2ab06daeec;p=qemu-gpiodev%2Flibgpiod.git core: add the offset argument to the simple event loop callback 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> --- diff --git a/include/gpiod.h b/include/gpiod.h index fb00106..c2c6e7c 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -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. diff --git a/src/lib/simple.c b/src/lib/simple.c index 90ea6ed..c141f7b 100644 --- a/src/lib/simple.c +++ b/src/lib/simple.c @@ -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; diff --git a/tests/tests-simple-api.c b/tests/tests-simple-api.c index bf262c9..ab89415 100644 --- a/tests/tests-simple-api.c +++ b/tests/tests-simple-api.c @@ -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",