Moved all NVS Handles into Span

Also added Span constructor that call nvs_open for each nvs_handle upon start-up instead of waiting for homeSpan.begin() and HAP::init()
This commit is contained in:
Gregg 2024-03-02 15:49:06 -06:00
parent 3bb9df4818
commit 24f36bbccb
5 changed files with 30 additions and 33 deletions

View File

@ -47,8 +47,8 @@ void HAPClient::init(){
} }
if(!strlen(homeSpan.qrID)){ // if Setup ID has not been specified in sketch 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 if(!nvs_get_str(homeSpan.hapNVS,"SETUPID",NULL,&len)){ // check for saved value
nvs_get_str(hapNVS,"SETUPID",homeSpan.qrID,&len); // retrieve data nvs_get_str(homeSpan.hapNVS,"SETUPID",homeSpan.qrID,&len); // retrieve data
} else { } else {
sprintf(homeSpan.qrID,"%s",DEFAULT_QR_ID); // use default 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 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 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 if(!nvs_get_blob(homeSpan.hapNVS,"ACCESSORY",NULL,&len)){ // if found long-term Accessory data in NVS
nvs_get_blob(hapNVS,"ACCESSORY",&accessory,&len); // retrieve data nvs_get_blob(homeSpan.hapNVS,"ACCESSORY",&accessory,&len); // retrieve data
} else { } else {
LOG0("Generating new random Accessory ID and Long-Term Ed25519 Signature Keys...\n\n"); LOG0("Generating new random Accessory ID and Long-Term Ed25519 Signature Keys...\n\n");
uint8_t buf[6]; uint8_t buf[6];
@ -71,13 +71,13 @@ void HAPClient::init(){
memcpy(accessory.ID,cBuf,17); // copy into Accessory ID for permanent storage 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 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_set_blob(homeSpan.hapNVS,"ACCESSORY",&accessory,sizeof(accessory)); // update data
nvs_commit(hapNVS); // commit to NVS 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<Controller> tBuf(len/sizeof(Controller)); TempBuffer<Controller> 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.size();i++){ for(int i=0;i<tBuf.size();i++){
if(tBuf[i].allocated) if(tBuf[i].allocated)
controllerList.push_back(tBuf[i]); controllerList.push_back(tBuf[i]);
@ -92,12 +92,12 @@ void HAPClient::init(){
printControllers(); printControllers();
if(!nvs_get_blob(hapNVS,"HAPHASH",NULL,&len)){ // if found HAP HASH structure if(!nvs_get_blob(homeSpan.hapNVS,"HAPHASH",NULL,&len)){ // if found HAP HASH structure
nvs_get_blob(hapNVS,"HAPHASH",&homeSpan.hapConfig,&len); // retrieve data nvs_get_blob(homeSpan.hapNVS,"HAPHASH",&homeSpan.hapConfig,&len); // retrieve data
} else { } else {
LOG0("Resetting Database Hash...\n"); LOG0("Resetting Database Hash...\n");
nvs_set_blob(hapNVS,"HAPHASH",&homeSpan.hapConfig,sizeof(homeSpan.hapConfig)); // save data (will default to all zero values, which will then be updated below) nvs_set_blob(homeSpan.hapNVS,"HAPHASH",&homeSpan.hapConfig,sizeof(homeSpan.hapConfig)); // save data (will default to all zero values, which will then be updated below)
nvs_commit(hapNVS); // commit to NVS nvs_commit(homeSpan.hapNVS); // commit to NVS
} }
if(homeSpan.updateDatabase(false)){ // create Configuration Number and Loop vector if(homeSpan.updateDatabase(false)){ // create Configuration Number and Loop vector
@ -1504,15 +1504,15 @@ void HAPClient::printControllers(int minLogLevel){
void HAPClient::saveControllers(){ void HAPClient::saveControllers(){
if(controllerList.empty()){ if(controllerList.empty()){
nvs_erase_key(hapNVS,"CONTROLLERS"); nvs_erase_key(homeSpan.hapNVS,"CONTROLLERS");
return; return;
} }
TempBuffer<Controller> tBuf(controllerList.size()); // create temporary buffer to hold Controller data TempBuffer<Controller> 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_set_blob(homeSpan.hapNVS,"CONTROLLERS",tBuf,tBuf.len()); // update data
nvs_commit(hapNVS); // commit to NVS 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 // instantiate all static HAP Client structures and data
nvs_handle HAPClient::hapNVS;
HKDF HAPClient::hkdf; HKDF HAPClient::hkdf;
pairState HAPClient::pairStatus; pairState HAPClient::pairStatus;
Accessory HAPClient::accessory; Accessory HAPClient::accessory;
list<Controller, Mallocator<Controller>> HAPClient::controllerList; list<Controller, Mallocator<Controller>> HAPClient::controllerList;
//SRP6A *HAPClient::srp=NULL;
int HAPClient::conNum; int HAPClient::conNum;

View File

@ -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_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 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 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 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<Controller, Mallocator<Controller>> controllerList; // linked-list of Paired Controller IDs and ED25519 long-term public keys - permanently stored static list<Controller, Mallocator<Controller>> 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 static int conNum; // connection number - used to keep track of per-connection EV notifications

View File

@ -63,7 +63,7 @@ Span::Span(){
nvs_open("OTA",NVS_READWRITE,&otaNVS); // open OTA data namespace in NVS 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("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); nvs_get_u8(wifiNVS,"REBOOTS",&rebootCount);
rebootCount++; rebootCount++;
@ -648,8 +648,8 @@ void Span::processSerialCommand(const char *c){
if(strlen(s)==4 && strlen(tBuf)==4){ if(strlen(s)==4 && strlen(tBuf)==4){
sprintf(qrID,"%s",tBuf); sprintf(qrID,"%s",tBuf);
LOG0("\nChanging default Setup ID for QR Code to: '%s'. Will take effect after next restart.\n\n",qrID); 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_set_str(hapNVS,"SETUPID",qrID);
nvs_commit(HAPClient::hapNVS); nvs_commit(hapNVS);
} else { } 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); 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': { case 'H': {
nvs_erase_all(HAPClient::hapNVS); nvs_erase_all(hapNVS);
nvs_commit(HAPClient::hapNVS); nvs_commit(hapNVS);
LOG0("\n*** HomeSpan Device ID and Pairing Data DELETED! Restarting...\n\n"); LOG0("\n*** HomeSpan Device ID and Pairing Data DELETED! Restarting...\n\n");
reboot(); reboot();
} }
@ -792,8 +792,8 @@ void Span::processSerialCommand(const char *c){
case 'F': { case 'F': {
nvs_erase_all(HAPClient::hapNVS); nvs_erase_all(hapNVS);
nvs_commit(HAPClient::hapNVS); nvs_commit(hapNVS);
nvs_erase_all(wifiNVS); nvs_erase_all(wifiNVS);
nvs_commit(wifiNVS); nvs_commit(wifiNVS);
nvs_erase_all(charNVS); nvs_erase_all(charNVS);
@ -1078,7 +1078,7 @@ void Span::processSerialCommand(const char *c){
readSerial(qSave,1); readSerial(qSave,1);
if(qSave[0]=='y'){ if(qSave[0]=='y'){
LOG0("(yes)\nData saved! Rebooting..."); 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(); HAPClient::saveControllers();
reboot(); reboot();
} else } else
@ -1578,8 +1578,8 @@ boolean Span::updateDatabase(boolean updateMDNS){
if(hapConfig.configNumber==65536) // reached max value if(hapConfig.configNumber==65536) // reached max value
hapConfig.configNumber=1; // reset to 1 hapConfig.configNumber=1; // reset to 1
nvs_set_blob(HAPClient::hapNVS,"HAPHASH",&hapConfig,sizeof(hapConfig)); // update data nvs_set_blob(hapNVS,"HAPHASH",&hapConfig,sizeof(hapConfig)); // update data
nvs_commit(HAPClient::hapNVS); // commit to NVS nvs_commit(hapNVS); // commit to NVS
changed=true; changed=true;
if(updateMDNS){ if(updateMDNS){

View File

@ -226,6 +226,7 @@ class Span{
nvs_handle wifiNVS=0; // handle for non-volatile-storage of WiFi data 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 otaNVS; // handle for non-volatile storage of OTA data
nvs_handle srpNVS; // handle for non-volatile storage of SRP 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) 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 unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts

View File

@ -35,7 +35,7 @@ void setup() {
homeSpan.setLogLevel(2); homeSpan.setLogLevel(2);
homeSpan.enableWebLog(50,"pool.ntp.org","UTC",NULL); 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,"pool.ntp.org","UTC","myStatus");
// homeSpan.enableWebLog(50,NULL,NULL,NULL); // homeSpan.enableWebLog(50,NULL,NULL,NULL);