From 8087808b648f6bf8176e57c18e13634340783f9a Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Wed, 22 Feb 2017 12:01:41 +0100 Subject: [PATCH] core: extend gpiod_chip_open_lookup() Make gpiod_chip_open_lookup() also check the chip label. Signed-off-by: Bartosz Golaszewski --- include/gpiod.h | 4 ++-- src/lib/core.c | 21 +++++++++++++++------ tests/unit/tests-chip.c | 16 ++++++++++++---- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/include/gpiod.h b/include/gpiod.h index 0cb5d1a..f3c5950 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -884,8 +884,8 @@ struct gpiod_chip * gpiod_chip_open_by_label(const char *label) GPIOD_API; * @return GPIO chip handle or NULL if an error occurred. * * This routine tries to figure out whether the user passed it the path to - * the GPIO chip, its name or number as a string. Then it tries to open it - * using one of the other gpiod_chip_open** routines. + * the GPIO chip, its name, label or number as a string. Then it tries to + * open it using one of the other gpiod_chip_open** routines. */ struct gpiod_chip * gpiod_chip_open_lookup(const char *descr) GPIOD_API; diff --git a/src/lib/core.c b/src/lib/core.c index 9b54128..bb13a04 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -986,12 +986,21 @@ out: struct gpiod_chip * gpiod_chip_open_lookup(const char *descr) { - if (is_unsigned_int(descr)) - return gpiod_chip_open_by_number(strtoul(descr, NULL, 10)); - else if (strncmp(descr, dev_dir, sizeof(dev_dir) - 1) != 0) - return gpiod_chip_open_by_name(descr); - else - return gpiod_chip_open(descr); + struct gpiod_chip *chip; + + if (is_unsigned_int(descr)) { + chip = gpiod_chip_open_by_number(strtoul(descr, NULL, 10)); + } else { + chip = gpiod_chip_open_by_label(descr); + if (!chip) { + if (strncmp(descr, dev_dir, sizeof(dev_dir) - 1)) + chip = gpiod_chip_open_by_name(descr); + else + chip = gpiod_chip_open(descr); + } + } + + return chip; } void gpiod_chip_close(struct gpiod_chip *chip) diff --git a/tests/unit/tests-chip.c b/tests/unit/tests-chip.c index 0690e4a..7f6f27b 100644 --- a/tests/unit/tests-chip.c +++ b/tests/unit/tests-chip.c @@ -61,24 +61,32 @@ GU_DEFINE_TEST(chip_open_by_number_good, static void chip_open_lookup(void) { + GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_label = NULL; GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_name = NULL; GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_path = NULL; GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_num = NULL; GU_CLEANUP(gu_free_str) char *chip_num; - GU_ASSERT(asprintf(&chip_num, "%u", gu_chip_num(0)) > 0); + GU_ASSERT(asprintf(&chip_num, "%u", gu_chip_num(1)) > 0); - chip_by_name = gpiod_chip_open_lookup(gu_chip_name(0)); - chip_by_path = gpiod_chip_open_lookup(gu_chip_path(0)); + chip_by_name = gpiod_chip_open_lookup(gu_chip_name(1)); + chip_by_path = gpiod_chip_open_lookup(gu_chip_path(1)); chip_by_num = gpiod_chip_open_lookup(chip_num); + chip_by_label = gpiod_chip_open_lookup("gpio-mockup-B"); GU_ASSERT_NOT_NULL(chip_by_name); GU_ASSERT_NOT_NULL(chip_by_path); GU_ASSERT_NOT_NULL(chip_by_num); + GU_ASSERT_NOT_NULL(chip_by_label); + + GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_name), gu_chip_name(1)); + GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_path), gu_chip_name(1)); + GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_num), gu_chip_name(1)); + GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_label), gu_chip_name(1)); } GU_DEFINE_TEST(chip_open_lookup, "gpiod_chip_open_lookup()", - GU_LINES_UNNAMED, { 8 }); + GU_LINES_UNNAMED, { 8, 8, 8 }); static void chip_open_by_label_good(void) { -- 2.30.2