core: update line info after a line request
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 15:23:24 +0000 (16:23 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 15:23:24 +0000 (16:23 +0100)
Currently the line information isn't updated after it's been
requested. Re-read the line info after requesting it.

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

diff --git a/core.c b/core.c
index a108eda59397b893cbb6906680b471877751b425..e376d6a479ade294b3049e2d6d2239c4643bf33c 100644 (file)
--- a/core.c
+++ b/core.c
@@ -159,6 +159,23 @@ struct gpiod_line {
        struct gpiohandle_request *req;
 };
 
+static int update_line_info(struct gpiod_line *line,
+                           struct gpiod_chip *chip, unsigned int offset)
+{
+       int status, fd;
+
+       memset(&line->info, 0, sizeof(line->info));
+       line->info.line_offset = offset;
+
+       fd = gpiod_chip_get_fd(chip);
+
+       status = gpio_ioctl(fd, GPIO_GET_LINEINFO_IOCTL, &line->info);
+       if (status < 0)
+               return -1;
+
+       return 0;
+}
+
 unsigned int gpiod_line_offset(struct gpiod_line *line)
 {
        return (unsigned int)line->info.line_offset;
@@ -243,6 +260,13 @@ int gpiod_line_request(struct gpiod_line *line, const char *consumer,
 
        line->requested = true;
 
+       /* Update line info to include the changes after the request. */
+       status = update_line_info(line, chip, gpiod_line_offset(line));
+       if (status < 0) {
+               gpiod_line_release(line);
+               return -1;
+       }
+
        return 0;
 }
 
@@ -430,16 +454,12 @@ gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset)
        }
 
        line = &chip->lines[offset];
+       line->chip = chip;
 
-       memset(&line->info, 0, sizeof(line->info));
-       line->info.line_offset = offset;
-
-       status = gpio_ioctl(chip->fd, GPIO_GET_LINEINFO_IOCTL, &line->info);
+       status = update_line_info(line, chip, offset);
        if (status < 0)
                return NULL;
 
-       line->chip = chip;
-
        return line;
 }