core: ioctl() code shrink
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 13:06:03 +0000 (14:06 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 13:06:03 +0000 (14:06 +0100)
Wrap GPIO ioctl() calls in a routine setting the libgpiod errno in
case of an error.

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

diff --git a/core.c b/core.c
index 716313766e7b0dc27c70bf97ae09d0dc18a3fd71..539862c32357f37f09b47491ae96129df652b48e 100644 (file)
--- a/core.c
+++ b/core.c
@@ -59,6 +59,19 @@ static bool is_unsigned_int(const char *str)
        return *str == '\0';
 }
 
+static int gpio_ioctl(int fd, unsigned long request, void *data)
+{
+       int status;
+
+       status = ioctl(fd, request, data);
+       if (status < 0) {
+               last_error_from_errno();
+               return -1;
+       }
+
+       return 0;
+}
+
 int gpiod_errno(void)
 {
        return last_error;
@@ -210,11 +223,9 @@ int gpiod_line_request(struct gpiod_line *line, const char *consumer,
        chip = gpiod_line_get_chip(line);
        fd = gpiod_chip_get_fd(chip);
 
-       status = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, req);
-       if (status < 0) {
-               last_error_from_errno();
+       status = gpio_ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, req);
+       if (status < 0)
                return -1;
-       }
 
        line->requested = true;
 
@@ -244,11 +255,10 @@ int gpiod_line_get_value(struct gpiod_line *line)
 
        memset(&data, 0, sizeof(data));
 
-       status = ioctl(line->lreq.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
-       if (status < 0) {
-               last_error_from_errno();
+       status = gpio_ioctl(line->lreq.fd,
+                           GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
+       if (status < 0)
                return -1;
-       }
 
        return data.values[0];
 }
@@ -266,11 +276,10 @@ int gpiod_line_set_value(struct gpiod_line *line, int value)
        memset(&data, 0, sizeof(data));
        data.values[0] = value ? 1 : 0;
 
-       status = ioctl(line->lreq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
-       if (status < 0) {
-               last_error_from_errno();
+       status = gpio_ioctl(line->lreq.fd,
+                           GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
+       if (status < 0)
                return -1;
-       }
 
        return 0;
 }
@@ -302,11 +311,10 @@ struct gpiod_chip * gpiod_chip_open(const char *path)
 
        chip->fd = fd;
 
-       status = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &chip->cinfo);
+       status = gpio_ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &chip->cinfo);
        if (status < 0) {
                close(chip->fd);
                free(chip);
-               last_error_from_errno();
                return NULL;
        }
 
@@ -406,11 +414,9 @@ gpiod_chip_get_line(struct gpiod_chip *chip, unsigned int offset)
        memset(&line->linfo, 0, sizeof(line->linfo));
        line->linfo.line_offset = offset;
 
-       status = ioctl(chip->fd, GPIO_GET_LINEINFO_IOCTL, &line->linfo);
-       if (status < 0) {
-               last_error_from_errno();
+       status = gpio_ioctl(chip->fd, GPIO_GET_LINEINFO_IOCTL, &line->linfo);
+       if (status < 0)
                return NULL;
-       }
 
        line->chip = chip;