update tlv decode to support > 255 item

This commit is contained in:
hathach committed 2018-01-13 11:59:54 +07:00
1 parent fd41ae543c
commit 6b67563028
3 files changed
+54 -15

No files matched your search

@@ -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 <count ; i++)
{
body_len += tlv_para[i].len + 2;
@@ -206,7 +207,9 @@ HAPResponse_t* HAPCharacteristic::createHapResponse(uint8_t tid, uint8_t status,
uint8_t* pdata = hap_resp->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);
+43 -8
View File
@@ -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;
}
+6 -5
View File
@@ -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_ */