From 8e164e9926eb32f16382b5235bca550aff570648 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Wed, 12 Jul 2017 20:17:01 +0200 Subject: [PATCH] core: set the active state using the request flags Having to always set the active state in the line request config structure makes users create unnecessary boilerplate code. Remove the active state field from the config structure and add a new flag, which when set, sets the active state to low, while high is the default. This patch updates the main request function and all the callers - we still need to update all the helper request routines. Signed-off-by: Bartosz Golaszewski --- include/gpiod.h | 14 ++------------ src/lib/core.c | 31 ++++++++----------------------- src/tools/gpiomon.c | 4 +--- tests/tests-line.c | 5 +---- 4 files changed, 12 insertions(+), 42 deletions(-) diff --git a/include/gpiod.h b/include/gpiod.h index e423cdd..a8196c9 100644 --- a/include/gpiod.h +++ b/include/gpiod.h @@ -287,16 +287,6 @@ enum { /**< Only watch falling edge events. */ }; -/** - * @brief Available active states for line requests. - */ -enum { - GPIOD_REQUEST_ACTIVE_HIGH, - /**< Request the line(s) with active-high state. */ - GPIOD_REQUEST_ACTIVE_LOW, - /**< Request the line(s) with active-low state. */ -}; - /** * @brief Miscellaneous GPIO flags. */ @@ -305,6 +295,8 @@ enum { /**< The line is an open-drain port. */ GPIOD_REQUEST_OPEN_SOURCE = GPIOD_BIT(1), /**< The line is an open-source port. */ + GPIOD_REQUEST_ACTIVE_LOW = GPIOD_BIT(2), + /**< The active state of the line is low (high is the default). */ }; /** @@ -470,8 +462,6 @@ struct gpiod_line_request_config { /**< Name of the consumer. */ int request_type; /**< Request type. */ - int active_state; - /**< Requested active state configuration. */ int flags; /**< Other configuration flags. */ }; diff --git a/src/lib/core.c b/src/lib/core.c index 3fba33d..1e07b82 100644 --- a/src/lib/core.c +++ b/src/lib/core.c @@ -228,8 +228,7 @@ int gpiod_line_request_input(struct gpiod_line *line, struct gpiod_line_request_config config = { .consumer = consumer, .request_type = GPIOD_REQUEST_DIRECTION_INPUT, - .active_state = active_low ? GPIOD_REQUEST_ACTIVE_LOW - : GPIOD_REQUEST_ACTIVE_HIGH, + .flags = active_low ? GPIOD_REQUEST_ACTIVE_LOW : 0, }; return gpiod_line_request(line, &config, 0); @@ -241,8 +240,7 @@ int gpiod_line_request_output(struct gpiod_line *line, const char *consumer, struct gpiod_line_request_config config = { .consumer = consumer, .request_type = GPIOD_REQUEST_DIRECTION_OUTPUT, - .active_state = active_low ? GPIOD_REQUEST_ACTIVE_LOW - : GPIOD_REQUEST_ACTIVE_HIGH, + .flags = active_low ? GPIOD_REQUEST_ACTIVE_LOW : 0, }; return gpiod_line_request(line, &config, default_val); @@ -295,19 +293,14 @@ static int line_request_values(struct gpiod_line_bulk *bulk, req->flags |= GPIOHANDLE_REQUEST_OPEN_DRAIN; if (config->flags & GPIOD_REQUEST_OPEN_SOURCE) req->flags |= GPIOHANDLE_REQUEST_OPEN_SOURCE; + if (config->flags & GPIOD_REQUEST_ACTIVE_LOW) + req->flags |= GPIOHANDLE_REQUEST_ACTIVE_LOW; if (config->request_type == GPIOD_REQUEST_DIRECTION_INPUT) req->flags |= GPIOHANDLE_REQUEST_INPUT; else if (config->request_type == GPIOD_REQUEST_DIRECTION_OUTPUT) req->flags |= GPIOHANDLE_REQUEST_OUTPUT; - if (config->active_state == GPIOD_REQUEST_ACTIVE_LOW) { - req->flags |= GPIOHANDLE_REQUEST_ACTIVE_LOW; - } else if (config->active_state != GPIOD_REQUEST_ACTIVE_HIGH) { - errno = EINVAL; - return -1; - } - req->lines = bulk->num_lines; for (i = 0; i < bulk->num_lines; i++) { @@ -358,13 +351,8 @@ static int line_request_event_single(struct gpiod_line *line, req->handleflags |= GPIOHANDLE_REQUEST_OPEN_DRAIN; if (config->flags & GPIOD_REQUEST_OPEN_SOURCE) req->handleflags |= GPIOHANDLE_REQUEST_OPEN_SOURCE; - - if (config->active_state == GPIOD_REQUEST_ACTIVE_LOW) { + if (config->flags & GPIOD_REQUEST_ACTIVE_LOW) req->handleflags |= GPIOHANDLE_REQUEST_ACTIVE_LOW; - } else if (config->active_state != GPIOD_REQUEST_ACTIVE_HIGH) { - errno = EINVAL; - return -1; - } if (config->request_type == GPIOD_REQUEST_EVENT_RISING_EDGE) req->eventflags |= GPIOEVENT_REQUEST_RISING_EDGE; @@ -428,8 +416,7 @@ int gpiod_line_request_bulk_input(struct gpiod_line_bulk *bulk, struct gpiod_line_request_config config = { .consumer = consumer, .request_type = GPIOD_REQUEST_DIRECTION_INPUT, - .active_state = active_low ? GPIOD_REQUEST_ACTIVE_LOW - : GPIOD_REQUEST_ACTIVE_HIGH, + .flags = active_low ? GPIOD_REQUEST_ACTIVE_LOW : 0, }; return gpiod_line_request_bulk(bulk, &config, 0); @@ -442,8 +429,7 @@ int gpiod_line_request_bulk_output(struct gpiod_line_bulk *bulk, struct gpiod_line_request_config config = { .consumer = consumer, .request_type = GPIOD_REQUEST_DIRECTION_OUTPUT, - .active_state = active_low ? GPIOD_REQUEST_ACTIVE_LOW - : GPIOD_REQUEST_ACTIVE_HIGH, + .flags = active_low ? GPIOD_REQUEST_ACTIVE_LOW: 0, }; return gpiod_line_request_bulk(bulk, &config, default_vals); @@ -611,8 +597,7 @@ static int line_event_request_type(struct gpiod_line *line, struct gpiod_line_request_config config = { .consumer = consumer, .request_type = type, - .active_state = active_low ? GPIOD_REQUEST_ACTIVE_LOW - : GPIOD_REQUEST_ACTIVE_HIGH, + .flags = active_low ? GPIOD_REQUEST_ACTIVE_LOW : 0, }; return gpiod_line_request(line, &config, 0); diff --git a/src/tools/gpiomon.c b/src/tools/gpiomon.c index a8e008b..75eacf4 100644 --- a/src/tools/gpiomon.c +++ b/src/tools/gpiomon.c @@ -239,9 +239,7 @@ int main(int argc, char **argv) die_perror("error opening gpiochip '%s'", argv[0]); evconf.consumer = "gpiomon"; - evconf.flags = 0; - evconf.active_state = active_low ? GPIOD_REQUEST_ACTIVE_LOW - : GPIOD_REQUEST_ACTIVE_HIGH; + evconf.flags = active_low ? GPIOD_REQUEST_ACTIVE_LOW : 0; if (watch_falling && !watch_rising) evconf.request_type = GPIOD_REQUEST_EVENT_FALLING_EDGE; diff --git a/tests/tests-line.c b/tests/tests-line.c index 75ac680..8f5862e 100644 --- a/tests/tests-line.c +++ b/tests/tests-line.c @@ -245,7 +245,7 @@ static void line_request_bulk_different_chips(void) req.consumer = TEST_CONSUMER; req.request_type = GPIOD_REQUEST_DIRECTION_INPUT; - req.active_state = GPIOD_ACTIVE_STATE_HIGH; + req.flags = GPIOD_ACTIVE_STATE_HIGH; status = gpiod_line_request_bulk(&bulk, &req, NULL); TEST_ASSERT_NOTEQ(status, 0); @@ -372,7 +372,6 @@ static void line_misc_flags(void) config.request_type = GPIOD_REQUEST_DIRECTION_INPUT; config.consumer = TEST_CONSUMER; - config.active_state = GPIOD_REQUEST_ACTIVE_HIGH; config.flags = GPIOD_REQUEST_OPEN_DRAIN; status = gpiod_line_request(line, &config, 0); @@ -412,7 +411,6 @@ static void line_null_consumer(void) config.request_type = GPIOD_REQUEST_DIRECTION_INPUT; config.consumer = NULL; - config.active_state = GPIOD_REQUEST_ACTIVE_HIGH; config.flags = 0; rv = gpiod_line_request(line, &config, 0); @@ -450,7 +448,6 @@ static void line_empty_consumer(void) config.request_type = GPIOD_REQUEST_DIRECTION_INPUT; config.consumer = ""; - config.active_state = GPIOD_REQUEST_ACTIVE_HIGH; config.flags = 0; rv = gpiod_line_request(line, &config, 0); -- 2.30.2