Skip to content

Commit

Permalink
[tlv] Change method AppendTlv to accept uint16_t.
Browse files Browse the repository at this point in the history
Change `AppendTlv` to accept `uint16_t` and in case of TLV's
longer than 254 uses ExtendedTlv.
  • Loading branch information
canisLupus1313 committed Aug 14, 2024
1 parent 5edc367 commit 1cfe6b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/core/common/tlvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,24 @@ template Error Tlv::AppendUintTlv<uint8_t>(Message &aMessage, uint8_t aType, uin
template Error Tlv::AppendUintTlv<uint16_t>(Message &aMessage, uint8_t aType, uint16_t aValue);
template Error Tlv::AppendUintTlv<uint32_t>(Message &aMessage, uint8_t aType, uint32_t aValue);

Error Tlv::AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength)
Error Tlv::AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint16_t aLength)
{
Error error = kErrorNone;
Tlv tlv;

OT_ASSERT(aLength <= Tlv::kBaseTlvMaxLength);
Error error = kErrorNone;
ExtendedTlv extTlv;
Tlv tlv;

tlv.SetType(aType);
tlv.SetLength(aLength);
SuccessOrExit(error = aMessage.Append(tlv));
if (aLength > kBaseTlvMaxLength)
{
extTlv.SetType(aType);
extTlv.SetLength(aLength);
SuccessOrExit(error = aMessage.Append(extTlv));
}
else
{
tlv.SetType(aType);
tlv.SetLength(static_cast<uint8_t>(aLength));
SuccessOrExit(error = aMessage.Append(tlv));
}

VerifyOrExit(aLength > 0);
error = aMessage.AppendBytes(aValue, aLength);
Expand Down
5 changes: 4 additions & 1 deletion src/core/common/tlvs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ class Tlv
/**
* Appends a TLV with a given type and value to a message.
*
* If the TLV length is longer than maximum base TLV size defined by `kBaseTlvMaxLength` then
* appends extended TLV.
*
* On success this method grows the message by the size of the TLV.
*
* @param[in] aMessage The message to append to.
Expand All @@ -585,7 +588,7 @@ class Tlv
* @retval kErrorNoBufs Insufficient available buffers to grow the message.
*
*/
static Error AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength);
static Error AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint16_t aLength);

/**
* Appends a TLV with a given type and value to a message.
Expand Down

0 comments on commit 1cfe6b4

Please sign in to comment.