From: Bartosz Golaszewski Date: Wed, 11 Jan 2017 13:20:38 +0000 (+0100) Subject: event: implement gpiod_line_event_read_fd() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=49a11437a576886457cad6208d611b01591b9c1a;p=qemu-gpiodev%2Flibgpiod.git event: implement gpiod_line_event_read_fd() 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 --- diff --git a/core.c b/core.c index 8051783..7928578 100644 --- 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 dc3c2f2..13b0277 100644 --- 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; + /** * @} *