From 2841452b5dfca59cee77e1bb749013a8244a9306 Mon Sep 17 00:00:00 2001 From: Segwon Date: Tue, 14 Nov 2017 22:13:29 +0900 Subject: [PATCH] gdbus: refactor gdbus proxy functions - changed to static - proxy init function has been modified to return error. Change-Id: I220d66e31d19c4cd7e27caa18f2c5cd2cba19b00 Signed-off-by: Segwon --- include/gdbus/peripheral_gdbus_gpio.h | 3 --- include/gdbus/peripheral_gdbus_i2c.h | 3 --- include/gdbus/peripheral_gdbus_pwm.h | 3 --- include/gdbus/peripheral_gdbus_spi.h | 3 --- include/gdbus/peripheral_gdbus_uart.h | 3 --- src/gdbus/peripheral_gdbus_gpio.c | 27 +++++++++++++++++++-------- src/gdbus/peripheral_gdbus_i2c.c | 26 +++++++++++++++++++++----- src/gdbus/peripheral_gdbus_pwm.c | 26 +++++++++++++++++++++----- src/gdbus/peripheral_gdbus_spi.c | 26 +++++++++++++++++++++----- src/gdbus/peripheral_gdbus_uart.c | 27 +++++++++++++++++++++------ src/peripheral_gpio.c | 4 ---- src/peripheral_i2c.c | 3 --- src/peripheral_pwm.c | 3 --- src/peripheral_spi.c | 3 --- src/peripheral_uart.c | 3 --- 15 files changed, 103 insertions(+), 60 deletions(-) diff --git a/include/gdbus/peripheral_gdbus_gpio.h b/include/gdbus/peripheral_gdbus_gpio.h index f9e35e6..3fd5307 100644 --- a/include/gdbus/peripheral_gdbus_gpio.h +++ b/include/gdbus/peripheral_gdbus_gpio.h @@ -17,9 +17,6 @@ #ifndef __PERIPHERAL_GDBUS_GPIO_H__ #define __PERIPHERAL_GDBUS_GPIO_H__ -void gpio_proxy_init(void); -void gpio_proxy_deinit(void); - int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio); int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio); diff --git a/include/gdbus/peripheral_gdbus_i2c.h b/include/gdbus/peripheral_gdbus_i2c.h index 30604c7..0670442 100644 --- a/include/gdbus/peripheral_gdbus_i2c.h +++ b/include/gdbus/peripheral_gdbus_i2c.h @@ -17,9 +17,6 @@ #ifndef __PERIPHERAL_GDBUS_I2C_H__ #define __PERIPHERAL_GDBUS_I2C_H__ -void i2c_proxy_init(void); -void i2c_proxy_deinit(void); - int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c); diff --git a/include/gdbus/peripheral_gdbus_pwm.h b/include/gdbus/peripheral_gdbus_pwm.h index 0ce6159..148e938 100644 --- a/include/gdbus/peripheral_gdbus_pwm.h +++ b/include/gdbus/peripheral_gdbus_pwm.h @@ -17,9 +17,6 @@ #ifndef __PERIPHERAL_GDBUS_PWM_H__ #define __PERIPHERAL_GDBUS_PWM_H__ -void pwm_proxy_init(void); -void pwm_proxy_deinit(void); - int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int chip, int pin); int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm); diff --git a/include/gdbus/peripheral_gdbus_spi.h b/include/gdbus/peripheral_gdbus_spi.h index d6be9b5..8a6ac61 100644 --- a/include/gdbus/peripheral_gdbus_spi.h +++ b/include/gdbus/peripheral_gdbus_spi.h @@ -16,9 +16,6 @@ #ifndef __PERIPHERAL_GDBUS_SPI_H_ #define __PERIPHERAL_GDBUS_SPI_H_ -void spi_proxy_init(void); -void spi_proxy_deinit(); - int peripheral_gdbus_spi_open(peripheral_spi_h spi, int bus, int cs); int peripheral_gdbus_spi_close(peripheral_spi_h spi); diff --git a/include/gdbus/peripheral_gdbus_uart.h b/include/gdbus/peripheral_gdbus_uart.h index d19440c..175452c 100644 --- a/include/gdbus/peripheral_gdbus_uart.h +++ b/include/gdbus/peripheral_gdbus_uart.h @@ -16,9 +16,6 @@ #ifndef __PERIPHERAL_GDBUS_UART_H_ #define __PERIPHERAL_GDBUS_UART_H_ -void uart_proxy_init(void); -void uart_proxy_deinit(); - int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port); int peripheral_gdbus_uart_close(peripheral_uart_h uart); diff --git a/src/gdbus/peripheral_gdbus_gpio.c b/src/gdbus/peripheral_gdbus_gpio.c index 17a7f2f..ca5badd 100644 --- a/src/gdbus/peripheral_gdbus_gpio.c +++ b/src/gdbus/peripheral_gdbus_gpio.c @@ -30,13 +30,14 @@ static PeripheralIoGdbusGpio *gpio_proxy = NULL; -void gpio_proxy_init(void) +static int __gpio_proxy_init() { GError *error = NULL; if (gpio_proxy != NULL) { + _E("Gpio proxy is already created"); g_object_ref(gpio_proxy); - return; + return PERIPHERAL_ERROR_NONE; } gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync( @@ -46,16 +47,19 @@ void gpio_proxy_init(void) PERIPHERAL_GDBUS_GPIO_PATH, NULL, &error); + if (gpio_proxy == NULL) { - _E("Can not create gpio proxy : %s", error->message); + _E("Failed to create gpio proxy : %s", error->message); g_error_free(error); - return; + return PERIPHERAL_ERROR_UNKNOWN; } + + return PERIPHERAL_ERROR_NONE; } -void gpio_proxy_deinit() +static void __gpio_proxy_deinit() { - if (gpio_proxy) { + if (gpio_proxy != NULL) { g_object_unref(gpio_proxy); if (!G_IS_OBJECT(gpio_proxy)) gpio_proxy = NULL; @@ -68,7 +72,9 @@ int peripheral_gdbus_gpio_open(peripheral_gpio_h gpio) gint32 ret = PERIPHERAL_ERROR_NONE; GUnixFDList *fd_list = NULL; - if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + ret = __gpio_proxy_init(); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; if (peripheral_io_gdbus_gpio_call_open_sync( gpio_proxy, @@ -115,7 +121,10 @@ int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio) GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + if (gpio_proxy == NULL) { + _E("Can't try to gpio close because gpio proxy is NULL."); + return PERIPHERAL_ERROR_UNKNOWN; + } if (peripheral_io_gdbus_gpio_call_close_sync( gpio_proxy, @@ -128,5 +137,7 @@ int peripheral_gdbus_gpio_close(peripheral_gpio_h gpio) return PERIPHERAL_ERROR_UNKNOWN; } + __gpio_proxy_deinit(); + return ret; } \ No newline at end of file diff --git a/src/gdbus/peripheral_gdbus_i2c.c b/src/gdbus/peripheral_gdbus_i2c.c index 63fda68..b10b10f 100644 --- a/src/gdbus/peripheral_gdbus_i2c.c +++ b/src/gdbus/peripheral_gdbus_i2c.c @@ -28,13 +28,14 @@ static PeripheralIoGdbusI2c *i2c_proxy = NULL; -void i2c_proxy_init(void) +static int __i2c_proxy_init() { GError *error = NULL; if (i2c_proxy != NULL) { + _E("I2c proxy is already created"); g_object_ref(i2c_proxy); - return; + return PERIPHERAL_ERROR_NONE; } i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync( @@ -44,9 +45,17 @@ void i2c_proxy_init(void) PERIPHERAL_GDBUS_I2C_PATH, NULL, &error); + + if (i2c_proxy == NULL) { + _E("Failed to create i2c proxy : %s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return PERIPHERAL_ERROR_NONE; } -void i2c_proxy_deinit() +static void __i2c_proxy_deinit() { if (i2c_proxy) { g_object_unref(i2c_proxy); @@ -61,7 +70,9 @@ int peripheral_gdbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) peripheral_error_e ret = PERIPHERAL_ERROR_NONE; GUnixFDList *fd_list = NULL; - if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + ret = __i2c_proxy_init(); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; if (peripheral_io_gdbus_i2c_call_open_sync( i2c_proxy, @@ -95,7 +106,10 @@ int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c) GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + if (i2c_proxy == NULL) { + _E("Can't try to i2c close because i2c proxy is NULL."); + return PERIPHERAL_ERROR_UNKNOWN; + } if (peripheral_io_gdbus_i2c_call_close_sync( i2c_proxy, @@ -108,5 +122,7 @@ int peripheral_gdbus_i2c_close(peripheral_i2c_h i2c) return PERIPHERAL_ERROR_UNKNOWN; } + __i2c_proxy_deinit(); + return ret; } \ No newline at end of file diff --git a/src/gdbus/peripheral_gdbus_pwm.c b/src/gdbus/peripheral_gdbus_pwm.c index f512e2c..3279d15 100644 --- a/src/gdbus/peripheral_gdbus_pwm.c +++ b/src/gdbus/peripheral_gdbus_pwm.c @@ -31,13 +31,14 @@ static PeripheralIoGdbusPwm *pwm_proxy = NULL; -void pwm_proxy_init(void) +static int __pwm_proxy_init() { GError *error = NULL; if (pwm_proxy != NULL) { + _E("Pwm proxy is already created"); g_object_ref(pwm_proxy); - return; + return PERIPHERAL_ERROR_NONE; } pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync( @@ -47,9 +48,17 @@ void pwm_proxy_init(void) PERIPHERAL_GDBUS_PWM_PATH, NULL, &error); + + if (pwm_proxy == NULL) { + _E("Failed to create pwm proxy : %s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return PERIPHERAL_ERROR_NONE; } -void pwm_proxy_deinit() +static void __pwm_proxy_deinit() { if (pwm_proxy) { g_object_unref(pwm_proxy); @@ -64,7 +73,9 @@ int peripheral_gdbus_pwm_open(peripheral_pwm_h pwm, int chip, int pin) peripheral_error_e ret = PERIPHERAL_ERROR_NONE; GUnixFDList *fd_list = NULL; - if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + ret = __pwm_proxy_init(); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; if (peripheral_io_gdbus_pwm_call_open_sync( pwm_proxy, @@ -119,7 +130,10 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + if (pwm_proxy == NULL) { + _E("Can't try to pwm close because pwm proxy is NULL."); + return PERIPHERAL_ERROR_UNKNOWN; + } if (peripheral_io_gdbus_pwm_call_close_sync( pwm_proxy, @@ -132,5 +146,7 @@ int peripheral_gdbus_pwm_close(peripheral_pwm_h pwm) return PERIPHERAL_ERROR_UNKNOWN; } + __pwm_proxy_deinit(); + return ret; } \ No newline at end of file diff --git a/src/gdbus/peripheral_gdbus_spi.c b/src/gdbus/peripheral_gdbus_spi.c index e286f40..80e267a 100644 --- a/src/gdbus/peripheral_gdbus_spi.c +++ b/src/gdbus/peripheral_gdbus_spi.c @@ -28,13 +28,14 @@ static PeripheralIoGdbusSpi *spi_proxy = NULL; -void spi_proxy_init(void) +static int __spi_proxy_init() { GError *error = NULL; if (spi_proxy != NULL) { + _E("Spi proxy is already created"); g_object_ref(spi_proxy); - return; + return PERIPHERAL_ERROR_NONE; } spi_proxy = peripheral_io_gdbus_spi_proxy_new_for_bus_sync( @@ -44,9 +45,17 @@ void spi_proxy_init(void) PERIPHERAL_GDBUS_SPI_PATH, NULL, &error); + + if (spi_proxy == NULL) { + _E("Failed to create spi proxy : %s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return PERIPHERAL_ERROR_NONE; } -void spi_proxy_deinit() +static void __spi_proxy_deinit() { if (spi_proxy) { g_object_unref(spi_proxy); @@ -61,7 +70,9 @@ int peripheral_gdbus_spi_open(peripheral_spi_h spi, int bus, int cs) peripheral_error_e ret = PERIPHERAL_ERROR_NONE; GUnixFDList *fd_list = NULL; - if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + ret = __spi_proxy_init(); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; if (peripheral_io_gdbus_spi_call_open_sync( spi_proxy, @@ -95,7 +106,10 @@ int peripheral_gdbus_spi_close(peripheral_spi_h spi) GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - if (spi_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + if (spi_proxy == NULL) { + _E("Can't try to spi close because spi proxy is NULL."); + return PERIPHERAL_ERROR_UNKNOWN; + } if (peripheral_io_gdbus_spi_call_close_sync( spi_proxy, @@ -108,5 +122,7 @@ int peripheral_gdbus_spi_close(peripheral_spi_h spi) return PERIPHERAL_ERROR_UNKNOWN; } + __spi_proxy_deinit(); + return ret; } \ No newline at end of file diff --git a/src/gdbus/peripheral_gdbus_uart.c b/src/gdbus/peripheral_gdbus_uart.c index 16111dd..99620b6 100644 --- a/src/gdbus/peripheral_gdbus_uart.c +++ b/src/gdbus/peripheral_gdbus_uart.c @@ -28,13 +28,14 @@ static PeripheralIoGdbusUart *uart_proxy = NULL; -void uart_proxy_init(void) +static int __uart_proxy_init() { GError *error = NULL; if (uart_proxy != NULL) { + _E("Uart proxy is already created"); g_object_ref(uart_proxy); - return; + return PERIPHERAL_ERROR_NONE; } uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync( @@ -44,9 +45,17 @@ void uart_proxy_init(void) PERIPHERAL_GDBUS_UART_PATH, NULL, &error); + + if (uart_proxy == NULL) { + _E("Failed to create uart proxy : %s", error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return PERIPHERAL_ERROR_NONE; } -void uart_proxy_deinit() +static void __uart_proxy_deinit() { if (uart_proxy) { g_object_unref(uart_proxy); @@ -61,7 +70,9 @@ int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port) peripheral_error_e ret = PERIPHERAL_ERROR_NONE; GUnixFDList *fd_list = NULL; - if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + ret = __uart_proxy_init(); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; if (peripheral_io_gdbus_uart_call_open_sync( uart_proxy, @@ -94,8 +105,10 @@ int peripheral_gdbus_uart_close(peripheral_uart_h uart) GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - + if (uart_proxy == NULL) { + _E("Can't try to uart close because uart proxy is NULL."); + return PERIPHERAL_ERROR_UNKNOWN; + } if (peripheral_io_gdbus_uart_call_close_sync( uart_proxy, uart->handle, @@ -107,5 +120,7 @@ int peripheral_gdbus_uart_close(peripheral_uart_h uart) return PERIPHERAL_ERROR_UNKNOWN; } + __uart_proxy_deinit(); + return ret; } \ No newline at end of file diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index a66cbaa..6a03a5a 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -69,8 +69,6 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) } handle->pin = gpio_pin; - gpio_proxy_init(); - ret = peripheral_gdbus_gpio_open(handle); if (ret != PERIPHERAL_ERROR_NONE) { @@ -99,7 +97,6 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) ret = peripheral_gdbus_gpio_close(gpio); if (ret != PERIPHERAL_ERROR_NONE) _E("Failed to close the gpio pin, ret : %d", ret); - gpio_proxy_deinit(); free(gpio); gpio = NULL; @@ -124,7 +121,6 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct return ret; } - /** * @brief Sets the edge mode of the gpio pin. */ diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 60e0c59..d729356 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -74,8 +74,6 @@ int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - i2c_proxy_init(); - ret = peripheral_gdbus_i2c_open(handle, bus, address); if (ret != PERIPHERAL_ERROR_NONE) { @@ -98,7 +96,6 @@ int peripheral_i2c_close(peripheral_i2c_h i2c) ret = peripheral_gdbus_i2c_close(i2c); if (ret != PERIPHERAL_ERROR_NONE) _E("Failed to close i2c communcation, ret : %d", ret); - i2c_proxy_deinit(); free(i2c); i2c = NULL; diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 1329bf1..e21bcbb 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -66,8 +66,6 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - pwm_proxy_init(); - ret = peripheral_gdbus_pwm_open(handle, chip, pin); if (ret != PERIPHERAL_ERROR_NONE) { @@ -90,7 +88,6 @@ int peripheral_pwm_close(peripheral_pwm_h pwm) if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0) _E("Failed to close PWM chip, continuing anyway, ret : %d", ret); - pwm_proxy_deinit(); free(pwm); return ret; diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c index b25dee4..68e9761 100644 --- a/src/peripheral_spi.c +++ b/src/peripheral_spi.c @@ -66,8 +66,6 @@ int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - spi_proxy_init(); - ret = peripheral_gdbus_spi_open(handle, bus, cs); if (ret != PERIPHERAL_ERROR_NONE) { @@ -91,7 +89,6 @@ int peripheral_spi_close(peripheral_spi_h spi) if (ret < PERIPHERAL_ERROR_NONE) _E("Failed to close SPI device, continuing anyway, ret : %d", ret); - spi_proxy_deinit(); free(spi); return ret; diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index 70d711a..ad58c0c 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -67,8 +67,6 @@ int peripheral_uart_open(int port, peripheral_uart_h *uart) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - uart_proxy_init(); - ret = peripheral_gdbus_uart_open(handle, port); if (ret != PERIPHERAL_ERROR_NONE) { @@ -94,7 +92,6 @@ int peripheral_uart_close(peripheral_uart_h uart) ret = peripheral_gdbus_uart_close(uart); if (ret < PERIPHERAL_ERROR_NONE) _E("Failed to close uart communication, continuing anyway, ret : %d", ret); - uart_proxy_deinit(); free(uart); uart = NULL; -- 2.7.4