Refactoring exception handling routines to remove duplication operations 61/130761/3
authorSungguk Na <sungguk.na@samsung.com>
Wed, 24 May 2017 01:18:20 +0000 (10:18 +0900)
committerSungguk Na <sungguk.na@samsung.com>
Wed, 24 May 2017 04:46:31 +0000 (13:46 +0900)
Change-Id: Ib62e79d947b01acd0c24eca9001d85009be9cbc3
Signed-off-by: Sungguk Na <sungguk.na@samsung.com>
src/daemon/peripheral_bus.c
src/daemon/peripheral_bus.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_uart.c
src/daemon/peripheral_bus_uart.h
src/interface/i2c.c

index a416231..21c75b0 100644 (file)
 #include "peripheral_common.h"
 #include "peripheral_bus_util.h"
 
-static void __on_name_appeared(GDBusConnection *connection,
+static void __i2c_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
-               const gchar     *name_owner,
                gpointer         user_data)
 {
-       _D("appid [%s] appears ", name);
+       pb_i2c_data_h i2c_handle = (pb_i2c_data_h)user_data;
+       _D("appid [%s] vanished ", name);
+
+       g_bus_unwatch_name(i2c_handle->watch_id);
+       peripheral_bus_i2c_close(i2c_handle);
 }
 
-static void __on_name_vanished(GDBusConnection *connection,
+static void __pwm_on_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
 {
-       GList *link;
-       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
-
+       pb_pwm_data_h pwm_handle = (pb_pwm_data_h)user_data;
        _D("appid [%s] vanished ", name);
-       link = pb_data->i2c_list;
-
-       if (link != NULL) {
-               pb_i2c_data_h i2c_data;
-               while (link) {
-                       i2c_data = (pb_i2c_data_h) link->data;
-                       if (strcmp(i2c_data->client_info.id, name) == 0) {
-                               g_bus_unwatch_name(i2c_data->watch_id);
-                               peripheral_bus_i2c_close(i2c_data, user_data);
-                       }
-                       link = g_list_next(link);
-               }
-       }
 
-       link = pb_data->pwm_list;
-
-       if (link != NULL) {
-               pb_pwm_data_h pwm_data;
-               while (link) {
-                       pwm_data = (pb_pwm_data_h) link->data;
-                       if (strcmp(pwm_data->client_info.id, name) == 0) {
-                               g_bus_unwatch_name(pwm_data->watch_id);
-                               peripheral_bus_pwm_close(pwm_data, user_data);
-                       }
-                       link = g_list_next(link);
-               }
-       }
+       g_bus_unwatch_name(pwm_handle->watch_id);
+       peripheral_bus_pwm_close(pwm_handle);
+}
 
-       link = pb_data->uart_list;
-
-       if (link != NULL) {
-               pb_uart_data_h uart_data;
-               while (link) {
-                       uart_data = (pb_uart_data_h) link->data;
-                       if (strcmp(uart_data->client_info.id, name) == 0) {
-                               g_bus_unwatch_name(uart_data->watch_id);
-                               peripheral_bus_uart_close(uart_data, user_data);
-                       }
-                       link = g_list_next(link);
-               }
-       }
+
+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;
+       _D("appid [%s] vanished ", name);
+
+       g_bus_unwatch_name(uart_handle->watch_id);
+       peripheral_bus_uart_close(uart_handle);
 }
 
 gboolean handle_gpio_open(
@@ -261,9 +236,9 @@ gboolean handle_i2c_open(
        i2c_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM ,
                        i2c_handle->client_info.id,
                        G_BUS_NAME_WATCHER_FLAGS_NONE ,
-                       __on_name_appeared,
-                       __on_name_vanished,
-                       user_data,
+                       NULL,
+                       __i2c_on_name_vanished,
+                       i2c_handle,
                        NULL);
 
        peripheral_io_gdbus_i2c_complete_open(i2c, invocation, GPOINTER_TO_UINT(i2c_handle), ret);
@@ -295,7 +270,7 @@ gboolean handle_i2c_close(
        }
 
        g_bus_unwatch_name(i2c_handle->watch_id);
-       ret = peripheral_bus_i2c_close(i2c_handle, user_data);
+       ret = peripheral_bus_i2c_close(i2c_handle);
 
 out:
        peripheral_io_gdbus_i2c_complete_close(i2c, invocation, ret);
@@ -395,9 +370,9 @@ gboolean handle_pwm_open(
        pwm_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM ,
                        pwm_handle->client_info.id,
                        G_BUS_NAME_WATCHER_FLAGS_NONE ,
-                       __on_name_appeared,
-                       __on_name_vanished,
-                       user_data,
+                       NULL,
+                       __pwm_on_name_vanished,
+                       pwm_handle,
                        NULL);
 
        peripheral_io_gdbus_pwm_complete_open(pwm, invocation, GPOINTER_TO_UINT(pwm_handle), ret);
@@ -426,7 +401,7 @@ gboolean handle_pwm_close(
                        ret = PERIPHERAL_ERROR_INVALID_OPERATION;
                } else {
                        g_bus_unwatch_name(pwm_handle->watch_id);
-                       ret = peripheral_bus_pwm_close(pwm_handle, user_data);
+                       ret = peripheral_bus_pwm_close(pwm_handle);
                }
        }
 
@@ -678,45 +653,21 @@ gboolean handle_uart_open(
        pb_uart_data_h uart_handle;
 
        ret = peripheral_bus_uart_open(port, &uart_handle, user_data);
+
        if (ret == PERIPHERAL_ERROR_NONE) {
-               guint pid = 0;
-               GError *error = NULL;
-               GVariant *_ret;
-               const gchar *id;
+               if (peripheral_bus_get_client_info(invocation, pb_data, &uart_handle->client_info) == 0)
+                       _D("port : %d, id = %s", port, uart_handle->client_info.id);
+               else
+                       ret = PERIPHERAL_ERROR_UNKNOWN;
+       }
 
-               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,
+       uart_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM ,
+                       uart_handle->client_info.id,
+                       G_BUS_NAME_WATCHER_FLAGS_NONE ,
                        NULL,
-                       &error);
-
-               if (_ret != NULL) {
-                       g_variant_get(_ret, "(u)", &pid);
-                       g_variant_unref(_ret);
-               } else
-                       g_error_free(error);
-
-               uart_handle->client_info.pid = (pid_t)pid;
-               uart_handle->client_info.pgid = getpgid(pid);
-               uart_handle->client_info.id = strdup(id);
-
-               uart_handle->watch_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM ,
-                               uart_handle->client_info.id,
-                               G_BUS_NAME_WATCHER_FLAGS_NONE ,
-                               __on_name_appeared,
-                               __on_name_vanished,
-                               user_data,
-                               NULL);
-
-               _D("port : %d, id = %s", port, uart_handle->client_info.id);
-       }
+                       __uart_on_name_vanished,
+                       uart_handle,
+                       NULL);
 
        peripheral_io_gdbus_uart_complete_open(uart, invocation, GPOINTER_TO_UINT(uart_handle), ret);
 
@@ -744,7 +695,7 @@ gboolean handle_uart_close(
                        ret = PERIPHERAL_ERROR_INVALID_OPERATION;
                } else {
                        g_bus_unwatch_name(uart_handle->watch_id);
-                       ret = peripheral_bus_uart_close(uart_handle, user_data);
+                       ret = peripheral_bus_uart_close(uart_handle);
                }
        }
 
index db35f15..9c14df0 100644 (file)
@@ -66,6 +66,7 @@ typedef struct {
        uint8_t *buffer;
        int buffer_size;
        uint watch_id;
+       GList **list;
        /* client info */
        pb_client_info_s client_info;
 } peripheral_bus_i2c_data_s;
@@ -74,6 +75,7 @@ typedef struct {
        int device;
        int channel;
        uint watch_id;
+       GList **list;
        /* client info */
        pb_client_info_s client_info;
 } peripheral_bus_pwm_data_s;
@@ -84,6 +86,7 @@ typedef struct {
        uint8_t *buffer;
        int buffer_size;
        uint watch_id;
+       GList **list;
        /* client info */
        pb_client_info_s client_info;
 } peripheral_bus_uart_data_s;
index eb17dd6..9c0146c 100644 (file)
@@ -116,6 +116,7 @@ int peripheral_bus_i2c_open(int bus, int address, pb_i2c_data_h *i2c, gpointer u
        i2c_handle->fd = fd;
        i2c_handle->bus = bus;
        i2c_handle->address = address;
+       i2c_handle->list = &pb_data->i2c_list;
        i2c_handle->buffer = malloc(INITIAL_BUFFER_SIZE);
 
        if (!(i2c_handle->buffer)) {
@@ -134,9 +135,8 @@ 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, gpointer user_data)
+int peripheral_bus_i2c_close(pb_i2c_data_h i2c)
 {
-       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        int ret;
 
        _D("bus : %d, address : %d, pgid = %d", i2c->bus, i2c->address, i2c->client_info.pgid);
@@ -144,7 +144,7 @@ int peripheral_bus_i2c_close(pb_i2c_data_h i2c, gpointer user_data)
        if ((ret = i2c_close(i2c->fd)) < 0)
                return ret;
 
-       if (peripheral_bus_i2c_data_free(i2c, &pb_data->i2c_list) < 0)
+       if (peripheral_bus_i2c_data_free(i2c, i2c->list) < 0)
                _E("Failed to free i2c data");
 
        return ret;
index 350a53a..d65705c 100644 (file)
@@ -18,7 +18,7 @@
 #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, 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);
 
index 0fff255..35d5e9f 100644 (file)
@@ -101,6 +101,7 @@ int peripheral_bus_pwm_open(int device, int channel, pb_pwm_data_h *pwm, gpointe
                goto err;
        }
 
+       pwm_handle->list = &pb_data->pwm_list;
        pwm_handle->device = device;
        pwm_handle->channel = channel;
        *pwm = pwm_handle;
@@ -114,9 +115,8 @@ open_err:
        return ret;
 }
 
-int peripheral_bus_pwm_close(pb_pwm_data_h pwm, gpointer user_data)
+int peripheral_bus_pwm_close(pb_pwm_data_h pwm)
 {
-       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        int ret;
 
        if ((ret = pwm_close(pwm->device, pwm->channel)) < 0) {
@@ -124,7 +124,7 @@ int peripheral_bus_pwm_close(pb_pwm_data_h pwm, gpointer user_data)
                return ret;
        }
 
-       peripheral_bus_pwm_data_free(pwm, &pb_data->pwm_list);
+       peripheral_bus_pwm_data_free(pwm, pwm->list);
 
        return PERIPHERAL_ERROR_NONE;
 }
index a98e3ea..915f6fa 100644 (file)
@@ -18,7 +18,7 @@
 #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, 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);
index cd81e6b..c555e45 100644 (file)
@@ -109,6 +109,7 @@ int peripheral_bus_uart_open(int port, pb_uart_data_h *uart, gpointer user_data)
 
        uart_handle->fd = fd;
        uart_handle->port = port;
+       uart_handle->list = &pb_data->uart_list;
 
        uart_handle->buffer = (uint8_t*)calloc(1, INITIAL_BUFFER_SIZE);
        if (!uart_handle->buffer) {
@@ -124,9 +125,8 @@ int peripheral_bus_uart_open(int port, pb_uart_data_h *uart, gpointer user_data)
        return ret;
 }
 
-int peripheral_bus_uart_close(pb_uart_data_h uart, gpointer user_data)
+int peripheral_bus_uart_close(pb_uart_data_h uart)
 {
-       peripheral_bus_s *pb_data = (peripheral_bus_s*)user_data;
        int ret;
 
        _D("uart_close, port : %d, id = %s", uart->port, uart->client_info.id);
@@ -134,7 +134,7 @@ int peripheral_bus_uart_close(pb_uart_data_h uart, gpointer user_data)
        if ((ret = uart_close(uart->fd)) < 0)
                return ret;
 
-       if (peripheral_bus_uart_data_free(uart, &pb_data->uart_list) < 0)
+       if (peripheral_bus_uart_data_free(uart, uart->list) < 0)
                _E("Failed to free uart data");
 
        return ret;
index fa710ae..0f5e7a2 100644 (file)
@@ -18,7 +18,7 @@
 #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, 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);
index ae5133b..956f358 100644 (file)
@@ -110,7 +110,7 @@ int i2c_write(int fd, const unsigned char *data, int length)
        if (status != length) {
                char errmsg[MAX_ERR_LEN];
                strerror_r(errno, errmsg, MAX_ERR_LEN);
-               _E("i2c write failed : %s\n", fd, errmsg);
+               _E("i2c write failed : %s\n", errmsg);
                return -EIO;
        }