updated setData and setTLV to use new common setValCheck() and setValFinish()

This commit is contained in:
Gregg 2024-03-30 22:13:52 -05:00
parent f6d4d37ff7
commit 11bd605a03
3 changed files with 69 additions and 41 deletions

View File

@ -887,8 +887,8 @@ void Span::processSerialCommand(const char *c){
isBridge=false; // ...this is not a bridge device isBridge=false; // ...this is not a bridge device
for(auto chr=(*svc)->Characteristics.begin(); chr!=(*svc)->Characteristics.end(); chr++){ for(auto chr=(*svc)->Characteristics.begin(); chr!=(*svc)->Characteristics.end(); chr++){
LOG0(" \u21e8 Characteristic %s(%.32s%s): IID=%d, %sUUID=\"%s\", %sPerms=", LOG0(" \u21e8 Characteristic %s(%.33s%s): IID=%d, %sUUID=\"%s\", %sPerms=",
(*chr)->hapName,(*chr)->uvPrint((*chr)->value).c_str(),strlen((*chr)->uvPrint((*chr)->value).c_str())>32?"...":"",(*chr)->iid,(*chr)->isCustom?"Custom-":"",(*chr)->type,(*chr)->perms!=(*chr)->hapChar->perms?"Custom-":""); (*chr)->hapName,(*chr)->uvPrint((*chr)->value).c_str(),strlen((*chr)->uvPrint((*chr)->value).c_str())>33?"...\"":"",(*chr)->iid,(*chr)->isCustom?"Custom-":"",(*chr)->type,(*chr)->perms!=(*chr)->hapChar->perms?"Custom-":"");
int foundPerms=0; int foundPerms=0;
for(uint8_t i=0;i<7;i++){ for(uint8_t i=0;i<7;i++){

View File

@ -677,11 +677,6 @@ class SpanCharacteristic{
void setString(const char *val, boolean notify=true){ void setString(const char *val, boolean notify=true){
if(!((perms&EV) || (updateFlag==2))){
LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setString() ignored. No EVENT NOTIFICATION (EV) permission on this characteristic\n\n",hapName);
return;
}
if(updateFlag==1) if(updateFlag==1)
LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setString() within update() while it is being updated by Home App. This may cause device to become non-responsive!\n\n",hapName); LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setString() within update() while it is being updated by Home App. This may cause device to become non-responsive!\n\n",hapName);
@ -746,52 +741,79 @@ class SpanCharacteristic{
void setData(uint8_t *data, size_t len, boolean notify=true){ void setData(uint8_t *data, size_t len, boolean notify=true){
if(!((perms&EV) || (updateFlag==2))){ setValCheck();
LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setData() ignored. No EVENT NOTIFICATION (EV) permission on this characteristic\n\n",hapName);
return; if(len>0){
size_t olen;
mbedtls_base64_encode(NULL,0,&olen,NULL,len); // get length of string buffer needed (mbedtls includes the trailing null in this size)
value.STRING = (char *)HS_REALLOC(value.STRING,olen); // allocate sufficient size for storing value
mbedtls_base64_encode((uint8_t*)value.STRING,olen,&olen,data,len ); // encode data into string buf
} else {
value.STRING = (char *)HS_REALLOC(value.STRING,1); // allocate sufficient size for just trailing null character
*value.STRING ='\0';
} }
if(len<1){ setValFinish(notify);
LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setData() ignored. Size of data buffer must be greater than zero\n\n",hapName);
return;
}
size_t olen;
mbedtls_base64_encode(NULL,0,&olen,NULL,len); // get length of string buffer needed (mbedtls includes the trailing null in this size)
TempBuffer<char> tBuf(olen); // create temporary string buffer, with room for trailing null
mbedtls_base64_encode((uint8_t*)tBuf.get(),olen,&olen,data,len ); // encode data into string buf
setString(tBuf,notify); // call setString to continue processing as if characteristic was a string
} }
void setTLV(TLV8 &tlv, boolean notify=true){ void setTLV(TLV8 &tlv, boolean notify=true){
if(updateFlag==1) setValCheck();
LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setVal() within update() while it is being updated by Home App. This may cause device to become non-responsive!\n\n",hapName);
const size_t bufSize=36; // maximum size of buffer to store packed TLV bytes before encoding directly into value; must be multiple of 3 const size_t bufSize=36; // maximum size of buffer to store packed TLV bytes before encoding directly into value; must be multiple of 3
size_t nBytes=tlv.pack_size(); // total size of packed TLV in bytes size_t nBytes=tlv.pack_size(); // total size of packed TLV in bytes
size_t nChars;
mbedtls_base64_encode(NULL,0,&nChars,NULL,nBytes); // get length of string buffer needed (mbedtls includes the trailing null in this size) if(nBytes>0){
value.STRING = (char *)HS_REALLOC(value.STRING,nChars); // allocate sufficient size for storing value size_t nChars;
TempBuffer<uint8_t> tBuf(bufSize); // create fixed-size buffer to store packed TLV bytes mbedtls_base64_encode(NULL,0,&nChars,NULL,nBytes); // get length of string buffer needed (mbedtls includes the trailing null in this size)
tlv.pack_init(); // initialize TLV packing value.STRING = (char *)HS_REALLOC(value.STRING,nChars); // allocate sufficient size for storing value
uint8_t *p=(uint8_t *)value.STRING; // set pointer to beginning of value TempBuffer<uint8_t> tBuf(bufSize); // create fixed-size buffer to store packed TLV bytes
while((nBytes=tlv.pack(tBuf,bufSize))>0){ // pack the next set of TLV bytes, up to a maximum of bufSize, into tBuf tlv.pack_init(); // initialize TLV packing
size_t olen; // number of characters written (excludes null character) uint8_t *p=(uint8_t *)value.STRING; // set pointer to beginning of value
mbedtls_base64_encode(p,nChars,&olen,tBuf,nBytes); // encode data directly into value while((nBytes=tlv.pack(tBuf,bufSize))>0){ // pack the next set of TLV bytes, up to a maximum of bufSize, into tBuf
p+=olen; // advance pointer to null character size_t olen; // number of characters written (excludes null character)
nChars-=olen; // subtract number of characters remaining mbedtls_base64_encode(p,nChars,&olen,tBuf,nBytes); // encode data directly into value
p+=olen; // advance pointer to null character
nChars-=olen; // subtract number of characters remaining
}
} else {
value.STRING = (char *)HS_REALLOC(value.STRING,1); // allocate sufficient size for just trailing null character
*value.STRING ='\0';
}
setValFinish(notify);
}
void setValCheck(){
if(updateFlag==1)
LOG0("\n*** WARNING: Attempt to set value of Characteristic::%s within update() while it is being simultaneously updated by Home App. This may cause device to become non-responsive!\n\n",hapName);
}
void setValFinish(boolean notify){
uvSet(newValue,value);
updateTime=homeSpan.snapTime;
if(notify){
if((perms&EV) && (updateFlag!=2)){ // only broadcast notification if EV permission is set AND update is NOT being done in context of write-response
SpanBuf sb; // create SpanBuf object
sb.characteristic=this; // set characteristic
sb.status=StatusCode::OK; // set status
char dummy[]="";
sb.val=dummy; // set dummy "val" so that printfNotify knows to consider this "update"
homeSpan.Notifications.push_back(sb); // store SpanBuf in Notifications vector
}
if(nvsKey){
nvs_set_str(homeSpan.charNVS,nvsKey,value.STRING); // store data
nvs_commit(homeSpan.charNVS);
}
} }
} }
template <typename T> void setVal(T val, boolean notify=true){ template <typename T> void setVal(T val, boolean notify=true){
if(!((perms&EV) || (updateFlag==2))){
LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setVal() ignored. No EVENT NOTIFICATION (EV) permission on this characteristic\n\n",hapName);
return;
}
if(updateFlag==1) if(updateFlag==1)
LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setVal() within update() while it is being updated by Home App. This may cause device to become non-responsive!\n\n",hapName); LOG0("\n*** WARNING: Attempt to update Characteristic::%s with setVal() within update() while it is being updated by Home App. This may cause device to become non-responsive!\n\n",hapName);

View File

@ -28,6 +28,7 @@
#include "HomeSpan.h" #include "HomeSpan.h"
CUSTOM_CHAR_TLV(DisplayOrder,136,PR+EV); CUSTOM_CHAR_TLV(DisplayOrder,136,PR+EV);
CUSTOM_CHAR_DATA(TestData,333,PR+EV);
struct HomeSpanTV : Service::Television { struct HomeSpanTV : Service::Television {
@ -36,6 +37,7 @@ struct HomeSpanTV : Service::Television {
SpanCharacteristic *remoteKey = new Characteristic::RemoteKey(); // Used to receive button presses from the Remote Control widget SpanCharacteristic *remoteKey = new Characteristic::RemoteKey(); // Used to receive button presses from the Remote Control widget
SpanCharacteristic *settingsKey = new Characteristic::PowerModeSelection(); // Adds "View TV Setting" option to Selection Screen SpanCharacteristic *settingsKey = new Characteristic::PowerModeSelection(); // Adds "View TV Setting" option to Selection Screen
SpanCharacteristic *displayOrder = new Characteristic::DisplayOrder(); SpanCharacteristic *displayOrder = new Characteristic::DisplayOrder();
SpanCharacteristic *testData = new Characteristic::TestData();
HomeSpanTV(const char *name) : Service::Television() { HomeSpanTV(const char *name) : Service::Television() {
new Characteristic::ConfiguredName(name); // Name of TV new Characteristic::ConfiguredName(name); // Name of TV
@ -53,6 +55,10 @@ struct HomeSpanTV : Service::Television {
orderTLV.print(); orderTLV.print();
displayOrder->setTLV(orderTLV); displayOrder->setTLV(orderTLV);
uint8_t blob[]={1,2,3,4,5,6,7,8,9,10,11,12};
// testData->setData(blob,sizeof(blob));
testData->setData(blob,1);
new SpanUserCommand('P', "- change order of inputs", changeOrder, this); new SpanUserCommand('P', "- change order of inputs", changeOrder, this);
} }