Mesh: Handle mesh application termination event
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-main.c
index 849edbc..a0779bc 100644 (file)
@@ -52,6 +52,8 @@
 static guint launch_timer = 0;
 static guint mesh_app_ref_count;
 
+static GSList *apps;
+
 /* Event handlers */
 static void __bt_mesh_handle_pending_request_info(int result,
                int service_function, void *param,
@@ -73,9 +75,17 @@ static void __bt_mesh_handle_pending_request_info(int result,
                        BT_INFO("Mesh: Request: BT_MESH_INIT Sender: [%s] result[%d]",
                                        req_info->sender, result);
 
-                       if (result == BLUETOOTH_ERROR_NONE)
+                       if (result == BLUETOOTH_ERROR_NONE) {
+
+                               /* Save Mesh App Owner */
+                               char *owner = g_strdup(req_info->sender);
+                               BT_INFO("Mesh: Insert Sender Mesh App List[%s]",
+                                               req_info->sender);
+                               apps = g_slist_append(apps, owner);
+
                                /* Increase mesh app ref count */
                                mesh_app_ref_count++;
+                       }
 
                        out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
                        _bt_service_method_return(req_info->context,
@@ -820,13 +830,15 @@ static gboolean __bt_mesh_launch_timer_expired_cb(gpointer data)
        return FALSE;
 }
 
-int _bt_mesh_init(void)
+int _bt_mesh_init(const char *sender)
 {
        int ret = BLUETOOTH_ERROR_NONE;
 
        BT_INFO("Mesh: Total apps using Mesh: [%d]",
                mesh_app_ref_count);
 
+       BT_INFO("Mesh: Mesh App Sender [%s]", sender);
+
        BT_INFO("Mesh: Current Timer ID [%u]", launch_timer);
        if (launch_timer > 0) {
                BT_INFO("Mesh: BT_MESH_INIT in progress");
@@ -835,6 +847,10 @@ int _bt_mesh_init(void)
 
        if (mesh_app_ref_count) {
                BT_INFO("Mesh: Mesh Stack already initialized");
+
+               /* Save Mesh App Owner */
+               apps = g_slist_append(apps, g_strdup(sender));
+
                /* Increase mesh app ref count */
                mesh_app_ref_count++;
                return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
@@ -857,11 +873,35 @@ int _bt_mesh_init(void)
        return ret;
 }
 
-int _bt_mesh_deinit(void)
+static gint __bt_mesh_match_sender(gconstpointer a, gconstpointer b)
+{
+       char *owner_a = (char*) a;
+       char *owner_b = (char*) b;
+
+       return g_strcmp0(owner_a, owner_b);
+}
+
+int _bt_mesh_deinit(const char *sender)
 {
+       GSList *l = NULL;
+       char *owner = NULL;
        oal_status_t status = OAL_STATUS_SUCCESS;
        int ret = UNIT_CONTROL_OK;
 
+       BT_INFO("Mesh: Deinit Request from App [%s]", sender);
+       BT_INFO("Mesh: Total Apps available in list [%d]",
+                       g_slist_length(apps));
+
+       /* Find & remove app entry from list */
+       l = g_slist_find_custom(apps, sender,
+                       (GCompareFunc)__bt_mesh_match_sender);
+       if (!l) {
+               BT_ERR("Mesh: App is not Mesh App");
+               return BLUETOOTH_ERROR_INTERNAL;
+       }
+
+       BT_INFO("Mesh: Deinit Mesh: Sender Found: [%s]", sender);
+
        if (launch_timer > 0) {
                g_source_remove(launch_timer);
                launch_timer = 0;
@@ -893,5 +933,17 @@ int _bt_mesh_deinit(void)
        /* Decrease mesh app ref count */
        mesh_app_ref_count--;
 
+       owner = l->data;
+       apps = g_slist_remove(apps, owner);
+       g_free(owner);
+
        return BLUETOOTH_ERROR_NONE;
 }
+
+void _bt_mesh_handle_app_termination(const char *sender)
+{
+       BT_INFO("Mesh: Handle App termination: dbus app name[%s]",
+                       sender);
+
+       _bt_mesh_deinit(sender);
+}