Progress on additional SafeMode logic to Auto-enable OTA

This commit is contained in:
Gregg 2022-03-13 14:42:19 -05:00
parent b6eb5afcbf
commit 1b0c4835cb
3 changed files with 41 additions and 45 deletions

View File

@ -531,44 +531,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 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 mdns_service_txt_item_set("_hap","_tcp","sh",setupHash); // Step 4: broadcast the resulting Setup Hash
int otaStatus=SpanOTA::OTA_OPTIONAL; boolean autoEnable=false;
nvs_get_i32(otaNVS,"OTASTATUS",&otaStatus); uint32_t otaStatus=0;
nvs_get_u32(otaNVS,"OTASTATUS",&otaStatus);
Serial.printf("*** OTA STATUS: %d ***\n\r",otaStatus); Serial.printf("*** OTA STATUS: %d ***\n\r",otaStatus);
if(otaStatus==SpanOTA::OTA_REQUIRED){ // most recent reboot was a result of new code being downloaded via OTA if(otaStatus&SpanOTA::OTA_DOWNLOADED ){ // if OTA was used for last download
spanOTA.enabled=true; // must enable OTA even if it is not set otaStatus^=SpanOTA::OTA_DOWNLOADED; // turn off OTA_DOWNLOADED flag
Serial.printf("AUTO-ENABLING OTA-1\n\r"); if(!spanOTA.enabled && (otaStatus&SpanOTA::OTA_SAFEMODE)) // if OTA is not enabled, but it was last enabled in safe mode
nvs_set_i32(otaNVS,"OTASTATUS",SpanOTA::OTA_MAINTAIN); // reset flag to OTA_MAINTAIN autoEnable=true; // activate auto-enable
} // OTA_REQUIRED } 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
else if(otaStatus==SpanOTA::OTA_MAINTAIN){ // most recent reboot was NOT a direct result of new code being downloaded via OTA if(!spanOTA.enabled && (otaStatus&SpanOTA::OTA_SAFEMODE)) // if OTA is not enabled, but it was last enabled in safe mode
if(!newCode){ // codebase has not changed - this is just a reboot of code previously downloaded via OTA autoEnable=true; // activate auto-enable
spanOTA.enabled=true; // must enable OTA even if it is not set } else { // code has changed - do not activate auto-enable
Serial.printf("AUTO-ENABLING OTA-2\n\r"); otaStatus^=SpanOTA::OTA_BOOTED; // turn off OTA_DOWNLOADED flag
} else { // codebase has changed, but was NOT a result of an OTA update (must be serial download) }
Serial.printf("SKIPPING OTA\n\r"); }
nvs_set_i32(otaNVS,"OTASTATUS",SpanOTA::OTA_OPTIONAL); // reset flag to OTA_OPTIONAL
} nvs_set_u32(otaNVS,"OTASTATUS",otaStatus);
} // OTA_MAINTAIN
nvs_commit(otaNVS); nvs_commit(otaNVS);
Serial.printf("\n\rRESET REASON=%d\n\r",esp_reset_reason()); if(autoEnable){
spanOTA.enabled=true;
// for(int i=0;i<32;i++) spanOTA.auth=otaStatus&SpanOTA::OTA_AUTHORIZED;
// Serial.printf("%02X",prevSHA[i]); spanOTA.safeLoad=true;
// Serial.printf("\n"); Serial.printf("OTA Safe Mode: OTA Auto-Enabled\n");
// }
// for(int i=0;i<32;i++)
// Serial.printf("%02X",sha256[i]);
// Serial.printf("\n");
//
// if(memcmp(prevSHA,sha256,32))
// Serial.printf("SHAs are DIFFERENT\n");
// else
// Serial.printf("SHAs do MATCH\n");
if(spanOTA.enabled){ if(spanOTA.enabled){
if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){ if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){
@ -580,6 +571,7 @@ void Span::checkConnect(){
ArduinoOTA.onStart(spanOTA.start).onEnd(spanOTA.end).onProgress(spanOTA.progress).onError(spanOTA.error); ArduinoOTA.onStart(spanOTA.start).onEnd(spanOTA.end).onProgress(spanOTA.progress).onError(spanOTA.error);
ArduinoOTA.begin(); ArduinoOTA.begin();
reserveSocketConnections(1);
Serial.print("Starting OTA Server: "); Serial.print("Starting OTA Server: ");
Serial.print(displayName); Serial.print(displayName);
Serial.print(" at "); Serial.print(" at ");
@ -2027,11 +2019,10 @@ void SpanWebLog::addLog(const char *fmt, ...){
// SpanOTA // // SpanOTA //
/////////////////////////////// ///////////////////////////////
void SpanOTA::init(boolean auth, boolean safeLoad){ void SpanOTA::init(boolean _auth, boolean _safeLoad){
enabled=true; enabled=true;
safeLoad=safeLoad; safeLoad=_safeLoad;
this->auth=auth; auth=_auth;
homeSpan.reserveSocketConnections(1);
} }
/////////////////////////////// ///////////////////////////////
@ -2046,7 +2037,7 @@ void SpanOTA::start(){
/////////////////////////////// ///////////////////////////////
void SpanOTA::end(){ void SpanOTA::end(){
nvs_set_i32(homeSpan.otaNVS,"OTASTATUS",OTA_REQUIRED); nvs_set_u32(homeSpan.otaNVS, "OTASTATUS", OTA_DOWNLOADED | OTA_BOOTED | (auth?OTA_AUTHORIZED:0) | (safeLoad?OTA_SAFEMODE:0));
nvs_commit(homeSpan.otaNVS); nvs_commit(homeSpan.otaNVS);
Serial.printf(" DONE! Rebooting...\n"); Serial.printf(" DONE! Rebooting...\n");
homeSpan.statusLED.off(); homeSpan.statusLED.off();
@ -2086,5 +2077,7 @@ void SpanOTA::error(ota_error_t err){
int SpanOTA::otaPercent; int SpanOTA::otaPercent;
boolean SpanOTA::safeLoad; boolean SpanOTA::safeLoad;
boolean SpanOTA::enabled=false;
boolean SpanOTA::auth;

View File

@ -128,17 +128,20 @@ struct SpanWebLog{ // optional web status/log data
/////////////////////////////// ///////////////////////////////
struct SpanOTA{ // manages OTA process struct SpanOTA{ // manages OTA process
enum { // keep track of whether OTA need to be required based on prior download enum { // flag to keep track of OTA status between reboots
OTA_OPTIONAL, OTA_BOOTED=1,
OTA_MAINTAIN, OTA_DOWNLOADED=2,
OTA_REQUIRED OTA_AUTHORIZED=4,
OTA_SAFEMODE=8
}; };
boolean enabled=false; // enables OTA - default if not enabled
boolean auth; // indicates whether OTA password is required
char otaPwd[33]; // MD5 Hash of OTA password, represented as a string of hexidecimal characters char otaPwd[33]; // MD5 Hash of OTA password, represented as a string of hexidecimal characters
static boolean enabled; // enables OTA - default if not enabled
static boolean auth; // indicates whether OTA password is required
static int otaPercent; static int otaPercent;
static boolean safeLoad; // indicates whether OTA update should reject any application update that is not another HomeSpan sketch static boolean safeLoad; // indicates whether OTA update should reject any application update that is not another HomeSpan sketch
void init(boolean auth, boolean safeLoad); void init(boolean auth, boolean safeLoad);
static void start(); static void start();
static void end(); static void end();

View File

@ -21,7 +21,7 @@ void setup() {
homeSpan.setPortNum(1201); homeSpan.setPortNum(1201);
// homeSpan.setMaxConnections(6); // homeSpan.setMaxConnections(6);
// homeSpan.setQRID("One1"); // homeSpan.setQRID("One1");
// homeSpan.enableOTA(false); homeSpan.enableOTA();
homeSpan.setSketchVersion("OTA Test 5"); homeSpan.setSketchVersion("OTA Test 5");
homeSpan.setWifiCallback(wifiEstablished); homeSpan.setWifiCallback(wifiEstablished);