Moved all OTA logic into SpanOTA and completed "safeLoad" protocol

This commit is contained in:
Gregg 2022-03-12 16:31:50 -06:00
parent f3d5092340
commit eac06129fc
3 changed files with 13 additions and 13 deletions

View File

@ -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

View File

@ -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;

View File

@ -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