From 8b765906594dcd05bb18dbbf1d8af1ff88833079 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 13 Jan 2018 12:19:23 +0700 Subject: [PATCH] wrap up tlv8 decode/encode with item len > 255 --- libraries/BLEHomekit/src/HAPProcedure.cpp | 41 ++++++++++++++++++----- libraries/BLEHomekit/src/HAPProcedure.h | 2 +- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/libraries/BLEHomekit/src/HAPProcedure.cpp b/libraries/BLEHomekit/src/HAPProcedure.cpp index 0de82e87..39000adf 100644 --- a/libraries/BLEHomekit/src/HAPProcedure.cpp +++ b/libraries/BLEHomekit/src/HAPProcedure.cpp @@ -53,10 +53,10 @@ TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len, void* buf, uin TLV8_t tlv; varclr(&tlv); -// if ( buf && bufsize ) tlv.value = buf; - if ( (*p_len) ) { + if ( buf && bufsize ) tlv.value = buf; + tlv.type = (**pp_data); uint8_t fraqlen; @@ -70,7 +70,21 @@ TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len, void* buf, uin tlv.len += fraqlen; - tlv.value = data+2; + if ( buf ) + { + uint16_t cp = min16(bufsize, fraqlen); + + if ( cp ) + { + memcpy(buf, data+2, cp); + + buf += cp; + bufsize -= cp; + } + } else + { + tlv.value = data+2; + } // next item (*p_len) -= fraqlen+2; @@ -82,14 +96,25 @@ TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len, void* buf, uin return tlv; } -bool tlv8_encode_next(uint8_t** pp_buf, uint16_t* buflen, TLV8_t tlv) +bool tlv8_encode_next(uint8_t** pp_buf, uint16_t* p_buflen, TLV8_t tlv) { - VERIFY( (*buflen) >= tlv.len + 2 ); + while ( tlv.len ) + { + uint8_t* buf = *pp_buf; + uint8_t const fraqlen = (uint8_t) min16(tlv.len, 255); - uint8_t* buf = *pp_buf; + buf[0] = tlv.type; + buf[1] = fraqlen; - buf[0] = tlv.type; - buf[1] = tlv.len; + VERIFY( (*p_buflen) >= fraqlen + 2 ); + memcpy(buf+2, tlv.value, fraqlen); + + tlv.len -= fraqlen; + tlv.value += fraqlen; + + (*p_buflen) -= fraqlen+2; + (*pp_buf) += fraqlen+2; + } return true; } diff --git a/libraries/BLEHomekit/src/HAPProcedure.h b/libraries/BLEHomekit/src/HAPProcedure.h index 796d3f6f..0553a865 100644 --- a/libraries/BLEHomekit/src/HAPProcedure.h +++ b/libraries/BLEHomekit/src/HAPProcedure.h @@ -149,6 +149,6 @@ typedef struct ATTR_PACKED TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len, void* buf = NULL, uint16_t bufsize = 0); -bool tlv8_encode_next(uint8_t** pp_buf, uint16_t* buflen, TLV8_t tlv); +bool tlv8_encode_next(uint8_t** pp_buf, uint16_t* p_buflen, TLV8_t tlv); #endif /* HAPPROCEDURE_H_ */