diff --git a/libraries/BLEHomekit/src/HAPCharacteristic.cpp b/libraries/BLEHomekit/src/HAPCharacteristic.cpp index 1ffa998d..f1be014e 100644 --- a/libraries/BLEHomekit/src/HAPCharacteristic.cpp +++ b/libraries/BLEHomekit/src/HAPCharacteristic.cpp @@ -185,7 +185,8 @@ uint16_t HAPCharacteristic::writeHapValue(uint32_t num) HAPResponse_t* HAPCharacteristic::createHapResponse(uint8_t tid, uint8_t status, TLV8_t tlv_para[], uint8_t count) { // Determine body len, does not to include 2 byte length itself - uint8_t body_len = 0; + // FIXME multiple fragment len + uint16_t body_len = 0; for(uint8_t i=0; i body_data; for(uint8_t i=0; i < count; i++) { - memcpy(pdata, &tlv_para[i], 2); // type + len + pdata[0] = tlv_para[i].type; + pdata[1] = (uint8_t) tlv_para[i].len; // FIXME > 256 tlv + pdata += 2; memcpy(pdata, tlv_para[i].value, tlv_para[i].len); diff --git a/libraries/BLEHomekit/src/HAPProcedure.cpp b/libraries/BLEHomekit/src/HAPProcedure.cpp index 48d3c903..0de82e87 100644 --- a/libraries/BLEHomekit/src/HAPProcedure.cpp +++ b/libraries/BLEHomekit/src/HAPProcedure.cpp @@ -38,23 +38,58 @@ #include "HAPUuid.h" #include "HAPProcedure.h" -TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len) +/** + * Decode a TLV8 item + * @param pp_data payload pointer, got update to next item when done + * @param p_len len of payload, got updated to remaining item when done + * @param buf optional buffer, required if the decoded payload > 255 + * to assembly data into continuous buffer + * @param bufsize size of buffer (optional) + * + * @return TLV8 item + */ +TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len, void* buf, uint16_t bufsize) { TLV8_t tlv; varclr(&tlv); +// if ( buf && bufsize ) tlv.value = buf; + if ( (*p_len) ) { - uint8_t const* data = *pp_data; + tlv.type = (**pp_data); - tlv.type = data[0]; - tlv.len = data[1]; - tlv.value = data+2; + uint8_t fraqlen; + do { + uint8_t const* data = *pp_data; - // next item - (*p_len) -= tlv.len+2; - (*pp_data) += tlv.len+2; + // different item type + if ( tlv.type != data[0] ) break; + + fraqlen = data[1]; + + tlv.len += fraqlen; + + tlv.value = data+2; + + // next item + (*p_len) -= fraqlen+2; + (*pp_data) += fraqlen+2; + + }while( (fraqlen == 255) && (*p_len) ); } return tlv; } + +bool tlv8_encode_next(uint8_t** pp_buf, uint16_t* buflen, TLV8_t tlv) +{ + VERIFY( (*buflen) >= tlv.len + 2 ); + + uint8_t* buf = *pp_buf; + + buf[0] = tlv.type; + buf[1] = tlv.len; + + return true; +} diff --git a/libraries/BLEHomekit/src/HAPProcedure.h b/libraries/BLEHomekit/src/HAPProcedure.h index b5e58fa9..796d3f6f 100644 --- a/libraries/BLEHomekit/src/HAPProcedure.h +++ b/libraries/BLEHomekit/src/HAPProcedure.h @@ -86,11 +86,11 @@ enum HAPPduType_t }; -typedef struct ATTR_PACKED +typedef struct { - uint8_t type; - uint8_t len; - const void* value; // need serialization, not reflect physical layout + uint8_t type; + uint16_t len; + const void* value; }TLV8_t; @@ -148,6 +148,7 @@ typedef struct ATTR_PACKED -TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len); +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); #endif /* HAPPROCEDURE_H_ */