core: new error code
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 9 Jan 2017 12:30:05 +0000 (13:30 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 9 Jan 2017 12:30:05 +0000 (13:30 +0100)
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 <bartekgola@gmail.com>
core.c
gpiod.h

diff --git a/core.c b/core.c
index 2ccd6adec83d550d10a41f161b224aa4c2b477ea..0a0eaa737fee66775cc83cfc3824620d2c1020ce 100644 (file)
--- 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 f4906712be9a637f4b03149bbb8e40d954ccc6e1..473f41e01636c91c41f55cbb3dd4013071b89582 100644 (file)
--- 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,