From: Bartosz Golaszewski Date: Fri, 29 Sep 2017 16:27:39 +0000 (+0200) Subject: core: rework gpiod_line_find() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=d477b96778ffee47dfdb4607d375f2c995b0cb97;p=qemu-gpiodev%2Flibgpiod.git core: rework gpiod_line_find() Use gpiod_chip_find_line() internally for smaller code. Return NULL if an error occurrs during the lookup. Update the documentation. While we're at it: update the tests for line lookups. Signed-off-by: Bartosz Golaszewski --- diff --git a/include/gpiod.h b/include/gpiod.h index 7b32f70..e1b9f91 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -883,10 +883,11 @@ int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, * @brief Find a GPIO line by its name. * @param name Name of the GPIO line. * @return Returns the GPIO line handle if the line exists in the system or - * NULL if it couldn't be located. + * NULL if it couldn't be located or an error occurred. * * If this routine succeeds, the user must manually close the GPIO chip owning - * this line to avoid memory leaks. + * this line to avoid memory leaks. If the line could not be found, this + * functions sets errno to ENOENT. */ struct gpiod_line * gpiod_line_find(const char *name) GPIOD_API; diff --git a/src/lib/core.c b/src/lib/core.c index f3bfddf..5b41f1d 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -923,37 +923,32 @@ int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, int *values) struct gpiod_line * gpiod_line_find(const char *name) { - struct gpiod_chip_iter *chip_iter; - struct gpiod_line_iter line_iter; + struct gpiod_chip_iter *iter; struct gpiod_chip *chip; struct gpiod_line *line; - const char *line_name; - chip_iter = gpiod_chip_iter_new(); - if (!chip_iter) + iter = gpiod_chip_iter_new(); + if (!iter) return NULL; - gpiod_foreach_chip(chip_iter, chip) { - if (gpiod_chip_iter_err(chip_iter)) - continue; - - gpiod_line_iter_init(&line_iter, chip); - gpiod_foreach_line(&line_iter, line) { - if (gpiod_line_iter_err(&line_iter)) - continue; - - line_name = gpiod_line_name(line); - if (!line_name) - continue; + gpiod_foreach_chip(iter, chip) { + if (gpiod_chip_iter_err(iter)) + goto out; - if (strcmp(line_name, name) == 0) { - gpiod_chip_iter_free_noclose(chip_iter); - return line; - } + line = gpiod_chip_find_line(chip, name); + if (line) { + gpiod_chip_iter_free_noclose(iter); + return line; } + + if (errno != ENOENT) + goto out; } - gpiod_chip_iter_free(chip_iter); + errno = ENOENT; + +out: + gpiod_chip_iter_free(iter); return NULL; } diff --git a/tests/tests-line.c b/tests/tests-line.c index 38c4bae..8e283b6 100644 --- a/tests/tests-line.c +++ b/tests/tests-line.c @@ -278,7 +278,7 @@ TEST_DEFINE(line_set_value, "gpiod_line_set_value() - good", 0, { 8 }); -static void line_find_by_name_good(void) +static void line_find_good(void) { TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL; @@ -287,8 +287,20 @@ static void line_find_by_name_good(void) TEST_ASSERT_EQ(gpiod_line_offset(line), 12); } -TEST_DEFINE(line_find_by_name_good, - "gpiod_line_find_by_name() - good", +TEST_DEFINE(line_find_good, + "gpiod_line_find() - good", + TEST_FLAG_NAMED_LINES, { 16, 16, 32, 16 }); + +static void line_find_not_found(void) +{ + TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL; + + line = gpiod_line_find("nonexistent"); + TEST_ASSERT_NULL(line); + TEST_ASSERT_ERRNO_IS(ENOENT); +} +TEST_DEFINE(line_find_not_found, + "gpiod_line_find() - not found", TEST_FLAG_NAMED_LINES, { 16, 16, 32, 16 }); static void line_direction(void)