};
};
+enum {
+ CHIP_ITER_INIT = 0,
+ CHIP_ITER_DONE,
+ CHIP_ITER_ERR,
+};
+
struct gpiod_chip_iter
{
DIR *dir;
struct gpiod_chip *current;
+ int state;
};
static const char dev_dir[] = "/dev/";
return NULL;
}
+ new->state = CHIP_ITER_INIT;
+
return new;
}
status = strncmp(dentry->d_name, cdev_prefix,
sizeof(cdev_prefix) - 1);
if (status == 0) {
+ iter->state = CHIP_ITER_INIT;
+
chip = gpiod_chip_open_by_name(dentry->d_name);
+ if (!chip)
+ iter->state = CHIP_ITER_ERR;
+
iter->current = chip;
return iter->current;
}
}
+ iter->state = CHIP_ITER_DONE;
return NULL;
}
+
+bool gpiod_chip_iter_done(struct gpiod_chip_iter *iter)
+{
+ return iter->state == CHIP_ITER_DONE;
+}
+
+bool gpiod_chip_iter_iserr(struct gpiod_chip_iter *iter)
+{
+ return iter->state == CHIP_ITER_ERR;
+}
*/
#define gpiod_foreach_chip(iter, chip) \
for ((chip) = gpiod_chip_iter_next(iter); \
- (chip); \
+ !gpiod_chip_iter_done(iter); \
(chip) = gpiod_chip_iter_next(iter))
+/**
+ * @brief Check if we're done iterating over gpiochips on this iterator.
+ * @param iter The gpiochip iterator object.
+ * @return True if we've iterated over all chips, false otherwise.
+ */
+bool gpiod_chip_iter_done(struct gpiod_chip_iter *iter) GPIOD_API;
+
+/**
+ * @brief Check if we've encountered an error condition while opening a
+ * gpiochip.
+ * @param iter The gpiochip iterator object.
+ * @return True if there was an error opening a gpiochip device file,
+ * false otherwise.
+ */
+bool gpiod_chip_iter_iserr(struct gpiod_chip_iter *iter) GPIOD_API;
+
/**
* @brief GPIO line iterator structure.
*
die_perror("unable to access GPIO chips");
gpiod_foreach_chip(iter, chip) {
+ if (gpiod_chip_iter_iserr(iter))
+ die_perror("error accessing gpiochip");
+
printf("%s [%s] (%u lines)\n",
gpiod_chip_name(chip),
gpiod_chip_label(chip),