[mle] add InitNeighbor() method (#8836)

This commit adds a helper method `Mle::InitNeighbor()` which performs
common initialization of a `Neighbor`  using `RxInfo`, e.g., setting
the Extended MAC address from the sender's IPv6 address IID, updating
the `LinkInfo`, and setting `LastHeard` time. This method is used
from different methods in `Mle` and `MleRouter where a new neighbor
(router, child, or parent candidate) is initialized.
This commit is contained in:
Abtin Keshavarzian
2023-03-07 10:09:02 -08:00
committed by GitHub
parent d2d9aff012
commit d756a2275b
4 changed files with 60 additions and 62 deletions
+26 -20
View File
@@ -1122,6 +1122,15 @@ exit:
}
#endif
void Mle::InitNeighbor(Neighbor &aNeighbor, const RxInfo &aRxInfo)
{
aRxInfo.mMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(aNeighbor.GetExtAddress());
aNeighbor.GetLinkInfo().Clear();
aNeighbor.GetLinkInfo().AddRss(aRxInfo.mMessageInfo.GetThreadLinkInfo()->GetRss());
aNeighbor.ResetLinkFailures();
aNeighbor.SetLastHeard(TimerMilli::GetNow());
}
void Mle::HandleNotifierEvents(Events aEvents)
{
VerifyOrExit(!IsDisabled());
@@ -3085,20 +3094,20 @@ exit:
void Mle::HandleParentResponse(RxInfo &aRxInfo)
{
Error error = kErrorNone;
const ThreadLinkInfo *linkInfo = aRxInfo.mMessageInfo.GetThreadLinkInfo();
Challenge response;
uint16_t version;
uint16_t sourceAddress;
LeaderData leaderData;
uint8_t linkMarginFromTlv;
uint8_t linkMargin;
LinkQuality linkQuality;
ConnectivityTlv connectivityTlv;
uint32_t linkFrameCounter;
uint32_t mleFrameCounter;
Mac::ExtAddress extAddress;
Mac::CslAccuracy cslAccuracy;
Error error = kErrorNone;
int8_t rss = aRxInfo.mMessageInfo.GetThreadLinkInfo()->GetRss();
Challenge response;
uint16_t version;
uint16_t sourceAddress;
LeaderData leaderData;
uint8_t linkMarginFromTlv;
uint8_t linkMargin;
LinkQuality linkQuality;
ConnectivityTlv connectivityTlv;
uint32_t linkFrameCounter;
uint32_t mleFrameCounter;
Mac::ExtAddress extAddress;
Mac::CslAccuracy cslAccuracy;
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
TimeParameterTlv timeParameterTlv;
#endif
@@ -3129,7 +3138,7 @@ void Mle::HandleParentResponse(RxInfo &aRxInfo)
// Link Margin
SuccessOrExit(error = Tlv::Find<LinkMarginTlv>(aRxInfo.mMessage, linkMarginFromTlv));
linkMargin = Get<Mac::Mac>().ComputeLinkMargin(linkInfo->GetRss());
linkMargin = Get<Mac::Mac>().ComputeLinkMargin(rss);
if (linkMargin > linkMarginFromTlv)
{
@@ -3165,7 +3174,7 @@ void Mle::HandleParentResponse(RxInfo &aRxInfo)
parentinfo.mExtAddr = extAddress;
parentinfo.mRloc16 = sourceAddress;
parentinfo.mRssi = linkInfo->GetRss();
parentinfo.mRssi = rss;
parentinfo.mPriority = connectivityTlv.GetParentPriority();
parentinfo.mLinkQuality3 = connectivityTlv.GetLinkQuality3();
parentinfo.mLinkQuality2 = connectivityTlv.GetLinkQuality2();
@@ -3265,7 +3274,7 @@ void Mle::HandleParentResponse(RxInfo &aRxInfo)
// Challenge
SuccessOrExit(error = aRxInfo.mMessage.ReadChallengeTlv(mParentCandidate.mChallenge));
mParentCandidate.SetExtAddress(extAddress);
InitNeighbor(mParentCandidate, aRxInfo);
mParentCandidate.SetRloc16(sourceAddress);
mParentCandidate.GetLinkFrameCounters().SetAll(linkFrameCounter);
mParentCandidate.SetLinkAckFrameCounter(linkFrameCounter);
@@ -3273,9 +3282,6 @@ void Mle::HandleParentResponse(RxInfo &aRxInfo)
mParentCandidate.SetVersion(version);
mParentCandidate.SetDeviceMode(DeviceMode(DeviceMode::kModeFullThreadDevice | DeviceMode::kModeRxOnWhenIdle |
DeviceMode::kModeFullNetworkData));
mParentCandidate.GetLinkInfo().Clear();
mParentCandidate.GetLinkInfo().AddRss(linkInfo->GetRss());
mParentCandidate.ResetLinkFailures();
mParentCandidate.SetLinkQualityOut(LinkQualityForLinkMargin(linkMarginFromTlv));
mParentCandidate.SetState(Neighbor::kStateParentResponse);
mParentCandidate.SetKeySequence(aRxInfo.mKeySequence);
+16
View File
@@ -1461,6 +1461,22 @@ protected:
*/
void SetAttachState(AttachState aState);
/**
* This method initializes a given @p aNeighbor with information from @p aRxInfo.
*
* This method updates the following properties on @p aNeighbor from @p aRxInfo:
*
* - Sets the Extended MAC address from `MessageInfo` peer IPv6 address IID.
* - Clears the `GetLinkInfo()` and adds RSS from the `GetThreadLinkInfo()`.
* - Resets the link failure counter (`ResetLinkFailures()`).
* - Sets the "last heard" time to now (`SetLastHeard()`).
*
* @param[in,out] aNeighbor The `Neighbor` to initialize.
* @param[in] aRxInfo The `RxtInfo` to use for initialization.
*
*/
void InitNeighbor(Neighbor &aNeighbor, const RxInfo &aRxInfo);
/**
* This method clears the parent candidate.
*
+13 -39
View File
@@ -632,25 +632,20 @@ void MleRouter::HandleLinkRequest(RxInfo &aRxInfo)
case kErrorNone:
if (IsActiveRouter(sourceAddress))
{
Mac::ExtAddress extAddr;
aRxInfo.mMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(extAddr);
neighbor = mRouterTable.FindRouterByRloc16(sourceAddress);
VerifyOrExit(neighbor != nullptr, error = kErrorParse);
VerifyOrExit(!neighbor->IsStateLinkRequest(), error = kErrorAlready);
if (!neighbor->IsStateValid())
{
neighbor->SetExtAddress(extAddr);
neighbor->GetLinkInfo().Clear();
neighbor->GetLinkInfo().AddRss(aRxInfo.mMessageInfo.GetThreadLinkInfo()->GetRss());
neighbor->ResetLinkFailures();
neighbor->SetLastHeard(TimerMilli::GetNow());
InitNeighbor(*neighbor, aRxInfo);
neighbor->SetState(Neighbor::kStateLinkRequest);
}
else
{
Mac::ExtAddress extAddr;
aRxInfo.mMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(extAddr);
VerifyOrExit(neighbor->GetExtAddress() == extAddr);
}
}
@@ -811,7 +806,6 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
Error error = kErrorNone;
Router *router;
Neighbor::State neighborState;
Mac::ExtAddress extAddr;
uint16_t version;
Challenge response;
uint16_t sourceAddress;
@@ -971,20 +965,15 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
}
// finish link synchronization
aRxInfo.mMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(extAddr);
router->SetExtAddress(extAddr);
InitNeighbor(*router, aRxInfo);
router->SetRloc16(sourceAddress);
router->GetLinkFrameCounters().SetAll(linkFrameCounter);
router->SetLinkAckFrameCounter(linkFrameCounter);
router->SetMleFrameCounter(mleFrameCounter);
router->SetLastHeard(TimerMilli::GetNow());
router->SetVersion(version);
router->SetDeviceMode(DeviceMode(DeviceMode::kModeFullThreadDevice | DeviceMode::kModeRxOnWhenIdle |
DeviceMode::kModeFullNetworkData));
router->GetLinkInfo().Clear();
router->GetLinkInfo().AddRss(aRxInfo.mMessageInfo.GetThreadLinkInfo()->GetRss());
router->SetLinkQualityOut(LinkQualityForLinkMargin(linkMargin));
router->ResetLinkFailures();
router->SetState(Neighbor::kStateValid);
router->SetKeySequence(aRxInfo.mKeySequence);
@@ -1121,15 +1110,11 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo, uint16_t aSourceAddress, c
// - `aSourceAdress` is the read value from `SourceAddressTlv`.
// - `aLeaderData` is the read value from `LeaderDataTlv`.
Error error = kErrorNone;
const ThreadLinkInfo *linkInfo = aRxInfo.mMessageInfo.GetThreadLinkInfo();
uint8_t linkMargin = Get<Mac::Mac>().ComputeLinkMargin(linkInfo->GetRss());
Mac::ExtAddress extAddr;
RouteTlv routeTlv;
Router *router;
uint8_t routerId;
aRxInfo.mMessageInfo.GetPeerAddr().GetIid().ConvertToExtAddress(extAddr);
Error error = kErrorNone;
uint8_t linkMargin = Get<Mac::Mac>().ComputeLinkMargin(aRxInfo.mMessageInfo.GetThreadLinkInfo()->GetRss());
RouteTlv routeTlv;
Router *router;
uint8_t routerId;
if (Tlv::FindTlv(aRxInfo.mMessage, routeTlv) == kErrorNone)
{
@@ -1267,11 +1252,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo, uint16_t aSourceAddress, c
if (!router->IsStateValid() && !router->IsStateLinkRequest() &&
(mRouterTable.GetNeighborCount() < mChildRouterLinks))
{
router->SetExtAddress(extAddr);
router->GetLinkInfo().Clear();
router->GetLinkInfo().AddRss(linkInfo->GetRss());
router->ResetLinkFailures();
router->SetLastHeard(TimerMilli::GetNow());
InitNeighbor(*router, aRxInfo);
router->SetState(Neighbor::kStateLinkRequest);
IgnoreError(SendLinkRequest(router));
ExitNow(error = kErrorNoRoute);
@@ -1314,11 +1295,7 @@ Error MleRouter::HandleAdvertisement(RxInfo &aRxInfo, uint16_t aSourceAddress, c
if (!router->IsStateValid() && !router->IsStateLinkRequest() && (mChallengeTimeout == 0) &&
(linkMargin >= kLinkRequestMinMargin))
{
router->SetExtAddress(extAddr);
router->GetLinkInfo().Clear();
router->GetLinkInfo().AddRss(linkInfo->GetRss());
router->ResetLinkFailures();
router->SetLastHeard(TimerMilli::GetNow());
InitNeighbor(*router, aRxInfo);
router->SetState(Neighbor::kStateLinkRequest);
IgnoreError(SendLinkRequest(router));
ExitNow(error = kErrorNoRoute);
@@ -1410,10 +1387,7 @@ void MleRouter::HandleParentRequest(RxInfo &aRxInfo)
VerifyOrExit((child = mChildTable.GetNewChild()) != nullptr, error = kErrorNoBufs);
// MAC Address
child->SetExtAddress(extAddr);
child->GetLinkInfo().Clear();
child->GetLinkInfo().AddRss(aRxInfo.mMessageInfo.GetThreadLinkInfo()->GetRss());
child->ResetLinkFailures();
InitNeighbor(*child, aRxInfo);
child->SetState(Neighbor::kStateParentRequest);
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
child->SetTimeSyncEnabled(Tlv::Find<TimeRequestTlv>(aRxInfo.mMessage, nullptr, 0) == kErrorNone);
+5 -3
View File
@@ -364,10 +364,12 @@ public:
NetworkData::Type GetNetworkDataType(void) const { return GetDeviceMode().GetNetworkDataType(); }
/**
* This method sets all bytes of the Extended Address to zero.
* This method returns the Extended Address.
*
* @returns A const reference to the Extended Address.
*
*/
void ClearExtAddress(void) { memset(&mMacAddr, 0, sizeof(mMacAddr)); }
const Mac::ExtAddress &GetExtAddress(void) const { return mMacAddr; }
/**
* This method returns the Extended Address.
@@ -375,7 +377,7 @@ public:
* @returns A reference to the Extended Address.
*
*/
const Mac::ExtAddress &GetExtAddress(void) const { return mMacAddr; }
Mac::ExtAddress &GetExtAddress(void) { return mMacAddr; }
/**
* This method sets the Extended Address.