Change NVS storage for Characteristics to always use nvs_set_u64()
Reduces NVS consumption by from 3 to only 1 records when storing numerical values. Also: Fixed memory problem in getCharacteristics by replacing dynamically-sized stack buffers with TempBuffer (must do this everywhere), which was causing stack probllem when combination number of Accessories and number of Characteristics got very large.
This commit is contained in:
parent
9e0512e48b
commit
b7c294d210
18
src/HAP.cpp
18
src/HAP.cpp
|
|
@ -1006,7 +1006,8 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){
|
||||||
if(urlBuf[i]==',')
|
if(urlBuf[i]==',')
|
||||||
numIDs++;
|
numIDs++;
|
||||||
|
|
||||||
char *ids[numIDs]; // reserve space for number of IDs found
|
// char *ids[numIDs]; // reserve space for number of IDs found
|
||||||
|
TempBuffer<char *> ids(numIDs); // reserve space for number of IDs found
|
||||||
int flags=GET_VALUE|GET_AID; // flags indicating which characteristic fields to include in response (HAP Table 6-13)
|
int flags=GET_VALUE|GET_AID; // flags indicating which characteristic fields to include in response (HAP Table 6-13)
|
||||||
numIDs=0; // reset number of IDs found
|
numIDs=0; // reset number of IDs found
|
||||||
|
|
||||||
|
|
@ -1035,7 +1036,7 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){
|
||||||
char *p2;
|
char *p2;
|
||||||
while(char *t2=strtok_r(t1,",",&p2)){ // parse IDs
|
while(char *t2=strtok_r(t1,",",&p2)){ // parse IDs
|
||||||
t1=NULL;
|
t1=NULL;
|
||||||
ids[numIDs++]=t2;
|
ids.get()[numIDs++]=t2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // parse URL
|
} // parse URL
|
||||||
|
|
@ -1043,11 +1044,12 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){
|
||||||
if(!numIDs) // could not find any IDs
|
if(!numIDs) // could not find any IDs
|
||||||
return(0);
|
return(0);
|
||||||
|
|
||||||
int nBytes=homeSpan.sprintfAttributes(ids,numIDs,flags,NULL); // get JSON response - includes terminating null (will be recast to uint8_t* below)
|
int nBytes=homeSpan.sprintfAttributes(ids.get(),numIDs,flags,NULL); // get JSON response - includes terminating null (will be recast to uint8_t* below)
|
||||||
char jsonBuf[nBytes+1];
|
// char jsonBuf[nBytes+1];
|
||||||
homeSpan.sprintfAttributes(ids,numIDs,flags,jsonBuf);
|
TempBuffer<char> jsonBuf(nBytes+1);
|
||||||
|
homeSpan.sprintfAttributes(ids.get(),numIDs,flags,jsonBuf.get());
|
||||||
|
|
||||||
boolean sFlag=strstr(jsonBuf,"status"); // status attribute found?
|
boolean sFlag=strstr(jsonBuf.get(),"status"); // status attribute found?
|
||||||
|
|
||||||
char *body;
|
char *body;
|
||||||
asprintf(&body,"HTTP/1.1 %s\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",!sFlag?"200 OK":"207 Multi-Status",nBytes);
|
asprintf(&body,"HTTP/1.1 %s\r\nContent-Type: application/hap+json\r\nContent-Length: %d\r\n\r\n",!sFlag?"200 OK":"207 Multi-Status",nBytes);
|
||||||
|
|
@ -1056,10 +1058,10 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){
|
||||||
LOG2(client.remoteIP());
|
LOG2(client.remoteIP());
|
||||||
LOG2(" >>>>>>>>>>\n");
|
LOG2(" >>>>>>>>>>\n");
|
||||||
LOG2(body);
|
LOG2(body);
|
||||||
LOG2(jsonBuf);
|
LOG2(jsonBuf.get());
|
||||||
LOG2("\n");
|
LOG2("\n");
|
||||||
|
|
||||||
sendEncrypted(body,(uint8_t *)jsonBuf,nBytes); // note recasting of jsonBuf into uint8_t*
|
sendEncrypted(body,(uint8_t *)jsonBuf.get(),nBytes); // note recasting of jsonBuf into uint8_t*
|
||||||
free(body);
|
free(body);
|
||||||
|
|
||||||
return(1);
|
return(1);
|
||||||
|
|
|
||||||
|
|
@ -1452,7 +1452,7 @@ int Span::updateCharacteristics(char *buf, SpanBuf *pObj){
|
||||||
pObj[j].characteristic->uvSet(pObj[j].characteristic->value,pObj[j].characteristic->newValue); // update characteristic value with new value
|
pObj[j].characteristic->uvSet(pObj[j].characteristic->value,pObj[j].characteristic->newValue); // update characteristic value with new value
|
||||||
if(pObj[j].characteristic->nvsKey){ // if storage key found
|
if(pObj[j].characteristic->nvsKey){ // if storage key found
|
||||||
if(pObj[j].characteristic->format!=FORMAT::STRING && pObj[j].characteristic->format!=FORMAT::DATA)
|
if(pObj[j].characteristic->format!=FORMAT::STRING && pObj[j].characteristic->format!=FORMAT::DATA)
|
||||||
nvs_set_blob(charNVS,pObj[j].characteristic->nvsKey,&(pObj[j].characteristic->value),sizeof(pObj[j].characteristic->value)); // store data
|
nvs_set_u64(charNVS,pObj[j].characteristic->nvsKey,pObj[j].characteristic->value.UINT64); // store data as uint64_t regardless of actual type (it will be read correctly when access through uvGet())
|
||||||
else
|
else
|
||||||
nvs_set_str(charNVS,pObj[j].characteristic->nvsKey,pObj[j].characteristic->value.STRING); // store data
|
nvs_set_str(charNVS,pObj[j].characteristic->nvsKey,pObj[j].characteristic->value.STRING); // store data
|
||||||
nvs_commit(charNVS);
|
nvs_commit(charNVS);
|
||||||
|
|
|
||||||
|
|
@ -627,11 +627,8 @@ class SpanCharacteristic{
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
if(format!=FORMAT::STRING && format!=FORMAT::DATA){
|
if(format!=FORMAT::STRING && format!=FORMAT::DATA){
|
||||||
if(!nvs_get_blob(homeSpan.charNVS,nvsKey,NULL,&len)){
|
if(nvs_get_u64(homeSpan.charNVS,nvsKey,&(value.UINT64))!=ESP_OK) {
|
||||||
nvs_get_blob(homeSpan.charNVS,nvsKey,&value,&len);
|
nvs_set_u64(homeSpan.charNVS,nvsKey,value.UINT64); // store data as uint64_t regardless of actual type (it will be read correctly when access through uvGet())
|
||||||
}
|
|
||||||
else {
|
|
||||||
nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data
|
|
||||||
nvs_commit(homeSpan.charNVS); // commit to NVS
|
nvs_commit(homeSpan.charNVS); // commit to NVS
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -790,7 +787,7 @@ class SpanCharacteristic{
|
||||||
homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector
|
homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector
|
||||||
|
|
||||||
if(nvsKey){
|
if(nvsKey){
|
||||||
nvs_set_blob(homeSpan.charNVS,nvsKey,&value,sizeof(UVal)); // store data
|
nvs_set_u64(homeSpan.charNVS,nvsKey,value.UINT64); // store data as uint64_t regardless of actual type (it will be read correctly when access through uvGet())
|
||||||
nvs_commit(homeSpan.charNVS);
|
nvs_commit(homeSpan.charNVS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
src/src.ino
15
src/src.ino
|
|
@ -32,10 +32,9 @@ void setup() {
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
homeSpan.setLogLevel(2)
|
homeSpan.setLogLevel(2);
|
||||||
.enableWebLog(500,"pool.ntp.org","UTC")
|
// homeSpan.enableWebLog(500,"pool.ntp.org","UTC");
|
||||||
.setWifiCallback(wifiEstablished)
|
// homeSpan.setWifiCallback(wifiEstablished);
|
||||||
;
|
|
||||||
|
|
||||||
homeSpan.begin(Category::Lighting,"HomeSpan Max");
|
homeSpan.begin(Category::Lighting,"HomeSpan Max");
|
||||||
|
|
||||||
|
|
@ -43,7 +42,7 @@ void setup() {
|
||||||
ps_new(Service::AccessoryInformation)();
|
ps_new(Service::AccessoryInformation)();
|
||||||
ps_new(Characteristic::Identify)();
|
ps_new(Characteristic::Identify)();
|
||||||
|
|
||||||
for(int i=0;i<100;i++){
|
for(int i=0;i<149;i++){
|
||||||
ps_new(SpanAccessory)();
|
ps_new(SpanAccessory)();
|
||||||
ps_new(Service::AccessoryInformation)();
|
ps_new(Service::AccessoryInformation)();
|
||||||
ps_new(Characteristic::Identify)();
|
ps_new(Characteristic::Identify)();
|
||||||
|
|
@ -51,8 +50,10 @@ void setup() {
|
||||||
sprintf(c,"Light-%d",i);
|
sprintf(c,"Light-%d",i);
|
||||||
ps_new(Characteristic::Name)(c);
|
ps_new(Characteristic::Name)(c);
|
||||||
ps_new(Service::LightBulb)();
|
ps_new(Service::LightBulb)();
|
||||||
ps_new(Characteristic::On)();
|
ps_new(Characteristic::On)(0,true);
|
||||||
ps_new(Characteristic::Brightness)(50,false);
|
ps_new(Characteristic::Brightness)(50,true);
|
||||||
|
ps_new(Characteristic::Hue)(120,true);
|
||||||
|
ps_new(Characteristic::Saturation)(100,true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue