fix(wifi_prov): Added API to set random address

This commit is contained in:
Rahul Tank 2024-07-16 13:43:56 +05:30
parent e0991facf5
commit a2666d6f2c
9 changed files with 121 additions and 27 deletions

View File

@ -41,6 +41,7 @@ typedef enum {
* (29 - (BLE device name length) - 2). */ * (29 - (BLE device name length) - 2). */
#define MAX_BLE_MANUFACTURER_DATA_LEN 29 #define MAX_BLE_MANUFACTURER_DATA_LEN 29
#define BLE_ADDR_LEN 6
/** /**
* @brief This structure maps handler required by protocomm layer to * @brief This structure maps handler required by protocomm layer to
* UUIDs which are used to uniquely identify BLE characteristics * UUIDs which are used to uniquely identify BLE characteristics
@ -137,6 +138,10 @@ typedef struct protocomm_ble_config {
*/ */
unsigned ble_link_encryption:1; unsigned ble_link_encryption:1;
/**
* BLE address
*/
uint8_t *ble_addr;
} protocomm_ble_config_t; } protocomm_ble_config_t;
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -49,12 +49,24 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
switch (event) { switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT: case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~adv_config_flag); adv_config_done &= (~adv_config_flag);
if (g_ble_cfg_p->ble_addr) {
esp_ble_gap_set_rand_addr(g_ble_cfg_p->ble_addr);
g_ble_cfg_p->adv_params.own_addr_type = BLE_ADDR_TYPE_RANDOM;
}
if (adv_config_done == 0) { if (adv_config_done == 0) {
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params); esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
} }
break; break;
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT: case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~scan_rsp_config_flag); adv_config_done &= (~scan_rsp_config_flag);
if (g_ble_cfg_p->ble_addr) {
esp_ble_gap_set_rand_addr(g_ble_cfg_p->ble_addr);
g_ble_cfg_p->adv_params.own_addr_type = BLE_ADDR_TYPE_RANDOM;
}
if (adv_config_done == 0) { if (adv_config_done == 0) {
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params); esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
} }

View File

@ -51,6 +51,9 @@ typedef struct {
unsigned ble_bonding:1; unsigned ble_bonding:1;
/** BLE Secure Connection flag */ /** BLE Secure Connection flag */
unsigned ble_sm_sc:1; unsigned ble_sm_sc:1;
/** BLE Address */
uint8_t *ble_addr;
} simple_ble_cfg_t; } simple_ble_cfg_t;

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -103,6 +103,7 @@ static esp_ble_adv_params_t adv_params = {
static char *protocomm_ble_device_name = NULL; static char *protocomm_ble_device_name = NULL;
static uint8_t *protocomm_ble_mfg_data = NULL; static uint8_t *protocomm_ble_mfg_data = NULL;
static size_t protocomm_ble_mfg_data_len; static size_t protocomm_ble_mfg_data_len;
static uint8_t *protocomm_ble_addr = NULL;
static void hexdump(const char *msg, uint8_t *buf, int len) static void hexdump(const char *msg, uint8_t *buf, int len)
{ {
@ -524,6 +525,10 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con
protocomm_ble_mfg_data_len = config->manufacturer_data_len; protocomm_ble_mfg_data_len = config->manufacturer_data_len;
} }
if (config->ble_addr != NULL) {
protocomm_ble_addr = config->ble_addr;
}
protoble_internal = (_protocomm_ble_internal_t *) calloc(1, sizeof(_protocomm_ble_internal_t)); protoble_internal = (_protocomm_ble_internal_t *) calloc(1, sizeof(_protocomm_ble_internal_t));
if (protoble_internal == NULL) { if (protoble_internal == NULL) {
ESP_LOGE(TAG, "Error allocating internal protocomm structure"); ESP_LOGE(TAG, "Error allocating internal protocomm structure");
@ -594,6 +599,10 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con
ble_config->ble_bonding = config->ble_bonding; ble_config->ble_bonding = config->ble_bonding;
ble_config->ble_sm_sc = config->ble_sm_sc; ble_config->ble_sm_sc = config->ble_sm_sc;
if (config->ble_addr != NULL) {
ble_config->ble_addr = protocomm_ble_addr;
}
if (ble_config->gatt_db_count == -1) { if (ble_config->gatt_db_count == -1) {
ESP_LOGE(TAG, "Invalid GATT database count"); ESP_LOGE(TAG, "Invalid GATT database count");
simple_ble_deinit(); simple_ble_deinit();

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -79,6 +79,7 @@ static struct ble_hs_adv_fields adv_data, resp_data;
static uint8_t *protocomm_ble_mfg_data; static uint8_t *protocomm_ble_mfg_data;
static size_t protocomm_ble_mfg_data_len; static size_t protocomm_ble_mfg_data_len;
static uint8_t *protocomm_ble_addr;
/********************************************************************** /**********************************************************************
* Maintain database of uuid_name addresses to free memory afterwards * * Maintain database of uuid_name addresses to free memory afterwards *
**********************************************************************/ **********************************************************************/
@ -130,6 +131,8 @@ typedef struct {
unsigned ble_sm_sc:1; unsigned ble_sm_sc:1;
/** BLE Link Encryption flag */ /** BLE Link Encryption flag */
unsigned ble_link_encryption:1; unsigned ble_link_encryption:1;
/** BLE address */
uint8_t *ble_addr;
} simple_ble_cfg_t; } simple_ble_cfg_t;
static simple_ble_cfg_t *ble_cfg_p; static simple_ble_cfg_t *ble_cfg_p;
@ -461,10 +464,25 @@ simple_ble_on_sync(void)
{ {
int rc; int rc;
rc = ble_hs_util_ensure_addr(0); if (protocomm_ble_addr) {
if (rc != 0) { rc = ble_hs_id_set_rnd(protocomm_ble_addr);
ESP_LOGE(TAG, "Error loading address"); if (rc != 0) {
return; ESP_LOGE(TAG,"Error in setting address");
return;
}
rc = ble_hs_util_ensure_addr(1);
if (rc != 0) {
ESP_LOGE(TAG,"Error loading address");
return;
}
}
else {
rc = ble_hs_util_ensure_addr(0);
if (rc != 0) {
ESP_LOGE(TAG, "Error loading address");
return;
}
} }
/* Figure out address to use while advertising (no privacy for now) */ /* Figure out address to use while advertising (no privacy for now) */
@ -727,7 +745,7 @@ ble_gatt_add_primary_svcs(struct ble_gatt_svc_def *gatt_db_svcs, int char_count)
gatt_db_svcs->type = BLE_GATT_SVC_TYPE_PRIMARY; gatt_db_svcs->type = BLE_GATT_SVC_TYPE_PRIMARY;
/* Allocate (number of characteristics + 1) memory for characteristics, the /* Allocate (number of characteristics + 1) memory for characteristics, the
* addtional characteristic consist of all 0s indicating end of * additional characteristic consist of all 0s indicating end of
* characteristics */ * characteristics */
gatt_db_svcs->characteristics = (struct ble_gatt_chr_def *) calloc((char_count + 1), gatt_db_svcs->characteristics = (struct ble_gatt_chr_def *) calloc((char_count + 1),
sizeof(struct ble_gatt_chr_def)); sizeof(struct ble_gatt_chr_def));
@ -781,7 +799,7 @@ populate_gatt_db(struct ble_gatt_svc_def **gatt_db_svcs, const protocomm_ble_con
rc = ble_gatt_add_char_dsc((void *) (*gatt_db_svcs)->characteristics, rc = ble_gatt_add_char_dsc((void *) (*gatt_db_svcs)->characteristics,
i, BLE_GATT_UUID_CHAR_DSC); i, BLE_GATT_UUID_CHAR_DSC);
if (rc != 0) { if (rc != 0) {
ESP_LOGE(TAG, "Error adding GATT Discriptor !!"); ESP_LOGE(TAG, "Error adding GATT Descriptor !!");
return rc; return rc;
} }
} }
@ -813,6 +831,11 @@ static void protocomm_ble_cleanup(void)
protocomm_ble_mfg_data = NULL; protocomm_ble_mfg_data = NULL;
protocomm_ble_mfg_data_len = 0; protocomm_ble_mfg_data_len = 0;
} }
if (protocomm_ble_addr) {
free(protocomm_ble_addr);
protocomm_ble_addr = NULL;
}
} }
static void free_gatt_ble_misc_memory(simple_ble_cfg_t *ble_config) static void free_gatt_ble_misc_memory(simple_ble_cfg_t *ble_config)
@ -970,6 +993,10 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con
ble_config->ble_bonding = config->ble_bonding; ble_config->ble_bonding = config->ble_bonding;
ble_config->ble_sm_sc = config->ble_sm_sc; ble_config->ble_sm_sc = config->ble_sm_sc;
if (config->ble_addr != NULL) {
protocomm_ble_addr = config->ble_addr;
}
if (populate_gatt_db(&ble_config->gatt_db, config) != 0) { if (populate_gatt_db(&ble_config->gatt_db, config) != 0) {
ESP_LOGE(TAG, "Error populating GATT Database"); ESP_LOGE(TAG, "Error populating GATT Database");
free_gatt_ble_misc_memory(ble_config); free_gatt_ble_misc_memory(ble_config);

View File

@ -1,16 +1,8 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD /*
// * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
// Licensed under the Apache License, Version 2.0 (the "License"); *
// you may not use this file except in compliance with the License. * SPDX-License-Identifier: Apache-2.0
// You may obtain a copy of the License at */
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once #pragma once
@ -65,9 +57,9 @@ void wifi_prov_scheme_ble_event_cb_free_bt (void *user_data, wifi_prov_cb_event
* the default UUID will be used. * the default UUID will be used.
* *
* @note The data being pointed to by the argument must be valid * @note The data being pointed to by the argument must be valid
* atleast till provisioning is started. Upon start, the * at least till provisioning is started. Upon start, the
* manager will store an internal copy of this UUID, and * manager will store an internal copy of this UUID, and
* this data can be freed or invalidated afterwords. * this data can be freed or invalidated afterwards.
* *
* @param[in] uuid128 A custom 128 bit UUID * @param[in] uuid128 A custom 128 bit UUID
* *
@ -100,6 +92,31 @@ esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128);
*/ */
esp_err_t wifi_prov_scheme_ble_set_mfg_data(uint8_t *mfg_data, ssize_t mfg_data_len); esp_err_t wifi_prov_scheme_ble_set_mfg_data(uint8_t *mfg_data, ssize_t mfg_data_len);
/**
* @brief Set Bluetooth Random address
*
* This must be called before starting provisioning, i.e. before
* making a call to wifi_prov_mgr_start_provisioning().
*
* This API can be used in cases where a new identity address is to be used during
* provisioning. This will result in this device being treated as a new device by remote
* devices.
*
* @note This API will change the existing BD address for the device. The address once
* set will remain unchanged until BLE stack tear down happens when
* wifi_prov_mgr_deinit is invoked.
*
* This API is only to be called to set random address. Re-invoking this API
* after provisioning is started will have no effect.
*
* @param[in] rand_addr The static random address to be set of length 6 bytes.
*
* @return
* - ESP_OK : Success
* - ESP_ERR_INVALID_ARG : Null argument
*/
esp_err_t wifi_prov_scheme_ble_set_random_addr(const uint8_t *rand_addr);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -22,7 +22,7 @@ static const char *TAG = "wifi_prov_scheme_ble";
extern const wifi_prov_scheme_t wifi_prov_scheme_ble; extern const wifi_prov_scheme_t wifi_prov_scheme_ble;
static uint8_t *custom_service_uuid; static uint8_t *custom_service_uuid;
static uint8_t *custom_ble_addr;
static uint8_t *custom_manufacturer_data; static uint8_t *custom_manufacturer_data;
static size_t custom_manufacturer_data_len; static size_t custom_manufacturer_data_len;
@ -59,6 +59,22 @@ static esp_err_t prov_start(protocomm_t *pc, void *config)
return ESP_OK; return ESP_OK;
} }
esp_err_t wifi_prov_scheme_ble_set_random_addr(const uint8_t *addr)
{
if (!addr) {
return ESP_ERR_INVALID_ARG;
}
custom_ble_addr = (uint8_t *) malloc(BLE_ADDR_LEN);
if (custom_ble_addr == NULL) {
ESP_LOGE(TAG, "Error allocating memory for random address");
return ESP_ERR_NO_MEM;
}
memcpy(custom_ble_addr, addr, BLE_ADDR_LEN);
return ESP_OK;
}
esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128) esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128)
{ {
if (!uuid128) { if (!uuid128) {
@ -159,6 +175,12 @@ static esp_err_t set_config_service(void *config, const char *service_name, cons
ble_config->manufacturer_data_len = 0; ble_config->manufacturer_data_len = 0;
} }
if (custom_ble_addr){
ble_config->ble_addr = custom_ble_addr;
} else {
ble_config->ble_addr = NULL;
}
return ESP_OK; return ESP_OK;
} }

View File

@ -136,7 +136,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
#ifdef CONFIG_EXAMPLE_RESET_PROV_MGR_ON_FAILURE #ifdef CONFIG_EXAMPLE_RESET_PROV_MGR_ON_FAILURE
retries++; retries++;
if (retries >= CONFIG_EXAMPLE_PROV_MGR_MAX_RETRY_CNT) { if (retries >= CONFIG_EXAMPLE_PROV_MGR_MAX_RETRY_CNT) {
ESP_LOGI(TAG, "Failed to connect with provisioned AP, reseting provisioned credentials"); ESP_LOGI(TAG, "Failed to connect with provisioned AP, resetting provisioned credentials");
wifi_prov_mgr_reset_sm_state_on_failure(); wifi_prov_mgr_reset_sm_state_on_failure();
retries = 0; retries = 0;
} }

View File

@ -731,7 +731,6 @@ components/spi_flash/spi_flash_chip_boya.c
components/spi_flash/spi_flash_chip_issi.c components/spi_flash/spi_flash_chip_issi.c
components/tcp_transport/include/esp_transport_ws.h components/tcp_transport/include/esp_transport_ws.h
components/vfs/include/esp_vfs_common.h components/vfs/include/esp_vfs_common.h
components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h
components/wifi_provisioning/include/wifi_provisioning/scheme_console.h components/wifi_provisioning/include/wifi_provisioning/scheme_console.h
components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h
components/wifi_provisioning/include/wifi_provisioning/wifi_scan.h components/wifi_provisioning/include/wifi_provisioning/wifi_scan.h