linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libgpiod][PATCH] examples: simplify find_line_by_name
@ 2023-07-27 21:10 Kent Gibson
  2023-07-28 15:00 ` Bartosz Golaszewski
  0 siblings, 1 reply; 2+ messages in thread
From: Kent Gibson @ 2023-07-27 21:10 UTC (permalink / raw)
  To: linux-gpio, brgl; +Cc: Kent Gibson

Simplify the find_line_by_name example by using
gpiod_chip_get_line_offset_from_name() rather than iterating over each
line in a chip.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 examples/find_line_by_name.c | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/examples/find_line_by_name.c b/examples/find_line_by_name.c
index 16d2c4c..87b66ac 100644
--- a/examples/find_line_by_name.c
+++ b/examples/find_line_by_name.c
@@ -69,12 +69,9 @@ int main(void)
 	static const char *const line_name = "GPIO19";
 
 	struct gpiod_chip_info *cinfo;
-	struct gpiod_line_info *linfo;
-	unsigned int j, num_lines;
 	struct gpiod_chip *chip;
 	char **chip_paths;
-	const char *name;
-	int i, num_chips;
+	int i, num_chips, offset;
 
 	/*
 	 * Names are not guaranteed unique, so this finds the first line with
@@ -85,26 +82,20 @@ int main(void)
 		chip = gpiod_chip_open(chip_paths[i]);
 		if (!chip)
 			continue;
+
+		offset = gpiod_chip_get_line_offset_from_name(chip, line_name);
+		if (offset == -1)
+			goto close_chip;
+
 		cinfo = gpiod_chip_get_info(chip);
 		if (!cinfo)
-			continue;
+			goto close_chip;
 
-		num_lines = gpiod_chip_info_get_num_lines(cinfo);
-		for (j = 0; j < num_lines; j++) {
-			linfo = gpiod_chip_get_line_info(chip, j);
-			if (!linfo)
-				continue;
-			name = gpiod_line_info_get_name(linfo);
-			if (name && (strcmp(line_name, name) == 0)) {
-				printf("%s: %s %d\n", line_name,
-				       gpiod_chip_info_get_name(cinfo), j);
-				return EXIT_SUCCESS;
-			}
-
-			gpiod_line_info_free(linfo);
-		}
+		printf("%s: %s %d\n", line_name,
+				       gpiod_chip_info_get_name(cinfo), offset);
+		return EXIT_SUCCESS;
 
-		gpiod_chip_info_free(cinfo);
+close_chip:
 		gpiod_chip_close(chip);
 	}
 
-- 
2.41.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-28 15:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-27 21:10 [libgpiod][PATCH] examples: simplify find_line_by_name Kent Gibson
2023-07-28 15:00 ` Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).