Skip to content

Commit

Permalink
Merge branch 'feature/support_ble_53_feature_for_pts' into 'master'
Browse files Browse the repository at this point in the history
Bluedroid: support ble host features for PTS

See merge request espressif/esp-idf!21253
  • Loading branch information
wmy-espressif committed Dec 12, 2022
2 parents 7064b66 + 2afe5cd commit 84e48db
Show file tree
Hide file tree
Showing 54 changed files with 2,140 additions and 109 deletions.
1 change: 1 addition & 0 deletions components/bt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ if(CONFIG_BT_ENABLED)
"host/bluedroid/stack/gatt/gatt_db.c"
"host/bluedroid/stack/gatt/gatt_main.c"
"host/bluedroid/stack/gatt/gatt_sr.c"
"host/bluedroid/stack/gatt/gatt_sr_hash.c"
"host/bluedroid/stack/gatt/gatt_utils.c"
"host/bluedroid/stack/hcic/hciblecmds.c"
"host/bluedroid/stack/hcic/hcicmds.c"
Expand Down
30 changes: 30 additions & 0 deletions components/bt/host/bluedroid/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ config BT_GATTS_SEND_SERVICE_CHANGE_MODE
default 1 if BT_GATTS_SEND_SERVICE_CHANGE_MANUAL
default 0

config BT_GATTS_ROBUST_CACHING_ENABLED
bool "Enable Robust Caching on Server Side"
depends on BT_GATTS_ENABLE
default n
help
This option enable gatt robust caching feature on server

config BT_GATTS_DEVICE_NAME_WRITABLE
bool "Allow to write device name by GATT clients"
depends on BT_GATTS_ENABLE
default n
help
Enabling this option allows remote GATT clients to write device name

config BT_GATTS_APPEARANCE_WRITABLE
bool "Allow to write appearance by GATT clients"
depends on BT_GATTS_ENABLE
default n
help
Enabling this option allows remote GATT clients to write appearance

config BT_GATTC_ENABLE
bool "Include GATT client module(GATTC)"
depends on BT_BLE_ENABLED
Expand Down Expand Up @@ -1094,6 +1115,15 @@ config BT_BLE_RPA_SUPPORTED
For ESP32C3, ESP32S3, ESP32H4 and ESP32C2, devices support network privacy mode and device privacy mode,
users can switch the two modes according to their own needs. So this option is enabled by default.

config BT_BLE_RPA_TIMEOUT
int "Timeout of resolvable private address"
depends on BT_BLUEDROID_ENABLED
range 1 3600
default 900
help
This set RPA timeout of Controller and Host.
Default is 900 s (15 minutes). Range is 1 s to 1 hour (3600 s).

config BT_BLE_50_FEATURES_SUPPORTED
bool "Enable BLE 5.0 features"
depends on (BT_BLUEDROID_ENABLED && (IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3 || SOC_ESP_NIMBLE_CONTROLLER))
Expand Down
32 changes: 32 additions & 0 deletions components/bt/host/bluedroid/api/esp_gap_ble_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,38 @@ esp_err_t esp_ble_oob_req_reply(esp_bd_addr_t bd_addr, uint8_t *TK, uint8_t len)
btc_gap_ble_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

esp_err_t esp_ble_sc_oob_req_reply(esp_bd_addr_t bd_addr, uint8_t p_c[16], uint8_t p_r[16])
{
if (!p_c || !p_r) {
return ESP_ERR_INVALID_ARG;
}

btc_msg_t msg = {0};
btc_ble_gap_args_t arg;

ESP_BLUEDROID_STATUS_CHECK(ESP_BLUEDROID_STATUS_ENABLED);

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_SC_OOB_REQ_REPLY_EVT;
memcpy(arg.sc_oob_req_reply.bd_addr, bd_addr, ESP_BD_ADDR_LEN);
arg.sc_oob_req_reply.p_c = p_c;
arg.sc_oob_req_reply.p_r = p_r;

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), btc_gap_ble_arg_deep_copy,
btc_gap_ble_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

esp_err_t esp_ble_create_sc_oob_data(void)
{
btc_msg_t msg = {0};

msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_SC_CR_OOB_DATA_EVT;

return (btc_transfer_context(&msg, NULL, 0, NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}
#endif /* #if (SMP_INCLUDED == TRUE) */

esp_err_t esp_ble_gap_disconnect(esp_bd_addr_t remote_device)
Expand Down
33 changes: 33 additions & 0 deletions components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ typedef enum {
ESP_GAP_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_COMPLETE_EVT, /*!< When update duplicate exceptional list complete, the event comes */
#endif //#if (BLE_42_FEATURE_SUPPORT == TRUE)
ESP_GAP_BLE_SET_CHANNELS_EVT = 29, /*!< When setting BLE channels complete, the event comes */
ESP_GAP_BLE_SC_OOB_REQ_EVT, /*!< Secure Connection OOB request event */
ESP_GAP_BLE_SC_CR_LOC_OOB_EVT, /*!< Secure Connection create OOB data complete event */
#if (BLE_50_FEATURE_SUPPORT == TRUE)
ESP_GAP_BLE_READ_PHY_COMPLETE_EVT, /*!< when reading phy complete, this event comes */
ESP_GAP_BLE_SET_PREFERRED_DEFAULT_PHY_COMPLETE_EVT, /*!< when preferred default phy complete, this event comes */
Expand Down Expand Up @@ -583,6 +585,13 @@ typedef struct {
esp_bt_octet16_t dhk; /*!< the 16 bits of the dh key value */
} esp_ble_local_id_keys_t; /*!< the structure of the ble local id keys value type*/

/**
* @brief structure type of the ble local oob data value
*/
typedef struct {
esp_bt_octet16_t oob_c; /*!< the 128 bits of confirmation value */
esp_bt_octet16_t oob_r; /*!< the 128 bits of randomizer value */
} esp_ble_local_oob_data_t;

/**
* @brief Structure associated with ESP_AUTH_CMPL_EVT
Expand All @@ -609,6 +618,7 @@ typedef union
esp_ble_sec_req_t ble_req; /*!< BLE SMP related request */
esp_ble_key_t ble_key; /*!< BLE SMP keys used when pairing */
esp_ble_local_id_keys_t ble_id_keys; /*!< BLE IR event */
esp_ble_local_oob_data_t oob_data; /*!< BLE SMP secure connection OOB data */
esp_ble_auth_cmpl_t auth_cmpl; /*!< Authentication complete indication. */
} esp_ble_sec_t; /*!< BLE security type */
#if (BLE_42_FEATURE_SUPPORT == TRUE)
Expand Down Expand Up @@ -1769,6 +1779,29 @@ esp_err_t esp_ble_get_bond_device_list(int *dev_num, esp_ble_bond_dev_t *dev_lis
*/
esp_err_t esp_ble_oob_req_reply(esp_bd_addr_t bd_addr, uint8_t *TK, uint8_t len);

/**
* @brief This function is called to provide the OOB data for
* SMP in response to ESP_GAP_BLE_SC_OOB_REQ_EVT
*
* @param[in] bd_addr: BD address of the peer device.
* @param[in] p_c: Confirmation value, it shall be a 128-bit random number
* @param[in] p_r: Randomizer value, it should be a 128-bit random number
*
* @return - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_sc_oob_req_reply(esp_bd_addr_t bd_addr, uint8_t p_c[16], uint8_t p_r[16]);

/**
* @brief This function is called to create the OOB data for
* SMP when secure connection
*
* @return - ESP_OK : success
* - other : failed
*
*/
esp_err_t esp_ble_create_sc_oob_data(void);
#endif /* #if (SMP_INCLUDED == TRUE) */

/**
Expand Down
45 changes: 45 additions & 0 deletions components/bt/host/bluedroid/bta/dm/bta_dm_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,40 @@ void bta_dm_oob_reply(tBTA_DM_MSG *p_data)
#endif
}

/*******************************************************************************
**
** Function bta_dm_sc_oob_reply
**
** Description This function is called to provide the OOB data for
** SMP in response to BLE secure connection OOB request.
**
** Returns void
**
*******************************************************************************/
void bta_dm_sc_oob_reply(tBTA_DM_MSG *p_data)
{
#if (BLE_INCLUDED)
BTM_BleSecureConnectionOobDataReply(p_data->sc_oob_reply.bd_addr, p_data->sc_oob_reply.c, p_data->sc_oob_reply.r);
#endif
}

/*******************************************************************************
**
** Function bta_dm_sc_create_oob_data
**
** Description This function is called to create the OOB data for
** SMP when secure connection.
**
** Returns void
**
*******************************************************************************/
void bta_dm_sc_create_oob_data(tBTA_DM_MSG *p_data)
{
#if (BLE_INCLUDED)
BTM_BleSecureConnectionCreateOobData();
#endif
}

/*******************************************************************************
**
** Function bta_dm_ci_io_req_act
Expand Down Expand Up @@ -4711,6 +4745,17 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D
bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
break;

case BTM_LE_SC_OOB_REQ_EVT:
bdcpy(sec_event.ble_req.bd_addr, bda);
bta_dm_cb.p_sec_cback(BTA_DM_BLE_SC_OOB_REQ_EVT, &sec_event);
break;

case BTM_LE_SC_LOC_OOB_EVT:
memcpy(sec_event.local_oob_data.local_oob_c, p_data->local_oob_data.commitment, BT_OCTET16_LEN);
memcpy(sec_event.local_oob_data.local_oob_r, p_data->local_oob_data.randomizer, BT_OCTET16_LEN);
bta_dm_cb.p_sec_cback(BTA_DM_BLE_SC_CR_LOC_OOB_EVT, &sec_event);
break;

case BTM_LE_NC_REQ_EVT:
bdcpy(sec_event.key_notif.bd_addr, bda);
BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name,bta_dm_get_remname(), BD_NAME_LEN);
Expand Down
65 changes: 64 additions & 1 deletion components/bt/host/bluedroid/bta/dm/bta_dm_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ void BTA_DmLocalOob(void)
** Function BTA_DmOobReply
**
** This function is called to provide the OOB data for
** SMP in response to BTM_LE_OOB_REQ_EVT
** SMP in response to BTA_LE_OOB_REQ_EVT
**
** Parameters: bd_addr - Address of the peer device
** len - length of simple pairing Randomizer C
Expand All @@ -675,6 +675,55 @@ void BTA_DmOobReply(BD_ADDR bd_addr, UINT8 len, UINT8 *p_value)
bta_sys_sendmsg(p_msg);
}
}

/*******************************************************************************
**
** Function BTA_DmSecureConnectionOobReply
**
** This function is called to provide the OOB data for
** SMP in response to BTA_LE_OOB_REQ_EVT
**
** Parameters: bd_addr - Address of the peer device
** p_c - Pointer to Confirmation
** p_r - Pointer to Randomizer
**
** Returns void
**
*******************************************************************************/
void BTA_DmSecureConnectionOobReply(BD_ADDR bd_addr, UINT8 *p_c, UINT8 *p_r)
{
tBTA_DM_API_SC_OOB_REPLY *p_msg;

if ((p_msg = (tBTA_DM_API_SC_OOB_REPLY *) osi_malloc(sizeof(tBTA_DM_API_OOB_REPLY))) != NULL) {
p_msg->hdr.event = BTA_DM_API_SC_OOB_REPLY_EVT;
if((p_c == NULL) || (p_r == NULL)) {
return;
}
memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
memcpy(p_msg->c, p_c, BT_OCTET16_LEN);
memcpy(p_msg->r, p_r, BT_OCTET16_LEN);
bta_sys_sendmsg(p_msg);
}
}
/*******************************************************************************
**
** Function BTA_DmSecureConnectionCreateOobData
**
** This function is called to create the OOB data for
** SMP when secure connection
**
** Returns void
**
*******************************************************************************/
void BTA_DmSecureConnectionCreateOobData(void)
{
tBTA_DM_API_SC_CR_OOB_DATA *p_msg;

if ((p_msg = (tBTA_DM_API_SC_CR_OOB_DATA *) osi_malloc(sizeof(tBTA_DM_API_SC_CR_OOB_DATA))) != NULL) {
p_msg->hdr.event = BTA_DM_API_SC_CR_OOB_DATA_EVT;
bta_sys_sendmsg(p_msg);
}
}
#endif /* BTM_OOB_INCLUDED */
/*******************************************************************************
**
Expand Down Expand Up @@ -3089,4 +3138,18 @@ void BTA_DmBleGapExtConnect(tBLE_ADDR_TYPE own_addr_type, const BD_ADDR peer_add

#endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)

#if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)
uint8_t BTA_DmBlePeriodicAdvSetInfoTrans(uint8_t addr[6], uint16_t service_data, uint8_t adv_handle)
{
BTM_BlePeriodicAdvSetInfoTrans(addr, service_data, adv_handle);
return 0;
}

uint8_t BTA_DmBleSetPeriodicAdvSyncTransParams(uint8_t addr[6], uint8_t mode, uint16_t skip, uint16_t sync_timeout)
{
BTM_BleSetPeriodicAdvSyncTransParams(addr, mode, skip, sync_timeout, 0);
return 0;
}
#endif // #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)

#endif
4 changes: 0 additions & 4 deletions components/bt/host/bluedroid/bta/dm/bta_dm_co.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,6 @@ void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,

*p_auth_req = bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & BTA_LE_AUTH_REQ_MITM) | ((*p_auth_req) & BTA_LE_AUTH_REQ_MITM);

if (*p_oob_data == BTM_BLE_OOB_ENABLE) {
*p_auth_req = (*p_auth_req)&(~BTA_LE_AUTH_REQ_SC_ONLY);
}

if (bte_appl_cfg.ble_io_cap <= 4) {
*p_io_cap = bte_appl_cfg.ble_io_cap;
}
Expand Down
2 changes: 2 additions & 0 deletions components/bt/host/bluedroid/bta/dm/bta_dm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
#if (BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
bta_dm_loc_oob, /* BTA_DM_API_LOC_OOB_EVT */
bta_dm_oob_reply, /* BTA_DM_API_OOB_REPLY_EVT */
bta_dm_sc_oob_reply, /* BTA_DM_API_SC_OOB_REPLY_EVT */
bta_dm_sc_create_oob_data, /* BTA_DM_API_SC_CR_OOB_DATA_EVT */
bta_dm_ci_io_req_act, /* BTA_DM_CI_IO_REQ_EVT */
bta_dm_ci_rmt_oob_act, /* BTA_DM_CI_RMT_OOB_EVT */
#endif /* BTM_OOB_INCLUDED */
Expand Down
20 changes: 20 additions & 0 deletions components/bt/host/bluedroid/bta/dm/include/bta_dm_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ enum {
#if (BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
BTA_DM_API_LOC_OOB_EVT,
BTA_DM_API_OOB_REPLY_EVT,
BTA_DM_API_SC_OOB_REPLY_EVT,
BTA_DM_API_SC_CR_OOB_DATA_EVT,
BTA_DM_CI_IO_REQ_EVT,
BTA_DM_CI_RMT_OOB_EVT,
#endif /* BTM_OOB_INCLUDED */
Expand Down Expand Up @@ -394,8 +396,23 @@ typedef struct {
BD_ADDR bd_addr;
UINT8 len;
UINT8 value[BT_OCTET16_LEN];
UINT8 c[BT_OCTET16_LEN];
UINT8 r[BT_OCTET16_LEN];
} tBTA_DM_API_OOB_REPLY;

/* data type for BTA_DM_API_SC_OOB_REPLY_EVT */
typedef struct {
BT_HDR hdr;
BD_ADDR bd_addr;
UINT8 c[BT_OCTET16_LEN];
UINT8 r[BT_OCTET16_LEN];
} tBTA_DM_API_SC_OOB_REPLY;

/* data type for BTA_DM_API_SC_CR_OOB_DATA_EVT */
typedef struct {
BT_HDR hdr;
} tBTA_DM_API_SC_CR_OOB_DATA;

/* data type for BTA_DM_API_CONFIRM_EVT */
typedef struct {
BT_HDR hdr;
Expand Down Expand Up @@ -1047,6 +1064,7 @@ typedef union {

tBTA_DM_API_LOC_OOB loc_oob;
tBTA_DM_API_OOB_REPLY oob_reply;
tBTA_DM_API_SC_OOB_REPLY sc_oob_reply;
tBTA_DM_API_CONFIRM confirm;
tBTA_DM_API_KEY_REQ key_req;
tBTA_DM_CI_IO_REQ ci_io_req;
Expand Down Expand Up @@ -1605,6 +1623,8 @@ extern BOOLEAN bta_dm_check_if_only_hd_connected(BD_ADDR peer_addr);
#if (BTM_OOB_INCLUDED == TRUE)
extern void bta_dm_loc_oob(tBTA_DM_MSG *p_data);
extern void bta_dm_oob_reply(tBTA_DM_MSG *p_data);
extern void bta_dm_sc_oob_reply(tBTA_DM_MSG *p_data);
extern void bta_dm_sc_create_oob_data(tBTA_DM_MSG *p_data);
extern void bta_dm_ci_io_req_act(tBTA_DM_MSG *p_data);
extern void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG *p_data);
#endif /* BTM_OOB_INCLUDED */
Expand Down
9 changes: 6 additions & 3 deletions components/bt/host/bluedroid/bta/gatt/bta_gattc_act.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
if (p_cb->state == BTA_GATTC_STATE_DISABLED) {
/* initialize control block */
memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
bta_gattc_cb.auto_disc = true;
p_cb->state = BTA_GATTC_STATE_ENABLED;
} else {
APPL_TRACE_DEBUG("GATTC is already enabled");
Expand Down Expand Up @@ -692,9 +693,11 @@ void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
} else
#endif
{ /* cache is building */
p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
/* cache load failure, start discovery */
bta_gattc_start_discover(p_clcb, NULL);
if (bta_gattc_cb.auto_disc) {
p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
/* cache load failure, start discovery */
bta_gattc_start_discover(p_clcb, NULL);
}
}
} else { /* cache is building */
p_clcb->state = BTA_GATTC_DISCOVER_ST;
Expand Down
Loading

0 comments on commit 84e48db

Please sign in to comment.