Skip to contentSkip to conversation

examples: simplify find_line_by_name

PATCH Applied Kent Gibson 2 participants 2 messages Jul 27, 2023 – Jul 28, 2023 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 examples: simplify find_line_by_name

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.

---
 examples/find_line_by_name.c | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)
examples/find_line_by_name.c +11 -20
index 16d2c4c..87b66ac 100644
@@ -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);
 	}
 
Signature · 1 line
2.41.0
#1 lore raw
Quoted 66 lines · 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.

---
 examples/find_line_by_name.c | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)
examples/find_line_by_name.c +11 -21
index 16d2c4c..87b66ac 100644
@@ -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);
        }
 
--
Quoted 2 lines

2.41.0

Applied, thanks!

Bart