mirror of
https://github.com/espressif/openthread.git
synced 2026-08-06 10:47:46 +00:00
[dataset-manager] simplify Restore() (#10290)
This commit simplifies the `Restore()` methods: - A private overload `Restore(const Dataset&)` is added, taking the read `Dataset` as input to avoid duplicate reads. - The public `Restore(void)` is updated to use the new private `Restore(const Dataset&)`. - The `HandleDetach()` method is removed, and its use in the `Mle` class is replaced with `Restore()`.
This commit is contained in:
@@ -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<Mle::MleRouter>().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<NetworkData::Leader>().IncrementVersionAndStableVersion();
|
||||
break;
|
||||
#endif
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -383,7 +383,7 @@ Error ActiveDatasetManager::GenerateLocal(void)
|
||||
}
|
||||
|
||||
LocalSave(dataset);
|
||||
IgnoreError(Restore());
|
||||
Restore(dataset);
|
||||
|
||||
LogInfo("Generated local dataset");
|
||||
|
||||
|
||||
@@ -236,8 +236,8 @@ void Mle::Stop(StopMode aMode)
|
||||
{
|
||||
if (aMode == kUpdateNetworkDatasets)
|
||||
{
|
||||
Get<MeshCoP::ActiveDatasetManager>().HandleDetach();
|
||||
Get<MeshCoP::PendingDatasetManager>().HandleDetach();
|
||||
IgnoreError(Get<MeshCoP::ActiveDatasetManager>().Restore());
|
||||
IgnoreError(Get<MeshCoP::PendingDatasetManager>().Restore());
|
||||
}
|
||||
|
||||
VerifyOrExit(!IsDisabled());
|
||||
@@ -530,7 +530,7 @@ Error Mle::BecomeDetached(void)
|
||||
// Not in reattach stage after reset
|
||||
if (mReattachState == kReattachStop)
|
||||
{
|
||||
Get<MeshCoP::PendingDatasetManager>().HandleDetach();
|
||||
IgnoreError(Get<MeshCoP::PendingDatasetManager>().Restore());
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
|
||||
|
||||
Reference in New Issue
Block a user