Skip to content

Commit

Permalink
[mle] add Mle::RxMessage::ReadAndSave{Active/Pending}Dataset() (ope…
Browse files Browse the repository at this point in the history
…nthread#10348)

This commit adds `ReadAndSave{Active/Pending}Dataset()` helper methods
to `Mle::RxMessage` to read the Active or Pending Dataset from a
received MLE message and save it in the corresponding
`DatasetManager`.

This commit also refactors the code for parsing the MLE TLVs, moving
it from `DatasetManager` to the `Mle` class for better alignment of
responsibilities.
  • Loading branch information
abtink authored Jun 7, 2024
1 parent 22ee728 commit dd1e5f4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
16 changes: 0 additions & 16 deletions src/core/meshcop/dataset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,6 @@ void DatasetManager::Clear(void)
SignalDatasetChange();
}

Error DatasetManager::Save(const Timestamp &aTimestamp, const Message &aMessage, uint16_t aOffset, uint16_t aLength)
{
Error error = kErrorNone;
Dataset dataset;

SuccessOrExit(error = dataset.SetFrom(aMessage, aOffset, aLength));
SuccessOrExit(error = dataset.ValidateTlvs());

SuccessOrExit(error = dataset.WriteTimestamp(mType, aTimestamp));

error = Save(dataset);

exit:
return error;
}

Error DatasetManager::Save(const Dataset &aDataset, bool aAllowOlderTimestamp)
{
Error error = kErrorNone;
Expand Down
17 changes: 0 additions & 17 deletions src/core/meshcop/dataset_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,6 @@ class DatasetManager : public InstanceLocator
*/
Error Save(const Dataset &aDataset) { return Save(aDataset, /* aAllowOlderTimestamp */ false); }

/**
* Sets the Operational Dataset for the partition read from a given message.
*
* Also updates the non-volatile local version if the partition's Operational Dataset is newer. If Active
* Operational Dataset is changed, applies the configuration to to Thread interface.
*
* @param[in] aTimestamp The timestamp for the Operational Dataset.
* @param[in] aMessage The message to read from.
* @param[in] aOffset The offset where the Operational Dataset begins.
* @param[in] aLength The length of the Operational Dataset.
*
* @retval kErrorNone Successfully parsed the Dataset from the @p aMessage and saved it.
* @retval kErrorParse Could not parse the Dataset from @p aMessage.
*
*/
Error Save(const Timestamp &aTimestamp, const Message &aMessage, uint16_t aOffset, uint16_t aLength);

/**
* Retrieves the channel mask from local dataset.
*
Expand Down
69 changes: 49 additions & 20 deletions src/core/thread/mle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2999,19 +2999,16 @@ Error Mle::HandleLeaderData(RxInfo &aRxInfo)
{
// We previously confirmed the message contains an
// Active or a Pending Dataset TLV before setting the
// corresponding `saveDataset` flag, so we can safely
// `IgnoreError()` on `FindTlvValueOffset()`.
// corresponding `saveDataset` flag.

if (saveActiveDataset)
{
IgnoreError(Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kActiveDataset, offset, length));
IgnoreError(Get<MeshCoP::ActiveDatasetManager>().Save(activeTimestamp, aRxInfo.mMessage, offset, length));
IgnoreError(aRxInfo.mMessage.ReadAndSaveActiveDataset(activeTimestamp));
}

if (savePendingDataset)
{
IgnoreError(Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kPendingDataset, offset, length));
IgnoreError(Get<MeshCoP::PendingDatasetManager>().Save(pendingTimestamp, aRxInfo.mMessage, offset, length));
IgnoreError(aRxInfo.mMessage.ReadAndSavePendingDataset(pendingTimestamp));
}
}

Expand Down Expand Up @@ -3313,8 +3310,6 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo)
MeshCoP::Timestamp timestamp;
uint16_t networkDataOffset;
uint16_t networkDataLength;
uint16_t offset;
uint16_t length;

SuccessOrExit(error = Tlv::Find<SourceAddressTlv>(aRxInfo.mMessage, sourceAddress));

Expand All @@ -3335,11 +3330,9 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo)
switch (Tlv::Find<ActiveTimestampTlv>(aRxInfo.mMessage, timestamp))
{
case kErrorNone:
if (Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kActiveDataset, offset, length) == kErrorNone)
{
SuccessOrExit(error =
Get<MeshCoP::ActiveDatasetManager>().Save(timestamp, aRxInfo.mMessage, offset, length));
}
error = aRxInfo.mMessage.ReadAndSaveActiveDataset(timestamp);
error = (error == kErrorNotFound) ? kErrorNone : error;
SuccessOrExit(error);
break;

case kErrorNotFound:
Expand All @@ -3358,10 +3351,7 @@ void Mle::HandleChildIdResponse(RxInfo &aRxInfo)
switch (Tlv::Find<PendingTimestampTlv>(aRxInfo.mMessage, timestamp))
{
case kErrorNone:
if (Tlv::FindTlvValueOffset(aRxInfo.mMessage, Tlv::kPendingDataset, offset, length) == kErrorNone)
{
IgnoreError(Get<MeshCoP::PendingDatasetManager>().Save(timestamp, aRxInfo.mMessage, offset, length));
}
IgnoreError(aRxInfo.mMessage.ReadAndSavePendingDataset(timestamp));
break;

case kErrorNotFound:
Expand Down Expand Up @@ -4907,13 +4897,13 @@ Error Mle::TxMessage::AppendActiveDatasetTlv(void) { return AppendDatasetTlv(Mes

Error Mle::TxMessage::AppendPendingDatasetTlv(void) { return AppendDatasetTlv(MeshCoP::Dataset::kPending); }

Error Mle::TxMessage::AppendDatasetTlv(MeshCoP::Dataset::Type mDatasetType)
Error Mle::TxMessage::AppendDatasetTlv(MeshCoP::Dataset::Type aDatasetType)
{
Error error = kErrorNotFound;
Tlv::Type tlvType;
MeshCoP::Dataset dataset;

switch (mDatasetType)
switch (aDatasetType)
{
case MeshCoP::Dataset::kActive:
error = Get<MeshCoP::ActiveDatasetManager>().Read(dataset);
Expand All @@ -4938,7 +4928,7 @@ Error Mle::TxMessage::AppendDatasetTlv(MeshCoP::Dataset::Type mDatasetType)
// message. The Timestamp is appended as its own MLE TLV to the
// message.

dataset.RemoveTimestamp(mDatasetType);
dataset.RemoveTimestamp(aDatasetType);

error = Tlv::AppendTlv(*this, tlvType, dataset.GetBytes(), dataset.GetLength());

Expand Down Expand Up @@ -5074,6 +5064,45 @@ Error Mle::RxMessage::ReadLeaderDataTlv(LeaderData &aLeaderData) const
return error;
}

Error Mle::RxMessage::ReadAndSaveActiveDataset(const MeshCoP::Timestamp &aActiveTimestamp) const
{
return ReadAndSaveDataset(MeshCoP::Dataset::kActive, aActiveTimestamp);
}

Error Mle::RxMessage::ReadAndSavePendingDataset(const MeshCoP::Timestamp &aPendingTimestamp) const
{
return ReadAndSaveDataset(MeshCoP::Dataset::kPending, aPendingTimestamp);
}

Error Mle::RxMessage::ReadAndSaveDataset(MeshCoP::Dataset::Type aDatasetType,
const MeshCoP::Timestamp &aTimestamp) const
{
Error error = kErrorNone;
Tlv::Type tlvType = (aDatasetType == MeshCoP::Dataset::kActive) ? Tlv::kActiveDataset : Tlv::kPendingDataset;
MeshCoP::Dataset dataset;
uint16_t offset;
uint16_t length;

SuccessOrExit(error = Tlv::FindTlvValueOffset(*this, tlvType, offset, length));

SuccessOrExit(error = dataset.SetFrom(*this, offset, length));
SuccessOrExit(error = dataset.ValidateTlvs());
SuccessOrExit(error = dataset.WriteTimestamp(aDatasetType, aTimestamp));

switch (aDatasetType)
{
case MeshCoP::Dataset::kActive:
error = Get<MeshCoP::ActiveDatasetManager>().Save(dataset);
break;
case MeshCoP::Dataset::kPending:
error = Get<MeshCoP::PendingDatasetManager>().Save(dataset);
break;
}

exit:
return error;
}

Error Mle::RxMessage::ReadTlvRequestTlv(TlvList &aTlvList) const
{
Error error;
Expand Down
5 changes: 4 additions & 1 deletion src/core/thread/mle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ class Mle : public InstanceLocator, private NonCopyable
private:
Error AppendCompressedAddressEntry(uint8_t aContextId, const Ip6::Address &aAddress);
Error AppendAddressEntry(const Ip6::Address &aAddress);
Error AppendDatasetTlv(MeshCoP::Dataset::Type mDatasetType);
Error AppendDatasetTlv(MeshCoP::Dataset::Type aDatasetType);
};

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand All @@ -1072,6 +1072,8 @@ class Mle : public InstanceLocator, private NonCopyable
Error ReadFrameCounterTlvs(uint32_t &aLinkFrameCounter, uint32_t &aMleFrameCounter) const;
Error ReadTlvRequestTlv(TlvList &aTlvList) const;
Error ReadLeaderDataTlv(LeaderData &aLeaderData) const;
Error ReadAndSaveActiveDataset(const MeshCoP::Timestamp &aActiveTimestamp) const;
Error ReadAndSavePendingDataset(const MeshCoP::Timestamp &aPendingTimestamp) const;
#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE
Error ReadCslClockAccuracyTlv(Mac::CslAccuracy &aCslAccuracy) const;
#endif
Expand All @@ -1081,6 +1083,7 @@ class Mle : public InstanceLocator, private NonCopyable

private:
Error ReadChallengeOrResponse(uint8_t aTlvType, RxChallenge &aRxChallenge) const;
Error ReadAndSaveDataset(MeshCoP::Dataset::Type aDatasetType, const MeshCoP::Timestamp &aTimestamp) const;
};

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down

0 comments on commit dd1e5f4

Please sign in to comment.