diff --git a/src/HAP.cpp b/src/HAP.cpp index 92a9eec..c53adfb 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -34,33 +34,6 @@ ////////////////////////////////////// -std::ostream& operator<<(std::ostream& os, tlv8_t &tlv){ - - uint8_t *p=tlv.val.get(); // starting pointer - uint8_t *pend=p+tlv.len; // ending pointer (may equal starting if len=0) - - do{ - uint8_t nBytes=(pend-p)>255?255:(pend-p); // max is 255 bytes per TLV record - os.write((char *)&tlv.tag,1); - os.write((char *)&nBytes,1); - os.write((char *)p,nBytes); - p+=nBytes; - } while(p\n"; hapOut.flush(); diff --git a/src/TLV8.cpp b/src/TLV8.cpp index e58d2ab..8fcd24e 100644 --- a/src/TLV8.cpp +++ b/src/TLV8.cpp @@ -27,6 +27,45 @@ #include "TLV8.h" +////////////////////////////////////// + +tlv8_t::tlv8_t(uint8_t tag, size_t len, const uint8_t* val) : tag{tag}, len{len} { + if(len>0){ + this->val=std::unique_ptr((uint8_t *)HS_MALLOC(len)); + if(val!=NULL) + memcpy((this->val).get(),val,len); + } +} + +////////////////////////////////////// + +void tlv8_t::update(size_t addLen, const uint8_t *addVal){ + if(addLen>0){ + uint8_t *p=val.release(); + p=(uint8_t *)HS_REALLOC(p,len+addLen); + val=std::unique_ptr(p); + if(addVal!=NULL) + memcpy(p+len,addVal,addLen); + len+=addLen; + } +} + +///////////////////////////////////// + +void tlv8_t::osprint(std::ostream& os){ + + uint8_t *p=val.get(); // starting pointer + uint8_t *pend=p+len; // ending pointer (may equal starting if len=0) + + do{ + uint8_t nBytes=(pend-p)>255?255:(pend-p); // max is 255 bytes per TLV record + os.write((char *)&tag,1); + os.write((char *)&nBytes,1); + os.write((char *)p,nBytes); + p+=nBytes; + } while(p #include +#include "HomeSpan.h" + struct tlv8_t { uint8_t tag; size_t len; std::unique_ptr val; - tlv8_t(uint8_t tag, size_t len, const uint8_t* val) : tag{tag}, len{len} { - if(len>0){ - this->val=std::unique_ptr((uint8_t *)HS_MALLOC(len)); - if(val!=NULL) - memcpy((this->val).get(),val,len); - } - } - - void update(size_t addLen, const uint8_t *addVal){ - if(addLen>0){ - uint8_t *p=val.release(); - p=(uint8_t *)HS_REALLOC(p,len+addLen); - val=std::unique_ptr(p); - if(addVal!=NULL) - memcpy(p+len,addVal,addLen); - len+=addLen; - } - } + tlv8_t(uint8_t tag, size_t len, const uint8_t* val); + void update(size_t addLen, const uint8_t *addVal); + void osprint(std::ostream& os); operator uint8_t*() const{ return(val.get()); - } - + } + }; ///////////////////////////////////// @@ -114,8 +101,12 @@ class TLV8 : public std::forward_list> { void print(TLV8_it it1){print(it1, it1++);} void print(){print(begin(), end());} + void osprint(std::ostream& os, TLV8_it it1, TLV8_it it2); + 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); void wipe(){std::forward_list>().swap(*this);} - + };