diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index ec6e8907755..87fddbaf7b7 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -267,22 +267,6 @@ void DatasetManager::Clear(void) SignalDatasetChange(); } -Error DatasetManager::Save(const Timestamp &aTimestamp, const Message &aMessage, uint16_t aOffset, uint16_t aLength) -{ - Error error = kErrorNone; - Dataset dataset; - - SuccessOrExit(error = dataset.SetFrom(aMessage, aOffset, aLength)); - SuccessOrExit(error = dataset.ValidateTlvs()); - - SuccessOrExit(error = dataset.WriteTimestamp(mType, aTimestamp)); - - error = Save(dataset); - -exit: - return error; -} - Error DatasetManager::Save(const Dataset &aDataset, bool aAllowOlderTimestamp) { Error error = kErrorNone; diff --git a/src/core/meshcop/dataset_manager.hpp b/src/core/meshcop/dataset_manager.hpp index 87c9544e63f..edb63f5c0f2 100644 --- a/src/core/meshcop/dataset_manager.hpp +++ b/src/core/meshcop/dataset_manager.hpp @@ -162,23 +162,6 @@ class DatasetManager : public InstanceLocator */ Error Save(const Dataset &aDataset) { return Save(aDataset, /* aAllowOlderTimestamp */ false); } - /** - * Sets the Operational Dataset for the partition read from a given message. - * - * Also updates the non-volatile local version if the partition's Operational Dataset is newer. If Active - * Operational Dataset is changed, applies the configuration to to Thread interface. - * - * @param[in] aTimestamp The timestamp for the Operational Dataset. - * @param[in] aMessage The message to read from. - * @param[in] aOffset The offset where the Operational Dataset begins. - * @param[in] aLength The length of the Operational Dataset. - * - * @retval kErrorNone Successfully parsed the Dataset from the @p aMessage and saved it. - * @retval kErrorParse Could not parse the Dataset from @p aMessage. - * - */ - Error Save(const Timestamp &aTimestamp, const Message &aMessage, uint16_t aOffset, uint16_t aLength); - /** * Retrieves the channel mask from local dataset. * diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 516a4f9b6f1..838ff8ef286 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -2999,19 +2999,16 @@ Error Mle::HandleLeaderData(RxInfo &aRxInfo) { // We previously confirmed the message contains an // Active or a Pending Dataset TLV before setting the - // corresponding `saveDataset` flag, so we can safely - // `IgnoreError()` on `FindTlvValueOffset()`. + // corresponding `saveDataset` flag. if (saveActiveDataset) { - IgnoreError(Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kActiveDataset, offset, length)); - IgnoreError(Get().Save(activeTimestamp, aRxInfo.mMessage, offset, length)); + IgnoreError(aRxInfo.mMessage.ReadAndSaveActiveDataset(activeTimestamp)); } if (savePendingDataset) { - IgnoreError(Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kPendingDataset, offset, length)); - IgnoreError(Get().Save(pendingTimestamp, aRxInfo.mMessage, offset, length)); + IgnoreError(aRxInfo.mMessage.ReadAndSavePendingDataset(pendingTimestamp)); } } @@ -3313,8 +3310,6 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo) MeshCoP::Timestamp timestamp; uint16_t networkDataOffset; uint16_t networkDataLength; - uint16_t offset; - uint16_t length; SuccessOrExit(error = Tlv::Find(aRxInfo.mMessage, sourceAddress)); @@ -3335,11 +3330,9 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo) switch (Tlv::Find(aRxInfo.mMessage, timestamp)) { case kErrorNone: - if (Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kActiveDataset, offset, length) == kErrorNone) - { - SuccessOrExit(error = - Get().Save(timestamp, aRxInfo.mMessage, offset, length)); - } + error = aRxInfo.mMessage.ReadAndSaveActiveDataset(timestamp); + error = (error == kErrorNotFound) ? kErrorNone : error; + SuccessOrExit(error); break; case kErrorNotFound: @@ -3358,10 +3351,7 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo) switch (Tlv::Find(aRxInfo.mMessage, timestamp)) { case kErrorNone: - if (Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kPendingDataset, offset, length) == kErrorNone) - { - IgnoreError(Get().Save(timestamp, aRxInfo.mMessage, offset, length)); - } + IgnoreError(aRxInfo.mMessage.ReadAndSavePendingDataset(timestamp)); break; case kErrorNotFound: @@ -4907,13 +4897,13 @@ Error Mle::TxMessage::AppendActiveDatasetTlv(void) { return AppendDatasetTlv(Mes Error Mle::TxMessage::AppendPendingDatasetTlv(void) { return AppendDatasetTlv(MeshCoP::Dataset::kPending); } -Error Mle::TxMessage::AppendDatasetTlv(MeshCoP::Dataset::Type mDatasetType) +Error Mle::TxMessage::AppendDatasetTlv(MeshCoP::Dataset::Type aDatasetType) { Error error = kErrorNotFound; Tlv::Type tlvType; MeshCoP::Dataset dataset; - switch (mDatasetType) + switch (aDatasetType) { case MeshCoP::Dataset::kActive: error = Get().Read(dataset); @@ -4938,7 +4928,7 @@ Error Mle::TxMessage::AppendDatasetTlv(MeshCoP::Dataset::Type mDatasetType) // message. The Timestamp is appended as its own MLE TLV to the // message. - dataset.RemoveTimestamp(mDatasetType); + dataset.RemoveTimestamp(aDatasetType); error = Tlv::AppendTlv(*this, tlvType, dataset.GetBytes(), dataset.GetLength()); @@ -5074,6 +5064,45 @@ Error Mle::RxMessage::ReadLeaderDataTlv(LeaderData &aLeaderData) const return error; } +Error Mle::RxMessage::ReadAndSaveActiveDataset(const MeshCoP::Timestamp &aActiveTimestamp) const +{ + return ReadAndSaveDataset(MeshCoP::Dataset::kActive, aActiveTimestamp); +} + +Error Mle::RxMessage::ReadAndSavePendingDataset(const MeshCoP::Timestamp &aPendingTimestamp) const +{ + return ReadAndSaveDataset(MeshCoP::Dataset::kPending, aPendingTimestamp); +} + +Error Mle::RxMessage::ReadAndSaveDataset(MeshCoP::Dataset::Type aDatasetType, + const MeshCoP::Timestamp &aTimestamp) const +{ + Error error = kErrorNone; + Tlv::Type tlvType = (aDatasetType == MeshCoP::Dataset::kActive) ? Tlv::kActiveDataset : Tlv::kPendingDataset; + MeshCoP::Dataset dataset; + uint16_t offset; + uint16_t length; + + SuccessOrExit(error = Tlv::FindTlvValueOffset(*this, tlvType, offset, length)); + + SuccessOrExit(error = dataset.SetFrom(*this, offset, length)); + SuccessOrExit(error = dataset.ValidateTlvs()); + SuccessOrExit(error = dataset.WriteTimestamp(aDatasetType, aTimestamp)); + + switch (aDatasetType) + { + case MeshCoP::Dataset::kActive: + error = Get().Save(dataset); + break; + case MeshCoP::Dataset::kPending: + error = Get().Save(dataset); + break; + } + +exit: + return error; +} + Error Mle::RxMessage::ReadTlvRequestTlv(TlvList &aTlvList) const { Error error; diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 7633ffbd2a3..318648b5eec 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -1055,7 +1055,7 @@ class Mle : public InstanceLocator, private NonCopyable private: Error AppendCompressedAddressEntry(uint8_t aContextId, const Ip6::Address &aAddress); Error AppendAddressEntry(const Ip6::Address &aAddress); - Error AppendDatasetTlv(MeshCoP::Dataset::Type mDatasetType); + Error AppendDatasetTlv(MeshCoP::Dataset::Type aDatasetType); }; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1072,6 +1072,8 @@ class Mle : public InstanceLocator, private NonCopyable Error ReadFrameCounterTlvs(uint32_t &aLinkFrameCounter, uint32_t &aMleFrameCounter) const; Error ReadTlvRequestTlv(TlvList &aTlvList) const; Error ReadLeaderDataTlv(LeaderData &aLeaderData) const; + Error ReadAndSaveActiveDataset(const MeshCoP::Timestamp &aActiveTimestamp) const; + Error ReadAndSavePendingDataset(const MeshCoP::Timestamp &aPendingTimestamp) const; #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE Error ReadCslClockAccuracyTlv(Mac::CslAccuracy &aCslAccuracy) const; #endif @@ -1081,6 +1083,7 @@ class Mle : public InstanceLocator, private NonCopyable private: Error ReadChallengeOrResponse(uint8_t aTlvType, RxChallenge &aRxChallenge) const; + Error ReadAndSaveDataset(MeshCoP::Dataset::Type aDatasetType, const MeshCoP::Timestamp &aTimestamp) const; }; //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -