[libgpiod][PATCH] examples: simplify find_line_by_name

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 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




[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux