From: Bartosz Golaszewski Date: Fri, 29 Sep 2017 13:24:00 +0000 (+0200) Subject: core: implement gpiod_chip_find_line() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=1a6a61cbe1a3cdae45207cbe2725d9f707fc13ad;p=qemu-gpiodev%2Flibgpiod.git core: implement gpiod_chip_find_line() The low-level line lookup should be more fine-grained. As the first step: introduce a routine performing a line lookup for specific chip. Signed-off-by: Bartosz Golaszewski --- diff --git a/include/gpiod.h b/include/gpiod.h index 506f3e4..379b88c 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -330,6 +330,19 @@ unsigned int gpiod_chip_num_lines(struct gpiod_chip *chip) GPIOD_API; struct gpiod_line * gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset) GPIOD_API; +/** + * @brief Find a GPIO line by name among lines associated with given GPIO chip. + * @param chip The GPIO chip object. + * @param name The name of the GPIO line. + * @return Pointer to the GPIO line handle or NULL if the line could not be + * found or an error occurred. + * + * In case a line with given name is not associated with given chip, the + * functions sets errno to ENOENT. + */ +struct gpiod_line * +gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) GPIOD_API; + /** * @} * diff --git a/src/lib/core.c b/src/lib/core.c index 2dc78b4..3b3b6ef 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -237,6 +237,26 @@ gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset) return line; } +struct gpiod_line * +gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) +{ + struct gpiod_line_iter iter; + struct gpiod_line *line; + + gpiod_line_iter_init(&iter, chip); + gpiod_foreach_line(&iter, line) { + if (gpiod_line_iter_err(&iter)) + return NULL; + + if (strcmp(gpiod_line_name(line), name) == 0) + return line; + } + + errno = ENOENT; + + return NULL; +} + static int line_get_state(struct gpiod_line *line) { return line->state;