From: Hansong Zhang Date: Sun, 27 Sep 2020 23:49:17 +0000 (-0700) Subject: L2cap: Auto send ConnectRsp X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=d8d7224c2e1451b0dba85a64007366149253ce00;p=android-x86%2Fsystem-bt.git L2cap: Auto send ConnectRsp 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 --- diff --git a/stack/avct/avct_l2c.cc b/stack/avct/avct_l2c.cc index adac96135..1c910a4c5 100644 --- a/stack/avct/avct_l2c.cc +++ b/stack/avct/avct_l2c.cc @@ -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) { diff --git a/stack/avct/avct_l2c_br.cc b/stack/avct/avct_l2c_br.cc index 7415c11a8..7833bcfde 100644 --- a/stack/avct/avct_l2c_br.cc +++ b/stack/avct/avct_l2c_br.cc @@ -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) { diff --git a/stack/avdt/avdt_l2c.cc b/stack/avdt/avdt_l2c.cc index f1d3515c7..3a6550aab 100644 --- a/stack/avdt/avdt_l2c.cc +++ b/stack/avdt/avdt_l2c.cc @@ -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) { diff --git a/stack/bnep/bnep_main.cc b/stack/bnep/bnep_main.cc index 1a25aab56..da5aea390 100644 --- a/stack/bnep/bnep_main.cc +++ b/stack/bnep/bnep_main.cc @@ -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); diff --git a/stack/gap/gap_conn.cc b/stack/gap/gap_conn.cc index d3e49aa46..fd75bc5f9 100644 --- a/stack/gap/gap_conn.cc +++ b/stack/gap/gap_conn.cc @@ -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); diff --git a/stack/gatt/gatt_main.cc b/stack/gatt/gatt_main.cc index aac3d2432..84e368416 100644 --- a/stack/gatt/gatt_main.cc +++ b/stack/gatt/gatt_main.cc @@ -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); diff --git a/stack/hid/hidd_conn.cc b/stack/hid/hidd_conn.cc index ed4f4cf02..9c196ab53 100644 --- a/stack/hid/hidd_conn.cc +++ b/stack/hid/hidd_conn.cc @@ -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) { diff --git a/stack/hid/hidh_conn.cc b/stack/hid/hidh_conn.cc index 794d04ced..6fb276e1e 100644 --- a/stack/hid/hidh_conn.cc +++ b/stack/hid/hidh_conn.cc @@ -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); diff --git a/stack/l2cap/l2c_csm.cc b/stack/l2cap/l2c_csm.cc index 7c2224337..809f9041d 100644 --- a/stack/l2cap/l2c_csm.cc +++ b/stack/l2cap/l2c_csm.cc @@ -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); diff --git a/stack/rfcomm/rfc_l2cap_if.cc b/stack/rfcomm/rfc_l2cap_if.cc index a3f0b6945..c547eb5f4 100644 --- a/stack/rfcomm/rfc_l2cap_if.cc +++ b/stack/rfcomm/rfc_l2cap_if.cc @@ -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; } diff --git a/stack/rfcomm/rfc_mx_fsm.cc b/stack/rfcomm/rfc_mx_fsm.cc index 511e590b2..60b0412dc 100644 --- a/stack/rfcomm/rfc_mx_fsm.cc +++ b/stack/rfcomm/rfc_mx_fsm.cc @@ -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); diff --git a/stack/sdp/sdp_main.cc b/stack/sdp/sdp_main.cc index ec49e4f23..1646724fd 100644 --- a/stack/sdp/sdp_main.cc +++ b/stack/sdp/sdp_main.cc @@ -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) {