diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 1884cf1..e7a2475 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -1874,7 +1874,7 @@ void SpanCharacteristic::uvSet(UVal &u, const char *val){ /////////////////////////////// -void SpanCharacteristic::uvSet(UVal &u, TLV8 &tlv){ +void SpanCharacteristic::uvSet(UVal &u, const TLV8 &tlv){ 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 @@ -1995,7 +1995,7 @@ return(tlv.pack_size()); /////////////////////////////// -void SpanCharacteristic::setTLV(TLV8 &tlv, boolean notify){ +void SpanCharacteristic::setTLV(const TLV8 &tlv, boolean notify){ setValCheck(); uvSet(value,tlv); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 9a2ccf1..b363d42 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -535,7 +535,7 @@ class SpanCharacteristic{ void uvSet(UVal &dest, UVal &src); // copies UVal src into UVal dest void uvSet(UVal &u, const char *val); // copies string val into UVal u - void uvSet(UVal &u, TLV8 &tlv); // copies TLV8 val into UVal u (after transforming to a char *) + void uvSet(UVal &u, const TLV8 &tlv); // copies TLV8 val into UVal u (after transforming to a char *) template void uvSet(UVal &u, T val){ // copies numeric val into UVal u switch(format){ @@ -658,7 +658,7 @@ class SpanCharacteristic{ void setString(const char *val, boolean notify=true); // sets the value and newValue for string-based Characteristic void setData(uint8_t *data, size_t len, boolean notify=true); // sets the value and newValue for data-based Characteristic - void setTLV(TLV8 &tlv, boolean notify=true); // sets the value and newValue for tlv8-based Characteristic + void setTLV(const TLV8 &tlv, boolean notify=true); // sets the value and newValue for tlv8-based Characteristic template void setVal(T val, boolean notify=true){ // sets the value and newValue for numeric-based Characteristics diff --git a/src/Span.h b/src/Span.h index 3f36809..e43a88a 100644 --- a/src/Span.h +++ b/src/Span.h @@ -513,7 +513,7 @@ namespace Characteristic { CREATE_CHAR(double,CurrentRelativeHumidity,0,0,100); // current humidity measured as a percentage CREATE_CHAR(double,CurrentTemperature,0,0,100); // current temperature measured in Celsius CREATE_CHAR(int,CurrentTiltAngle,0,-90,90); // current angle (in degrees) of slats from fully up or left (-90) to fully open (0) to fully down or right (90) - CREATE_CHAR(TLV8 &,DisplayOrder,TLV8::NULL_TLV,TLV8::NULL_TLV,TLV8::NULL_TLV); // specifies the order in which the TV inputs are displayed for selection in the Home App + CREATE_CHAR(const TLV8 &,DisplayOrder,TLV8::NULL_TLV,TLV8::NULL_TLV,TLV8::NULL_TLV); // specifies the order in which the TV inputs are displayed for selection in the Home App CREATE_CHAR(double,FilterLifeLevel,100,0,100); // measured as a percentage of remaining life CREATE_CHAR(uint8_t,FilterChangeIndication,0,0,1,NO_CHANGE_NEEDED,CHANGE_NEEDED); // indicates state of filter CREATE_CHAR(const char *,FirmwareRevision,"1.0.0",NULL,NULL); // must be in form x[.y[.z]] - informational only diff --git a/src/TLV8.cpp b/src/TLV8.cpp index 8bb4267..4b161da 100644 --- a/src/TLV8.cpp +++ b/src/TLV8.cpp @@ -52,7 +52,7 @@ void tlv8_t::update(size_t addLen, const uint8_t *addVal){ ///////////////////////////////////// -void tlv8_t::osprint(std::ostream& os){ +void tlv8_t::osprint(std::ostream& os) const { uint8_t *p=val.get(); // starting pointer uint8_t *pend=p+len; // ending pointer (may equal starting if len=0) @@ -104,7 +104,7 @@ TLV8_it TLV8::add(uint8_t tag, uint64_t val){ ///////////////////////////////////// -TLV8_it TLV8::find(uint8_t tag, TLV8_it it1, TLV8_it it2){ +TLV8_itc TLV8::find(uint8_t tag, TLV8_itc it1, TLV8_itc it2) const { auto it=it1; while(it!=it2 && it->getTag()!=tag) @@ -114,7 +114,7 @@ TLV8_it TLV8::find(uint8_t tag, TLV8_it it1, TLV8_it it2){ ///////////////////////////////////// -size_t TLV8::pack_size(TLV8_it it1, TLV8_it it2){ +size_t TLV8::pack_size(TLV8_itc it1, TLV8_itc it2) const { size_t nBytes=0; @@ -130,7 +130,7 @@ size_t TLV8::pack_size(TLV8_it it1, TLV8_it it2){ ///////////////////////////////////// -size_t TLV8::pack(uint8_t *buf, size_t bufSize){ +size_t TLV8::pack(uint8_t *buf, size_t bufSize) const { size_t nBytes=0; @@ -237,7 +237,7 @@ int TLV8::unpack(TLV8_it it){ ///////////////////////////////////// -const char *TLV8::getName(uint8_t tag){ +const char *TLV8::getName(uint8_t tag) const { if(names==NULL) return(NULL); @@ -252,7 +252,7 @@ const char *TLV8::getName(uint8_t tag){ ///////////////////////////////////// -void TLV8::print(TLV8_it it1, TLV8_it it2){ +void TLV8::print(TLV8_itc it1, TLV8_itc it2) const { while(it1!=it2){ const char *name=getName(it1->getTag()); @@ -276,7 +276,7 @@ void TLV8::print(TLV8_it it1, TLV8_it it2){ ////////////////////////////////////// -void TLV8::printAll_r(String label){ +void TLV8::printAll_r(String label) const{ for(auto it=begin();it!=end();it++){ Serial.printf("%s",label.c_str()); @@ -290,7 +290,7 @@ void TLV8::printAll_r(String label){ ////////////////////////////////////// -void TLV8::osprint(std::ostream& os, TLV8_it it1, TLV8_it it2){ +void TLV8::osprint(std::ostream& os, TLV8_itc it1, TLV8_itc it2) const { for(auto it=it1;it!=it2;it++) (*it).osprint(os); diff --git a/src/TLV8.h b/src/TLV8.h index 50f73e1..a31f5ad 100644 --- a/src/TLV8.h +++ b/src/TLV8.h @@ -46,29 +46,29 @@ class tlv8_t { 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); + void osprint(std::ostream& os) const; - operator uint8_t*() const{ + operator uint8_t*() const { return(val.get()); } - uint8_t & operator[](int index){ + uint8_t & operator[](int index) const { return(val.get()[index]); } - uint8_t *get(){ + uint8_t *get() const { return(val.get()); } - size_t getLen(){ + size_t getLen() const { return(len); } - uint8_t getTag(){ + uint8_t getTag() const { return(tag); } - template T getVal(){ + template T getVal() const { T iVal=0; for(int i=0;i(val.get()[i])<<(i*8); @@ -80,18 +80,19 @@ class tlv8_t { ///////////////////////////////////// typedef std::list>::iterator TLV8_it; +typedef std::list>::const_iterator TLV8_itc; typedef struct { const uint8_t tag; const char *name; } TLV8_names; ///////////////////////////////////// class TLV8 : public std::list> { - TLV8_it currentPackIt; - TLV8_it endPackIt; - uint8_t *currentPackBuf; - uint8_t *endPackBuf; - int currentPackPhase; - size_t currentPackLen; + TLV8_itc mutable currentPackIt; + TLV8_itc mutable endPackIt; + uint8_t mutable *currentPackBuf; + uint8_t mutable *endPackBuf; + int mutable currentPackPhase; + size_t mutable currentPackLen; uint8_t unpackTag; size_t unpackBytes; @@ -100,12 +101,10 @@ class TLV8 : public std::list> { const TLV8_names *names=NULL; int nNames=0; - void printAll_r(String label); + void printAll_r(String label) const; public: - static TLV8 NULL_TLV; - TLV8(){}; TLV8(const TLV8_names *names, int nNames) : names{names}, nNames{nNames} {}; @@ -115,36 +114,37 @@ class TLV8 : public std::list> { TLV8_it add(uint8_t tag){return(add(tag, 0, NULL));} TLV8_it add(uint8_t tag, const char *val){return(add(tag, strlen(val), reinterpret_cast(val)));} - TLV8_it find(uint8_t tag, TLV8_it it1, TLV8_it it2); - TLV8_it find(uint8_t tag, TLV8_it it1){return(find(tag, it1, end()));} - TLV8_it find(uint8_t tag){return(find(tag, begin(), end()));} + TLV8_itc find(uint8_t tag, TLV8_itc it1, TLV8_itc it2) const; + TLV8_itc find(uint8_t tag, TLV8_itc it1) const {return(find(tag, it1, end()));} + TLV8_itc find(uint8_t tag) const {return(find(tag, begin(), end()));} - int len(TLV8_it it){return(it==end()?-1:(*it).getLen());} + int len(TLV8_itc it) const {return(it==end()?-1:(*it).getLen());} - size_t pack_size(TLV8_it it1, TLV8_it it2); - size_t pack_size(){return(pack_size(begin(), end()));} + size_t pack_size(TLV8_itc it1, TLV8_itc it2) const; + size_t pack_size() const {return(pack_size(begin(), end()));} - void pack_init(TLV8_it it1, TLV8_it it2){currentPackIt=it1; endPackIt=it2; currentPackPhase=0;} - void pack_init(TLV8_it it1){pack_init(it1, std::next(it1));} - void pack_init(){pack_init(begin(),end());} + void pack_init(TLV8_itc it1, TLV8_itc it2) const {currentPackIt=it1; endPackIt=it2; currentPackPhase=0;} + void pack_init(TLV8_itc it1) const {pack_init(it1, std::next(it1));} + void pack_init() const {pack_init(begin(),end());} - size_t pack(uint8_t *buf, size_t bufSize); - size_t pack(uint8_t *buf){pack_init(); return(pack(buf,pack_size()));} + size_t pack(uint8_t *buf, size_t bufSize) const; + size_t pack(uint8_t *buf) const {pack_init(); return(pack(buf,pack_size()));} - const char *getName(uint8_t tag); + const char *getName(uint8_t tag) const; - void print(TLV8_it it1, TLV8_it it2); - void print(TLV8_it it1){print(it1, std::next(it1));} - void print(){print(begin(), end());} - void printAll(){printAll_r("");} + void print(TLV8_itc it1, TLV8_itc it2) const; + void print(TLV8_itc it1) const {print(it1, std::next(it1));} + void print() const {print(begin(), end());} + void printAll() const {printAll_r("");} - void osprint(std::ostream& os, TLV8_it it1, TLV8_it it2); - void osprint(std::ostream& os, TLV8_it it1){osprint(os, it1, std::next(it1));} - void osprint(std::ostream& os){osprint(os, begin(), end());} + void osprint(std::ostream& os, TLV8_itc it1, TLV8_itc it2) const; + void osprint(std::ostream& os, TLV8_itc it1) const {osprint(os, it1, std::next(it1));} + void osprint(std::ostream& os) const {osprint(os, begin(), end());} int unpack(uint8_t *buf, size_t bufSize); int unpack(TLV8_it it); void wipe(){std::list>().swap(*this);} - + + static TLV8 NULL_TLV; };