Skip to content

Commit 8795e7a

Browse files
committed
Merge branch 'bugfix/provide_random_addr_api_v5.3' into 'release/v5.3'
fix(wifi_prov): Added API to set random address (v5.3) See merge request espressif/esp-idf!32325
2 parents 509bd0e + a2666d6 commit 8795e7a

File tree

9 files changed

+121
-27
lines changed

9 files changed

+121
-27
lines changed

components/protocomm/include/transports/protocomm_ble.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef enum {
4141
* (29 - (BLE device name length) - 2). */
4242
#define MAX_BLE_MANUFACTURER_DATA_LEN 29
4343

44+
#define BLE_ADDR_LEN 6
4445
/**
4546
* @brief This structure maps handler required by protocomm layer to
4647
* UUIDs which are used to uniquely identify BLE characteristics
@@ -137,6 +138,10 @@ typedef struct protocomm_ble_config {
137138
*/
138139
unsigned ble_link_encryption:1;
139140

141+
/**
142+
* BLE address
143+
*/
144+
uint8_t *ble_addr;
140145
} protocomm_ble_config_t;
141146

142147
/**

components/protocomm/src/simple_ble/simple_ble.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -49,12 +49,24 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
4949
switch (event) {
5050
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
5151
adv_config_done &= (~adv_config_flag);
52+
53+
if (g_ble_cfg_p->ble_addr) {
54+
esp_ble_gap_set_rand_addr(g_ble_cfg_p->ble_addr);
55+
g_ble_cfg_p->adv_params.own_addr_type = BLE_ADDR_TYPE_RANDOM;
56+
}
57+
5258
if (adv_config_done == 0) {
5359
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
5460
}
5561
break;
5662
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
5763
adv_config_done &= (~scan_rsp_config_flag);
64+
65+
if (g_ble_cfg_p->ble_addr) {
66+
esp_ble_gap_set_rand_addr(g_ble_cfg_p->ble_addr);
67+
g_ble_cfg_p->adv_params.own_addr_type = BLE_ADDR_TYPE_RANDOM;
68+
}
69+
5870
if (adv_config_done == 0) {
5971
esp_ble_gap_start_advertising(&g_ble_cfg_p->adv_params);
6072
}

components/protocomm/src/simple_ble/simple_ble.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ typedef struct {
5151
unsigned ble_bonding:1;
5252
/** BLE Secure Connection flag */
5353
unsigned ble_sm_sc:1;
54+
/** BLE Address */
55+
uint8_t *ble_addr;
56+
5457
} simple_ble_cfg_t;
5558

5659

components/protocomm/src/transports/protocomm_ble.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -103,6 +103,7 @@ static esp_ble_adv_params_t adv_params = {
103103
static char *protocomm_ble_device_name = NULL;
104104
static uint8_t *protocomm_ble_mfg_data = NULL;
105105
static size_t protocomm_ble_mfg_data_len;
106+
static uint8_t *protocomm_ble_addr = NULL;
106107

107108
static void hexdump(const char *msg, uint8_t *buf, int len)
108109
{
@@ -524,6 +525,10 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con
524525
protocomm_ble_mfg_data_len = config->manufacturer_data_len;
525526
}
526527

528+
if (config->ble_addr != NULL) {
529+
protocomm_ble_addr = config->ble_addr;
530+
}
531+
527532
protoble_internal = (_protocomm_ble_internal_t *) calloc(1, sizeof(_protocomm_ble_internal_t));
528533
if (protoble_internal == NULL) {
529534
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
594599
ble_config->ble_bonding = config->ble_bonding;
595600
ble_config->ble_sm_sc = config->ble_sm_sc;
596601

602+
if (config->ble_addr != NULL) {
603+
ble_config->ble_addr = protocomm_ble_addr;
604+
}
605+
597606
if (ble_config->gatt_db_count == -1) {
598607
ESP_LOGE(TAG, "Invalid GATT database count");
599608
simple_ble_deinit();

components/protocomm/src/transports/protocomm_nimble.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -79,6 +79,7 @@ static struct ble_hs_adv_fields adv_data, resp_data;
7979
static uint8_t *protocomm_ble_mfg_data;
8080
static size_t protocomm_ble_mfg_data_len;
8181

82+
static uint8_t *protocomm_ble_addr;
8283
/**********************************************************************
8384
* Maintain database of uuid_name addresses to free memory afterwards *
8485
**********************************************************************/
@@ -130,6 +131,8 @@ typedef struct {
130131
unsigned ble_sm_sc:1;
131132
/** BLE Link Encryption flag */
132133
unsigned ble_link_encryption:1;
134+
/** BLE address */
135+
uint8_t *ble_addr;
133136
} simple_ble_cfg_t;
134137

135138
static simple_ble_cfg_t *ble_cfg_p;
@@ -461,10 +464,25 @@ simple_ble_on_sync(void)
461464
{
462465
int rc;
463466

464-
rc = ble_hs_util_ensure_addr(0);
465-
if (rc != 0) {
466-
ESP_LOGE(TAG, "Error loading address");
467-
return;
467+
if (protocomm_ble_addr) {
468+
rc = ble_hs_id_set_rnd(protocomm_ble_addr);
469+
if (rc != 0) {
470+
ESP_LOGE(TAG,"Error in setting address");
471+
return;
472+
}
473+
474+
rc = ble_hs_util_ensure_addr(1);
475+
if (rc != 0) {
476+
ESP_LOGE(TAG,"Error loading address");
477+
return;
478+
}
479+
}
480+
else {
481+
rc = ble_hs_util_ensure_addr(0);
482+
if (rc != 0) {
483+
ESP_LOGE(TAG, "Error loading address");
484+
return;
485+
}
468486
}
469487

470488
/* 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)
727745
gatt_db_svcs->type = BLE_GATT_SVC_TYPE_PRIMARY;
728746

729747
/* Allocate (number of characteristics + 1) memory for characteristics, the
730-
* addtional characteristic consist of all 0s indicating end of
748+
* additional characteristic consist of all 0s indicating end of
731749
* characteristics */
732750
gatt_db_svcs->characteristics = (struct ble_gatt_chr_def *) calloc((char_count + 1),
733751
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
781799
rc = ble_gatt_add_char_dsc((void *) (*gatt_db_svcs)->characteristics,
782800
i, BLE_GATT_UUID_CHAR_DSC);
783801
if (rc != 0) {
784-
ESP_LOGE(TAG, "Error adding GATT Discriptor !!");
802+
ESP_LOGE(TAG, "Error adding GATT Descriptor !!");
785803
return rc;
786804
}
787805
}
@@ -813,6 +831,11 @@ static void protocomm_ble_cleanup(void)
813831
protocomm_ble_mfg_data = NULL;
814832
protocomm_ble_mfg_data_len = 0;
815833
}
834+
835+
if (protocomm_ble_addr) {
836+
free(protocomm_ble_addr);
837+
protocomm_ble_addr = NULL;
838+
}
816839
}
817840

818841
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
970993
ble_config->ble_bonding = config->ble_bonding;
971994
ble_config->ble_sm_sc = config->ble_sm_sc;
972995

996+
if (config->ble_addr != NULL) {
997+
protocomm_ble_addr = config->ble_addr;
998+
}
999+
9731000
if (populate_gatt_db(&ble_config->gatt_db, config) != 0) {
9741001
ESP_LOGE(TAG, "Error populating GATT Database");
9751002
free_gatt_ble_misc_memory(ble_config);

components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
/*
2+
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
146

157
#pragma once
168

@@ -65,9 +57,9 @@ void wifi_prov_scheme_ble_event_cb_free_bt (void *user_data, wifi_prov_cb_event
6557
* the default UUID will be used.
6658
*
6759
* @note The data being pointed to by the argument must be valid
68-
* atleast till provisioning is started. Upon start, the
60+
* at least till provisioning is started. Upon start, the
6961
* manager will store an internal copy of this UUID, and
70-
* this data can be freed or invalidated afterwords.
62+
* this data can be freed or invalidated afterwards.
7163
*
7264
* @param[in] uuid128 A custom 128 bit UUID
7365
*
@@ -100,6 +92,31 @@ esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128);
10092
*/
10193
esp_err_t wifi_prov_scheme_ble_set_mfg_data(uint8_t *mfg_data, ssize_t mfg_data_len);
10294

95+
/**
96+
* @brief Set Bluetooth Random address
97+
*
98+
* This must be called before starting provisioning, i.e. before
99+
* making a call to wifi_prov_mgr_start_provisioning().
100+
*
101+
* This API can be used in cases where a new identity address is to be used during
102+
* provisioning. This will result in this device being treated as a new device by remote
103+
* devices.
104+
*
105+
* @note This API will change the existing BD address for the device. The address once
106+
* set will remain unchanged until BLE stack tear down happens when
107+
* wifi_prov_mgr_deinit is invoked.
108+
*
109+
* This API is only to be called to set random address. Re-invoking this API
110+
* after provisioning is started will have no effect.
111+
*
112+
* @param[in] rand_addr The static random address to be set of length 6 bytes.
113+
*
114+
* @return
115+
* - ESP_OK : Success
116+
* - ESP_ERR_INVALID_ARG : Null argument
117+
*/
118+
esp_err_t wifi_prov_scheme_ble_set_random_addr(const uint8_t *rand_addr);
119+
103120
#ifdef __cplusplus
104121
}
105122
#endif

components/wifi_provisioning/src/scheme_ble.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static const char *TAG = "wifi_prov_scheme_ble";
2222
extern const wifi_prov_scheme_t wifi_prov_scheme_ble;
2323

2424
static uint8_t *custom_service_uuid;
25-
25+
static uint8_t *custom_ble_addr;
2626
static uint8_t *custom_manufacturer_data;
2727
static size_t custom_manufacturer_data_len;
2828

@@ -59,6 +59,22 @@ static esp_err_t prov_start(protocomm_t *pc, void *config)
5959
return ESP_OK;
6060
}
6161

62+
esp_err_t wifi_prov_scheme_ble_set_random_addr(const uint8_t *addr)
63+
{
64+
if (!addr) {
65+
return ESP_ERR_INVALID_ARG;
66+
}
67+
68+
custom_ble_addr = (uint8_t *) malloc(BLE_ADDR_LEN);
69+
if (custom_ble_addr == NULL) {
70+
ESP_LOGE(TAG, "Error allocating memory for random address");
71+
return ESP_ERR_NO_MEM;
72+
}
73+
74+
memcpy(custom_ble_addr, addr, BLE_ADDR_LEN);
75+
return ESP_OK;
76+
}
77+
6278
esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128)
6379
{
6480
if (!uuid128) {
@@ -159,6 +175,12 @@ static esp_err_t set_config_service(void *config, const char *service_name, cons
159175
ble_config->manufacturer_data_len = 0;
160176
}
161177

178+
if (custom_ble_addr){
179+
ble_config->ble_addr = custom_ble_addr;
180+
} else {
181+
ble_config->ble_addr = NULL;
182+
}
183+
162184
return ESP_OK;
163185
}
164186

examples/provisioning/wifi_prov_mgr/main/app_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
136136
#ifdef CONFIG_EXAMPLE_RESET_PROV_MGR_ON_FAILURE
137137
retries++;
138138
if (retries >= CONFIG_EXAMPLE_PROV_MGR_MAX_RETRY_CNT) {
139-
ESP_LOGI(TAG, "Failed to connect with provisioned AP, reseting provisioned credentials");
139+
ESP_LOGI(TAG, "Failed to connect with provisioned AP, resetting provisioned credentials");
140140
wifi_prov_mgr_reset_sm_state_on_failure();
141141
retries = 0;
142142
}

tools/ci/check_copyright_ignore.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,6 @@ components/spi_flash/spi_flash_chip_boya.c
730730
components/spi_flash/spi_flash_chip_issi.c
731731
components/tcp_transport/include/esp_transport_ws.h
732732
components/vfs/include/esp_vfs_common.h
733-
components/wifi_provisioning/include/wifi_provisioning/scheme_ble.h
734733
components/wifi_provisioning/include/wifi_provisioning/scheme_console.h
735734
components/wifi_provisioning/include/wifi_provisioning/scheme_softap.h
736735
components/wifi_provisioning/include/wifi_provisioning/wifi_scan.h

0 commit comments

Comments
 (0)