mirror of
https://github.com/espressif/openthread.git
synced 2026-07-27 14:27:47 +00:00
[mle] add methods for leader and partition ID selection (#11820)
This commit extracts the logic for selecting a leader ID and a partition ID out of the `BecomeLeader()` method into two new private helper methods: `SelectLeaderId()` and `SelectPartitionId()`. This is a pure refactoring that does not alter behavior. It improves the readability and maintainability of the `BecomeLeader()` method by simplifying its implementation.
This commit is contained in:
@@ -2129,6 +2129,8 @@ private:
|
||||
#if OPENTHREAD_FTD
|
||||
void SetAlternateRloc16(uint16_t aRloc16);
|
||||
void ClearAlternateRloc16(void);
|
||||
uint8_t SelectLeaderId(void) const;
|
||||
uint32_t SelectPartitionId(void) const;
|
||||
void HandleDetachStart(void);
|
||||
void HandleChildStart(AttachMode aMode);
|
||||
void HandleSecurityPolicyChanged(void);
|
||||
|
||||
+39
-21
@@ -219,10 +219,9 @@ exit:
|
||||
|
||||
Error Mle::BecomeLeader(LeaderWeightCheck aMode)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
Router *router;
|
||||
uint32_t partitionId;
|
||||
uint8_t leaderId;
|
||||
Error error = kErrorNone;
|
||||
Router *router;
|
||||
uint8_t leaderId;
|
||||
|
||||
#if OPENTHREAD_CONFIG_OPERATIONAL_DATASET_AUTO_INIT
|
||||
VerifyOrExit(!Get<MeshCoP::ActiveDatasetManager>().IsPartiallyComplete(), error = kErrorInvalidState);
|
||||
@@ -240,24 +239,9 @@ Error Mle::BecomeLeader(LeaderWeightCheck aMode)
|
||||
|
||||
mRouterTable.Clear();
|
||||
|
||||
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
|
||||
{
|
||||
uint8_t minId;
|
||||
uint8_t maxId;
|
||||
leaderId = SelectLeaderId();
|
||||
|
||||
mRouterTable.GetRouterIdRange(minId, maxId);
|
||||
partitionId = mPreferredLeaderPartitionId ? mPreferredLeaderPartitionId : Random::NonCrypto::GetUint32();
|
||||
leaderId = (IsRouterIdValid(mPreviousRouterId) && minId <= mPreviousRouterId && mPreviousRouterId <= maxId)
|
||||
? mPreviousRouterId
|
||||
: Random::NonCrypto::GetUint8InRange(minId, maxId + 1);
|
||||
}
|
||||
#else
|
||||
partitionId = Random::NonCrypto::GetUint32();
|
||||
leaderId = IsRouterIdValid(mPreviousRouterId) ? mPreviousRouterId
|
||||
: Random::NonCrypto::GetUint8InRange(0, kMaxRouterId + 1);
|
||||
#endif
|
||||
|
||||
SetLeaderData(partitionId, mLeaderWeight, leaderId);
|
||||
SetLeaderData(SelectPartitionId(), mLeaderWeight, leaderId);
|
||||
|
||||
router = mRouterTable.Allocate(leaderId);
|
||||
OT_ASSERT(router != nullptr);
|
||||
@@ -274,6 +258,40 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
uint8_t Mle::SelectLeaderId(void) const
|
||||
{
|
||||
uint8_t minId;
|
||||
uint8_t maxId;
|
||||
|
||||
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
|
||||
mRouterTable.GetRouterIdRange(minId, maxId);
|
||||
#else
|
||||
minId = 0;
|
||||
maxId = kMaxRouterId;
|
||||
#endif
|
||||
|
||||
return IsValueInRange(mPreviousRouterId, minId, maxId) ? mPreviousRouterId
|
||||
: Random::NonCrypto::GetUint8InRange(minId, maxId + 1);
|
||||
}
|
||||
|
||||
uint32_t Mle::SelectPartitionId(void) const
|
||||
{
|
||||
uint32_t partitionId;
|
||||
|
||||
#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
|
||||
if (mPreferredLeaderPartitionId != 0)
|
||||
{
|
||||
partitionId = mPreferredLeaderPartitionId;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
partitionId = Random::NonCrypto::GetUint32();
|
||||
}
|
||||
|
||||
return partitionId;
|
||||
}
|
||||
|
||||
void Mle::StopLeader(void)
|
||||
{
|
||||
StopAdvertiseTrickleTimer();
|
||||
|
||||
Reference in New Issue
Block a user