core: put the line request configuration into a structure
authorBartosz Golaszewski <bartekgola@gmail.com>
Wed, 4 Jan 2017 16:52:26 +0000 (17:52 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Wed, 4 Jan 2017 16:52:26 +0000 (17:52 +0100)
The base routine for line requesting should have all config parameters
packed in a single structure. Future request helpers will simplify
this.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
core.c
gpiod.h
gpioset.c

diff --git a/core.c b/core.c
index f582dde5512bd974ba707315c485976f3a83b319..905f36c62470ec0d5209d43ac3666f012d73f6c0 100644 (file)
--- a/core.c
+++ b/core.c
@@ -94,10 +94,15 @@ const char * gpiod_strerror(int errnum)
 
 int gpiod_simple_get_value(const char *device, unsigned int offset)
 {
+       struct gpiod_line_request_config config;
        struct gpiod_chip *chip;
        struct gpiod_line *line;
        int status, value;
 
+       memset(&config, 0, sizeof(config));
+       config.consumer = libgpiod_consumer;
+       config.direction = GPIOD_DIRECTION_AS_IS;
+
        chip = gpiod_chip_open_lookup(device);
        if (!chip)
                return -1;
@@ -108,8 +113,7 @@ int gpiod_simple_get_value(const char *device, unsigned int offset)
                return -1;
        }
 
-       status = gpiod_line_request(line, libgpiod_consumer, 0,
-                                   GPIOD_REQUEST_DIRECTION_INPUT);
+       status = gpiod_line_request(line, &config, 0);
        if (status < 0) {
                gpiod_chip_close(chip);
                return -1;
@@ -204,14 +208,15 @@ int gpiod_line_update(struct gpiod_line *line)
 }
 
 int gpiod_line_request(struct gpiod_line *line,
-                      const char *consumer, int default_val, int flags)
+                      const struct gpiod_line_request_config *config,
+                      int default_val)
 {
        struct gpiod_line_bulk bulk;
 
        gpiod_line_bulk_init(&bulk);
        gpiod_line_bulk_add(&bulk, line);
 
-       return gpiod_line_request_bulk(&bulk, consumer, &default_val, flags);
+       return gpiod_line_request_bulk(&bulk, config, &default_val);
 }
 
 static bool verify_line_bulk(struct gpiod_line_bulk *line_bulk)
@@ -230,12 +235,13 @@ static bool verify_line_bulk(struct gpiod_line_bulk *line_bulk)
 }
 
 int gpiod_line_request_bulk(struct gpiod_line_bulk *line_bulk,
-                           const char *consumer, int *default_vals, int flags)
+                           const struct gpiod_line_request_config *config,
+                           int *default_vals)
 {
        struct gpiohandle_request *req;
        struct gpiod_chip *chip;
-       unsigned int i;
        int status, fd;
+       unsigned int i;
 
        /* Paranoia: verify that all lines are from the same gpiochip. */
        if (!verify_line_bulk(line_bulk))
@@ -247,39 +253,28 @@ int gpiod_line_request_bulk(struct gpiod_line_bulk *line_bulk,
 
        memset(req, 0, sizeof(*req));
 
-       if ((flags & GPIOD_REQUEST_POLARITY_ACTIVE_HIGH) &&
-           (flags & GPIOD_REQUEST_POLARITY_ACTIVE_LOW)) {
-               set_last_error(EINVAL);
-               return -1;
-       }
-
-       if ((flags & GPIOD_REQUEST_DIRECTION_INPUT) &&
-           (flags & GPIOD_REQUEST_DIRECTION_OUTPUT)) {
-               set_last_error(EINVAL);
-               return -1;
-       }
-
-       if (flags & GPIOD_REQUEST_POLARITY_ACTIVE_LOW)
-               req->flags |= GPIOHANDLE_REQUEST_ACTIVE_LOW;
-       if (flags & GPIOD_REQUEST_OPEN_DRAIN)
+       if (config->flags & GPIOD_REQUEST_OPEN_DRAIN)
                req->flags |= GPIOHANDLE_REQUEST_OPEN_DRAIN;
-       if (flags & GPIOD_REQUEST_OPEN_SOURCE)
+       if (config->flags & GPIOD_REQUEST_OPEN_SOURCE)
                req->flags |= GPIOHANDLE_REQUEST_OPEN_SOURCE;
 
-       if (flags & GPIOD_REQUEST_DIRECTION_INPUT)
+       if (config->direction == GPIOD_DIRECTION_IN)
                req->flags |= GPIOHANDLE_REQUEST_INPUT;
-       else if (flags & GPIOD_REQUEST_DIRECTION_OUTPUT)
+       else if (config->direction == GPIOD_DIRECTION_OUT)
                req->flags |= GPIOHANDLE_REQUEST_OUTPUT;
 
+       if (config->polarity == GPIOD_POLARITY_ACTIVE_LOW)
+               req->flags |= GPIOHANDLE_REQUEST_ACTIVE_LOW;
+
        req->lines = line_bulk->num_lines;
 
        for (i = 0; i < line_bulk->num_lines; i++) {
                req->lineoffsets[i] = gpiod_line_offset(line_bulk->lines[i]);
-               if (flags & GPIOD_REQUEST_DIRECTION_OUTPUT)
+               if (config->direction == GPIOD_DIRECTION_OUT)
                        req->default_values[i] = !!default_vals[i];
        }
 
-       strncpy(req->consumer_label, consumer,
+       strncpy(req->consumer_label, config->consumer,
                sizeof(req->consumer_label) - 1);
 
        chip = gpiod_line_get_chip(line_bulk->lines[0]);
diff --git a/gpiod.h b/gpiod.h
index 7744ce87a13a7ccb180a2c6d0ed799105940676f..d6b26b1dda8ed3feae8ea49b17414cb8d5865a66 100644 (file)
--- a/gpiod.h
+++ b/gpiod.h
@@ -47,6 +47,7 @@ const char * gpiod_strerror(int errnum) GPIOD_API;
 int gpiod_simple_get_value(const char *device, unsigned int offset) GPIOD_API;
 
 enum {
+       GPIOD_DIRECTION_AS_IS,
        GPIOD_DIRECTION_IN,
        GPIOD_DIRECTION_OUT,
 };
@@ -57,11 +58,6 @@ enum {
 };
 
 enum {
-       GPIOD_REQUEST_DIRECTION_AS_IS           = GPIOD_BIT(0),
-       GPIOD_REQUEST_DIRECTION_INPUT           = GPIOD_BIT(1),
-       GPIOD_REQUEST_DIRECTION_OUTPUT          = GPIOD_BIT(2),
-       GPIOD_REQUEST_POLARITY_ACTIVE_HIGH      = GPIOD_BIT(3),
-       GPIOD_REQUEST_POLARITY_ACTIVE_LOW       = GPIOD_BIT(4),
        GPIOD_REQUEST_OPEN_DRAIN                = GPIOD_BIT(5),
        GPIOD_REQUEST_OPEN_SOURCE               = GPIOD_BIT(6),
 };
@@ -90,8 +86,16 @@ bool gpiod_line_needs_update(struct gpiod_line *line) GPIOD_API;
 
 int gpiod_line_update(struct gpiod_line *line) GPIOD_API;
 
-int gpiod_line_request(struct gpiod_line *line, const char *consumer,
-                      int default_val, int flags) GPIOD_API;
+struct gpiod_line_request_config {
+       const char *consumer;
+       int direction;
+       int polarity;
+       int flags;
+};
+
+int gpiod_line_request(struct gpiod_line *line,
+                      const struct gpiod_line_request_config *config,
+                      int default_val) GPIOD_API;
 
 struct gpiod_line_bulk {
        struct gpiod_line *lines[GPIOD_REQUEST_MAX_LINES];
@@ -112,8 +116,8 @@ static inline void gpiod_line_bulk_add(struct gpiod_line_bulk *line_bulk,
 }
 
 int gpiod_line_request_bulk(struct gpiod_line_bulk *line_bulk,
-                           const char *consumer, int *default_vals,
-                           int flags) GPIOD_API;
+                           const struct gpiod_line_request_config *config,
+                           int *default_vals) GPIOD_API;
 
 void gpiod_line_release(struct gpiod_line *line) GPIOD_API;
 
index f2b1acac859ccd0ed6b7f40cabc3bb03824ff56c..5245cd00966676365d11f050a8019e54c2b73e36 100644 (file)
--- a/gpioset.c
+++ b/gpioset.c
@@ -15,6 +15,7 @@
 
 int main(int argc, char **argv)
 {
+       struct gpiod_line_request_config config;
        struct gpiod_chip *chip;
        struct gpiod_line *line;
        unsigned int offset;
@@ -68,8 +69,11 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       status = gpiod_line_request(line, "gpioset",
-                                   value, GPIOD_REQUEST_DIRECTION_OUTPUT);
+       memset(&config, 0, sizeof(config));
+       config.consumer = "gpioset";
+       config.direction = GPIOD_DIRECTION_OUT;
+
+       status = gpiod_line_request(line, &config, value);
        if (status < 0) {
                fprintf(stderr,
                        "%s: error requesting GPIO line: %s\n",