simple-api: provide gpiod_simple_find_line()
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 17:17:37 +0000 (19:17 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 17:17:37 +0000 (19:17 +0200)
Implement a simple API function for looking up GPIO lines. Include
a set of test cases.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
include/gpiod.h
src/lib/simple.c
tests/tests-simple-api.c

index aa5376ecd6fea923104894f5b5811d0f2aa9c660..7e33197f82db21a5920a8ad68b6c126cda780957 100644 (file)
@@ -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;
+
 /**
  * @}
  *
index a1bb49b4a1c1d550d9861495e103cbc84009234d..ddbba03eed8f5298ebcadbc23a0eeb13327b1710 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <gpiod.h>
 
+#include <stdio.h>
 #include <string.h>
 #include <errno.h>
 #include <poll.h>
@@ -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;
+}
index dba57314f17060c1899eca5eafc47898f5185c3e..fbf7bafd8ea26e54410f1239ae97db113233f0b7 100644 (file)
@@ -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 });