gpiod_event_handle_cb event_cb,
void *data) GPIOD_API;
+/**
+ * @brief Determine the chip name and line offset of a line with given name.
+ * @param name The name of the GPIO line to lookup.
+ * @param chipname Buffer in which the name of the GPIO chip will be stored.
+ * @param chipname_size Size of the chip name buffer.
+ * @param offset Pointer to an integer in which the line offset will be stored.
+ * @return -1 on error, 0 if the line with given name doesn't exist and 1 if
+ * the line was found. In the first two cases the contents of chipname
+ * and offset remain unchanged.
+ *
+ * The chip name is truncated if the buffer can't hold its entire size.
+ */
+int gpiod_simple_find_line(const char *name, char *chipname,
+ size_t chipname_size,
+ unsigned int *offset) GPIOD_API;
+
/**
* @}
*
#include <gpiod.h>
+#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
return ret;
}
+
+int gpiod_simple_find_line(const char *name, char *chipname,
+ size_t chipname_size, unsigned int *offset)
+{
+ struct gpiod_chip *chip;
+ struct gpiod_line *line;
+
+ line = gpiod_line_find(name);
+ if (!line) {
+ if (errno == ENOENT)
+ return 0;
+ else
+ return -1;
+ }
+
+ chip = gpiod_line_get_chip(line);
+ snprintf(chipname, chipname_size, "%s", gpiod_chip_name(chip));
+ *offset = gpiod_line_offset(line);
+ gpiod_chip_close(chip);
+
+ return 1;
+}
TEST_DEFINE(simple_event_loop_multiple,
"gpiod_simple_event_loop_multiple() - single event",
0, { 8 });
+
+static void simple_find_line_good(void)
+{
+ unsigned int offset;
+ char chip[32];
+ int rv;
+
+ rv = gpiod_simple_find_line("gpio-mockup-C-14", chip,
+ sizeof(chip), &offset);
+ TEST_ASSERT_EQ(rv, 1);
+ TEST_ASSERT_EQ(offset, 14);
+ TEST_ASSERT_STR_EQ(chip, test_chip_name(2));
+}
+TEST_DEFINE(simple_find_line_good,
+ "gpiod_simple_find_line() - good",
+ TEST_FLAG_NAMED_LINES, { 8, 16, 16, 8 });
+
+static void simple_find_line_truncated(void)
+{
+ unsigned int offset;
+ char chip[6];
+ int rv;
+
+ rv = gpiod_simple_find_line("gpio-mockup-C-14", chip,
+ sizeof(chip), &offset);
+ TEST_ASSERT_EQ(rv, 1);
+ TEST_ASSERT_EQ(offset, 14);
+ TEST_ASSERT_STR_EQ(chip, "gpioc");
+}
+TEST_DEFINE(simple_find_line_truncated,
+ "gpiod_simple_find_line() - chip name truncated",
+ TEST_FLAG_NAMED_LINES, { 8, 16, 16, 8 });
+
+static void simple_find_line_not_found(void)
+{
+ unsigned int offset;
+ char chip[32];
+ int rv;
+
+ rv = gpiod_simple_find_line("nonexistent", chip,
+ sizeof(chip), &offset);
+ TEST_ASSERT_EQ(rv, 0);
+}
+TEST_DEFINE(simple_find_line_not_found,
+ "gpiod_simple_find_line() - not found",
+ TEST_FLAG_NAMED_LINES, { 8, 16, 16, 8 });