core: provide gpiod_line_get()
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 16:47:51 +0000 (18:47 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 16:47:51 +0000 (18:47 +0200)
Provide a routine which simplifies the process of getting the handle
for a single GPIO line by encapsulating the calls opening the chip and
retrieving the line handle in a single function.

Implement test cases as well.

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

index e1b9f91533e4ffd7bccb6c8da99870825fde5fc7..aa5376ecd6fea923104894f5b5811d0f2aa9c660 100644 (file)
@@ -879,6 +879,21 @@ int gpiod_line_set_value(struct gpiod_line *line, int value) GPIOD_API;
 int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk,
                              int *values) GPIOD_API;
 
+/**
+ * @brief Get a GPIO line handle by GPIO chip description and offset.
+ * @param device String describing the gpiochip.
+ * @param offset The offset of the GPIO line.
+ * @return GPIO line handle or NULL if an error occurred.
+ *
+ * This routine provides a shorter alternative to calling
+ * ::gpiod_chip_open_lookup and ::gpiod_chip_get_line.
+ *
+ * If this function succeeds, the caller is responsible for closing the
+ * associated GPIO chip.
+ */
+struct gpiod_line *
+gpiod_line_get(const char *device, unsigned int offset) GPIOD_API;
+
 /**
  * @brief Find a GPIO line by its name.
  * @param name Name of the GPIO line.
index 5b41f1d8a50cd822c26f340aa246dcf403001f71..eabee8961cda608ea7f2c82e46be0224180c079d 100644 (file)
@@ -921,6 +921,24 @@ int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, int *values)
        return 0;
 }
 
+struct gpiod_line * gpiod_line_get(const char *device, unsigned int offset)
+{
+       struct gpiod_chip *chip;
+       struct gpiod_line *line;
+
+       chip = gpiod_chip_open_lookup(device);
+       if (!chip)
+               return NULL;
+
+       line = gpiod_chip_get_line(chip, offset);
+       if (!line) {
+               gpiod_chip_close(chip);
+               return NULL;
+       }
+
+       return line;
+}
+
 struct gpiod_line * gpiod_line_find(const char *name)
 {
        struct gpiod_chip_iter *iter;
index 8e283b678703b3b99ed3d5fa051d1d0e18b56b96..41b78fd1881f606cbff22c1b5cacc3b1feb1dc98 100644 (file)
@@ -278,6 +278,30 @@ TEST_DEFINE(line_set_value,
            "gpiod_line_set_value() - good",
            0, { 8 });
 
+static void line_get_good(void)
+{
+       TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL;
+
+       line = gpiod_line_get(test_chip_name(2), 18);
+       TEST_ASSERT_NOT_NULL(line);
+       TEST_ASSERT_EQ(gpiod_line_offset(line), 18);
+}
+TEST_DEFINE(line_get_good,
+           "gpiod_line_get() - good",
+           TEST_FLAG_NAMED_LINES, { 16, 16, 32, 16 });
+
+static void line_get_invalid_offset(void)
+{
+       TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL;
+
+       line = gpiod_line_get(test_chip_name(3), 18);
+       TEST_ASSERT_NULL(line);
+       TEST_ASSERT_ERRNO_IS(EINVAL);
+}
+TEST_DEFINE(line_get_invalid_offset,
+           "gpiod_line_get() - invalid offset",
+           TEST_FLAG_NAMED_LINES, { 16, 16, 32, 16 });
+
 static void line_find_good(void)
 {
        TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL;