1. Ensure that recalibration doesn't cause issues with failing to pause watchdog and clear flags

2. Ensure all credentials are purged on device deletion to avoid security issues

3. Clear calibration state (set to false) on device deletion.
This commit is contained in:
2026-01-02 13:16:43 -06:00
parent c19edcc569
commit 5f64e5f395
6 changed files with 43 additions and 1 deletions

View File

@@ -1,5 +1,7 @@
#include "bmHTTP.hpp"
#include "esp_http_client.h"
#include "nvs_flash.h"
#include "defines.h"
#define httpSrv "http://192.168.1.190:3000/"
std::string webToken;
@@ -63,3 +65,27 @@ bool httpGET(std::string endpoint, std::string token, cJSON* &JSONresponse) {
esp_http_client_cleanup(client);
return success;
}
void deleteWiFiAndTokenDetails() {
nvs_handle_t wifiHandle;
if (nvs_open(nvsWiFi, NVS_READWRITE, &wifiHandle) == ESP_OK) {
if (nvs_erase_all(wifiHandle) == ESP_OK) {
printf("Successfully erased WiFi details\n");
nvs_commit(wifiHandle);
}
else printf("ERROR: Erase wifi failed\n");
nvs_close(wifiHandle);
}
else printf("ERROR: Failed to open WiFi section for deletion\n");
nvs_handle_t authHandle;
if (nvs_open(nvsAuth, NVS_READWRITE, &authHandle) == ESP_OK) {
if (nvs_erase_all(authHandle) == ESP_OK) {
printf("Successfully erased Auth details\n");
nvs_commit(authHandle);
}
else printf("ERROR: Erase auth failed\n");
nvs_close(authHandle);
}
else printf("ERROR: Failed to open Auth section for deletion\n");
}

View File

@@ -7,4 +7,6 @@ extern std::string webToken;
bool httpGET(std::string endpoint, std::string token, cJSON* &JSONresponse);
void deleteWiFiAndTokenDetails();
#endif

View File

@@ -40,6 +40,7 @@ bool Calibration::clearCalibrated() {
printf("Error saving calibration status as false.\n");
return false;
}
nvs_commit(calibHandle);
nvs_close(calibHandle);
}
else {
@@ -56,6 +57,7 @@ bool Calibration::beginDownwardCalib(Encoder& topEnc) {
if (nvs_set_i32(calibHandle, UpTicksTag, tempUpTicks) == ESP_OK) {
printf("Saved UpTicks to NVS\n");
UpTicks = tempUpTicks;
nvs_commit(calibHandle);
}
else {
printf("Error saving UpTicks.\n");
@@ -88,6 +90,7 @@ bool Calibration::completeCalib(Encoder& topEnc) {
DownTicks = tempDownTicks;
calibrated = true;
printf("Range: %d - %d\n", UpTicks.load(), tempDownTicks);
nvs_commit(calibHandle);
nvs_close(calibHandle);
}
else {

View File

@@ -109,7 +109,7 @@ void Encoder::setupWatchdog() {
void Encoder::pauseWatchdog() {
feedWDog = false;
esp_timer_stop(watchdog_handle);
if (watchdog_handle != NULL) esp_timer_stop(watchdog_handle);
}
Encoder::~Encoder() {

View File

@@ -66,6 +66,12 @@ void servoMainSwitch(uint8_t onOff) {
}
bool servoInitCalib() {
topEnc->pauseWatchdog();
// get ready for calibration by clearing all these listeners
bottomEnc->wandListen = false;
topEnc->wandListen = false;
topEnc->serverListen = false;
if (!calib.clearCalibrated()) return false;
if (topEnc == nullptr || bottomEnc == nullptr) {
printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n");
@@ -138,6 +144,7 @@ void servoSavePos() {
if (nvs_set_i32(servoHandle, posTag, topCount) != ESP_OK)
printf("Error saving current position\n");
else printf("Success - Current position saved as: %d\n", topCount);
nvs_commit(servoHandle);
nvs_close(servoHandle);
}
else {

View File

@@ -104,6 +104,8 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base,
statusResolved = true;
} else {
printf("Device authentication failed\n");
calib.clearCalibrated();
deleteWiFiAndTokenDetails();
connected = false;
statusResolved = true;
}
@@ -119,6 +121,8 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base,
printf("Server message: %s\n", message->valuestring);
}
}
calib.clearCalibrated();
deleteWiFiAndTokenDetails();
connected = false;
statusResolved = true;
}