From 685094b378c2359e434fb51e16acd8972e6f5f62 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 30 Nov 2023 21:29:12 -0800 Subject: [PATCH] [dataset] introduce `WriteTlv()` methods (#9664) This commit adds different flavors of `WriteTlv()` including template version as `Write()`. These methods write or update a TLV in `Dataset`. The new methods replace the previous `SetTlv()` methods. This new approach introduces type safety checks during compilation, guaranteeing the use of the correct value type for each TLV. For instance, `Write()` only accepts `uint16_t` value, while `Write()` only accepts `NetworkKey` value. This commit also renames the previous `GetTlv()` to `FindTlv()`. --- src/core/meshcop/dataset.cpp | 93 ++++++------ src/core/meshcop/dataset.hpp | 179 +++++++++-------------- src/core/meshcop/dataset_local.cpp | 2 +- src/core/meshcop/dataset_manager.cpp | 8 +- src/core/meshcop/dataset_manager_ftd.cpp | 29 ++-- src/core/meshcop/dataset_updater.cpp | 4 +- src/core/meshcop/joiner_router.cpp | 2 +- 7 files changed, 137 insertions(+), 180 deletions(-) diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 3e47300f4b7..00c8d6d1703 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -179,7 +179,7 @@ bool Dataset::IsValid(void) const return rval; } -const Tlv *Dataset::GetTlv(Tlv::Type aType) const { return As(Tlv::FindTlv(mTlvs, mLength, aType)); } +const Tlv *Dataset::FindTlv(Tlv::Type aType) const { return As(Tlv::FindTlv(mTlvs, mLength, aType)); } void Dataset::ConvertTo(Info &aDatasetInfo) const { @@ -286,7 +286,7 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) Timestamp activeTimestamp; aDatasetInfo.GetActiveTimestamp(activeTimestamp); - IgnoreError(SetTlv(Tlv::kActiveTimestamp, activeTimestamp)); + IgnoreError(Write(activeTimestamp)); } if (aDatasetInfo.IsPendingTimestampPresent()) @@ -294,12 +294,12 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) Timestamp pendingTimestamp; aDatasetInfo.GetPendingTimestamp(pendingTimestamp); - IgnoreError(SetTlv(Tlv::kPendingTimestamp, pendingTimestamp)); + IgnoreError(Write(pendingTimestamp)); } if (aDatasetInfo.IsDelayPresent()) { - IgnoreError(SetTlv(Tlv::kDelayTimer, aDatasetInfo.GetDelay())); + IgnoreError(Write(aDatasetInfo.GetDelay())); } if (aDatasetInfo.IsChannelPresent()) @@ -307,7 +307,7 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) ChannelTlv tlv; tlv.Init(); tlv.SetChannel(aDatasetInfo.GetChannel()); - IgnoreError(SetTlv(tlv)); + IgnoreError(WriteTlv(tlv)); } if (aDatasetInfo.IsChannelMaskPresent()) @@ -315,39 +315,39 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) ChannelMaskTlv tlv; tlv.Init(); tlv.SetChannelMask(aDatasetInfo.GetChannelMask()); - IgnoreError(SetTlv(tlv)); + IgnoreError(WriteTlv(tlv)); } if (aDatasetInfo.IsExtendedPanIdPresent()) { - IgnoreError(SetTlv(Tlv::kExtendedPanId, aDatasetInfo.GetExtendedPanId())); + IgnoreError(Write(aDatasetInfo.GetExtendedPanId())); } if (aDatasetInfo.IsMeshLocalPrefixPresent()) { - IgnoreError(SetTlv(Tlv::kMeshLocalPrefix, aDatasetInfo.GetMeshLocalPrefix())); + IgnoreError(Write(aDatasetInfo.GetMeshLocalPrefix())); } if (aDatasetInfo.IsNetworkKeyPresent()) { - IgnoreError(SetTlv(Tlv::kNetworkKey, aDatasetInfo.GetNetworkKey())); + IgnoreError(Write(aDatasetInfo.GetNetworkKey())); } if (aDatasetInfo.IsNetworkNamePresent()) { NameData nameData = aDatasetInfo.GetNetworkName().GetAsData(); - IgnoreError(SetTlv(Tlv::kNetworkName, nameData.GetBuffer(), nameData.GetLength())); + IgnoreError(WriteTlv(Tlv::kNetworkName, nameData.GetBuffer(), nameData.GetLength())); } if (aDatasetInfo.IsPanIdPresent()) { - IgnoreError(SetTlv(Tlv::kPanId, aDatasetInfo.GetPanId())); + IgnoreError(Write(aDatasetInfo.GetPanId())); } if (aDatasetInfo.IsPskcPresent()) { - IgnoreError(SetTlv(Tlv::kPskc, aDatasetInfo.GetPskc())); + IgnoreError(Write(aDatasetInfo.GetPskc())); } if (aDatasetInfo.IsSecurityPolicyPresent()) @@ -356,7 +356,7 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) tlv.Init(); tlv.SetSecurityPolicy(aDatasetInfo.GetSecurityPolicy()); - IgnoreError(SetTlv(tlv)); + IgnoreError(WriteTlv(tlv)); } mUpdateTime = TimerMilli::GetNow(); @@ -371,13 +371,13 @@ Error Dataset::GetTimestamp(Type aType, Timestamp &aTimestamp) const if (aType == kActive) { - tlv = GetTlv(Tlv::kActiveTimestamp); + tlv = FindTlv(Tlv::kActiveTimestamp); VerifyOrExit(tlv != nullptr, error = kErrorNotFound); aTimestamp = tlv->ReadValueAs(); } else { - tlv = GetTlv(Tlv::kPendingTimestamp); + tlv = FindTlv(Tlv::kPendingTimestamp); VerifyOrExit(tlv != nullptr, error = kErrorNotFound); aTimestamp = tlv->ReadValueAs(); } @@ -388,35 +388,38 @@ Error Dataset::GetTimestamp(Type aType, Timestamp &aTimestamp) const void Dataset::SetTimestamp(Type aType, const Timestamp &aTimestamp) { - IgnoreError(SetTlv((aType == kActive) ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp, aTimestamp)); + if (aType == kActive) + { + IgnoreError(Write(aTimestamp)); + } + else + { + IgnoreError(Write(aTimestamp)); + } } -Error Dataset::SetTlv(Tlv::Type aType, const void *aValue, uint8_t aLength) +Error Dataset::WriteTlv(Tlv::Type aType, const void *aValue, uint8_t aLength) { Error error = kErrorNone; uint16_t bytesAvailable = sizeof(mTlvs) - mLength; - Tlv *old = GetTlv(aType); - Tlv tlv; + Tlv *oldTlv = FindTlv(aType); + Tlv *newTlv; - if (old != nullptr) + if (oldTlv != nullptr) { - bytesAvailable += sizeof(Tlv) + old->GetLength(); + bytesAvailable += sizeof(Tlv) + oldTlv->GetLength(); } VerifyOrExit(sizeof(Tlv) + aLength <= bytesAvailable, error = kErrorNoBufs); - if (old != nullptr) - { - RemoveTlv(old); - } + RemoveTlv(oldTlv); - tlv.SetType(aType); - tlv.SetLength(aLength); - memcpy(mTlvs + mLength, &tlv, sizeof(Tlv)); - mLength += sizeof(Tlv); + newTlv = GetTlvsEnd(); + mLength += sizeof(Tlv) + aLength; - memcpy(mTlvs + mLength, aValue, aLength); - mLength += aLength; + newTlv->SetType(aType); + newTlv->SetLength(aLength); + memcpy(newTlv->GetValue(), aValue, aLength); mUpdateTime = TimerMilli::GetNow(); @@ -424,7 +427,7 @@ Error Dataset::SetTlv(Tlv::Type aType, const void *aValue, uint8_t aLength) return error; } -Error Dataset::SetTlv(const Tlv &aTlv) { return SetTlv(aTlv.GetType(), aTlv.GetValue(), aTlv.GetLength()); } +Error Dataset::WriteTlv(const Tlv &aTlv) { return WriteTlv(aTlv.GetType(), aTlv.GetValue(), aTlv.GetLength()); } Error Dataset::ReadFromMessage(const Message &aMessage, uint16_t aOffset, uint16_t aLength) { @@ -444,16 +447,7 @@ Error Dataset::ReadFromMessage(const Message &aMessage, uint16_t aOffset, uint16 return error; } -void Dataset::RemoveTlv(Tlv::Type aType) -{ - Tlv *tlv; - - VerifyOrExit((tlv = GetTlv(aType)) != nullptr); - RemoveTlv(tlv); - -exit: - return; -} +void Dataset::RemoveTlv(Tlv::Type aType) { RemoveTlv(FindTlv(aType)); } Error Dataset::AppendMleDatasetTlv(Type aType, Message &aMessage) const { @@ -504,11 +498,14 @@ Error Dataset::AppendMleDatasetTlv(Type aType, Message &aMessage) const void Dataset::RemoveTlv(Tlv *aTlv) { - uint8_t *start = reinterpret_cast(aTlv); - uint16_t length = sizeof(Tlv) + aTlv->GetLength(); + if (aTlv != nullptr) + { + uint8_t *start = reinterpret_cast(aTlv); + uint16_t length = sizeof(Tlv) + aTlv->GetLength(); - memmove(start, start + length, mLength - (static_cast(start - mTlvs) + length)); - mLength -= length; + memmove(start, start + length, mLength - (static_cast(start - mTlvs) + length)); + mLength -= length; + } } Error Dataset::ApplyConfiguration(Instance &aInstance, bool *aIsNetworkKeyUpdated) const @@ -609,7 +606,7 @@ void Dataset::SaveTlvInSecureStorageAndClearValue(Tlv::Type aTlvType, Crypto::St { using namespace ot::Crypto::Storage; - Tlv *tlv = GetTlv(aTlvType); + Tlv *tlv = FindTlv(aTlvType); VerifyOrExit(tlv != nullptr); VerifyOrExit(tlv->GetLength() > 0); @@ -628,7 +625,7 @@ Error Dataset::ReadTlvFromSecureStorage(Tlv::Type aTlvType, Crypto::Storage::Key using namespace ot::Crypto::Storage; Error error = kErrorNone; - Tlv *tlv = GetTlv(aTlvType); + Tlv *tlv = FindTlv(aTlvType); size_t readLength; VerifyOrExit(tlv != nullptr); diff --git a/src/core/meshcop/dataset.hpp b/src/core/meshcop/dataset.hpp index e3f5ab0b8ae..131a271a2f6 100644 --- a/src/core/meshcop/dataset.hpp +++ b/src/core/meshcop/dataset.hpp @@ -631,7 +631,7 @@ class Dataset * @retval FALSE TLV with @p aType is not present in the Dataset. * */ - bool ContainsTlv(Tlv::Type aType) const { return (GetTlv(aType) != nullptr); } + bool ContainsTlv(Tlv::Type aType) const { return (FindTlv(aType) != nullptr); } /** * Indicates whether or not a given TLV type is present in the Dataset. @@ -648,47 +648,101 @@ class Dataset } /** - * Returns a pointer to the TLV with a given type. + * Searches for a given TLV type in the Dataset. * - * @param[in] aType A TLV type. + * @param[in] aType The TLV type to find. * - * @returns A pointer to the TLV or `nullptr` if none is found. + * @returns A pointer to the TLV or `nullptr` if not found. * */ - Tlv *GetTlv(Tlv::Type aType) { return AsNonConst(AsConst(this)->GetTlv(aType)); } + Tlv *FindTlv(Tlv::Type aType) { return AsNonConst(AsConst(this)->FindTlv(aType)); } /** - * Returns a pointer to the TLV with a given type. + * Searches for a given TLV type in the Dataset. * - * @param[in] aType The TLV type. + * @param[in] aType The TLV type to find. * - * @returns A pointer to the TLV or `nullptr` if none is found. + * @returns A pointer to the TLV or `nullptr` if not found. * */ - const Tlv *GetTlv(Tlv::Type aType) const; + const Tlv *FindTlv(Tlv::Type aType) const; /** - * Returns a pointer to the TLV with a given template type `TlvType` + * Writes a TLV to the Dataset. * - * @returns A pointer to the TLV or `nullptr` if none is found. + * If the specified TLV type already exists, it will be replaced. Otherwise, the TLV will be appended. + * + * @param[in] aTlv A reference to the TLV. + * + * @retval kErrorNone Successfully updated the TLV. + * @retval kErrorNoBufs Could not add the TLV due to insufficient buffer space. + * + */ + Error WriteTlv(const Tlv &aTlv); + + /** + * Writes a TLV in the Dataset. + * + * If the specified TLV type already exists, it will be replaced. Otherwise, the TLV will be appended. + * + * @param[in] aType The TLV type. + * @param[in] aValue A pointer to a buffer containing the TLV value. + * @param[in] aLength The TLV length. + * + * @retval kErrorNone Successfully updated the TLV. + * @retval kErrorNoBufs Could not add the TLV due to insufficient buffer space. * */ - template TlvType *GetTlv(void) + Error WriteTlv(Tlv::Type aType, const void *aValue, uint8_t aLength); + + /** + * Writes a simple TLV in the Dataset. + * + * If the specified TLV type already exists, it will be replaced. Otherwise, the TLV will be appended. + * + * @tparam SimpleTlvType The simple TLV type (must be a sub-class of `SimpleTlvInfo`). + * + * @param[in] aValue The TLV value. + * + * @retval kErrorNone Successfully updated the TLV. + * @retval kErrorNoBufs Could not add the TLV due to insufficient buffer space. + * + */ + template Error Write(const typename SimpleTlvType::ValueType &aValue) { - return As(GetTlv(static_cast(TlvType::kType))); + return WriteTlv(static_cast(SimpleTlvType::kType), &aValue, sizeof(aValue)); } /** - * Returns a pointer to the TLV with a given template type `TlvType` + * Writes a `uint` TLV in the Dataset. + * + * If the specified TLV type already exists, it will be replaced. Otherwise, the TLV will be appended. + * + * @tparam UintTlvType The integer simple TLV type (must be a sub-class of `UintTlvInfo`). * - * @returns A pointer to the TLV or `nullptr` if none is found. + * @param[in] aValue The TLV value. + * + * @retval kErrorNone Successfully updated the TLV. + * @retval kErrorNoBufs Could not add the TLV due to insufficient buffer space. * */ - template const TlvType *GetTlv(void) const + template Error Write(typename UintTlvType::UintValueType aValue) { - return As(GetTlv(static_cast(TlvType::kType))); + typename UintTlvType::UintValueType value = BigEndian::HostSwap(aValue); + + return WriteTlv(static_cast(UintTlvType::kType), &value, sizeof(value)); } + /** + * Removes a TLV from the Dataset. + * + * If the Dataset does not contain the given TLV type, no action is performed. + * + * @param[in] aType The TLV type to remove. + * + */ + void RemoveTlv(Tlv::Type aType); + /** * Returns a pointer to the byte representation of the Dataset. * @@ -766,55 +820,6 @@ class Dataset */ void SetTimestamp(Type aType, const Timestamp &aTimestamp); - /** - * Sets a TLV in the Dataset. - * - * @param[in] aTlv A reference to the TLV. - * - * @retval kErrorNone Successfully set the TLV. - * @retval kErrorNoBufs Could not set the TLV due to insufficient buffer space. - * - */ - Error SetTlv(const Tlv &aTlv); - - /** - * Sets a TLV with a given TLV Type and Value. - * - * @param[in] aType The TLV Type. - * @param[in] aValue A pointer to TLV Value. - * @param[in] aLength The TLV Length in bytes (length of @p aValue). - * - * @retval kErrorNone Successfully set the TLV. - * @retval kErrorNoBufs Could not set the TLV due to insufficient buffer space. - * - */ - Error SetTlv(Tlv::Type aType, const void *aValue, uint8_t aLength); - - /** - * Sets a TLV with a given TLV Type and Value. - * - * @tparam ValueType The type of TLV's Value. - * - * Specializations of this template method are provided for `uint16_t` and `uint32_t` types which ensure big-endian - * encoding is used. - * - * @param[in] aType The TLV Type. - * @param[in] aValue The TLV Value (of type `ValueType`). - * - * @retval kErrorNone Successfully set the TLV. - * @retval kErrorNoBufs Could not set the TLV due to insufficient buffer space. - * - */ - template Error SetTlv(Tlv::Type aType, const ValueType &aValue) - { - static_assert(!TypeTraits::IsPointer::kValue, "ValueType must not be a pointer"); - static_assert(!TypeTraits::IsSame::kValue, "Specialization must be used for uint16_t"); - static_assert(!TypeTraits::IsSame::kValue, "Specialization must be used for uint32_t"); - static_assert(!TypeTraits::IsSame::kValue, "Specialization must be used for uint64_t"); - - return SetTlv(aType, &aValue, sizeof(ValueType)); - } - /** * Reads the Dataset from a given message and checks that it is well-formed and valid. * @@ -859,14 +864,6 @@ class Dataset */ void SetFrom(const otOperationalDatasetTlvs &aDataset); - /** - * Removes a TLV from the Dataset. - * - * @param[in] aType The type of a specific TLV. - * - */ - void RemoveTlv(Tlv::Type aType); - /** * Appends the MLE Dataset TLV but excluding MeshCoP Sub Timestamp TLV. * @@ -980,40 +977,6 @@ class Dataset uint16_t mLength; ///< The number of valid bytes in @var mTlvs }; -/** - * This is a template specialization of `SetTlv` with a `uint16_t` value type. - * - * @param[in] aType The TLV Type. - * @param[in] aValue The TLV value (as `uint16_t`). - * - * @retval kErrorNone Successfully set the TLV. - * @retval kErrorNoBufs Could not set the TLV due to insufficient buffer space. - * - */ -template <> inline Error Dataset::SetTlv(Tlv::Type aType, const uint16_t &aValue) -{ - uint16_t value = BigEndian::HostSwap16(aValue); - - return SetTlv(aType, &value, sizeof(uint16_t)); -} - -/** - * This is a template specialization of `SetTlv` with a `uint32_t` value type - * - * @param[in] aType The TLV Type. - * @param[in] aValue The TLV value (as `uint32_t`). - * - * @retval kErrorNone Successfully set the TLV. - * @retval kErrorNoBufs Could not set the TLV due to insufficient buffer space. - * - */ -template <> inline Error Dataset::SetTlv(Tlv::Type aType, const uint32_t &aValue) -{ - uint32_t value = BigEndian::HostSwap32(aValue); - - return SetTlv(aType, &value, sizeof(uint32_t)); -} - } // namespace MeshCoP DefineCoreType(otOperationalDatasetComponents, MeshCoP::Dataset::Components); diff --git a/src/core/meshcop/dataset_local.cpp b/src/core/meshcop/dataset_local.cpp index 11010c49ead..4c971c7bbb1 100644 --- a/src/core/meshcop/dataset_local.cpp +++ b/src/core/meshcop/dataset_local.cpp @@ -108,7 +108,7 @@ Error DatasetLocal::Read(Dataset &aDataset) const { uint32_t elapsed; uint32_t delayTimer; - Tlv *tlv = aDataset.GetTlv(Tlv::kDelayTimer); + Tlv *tlv = aDataset.FindTlv(Tlv::kDelayTimer); VerifyOrExit(tlv != nullptr); diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 034985d907a..bc316980e15 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -226,7 +226,7 @@ Error DatasetManager::GetChannelMask(Mac::ChannelMask &aChannelMask) const SuccessOrExit(error = Read(dataset)); - channelMaskTlv = dataset.GetTlv(); + channelMaskTlv = As(dataset.FindTlv(Tlv::kChannelMask)); VerifyOrExit(channelMaskTlv != nullptr, error = kErrorNotFound); VerifyOrExit((mask = channelMaskTlv->GetChannelMask()) != 0); @@ -418,7 +418,7 @@ void DatasetManager::SendGetResponse(const Coap::Message &aRequest, continue; } - if ((tlv = dataset.GetTlv(static_cast(aTlvs[index]))) != nullptr) + if ((tlv = dataset.FindTlv(static_cast(aTlvs[index]))) != nullptr) { SuccessOrExit(error = tlv->AppendTo(*message)); } @@ -746,7 +746,7 @@ void PendingDatasetManager::StartDelayTimer(void) mDelayTimer.Stop(); - tlv = dataset.GetTlv(Tlv::kDelayTimer); + tlv = dataset.FindTlv(Tlv::kDelayTimer); VerifyOrExit(tlv != nullptr); delay = tlv->ReadValueAs(); @@ -770,7 +770,7 @@ void PendingDatasetManager::HandleDelayTimer(void) // if the Delay Timer value is larger than what our Timer implementation can handle, we have to compute // the remainder and wait some more. - if ((tlv = dataset.GetTlv(Tlv::kDelayTimer)) != nullptr) + if ((tlv = dataset.FindTlv(Tlv::kDelayTimer)) != nullptr) { uint32_t elapsed = mDelayTimer.GetFireTime() - dataset.GetUpdateTime(); uint32_t delay = tlv->ReadValueAs(); diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index a2e1cdebbe6..3eec1a8b666 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -220,7 +220,7 @@ Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo OT_FALL_THROUGH; default: - SuccessOrExit(dataset.SetTlv(datasetTlv)); + SuccessOrExit(dataset.WriteTlv(datasetTlv)); break; } @@ -307,7 +307,7 @@ Error ActiveDatasetManager::GenerateLocal(void) Timestamp timestamp; timestamp.Clear(); - IgnoreError(dataset.SetTlv(Tlv::kActiveTimestamp, timestamp)); + IgnoreError(dataset.Write(timestamp)); } if (!dataset.Contains()) @@ -315,7 +315,7 @@ Error ActiveDatasetManager::GenerateLocal(void) ChannelTlv tlv; tlv.Init(); tlv.SetChannel(Get().GetPanChannel()); - IgnoreError(dataset.SetTlv(tlv)); + IgnoreError(dataset.WriteTlv(tlv)); } if (!dataset.Contains()) @@ -323,17 +323,17 @@ Error ActiveDatasetManager::GenerateLocal(void) ChannelMaskTlv tlv; tlv.Init(); tlv.SetChannelMask(Get().GetSupportedChannelMask().GetMask()); - IgnoreError(dataset.SetTlv(tlv)); + IgnoreError(dataset.WriteTlv(tlv)); } if (!dataset.Contains()) { - IgnoreError(dataset.SetTlv(Tlv::kExtendedPanId, Get().GetExtPanId())); + IgnoreError(dataset.Write(Get().GetExtPanId())); } if (!dataset.Contains()) { - IgnoreError(dataset.SetTlv(Tlv::kMeshLocalPrefix, Get().GetMeshLocalPrefix())); + IgnoreError(dataset.Write(Get().GetMeshLocalPrefix())); } if (!dataset.Contains()) @@ -341,19 +341,19 @@ Error ActiveDatasetManager::GenerateLocal(void) NetworkKey networkKey; Get().GetNetworkKey(networkKey); - IgnoreError(dataset.SetTlv(Tlv::kNetworkKey, networkKey)); + IgnoreError(dataset.Write(networkKey)); } if (!dataset.Contains()) { NameData nameData = Get().GetNetworkName().GetAsData(); - IgnoreError(dataset.SetTlv(Tlv::kNetworkName, nameData.GetBuffer(), nameData.GetLength())); + IgnoreError(dataset.WriteTlv(Tlv::kNetworkName, nameData.GetBuffer(), nameData.GetLength())); } if (!dataset.Contains()) { - IgnoreError(dataset.SetTlv(Tlv::kPanId, Get().GetPanId())); + IgnoreError(dataset.Write(Get().GetPanId())); } if (!dataset.Contains()) @@ -369,7 +369,7 @@ Error ActiveDatasetManager::GenerateLocal(void) SuccessOrExit(error = pskc.GenerateRandom()); } - IgnoreError(dataset.SetTlv(Tlv::kPskc, pskc)); + IgnoreError(dataset.Write(pskc)); } if (!dataset.Contains()) @@ -378,7 +378,7 @@ Error ActiveDatasetManager::GenerateLocal(void) tlv.Init(); tlv.SetSecurityPolicy(Get().GetSecurityPolicy()); - IgnoreError(dataset.SetTlv(tlv)); + IgnoreError(dataset.WriteTlv(tlv)); } SuccessOrExit(error = mLocal.Save(dataset)); @@ -432,17 +432,14 @@ void PendingDatasetManager::ApplyActiveDataset(const Timestamp &aTimestamp, Coap SuccessOrExit(datasetTlv.ReadFromMessage(aMessage, offset)); offset += static_cast(datasetTlv.GetSize()); - IgnoreError(dataset.SetTlv(datasetTlv)); + IgnoreError(dataset.WriteTlv(datasetTlv)); } - // add delay timer tlv - IgnoreError(dataset.SetTlv(Tlv::kDelayTimer, Get().GetDelayTimerMinimal())); + IgnoreError(dataset.Write(Get().GetDelayTimerMinimal())); - // add pending timestamp tlv dataset.SetTimestamp(Dataset::kPending, aTimestamp); IgnoreError(DatasetManager::Save(dataset)); - // reset delay timer StartDelayTimer(); exit: diff --git a/src/core/meshcop/dataset_updater.cpp b/src/core/meshcop/dataset_updater.cpp index e34f2205d89..c4b1145cf50 100644 --- a/src/core/meshcop/dataset_updater.cpp +++ b/src/core/meshcop/dataset_updater.cpp @@ -123,7 +123,7 @@ void DatasetUpdater::PreparePendingDataset(void) { uint32_t delay = kDefaultDelay; - SuccessOrExit(error = dataset.SetTlv(Tlv::kDelayTimer, delay)); + SuccessOrExit(error = dataset.Write(delay)); } { @@ -143,7 +143,7 @@ void DatasetUpdater::PreparePendingDataset(void) } { - Timestamp timestamp = dataset.GetTlv(Tlv::kActiveTimestamp)->ReadValueAs(); + Timestamp timestamp = dataset.FindTlv(Tlv::kActiveTimestamp)->ReadValueAs(); timestamp.AdvanceRandomTicks(); dataset.SetTimestamp(Dataset::kActive, timestamp); diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index fe58e420140..acadf1a9d83 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -308,7 +308,7 @@ Coap::Message *JoinerRouter::PrepareJoinerEntrustMessage(void) for (Tlv::Type tlvType : kTlvTypes) { - const Tlv *tlv = dataset.GetTlv(tlvType); + const Tlv *tlv = dataset.FindTlv(tlvType); VerifyOrExit(tlv != nullptr, error = kErrorInvalidState); SuccessOrExit(error = tlv->AppendTo(*message));