From: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> We'll be dropping chip iterators treewide. Instead we'll encourage scanning /dev for GPIO chip devices. Exporting this function allows users to easily check if given file represents a GPIO chip character device. Signed-off-by: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> --- include/gpiod.h | 8 ++++++++ lib/core.c | 4 ++-- tests/tests-chip.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/gpiod.h b/include/gpiod.h index 355a99a..742dfc2 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -71,6 +71,14 @@ struct gpiod_line_bulk; * Functions and data structures dealing with GPIO chips. */ +/** + * @brief Check if the file pointed to by path is a GPIO chip character device. + * @param path Path to check. + * @return True if the file exists and is a GPIO chip character device or a + * symbolic link to it. + */ +bool gpiod_is_gpiochip_device(const char *path) GPIOD_API; + /** * @brief Open a gpiochip by path. * @param path Path to the gpiochip device file. diff --git a/lib/core.c b/lib/core.c index bf52fa6..efba959 100644 --- a/lib/core.c +++ b/lib/core.c @@ -177,7 +177,7 @@ void gpiod_line_bulk_foreach_line(struct gpiod_line_bulk *bulk, (index) < (bulk)->num_lines; \ (index)++, (line) = (bulk)->lines[(index)]) -static bool is_gpiochip_cdev(const char *path) +bool gpiod_is_gpiochip_device(const char *path) { char *name, *realname, *sysfsp, sysfsdev[16], devstr[16]; struct stat statbuf; @@ -282,7 +282,7 @@ struct gpiod_chip *gpiod_chip_open(const char *path) * We were able to open the file but is it really a gpiochip character * device? */ - if (!is_gpiochip_cdev(path)) + if (!gpiod_is_gpiochip_device(path)) goto err_close_fd; chip = malloc(sizeof(*chip)); diff --git a/tests/tests-chip.c b/tests/tests-chip.c index 289c458..2f19c49 100644 --- a/tests/tests-chip.c +++ b/tests/tests-chip.c @@ -11,6 +11,21 @@ #define GPIOD_TEST_GROUP "chip" +GPIOD_TEST_CASE(is_gpiochip_good, 0, { 8 }) +{ + g_assert_true(gpiod_is_gpiochip_device(gpiod_test_chip_path(0))); +} + +GPIOD_TEST_CASE(is_gpiochip_bad, 0, { 8 }) +{ + g_assert_false(gpiod_is_gpiochip_device("/dev/null")); +} + +GPIOD_TEST_CASE(is_gpiochip_nonexistent, 0, { 8 }) +{ + g_assert_false(gpiod_is_gpiochip_device("/dev/nonexistent_gpiochip")); +} + GPIOD_TEST_CASE(open_good, 0, { 8 }) { g_autoptr(gpiod_chip_struct) chip = NULL; -- 2.29.1