created getTLVGeneric() and used as base for getTLV() and getNewTLV()

avoids unnecessary duplication of code where the only difference is whether you need the value or the newValue of a Characteristic.
This commit is contained in:
Gregg 2024-03-31 21:25:22 -05:00
parent f137b0bee5
commit 1892a0a5a2
1 changed files with 13 additions and 10 deletions

View File

@ -693,9 +693,9 @@ class SpanCharacteristic{
return(olen); return(olen);
if(ret==MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) if(ret==MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL)
LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Destination buffer is too small (%d out of %d bytes needed)\n\n",hapName,len,olen); LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Destination buffer is too small (%d out of %d bytes needed)!\n\n",hapName,len,olen);
else if(ret==MBEDTLS_ERR_BASE64_INVALID_CHARACTER) else if(ret==MBEDTLS_ERR_BASE64_INVALID_CHARACTER)
LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Data is not in base-64 format\n\n",hapName); LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Data is not in base-64 format!\n\n",hapName);
return(olen); return(olen);
} }
@ -711,9 +711,9 @@ class SpanCharacteristic{
return(olen); return(olen);
if(ret==MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) if(ret==MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL)
LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Destination buffer is too small (%d out of %d bytes needed)\n\n",hapName,len,olen); LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Destination buffer is too small (%d out of %d bytes needed)!\n\n",hapName,len,olen);
else if(ret==MBEDTLS_ERR_BASE64_INVALID_CHARACTER) else if(ret==MBEDTLS_ERR_BASE64_INVALID_CHARACTER)
LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Data is not in base-64 format\n\n",hapName); LOG0("\n*** WARNING: Can't decode Characteristic::%s with getData(). Data is not in base-64 format!\n\n",hapName);
return(olen); return(olen);
} }
@ -735,7 +735,7 @@ class SpanCharacteristic{
setValFinish(notify); setValFinish(notify);
} }
size_t getTLV(TLV8 &tlv){ size_t getTLVGeneric(TLV8 &tlv, UVal &val){
if(format<FORMAT::TLV_ENC) if(format<FORMAT::TLV_ENC)
return(0); return(0);
@ -744,8 +744,8 @@ class SpanCharacteristic{
TempBuffer<uint8_t> tBuf(bufSize); // create fixed-size buffer to store decoded bytes TempBuffer<uint8_t> tBuf(bufSize); // create fixed-size buffer to store decoded bytes
tlv.wipe(); // clear TLV completely tlv.wipe(); // clear TLV completely
size_t nChars=strlen(value.STRING); // total characters to decode size_t nChars=strlen(val.STRING); // total characters to decode
uint8_t *p=(uint8_t *)value.STRING; // set pointer to beginning of value uint8_t *p=(uint8_t *)val.STRING; // set pointer to beginning of value
const size_t decodeSize=bufSize/3*4; // number of characters to decode in each pass const size_t decodeSize=bufSize/3*4; // number of characters to decode in each pass
int status=0; int status=0;
@ -755,7 +755,7 @@ class SpanCharacteristic{
int ret=mbedtls_base64_decode(tBuf,tBuf.len(),&olen,p,n); int ret=mbedtls_base64_decode(tBuf,tBuf.len(),&olen,p,n);
if(ret==MBEDTLS_ERR_BASE64_INVALID_CHARACTER){ if(ret==MBEDTLS_ERR_BASE64_INVALID_CHARACTER){
LOG0("\n*** WARNING: Can't decode Characteristic::%s with getTLV(). Data is not in base-64 format\n\n",hapName); LOG0("\n*** WARNING: Can't decode Characteristic::%s with getTLV(). Data is not in base-64 format!\n\n",hapName);
tlv.wipe(); tlv.wipe();
return(0); return(0);
} }
@ -764,13 +764,16 @@ class SpanCharacteristic{
nChars-=n; nChars-=n;
} }
if(status>0){ if(status>0){
LOG0("\n*** WARNING: Can't unpack Characteristic::%s with getTLV(). TLV record is incomplete or corrupted\n\n",hapName); LOG0("\n*** WARNING: Can't unpack Characteristic::%s with getTLV(). TLV record is incomplete or corrupted!\n\n",hapName);
tlv.wipe(); tlv.wipe();
return(0); return(0);
} }
return(tlv.pack_size()); return(tlv.pack_size());
} }
size_t getTLV(TLV8 &tlv){return(getTLVGeneric(tlv,value));}
size_t getNewTLV(TLV8 &tlv){return(getTLVGeneric(tlv,newValue));}
void setTLV(TLV8 &tlv, boolean notify=true){ void setTLV(TLV8 &tlv, boolean notify=true){
setValCheck(); setValCheck();