From 5da2e8d7900c4fde7fab6c1570ea517f86965c66 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Sun, 13 May 2018 13:56:49 +0200 Subject: [PATCH] helpers: provide gpiod_chip_find_lines() 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 --- include/gpiod.h | 13 ++++++++++++ src/lib/helpers.c | 19 +++++++++++++++++ tests/tests-chip.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/include/gpiod.h b/include/gpiod.h index 166b14d..983cb66 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -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; + /** * @} * diff --git a/src/lib/helpers.c b/src/lib/helpers.c index ac564e7..80b8eff 100644 --- a/src/lib/helpers.c +++ b/src/lib/helpers.c @@ -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 = { diff --git a/tests/tests-chip.c b/tests/tests-chip.c index 226fc20..e199ce7 100644 --- a/tests/tests-chip.c +++ b/tests/tests-chip.c @@ -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 }); -- 2.30.2