diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 91501c6e4..4bfe4aa5e 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -624,7 +624,7 @@ otError Mle::BecomeChild(AttachMode aMode) otError error = OT_ERROR_NONE; VerifyOrExit(mRole != OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE); - VerifyOrExit(mAttachState == kAttachStateIdle, error = OT_ERROR_BUSY); + VerifyOrExit(!IsAttaching(), error = OT_ERROR_BUSY); if (mReattachState == kReattachStart) { diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index bd345824c..eaf101a9c 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -602,6 +602,19 @@ public: */ bool IsAttached(void) const; + /** + * This method indicates whether device is currently attaching or not. + * + * Note that an already attached device may also be in attaching state. Examples of this include a leader/router + * trying to attach to a better partition, or a child trying to find a better parent (when feature + * `OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE` is enabled). + * + * @retval TRUE Device is currently trying to attach. + * @retval FALSE Device is not in middle of attach process. + * + */ + bool IsAttaching(void) const { return (mAttachState != kAttachStateIdle); } + /** * This method returns the current Thread interface state. * diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 125fcf680..99ce3178c 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -416,7 +416,7 @@ otError MleRouter::SendAdvertisement(void) // Without this suppression, a device may send an MLE Advertisement before receiving the MLE Child ID Response. // The candidate parent then removes the attaching device because the Source Address TLV includes an RLOC16 that // indicates a Router role (i.e. a Child ID equal to zero). - VerifyOrExit(mAttachState == kAttachStateIdle); + VerifyOrExit(!IsAttaching()); // Suppress MLE Advertisements when transitioning to the router role. // @@ -573,7 +573,7 @@ otError MleRouter::HandleLinkRequest(const Message &aMessage, const Ip6::Message VerifyOrExit(mRole == OT_DEVICE_ROLE_ROUTER || mRole == OT_DEVICE_ROLE_LEADER, error = OT_ERROR_INVALID_STATE); - VerifyOrExit(mAttachState == kAttachStateIdle, error = OT_ERROR_INVALID_STATE); + VerifyOrExit(!IsAttaching(), error = OT_ERROR_INVALID_STATE); // Challenge SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kChallenge, sizeof(challenge), challenge)); @@ -1603,7 +1603,7 @@ otError MleRouter::HandleParentRequest(const Message &aMessage, const Ip6::Messa // A Router MUST NOT send an MLE Parent Response if: // 0. It is detached or attempting to another partition - VerifyOrExit((mRole != OT_DEVICE_ROLE_DETACHED) && (mAttachState == kAttachStateIdle), error = OT_ERROR_DROP); + VerifyOrExit((mRole != OT_DEVICE_ROLE_DETACHED) && !IsAttaching(), error = OT_ERROR_DROP); // 1. It has no available Child capacity (if Max Child Count minus // Child Count would be equal to zero) @@ -3287,7 +3287,7 @@ Neighbor *MleRouter::GetNeighbor(const Mac::ExtAddress &aAddress) ExitNow(); } - if (mAttachState != kAttachStateIdle) + if (IsAttaching()) { rval = Mle::GetNeighbor(aAddress); }