mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-24 11:40:27 +00:00
Compare commits
3 Commits
thinknode-
...
delete-nim
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbc2bf3838 | ||
|
|
dec8ff0339 | ||
|
|
9dd9baf278 |
@@ -26,6 +26,16 @@
|
||||
#include "nimble/nimble/host/include/host/ble_gap.h"
|
||||
#endif
|
||||
|
||||
#ifdef ARCH_ESP32
|
||||
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
||||
#include "host/ble_store.h"
|
||||
#else
|
||||
#include "nimble/nimble/host/include/host/ble_store.h"
|
||||
#endif
|
||||
#include <nvs.h>
|
||||
#include <nvs_flash.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr uint16_t kPreferredBleMtu = 517;
|
||||
@@ -33,6 +43,72 @@ constexpr uint16_t kPreferredBleTxOctets = 251;
|
||||
constexpr uint16_t kPreferredBleTxTimeUs = (kPreferredBleTxOctets + 14) * 8;
|
||||
} // namespace
|
||||
|
||||
#ifdef ARCH_ESP32
|
||||
// Credit: https://github.com/h2zero/NimBLE-Arduino/issues/740#issuecomment-2539923656
|
||||
// Note: Despite the name, this function is invoked on every device boot (from setup()).
|
||||
// It performs a routine startup check on the stored NimBLE bond data and deletes bonds
|
||||
// only if a mismatch or migration condition is detected. It is not a one-time hook that
|
||||
// runs only when the NimBLE version changes.
|
||||
static void deleteBondsIfNimBLEVersionChanged()
|
||||
{
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
err = nvs_flash_erase();
|
||||
if (err != ESP_OK) {
|
||||
LOG_ERROR("Failed to erase NVS for NimBLE migration, err=%d", err);
|
||||
return;
|
||||
}
|
||||
err = nvs_flash_init();
|
||||
if (err != ESP_OK) {
|
||||
LOG_ERROR("Failed to re-init NVS after erase, err=%d", err);
|
||||
return;
|
||||
}
|
||||
} else if (err != ESP_OK) {
|
||||
LOG_ERROR("nvs_flash_init failed, err=%d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
nvs_handle_t nimbleHandle = 0;
|
||||
err = nvs_open("nimble_bond", NVS_READWRITE, &nimbleHandle);
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND) {
|
||||
return;
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
LOG_ERROR("Failed to open nimble_bond namespace, err=%d", err);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t requiredSize = 0;
|
||||
bool bondExists = nvs_get_blob(nimbleHandle, "peer_sec_1", nullptr, &requiredSize) == ESP_OK;
|
||||
bool rpaExists = nvs_get_blob(nimbleHandle, "rpa_rec_1", nullptr, &requiredSize) == ESP_OK;
|
||||
bool irkExists = nvs_get_blob(nimbleHandle, "local_irk_1", nullptr, &requiredSize) == ESP_OK;
|
||||
|
||||
bool erasePartition = false;
|
||||
#if defined(BLE_STORE_OBJ_TYPE_LOCAL_IRK)
|
||||
erasePartition = bondExists && !rpaExists;
|
||||
#else
|
||||
erasePartition = rpaExists || irkExists;
|
||||
#endif
|
||||
|
||||
bool restartRequired = false;
|
||||
if (erasePartition) {
|
||||
LOG_WARN("Clearing NimBLE bonds due to version migration");
|
||||
if (nvs_erase_all(nimbleHandle) != ESP_OK || nvs_commit(nimbleHandle) != ESP_OK) {
|
||||
LOG_ERROR("Failed to erase nimble_bond namespace");
|
||||
} else {
|
||||
restartRequired = true;
|
||||
}
|
||||
}
|
||||
|
||||
nvs_close(nimbleHandle);
|
||||
|
||||
if (restartRequired) {
|
||||
LOG_INFO("Restarting after NimBLE bond cleanup");
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Debugging options: careful, they slow things down quite a bit!
|
||||
// #define DEBUG_NIMBLE_ON_READ_TIMING // uncomment to time onRead duration
|
||||
// #define DEBUG_NIMBLE_ON_WRITE_TIMING // uncomment to time onWrite duration
|
||||
@@ -800,6 +876,10 @@ void NimbleBluetooth::setup()
|
||||
// Uncomment for testing
|
||||
// NimbleBluetooth::clearBonds();
|
||||
|
||||
#ifdef ARCH_ESP32
|
||||
deleteBondsIfNimBLEVersionChanged();
|
||||
#endif
|
||||
|
||||
LOG_INFO("Init the NimBLE bluetooth module");
|
||||
|
||||
NimBLEDevice::init(getDeviceName());
|
||||
|
||||
Reference in New Issue
Block a user