Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_some_ble_bug_v5.1' into 'release/v5.1'
Browse files Browse the repository at this point in the history
Fixed some BLE bugs (backport v5.1)

See merge request espressif/esp-idf!23699
  • Loading branch information
jack0c committed May 18, 2023
2 parents 8b7cd4d + b77479d commit 7fcba0f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 20 deletions.
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32c3_family
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/bta/dm/bta_dm_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -5102,7 +5102,7 @@ void bta_dm_ble_update_conn_params (tBTA_DM_MSG *p_data)
*******************************************************************************/
void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data)
{
L2CA_RemoveFixedChnl(L2CAP_ATT_CID, p_data->ble_disconnect.remote_bda);
L2CA_BleDisconnect(p_data->ble_disconnect.remote_bda);
}

/*******************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions components/bt/host/bluedroid/bta/dm/bta_dm_co.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,15 @@ void bta_dm_co_ble_set_init_key_req(UINT8 init_key)
{
#if (SMP_INCLUDED == TRUE)
init_key &= 0x0f; // 4~7bit reservd, only used the 0~3bit
bte_appl_cfg.ble_init_key &= init_key;
bte_appl_cfg.ble_init_key = init_key;
#endif ///SMP_INCLUDED == TRUE
}

void bta_dm_co_ble_set_rsp_key_req(UINT8 rsp_key)
{
#if (SMP_INCLUDED == TRUE)
rsp_key &= 0x0f; // 4~7bit reservd, only used the 0~3bit
bte_appl_cfg.ble_resp_key &= rsp_key;
bte_appl_cfg.ble_resp_key = rsp_key;
#endif ///SMP_INCLUDED == TRUE
}

Expand Down
39 changes: 25 additions & 14 deletions components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,28 +302,39 @@ tBTM_STATUS BTM_BleSetExtendedAdvRandaddr(UINT8 instance, BD_ADDR rand_addr)
BD_ADDR invalid_rand_addr_a, invalid_rand_addr_b;
memset(invalid_rand_addr_a, 0xff, sizeof(BD_ADDR));
memset(invalid_rand_addr_b, 0x00, sizeof(BD_ADDR));
invalid_rand_addr_b[0] = invalid_rand_addr_b[0] | BT_STATIC_RAND_ADDR_MASK;
if((rand_addr[0] & BT_STATIC_RAND_ADDR_MASK) == BT_STATIC_RAND_ADDR_MASK
&& memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) != 0
&& memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) != 0){
// set random address
if((err = btsnd_hcic_ble_set_extend_rand_address(instance, rand_addr)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("%s, fail to send the hci command, the error code = %s(0x%x)",
__func__, btm_ble_hci_status_to_str(err), err);
if((rand_addr[0] & BT_STATIC_RAND_ADDR_MASK) == BT_STATIC_RAND_ADDR_MASK) {
invalid_rand_addr_b[0] = invalid_rand_addr_b[0] | BT_STATIC_RAND_ADDR_MASK;
if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) == 0
|| memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) == 0) {
status = BTM_ILLEGAL_VALUE;
} else {
// set random address success, update address infor
if(extend_adv_cb.inst[instance].configured && extend_adv_cb.inst[instance].connetable) {
BTM_BleSetStaticAddr(rand_addr);
BTM_UpdateAddrInfor(BLE_ADDR_RANDOM, rand_addr);
}
goto end;
}
} else if ((rand_addr[0] | BT_NON_RPA_MASK) == BT_NON_RPA_MASK) {
invalid_rand_addr_a[0] = invalid_rand_addr_a[0] & BT_NON_RPA_MASK;
if (memcmp(invalid_rand_addr_a, rand_addr, BD_ADDR_LEN) == 0
|| memcmp(invalid_rand_addr_b, rand_addr, BD_ADDR_LEN) == 0) {
status = BTM_ILLEGAL_VALUE;
goto end;
}
} else {
BTM_TRACE_ERROR("%s invalid random address", __func__);
status = BTM_ILLEGAL_VALUE;
goto end;
}

// set random address
if((err = btsnd_hcic_ble_set_extend_rand_address(instance, rand_addr)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("%s, fail to send the hci command, the error code = %s(0x%x)",
__func__, btm_ble_hci_status_to_str(err), err);
status = BTM_ILLEGAL_VALUE;
} else {
// set random address success, update address infor
if(extend_adv_cb.inst[instance].configured && extend_adv_cb.inst[instance].connetable) {
BTM_BleSetStaticAddr(rand_addr);
BTM_UpdateAddrInfor(BLE_ADDR_RANDOM, rand_addr);
}
}

end:
cb_params.status = status;

Expand Down
5 changes: 3 additions & 2 deletions components/bt/host/bluedroid/stack/btm/btm_ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2237,9 +2237,10 @@ UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst,
#if BTM_MAX_LOC_BD_NAME_LEN > 0
if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_DEV_NAME) {
if (strlen(btm_cb.cfg.bd_name) > (UINT16)(len - MIN_ADV_LENGTH)) {
*p++ = len - MIN_ADV_LENGTH + 1;
cp_len = (UINT16)(len - MIN_ADV_LENGTH);
*p++ = cp_len + 1;
*p++ = BTM_BLE_AD_TYPE_NAME_SHORT;
ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, len - MIN_ADV_LENGTH);
ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, cp_len);
} else {
cp_len = (UINT16)strlen(btm_cb.cfg.bd_name);
*p++ = cp_len + 1;
Expand Down
13 changes: 13 additions & 0 deletions components/bt/host/bluedroid/stack/include/stack/l2c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,19 @@ extern BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable);
**
*******************************************************************************/
extern UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr);

/*******************************************************************************
**
** Function L2CA_BleDisconnect
**
** Description This function use to disconnect LE connection.
**
** Parameters BD Address of remote
**
** Returns TRUE if disconnect successfully.
**
*******************************************************************************/
extern BOOLEAN L2CA_BleDisconnect (BD_ADDR rem_bda);
#endif /* (BLE_INCLUDED == TRUE) */

/*******************************************************************************
Expand Down
30 changes: 30 additions & 0 deletions components/bt/host/bluedroid/stack/l2cap/l2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,36 @@ BOOLEAN L2CA_RemoveFixedChnl (UINT16 fixed_cid, BD_ADDR rem_bda)
return (TRUE);
}

#if BLE_INCLUDED == TRUE
BOOLEAN L2CA_BleDisconnect (BD_ADDR rem_bda)
{
tL2C_LCB *p_lcb;
tGATT_TCB *p_tcb;

p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
if (p_lcb == NULL) {
return FALSE;
}

if (p_lcb->link_state != LST_CONNECTED) {
return FALSE;
}

p_lcb->disc_reason = HCI_ERR_CONN_CAUSE_LOCAL_HOST;
p_lcb->link_state = LST_DISCONNECTING;
btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER);

p_tcb = gatt_find_tcb_by_addr(rem_bda, BT_TRANSPORT_LE);
if (p_tcb == NULL) {
return FALSE;
}

gatt_set_ch_state(p_tcb, GATT_CH_CLOSING);

return TRUE;
}
#endif

/*******************************************************************************
**
** Function L2CA_SetFixedChannelTout
Expand Down

0 comments on commit 7fcba0f

Please sign in to comment.