From f4f01b6c3eea396155e9be8091f8e868c7db328b Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Mon, 18 Dec 2017 14:42:50 +0300 Subject: [PATCH] Use random BT addr; add esp32_bt_wipe_config PUBLISHED_FROM=d6634562b654a87b72c2c1168d159da3afb7e91e --- include/esp32/esp32_bt_internal.h | 3 +++ mos.yml | 1 + src/esp32/esp32_bt.c | 15 +++++++++++++++ src/esp32/esp32_bt_gap.c | 11 ++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/esp32/esp32_bt_internal.h b/include/esp32/esp32_bt_internal.h index 4705fd5..b9a33f7 100644 --- a/include/esp32/esp32_bt_internal.h +++ b/include/esp32/esp32_bt_internal.h @@ -26,6 +26,9 @@ void esp32_bt_gatts_auth_cmpl(const esp_bd_addr_t addr); void esp32_bt_set_is_advertising(bool is_advertising); +/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */ +bool esp32_bt_wipe_config(void); + #ifdef __cplusplus } #endif diff --git a/mos.yml b/mos.yml index 3caeeca..8cb5636 100644 --- a/mos.yml +++ b/mos.yml @@ -19,6 +19,7 @@ config_schema: - ["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."}] - ["bt.allow_pairing", "b", true, {title: "Allow pairing/bonding with other devices"}] - ["bt.max_paired_devices", "i", -1, {title: "Max number of paired devices; -1 - no limit"}] + - ["bt.random_address", "b", true, {title: "Use random BT address"}] - ["bt.gatts", "o", {title: "GATTS settings"}] - ["bt.gatts.min_sec_level", "i", 0, {title: "0 - no auth required, 1 - encryption reqd, 2 - encryption + MITM reqd"}] - ["bt.gatts.require_pairing", "b", false, {title: "Require device to be paired before accessing services"}] diff --git a/src/esp32/esp32_bt.c b/src/esp32/esp32_bt.c index 0b777ec..14469f2 100644 --- a/src/esp32/esp32_bt.c +++ b/src/esp32/esp32_bt.c @@ -16,6 +16,7 @@ #include "esp_bt_main.h" #include "esp_gap_ble_api.h" #include "esp_gatt_common_api.h" +#include "nvs.h" #include "common/mg_str.h" @@ -167,6 +168,20 @@ int mgos_bt_ble_get_num_paired_devices(void) { return esp_ble_get_bond_device_num(); } +/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */ +bool esp32_bt_wipe_config(void) { + bool result = false; + nvs_handle h = 0; + /* CONFIG_FILE_PATH form btc_config.c */ + if (nvs_open("bt_config.conf", NVS_READWRITE, &h) != ESP_OK) goto clean; + if (nvs_erase_key(h, "bt_cfg_key") != ESP_OK) goto clean; + result = true; + +clean: + if (h != 0) nvs_close(h); + return result; +} + bool mgos_bt_common_init(void) { bool ret = false; if (!mgos_sys_config_get_bt_enable()) { diff --git a/src/esp32/esp32_bt_gap.c b/src/esp32/esp32_bt_gap.c index 35dcea1..261e03d 100644 --- a/src/esp32/esp32_bt_gap.c +++ b/src/esp32/esp32_bt_gap.c @@ -13,6 +13,7 @@ #include "esp_bt.h" #include "esp_bt_defs.h" #include "esp_gap_ble_api.h" +#include "nvs.h" #include "frozen/frozen.h" @@ -60,7 +61,7 @@ static esp_ble_adv_params_t s_adv_params = { .adv_int_min = 0x50, /* 0x100 * 0.625 = 100 ms */ .adv_int_max = 0x100, /* 0x200 * 0.625 = 200 ms */ .adv_type = ADV_TYPE_IND, - .own_addr_type = BLE_ADDR_TYPE_PUBLIC, + .own_addr_type = BLE_ADDR_TYPE_RANDOM, .channel_map = ADV_CHNL_ALL, .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, }; @@ -567,5 +568,13 @@ bool esp32_bt_gap_init(void) { esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(key_size)); + if (mgos_sys_config_get_bt_random_address()) { + esp_ble_gap_config_local_privacy(true); + s_adv_params.own_addr_type = BLE_ADDR_TYPE_RANDOM; + } else { + esp_ble_gap_config_local_privacy(false); + s_adv_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC; + } + return mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable()); }