mirror of
https://github.com/espressif/openthread.git
synced 2026-07-28 22:57:47 +00:00
[mle] helper method appending TLV Request TLV (#8001)
This commit contains smaller enhancements in MLE: - We use `static const` to define array of TLVs to request. - Rename the const arrays to `kTlvs`. - Adds template `AppendTlvRequestTlv()`. - Adds template `SendDataRequest()`.
This commit is contained in:
+17
-13
@@ -1731,9 +1731,10 @@ void Mle::RequestShorterChildIdRequest(void)
|
||||
|
||||
Error Mle::SendChildIdRequest(void)
|
||||
{
|
||||
static const uint8_t kTlvs[] = {Tlv::kAddress16, Tlv::kNetworkData, Tlv::kRoute};
|
||||
|
||||
Error error = kErrorNone;
|
||||
uint8_t tlvs[] = {Tlv::kAddress16, Tlv::kNetworkData, Tlv::kRoute};
|
||||
uint8_t tlvsLen = sizeof(tlvs);
|
||||
uint8_t tlvsLen = sizeof(kTlvs);
|
||||
TxMessage * message = nullptr;
|
||||
Ip6::Address destination;
|
||||
|
||||
@@ -1771,7 +1772,7 @@ Error Mle::SendChildIdRequest(void)
|
||||
tlvsLen -= 1;
|
||||
}
|
||||
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(tlvs, tlvsLen));
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(kTlvs, tlvsLen));
|
||||
SuccessOrExit(error = message->AppendActiveTimestampTlv());
|
||||
SuccessOrExit(error = message->AppendPendingTimestampTlv());
|
||||
|
||||
@@ -1921,14 +1922,15 @@ void Mle::HandleMessageTransmissionTimer(void)
|
||||
case kChildUpdateRequestNone:
|
||||
if (mDataRequestState == kDataRequestActive)
|
||||
{
|
||||
static const uint8_t tlvs[] = {Tlv::kNetworkData};
|
||||
Ip6::Address destination;
|
||||
static const uint8_t kTlvs[] = {Tlv::kNetworkData};
|
||||
|
||||
Ip6::Address destination;
|
||||
|
||||
VerifyOrExit(mDataRequestAttempts < kMaxChildKeepAliveAttempts, IgnoreError(BecomeDetached()));
|
||||
|
||||
destination.SetToLinkLocalAddress(mParent.GetExtAddress());
|
||||
|
||||
if (SendDataRequest(destination, tlvs, sizeof(tlvs), 0) == kErrorNone)
|
||||
if (SendDataRequest(destination, kTlvs) == kErrorNone)
|
||||
{
|
||||
mDataRequestAttempts++;
|
||||
}
|
||||
@@ -2673,10 +2675,11 @@ exit:
|
||||
|
||||
void Mle::HandleAdvertisement(RxInfo &aRxInfo)
|
||||
{
|
||||
static const uint8_t kTlvs[] = {Tlv::kNetworkData};
|
||||
|
||||
Error error = kErrorNone;
|
||||
uint16_t sourceAddress;
|
||||
LeaderData leaderData;
|
||||
uint8_t tlvs[] = {Tlv::kNetworkData};
|
||||
uint16_t delay;
|
||||
|
||||
// Source Address
|
||||
@@ -2748,7 +2751,7 @@ void Mle::HandleAdvertisement(RxInfo &aRxInfo)
|
||||
if (mRetrieveNewNetworkData || IsNetworkDataNewer(leaderData))
|
||||
{
|
||||
delay = Random::NonCrypto::GetUint16InRange(0, kMleMaxResponseDelay);
|
||||
IgnoreError(SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), tlvs, sizeof(tlvs), delay));
|
||||
IgnoreError(SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), kTlvs, delay));
|
||||
}
|
||||
|
||||
aRxInfo.mClass = RxInfo::kPeerMessage;
|
||||
@@ -2938,8 +2941,9 @@ exit:
|
||||
|
||||
if (dataRequest)
|
||||
{
|
||||
static const uint8_t tlvs[] = {Tlv::kNetworkData};
|
||||
uint16_t delay;
|
||||
static const uint8_t kTlvs[] = {Tlv::kNetworkData};
|
||||
|
||||
uint16_t delay;
|
||||
|
||||
if (aRxInfo.mMessageInfo.GetSockAddr().IsMulticast())
|
||||
{
|
||||
@@ -2953,7 +2957,7 @@ exit:
|
||||
delay = 10;
|
||||
}
|
||||
|
||||
IgnoreError(SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), tlvs, sizeof(tlvs), delay));
|
||||
IgnoreError(SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), kTlvs, delay));
|
||||
}
|
||||
else if (error == kErrorNone)
|
||||
{
|
||||
@@ -3420,8 +3424,8 @@ void Mle::HandleChildUpdateRequest(RxInfo &aRxInfo)
|
||||
uint16_t sourceAddress;
|
||||
Challenge challenge;
|
||||
RequestedTlvs requestedTlvs;
|
||||
uint8_t tlvs[kMaxResponseTlvs] = {};
|
||||
uint8_t numTlvs = 0;
|
||||
uint8_t tlvs[kMaxResponseTlvs];
|
||||
uint8_t numTlvs = 0;
|
||||
|
||||
// Source Address
|
||||
SuccessOrExit(error = Tlv::Find<SourceAddressTlv>(aRxInfo.mMessage, sourceAddress));
|
||||
|
||||
+37
-2
@@ -1078,6 +1078,22 @@ protected:
|
||||
*/
|
||||
Error AppendTlvRequestTlv(const uint8_t *aTlvs, uint8_t aTlvsLength);
|
||||
|
||||
/**
|
||||
* This method appends a TLV Request TLV to the message.
|
||||
*
|
||||
* @tparam kArrayLength The TLV array length.
|
||||
*
|
||||
* @param[in] aTlvArray A reference to an array of TLV types of @p kArrayLength length.
|
||||
*
|
||||
* @retval kErrorNone Successfully appended the TLV Request TLV.
|
||||
* @retval kErrorNoBufs Insufficient buffers available to append the TLV Request TLV.
|
||||
*
|
||||
*/
|
||||
template <uint8_t kArrayLength> Error AppendTlvRequestTlv(const uint8_t (&aTlvArray)[kArrayLength])
|
||||
{
|
||||
return AppendTlvRequestTlv(aTlvArray, kArrayLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method appends a Leader Data TLV to the message.
|
||||
*
|
||||
@@ -1486,8 +1502,27 @@ protected:
|
||||
const uint8_t * aTlvs,
|
||||
uint8_t aTlvsLength,
|
||||
uint16_t aDelay,
|
||||
const uint8_t * aExtraTlvs = nullptr,
|
||||
uint8_t aExtraTlvsLength = 0);
|
||||
const uint8_t * aExtraTlvs,
|
||||
uint8_t aExtraTlvsLength);
|
||||
|
||||
/**
|
||||
* This method generates an MLE Data Request message.
|
||||
*
|
||||
* @tparam kArrayLength The TLV array length.
|
||||
*
|
||||
* @param[in] aDestination A reference to the IPv6 address of the destination.
|
||||
* @param[in] aTlvs An array of requested TLVs.
|
||||
* @param[in] aDelay Delay in milliseconds before the Data Request message is sent.
|
||||
*
|
||||
* @retval kErrorNone Successfully generated an MLE Data Request message.
|
||||
* @retval kErrorNoBufs Insufficient buffers to generate the MLE Data Request message.
|
||||
*
|
||||
*/
|
||||
template <uint8_t kArrayLength>
|
||||
Error SendDataRequest(const Ip6::Address &aDestination, const uint8_t (&aTlvs)[kArrayLength], uint16_t aDelay = 0)
|
||||
{
|
||||
return SendDataRequest(aDestination, aTlvs, kArrayLength, aDelay, nullptr, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method generates an MLE Child Update Request message.
|
||||
|
||||
@@ -543,12 +543,13 @@ exit:
|
||||
|
||||
Error MleRouter::SendLinkRequest(Neighbor *aNeighbor)
|
||||
{
|
||||
static const uint8_t detachedTlvs[] = {Tlv::kAddress16, Tlv::kRoute};
|
||||
static const uint8_t routerTlvs[] = {Tlv::kLinkMargin};
|
||||
static const uint8_t validNeighborTlvs[] = {Tlv::kLinkMargin, Tlv::kRoute};
|
||||
Error error = kErrorNone;
|
||||
TxMessage * message = nullptr;
|
||||
Ip6::Address destination;
|
||||
static const uint8_t kDetachedTlvs[] = {Tlv::kAddress16, Tlv::kRoute};
|
||||
static const uint8_t kRouterTlvs[] = {Tlv::kLinkMargin};
|
||||
static const uint8_t kValidNeighborTlvs[] = {Tlv::kLinkMargin, Tlv::kRoute};
|
||||
|
||||
Error error = kErrorNone;
|
||||
TxMessage * message = nullptr;
|
||||
Ip6::Address destination;
|
||||
|
||||
VerifyOrExit(mLinkRequestDelay == 0 && mChallengeTimeout == 0);
|
||||
|
||||
@@ -564,7 +565,7 @@ Error MleRouter::SendLinkRequest(Neighbor *aNeighbor)
|
||||
OT_UNREACHABLE_CODE(break);
|
||||
|
||||
case kRoleDetached:
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(detachedTlvs, sizeof(detachedTlvs)));
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(kDetachedTlvs));
|
||||
break;
|
||||
|
||||
case kRoleChild:
|
||||
@@ -576,11 +577,11 @@ Error MleRouter::SendLinkRequest(Neighbor *aNeighbor)
|
||||
case kRoleLeader:
|
||||
if (aNeighbor == nullptr || !aNeighbor->IsStateValid())
|
||||
{
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(routerTlvs, sizeof(routerTlvs)));
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(kRouterTlvs));
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(validNeighborTlvs, sizeof(validNeighborTlvs)));
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(kValidNeighborTlvs));
|
||||
}
|
||||
|
||||
SuccessOrExit(error = message->AppendSourceAddressTlv());
|
||||
@@ -745,11 +746,12 @@ Error MleRouter::SendLinkAccept(const Ip6::MessageInfo &aMessageInfo,
|
||||
const RequestedTlvs & aRequestedTlvs,
|
||||
const Challenge & aChallenge)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
static const uint8_t routerTlvs[] = {Tlv::kLinkMargin};
|
||||
TxMessage * message;
|
||||
Command command;
|
||||
uint8_t linkMargin;
|
||||
static const uint8_t kRouterTlvs[] = {Tlv::kLinkMargin};
|
||||
|
||||
Error error = kErrorNone;
|
||||
TxMessage *message;
|
||||
Command command;
|
||||
uint8_t linkMargin;
|
||||
|
||||
command = (aNeighbor == nullptr || aNeighbor->IsStateValid()) ? kCommandLinkAccept : kCommandLinkAcceptAndRequest;
|
||||
|
||||
@@ -797,7 +799,7 @@ Error MleRouter::SendLinkAccept(const Ip6::MessageInfo &aMessageInfo,
|
||||
aNeighbor->GenerateChallenge();
|
||||
|
||||
SuccessOrExit(error = message->AppendChallengeTlv(aNeighbor->GetChallenge(), aNeighbor->GetChallengeSize()));
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(routerTlvs, sizeof(routerTlvs)));
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(kRouterTlvs));
|
||||
aNeighbor->SetLastHeard(TimerMilli::GetNow());
|
||||
aNeighbor->SetState(Neighbor::kStateLinkRequest);
|
||||
}
|
||||
@@ -844,7 +846,7 @@ void MleRouter::HandleLinkAcceptAndRequest(RxInfo &aRxInfo)
|
||||
|
||||
Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
|
||||
{
|
||||
static const uint8_t dataRequestTlvs[] = {Tlv::kNetworkData};
|
||||
static const uint8_t kDataRequestTlvs[] = {Tlv::kNetworkData};
|
||||
|
||||
Error error = kErrorNone;
|
||||
Router * router;
|
||||
@@ -954,7 +956,7 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
|
||||
}
|
||||
|
||||
mRetrieveNewNetworkData = true;
|
||||
IgnoreError(SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), dataRequestTlvs, sizeof(dataRequestTlvs), 0));
|
||||
IgnoreError(SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), kDataRequestTlvs));
|
||||
|
||||
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
|
||||
Get<TimeSync>().HandleTimeSyncMessage(aRxInfo.mMessage);
|
||||
@@ -977,8 +979,7 @@ Error MleRouter::HandleLinkAccept(RxInfo &aRxInfo, bool aRequest)
|
||||
SerialNumber::IsGreater(leaderData.GetDataVersion(NetworkData::kFullSet),
|
||||
Get<NetworkData::Leader>().GetVersion(NetworkData::kFullSet)))
|
||||
{
|
||||
IgnoreError(
|
||||
SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), dataRequestTlvs, sizeof(dataRequestTlvs), 0));
|
||||
IgnoreError(SendDataRequest(aRxInfo.mMessageInfo.GetPeerAddr(), kDataRequestTlvs));
|
||||
}
|
||||
|
||||
// Route (optional)
|
||||
@@ -2917,16 +2918,17 @@ exit:
|
||||
|
||||
void MleRouter::HandleNetworkDataUpdateRouter(void)
|
||||
{
|
||||
static const uint8_t tlvs[] = {Tlv::kNetworkData};
|
||||
Ip6::Address destination;
|
||||
uint16_t delay;
|
||||
static const uint8_t kTlvs[] = {Tlv::kNetworkData};
|
||||
|
||||
Ip6::Address destination;
|
||||
uint16_t delay;
|
||||
|
||||
VerifyOrExit(IsRouterOrLeader());
|
||||
|
||||
destination.SetToLinkLocalAllNodesMulticast();
|
||||
|
||||
delay = IsLeader() ? 0 : Random::NonCrypto::GetUint16InRange(0, kUnsolicitedDataResponseJitter);
|
||||
SendDataResponse(destination, tlvs, sizeof(tlvs), delay);
|
||||
SendDataResponse(destination, kTlvs, sizeof(kTlvs), delay);
|
||||
|
||||
SynchronizeChildNetworkData();
|
||||
|
||||
@@ -3256,10 +3258,11 @@ exit:
|
||||
|
||||
Error MleRouter::SendChildUpdateRequest(Child &aChild)
|
||||
{
|
||||
static const uint8_t tlvs[] = {Tlv::kTimeout, Tlv::kAddressRegistration};
|
||||
Error error = kErrorNone;
|
||||
Ip6::Address destination;
|
||||
TxMessage * message = nullptr;
|
||||
static const uint8_t kTlvs[] = {Tlv::kTimeout, Tlv::kAddressRegistration};
|
||||
|
||||
Error error = kErrorNone;
|
||||
Ip6::Address destination;
|
||||
TxMessage * message = nullptr;
|
||||
|
||||
if (!aChild.IsRxOnWhenIdle())
|
||||
{
|
||||
@@ -3292,7 +3295,7 @@ Error MleRouter::SendChildUpdateRequest(Child &aChild)
|
||||
|
||||
if (!aChild.IsStateValid())
|
||||
{
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(tlvs, sizeof(tlvs)));
|
||||
SuccessOrExit(error = message->AppendTlvRequestTlv(kTlvs));
|
||||
aChild.GenerateChallenge();
|
||||
SuccessOrExit(error = message->AppendChallengeTlv(aChild.GetChallenge(), aChild.GetChallengeSize()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user