Skip to content

Commit

Permalink
Use random BT addr; add esp32_bt_wipe_config
Browse files Browse the repository at this point in the history
PUBLISHED_FROM=d6634562b654a87b72c2c1168d159da3afb7e91e
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Dec 18, 2017
1 parent 4552336 commit f4f01b6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/esp32/esp32_bt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]
Expand Down
15 changes: 15 additions & 0 deletions src/esp32/esp32_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()) {
Expand Down
11 changes: 10 additions & 1 deletion src/esp32/esp32_bt_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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());
}

0 comments on commit f4f01b6

Please sign in to comment.