open handle: open function flow was matched. 23/154423/3
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 12:14:17 +0000 (21:14 +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 2715210..41ba5bb 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 d55bcef..b0a8899 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;
@@ -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;
 }
 
index c336e2a..b66da6f 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 015be63..04252ac 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;
 }