Added "constantness" to all aspects of TLV8 code, including Characteristic Constructors

This commit is contained in:
Gregg 2024-06-25 21:59:23 -05:00
parent 2d25c044ae
commit f73c779913
5 changed files with 49 additions and 49 deletions

View File

@ -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 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 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(); setValCheck();
uvSet(value,tlv); uvSet(value,tlv);

View File

@ -535,7 +535,7 @@ class SpanCharacteristic{
void uvSet(UVal &dest, UVal &src); // copies UVal src into UVal dest 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, 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 <typename T> void uvSet(UVal &u, T val){ // copies numeric val into UVal u template <typename T> void uvSet(UVal &u, T val){ // copies numeric val into UVal u
switch(format){ 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 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 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 <typename T> void setVal(T val, boolean notify=true){ // sets the value and newValue for numeric-based Characteristics template <typename T> void setVal(T val, boolean notify=true){ // sets the value and newValue for numeric-based Characteristics

View File

@ -513,7 +513,7 @@ namespace Characteristic {
CREATE_CHAR(double,CurrentRelativeHumidity,0,0,100); // current humidity measured as a percentage 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(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(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(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(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 CREATE_CHAR(const char *,FirmwareRevision,"1.0.0",NULL,NULL); // must be in form x[.y[.z]] - informational only

View File

@ -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 *p=val.get(); // starting pointer
uint8_t *pend=p+len; // ending pointer (may equal starting if len=0) 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; auto it=it1;
while(it!=it2 && it->getTag()!=tag) 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; 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; 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) if(names==NULL)
return(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){ while(it1!=it2){
const char *name=getName(it1->getTag()); 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++){ for(auto it=begin();it!=end();it++){
Serial.printf("%s",label.c_str()); 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++) for(auto it=it1;it!=it2;it++)
(*it).osprint(os); (*it).osprint(os);

View File

@ -46,29 +46,29 @@ class tlv8_t {
tlv8_t(uint8_t tag, size_t len, const uint8_t* val); tlv8_t(uint8_t tag, size_t len, const uint8_t* val);
void update(size_t addLen, const uint8_t *addVal); 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()); return(val.get());
} }
uint8_t & operator[](int index){ uint8_t & operator[](int index) const {
return(val.get()[index]); return(val.get()[index]);
} }
uint8_t *get(){ uint8_t *get() const {
return(val.get()); return(val.get());
} }
size_t getLen(){ size_t getLen() const {
return(len); return(len);
} }
uint8_t getTag(){ uint8_t getTag() const {
return(tag); return(tag);
} }
template<class T=uint32_t> T getVal(){ template<class T=uint32_t> T getVal() const {
T iVal=0; T iVal=0;
for(int i=0;i<len;i++) for(int i=0;i<len;i++)
iVal|=static_cast<T>(val.get()[i])<<(i*8); iVal|=static_cast<T>(val.get()[i])<<(i*8);
@ -80,18 +80,19 @@ class tlv8_t {
///////////////////////////////////// /////////////////////////////////////
typedef std::list<tlv8_t, Mallocator<tlv8_t>>::iterator TLV8_it; typedef std::list<tlv8_t, Mallocator<tlv8_t>>::iterator TLV8_it;
typedef std::list<tlv8_t, Mallocator<tlv8_t>>::const_iterator TLV8_itc;
typedef struct { const uint8_t tag; const char *name; } TLV8_names; typedef struct { const uint8_t tag; const char *name; } TLV8_names;
///////////////////////////////////// /////////////////////////////////////
class TLV8 : public std::list<tlv8_t, Mallocator<tlv8_t>> { class TLV8 : public std::list<tlv8_t, Mallocator<tlv8_t>> {
TLV8_it currentPackIt; TLV8_itc mutable currentPackIt;
TLV8_it endPackIt; TLV8_itc mutable endPackIt;
uint8_t *currentPackBuf; uint8_t mutable *currentPackBuf;
uint8_t *endPackBuf; uint8_t mutable *endPackBuf;
int currentPackPhase; int mutable currentPackPhase;
size_t currentPackLen; size_t mutable currentPackLen;
uint8_t unpackTag; uint8_t unpackTag;
size_t unpackBytes; size_t unpackBytes;
@ -100,12 +101,10 @@ class TLV8 : public std::list<tlv8_t, Mallocator<tlv8_t>> {
const TLV8_names *names=NULL; const TLV8_names *names=NULL;
int nNames=0; int nNames=0;
void printAll_r(String label); void printAll_r(String label) const;
public: public:
static TLV8 NULL_TLV;
TLV8(){}; TLV8(){};
TLV8(const TLV8_names *names, int nNames) : names{names}, nNames{nNames} {}; TLV8(const TLV8_names *names, int nNames) : names{names}, nNames{nNames} {};
@ -115,36 +114,37 @@ class TLV8 : public std::list<tlv8_t, Mallocator<tlv8_t>> {
TLV8_it add(uint8_t tag){return(add(tag, 0, NULL));} 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<const uint8_t*>(val)));} TLV8_it add(uint8_t tag, const char *val){return(add(tag, strlen(val), reinterpret_cast<const uint8_t*>(val)));}
TLV8_it find(uint8_t tag, TLV8_it it1, TLV8_it it2); TLV8_itc find(uint8_t tag, TLV8_itc it1, TLV8_itc it2) const;
TLV8_it find(uint8_t tag, TLV8_it it1){return(find(tag, it1, end()));} TLV8_itc find(uint8_t tag, TLV8_itc it1) const {return(find(tag, it1, end()));}
TLV8_it find(uint8_t tag){return(find(tag, begin(), 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(TLV8_itc it1, TLV8_itc it2) const;
size_t pack_size(){return(pack_size(begin(), end()));} 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_itc it1, TLV8_itc it2) const {currentPackIt=it1; endPackIt=it2; currentPackPhase=0;}
void pack_init(TLV8_it it1){pack_init(it1, std::next(it1));} void pack_init(TLV8_itc it1) const {pack_init(it1, std::next(it1));}
void pack_init(){pack_init(begin(),end());} void pack_init() const {pack_init(begin(),end());}
size_t pack(uint8_t *buf, size_t bufSize); size_t pack(uint8_t *buf, size_t bufSize) const;
size_t pack(uint8_t *buf){pack_init(); return(pack(buf,pack_size()));} 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_itc it1, TLV8_itc it2) const;
void print(TLV8_it it1){print(it1, std::next(it1));} void print(TLV8_itc it1) const {print(it1, std::next(it1));}
void print(){print(begin(), end());} void print() const {print(begin(), end());}
void printAll(){printAll_r("");} void printAll() const {printAll_r("");}
void osprint(std::ostream& os, TLV8_it it1, TLV8_it it2); void osprint(std::ostream& os, TLV8_itc it1, TLV8_itc it2) const;
void osprint(std::ostream& os, TLV8_it it1){osprint(os, it1, std::next(it1));} void osprint(std::ostream& os, TLV8_itc it1) const {osprint(os, it1, std::next(it1));}
void osprint(std::ostream& os){osprint(os, begin(), end());} void osprint(std::ostream& os) const {osprint(os, begin(), end());}
int unpack(uint8_t *buf, size_t bufSize); int unpack(uint8_t *buf, size_t bufSize);
int unpack(TLV8_it it); int unpack(TLV8_it it);
void wipe(){std::list<tlv8_t, Mallocator<tlv8_t>>().swap(*this);} void wipe(){std::list<tlv8_t, Mallocator<tlv8_t>>().swap(*this);}
static TLV8 NULL_TLV;
}; };