core: implement gpiod_chip_find_line()
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 13:24:00 +0000 (15:24 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 29 Sep 2017 15:15:36 +0000 (17:15 +0200)
The low-level line lookup should be more fine-grained. As the first
step: introduce a routine performing a line lookup for specific chip.

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

index 506f3e48fc578c7f0cf512e4ab03f6282edd5ebe..379b88cbb8253bd20f3e075dfe275291c16cdd6e 100644 (file)
@@ -330,6 +330,19 @@ unsigned int gpiod_chip_num_lines(struct gpiod_chip *chip) GPIOD_API;
 struct gpiod_line *
 gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset) GPIOD_API;
 
+/**
+ * @brief Find a GPIO line by name among lines associated with given GPIO chip.
+ * @param chip The GPIO chip object.
+ * @param name The name of the GPIO line.
+ * @return Pointer to the GPIO line handle or NULL if the line could not be
+ *         found or an error occurred.
+ *
+ * In case a line with given name is not associated with given chip, the
+ * functions sets errno to ENOENT.
+ */
+struct gpiod_line *
+gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) GPIOD_API;
+
 /**
  * @}
  *
index 2dc78b4e2ad5a2a7093b18d25421162ac6bd9393..3b3b6efb129515ad508c269e5b138ba01eb21cfc 100644 (file)
@@ -237,6 +237,26 @@ gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset)
        return line;
 }
 
+struct gpiod_line *
+gpiod_chip_find_line(struct gpiod_chip *chip, const char *name)
+{
+       struct gpiod_line_iter iter;
+       struct gpiod_line *line;
+
+       gpiod_line_iter_init(&iter, chip);
+       gpiod_foreach_line(&iter, line) {
+               if (gpiod_line_iter_err(&iter))
+                       return NULL;
+
+               if (strcmp(gpiod_line_name(line), name) == 0)
+                       return line;
+       }
+
+       errno = ENOENT;
+
+       return NULL;
+}
+
 static int line_get_state(struct gpiod_line *line)
 {
        return line->state;