From f6d4d37ff7a1059ea4065b5eb6d1a1b7508f8cae Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 30 Mar 2024 17:51:37 -0500 Subject: [PATCH] Initial creation of setTLV() --- src/HAP.h | 1 - src/HomeSpan.h | 25 ++++++++++++++++++++++++- src/src.ino | 7 +------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/HAP.h b/src/HAP.h index 71779cb..c263277 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -34,7 +34,6 @@ #include "HAPConstants.h" #include "HKDF.h" #include "SRP.h" -#include "TLV8.h" const TLV8_names HAP_Names[] = { {kTLVType_Separator,"SEPARATOR"}, diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 2d0fa22..7298a7d 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -55,6 +55,7 @@ #include "HAPConstants.h" #include "HapQR.h" #include "Characteristics.h" +#include "TLV8.h" using std::vector; using std::unordered_map; @@ -756,12 +757,34 @@ class SpanCharacteristic{ } size_t olen; - mbedtls_base64_encode(NULL,0,&olen,data,len); // get length of string buffer needed (mbedtls includes the trailing null in this size) + mbedtls_base64_encode(NULL,0,&olen,NULL,len); // get length of string buffer needed (mbedtls includes the trailing null in this size) TempBuffer 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){ + + 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); + + 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 nChars; + mbedtls_base64_encode(NULL,0,&nChars,NULL,nBytes); // get length of string buffer needed (mbedtls includes the trailing null in this size) + value.STRING = (char *)HS_REALLOC(value.STRING,nChars); // allocate sufficient size for storing value + TempBuffer tBuf(bufSize); // create fixed-size buffer to store packed TLV bytes + tlv.pack_init(); // initialize TLV packing + uint8_t *p=(uint8_t *)value.STRING; // set pointer to beginning of value + while((nBytes=tlv.pack(tBuf,bufSize))>0){ // pack the next set of TLV bytes, up to a maximum of bufSize, into tBuf + size_t olen; // number of characters written (excludes null character) + 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 + } + } + template void setVal(T val, boolean notify=true){ if(!((perms&EV) || (updateFlag==2))){ diff --git a/src/src.ino b/src/src.ino index 01f20f2..611143a 100644 --- a/src/src.ino +++ b/src/src.ino @@ -26,7 +26,6 @@ ********************************************************************************/ #include "HomeSpan.h" -#include "TLV8.h" CUSTOM_CHAR_TLV(DisplayOrder,136,PR+EV); @@ -52,11 +51,7 @@ struct HomeSpanTV : Service::Television { } orderTLV.print(); - size_t n=orderTLV.pack_size(); - Serial.printf("Size=%d\n",n); - uint8_t c[n]; - orderTLV.pack(c); - displayOrder->setData(c,n); + displayOrder->setTLV(orderTLV); new SpanUserCommand('P', "- change order of inputs", changeOrder, this); }