Skip to content

Commit

Permalink
Merge branch 'feat/add_api_to_set_privacy_mode_v4.4' into 'release/v4.4'
Browse files Browse the repository at this point in the history
feat(bt/bluedroid): support BLE set privacy mode (v4.4)

See merge request espressif/esp-idf!30908
  • Loading branch information
Isl2017 committed May 27, 2024
2 parents 799e503 + 543276b commit d84b40d
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 8 deletions.
19 changes: 19 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 @@ -998,6 +998,25 @@ esp_err_t esp_ble_dtm_stop(void)
return (btc_transfer_context(&msg, NULL, 0, NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

esp_err_t esp_ble_gap_set_privacy_mode(esp_ble_addr_type_t addr_type, esp_bd_addr_t addr, esp_ble_privacy_mode_t mode)
{
btc_msg_t msg;
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_SET_PRIVACY_MODE;

arg.set_privacy_mode.addr_type = addr_type;
memcpy(arg.set_privacy_mode.addr, addr, sizeof(esp_bd_addr_t));
arg.set_privacy_mode.privacy_mode = mode;

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

#if (BLE_50_FEATURE_SUPPORT == TRUE)

esp_err_t esp_ble_gap_read_phy(esp_bd_addr_t bd_addr)
Expand Down
27 changes: 27 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 @@ -228,6 +228,7 @@ typedef enum {
ESP_GAP_BLE_SET_RPA_TIMEOUT_COMPLETE_EVT, /*!< When set the Resolvable Private Address (RPA) timeout completes, the event comes */
ESP_GAP_BLE_ADD_DEV_TO_RESOLVING_LIST_COMPLETE_EVT, /*!< when add a device to the resolving list completes, the event comes*/
ESP_GAP_BLE_VENDOR_CMD_COMPLETE_EVT, /*!< When vendor hci command complete, the event comes */
ESP_GAP_BLE_SET_PRIVACY_MODE_COMPLETE_EVT, /*!< When set privacy mode complete, the event comes */
ESP_GAP_BLE_EVT_MAX, /*!< when maximum advertising event complete, the event comes */
} esp_gap_ble_cb_event_t;

Expand Down Expand Up @@ -1021,6 +1022,11 @@ typedef struct {
} esp_ble_gap_past_params_t;
#endif // #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)

typedef enum{
ESP_BLE_NETWORK_PRIVACY_MODE = 0X00, /*!< Network Privacy Mode for peer device (default) */
ESP_BLE_DEVICE_PRIVACY_MODE = 0X01, /*!< Device Privacy Mode for peer device */
} esp_ble_privacy_mode_t;

/**
* @brief Gap callback parameters union
*/
Expand Down Expand Up @@ -1498,6 +1504,12 @@ typedef union {
uint16_t param_len; /*!< The length of parameter buffer */
uint8_t *p_param_buf; /*!< The point of parameter buffer */
} vendor_cmd_cmpl; /*!< Event parameter of ESP_GAP_BLE_VENDOR_CMD_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_SET_PRIVACY_MODE_COMPLETE_EVT
*/
struct ble_set_privacy_mode_cmpl_evt_param {
esp_bt_status_t status; /*!< Indicate privacy mode set operation success status */
} set_privacy_mode_cmpl; /*!< Event parameter of ESP_GAP_BLE_SET_PRIVACY_MODE_COMPLETE_EVT */
} esp_ble_gap_cb_param_t;

/**
Expand Down Expand Up @@ -2631,6 +2643,21 @@ esp_err_t esp_ble_gap_clear_advertising(void);
*/
esp_err_t esp_ble_gap_vendor_command_send(esp_ble_vendor_cmd_params_t *vendor_cmd_param);

/**
* @brief This function set the privacy mode of the device in resolving list.
*
* @note This feature is not supported on ESP32.
*
* @param[in] addr_type: The address type of the peer identity address (BLE_ADDR_TYPE_PUBLIC or BLE_ADDR_TYPE_RANDOM).
* @param[in] addr: The peer identity address of the device.
* @param[in] mode: The privacy mode of the device.
*
* @return
* - ESP_OK : success
* - other : failed
*/
esp_err_t esp_ble_gap_set_privacy_mode(esp_ble_addr_type_t addr_type, esp_bd_addr_t addr, esp_ble_privacy_mode_t mode);

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 7 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 @@ -5694,6 +5694,13 @@ void bta_dm_ble_gap_add_dev_to_resolving_list(tBTA_DM_MSG *p_data)
p_data->add_dev_to_resolving_list.p_add_dev_to_resolving_list_callback);
}

void bta_dm_ble_gap_set_privacy_mode(tBTA_DM_MSG *p_data)
{
APPL_TRACE_API("%s, privacy_mode = %d", __func__, p_data->ble_set_privacy_mode.privacy_mode);
BTM_BleSetPrivacyMode(p_data->ble_set_privacy_mode.addr_type, p_data->ble_set_privacy_mode.addr,
p_data->ble_set_privacy_mode.privacy_mode, p_data->ble_set_privacy_mode.p_cback);
}

#if (BLE_50_FEATURE_SUPPORT == TRUE)
void bta_dm_ble_gap_dtm_enhance_tx_start(tBTA_DM_MSG *p_data)
{
Expand Down
15 changes: 15 additions & 0 deletions components/bt/host/bluedroid/bta/dm/bta_dm_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,21 @@ void BTA_DmBleDtmStop(tBTA_DTM_CMD_CMPL_CBACK *p_dtm_cmpl_cback)
}
}

void BTA_DmBleSetPrivacyMode(uint8_t addr_type, BD_ADDR addr, uint8_t privacy_mode, tBTA_SET_PRIVACY_MODE_CMPL_CBACK *p_cback)
{
tBTA_DM_API_SET_PRIVACY_MODE *p_msg;

if ((p_msg = (tBTA_DM_API_SET_PRIVACY_MODE *)osi_malloc(sizeof(tBTA_DM_API_SET_PRIVACY_MODE)))
!= NULL) {
p_msg->hdr.event = BTA_DM_API_SET_PRIVACY_MODE_EVT;
p_msg->addr_type = addr_type;
memcpy(p_msg->addr, addr, sizeof(BD_ADDR));
p_msg->privacy_mode = privacy_mode;
p_msg->p_cback = p_cback;
bta_sys_sendmsg(p_msg);
}
}

#endif

/*******************************************************************************
Expand Down
1 change: 1 addition & 0 deletions components/bt/host/bluedroid/bta/dm/bta_dm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
bta_dm_ble_gap_clear_adv, /* BTA_DM_API_BLE_CLEAR_ADV_EVT */
bta_dm_ble_gap_set_rpa_timeout, /* BTA_DM_API_SET_RPA_TIMEOUT_EVT */
bta_dm_ble_gap_add_dev_to_resolving_list, /* BTA_DM_API_ADD_DEV_TO_RESOLVING_LIST_EVT */
bta_dm_ble_gap_set_privacy_mode, /* BTA_DM_API_SET_PRIVACY_MODE_EVT */
#endif
};

Expand Down
11 changes: 11 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 @@ -216,6 +216,7 @@ enum {
BTA_DM_API_BLE_CLEAR_ADV_EVT,
BTA_DM_API_SET_RPA_TIMEOUT_EVT,
BTA_DM_API_ADD_DEV_TO_RESOLVING_LIST_EVT,
BTA_DM_API_SET_PRIVACY_MODE_EVT,
#endif
BTA_DM_MAX_EVT
};
Expand Down Expand Up @@ -899,6 +900,14 @@ typedef struct {
tBTA_CLEAR_ADV_CMPL_CBACK *p_clear_adv_cback;
} tBTA_DM_API_CLEAR_ADV;

typedef struct {
BT_HDR hdr;
tBLE_ADDR_TYPE addr_type;
BD_ADDR addr;
UINT8 privacy_mode;
tBTA_SET_PRIVACY_MODE_CMPL_CBACK *p_cback;
} tBTA_DM_API_SET_PRIVACY_MODE;

#endif /* BLE_INCLUDED */

/* data type for BTA_DM_API_REMOVE_ACL_EVT */
Expand Down Expand Up @@ -1295,6 +1304,7 @@ typedef union {
tBTA_DM_API_BLE_DTM_RX_START dtm_rx_start;
tBTA_DM_API_BLE_DTM_STOP dtm_stop;
tBTA_DM_API_CLEAR_ADV ble_clear_adv;
tBTA_DM_API_SET_PRIVACY_MODE ble_set_privacy_mode;
#endif

tBTA_DM_API_REMOVE_ACL remove_acl;
Expand Down Expand Up @@ -1733,6 +1743,7 @@ extern void bta_dm_ble_gap_dtm_stop(tBTA_DM_MSG *p_data);
extern void bta_dm_ble_gap_clear_adv(tBTA_DM_MSG *p_data);
extern void bta_dm_ble_gap_set_rpa_timeout(tBTA_DM_MSG *p_data);
extern void bta_dm_ble_gap_add_dev_to_resolving_list(tBTA_DM_MSG *p_data);
extern void bta_dm_ble_gap_set_privacy_mode(tBTA_DM_MSG *p_data);
#if (BLE_50_FEATURE_SUPPORT == TRUE)
extern void bta_dm_ble_gap_dtm_enhance_tx_start(tBTA_DM_MSG *p_data);
extern void bta_dm_ble_gap_dtm_enhance_rx_start(tBTA_DM_MSG *p_data);
Expand Down
4 changes: 4 additions & 0 deletions components/bt/host/bluedroid/bta/include/bta/bta_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ typedef tBTM_SET_RPA_TIMEOUT_CMPL_CBACK tBTA_SET_RPA_TIMEOUT_CMPL_CBACK;

typedef tBTM_ADD_DEV_TO_RESOLVING_LIST_CMPL_CBACK tBTA_ADD_DEV_TO_RESOLVING_LIST_CMPL_CBACK;

typedef tBTM_SET_PRIVACY_MODE_CMPL_CBACK tBTA_SET_PRIVACY_MODE_CMPL_CBACK;

typedef tBTM_CMPL_CB tBTA_CMPL_CB;

typedef tBTM_VSC_CMPL tBTA_VSC_CMPL;
Expand Down Expand Up @@ -2809,6 +2811,8 @@ extern void BTA_DmBleDtmTxStart(uint8_t tx_channel, uint8_t len_of_data, uint8_t
extern void BTA_DmBleDtmRxStart(uint8_t rx_channel, tBTA_DTM_CMD_CMPL_CBACK *p_dtm_cmpl_cback);
extern void BTA_DmBleDtmStop(tBTA_DTM_CMD_CMPL_CBACK *p_dtm_cmpl_cback);

extern void BTA_DmBleSetPrivacyMode(uint8_t addr_type, BD_ADDR addr, uint8_t privacy_mode, tBTA_SET_PRIVACY_MODE_CMPL_CBACK *p_cback);

/*******************************************************************************
**
** Function BTA_DmBleSetStorageParams
Expand Down
30 changes: 30 additions & 0 deletions components/bt/host/bluedroid/btc/profile/std/gap/btc_gap_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,25 @@ static void btc_ble_vendor_hci_cmd_complete_callback(tBTA_VSC_CMPL *p_param)
}
}

static void btc_ble_set_privacy_mode_callback(UINT8 status)
{
esp_ble_gap_cb_param_t param;
bt_status_t ret;
btc_msg_t msg = {0};

msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_GAP_BLE;
msg.act = ESP_GAP_BLE_SET_PRIVACY_MODE_COMPLETE_EVT;

param.set_privacy_mode_cmpl.status = btc_btm_status_to_esp_status(status);

ret = btc_transfer_context(&msg, &param, sizeof(esp_ble_gap_cb_param_t), NULL, NULL);

if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
}
}

void btc_get_whitelist_size(uint16_t *length)
{
BTM_BleGetWhiteListSize(length);
Expand Down Expand Up @@ -1497,6 +1516,13 @@ static void btc_ble_dtm_stop(tBTA_DTM_CMD_CMPL_CBACK *p_dtm_cmpl_cback)
BTA_DmBleDtmStop(p_dtm_cmpl_cback);
}

static void btc_ble_set_privacy_mode(uint8_t addr_type,
BD_ADDR addr,
uint8_t privacy_mode,
tBTA_SET_PRIVACY_MODE_CMPL_CBACK *p_cback)
{
BTA_DmBleSetPrivacyMode(addr_type, addr, privacy_mode, p_cback);
}

void btc_gap_ble_cb_handler(btc_msg_t *msg)
{
Expand Down Expand Up @@ -2323,6 +2349,10 @@ void btc_gap_ble_call_handler(btc_msg_t *msg)
arg->vendor_cmd_send.p_param_buf,
btc_ble_vendor_hci_cmd_complete_callback);
break;
case BTC_GAP_BLE_SET_PRIVACY_MODE:
btc_ble_set_privacy_mode(arg->set_privacy_mode.addr_type, arg->set_privacy_mode.addr,
arg->set_privacy_mode.privacy_mode, btc_ble_set_privacy_mode_callback);
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ typedef enum {
BTC_GAP_BLE_ACT_SET_RESOLVABLE_PRIVATE_ADDRESS_TIMEOUT,
BTC_GAP_BLE_ACT_ADD_DEVICE_TO_RESOLVING_LIST,
BTC_GAP_BLE_ACT_VENDOR_HCI_CMD_EVT,
BTC_GAP_BLE_SET_PRIVACY_MODE,
} btc_gap_ble_act_t;

/* btc_ble_gap_args_t */
Expand Down Expand Up @@ -267,9 +268,15 @@ typedef union {
uint8_t param_len;
uint8_t *p_param_buf;
} vendor_cmd_send;
// BTC_GAP_BLE_SET_PRIVACY_MODE
struct set_privacy_mode {
esp_ble_addr_type_t addr_type;
esp_bd_addr_t addr;
uint8_t privacy_mode;
} set_privacy_mode;
} btc_ble_gap_args_t;
#if (BLE_50_FEATURE_SUPPORT == TRUE)

#if (BLE_50_FEATURE_SUPPORT == TRUE)
typedef union {
struct read_phy_args {
esp_bd_addr_t bd_addr;
Expand Down
11 changes: 11 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_ble_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4727,6 +4727,17 @@ BOOLEAN BTM_BleAddDevToResolvingList(BD_ADDR addr,
return TRUE;
}

BOOLEAN BTM_BleSetPrivacyMode(UINT8 addr_type, BD_ADDR bd_addr, UINT8 privacy_mode, tBTM_SET_PRIVACY_MODE_CMPL_CBACK *p_callback)
{
if (btsnd_hcic_ble_set_privacy_mode(addr_type, bd_addr, privacy_mode) != TRUE) {
BTM_TRACE_ERROR("LE SetPrivacyMode Mode=%d: error", privacy_mode);
return FALSE;
}

btm_cb.devcb.p_set_privacy_mode_cmpl_cb = p_callback;
return TRUE;
}

bool btm_ble_adv_pkt_ready(void)
{
tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
Expand Down
28 changes: 28 additions & 0 deletions components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,34 @@ void btm_ble_set_rpa_timeout_complete(UINT8 *p, UINT16 evt_len)

}

/*******************************************************************************
**
** Function btm_ble_set_privacy_mode_complete
**
** Description This function is called when the LE Set Privacy Mode command completes.
**
** Parameters p: Pointer to the command complete event data.
** evt_len: Length of the event data.
**
** Returns void
**
*******************************************************************************/
void btm_ble_set_privacy_mode_complete(UINT8 *p, UINT16 evt_len)
{
UINT8 status;

// Extract the status of the command completion from the event data
STREAM_TO_UINT8(status, p);

BTM_TRACE_DEBUG("%s status = 0x%x", __func__, status);

tBTM_SET_PRIVACY_MODE_CMPL_CBACK *p_cb = btm_cb.devcb.p_set_privacy_mode_cmpl_cb;

if (p_cb) {
(*p_cb)(status);
}
}

/*******************************************************************************
VSC that implement controller based privacy
********************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ 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);
void btm_ble_set_privacy_mode_complete(UINT8 *p, UINT16 evt_len);
#endif

void btm_ble_multi_adv_configure_rpa (tBTM_BLE_MULTI_ADV_INST *p_inst);
Expand Down
3 changes: 3 additions & 0 deletions components/bt/host/bluedroid/stack/btm/include/btm_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ tBTM_SET_RPA_TIMEOUT_CMPL_CBACK *p_ble_set_rpa_timeout_cmpl_cb; /* Callback fun
ble set rpa timeout is completed */

tBTM_ADD_DEV_TO_RESOLVING_LIST_CMPL_CBACK *p_add_dev_to_resolving_list_cmpl_cb;

tBTM_SET_PRIVACY_MODE_CMPL_CBACK *p_set_privacy_mode_cmpl_cb;

tBTM_CMPL_CB *p_le_test_cmd_cmpl_cb; /* Callback function to be called when
LE test mode command has been sent successfully */

Expand Down
5 changes: 4 additions & 1 deletion components/bt/host/bluedroid/stack/btu/btu_hcif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,10 @@ static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_l
case HCI_BLE_SET_RAND_PRIV_ADDR_TIMOUT:
btm_ble_set_rpa_timeout_complete(p, evt_len);
break;
case HCI_BLE_SET_PRIVACY_MODE:
btm_ble_set_privacy_mode_complete(p, evt_len);
break;
#endif // #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
#if (BLE_50_FEATURE_SUPPORT == TRUE)
case HCI_BLE_SET_EXT_ADV_PARAM:
case HCI_BLE_SET_EXT_ADV_DATA:
Expand Down Expand Up @@ -1133,7 +1137,6 @@ static void btu_hcif_hdl_command_complete (UINT16 opcode, UINT8 *p, UINT16 evt_l
break;
}
#endif // #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)
#endif
#endif /* (BLE_INCLUDED == TRUE) */

default: {
Expand Down
24 changes: 24 additions & 0 deletions components/bt/host/bluedroid/stack/hcic/hciblecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1909,4 +1909,28 @@ UINT8 btsnd_hcic_ble_set_default_periodic_adv_sync_trans_params(UINT8 mode, UINT
return btu_hcif_send_cmd_sync(LOCAL_BR_EDR_CONTROLLER_ID, p);
}
#endif // #if (BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER == TRUE)

UINT8 btsnd_hcic_ble_set_privacy_mode(UINT8 addr_type, BD_ADDR addr, UINT8 privacy_mode)
{
BT_HDR *p;
UINT8 *pp;

if ((p = HCI_GET_CMD_BUF (HCIC_PARAM_SIZE_SET_PRIVACY_MODE)) == NULL) {
return (FALSE);
}

pp = (UINT8 *)(p + 1);
p->len = HCIC_PREAMBLE_SIZE + HCIC_PARAM_SIZE_SET_PRIVACY_MODE;
p->offset = 0;

UINT16_TO_STREAM(pp, HCI_BLE_SET_PRIVACY_MODE);
UINT8_TO_STREAM(pp, HCIC_PARAM_SIZE_SET_PRIVACY_MODE);

UINT8_TO_STREAM(pp, addr_type);
BDADDR_TO_STREAM(pp, addr);
UINT8_TO_STREAM(pp, privacy_mode);

btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
return (TRUE);
}
#endif
Loading

0 comments on commit d84b40d

Please sign in to comment.