From eac06129fc40a3db332a3b4fe574fdf5a490e82f Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 12 Mar 2022 16:31:50 -0600 Subject: [PATCH] Moved all OTA logic into SpanOTA and completed "safeLoad" protocol --- src/HAP.cpp | 4 ++-- src/HomeSpan.cpp | 13 +++++++------ src/HomeSpan.h | 9 ++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 98c0015..86d50ec 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -44,13 +44,13 @@ void HAPClient::init(){ nvs_open("STATE",NVS_READWRITE,&stateNVS); // open STATE data namespace in NVS if(!nvs_get_str(otaNVS,"OTADATA",NULL,&len)){ // if found OTA data in NVS - nvs_get_str(otaNVS,"OTADATA",homeSpan.otaPwd,&len); // retrieve data + nvs_get_str(otaNVS,"OTADATA",homeSpan.spanOTA.otaPwd,&len); // retrieve data } else { MD5Builder otaPwdHash; otaPwdHash.begin(); otaPwdHash.add(DEFAULT_OTA_PASSWORD); otaPwdHash.calculate(); - otaPwdHash.getChars(homeSpan.otaPwd); + otaPwdHash.getChars(homeSpan.spanOTA.otaPwd); } if(strlen(homeSpan.pairingCodeCommand)){ // load verification setup code if provided diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 9f0f519..cff9a12 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -525,8 +525,8 @@ void Span::checkConnect(){ if(esp_ota_get_running_partition()!=esp_ota_get_next_update_partition(NULL)){ ArduinoOTA.setHostname(hostName); - if(otaAuth) - ArduinoOTA.setPasswordHash(otaPwd); + if(spanOTA.auth) + ArduinoOTA.setPasswordHash(spanOTA.otaPwd); ArduinoOTA.onStart(spanOTA.start).onEnd(spanOTA.end).onProgress(spanOTA.progress).onError(spanOTA.error); @@ -536,7 +536,7 @@ void Span::checkConnect(){ Serial.print(" at "); Serial.print(WiFi.localIP()); Serial.print("\nAuthorization Password: "); - Serial.print(otaAuth?"Enabled\n\n":"DISABLED!\n\n"); + Serial.print(spanOTA.auth?"Enabled\n\n":"DISABLED!\n\n"); } else { Serial.print("\n*** WARNING: Can't start OTA Server - Partition table used to compile this sketch is not configured for OTA.\n\n"); spanOTA.enabled=false; @@ -695,8 +695,8 @@ void Span::processSerialCommand(const char *c){ otaPwdHash.begin(); otaPwdHash.add(textPwd); otaPwdHash.calculate(); - otaPwdHash.getChars(otaPwd); - nvs_set_str(HAPClient::otaNVS,"OTADATA",otaPwd); // update data + otaPwdHash.getChars(spanOTA.otaPwd); + nvs_set_str(HAPClient::otaNVS,"OTADATA",spanOTA.otaPwd); // update data nvs_commit(HAPClient::otaNVS); Serial.print("... Accepted! Password change will take effect after next restart.\n"); @@ -1983,6 +1983,8 @@ void SpanOTA::init(boolean auth, boolean safeLoad){ homeSpan.reserveSocketConnections(1); } +/////////////////////////////// + void SpanOTA::start(){ Serial.printf("\n*** Current Partition: %s\n*** New Partition: %s\n*** OTA Starting..", esp_ota_get_running_partition()->label,esp_ota_get_next_update_partition(NULL)->label); @@ -2031,5 +2033,4 @@ void SpanOTA::error(ota_error_t err){ int SpanOTA::otaPercent; boolean SpanOTA::safeLoad; - diff --git a/src/HomeSpan.h b/src/HomeSpan.h index dc50436..8cc3179 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -128,10 +128,11 @@ struct SpanWebLog{ // optional web status/log data /////////////////////////////// struct SpanOTA{ // manages OTA process - boolean enabled=false; - boolean auth; + 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 static int otaPercent; - static boolean safeLoad; + static boolean safeLoad; // indicates whether OTA update should reject any application update that is not another HomeSpan sketch void init(boolean auth, boolean safeLoad); static void start(); static void end(); @@ -176,8 +177,6 @@ struct Span{ unsigned long comModeLife=DEFAULT_COMMAND_TIMEOUT*1000; // length of time (in milliseconds) to keep Command Mode alive before resuming normal operations uint16_t tcpPortNum=DEFAULT_TCP_PORT; // port for TCP communications between HomeKit and HomeSpan char qrID[5]=""; // Setup ID used for pairing with QR Code - char otaPwd[33]; // MD5 Hash of OTA password, represented as a string of hexidecimal characters - boolean otaAuth; // OTA requires password when set to true void (*wifiCallback)()=NULL; // optional callback function to invoke once WiFi connectivity is established void (*pairCallback)(boolean isPaired)=NULL; // optional callback function to invoke when pairing is established (true) or lost (false) boolean autoStartAPEnabled=false; // enables auto start-up of Access Point when WiFi Credentials not found