OSDN Git Service

RFCOMM: Use std map to store channel map
authorHansong Zhang <hsz@google.com>
Thu, 4 Feb 2021 00:59:39 +0000 (16:59 -0800)
committerHansong Zhang <hsz@google.com>
Thu, 4 Feb 2021 00:59:39 +0000 (16:59 -0800)
Instead of using legacy fixed size array

Test: Speaker
Bug: 179117950
Tag: #stability
Change-Id: Ibc8a2e947f218a6d5870cc7a9566306ddc7eb2b3

stack/rfcomm/port_api.cc
stack/rfcomm/rfc_int.h
stack/rfcomm/rfc_l2cap_if.cc
stack/rfcomm/rfc_port_if.cc

index c0bed2e..a0b9c9c 100644 (file)
@@ -1110,6 +1110,7 @@ int PORT_WriteData(uint16_t handle, const char* p_data, uint16_t max_len,
 void RFCOMM_Init(void) {
   memset(&rfc_cb, 0, sizeof(tRFC_CB)); /* Init RFCOMM control block */
   rfcomm_security_records = {};
+  rfc_lcid_mcb = {};
 
   rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS;
 
index 28583e6..3c4dedf 100644 (file)
@@ -235,8 +235,6 @@ typedef struct {
   MX_FRAME rx_frame;
   tL2CAP_APPL_INFO reg_info; /* L2CAP Registration info */
 
-  /* MCB based on the L2CAP's lcid */
-  tRFC_MCB* p_rfc_lcid_mcb[MAX_L2CAP_CHANNELS];
   bool peer_rx_disabled; /* If true peer sent FCOFF */
   uint8_t last_mux;      /* Last mux allocated */
   uint8_t last_port_index;  // Index of last port allocated in rfc_cb.port
@@ -254,6 +252,9 @@ extern tRFC_CB rfc_cb;
 extern std::unordered_map<uint32_t /* scn */, uint16_t /* sec_mask */>
     rfcomm_security_records;
 
+/* MCB based on the L2CAP's lcid */
+extern std::unordered_map<uint16_t /* cid */, tRFC_MCB*> rfc_lcid_mcb;
+
 /* Timer running on the multiplexor channel while no DLCI connection is open */
 #define RFC_MCB_INIT_INACT_TIMER 60 /* in seconds */
 
index 463f95d..689c0aa 100644 (file)
@@ -357,20 +357,15 @@ void RFCOMM_CongestionStatusInd(uint16_t lcid, bool is_congested) {
  *
  ******************************************************************************/
 tRFC_MCB* rfc_find_lcid_mcb(uint16_t lcid) {
-  if (lcid - L2CAP_BASE_APPL_CID >= MAX_L2CAP_CHANNELS) {
-    RFCOMM_TRACE_ERROR("rfc_find_lcid_mcb LCID:0x%x", lcid);
-    return nullptr;
-  } else {
-    tRFC_MCB* p_mcb = rfc_cb.rfc.p_rfc_lcid_mcb[lcid - L2CAP_BASE_APPL_CID];
-    if (p_mcb != nullptr) {
-      if (p_mcb->lcid != lcid) {
-        LOG(WARNING) << __func__ << "LCID reused lcid=:" << loghex(lcid)
-                     << ", current_lcid=" << loghex(p_mcb->lcid);
-        return nullptr;
-      }
+  tRFC_MCB* p_mcb = rfc_lcid_mcb[lcid];
+  if (p_mcb != nullptr) {
+    if (p_mcb->lcid != lcid) {
+      LOG(WARNING) << __func__ << "LCID reused lcid=:" << loghex(lcid)
+                   << ", current_lcid=" << loghex(p_mcb->lcid);
+      return nullptr;
     }
-    return p_mcb;
   }
+  return p_mcb;
 }
 
 /*******************************************************************************
@@ -381,14 +376,6 @@ tRFC_MCB* rfc_find_lcid_mcb(uint16_t lcid) {
  *
  ******************************************************************************/
 void rfc_save_lcid_mcb(tRFC_MCB* p_mcb, uint16_t lcid) {
-  if (lcid < L2CAP_BASE_APPL_CID) {
-    LOG(ERROR) << __func__ << ": LCID " << lcid << " is too small";
-    return;
-  }
-  auto mcb_index = static_cast<size_t>(lcid - L2CAP_BASE_APPL_CID);
-  if (mcb_index >= MAX_L2CAP_CHANNELS) {
-    LOG(ERROR) << __func__ << ": LCID " << lcid << " is too large";
-    return;
-  }
-  rfc_cb.rfc.p_rfc_lcid_mcb[mcb_index] = p_mcb;
+  auto mcb_index = static_cast<size_t>(lcid);
+  rfc_lcid_mcb[mcb_index] = p_mcb;
 }
index 2abd32c..0b454b4 100644 (file)
@@ -36,6 +36,7 @@
 
 tRFC_CB rfc_cb;
 std::unordered_map<uint32_t, uint16_t> rfcomm_security_records;
+std::unordered_map<uint16_t /* sci */, tRFC_MCB*> rfc_lcid_mcb;
 
 /*******************************************************************************
  *