[mle] add IsRouterRloc16() and IsChildRloc16() (#10423)

This commit adds `IsRouterRloc16()` (replacing `IsActiveRouter()`),
which checks whether a given RLOC16 refers to a router and not
a child. The new name clarifies that it is only a check on the RLOC16
value and does not verify if the corresponding router ID is allocated
and active. This commit also adds `IsChildRloc16()`, which is similar
to `IsRouterRloc16()` and checks if the given RLOC16 is for a child.
This simplifies the code and improves readability.
This commit is contained in:
Abtin Keshavarzian
2024-06-24 08:29:53 -04:00
committed by GitHub
parent a9f34d944c
commit 336984b25a
11 changed files with 38 additions and 27 deletions
+1 -1
View File
@@ -1157,7 +1157,7 @@ void MeshForwarder::UpdateNeighborLinkFailures(Neighbor &aNeighbor,
{
aNeighbor.IncrementLinkFailures();
if (aAllowNeighborRemove && (Mle::IsActiveRouter(aNeighbor.GetRloc16())) &&
if (aAllowNeighborRemove && (Mle::IsRouterRloc16(aNeighbor.GetRloc16())) &&
(aNeighbor.GetLinkFailures() >= aFailLimit))
{
#if OPENTHREAD_FTD
+1 -1
View File
@@ -487,7 +487,7 @@ Error MeshForwarder::AnycastRouteLookup(uint8_t aServiceId, AnycastType aType, u
routerId = Mle::RouterIdFromRloc16(bestDest);
if (!(Mle::IsActiveRouter(bestDest) || Mle::Rloc16FromRouterId(routerId) == Get<Mle::MleRouter>().GetRloc16()))
if (!(Mle::IsRouterRloc16(bestDest) || Mle::Rloc16FromRouterId(routerId) == Get<Mle::MleRouter>().GetRloc16()))
{
// if agent is neither active router nor child of this device
// use the parent of the ED Agent as Dest
+5 -5
View File
@@ -214,7 +214,7 @@ Error Mle::Start(StartMode aMode)
Attach(kAnyPartition);
}
#if OPENTHREAD_FTD
else if (IsActiveRouter(GetRloc16()))
else if (IsRouterRloc16(GetRloc16()))
{
if (Get<MleRouter>().BecomeRouter(ThreadStatusTlv::kTooFewRouters) != kErrorNone)
{
@@ -397,7 +397,7 @@ void Mle::Restore(void)
}
#if OPENTHREAD_MTD
if (!IsActiveRouter(networkInfo.GetRloc16()))
if (IsChildRloc16(networkInfo.GetRloc16()))
#endif
{
Get<Mac::Mac>().SetShortAddress(networkInfo.GetRloc16());
@@ -412,7 +412,7 @@ void Mle::Restore(void)
ExitNow();
}
if (!IsActiveRouter(networkInfo.GetRloc16()))
if (IsChildRloc16(networkInfo.GetRloc16()))
{
if (Get<Settings>().Read(parentInfo) != kErrorNone)
{
@@ -2751,7 +2751,7 @@ void Mle::ReestablishLinkWithNeighbor(Neighbor &aNeighbor)
#if OPENTHREAD_FTD
VerifyOrExit(IsFullThreadDevice());
if (IsActiveRouter(aNeighbor.GetRloc16()))
if (IsRouterRloc16(aNeighbor.GetRloc16()))
{
IgnoreError(Get<MleRouter>().SendLinkRequest(&aNeighbor));
}
@@ -3045,7 +3045,7 @@ bool Mle::IsBetterParent(uint16_t aRloc16,
rval = ThreeWayCompare(LinkQualityForLinkMargin(aTwoWayLinkMargin), mParentCandidate.GetTwoWayLinkQuality());
VerifyOrExit(rval == 0);
rval = ThreeWayCompare(IsActiveRouter(aRloc16), IsActiveRouter(mParentCandidate.GetRloc16()));
rval = ThreeWayCompare(IsRouterRloc16(aRloc16), IsRouterRloc16(mParentCandidate.GetRloc16()));
VerifyOrExit(rval == 0);
rval = ThreeWayCompare(aConnectivityTlv.GetParentPriority(), mParentCandidate.mPriority);
+7 -7
View File
@@ -702,7 +702,7 @@ void MleRouter::HandleLinkRequest(RxInfo &aRxInfo)
switch (Tlv::Find<SourceAddressTlv>(aRxInfo.mMessage, sourceAddress))
{
case kErrorNone:
if (IsActiveRouter(sourceAddress))
if (IsRouterRloc16(sourceAddress))
{
neighbor = mRouterTable.FindRouterByRloc16(sourceAddress);
VerifyOrExit(neighbor != nullptr, error = kErrorParse);
@@ -727,7 +727,7 @@ void MleRouter::HandleLinkRequest(RxInfo &aRxInfo)
case kErrorNotFound:
// A missing source address indicates that the router was
// recently reset.
VerifyOrExit(aRxInfo.IsNeighborStateValid() && IsActiveRouter(aRxInfo.mNeighbor->GetRloc16()),
VerifyOrExit(aRxInfo.IsNeighborStateValid() && IsRouterRloc16(aRxInfo.mNeighbor->GetRloc16()),
error = kErrorDrop);
neighbor = aRxInfo.mNeighbor;
break;
@@ -791,7 +791,7 @@ Error MleRouter::SendLinkAccept(const RxInfo &aRxInfo,
linkMargin = Get<Mac::Mac>().ComputeLinkMargin(aRxInfo.mMessage.GetAverageRss());
SuccessOrExit(error = message->AppendLinkMarginTlv(linkMargin));
if (aNeighbor != nullptr && IsActiveRouter(aNeighbor->GetRloc16()))
if (aNeighbor != nullptr && IsRouterRloc16(aNeighbor->GetRloc16()))
{
SuccessOrExit(error = message->AppendLeaderDataTlv());
}
@@ -891,7 +891,7 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
Log(kMessageReceive, aRequest ? kTypeLinkAcceptAndRequest : kTypeLinkAccept, aRxInfo.mMessageInfo.GetPeerAddr(),
sourceAddress);
VerifyOrExit(IsActiveRouter(sourceAddress), error = kErrorParse);
VerifyOrExit(IsRouterRloc16(sourceAddress), error = kErrorParse);
routerId = RouterIdFromRloc16(sourceAddress);
router = mRouterTable.FindRouterById(routerId);
@@ -1249,7 +1249,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo, uint16_t aSourceAddress, c
ExitNow();
}
VerifyOrExit(IsActiveRouter(aSourceAddress) && routeTlv.IsValid());
VerifyOrExit(IsRouterRloc16(aSourceAddress) && routeTlv.IsValid());
routerId = RouterIdFromRloc16(aSourceAddress);
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
@@ -2353,7 +2353,7 @@ void MleRouter::HandleChildUpdateResponse(RxInfo &aRxInfo)
LeaderData leaderData;
Child *child;
if ((aRxInfo.mNeighbor == nullptr) || IsActiveRouter(aRxInfo.mNeighbor->GetRloc16()) ||
if ((aRxInfo.mNeighbor == nullptr) || IsRouterRloc16(aRxInfo.mNeighbor->GetRloc16()) ||
!Get<ChildTable>().Contains(*aRxInfo.mNeighbor))
{
Log(kMessageReceive, kTypeChildUpdateResponseOfUnknownChild, aRxInfo.mMessageInfo.GetPeerAddr());
@@ -3143,7 +3143,7 @@ void MleRouter::RemoveNeighbor(Neighbor &aNeighbor)
{
ClearParentCandidate();
}
else if (!IsActiveRouter(aNeighbor.GetRloc16()))
else if (IsChildRloc16(aNeighbor.GetRloc16()))
{
OT_ASSERT(mChildTable.Contains(aNeighbor));
+15 -4
View File
@@ -682,15 +682,26 @@ inline uint16_t CommissionerAloc16FromId(uint16_t aSessionId)
inline uint16_t Rloc16FromRouterId(uint8_t aRouterId) { return static_cast<uint16_t>(aRouterId << kRouterIdOffset); }
/**
* Indicates whether or not @p aRloc16 refers to an active router.
* Indicates whether or not @p aRloc16 refers to a router.
*
* @param[in] aRloc16 The RLOC16 value.
*
* @retval TRUE If @p aRloc16 refers to an active router.
* @retval FALSE If @p aRloc16 does not refer to an active router.
* @retval TRUE If @p aRloc16 refers to a router.
* @retval FALSE If @p aRloc16 does not refer to a router.
*
*/
inline bool IsActiveRouter(uint16_t aRloc16) { return ChildIdFromRloc16(aRloc16) == 0; }
inline bool IsRouterRloc16(uint16_t aRloc16) { return ChildIdFromRloc16(aRloc16) == 0; }
/**
* Indicates whether or not @p aRloc16 refers to a child.
*
* @param[in] aRloc16 The RLOC16 value.
*
* @retval TRUE If @p aRloc16 refers to a child.
* @retval FALSE If @p aRloc16 does not refer to a child.
*
*/
inline bool IsChildRloc16(uint16_t aRloc16) { return ChildIdFromRloc16(aRloc16) != 0; }
/**
* Converts a device role into a human-readable string.
+2 -2
View File
@@ -579,11 +579,11 @@ void NetworkData::AddRloc16ToRlocs(uint16_t aRloc16, Rlocs &aRlocs, RoleFilter a
break;
case kRouterRoleOnly:
VerifyOrExit(Mle::IsActiveRouter(aRloc16));
VerifyOrExit(Mle::IsRouterRloc16(aRloc16));
break;
case kChildRoleOnly:
VerifyOrExit(!Mle::IsActiveRouter(aRloc16));
VerifyOrExit(Mle::IsChildRloc16(aRloc16));
break;
}
+1 -1
View File
@@ -339,7 +339,7 @@ int Leader::CompareRouteEntries(int8_t aFirstPreference,
// If all the same, prefer the BR acting as a router over an
// end device.
result = ThreeWayCompare(Mle::IsActiveRouter(aFirstRloc), Mle::IsActiveRouter(aSecondRloc));
result = ThreeWayCompare(Mle::IsRouterRloc16(aFirstRloc), Mle::IsRouterRloc16(aSecondRloc));
#endif
exit:
+1 -1
View File
@@ -138,7 +138,7 @@ Error Notifier::RemoveStaleChildEntries(void)
for (uint16_t rloc16 : rlocs)
{
if (!Mle::IsActiveRouter(rloc16) && Mle::RouterIdMatch(Get<Mle::MleRouter>().GetRloc16(), rloc16) &&
if (Mle::IsChildRloc16(rloc16) && Mle::RouterIdMatch(Get<Mle::MleRouter>().GetRloc16(), rloc16) &&
Get<ChildTable>().FindChild(rloc16, Child::kInStateValid) == nullptr)
{
error = SendServerDataNotification(rloc16);
+1 -1
View File
@@ -275,7 +275,7 @@ bool Publisher::Entry::IsPreferred(uint16_t aRloc16) const
// router over an entry from an end-device (e.g., a REED). If both
// are the same type, then the one with smaller RLOC16 is preferred.
bool isOtherRouter = Mle::IsActiveRouter(aRloc16);
bool isOtherRouter = Mle::IsRouterRloc16(aRloc16);
return (Get<Mle::Mle>().IsRouterOrLeader() == isOtherRouter) ? (aRloc16 < Get<Mle::Mle>().GetRloc16())
: isOtherRouter;
+3 -3
View File
@@ -322,7 +322,7 @@ Error RouterTable::GetRouterInfo(uint16_t aRouterId, Router::Info &aRouterInfo)
}
else
{
VerifyOrExit(Mle::IsActiveRouter(aRouterId), error = kErrorInvalidArgs);
VerifyOrExit(Mle::IsRouterRloc16(aRouterId), error = kErrorInvalidArgs);
routerId = Mle::RouterIdFromRloc16(aRouterId);
VerifyOrExit(routerId <= Mle::kMaxRouterId, error = kErrorInvalidArgs);
}
@@ -486,7 +486,7 @@ void RouterTable::GetNextHopAndPathCost(uint16_t aDestRloc16, uint16_t &aNextHop
}
}
if (!Mle::IsActiveRouter(aDestRloc16))
if (Mle::IsChildRloc16(aDestRloc16))
{
// Destination is a child. we assume best link quality
// between destination and its parent router.
@@ -721,7 +721,7 @@ void RouterTable::FillRouteTlv(Mle::RouteTlv &aRouteTlv, const Neighbor *aNeighb
mRouterIdMap.GetAsRouterIdSet(routerIdSet);
if ((aNeighbor != nullptr) && Mle::IsActiveRouter(aNeighbor->GetRloc16()))
if ((aNeighbor != nullptr) && Mle::IsRouterRloc16(aNeighbor->GetRloc16()))
{
// Sending a Link Accept message that may require truncation
// of Route64 TLV.
+1 -1
View File
@@ -172,7 +172,7 @@ Error MeshDiag::SendQuery(uint16_t aRloc16, const uint8_t *aTlvs, uint8_t aTlvsL
VerifyOrExit(Get<Mle::Mle>().IsAttached(), error = kErrorInvalidState);
VerifyOrExit(mState == kStateIdle, error = kErrorBusy);
VerifyOrExit(Mle::IsActiveRouter(aRloc16), error = kErrorInvalidArgs);
VerifyOrExit(Mle::IsRouterRloc16(aRloc16), error = kErrorInvalidArgs);
VerifyOrExit(Get<RouterTable>().IsAllocated(Mle::RouterIdFromRloc16(aRloc16)), error = kErrorNotFound);
destination.SetToRoutingLocator(Get<Mle::Mle>().GetMeshLocalPrefix(), aRloc16);