diff --git a/src/HAP.cpp b/src/HAP.cpp index b87b7b7..784e496 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -47,8 +47,8 @@ void HAPClient::init(){ } if(!strlen(homeSpan.qrID)){ // if Setup ID has not been specified in sketch - if(!nvs_get_str(hapNVS,"SETUPID",NULL,&len)){ // check for saved value - nvs_get_str(hapNVS,"SETUPID",homeSpan.qrID,&len); // retrieve data + if(!nvs_get_str(homeSpan.hapNVS,"SETUPID",NULL,&len)){ // check for saved value + nvs_get_str(homeSpan.hapNVS,"SETUPID",homeSpan.qrID,&len); // retrieve data } else { sprintf(homeSpan.qrID,"%s",DEFAULT_QR_ID); // use default } @@ -57,8 +57,8 @@ void HAPClient::init(){ if(nvs_get_blob(homeSpan.srpNVS,"VERIFYDATA",NULL,&len)) // if Pair-Setup verification code data not found in NVS homeSpan.setPairingCode(DEFAULT_SETUP_CODE,false); // create and save verification from default Pairing Setup Code - if(!nvs_get_blob(hapNVS,"ACCESSORY",NULL,&len)){ // if found long-term Accessory data in NVS - nvs_get_blob(hapNVS,"ACCESSORY",&accessory,&len); // retrieve data + if(!nvs_get_blob(homeSpan.hapNVS,"ACCESSORY",NULL,&len)){ // if found long-term Accessory data in NVS + nvs_get_blob(homeSpan.hapNVS,"ACCESSORY",&accessory,&len); // retrieve data } else { LOG0("Generating new random Accessory ID and Long-Term Ed25519 Signature Keys...\n\n"); uint8_t buf[6]; @@ -71,13 +71,13 @@ void HAPClient::init(){ memcpy(accessory.ID,cBuf,17); // copy into Accessory ID for permanent storage crypto_sign_keypair(accessory.LTPK,accessory.LTSK); // generate new random set of keys using libsodium public-key signature - nvs_set_blob(hapNVS,"ACCESSORY",&accessory,sizeof(accessory)); // update data - nvs_commit(hapNVS); // commit to NVS + nvs_set_blob(homeSpan.hapNVS,"ACCESSORY",&accessory,sizeof(accessory)); // update data + nvs_commit(homeSpan.hapNVS); // commit to NVS } - if(!nvs_get_blob(hapNVS,"CONTROLLERS",NULL,&len)){ // if found long-term Controller Pairings data from NVS + if(!nvs_get_blob(homeSpan.hapNVS,"CONTROLLERS",NULL,&len)){ // if found long-term Controller Pairings data from NVS TempBuffer tBuf(len/sizeof(Controller)); - nvs_get_blob(hapNVS,"CONTROLLERS",tBuf,&len); // retrieve data + nvs_get_blob(homeSpan.hapNVS,"CONTROLLERS",tBuf,&len); // retrieve data for(int i=0;i tBuf(controllerList.size()); // create temporary buffer to hold Controller data - std::copy(controllerList.begin(),controllerList.end(),tBuf.get()); // copy data from linked list to buffer + std::copy(controllerList.begin(),controllerList.end(),tBuf.get()); // copy data from linked list to buffer - nvs_set_blob(hapNVS,"CONTROLLERS",tBuf,tBuf.len()); // update data - nvs_commit(hapNVS); // commit to NVS + nvs_set_blob(homeSpan.hapNVS,"CONTROLLERS",tBuf,tBuf.len()); // update data + nvs_commit(homeSpan.hapNVS); // commit to NVS } @@ -1690,11 +1690,9 @@ void HapOut::HapStreamBuffer::printFormatted(char *buf, size_t nChars, size_t ns // instantiate all static HAP Client structures and data -nvs_handle HAPClient::hapNVS; HKDF HAPClient::hkdf; pairState HAPClient::pairStatus; Accessory HAPClient::accessory; list> HAPClient::controllerList; -//SRP6A *HAPClient::srp=NULL; int HAPClient::conNum; diff --git a/src/HAP.h b/src/HAP.h index cdf176a..6647597 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -105,11 +105,9 @@ struct HAPClient { static const int MAX_CONTROLLERS=16; // maximum number of paired controllers (HAP requires at least 16) static const int MAX_ACCESSORIES=150; // maximum number of allowed Accessories (HAP limit=150) - static nvs_handle hapNVS; // handle for non-volatile-storage of HAP data static HKDF hkdf; // generates (and stores) HKDF-SHA-512 32-byte keys derived from an inputKey of arbitrary length, a salt string, and an info string static pairState pairStatus; // tracks pair-setup status -// static SRP6A *srp; // stores all SRP-6A keys used for Pair-Setup (must persist through multiple calls to Pair-Setup) - static Accessory accessory; // Accessory ID and Ed25519 public and secret keys- permanently stored + static Accessory accessory; // Accessory ID and Ed25519 public and secret keys - permanently stored static list> controllerList; // linked-list of Paired Controller IDs and ED25519 long-term public keys - permanently stored static int conNum; // connection number - used to keep track of per-connection EV notifications diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 2b57ce7..73c0499 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -63,7 +63,7 @@ Span::Span(){ nvs_open("OTA",NVS_READWRITE,&otaNVS); // open OTA data namespace in NVS nvs_open("SRP",NVS_READWRITE,&srpNVS); // open SRP data namespace in NVS - nvs_open("HAP",NVS_READWRITE,&HAPClient::hapNVS); // open HAP data namespace in NVS + nvs_open("HAP",NVS_READWRITE,&hapNVS); // open HAP data namespace in NVS nvs_get_u8(wifiNVS,"REBOOTS",&rebootCount); rebootCount++; @@ -268,7 +268,7 @@ void Span::pollTask() { LOG1("\n"); LOG2("\n"); - hap[freeSlot]->cPair=NULL; // reset pointer to verified ID + hap[freeSlot]->cPair=NULL; // reset pointer to verified ID homeSpan.clearNotify(freeSlot); // clear all notification requests for this connection HAPClient::pairStatus=pairState_M1; // reset starting PAIR STATE (which may be needed if Accessory failed in middle of pair-setup) } @@ -648,8 +648,8 @@ void Span::processSerialCommand(const char *c){ if(strlen(s)==4 && strlen(tBuf)==4){ sprintf(qrID,"%s",tBuf); LOG0("\nChanging default Setup ID for QR Code to: '%s'. Will take effect after next restart.\n\n",qrID); - nvs_set_str(HAPClient::hapNVS,"SETUPID",qrID); - nvs_commit(HAPClient::hapNVS); + nvs_set_str(hapNVS,"SETUPID",qrID); + nvs_commit(hapNVS); } else { LOG0("\n*** Invalid request to change Setup ID for QR Code to: '%s'. Setup ID must be exactly 4 alphanumeric characters (0-9, A-Z, and a-z).\n\n",s); } @@ -777,8 +777,8 @@ void Span::processSerialCommand(const char *c){ case 'H': { - nvs_erase_all(HAPClient::hapNVS); - nvs_commit(HAPClient::hapNVS); + nvs_erase_all(hapNVS); + nvs_commit(hapNVS); LOG0("\n*** HomeSpan Device ID and Pairing Data DELETED! Restarting...\n\n"); reboot(); } @@ -792,8 +792,8 @@ void Span::processSerialCommand(const char *c){ case 'F': { - nvs_erase_all(HAPClient::hapNVS); - nvs_commit(HAPClient::hapNVS); + nvs_erase_all(hapNVS); + nvs_commit(hapNVS); nvs_erase_all(wifiNVS); nvs_commit(wifiNVS); nvs_erase_all(charNVS); @@ -1078,7 +1078,7 @@ void Span::processSerialCommand(const char *c){ readSerial(qSave,1); if(qSave[0]=='y'){ LOG0("(yes)\nData saved! Rebooting..."); - nvs_set_blob(HAPClient::hapNVS,"ACCESSORY",&HAPClient::accessory,sizeof(HAPClient::accessory)); // update data + nvs_set_blob(hapNVS,"ACCESSORY",&HAPClient::accessory,sizeof(HAPClient::accessory)); // update data (commit is included in saveControllers below) HAPClient::saveControllers(); reboot(); } else @@ -1578,8 +1578,8 @@ boolean Span::updateDatabase(boolean updateMDNS){ if(hapConfig.configNumber==65536) // reached max value hapConfig.configNumber=1; // reset to 1 - nvs_set_blob(HAPClient::hapNVS,"HAPHASH",&hapConfig,sizeof(hapConfig)); // update data - nvs_commit(HAPClient::hapNVS); // commit to NVS + nvs_set_blob(hapNVS,"HAPHASH",&hapConfig,sizeof(hapConfig)); // update data + nvs_commit(hapNVS); // commit to NVS changed=true; if(updateMDNS){ diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 7ab6f06..ad16dd6 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -226,6 +226,7 @@ class Span{ nvs_handle wifiNVS=0; // handle for non-volatile-storage of WiFi data nvs_handle otaNVS; // handle for non-volatile storage of OTA data nvs_handle srpNVS; // handle for non-volatile storage of SRP data + nvs_handle hapNVS; // handle for non-volatile-storage of HAP data int connected=0; // WiFi connection status (increments upon each connect and disconnect) unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts diff --git a/src/src.ino b/src/src.ino index 2909559..ea87240 100644 --- a/src/src.ino +++ b/src/src.ino @@ -35,7 +35,7 @@ void setup() { homeSpan.setLogLevel(2); homeSpan.enableWebLog(50,"pool.ntp.org","UTC",NULL); - homeSpan.setPairingCode("12345670"); +// homeSpan.setPairingCode("12345670"); // homeSpan.enableWebLog(50,"pool.ntp.org","UTC","myStatus"); // homeSpan.enableWebLog(50,NULL,NULL,NULL);