From 1cfe6b4d0cfc390e003adf4257a940489538bcd5 Mon Sep 17 00:00:00 2001 From: Przemyslaw Bida Date: Thu, 25 Jul 2024 23:55:37 +0200 Subject: [PATCH] [tlv] Change method AppendTlv to accept uint16_t. Change `AppendTlv` to accept `uint16_t` and in case of TLV's longer than 254 uses ExtendedTlv. --- src/core/common/tlvs.cpp | 24 ++++++++++++++++-------- src/core/common/tlvs.hpp | 5 ++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/core/common/tlvs.cpp b/src/core/common/tlvs.cpp index 6f02bc4050f0..a91db29fefef 100644 --- a/src/core/common/tlvs.cpp +++ b/src/core/common/tlvs.cpp @@ -269,16 +269,24 @@ template Error Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, uin template Error Tlv::AppendUintTlv(Message &aMessage, uint8_t aType, uint16_t aValue); template Error Tlv::AppendUintTlv(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(aLength)); + SuccessOrExit(error = aMessage.Append(tlv)); + } VerifyOrExit(aLength > 0); error = aMessage.AppendBytes(aValue, aLength); diff --git a/src/core/common/tlvs.hpp b/src/core/common/tlvs.hpp index f89d0d643849..54e555720b2c 100644 --- a/src/core/common/tlvs.hpp +++ b/src/core/common/tlvs.hpp @@ -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. @@ -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.