diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 5dadd1189..8a717551d 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -80,16 +80,28 @@ Error DatasetManager::Restore(void) Error error; Dataset dataset; + // If `Read()` fails, `dataset` will remain empty. We still call + // `Restore(dataset)` to stop timer and clear the timestamp + // flags. + + error = Read(dataset); + Restore(dataset); + + return error; +} + +void DatasetManager::Restore(const Dataset &aDataset) +{ mTimer.Stop(); mNetworkTimestampValid = false; mLocalTimestampValid = false; - SuccessOrExit(error = Read(dataset)); + VerifyOrExit(aDataset.GetLength() != 0); mLocalSaved = true; - if (dataset.ReadTimestamp(mType, mLocalTimestamp) == kErrorNone) + if (aDataset.ReadTimestamp(mType, mLocalTimestamp) == kErrorNone) { mLocalTimestampValid = true; mNetworkTimestampValid = true; @@ -98,13 +110,13 @@ Error DatasetManager::Restore(void) if (IsActiveDataset()) { - IgnoreError(ApplyConfiguration(dataset)); + IgnoreError(ApplyConfiguration(aDataset)); } SignalDatasetChange(); exit: - return error; + return; } Error DatasetManager::Read(Dataset &aDataset) const @@ -266,8 +278,6 @@ void DatasetManager::Clear(void) SignalDatasetChange(); } -void DatasetManager::HandleDetach(void) { IgnoreError(Restore()); } - Error DatasetManager::Save(const Timestamp &aTimestamp, const Message &aMessage, uint16_t aOffset, uint16_t aLength) { Error error = kErrorNone; @@ -366,7 +376,7 @@ void DatasetManager::SaveLocal(const Dataset &aDataset) switch (Get().GetRole()) { case Mle::kRoleDisabled: - IgnoreError(Restore()); + Restore(aDataset); break; case Mle::kRoleChild: @@ -378,7 +388,7 @@ void DatasetManager::SaveLocal(const Dataset &aDataset) break; case Mle::kRoleLeader: - IgnoreError(Restore()); + Restore(aDataset); Get().IncrementVersionAndStableVersion(); break; #endif diff --git a/src/core/meshcop/dataset_manager.hpp b/src/core/meshcop/dataset_manager.hpp index 49cd452d5..d641ef0c9 100644 --- a/src/core/meshcop/dataset_manager.hpp +++ b/src/core/meshcop/dataset_manager.hpp @@ -199,14 +199,6 @@ public: */ Error ApplyConfiguration(void) const; - /** - * Updates the Operational Dataset when detaching from the network. - * - * On detach, the Operational Dataset is restored from non-volatile memory. - * - */ - void HandleDetach(void); - /** * Sends a MGMT_SET request to the Leader. * @@ -305,6 +297,7 @@ private: bool IsActiveDataset(void) const { return (mType == Dataset::kActive); } bool IsPendingDataset(void) const { return (mType == Dataset::kPending); } + void Restore(const Dataset &aDataset); Error ApplyConfiguration(const Dataset &aDataset) const; void HandleGet(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) const; void HandleTimer(void); diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 2572532ac..1e9af4565 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -383,7 +383,7 @@ Error ActiveDatasetManager::GenerateLocal(void) } LocalSave(dataset); - IgnoreError(Restore()); + Restore(dataset); LogInfo("Generated local dataset"); diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 523db179d..b72db8fff 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -236,8 +236,8 @@ void Mle::Stop(StopMode aMode) { if (aMode == kUpdateNetworkDatasets) { - Get().HandleDetach(); - Get().HandleDetach(); + IgnoreError(Get().Restore()); + IgnoreError(Get().Restore()); } VerifyOrExit(!IsDisabled()); @@ -530,7 +530,7 @@ Error Mle::BecomeDetached(void) // Not in reattach stage after reset if (mReattachState == kReattachStop) { - Get().HandleDetach(); + IgnoreError(Get().Restore()); } #if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE