From 058bbd127a9d4509fccbbf7aff0d739b6e2edeac Mon Sep 17 00:00:00 2001 From: Segwon Date: Tue, 10 Oct 2017 16:26:13 +0900 Subject: [PATCH] open handle: open function flow was matched. - added the NULL check when trying to allocate memory for i2c handle. - modified to return an OUT_OF_MEMORY when failed allocation from peripheral_bus_data_new(). - open i2c, uart handle flow was matched with another open flow. - add log before return error. Change-Id: Ibcee972e2bba39cc42b8bc53bd91efcb0526b0bf Signed-off-by: Segwon --- src/daemon/peripheral_bus_gpio.c | 2 +- src/daemon/peripheral_bus_i2c.c | 31 ++++++++++++++++++++++--------- src/daemon/peripheral_bus_spi.c | 4 +++- src/daemon/peripheral_bus_uart.c | 20 ++++++++++++++------ 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/daemon/peripheral_bus_gpio.c b/src/daemon/peripheral_bus_gpio.c index 2715210..41ba5bb 100644 --- a/src/daemon/peripheral_bus_gpio.c +++ b/src/daemon/peripheral_bus_gpio.c @@ -152,7 +152,7 @@ int peripheral_bus_gpio_open(gint pin, pb_data_h *handle, gpointer user_data) gpio_handle = peripheral_bus_data_new(&pb_data->gpio_list); if (!gpio_handle) { _E("peripheral_bus_data_new error"); - ret = PERIPHERAL_ERROR_UNKNOWN; + ret = PERIPHERAL_ERROR_OUT_OF_MEMORY; goto err; } diff --git a/src/daemon/peripheral_bus_i2c.c b/src/daemon/peripheral_bus_i2c.c index d55bcef..b0a8899 100644 --- a/src/daemon/peripheral_bus_i2c.c +++ b/src/daemon/peripheral_bus_i2c.c @@ -63,20 +63,27 @@ int peripheral_bus_i2c_open(int bus, int address, pb_data_h *handle, gpointer us int ret; int fd; - _D("bus : %d, address : 0x%x", bus, address); - - if (!peripheral_bus_i2c_is_available(bus, address, pb_data)) + if (!peripheral_bus_i2c_is_available(bus, address, pb_data)) { + _E("bus : %d, address : 0x%x is not available", bus, address); return PERIPHERAL_ERROR_RESOURCE_BUSY; + } - if ((ret = i2c_open(bus, &fd)) < 0) - return ret; + if ((ret = i2c_open(bus, &fd)) < 0) { + _E("i2c_open error (%d)", ret); + goto open_err; + } if ((ret = i2c_set_address(fd, address)) < 0) { - i2c_close(fd); - return ret; + _E("i2c_set_address error (%d)", ret); + goto err; } i2c_handle = peripheral_bus_data_new(&pb_data->i2c_list); + if (!i2c_handle) { + _E("peripheral_bus_data_new error"); + ret = PERIPHERAL_ERROR_OUT_OF_MEMORY; + goto err; + } i2c_handle->type = PERIPHERAL_BUS_TYPE_I2C; i2c_handle->list = &pb_data->i2c_list; @@ -87,14 +94,20 @@ int peripheral_bus_i2c_open(int bus, int address, pb_data_h *handle, gpointer us if (!(i2c_handle->dev.i2c.buffer)) { peripheral_bus_data_free(i2c_handle); - i2c_close(fd); _E("Failed to allocate data buffer"); - return PERIPHERAL_ERROR_OUT_OF_MEMORY; + ret = PERIPHERAL_ERROR_OUT_OF_MEMORY; + goto err; } i2c_handle->dev.i2c.buffer_size = INITIAL_BUFFER_SIZE; *handle = i2c_handle; + return PERIPHERAL_ERROR_NONE; + +err: + i2c_close(fd); + +open_err: return ret; } diff --git a/src/daemon/peripheral_bus_spi.c b/src/daemon/peripheral_bus_spi.c index c336e2a..b66da6f 100644 --- a/src/daemon/peripheral_bus_spi.c +++ b/src/daemon/peripheral_bus_spi.c @@ -68,8 +68,10 @@ int peripheral_bus_spi_open(int bus, int cs, pb_data_h *handle, gpointer user_da return PERIPHERAL_ERROR_RESOURCE_BUSY; } - if ((ret = spi_open(bus, cs, &fd)) < 0) + if ((ret = spi_open(bus, cs, &fd)) < 0) { + _E("spi_open error (%d)", ret); goto err_open; + } spi_handle = peripheral_bus_data_new(&pb_data->spi_list); if (!spi_handle) { diff --git a/src/daemon/peripheral_bus_uart.c b/src/daemon/peripheral_bus_uart.c index 015be63..04252ac 100644 --- a/src/daemon/peripheral_bus_uart.c +++ b/src/daemon/peripheral_bus_uart.c @@ -69,14 +69,16 @@ int peripheral_bus_uart_open(int port, pb_data_h *handle, gpointer user_data) return PERIPHERAL_ERROR_RESOURCE_BUSY; } - if ((ret = uart_open(port, &fd)) < 0) - return ret; + if ((ret = uart_open(port, &fd)) < 0) { + _E("uart_open error (%d)", ret); + goto open_err; + } uart_handle = peripheral_bus_data_new(&pb_data->uart_list); if (!uart_handle) { _E("peripheral_bus_data_new error"); - uart_close(fd); - return PERIPHERAL_ERROR_OUT_OF_MEMORY; + ret = PERIPHERAL_ERROR_OUT_OF_MEMORY; + goto err; } uart_handle->type = PERIPHERAL_BUS_TYPE_UART; @@ -88,13 +90,19 @@ int peripheral_bus_uart_open(int port, pb_data_h *handle, gpointer user_data) if (!uart_handle->dev.uart.buffer) { _E("Failed to allocate buffer"); peripheral_bus_data_free(uart_handle); - uart_close(fd); - return PERIPHERAL_ERROR_OUT_OF_MEMORY; + ret = PERIPHERAL_ERROR_OUT_OF_MEMORY; + goto err; } uart_handle->dev.uart.buffer_size = INITIAL_BUFFER_SIZE; *handle = uart_handle; + return PERIPHERAL_ERROR_NONE; + +err: + uart_close(fd); + +open_err: return ret; } -- 2.7.4