diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index e5646dace..696c4c435 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -1143,21 +1143,15 @@ exit: return error; } -bool MleRouter::IsSingleton(void) +bool MleRouter::IsSingleton(void) const { - bool rval = true; + bool isSingleton = true; - if (IsAttached() && IsRouterEligible()) - { - // not a singleton if any other routers exist - if (mRouterTable.GetActiveRouterCount() > 1) - { - ExitNow(rval = false); - } - } + VerifyOrExit(IsAttached() && IsRouterEligible()); + isSingleton = (mRouterTable.GetActiveRouterCount() <= 1); exit: - return rval; + return isSingleton; } int MleRouter::ComparePartitions(bool aSingletonA, @@ -1180,30 +1174,6 @@ exit: return rval; } -bool MleRouter::IsSingleton(const RouteTlv &aRouteTlv) -{ - bool rval = true; - uint8_t count = 0; - - // REEDs do not include a Route TLV and indicate not a singleton - if (!aRouteTlv.IsValid()) - { - ExitNow(rval = false); - } - - // Check if 2 or more active routers - for (uint8_t routerId = 0; routerId <= kMaxRouterId; routerId++) - { - if (aRouteTlv.IsRouterIdSet(routerId) && (++count >= 2)) - { - ExitNow(rval = false); - } - } - -exit: - return rval; -} - Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo) { Error error = kErrorNone; @@ -1258,7 +1228,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo) ExitNow(); } - if (ComparePartitions(IsSingleton(routeTlv), leaderData, IsSingleton(), mLeaderData) > 0 + if (ComparePartitions(routeTlv.IsSingleton(), leaderData, IsSingleton(), mLeaderData) > 0 #if OPENTHREAD_CONFIG_TIME_SYNC_REQUIRED // if time sync is required, it will only migrate to a better network which also enables time sync. && aRxInfo.mMessage.GetTimeSyncSeq() != OT_TIME_SYNC_INVALID_SEQ diff --git a/src/core/thread/mle_router.hpp b/src/core/thread/mle_router.hpp index d9f8f38dd..f3d84aef6 100644 --- a/src/core/thread/mle_router.hpp +++ b/src/core/thread/mle_router.hpp @@ -118,7 +118,7 @@ public: * @retval FALSE It is a child or is not a single router in the network. * */ - bool IsSingleton(void); + bool IsSingleton(void) const; /** * This method generates an Address Solicit request for a Router ID. @@ -634,8 +634,6 @@ private: template void HandleTmf(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); - static bool IsSingleton(const RouteTlv &aRouteTlv); - void HandlePartitionChange(void); void SetChildStateToValid(Child &aChild); diff --git a/src/core/thread/mle_tlvs.hpp b/src/core/thread/mle_tlvs.hpp index 6e86146aa..1970df397 100644 --- a/src/core/thread/mle_tlvs.hpp +++ b/src/core/thread/mle_tlvs.hpp @@ -308,6 +308,15 @@ public: */ bool IsRouterIdSet(uint8_t aRouterId) const { return mRouterIdMask.Contains(aRouterId); } + /** + * This method indicates whether the `RouteTlv` is a singleton, i.e., only one router is allocated. + * + * @retval TRUE It is a singleton. + * @retval FALSE It is not a singleton. + * + */ + bool IsSingleton(void) const { return IsValid() && (mRouterIdMask.GetNumberOfAllocatedIds() <= 1); } + /** * This method returns the Route Data Length value. * @@ -459,6 +468,15 @@ public: */ bool IsRouterIdSet(uint8_t aRouterId) const { return mRouterIdMask.Contains(aRouterId); } + /** + * This method indicates whether the `RouteTlv` is a singleton, i.e., only one router is allocated. + * + * @retval TRUE It is a singleton. + * @retval FALSE It is not a singleton. + * + */ + bool IsSingleton(void) const { return IsValid() && (mRouterIdMask.GetNumberOfAllocatedIds() <= 1); } + /** * This method sets the Router ID bit. *