core: extend gpiod_chip_open_lookup()
authorBartosz Golaszewski <bartekgola@gmail.com>
Wed, 22 Feb 2017 11:01:41 +0000 (12:01 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Wed, 22 Feb 2017 11:01:41 +0000 (12:01 +0100)
Make gpiod_chip_open_lookup() also check the chip label.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
include/gpiod.h
src/lib/core.c
tests/unit/tests-chip.c

index 0cb5d1a3294831e9b223dc1617de30d43f7fafb3..f3c5950f1679e54c600e287db0baf62e539efbec 100644 (file)
@@ -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;
 
index 9b541280df99852db9bb384c13bef881680d866a..bb13a048bee5fb644a9c82fbfbcdcff219ba2eda 100644 (file)
@@ -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)
index 0690e4a39d21c8bcc467332f56d0f33bf30e50d5..7f6f27b33fbb2a5f524cad9ccab99d74fb415707 100644 (file)
@@ -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)
 {