diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 97a102e..0e90699 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -735,24 +735,40 @@ class SpanCharacteristic{ setValFinish(notify); } - - size_t getTLV(TLV8 &tlv){ + size_t getTLV(TLV8 &tlv){ + if(format tBuf(olen); // create temporary buffer + const size_t bufSize=36; // maximum size of buffer to store decoded bytes before unpacking into TLV; must be multiple of 3 + TempBuffer tBuf(bufSize); // create fixed-size buffer to store decoded bytes + tlv.wipe(); // clear TLV completely - int ret=mbedtls_base64_decode(tBuf,olen,&olen,(uint8_t *)value.STRING,strlen(value.STRING)); + size_t nChars=strlen(value.STRING); // total characters to decode + uint8_t *p=(uint8_t *)value.STRING; // set pointer to beginning of value + const size_t decodeSize=bufSize/3*4; // number of characters to decode in each pass + int status=0; - if(ret==MBEDTLS_ERR_BASE64_INVALID_CHARACTER){ - LOG0("\n*** WARNING: Can't decode Characteristic::%s with getTLV(). Data is not in base-64 format\n\n",hapName); - return(0); + while(nChars>0){ + size_t olen; + size_t n=nChars0){ + LOG0("\n*** WARNING: Can't unpack Characteristic::%s with getTLV(). TLV record is incomplete or corrupted\n\n",hapName); + tlv.wipe(); + return(0); + } + return(tlv.pack_size()); } void setTLV(TLV8 &tlv, boolean notify=true){ diff --git a/src/TLV8.cpp b/src/TLV8.cpp index 3a52bd9..ff766f7 100644 --- a/src/TLV8.cpp +++ b/src/TLV8.cpp @@ -160,7 +160,10 @@ size_t TLV8::pack(uint8_t *buf, size_t bufSize){ ///////////////////////////////////// -void TLV8::unpack(uint8_t *buf, size_t bufSize){ +int TLV8::unpack(uint8_t *buf, size_t bufSize){ + + if(bufSize==0) + return(-1); if(empty()) unpackPhase=0; @@ -171,18 +174,17 @@ void TLV8::unpack(uint8_t *buf, size_t bufSize){ case 0: unpackTag=*buf++; bufSize--; + add(unpackTag); unpackPhase=1; break; case 1: unpackBytes=*buf++; bufSize--; - if(unpackBytes==0){ - add(unpackTag); + if(unpackBytes==0) unpackPhase=0; - } else { + else unpackPhase=2; - } break; case 2: @@ -196,6 +198,7 @@ void TLV8::unpack(uint8_t *buf, size_t bufSize){ break; } } + return(unpackPhase); } diff --git a/src/TLV8.h b/src/TLV8.h index af752e8..1ca1ea3 100644 --- a/src/TLV8.h +++ b/src/TLV8.h @@ -107,7 +107,7 @@ class TLV8 : public std::list> { void osprint(std::ostream& os, TLV8_it it1){osprint(os, it1, it1++);} void osprint(std::ostream& os){osprint(os, begin(), end());} - void unpack(uint8_t *buf, size_t bufSize); + int unpack(uint8_t *buf, size_t bufSize); void wipe(){std::list>().swap(*this);} diff --git a/src/src.ino b/src/src.ino index d6ce93f..aa236a0 100644 --- a/src/src.ino +++ b/src/src.ino @@ -122,8 +122,7 @@ struct HomeSpanTV : Service::Television { HomeSpanTV *hsTV=(HomeSpanTV *)arg; TLV8 orderTLV; - - Serial.printf("BEFORE:\n"); + hsTV->displayOrder->getTLV(orderTLV); orderTLV.print(); orderTLV.wipe();