diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 09364a418..8f2202323 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -352,11 +352,6 @@ exit: return error; } -Error Dataset::ReadTimestamp(Type aType, Timestamp &aTimestamp) const -{ - return (aType == kActive) ? Read(aTimestamp) : Read(aTimestamp); -} - Error Dataset::WriteTlv(Tlv::Type aType, const void *aValue, uint8_t aLength) { Error error = kErrorNone; @@ -530,6 +525,30 @@ void Dataset::RemoveTlv(Tlv *aTlv) } } +Error Dataset::ReadTimestamp(Type aType, Timestamp &aTimestamp) const +{ + Error error = kErrorNone; + const Tlv *tlv = FindTlv(TimestampTlvFor(aType)); + + VerifyOrExit(tlv != nullptr, error = kErrorNotFound); + + // Since both `ActiveTimestampTlv` and `PendingTimestampTlv` use + // `Timestamp` as their TLV value format, we can safely use + // `ReadValueAs()` for both. + + aTimestamp = tlv->ReadValueAs(); + +exit: + return error; +} + +Error Dataset::WriteTimestamp(Type aType, const Timestamp &aTimestamp) +{ + return WriteTlv(TimestampTlvFor(aType), &aTimestamp, sizeof(Timestamp)); +} + +void Dataset::RemoveTimestamp(Type aType) { RemoveTlv(TimestampTlvFor(aType)); } + bool Dataset::IsSubsetOf(const Dataset &aOther) const { bool isSubset = false; diff --git a/src/core/meshcop/dataset.hpp b/src/core/meshcop/dataset.hpp index 7bfe02247..3904cf88a 100644 --- a/src/core/meshcop/dataset.hpp +++ b/src/core/meshcop/dataset.hpp @@ -365,18 +365,6 @@ public: 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. * @@ -513,6 +501,40 @@ public: */ void RemoveTlv(Tlv::Type aType); + /** + * Reads the Timestamp TLV (Active or Pending). + * + * @param[in] aType The timestamp 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 the Timestamp TLV (Active or Pending). + * + * If the TLV already exists, it will be replaced. Otherwise, the TLV will be appended. + * + * @param[in] aType The timestamp type, active or pending. + * @param[in] aTimestamp The timestamp value. + * + * @retval kErrorNone Successfully updated the Timestamp TLV. + * @retval kErrorNoBufs Could not append the Timestamp TLV due to insufficient buffer space. + * + */ + Error WriteTimestamp(Type aType, const Timestamp &aTimestamp); + + /** + * Removes the Timestamp TLV (Active or Pending) from the Dataset. + * + * @param[in] aType The timestamp type, active or pending. + * + */ + void RemoveTimestamp(Type aType); + /** * Returns a pointer to the byte representation of the Dataset. * @@ -712,6 +734,11 @@ public: private: void RemoveTlv(Tlv *aTlv); + static Tlv::Type TimestampTlvFor(Type aType) + { + return (aType == kActive) ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp; + } + uint8_t mTlvs[kMaxLength]; uint8_t mLength; TimeMilli mUpdateTime; // Local time last updated diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index a78472da6..40ead331d 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -286,14 +286,7 @@ Error DatasetManager::Save(const Timestamp &aTimestamp, const Message &aMessage, SuccessOrExit(error = dataset.SetFrom(aMessage, aOffset, aLength)); SuccessOrExit(error = dataset.ValidateTlvs()); - if (IsActiveDataset()) - { - SuccessOrExit(error = dataset.Write(aTimestamp)); - } - else - { - SuccessOrExit(error = dataset.Write(aTimestamp)); - } + SuccessOrExit(error = dataset.WriteTimestamp(mType, aTimestamp)); error = Save(dataset); diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 1e9af4565..1eb6a9b26 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -76,7 +76,7 @@ Error DatasetManager::AppendMleDatasetTlv(Message &aMessage) const // appending to the message. The timestamp is appended as its own // MLE TLV to the message. - dataset.RemoveTlv(IsActiveDataset() ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp); + dataset.RemoveTimestamp(mType); return Tlv::AppendTlv(aMessage, mleTlvType, dataset.GetBytes(), dataset.GetLength()); }