core: export gpiod_is_gpiochip_device()
authorBartosz Golaszewski <bgolaszewski@baylibre.com>
Tue, 24 Nov 2020 14:36:47 +0000 (15:36 +0100)
committerBartosz Golaszewski <bgolaszewski@baylibre.com>
Mon, 14 Dec 2020 14:56:37 +0000 (15:56 +0100)
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@baylibre.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
include/gpiod.h
lib/core.c
tests/tests-chip.c

index 355a99a99fbddf16bc48cdb1b72345b0f7612a35..742dfc250e699456a92ff49c924d01b65c653e9c 100644 (file)
@@ -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.
index bf52fa62baf828b1023af59fd63f4991400208dd..efba959b27fead83dbe4a8c9bed66bdf4b014a8e 100644 (file)
@@ -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));
index 289c4582ef39b5fddc89107ad833a4dfa4dbf811..2f19c49151237b558c3bb6905e8fd9eee55fe126 100644 (file)
 
 #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;