OSDN Git Service

Streamline stack/acl/btm_acl::btm_acl_created
authorChris Manton <cmanton@google.com>
Tue, 22 Sep 2020 21:22:30 +0000 (14:22 -0700)
committerChris Manton <cmanton@google.com>
Wed, 23 Sep 2020 20:54:11 +0000 (13:54 -0700)
Towards readable code

Bug: 163134718
Tag: #refactor
Test: acts -tc BleCocTest
Test: ble paired 2 phones
Change-Id: I0b3f15344dc9b7b19f810a39a98e0dc6d46d520b

stack/acl/btm_acl.cc

index 79133ee..5b4b8bd 100644 (file)
@@ -351,8 +351,6 @@ tACL_CONN* StackAclBtmAcl::acl_allocate_connection() {
 
 void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
                      uint8_t link_role, tBT_TRANSPORT transport) {
-  tBTM_SEC_DEV_REC* p_dev_rec = NULL;
-  uint8_t xx;
 
   tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, transport);
   if (p_acl != (tACL_CONN*)NULL) {
@@ -372,76 +370,89 @@ void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
             RoleText(link_role).c_str(), BtTransportText(transport).c_str());
 
   /* Allocate acl_db entry */
+  uint8_t xx = 0;
   for (xx = 0, p_acl = &btm_cb.acl_cb_.acl_db[0]; xx < MAX_L2CAP_LINKS;
        xx++, p_acl++) {
     if (!p_acl->in_use) {
-      p_acl->in_use = true;
-      p_acl->hci_handle = hci_handle;
-      p_acl->link_role = link_role;
-      p_acl->link_up_issued = false;
-      p_acl->remote_addr = bda;
-      p_acl->sca = 0xFF;
-      btm_set_link_policy(p_acl, btm_cb.acl_cb_.btm_def_link_policy);
-
-      p_acl->transport = transport;
-      if (transport == BT_TRANSPORT_LE)
-        btm_ble_refresh_local_resolvable_private_addr(
-            bda, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
-      p_acl->switch_role_failed_attempts = 0;
-      p_acl->reset_switch_role();
-
-      tBTM_PM_MCB* p_db = &btm_cb.acl_cb_.pm_mode_db[xx]; /* per ACL link */
-      memset(p_db, 0, sizeof(tBTM_PM_MCB));
-      p_db->state = BTM_PM_ST_ACTIVE;
-
-      /* if BR/EDR do something more */
-      if (transport == BT_TRANSPORT_BR_EDR) {
-        btsnd_hcic_read_rmt_clk_offset(p_acl->hci_handle);
-        btsnd_hcic_rmt_ver_req(p_acl->hci_handle);
-      }
-      p_dev_rec = btm_find_dev_by_handle(hci_handle);
+      break;
+    }
+  }
 
-      if (p_dev_rec && !(transport == BT_TRANSPORT_LE)) {
-        /* If remote features already known, copy them and continue connection
-         * setup */
-        if ((p_dev_rec->num_read_pages) &&
-            (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1))) {
-          memcpy(p_acl->peer_lmp_feature_pages, p_dev_rec->feature_pages,
-                 (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
-          p_acl->num_read_pages = p_dev_rec->num_read_pages;
+  if (xx == MAX_L2CAP_LINKS) {
+    LOG_ERROR("Unable to allocate acl resources handle:%hu", hci_handle);
+    return;
+  }
 
-          const uint8_t req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
+  p_acl->in_use = true;
+  p_acl->hci_handle = hci_handle;
+  p_acl->link_role = link_role;
+  p_acl->link_up_issued = false;
+  p_acl->remote_addr = bda;
+  p_acl->sca = 0xFF;
+  btm_set_link_policy(p_acl, btm_cb.acl_cb_.btm_def_link_policy);
 
-          /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
-          btm_sec_set_peer_sec_caps(p_acl, p_dev_rec);
+  p_acl->transport = transport;
+  if (transport == BT_TRANSPORT_LE) {
+    btm_ble_refresh_local_resolvable_private_addr(
+        bda, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
+  }
+  p_acl->switch_role_failed_attempts = 0;
+  p_acl->reset_switch_role();
 
-          if (req_pend) {
-            /* Request for remaining Security Features (if any) */
-            l2cu_resubmit_pending_sec_req(&p_dev_rec->bd_addr);
-          }
-          internal_.btm_establish_continue(p_acl);
-          return;
-        }
-      }
+  tBTM_PM_MCB* p_db = &btm_cb.acl_cb_.pm_mode_db[xx]; /* per ACL link */
+  memset(p_db, 0, sizeof(tBTM_PM_MCB));
+  p_db->state = BTM_PM_ST_ACTIVE;
 
-      /* If here, features are not known yet */
-      if (p_dev_rec && transport == BT_TRANSPORT_LE) {
-        btm_ble_get_acl_remote_addr(p_dev_rec, p_acl->active_remote_addr,
-                                    &p_acl->active_remote_addr_type);
-
-        if (controller_get_interface()
-                ->supports_ble_peripheral_initiated_feature_exchange() ||
-            link_role == HCI_ROLE_MASTER) {
-          btsnd_hcic_ble_read_remote_feat(p_acl->hci_handle);
-        } else {
-          internal_.btm_establish_continue(p_acl);
-        }
-      }
+  /* if BR/EDR do something more */
+  if (transport == BT_TRANSPORT_BR_EDR) {
+    btsnd_hcic_read_rmt_clk_offset(hci_handle);
+    btsnd_hcic_rmt_ver_req(hci_handle);
+  }
+
+  tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(hci_handle);
+  if (p_dev_rec == nullptr) {
+    LOG_ERROR("Unable to find security device record hci_handle:%hu",
+              hci_handle);
+    // TODO Release acl resource
+    return;
+  }
+
+  if (transport != BT_TRANSPORT_LE) {
+    /* If remote features already known, copy them and continue connection
+     * setup */
+    if ((p_dev_rec->num_read_pages) &&
+        (p_dev_rec->num_read_pages <= (HCI_EXT_FEATURES_PAGE_MAX + 1))) {
+      memcpy(p_acl->peer_lmp_feature_pages, p_dev_rec->feature_pages,
+             (HCI_FEATURE_BYTES_PER_PAGE * p_dev_rec->num_read_pages));
+      // TODO We do not need to store the pages read here
+      p_acl->num_read_pages = p_dev_rec->num_read_pages;
+
+      const uint8_t req_pend = (p_dev_rec->sm4 & BTM_SM4_REQ_PEND);
 
-      /* read page 1 - on rmt feature event for buffer reasons */
+      /* Store the Peer Security Capabilites (in SM4 and rmt_sec_caps) */
+      btm_sec_set_peer_sec_caps(p_acl, p_dev_rec);
+
+      if (req_pend) {
+        /* Request for remaining Security Features (if any) */
+        l2cu_resubmit_pending_sec_req(&p_dev_rec->bd_addr);
+      }
+      internal_.btm_establish_continue(p_acl);
       return;
     }
   }
+
+  if (transport == BT_TRANSPORT_LE) {
+    btm_ble_get_acl_remote_addr(p_dev_rec, p_acl->active_remote_addr,
+                                &p_acl->active_remote_addr_type);
+
+    if (controller_get_interface()
+            ->supports_ble_peripheral_initiated_feature_exchange() ||
+        link_role == HCI_ROLE_MASTER) {
+      btsnd_hcic_ble_read_remote_feat(p_acl->hci_handle);
+    } else {
+      internal_.btm_establish_continue(p_acl);
+    }
+  }
 }
 
 void btm_acl_update_conn_addr(uint16_t conn_handle, const RawAddress& address) {