core: rework gpiod_line_find()
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 16:27:39 +0000 (18:27 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 16:35:28 +0000 (18:35 +0200)
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 <bartekgola@gmail.com>
include/gpiod.h
src/lib/core.c
tests/tests-line.c

index 7b32f7019c5c6d3e86e4525f748961ba94387c51..e1b9f91533e4ffd7bccb6c8da99870825fde5fc7 100644 (file)
@@ -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;
 
index f3bfddfea2e30ce185402cc9599627b20d949bbc..5b41f1d8a50cd822c26f340aa246dcf403001f71 100644 (file)
@@ -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;
 }
index 38c4bae06efc1df6962a6221d9a6db4f67d1cb55..8e283b678703b3b99ed3d5fa051d1d0e18b56b96 100644 (file)
@@ -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)