Skip to content

Commit

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

See merge request espressif/esp-idf!22364
  • Loading branch information
jack0c committed Feb 26, 2023
2 parents f89eedd + 71ed13c commit 17a0cda
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 11 deletions.
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32
2 changes: 2 additions & 0 deletions components/bt/host/bluedroid/api/include/api/esp_gattc_api.h
Expand Up @@ -212,6 +212,8 @@ typedef union {
uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
esp_gatt_conn_params_t conn_params; /*!< current connection parameters */
esp_ble_addr_type_t ble_addr_type; /*!< Remote BLE device address type */
uint16_t conn_handle; /*!< HCI connection handle */
} connect; /*!< Gatt client callback param of ESP_GATTC_CONNECT_EVT */

/**
Expand Down
2 changes: 2 additions & 0 deletions components/bt/host/bluedroid/api/include/api/esp_gatts_api.h
Expand Up @@ -199,6 +199,8 @@ typedef union {
uint8_t link_role; /*!< Link role : master role = 0 ; slave role = 1*/
esp_bd_addr_t remote_bda; /*!< Remote bluetooth device address */
esp_gatt_conn_params_t conn_params; /*!< current Connection parameters */
esp_ble_addr_type_t ble_addr_type; /*!< Remote BLE device address type */
uint16_t conn_handle; /*!< HCI connection handle */
} connect; /*!< Gatt server callback param of ESP_GATTS_CONNECT_EVT */

/**
Expand Down
8 changes: 7 additions & 1 deletion components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c
Expand Up @@ -742,7 +742,8 @@ void bta_gattc_conncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data)
if (p_rcb) {
bta_gattc_send_connect_cback(p_rcb,
p_data->int_conn.remote_bda,
p_data->int_conn.hdr.layer_specific, p_data->int_conn.conn_params, p_data->int_conn.role);
p_data->int_conn.hdr.layer_specific, p_data->int_conn.conn_params, p_data->int_conn.role,
p_data->int_conn.ble_addr_type, p_data->int_conn.conn_handle);

}
}
Expand Down Expand Up @@ -1689,6 +1690,11 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
p_buf->int_conn.conn_params.interval = p_lcb->current_used_conn_interval;
p_buf->int_conn.conn_params.latency = p_lcb->current_used_conn_latency;
p_buf->int_conn.conn_params.timeout = p_lcb->current_used_conn_timeout;
#if (BLE_INCLUDED == TRUE)
p_buf->int_conn.ble_addr_type = p_lcb->ble_addr_type;
#endif
p_buf->int_conn.conn_handle = p_lcb->handle;

} else {
APPL_TRACE_WARNING("gattc_conn_cb: conn params not found");
}
Expand Down
5 changes: 4 additions & 1 deletion components/bt/host/bluedroid/bta/gatt/bta_gattc_utils.c
Expand Up @@ -765,7 +765,8 @@ void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status
** Returns
**
*******************************************************************************/
void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id, tBTA_GATT_CONN_PARAMS conn_params, UINT8 link_role)
void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id,
tBTA_GATT_CONN_PARAMS conn_params, UINT8 link_role, UINT8 ble_addr_type, UINT16 conn_handle)
{
tBTA_GATTC cb_data;

Expand All @@ -779,6 +780,8 @@ void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda,
cb_data.connect.conn_params.latency = conn_params.latency;
cb_data.connect.conn_params.timeout = conn_params.timeout;
bdcpy(cb_data.connect.remote_bda, remote_bda);
cb_data.connect.ble_addr_type = ble_addr_type;
cb_data.connect.conn_handle = conn_handle;

(*p_clreg->p_cback)(BTA_GATTC_CONNECT_EVT, &cb_data);
}
Expand Down
4 changes: 4 additions & 0 deletions components/bt/host/bluedroid/bta/gatt/bta_gatts_act.c
Expand Up @@ -1003,6 +1003,10 @@ static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
cb_data.conn.conn_params.interval = p_lcb->current_used_conn_interval;
cb_data.conn.conn_params.latency = p_lcb->current_used_conn_latency;
cb_data.conn.conn_params.timeout = p_lcb->current_used_conn_timeout;
#if (BLE_INCLUDED == TRUE)
cb_data.conn.ble_addr_type = p_lcb->ble_addr_type;
#endif
cb_data.conn.conn_handle = p_lcb->handle;
}else {
APPL_TRACE_WARNING("%s not found connection parameters of the device ", __func__);
}
Expand Down
Expand Up @@ -223,6 +223,8 @@ typedef struct {
tGATT_DISCONN_REASON reason;
BOOLEAN already_connect;
tBTA_GATT_CONN_PARAMS conn_params;
UINT8 ble_addr_type;
UINT16 conn_handle;
} tBTA_GATTC_INT_CONN;

typedef struct {
Expand Down Expand Up @@ -474,7 +476,8 @@ extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *
extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data);
extern void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
BD_ADDR remote_bda, UINT16 conn_id, tBTA_TRANSPORT transport, UINT16 mtu);
extern void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id, tBTA_GATT_CONN_PARAMS conn_params, UINT8 link_role);
extern void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id,
tBTA_GATT_CONN_PARAMS conn_params, UINT8 link_role, UINT8 ble_addr_type, UINT16 conn_handle);
extern void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tGATT_DISCONN_REASON reason,
BD_ADDR remote_bda, UINT16 conn_id, UINT8 link_role);
extern void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg);
Expand Down
4 changes: 4 additions & 0 deletions components/bt/host/bluedroid/bta/include/bta/bta_gatt_api.h
Expand Up @@ -410,6 +410,8 @@ typedef struct {
tBTA_GATTC_IF client_if;
BD_ADDR remote_bda;
tBTA_GATT_CONN_PARAMS conn_params;
UINT8 ble_addr_type;
UINT16 conn_handle;
} tBTA_GATTC_CONNECT;

typedef struct {
Expand Down Expand Up @@ -622,6 +624,8 @@ typedef struct {
tBTA_GATT_REASON reason; /* report disconnect reason */
tBTA_GATT_TRANSPORT transport;
tBTA_GATT_CONN_PARAMS conn_params;
UINT8 ble_addr_type;
UINT16 conn_handle;
} tBTA_GATTS_CONN;

typedef struct {
Expand Down
2 changes: 2 additions & 0 deletions components/bt/host/bluedroid/btc/profile/std/gatt/btc_gattc.c
Expand Up @@ -914,6 +914,8 @@ void btc_gattc_cb_handler(btc_msg_t *msg)
param.connect.conn_params.interval = connect->conn_params.interval;
param.connect.conn_params.latency = connect->conn_params.latency;
param.connect.conn_params.timeout = connect->conn_params.timeout;
param.connect.ble_addr_type = connect->ble_addr_type;
param.connect.conn_handle = connect->conn_handle;
btc_gattc_cb_to_app(ESP_GATTC_CONNECT_EVT, gattc_if, &param);
break;
}
Expand Down
2 changes: 2 additions & 0 deletions components/bt/host/bluedroid/btc/profile/std/gatt/btc_gatts.c
Expand Up @@ -898,6 +898,8 @@ void btc_gatts_cb_handler(btc_msg_t *msg)
param.connect.conn_params.interval = p_data->conn.conn_params.interval;
param.connect.conn_params.latency = p_data->conn.conn_params.latency;
param.connect.conn_params.timeout = p_data->conn.conn_params.timeout;
param.connect.ble_addr_type = p_data->conn.ble_addr_type;
param.connect.conn_handle = p_data->conn.conn_handle;
btc_gatts_cb_to_app(ESP_GATTS_CONNECT_EVT, gatts_if, &param);
break;
case BTA_GATTS_DISCONNECT_EVT:
Expand Down
18 changes: 17 additions & 1 deletion components/bt/host/bluedroid/stack/btm/btm_ble.c
Expand Up @@ -40,6 +40,7 @@
//#define LOG_TAG "bt_btm_ble"
//#include "osi/include/log.h"
#if BLE_INCLUDED == TRUE
extern void BTM_UpdateAddrInfor(uint8_t addr_type, BD_ADDR bda);
#if SMP_INCLUDED == TRUE
// The temp variable to pass parameter between functions when in the connected event callback.
static BOOLEAN temp_enhanced = FALSE;
Expand Down Expand Up @@ -1987,6 +1988,17 @@ void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len, BOOLEAN enhanced)
if (enhanced) {
STREAM_TO_BDADDR (local_rpa, p);
STREAM_TO_BDADDR (peer_rpa, p);
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
BD_ADDR dummy_bda = {0};
/* For controller generates RPA, if resolving list contains no matching entry, it use identity address.
* So we should update own addr type in Host */
if (memcmp(local_rpa, dummy_bda, BD_ADDR_LEN)) {
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type |= (BLE_ADDR_TYPE_ID_BIT);
BTM_UpdateAddrInfor(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, local_rpa);
} else {
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type &= (~BLE_ADDR_TYPE_ID_BIT);
}
#endif
}
#if (BLE_PRIVACY_SPT == TRUE )
peer_addr_type = bda_type;
Expand Down Expand Up @@ -2566,7 +2578,11 @@ static void btm_ble_process_irk(tSMP_ENC *p)
memcpy(btm_cb.devcb.id_keys.irk, p->param_buf, BT_OCTET16_LEN);
btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);

#if BLE_PRIVACY_SPT == TRUE
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
btm_ble_add_default_entry_to_resolving_list();
#endif

#if (BLE_PRIVACY_SPT == TRUE) && (CONTROLLER_RPA_LIST_ENABLE == FALSE)
/* if privacy is enabled, new RPA should be calculated */
if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
Expand Down
5 changes: 5 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c
Expand Up @@ -375,6 +375,8 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR
} else {
extend_adv_cb.inst[instance].legacy_pdu = false;
}

#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
// if own_addr_type == BLE_ADDR_PUBLIC_ID or BLE_ADDR_RANDOM_ID,
if((params->own_addr_type == BLE_ADDR_PUBLIC_ID || params->own_addr_type == BLE_ADDR_RANDOM_ID) && BTM_GetLocalResolvablePrivateAddr(rand_addr)) {
params->own_addr_type = BLE_ADDR_RANDOM;
Expand All @@ -384,6 +386,9 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR
} else if (params->own_addr_type == BLE_ADDR_RANDOM_ID) {
params->own_addr_type = BLE_ADDR_RANDOM;
}
#else
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = params->own_addr_type;
#endif

if ((err = btsnd_hcic_ble_set_ext_adv_params(instance, params->type, params->interval_min, params->interval_max,
params->channel_map, params->own_addr_type, params->peer_addr_type,
Expand Down
2 changes: 1 addition & 1 deletion components/bt/host/bluedroid/stack/btm/btm_ble_addr.c
Expand Up @@ -591,7 +591,7 @@ void btm_ble_refresh_local_resolvable_private_addr(BD_ADDR pseudo_addr,
BD_ADDR dummy_bda = {0};

if (p != NULL) {
if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type >= BLE_ADDR_RANDOM) {
p->conn_addr_type = BLE_ADDR_RANDOM;
if (memcmp(local_rpa, dummy_bda, BD_ADDR_LEN)) {
memcpy(p->conn_addr, local_rpa, BD_ADDR_LEN);
Expand Down
20 changes: 19 additions & 1 deletion components/bt/host/bluedroid/stack/btm/btm_ble_gap.c
Expand Up @@ -893,8 +893,16 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK
// Disable RPA function
btsnd_hcic_ble_set_addr_resolution_enable(FALSE);
} else { /* privacy is turned on*/
#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
/* always set host random address, used when privacy 1.1 or priavcy 1.2 is disabled */
btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
#else
/* Controller generates RPA, Host don't need to set random address */
if (random_cb && random_cb->set_local_privacy_cback){
(*random_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_SUCCESS);
random_cb->set_local_privacy_cback = NULL;
}
#endif

if (BTM_BleMaxMultiAdvInstanceCount() > 0) {
btm_ble_multi_adv_enb_privacy(privacy_mode);
Expand All @@ -910,7 +918,7 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK
} else { /* 4.1/4.0 controller */
p_cb->privacy_mode = BTM_PRIVACY_1_1;
}
// Disable RPA function
// Enable RPA function
btsnd_hcic_ble_set_addr_resolution_enable(TRUE);
}

Expand Down Expand Up @@ -990,6 +998,7 @@ void BTM_BleSetStaticAddr(BD_ADDR rand_addr)
btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RANDOM;
}

#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb)
{
if(*own_bda_type == BLE_ADDR_RANDOM) {
Expand Down Expand Up @@ -1056,6 +1065,13 @@ uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *

return BTM_SUCCESS;
}
#else
uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb)
{
btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = *own_bda_type;
return BTM_SUCCESS;
}
#endif


/*******************************************************************************
Expand Down Expand Up @@ -4238,8 +4254,10 @@ void btm_ble_timeout(TIMER_LIST_ENT *p_tle)
case BTU_TTYPE_BLE_RANDOM_ADDR:
if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
if (NULL == (void *)(p_tle->param)) {
#if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
/* refresh the random addr */
btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
#endif
} else {
if (BTM_BleMaxMultiAdvInstanceCount() > 0) {
btm_ble_multi_adv_configure_rpa((tBTM_BLE_MULTI_ADV_INST *)p_tle->param);
Expand Down
15 changes: 15 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c
Expand Up @@ -1021,4 +1021,19 @@ void btm_ble_resolving_list_cleanup(void)
}

}

void btm_ble_add_default_entry_to_resolving_list(void)
{
/*
* Add local IRK entry with 00:00:00:00:00:00 address. This entry will
* be used to generate RPA for non-directed advertising if own_addr_type
* is set to rpa_pub since we use all-zero address as peer addres in
* such case. Peer IRK should be left all-zero since this is not for an
* actual peer.
*/
BD_ADDR peer_addr = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
BT_OCTET16 peer_irk = {0x0};

btsnd_hcic_ble_add_device_resolving_list (BLE_ADDR_PUBLIC, peer_addr, peer_irk, btm_cb.devcb.id_keys.irk);
}
#endif
5 changes: 5 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_sec.c
Expand Up @@ -240,6 +240,11 @@ BOOLEAN BTM_SecRegister(tBTM_APPL_INFO *p_cb_info)
if (memcmp(btm_cb.devcb.id_keys.ir, &temp_value, sizeof(BT_OCTET16)) == 0) {
btm_ble_reset_id();
}
#if (CONTROLLER_RPA_LIST_ENABLE == TRUE)
else {
btm_ble_add_default_entry_to_resolving_list();
}
#endif
} else {
BTM_TRACE_WARNING("%s p_cb_info->p_le_callback == NULL\n", __func__);
}
Expand Down
Expand Up @@ -497,6 +497,7 @@ BOOLEAN btm_ble_disable_resolving_list(UINT8 rl_mask, BOOLEAN to_resume);
void btm_ble_enable_resolving_list_for_platform (UINT8 rl_mask);
void btm_ble_resolving_list_init(UINT8 max_irk_list_sz);
void btm_ble_resolving_list_cleanup(void);
void btm_ble_add_default_entry_to_resolving_list(void);
#endif

void btm_ble_multi_adv_configure_rpa (tBTM_BLE_MULTI_ADV_INST *p_inst);
Expand Down
Expand Up @@ -100,7 +100,7 @@ static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x100,
.adv_int_max = 0x100,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
Expand Down
Expand Up @@ -56,7 +56,7 @@ static const char remote_device_name[] = "ESP_BLE_SECURITY";

static esp_ble_scan_params_t ble_scan_params = {
.scan_type = BLE_SCAN_TYPE_ACTIVE,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
.scan_interval = 0x50,
.scan_window = 0x30,
Expand Down
Expand Up @@ -72,7 +72,7 @@ static esp_ble_adv_params_t heart_rate_adv_params = {
.adv_int_min = 0x100,
.adv_int_max = 0x100,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
Expand Down
Expand Up @@ -99,7 +99,7 @@ static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x060,
.adv_int_max = 0x060,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
Expand Down

0 comments on commit 17a0cda

Please sign in to comment.