Skip to content

Commit

Permalink
[dataset] move ApplyConfiguration() to DatasetManager (openthread…
Browse files Browse the repository at this point in the history
…#10280)

This commit moves the implementation of `ApplyConfiguration()` from
the `Dataset` class to `DatasetManager`. This aligns better with the
responsibility of the `DatasetManager` class (managing the active and
pending dataset and being an `InstanceLocator`), keeping the
`Dataset` class focused on providing helper functions related to TLVs
in the Dataset: finding TLVs, reading TLV values, writing/updating
TLVs, etc.
  • Loading branch information
abtink authored May 21, 2024
1 parent b01b262 commit 8b04e9c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 80 deletions.
66 changes: 0 additions & 66 deletions src/core/meshcop/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,72 +585,6 @@ void Dataset::RemoveTlv(Tlv *aTlv)
}
}

Error Dataset::ApplyConfiguration(Instance &aInstance) const
{
Mac::Mac &mac = aInstance.Get<Mac::Mac>();
KeyManager &keyManager = aInstance.Get<KeyManager>();
Error error = kErrorNone;

SuccessOrExit(error = ValidateTlvs());

for (const Tlv *cur = GetTlvsStart(); cur < GetTlvsEnd(); cur = cur->GetNext())
{
switch (cur->GetType())
{
case Tlv::kChannel:
{
uint8_t channel = static_cast<uint8_t>(cur->ReadValueAs<ChannelTlv>().GetChannel());

error = mac.SetPanChannel(channel);

if (error != kErrorNone)
{
LogWarn("ApplyConfiguration() Failed to set channel to %d (%s)", channel, ErrorToString(error));
ExitNow();
}

break;
}

case Tlv::kPanId:
mac.SetPanId(cur->ReadValueAs<PanIdTlv>());
break;

case Tlv::kExtendedPanId:
aInstance.Get<ExtendedPanIdManager>().SetExtPanId(cur->ReadValueAs<ExtendedPanIdTlv>());
break;

case Tlv::kNetworkName:
IgnoreError(aInstance.Get<NetworkNameManager>().SetNetworkName(As<NetworkNameTlv>(cur)->GetNetworkName()));
break;

case Tlv::kNetworkKey:
keyManager.SetNetworkKey(cur->ReadValueAs<NetworkKeyTlv>());
break;

#if OPENTHREAD_FTD
case Tlv::kPskc:
keyManager.SetPskc(cur->ReadValueAs<PskcTlv>());
break;
#endif

case Tlv::kMeshLocalPrefix:
aInstance.Get<Mle::MleRouter>().SetMeshLocalPrefix(cur->ReadValueAs<MeshLocalPrefixTlv>());
break;

case Tlv::kSecurityPolicy:
keyManager.SetSecurityPolicy(As<SecurityPolicyTlv>(cur)->GetSecurityPolicy());
break;

default:
break;
}
}

exit:
return error;
}

const char *Dataset::TypeToString(Type aType) { return (aType == kActive) ? "Active" : "Pending"; }

#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
Expand Down
11 changes: 0 additions & 11 deletions src/core/meshcop/dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,17 +636,6 @@ class Dataset
*/
Error SetFrom(const Message &aMessage, uint16_t aOffset, uint16_t aLength);

/**
* Applies the Active or Pending Dataset to the Thread interface.
*
* @param[in] aInstance A reference to the OpenThread instance.
*
* @retval kErrorNone Successfully applied configuration.
* @retval kErrorParse The dataset has at least one TLV with invalid format.
*
*/
Error ApplyConfiguration(Instance &aInstance) const;

/**
* Returns a pointer to the start of Dataset TLVs sequence.
*
Expand Down
70 changes: 67 additions & 3 deletions src/core/meshcop/dataset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Error DatasetManager::Restore(void)

if (IsActiveDataset())
{
IgnoreError(dataset.ApplyConfiguration(GetInstance()));
IgnoreError(ApplyConfiguration(dataset));
}

SignalDatasetChange();
Expand Down Expand Up @@ -172,7 +172,71 @@ Error DatasetManager::ApplyConfiguration(void) const
Dataset dataset;

SuccessOrExit(error = Read(dataset));
SuccessOrExit(error = dataset.ApplyConfiguration(GetInstance()));
error = ApplyConfiguration(dataset);

exit:
return error;
}

Error DatasetManager::ApplyConfiguration(const Dataset &aDataset) const
{
Error error = kErrorNone;

SuccessOrExit(error = aDataset.ValidateTlvs());

for (const Tlv *cur = aDataset.GetTlvsStart(); cur < aDataset.GetTlvsEnd(); cur = cur->GetNext())
{
switch (cur->GetType())
{
case Tlv::kChannel:
{
uint8_t channel = static_cast<uint8_t>(cur->ReadValueAs<ChannelTlv>().GetChannel());

error = Get<Mac::Mac>().SetPanChannel(channel);

if (error != kErrorNone)
{
LogWarn("ApplyConfiguration() Failed to set channel to %d (%s)", channel, ErrorToString(error));
ExitNow();
}

break;
}

case Tlv::kPanId:
Get<Mac::Mac>().SetPanId(cur->ReadValueAs<PanIdTlv>());
break;

case Tlv::kExtendedPanId:
Get<ExtendedPanIdManager>().SetExtPanId(cur->ReadValueAs<ExtendedPanIdTlv>());
break;

case Tlv::kNetworkName:
IgnoreError(Get<NetworkNameManager>().SetNetworkName(As<NetworkNameTlv>(cur)->GetNetworkName()));
break;

case Tlv::kNetworkKey:
Get<KeyManager>().SetNetworkKey(cur->ReadValueAs<NetworkKeyTlv>());
break;

#if OPENTHREAD_FTD
case Tlv::kPskc:
Get<KeyManager>().SetPskc(cur->ReadValueAs<PskcTlv>());
break;
#endif

case Tlv::kMeshLocalPrefix:
Get<Mle::MleRouter>().SetMeshLocalPrefix(cur->ReadValueAs<MeshLocalPrefixTlv>());
break;

case Tlv::kSecurityPolicy:
Get<KeyManager>().SetSecurityPolicy(As<SecurityPolicyTlv>(cur)->GetSecurityPolicy());
break;

default:
break;
}
}

exit:
return error;
Expand Down Expand Up @@ -237,7 +301,7 @@ Error DatasetManager::Save(const Dataset &aDataset, bool aAllowOlderTimestamp)

if (IsActiveDataset())
{
SuccessOrExit(error = aDataset.ApplyConfiguration(GetInstance()));
SuccessOrExit(error = ApplyConfiguration(aDataset));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/core/meshcop/dataset_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class DatasetManager : public InstanceLocator
bool IsActiveDataset(void) const { return (mType == Dataset::kActive); }
bool IsPendingDataset(void) const { return (mType == Dataset::kPending); }
void Clear(void);
Error ApplyConfiguration(const Dataset &aDataset) const;
void HandleGet(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) const;
void HandleTimer(void);
Error Save(const Dataset &aDataset, bool aAllowOlderTimestamp);
Expand Down

0 comments on commit 8b04e9c

Please sign in to comment.