event: implement gpiod_line_event_read_fd()
authorBartosz Golaszewski <bartekgola@gmail.com>
Wed, 11 Jan 2017 13:20:38 +0000 (14:20 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Wed, 11 Jan 2017 13:20:38 +0000 (14:20 +0100)
Add a new function which allows to read GPIO event data directly from
a file descriptor instead of a gpiod_line object. This is useful if
the user is already directly polling the file descriptors for events.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
core.c
gpiod.h

diff --git a/core.c b/core.c
index 805178312005af8e791288b3cc704acdd27ff594..7928578292a140e3a8b86145c098dbd640eb922c 100644 (file)
--- a/core.c
+++ b/core.c
@@ -815,17 +815,32 @@ int gpiod_line_event_wait_bulk(struct gpiod_line_bulk *bulk,
 int gpiod_line_event_read(struct gpiod_line *line,
                          struct gpiod_line_event *event)
 {
-       struct gpioevent_data evdata;
-       ssize_t rd;
+       int fd;
 
        if (!gpiod_line_event_configured(line)) {
                set_last_error(GPIOD_EEVREQUEST);
                return -1;
        }
 
+       fd = line_get_event_fd(line);
+
+       return gpiod_line_event_read_fd(fd, event);
+}
+
+int gpiod_line_event_get_fd(struct gpiod_line *line)
+{
+       return line_get_state(line) == LINE_EVENT
+                               ? line_get_event_fd(line) : -1;
+}
+
+int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event)
+{
+       struct gpioevent_data evdata;
+       ssize_t rd;
+
        memset(&evdata, 0, sizeof(evdata));
 
-       rd = read(line_get_event_fd(line), &evdata, sizeof(evdata));
+       rd = read(fd, &evdata, sizeof(evdata));
        if (rd < 0) {
                last_error_from_errno();
                return -1;
@@ -842,12 +857,6 @@ int gpiod_line_event_read(struct gpiod_line *line,
        return 0;
 }
 
-int gpiod_line_event_get_fd(struct gpiod_line *line)
-{
-       return line_get_state(line) == LINE_EVENT
-                               ? line_get_event_fd(line) : -1;
-}
-
 struct gpiod_chip * gpiod_chip_open(const char *path)
 {
        struct gpiod_chip *chip;
diff --git a/gpiod.h b/gpiod.h
index dc3c2f23b17d3e83ed09fa830a508c080513d56e..13b0277b8cf0a8dd0d9ed3204bf5997fa517c5f9 100644 (file)
--- a/gpiod.h
+++ b/gpiod.h
@@ -603,6 +603,18 @@ int gpiod_line_event_read(struct gpiod_line *line,
  */
 int gpiod_line_event_get_fd(struct gpiod_line *line) GPIOD_API;
 
+/**
+ * @brief Read the last GPIO event directly from a file descriptor.
+ * @param fd File descriptor.
+ * @param event Buffer to which the event data will be copied.
+ * @return 0 if the event was read correctly, -1 on error.
+ *
+ * Users who directly poll the file descriptor for incoming events can also
+ * directly read the event data from it using this routine. This function
+ * translates the kernel representation of the event to the libgpiod format.
+ */
+int gpiod_line_event_read_fd(int fd, struct gpiod_line_event *event) GPIOD_API;
+
 /**
  * @}
  *