From 3f9d0b308e111023a754337b9ed6434e92819571 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 26 Apr 2017 20:31:03 +0900 Subject: [PATCH] Fix logic in gpio_write and gpio_get_direction - Return error if direction is input mode - Update direction state along with input value Change-Id: I360df5a6c986defaf25a71cf3a47785854b3c9a3 Signed-off-by: Hyeongsik Min --- src/daemon/peripheral_bus_gpio.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/daemon/peripheral_bus_gpio.c b/src/daemon/peripheral_bus_gpio.c index bf9ad16..621cef0 100644 --- a/src/daemon/peripheral_bus_gpio.c +++ b/src/daemon/peripheral_bus_gpio.c @@ -19,14 +19,12 @@ #include #include -#include #include "gpio.h" #include "peripheral_io_gdbus.h" #include "peripheral_bus.h" #include "peripheral_common.h" - static pb_gpio_data_h peripheral_bus_gpio_data_get(int pin, GList **list) { GList *gpio_list = *list; @@ -162,6 +160,7 @@ int peripheral_bus_gpio_get_direction(gint pin, gint *direction, gpointer user_d { peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data; pb_gpio_data_h gpio; + gint value; int ret; gpio = peripheral_bus_gpio_data_get(pin, &pb_data->gpio_list); @@ -175,7 +174,14 @@ int peripheral_bus_gpio_get_direction(gint pin, gint *direction, gpointer user_d return ret; } - gpio->direction = *direction; + if (*direction == GPIO_DIRECTION_OUT) { + if ((ret = gpio_read(pin, &value)) < 0) { + return ret; + } + /* Update direction state with the current value */ + *direction = GPIO_DIRECTION_OUT + value; + gpio->direction = *direction; + } return PERIPHERAL_ERROR_NONE; } @@ -236,10 +242,16 @@ int peripheral_bus_gpio_write(gint pin, gint value, gpointer user_data) return PERIPHERAL_ERROR_UNKNOWN; } + /* Return error if direction is input mode */ + if (gpio->direction == GPIO_DIRECTION_IN) + return PERIPHERAL_ERROR_IO_ERROR; + if ((ret = gpio_write(pin, value)) < 0) { _E("gpio_write error (%d)", ret); return ret; } + /* Update direction state along with the value */ + gpio->direction = GPIO_DIRECTION_OUT + (value > 0) ? 1 : 0; return PERIPHERAL_ERROR_NONE; } -- 2.7.4