Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 46005567 authored by Chris Manton's avatar Chris Manton Committed by Automerger Merge Worker
Browse files

gd_acl: Connect gd acl shutdown am: 1c998685

Original change: https://android-review.googlesource.com/c/platform/system/bt/+/1575235

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I1b19c4232c9acc7efb243595d3a9c1ca7e6f70c7
parents 572af79a 1c998685
Loading
Loading
Loading
Loading
+96 −36
Original line number Original line Diff line number Diff line
@@ -35,6 +35,7 @@
#include "btif/include/stack_manager.h"
#include "btif/include/stack_manager.h"
#include "device/include/controller.h"
#include "device/include/controller.h"
#include "device/include/interop.h"
#include "device/include/interop.h"
#include "main/shim/acl_api.h"
#include "main/shim/btm_api.h"
#include "main/shim/btm_api.h"
#include "main/shim/dumpsys.h"
#include "main/shim/dumpsys.h"
#include "main/shim/shim.h"
#include "main/shim/shim.h"
@@ -45,7 +46,7 @@
#include "stack/include/acl_api.h"
#include "stack/include/acl_api.h"
#include "stack/include/bt_types.h"
#include "stack/include/bt_types.h"
#include "stack/include/btm_client_interface.h"
#include "stack/include/btm_client_interface.h"
#include "stack/include/btu.h"
#include "stack/include/btu.h"  // do_in_main_thread
#include "types/raw_address.h"
#include "types/raw_address.h"


#if (GAP_INCLUDED == TRUE)
#if (GAP_INCLUDED == TRUE)
@@ -139,13 +140,58 @@ static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result);
#define BTA_DM_SWITCH_DELAY_TIMER_MS 500
#define BTA_DM_SWITCH_DELAY_TIMER_MS 500
#endif
#endif


namespace {

// Time to wait after receiving shutdown request to delay the actual shutdown
// process. This time may be zero which invokes immediate shutdown.
#ifndef BTA_DISABLE_DELAY
constexpr uint64_t kDisableDelayTimerInMs = 0;
#else
constexpr uint64_t kDisableDelayTimerInMs =
    static_cast<uint64_t>(BTA_DISABLE_DELAY);
#endif

struct WaitForAllAclConnectionsToDrain {
  uint64_t time_to_wait_in_ms;
  unsigned long TimeToWaitInMs() const {
    return static_cast<unsigned long>(time_to_wait_in_ms);
  }
  void* AlarmCallbackData() const {
    return const_cast<void*>(static_cast<const void*>(this));
  }

  static const WaitForAllAclConnectionsToDrain* FromAlarmCallbackData(
      void* data);
  static bool IsFirstPass(const WaitForAllAclConnectionsToDrain*);
} first_pass =
    {
        .time_to_wait_in_ms = static_cast<uint64_t>(BTA_DM_DISABLE_TIMER_MS),
},
  second_pass = {
      .time_to_wait_in_ms =
          static_cast<uint64_t>(BTA_DM_DISABLE_TIMER_RETRIAL_MS),
};

bool WaitForAllAclConnectionsToDrain::IsFirstPass(
    const WaitForAllAclConnectionsToDrain* pass) {
  return pass == &first_pass;
}

const WaitForAllAclConnectionsToDrain*
WaitForAllAclConnectionsToDrain::FromAlarmCallbackData(void* data) {
  return const_cast<const WaitForAllAclConnectionsToDrain*>(
      static_cast<WaitForAllAclConnectionsToDrain*>(data));
}

}  // namespace

static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr);
static void bta_dm_reset_sec_dev_pending(const RawAddress& remote_bd_addr);
static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr);
static void bta_dm_remove_sec_dev_entry(const RawAddress& remote_bd_addr);
static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq, uint8_t* p_eir,
                                      uint16_t eir_len);
                                      uint16_t eir_len);
static void bta_dm_observe_cmpl_cb(void* p_result);
static void bta_dm_observe_cmpl_cb(void* p_result);
static void bta_dm_delay_role_switch_cback(void* data);
static void bta_dm_delay_role_switch_cback(void* data);
static void bta_dm_disable_timer_cback(void* data);
static void bta_dm_wait_for_acl_to_drain_cback(void* data);


const uint16_t bta_service_id_to_uuid_lkup_tbl[BTA_MAX_SERVICE_ID] = {
const uint16_t bta_service_id_to_uuid_lkup_tbl[BTA_MAX_SERVICE_ID] = {
    UUID_SERVCLASS_PNP_INFORMATION,       /* Reserved */
    UUID_SERVCLASS_PNP_INFORMATION,       /* Reserved */
@@ -377,26 +423,30 @@ void bta_dm_disable() {
  connection_manager::reset(false);
  connection_manager::reset(false);


  if (BTM_GetNumAclLinks() == 0) {
  if (BTM_GetNumAclLinks() == 0) {
#if (BTA_DISABLE_DELAY > 0)
    // We can shut down faster if there are no ACL links
    /* If BTA_DISABLE_DELAY is defined and greater than zero, then delay the
    switch (kDisableDelayTimerInMs) {
     * shutdown by
      case 0:
     * BTA_DISABLE_DELAY milliseconds
        LOG_DEBUG("Immediately disabling device manager");
     */
        bta_dm_disable_conn_down_timer_cback(nullptr);
    LOG_WARN("BTA_DISABLE_DELAY set to %d ms", BTA_DISABLE_DELAY);
        break;
    alarm_set_on_mloop(bta_dm_cb.disable_timer, BTA_DISABLE_DELAY,
      default:
                       bta_dm_disable_conn_down_timer_cback, NULL);
        LOG_DEBUG("Set timer to delay disable initiation:%lu ms",
#else
                  static_cast<unsigned long>(kDisableDelayTimerInMs));
    bta_dm_disable_conn_down_timer_cback(NULL);
        alarm_set_on_mloop(bta_dm_cb.disable_timer, kDisableDelayTimerInMs,
#endif
                           bta_dm_disable_conn_down_timer_cback, nullptr);
    }
  } else {
  } else {
    alarm_set_on_mloop(bta_dm_cb.disable_timer, BTA_DM_DISABLE_TIMER_MS,
    LOG_DEBUG("Set timer to wait for all ACL connections to close:%lu ms",
                       bta_dm_disable_timer_cback, UINT_TO_PTR(0));
              first_pass.TimeToWaitInMs());
    alarm_set_on_mloop(bta_dm_cb.disable_timer, first_pass.time_to_wait_in_ms,
                       bta_dm_wait_for_acl_to_drain_cback,
                       first_pass.AlarmCallbackData());
  }
  }
}
}


/*******************************************************************************
/*******************************************************************************
 *
 *
 * Function         bta_dm_disable_timer_cback
 * Function         bta_dm_wait_for_all_acl_to_drain
 *
 *
 * Description      Called if the disable timer expires
 * Description      Called if the disable timer expires
 *                  Used to close ACL connections which are still active
 *                  Used to close ACL connections which are still active
@@ -406,31 +456,41 @@ void bta_dm_disable() {
 * Returns          void
 * Returns          void
 *
 *
 ******************************************************************************/
 ******************************************************************************/
static void bta_dm_disable_timer_cback(void* data) {
static bool force_disconnect_all_acl_connections() {
  uint8_t i;
  const bool is_force_disconnect_needed = (bta_dm_cb.device_list.count > 0);
  tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
  bool trigger_disc = false;
  uint32_t param = PTR_TO_UINT(data);


  APPL_TRACE_EVENT("%s trial %u", __func__, param);
  for (auto i = 0; i < bta_dm_cb.device_list.count; i++) {

  if (BTM_GetNumAclLinks() && (param == 0)) {
    for (i = 0; i < bta_dm_cb.device_list.count; i++) {
      transport = bta_dm_cb.device_list.peer_device[i].transport;
    btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
    btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
                     transport);
                   bta_dm_cb.device_list.peer_device[i].transport);
      trigger_disc = true;
  }
  return is_force_disconnect_needed;
}
}


    /* Retrigger disable timer in case ACL disconnect failed, DISABLE_EVT still
static void bta_dm_wait_for_acl_to_drain_cback(void* data) {
       need
  ASSERT(data != nullptr);
        to be sent out to avoid jave layer disable timeout */
  const WaitForAllAclConnectionsToDrain* pass =
    if (trigger_disc) {
      WaitForAllAclConnectionsToDrain::FromAlarmCallbackData(data);
      alarm_set_on_mloop(bta_dm_cb.disable_timer,

                         BTA_DM_DISABLE_TIMER_RETRIAL_MS,
  if (BTM_GetNumAclLinks() &&
                         bta_dm_disable_timer_cback, UINT_TO_PTR(1));
      WaitForAllAclConnectionsToDrain::IsFirstPass(pass)) {
    /* DISABLE_EVT still need to be sent out to avoid java layer disable timeout
     */
    if (force_disconnect_all_acl_connections()) {
      LOG_DEBUG(
          "Set timer for second pass to wait for all ACL connections to "
          "close:%lu ms ",
          second_pass.TimeToWaitInMs());
      alarm_set_on_mloop(
          bta_dm_cb.disable_timer, second_pass.time_to_wait_in_ms,
          bta_dm_wait_for_acl_to_drain_cback, second_pass.AlarmCallbackData());
    }
    }
  } else {
  } else {
    // No ACL links were up or is second pass at ACL closure
    if (bluetooth::shim::is_gd_acl_enabled()) {
      LOG_INFO("Ensuring all ACL connections have been properly flushed");
      bluetooth::shim::ACL_Shutdown();
    }

    bta_dm_cb.disabling = false;
    bta_dm_cb.disabling = false;


    bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
    bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
+5 −3
Original line number Original line Diff line number Diff line
@@ -304,10 +304,12 @@ static void bta_dm_pm_stop_timer_by_index(tBTA_PM_TIMER* p_timer,


  std::unique_lock<std::recursive_mutex> schedule_lock(pm_timer_schedule_mutex);
  std::unique_lock<std::recursive_mutex> schedule_lock(pm_timer_schedule_mutex);
  std::unique_lock<std::recursive_mutex> state_lock(pm_timer_state_mutex);
  std::unique_lock<std::recursive_mutex> state_lock(pm_timer_state_mutex);
  if (p_timer->srvc_id[timer_idx] == BTA_ID_MAX)
  if (p_timer->srvc_id[timer_idx] == BTA_ID_MAX) {
    return; /* The timer was not scheduled */
    return;
  } /* The timer was not scheduled */


  CHECK(p_timer->in_use && (p_timer->active > 0));
  CHECK(p_timer->in_use);
  CHECK(p_timer->active > 0);


  p_timer->srvc_id[timer_idx] = BTA_ID_MAX;
  p_timer->srvc_id[timer_idx] = BTA_ID_MAX;
  /* NOTE: pm_action[timer_idx] intentionally not reset */
  /* NOTE: pm_action[timer_idx] intentionally not reset */