From 87ddbe9a6a700492ecaebcbe5715787fe78b52e2 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Sat, 24 Jun 2017 13:05:34 +0200 Subject: [PATCH] core: move iterator definitions to a separate file Move all iterator-related code to iter.c. Signed-off-by: Bartosz Golaszewski --- src/lib/Makefile.am | 2 +- src/lib/core.c | 125 --------------------------------------- src/lib/iter.c | 138 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 126 deletions(-) create mode 100644 src/lib/iter.c diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index dbcd8a4..9128f64 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -7,7 +7,7 @@ # lib_LTLIBRARIES = libgpiod.la -libgpiod_la_SOURCES = core.c simple.c +libgpiod_la_SOURCES = core.c iter.c simple.c libgpiod_la_CFLAGS = -Wall -Wextra -g libgpiod_la_CFLAGS += -fvisibility=hidden -I$(top_srcdir)/include/ libgpiod_la_CFLAGS += -include $(top_builddir)/config.h diff --git a/src/lib/core.c b/src/lib/core.c index 3864f94..59c525c 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -51,19 +51,6 @@ struct gpiod_line { }; }; -enum { - CHIP_ITER_INIT = 0, - CHIP_ITER_DONE, - CHIP_ITER_ERR, -}; - -struct gpiod_chip_iter { - DIR *dir; - struct gpiod_chip *current; - int state; - char *failed_chip; -}; - static const char dev_dir[] = "/dev/"; static const char cdev_prefix[] = "gpiochip"; @@ -899,118 +886,6 @@ struct gpiod_chip * gpiod_line_get_chip(struct gpiod_line *line) return line->chip; } -struct gpiod_chip_iter * gpiod_chip_iter_new(void) -{ - struct gpiod_chip_iter *new; - - new = malloc(sizeof(*new)); - if (!new) - return NULL; - - memset(new, 0, sizeof(*new)); - - new->dir = opendir(dev_dir); - if (!new->dir) - return NULL; - - new->state = CHIP_ITER_INIT; - - return new; -} - -void gpiod_chip_iter_free(struct gpiod_chip_iter *iter) -{ - if (iter->current) - gpiod_chip_close(iter->current); - - gpiod_chip_iter_free_noclose(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); -} - -struct gpiod_chip * gpiod_chip_iter_next(struct gpiod_chip_iter *iter) -{ - if (iter->current) { - gpiod_chip_close(iter->current); - iter->current = NULL; - } - - return gpiod_chip_iter_next_noclose(iter); -} - -struct gpiod_chip * gpiod_chip_iter_next_noclose(struct gpiod_chip_iter *iter) -{ - struct gpiod_chip *chip; - struct dirent *dentry; - int status; - - for (dentry = readdir(iter->dir); - dentry; - dentry = readdir(iter->dir)) { - status = strncmp(dentry->d_name, cdev_prefix, - 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) { - 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; - } - } - - 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_err(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; -} - -struct gpiod_line * gpiod_line_iter_next(struct gpiod_line_iter *iter) -{ - struct gpiod_line *line; - - if (iter->offset >= gpiod_chip_num_lines(iter->chip)) { - iter->state = GPIOD_LINE_ITER_DONE; - return NULL; - } - - iter->state = GPIOD_LINE_ITER_INIT; - line = gpiod_chip_get_line(iter->chip, iter->offset++); - if (!line) - iter->state = GPIOD_LINE_ITER_ERR; - - return line; -} - const char * gpiod_version_string(void) { return GPIOD_VERSION_STR; diff --git a/src/lib/iter.c b/src/lib/iter.c new file mode 100644 index 0000000..e81bf71 --- /dev/null +++ b/src/lib/iter.c @@ -0,0 +1,138 @@ +/* + * GPIO chip and line iterators for libgpiod. + * + * Copyright (C) 2017 Bartosz Golaszewski + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2.1 of the GNU Lesser General Public License + * as published by the Free Software Foundation. + */ + +#include + +#include +#include + +enum { + CHIP_ITER_INIT = 0, + CHIP_ITER_DONE, + CHIP_ITER_ERR, +}; + +struct gpiod_chip_iter { + DIR *dir; + struct gpiod_chip *current; + int state; + char *failed_chip; +}; + +struct gpiod_chip_iter * gpiod_chip_iter_new(void) +{ + struct gpiod_chip_iter *new; + + new = malloc(sizeof(*new)); + if (!new) + return NULL; + + memset(new, 0, sizeof(*new)); + + new->dir = opendir("/dev"); + if (!new->dir) + return NULL; + + new->state = CHIP_ITER_INIT; + + return new; +} + +void gpiod_chip_iter_free(struct gpiod_chip_iter *iter) +{ + if (iter->current) + gpiod_chip_close(iter->current); + + gpiod_chip_iter_free_noclose(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); +} + +struct gpiod_chip * gpiod_chip_iter_next(struct gpiod_chip_iter *iter) +{ + if (iter->current) { + gpiod_chip_close(iter->current); + iter->current = NULL; + } + + return gpiod_chip_iter_next_noclose(iter); +} + +struct gpiod_chip * gpiod_chip_iter_next_noclose(struct gpiod_chip_iter *iter) +{ + struct gpiod_chip *chip; + struct dirent *dentry; + int status; + + for (dentry = readdir(iter->dir); + dentry; + dentry = readdir(iter->dir)) { + status = strncmp(dentry->d_name, "gpiochip", 8); + 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) { + 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; + } + } + + 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_err(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; +} + +struct gpiod_line * gpiod_line_iter_next(struct gpiod_line_iter *iter) +{ + struct gpiod_line *line; + + if (iter->offset >= gpiod_chip_num_lines(iter->chip)) { + iter->state = GPIOD_LINE_ITER_DONE; + return NULL; + } + + iter->state = GPIOD_LINE_ITER_INIT; + line = gpiod_chip_get_line(iter->chip, iter->offset++); + if (!line) + iter->state = GPIOD_LINE_ITER_ERR; + + return line; +} -- 2.30.2