diff --git a/src/core/common/tlvs.hpp b/src/core/common/tlvs.hpp index fd2d6791f..1acc80694 100644 --- a/src/core/common/tlvs.hpp +++ b/src/core/common/tlvs.hpp @@ -525,6 +525,22 @@ public: return FindStringTlv(aMessage, StringTlvType::kType, StringTlvType::kMaxStringLength, aValue); } + /** + * Appends a TLV with a given type and value 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. + * @param[in] aValue A buffer containing the TLV value. + * @param[in] aLength The value length (in bytes). + * + * @retval kErrorNone Successfully appended the TLV to the message. + * @retval kErrorNoBufs Insufficient available buffers to grow the message. + * + */ + static Error AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength); + /** * Appends a TLV with a given type and value to a message. * @@ -687,7 +703,6 @@ private: }; static Error FindTlv(const Message &aMessage, uint8_t aType, void *aValue, uint16_t aLength); - static Error AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength); static Error ReadStringTlv(const Message &aMessage, uint16_t aOffset, uint8_t aMaxStringLength, char *aValue); static Error FindStringTlv(const Message &aMessage, uint8_t aType, uint8_t aMaxStringLength, char *aValue); static Error AppendStringTlv(Message &aMessage, uint8_t aType, uint8_t aMaxStringLength, const char *aValue); diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index b40e1527a..7f63cb215 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -450,43 +450,6 @@ exit: void Dataset::RemoveTlv(Tlv::Type aType) { RemoveTlv(FindTlv(aType)); } -Error Dataset::AppendMleDatasetTlv(Type aType, Message &aMessage) const -{ - Error error = kErrorNone; - Mle::Tlv tlv; - Mle::Tlv::Type type; - - VerifyOrExit(mLength > 0); - - type = (aType == kActive ? Mle::Tlv::kActiveDataset : Mle::Tlv::kPendingDataset); - - tlv.SetType(type); - tlv.SetLength(static_cast(mLength) - sizeof(Tlv) - sizeof(Timestamp)); - SuccessOrExit(error = aMessage.Append(tlv)); - - for (const Tlv *cur = GetTlvsStart(); cur < GetTlvsEnd(); cur = cur->GetNext()) - { - if (((aType == kActive) && (cur->GetType() == Tlv::kActiveTimestamp)) || - ((aType == kPending) && (cur->GetType() == Tlv::kPendingTimestamp))) - { - ; // skip Active or Pending Timestamp TLV - } - else if (cur->GetType() == Tlv::kDelayTimer) - { - uint32_t remainingDelay = DelayTimerTlv::CalculateRemainingDelay(*cur, mUpdateTime); - - SuccessOrExit(error = Tlv::Append(aMessage, remainingDelay)); - } - else - { - SuccessOrExit(error = cur->AppendTo(aMessage)); - } - } - -exit: - return error; -} - void Dataset::RemoveTlv(Tlv *aTlv) { if (aTlv != nullptr) diff --git a/src/core/meshcop/dataset.hpp b/src/core/meshcop/dataset.hpp index c81173e05..627bbb8fd 100644 --- a/src/core/meshcop/dataset.hpp +++ b/src/core/meshcop/dataset.hpp @@ -494,18 +494,6 @@ public: */ void SetFrom(const Tlvs &aTlvs); - /** - * Appends the MLE Dataset TLV but excluding MeshCoP Sub Timestamp TLV. - * - * @param[in] aType The type of the dataset, active or pending. - * @param[in] aMessage A message to append to. - * - * @retval kErrorNone Successfully append MLE Dataset TLV without MeshCoP Sub Timestamp TLV. - * @retval kErrorNoBufs Insufficient available buffers to append the message with MLE Dataset TLV. - * - */ - Error AppendMleDatasetTlv(Type aType, Message &aMessage) const; - /** * Applies the Active or Pending Dataset to the Thread interface. * diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index d0ac9c346..dfa9053c7 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -64,11 +64,18 @@ RegisterLogModule("DatasetManager"); Error DatasetManager::AppendMleDatasetTlv(Message &aMessage) const { - Dataset dataset; + Mle::Tlv::Type mleTlvType = IsActiveDataset() ? Mle::Tlv::kActiveDataset : Mle::Tlv::kPendingDataset; + Dataset dataset; IgnoreError(Read(dataset)); - return dataset.AppendMleDatasetTlv(GetType(), aMessage); + // Remove the Active or Pending Timestamp TLV from Dataset before + // appending to the message. The timestamp is appended as its own + // MLE TLV to the message. + + dataset.RemoveTlv(IsActiveDataset() ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp); + + return Tlv::AppendTlv(aMessage, mleTlvType, dataset.GetBytes(), static_cast(dataset.GetSize())); } Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)