diff --git a/src/core/common/tlvs.cpp b/src/core/common/tlvs.cpp index 9f1717ed2..ccf7bb028 100644 --- a/src/core/common/tlvs.cpp +++ b/src/core/common/tlvs.cpp @@ -282,6 +282,8 @@ 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::AppendEmptyTlv(Message &aMessage, uint8_t aType) { return AppendTlv(aMessage, aType, nullptr, 0); } + Error Tlv::AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint16_t aLength) { Error error = kErrorNone; diff --git a/src/core/common/tlvs.hpp b/src/core/common/tlvs.hpp index 505b9d742..4b0690b07 100644 --- a/src/core/common/tlvs.hpp +++ b/src/core/common/tlvs.hpp @@ -537,6 +537,36 @@ public: return FindStringTlv(aMessage, StringTlvType::kType, StringTlvType::kMaxStringLength, aValue); } + /** + * Appends an empty TLV (no value) with a given type to a message. + * + * On success this method grows the message by the size of the TLV. + * + * @param[in] aMessage The message to append to. + * @param[in] aType The TLV type to append. + * + * @retval kErrorNone Successfully appended the TLV to the message. + * @retval kErrorNoBufs Insufficient available buffers to grow the message. + */ + static Error AppendEmptyTlv(Message &aMessage, uint8_t aType); + + /** + * Appends an empty TLV (no value) with a given type to a message. + * + * On success this method grows the message by the size of the TLV. + * + * @tparam TlvType The TLV type to append. + * + * @param[in] aMessage The message to append to. + * + * @retval kErrorNone Successfully appended the TLV to the message. + * @retval kErrorNoBufs Insufficient available buffers to grow the message. + */ + template static Error AppendEmpty(Message &aMessage) + { + return AppendEmptyTlv(aMessage, TlvType::kType); + } + /** * Appends a TLV with a given type and value to a message. * diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 458854323..114447b8b 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -223,10 +223,7 @@ Error Server::AppendChildTableAsChildTlvs(Message &aMessage) SuccessOrExit(error = childTlv.AppendTo(aMessage)); } - // Add empty TLV to indicate end of the list - - childTlv.InitAsEmpty(); - SuccessOrExit(error = childTlv.AppendTo(aMessage)); + error = Tlv::AppendEmpty(aMessage); exit: return error; @@ -246,10 +243,7 @@ Error Server::AppendRouterNeighborTlvs(Message &aMessage) } } - // Add empty TLV to indicate end of the list - - neighborTlv.InitAsEmpty(); - SuccessOrExit(error = neighborTlv.AppendTo(aMessage)); + error = Tlv::AppendEmpty(aMessage); exit: return error; @@ -258,18 +252,13 @@ exit: Error Server::AppendChildTableIp6AddressList(Message &aMessage) { Error error = kErrorNone; - Tlv tlv; for (const Child &child : Get().Iterate(Child::kInStateValid)) { SuccessOrExit(error = AppendChildIp6AddressListTlv(aMessage, child)); } - // Add empty TLV to indicate end of the list - - tlv.SetType(Tlv::kChildIp6AddressList); - tlv.SetLength(0); - SuccessOrExit(error = aMessage.Append(tlv)); + error = Tlv::AppendEmpty(aMessage); exit: return error; @@ -913,10 +902,7 @@ Error Server::AppendChildTableAsChildTlvs(Coap::Message *&aAnswer, AnswerInfo &a SuccessOrExit(error = CheckAnswerLength(aAnswer, aInfo)); } - // Add empty TLV to indicate end of the list - - childTlv.InitAsEmpty(); - SuccessOrExit(error = childTlv.AppendTo(*aAnswer)); + error = Tlv::AppendEmpty(*aAnswer); exit: return error; @@ -940,10 +926,7 @@ Error Server::AppendRouterNeighborTlvs(Coap::Message *&aAnswer, AnswerInfo &aInf SuccessOrExit(error = CheckAnswerLength(aAnswer, aInfo)); } - // Add empty TLV to indicate end of the list - - neighborTlv.InitAsEmpty(); - SuccessOrExit(error = neighborTlv.AppendTo(*aAnswer)); + error = Tlv::AppendEmpty(*aAnswer); exit: return error; @@ -952,7 +935,6 @@ exit: Error Server::AppendChildTableIp6AddressList(Coap::Message *&aAnswer, AnswerInfo &aInfo) { Error error = kErrorNone; - Tlv tlv; for (const Child &child : Get().Iterate(Child::kInStateValid)) { @@ -960,11 +942,7 @@ Error Server::AppendChildTableIp6AddressList(Coap::Message *&aAnswer, AnswerInfo SuccessOrExit(error = CheckAnswerLength(aAnswer, aInfo)); } - // Add empty TLV to indicate end of the list - - tlv.SetType(Tlv::kChildIp6AddressList); - tlv.SetLength(0); - SuccessOrExit(error = aAnswer->Append(tlv)); + error = Tlv::AppendEmpty(*aAnswer); exit: return error; diff --git a/src/core/thread/network_diagnostic_tlvs.hpp b/src/core/thread/network_diagnostic_tlvs.hpp index ec9bf780e..92c042f4e 100644 --- a/src/core/thread/network_diagnostic_tlvs.hpp +++ b/src/core/thread/network_diagnostic_tlvs.hpp @@ -701,15 +701,6 @@ public: */ void InitFrom(const Child &aChild); - /** - * Initializes the TLV as empty (zero length). - */ - void InitAsEmpty(void) - { - SetType(kChild); - SetLength(0); - } - /** * Returns the Flags field (`kFlags*` constants define bits in flags). * @@ -900,15 +891,6 @@ public: */ void InitFrom(const Router &aRouter); - /** - * Initializes the TLV as empty (zero length). - */ - void InitAsEmpty(void) - { - SetType(kRouterNeighbor); - SetLength(0); - } - /** * Returns the Flags field (`kFlags*` constants define bits in flags). * diff --git a/src/core/utils/history_tracker_server.cpp b/src/core/utils/history_tracker_server.cpp index 20a44a684..11bd6765e 100644 --- a/src/core/utils/history_tracker_server.cpp +++ b/src/core/utils/history_tracker_server.cpp @@ -325,22 +325,12 @@ Error Server::AppendNetworkInfo(Coap::Message *&aAnswer, AnswerInfo &aInfo, cons SuccessOrExit(error = CheckAnswerLength(aAnswer, aInfo)); } - SuccessOrExit(error = AppendEmptyTlv(*aAnswer, Tlv::kNetworkInfo)); + SuccessOrExit(error = Tlv::AppendEmpty(*aAnswer)); exit: return error; } -Error Server::AppendEmptyTlv(Coap::Message &aAnswer, Tlv::Type aTlvType) -{ - Tlv tlv; - - tlv.SetType(aTlvType); - tlv.SetLength(0); - - return aAnswer.Append(tlv); -} - } // namespace HistoryTracker } // namespace ot diff --git a/src/core/utils/history_tracker_server.hpp b/src/core/utils/history_tracker_server.hpp index b31f734a2..bd41c5722 100644 --- a/src/core/utils/history_tracker_server.hpp +++ b/src/core/utils/history_tracker_server.hpp @@ -90,7 +90,6 @@ private: void SendNextAnswer(Coap::Message &aAnswer, const Ip6::Address &aDestination); void PrepareMessageInfoForDest(const Ip6::Address &aDestination, Tmf::MessageInfo &aMessageInfo) const; Error AppendNetworkInfo(Coap::Message *&aAnswer, AnswerInfo &aInfo, const RequestTlv &aRequestTlv); - Error AppendEmptyTlv(Coap::Message &aAnswer, Tlv::Type aTlvType); static void HandleAnswerResponse(void *aContext, otMessage *aMessage,