From ffdf0296c6f84cd9ec0c09171759e81fd7b90ad4 Mon Sep 17 00:00:00 2001 From: Gregg Date: Mon, 14 Mar 2022 21:46:37 -0500 Subject: [PATCH] Dramatically simplified OTA enable check in safemode Rather than auto-enable OTA if not already enabled in safemode, changed the logic to simply rollback to previous app if OTA was used to download a sketch that does not itself have OTA enabled, unless OTA was previously enabled without safemode. To do: Delete all complicated SpanOTA logic that (unsuccessfully) tried to track OTA status and check SHA246 partition codes to determine if reboot was OTA or Serial. None of this is need, but some of the code may be useful for other things in the future. --- src/HomeSpan.cpp | 71 ++++++++++++++++++++++++++++-------------------- src/src.ino | 4 +-- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index bdf6d8c..8e9a3e8 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -154,6 +154,16 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa Serial.print(displayName); Serial.print("\n\n"); + uint8_t otaRequired=0; + nvs_get_u8(otaNVS,"OTA_REQUIRED",&otaRequired); + nvs_set_u8(otaNVS,"OTA_REQUIRED",0); + nvs_commit(otaNVS); + if(otaRequired && !spanOTA.enabled){ + Serial.printf("\n\n*** OTA SAFE MODE ALERT: OTA REQUIRED BUT NOT ENABLED. ROLLING BACK TO PREVIOUS APPLICATION ***\n\n"); + delay(100); + esp_ota_mark_app_invalid_rollback_and_reboot(); + } + } // begin /////////////////////////////// @@ -531,35 +541,35 @@ void Span::checkConnect(){ mbedtls_base64_encode((uint8_t *)setupHash,9,&len,hashOutput,4); // Step 3: Encode the first 4 bytes of hashOutput in base64, which results in an 8-character, null-terminated, setupHash mdns_service_txt_item_set("_hap","_tcp","sh",setupHash); // Step 4: broadcast the resulting Setup Hash - boolean autoEnable=false; - uint32_t otaStatus=0; - nvs_get_u32(otaNVS,"OTASTATUS",&otaStatus); - - Serial.printf("*** OTA STATUS: %d ***\n\r",otaStatus); - - if(otaStatus&SpanOTA::OTA_DOWNLOADED ){ // if OTA was used for last download - otaStatus^=SpanOTA::OTA_DOWNLOADED; // turn off OTA_DOWNLOADED flag - if(!spanOTA.enabled && (otaStatus&SpanOTA::OTA_SAFEMODE)) // if OTA is not enabled, but it was last enabled in safe mode - autoEnable=true; // activate auto-enable - - } else if(otaStatus&SpanOTA::OTA_BOOTED ){ // if OTA was present in last boot, but not used for download - if(!newCode){ // if code has NOT changed - if(!spanOTA.enabled && (otaStatus&SpanOTA::OTA_SAFEMODE)) // if OTA is not enabled, but it was last enabled in safe mode - autoEnable=true; // activate auto-enable - } else { // code has changed - do not activate auto-enable - otaStatus^=SpanOTA::OTA_BOOTED; // turn off OTA_DOWNLOADED flag - } - } - - nvs_set_u32(otaNVS,"OTASTATUS",otaStatus); - nvs_commit(otaNVS); - - if(autoEnable){ - spanOTA.enabled=true; - spanOTA.auth=otaStatus&SpanOTA::OTA_AUTHORIZED; - spanOTA.safeLoad=true; - Serial.printf("OTA Safe Mode: OTA Auto-Enabled\n"); - } +// boolean autoEnable=false; +// uint32_t otaStatus=0; +// nvs_get_u32(otaNVS,"OTASTATUS",&otaStatus); +// +// Serial.printf("*** OTA STATUS: %d ***\n\r",otaStatus); +// +// if(otaStatus&SpanOTA::OTA_DOWNLOADED ){ // if OTA was used for last download +// otaStatus^=SpanOTA::OTA_DOWNLOADED; // turn off OTA_DOWNLOADED flag +// if(!spanOTA.enabled && (otaStatus&SpanOTA::OTA_SAFEMODE)) // if OTA is not enabled, but it was last enabled in safe mode +// autoEnable=true; // activate auto-enable +// +// } else if(otaStatus&SpanOTA::OTA_BOOTED ){ // if OTA was present in last boot, but not used for download +// if(!newCode){ // if code has NOT changed +// if(!spanOTA.enabled && (otaStatus&SpanOTA::OTA_SAFEMODE)) // if OTA is not enabled, but it was last enabled in safe mode +// autoEnable=true; // activate auto-enable +// } else { // code has changed - do not activate auto-enable +// otaStatus^=SpanOTA::OTA_BOOTED; // turn off OTA_DOWNLOADED flag +// } +// } +// +// nvs_set_u32(otaNVS,"OTASTATUS",otaStatus); +// nvs_commit(otaNVS); +// +// if(autoEnable){ +// spanOTA.enabled=true; +// spanOTA.auth=otaStatus&SpanOTA::OTA_AUTHORIZED; +// spanOTA.safeLoad=true; +// Serial.printf("OTA Safe Mode: OTA Auto-Enabled\n"); +// } if(spanOTA.enabled){ if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){ @@ -2037,7 +2047,8 @@ void SpanOTA::start(){ /////////////////////////////// void SpanOTA::end(){ - nvs_set_u32(homeSpan.otaNVS, "OTASTATUS", OTA_DOWNLOADED | OTA_BOOTED | (auth?OTA_AUTHORIZED:0) | (safeLoad?OTA_SAFEMODE:0)); +// nvs_set_u32(homeSpan.otaNVS, "OTASTATUS", OTA_DOWNLOADED | OTA_BOOTED | (auth?OTA_AUTHORIZED:0) | (safeLoad?OTA_SAFEMODE:0)); + nvs_set_u8(homeSpan.otaNVS,"OTA_REQUIRED",safeLoad); nvs_commit(homeSpan.otaNVS); Serial.printf(" DONE! Rebooting...\n"); homeSpan.statusLED.off(); diff --git a/src/src.ino b/src/src.ino index 8ac2f66..b345897 100644 --- a/src/src.ino +++ b/src/src.ino @@ -21,8 +21,8 @@ void setup() { homeSpan.setPortNum(1201); // homeSpan.setMaxConnections(6); // homeSpan.setQRID("One1"); - homeSpan.enableOTA(); - homeSpan.setSketchVersion("OTA Test 5"); +// homeSpan.enableOTA(false,false); + homeSpan.setSketchVersion("OTA Test 8"); homeSpan.setWifiCallback(wifiEstablished); new SpanUserCommand('d',"- My Description",userCom1);