From 8f67c5e72daf28221fe2a54f30baf1c1a9bc2046 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 10 Jul 2025 18:13:45 -0700 Subject: [PATCH] [mle] refactor `BecomeLeader` to use enum for weight check (#11704) This commit refactors the `Mle::BecomeLeader()` method to use a new `LeaderWeightCheck` enum instead of a boolean parameter for checking the leader weight. The new enum, with values `kCheckLeaderWeight` and `kIgnoreLeaderWeight`, makes the intent at the call sites more explicit and improves code readability by avoiding the ambiguity of a `true`/`false` flag. The functional behavior remains unchanged. --- src/core/api/thread_ftd_api.cpp | 2 +- src/core/thread/mle.cpp | 2 +- src/core/thread/mle.hpp | 33 +++++++++++++++++++-------------- src/core/thread/mle_ftd.cpp | 4 ++-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/core/api/thread_ftd_api.cpp b/src/core/api/thread_ftd_api.cpp index 4b758f6d5..0a9cde63f 100644 --- a/src/core/api/thread_ftd_api.cpp +++ b/src/core/api/thread_ftd_api.cpp @@ -181,7 +181,7 @@ otError otThreadBecomeRouter(otInstance *aInstance) otError otThreadBecomeLeader(otInstance *aInstance) { - return AsCoreType(aInstance).Get().BecomeLeader(/* aCheckWeight */ true); + return AsCoreType(aInstance).Get().BecomeLeader(Mle::Mle::kCheckLeaderWeight); } uint8_t otThreadGetRouterDowngradeThreshold(otInstance *aInstance) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index c5d9a6a6e..998b1366d 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1655,7 +1655,7 @@ uint32_t Mle::Reattach(void) } #if OPENTHREAD_FTD - if (IsFullThreadDevice() && BecomeLeader(/* aCheckWeight */ false) == kErrorNone) + if (IsFullThreadDevice() && BecomeLeader(kIgnoreLeaderWeight) == kErrorNone) { ExitNow(); } diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 4f5de6063..3133949ec 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -810,21 +810,26 @@ public: Error BecomeRouter(ThreadStatusTlv::Status aStatus); /** - * Becomes a leader and starts a new partition. - * - * If the device is already attached, this method can be used to attempt to take over as the leader, creating a new - * partition. For this to work, the local leader weight must be greater than the weight of the current leader. The - * @p aCheckWeight can be used to ensure that this check is performed. - * - * @param[in] aCheckWeight Check that the local leader weight is larger than the weight of the current leader. - * - * @retval kErrorNone Successfully become a Leader and started a new partition. - * @retval kErrorInvalidState Thread is not enabled. - * @retval kErrorNotCapable Device is not capable of becoming a leader (not router eligible), or - * @p aCheckWeight is true and cannot override the current leader due to its local - * leader weight being same or smaller than current leader's weight. + * Specifies the leader weight check behavior used in `BecomeLeader()`. */ - Error BecomeLeader(bool aCheckWeight); + enum LeaderWeightCheck : uint8_t{ + kCheckLeaderWeight, ///< Enforces that the local leader weight is greater than the current leader's weight. + kIgnoreLeaderWeight, ///< Skips the leader weight check, attempting to become leader regardless. + }; + + /** + * Attempts to become the leader and start a new partition. + * + * If the device is already attached, this method can be used to take over the leader role. + * + * @param[in] aMode Specifies whether to enforce or ignore the leader weight check. + * + * @retval kErrorNone Successfully became leader and started a new partition. + * @retval kErrorInvalidState The Thread interface is not enabled. + * @retval kErrorNotCapable The device is not router-eligible, or the leader weight check is enabled and the + * local leader weight is less than or equal to the current leader's weight. + */ + Error BecomeLeader(LeaderWeightCheck aMode); #if OPENTHREAD_CONFIG_MLE_DEVICE_PROPERTY_LEADER_WEIGHT_ENABLE /** diff --git a/src/core/thread/mle_ftd.cpp b/src/core/thread/mle_ftd.cpp index ddb0c978d..18299f794 100644 --- a/src/core/thread/mle_ftd.cpp +++ b/src/core/thread/mle_ftd.cpp @@ -216,7 +216,7 @@ exit: return error; } -Error Mle::BecomeLeader(bool aCheckWeight) +Error Mle::BecomeLeader(LeaderWeightCheck aMode) { Error error = kErrorNone; Router *router; @@ -232,7 +232,7 @@ Error Mle::BecomeLeader(bool aCheckWeight) VerifyOrExit(!IsLeader(), error = kErrorNone); VerifyOrExit(IsRouterEligible(), error = kErrorNotCapable); - if (aCheckWeight && IsAttached()) + if ((aMode == kCheckLeaderWeight) && IsAttached()) { VerifyOrExit(mLeaderWeight > mLeaderData.GetWeighting(), error = kErrorNotCapable); }