Mesh: Handle Multi-application init & deinit logic
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-main.c
index ee82600..849edbc 100644 (file)
@@ -50,6 +50,7 @@
 #define MESH_LAUNCH_DELAY 500
 
 static guint launch_timer = 0;
+static guint mesh_app_ref_count;
 
 /* Event handlers */
 static void __bt_mesh_handle_pending_request_info(int result,
@@ -71,6 +72,11 @@ static void __bt_mesh_handle_pending_request_info(int result,
                case BT_MESH_INIT: {
                        BT_INFO("Mesh: Request: BT_MESH_INIT Sender: [%s] result[%d]",
                                        req_info->sender, result);
+
+                       if (result == BLUETOOTH_ERROR_NONE)
+                               /* 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,
                                out_param, result);
@@ -808,6 +814,7 @@ static gboolean __bt_mesh_launch_timer_expired_cb(gpointer data)
                        NULL, 0);
 
        /* Reset launch timer ID */
+       BT_INFO("Mesh: Timer ID after expiry [%u]", launch_timer);
        launch_timer = 0;
 
        return FALSE;
@@ -817,12 +824,23 @@ int _bt_mesh_init(void)
 {
        int ret = BLUETOOTH_ERROR_NONE;
 
-       BT_INFO("Mesh: Launch Timer val [%u]", launch_timer);
+       BT_INFO("Mesh: Total apps using Mesh: [%d]",
+               mesh_app_ref_count);
+
+       BT_INFO("Mesh: Current Timer ID [%u]", launch_timer);
        if (launch_timer > 0) {
                BT_INFO("Mesh: BT_MESH_INIT in progress");
                return ret;
        }
 
+       if (mesh_app_ref_count) {
+               BT_INFO("Mesh: Mesh Stack already initialized");
+               /* Increase mesh app ref count */
+               mesh_app_ref_count++;
+               return BLUETOOTH_ERROR_ALREADY_INITIALIZED;
+       }
+
+       BT_INFO("Mesh: Mesh Stack init not done yet!");
        /* Launch bluetooth-meshd */
        ret = __bt_meshd_launch();
        if (BLUETOOTH_ERROR_NONE != ret) {
@@ -835,6 +853,7 @@ int _bt_mesh_init(void)
                                (GSourceFunc)__bt_mesh_launch_timer_expired_cb,
                                                                        NULL);
 
+       BT_INFO("Mesh: Timer ID [%u]", launch_timer);
        return ret;
 }
 
@@ -848,22 +867,31 @@ int _bt_mesh_deinit(void)
                launch_timer = 0;
        }
 
-       /* Terminate bluetooth-meshd */
-       ret = actd_stop_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM,
-                       MESH_SYSTEMD_SERVICE_NAME, 5000);
+       BT_INFO("Mesh: Current number of applications using mesh [%d]",
+                       mesh_app_ref_count);
 
-       if (ret != UNIT_CONTROL_OK)
-               BT_ERR("Failed to call systemact service: %d", ret);
+       if (mesh_app_ref_count == 1) {
+               BT_INFO("Mesh: Only one app using Mesh Stack: Unload & reduce ref count");
+               /* Terminate bluetooth-meshd */
+               ret = actd_stop_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM,
+                               MESH_SYSTEMD_SERVICE_NAME, 5000);
 
-       status = mesh_disable();
-       if (OAL_STATUS_SUCCESS != status) {
-               BT_ERR("Mesh: Failed to de-initialize Mesh profile, status: %d",
-                               status);
-               return BLUETOOTH_ERROR_INTERNAL;
+               if (ret != UNIT_CONTROL_OK)
+                       BT_ERR("Failed to call systemact service: %d", ret);
+
+               status = mesh_disable();
+               if (OAL_STATUS_SUCCESS != status) {
+                       BT_ERR("Mesh: Failed to de-initialize Mesh profile, status: %d",
+                                       status);
+                       return BLUETOOTH_ERROR_INTERNAL;
+               }
+
+               /* Un-Register Mesh event handler */
+               _bt_service_unregister_event_handler_callback(BT_MESH_MODULE);
        }
 
-       /* Register AVRCP target event handler */
-       _bt_service_unregister_event_handler_callback(BT_MESH_MODULE);
+       /* Decrease mesh app ref count */
+       mesh_app_ref_count--;
 
        return BLUETOOTH_ERROR_NONE;
 }