From 48b21df3298820e338140d8f6f6490397cccd555 Mon Sep 17 00:00:00 2001 From: Gregg Date: Fri, 12 Nov 2021 18:01:27 -0600 Subject: [PATCH] Created method setPairingCode(const char *s) Allows for programmatic creation of Pairing Setup Code. Not recommended, but added for a convenience - should use 'S' from CLI instead. --- src/HAP.cpp | 6 +++++- src/HomeSpan.cpp | 4 ++-- src/HomeSpan.h | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 822f84e..a079b72 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -52,11 +52,15 @@ void HAPClient::init(){ otaPwdHash.getChars(homeSpan.otaPwd); } + if(strlen(homeSpan.pairingCodeCommand)){ // load verification setup code if provided + homeSpan.processSerialCommand(homeSpan.pairingCodeCommand); // if load failed due to invalid code, the logic below still runs and will pick up previous code or use the default one + } + struct { // temporary structure to hold SRP verification code and salt stored in NVS uint8_t salt[16]; uint8_t verifyCode[384]; } verifyData; - + if(!nvs_get_blob(srpNVS,"VERIFYDATA",NULL,&len)){ // if found verification code data in NVS nvs_get_blob(srpNVS,"VERIFYDATA",&verifyData,&len); // retrieve data srp.loadVerifyCode(verifyData.verifyCode,verifyData.salt); // load verification code and salt into SRP structure diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 3c2928e..f3af167 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -721,11 +721,11 @@ void Span::processSerialCommand(const char *c){ sscanf(c+1," %9[0-9]",setupCode); if(strlen(setupCode)!=8){ - Serial.print("\n*** Invalid request to change Setup Code. Code must be exactly 8 digits.\n"); + Serial.print("\n*** Invalid request to change Setup Code. Code must be exactly 8 digits.\n\n"); } else if(!network.allowedCode(setupCode)){ - Serial.print("\n*** Invalid request to change Setup Code. Code too simple.\n"); + Serial.print("\n*** Invalid request to change Setup Code. Code too simple.\n\n"); } else { sprintf(buf,"\n\nGenerating SRP verification data for new Setup Code: %.3s-%.2s-%.3s ... ",setupCode,setupCode+3,setupCode+5); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 481e7f7..664d4b9 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -112,6 +112,7 @@ struct Span{ const char *sketchVersion="n/a"; // version of the sketch nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data nvs_handle wifiNVS=0; // handle for non-volatile-storage of WiFi data + char pairingCodeCommand[12]=""; // user-specified Pairing Code - only needed if Pairing Setup Code is specified in sketch using setPairingCode() boolean connected=false; // WiFi connection status unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts @@ -188,6 +189,8 @@ struct Span{ void setWifiCallback(void (*f)()){wifiCallback=f;} // sets an optional user-defined function to call once WiFi connectivity is established void setApFunction(void (*f)()){apFunction=f;} // sets an optional user-defined function to call when activating the WiFi Access Point + void setPairingCode(const char *s){sprintf(pairingCodeCommand,"S %9s",s);} // sets the Pairing Code - use is NOT recommended. Use 'S' from CLI instead + void enableAutoStartAP(){autoStartAPEnabled=true;} // enables auto start-up of Access Point when WiFi Credentials not found void setWifiCredentials(const char *ssid, const char *pwd); // sets WiFi Credentials };