* @return GPIO chip handle or NULL if an error occurred.
*
* This routine tries to figure out whether the user passed it the path to
- * the GPIO chip, its name or number as a string. Then it tries to open it
- * using one of the other gpiod_chip_open** routines.
+ * the GPIO chip, its name, label or number as a string. Then it tries to
+ * open it using one of the other gpiod_chip_open** routines.
*/
struct gpiod_chip * gpiod_chip_open_lookup(const char *descr) GPIOD_API;
struct gpiod_chip * gpiod_chip_open_lookup(const char *descr)
{
- if (is_unsigned_int(descr))
- return gpiod_chip_open_by_number(strtoul(descr, NULL, 10));
- else if (strncmp(descr, dev_dir, sizeof(dev_dir) - 1) != 0)
- return gpiod_chip_open_by_name(descr);
- else
- return gpiod_chip_open(descr);
+ struct gpiod_chip *chip;
+
+ if (is_unsigned_int(descr)) {
+ chip = gpiod_chip_open_by_number(strtoul(descr, NULL, 10));
+ } else {
+ chip = gpiod_chip_open_by_label(descr);
+ if (!chip) {
+ if (strncmp(descr, dev_dir, sizeof(dev_dir) - 1))
+ chip = gpiod_chip_open_by_name(descr);
+ else
+ chip = gpiod_chip_open(descr);
+ }
+ }
+
+ return chip;
}
void gpiod_chip_close(struct gpiod_chip *chip)
static void chip_open_lookup(void)
{
+ GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_label = NULL;
GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_name = NULL;
GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_path = NULL;
GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip_by_num = NULL;
GU_CLEANUP(gu_free_str) char *chip_num;
- GU_ASSERT(asprintf(&chip_num, "%u", gu_chip_num(0)) > 0);
+ GU_ASSERT(asprintf(&chip_num, "%u", gu_chip_num(1)) > 0);
- chip_by_name = gpiod_chip_open_lookup(gu_chip_name(0));
- chip_by_path = gpiod_chip_open_lookup(gu_chip_path(0));
+ chip_by_name = gpiod_chip_open_lookup(gu_chip_name(1));
+ chip_by_path = gpiod_chip_open_lookup(gu_chip_path(1));
chip_by_num = gpiod_chip_open_lookup(chip_num);
+ chip_by_label = gpiod_chip_open_lookup("gpio-mockup-B");
GU_ASSERT_NOT_NULL(chip_by_name);
GU_ASSERT_NOT_NULL(chip_by_path);
GU_ASSERT_NOT_NULL(chip_by_num);
+ GU_ASSERT_NOT_NULL(chip_by_label);
+
+ GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_name), gu_chip_name(1));
+ GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_path), gu_chip_name(1));
+ GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_num), gu_chip_name(1));
+ GU_ASSERT_STR_EQ(gpiod_chip_name(chip_by_label), gu_chip_name(1));
}
GU_DEFINE_TEST(chip_open_lookup,
"gpiod_chip_open_lookup()",
- GU_LINES_UNNAMED, { 8 });
+ GU_LINES_UNNAMED, { 8, 8, 8 });
static void chip_open_by_label_good(void)
{