Skip to content

Commit

Permalink
A number of fixes related to advertising cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Nov 29, 2021
1 parent 3574986 commit 73179cb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 191 deletions.
6 changes: 1 addition & 5 deletions include/esp32/esp32_bt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@
extern "C" {
#endif

bool esp32_bt_is_scanning(void);
bool esp32_bt_gattc_init(void);

bool esp32_bt_gap_start_advertising(void);
bool esp32_bt_gatts_init(void);
bool esp32_bt_gatts_start(void);
void esp32_bt_restart(void);

void esp32_bt_set_is_advertising(bool is_advertising);

struct ble_gap_event;
int esp32_bt_gatts_event(const struct ble_gap_event *event, void *arg);

Expand Down
3 changes: 2 additions & 1 deletion mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ 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"}]
# TODO: Random address support on NimBLE
- ["bt.random_address", "b", false, {title: "Use random BT address"}]
- ["bt.gatt_mtu", "i", 500, {title: "Local MTU setting, used when negotiating with clients"}]
- ["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"}]
Expand Down
14 changes: 10 additions & 4 deletions src/esp32/esp32_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static void mgos_bt_net_ev(int ev, void *evd, void *arg) {
mgos_sys_config_set_bt_enable(false);
char *msg = NULL;
if (save_cfg(&mgos_sys_config, &msg)) {
// nimble_port_stop();
mgos_bt_stop();
}
(void) arg;
}
Expand All @@ -149,7 +149,7 @@ bool mgos_bt_get_device_address(struct mgos_bt_addr *addr) {
int is_nrpa = false;
rc = ble_hs_id_copy_addr(own_addr_type, baddr.val, &is_nrpa);
if (rc != 0) {
LOG(LL_ERROR, ("BLE addr error %d", rc));
LOG(LL_ERROR, ("BLE addr type %d error %d", own_addr_type, rc));
return false;
}
switch (own_addr_type) {
Expand All @@ -175,7 +175,6 @@ static void esp32_bt_reset(int reason) {
static void esp32_bt_synced(void) {
int rc;

LOG(LL_INFO, ("BLE started"));
s_state = ESP32_BT_STARTED;

if (!s_should_be_running) {
Expand All @@ -198,7 +197,7 @@ static void esp32_bt_synced(void) {
("BLE Device Address: %s",
mgos_bt_addr_to_str(&addr, MGOS_BT_ADDR_STRINGIFY_TYPE, addr_str)));
}
mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable());
esp32_bt_gap_start_advertising();
}

// Handler callback that executes host events on the mgos task.
Expand Down Expand Up @@ -298,6 +297,8 @@ static bool esp32_bt_init(void) {
goto out;
}

mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable());

LOG(LL_INFO, ("Bluetooth init ok, MTU %d, pairing %s, %d paired devices",
mgos_sys_config_get_bt_gatt_mtu(),
(mgos_bt_gap_get_pairing_enable() ? "enabled" : "disabled"),
Expand Down Expand Up @@ -352,6 +353,11 @@ bool mgos_bt_common_init(void) {
LOG(LL_INFO, ("Bluetooth is disabled"));
return true;
}
// TODO(rojer): Figure out random address support under NimBLE.
if (mgos_sys_config_get_bt_random_address()) {
LOG(LL_ERROR, ("Random addresses are not supported, using public"));
mgos_sys_config_set_bt_random_address(false);
}
esp32_bt_init();
// Delay starting the stack until other libraries are initialized
// and services registered to avoid unnecessary restarts.
Expand Down
Loading

0 comments on commit 73179cb

Please sign in to comment.