[mle] define ParentSearch class (#8022)

This commit adds `Mle::ParentSearch` class which encapsulates all
definitions (variables, constants, and methods) related to "Parent
Search" feature.
This commit is contained in:
Abtin Keshavarzian
2022-08-16 15:55:09 -07:00
committed by GitHub
parent 4c8ae758fd
commit 47e5277659
2 changed files with 73 additions and 58 deletions
+36 -39
View File
@@ -109,11 +109,7 @@ Mle::Mle(Instance &aInstance)
#endif
, mPreviousParentRloc(Mac::kShortAddrInvalid)
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
, mParentSearchIsInBackoff(false)
, mParentSearchBackoffWasCanceled(false)
, mParentSearchRecentlyDetached(false)
, mParentSearchBackoffCancelTime(0)
, mParentSearchTimer(aInstance, Mle::HandleParentSearchTimer)
, mParentSearch(aInstance)
#endif
, mAnnounceChannel(0)
, mAlternateChannel(0)
@@ -169,7 +165,7 @@ Error Mle::Enable(void)
SuccessOrExit(error = mSocket.Bind(kUdpPort));
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
StartParentSearchTimer();
mParentSearch.StartTimer();
#endif
exit:
return error;
@@ -499,7 +495,7 @@ Error Mle::BecomeDetached(void)
}
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
mParentSearchRecentlyDetached = true;
mParentSearch.SetRecentlyDetached();
#endif
SetStateDetached();
@@ -720,7 +716,7 @@ void Mle::SetStateChild(uint16_t aRloc16)
InformPreviousChannel();
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
UpdateParentSearchState();
mParentSearch.UpdateState();
#endif
if ((mPreviousParentRloc != Mac::kShortAddrInvalid) && (mPreviousParentRloc != mParent.GetRloc16()))
@@ -3909,70 +3905,70 @@ exit:
#endif // OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
void Mle::HandleParentSearchTimer(Timer &aTimer)
void Mle::ParentSearch::HandleTimer(Timer &aTimer)
{
aTimer.Get<Mle>().HandleParentSearchTimer();
aTimer.Get<Mle>().mParentSearch.HandleTimer();
}
void Mle::HandleParentSearchTimer(void)
void Mle::ParentSearch::HandleTimer(void)
{
int8_t parentRss;
LogInfo("PeriodicParentSearch: %s interval passed", mParentSearchIsInBackoff ? "Backoff" : "Check");
LogInfo("PeriodicParentSearch: %s interval passed", mIsInBackoff ? "Backoff" : "Check");
if (mParentSearchBackoffWasCanceled)
if (mBackoffWasCanceled)
{
// Backoff can be canceled if the device switches to a new parent.
// from `UpdateParentSearchState()`. We want to limit this to happen
// only once within a backoff interval.
if (TimerMilli::GetNow() - mParentSearchBackoffCancelTime >= kParentSearchBackoffInterval)
if (TimerMilli::GetNow() - mBackoffCancelTime >= kBackoffInterval)
{
mParentSearchBackoffWasCanceled = false;
mBackoffWasCanceled = false;
LogInfo("PeriodicParentSearch: Backoff cancellation is allowed on parent switch");
}
}
mParentSearchIsInBackoff = false;
mIsInBackoff = false;
VerifyOrExit(IsChild());
VerifyOrExit(Get<Mle>().IsChild());
parentRss = GetParent().GetLinkInfo().GetAverageRss();
parentRss = Get<Mle>().GetParent().GetLinkInfo().GetAverageRss();
LogInfo("PeriodicParentSearch: Parent RSS %d", parentRss);
VerifyOrExit(parentRss != OT_RADIO_RSSI_INVALID);
if (parentRss < kParentSearchRssThreadhold)
if (parentRss < kRssThreadhold)
{
LogInfo("PeriodicParentSearch: Parent RSS less than %d, searching for new parents", kParentSearchRssThreadhold);
mParentSearchIsInBackoff = true;
Attach(kBetterParent);
LogInfo("PeriodicParentSearch: Parent RSS less than %d, searching for new parents", kRssThreadhold);
mIsInBackoff = true;
Get<Mle>().Attach(kBetterParent);
}
exit:
StartParentSearchTimer();
StartTimer();
}
void Mle::StartParentSearchTimer(void)
void Mle::ParentSearch::StartTimer(void)
{
uint32_t interval;
interval = Random::NonCrypto::GetUint32InRange(0, kParentSearchJitterInterval);
interval = Random::NonCrypto::GetUint32InRange(0, kJitterInterval);
if (mParentSearchIsInBackoff)
if (mIsInBackoff)
{
interval += kParentSearchBackoffInterval;
interval += kBackoffInterval;
}
else
{
interval += kParentSearchCheckInterval;
interval += kCheckInterval;
}
mParentSearchTimer.Start(interval);
mTimer.Start(interval);
LogInfo("PeriodicParentSearch: (Re)starting timer for %s interval", mParentSearchIsInBackoff ? "backoff" : "check");
LogInfo("PeriodicParentSearch: (Re)starting timer for %s interval", mIsInBackoff ? "backoff" : "check");
}
void Mle::UpdateParentSearchState(void)
void Mle::ParentSearch::UpdateState(void)
{
#if OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH
@@ -3991,24 +3987,25 @@ void Mle::UpdateParentSearchState(void)
// the chance to switch back to the original (and possibly
// preferred) parent more quickly.
if (mParentSearchIsInBackoff && !mParentSearchBackoffWasCanceled && mParentSearchRecentlyDetached)
if (mIsInBackoff && !mBackoffWasCanceled && mRecentlyDetached)
{
if ((mPreviousParentRloc != Mac::kShortAddrInvalid) && (mPreviousParentRloc != mParent.GetRloc16()))
if ((Get<Mle>().mPreviousParentRloc != Mac::kShortAddrInvalid) &&
(Get<Mle>().mPreviousParentRloc != Get<Mle>().mParent.GetRloc16()))
{
mParentSearchIsInBackoff = false;
mParentSearchBackoffWasCanceled = true;
mParentSearchBackoffCancelTime = TimerMilli::GetNow();
mIsInBackoff = false;
mBackoffWasCanceled = true;
mBackoffCancelTime = TimerMilli::GetNow();
LogInfo("PeriodicParentSearch: Canceling backoff on switching to a new parent");
}
}
#endif // OPENTHREAD_CONFIG_MLE_INFORM_PREVIOUS_PARENT_ON_REATTACH
mParentSearchRecentlyDetached = false;
mRecentlyDetached = false;
if (!mParentSearchIsInBackoff)
if (!mIsInBackoff)
{
StartParentSearchTimer();
StartTimer();
}
}
#endif // OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
+37 -19
View File
@@ -1794,13 +1794,6 @@ private:
static constexpr uint8_t kMleHopLimit = 255;
static constexpr uint8_t kMleSecurityTagSize = 4; // Security tag size in bytes.
// Parameters related to "periodic parent search" feature (CONFIG_ENABLE_PERIODIC_PARENT_SEARCH).
// All timer intervals are converted to milliseconds.
static constexpr uint32_t kParentSearchCheckInterval = (OPENTHREAD_CONFIG_PARENT_SEARCH_CHECK_INTERVAL * 1000u);
static constexpr uint32_t kParentSearchBackoffInterval = (OPENTHREAD_CONFIG_PARENT_SEARCH_BACKOFF_INTERVAL * 1000u);
static constexpr uint32_t kParentSearchJitterInterval = (15 * 1000u);
static constexpr int8_t kParentSearchRssThreadhold = OPENTHREAD_CONFIG_PARENT_SEARCH_RSS_THRESHOLD;
// Parameters for "attach backoff" feature (CONFIG_ENABLE_ATTACH_BACKOFF) - Intervals are in milliseconds.
static constexpr uint32_t kAttachBackoffMinInterval = OPENTHREAD_CONFIG_MLE_ATTACH_BACKOFF_MINIMUM_INTERVAL;
static constexpr uint32_t kAttachBackoffMaxInterval = OPENTHREAD_CONFIG_MLE_ATTACH_BACKOFF_MAXIMUM_INTERVAL;
@@ -1917,6 +1910,42 @@ private:
};
#endif
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
class ParentSearch : public InstanceLocator
{
public:
explicit ParentSearch(Instance &aInstance)
: InstanceLocator(aInstance)
, mIsInBackoff(false)
, mBackoffWasCanceled(false)
, mRecentlyDetached(false)
, mBackoffCancelTime(0)
, mTimer(aInstance, HandleTimer)
{
}
void StartTimer(void);
void UpdateState(void);
void SetRecentlyDetached(void) { mRecentlyDetached = true; }
private:
// All timer intervals are converted to milliseconds.
static constexpr uint32_t kCheckInterval = (OPENTHREAD_CONFIG_PARENT_SEARCH_CHECK_INTERVAL * 1000u);
static constexpr uint32_t kBackoffInterval = (OPENTHREAD_CONFIG_PARENT_SEARCH_BACKOFF_INTERVAL * 1000u);
static constexpr uint32_t kJitterInterval = (15 * 1000u);
static constexpr int8_t kRssThreadhold = OPENTHREAD_CONFIG_PARENT_SEARCH_RSS_THRESHOLD;
static void HandleTimer(Timer &aTimer);
void HandleTimer(void);
bool mIsInBackoff : 1;
bool mBackoffWasCanceled : 1;
bool mRecentlyDetached : 1;
TimeMilli mBackoffCancelTime;
TimerMilli mTimer;
};
#endif // OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
Error Start(StartMode aMode);
void Stop(StopMode aMode);
void HandleNotifierEvents(Events aEvents);
@@ -2004,13 +2033,6 @@ private:
void InformPreviousParent(void);
#endif
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
static void HandleParentSearchTimer(Timer &aTimer);
void HandleParentSearchTimer(void);
void StartParentSearchTimer(void);
void UpdateParentSearchState(void);
#endif
#if OT_SHOULD_LOG_AT(OT_LOG_LEVEL_WARN)
static void LogError(MessageAction aAction, MessageType aType, Error aError);
static const char *MessageActionToString(MessageAction aAction);
@@ -2054,11 +2076,7 @@ private:
uint16_t mPreviousParentRloc;
#if OPENTHREAD_CONFIG_PARENT_SEARCH_ENABLE
bool mParentSearchIsInBackoff : 1;
bool mParentSearchBackoffWasCanceled : 1;
bool mParentSearchRecentlyDetached : 1;
TimeMilli mParentSearchBackoffCancelTime;
TimerMilli mParentSearchTimer;
ParentSearch mParentSearch;
#endif
uint8_t mAnnounceChannel;