[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.
This commit is contained in:
Abtin Keshavarzian
2025-07-10 18:13:45 -07:00
committed by GitHub
parent 0d91ffc5ca
commit 8f67c5e72d
4 changed files with 23 additions and 18 deletions
+1 -1
View File
@@ -181,7 +181,7 @@ otError otThreadBecomeRouter(otInstance *aInstance)
otError otThreadBecomeLeader(otInstance *aInstance)
{
return AsCoreType(aInstance).Get<Mle::Mle>().BecomeLeader(/* aCheckWeight */ true);
return AsCoreType(aInstance).Get<Mle::Mle>().BecomeLeader(Mle::Mle::kCheckLeaderWeight);
}
uint8_t otThreadGetRouterDowngradeThreshold(otInstance *aInstance)
+1 -1
View File
@@ -1655,7 +1655,7 @@ uint32_t Mle::Reattach(void)
}
#if OPENTHREAD_FTD
if (IsFullThreadDevice() && BecomeLeader(/* aCheckWeight */ false) == kErrorNone)
if (IsFullThreadDevice() && BecomeLeader(kIgnoreLeaderWeight) == kErrorNone)
{
ExitNow();
}
+19 -14
View File
@@ -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
/**
+2 -2
View File
@@ -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);
}