From 9c38d1f29ef05175b72b7416b3610b8c902b17d5 Mon Sep 17 00:00:00 2001 From: zhanghaipeng Date: Mon, 15 Apr 2024 20:38:44 +0800 Subject: [PATCH] feat(ble/bluedroid): Support create BLE static random address and NRPA --- .../bt/host/bluedroid/api/esp_gap_ble_api.c | 21 +++++++++++- .../api/include/api/esp_gap_ble_api.h | 32 +++++++++++++------ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/components/bt/host/bluedroid/api/esp_gap_ble_api.c b/components/bt/host/bluedroid/api/esp_gap_ble_api.c index e572de4560..a412252979 100644 --- a/components/bt/host/bluedroid/api/esp_gap_ble_api.c +++ b/components/bt/host/bluedroid/api/esp_gap_ble_api.c @@ -13,7 +13,7 @@ #include "btc/btc_manage.h" #include "btc_gap_ble.h" #include "btc/btc_ble_storage.h" - +#include "esp_random.h" esp_err_t esp_ble_gap_register_callback(esp_gap_ble_cb_t callback) { @@ -188,6 +188,25 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_ return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_gap_args_t), NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL); } +esp_err_t esp_ble_gap_addr_create_static(esp_bd_addr_t rand_addr) +{ + // Static device address: First two bits are '11', rest is random + rand_addr[0] = 0xC0 | (esp_random() & 0x3F); + for (int i = 1; i < 6; i++) { + rand_addr[i] = esp_random() & 0xFF; // Randomize remaining bits + } + return ESP_OK; +} + +esp_err_t esp_ble_gap_addr_create_nrpa(esp_bd_addr_t rand_addr) +{ + // Non-resolvable private address: First two bits are '00', rest is random + rand_addr[0] = (esp_random() & 0x3F); + for (int i = 1; i < 6; i++) { + rand_addr[i] = esp_random() & 0xFF; // Randomize remaining bits + } + return ESP_OK; +} esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr) { diff --git a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h index 9ae1b3f686..902a00295f 100644 --- a/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h +++ b/components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h @@ -1716,13 +1716,13 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_ * * @param[in] rand_addr: The address to be configured. Refer to the table below for possible address subtypes: * - * | address [47:46] | Address Type | - * |-----------------|--------------------------| - * | 0b00 | Non-Resolvable Private | - * | | Address | - * |-----------------|--------------------------| - * | 0b11 | Static Random Address | - * |-----------------|--------------------------| + * | address [47:46] | Address Type | Corresponding API | + * |-----------------|-----------------------------|----------------------------------------| + * | 0b00 | Non-Resolvable Private | esp_ble_gap_addr_create_nrpa | + * | | Address (NRPA) | | + * |-----------------|-----------------------------|----------------------------------------| + * | 0b11 | Static Random Address | esp_ble_gap_addr_create_static | + * |-----------------|-----------------------------|----------------------------------------| * * @return * - ESP_OK : success @@ -1731,6 +1731,22 @@ esp_err_t esp_ble_gap_set_pkt_data_len(esp_bd_addr_t remote_device, uint16_t tx_ */ esp_err_t esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr); +/** + * @brief Create a static device address + * @param[out] rand_addr: Pointer to the buffer where the static device address will be stored. + * @return - ESP_OK : Success + * - Other : Failed + */ +esp_err_t esp_ble_gap_addr_create_static(esp_bd_addr_t rand_addr); + +/** + * @brief Create a non-resolvable private address (NRPA) + * @param[out] rand_addr: Pointer to the buffer where the NRPA will be stored. + * @return - ESP_OK : Success + * - Other : Failed + */ +esp_err_t esp_ble_gap_addr_create_nrpa(esp_bd_addr_t rand_addr); + /** * @brief This function sets the length of time the Controller uses a Resolvable Private Address * before generating and starting to use a new resolvable private address. @@ -1779,7 +1795,6 @@ esp_err_t esp_ble_gap_add_device_to_resolving_list(esp_bd_addr_t peer_addr, uint */ esp_err_t esp_ble_gap_clear_rand_addr(void); - /** * @brief Enable/disable privacy (including address resolution) on the local device * @@ -2118,7 +2133,6 @@ esp_err_t esp_ble_remove_bond_device(esp_bd_addr_t bd_addr); */ int esp_ble_get_bond_device_num(void); - /** * @brief Get the device from the security database list of peer device. * It will return the device bonded information immediately.