From 018f54f71b059bf338247595eb1f6a6710e3d0c9 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 25 Mar 2022 12:10:06 -0700 Subject: [PATCH] [mle] renames and smaller changes (#7515) This commit contains small changes in `Mle`: - Renames `mParentRequestMode` to `mAttachMode` (to match its type). - Shortens `ParentRequestType` enumerator names. - Changes `SendParentRequest()` to return `void`. - Adds a protected method `Mle::Attach()` (which replaces the `BecomeChild()`). - Moves `AttachMode` enum as a protected definition in `Mle` class itself and renames the `AttachMode` constants. --- src/core/api/thread_api.cpp | 2 +- src/core/thread/mle.cpp | 107 ++++++++++++++++++--------------- src/core/thread/mle.hpp | 41 +++++++++---- src/core/thread/mle_router.cpp | 17 +++--- src/core/thread/mle_types.hpp | 13 ---- 5 files changed, 97 insertions(+), 83 deletions(-) diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index 9ebafa9d7..8ec7a6bf7 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -295,7 +295,7 @@ otError otThreadBecomeDetached(otInstance *aInstance) otError otThreadBecomeChild(otInstance *aInstance) { - return AsCoreType(aInstance).Get().BecomeChild(Mle::kAttachAny); + return AsCoreType(aInstance).Get().BecomeChild(); } otError otThreadGetNextNeighborInfo(otInstance *aInstance, otNeighborInfoIterator *aIterator, otNeighborInfo *aInfo) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 23f6f4c62..ccf5bc1ec 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -79,7 +79,7 @@ Mle::Mle(Instance &aInstance) , mDelayedResponseTimer(aInstance, Mle::HandleDelayedResponseTimer) , mMessageTransmissionTimer(aInstance, Mle::HandleMessageTransmissionTimer) , mParentLeaderCost(0) - , mParentRequestMode(kAttachAny) + , mAttachMode(kAnyPartition) , mParentPriority(0) , mParentLinkQuality3(0) , mParentLinkQuality2(0) @@ -219,14 +219,14 @@ Error Mle::Start(StartMode aMode) if ((aMode == kAnnounceAttach) || (GetRloc16() == Mac::kShortAddrInvalid)) { - IgnoreError(BecomeChild(kAttachAny)); + Attach(kAnyPartition); } #if OPENTHREAD_FTD else if (IsActiveRouter(GetRloc16())) { if (Get().BecomeRouter(ThreadStatusTlv::kTooFewRouters) != kErrorNone) { - IgnoreError(BecomeChild(kAttachAny)); + Attach(kAnyPartition); } } #endif @@ -484,19 +484,29 @@ Error Mle::BecomeDetached(void) SetStateDetached(); mParent.SetState(Neighbor::kStateInvalid); SetRloc16(Mac::kShortAddrInvalid); - IgnoreError(BecomeChild(kAttachAny)); + Attach(kAnyPartition); exit: return error; } -Error Mle::BecomeChild(AttachMode aMode) +Error Mle::BecomeChild(void) { Error error = kErrorNone; VerifyOrExit(!IsDisabled(), error = kErrorInvalidState); VerifyOrExit(!IsAttaching(), error = kErrorBusy); + Attach(kAnyPartition); + +exit: + return error; +} + +void Mle::Attach(AttachMode aMode) +{ + VerifyOrExit(!IsDisabled() && !IsAttaching()); + if (!IsDetached()) { mAttachCounter = 0; @@ -516,9 +526,9 @@ Error Mle::BecomeChild(AttachMode aMode) mParentCandidate.Clear(); SetAttachState(kAttachStateStart); - mParentRequestMode = aMode; + mAttachMode = aMode; - if (aMode != kAttachBetter) + if (aMode != kBetterPartition) { #if OPENTHREAD_FTD if (IsFullThreadDevice()) @@ -552,7 +562,7 @@ Error Mle::BecomeChild(AttachMode aMode) } exit: - return error; + return; } uint32_t Mle::GetAttachStartDelay(void) const @@ -668,7 +678,7 @@ void Mle::SetStateChild(uint16_t aRloc16) #if OPENTHREAD_FTD if (IsFullThreadDevice()) { - Get().HandleChildStart(mParentRequestMode); + Get().HandleChildStart(mAttachMode); } #endif @@ -772,7 +782,7 @@ Error Mle::SetDeviceMode(DeviceMode aDeviceMode) case kRoleDetached: mAttachCounter = 0; SetStateDetached(); - IgnoreError(BecomeChild(kAttachAny)); + Attach(kAnyPartition); break; case kRoleChild: @@ -1751,7 +1761,7 @@ bool Mle::HasAcceptableParentCandidate(void) const // new parent candidate is compared with the current parent // and that it is indeed preferred over the current one. - VerifyOrExit(mReceivedResponseFromParent || (mParentRequestMode == kAttachBetter)); + VerifyOrExit(mReceivedResponseFromParent || (mAttachMode == kBetterPartition)); } hasAcceptableParent = true; @@ -1788,12 +1798,12 @@ void Mle::HandleAttachTimer(void) case kAttachStateStart: if (mAttachCounter > 0) { - LogNote("Attempt to attach - attempt %d, %s %s", mAttachCounter, AttachModeToString(mParentRequestMode), + LogNote("Attempt to attach - attempt %d, %s %s", mAttachCounter, AttachModeToString(mAttachMode), ReattachStateToString(mReattachState)); } else { - LogNote("Attempt to attach - %s %s", AttachModeToString(mParentRequestMode), + LogNote("Attempt to attach - %s %s", AttachModeToString(mAttachMode), ReattachStateToString(mReattachState)); } @@ -1804,16 +1814,16 @@ void Mle::HandleAttachTimer(void) // initial MLE Parent Request has both E and R flags set in Scan Mask TLV // during reattach when losing connectivity. - if (mParentRequestMode == kAttachSame1 || mParentRequestMode == kAttachSame2) + if (mAttachMode == kSamePartition || mAttachMode == kSamePartitionRetry) { - IgnoreError(SendParentRequest(kParentRequestTypeRoutersAndReeds)); + SendParentRequest(kToRoutersAndReeds); delay = kParentRequestReedTimeout; } // initial MLE Parent Request has only R flag set in Scan Mask TLV for // during initial attach or downgrade process else { - IgnoreError(SendParentRequest(kParentRequestTypeRouters)); + SendParentRequest(kToRouters); delay = kParentRequestRouterTimeout; } @@ -1821,7 +1831,7 @@ void Mle::HandleAttachTimer(void) case kAttachStateParentRequestRouter: SetAttachState(kAttachStateParentRequestReed); - IgnoreError(SendParentRequest(kParentRequestTypeRoutersAndReeds)); + SendParentRequest(kToRoutersAndReeds); delay = kParentRequestReedTimeout; break; @@ -1839,7 +1849,7 @@ void Mle::HandleAttachTimer(void) // (with `mAnnounceDelay` wait between them). SetAttachState(kAttachStateAnnounce); - IgnoreError(SendParentRequest(kParentRequestTypeRoutersAndReeds)); + SendParentRequest(kToRoutersAndReeds); mAnnounceChannel = Mac::ChannelMask::kChannelIteratorFirst; delay = mAnnounceDelay; break; @@ -1924,9 +1934,9 @@ uint32_t Mle::Reattach(void) VerifyOrExit(mReattachState == kReattachStop); - switch (mParentRequestMode) + switch (mAttachMode) { - case kAttachAny: + case kAnyPartition: if (!IsChild()) { if (mAlternatePanId != Mac::kPanIdBroadcast) @@ -1956,16 +1966,16 @@ uint32_t Mle::Reattach(void) break; - case kAttachSame1: - IgnoreError(BecomeChild(kAttachSame2)); + case kSamePartition: + Attach(kSamePartitionRetry); break; - case kAttachSame2: - case kAttachSameDowngrade: - IgnoreError(BecomeChild(kAttachAny)); + case kSamePartitionRetry: + case kDowngradeToReed: + Attach(kAnyPartition); break; - case kAttachBetter: + case kBetterPartition: break; } @@ -2065,7 +2075,7 @@ void Mle::RemoveDelayedMessage(Message::SubType aSubType, MessageType aMessageTy } } -Error Mle::SendParentRequest(ParentRequestType aType) +void Mle::SendParentRequest(ParentRequestType aType) { Error error = kErrorNone; Message * message; @@ -2076,11 +2086,11 @@ Error Mle::SendParentRequest(ParentRequestType aType) switch (aType) { - case kParentRequestTypeRouters: + case kToRouters: scanMask = ScanMaskTlv::kRouterFlag; break; - case kParentRequestTypeRoutersAndReeds: + case kToRoutersAndReeds: scanMask = ScanMaskTlv::kRouterFlag | ScanMaskTlv::kEndDeviceFlag; break; } @@ -2100,18 +2110,17 @@ Error Mle::SendParentRequest(ParentRequestType aType) switch (aType) { - case kParentRequestTypeRouters: + case kToRouters: Log(kMessageSend, kTypeParentRequestToRouters, destination); break; - case kParentRequestTypeRoutersAndReeds: + case kToRoutersAndReeds: Log(kMessageSend, kTypeParentRequestToRoutersReeds, destination); break; } exit: FreeMessageOnError(message, error); - return error; } void Mle::RequestShorterChildIdRequest(void) @@ -3516,22 +3525,22 @@ void Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInfo & bool isIdSequenceGreater = SerialNumber::IsGreater(connectivity.GetIdSequence(), Get().GetRouterIdSequence()); - switch (mParentRequestMode) + switch (mAttachMode) { - case kAttachAny: + case kAnyPartition: VerifyOrExit(!isPartitionIdSame || isIdSequenceGreater); break; - case kAttachSame1: - case kAttachSame2: + case kSamePartition: + case kSamePartitionRetry: VerifyOrExit(isPartitionIdSame && isIdSequenceGreater); break; - case kAttachSameDowngrade: + case kDowngradeToReed: VerifyOrExit(isPartitionIdSame && (isIdSequenceSame || isIdSequenceGreater)); break; - case kAttachBetter: + case kBetterPartition: VerifyOrExit(!isPartitionIdSame); VerifyOrExit(MleRouter::ComparePartitions(connectivity.GetActiveRouters() <= 1, leaderData, @@ -4248,7 +4257,7 @@ void Mle::HandleParentSearchTimer(void) { LogInfo("PeriodicParentSearch: Parent RSS less than %d, searching for new parents", kParentSearchRssThreadhold); mParentSearchIsInBackoff = true; - IgnoreError(BecomeChild(kAttachAny)); + Attach(kAnyPartition); } exit: @@ -4546,18 +4555,18 @@ const char *Mle::RoleToString(DeviceRole aRole) const char *Mle::AttachModeToString(AttachMode aMode) { static const char *const kAttachModeStrings[] = { - "any-partition", // (0) kAttachAny - "same-partition-try-1", // (1) kAttachSame1 - "same-partition-try-2", // (2) kAttachSame2 - "better-partition", // (3) kAttachBetter - "same-partition-downgrade", // (4) kAttachSameDowngrade + "AnyPartition", // (0) kAnyPartition + "SamePartition", // (1) kSamePartition + "SamePartitionRetry", // (2) kSamePartitionRetry + "BetterPartition", // (3) kBetterPartition + "DowngradeToReed", // (4) kDowngradeToReed }; - static_assert(kAttachAny == 0, "kAttachAny value is incorrect"); - static_assert(kAttachSame1 == 1, "kAttachSame1 value is incorrect"); - static_assert(kAttachSame2 == 2, "kAttachSame2 value is incorrect"); - static_assert(kAttachBetter == 3, "kAttachBetter value is incorrect"); - static_assert(kAttachSameDowngrade == 4, "kAttachSameDowngrade value is incorrect"); + static_assert(kAnyPartition == 0, "kAnyPartition value is incorrect"); + static_assert(kSamePartition == 1, "kSamePartition value is incorrect"); + static_assert(kSamePartitionRetry == 2, "kSamePartitionRetry value is incorrect"); + static_assert(kBetterPartition == 3, "kBetterPartition value is incorrect"); + static_assert(kDowngradeToReed == 4, "kDowngradeToReed value is incorrect"); return kAttachModeStrings[aMode]; } diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 00537696d..ddd7225b1 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -181,14 +181,12 @@ public: /** * This method causes the Thread interface to attempt an MLE attach. * - * @param[in] aMode Indicates what partitions to attach to. - * * @retval kErrorNone Successfully began the attach process. * @retval kErrorInvalidState MLE is Disabled. * @retval kErrorBusy An attach process is in progress. * */ - Error BecomeChild(AttachMode aMode); + Error BecomeChild(void); /** * This method indicates whether or not the Thread device is attached to a Thread network. @@ -785,6 +783,19 @@ protected: kCommandTimeSync = 99, ///< Time Sync (when OPENTHREAD_CONFIG_TIME_SYNC_ENABLE enabled) }; + /** + * Attach mode. + * + */ + enum AttachMode : uint8_t + { + kAnyPartition, ///< Attach to any Thread partition. + kSamePartition, ///< Attach to the same Thread partition (attempt 1 when losing connectivity). + kSamePartitionRetry, ///< Attach to the same Thread partition (attempt 2 when losing connectivity). + kBetterPartition, ///< Attach to a better (i.e. higher weight/partition id) Thread partition. + kDowngradeToReed, ///< Attach to the same Thread partition during downgrade process. + }; + /** * States during attach (when searching for a parent). * @@ -806,10 +817,10 @@ protected: */ enum ReattachState : uint8_t { - kReattachStop = 0, ///< Reattach process is disabled or finished - kReattachStart = 1, ///< Start reattach process - kReattachActive = 2, ///< Reattach using stored Active Dataset - kReattachPending = 3, ///< Reattach using stored Pending Dataset + kReattachStop, ///< Reattach process is disabled or finished + kReattachStart, ///< Start reattach process + kReattachActive, ///< Reattach using stored Active Dataset + kReattachPending, ///< Reattach using stored Pending Dataset }; static constexpr uint16_t kMleMaxResponseDelay = 1000u; ///< Max delay before responding to a multicast request. @@ -950,6 +961,14 @@ protected: */ void SetRole(DeviceRole aRole); + /** + * This method causes the Thread interface to attempt an MLE attach. + * + * @param[in] aMode Indicates what partitions to attach to. + * + */ + void Attach(AttachMode aMode); + /** * This method sets the attach state * @@ -1646,8 +1665,8 @@ private: enum ParentRequestType : uint8_t { - kParentRequestTypeRouters, // Parent Request to all routers. - kParentRequestTypeRoutersAndReeds, // Parent Request to all routers and REEDs. + kToRouters, // Parent Request to routers only. + kToRoutersAndReeds, // Parent Request to all routers and REEDs. }; enum ChildUpdateRequestState : uint8_t @@ -1825,7 +1844,7 @@ private: bool HasUnregisteredAddress(void); uint32_t GetAttachStartDelay(void) const; - Error SendParentRequest(ParentRequestType aType); + void SendParentRequest(ParentRequestType aType); Error SendChildIdRequest(void); Error GetNextAnnouceChannel(uint8_t &aChannel) const; bool HasMoreChannelsToAnnouce(void) const; @@ -1876,7 +1895,7 @@ private: Challenge mParentRequestChallenge; - AttachMode mParentRequestMode; + AttachMode mAttachMode; int8_t mParentPriority; uint8_t mParentLinkQuality3; uint8_t mParentLinkQuality2; diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 61b7419b3..2b6b9b931 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -312,7 +312,7 @@ void MleRouter::HandleChildStart(AttachMode aMode) switch (aMode) { - case kAttachSameDowngrade: + case kDowngradeToReed: SendAddressRelease(); // reset children info if any @@ -325,8 +325,8 @@ void MleRouter::HandleChildStart(AttachMode aMode) SetRouterId(kInvalidRouterId); break; - case kAttachSame1: - case kAttachSame2: + case kSamePartition: + case kSamePartitionRetry: if (HasChildren()) { IgnoreError(BecomeRouter(ThreadStatusTlv::kHaveChildIdRequest)); @@ -334,7 +334,7 @@ void MleRouter::HandleChildStart(AttachMode aMode) break; - case kAttachAny: + case kAnyPartition: // If attach was started due to receiving MLE Announce Messages, all rx-on-when-idle devices would // start attach immediately when receiving such Announce message as in Thread 1.1 specification, // Section 4.8.1, @@ -351,7 +351,7 @@ void MleRouter::HandleChildStart(AttachMode aMode) OT_FALL_THROUGH; - case kAttachBetter: + case kBetterPartition: if (HasChildren() && mPreviousPartitionIdRouter != mLeaderData.GetPartitionId()) { IgnoreError(BecomeRouter(ThreadStatusTlv::kParentPartitionChange)); @@ -1236,7 +1236,7 @@ Error MleRouter::HandleAdvertisement(const Message &aMessage, const Ip6::Message #endif ) { - IgnoreError(BecomeChild(kAttachBetter)); + Attach(kBetterPartition); } ExitNow(error = kErrorDrop); @@ -1816,14 +1816,13 @@ void MleRouter::HandleTimeTick(void) if ((mRouterTable.GetActiveRouterCount() > 0) && (mRouterTable.GetLeaderAge() >= mNetworkIdTimeout)) { LogInfo("Router ID Sequence timeout"); - IgnoreError(BecomeChild(kAttachSame1)); + Attach(kSamePartition); } if (routerStateUpdate && mRouterTable.GetActiveRouterCount() > mRouterDowngradeThreshold) { - // downgrade to REED LogNote("Downgrade to REED"); - IgnoreError(BecomeChild(kAttachSameDowngrade)); + Attach(kDowngradeToReed); } break; diff --git a/src/core/thread/mle_types.hpp b/src/core/thread/mle_types.hpp index f939836ae..be080a61b 100644 --- a/src/core/thread/mle_types.hpp +++ b/src/core/thread/mle_types.hpp @@ -194,19 +194,6 @@ enum DeviceRole : uint8_t kRoleLeader = OT_DEVICE_ROLE_LEADER, ///< The Thread Leader role. }; -/** - * MLE Attach modes - * - */ -enum AttachMode : uint8_t -{ - kAttachAny = 0, ///< Attach to any Thread partition. - kAttachSame1 = 1, ///< Attach to the same Thread partition (attempt 1 when losing connectivity). - kAttachSame2 = 2, ///< Attach to the same Thread partition (attempt 2 when losing connectivity). - kAttachBetter = 3, ///< Attach to a better (i.e. higher weight/partition id) Thread partition. - kAttachSameDowngrade = 4, ///< Attach to the same Thread partition during downgrade process. -}; - constexpr uint16_t kAloc16Leader = 0xfc00; constexpr uint16_t kAloc16DhcpAgentStart = 0xfc01; constexpr uint16_t kAloc16DhcpAgentEnd = 0xfc0f;