open handle: open function flow was matched. 24/154424/2
authorSegwon <segwon.han@samsung.com>
Tue, 10 Oct 2017 07:26:13 +0000 (16:26 +0900)
committerSegwon <segwon.han@samsung.com>
Tue, 10 Oct 2017 11:48:34 +0000 (20:48 +0900)
 - 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 <segwon.han@samsung.com>
src/daemon/peripheral_bus_gpio.c
src/daemon/peripheral_bus_i2c.c
src/daemon/peripheral_bus_spi.c
src/daemon/peripheral_bus_uart.c

index 2715210bd98b1c8463fea9c2bdd04325d5b6503b..41ba5bb4785021d7778f91cceb34404b9f359599 100644 (file)
@@ -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;
        }
 
index f989022dec21b62747c1ebb43272eedbb0296277..6c5ad4aeec51b69caa3dc099b3165d349b9cba71 100644 (file)
@@ -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;
@@ -86,14 +93,20 @@ int peripheral_bus_i2c_open(int bus, int address, pb_data_h *handle, gpointer us
        i2c_handle->dev.i2c.buffer = malloc(INITIAL_BUFFER_SIZE);
 
        if (!(i2c_handle->dev.i2c.buffer)) {
-               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;
 }
 
index c336e2ad87a5c3652ee2186bc51ae75c554a2460..b66da6f142be08f2bf93cc4e20ad9a2c3d3c8a92 100644 (file)
@@ -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) {
index 015be639abf1e8a8bf82e96c014f0fea8b93c035..04252ac3dee5d28d99a57aea0131d2f753cab7fd 100644 (file)
@@ -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;
 }