From 73561328e620ac9be814a59e5aac04192d4bfa6b Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 14 Apr 2024 21:29:02 -0500 Subject: [PATCH] Changed TLV8:add() for *values* to ensure resulting size is always multiple of 2 Checked that this is correctly interpreted by HomeKit using adaptive light TLV. TLV code is now ready for documentation and then transfer to dev branch. --- src/TLV8.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TLV8.cpp b/src/TLV8.cpp index aae7686..6c629ad 100644 --- a/src/TLV8.cpp +++ b/src/TLV8.cpp @@ -93,8 +93,12 @@ TLV8_it TLV8::add(uint8_t tag, uint64_t val){ uint8_t *p=reinterpret_cast(&val); size_t nBytes=sizeof(uint64_t); - while(nBytes>1 && p[nBytes-1]==0) // TLV requires little endian without any trailing zero bytes (i.e. only use what is needed to fully represent the value) + while(nBytes>1 && p[nBytes-1]==0) // TLV requires little endian of size 1, 2, 4, or 8 bytes (include trailing zeros as needed) nBytes--; + if(nBytes==3) // need to include a trailing zero so that total bytes=4 + nBytes=4; + else if(nBytes>4) // need to include multiple trailing zeros so that total bytes=8 + nBytes=8; return(add(tag, nBytes, p)); }