Refactoring the handle structure 91/134591/5
authorjino.cho <jino.cho@samsung.com>
Mon, 19 Jun 2017 09:34:35 +0000 (18:34 +0900)
committerjino.cho <jino.cho@samsung.com>
Tue, 20 Jun 2017 06:17:24 +0000 (15:17 +0900)
This patch unifies the structures of the device handles.
It was extracted duplicated code into one method and enhanced the
handle validation.

Change-Id: I8e91ddc5c687fce15d64598da134d3c5d8c6ea5a
Signed-off-by: jino.cho <jino.cho@samsung.com>
14 files changed:
src/daemon/peripheral_bus.c
src/daemon/peripheral_bus.h
src/daemon/peripheral_bus_gpio.c
src/daemon/peripheral_bus_gpio.h
src/daemon/peripheral_bus_i2c.c
src/daemon/peripheral_bus_i2c.h
src/daemon/peripheral_bus_pwm.c
src/daemon/peripheral_bus_pwm.h
src/daemon/peripheral_bus_spi.c
src/daemon/peripheral_bus_spi.h
src/daemon/peripheral_bus_uart.c
src/daemon/peripheral_bus_uart.h
src/daemon/peripheral_bus_util.c
src/daemon/peripheral_bus_util.h

index 5e96f8e..f0bcbfb 100644 (file)
@@ -37,7 +37,7 @@ static void __gpio_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
 {
-       pb_gpio_data_h gpio_handle = (pb_gpio_data_h)user_data;
+       pb_data_h gpio_handle = (pb_data_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(gpio_handle->watch_id);
@@ -48,7 +48,7 @@ static void __i2c_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
 {
-       pb_i2c_data_h i2c_handle = (pb_i2c_data_h)user_data;
+       pb_data_h i2c_handle = (pb_data_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(i2c_handle->watch_id);
@@ -59,7 +59,7 @@ static void __pwm_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
 {
-       pb_pwm_data_h pwm_handle = (pb_pwm_data_h)user_data;
+       pb_data_h pwm_handle = (pb_data_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(pwm_handle->watch_id);
@@ -70,7 +70,7 @@ static void __uart_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
 {
-       pb_uart_data_h uart_handle = (pb_uart_data_h)user_data;
+       pb_data_h uart_handle = (pb_data_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(uart_handle->watch_id);
@@ -81,13 +81,74 @@ static void __spi_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
 {
-       pb_spi_data_h spi_handle = (pb_spi_data_h)user_data;
+       pb_data_h spi_handle = (pb_data_h)user_data;
        _D("appid [%s] vanished ", name);
 
        g_bus_unwatch_name(spi_handle->watch_id);
        peripheral_bus_spi_close(spi_handle);
 }
 
+static int peripheral_bus_get_client_info(
+               GDBusMethodInvocation *invocation,
+               peripheral_bus_s *pb_data,
+               pb_client_info_s *client_info)
+{
+       guint pid = 0;
+       GError *error = NULL;
+       GVariant *_ret;
+       const gchar *id;
+
+       id = g_dbus_method_invocation_get_sender(invocation);
+       _ret = g_dbus_connection_call_sync(pb_data->connection,
+               "org.freedesktop.DBus",
+               "/org/freedesktop/DBus",
+               "org.freedesktop.DBus",
+               "GetConnectionUnixProcessID",
+               g_variant_new("(s)", id),
+               NULL,
+               G_DBUS_CALL_FLAGS_NONE,
+               -1,
+               NULL,
+               &error);
+
+       if (_ret == NULL) {
+               _E("Failed to get client pid, %s", error->message);
+               g_error_free(error);
+
+               return -1;
+       }
+
+       g_variant_get(_ret, "(u)", &pid);
+       g_variant_unref(_ret);
+
+       client_info->pid = (pid_t)pid;
+       client_info->pgid = getpgid(pid);
+       client_info->id = strdup(id);
+
+       return 0;
+}
+
+static int peripheral_bus_handle_is_valid(
+               GDBusMethodInvocation *invocation,
+               pb_data_h handle,
+               GList *list)
+{
+       const gchar *id;
+
+       if (!g_list_find(list, handle)) {
+               _E("Cannot find handle");
+               return -1;
+       }
+
+       id = g_dbus_method_invocation_get_sender(invocation);
+       if (strcmp(handle->client_info.id, id)) {
+               _E("Invalid access, handle id : %s, current id : %s", handle->client_info.id, id);
+               return -1;
+       }
+
+       return 0;
+}
+
 gboolean handle_gpio_open(
                PeripheralIoGdbusGpio *gpio,
                GDBusMethodInvocation *invocation,
@@ -96,7 +157,7 @@ gboolean handle_gpio_open(
 {
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-       pb_gpio_data_h gpio_handle;
+       pb_data_h gpio_handle;
 
        if ((ret = peripheral_bus_gpio_open(pin, &gpio_handle, user_data)) < PERIPHERAL_ERROR_NONE)
                goto out;
@@ -114,7 +175,7 @@ gboolean handle_gpio_open(
                        __gpio_on_name_vanished,
                        gpio_handle,
                        NULL);
-       _D("gpio : %d, id = %s", gpio_handle->pin, gpio_handle->client_info.id);
+       _D("gpio : %d, id = %s", gpio_handle->dev.gpio.pin, gpio_handle->client_info.id);
 
 out:
        peripheral_io_gdbus_gpio_complete_open(gpio, invocation, GPOINTER_TO_UINT(gpio_handle), ret);
@@ -128,27 +189,18 @@ gboolean handle_gpio_close(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!gpio_handle || !gpio_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
                _E("gpio handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(gpio_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", gpio_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else {
+               g_bus_unwatch_name(gpio_handle->watch_id);
+               ret = peripheral_bus_gpio_close(gpio_handle);
        }
 
-       g_bus_unwatch_name(gpio_handle->watch_id);
-       ret = peripheral_bus_gpio_close(gpio_handle);
-out:
        peripheral_io_gdbus_gpio_complete_close(gpio, invocation, ret);
 
        return true;
@@ -160,11 +212,17 @@ gboolean handle_gpio_get_direction(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
-       gint direction;
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       gint direction = 0;
+
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_get_direction(gpio_handle, &direction);
 
-       ret = peripheral_bus_gpio_get_direction(gpio_handle, &direction);
        peripheral_io_gdbus_gpio_complete_get_direction(gpio, invocation, direction, ret);
 
        return true;
@@ -177,10 +235,16 @@ gboolean handle_gpio_set_direction(
                gint direction,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
+
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_set_direction(gpio_handle, direction);
 
-       ret = peripheral_bus_gpio_set_direction(gpio_handle, direction);
        peripheral_io_gdbus_gpio_complete_set_direction(gpio, invocation, ret);
 
        return true;
@@ -192,11 +256,17 @@ gboolean handle_gpio_read(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
        gint read_value = 0;
 
-       ret = peripheral_bus_gpio_read(gpio_handle, &read_value);
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_read(gpio_handle, &read_value);
+
        peripheral_io_gdbus_gpio_complete_read(gpio, invocation, read_value, ret);
 
        return true;
@@ -209,10 +279,16 @@ gboolean handle_gpio_write(
                gint value,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
+
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_write(gpio_handle, value);
 
-       ret = peripheral_bus_gpio_write(gpio_handle, value);
        peripheral_io_gdbus_gpio_complete_write(gpio, invocation, ret);
 
        return true;
@@ -224,11 +300,17 @@ gboolean handle_gpio_get_edge_mode(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
-       gint edge;
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       gint edge = 0;
+
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_get_edge(gpio_handle, &edge);
 
-       ret = peripheral_bus_gpio_get_edge(gpio_handle, &edge);
        peripheral_io_gdbus_gpio_complete_get_edge_mode(gpio, invocation, edge, ret);
 
        return true;
@@ -241,10 +323,16 @@ gboolean handle_gpio_set_edge_mode(
                gint edge,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
+
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_set_edge(gpio_handle, edge);
 
-       ret = peripheral_bus_gpio_set_edge(gpio_handle, edge);
        peripheral_io_gdbus_gpio_complete_set_edge_mode(gpio, invocation, ret);
 
        return true;
@@ -256,10 +344,16 @@ gboolean handle_gpio_register_irq(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
+
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_register_irq(gpio_handle);
 
-       ret = peripheral_bus_gpio_register_irq(gpio_handle);
        peripheral_io_gdbus_gpio_complete_register_irq(gpio, invocation, ret);
 
        return true;
@@ -271,10 +365,16 @@ gboolean handle_gpio_unregister_irq(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_gpio_data_h gpio_handle = GUINT_TO_POINTER(handle);
+       pb_data_h gpio_handle = GUINT_TO_POINTER(handle);
+
+       if (peripheral_bus_handle_is_valid(invocation, gpio_handle, pb_data->gpio_list) != 0) {
+               _E("gpio handle is not valid");
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_gpio_unregister_irq(gpio_handle);
 
-       ret = peripheral_bus_gpio_unregister_irq(gpio_handle);
        peripheral_io_gdbus_gpio_complete_unregister_irq(gpio, invocation, ret);
 
        return true;
@@ -289,7 +389,7 @@ gboolean handle_i2c_open(
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_i2c_data_h i2c_handle;
+       pb_data_h i2c_handle;
 
        if ((ret = peripheral_bus_i2c_open(bus, address, &i2c_handle, user_data)) < PERIPHERAL_ERROR_NONE)
                goto out;
@@ -321,27 +421,18 @@ gboolean handle_i2c_close(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_i2c_data_h i2c_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h i2c_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!i2c_handle || !i2c_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, i2c_handle, pb_data->i2c_list) != 0) {
                _E("i2c handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(i2c_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", i2c_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else {
+               g_bus_unwatch_name(i2c_handle->watch_id);
+               ret = peripheral_bus_i2c_close(i2c_handle);
        }
 
-       g_bus_unwatch_name(i2c_handle->watch_id);
-       ret = peripheral_bus_i2c_close(i2c_handle);
-
-out:
        peripheral_io_gdbus_i2c_complete_close(i2c, invocation, ret);
 
        return true;
@@ -354,30 +445,19 @@ gboolean handle_i2c_read(
                gint length,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_i2c_data_h i2c_handle = GUINT_TO_POINTER(handle);
+       pb_data_h i2c_handle = GUINT_TO_POINTER(handle);
        GVariant *data_array = NULL;
        uint8_t err_buf[2] = {0, };
-       const gchar *id;
 
-       /* Handle validation */
-       if (!i2c_handle || !i2c_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, i2c_handle, pb_data->i2c_list) != 0) {
                _E("i2c handle is not valid");
                data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(i2c_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", i2c_handle->client_info.id, id);
-               data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_i2c_read(i2c_handle, length, &data_array);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_i2c_read(i2c_handle, length, &data_array);
 
-out:
        peripheral_io_gdbus_i2c_complete_read(i2c, invocation, data_array, ret);
 
        return true;
@@ -391,26 +471,16 @@ gboolean handle_i2c_write(
                GVariant *data_array,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_i2c_data_h i2c_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h i2c_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!i2c_handle || !i2c_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, i2c_handle, pb_data->i2c_list) != 0) {
                _E("i2c handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(i2c_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", i2c_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_i2c_write(i2c_handle, length, data_array);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_i2c_write(i2c_handle, length, data_array);
 
-out:
        peripheral_io_gdbus_i2c_complete_write(i2c, invocation, ret);
 
        return true;
@@ -426,26 +496,17 @@ gboolean handle_i2c_smbus_ioctl(
                guint16 data_in,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_i2c_data_h i2c_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h i2c_handle = GUINT_TO_POINTER(handle);
        uint16_t data = 0xFFFF;
 
-       /* Handle validation */
-       if (!i2c_handle || !i2c_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, i2c_handle, pb_data->i2c_list) != 0) {
                _E("i2c handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(i2c_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", i2c_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_i2c_smbus_ioctl(i2c_handle, read_write, command, size, data_in, &data);
 
-       ret = peripheral_bus_i2c_smbus_ioctl(i2c_handle, read_write, command, size, data_in, &data);
-out:
        peripheral_io_gdbus_i2c_complete_smbus_ioctl(i2c, invocation, data, ret);
 
        return true;
@@ -460,7 +521,7 @@ gboolean handle_pwm_open(
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle;
+       pb_data_h pwm_handle;
 
        if ((ret = peripheral_bus_pwm_open(device, channel, &pwm_handle, user_data)) <  PERIPHERAL_ERROR_NONE)
                goto out;
@@ -492,23 +553,16 @@ gboolean handle_pwm_close(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
        } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else {
-                       g_bus_unwatch_name(pwm_handle->watch_id);
-                       ret = peripheral_bus_pwm_close(pwm_handle);
-               }
+               g_bus_unwatch_name(pwm_handle->watch_id);
+               ret = peripheral_bus_pwm_close(pwm_handle);
        }
 
        peripheral_io_gdbus_pwm_complete_close(pwm, invocation, ret);
@@ -523,22 +577,15 @@ gboolean handle_pwm_set_period(
                gint period,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_set_period(pwm_handle, period);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_pwm_set_period(pwm_handle, period);
 
        peripheral_io_gdbus_pwm_complete_set_period(pwm, invocation, ret);
 
@@ -551,23 +598,16 @@ gboolean handle_pwm_get_period(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
        int period = 0;
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_get_period(pwm_handle, &period);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_pwm_get_period(pwm_handle, &period);
 
        peripheral_io_gdbus_pwm_complete_get_period(pwm, invocation, period, ret);
 
@@ -581,22 +621,15 @@ gboolean handle_pwm_set_duty_cycle(
                gint duty_cycle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_set_duty_cycle(pwm_handle, duty_cycle);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_pwm_set_duty_cycle(pwm_handle, duty_cycle);
 
        peripheral_io_gdbus_pwm_complete_set_duty_cycle(pwm, invocation, ret);
 
@@ -609,23 +642,16 @@ gboolean handle_pwm_get_duty_cycle(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
        int duty_cycle = 0;
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_get_duty_cycle(pwm_handle, &duty_cycle);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_pwm_get_duty_cycle(pwm_handle, &duty_cycle);
 
        peripheral_io_gdbus_pwm_complete_get_duty_cycle(pwm, invocation, duty_cycle, ret);
 
@@ -639,22 +665,15 @@ gboolean handle_pwm_set_polarity(
                gint polarity,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_set_polarity(pwm_handle, polarity);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_pwm_set_polarity(pwm_handle, polarity);
 
        peripheral_io_gdbus_pwm_complete_set_polarity(pwm, invocation, ret);
 
@@ -667,23 +686,16 @@ gboolean handle_pwm_get_polarity(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
        int polarity = 0;
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_get_polarity(pwm_handle, &polarity);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+       ret = peripheral_bus_pwm_get_polarity(pwm_handle, &polarity);
 
        peripheral_io_gdbus_pwm_complete_get_polarity(pwm, invocation, polarity, ret);
 
@@ -697,22 +709,15 @@ gboolean handle_pwm_set_enable(
                gint enable,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_set_enable(pwm_handle, enable);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_pwm_set_enable(pwm_handle, enable);
 
        peripheral_io_gdbus_pwm_complete_set_enable(pwm, invocation, ret);
 
@@ -725,23 +730,16 @@ gboolean handle_pwm_get_enable(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_pwm_data_h pwm_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h pwm_handle = GUINT_TO_POINTER(handle);
        bool enable = false;
 
-       /* Handle validation */
-       if (!pwm_handle || !pwm_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, pwm_handle, pb_data->pwm_list) != 0) {
                _E("pwm handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(pwm_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", pwm_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_pwm_get_enable(pwm_handle, &enable);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_pwm_get_enable(pwm_handle, &enable);
 
        peripheral_io_gdbus_pwm_complete_get_enable(pwm, invocation, enable, ret);
 
@@ -783,7 +781,7 @@ gboolean handle_uart_open(
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle;
+       pb_data_h uart_handle;
 
        if ((ret = peripheral_bus_uart_open(port, &uart_handle, user_data)) < PERIPHERAL_ERROR_NONE)
                goto out;
@@ -815,23 +813,16 @@ gboolean handle_uart_close(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h uart_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!uart_handle || !uart_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, uart_handle, pb_data->uart_list) != 0) {
                _E("uart handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
        } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(uart_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", uart_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else {
-                       g_bus_unwatch_name(uart_handle->watch_id);
-                       ret = peripheral_bus_uart_close(uart_handle);
-               }
+               g_bus_unwatch_name(uart_handle->watch_id);
+               ret = peripheral_bus_uart_close(uart_handle);
        }
 
        peripheral_io_gdbus_uart_complete_close(uart, invocation, ret);
@@ -845,22 +836,15 @@ gboolean handle_uart_flush(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h uart_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!uart_handle || !uart_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, uart_handle, pb_data->uart_list) != 0) {
                _E("uart handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(uart_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", uart_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_uart_flush(uart_handle);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_uart_flush(uart_handle);
 
        peripheral_io_gdbus_uart_complete_flush(uart, invocation, ret);
 
@@ -874,22 +858,15 @@ gboolean handle_uart_set_baudrate(
                guint baudrate,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h uart_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!uart_handle || !uart_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, uart_handle, pb_data->uart_list) != 0) {
                _E("uart handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(uart_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", uart_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_uart_set_baudrate(uart_handle, baudrate);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_uart_set_baudrate(uart_handle, baudrate);
 
        peripheral_io_gdbus_uart_complete_set_baudrate(uart, invocation, ret);
 
@@ -904,22 +881,15 @@ gboolean handle_uart_set_mode(
                guint stop_bits,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h uart_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!uart_handle || !uart_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, uart_handle, pb_data->uart_list) != 0) {
                _E("uart handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(uart_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", uart_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_uart_set_mode(uart_handle, byte_size, parity, stop_bits);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_uart_set_mode(uart_handle, byte_size, parity, stop_bits);
 
        peripheral_io_gdbus_uart_complete_set_mode(uart, invocation, ret);
 
@@ -933,22 +903,15 @@ gboolean handle_uart_set_flowcontrol(
                gboolean rtscts,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h uart_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!uart_handle || !uart_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, uart_handle, pb_data->uart_list) != 0) {
                _E("uart handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(uart_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", uart_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_uart_set_flowcontrol(uart_handle, xonxoff, rtscts);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_uart_set_flowcontrol(uart_handle, xonxoff, rtscts);
 
        peripheral_io_gdbus_uart_complete_set_flowcontrol(uart, invocation, ret);
 
@@ -962,24 +925,17 @@ gboolean handle_uart_read(
                gint length,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h uart_handle = GUINT_TO_POINTER(handle);
        uint8_t err_buf[2] = {0, };
        GVariant *data_array = NULL;
 
-       /* Handle validation */
-       if (!uart_handle || !uart_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, uart_handle, pb_data->uart_list) != 0) {
                _E("uart handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(uart_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", uart_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_uart_read(uart_handle, &data_array, length);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_uart_read(uart_handle, &data_array, length);
 
        if (!data_array)
                data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
@@ -997,22 +953,15 @@ gboolean handle_uart_write(
                GVariant *data_array,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_uart_data_h uart_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h uart_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!uart_handle || !uart_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, uart_handle, pb_data->uart_list) != 0) {
                _E("uart handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-       } else {
-               id = g_dbus_method_invocation_get_sender(invocation);
-               if (strcmp(uart_handle->client_info.id, id)) {
-                       _E("Invalid access, handle id : %s, current id : %s", uart_handle->client_info.id, id);
-                       ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               } else
-                       ret = peripheral_bus_uart_write(uart_handle, data_array, length);
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_uart_write(uart_handle, data_array, length);
 
        peripheral_io_gdbus_uart_complete_write(uart, invocation, ret);
 
@@ -1028,14 +977,15 @@ gboolean handle_spi_open(
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle;
+       pb_data_h spi_handle;
 
-       ret = peripheral_bus_spi_open(bus, cs, &spi_handle, user_data);
-       if (ret == PERIPHERAL_ERROR_NONE) {
-               if (peripheral_bus_get_client_info(invocation, pb_data, &spi_handle->client_info) == 0)
-                       _D("bus : %d, cs : %d, id = %s", bus, cs, spi_handle->client_info.id);
-               else
-                       ret = PERIPHERAL_ERROR_UNKNOWN;
+       if ((ret = peripheral_bus_spi_open(bus, cs, &spi_handle, user_data)) < PERIPHERAL_ERROR_NONE)
+               goto out;
+
+       if (peripheral_bus_get_client_info(invocation, pb_data, &spi_handle->client_info) < 0) {
+               peripheral_bus_gpio_close(spi_handle);
+               ret = PERIPHERAL_ERROR_UNKNOWN;
+               goto out;
        }
 
        spi_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM ,
@@ -1045,7 +995,9 @@ gboolean handle_spi_open(
                        __spi_on_name_vanished,
                        spi_handle,
                        NULL);
+       _D("bus : %d, cs : %d, id = %s", bus, cs, spi_handle->client_info.id);
 
+out:
        peripheral_io_gdbus_spi_complete_open(spi, invocation, GPOINTER_TO_UINT(spi_handle), ret);
 
        return true;
@@ -1057,27 +1009,18 @@ gboolean handle_spi_close(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else {
+               g_bus_unwatch_name(spi_handle->watch_id);
+               ret = peripheral_bus_spi_close(spi_handle);
        }
 
-       g_bus_unwatch_name(spi_handle->watch_id);
-       ret = peripheral_bus_spi_close(spi_handle);
-
-out:
        peripheral_io_gdbus_spi_complete_close(spi, invocation, ret);
 
        return true;
@@ -1090,26 +1033,16 @@ gboolean handle_spi_set_mode(
                guchar mode,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_set_mode(spi_handle, mode);
 
-       ret = peripheral_bus_spi_set_mode(spi_handle, mode);
-
-out:
        peripheral_io_gdbus_spi_complete_set_mode(spi, invocation, ret);
 
        return true;
@@ -1121,27 +1054,17 @@ gboolean handle_spi_get_mode(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
        uint8_t mode = 0;
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_get_mode(spi_handle, &mode);
 
-       ret = peripheral_bus_spi_get_mode(spi_handle, &mode);
-
-out:
        peripheral_io_gdbus_spi_complete_get_mode(spi, invocation, mode, ret);
 
        return true;
@@ -1154,26 +1077,16 @@ gboolean handle_spi_set_lsb_first(
                gboolean lsb,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_set_lsb_first(spi_handle, lsb);
 
-       ret = peripheral_bus_spi_set_lsb_first(spi_handle, lsb);
-
-out:
        peripheral_io_gdbus_spi_complete_set_lsb_first(spi, invocation, ret);
 
        return true;
@@ -1185,27 +1098,17 @@ gboolean handle_spi_get_lsb_first(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
        gboolean lsb = 0;
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_spi_get_lsb_first(spi_handle, &lsb);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_get_lsb_first(spi_handle, &lsb);
 
-out:
        peripheral_io_gdbus_spi_complete_get_lsb_first(spi, invocation, lsb, ret);
 
        return true;
@@ -1218,26 +1121,16 @@ gboolean handle_spi_set_bits(
                guchar bits,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_spi_set_bits(spi_handle, bits);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_set_bits(spi_handle, bits);
 
-out:
        peripheral_io_gdbus_spi_complete_set_bits(spi, invocation, ret);
 
        return true;
@@ -1249,27 +1142,17 @@ gboolean handle_spi_get_bits(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
        uint8_t bits = 0;
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_spi_get_bits(spi_handle, &bits);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_get_bits(spi_handle, &bits);
 
-out:
        peripheral_io_gdbus_spi_complete_get_bits(spi, invocation, bits, ret);
 
        return true;
@@ -1282,26 +1165,16 @@ gboolean handle_spi_set_frequency(
                guint freq,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_spi_set_frequency(spi_handle, freq);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_set_frequency(spi_handle, freq);
 
-out:
        peripheral_io_gdbus_spi_complete_set_frequency(spi, invocation, ret);
 
        return true;
@@ -1313,27 +1186,17 @@ gboolean handle_spi_get_frequency(
                gint handle,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
        unsigned int freq = 0;
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_get_frequency(spi_handle, &freq);
 
-       ret = peripheral_bus_spi_get_frequency(spi_handle, &freq);
-
-out:
        peripheral_io_gdbus_spi_complete_get_frequency(spi, invocation, freq, ret);
 
        return true;
@@ -1346,30 +1209,19 @@ gboolean handle_spi_read(
                gint length,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
        GVariant *data_array = NULL;
        uint8_t err_buf[2] = {0, };
-       const gchar *id;
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
                data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_spi_read(spi_handle, &data_array, length);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_read(spi_handle, &data_array, length);
 
-out:
        peripheral_io_gdbus_spi_complete_read(spi, invocation, data_array, ret);
 
        return true;
@@ -1383,26 +1235,16 @@ gboolean handle_spi_write(
                GVariant *data_array,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
-       const gchar *id;
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
-
-       ret = peripheral_bus_spi_write(spi_handle, data_array, length);
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_write(spi_handle, data_array, length);
 
-out:
        peripheral_io_gdbus_spi_complete_write(spi, invocation, ret);
 
        return true;
@@ -1416,30 +1258,19 @@ gboolean handle_spi_read_write(
                GVariant *tx_data_array,
                gpointer user_data)
 {
+       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
-       pb_spi_data_h spi_handle = GUINT_TO_POINTER(handle);
+       pb_data_h spi_handle = GUINT_TO_POINTER(handle);
        GVariant *rx_data_array = NULL;
        uint8_t err_buf[2] = {0, };
-       const gchar *id;
 
-       /* Handle validation */
-       if (!spi_handle || !spi_handle->client_info.id) {
+       if (peripheral_bus_handle_is_valid(invocation, spi_handle, pb_data->spi_list) != 0) {
                _E("spi handle is not valid");
                rx_data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
-               ret = PERIPHERAL_ERROR_UNKNOWN;
-               goto out;
-       }
-       id = g_dbus_method_invocation_get_sender(invocation);
-       if (strcmp(spi_handle->client_info.id, id)) {
-               _E("Invalid access, handle id : %s, current id : %s", spi_handle->client_info.id, id);
-               rx_data_array = peripheral_bus_build_variant_ay(err_buf, sizeof(err_buf));
-               ret = PERIPHERAL_ERROR_INVALID_OPERATION;
-               goto out;
-       }
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else
+               ret = peripheral_bus_spi_read_write(spi_handle, tx_data_array, &rx_data_array, length);
 
-       ret = peripheral_bus_spi_read_write(spi_handle, tx_data_array, &rx_data_array, length);
-
-out:
        peripheral_io_gdbus_spi_complete_read_write(spi, invocation, rx_data_array, ret);
 
        return true;
index d1282c1..84bcab8 100644 (file)
 
 #include "peripheral_io_gdbus.h"
 
+typedef enum {
+       PERIPHERAL_BUS_TYPE_GPIO = 0,
+       PERIPHERAL_BUS_TYPE_I2C,
+       PERIPHERAL_BUS_TYPE_PWM,
+       PERIPHERAL_BUS_TYPE_ADC,
+       PERIPHERAL_BUS_TYPE_UART,
+       PERIPHERAL_BUS_TYPE_SPI,
+} peripheral_bus_type_e;
+
 typedef struct {
        /* daemon variable */
        char *adc_path;
@@ -57,13 +66,9 @@ typedef struct {
        int value_fd;
        GIOChannel *io;
        guint io_id;
-       uint watch_id;
-       GList **list;
-       /* client info */
-       pb_client_info_s client_info;
        /* gdbus variable */
        PeripheralIoGdbusGpio *gpio_skeleton;
-} peripheral_bus_gpio_data_s;
+} peripheral_bus_gpio_s;
 
 typedef struct {
        /* i2c device information */
@@ -73,31 +78,19 @@ typedef struct {
        /* data buffer */
        uint8_t *buffer;
        int buffer_size;
-       uint watch_id;
-       GList **list;
-       /* client info */
-       pb_client_info_s client_info;
-} peripheral_bus_i2c_data_s;
+} peripheral_bus_i2c_s;
 
 typedef struct {
        int device;
        int channel;
-       uint watch_id;
-       GList **list;
-       /* client info */
-       pb_client_info_s client_info;
-} peripheral_bus_pwm_data_s;
+} peripheral_bus_pwm_s;
 
 typedef struct {
        int port;
        int fd;
        uint8_t *buffer;
        int buffer_size;
-       uint watch_id;
-       GList **list;
-       /* client info */
-       pb_client_info_s client_info;
-} peripheral_bus_uart_data_s;
+} peripheral_bus_uart_s;
 
 typedef struct {
        int bus;
@@ -108,17 +101,24 @@ typedef struct {
        uint8_t *tx_buf;
        int rx_buf_size;
        int tx_buf_size;
+} peripheral_bus_spi_s;
+
+typedef struct {
+       peripheral_bus_type_e type;
        uint watch_id;
        GList **list;
        /* client info */
        pb_client_info_s client_info;
-} peripheral_bus_spi_data_s;
+       union {
+               peripheral_bus_gpio_s gpio;
+               peripheral_bus_i2c_s i2c;
+               peripheral_bus_pwm_s pwm;
+               peripheral_bus_uart_s uart;
+               peripheral_bus_spi_s spi;
+       } dev;
+} peripheral_bus_data_s;
 
-typedef peripheral_bus_gpio_data_s *pb_gpio_data_h;
-typedef peripheral_bus_i2c_data_s *pb_i2c_data_h;
-typedef peripheral_bus_pwm_data_s *pb_pwm_data_h;
-typedef peripheral_bus_uart_data_s *pb_uart_data_h;
-typedef peripheral_bus_spi_data_s *pb_spi_data_h;
+typedef peripheral_bus_data_s *pb_data_h;
 
 void peripheral_bus_emit_gpio_changed(PeripheralIoGdbusGpio *gpio,
                                                                        gint pin,
index 6017415..feefc70 100644 (file)
 #include "gpio.h"
 #include "peripheral_bus.h"
 #include "peripheral_common.h"
+#include "peripheral_bus_util.h"
 
-static pb_gpio_data_h peripheral_bus_gpio_data_get(int pin, GList **list)
+static bool peripheral_bus_gpio_is_available(int pin, GList *list)
 {
-       GList *gpio_list = *list;
        GList *link;
-       pb_gpio_data_h gpio_data = NULL;
+       pb_data_h handle = NULL;
 
-       if (!gpio_list)
-               return NULL;
-
-       link = gpio_list;
+       link = list;
        while (link) {
-               gpio_data = (pb_gpio_data_h)link->data;
-               if (gpio_data->pin == pin)
-                       return gpio_data;
-
+               handle = (pb_data_h)link->data;
+               if (handle->dev.gpio.pin == pin)
+                       return false;
                link = g_list_next(link);
        }
 
-       return NULL;
-}
-
-static pb_gpio_data_h peripheral_bus_gpio_data_new(GList **list)
-{
-       GList *gpio_list = *list;
-       pb_gpio_data_h gpio_data;
-
-       gpio_data = (pb_gpio_data_h)calloc(1, sizeof(peripheral_bus_gpio_data_s));
-       if (gpio_data == NULL) {
-               _E("failed to allocate peripheral_bus_gpio_data_s");
-               return NULL;
-       }
-
-       *list = g_list_append(gpio_list, gpio_data);
-
-       return gpio_data;
-}
-
-static int peripheral_bus_gpio_data_free(pb_gpio_data_h gpio_handle, GList **gpio_list)
-{
-       GList *link;
-
-       RETVM_IF(gpio_handle == NULL, -1, "handle is null");
-
-       link = g_list_find(*gpio_list, gpio_handle);
-       if (!link) {
-               _E("handle does not exist in list");
-               return -1;
-       }
-
-       *gpio_list = g_list_remove_link(*gpio_list, link);
-       free(gpio_handle);
-       g_list_free(link);
-
-       return 0;
+       return true;
 }
 
-int peripheral_bus_gpio_open(gint pin, pb_gpio_data_h *gpio, gpointer user_data)
+int peripheral_bus_gpio_open(gint pin, pb_data_h *handle, gpointer user_data)
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-       pb_gpio_data_h gpio_handle;
+       pb_data_h gpio_handle;
        int edge, direction;
        int ret;
 
-       gpio_handle = peripheral_bus_gpio_data_get(pin, &pb_data->gpio_list);
-       if (gpio_handle) {
+       if (!peripheral_bus_gpio_is_available(pin, pb_data->gpio_list)) {
                _E("gpio %d is busy", pin);
                return PERIPHERAL_ERROR_RESOURCE_BUSY;
        }
@@ -108,20 +68,21 @@ int peripheral_bus_gpio_open(gint pin, pb_gpio_data_h *gpio, gpointer user_data)
                goto err;
        }
 
-       gpio_handle = peripheral_bus_gpio_data_new(&pb_data->gpio_list);
+       gpio_handle = peripheral_bus_data_new(&pb_data->gpio_list);
        if (!gpio_handle) {
-               _E("peripheral_bus_gpio_data_new error");
+               _E("peripheral_bus_data_new error");
                ret = PERIPHERAL_ERROR_UNKNOWN;
                goto err;
        }
 
-       gpio_handle->pin = pin;
-       gpio_handle->edge = edge;
-       gpio_handle->direction = direction;
+       gpio_handle->type = PERIPHERAL_BUS_TYPE_GPIO;
        gpio_handle->list = &pb_data->gpio_list;
-       gpio_handle->gpio_skeleton = pb_data->gpio_skeleton;
+       gpio_handle->dev.gpio.pin = pin;
+       gpio_handle->dev.gpio.edge = edge;
+       gpio_handle->dev.gpio.direction = direction;
+       gpio_handle->dev.gpio.gpio_skeleton = pb_data->gpio_skeleton;
 
-       *gpio = gpio_handle;
+       *handle = gpio_handle;
 
        return PERIPHERAL_ERROR_NONE;
 
@@ -132,8 +93,9 @@ open_err:
        return ret;
 }
 
-int peripheral_bus_gpio_set_direction(pb_gpio_data_h gpio, gint direction)
+int peripheral_bus_gpio_set_direction(pb_data_h handle, gint direction)
 {
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
        int ret;
 
        gpio->direction = direction;
@@ -146,10 +108,11 @@ int peripheral_bus_gpio_set_direction(pb_gpio_data_h gpio, gint direction)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_bus_gpio_get_direction(pb_gpio_data_h gpio, gint *direction)
+int peripheral_bus_gpio_get_direction(pb_data_h handle, gint *direction)
 {
-       gint value;
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
        int ret;
+       gint value;
 
        if ((ret = gpio_get_direction(gpio->pin, (gpio_direction_e*)direction)) < 0) {
                _E("gpio_get_direction error (%d)", ret);
@@ -168,8 +131,9 @@ int peripheral_bus_gpio_get_direction(pb_gpio_data_h gpio, gint *direction)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_bus_gpio_set_edge(pb_gpio_data_h gpio, gint edge)
+int peripheral_bus_gpio_set_edge(pb_data_h handle, gint edge)
 {
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
        int ret;
 
        if ((ret = gpio_set_edge_mode(gpio->pin, (gpio_edge_e)edge)) < 0) {
@@ -182,8 +146,9 @@ int peripheral_bus_gpio_set_edge(pb_gpio_data_h gpio, gint edge)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_bus_gpio_get_edge(pb_gpio_data_h gpio, gint *edge)
+int peripheral_bus_gpio_get_edge(pb_data_h handle, gint *edge)
 {
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
        int ret;
 
        if ((ret = gpio_get_edge_mode(gpio->pin, (gpio_edge_e*)edge)) < 0) {
@@ -196,8 +161,9 @@ int peripheral_bus_gpio_get_edge(pb_gpio_data_h gpio, gint *edge)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_bus_gpio_write(pb_gpio_data_h gpio, gint value)
+int peripheral_bus_gpio_write(pb_data_h handle, gint value)
 {
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
        int ret;
 
        /* Return error if direction is input mode */
@@ -214,8 +180,9 @@ int peripheral_bus_gpio_write(pb_gpio_data_h gpio, gint value)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_bus_gpio_read(pb_gpio_data_h gpio, gint *value)
+int peripheral_bus_gpio_read(pb_data_h handle, gint *value)
 {
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
        int ret;
 
        if ((ret = gpio_read(gpio->pin, value)) < 0) {
@@ -228,7 +195,7 @@ int peripheral_bus_gpio_read(pb_gpio_data_h gpio, gint *value)
 
 static gboolean  peripheral_bus_gpio_cb(GIOChannel *io, GIOCondition condition, gpointer data)
 {
-       pb_gpio_data_h gpio_data = (pb_gpio_data_h)data;
+       peripheral_bus_gpio_s *gpio_data = (peripheral_bus_gpio_s *)data;
        GIOStatus status;
        gchar* strval;
        int value;
@@ -271,8 +238,9 @@ static gboolean  peripheral_bus_gpio_cb(GIOChannel *io, GIOCondition condition,
        return TRUE;
 }
 
-int peripheral_bus_gpio_register_irq(pb_gpio_data_h gpio)
+int peripheral_bus_gpio_register_irq(pb_data_h handle)
 {
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
        GIOStatus status;
        gchar* strval;
 
@@ -316,8 +284,10 @@ err_open_isr:
        return PERIPHERAL_ERROR_UNKNOWN;
 }
 
-int peripheral_bus_gpio_unregister_irq(pb_gpio_data_h gpio)
+int peripheral_bus_gpio_unregister_irq(pb_data_h handle)
 {
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
+
        if (gpio->io) {
                gpio->irq_en = 0;
                g_source_remove(gpio->io_id);
@@ -331,18 +301,18 @@ int peripheral_bus_gpio_unregister_irq(pb_gpio_data_h gpio)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_bus_gpio_close(pb_gpio_data_h gpio)
+int peripheral_bus_gpio_close(pb_data_h handle)
 {
-       int ret;
-       int pin = gpio->pin;
-
-       if (peripheral_bus_gpio_data_free(gpio, gpio->list) < 0)
-               _E("Failed to free gpio data");
+       peripheral_bus_gpio_s *gpio = &handle->dev.gpio;
+       int ret = PERIPHERAL_ERROR_NONE;
 
-       if ((ret = gpio_close(pin)) < 0) {
-               _E("gpio_close error (%d)", ret);
+       if ((ret = gpio_close(gpio->pin)) < 0)
                return ret;
+
+       if (peripheral_bus_data_free(handle) < 0) {
+               _E("Failed to free gpio data");
+               ret = PERIPHERAL_ERROR_UNKNOWN;
        }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;;
 }
index eec1e00..3a63530 100644 (file)
 #ifndef __PERIPHERAL_BUS_GPIO_H__
 #define __PERIPHERAL_BUS_GPIO_H__
 
-int peripheral_bus_gpio_open(gint pin, pb_gpio_data_h *gpio, gpointer user_data);
-int peripheral_bus_gpio_set_direction(pb_gpio_data_h gpio, gint direction);
-int peripheral_bus_gpio_get_direction(pb_gpio_data_h gpio, gint *direction);
-int peripheral_bus_gpio_set_edge(pb_gpio_data_h gpio, gint edge);
-int peripheral_bus_gpio_get_edge(pb_gpio_data_h gpio, gint *edge);
-int peripheral_bus_gpio_write(pb_gpio_data_h gpio, gint value);
-int peripheral_bus_gpio_read(pb_gpio_data_h gpio, gint *value);
-int peripheral_bus_gpio_register_irq(pb_gpio_data_h gpio);
-int peripheral_bus_gpio_unregister_irq(pb_gpio_data_h gpio);
-int peripheral_bus_gpio_close(pb_gpio_data_h gpio);
+int peripheral_bus_gpio_open(gint pin, pb_data_h *handle, gpointer user_data);
+int peripheral_bus_gpio_set_direction(pb_data_h handle, gint direction);
+int peripheral_bus_gpio_get_direction(pb_data_h handle, gint *direction);
+int peripheral_bus_gpio_set_edge(pb_data_h handle, gint edge);
+int peripheral_bus_gpio_get_edge(pb_data_h handle, gint *edge);
+int peripheral_bus_gpio_write(pb_data_h handle, gint value);
+int peripheral_bus_gpio_read(pb_data_h handle, gint *value);
+int peripheral_bus_gpio_register_irq(pb_data_h handle);
+int peripheral_bus_gpio_unregister_irq(pb_data_h handle);
+int peripheral_bus_gpio_close(pb_data_h handle);
 
 #endif /* __PERIPHERAL_BUS_GPIO_H__ */
index 57bb90c..2e0141f 100644 (file)
 #define INITIAL_BUFFER_SIZE 128
 #define MAX_BUFFER_SIZE 8192
 
-static pb_i2c_data_h peripheral_bus_i2c_data_get(int bus, int address, GList **list)
+static bool peripheral_bus_i2c_is_available(int bus, int address, GList *list)
 {
-       GList *i2c_list = *list;
        GList *link;
-       pb_i2c_data_h i2c_handle;
+       pb_data_h handle;
 
-       if (i2c_list == NULL)
-               return NULL;
-
-       link = i2c_list;
+       link = list;
        while (link) {
-               i2c_handle = (pb_i2c_data_h)link->data;
-               if (i2c_handle->bus == bus && i2c_handle->address == address)
-                       return i2c_handle;
+               handle = (pb_data_h)link->data;
+               if (handle->dev.i2c.bus == bus && handle->dev.i2c.address == address)
+                       return false;
                link = g_list_next(link);
        }
 
-       return NULL;
-}
-
-static pb_i2c_data_h peripheral_bus_i2c_data_new(GList **list)
-{
-       GList *i2c_list = *list;
-       pb_i2c_data_h i2c_handle;
-
-       i2c_handle = (pb_i2c_data_h)calloc(1, sizeof(peripheral_bus_i2c_data_s));
-       if (i2c_handle == NULL) {
-               _E("failed to allocate peripheral_bus_i2c_data_s");
-               return NULL;
-       }
-
-       *list = g_list_append(i2c_list, i2c_handle);
-
-       return i2c_handle;
+       return true;
 }
 
-static int peripheral_bus_i2c_data_free(pb_i2c_data_h i2c_handle, GList **i2c_list)
-{
-       GList *link;
-
-       RETVM_IF(i2c_handle == NULL, -1, "handle is null");
-
-       link = g_list_find(*i2c_list, i2c_handle);
-       if (!link) {
-               _E("handle does not exist in list");
-               return -1;
-       }
-
-       *i2c_list = g_list_remove_link(*i2c_list, link);
-       if (i2c_handle->buffer)
-               free(i2c_handle->buffer);
-       free(i2c_handle);
-       g_list_free(link);
-
-       return 0;
-}
-
-int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer user_data)
+int peripheral_bus_i2c_open(int bus, int address, pb_data_h *handle, gpointer user_data)
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-       pb_i2c_data_h i2c_handle;
+       pb_data_h i2c_handle;
        int ret;
        int fd;
 
-       if (peripheral_bus_i2c_data_get(bus, address, &pb_data->i2c_list)) {
+       if (!peripheral_bus_i2c_is_available(bus, address, pb_data->i2c_list)) {
                _E("Resource is in use, bus : %d, address : %d", bus, address);
                return PERIPHERAL_ERROR_RESOURCE_BUSY;
        }
@@ -105,22 +64,23 @@ int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer u
                return ret;
        }
 
-       i2c_handle = peripheral_bus_i2c_data_new(&pb_data->i2c_list);
+       i2c_handle = peripheral_bus_data_new(&pb_data->i2c_list);
 
-       i2c_handle->fd = fd;
-       i2c_handle->bus = bus;
-       i2c_handle->address = address;
+       i2c_handle->type = PERIPHERAL_BUS_TYPE_I2C;
        i2c_handle->list = &pb_data->i2c_list;
-       i2c_handle->buffer = malloc(INITIAL_BUFFER_SIZE);
+       i2c_handle->dev.i2c.fd = fd;
+       i2c_handle->dev.i2c.bus = bus;
+       i2c_handle->dev.i2c.address = address;
+       i2c_handle->dev.i2c.buffer = malloc(INITIAL_BUFFER_SIZE);
 
-       if (!(i2c_handle->buffer)) {
+       if (!(i2c_handle->dev.i2c.buffer)) {
                i2c_close(fd);
                _E("Failed to allocate data buffer");
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
-       i2c_handle->buffer_size = INITIAL_BUFFER_SIZE;
+       i2c_handle->dev.i2c.buffer_size = INITIAL_BUFFER_SIZE;
 
-       *i2c = i2c_handle;
+       *handle = i2c_handle;
 
        _D("bus : %d, address : %d, pgid = %d, id = %s", bus, address,
                i2c_handle->client_info.pgid,
@@ -129,23 +89,27 @@ int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer u
        return ret;
 }
 
-int peripheral_bus_i2c_close(pb_i2c_data_h i2c)
+int peripheral_bus_i2c_close(pb_data_h handle)
 {
-       int ret;
+       peripheral_bus_i2c_s *i2c = &handle->dev.i2c;
+       int ret = PERIPHERAL_ERROR_NONE;
 
-       _D("bus : %d, address : %d, pgid = %d", i2c->bus, i2c->address, i2c->client_info.pgid);
+       _D("bus : %d, address : %d, pgid = %d", i2c->bus, i2c->address, handle->client_info.pgid);
 
        if ((ret = i2c_close(i2c->fd)) < 0)
                return ret;
 
-       if (peripheral_bus_i2c_data_free(i2c, i2c->list) < 0)
+       if (peripheral_bus_data_free(handle) < 0) {
                _E("Failed to free i2c data");
+               ret = PERIPHERAL_ERROR_UNKNOWN;
+       }
 
        return ret;
 }
 
-int peripheral_bus_i2c_read(pb_i2c_data_h i2c, int length, GVariant **data_array)
+int peripheral_bus_i2c_read(pb_data_h handle, int length, GVariant **data_array)
 {
+       peripheral_bus_i2c_s *i2c = &handle->dev.i2c;
        uint8_t err_buf[2] = {0, };
        int ret;
 
@@ -177,8 +141,9 @@ out:
        return ret;
 }
 
-int peripheral_bus_i2c_write(pb_i2c_data_h i2c, int length, GVariant *data_array)
+int peripheral_bus_i2c_write(pb_data_h handle, int length, GVariant *data_array)
 {
+       peripheral_bus_i2c_s *i2c = &handle->dev.i2c;
        GVariantIter *iter;
        guchar str;
        int i = 0;
@@ -204,8 +169,9 @@ int peripheral_bus_i2c_write(pb_i2c_data_h i2c, int length, GVariant *data_array
        return i2c_write(i2c->fd, i2c->buffer, length);
 }
 
-int peripheral_bus_i2c_smbus_ioctl(pb_i2c_data_h i2c, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out)
+int peripheral_bus_i2c_smbus_ioctl(pb_data_h handle, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out)
 {
+       peripheral_bus_i2c_s *i2c = &handle->dev.i2c;
        struct i2c_smbus_ioctl_data data_arg;
        union i2c_smbus_data data;
        int ret;
index 40d0272..9c337b7 100644 (file)
 #ifndef __PERIPHERAL_BUS_I2C_H__
 #define __PERIPHERAL_BUS_I2C_H__
 
-int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer user_data);
-int peripheral_bus_i2c_close(pb_i2c_data_h i2c);
-int peripheral_bus_i2c_read(pb_i2c_data_h i2c, int length, GVariant **data_array);
-int peripheral_bus_i2c_write(pb_i2c_data_h i2c, int length, GVariant *data_array);
-int peripheral_bus_i2c_smbus_ioctl(pb_i2c_data_h i2c, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out);
+int peripheral_bus_i2c_open(int bus, int address, pb_data_h *handle, gpointer user_data);
+int peripheral_bus_i2c_close(pb_data_h handle);
+int peripheral_bus_i2c_read(pb_data_h handle, int length, GVariant **data_array);
+int peripheral_bus_i2c_write(pb_data_h handle, int length, GVariant *data_array);
+int peripheral_bus_i2c_smbus_ioctl(pb_data_h handle, uint8_t read_write, uint8_t command, uint32_t size, uint16_t data_in, uint16_t *data_out);
 
 #endif /* __PERIPHERAL_BUS_I2C_H__ */
index aed54dd..3291243 100644 (file)
 #include <peripheral_io.h>
 
 #include "pwm.h"
-#include "peripheral_io_gdbus.h"
 #include "peripheral_bus.h"
 #include "peripheral_common.h"
+#include "peripheral_bus_util.h"
 
-static pb_pwm_data_h peripheral_bus_pwm_data_get(int device, int channel, GList **list)
+static bool peripheral_bus_pwm_is_available(int device, int channel, GList *list)
 {
-       GList *pwm_list = *list;
        GList *link;
-       pb_pwm_data_h pwm_handle;
+       pb_data_h handle;
 
-       link = pwm_list;
+       link = list;
        while (link) {
-               pwm_handle = (pb_pwm_data_h)link->data;
-               if (pwm_handle->device == device && pwm_handle->channel == channel)
-                       return pwm_handle;
+               handle = (pb_data_h)link->data;
+               if (handle->dev.pwm.device == device && handle->dev.pwm.channel == channel)
+                       return false;
                link = g_list_next(link);
        }
 
-       return NULL;
+       return true;
 }
 
-static pb_pwm_data_h peripheral_bus_pwm_data_new(GList **list)
-{
-       GList *pwm_list = *list;
-       pb_pwm_data_h pwm_handle;
-
-       pwm_handle = (pb_pwm_data_h)calloc(1, sizeof(peripheral_bus_pwm_data_s));
-       if (pwm_handle == NULL) {
-               _E("failed to allocate peripheral_bus_pwm_data_s");
-               return NULL;
-       }
-
-       *list = g_list_append(pwm_list, pwm_handle);
-
-       return pwm_handle;
-}
-
-static int peripheral_bus_pwm_data_free(pb_pwm_data_h pwm_handle, GList **pwm_list)
-{
-       GList *link;
-
-       RETVM_IF(pwm_handle == NULL, -1, "handle is null");
-
-       link = g_list_find(*pwm_list, pwm_handle);
-       if (!link) {
-               _E("handle does not exist in list");
-               return -1;
-       }
-
-       *pwm_list = g_list_remove_link(*pwm_list, link);
-       free(pwm_handle);
-       g_list_free(link);
-
-       return 0;
-}
-
-int peripheral_bus_pwm_open(int device, int channel, pb_pwm_data_h *pwm, gpointer user_data)
+int peripheral_bus_pwm_open(int device, int channel, pb_data_h *handle, gpointer user_data)
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-       pb_pwm_data_h pwm_handle;
+       pb_data_h pwm_handle;
        int ret;
 
-       if (peripheral_bus_pwm_data_get(device, channel, &pb_data->pwm_list)) {
+       if (!peripheral_bus_pwm_is_available(device, channel, pb_data->pwm_list)) {
                _E("Resource is in use, device : %d, channel : %d", device, channel);
                return PERIPHERAL_ERROR_RESOURCE_BUSY;
        }
@@ -91,17 +55,18 @@ int peripheral_bus_pwm_open(int device, int channel, pb_pwm_data_h *pwm, gpointe
        if ((ret = pwm_open(device, channel)) < 0)
                goto open_err;
 
-       pwm_handle = peripheral_bus_pwm_data_new(&pb_data->pwm_list);
+       pwm_handle = peripheral_bus_data_new(&pb_data->pwm_list);
        if (!pwm_handle) {
-               _E("peripheral_bus_pwm_data_new error");
+               _E("peripheral_bus_data_new error");
                ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
                goto err;
        }
 
+       pwm_handle->type = PERIPHERAL_BUS_TYPE_PWM;
        pwm_handle->list = &pb_data->pwm_list;
-       pwm_handle->device = device;
-       pwm_handle->channel = channel;
-       *pwm = pwm_handle;
+       pwm_handle->dev.pwm.device = device;
+       pwm_handle->dev.pwm.channel = channel;
+       *handle = pwm_handle;
 
        return PERIPHERAL_ERROR_NONE;
 
@@ -112,56 +77,74 @@ open_err:
        return ret;
 }
 
-int peripheral_bus_pwm_close(pb_pwm_data_h pwm)
+int peripheral_bus_pwm_close(pb_data_h handle)
 {
-       int ret;
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+       int ret = PERIPHERAL_ERROR_NONE;
 
-       if ((ret = pwm_close(pwm->device, pwm->channel)) < 0) {
-               _E("gpio_close error (%d)", ret);
+       if ((ret = pwm_close(pwm->device, pwm->channel)) < 0)
                return ret;
-       }
 
-       peripheral_bus_pwm_data_free(pwm, pwm->list);
+       if (peripheral_bus_data_free(handle) < 0) {
+               _E("Failed to free i2c data");
+               ret = PERIPHERAL_ERROR_UNKNOWN;
+       }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
-int peripheral_bus_pwm_set_period(pb_pwm_data_h pwm, int period)
+int peripheral_bus_pwm_set_period(pb_data_h handle, int period)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_set_period(pwm->device, pwm->channel, period);
 }
 
-int peripheral_bus_pwm_get_period(pb_pwm_data_h pwm, int *period)
+int peripheral_bus_pwm_get_period(pb_data_h handle, int *period)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_get_period(pwm->device, pwm->channel, period);
 }
 
-int peripheral_bus_pwm_set_duty_cycle(pb_pwm_data_h pwm, int duty_cycle)
+int peripheral_bus_pwm_set_duty_cycle(pb_data_h handle, int duty_cycle)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_set_duty_cycle(pwm->device, pwm->channel, duty_cycle);
 }
 
-int peripheral_bus_pwm_get_duty_cycle(pb_pwm_data_h pwm, int *duty_cycle)
+int peripheral_bus_pwm_get_duty_cycle(pb_data_h handle, int *duty_cycle)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_get_duty_cycle(pwm->device, pwm->channel, duty_cycle);
 }
 
-int peripheral_bus_pwm_set_polarity(pb_pwm_data_h pwm, int polarity)
+int peripheral_bus_pwm_set_polarity(pb_data_h handle, int polarity)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_set_polarity(pwm->device, pwm->channel, (pwm_polarity_e)polarity);
 }
 
-int peripheral_bus_pwm_get_polarity(pb_pwm_data_h pwm, int *polarity)
+int peripheral_bus_pwm_get_polarity(pb_data_h handle, int *polarity)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_get_polarity(pwm->device, pwm->channel, (pwm_polarity_e*)polarity);
 }
 
-int peripheral_bus_pwm_set_enable(pb_pwm_data_h pwm, bool enable)
+int peripheral_bus_pwm_set_enable(pb_data_h handle, bool enable)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_set_enable(pwm->device, pwm->channel, enable);
 }
 
-int peripheral_bus_pwm_get_enable(pb_pwm_data_h pwm, bool *enable)
+int peripheral_bus_pwm_get_enable(pb_data_h handle, bool *enable)
 {
+       peripheral_bus_pwm_s *pwm = &handle->dev.pwm;
+
        return pwm_get_enable(pwm->device, pwm->channel, enable);
 }
index 915f6fa..fbcc6e3 100644 (file)
 #ifndef __PERIPHERAL_BUS_PWM_H__
 #define __PERIPHERAL_BUS_PWM_H__
 
-int peripheral_bus_pwm_open(int device, int channel, pb_pwm_data_h *pwm, gpointer user_data);
-int peripheral_bus_pwm_close(pb_pwm_data_h pwm);
-int peripheral_bus_pwm_set_period(pb_pwm_data_h pwm, int period);
-int peripheral_bus_pwm_get_period(pb_pwm_data_h pwm, int *period);
-int peripheral_bus_pwm_set_duty_cycle(pb_pwm_data_h pwm, int duty_cycle);
-int peripheral_bus_pwm_get_duty_cycle(pb_pwm_data_h pwm, int *duty_cycle);
-int peripheral_bus_pwm_set_polarity(pb_pwm_data_h pwm, int polarity);
-int peripheral_bus_pwm_get_polarity(pb_pwm_data_h pwm, int *polarity);
-int peripheral_bus_pwm_set_enable(pb_pwm_data_h pwm, bool enable);
-int peripheral_bus_pwm_get_enable(pb_pwm_data_h pwm, bool *enable);
+int peripheral_bus_pwm_open(int device, int channel, pb_data_h *handle, gpointer user_data);
+int peripheral_bus_pwm_close(pb_data_h handle);
+int peripheral_bus_pwm_set_period(pb_data_h handle, int period);
+int peripheral_bus_pwm_get_period(pb_data_h handle, int *period);
+int peripheral_bus_pwm_set_duty_cycle(pb_data_h handle, int duty_cycle);
+int peripheral_bus_pwm_get_duty_cycle(pb_data_h handle, int *duty_cycle);
+int peripheral_bus_pwm_set_polarity(pb_data_h handle, int polarity);
+int peripheral_bus_pwm_get_polarity(pb_data_h handle, int *polarity);
+int peripheral_bus_pwm_set_enable(pb_data_h handle, bool enable);
+int peripheral_bus_pwm_get_enable(pb_data_h handle, bool *enable);
 
 #endif /* __PERIPHERAL_BUS_PWM_H__ */
index 9047ead..9d8c784 100644 (file)
 static int initial_buffer_size = 128;
 static int max_buffer_size = 4096;
 
-static pb_spi_data_h peripheral_bus_spi_data_get(int bus, int cs, GList **list)
+static bool peripheral_bus_spi_is_available(int bus, int cs, GList *list)
 {
-       GList *spi_list = *list;
        GList *link;
-       pb_spi_data_h spi_handle;
+       pb_data_h handle;
 
-       link = spi_list;
+       link = list;
        while (link) {
-               spi_handle = (pb_spi_data_h)link->data;
-               if (spi_handle->bus == bus && spi_handle->cs == cs)
-                       return spi_handle;
+               handle = (pb_data_h)link->data;
+               if (handle->dev.spi.bus == bus && handle->dev.spi.cs == cs)
+                       return false;
                link = g_list_next(link);
        }
 
-       return NULL;
+       return true;
 }
 
-static pb_spi_data_h peripheral_bus_spi_data_new(GList **list)
-{
-       GList *spi_list = *list;
-       pb_spi_data_h spi_handle;
-
-       spi_handle = (pb_spi_data_h)calloc(1, sizeof(peripheral_bus_spi_data_s));
-       if (spi_handle == NULL) {
-               _E("failed to allocate peripheral_bus_spi_data_s");
-               return NULL;
-       }
-
-       *list = g_list_append(spi_list, spi_handle);
-
-       return spi_handle;
-}
-
-static int peripheral_bus_spi_data_free(pb_spi_data_h spi_handle, GList **spi_list)
-{
-       GList *link;
-
-       RETVM_IF(spi_handle == NULL, -1, "handle is null");
-
-       link = g_list_find(*spi_list, spi_handle);
-       if (!link) {
-               _E("handle does not exist in list");
-               return -1;
-       }
-
-       *spi_list = g_list_remove_link(*spi_list, link);
-       if (spi_handle->rx_buf)
-               free(spi_handle->rx_buf);
-       if (spi_handle->tx_buf)
-               free(spi_handle->tx_buf);
-       free(spi_handle);
-       g_list_free(link);
-
-       return 0;
-}
-
-int peripheral_bus_spi_open(int bus, int cs, pb_spi_data_h *spi, gpointer user_data)
+int peripheral_bus_spi_open(int bus, int cs, pb_data_h *handle, gpointer user_data)
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-       pb_spi_data_h spi_handle;
+       pb_data_h spi_handle;
        int ret = PERIPHERAL_ERROR_NONE;
        int fd, bufsiz;
 
-       if (peripheral_bus_spi_data_get(bus, cs, &pb_data->spi_list)) {
+       if (!peripheral_bus_spi_is_available(bus, cs, pb_data->spi_list)) {
                _E("Resource is in use, bus : %d, cs : %d", bus, cs);
                return PERIPHERAL_ERROR_RESOURCE_BUSY;
        }
@@ -99,49 +59,50 @@ int peripheral_bus_spi_open(int bus, int cs, pb_spi_data_h *spi, gpointer user_d
        if ((ret = spi_open(bus, cs, &fd)) < 0)
                goto err_open;
 
-       spi_handle = peripheral_bus_spi_data_new(&pb_data->spi_list);
+       spi_handle = peripheral_bus_data_new(&pb_data->spi_list);
        if (!spi_handle) {
-               _E("peripheral_bus_spi_data_new error");
+               _E("peripheral_bus_data_new error");
                ret = PERIPHERAL_ERROR_OUT_OF_MEMORY;
                goto err_spi_data;
        }
 
-       spi_handle->fd = fd;
-       spi_handle->bus = bus;
-       spi_handle->cs = cs;
+       spi_handle->type = PERIPHERAL_BUS_TYPE_SPI;
        spi_handle->list = &pb_data->spi_list;
+       spi_handle->dev.spi.fd = fd;
+       spi_handle->dev.spi.bus = bus;
+       spi_handle->dev.spi.cs = cs;
 
        if (!spi_get_buffer_size(&bufsiz)) {
                if (initial_buffer_size > bufsiz) initial_buffer_size = bufsiz;
                if (max_buffer_size > bufsiz) max_buffer_size = bufsiz;
        }
 
-       spi_handle->rx_buf = (uint8_t*)calloc(1, initial_buffer_size);
-       if (!spi_handle->rx_buf) {
+       spi_handle->dev.spi.rx_buf = (uint8_t*)calloc(1, initial_buffer_size);
+       if (!spi_handle->dev.spi.rx_buf) {
                _E("Failed to allocate rx buffer");
                ret =  PERIPHERAL_ERROR_OUT_OF_MEMORY;
                goto err_rx_buf;
        }
-       spi_handle->tx_buf = (uint8_t*)calloc(1, initial_buffer_size);
-       if (!spi_handle->rx_buf) {
+       spi_handle->dev.spi.tx_buf = (uint8_t*)calloc(1, initial_buffer_size);
+       if (!spi_handle->dev.spi.rx_buf) {
                _E("Failed to allocate tx buffer");
                ret =  PERIPHERAL_ERROR_OUT_OF_MEMORY;
                goto err_tx_buf;
        }
 
-       spi_handle->rx_buf_size = initial_buffer_size;
-       spi_handle->tx_buf_size = initial_buffer_size;
-       *spi = spi_handle;
+       spi_handle->dev.spi.rx_buf_size = initial_buffer_size;
+       spi_handle->dev.spi.tx_buf_size = initial_buffer_size;
+       *handle = spi_handle;
 
        return PERIPHERAL_ERROR_NONE;
 
 err_tx_buf:
-       if (spi_handle->rx_buf)
-               free(spi_handle->rx_buf);
-       spi_handle->rx_buf_size = 0;
+       if (spi_handle->dev.spi.rx_buf)
+               free(spi_handle->dev.spi.rx_buf);
+       spi_handle->dev.spi.rx_buf_size = 0;
 
 err_rx_buf:
-       peripheral_bus_spi_data_free(spi_handle, &pb_data->spi_list);
+       peripheral_bus_data_free(spi_handle);
 
 err_spi_data:
        spi_close(fd);
@@ -150,40 +111,46 @@ err_open:
        return ret;
 }
 
-int peripheral_bus_spi_close(pb_spi_data_h spi)
+int peripheral_bus_spi_close(pb_data_h handle)
 {
-       int ret;
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+       int ret = PERIPHERAL_ERROR_NONE;
 
-       if ((ret = spi_close(spi->fd)) < 0) {
-               _E("spi_close error (%d)", ret);
+       if ((ret = spi_close(spi->fd)) < 0)
                return ret;
-       }
 
-       if (peripheral_bus_spi_data_free(spi, spi->list) < 0) {
+       if (peripheral_bus_data_free(handle) < 0) {
                _E("Failed to free spi data");
-               return PERIPHERAL_ERROR_UNKNOWN;
+               ret = PERIPHERAL_ERROR_UNKNOWN;
        }
 
-       return PERIPHERAL_ERROR_NONE;
+       return ret;
 }
 
-int peripheral_bus_spi_set_mode(pb_spi_data_h spi, unsigned char mode)
+int peripheral_bus_spi_set_mode(pb_data_h handle, unsigned char mode)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+
        return spi_set_mode(spi->fd, mode);
 }
 
-int peripheral_bus_spi_get_mode(pb_spi_data_h spi, unsigned char *mode)
+int peripheral_bus_spi_get_mode(pb_data_h handle, unsigned char *mode)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+
        return spi_get_mode(spi->fd, mode);
 }
 
-int peripheral_bus_spi_set_lsb_first(pb_spi_data_h spi, gboolean lsb)
+int peripheral_bus_spi_set_lsb_first(pb_data_h handle, gboolean lsb)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+
        return spi_set_lsb_first(spi->fd, (unsigned char)lsb);
 }
 
-int peripheral_bus_spi_get_lsb_first(pb_spi_data_h spi, gboolean *lsb)
+int peripheral_bus_spi_get_lsb_first(pb_data_h handle, gboolean *lsb)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
        int ret;
        unsigned char value;
 
@@ -196,28 +163,37 @@ int peripheral_bus_spi_get_lsb_first(pb_spi_data_h spi, gboolean *lsb)
        return PERIPHERAL_ERROR_NONE;
 }
 
-int peripheral_bus_spi_set_bits(pb_spi_data_h spi, unsigned char bits)
+int peripheral_bus_spi_set_bits(pb_data_h handle, unsigned char bits)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+
        return spi_set_bits(spi->fd, bits);
 }
 
-int peripheral_bus_spi_get_bits(pb_spi_data_h spi, unsigned char *bits)
+int peripheral_bus_spi_get_bits(pb_data_h handle, unsigned char *bits)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+
        return spi_get_bits(spi->fd, bits);
 }
 
-int peripheral_bus_spi_set_frequency(pb_spi_data_h spi, unsigned int freq)
+int peripheral_bus_spi_set_frequency(pb_data_h handle, unsigned int freq)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+
        return spi_set_frequency(spi->fd, freq);
 }
 
-int peripheral_bus_spi_get_frequency(pb_spi_data_h spi, unsigned int *freq)
+int peripheral_bus_spi_get_frequency(pb_data_h handle, unsigned int *freq)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
+
        return spi_get_frequency(spi->fd, freq);
 }
 
-int peripheral_bus_spi_read(pb_spi_data_h spi, GVariant **data_array, int length)
+int peripheral_bus_spi_read(pb_data_h handle, GVariant **data_array, int length)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
        uint8_t err_buf[2] = {0, };
        int ret;
 
@@ -249,8 +225,9 @@ out:
        return ret;
 }
 
-int peripheral_bus_spi_write(pb_spi_data_h spi, GVariant *data_array, int length)
+int peripheral_bus_spi_write(pb_data_h handle, GVariant *data_array, int length)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
        GVariantIter *iter;
        guchar str;
        int i = 0;
@@ -277,8 +254,9 @@ int peripheral_bus_spi_write(pb_spi_data_h spi, GVariant *data_array, int length
        return spi_write(spi->fd, spi->tx_buf, length);
 }
 
-int peripheral_bus_spi_read_write(pb_spi_data_h spi, GVariant *tx_data_array, GVariant **rx_data_array, int length)
+int peripheral_bus_spi_read_write(pb_data_h handle, GVariant *tx_data_array, GVariant **rx_data_array, int length)
 {
+       peripheral_bus_spi_s *spi = &handle->dev.spi;
        uint8_t err_buf[2] = {0, };
        GVariantIter *iter;
        guchar str;
index e1c3999..1140600 100644 (file)
 #ifndef __PERIPHERAL_BUS_SPI_H__
 #define __PERIPHERAL_BUS_SPI_H__
 
-int peripheral_bus_spi_open(int bus, int cs, pb_spi_data_h *spi, gpointer user_data);
-int peripheral_bus_spi_close(pb_spi_data_h spi);
-int peripheral_bus_spi_set_mode(pb_spi_data_h spi, unsigned char mode);
-int peripheral_bus_spi_get_mode(pb_spi_data_h spi, unsigned char *mode);
-int peripheral_bus_spi_set_lsb_first(pb_spi_data_h spi, gboolean lsb);
-int peripheral_bus_spi_get_lsb_first(pb_spi_data_h spi, gboolean *lsb);
-int peripheral_bus_spi_set_bits(pb_spi_data_h spi, unsigned char bits);
-int peripheral_bus_spi_get_bits(pb_spi_data_h spi, unsigned char *bits);
-int peripheral_bus_spi_set_frequency(pb_spi_data_h spi, unsigned int freq);
-int peripheral_bus_spi_get_frequency(pb_spi_data_h spi, unsigned int *freq);
-int peripheral_bus_spi_read(pb_spi_data_h spi, GVariant **data_array, int length);
-int peripheral_bus_spi_write(pb_spi_data_h spi, GVariant *data_array, int length);
-int peripheral_bus_spi_read_write(pb_spi_data_h spi, GVariant *tx_data_array, GVariant **rx_data_array, int length);
+int peripheral_bus_spi_open(int bus, int cs, pb_data_h *handle, gpointer user_data);
+int peripheral_bus_spi_close(pb_data_h handle);
+int peripheral_bus_spi_set_mode(pb_data_h handle, unsigned char mode);
+int peripheral_bus_spi_get_mode(pb_data_h handle, unsigned char *mode);
+int peripheral_bus_spi_set_lsb_first(pb_data_h handle, gboolean lsb);
+int peripheral_bus_spi_get_lsb_first(pb_data_h handle, gboolean *lsb);
+int peripheral_bus_spi_set_bits(pb_data_h handle, unsigned char bits);
+int peripheral_bus_spi_get_bits(pb_data_h handle, unsigned char *bits);
+int peripheral_bus_spi_set_frequency(pb_data_h handle, unsigned int freq);
+int peripheral_bus_spi_get_frequency(pb_data_h handle, unsigned int *freq);
+int peripheral_bus_spi_read(pb_data_h handle, GVariant **data_array, int length);
+int peripheral_bus_spi_write(pb_data_h handle, GVariant *data_array, int length);
+int peripheral_bus_spi_read_write(pb_data_h handle, GVariant *tx_data_array, GVariant **rx_data_array, int length);
 
 #endif /* __PERIPHERAL_BUS_SPI_H__ */
index 549c1b4..f48fbdf 100644 (file)
 #define INITIAL_BUFFER_SIZE 128
 #define MAX_BUFFER_SIZE 8192
 
-static pb_uart_data_h peripheral_bus_uart_data_get(int port, GList **list)
+static bool peripheral_bus_uart_is_available(int port, GList *list)
 {
-       GList *uart_list = *list;
        GList *link;
-       pb_uart_data_h uart_handle;
+       pb_data_h handle;
 
-       link = uart_list;
+       link = list;
        while (link) {
-               uart_handle = (pb_uart_data_h)link->data;
-               if (uart_handle->port == port)
-                       return uart_handle;
+               handle = (pb_data_h)link->data;
+               if (handle->dev.uart.port == port)
+                       return false;
                link = g_list_next(link);
        }
 
-       return NULL;
+       return true;
 }
 
-static pb_uart_data_h peripheral_bus_uart_data_new(GList **list)
-{
-       GList *uart_list = *list;
-       pb_uart_data_h uart_handle;
-
-       uart_handle = (pb_uart_data_h)calloc(1, sizeof(peripheral_bus_uart_data_s));
-       if (uart_handle == NULL) {
-               _E("failed to allocate peripheral_bus_uart_data_s");
-               return NULL;
-       }
-
-       *list = g_list_append(uart_list, uart_handle);
-
-       return uart_handle;
-}
-
-static int peripheral_bus_uart_data_free(pb_uart_data_h uart_handle, GList **uart_list)
-{
-       GList *link;
-
-       RETVM_IF(uart_handle == NULL, -1, "handle is null");
-
-       link = g_list_find(*uart_list, uart_handle);
-       if (!link) {
-               _E("handle does not exist in list");
-               return -1;
-       }
-
-       *uart_list = g_list_remove_link(*uart_list, link);
-       if (uart_handle->buffer)
-               free(uart_handle->buffer);
-       free(uart_handle);
-       g_list_free(link);
-
-       return 0;
-}
-
-int peripheral_bus_uart_open(int port, pb_uart_data_h *uart, gpointer user_data)
+int peripheral_bus_uart_open(int port, pb_data_h *handle, gpointer user_data)
 {
        peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-       pb_uart_data_h uart_handle;
+       pb_data_h uart_handle;
        int ret = PERIPHERAL_ERROR_NONE;
        int fd;
 
-       if (peripheral_bus_uart_data_get(port, &pb_data->uart_list)) {
+       if (!peripheral_bus_uart_is_available(port, pb_data->uart_list)) {
                _E("Resource is in use, port : %d", port);
                return PERIPHERAL_ERROR_RESOURCE_BUSY;
        }
@@ -98,67 +60,81 @@ int peripheral_bus_uart_open(int port, pb_uart_data_h *uart, gpointer user_data)
        if ((ret = uart_open(port, &fd)) < 0)
                return ret;
 
-       uart_handle = peripheral_bus_uart_data_new(&pb_data->uart_list);
+       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;
        }
 
-       uart_handle->fd = fd;
-       uart_handle->port = port;
+       uart_handle->type = PERIPHERAL_BUS_TYPE_UART;
        uart_handle->list = &pb_data->uart_list;
+       uart_handle->dev.uart.fd = fd;
+       uart_handle->dev.uart.port = port;
 
-       uart_handle->buffer = (uint8_t*)calloc(1, INITIAL_BUFFER_SIZE);
-       if (!uart_handle->buffer) {
+       uart_handle->dev.uart.buffer = (uint8_t*)calloc(1, INITIAL_BUFFER_SIZE);
+       if (!uart_handle->dev.uart.buffer) {
                _E("Failed to allocate buffer");
-               peripheral_bus_uart_data_free(uart_handle, &pb_data->uart_list);
+               peripheral_bus_data_free(uart_handle);
                uart_close(fd);
                return  PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
 
-       uart_handle->buffer_size = INITIAL_BUFFER_SIZE;
-       *uart = uart_handle;
+       uart_handle->dev.uart.buffer_size = INITIAL_BUFFER_SIZE;
+       *handle = uart_handle;
 
        return ret;
 }
 
-int peripheral_bus_uart_close(pb_uart_data_h uart)
+int peripheral_bus_uart_close(pb_data_h handle)
 {
-       int ret;
+       peripheral_bus_uart_s *uart = &handle->dev.uart;
+       int ret = PERIPHERAL_ERROR_NONE;
 
-       _D("uart_close, port : %d, id = %s", uart->port, uart->client_info.id);
+       _D("uart_close, port : %d, id = %s", uart->port, handle->client_info.id);
 
        if ((ret = uart_close(uart->fd)) < 0)
                return ret;
 
-       if (peripheral_bus_uart_data_free(uart, uart->list) < 0)
+       if (peripheral_bus_data_free(handle) < 0) {
                _E("Failed to free uart data");
+               ret = PERIPHERAL_ERROR_UNKNOWN;
+       }
 
        return ret;
 }
 
-int peripheral_bus_uart_flush(pb_uart_data_h uart)
+int peripheral_bus_uart_flush(pb_data_h handle)
 {
+       peripheral_bus_uart_s *uart = &handle->dev.uart;
+
        return  uart_flush(uart->fd);
 }
 
-int peripheral_bus_uart_set_baudrate(pb_uart_data_h uart, int baudrate)
+int peripheral_bus_uart_set_baudrate(pb_data_h handle, int baudrate)
 {
+       peripheral_bus_uart_s *uart = &handle->dev.uart;
+
        return  uart_set_baudrate(uart->fd, baudrate);
 }
 
-int peripheral_bus_uart_set_mode(pb_uart_data_h uart, int byte_size, int parity, int stop_bits)
+int peripheral_bus_uart_set_mode(pb_data_h handle, int byte_size, int parity, int stop_bits)
 {
+       peripheral_bus_uart_s *uart = &handle->dev.uart;
+
        return  uart_set_mode(uart->fd, byte_size, parity, stop_bits);
 }
 
-int peripheral_bus_uart_set_flowcontrol(pb_uart_data_h uart, bool xonxoff, bool rtscts)
+int peripheral_bus_uart_set_flowcontrol(pb_data_h handle, bool xonxoff, bool rtscts)
 {
+       peripheral_bus_uart_s *uart = &handle->dev.uart;
+
        return  uart_set_flowcontrol(uart->fd, xonxoff, rtscts);
 }
 
-int peripheral_bus_uart_read(pb_uart_data_h uart, GVariant **data_array, int length)
+int peripheral_bus_uart_read(pb_data_h handle, GVariant **data_array, int length)
 {
+       peripheral_bus_uart_s *uart = &handle->dev.uart;
        int ret;
 
        /* Limit maximum length */
@@ -182,8 +158,9 @@ int peripheral_bus_uart_read(pb_uart_data_h uart, GVariant **data_array, int len
        return ret;
 }
 
-int peripheral_bus_uart_write(pb_uart_data_h uart, GVariant *data_array, int length)
+int peripheral_bus_uart_write(pb_data_h handle, GVariant *data_array, int length)
 {
+       peripheral_bus_uart_s *uart = &handle->dev.uart;
        GVariantIter *iter;
        guchar str;
        int i = 0;
index 0f5e7a2..680d72a 100644 (file)
 #ifndef __PERIPHERAL_BUS_UART_H__
 #define __PERIPHERAL_BUS_UART_H__
 
-int peripheral_bus_uart_open(int port, pb_uart_data_h *uart, gpointer user_data);
-int peripheral_bus_uart_close(pb_uart_data_h uart);
-int peripheral_bus_uart_flush(pb_uart_data_h uart);
-int peripheral_bus_uart_set_baudrate(pb_uart_data_h uart, int baudrate);
-int peripheral_bus_uart_set_mode(pb_uart_data_h uart, int byte_size, int parity, int stop_bits);
-int peripheral_bus_uart_set_flowcontrol(pb_uart_data_h uart, int xonxoff, int rtscts);
-int peripheral_bus_uart_read(pb_uart_data_h uart, GVariant **data_array, int length);
-int peripheral_bus_uart_write(pb_uart_data_h uart, GVariant *data_array, int length);
+int peripheral_bus_uart_open(int port, pb_data_h *handle, gpointer user_data);
+int peripheral_bus_uart_close(pb_data_h handle);
+int peripheral_bus_uart_flush(pb_data_h handle);
+int peripheral_bus_uart_set_baudrate(pb_data_h handle, int baudrate);
+int peripheral_bus_uart_set_mode(pb_data_h handle, int byte_size, int parity, int stop_bits);
+int peripheral_bus_uart_set_flowcontrol(pb_data_h handle, int xonxoff, int rtscts);
+int peripheral_bus_uart_read(pb_data_h handle, GVariant **data_array, int length);
+int peripheral_bus_uart_write(pb_data_h handle, GVariant *data_array, int length);
 
 #endif /* __PERIPHERAL_BUS_UART_H__ */
index 921a64c..92cb445 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include <gio/gio.h>
 #include "peripheral_bus.h"
 #include "peripheral_common.h"
 
-int peripheral_bus_get_client_info(GDBusMethodInvocation *invocation, peripheral_bus_s *pb_data, pb_client_info_s *client_info)
-{
-       guint pid = 0;
-       GError *error = NULL;
-       GVariant *_ret;
-       const gchar *id;
-
-       id = g_dbus_method_invocation_get_sender(invocation);
-       _ret = g_dbus_connection_call_sync(pb_data->connection,
-               "org.freedesktop.DBus",
-               "/org/freedesktop/DBus",
-               "org.freedesktop.DBus",
-               "GetConnectionUnixProcessID",
-               g_variant_new("(s)", id),
-               NULL,
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               &error);
-
-       if (_ret == NULL) {
-               _E("Failed to get client pid, %s", error->message);
-               g_error_free(error);
-
-               return -1;
-       }
-
-       g_variant_get(_ret, "(u)", &pid);
-       g_variant_unref(_ret);
-
-       client_info->pid = (pid_t)pid;
-       client_info->pgid = getpgid(pid);
-       client_info->id = strdup(id);
-
-       return 0;
-}
-
 GVariant *peripheral_bus_build_variant_ay(uint8_t *data, int length)
 {
        GVariantBuilder *builder;
@@ -77,3 +41,59 @@ GVariant *peripheral_bus_build_variant_ay(uint8_t *data, int length)
 
        return variant;
 }
+
+pb_data_h peripheral_bus_data_new(GList **plist)
+{
+       GList *list = *plist;
+       pb_data_h handle;
+
+       handle = (pb_data_h)calloc(1, sizeof(peripheral_bus_data_s));
+       if (handle == NULL) {
+               _E("failed to allocate peripheral_bus_data_s");
+               return NULL;
+       }
+
+       *plist = g_list_append(list, handle);
+
+       return handle;
+}
+
+int peripheral_bus_data_free(pb_data_h handle)
+{
+       GList *list = *handle->list;
+       GList *link;
+
+       RETVM_IF(handle == NULL, -1, "handle is null");
+
+       link = g_list_find(list, handle);
+       if (!link) {
+               _E("handle does not exist in list");
+               return -1;
+       }
+
+       *handle->list = g_list_remove_link(list, link);
+
+       switch (handle->type) {
+       case PERIPHERAL_BUS_TYPE_I2C:
+               if (handle->dev.i2c.buffer)
+                       free(handle->dev.i2c.buffer);
+               break;
+       case PERIPHERAL_BUS_TYPE_UART:
+               if (handle->dev.uart.buffer)
+                       free(handle->dev.uart.buffer);
+               break;
+       case PERIPHERAL_BUS_TYPE_SPI:
+               if (handle->dev.spi.rx_buf)
+                       free(handle->dev.spi.rx_buf);
+               if (handle->dev.spi.tx_buf)
+                       free(handle->dev.spi.tx_buf);
+               break;
+       default:
+               break;
+       }
+
+       free(handle);
+       g_list_free(link);
+
+       return 0;
+}
index 0aa49be..103956b 100644 (file)
@@ -17,7 +17,8 @@
 #ifndef __PERIPHERAL_UTIL_H__
 #define __PERIPHERAL_UTIL_H__
 
-int peripheral_bus_get_client_info(GDBusMethodInvocation *invocation, peripheral_bus_s *pb_data, pb_client_info_s *client_info);
 GVariant *peripheral_bus_build_variant_ay(uint8_t *data, int length);
+pb_data_h peripheral_bus_data_new(GList **plist);
+int peripheral_bus_data_free(pb_data_h handle);
 
 #endif /* __PERIPHERAL_UTIL_H__ */