OSDN Git Service

L2cap: Auto send ConnectRsp
authorHansong Zhang <hsz@google.com>
Sun, 27 Sep 2020 23:49:17 +0000 (16:49 -0700)
committerHansong Zhang <hsz@google.com>
Mon, 28 Sep 2020 18:55:20 +0000 (11:55 -0700)
In the past, L2cap user can decide whether to accept or reject a
ConnectReq. If reject, the channel is closed.

Now let L2cap layer always auto accept the remote ConnectReq and start
config process. If the L2cap user wants to reject, they can send a
DisconnectReq. According to L2cap state machine, a DisconnectReq after
ConnectRsp terminates the channel, which is equivalent to sending a
negative ConnectRsp, as before.

Bug: 159815595
Tag: #refactor
Test: compile & verify basic functions working
Change-Id: Ibed56206148922356e8d9cb10650e9d82ab61225

12 files changed:
stack/avct/avct_l2c.cc
stack/avct/avct_l2c_br.cc
stack/avdt/avdt_l2c.cc
stack/bnep/bnep_main.cc
stack/gap/gap_conn.cc
stack/gatt/gatt_main.cc
stack/hid/hidd_conn.cc
stack/hid/hidh_conn.cc
stack/l2cap/l2c_csm.cc
stack/rfcomm/rfc_l2cap_if.cc
stack/rfcomm/rfc_mx_fsm.cc
stack/sdp/sdp_main.cc

index adac961..1c910a4 100644 (file)
@@ -120,8 +120,11 @@ void avct_l2c_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
     AVCT_TRACE_DEBUG("avct_l2c_connect_ind_cback: 0x%x, res: %d, ch_state: %d",
                      lcid, result, p_lcb->ch_state);
   }
-  /* Send L2CAP connect rsp */
-  L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
+
+  /* If we reject the connection, send DisconnectReq */
+  if (result != L2CAP_CONN_OK) {
+    L2CA_DisconnectReq(lcid);
+  }
 
   /* if result ok, proceed with connection */
   if (result == L2CAP_CONN_OK) {
index 7415c11..7833bcf 100644 (file)
@@ -88,8 +88,10 @@ void avct_l2c_br_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
   /* Set the FCR options: Browsing channel mandates ERTM */
   ertm_info.preferred_mode = L2CAP_FCR_ERTM_MODE;
 
-  /* Send L2CAP connect rsp */
-  L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
+  /* If we reject the connection, send DisconnectReq */
+  if (result != L2CAP_CONN_OK) {
+    L2CA_DisconnectReq(lcid);
+  }
 
   /* if result ok, proceed with connection */
   if (result == L2CAP_CONN_OK) {
index f1d3515..3a6550a 100644 (file)
@@ -74,10 +74,6 @@ static void avdt_sec_check_complete_term(const RawAddress* bd_addr,
   p_tbl = avdt_ad_tc_tbl_by_st(AVDT_CHAN_SIG, p_ccb, AVDT_AD_ST_SEC_ACP);
   if (p_tbl == NULL) return;
 
-  /* Send response to the L2CAP layer. */
-  L2CA_ConnectRsp(*bd_addr, p_tbl->id, p_tbl->lcid, L2CAP_CONN_OK,
-                  L2CAP_CONN_OK);
-
   /* store idx in LCID table, store LCID in routing table */
   avdtp_cb.ad.lcid_tbl[p_tbl->lcid - L2CAP_BASE_APPL_CID] =
       avdt_ad_tc_tbl_to_idx(p_tbl);
@@ -199,19 +195,20 @@ void avdt_l2c_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
     }
   }
 
-  /* Send L2CAP connect rsp */
-  L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
+  /* If we reject the connection, send DisconnectReq */
+  if (result != L2CAP_CONN_OK) {
+    L2CA_DisconnectReq(lcid);
+    return;
+  }
 
   /* if result ok, proceed with connection */
-  if (result == L2CAP_CONN_OK) {
-    /* store idx in LCID table, store LCID in routing table */
-    avdtp_cb.ad.lcid_tbl[lcid - L2CAP_BASE_APPL_CID] =
-        avdt_ad_tc_tbl_to_idx(p_tbl);
-    avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][p_tbl->tcid].lcid = lcid;
+  /* store idx in LCID table, store LCID in routing table */
+  avdtp_cb.ad.lcid_tbl[lcid - L2CAP_BASE_APPL_CID] =
+      avdt_ad_tc_tbl_to_idx(p_tbl);
+  avdtp_cb.ad.rt_tbl[avdt_ccb_to_idx(p_ccb)][p_tbl->tcid].lcid = lcid;
 
-    /* transition to configuration state */
-    p_tbl->state = AVDT_AD_ST_CFG;
-  }
+  /* transition to configuration state */
+  p_tbl->state = AVDT_AD_ST_CFG;
 }
 
 static void avdt_on_l2cap_error(uint16_t lcid, uint16_t result) {
index 1a25aab..da5aea3 100644 (file)
@@ -115,7 +115,7 @@ static void bnep_connect_ind(const RawAddress& bd_addr, uint16_t l2cap_cid,
   /* no more resources to handle the connection, reject the connection.    */
   if (!(bnep_cb.profile_registered) || (p_bcb) ||
       ((p_bcb = bnepu_allocate_bcb(bd_addr)) == NULL)) {
-    L2CA_ConnectRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_NO_PSM, 0);
+    L2CA_DisconnectReq(l2cap_cid);
     return;
   }
 
@@ -125,9 +125,6 @@ static void bnep_connect_ind(const RawAddress& bd_addr, uint16_t l2cap_cid,
   /* Save the L2CAP Channel ID. */
   p_bcb->l2cap_cid = l2cap_cid;
 
-  /* Send response to the L2CAP layer. */
-  L2CA_ConnectRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
-
   /* Start timer waiting for config setup */
   alarm_set_on_mloop(p_bcb->conn_timer, BNEP_CONN_TIMEOUT_MS,
                      bnep_conn_timer_timeout, p_bcb);
index d3e49aa..fd75bc5 100644 (file)
@@ -615,10 +615,6 @@ static void gap_connect_ind(const RawAddress& bd_addr, uint16_t l2cap_cid,
   p_ccb->rem_dev_address = bd_addr;
   p_ccb->connection_id = l2cap_cid;
 
-  /* Send response to the L2CAP layer. */
-  if (p_ccb->transport == BT_TRANSPORT_BR_EDR)
-    L2CA_ConnectRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
-
   if (p_ccb->transport == BT_TRANSPORT_LE) {
     L2CA_ConnectLECocRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK,
                          L2CAP_CONN_OK, &p_ccb->local_coc_cfg);
index aac3d24..84e3684 100644 (file)
@@ -575,11 +575,11 @@ static void gatt_l2cif_connect_ind_cback(const RawAddress& bd_addr,
     result = L2CAP_CONN_NO_RESOURCES;
   }
 
-  /* Send L2CAP connect rsp */
-  L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
-
-  /* if result ok, proceed with connection */
-  if (result != L2CAP_CONN_OK) return;
+  /* If we reject the connection, send DisconnectReq */
+  if (result != L2CAP_CONN_OK) {
+    L2CA_DisconnectReq(lcid);
+    return;
+  }
 
   /* transition to configuration state */
   gatt_set_ch_state(p_tcb, GATT_CH_CFG);
index ed4f4cf..9c196ab 100644 (file)
@@ -108,7 +108,8 @@ static void hidd_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t cid,
   if (!hd_cb.allow_incoming) {
     HIDD_TRACE_WARNING("%s: incoming connections not allowed, rejecting",
                        __func__);
-    L2CA_ConnectRsp(bd_addr, id, cid, L2CAP_CONN_NO_RESOURCES, 0);
+    L2CA_DisconnectReq(cid);
+
     return;
   }
 
@@ -146,7 +147,7 @@ static void hidd_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t cid,
   }
 
   if (!accept) {
-    L2CA_ConnectRsp(bd_addr, id, cid, L2CAP_CONN_NO_RESOURCES, 0);
+    L2CA_DisconnectReq(cid);
     return;
   }
 
@@ -163,17 +164,12 @@ static void hidd_l2cif_connect_ind(const RawAddress& bd_addr, uint16_t cid,
     p_hcon->ctrl_id = id;
     p_hcon->disc_reason = HID_SUCCESS;
     p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
-
-    L2CA_ConnectRsp(p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid,
-                    L2CAP_CONN_OK, L2CAP_CONN_OK);
     return;
   }
 
   // for INTR we go directly to config state
   p_hcon->conn_state = HID_CONN_STATE_CONFIG;
   p_hcon->intr_cid = cid;
-
-  L2CA_ConnectRsp(bd_addr, id, cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
 }
 
 static void hidd_on_l2cap_error(uint16_t lcid, uint16_t result) {
index 794d04c..6fb276e 100644 (file)
@@ -159,7 +159,7 @@ static void hidh_l2cif_connect_ind(const RawAddress& bd_addr,
 
   /* always add incoming connection device into HID database by default */
   if (HID_HostAddDev(bd_addr, HID_SEC_REQUIRED, &i) != HID_SUCCESS) {
-    L2CA_ConnectRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_SECURITY_BLOCK, 0);
+    L2CA_DisconnectReq(l2cap_cid);
     return;
   }
 
@@ -193,7 +193,7 @@ static void hidh_l2cif_connect_ind(const RawAddress& bd_addr,
   }
 
   if (!bAccept) {
-    L2CA_ConnectRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_NO_RESOURCES, 0);
+    L2CA_DisconnectReq(l2cap_cid);
     return;
   }
 
@@ -205,10 +205,6 @@ static void hidh_l2cif_connect_ind(const RawAddress& bd_addr,
                                               disc_reason (from
                                               HID_ERR_AUTH_FAILED) */
     p_hcon->conn_state = HID_CONN_STATE_CONNECTING_INTR;
-
-    /* Send response to the L2CAP layer. */
-    L2CA_ConnectRsp(p_dev->addr, p_dev->conn.ctrl_id, p_dev->conn.ctrl_cid,
-                    L2CAP_CONN_OK, L2CAP_CONN_OK);
     return;
   }
 
@@ -216,9 +212,6 @@ static void hidh_l2cif_connect_ind(const RawAddress& bd_addr,
   p_hcon->conn_state = HID_CONN_STATE_CONFIG;
   p_hcon->intr_cid = l2cap_cid;
 
-  /* Send response to the L2CAP layer. */
-  L2CA_ConnectRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
-
   HIDH_TRACE_EVENT(
       "HID-Host Rcvd L2CAP conn ind, sent config req, PSM: 0x%04x  CID 0x%x",
       psm, l2cap_cid);
index 7c22243..809f904 100644 (file)
@@ -54,6 +54,11 @@ static void l2c_csm_w4_l2ca_disconnect_rsp(tL2C_CCB* p_ccb, uint16_t event,
 
 static const char* l2c_csm_get_event_name(uint16_t event);
 
+// Send a connect response with result OK and adjust the state machine
+static void l2c_csm_send_connect_rsp(tL2C_CCB* p_ccb) {
+  l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
+}
+
 // Send a config request and adjust the state machine
 static void l2c_csm_send_config_req(tL2C_CCB* p_ccb) {
   tL2CAP_CFG_INFO config{};
@@ -431,6 +436,7 @@ static void l2c_csm_term_w4_sec_comp(tL2C_CCB* p_ccb, uint16_t event,
         L2CAP_TRACE_API("L2CAP - Calling Connect_Ind_Cb(), CID: 0x%04x",
                         p_ccb->local_cid);
 
+        l2c_csm_send_connect_rsp(p_ccb);
         (*p_ccb->p_rcb->api.pL2CA_ConnectInd_Cb)(
             p_ccb->p_lcb->remote_bd_addr, p_ccb->local_cid, p_ccb->p_rcb->psm,
             p_ccb->remote_id);
@@ -708,6 +714,7 @@ static void l2c_csm_w4_l2ca_connect_rsp(tL2C_CCB* p_ccb, uint16_t event,
       L2CAP_TRACE_API("L2CAP - Calling Connect_Ind_Cb(), CID: 0x%04x",
                       p_ccb->local_cid);
 
+      l2c_csm_send_connect_rsp(p_ccb);
       (*p_ccb->p_rcb->api.pL2CA_ConnectInd_Cb)(
           p_ccb->p_lcb->remote_bd_addr, p_ccb->local_cid, p_ccb->p_rcb->psm,
           p_ccb->remote_id);
index a3f0b69..c547eb5 100644 (file)
@@ -119,7 +119,7 @@ void RFCOMM_ConnectInd(const RawAddress& bd_addr, uint16_t lcid,
   }
 
   if (p_mcb == nullptr) {
-    L2CA_ConnectRsp(bd_addr, id, lcid, L2CAP_CONN_NO_RESOURCES, 0);
+    L2CA_DisconnectReq(lcid);
     return;
   }
   p_mcb->lcid = lcid;
@@ -155,8 +155,7 @@ void RFCOMM_ConnectCnf(uint16_t lcid, uint16_t result) {
 
       /* Peer gave up its connection request, make sure cleaning up L2CAP
        * channel */
-      L2CA_ConnectRsp(p_mcb->bd_addr, p_mcb->pending_id, p_mcb->pending_lcid,
-                      L2CAP_CONN_NO_RESOURCES, 0);
+      L2CA_DisconnectReq(p_mcb->pending_lcid);
 
       p_mcb->pending_lcid = 0;
     }
index 511e590..60b0412 100644 (file)
@@ -141,8 +141,6 @@ void rfc_mx_sm_state_idle(tRFC_MCB* p_mcb, uint16_t event, void* p_data) {
     case RFC_MX_EVENT_CONN_IND:
 
       rfc_timer_start(p_mcb, RFCOMM_CONN_TIMEOUT);
-      L2CA_ConnectRsp(p_mcb->bd_addr, *((uint8_t*)p_data), p_mcb->lcid,
-                      L2CAP_CONN_OK, 0);
 
       rfc_mx_send_config_req(p_mcb);
 
index ec49e4f..1646724 100644 (file)
@@ -124,9 +124,6 @@ static void sdp_connect_ind(const RawAddress& bd_addr, uint16_t l2cap_cid,
   /* Save the BD Address and Channel ID. */
   p_ccb->device_address = bd_addr;
   p_ccb->connection_id = l2cap_cid;
-
-  /* Send response to the L2CAP layer. */
-  L2CA_ConnectRsp(bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
 }
 
 static void sdp_on_l2cap_error(uint16_t l2cap_cid, uint16_t result) {