gpiofind: new tool
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 6 Jan 2017 13:34:55 +0000 (14:34 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 6 Jan 2017 13:34:55 +0000 (14:34 +0100)
Add a simple program for finding the gpiochip and offset of a GPIO
line by name.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
.gitignore
Makefile.am
gpiofind.c [new file with mode: 0644]

index 2879c594bcb3d92571957772fc1bd0aa6555c1f5..67e5cadf20eaf1864294c7fde53dec30b85cfdaa 100644 (file)
@@ -5,6 +5,7 @@ gpioinfo
 gpioget
 gpioset
 gpiomon
+gpiofind
 *.o
 *.lo
 doc
index d996f870ac959f3aa8bc32f5dc70a39d6d59af8b..6dc31d8df348ec128167ef46580fccd192801b18 100644 (file)
@@ -19,7 +19,7 @@ include_HEADERS = gpiod.h
 
 libgpiod_la_SOURCES = core.c
 
-bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon
+bin_PROGRAMS = gpiodetect gpioinfo gpioget gpioset gpiomon gpiofind
 
 gpiodetect_SOURCES = gpiodetect.c
 gpiodetect_LDFLAGS = -lgpiod
@@ -41,6 +41,10 @@ gpiomon_SOURCES = gpiomon.c
 gpiomon_LDFLAGS = -lgpiod
 gpiomon_DEPENDENCIES = libgpiod.la
 
+gpiofind_SOURCES = gpiofind.c
+gpiofind_LDFLAGS = -lgpiod
+gpiofind_DEPENDENCIES = libgpiod.la
+
 doc:
        doxygen
 .PHONY: doc
diff --git a/gpiofind.c b/gpiofind.c
new file mode 100644 (file)
index 0000000..7c7c6f1
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Read value from GPIO line.
+ *
+ * Copyright (C) 2017 Bartosz Golaszewski <bartekgola@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ */
+
+#include <gpiod.h>
+
+#include <stdio.h>
+#include <string.h>
+
+int main(int argc, char **argv)
+{
+       struct gpiod_line *line;
+       struct gpiod_chip *chip;
+
+       if (argc < 2) {
+               fprintf(stderr, "%s: gpiochip must be specified\n", argv[0]);
+               return EXIT_FAILURE;
+       }
+
+       line = gpiod_line_find_by_name(argv[1]);
+       if (!line)
+               return EXIT_FAILURE;
+
+       chip = gpiod_line_get_chip(line);
+
+       printf("%s %u\n", gpiod_chip_name(chip), gpiod_line_offset(line));
+
+       gpiod_chip_close(chip);
+
+       return EXIT_SUCCESS;
+}