moved tlv streaming into tlv class
This commit is contained in:
parent
73f761adc9
commit
fa5455d6f9
29
src/HAP.cpp
29
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<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(){
|
||||
|
||||
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);
|
||||
tlv.add(52,256,x);
|
||||
|
||||
hapOut << tlv;
|
||||
tlv.osprint(hapOut);
|
||||
|
||||
hapOut << "</body></html>\n";
|
||||
hapOut.flush();
|
||||
|
|
|
|||
49
src/TLV8.cpp
49
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>((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){
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
//////////////////////////////////////
|
||||
|
|
|
|||
29
src/TLV8.h
29
src/TLV8.h
|
|
@ -27,32 +27,19 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "HomeSpan.h"
|
||||
#include <sstream>
|
||||
#include <forward_list>
|
||||
|
||||
#include "HomeSpan.h"
|
||||
|
||||
struct tlv8_t {
|
||||
uint8_t tag;
|
||||
size_t len;
|
||||
std::unique_ptr<uint8_t> 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>((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<uint8_t>(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,6 +101,10 @@ class TLV8 : public std::forward_list<tlv8_t, Mallocator<tlv8_t>> {
|
|||
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<tlv8_t, Mallocator<tlv8_t>>().swap(*this);}
|
||||
|
|
|
|||
Loading…
Reference in New Issue