Skip to content

Commit

Permalink
[netdata] simplify Leader class (openthread#9563)
Browse files Browse the repository at this point in the history
This commit updates the `NetworkData::Leader` sub-class model,
removing `LeaderBase` (which intended to provide common functions
shared between FTD and MTD) and instead adding all methods directly
in `Leader` class with all `FTD`-specific methods having conditional
`#if` check.
  • Loading branch information
abtink authored Oct 31, 2023
1 parent c6ffe1a commit 4a9fb06
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 473 deletions.
1 change: 0 additions & 1 deletion src/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,6 @@ openthread_core_files = [
"thread/network_data_leader.cpp",
"thread/network_data_leader.hpp",
"thread/network_data_leader_ftd.cpp",
"thread/network_data_leader_ftd.hpp",
"thread/network_data_local.cpp",
"thread/network_data_local.hpp",
"thread/network_data_notifier.cpp",
Expand Down
96 changes: 57 additions & 39 deletions src/core/thread/network_data_leader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,37 @@ namespace NetworkData {

RegisterLogModule("NetworkData");

void LeaderBase::Reset(void)
Leader::Leader(Instance &aInstance)
: MutableNetworkData(aInstance, mTlvBuffer, 0, sizeof(mTlvBuffer))
, mMaxLength(0)
#if OPENTHREAD_FTD
#if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL
, mIsClone(false)
#endif
, mWaitingForNetDataSync(false)
, mContextIds(aInstance)
, mTimer(aInstance)
#endif
{
Reset();
}

void Leader::Reset(void)
{
mVersion = Random::NonCrypto::GetUint8();
mStableVersion = Random::NonCrypto::GetUint8();
SetLength(0);
SignalNetDataChanged();

#if OPENTHREAD_FTD
mContextIds.Clear();
#endif
}

Error LeaderBase::GetServiceId(uint32_t aEnterpriseNumber,
const ServiceData &aServiceData,
bool aServerStable,
uint8_t &aServiceId) const
Error Leader::GetServiceId(uint32_t aEnterpriseNumber,
const ServiceData &aServiceData,
bool aServerStable,
uint8_t &aServiceId) const
{
Error error = kErrorNotFound;
Iterator iterator = kIteratorInit;
Expand All @@ -89,7 +108,7 @@ Error LeaderBase::GetServiceId(uint32_t aEnterpriseNumber,
return error;
}

Error LeaderBase::GetPreferredNat64Prefix(ExternalRouteConfig &aConfig) const
Error Leader::GetPreferredNat64Prefix(ExternalRouteConfig &aConfig) const
{
Error error = kErrorNotFound;
Iterator iterator = kIteratorInit;
Expand All @@ -113,7 +132,7 @@ Error LeaderBase::GetPreferredNat64Prefix(ExternalRouteConfig &aConfig) const
return error;
}

const PrefixTlv *LeaderBase::FindNextMatchingPrefixTlv(const Ip6::Address &aAddress, const PrefixTlv *aPrevTlv) const
const PrefixTlv *Leader::FindNextMatchingPrefixTlv(const Ip6::Address &aAddress, const PrefixTlv *aPrevTlv) const
{
// This method iterates over Prefix TLVs which match a given IPv6
// `aAddress`. If `aPrevTlv` is `nullptr` we start from the
Expand All @@ -135,7 +154,7 @@ const PrefixTlv *LeaderBase::FindNextMatchingPrefixTlv(const Ip6::Address &aAddr
return prefixTlv;
}

Error LeaderBase::GetContext(const Ip6::Address &aAddress, Lowpan::Context &aContext) const
Error Leader::GetContext(const Ip6::Address &aAddress, Lowpan::Context &aContext) const
{
const PrefixTlv *prefixTlv = nullptr;
const ContextTlv *contextTlv;
Expand Down Expand Up @@ -168,7 +187,7 @@ Error LeaderBase::GetContext(const Ip6::Address &aAddress, Lowpan::Context &aCon
return (aContext.mPrefix.GetLength() > 0) ? kErrorNone : kErrorNotFound;
}

Error LeaderBase::GetContext(uint8_t aContextId, Lowpan::Context &aContext) const
Error Leader::GetContext(uint8_t aContextId, Lowpan::Context &aContext) const
{
Error error = kErrorNotFound;
TlvIterator tlvIterator(GetTlvsStart(), GetTlvsEnd());
Expand Down Expand Up @@ -200,15 +219,15 @@ Error LeaderBase::GetContext(uint8_t aContextId, Lowpan::Context &aContext) cons
return error;
}

void LeaderBase::GetContextForMeshLocalPrefix(Lowpan::Context &aContext) const
void Leader::GetContextForMeshLocalPrefix(Lowpan::Context &aContext) const
{
aContext.mPrefix.Set(Get<Mle::MleRouter>().GetMeshLocalPrefix());
aContext.mContextId = Mle::kMeshLocalPrefixContextId;
aContext.mCompressFlag = true;
aContext.mIsValid = true;
}

bool LeaderBase::IsOnMesh(const Ip6::Address &aAddress) const
bool Leader::IsOnMesh(const Ip6::Address &aAddress) const
{
const PrefixTlv *prefixTlv = nullptr;
bool isOnMesh = false;
Expand Down Expand Up @@ -237,7 +256,7 @@ bool LeaderBase::IsOnMesh(const Ip6::Address &aAddress) const
return isOnMesh;
}

Error LeaderBase::RouteLookup(const Ip6::Address &aSource, const Ip6::Address &aDestination, uint16_t &aRloc16) const
Error Leader::RouteLookup(const Ip6::Address &aSource, const Ip6::Address &aDestination, uint16_t &aRloc16) const
{
Error error = kErrorNoRoute;
const PrefixTlv *prefixTlv = nullptr;
Expand All @@ -264,18 +283,17 @@ Error LeaderBase::RouteLookup(const Ip6::Address &aSource, const Ip6::Address &a
return error;
}

template <typename EntryType>
int LeaderBase::CompareRouteEntries(const EntryType &aFirst, const EntryType &aSecond) const
template <typename EntryType> int Leader::CompareRouteEntries(const EntryType &aFirst, const EntryType &aSecond) const
{
// `EntryType` can be `HasRouteEntry` or `BorderRouterEntry`.

return CompareRouteEntries(aFirst.GetPreference(), aFirst.GetRloc(), aSecond.GetPreference(), aSecond.GetRloc());
}

int LeaderBase::CompareRouteEntries(int8_t aFirstPreference,
uint16_t aFirstRloc,
int8_t aSecondPreference,
uint16_t aSecondRloc) const
int Leader::CompareRouteEntries(int8_t aFirstPreference,
uint16_t aFirstRloc,
int8_t aSecondPreference,
uint16_t aSecondRloc) const
{
// Performs three-way comparison between two BR entries.

Expand Down Expand Up @@ -312,7 +330,7 @@ int LeaderBase::CompareRouteEntries(int8_t aFirstPreference,
return result;
}

Error LeaderBase::ExternalRouteLookup(uint8_t aDomainId, const Ip6::Address &aDestination, uint16_t &aRloc16) const
Error Leader::ExternalRouteLookup(uint8_t aDomainId, const Ip6::Address &aDestination, uint16_t &aRloc16) const
{
Error error = kErrorNoRoute;
const PrefixTlv *prefixTlv = nullptr;
Expand Down Expand Up @@ -359,7 +377,7 @@ Error LeaderBase::ExternalRouteLookup(uint8_t aDomainId, const Ip6::Address &aDe
return error;
}

Error LeaderBase::DefaultRouteLookup(const PrefixTlv &aPrefix, uint16_t &aRloc16) const
Error Leader::DefaultRouteLookup(const PrefixTlv &aPrefix, uint16_t &aRloc16) const
{
Error error = kErrorNoRoute;
TlvIterator subTlvIterator(aPrefix);
Expand Down Expand Up @@ -392,12 +410,12 @@ Error LeaderBase::DefaultRouteLookup(const PrefixTlv &aPrefix, uint16_t &aRloc16
return error;
}

Error LeaderBase::SetNetworkData(uint8_t aVersion,
uint8_t aStableVersion,
Type aType,
const Message &aMessage,
uint16_t aOffset,
uint16_t aLength)
Error Leader::SetNetworkData(uint8_t aVersion,
uint8_t aStableVersion,
Type aType,
const Message &aMessage,
uint16_t aOffset,
uint16_t aLength)
{
Error error = kErrorNone;

Expand Down Expand Up @@ -428,12 +446,12 @@ Error LeaderBase::SetNetworkData(uint8_t aVersion,
return error;
}

const CommissioningDataTlv *LeaderBase::FindCommissioningData(void) const
const CommissioningDataTlv *Leader::FindCommissioningData(void) const
{
return NetworkDataTlv::Find<CommissioningDataTlv>(GetTlvsStart(), GetTlvsEnd());
}

const MeshCoP::Tlv *LeaderBase::FindCommissioningDataSubTlv(uint8_t aType) const
const MeshCoP::Tlv *Leader::FindCommissioningDataSubTlv(uint8_t aType) const
{
const MeshCoP::Tlv *subTlv = nullptr;
const NetworkDataTlv *dataTlv = FindCommissioningData();
Expand All @@ -445,7 +463,7 @@ const MeshCoP::Tlv *LeaderBase::FindCommissioningDataSubTlv(uint8_t aType) const
return subTlv;
}

Error LeaderBase::ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::Type aType, uint16_t &aValue) const
Error Leader::ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::Type aType, uint16_t &aValue) const
{
Error error = kErrorNone;
const MeshCoP::Tlv *subTlv = FindCommissioningDataSubTlv(aType);
Expand All @@ -458,7 +476,7 @@ Error LeaderBase::ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::Type aType, ui
return error;
}

void LeaderBase::GetCommissioningDataset(MeshCoP::CommissioningDataset &aDataset) const
void Leader::GetCommissioningDataset(MeshCoP::CommissioningDataset &aDataset) const
{
const CommissioningDataTlv *dataTlv = FindCommissioningData();
const MeshCoP::Tlv *subTlv;
Expand Down Expand Up @@ -496,22 +514,22 @@ void LeaderBase::GetCommissioningDataset(MeshCoP::CommissioningDataset &aDataset
return;
}

Error LeaderBase::FindBorderAgentRloc(uint16_t &aRloc16) const
Error Leader::FindBorderAgentRloc(uint16_t &aRloc16) const
{
return ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::kBorderAgentLocator, aRloc16);
}

Error LeaderBase::FindCommissioningSessionId(uint16_t &aSessionId) const
Error Leader::FindCommissioningSessionId(uint16_t &aSessionId) const
{
return ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::kCommissionerSessionId, aSessionId);
}

Error LeaderBase::FindJoinerUdpPort(uint16_t &aPort) const
Error Leader::FindJoinerUdpPort(uint16_t &aPort) const
{
return ReadCommissioningDataUint16SubTlv(MeshCoP::Tlv::kJoinerUdpPort, aPort);
}

Error LeaderBase::FindSteeringData(MeshCoP::SteeringData &aSteeringData) const
Error Leader::FindSteeringData(MeshCoP::SteeringData &aSteeringData) const
{
Error error = kErrorNone;
const MeshCoP::SteeringDataTlv *steeringDataTlv = FindInCommissioningData<MeshCoP::SteeringDataTlv>();
Expand All @@ -523,7 +541,7 @@ Error LeaderBase::FindSteeringData(MeshCoP::SteeringData &aSteeringData) const
return error;
}

bool LeaderBase::IsJoiningAllowed(void) const
bool Leader::IsJoiningAllowed(void) const
{
bool isAllowed = false;
MeshCoP::SteeringData steeringData;
Expand All @@ -535,7 +553,7 @@ bool LeaderBase::IsJoiningAllowed(void) const
return isAllowed;
}

Error LeaderBase::SteeringDataCheck(const FilterIndexes &aFilterIndexes) const
Error Leader::SteeringDataCheck(const FilterIndexes &aFilterIndexes) const
{
Error error = kErrorInvalidState;
MeshCoP::SteeringData steeringData;
Expand All @@ -547,7 +565,7 @@ Error LeaderBase::SteeringDataCheck(const FilterIndexes &aFilterIndexes) const
return error;
}

Error LeaderBase::SteeringDataCheckJoiner(const Mac::ExtAddress &aEui64) const
Error Leader::SteeringDataCheckJoiner(const Mac::ExtAddress &aEui64) const
{
FilterIndexes filterIndexes;
Mac::ExtAddress joinerId;
Expand All @@ -558,7 +576,7 @@ Error LeaderBase::SteeringDataCheckJoiner(const Mac::ExtAddress &aEui64) const
return SteeringDataCheck(filterIndexes);
}

Error LeaderBase::SteeringDataCheckJoiner(const MeshCoP::JoinerDiscerner &aDiscerner) const
Error Leader::SteeringDataCheckJoiner(const MeshCoP::JoinerDiscerner &aDiscerner) const
{
FilterIndexes filterIndexes;

Expand All @@ -567,7 +585,7 @@ Error LeaderBase::SteeringDataCheckJoiner(const MeshCoP::JoinerDiscerner &aDisce
return SteeringDataCheck(filterIndexes);
}

void LeaderBase::SignalNetDataChanged(void)
void Leader::SignalNetDataChanged(void)
{
mMaxLength = Max(mMaxLength, GetLength());
Get<ot::Notifier>().Signal(kEventThreadNetdataChanged);
Expand Down
Loading

0 comments on commit 4a9fb06

Please sign in to comment.