helpers: provide gpiod_chip_find_lines()
authorBartosz Golaszewski <bartekgola@gmail.com>
Sun, 13 May 2018 11:56:49 +0000 (13:56 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 14 May 2018 10:00:24 +0000 (12:00 +0200)
Implement a new helper that allows to look-up multiple GPIO lines by
their names. Include a set of test cases.

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

index 166b14d2c50d51e3de43a862b1431eb25426e376..983cb66cf565f12ead1553e77878f8928ff84c6c 100644 (file)
@@ -425,6 +425,19 @@ int gpiod_chip_get_all_lines(struct gpiod_chip *chip,
 struct gpiod_line *
 gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) GPIOD_API;
 
+/**
+ * @brief Find a set of GPIO lines by names among lines exposed by this chip.
+ * @param chip The GPIO chip object.
+ * @param names Array of pointers to C-strings containing the names of the
+ *              lines to lookup. Must end with a NULL-pointer.
+ * @param bulk Line bulk object in which the located lines will be stored.
+ * @return 0 if all lines were located, -1 on error.
+ * @note If at least one line from the list could not be found among the lines
+ *       exposed by this chip, the function sets errno to ENOENT.
+ */
+int gpiod_chip_find_lines(struct gpiod_chip *chip, const char **names,
+                         struct gpiod_line_bulk *bulk) GPIOD_API;
+
 /**
  * @}
  *
index ac564e78f324bb8b6fa78f03339af3004249259d..80b8eff8615c831c0428761fa5ec5a279261f8f1 100644 (file)
@@ -163,6 +163,25 @@ gpiod_chip_find_line(struct gpiod_chip *chip, const char *name)
        return NULL;
 }
 
+int gpiod_chip_find_lines(struct gpiod_chip *chip,
+                         const char **names, struct gpiod_line_bulk *bulk)
+{
+       struct gpiod_line *line;
+       int i;
+
+       gpiod_line_bulk_init(bulk);
+
+       for (i = 0; names[i]; i++) {
+               line = gpiod_chip_find_line(chip, names[i]);
+               if (!line)
+                       return -1;
+
+               gpiod_line_bulk_add(bulk, line);
+       }
+
+       return 0;
+}
+
 int gpiod_line_request_input(struct gpiod_line *line, const char *consumer)
 {
        struct gpiod_line_request_config config = {
index 226fc20b5869eb603ecf1264cc33bf6ad69c58dc..e199ce79950a6ba8437e0ba0231d2804be63d0c2 100644 (file)
@@ -283,3 +283,54 @@ static void chip_find_line_not_found(void)
 TEST_DEFINE(chip_find_line_not_found,
            "gpiod_chip_find_line() - not found",
            TEST_FLAG_NAMED_LINES, { 8, 8, 8 });
+
+static void chip_find_lines_good(void)
+{
+       static const char *names[] = { "gpio-mockup-B-3",
+                                      "gpio-mockup-B-6",
+                                      "gpio-mockup-B-7",
+                                      NULL };
+
+       TEST_CLEANUP_CHIP struct gpiod_chip *chip = NULL;
+       struct gpiod_line_bulk bulk;
+       struct gpiod_line *line;
+       int rv;
+
+       chip = gpiod_chip_open(test_chip_path(1));
+       TEST_ASSERT_NOT_NULL(chip);
+
+       rv = gpiod_chip_find_lines(chip, names, &bulk);
+       TEST_ASSERT_RET_OK(rv);
+       TEST_ASSERT_EQ(gpiod_line_bulk_num_lines(&bulk), 3);
+       line = gpiod_line_bulk_get_line(&bulk, 0);
+       TEST_ASSERT_EQ(gpiod_line_offset(line), 3);
+       line = gpiod_line_bulk_get_line(&bulk, 1);
+       TEST_ASSERT_EQ(gpiod_line_offset(line), 6);
+       line = gpiod_line_bulk_get_line(&bulk, 2);
+       TEST_ASSERT_EQ(gpiod_line_offset(line), 7);
+}
+TEST_DEFINE(chip_find_lines_good,
+           "gpiod_chip_find_lines() - good",
+           TEST_FLAG_NAMED_LINES, { 8, 8, 8 });
+
+static void chip_find_lines_not_found(void)
+{
+       static const char *names[] = { "gpio-mockup-B-3",
+                                      "nonexistent",
+                                      "gpio-mockup-B-7",
+                                      NULL };
+
+       TEST_CLEANUP_CHIP struct gpiod_chip *chip = NULL;
+       struct gpiod_line_bulk bulk;
+       int rv;
+
+       chip = gpiod_chip_open(test_chip_path(1));
+       TEST_ASSERT_NOT_NULL(chip);
+
+       rv = gpiod_chip_find_lines(chip, names, &bulk);
+       TEST_ASSERT_EQ(rv, -1);
+       TEST_ASSERT_ERRNO_IS(ENOENT);
+}
+TEST_DEFINE(chip_find_lines_not_found,
+           "gpiod_chip_find_lines() - not found",
+           TEST_FLAG_NAMED_LINES, { 8, 8, 8 });