From 6506d548590e069df6f5e8a7a93fb2e0a2f414ad Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 8 Jan 2022 11:54:02 -0800 Subject: [PATCH 1/3] Erase NVS as part of factory reset & new triple click behavior. --- src/main.cpp | 9 +++++++++ src/mesh/NodeDB.cpp | 2 ++ src/nimble/BluetoothUtil.cpp | 21 +++++++++++++++++++-- src/nimble/BluetoothUtil.h | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 15085034d..0b1b676b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -208,6 +208,7 @@ class ButtonThread : public OSThread userButton.attachClick(userButtonPressed); userButton.attachDuringLongPress(userButtonPressedLong); userButton.attachDoubleClick(userButtonDoublePressed); + userButton.attachMultiClick(userButtonMultiPressed); userButton.attachLongPressStart(userButtonPressedLongStart); userButton.attachLongPressStop(userButtonPressedLongStop); wakeOnIrq(BUTTON_PIN, FALLING); @@ -336,6 +337,14 @@ class ButtonThread : public OSThread #endif } + static void userButtonMultiPressed() + { +#ifndef NO_ESP32 + clearNVS(); +#endif + } + + static void userButtonPressedLongStart() { DEBUG_MSG("Long press start!\n"); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 4ba4c14d1..73e37a7ce 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -23,6 +23,7 @@ #include "mesh/http/WiFiAPClient.h" #include "plugins/esp32/StoreForwardPlugin.h" #include +#include #endif NodeDB nodeDB; @@ -86,6 +87,7 @@ bool NodeDB::resetRadioConfig() if (radioConfig.preferences.factory_reset) { DEBUG_MSG("Performing factory reset!\n"); installDefaultDeviceState(); + nvs_flash_erase(); didFactoryReset = true; } diff --git a/src/nimble/BluetoothUtil.cpp b/src/nimble/BluetoothUtil.cpp index 42ff06b7d..016eafe06 100644 --- a/src/nimble/BluetoothUtil.cpp +++ b/src/nimble/BluetoothUtil.cpp @@ -15,6 +15,7 @@ #ifndef NO_ESP32 #include "mesh/http/WiFiAPClient.h" +#include #endif static bool pinShowing; @@ -484,7 +485,24 @@ void disablePin() doublepressed = millis(); } +// This should go somewhere else. +void clearNVS() +{ +#ifndef NO_ESP32 + // As soon as the LED flashing from double click is done, immediately do a tripple click to + // erase nvs memory. + if (doublepressed > (millis() - 2000)) { + DEBUG_MSG("Clearing NVS memory\n"); + + // This will erase ble pairings, ssl key and persistent preferences. + nvs_flash_erase(); + + DEBUG_MSG("Restarting...\n"); + ESP.restart(); + } +#endif +} // This routine is called multiple times, once each time we come back from sleep void reinitBluetooth() @@ -556,8 +574,7 @@ void setBluetoothEnable(bool on) bluetoothOn = on; if (on) { - if (! isWifiAvailable() ) - { + if (!isWifiAvailable()) { Serial.printf("Pre BT: %u heap size\n", ESP.getFreeHeap()); // ESP_ERROR_CHECK( heap_trace_start(HEAP_TRACE_LEAKS) ); reinitBluetooth(); diff --git a/src/nimble/BluetoothUtil.h b/src/nimble/BluetoothUtil.h index 4e423769e..8d02c0afc 100644 --- a/src/nimble/BluetoothUtil.h +++ b/src/nimble/BluetoothUtil.h @@ -15,6 +15,7 @@ void deinitBLE(); void loopBLE(); void reinitBluetooth(); void disablePin(); +void clearNVS(); /** * A helper function that implements simple read and write handling for a uint32_t From 2ee1155c786658a0d648eec24185d041a86225ee Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 8 Jan 2022 11:54:30 -0800 Subject: [PATCH 2/3] add namespace count to nvs stats --- src/esp32/main-esp32.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/esp32/main-esp32.cpp b/src/esp32/main-esp32.cpp index e000ad5af..b0afab656 100644 --- a/src/esp32/main-esp32.cpp +++ b/src/esp32/main-esp32.cpp @@ -43,8 +43,8 @@ void esp32Setup() nvs_stats_t nvs_stats; auto res = nvs_get_stats(NULL, &nvs_stats); assert(res == ESP_OK); - DEBUG_MSG("NVS: UsedEntries %d, FreeEntries %d, AllEntries %d\n", nvs_stats.used_entries, nvs_stats.free_entries, - nvs_stats.total_entries); + DEBUG_MSG("NVS: UsedEntries %d, FreeEntries %d, AllEntries %d, NameSpaces %d\n", nvs_stats.used_entries, nvs_stats.free_entries, + nvs_stats.total_entries, nvs_stats.namespace_count); DEBUG_MSG("Setup Preferences in Flash Storage\n"); From 465ff3dd25a0bf82f69d3bd74f0c98f9f04491cd Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 8 Jan 2022 12:03:18 -0800 Subject: [PATCH 3/3] Fix nvs erase for native build --- src/mesh/NodeDB.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 73e37a7ce..04359aaed 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -87,7 +87,10 @@ bool NodeDB::resetRadioConfig() if (radioConfig.preferences.factory_reset) { DEBUG_MSG("Performing factory reset!\n"); installDefaultDeviceState(); +#ifndef NO_ESP32 + // This will erase what's in NVS including ssl keys, persistant variables and ble pairing nvs_flash_erase(); +#endif didFactoryReset = true; }