Skip to content

Commit

Permalink
[dataset] add ContainsAllTlvs() helper method (openthread#10249)
Browse files Browse the repository at this point in the history
This commit introduces the `ContainsAllTlvs()` method to `Dataset`,
which checks if a dataset contains all the specified TLVs. This new
method simplifies the implementation of `IsCommissioned()` and
`ContainsAllRequiredTlvsFor(Type)`
  • Loading branch information
abtink authored May 16, 2024
1 parent 72f2df8 commit f15dbd6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
54 changes: 34 additions & 20 deletions src/core/meshcop/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,36 +238,50 @@ bool Dataset::IsTlvValid(const Tlv &aTlv)
return isValid;
}

bool Dataset::ContainsAllRequiredTlvsFor(Type aType) const
bool Dataset::ContainsAllTlvs(const Tlv::Type aTlvTypes[], uint8_t aLength) const
{
static const Tlv::Type kActiveDatasetTlvs[] = {
Tlv::kActiveTimestamp, Tlv::kChannel, Tlv::kChannelMask, Tlv::kExtendedPanId, Tlv::kMeshLocalPrefix,
Tlv::kNetworkKey, Tlv::kNetworkName, Tlv::kPanId, Tlv::kPskc, Tlv::kSecurityPolicy,
};

static const Tlv::Type kPendingDatasetExtraTlvs[] = {Tlv::kPendingTimestamp, Tlv::kDelayTimer};

bool containsAll = false;

for (Tlv::Type tlvType : kActiveDatasetTlvs)
{
VerifyOrExit(ContainsTlv(tlvType));
}
bool containsAll = true;

if (aType == kPending)
for (uint8_t index = 0; index < aLength; index++)
{
for (Tlv::Type tlvType : kPendingDatasetExtraTlvs)
if (!ContainsTlv(aTlvTypes[index]))
{
VerifyOrExit(ContainsTlv(tlvType));
containsAll = false;
break;
}
}

containsAll = true;

exit:
return containsAll;
}

bool Dataset::ContainsAllRequiredTlvsFor(Type aType) const
{
static const Tlv::Type kDatasetTlvs[] = {
Tlv::kActiveTimestamp,
Tlv::kChannel,
Tlv::kChannelMask,
Tlv::kExtendedPanId,
Tlv::kMeshLocalPrefix,
Tlv::kNetworkKey,
Tlv::kNetworkName,
Tlv::kPanId,
Tlv::kPskc,
Tlv::kSecurityPolicy,
// The last two TLVs are for Pending Dataset
Tlv::kPendingTimestamp,
Tlv::kDelayTimer,
};

uint8_t length = sizeof(kDatasetTlvs);

if (aType == kActive)
{
length -= 2;
}

return ContainsAllTlvs(kDatasetTlvs, length);
}

const Tlv *Dataset::FindTlv(Tlv::Type aType) const { return As<Tlv>(Tlv::FindTlv(mTlvs, mLength, aType)); }

void Dataset::ConvertTo(Info &aDatasetInfo) const
Expand Down
12 changes: 12 additions & 0 deletions src/core/meshcop/dataset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ class Dataset
return ContainsTlv(static_cast<Tlv::Type>(TlvType::kType));
}

/**
* Indicates whether or not the Dataset contains all the TLVs from a given array.
*
* @param[in] aTlvTypes An array of TLV types.
* @param[in] aLength Length of @p aTlvTypes array.
*
* @retval TRUE The Dataset contains all the TLVs in @p aTlvTypes array.
* @retval FALSE The Dataset does not contain all the TLVs in @p aTlvTypes array.
*
*/
bool ContainsAllTlvs(const Tlv::Type aTlvTypes[], uint8_t aLength) const;

/**
* Indicates whether or not the Dataset contains all the required TLVs for an Active or Pending Dataset.
*
Expand Down
13 changes: 7 additions & 6 deletions src/core/meshcop/dataset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,14 +593,15 @@ bool ActiveDatasetManager::IsComplete(void) const { return mLocal.IsSaved() && m

bool ActiveDatasetManager::IsCommissioned(void) const
{
Dataset::Info datasetInfo;
bool isValid = false;
static const Tlv::Type kRequiredTlvs[] = {
Tlv::kNetworkKey, Tlv::kNetworkName, Tlv::kExtendedPanId, Tlv::kPanId, Tlv::kChannel,
};

SuccessOrExit(Read(datasetInfo));
Dataset dataset;
bool isValid = false;

isValid = (datasetInfo.IsPresent<Dataset::kNetworkKey>() && datasetInfo.IsPresent<Dataset::kNetworkName>() &&
datasetInfo.IsPresent<Dataset::kExtendedPanId>() && datasetInfo.IsPresent<Dataset::kPanId>() &&
datasetInfo.IsPresent<Dataset::kChannel>());
SuccessOrExit(Read(dataset));
isValid = dataset.ContainsAllTlvs(kRequiredTlvs, sizeof(kRequiredTlvs));

exit:
return isValid;
Expand Down

0 comments on commit f15dbd6

Please sign in to comment.