* @brief Find a GPIO line by its name.
* @param name Name of the GPIO line.
* @return Returns the GPIO line handle if the line exists in the system or
- * NULL if it couldn't be located.
+ * NULL if it couldn't be located or an error occurred.
*
* If this routine succeeds, the user must manually close the GPIO chip owning
- * this line to avoid memory leaks.
+ * this line to avoid memory leaks. If the line could not be found, this
+ * functions sets errno to ENOENT.
*/
struct gpiod_line * gpiod_line_find(const char *name) GPIOD_API;
struct gpiod_line * gpiod_line_find(const char *name)
{
- struct gpiod_chip_iter *chip_iter;
- struct gpiod_line_iter line_iter;
+ struct gpiod_chip_iter *iter;
struct gpiod_chip *chip;
struct gpiod_line *line;
- const char *line_name;
- chip_iter = gpiod_chip_iter_new();
- if (!chip_iter)
+ iter = gpiod_chip_iter_new();
+ if (!iter)
return NULL;
- gpiod_foreach_chip(chip_iter, chip) {
- if (gpiod_chip_iter_err(chip_iter))
- continue;
-
- gpiod_line_iter_init(&line_iter, chip);
- gpiod_foreach_line(&line_iter, line) {
- if (gpiod_line_iter_err(&line_iter))
- continue;
-
- line_name = gpiod_line_name(line);
- if (!line_name)
- continue;
+ gpiod_foreach_chip(iter, chip) {
+ if (gpiod_chip_iter_err(iter))
+ goto out;
- if (strcmp(line_name, name) == 0) {
- gpiod_chip_iter_free_noclose(chip_iter);
- return line;
- }
+ line = gpiod_chip_find_line(chip, name);
+ if (line) {
+ gpiod_chip_iter_free_noclose(iter);
+ return line;
}
+
+ if (errno != ENOENT)
+ goto out;
}
- gpiod_chip_iter_free(chip_iter);
+ errno = ENOENT;
+
+out:
+ gpiod_chip_iter_free(iter);
return NULL;
}
"gpiod_line_set_value() - good",
0, { 8 });
-static void line_find_by_name_good(void)
+static void line_find_good(void)
{
TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL;
TEST_ASSERT_EQ(gpiod_line_offset(line), 12);
}
-TEST_DEFINE(line_find_by_name_good,
- "gpiod_line_find_by_name() - good",
+TEST_DEFINE(line_find_good,
+ "gpiod_line_find() - good",
+ TEST_FLAG_NAMED_LINES, { 16, 16, 32, 16 });
+
+static void line_find_not_found(void)
+{
+ TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL;
+
+ line = gpiod_line_find("nonexistent");
+ TEST_ASSERT_NULL(line);
+ TEST_ASSERT_ERRNO_IS(ENOENT);
+}
+TEST_DEFINE(line_find_not_found,
+ "gpiod_line_find() - not found",
TEST_FLAG_NAMED_LINES, { 16, 16, 32, 16 });
static void line_direction(void)