diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 7f63cb215..35844bddc 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -365,38 +365,9 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) return error; } -Error Dataset::GetTimestamp(Type aType, Timestamp &aTimestamp) const +Error Dataset::ReadTimestamp(Type aType, Timestamp &aTimestamp) const { - Error error = kErrorNone; - const Tlv *tlv; - - if (aType == kActive) - { - tlv = FindTlv(Tlv::kActiveTimestamp); - VerifyOrExit(tlv != nullptr, error = kErrorNotFound); - aTimestamp = tlv->ReadValueAs(); - } - else - { - tlv = FindTlv(Tlv::kPendingTimestamp); - VerifyOrExit(tlv != nullptr, error = kErrorNotFound); - aTimestamp = tlv->ReadValueAs(); - } - -exit: - return error; -} - -void Dataset::SetTimestamp(Type aType, const Timestamp &aTimestamp) -{ - if (aType == kActive) - { - IgnoreError(Write(aTimestamp)); - } - else - { - IgnoreError(Write(aTimestamp)); - } + return (aType == kActive) ? Read(aTimestamp) : Read(aTimestamp); } Error Dataset::WriteTlv(Tlv::Type aType, const void *aValue, uint8_t aLength) diff --git a/src/core/meshcop/dataset.hpp b/src/core/meshcop/dataset.hpp index 627bbb8fd..32f74e331 100644 --- a/src/core/meshcop/dataset.hpp +++ b/src/core/meshcop/dataset.hpp @@ -297,6 +297,58 @@ public: */ const Tlv *FindTlv(Tlv::Type aType) const; + /** + * Finds and reads a simple TLV in the Dataset. + * + * If the specified TLV type is not found, `kErrorNotFound` is reported. + * + * @tparam SimpleTlvType The simple TLV type (must be a sub-class of `SimpleTlvInfo`). + * + * @param[out] aValue A reference to return the read TLV value. + * + * @retval kErrorNone Successfully found and read the TLV value. @p aValue is updated. + * @retval kErrorNotFound Could not find the TLV in the Dataset. + * + */ + template Error Read(typename SimpleTlvType::ValueType &aValue) const + { + const Tlv *tlv = FindTlv(static_cast(SimpleTlvType::kType)); + + return (tlv == nullptr) ? kErrorNotFound : (aValue = tlv->ReadValueAs(), kErrorNone); + } + + /** + * Finds and reads an `uint` TLV in the Dataset. + * + * If the specified TLV type is not found, `kErrorNotFound` is reported. + * + * @tparam UintTlvType The integer simple TLV type (must be a sub-class of `UintTlvInfo`). + * + * @param[out] aValue A reference to return the read TLV value. + * + * @retval kErrorNone Successfully found and read the TLV value. @p aValue is updated. + * @retval kErrorNotFound Could not find the TLV in the Dataset. + * + */ + template Error Read(typename UintTlvType::UintValueType &aValue) const + { + const Tlv *tlv = FindTlv(static_cast(UintTlvType::kType)); + + return (tlv == nullptr) ? kErrorNotFound : (aValue = tlv->ReadValueAs(), kErrorNone); + } + + /** + * Reads the Timestamp (Active or Pending). + * + * @param[in] aType The type: active or pending. + * @param[out] aTimestamp A reference to a `Timestamp` to output the value. + * + * @retval kErrorNone Timestamp was read successfully. @p aTimestamp is updated. + * @retval kErrorNotFound Could not find the requested Timestamp TLV. + * + */ + Error ReadTimestamp(Type aType, Timestamp &aTimestamp) const; + /** * Writes a TLV to the Dataset. * @@ -429,27 +481,6 @@ public: */ TimeMilli GetUpdateTime(void) const { return mUpdateTime; } - /** - * Gets the Timestamp (Active or Pending). - * - * @param[in] aType The type: active or pending. - * @param[out] aTimestamp A reference to a `Timestamp` to output the value. - * - * @retval kErrorNone Timestamp was read successfully. @p aTimestamp is updated. - * @retval kErrorNotFound Could not find the requested Timestamp TLV. - * - */ - Error GetTimestamp(Type aType, Timestamp &aTimestamp) const; - - /** - * Sets the Timestamp value. - * - * @param[in] aType The type: active or pending. - * @param[in] aTimestamp A Timestamp. - * - */ - void SetTimestamp(Type aType, const Timestamp &aTimestamp); - /** * Reads the Dataset from a given message and checks that it is well-formed and valid. * diff --git a/src/core/meshcop/dataset_local.cpp b/src/core/meshcop/dataset_local.cpp index 7b2a62e44..6fec08461 100644 --- a/src/core/meshcop/dataset_local.cpp +++ b/src/core/meshcop/dataset_local.cpp @@ -82,7 +82,7 @@ Error DatasetLocal::Restore(Dataset &aDataset) SuccessOrExit(error); mSaved = true; - mTimestampPresent = (aDataset.GetTimestamp(mType, mTimestamp) == kErrorNone); + mTimestampPresent = (aDataset.ReadTimestamp(mType, mTimestamp) == kErrorNone); exit: return error; @@ -199,7 +199,7 @@ Error DatasetLocal::Save(const Dataset &aDataset) LogInfo("%s dataset set", Dataset::TypeToString(mType)); } - mTimestampPresent = (aDataset.GetTimestamp(mType, mTimestamp) == kErrorNone); + mTimestampPresent = (aDataset.ReadTimestamp(mType, mTimestamp) == kErrorNone); mUpdateTime = TimerMilli::GetNow(); exit: diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 0ac60c030..ef4cfcd45 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -79,7 +79,7 @@ Error DatasetManager::Restore(void) SuccessOrExit(error = mLocal.Restore(dataset)); - mTimestampValid = (dataset.GetTimestamp(GetType(), mTimestamp) == kErrorNone); + mTimestampValid = (dataset.ReadTimestamp(GetType(), mTimestamp) == kErrorNone); if (IsActiveDataset()) { @@ -121,7 +121,7 @@ Error DatasetManager::Save(const Dataset &aDataset) int compare; bool isNetworkKeyUpdated = false; - if (aDataset.GetTimestamp(GetType(), mTimestamp) == kErrorNone) + if (aDataset.ReadTimestamp(GetType(), mTimestamp) == kErrorNone) { mTimestampValid = true; @@ -261,7 +261,7 @@ void DatasetManager::SendSet(void) IgnoreError(Get().Read(pendingDataset)); - if ((pendingDataset.GetTimestamp(Dataset::kActive, timestamp) == kErrorNone) && + if ((pendingDataset.Read(timestamp) == kErrorNone) && (Timestamp::Compare(×tamp, mLocal.GetTimestamp()) == 0)) { // stop registration attempts during dataset transition @@ -622,7 +622,7 @@ Error ActiveDatasetManager::Save(const Timestamp &aTimestamp, Dataset dataset; SuccessOrExit(error = dataset.ReadFromMessage(aMessage, aOffset, aLength)); - dataset.SetTimestamp(Dataset::kActive, aTimestamp); + SuccessOrExit(error = dataset.Write(aTimestamp)); error = DatasetManager::Save(dataset); exit: @@ -703,7 +703,7 @@ Error PendingDatasetManager::Save(const Timestamp &aTimestamp, Dataset dataset; SuccessOrExit(error = dataset.ReadFromMessage(aMessage, aOffset, aLength)); - dataset.SetTimestamp(Dataset::kPending, aTimestamp); + SuccessOrExit(dataset.Write(aTimestamp)); SuccessOrExit(error = DatasetManager::Save(dataset)); StartDelayTimer(); diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index dfa9053c7..16be30c3f 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -443,7 +443,7 @@ void PendingDatasetManager::ApplyActiveDataset(const Timestamp &aTimestamp, Coap IgnoreError(dataset.Write(Get().GetDelayTimerMinimal())); - dataset.SetTimestamp(Dataset::kPending, aTimestamp); + IgnoreError(dataset.Write(aTimestamp)); IgnoreError(DatasetManager::Save(dataset)); StartDelayTimer(); diff --git a/src/core/meshcop/dataset_updater.cpp b/src/core/meshcop/dataset_updater.cpp index 4f55a3036..a6a3dce32 100644 --- a/src/core/meshcop/dataset_updater.cpp +++ b/src/core/meshcop/dataset_updater.cpp @@ -139,14 +139,14 @@ void DatasetUpdater::PreparePendingDataset(void) } timestamp.AdvanceRandomTicks(); - dataset.SetTimestamp(Dataset::kPending, timestamp); + IgnoreError(dataset.Write(timestamp)); } { Timestamp timestamp = dataset.FindTlv(Tlv::kActiveTimestamp)->ReadValueAs(); timestamp.AdvanceRandomTicks(); - dataset.SetTimestamp(Dataset::kActive, timestamp); + IgnoreError(dataset.Write(timestamp)); } SuccessOrExit(error = Get().Save(dataset));