Skip to content

Commit

Permalink
ble: enable saved bonds to be cleared when Bluetooth not running
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieDriver committed Jun 7, 2024
1 parent 3460de4 commit 0d3d400
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
46 changes: 41 additions & 5 deletions main/ble/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ void ble_start(void)
esp_bt_controller_deinit();
esp_bt_mem_release(ESP_BT_MODE_BTDM);
*/
nimble_port_init();
const esp_err_t err = nimble_port_init();
if (err != ESP_OK) {
JADE_LOGE("Unable to init NimBLE!: %d", err);
return;
}

ble_hs_cfg.reset_cb = ble_on_reset;
ble_hs_cfg.sync_cb = ble_on_sync;
Expand Down Expand Up @@ -557,13 +561,19 @@ void ble_stop(void)
// flag tasks to die
ble_is_enabled = false;

int ret = nimble_port_stop();
// Log errors but carry on
const int ret = nimble_port_stop();
if (ret == 0) {
nimble_port_deinit();
const esp_err_t err = nimble_port_deinit();
if (err != ESP_OK) {
JADE_LOGE("Unable to deinit NimBLE!: %d", err);
}
} else {
JADE_LOGE("Unable to stop NimBLE!: %d", ret);
}

// The above kills the main BLE task
// Kill our writer task
// Kill our writer task in any case
xSemaphoreTake(writer_shutdown_done, portMAX_DELAY);
vTaskDelete(*p_ble_writer_handle);
*p_ble_writer_handle = NULL;
Expand Down Expand Up @@ -782,7 +792,7 @@ static int ble_gap_event(struct ble_gap_event* event, void* arg)
return 0;
}

bool ble_remove_all_devices(void)
static bool ble_remove_all_devices_impl(void)
{
bool errored = false;
ble_addr_t peer_id_addrs[CONFIG_BT_NIMBLE_MAX_BONDS];
Expand Down Expand Up @@ -819,3 +829,29 @@ bool ble_remove_all_devices(void)

return !errored;
}

bool ble_remove_all_devices(void)
{
// Simple case!
if (ble_is_enabled) {
return ble_remove_all_devices_impl();
}

// Need to temporarily initialise low-level ble subsystem
// for these calls to be valid / work as expected.
esp_err_t err = nimble_port_init();
if (err != ESP_OK) {
JADE_LOGE("Unable to init NimBLE!: %d", err);
return false;
}

ble_store_config_init();
const bool retval = ble_remove_all_devices_impl();

err = nimble_port_deinit();
if (err != ESP_OK) {
JADE_LOGE("Unable to deinit NimBLE!: %d", err);
}

return retval;
}
6 changes: 0 additions & 6 deletions main/process/dashboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,6 @@ static void handle_legal(void)
// Reset BLE pairing data
static void handle_ble_reset(void)
{
if (!ble_enabled()) {
const char* message[] = { "You must enable", "Bluetooth to reset", "pairings." };
await_message_activity(message, 3);
return;
}

const char* question[] = { "Delete Bluetooth", "pairings for all", "bonded devices?" };
if (!await_yesno_activity(device_name, question, 3, false, NULL)) {
return;
Expand Down

0 comments on commit 0d3d400

Please sign in to comment.