From 40798b15cfbb6431c0f4e31a0002b18f0459020d Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 13 Jun 2021 08:45:11 -0500 Subject: [PATCH] Begin development of NVS Characteristic storage Used method restore() to restore value. To do: Change this to a flag during instantiation of a new Characteristic instead of a separate method. --- src/HAP.cpp | 3 --- src/HAP.h | 1 - src/HomeSpan.cpp | 36 ++++++++++++++++++++++++++++++------ src/HomeSpan.h | 4 ++++ src/src.ino | 10 +++++----- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index c29658b..66512ea 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -26,7 +26,6 @@ ********************************************************************************/ #include -#include #include #include @@ -39,8 +38,6 @@ void HAPClient::init(){ size_t len; // not used but required to read blobs from NVS - nvs_flash_init(); // initialize non-volatile-storage partition in flash - nvs_open("WIFI",NVS_READWRITE,&wifiNVS); // open WIFI data namespace in NVS nvs_open("SRP",NVS_READWRITE,&srpNVS); // open SRP data namespace in NVS nvs_open("HAP",NVS_READWRITE,&hapNVS); // open HAP data namespace in NVS diff --git a/src/HAP.h b/src/HAP.h index 070a170..23c3f96 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -28,7 +28,6 @@ #pragma once #include -#include #include "HomeSpan.h" #include "TLV.h" diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index b273ae2..71b58ae 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -65,6 +65,9 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa hapServer=new WiFiServer(tcpPortNum); + nvs_flash_init(); // initialize non-volatile-storage partition in flash + nvs_open("CHAR",NVS_READWRITE,&charNVS); + delay(2000); Serial.print("\n************************************************************\n" @@ -1135,13 +1138,15 @@ int Span::updateCharacteristics(char *buf, SpanBuf *pObj){ LOG1(pObj[j].characteristic->aid); LOG1(" iid="); LOG1(pObj[j].characteristic->iid); - if(status==StatusCode::OK){ // if status is okay - pObj[j].characteristic->value - =pObj[j].characteristic->newValue; // update characteristic value with new value + if(status==StatusCode::OK){ // if status is okay + pObj[j].characteristic->value=pObj[j].characteristic->newValue; // update characteristic value with new value + if(pObj[j].characteristic->nvsKey){ // if storage key found + nvs_set_blob(charNVS,pObj[j].characteristic->nvsKey,&(pObj[j].characteristic->value),sizeof(pObj[j].characteristic->value)); // store data + nvs_commit(charNVS); + } LOG1(" (okay)\n"); - } else { // if status not okay - pObj[j].characteristic->newValue - =pObj[j].characteristic->value; // replace characteristic new value with original value + } else { // if status not okay + pObj[j].characteristic->newValue=pObj[j].characteristic->value; // replace characteristic new value with original value LOG1(" (failed)\n"); } pObj[j].characteristic->isUpdated=false; // reset isUpdated flag for characteristic @@ -1520,6 +1525,25 @@ SpanCharacteristic::SpanCharacteristic(HapChar *hapChar){ /////////////////////////////// +void SpanCharacteristic::restore(){ + + nvsKey=(char *)malloc(16); + uint16_t t; + sscanf(type,"%x",&t); + sprintf(nvsKey,"%04X%08X%03X",t,aid,iid&0xFFF); + size_t len; + + if(!nvs_get_blob(homeSpan.charNVS,nvsKey,NULL,&len)){ + nvs_get_blob(homeSpan.charNVS,nvsKey,&value,&len); + } + else { + nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data + nvs_commit(homeSpan.charNVS); // commit to NVS + } +} + +/////////////////////////////// + int SpanCharacteristic::sprintfAttributes(char *cBuf, int flags){ int nBytes=0; diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 1d604a6..1525ef8 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -33,6 +33,7 @@ #include #include +#include #include "Settings.h" #include "Utils.h" @@ -106,6 +107,7 @@ struct Span{ boolean isBridge=true; // flag indicating whether device is configured as a bridge (i.e. first Accessory contains nothing but AccessoryInformation and HAPProtocolInformation) HapQR qrCode; // optional QR Code to use for pairing const char *sketchVersion="n/a"; // version of the sketch + nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data boolean connected=false; // WiFi connection status unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts @@ -252,6 +254,7 @@ struct SpanCharacteristic{ boolean staticRange; // Flag that indiates whether Range is static and cannot be changed with setRange() boolean customRange=false; // Flag for custom ranges boolean *ev; // Characteristic Event Notify Enable (per-connection) + char *nvsKey=NULL; // key for NVS storage of Characteristic value uint32_t aid=0; // Accessory ID - passed through from Service containing this Characteristic boolean isUpdated=false; // set to true when new value has been requested by PUT /characteristic @@ -263,6 +266,7 @@ struct SpanCharacteristic{ int sprintfAttributes(char *cBuf, int flags); // prints Characteristic JSON records into buf, according to flags mask; return number of characters printed, excluding null terminator StatusCode loadUpdate(char *val, char *ev); // load updated val/ev from PUT /characteristic JSON request. Return intiial HAP status code (checks to see if characteristic is found, is writable, etc.) + void restore(); // loads previous value of Characteristic from NVS (if found) boolean updated(){return(isUpdated);} // returns isUpdated unsigned long timeVal(); // returns time elapsed (in millis) since value was last updated diff --git a/src/src.ino b/src/src.ino index 1f4c5ab..e103ba0 100644 --- a/src/src.ino +++ b/src/src.ino @@ -39,13 +39,13 @@ void setup() { new Characteristic::Version("1.1.0"); // Set the Version Characteristic to "1.1.0" as required by HAP new Service::LightBulb(); - new Characteristic::On(); - new Characteristic::Brightness(); + (new Characteristic::On())->restore(); + (new Characteristic::Brightness())->restore(); new Characteristic::Name("Light 1"); - new Characteristic::ColorTemperature(0); + new Characteristic::ColorTemperature(); new Service::LightBulb(); - new Characteristic::On(2); - (new Characteristic::Brightness(50))->setRange(10,100,5); + (new Characteristic::On())->restore(); + (new Characteristic::Brightness(50))->setRange(10,100,5)->restore(); new Characteristic::Name("Light 2"); } // end of setup()