treewide: kill opening chips by label
authorBartosz Golaszewski <bgolaszewski@baylibre.com>
Thu, 26 Nov 2020 11:48:08 +0000 (12:48 +0100)
committerBartosz Golaszewski <bgolaszewski@baylibre.com>
Mon, 14 Dec 2020 14:56:46 +0000 (15:56 +0100)
Chip labels are not unique - opening chips by label may lead to errors
or to ignoring chips with duplicate labels. Users can easily implement
chip lookup by label themselves so remove this part from the core library
and all bindings.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
bindings/cxx/chip.cpp
bindings/cxx/gpiod.hpp
bindings/cxx/tests/tests-chip.cpp
bindings/python/gpiodmodule.c
bindings/python/tests/gpiod_py_test.py
include/gpiod.h
lib/helpers.c
tests/tests-chip.c

index 1fc072381c36b59760c47cb2286e4e8a247d6c42..dffa7a2b38723d8760a9d078b96948e33a25c2fa 100644 (file)
@@ -30,11 +30,6 @@ namespace {
        return ::gpiod_chip_open_by_name(device.c_str());
 }
 
-::gpiod_chip* open_by_label(const ::std::string& device)
-{
-       return ::gpiod_chip_open_by_label(device.c_str());
-}
-
 ::gpiod_chip* open_by_number(const ::std::string& device)
 {
        return ::gpiod_chip_open_by_number(::std::stoul(device));
@@ -46,7 +41,6 @@ const ::std::map<int, open_func> open_funcs = {
        { chip::OPEN_LOOKUP,    open_lookup,    },
        { chip::OPEN_BY_PATH,   open_by_path,   },
        { chip::OPEN_BY_NAME,   open_by_name,   },
-       { chip::OPEN_BY_LABEL,  open_by_label,  },
        { chip::OPEN_BY_NUMBER, open_by_number, },
 };
 
index 0f1d9b2d4a11d6894c1d65443cc455c3d03b9251..0d443b0120531afc93ddcfc72e9db6ac202093bb 100644 (file)
@@ -190,8 +190,6 @@ public:
                /**< Assume the string is a path to the GPIO chardev. */
                OPEN_BY_NAME,
                /**< Assume the string is the name of the chip */
-               OPEN_BY_LABEL,
-               /**< Assume the string is the label of the GPIO chip. */
                OPEN_BY_NUMBER,
                /**< Assume the string is the number of the GPIO chip. */
        };
index 1c69872cded6029b3d084c90b7ec7a3c5db8207c..4c9f113f674d675f74e523367d8ff93e237afae7 100644 (file)
@@ -28,11 +28,6 @@ TEST_CASE("GPIO chip device can be opened in different modes", "[chip]")
                                ::gpiod::chip::OPEN_BY_PATH));
        }
 
-       SECTION("open by label")
-       {
-               REQUIRE_NOTHROW(::gpiod::chip("gpio-mockup-B", ::gpiod::chip::OPEN_BY_LABEL));
-       }
-
        SECTION("open by number")
        {
                REQUIRE_NOTHROW(::gpiod::chip(::std::to_string(mockup::instance().chip_num(1)),
@@ -54,11 +49,6 @@ TEST_CASE("GPIO chip device can be opened with implicit lookup", "[chip]")
                REQUIRE_NOTHROW(::gpiod::chip(mockup::instance().chip_path(1)));
        }
 
-       SECTION("lookup by label")
-       {
-               REQUIRE_NOTHROW(::gpiod::chip("gpio-mockup-B"));
-       }
-
        SECTION("lookup by number")
        {
                REQUIRE_NOTHROW(::gpiod::chip(::std::to_string(mockup::instance().chip_num(1))));
@@ -84,11 +74,6 @@ TEST_CASE("GPIO chip can be opened with the open() method in different modes", "
                                          ::gpiod::chip::OPEN_BY_PATH));
        }
 
-       SECTION("open by label")
-       {
-               REQUIRE_NOTHROW(chip.open("gpio-mockup-B", ::gpiod::chip::OPEN_BY_LABEL));
-       }
-
        SECTION("open by number")
        {
                REQUIRE_NOTHROW(chip.open(::std::to_string(mockup::instance().chip_num(1)),
@@ -126,11 +111,6 @@ TEST_CASE("GPIO chip can be opened with the open() method with implicit lookup",
                REQUIRE_NOTHROW(chip.open(mockup::instance().chip_path(1)));
        }
 
-       SECTION("lookup by label")
-       {
-               REQUIRE_NOTHROW(chip.open("gpio-mockup-B"));
-       }
-
        SECTION("lookup by number")
        {
                REQUIRE_NOTHROW(chip.open(::std::to_string(mockup::instance().chip_num(1))));
index 11d1407f005235a4a93583726dba35915d8e8424..17a58b12e61e5a8c31095943a601806b5c20f62e 100644 (file)
@@ -1961,7 +1961,6 @@ enum {
        gpiod_OPEN_LOOKUP = 1,
        gpiod_OPEN_BY_NAME,
        gpiod_OPEN_BY_PATH,
-       gpiod_OPEN_BY_LABEL,
        gpiod_OPEN_BY_NUMBER,
 };
 
@@ -1987,9 +1986,6 @@ static int gpiod_Chip_init(gpiod_ChipObject *self,
        case gpiod_OPEN_BY_PATH:
                self->chip = gpiod_chip_open(descr);
                break;
-       case gpiod_OPEN_BY_LABEL:
-               self->chip = gpiod_chip_open_by_label(descr);
-               break;
        case gpiod_OPEN_BY_NUMBER:
                self->chip = gpiod_chip_open_by_number(atoi(descr));
                break;
@@ -2503,10 +2499,10 @@ PyDoc_STRVAR(gpiod_ChipType_doc,
 "The Chip object's constructor takes a description string as argument the\n"
 "meaning of which depends on the second, optional parameter which defines\n"
 "the way the description string should be interpreted. The available\n"
-"options are: OPEN_BY_LABEL, OPEN_BY_NAME, OPEN_BY_NUMBER, OPEN_BY_PATH,\n"
-"and OPEN_LOOKUP. The last option means that libgpiod should open the chip\n"
-"based on the best guess what the path is. This is also the default if the\n"
-"second argument is missing.\n"
+"options are: OPEN_BY_NAME, OPEN_BY_NUMBER, OPEN_BY_PATH and OPEN_LOOKUP.\n"
+"The last option means that libgpiod should open the chip based on the best\n"
+"guess what the path is. This is also the default if the second argument is\n"
+"missing.\n"
 "\n"
 "Callers must close the chip by calling the close() method when it's no\n"
 "longer used.\n"
@@ -2786,11 +2782,6 @@ static gpiod_ConstDescr gpiod_ConstList[] = {
                .name = "OPEN_BY_NAME",
                .val = gpiod_OPEN_BY_NAME,
        },
-       {
-               .typeobj = &gpiod_ChipType,
-               .name = "OPEN_BY_LABEL",
-               .val = gpiod_OPEN_BY_LABEL,
-       },
        {
                .typeobj = &gpiod_ChipType,
                .name = "OPEN_BY_NUMBER",
index cb15fdcb947cf1d1cb819c211b6c4bf5bbebb31b..e4aaadcf742a98139292eb3cce8873bdf4166a0d 100755 (executable)
@@ -96,10 +96,6 @@ class ChipOpen(MockupTestCase):
                         gpiod.Chip.OPEN_BY_NUMBER) as chip:
             self.assertEqual(chip.name(), mockup.chip_name(1))
 
-    def test_open_chip_by_label(self):
-        with gpiod.Chip('gpio-mockup-B', gpiod.Chip.OPEN_BY_LABEL) as chip:
-            self.assertEqual(chip.name(), mockup.chip_name(1))
-
     def test_lookup_chip_by_name(self):
         with gpiod.Chip(mockup.chip_name(1)) as chip:
             self.assertEqual(chip.name(), mockup.chip_name(1))
@@ -112,10 +108,6 @@ class ChipOpen(MockupTestCase):
         with gpiod.Chip('{}'.format(mockup.chip_num(1))) as chip:
             self.assertEqual(chip.name(), mockup.chip_name(1))
 
-    def test_lookup_chip_by_label(self):
-        with gpiod.Chip('gpio-mockup-B') as chip:
-            self.assertEqual(chip.name(), mockup.chip_name(1))
-
     def test_nonexistent_chip(self):
         with self.assertRaises(FileNotFoundError):
             chip = gpiod.Chip('nonexistent-chip')
index b5965eda0f0cadd7d00d1c6b1ff252eb70c2a33f..b28cc9200fceff37f1b0dbf34f3921a5a8b8be5b 100644 (file)
@@ -103,24 +103,14 @@ struct gpiod_chip *gpiod_chip_open_by_name(const char *name) GPIOD_API;
  */
 struct gpiod_chip *gpiod_chip_open_by_number(unsigned int num) GPIOD_API;
 
-/**
- * @brief Open a gpiochip by label.
- * @param label Label of the gpiochip to open.
- * @return GPIO chip handle or NULL if the chip with given label was not found
- *         or an error occured.
- * @note If the chip cannot be found but no other error occurred, errno is set
- *       to ENOENT.
- */
-struct gpiod_chip *gpiod_chip_open_by_label(const char *label) GPIOD_API;
-
 /**
  * @brief Open a gpiochip based on the best guess what the path is.
  * @param descr String describing the gpiochip.
  * @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, label or number as a string. Then it tries to open it
- * using one of the gpiod_chip_open** variants.
+ * GPIO chip, its name or number as a string. Then it tries to open it using
+ * one of the gpiod_chip_open** variants.
  */
 struct gpiod_chip *gpiod_chip_open_lookup(const char *descr) GPIOD_API;
 
index 3b7428b3cb0cd54e2c5246f93b78bd389d7d77e3..3ec6c96d0fa36eeb41dec13771e5efb26a6ac0ca 100644 (file)
@@ -56,28 +56,6 @@ struct gpiod_chip *gpiod_chip_open_by_number(unsigned int num)
        return chip;
 }
 
-struct gpiod_chip *gpiod_chip_open_by_label(const char *label)
-{
-       struct gpiod_chip_iter *iter;
-       struct gpiod_chip *chip;
-
-       iter = gpiod_chip_iter_new();
-       if (!iter)
-               return NULL;
-
-       gpiod_foreach_chip(iter, chip) {
-               if (strcmp(label, gpiod_chip_label(chip)) == 0) {
-                       gpiod_chip_iter_free_noclose(iter);
-                       return chip;
-               }
-       }
-
-       errno = ENOENT;
-       gpiod_chip_iter_free(iter);
-
-       return NULL;
-}
-
 struct gpiod_chip *gpiod_chip_open_lookup(const char *descr)
 {
        struct gpiod_chip *chip;
@@ -85,13 +63,10 @@ struct gpiod_chip *gpiod_chip_open_lookup(const char *descr)
        if (isuint(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/", 5))
-                               chip = gpiod_chip_open_by_name(descr);
-                       else
-                               chip = gpiod_chip_open(descr);
-               }
+               if (strncmp(descr, "/dev/", 5))
+                       chip = gpiod_chip_open_by_name(descr);
+               else
+                       chip = gpiod_chip_open(descr);
        }
 
        return chip;
index 2f19c49151237b558c3bb6905e8fd9eee55fe126..0c2948a89219170ddedd4923d0a5592f4fee8ce6 100644 (file)
@@ -68,28 +68,8 @@ GPIOD_TEST_CASE(open_by_number_good, 0, { 8 })
        g_assert_nonnull(chip);
 }
 
-GPIOD_TEST_CASE(open_by_label_good, 0, { 4, 4, 4, 4, 4 })
-{
-       g_autoptr(gpiod_chip_struct) chip = NULL;
-
-       chip = gpiod_chip_open_by_label("gpio-mockup-D");
-       g_assert_nonnull(chip);
-       gpiod_test_return_if_failed();
-       g_assert_cmpstr(gpiod_chip_name(chip), ==, gpiod_test_chip_name(3));
-}
-
-GPIOD_TEST_CASE(open_by_label_bad, 0, { 4, 4, 4, 4, 4 })
-{
-       struct gpiod_chip *chip;
-
-       chip = gpiod_chip_open_by_label("nonexistent_gpio_chip");
-       g_assert_null(chip);
-       g_assert_cmpint(errno, ==, ENOENT);
-}
-
 GPIOD_TEST_CASE(open_lookup_good, 0, { 8, 8, 8})
 {
-       g_autoptr(gpiod_chip_struct) chip_by_label = NULL;
        g_autoptr(gpiod_chip_struct) chip_by_name = NULL;
        g_autoptr(gpiod_chip_struct) chip_by_path = NULL;
        g_autoptr(gpiod_chip_struct) chip_by_num = NULL;
@@ -99,12 +79,10 @@ GPIOD_TEST_CASE(open_lookup_good, 0, { 8, 8, 8})
        chip_by_name = gpiod_chip_open_lookup(gpiod_test_chip_name(1));
        chip_by_path = gpiod_chip_open_lookup(gpiod_test_chip_path(1));
        chip_by_num = gpiod_chip_open_lookup(chip_num_str);
-       chip_by_label = gpiod_chip_open_lookup("gpio-mockup-B");
 
        g_assert_nonnull(chip_by_name);
        g_assert_nonnull(chip_by_path);
        g_assert_nonnull(chip_by_num);
-       g_assert_nonnull(chip_by_label);
        gpiod_test_return_if_failed();
 
        g_assert_cmpstr(gpiod_chip_name(chip_by_name),
@@ -113,8 +91,6 @@ GPIOD_TEST_CASE(open_lookup_good, 0, { 8, 8, 8})
                        ==, gpiod_test_chip_name(1));
        g_assert_cmpstr(gpiod_chip_name(chip_by_num),
                        ==, gpiod_test_chip_name(1));
-       g_assert_cmpstr(gpiod_chip_name(chip_by_label),
-                       ==, gpiod_test_chip_name(1));
 }
 
 GPIOD_TEST_CASE(get_name, 0, { 8, 8, 8})