From 1333f6225f869284931008593fb409abed5a2560 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Wed, 4 Jan 2017 22:52:54 +0100 Subject: [PATCH] core: implement gpiod_line_find_by_name() Implement a routine for finding lines by name. Signed-off-by: Bartosz Golaszewski --- core.c | 28 ++++++++++++++++++++++++++++ gpiod.h | 2 ++ 2 files changed, 30 insertions(+) diff --git a/core.c b/core.c index 2c6bd56..a90c2ec 100644 --- a/core.c +++ b/core.c @@ -629,3 +629,31 @@ struct gpiod_chip * gpiod_chip_iter_next(struct gpiod_chip_iter *iter) return NULL; } + +struct gpiod_line * gpiod_line_find_by_name(const char *name) +{ + struct gpiod_chip_iter *chip_iter; + struct gpiod_line_iter line_iter; + struct gpiod_chip *chip; + struct gpiod_line *line; + + chip_iter = gpiod_chip_iter_new(); + if (!chip_iter) + return NULL; + + gpiod_foreach_chip(chip_iter, chip) { + gpiod_line_iter_init(&line_iter, chip); + gpiod_foreach_line(&line_iter, line) { + if (strcmp(gpiod_line_name(line), name) == 0) { + /* TODO A separate function for that maybe? */ + closedir(chip_iter->dir); + free(chip_iter); + return line; + } + } + } + + gpiod_chip_iter_free(chip_iter); + + return NULL; +} diff --git a/gpiod.h b/gpiod.h index d223b1c..e228dd2 100644 --- a/gpiod.h +++ b/gpiod.h @@ -135,6 +135,8 @@ int gpiod_line_set_value(struct gpiod_line *line, int value) GPIOD_API; int gpiod_line_set_value_bulk(struct gpiod_line_bulk *line_bulk, int *values) GPIOD_API; +struct gpiod_line * gpiod_line_find_by_name(const char *name) GPIOD_API; + struct gpiod_chip; struct gpiod_chip * gpiod_chip_open(const char *path) GPIOD_API; -- 2.30.2