Fix osp_server unregister issue 23/230123/2
authorWootak Jung <wootak.jung@samsung.com>
Wed, 8 Apr 2020 05:48:49 +0000 (14:48 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Wed, 8 Apr 2020 06:12:46 +0000 (15:12 +0900)
osp_server is not removed if app terminated

issue scenario:
rfcomm server create & listen > connected > server app termination
> new rfcomm server create & listen > try to connect > failed

Change-Id: I573bae5bafb94959cf5799c6be0f51b84d9576b3
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
bt-service-adaptation/services/bt-request-handler.c
bt-service-adaptation/services/bt-service-common.c
bt-service-adaptation/services/bt-service-main.c
bt-service-adaptation/services/include/bt-service-common.h
bt-service-adaptation/services/rfcomm/bt-service-rfcomm.c

index d91f448..4e230df 100644 (file)
@@ -3806,13 +3806,15 @@ int __bt_agent_request(int function_name,
                char *uuid;
                char *path;
                int fd;
+               char *sender;
 
+               sender = (char *)g_dbus_method_invocation_get_sender(context);
                __bt_service_get_parameters(in_param1, &type, sizeof(int));
                uuid = (char *)g_variant_get_data(in_param2);
                path = (char *)g_variant_get_data(in_param3);
                __bt_service_get_parameters(in_param4, &fd, sizeof(int));
 
-               result = _bt_register_osp_server_in_agent(type, uuid, path, fd);
+               result = _bt_register_osp_server_in_agent(sender, type, uuid, path, fd);
                break;
        }
        case BT_UNSET_AUTHORIZATION: {
index c6bbcfe..9c148c4 100644 (file)
 #include <oal-manager.h>
 #include <oal-device-mgr.h>
 
+struct osp_server_t {
+       char *sender;
+       char *uuid;
+};
+static GSList *osp_server_list;
+
 #ifdef TIZEN_FEATURE_BT_IPSP
 static GDBusProxy *ipsp_proxy;
 #endif
@@ -552,34 +558,90 @@ gboolean _bt_utf8_validate(char *name)
        return TRUE;
 }
 
-int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
+int _bt_register_osp_server_in_agent(const char *sender, int type, char *uuid, char *path, int fd)
 {
+       retv_if(sender == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
+       retv_if(uuid == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
+       retv_if(path == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
+
        BT_DBG("+");
 
-#if TODO_40 /* Need to add this function */
-       if (!_bt_agent_register_osp_server(type, uuid, path, fd))
-               return BLUETOOTH_ERROR_INTERNAL;
-#else
+       if (type == OAL_OSP_SERVER_RFCOMM) {
+               struct osp_server_t *osp_server;
+               osp_server = g_malloc0(sizeof(struct osp_server_t));
+               osp_server->sender = g_strdup(sender);
+               osp_server->uuid = g_strdup(uuid);
+               osp_server_list = g_slist_append(osp_server_list, osp_server);
+       }
+
        if (device_register_osp_server(type, uuid, path, fd) != 0)
                return BLUETOOTH_ERROR_INTERNAL;
-#endif
        return BLUETOOTH_ERROR_NONE;
 }
 
+static void __bt_free_osp_server(struct osp_server_t *osp_server)
+{
+       g_free(osp_server->sender);
+       g_free(osp_server->uuid);
+       g_free(osp_server);
+}
+
 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
 {
+       GSList *l;
+       struct osp_server_t *osp_server;
+       retv_if(uuid == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
+
        BT_DBG("+");
 
-#if TODO_40 /* Need to add this function */
-       if (!_bt_agent_unregister_osp_server(type, uuid))
-               return BLUETOOTH_ERROR_INTERNAL;
-#else
        if (device_unregister_osp_server(type, uuid) != 0)
                return BLUETOOTH_ERROR_INTERNAL;
-#endif
+
+       for (l = osp_server_list; l; l = g_slist_next(l)) {
+               osp_server = l->data;
+               if (!osp_server)
+                       continue;
+               if (g_strcmp0(osp_server->uuid, uuid) == 0) {
+                       osp_server_list = g_slist_remove(osp_server_list, osp_server);
+                       __bt_free_osp_server(osp_server);
+                       break;
+               }
+       }
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_unregister_osp_server_in_agent_by_sender(const char *sender)
+{
+       GSList *l;
+       struct osp_server_t *osp_server;
+       retv_if(sender == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
+
+       BT_DBG("Unregister osp server by sender: %s", sender);
+
+       /* App termination case, unregister all osp_server of sender */
+       for (l = osp_server_list; l; ) {
+               osp_server = l->data;
+               l = g_slist_next(l);
+               if (!osp_server)
+                       continue;
+               if (g_strcmp0(osp_server->sender, sender) == 0) {
+                       if (device_unregister_osp_server(OAL_OSP_SERVER_RFCOMM, osp_server->uuid) != 0)
+                               return BLUETOOTH_ERROR_INTERNAL;
+                       osp_server_list = g_slist_remove(osp_server_list, osp_server);
+                       __bt_free_osp_server(osp_server);
+               }
+       }
+
        return BLUETOOTH_ERROR_NONE;
 }
 
+void _bt_deinit_osp_server()
+{
+       g_slist_free_full(osp_server_list, (GDestroyNotify)__bt_free_osp_server);
+       osp_server_list = NULL;
+}
+
 int _bt_set_socket_non_blocking(int socket_fd)
 {
        /* Set Nonblocking */
index af2f2de..d63d0e3 100644 (file)
@@ -115,6 +115,7 @@ gboolean _bt_terminate_service(gpointer user_data)
 static gboolean __bt_main_loop_quit_idle_cb(gpointer user_data)
 {
        _bt_deinit_proxys();
+       _bt_deinit_osp_server();
 
        _bt_clear_request_list();
 
index 546af31..db3596f 100644 (file)
@@ -406,10 +406,14 @@ int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length);
 
 gboolean _bt_utf8_validate(char *name);
 
-int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd);
+int _bt_register_osp_server_in_agent(const char *sender, int type, char *uuid, char *path, int fd);
 
 int _bt_unregister_osp_server_in_agent(int type, char *uuid);
 
+int _bt_unregister_osp_server_in_agent_by_sender(const char *sender);
+
+void _bt_deinit_osp_server();
+
 int _bt_set_socket_non_blocking(int socket_fd);
 
 int _bt_set_non_blocking_tty(int sk);
index a8ff590..7311d40 100644 (file)
@@ -340,6 +340,9 @@ void _bt_rfcomm_server_check_termination(const char *name)
                                                conn_info->client_fd);
                }
        }
+
+       /* Cleanup osp server */
+       _bt_unregister_osp_server_in_agent_by_sender(name);
 }
 
 static void __bt_free_server_conn(bt_rfcomm_conn_info_t *conn_info)