From: Bartosz Golaszewski Date: Fri, 29 Sep 2017 17:17:37 +0000 (+0200) Subject: simple-api: provide gpiod_simple_find_line() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=be50e252351711968f76ab2cd48ae18b1587d88d;p=qemu-gpiodev%2Flibgpiod.git simple-api: provide gpiod_simple_find_line() Implement a simple API function for looking up GPIO lines. Include a set of test cases. Signed-off-by: Bartosz Golaszewski --- diff --git a/include/gpiod.h b/include/gpiod.h index aa5376e..7e33197 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -241,6 +241,22 @@ int gpiod_simple_event_loop_multiple(const char *consumer, const char *device, gpiod_event_handle_cb event_cb, void *data) GPIOD_API; +/** + * @brief Determine the chip name and line offset of a line with given name. + * @param name The name of the GPIO line to lookup. + * @param chipname Buffer in which the name of the GPIO chip will be stored. + * @param chipname_size Size of the chip name buffer. + * @param offset Pointer to an integer in which the line offset will be stored. + * @return -1 on error, 0 if the line with given name doesn't exist and 1 if + * the line was found. In the first two cases the contents of chipname + * and offset remain unchanged. + * + * The chip name is truncated if the buffer can't hold its entire size. + */ +int gpiod_simple_find_line(const char *name, char *chipname, + size_t chipname_size, + unsigned int *offset) GPIOD_API; + /** * @} * diff --git a/src/lib/simple.c b/src/lib/simple.c index a1bb49b..ddbba03 100644 --- a/src/lib/simple.c +++ b/src/lib/simple.c @@ -12,6 +12,7 @@ #include +#include #include #include #include @@ -268,3 +269,25 @@ out: return ret; } + +int gpiod_simple_find_line(const char *name, char *chipname, + size_t chipname_size, unsigned int *offset) +{ + struct gpiod_chip *chip; + struct gpiod_line *line; + + line = gpiod_line_find(name); + if (!line) { + if (errno == ENOENT) + return 0; + else + return -1; + } + + chip = gpiod_line_get_chip(line); + snprintf(chipname, chipname_size, "%s", gpiod_chip_name(chip)); + *offset = gpiod_line_offset(line); + gpiod_chip_close(chip); + + return 1; +} diff --git a/tests/tests-simple-api.c b/tests/tests-simple-api.c index dba5731..fbf7baf 100644 --- a/tests/tests-simple-api.c +++ b/tests/tests-simple-api.c @@ -194,3 +194,49 @@ static void simple_event_loop_multiple(void) TEST_DEFINE(simple_event_loop_multiple, "gpiod_simple_event_loop_multiple() - single event", 0, { 8 }); + +static void simple_find_line_good(void) +{ + unsigned int offset; + char chip[32]; + int rv; + + rv = gpiod_simple_find_line("gpio-mockup-C-14", chip, + sizeof(chip), &offset); + TEST_ASSERT_EQ(rv, 1); + TEST_ASSERT_EQ(offset, 14); + TEST_ASSERT_STR_EQ(chip, test_chip_name(2)); +} +TEST_DEFINE(simple_find_line_good, + "gpiod_simple_find_line() - good", + TEST_FLAG_NAMED_LINES, { 8, 16, 16, 8 }); + +static void simple_find_line_truncated(void) +{ + unsigned int offset; + char chip[6]; + int rv; + + rv = gpiod_simple_find_line("gpio-mockup-C-14", chip, + sizeof(chip), &offset); + TEST_ASSERT_EQ(rv, 1); + TEST_ASSERT_EQ(offset, 14); + TEST_ASSERT_STR_EQ(chip, "gpioc"); +} +TEST_DEFINE(simple_find_line_truncated, + "gpiod_simple_find_line() - chip name truncated", + TEST_FLAG_NAMED_LINES, { 8, 16, 16, 8 }); + +static void simple_find_line_not_found(void) +{ + unsigned int offset; + char chip[32]; + int rv; + + rv = gpiod_simple_find_line("nonexistent", chip, + sizeof(chip), &offset); + TEST_ASSERT_EQ(rv, 0); +} +TEST_DEFINE(simple_find_line_not_found, + "gpiod_simple_find_line() - not found", + TEST_FLAG_NAMED_LINES, { 8, 16, 16, 8 });