Fix osp_server unregister issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / bt-service-common.c
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 */