mirror of
https://github.com/espressif/openthread.git
synced 2026-08-03 17:37:46 +00:00
[dataset] add WriteTimestamp() and RemoveTimestamp() (#10333)
This commit introduces new helper methods in the `Dataset` class to write or remove a Timestamp TLV (Active or Pending) to/from the Dataset. These are added alongside the existing `ReadTimestamp()` method.
This commit is contained in:
@@ -352,11 +352,6 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
Error Dataset::ReadTimestamp(Type aType, Timestamp &aTimestamp) const
|
||||
{
|
||||
return (aType == kActive) ? Read<ActiveTimestampTlv>(aTimestamp) : Read<PendingTimestampTlv>(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<ActiveTimestampTlv>()` for both.
|
||||
|
||||
aTimestamp = tlv->ReadValueAs<ActiveTimestampTlv>();
|
||||
|
||||
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;
|
||||
|
||||
@@ -365,18 +365,6 @@ public:
|
||||
return (tlv == nullptr) ? kErrorNotFound : (aValue = tlv->ReadValueAs<UintTlvType>(), 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
|
||||
|
||||
@@ -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<ActiveTimestampTlv>(aTimestamp));
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = dataset.Write<PendingTimestampTlv>(aTimestamp));
|
||||
}
|
||||
SuccessOrExit(error = dataset.WriteTimestamp(mType, aTimestamp));
|
||||
|
||||
error = Save(dataset);
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user