From ee43fedd2cd6d75c35c2a689d63fca3d4540a467 Mon Sep 17 00:00:00 2001 From: Chris Manton Date: Sun, 23 Aug 2020 22:42:49 -0700 Subject: [PATCH] Encapsulate tL2C_CB::tL2C_LCB::handle Towards readable code Bug: 163134718 Tag: #refactor Test: compile & verify basic functions working Change-Id: Iad937e362df30804c507959b057b3fda6d6dd37b --- hci/src/btsnoop.cc | 2 +- stack/l2cap/l2c_api.cc | 2 +- stack/l2cap/l2c_ble.cc | 10 +++++----- stack/l2cap/l2c_csm.cc | 16 ++++++++-------- stack/l2cap/l2c_int.h | 14 +++++++++++++- stack/l2cap/l2c_link.cc | 20 ++++++++++---------- stack/l2cap/l2c_main.cc | 10 +++++----- stack/l2cap/l2c_utils.cc | 38 +++++++++++++++++++------------------- 8 files changed, 62 insertions(+), 50 deletions(-) diff --git a/hci/src/btsnoop.cc b/hci/src/btsnoop.cc index 9f7685599..355ed4a63 100644 --- a/hci/src/btsnoop.cc +++ b/hci/src/btsnoop.cc @@ -315,7 +315,7 @@ static void whitelist_rfc_dlci(uint16_t local_cid, uint8_t dlci) { std::lock_guard lock(filter_list_mutex); tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(nullptr, local_cid); - filter_list[p_ccb->p_lcb->handle].addRfcDlci(dlci); + filter_list[p_ccb->p_lcb->Handle()].addRfcDlci(dlci); } static void add_rfc_l2c_channel(uint16_t conn_handle, uint16_t local_cid, diff --git a/stack/l2cap/l2c_api.cc b/stack/l2cap/l2c_api.cc index f16fbbd0f..cd61bce4f 100644 --- a/stack/l2cap/l2c_api.cc +++ b/stack/l2cap/l2c_api.cc @@ -1792,7 +1792,7 @@ uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush) { if (controller->supports_non_flushable_pb() && (BTM_GetNumScoLinks() == 0)) { /* The only packet type defined - 0 - Automatically-Flushable Only */ - btsnd_hcic_enhanced_flush(p_lcb->handle, 0); + btsnd_hcic_enhanced_flush(p_lcb->Handle(), 0); } } diff --git a/stack/l2cap/l2c_ble.cc b/stack/l2cap/l2c_ble.cc index e3eb2f8f3..3d7039a18 100644 --- a/stack/l2cap/l2c_ble.cc +++ b/stack/l2cap/l2c_ble.cc @@ -297,7 +297,7 @@ void l2cble_conn_comp(uint16_t handle, uint8_t role, const RawAddress& bda, if (role == HCI_ROLE_MASTER) alarm_cancel(p_lcb->l2c_lcb_timer); /* Save the handle */ - p_lcb->handle = handle; + p_lcb->SetHandle(handle); /* Connected OK. Change state to connected, we were scanning so we are master */ @@ -384,7 +384,7 @@ static void l2cble_start_conn_update(tL2C_LCB* p_lcb) { p_lcb->remote_bd_addr)) #endif ) { - btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, + btsnd_hcic_ble_upd_ll_conn_params(p_lcb->Handle(), min_conn_int, max_conn_int, slave_latency, supervision_tout, 0, 0); p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING; @@ -407,7 +407,7 @@ static void l2cble_start_conn_update(tL2C_LCB* p_lcb) { p_lcb->remote_bd_addr)) #endif ) { - btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->min_interval, + btsnd_hcic_ble_upd_ll_conn_params(p_lcb->Handle(), p_lcb->min_interval, p_lcb->max_interval, p_lcb->latency, p_lcb->timeout, p_lcb->min_ce_len, p_lcb->max_ce_len); @@ -1317,7 +1317,7 @@ void l2cble_use_preferred_conn_params(const RawAddress& bda) { BTM_TRACE_DEBUG( "%s: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d " "supervision_tout=%d", - __func__, p_lcb->handle, p_dev_rec->conn_params.min_conn_int, + __func__, p_lcb->Handle(), p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int, p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout); @@ -1328,7 +1328,7 @@ void l2cble_use_preferred_conn_params(const RawAddress& bda) { p_lcb->latency = p_dev_rec->conn_params.slave_latency; btsnd_hcic_ble_upd_ll_conn_params( - p_lcb->handle, p_dev_rec->conn_params.min_conn_int, + p_lcb->Handle(), p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int, p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout, 0, 0); diff --git a/stack/l2cap/l2c_csm.cc b/stack/l2cap/l2c_csm.cc index 0aa0af802..d40c342d6 100644 --- a/stack/l2cap/l2c_csm.cc +++ b/stack/l2cap/l2c_csm.cc @@ -161,8 +161,8 @@ static void l2c_csm_closed(tL2C_CCB* p_ccb, uint16_t event, void* p_data) { } else { p_ccb->chnl_state = CST_ORIG_W4_SEC_COMP; btm_sec_l2cap_access_req(p_ccb->p_lcb->remote_bd_addr, - p_ccb->p_rcb->psm, p_ccb->p_lcb->handle, true, - &l2c_link_sec_comp, p_ccb); + p_ccb->p_rcb->psm, p_ccb->p_lcb->Handle(), + true, &l2c_link_sec_comp, p_ccb); } break; @@ -195,7 +195,7 @@ static void l2c_csm_closed(tL2C_CCB* p_ccb, uint16_t event, void* p_data) { /* If sec access does not result in started SEC_COM or COMP_NEG are * already processed */ if (btm_sec_l2cap_access_req(p_ccb->p_lcb->remote_bd_addr, - p_ccb->p_rcb->psm, p_ccb->p_lcb->handle, + p_ccb->p_rcb->psm, p_ccb->p_lcb->Handle(), true, &l2c_link_sec_comp, p_ccb) == BTM_CMD_STARTED) { p_ccb->chnl_state = CST_ORIG_W4_SEC_COMP; @@ -264,7 +264,7 @@ static void l2c_csm_closed(tL2C_CCB* p_ccb, uint16_t event, void* p_data) { p_ccb->chnl_state = CST_TERM_W4_SEC_COMP; if (btm_sec_l2cap_access_req(p_ccb->p_lcb->remote_bd_addr, - p_ccb->p_rcb->psm, p_ccb->p_lcb->handle, + p_ccb->p_rcb->psm, p_ccb->p_lcb->Handle(), false, &l2c_link_sec_comp, p_ccb) == BTM_CMD_STARTED) { /* started the security process, tell the peer to set a longer timer @@ -332,8 +332,8 @@ static void l2c_csm_orig_w4_sec_comp(tL2C_CCB* p_ccb, uint16_t event, false, &l2c_link_sec_comp2, p_ccb); } else { btm_sec_l2cap_access_req(p_ccb->p_lcb->remote_bd_addr, - p_ccb->p_rcb->psm, p_ccb->p_lcb->handle, true, - &l2c_link_sec_comp, p_ccb); + p_ccb->p_rcb->psm, p_ccb->p_lcb->Handle(), + true, &l2c_link_sec_comp, p_ccb); } break; @@ -489,8 +489,8 @@ static void l2c_csm_term_w4_sec_comp(tL2C_CCB* p_ccb, uint16_t event, case L2CEVT_SEC_RE_SEND_CMD: /* BTM has enough info to proceed */ btm_sec_l2cap_access_req(p_ccb->p_lcb->remote_bd_addr, p_ccb->p_rcb->psm, - p_ccb->p_lcb->handle, false, &l2c_link_sec_comp, - p_ccb); + p_ccb->p_lcb->Handle(), false, + &l2c_link_sec_comp, p_ccb); break; } } diff --git a/stack/l2cap/l2c_int.h b/stack/l2cap/l2c_int.h index fed9b4f93..b49a2b05f 100644 --- a/stack/l2cap/l2c_int.h +++ b/stack/l2cap/l2c_int.h @@ -332,7 +332,19 @@ typedef struct t_l2c_linkcb { tL2C_LINK_STATE link_state; alarm_t* l2c_lcb_timer; /* Timer entry for timeout evt */ - uint16_t handle; /* The handle used with LM */ + private: + uint16_t handle_; /* The handle used with LM */ + friend void l2cble_conn_comp(uint16_t handle, uint8_t role, + const RawAddress& bda, tBLE_ADDR_TYPE type, + uint16_t conn_interval, uint16_t conn_latency, + uint16_t conn_timeout); + friend void l2c_link_hci_conn_comp(uint8_t status, uint16_t handle, + const RawAddress& p_bda); + void SetHandle(uint16_t handle) { handle_ = handle; } + + public: + uint16_t Handle() const { return handle_; } + void InvalidateHandle() { handle_ = HCI_INVALID_HANDLE; } tL2C_CCB_Q ccb_queue; /* Queue of CCBs on this LCB */ diff --git a/stack/l2cap/l2c_link.cc b/stack/l2cap/l2c_link.cc index a81df0d98..11d9ef568 100644 --- a/stack/l2cap/l2c_link.cc +++ b/stack/l2cap/l2c_link.cc @@ -154,13 +154,13 @@ void l2c_link_hci_conn_comp(uint8_t status, uint16_t handle, L2CAP_TRACE_ERROR("L2CAP got conn_comp in bad state: %d status: 0x%d", p_lcb->link_state, status); - if (status != HCI_SUCCESS) l2c_link_hci_disc_comp(p_lcb->handle, status); + if (status != HCI_SUCCESS) l2c_link_hci_disc_comp(p_lcb->Handle(), status); return; } /* Save the handle */ - p_lcb->handle = handle; + p_lcb->SetHandle(handle); if (ci.status == HCI_SUCCESS) { /* Connected OK. Change state to connected */ @@ -207,7 +207,7 @@ void l2c_link_hci_conn_comp(uint8_t status, uint16_t handle, else if ((ci.status == HCI_ERR_MAX_NUM_OF_CONNECTIONS) && l2cu_lcb_disconnecting()) { p_lcb->link_state = LST_CONNECT_HOLDING; - p_lcb->handle = HCI_INVALID_HANDLE; + p_lcb->InvalidateHandle(); } else { /* Just in case app decides to try again in the callback context */ p_lcb->link_state = LST_DISCONNECTING; @@ -406,7 +406,7 @@ bool l2c_link_hci_disc_comp(uint16_t handle, uint8_t reason) { "link_role = %d is_bonding = %d disc_reason = %d transport = " "%d", __func__, xx, p_lcb->remote_bd_addr.ToString().c_str(), p_lcb, - p_lcb->in_use, p_lcb->link_state, p_lcb->handle, + p_lcb->in_use, p_lcb->link_state, p_lcb->Handle(), p_lcb->LinkRole(), p_lcb->IsBonding(), p_lcb->DisconnectReason(), p_lcb->transport); } @@ -488,7 +488,7 @@ void l2c_link_timeout(tL2C_LCB* p_lcb) { uint64_t timeout_ms; bool start_timeout = true; - rc = btm_sec_disconnect(p_lcb->handle, HCI_ERR_PEER_USER); + rc = btm_sec_disconnect(p_lcb->Handle(), HCI_ERR_PEER_USER); if (rc == BTM_CMD_STORED) { /* Security Manager will take care of disconnecting, state will be @@ -842,7 +842,7 @@ bool l2c_link_check_power_mode(tL2C_LCB* p_lcb) { /* check power mode */ if (BTM_ReadPowerMode(p_lcb->remote_bd_addr, &mode)) { if (mode == BTM_PM_STS_PENDING) { - L2CAP_TRACE_DEBUG("LCB(0x%x) is in PM pending state", p_lcb->handle); + L2CAP_TRACE_DEBUG("LCB(0x%x) is in PM pending state", p_lcb->Handle()); return true; } @@ -1063,7 +1063,7 @@ static void l2c_link_send_to_lower_br_edr(tL2C_LCB* p_lcb, BT_HDR* p_buf) { acl_send_data_packet(p_buf, kDataPacketEventBrEdr); L2CAP_TRACE_DEBUG( "TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d", - l2cb.controller_xmit_window, p_lcb->handle, p_lcb->link_xmit_quota, + l2cb.controller_xmit_window, p_lcb->Handle(), p_lcb->link_xmit_quota, p_lcb->sent_not_acked, l2cb.round_robin_quota, l2cb.round_robin_unacked); } @@ -1110,7 +1110,7 @@ static void l2c_link_send_to_lower_ble(tL2C_LCB* p_lcb, BT_HDR* p_buf) { acl_send_data_packet(p_buf, kDataPacketEventBle); L2CAP_TRACE_DEBUG( "TotalWin=%d,Hndl=0x%x,Quota=%d,Unack=%d,RRQuota=%d,RRUnack=%d", - l2cb.controller_le_xmit_window, p_lcb->handle, p_lcb->link_xmit_quota, + l2cb.controller_le_xmit_window, p_lcb->Handle(), p_lcb->link_xmit_quota, p_lcb->sent_not_acked, l2cb.ble_round_robin_quota, l2cb.ble_round_robin_unacked); } @@ -1218,13 +1218,13 @@ void l2c_link_process_num_completed_pkts(uint8_t* p, uint8_t evt_len) { if (p_lcb->transport == BT_TRANSPORT_LE) { L2CAP_TRACE_DEBUG( "TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d", - l2cb.controller_le_xmit_window, p_lcb->handle, + l2cb.controller_le_xmit_window, p_lcb->Handle(), p_lcb->sent_not_acked, l2cb.ble_check_round_robin, l2cb.ble_round_robin_unacked); } else { L2CAP_TRACE_DEBUG( "TotalWin=%d,LinkUnack(0x%x)=%d,RRCheck=%d,RRUnack=%d", - l2cb.controller_xmit_window, p_lcb->handle, p_lcb->sent_not_acked, + l2cb.controller_xmit_window, p_lcb->Handle(), p_lcb->sent_not_acked, l2cb.check_round_robin, l2cb.round_robin_unacked); } } else { diff --git a/stack/l2cap/l2c_main.cc b/stack/l2cap/l2c_main.cc index 2e13b7b79..160998874 100644 --- a/stack/l2cap/l2c_main.cc +++ b/stack/l2cap/l2c_main.cc @@ -325,7 +325,7 @@ static void process_l2cap_cmd(tL2C_LCB* p_lcb, uint8_t* p, uint16_t pkt_len) { * we will ignore it and let a higher protocol timeout take care of it */ L2CAP_TRACE_WARNING("L2CAP - MTU rej Handle: %d MTU: %d", - p_lcb->handle, rej_mtu); + p_lcb->Handle(), rej_mtu); } if (rej_reason == L2CAP_CMD_REJ_INVALID_CID) { uint16_t lcid, rcid; @@ -395,10 +395,10 @@ static void process_l2cap_cmd(tL2C_LCB* p_lcb, uint8_t* p, uint16_t pkt_len) { if (p_rcb->psm == BT_PSM_RFCOMM) { btsnoop_get_interface()->add_rfc_l2c_channel( - p_lcb->handle, p_ccb->local_cid, p_ccb->remote_cid); + p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid); } else if (p_rcb->log_packets) { btsnoop_get_interface()->whitelist_l2c_channel( - p_lcb->handle, p_ccb->local_cid, p_ccb->remote_cid); + p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid); } l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info); @@ -435,10 +435,10 @@ static void process_l2cap_cmd(tL2C_LCB* p_lcb, uint8_t* p, uint16_t pkt_len) { tL2C_RCB* p_rcb = p_ccb->p_rcb; if (p_rcb->psm == BT_PSM_RFCOMM) { btsnoop_get_interface()->add_rfc_l2c_channel( - p_lcb->handle, p_ccb->local_cid, p_ccb->remote_cid); + p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid); } else if (p_rcb->log_packets) { btsnoop_get_interface()->whitelist_l2c_channel( - p_lcb->handle, p_ccb->local_cid, p_ccb->remote_cid); + p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid); } break; diff --git a/stack/l2cap/l2c_utils.cc b/stack/l2cap/l2c_utils.cc index ec04f87a3..1b9bfdafe 100644 --- a/stack/l2cap/l2c_utils.cc +++ b/stack/l2cap/l2c_utils.cc @@ -65,7 +65,7 @@ tL2C_LCB* l2cu_allocate_lcb(const RawAddress& p_bd_addr, bool is_bonding, p_lcb->in_use = true; p_lcb->link_state = LST_DISCONNECTED; - p_lcb->handle = HCI_INVALID_HANDLE; + p_lcb->InvalidateHandle(); p_lcb->SetLinkFlushTimeout(L2CAP_NO_AUTOMATIC_FLUSH); p_lcb->l2c_lcb_timer = alarm_new("l2c_lcb.l2c_lcb_timer"); p_lcb->info_resp_timer = alarm_new("l2c_lcb.info_resp_timer"); @@ -290,10 +290,10 @@ BT_HDR* l2cu_build_header(tL2C_LCB* p_lcb, uint16_t len, uint8_t cmd, /* Put in HCI header - handle + pkt boundary */ if (p_lcb->transport == BT_TRANSPORT_LE) { - UINT16_TO_STREAM(p, (p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE - << L2CAP_PKT_TYPE_SHIFT))); + UINT16_TO_STREAM(p, (p_lcb->Handle() | (L2CAP_PKT_START_NON_FLUSHABLE + << L2CAP_PKT_TYPE_SHIFT))); } else { - UINT16_TO_STREAM(p, p_lcb->handle | l2cb.non_flushable_pbf); + UINT16_TO_STREAM(p, p_lcb->Handle() | l2cb.non_flushable_pbf); } UINT16_TO_STREAM(p, len + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD); @@ -710,12 +710,12 @@ void l2cu_send_peer_config_rej(tL2C_CCB* p_ccb, uint8_t* p_data, /* Put in HCI header - handle + pkt boundary */ if (controller->supports_non_flushable_pb()) { - UINT16_TO_STREAM(p, (p_ccb->p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE - << L2CAP_PKT_TYPE_SHIFT))); + UINT16_TO_STREAM(p, (p_ccb->p_lcb->Handle() | (L2CAP_PKT_START_NON_FLUSHABLE + << L2CAP_PKT_TYPE_SHIFT))); } else { - UINT16_TO_STREAM( - p, (p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT))); + UINT16_TO_STREAM(p, (p_ccb->p_lcb->Handle() | + (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT))); } /* Remember the HCI header length position, and save space for it */ @@ -1482,7 +1482,7 @@ void l2cu_release_ccb(tL2C_CCB* p_ccb) { if (!p_ccb->in_use) return; btsnoop_get_interface()->clear_l2cap_whitelist( - p_lcb->handle, p_ccb->local_cid, p_ccb->remote_cid); + p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid); if (p_rcb && (p_rcb->psm != p_rcb->real_psm)) { BTM_SecClrServiceByPsm(p_rcb->psm); @@ -2010,8 +2010,8 @@ void l2cu_device_reset(void) { tL2C_LCB* p_lcb = &l2cb.lcb_pool[0]; for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) { - if ((p_lcb->in_use) && (p_lcb->handle != HCI_INVALID_HANDLE)) { - l2c_link_hci_disc_comp(p_lcb->handle, (uint8_t)-1); + if ((p_lcb->in_use) && (p_lcb->Handle() != HCI_INVALID_HANDLE)) { + l2c_link_hci_disc_comp(p_lcb->Handle(), (uint8_t)-1); } } } @@ -2230,7 +2230,7 @@ bool l2cu_set_acl_priority(const RawAddress& bd_addr, uint8_t priority, vs_param = (priority == L2CAP_PRIORITY_HIGH) ? HCI_BRCM_ACL_PRIORITY_HIGH : HCI_BRCM_ACL_PRIORITY_LOW; - UINT16_TO_STREAM(pp, p_lcb->handle); + UINT16_TO_STREAM(pp, p_lcb->Handle()); UINT8_TO_STREAM(pp, vs_param); BTM_VendorSpecificCommand(HCI_BRCM_SET_ACL_PRIORITY, @@ -2459,7 +2459,7 @@ void l2cu_no_dynamic_ccbs(tL2C_LCB* p_lcb) { L2CAP_TRACE_DEBUG( "l2cu_no_dynamic_ccbs() IDLE timer 0, disconnecting link"); - rc = btm_sec_disconnect(p_lcb->handle, HCI_ERR_PEER_USER); + rc = btm_sec_disconnect(p_lcb->Handle(), HCI_ERR_PEER_USER); if (rc == BTM_CMD_STARTED) { l2cu_process_fixed_disc_cback(p_lcb); p_lcb->link_state = LST_DISCONNECTING; @@ -2878,7 +2878,7 @@ tL2C_LCB* l2cu_find_lcb_by_handle(uint16_t handle) { tL2C_LCB* p_lcb = &l2cb.lcb_pool[0]; for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) { - if ((p_lcb->in_use) && (p_lcb->handle == handle)) { + if ((p_lcb->in_use) && (p_lcb->Handle() == handle)) { return (p_lcb); } } @@ -2941,8 +2941,8 @@ void l2cu_set_acl_hci_header(BT_HDR* p_buf, tL2C_CCB* p_ccb) { p = (uint8_t*)(p_buf + 1) + p_buf->offset - HCI_DATA_PREAMBLE_SIZE; if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE) { - UINT16_TO_STREAM(p, p_ccb->p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE - << L2CAP_PKT_TYPE_SHIFT)); + UINT16_TO_STREAM(p, p_ccb->p_lcb->Handle() | (L2CAP_PKT_START_NON_FLUSHABLE + << L2CAP_PKT_TYPE_SHIFT)); uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_ble(); @@ -2958,10 +2958,10 @@ void l2cu_set_acl_hci_header(BT_HDR* p_buf, tL2C_CCB* p_ccb) { (p_ccb->is_flushable)) || ((p_buf->layer_specific & L2CAP_FLUSHABLE_MASK) == L2CAP_FLUSHABLE_PKT)) { - UINT16_TO_STREAM( - p, p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT)); + UINT16_TO_STREAM(p, p_ccb->p_lcb->Handle() | + (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT)); } else { - UINT16_TO_STREAM(p, p_ccb->p_lcb->handle | l2cb.non_flushable_pbf); + UINT16_TO_STREAM(p, p_ccb->p_lcb->Handle() | l2cb.non_flushable_pbf); } uint16_t acl_data_size = -- 2.11.0