moved tlv streaming into tlv class

This commit is contained in:
Gregg 2024-01-10 20:44:16 -06:00
parent 73f761adc9
commit fa5455d6f9
3 changed files with 62 additions and 51 deletions

View File

@ -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<pend);
return(os);
}
//////////////////////////////////////
std::ostream& operator<<(std::ostream& os, TLV8 &tlv){
for(auto it=tlv.begin();it!=tlv.end();it++)
os << (*it);
return(os);
}
//////////////////////////////////////
void HAPClient::init(){ void HAPClient::init(){
size_t len; // not used but required to read blobs from NVS size_t len; // not used but required to read blobs from NVS
@ -1261,7 +1234,7 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *
memset(x,'C',400); memset(x,'C',400);
tlv.add(52,256,x); tlv.add(52,256,x);
hapOut << tlv; tlv.osprint(hapOut);
hapOut << "</body></html>\n"; hapOut << "</body></html>\n";
hapOut.flush(); hapOut.flush();

View File

@ -27,6 +27,45 @@
#include "TLV8.h" #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>((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<uint8_t>(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<pend);
}
///////////////////////////////////// /////////////////////////////////////
TLV8_it TLV8::add(uint8_t tag, size_t len, const uint8_t* val){ TLV8_it TLV8::add(uint8_t tag, size_t len, const uint8_t* val){
@ -196,4 +235,12 @@ void TLV8::print(TLV8_it it1, TLV8_it it2){
} }
} }
///////////////////////////////////// //////////////////////////////////////
void TLV8::osprint(std::ostream& os, TLV8_it it1, TLV8_it it2){
for(auto it=it1;it!=it2;it++)
(*it).osprint(os);
}
//////////////////////////////////////

View File

@ -27,32 +27,19 @@
#pragma once #pragma once
#include "HomeSpan.h" #include <sstream>
#include <forward_list> #include <forward_list>
#include "HomeSpan.h"
struct tlv8_t { struct tlv8_t {
uint8_t tag; uint8_t tag;
size_t len; size_t len;
std::unique_ptr<uint8_t> val; std::unique_ptr<uint8_t> val;
tlv8_t(uint8_t tag, size_t len, const uint8_t* val) : tag{tag}, len{len} { tlv8_t(uint8_t tag, size_t len, const uint8_t* val);
if(len>0){ void update(size_t addLen, const uint8_t *addVal);
this->val=std::unique_ptr<uint8_t>((uint8_t *)HS_MALLOC(len)); void osprint(std::ostream& os);
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<uint8_t>(p);
if(addVal!=NULL)
memcpy(p+len,addVal,addLen);
len+=addLen;
}
}
operator uint8_t*() const{ operator uint8_t*() const{
return(val.get()); return(val.get());
@ -114,6 +101,10 @@ class TLV8 : public std::forward_list<tlv8_t, Mallocator<tlv8_t>> {
void print(TLV8_it it1){print(it1, it1++);} void print(TLV8_it it1){print(it1, it1++);}
void print(){print(begin(), end());} 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 unpack(uint8_t *buf, size_t bufSize);
void wipe(){std::forward_list<tlv8_t, Mallocator<tlv8_t>>().swap(*this);} void wipe(){std::forward_list<tlv8_t, Mallocator<tlv8_t>>().swap(*this);}