From fe0037e67a2456f24c459e3b51bb92d4a74a6f08 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 11 Jun 2024 08:45:11 -0700 Subject: [PATCH] [mle] add `Mle::RxMessage::ReadAndSetNetworkDataTlv()` (#10358) This commit adds `ReadAndSetNetworkDataTlv()` to `Mle::RxMessage` to read the Network Data TLV from a received MLE message and set it in `NetworkData::Leader`. --- src/core/thread/mle.cpp | 43 ++++++++++++++++++++++++----------------- src/core/thread/mle.hpp | 1 + 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 838ff8ef2..b45d07529 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -2902,8 +2902,6 @@ Error Mle::HandleLeaderData(RxInfo &aRxInfo) bool saveActiveDataset = false; bool savePendingDataset = false; bool dataRequest = false; - uint16_t offset; - uint16_t length; SuccessOrExit(error = aRxInfo.mMessage.ReadLeaderDataTlv(leaderData)); @@ -2977,16 +2975,15 @@ Error Mle::HandleLeaderData(RxInfo &aRxInfo) ExitNow(error = kErrorParse); } - if (Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kNetworkData, offset, length) == kErrorNone) + switch (error = aRxInfo.mMessage.ReadAndSetNetworkDataTlv(leaderData)) { - error = Get().SetNetworkData(leaderData.GetDataVersion(NetworkData::kFullSet), - leaderData.GetDataVersion(NetworkData::kStableSubset), - GetNetworkDataType(), aRxInfo.mMessage, offset, length); - SuccessOrExit(error); - } - else - { - ExitNow(dataRequest = true); + case kErrorNone: + break; + case kErrorNotFound: + dataRequest = true; + OT_FALL_THROUGH; + default: + ExitNow(); } #if OPENTHREAD_FTD @@ -3308,8 +3305,6 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo) uint16_t sourceAddress; uint16_t shortAddress; MeshCoP::Timestamp timestamp; - uint16_t networkDataOffset; - uint16_t networkDataLength; SuccessOrExit(error = Tlv::Find(aRxInfo.mMessage, sourceAddress)); @@ -3324,8 +3319,7 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo) SuccessOrExit(error = aRxInfo.mMessage.ReadLeaderDataTlv(leaderData)); - SuccessOrExit( - error = Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kNetworkData, networkDataOffset, networkDataLength)); + VerifyOrExit(aRxInfo.mMessage.ContainsTlv(Tlv::kNetworkData)); switch (Tlv::Find(aRxInfo.mMessage, timestamp)) { @@ -3388,9 +3382,7 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo) mParent.SetRloc16(sourceAddress); - IgnoreError(Get().SetNetworkData( - leaderData.GetDataVersion(NetworkData::kFullSet), leaderData.GetDataVersion(NetworkData::kStableSubset), - GetNetworkDataType(), aRxInfo.mMessage, networkDataOffset, networkDataLength)); + IgnoreError(aRxInfo.mMessage.ReadAndSetNetworkDataTlv(leaderData)); SetStateChild(shortAddress); @@ -5064,6 +5056,21 @@ exit: return error; } +Error Mle::RxMessage::ReadAndSetNetworkDataTlv(const LeaderData &aLeaderData) const +{ + Error error; + uint16_t offset; + uint16_t length; + + SuccessOrExit(error = Tlv::FindTlvValueOffset(*this, Tlv::kNetworkData, offset, length)); + + error = Get().SetNetworkData(aLeaderData.GetDataVersion(NetworkData::kFullSet), + aLeaderData.GetDataVersion(NetworkData::kStableSubset), + Get().GetNetworkDataType(), *this, offset, length); +exit: + return error; +} + Error Mle::RxMessage::ReadAndSaveActiveDataset(const MeshCoP::Timestamp &aActiveTimestamp) const { return ReadAndSaveDataset(MeshCoP::Dataset::kActive, aActiveTimestamp); diff --git a/src/core/thread/mle.hpp b/src/core/thread/mle.hpp index 318648b5e..a13c5f594 100644 --- a/src/core/thread/mle.hpp +++ b/src/core/thread/mle.hpp @@ -1072,6 +1072,7 @@ private: Error ReadFrameCounterTlvs(uint32_t &aLinkFrameCounter, uint32_t &aMleFrameCounter) const; Error ReadTlvRequestTlv(TlvList &aTlvList) const; Error ReadLeaderDataTlv(LeaderData &aLeaderData) const; + Error ReadAndSetNetworkDataTlv(const LeaderData &aLeaderData) const; Error ReadAndSaveActiveDataset(const MeshCoP::Timestamp &aActiveTimestamp) const; Error ReadAndSavePendingDataset(const MeshCoP::Timestamp &aPendingTimestamp) const; #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE