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

Commit 30551c62 authored by Chris Manton's avatar Chris Manton
Browse files

Simplify stack/acl/btm_acl::btm_acl_role_changed

Bug: 163134718
Tag: #refactor
Test: compile & verify basic functions working
Change-Id: Ife020abe848bbcf93e3d3bb7db68e57211c4d871
parent 261e6dad
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -118,7 +118,7 @@ typedef struct {
                                     uint8_t encr_enable);
  friend void btm_acl_init(void);
  friend void btm_acl_role_changed(uint8_t hci_status,
                                   const RawAddress* bd_addr, uint8_t new_role);
                                   const RawAddress& bd_addr, uint8_t new_role);
  friend void btm_acl_update_conn_addr(uint16_t conn_handle,
                                       const RawAddress& address);
  friend void btm_pm_proc_cmd_status(uint8_t status);
+30 −40
Original line number Diff line number Diff line
@@ -1439,84 +1439,74 @@ void btm_blacklist_role_change_device(const RawAddress& bd_addr,
 * Returns          void
 *
 ******************************************************************************/
void btm_acl_role_changed(uint8_t hci_status, const RawAddress* bd_addr,
void btm_acl_role_changed(uint8_t hci_status, const RawAddress& bd_addr,
                          uint8_t new_role) {
  const RawAddress* p_bda =
      (bd_addr) ? bd_addr : &btm_cb.acl_cb_.switch_role_ref_data.remote_bd_addr;
  tACL_CONN* p = internal_.btm_bda_to_acl(*p_bda, BT_TRANSPORT_BR_EDR);
  tBTM_ROLE_SWITCH_CMPL* p_data = &btm_cb.acl_cb_.switch_role_ref_data;
  tBTM_SEC_DEV_REC* p_dev_rec;

  BTM_TRACE_DEBUG("%s: peer %s hci_status:0x%x new_role:%d", __func__,
                  (p_bda != nullptr) ? bd_addr->ToString().c_str() : "nullptr",
                  hci_status, new_role);
  /* Ignore any stray events */
  if (p == NULL) {
  tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
  if (p_acl == nullptr) {
    BTM_TRACE_WARNING("%s: Unsolicited role change for unknown ACL", __func__);
    return;
  }

  p_data->hci_status = hci_status;
  tBTM_ROLE_SWITCH_CMPL* p_switch_role = &btm_cb.acl_cb_.switch_role_ref_data;
  BTM_TRACE_DEBUG("%s: peer %s hci_status:0x%x new_role:%d", __func__,
                  bd_addr.ToString().c_str(), hci_status, new_role);

  p_switch_role->hci_status = hci_status;
  if (hci_status == HCI_SUCCESS) {
    p_data->role = new_role;
    p_data->remote_bd_addr = *p_bda;
    p_switch_role->role = new_role;
    p_switch_role->remote_bd_addr = bd_addr;

    /* Update cached value */
    p->link_role = new_role;
    p_acl->link_role = new_role;

    /* Reload LSTO: link supervision timeout is reset in the LM after a role
     * switch */
    if (new_role == HCI_ROLE_MASTER) {
      BTM_SetLinkSuperTout(p->remote_addr, p->link_super_tout);
      BTM_SetLinkSuperTout(p_acl->remote_addr, p_acl->link_super_tout);
    }
  } else {
    new_role = p->link_role;
    new_role = p_acl->link_role;
  }

  /* Check if any SCO req is pending for role change */
  btm_sco_chk_pend_rolechange(p->hci_handle);
  btm_sco_chk_pend_rolechange(p_acl->hci_handle);

  /* if switching state is switching we need to turn encryption on */
  /* if idle, we did not change encryption */
  if (p->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING) {
    btsnd_hcic_set_conn_encrypt(p->hci_handle, true);
    p->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
    p->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
  if (p_acl->switch_role_state == BTM_ACL_SWKEY_STATE_SWITCHING) {
    btsnd_hcic_set_conn_encrypt(p_acl->hci_handle, true);
    p_acl->encrypt_state = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
    p_acl->switch_role_state = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
    return;
  }

  /* Set the switch_role_state to IDLE since the reply received from HCI */
  /* regardless of its result either success or failed. */
  if (p->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS) {
    p->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
    p->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
  if (p_acl->switch_role_state == BTM_ACL_SWKEY_STATE_IN_PROGRESS) {
    p_acl->switch_role_state = BTM_ACL_SWKEY_STATE_IDLE;
    p_acl->encrypt_state = BTM_ACL_ENCRYPT_STATE_IDLE;
  }

  BTA_dm_report_role_change(*p_bda, new_role, hci_status);

  BTA_dm_report_role_change(bd_addr, new_role, hci_status);
  BTM_TRACE_DEBUG(
      "%s: peer %s Role Switch Event: new_role 0x%02x, HCI Status 0x%02x, "
      "rs_st:%d",
      __func__, (p_bda != nullptr) ? p_bda->ToString().c_str() : "nullptr",
      p_data->role, p_data->hci_status, p->switch_role_state);
      __func__, bd_addr.ToString().c_str(), p_switch_role->role,
      p_switch_role->hci_status, p_acl->switch_role_state);

#if (BTM_DISC_DURING_RS == TRUE)
  /* If a disconnect is pending, issue it now that role switch has completed */
  p_dev_rec = btm_find_dev(*p_bda);
  if (p_dev_rec != NULL) {
  tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
  if (p_dev_rec != nullptr) {
    if (p_dev_rec->rs_disc_pending == BTM_SEC_DISC_PENDING) {
      BTM_TRACE_WARNING(
          "%s peer %s Issuing delayed HCI_Disconnect!!!", __func__,
          (p_bda != nullptr) ? p_bda->ToString().c_str() : "nullptr");
      BTM_TRACE_WARNING("%s peer %s Issuing delayed HCI_Disconnect!!!",
                        __func__, bd_addr.ToString().c_str());
      btsnd_hcic_disconnect(p_dev_rec->hci_handle, HCI_ERR_PEER_USER);
    }
    BTM_TRACE_ERROR("%s: peer %s tBTM_SEC_DEV:0x%x rs_disc_pending=%d",
                    __func__,
                    (p_bda != nullptr) ? p_bda->ToString().c_str() : "nullptr",
                    PTR_TO_UINT(p_dev_rec), p_dev_rec->rs_disc_pending);
    BTM_TRACE_ERROR("%s: peer %s rs_disc_pending=%d", __func__,
                    bd_addr.ToString().c_str(), p_dev_rec->rs_disc_pending);
    p_dev_rec->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
  }

#endif
}

+2 −2
Original line number Diff line number Diff line
@@ -1400,7 +1400,7 @@ static void btu_hcif_hdl_command_status(uint16_t opcode, uint8_t status,
      if (status != HCI_SUCCESS) {
        // Tell BTM that the command failed
        STREAM_TO_BDADDR(bd_addr, p_cmd);
        btm_acl_role_changed(status, &bd_addr, HCI_ROLE_UNKNOWN);
        btm_acl_role_changed(status, bd_addr, HCI_ROLE_UNKNOWN);
        l2c_link_role_changed(nullptr, HCI_ROLE_UNKNOWN,
                              HCI_ERR_COMMAND_DISALLOWED);
      }
@@ -1560,7 +1560,7 @@ static void btu_hcif_role_change_evt(uint8_t* p) {

  btm_blacklist_role_change_device(bda, status);
  l2c_link_role_changed(&bda, role, status);
  btm_acl_role_changed(status, &bda, role);
  btm_acl_role_changed(status, bda, role);
}

/*******************************************************************************
+1 −1
Original line number Diff line number Diff line
@@ -221,7 +221,7 @@ uint16_t BTM_GetNumAclLinks(void);
 * Returns          void
 *
 ******************************************************************************/
void btm_acl_role_changed(uint8_t hci_status, const RawAddress* bd_addr,
void btm_acl_role_changed(uint8_t hci_status, const RawAddress& bd_addr,
                          uint8_t new_role);

void btm_set_packet_types_from_address(const RawAddress& bda,