[Adapt: HAL] Remove unneeded rfcomm client data 35/87735/1
authorAtul Rai <a.rai@samsung.com>
Thu, 11 Aug 2016 10:48:54 +0000 (16:18 +0530)
committerAtul Rai <a.rai@samsung.com>
Wed, 31 Aug 2016 04:25:35 +0000 (09:55 +0530)
This patch removes unnecessary data/functions maintained for
rfcomm cilent context.

Change-Id: I659d4545207f8529b2289bdb3c6ce9e44318d4da
Signed-off-by: Atul Rai <a.rai@samsung.com>
bt-oal/bluez_hal/src/bt-hal-rfcomm-dbus-handler.c

index c486175..8d94ec2 100644 (file)
 #define BT_HAL_RFCOMM_MAX_BUFFER_SIZE 1024
 
 typedef struct {
-       char uuid[BT_HAL_UUID_STRING_LEN];
-       char *device_path;
-       char *obj_path;
-       int object_id;
-       int id;
-       GSList *rfcomm_conns;
-} rfcomm_cb_data_t;
-
-typedef struct {
        char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
        int hal_fd;
        unsigned int hal_watch;
@@ -56,6 +47,15 @@ typedef struct {
        unsigned int bt_watch;
 } rfcomm_conn_info_t;
 
+typedef struct {
+       char uuid[BT_HAL_UUID_STRING_LEN];
+       char *device_path;
+       char *obj_path;
+       int object_id;
+       int id;
+       rfcomm_conn_info_t *conn_info;
+} rfcomm_cb_data_t;
+
 static GSList *rfcomm_clients;
 static int latest_id = -1;
 static gboolean id_used[BT_HAL_RFCOMM_ID_MAX];
@@ -103,33 +103,46 @@ void __rfcomm_delete_id(int id)
        DBG("id: %d, latest_id: %d", id, latest_id);
 }
 
-static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
+static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
 {
        GSList *l;
 
        for (l = rfcomm_clients; l != NULL; l = l->next) {
                rfcomm_cb_data_t *info = l->data;
 
-               if (g_strcmp0(info->uuid, uuid) == 0)
-                       return info;
+               if (info != NULL)
+                       if (g_strcmp0(info->obj_path, path) == 0)
+                               return info;
        }
 
        return NULL;
 }
 
-static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
+static void __bt_free_conn(rfcomm_conn_info_t *conn)
 {
-       GSList *l;
+       DBG("+");
 
-       for (l = rfcomm_clients; l != NULL; l = l->next) {
-               rfcomm_cb_data_t *info = l->data;
+       if (conn == NULL)
+               return;
 
-               if (info != NULL)
-                       if (g_strcmp0(info->obj_path, path) == 0)
-                               return info;
+       if (0 < conn->hal_fd)
+               close(conn->hal_fd);
+
+       if (conn->hal_watch > 0) {
+               g_source_remove(conn->hal_watch);
+               conn->hal_watch = 0;
        }
 
-       return NULL;
+       if (0 < conn->stack_fd)
+               close(conn->stack_fd);
+
+       if (conn->bt_watch > 0) {
+               g_source_remove(conn->bt_watch);
+               conn->bt_watch = 0;
+       }
+
+       g_free(conn);
+       DBG("-");
 }
 
 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
@@ -162,95 +175,9 @@ static void __rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
                return;
        }
 
-       if (info->rfcomm_conns == NULL) {
-               INFO("No more device connected remove info");
-               rfcomm_clients = g_slist_remove(rfcomm_clients, info);
-               __bt_free_cb_data(info);
-       }
-}
-
-static rfcomm_conn_info_t *__find_conn_info_with_stack_fd(rfcomm_cb_data_t *info, int fd)
-{
-       GSList *l;
-
-       for (l = info->rfcomm_conns; l != NULL; l = l->next) {
-               rfcomm_conn_info_t *conn_info = l->data;
-
-               if (conn_info && (conn_info->stack_fd == fd))
-                       return conn_info;
-       }
-
-       return NULL;
-}
-
-static rfcomm_conn_info_t *__find_conn_info_with_hal_fd(rfcomm_cb_data_t *info, int fd)
-{
-       GSList *l;
-       for (l = info->rfcomm_conns; l != NULL; l = l->next) {
-               rfcomm_conn_info_t *conn_info = l->data;
-
-               if (conn_info && (conn_info->hal_fd == fd))
-                       return conn_info;
-       }
-
-       return NULL;
-}
-
-static gint compare(gpointer *a, gpointer *b)
-{
-       rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
-       char *address = (char *)b;
-       return g_strcmp0(node->remote_addr, address);
-}
-
-static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
-               char *dev_address)
-{
-       GSList *l = NULL;
-       rfcomm_conn_info_t *conn_info = NULL;
-       l = g_slist_find_custom(info->rfcomm_conns, dev_address,
-                       (GCompareFunc)compare);
-       if (l)
-               conn_info = l->data;
-       return conn_info;
-}
-
-static void __bt_free_conn(rfcomm_conn_info_t *conn)
-{
-       DBG("+");
-
-       if (conn == NULL)
-               return;
-
-       if (0 < conn->hal_fd)
-               close(conn->hal_fd);
-
-       if (conn->hal_watch > 0) {
-               g_source_remove(conn->hal_watch);
-               conn->hal_watch = 0;
-       }
-
-       if (0 < conn->stack_fd)
-               close(conn->stack_fd);
-
-       if (conn->bt_watch > 0) {
-               g_source_remove(conn->bt_watch);
-               conn->bt_watch = 0;
-       }
-
-       g_free(conn);
-       DBG("-");
-}
-
-static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
-{
-       rfcomm_conn_info_t *conn_info;
-
-       conn_info = __get_conn_info_from_address(info, address);
-       if (conn_info) {
-               info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
-               __bt_free_conn(conn_info);
-       }
+       rfcomm_clients = g_slist_remove(rfcomm_clients, info);
+       __bt_free_conn(info->conn_info);
+       __bt_free_cb_data(info);
 }
 
 static int write_all(int fd, unsigned char *buf, int len)
@@ -288,7 +215,7 @@ static gboolean app_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
 
        DBG("+");
        fd = g_io_channel_unix_get_fd(io);
-       conn_info = __find_conn_info_with_hal_fd(info, fd);
+       conn_info = info->conn_info;
 
        if (cond & G_IO_HUP) {
                ERR("Socket %d hang up", fd);
@@ -332,7 +259,6 @@ static gboolean app_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
        DBG("-");
        return TRUE;
 fail:
-       __rfcomm_remove_conn_info_t(info, conn_info->remote_addr);
        __rfcomm_cb_data_remove(info);
        return FALSE;
 }
@@ -350,7 +276,7 @@ static gboolean stack_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
        DBG("+");
 
        fd = g_io_channel_unix_get_fd(io);
-       conn_info = __find_conn_info_with_stack_fd(info, fd);
+       conn_info = info->conn_info;
 
        if (cond & G_IO_HUP) {
                ERR("Socket %d hang up", fd);
@@ -394,7 +320,6 @@ static gboolean stack_event_cb(GIOChannel *io, GIOCondition cond, gpointer data)
        DBG("-");
        return TRUE;
 fail:
-       __rfcomm_remove_conn_info_t(info, conn_info->remote_addr);
        __rfcomm_cb_data_remove(info);
        return FALSE;
 }
@@ -417,14 +342,14 @@ static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
                return -1;
        }
 
-       _bt_convert_addr_type_to_string(address, addr->address);
        info = __find_rfcomm_info_from_path(path);
        if (info == NULL)
                return -1;
 
-       conn_info = __get_conn_info_from_address(info, address);
+       conn_info = info->conn_info;
+       _bt_convert_addr_type_to_string(address, addr->address);
        if (conn_info == NULL) {
-               ERR("Device Address %s not found in connection list", address);
+               ERR("conn_info is NULL for dev:[%s]", address);
                return -1;
        }
 
@@ -468,7 +393,6 @@ static int __new_connection(const char *path, int fd, bt_bdaddr_t *addr)
 
        return 0;
 fail:
-       __rfcomm_remove_conn_info_t(info, address);
        __rfcomm_cb_data_remove(info);
        return -1;
 }
@@ -478,8 +402,6 @@ static void __bt_connect_response_cb(GDBusProxy *proxy,
 {
        GError *error = NULL;
        rfcomm_cb_data_t *cb_data;
-       char dev_address[BT_HAL_ADDRESS_STRING_SIZE];
-       const char *path;
 
        DBG("+");
 
@@ -491,9 +413,6 @@ static void __bt_connect_response_cb(GDBusProxy *proxy,
 
        if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
                ERR("Error : %s \n", error->message);
-               path = g_dbus_proxy_get_object_path(proxy);
-               _bt_convert_device_path_to_address(path, dev_address);
-               __rfcomm_remove_conn_info_t(cb_data, dev_address);
                __rfcomm_cb_data_remove(cb_data);
                g_error_free(error);
        }
@@ -531,7 +450,6 @@ static void __bt_discover_service_response_cb(GDBusProxy *proxy,
                g_object_unref(proxy);
        if (err != NULL) {
                ERR("Error occured in Proxy call [%s]\n", err->message);
-               __rfcomm_remove_conn_info_t(cb_data, dev_address);
                __rfcomm_cb_data_remove(cb_data);
                goto done;
        } else {
@@ -550,13 +468,11 @@ static void __bt_discover_service_response_cb(GDBusProxy *proxy,
                                        __bt_connect_response_cb, cb_data);
                        if (ret != BT_STATUS_SUCCESS) {
                                ERR("ConnectProfile failed");
-                               __rfcomm_remove_conn_info_t(cb_data, dev_address);
                                __rfcomm_cb_data_remove(cb_data);
                                goto done;
                        }
                } else {
                        ERR("remote uuid not found");
-                       __rfcomm_remove_conn_info_t(cb_data, dev_address);
                        __rfcomm_cb_data_remove(cb_data);
                }
        }
@@ -574,29 +490,26 @@ static rfcomm_cb_data_t *__get_rfcomm_cb_data(char *remote_uuid)
 
        DBG("+");
 
-       cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
-       if (!cb_data) {
-               id = __rfcomm_assign_id();
-               if (id < 0) {
-                       ERR("__rfcomm_assign_id failed");
-                       return NULL;
-               }
-
-               path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
-               object_id = _bt_register_new_gdbus_object(path, __new_connection);
-               if (object_id < 0) {
-                       ERR("_bt_register_new_gdbus_object failed");
-                       __rfcomm_delete_id(id);
-                       return NULL;
-               }
+       id = __rfcomm_assign_id();
+       if (id < 0) {
+               ERR("__rfcomm_assign_id failed");
+               return NULL;
+       }
 
-               cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
-               g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
-               cb_data->obj_path = path;
-               cb_data->object_id = object_id;
-               cb_data->id = id;
+       path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
+       object_id = _bt_register_new_gdbus_object(path, __new_connection);
+       if (object_id < 0) {
+               ERR("_bt_register_new_gdbus_object failed");
+               __rfcomm_delete_id(id);
+               return NULL;
        }
 
+       cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
+       g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
+       cb_data->obj_path = path;
+       cb_data->object_id = object_id;
+       cb_data->id = id;
+
        DBG("-");
        return cb_data;
 }
@@ -654,24 +567,23 @@ int _bt_hal_dbus_handler_rfcomm_connect(unsigned char *addr, unsigned char *uuid
        _bt_convert_addr_type_to_string(remote_addr, addr);
        DBG("Connecting to %s, uuid %s", remote_addr, remote_uuid);
        conn = __rfcomm_create_conn_info(remote_addr, sock);
-       if (!conn)
+       if (!conn) {
+               __rfcomm_cb_data_remove(cb_data);
                return BT_STATUS_FAIL;
+       }
 
-       cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
+       cb_data->conn_info = conn;
        ret = _bt_discover_services(remote_addr, (char *)remote_uuid,
                        __bt_discover_service_response_cb, cb_data);
        if (ret != BT_STATUS_SUCCESS) {
                ERR("Error returned while service discovery");
-               __rfcomm_remove_conn_info_t(cb_data, conn->remote_addr);
-               __rfcomm_cb_data_remove(cb_data);
+               __bt_free_conn(conn);
+               __bt_free_cb_data(cb_data);
                return BT_STATUS_FAIL;
        }
 
-       if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
-               INFO("Adding callback information to rfcomm_clients");
-               rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
-       } else
-               INFO("Callback information is already added");
+       INFO("Adding callback information to rfcomm_clients");
+       rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
 
        return BT_STATUS_SUCCESS;
 }