Skip to content

Commit f4f01b6

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
Use random BT addr; add esp32_bt_wipe_config
PUBLISHED_FROM=d6634562b654a87b72c2c1168d159da3afb7e91e
1 parent 4552336 commit f4f01b6

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

include/esp32/esp32_bt_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ void esp32_bt_gatts_auth_cmpl(const esp_bd_addr_t addr);
2626

2727
void esp32_bt_set_is_advertising(bool is_advertising);
2828

29+
/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */
30+
bool esp32_bt_wipe_config(void);
31+
2932
#ifdef __cplusplus
3033
}
3134
#endif

mos.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ config_schema:
1919
- ["bt.keep_enabled", "b", false, {title: "By default, BT will be disabled once WiFi is configured and connects. Set this to true to keep BT enabled."}]
2020
- ["bt.allow_pairing", "b", true, {title: "Allow pairing/bonding with other devices"}]
2121
- ["bt.max_paired_devices", "i", -1, {title: "Max number of paired devices; -1 - no limit"}]
22+
- ["bt.random_address", "b", true, {title: "Use random BT address"}]
2223
- ["bt.gatts", "o", {title: "GATTS settings"}]
2324
- ["bt.gatts.min_sec_level", "i", 0, {title: "0 - no auth required, 1 - encryption reqd, 2 - encryption + MITM reqd"}]
2425
- ["bt.gatts.require_pairing", "b", false, {title: "Require device to be paired before accessing services"}]

src/esp32/esp32_bt.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "esp_bt_main.h"
1717
#include "esp_gap_ble_api.h"
1818
#include "esp_gatt_common_api.h"
19+
#include "nvs.h"
1920

2021
#include "common/mg_str.h"
2122

@@ -167,6 +168,20 @@ int mgos_bt_ble_get_num_paired_devices(void) {
167168
return esp_ble_get_bond_device_num();
168169
}
169170

171+
/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */
172+
bool esp32_bt_wipe_config(void) {
173+
bool result = false;
174+
nvs_handle h = 0;
175+
/* CONFIG_FILE_PATH form btc_config.c */
176+
if (nvs_open("bt_config.conf", NVS_READWRITE, &h) != ESP_OK) goto clean;
177+
if (nvs_erase_key(h, "bt_cfg_key") != ESP_OK) goto clean;
178+
result = true;
179+
180+
clean:
181+
if (h != 0) nvs_close(h);
182+
return result;
183+
}
184+
170185
bool mgos_bt_common_init(void) {
171186
bool ret = false;
172187
if (!mgos_sys_config_get_bt_enable()) {

src/esp32/esp32_bt_gap.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "esp_bt.h"
1414
#include "esp_bt_defs.h"
1515
#include "esp_gap_ble_api.h"
16+
#include "nvs.h"
1617

1718
#include "frozen/frozen.h"
1819

@@ -60,7 +61,7 @@ static esp_ble_adv_params_t s_adv_params = {
6061
.adv_int_min = 0x50, /* 0x100 * 0.625 = 100 ms */
6162
.adv_int_max = 0x100, /* 0x200 * 0.625 = 200 ms */
6263
.adv_type = ADV_TYPE_IND,
63-
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
64+
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
6465
.channel_map = ADV_CHNL_ALL,
6566
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
6667
};
@@ -567,5 +568,13 @@ bool esp32_bt_gap_init(void) {
567568
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size,
568569
sizeof(key_size));
569570

571+
if (mgos_sys_config_get_bt_random_address()) {
572+
esp_ble_gap_config_local_privacy(true);
573+
s_adv_params.own_addr_type = BLE_ADDR_TYPE_RANDOM;
574+
} else {
575+
esp_ble_gap_config_local_privacy(false);
576+
s_adv_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
577+
}
578+
570579
return mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable());
571580
}

0 commit comments

Comments
 (0)