From 500c3093cd3e8261e8192ccc90a6e925279a484b Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Wed, 18 Oct 2017 17:16:07 +0200 Subject: [PATCH] event: improve gpiod_line_event_get_fd() Set errno to EINVAL when the user calls this routine for a line requested for values. Verify correct behavior with a test case. Signed-off-by: Bartosz Golaszewski --- src/lib/core.c | 7 ++++++- tests/tests-event.c | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/lib/core.c b/src/lib/core.c index d5bbe08..fec7944 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -988,7 +988,12 @@ int gpiod_line_event_read(struct gpiod_line *line, int gpiod_line_event_get_fd(struct gpiod_line *line) { - return line_get_state(line) == LINE_REQUESTED_EVENTS ? line->fd : -1; + if (line_get_state(line) != LINE_REQUESTED_EVENTS) { + errno = EINVAL; + return -1; + } + + return line->fd; } int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event) diff --git a/tests/tests-event.c b/tests/tests-event.c index 423c6d0..b4e3419 100644 --- a/tests/tests-event.c +++ b/tests/tests-event.c @@ -12,6 +12,8 @@ #include "gpiod-test.h" +#include + static void event_rising_edge_good(void) { TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL; @@ -203,3 +205,26 @@ static void event_wait_multiple(void) TEST_DEFINE(event_wait_multiple, "events - wait for events on multiple lines", 0, { 8 }); + +static void event_get_fd_when_values_requested(void) +{ + TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL; + struct gpiod_line *line; + int rv, fd; + + chip = gpiod_chip_open(test_chip_path(0)); + TEST_ASSERT_NOT_NULL(chip); + + line = gpiod_chip_get_line(chip, 3); + TEST_ASSERT_NOT_NULL(line); + + rv = gpiod_line_request_input(line, TEST_CONSUMER); + TEST_ASSERT_RET_OK(rv); + + fd = gpiod_line_event_get_fd(line); + TEST_ASSERT_EQ(fd, -1); + TEST_ASSERT_ERRNO_IS(EINVAL); +} +TEST_DEFINE(event_get_fd_when_values_requested, + "events - gpiod_line_event_get_fd(): line requested for values", + 0, { 8 }); -- 2.30.2