core: set the active state using the request flags
authorBartosz Golaszewski <bartekgola@gmail.com>
Wed, 12 Jul 2017 18:17:01 +0000 (20:17 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Wed, 12 Jul 2017 18:17:01 +0000 (20:17 +0200)
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 <bartekgola@gmail.com>
include/gpiod.h
src/lib/core.c
src/tools/gpiomon.c
tests/tests-line.c

index e423cddebd8f05916d59ad64633db302c6317006..a8196c9a98fa306659883ac83bb22778fb930ab9 100644 (file)
@@ -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. */
 };
index 3fba33d957e61021d5f4b9fef3a079fc577486fb..1e07b82e9eddc929e80edff27140749c0f422118 100644 (file)
@@ -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);
index a8e008b934f2ac72a4c5f27e3628316184a7d663..75eacf4a2f207c474035c67c38275238de24bd11 100644 (file)
@@ -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;
index 75ac680d1903af260b26ef4b5a6bbac1c7740eb3..8f5862ecb1cb0cfe88d5d9fa1e0d1f7a6ffa1af6 100644 (file)
@@ -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);