Fix issues during cleanup stage of the Bluetooth stack

 * Moved free-ing of bta_av_cb timers from the init function
   to the cleanup stage.
 * Changed the usage of btif_jni_disassociate() so it is called
   synchronously. Its previous usage was complicated -
   the function was called asynchronously on a different thread,
   and we had to wait on a future for its completion.
 * Renamed function btif_shutdown_bluetooth() to
   btif_cleanup_bluetooth() to represent better its purpose.
   Similarly, bte_main_shutdown() is renamed to bte_main_cleanup()

Also:
 * Removed function btif_init_fail(), because it is not used.
 * Updated an error log message inside function
   btif_in_execute_service_request() so the log information
   is accurate and more useful.
 * Updated the log messages related to the lifecycle of a module
   in btcore/src/module.c

Bug: 26982349
Change-Id: Icd6f159d993bdb9c8ef09bfb5b1386b3d6ea4ff2
diff --git a/bta/av/bta_av_act.c b/bta/av/bta_av_act.c
index d5abc75..cf01a84 100644
--- a/bta/av/bta_av_act.c
+++ b/bta/av/bta_av_act.c
@@ -1410,6 +1410,11 @@
         hdr.layer_specific = xx + 1;
         bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
     }
+
+    alarm_free(p_cb->link_signalling_timer);
+    p_cb->link_signalling_timer = NULL;
+    alarm_free(p_cb->accept_signalling_timer);
+    p_cb->accept_signalling_timer = NULL;
 }
 
 /*******************************************************************************
diff --git a/bta/av/bta_av_main.c b/bta/av/bta_av_main.c
index 6ff8a0c..735ee2c 100644
--- a/bta/av/bta_av_main.c
+++ b/bta/av/bta_av_main.c
@@ -219,16 +219,10 @@
 *******************************************************************************/
 static void bta_av_api_enable(tBTA_AV_DATA *p_data)
 {
-    int i;
-    tBTA_AV_ENABLE      enable;
-
-    alarm_free(bta_av_cb.link_signalling_timer);
-    alarm_free(bta_av_cb.accept_signalling_timer);
-
     /* initialize control block */
     memset(&bta_av_cb, 0, sizeof(tBTA_AV_CB));
 
-    for(i=0; i<BTA_AV_NUM_RCB; i++)
+    for (int i = 0; i < BTA_AV_NUM_RCB; i++)
         bta_av_cb.rcb[i].handle = BTA_AV_RC_HANDLE_NONE;
 
     bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
@@ -246,6 +240,7 @@
     bta_av_cb.features = p_data->api_enable.features;
     bta_av_cb.sec_mask = p_data->api_enable.sec_mask;
 
+    tBTA_AV_ENABLE enable;
     enable.features = bta_av_cb.features;
 
     /* Register for SCO change event */
diff --git a/btcore/src/module.c b/btcore/src/module.c
index 6af7d7e..07b3dd0 100644
--- a/btcore/src/module.c
+++ b/btcore/src/module.c
@@ -78,12 +78,13 @@
   assert(module != NULL);
   assert(get_module_state(module) == MODULE_STATE_NONE);
 
-  LOG_WARN(LOG_TAG, "%s initializing the module \"%s\"", __func__, module->name);
+  LOG_INFO(LOG_TAG, "%s Initializing module \"%s\"", __func__, module->name);
   if (!call_lifecycle_function(module->init)) {
-    LOG_ERROR(LOG_TAG, "%s failed to initialize \"%s\"", __func__, module->name);
+    LOG_ERROR(LOG_TAG, "%s Failed to initialize module \"%s\"",
+              __func__, module->name);
     return false;
   }
-  LOG_WARN(LOG_TAG, "%s initialized the module \"%s\"", __func__, module->name);
+  LOG_INFO(LOG_TAG, "%s Initialized module \"%s\"", __func__, module->name);
 
   set_module_state(module, MODULE_STATE_INITIALIZED);
   return true;
@@ -97,12 +98,13 @@
   // as we're converting the startup sequence.
   assert(get_module_state(module) == MODULE_STATE_INITIALIZED || module->init == NULL);
 
-  LOG_WARN(LOG_TAG, "%s Starting the module \"%s\"", __func__, module->name);
+  LOG_INFO(LOG_TAG, "%s Starting module \"%s\"", __func__, module->name);
   if (!call_lifecycle_function(module->start_up)) {
-    LOG_ERROR(LOG_TAG, "%s failed to start up \"%s\"", __func__, module->name);
+    LOG_ERROR(LOG_TAG, "%s Failed to start up module \"%s\"",
+              __func__, module->name);
     return false;
   }
-  LOG_WARN(LOG_TAG, "%s Started the module \"%s\"", __func__, module->name);
+  LOG_INFO(LOG_TAG, "%s Started module \"%s\"", __func__, module->name);
 
   set_module_state(module, MODULE_STATE_STARTED);
   return true;
@@ -118,11 +120,13 @@
   if (state < MODULE_STATE_STARTED)
     return;
 
-  LOG_WARN(LOG_TAG, "%s Shutting the module \"%s\"", __func__, module->name);
-  if (!call_lifecycle_function(module->shut_down))
-    LOG_ERROR(LOG_TAG, "%s found \"%s\" reported failure during shutdown. Continuing anyway.", __func__, module->name);
-  else
-    LOG_WARN(LOG_TAG, "%s Shutdown the module \"%s\"", __func__, module->name);
+  LOG_INFO(LOG_TAG, "%s Shutting down module \"%s\"", __func__, module->name);
+  if (!call_lifecycle_function(module->shut_down)) {
+    LOG_ERROR(LOG_TAG, "%s Failed to shutdown module \"%s\". Continuing anyway.",
+              __func__, module->name);
+  }
+  LOG_INFO(LOG_TAG, "%s Shutdown of module \"%s\" completed",
+           __func__, module->name);
 
   set_module_state(module, MODULE_STATE_INITIALIZED);
 }
@@ -137,8 +141,13 @@
   if (state < MODULE_STATE_INITIALIZED)
     return;
 
-  if (!call_lifecycle_function(module->clean_up))
-    LOG_ERROR(LOG_TAG, "%s found \"%s\" reported failure during cleanup. Continuing anyway.", __func__, module->name);
+  LOG_INFO(LOG_TAG, "%s Cleaning up module \"%s\"", __func__, module->name);
+  if (!call_lifecycle_function(module->clean_up)) {
+    LOG_ERROR(LOG_TAG, "%s Failed to cleanup module \"%s\". Continuing anyway.",
+              __func__, module->name);
+  }
+  LOG_INFO(LOG_TAG, "%s Cleanup of module \"%s\" completed",
+           __func__, module->name);
 
   set_module_state(module, MODULE_STATE_NONE);
 }
diff --git a/btif/include/btif_api.h b/btif/include/btif_api.h
index ed67b9e..355f188 100644
--- a/btif/include/btif_api.h
+++ b/btif/include/btif_api.h
@@ -76,16 +76,15 @@
 
 /*******************************************************************************
 **
-** Function         btif_shutdown_bluetooth
+** Function         btif_cleanup_bluetooth
 **
-** Description      Finalizes BT scheduler shutdown and terminates BTIF
-**                  task.
+** Description      Cleanup BTIF state.
 **
 **
 ** Returns          void
 **
 *******************************************************************************/
-bt_status_t btif_shutdown_bluetooth(void);
+bt_status_t btif_cleanup_bluetooth(void);
 
 /*******************************************************************************
 **
diff --git a/btif/include/btif_common.h b/btif/include/btif_common.h
index 50c2ea5..b48631b 100644
--- a/btif/include/btif_common.h
+++ b/btif/include/btif_common.h
@@ -215,6 +215,5 @@
                                    uint32_t num_props, bt_property_t *p_props);
 
 void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
-void btif_init_fail(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
 
 #endif /* BTIF_COMMON_H */
diff --git a/btif/src/btif_core.c b/btif/src/btif_core.c
index ab69f1f..ecb14d9 100644
--- a/btif/src/btif_core.c
+++ b/btif/src/btif_core.c
@@ -129,7 +129,7 @@
 **  Static functions
 ************************************************************************************/
 static void btif_jni_associate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
-static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param);
+static void btif_jni_disassociate();
 static bool btif_fetch_property(const char *key, bt_bdaddr_t *addr);
 
 /* sends message to btif task */
@@ -145,7 +145,7 @@
 /** TODO: Move these to _common.h */
 void bte_main_boot_entry(void);
 void bte_main_disable(void);
-void bte_main_shutdown(void);
+void bte_main_cleanup(void);
 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
 void bte_main_enable_lpm(BOOLEAN enable);
 #endif
@@ -260,16 +260,6 @@
   BTA_EnableBluetooth(bte_dm_evt);
 }
 
-void btif_init_fail(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
-  BTIF_TRACE_DEBUG("btif_task: hardware init failed");
-  bte_main_disable();
-  btif_queue_release();
-  bte_main_shutdown();
-  btif_dut_mode = 0;
-
-  future_ready(stack_manager_get_hack_future(), FUTURE_FAIL);
-}
-
 /*******************************************************************************
 **
 ** Function         btif_task
@@ -599,29 +589,26 @@
 
 /*******************************************************************************
 **
-** Function         btif_shutdown_bluetooth
+** Function         btif_cleanup_bluetooth
 **
-** Description      Finalizes BT scheduler shutdown and terminates BTIF
-**                  task.
+** Description      Cleanup BTIF state.
 **
 ** Returns          void
 **
 *******************************************************************************/
 
-bt_status_t btif_shutdown_bluetooth(void)
+bt_status_t btif_cleanup_bluetooth(void)
 {
     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
 
     btif_dm_cleanup();
-
-    btif_transfer_context(btif_jni_disassociate, 0, NULL, 0, NULL);
-
+    btif_jni_disassociate();
     btif_queue_release();
 
     thread_free(bt_jni_workqueue_thread);
     bt_jni_workqueue_thread = NULL;
 
-    bte_main_shutdown();
+    bte_main_cleanup();
 
     btif_dut_mode = 0;
 
@@ -1328,10 +1315,8 @@
   HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
 }
 
-static void btif_jni_disassociate(UNUSED_ATTR uint16_t event, UNUSED_ATTR char *p_param) {
+static void btif_jni_disassociate() {
   BTIF_TRACE_DEBUG("%s Disassociating thread from JVM", __func__);
   HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
   bt_hal_cbacks = NULL;
-  future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
 }
-
diff --git a/btif/src/btif_dm.c b/btif/src/btif_dm.c
index 8b7edb2..1bc9fa9 100644
--- a/btif/src/btif_dm.c
+++ b/btif/src/btif_dm.c
@@ -345,7 +345,9 @@
              btif_sdp_execute_service(b_enable);
          }break;
          default:
-              BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
+           BTIF_TRACE_ERROR("%s: Unknown service %d being %s",
+                            __func__, service_id,
+                            (b_enable) ? "enabled" : "disabled");
               return BT_STATUS_FAIL;
     }
     return BT_STATUS_SUCCESS;
diff --git a/btif/src/stack_manager.c b/btif/src/stack_manager.c
index dc63d7d..e132221 100644
--- a/btif/src/stack_manager.c
+++ b/btif/src/stack_manager.c
@@ -202,11 +202,9 @@
   hack_future = local_hack_future;
   stack_is_initialized = false;
 
-  btif_shutdown_bluetooth();
+  btif_cleanup_bluetooth();
   module_clean_up(get_module(BTIF_CONFIG_MODULE));
   module_clean_up(get_module(BT_UTILS_MODULE));
-
-  future_await(local_hack_future);
   module_clean_up(get_module(OSI_MODULE));
   module_management_stop();
   LOG_INFO(LOG_TAG, "%s finished", __func__);
diff --git a/main/bte_main.c b/main/bte_main.c
index 33266e7..da36056 100644
--- a/main/bte_main.c
+++ b/main/bte_main.c
@@ -119,14 +119,14 @@
 
 /******************************************************************************
 **
-** Function         bte_main_shutdown
+** Function         bte_main_cleanup
 **
-** Description      BTE MAIN API - Shutdown code for BTE chip/stack
+** Description      BTE MAIN API - Cleanup code for BTE chip/stack
 **
 ** Returns          None
 **
 ******************************************************************************/
-void bte_main_shutdown()
+void bte_main_cleanup()
 {
     data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
     hci->set_data_queue(NULL);