From: Bartosz Golaszewski Date: Wed, 4 Oct 2017 07:07:44 +0000 (+0200) Subject: line: provide gpiod_line_bulk_foreach_line() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=5f20e774798e930467bed054e0f3a52e2e7e9c1d;p=qemu-gpiodev%2Flibgpiod.git line: provide gpiod_line_bulk_foreach_line() Add a macro allowing to easily iterate over all lines held by a line bulk object. Signed-off-by: Bartosz Golaszewski --- diff --git a/include/gpiod.h b/include/gpiod.h index 8c672a9..438744f 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -442,6 +442,18 @@ gpiod_line_bulk_num_lines(struct gpiod_line_bulk *bulk) return bulk->num_lines; } +/** + * @brief Iterate over all line handles held by a line bulk object. + * @param bulk Line bulk object. + * @param line GPIO line handle. On each iteration, the subsequent line handle + * is assigned to this pointer. + * @param lineptr Pointer to a GPIO line handle used to store the loop state. + */ +#define gpiod_line_bulk_foreach_line(bulk, line, lineptr) \ + for ((lineptr) = (bulk)->lines, (line) = *(lineptr); \ + (lineptr) <= (bulk)->lines + ((bulk)->num_lines - 1); \ + (lineptr)++, (line) = *(lineptr)) + /** * @} * diff --git a/tests/tests-line.c b/tests/tests-line.c index b238c9b..7d4e3e1 100644 --- a/tests/tests-line.c +++ b/tests/tests-line.c @@ -501,3 +501,40 @@ static void line_empty_consumer(void) TEST_DEFINE(line_empty_consumer, "line request - empty consumer string", 0, { 8 }); + +static void line_bulk_foreach(void) +{ + static const char *const line_names[] = { "gpio-mockup-A-0", + "gpio-mockup-A-1", + "gpio-mockup-A-2", + "gpio-mockup-A-3" }; + + TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL; + struct gpiod_line_bulk bulk = GPIOD_LINE_BULK_INITIALIZER; + struct gpiod_line *line, **lineptr; + int i; + + chip = gpiod_chip_open(test_chip_path(0)); + TEST_ASSERT_NOT_NULL(chip); + + for (i = 0; i < 4; i++) { + line = gpiod_chip_get_line(chip, i); + TEST_ASSERT_NOT_NULL(line); + + gpiod_line_bulk_add(&bulk, line); + } + + i = 0; + gpiod_line_bulk_foreach_line(&bulk, line, lineptr) + TEST_ASSERT_STR_EQ(gpiod_line_name(line), line_names[i++]); + + i = 0; + gpiod_line_bulk_foreach_line(&bulk, line, lineptr) { + TEST_ASSERT_STR_EQ(gpiod_line_name(line), line_names[i++]); + if (i == 2) + break; + } +} +TEST_DEFINE(line_bulk_foreach, + "line bulk - iterate over all lines", + TEST_FLAG_NAMED_LINES, { 8 });