From: Bartosz Golaszewski Date: Wed, 11 Jan 2017 10:06:50 +0000 (+0100) Subject: iterators: extend chip iterator error handling X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=8ea028f0d5dc60b1fc42fb5a9dbfbca3de6ec9f6;p=qemu-gpiodev%2Flibgpiod.git iterators: extend chip iterator error handling Add a function that allows to retrieve the name of the gpiochip that we failed to open from an iterator routine. Use it in gpiodetect. Signed-off-by: Bartosz Golaszewski --- diff --git a/core.c b/core.c index e7696d7..2e1e360 100644 --- a/core.c +++ b/core.c @@ -63,6 +63,7 @@ struct gpiod_chip_iter DIR *dir; struct gpiod_chip *current; int state; + char *failed_chip; }; static const char dev_dir[] = "/dev/"; @@ -955,6 +956,8 @@ void gpiod_chip_iter_free(struct gpiod_chip_iter *iter) void gpiod_chip_iter_free_noclose(struct gpiod_chip_iter *iter) { closedir(iter->dir); + if (iter->failed_chip) + free(iter->failed_chip); free(iter); } @@ -976,10 +979,17 @@ struct gpiod_chip * gpiod_chip_iter_next(struct gpiod_chip_iter *iter) sizeof(cdev_prefix) - 1); if (status == 0) { iter->state = CHIP_ITER_INIT; + if (iter->failed_chip) { + free(iter->failed_chip); + iter->failed_chip = NULL; + } chip = gpiod_chip_open_by_name(dentry->d_name); - if (!chip) + if (!chip) { iter->state = CHIP_ITER_ERR; + iter->failed_chip = strdup(dentry->d_name); + /* No point in an error check here. */ + } iter->current = chip; return iter->current; @@ -999,3 +1009,9 @@ bool gpiod_chip_iter_iserr(struct gpiod_chip_iter *iter) { return iter->state == CHIP_ITER_ERR; } + +const char * +gpiod_chip_iter_failed_chip(struct gpiod_chip_iter *iter) +{ + return iter->failed_chip; +} diff --git a/gpiod.h b/gpiod.h index 5cf3254..4d2e7bc 100644 --- a/gpiod.h +++ b/gpiod.h @@ -723,6 +723,19 @@ bool gpiod_chip_iter_done(struct gpiod_chip_iter *iter) GPIOD_API; */ bool gpiod_chip_iter_iserr(struct gpiod_chip_iter *iter) GPIOD_API; +/** + * @brief Get the name of the gpiochip that we failed to access. + * @param iter The gpiochip iterator object. + * @return If gpiod_chip_iter_iserr() returned true, this function returns a + * pointer to the name of the gpiochip that we failed to access. + * If there was no error this function returns NULL. + * + * NOTE: this function will return NULL if the internal memory allocation + * fails. + */ +const char * +gpiod_chip_iter_failed_chip(struct gpiod_chip_iter *iter) GPIOD_API; + /** * @brief GPIO line iterator structure. * diff --git a/gpiodetect.c b/gpiodetect.c index aab022d..c35b618 100644 --- a/gpiodetect.c +++ b/gpiodetect.c @@ -66,7 +66,8 @@ int main(int argc, char **argv) gpiod_foreach_chip(iter, chip) { if (gpiod_chip_iter_iserr(iter)) - die_perror("error accessing gpiochip"); + die_perror("error accessing gpiochip %s", + gpiod_chip_iter_failed_chip(iter)); printf("%s [%s] (%u lines)\n", gpiod_chip_name(chip),