From: Bartosz Golaszewski Date: Mon, 9 Jan 2017 12:30:05 +0000 (+0100) Subject: core: new error code X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=fdc35bce163b2010432b9b2b726ffda682a3d2b9;p=qemu-gpiodev%2Flibgpiod.git core: new error code Add an error code indicating that an event operation was requested on a line for which no events were configured. Signed-off-by: Bartosz Golaszewski --- diff --git a/core.c b/core.c index 2ccd6ad..0a0eaa7 100644 --- a/core.c +++ b/core.c @@ -67,6 +67,7 @@ static __thread char errmsg[ERRSTR_MAX]; static const char *const error_descr[] = { "success", "GPIO line not requested", + "no events configured on GPIO line", "GPIO lines in bulk don't belong to the same gpiochip", }; @@ -564,7 +565,7 @@ int gpiod_line_event_request(struct gpiod_line *line, struct gpiod_chip *chip; int status, fd; - if (line->reserved_status != LINE_FREE) + if (gpiod_line_event_configured(line)) return -EBUSY; req = &line->event; @@ -624,6 +625,18 @@ int gpiod_line_event_wait(struct gpiod_line *line, return gpiod_line_event_wait_bulk(&bulk, timeout, NULL); } +static bool line_bulk_is_event_configured(struct gpiod_line_bulk *line_bulk) +{ + unsigned int i; + + for (i = 0; i < line_bulk->num_lines; i++) { + if (!gpiod_line_event_configured(line_bulk->lines[i])) + return false; + } + + return true; +} + int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk, const struct timespec *timeout, unsigned int *index) @@ -635,7 +648,10 @@ int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk, memset(fds, 0, sizeof(fds)); - /* TODO Check if all lines are requested. */ + if (!line_bulk_is_event_configured(bulk)) { + set_last_error(GPIOD_EEVREQUEST); + return -1; + } for (i = 0; i < bulk->num_lines; i++) { line = bulk->lines[i]; @@ -665,6 +681,11 @@ int gpiod_line_event_read(struct gpiod_line *line, struct gpioevent_data evdata; ssize_t rd; + if (!gpiod_line_event_configured(line)) { + set_last_error(GPIOD_EEVREQUEST); + return -1; + } + memset(&evdata, 0, sizeof(evdata)); rd = read(line->event.fd, &evdata, sizeof(evdata)); diff --git a/gpiod.h b/gpiod.h index f490671..473f41e 100644 --- a/gpiod.h +++ b/gpiod.h @@ -79,6 +79,8 @@ enum { /**< No error. */ GPIOD_EREQUEST, /**< The caller has no ownership of this line. */ + GPIOD_EEVREQUEST, + /**< The caller has not configured any events on this line. */ GPIOD_EBULKINCOH, /**< Not all lines in bulk belong to the same GPIO chip. */ __GPIOD_MAX_ERR,