[dataset] add generic Read<Tlv>() methods, refactor timestamp (#10059)

handling

This commit introduces several smaller changes to the `Dataset`
class:

- Provides template methods `Read<Tlv>()` for flexible reading of any
  TLV from dataset, mirroring the existing `Write<Tlv>()` methods.
- Replaces `SetTimestamp()` method with the `Write<Tlv>()` with
  specific timestamp TLVs
- Renames `GetTimestamp()` to `ReadTimestamp()`, aligning the naming
  convention with other methods.
This commit is contained in:
Abtin Keshavarzian
2024-04-25 08:24:51 -07:00
committed by GitHub
parent ade9c2b257
commit 79a6a85fed
6 changed files with 64 additions and 62 deletions
+2 -31
View File
@@ -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<ActiveTimestampTlv>();
}
else
{
tlv = FindTlv(Tlv::kPendingTimestamp);
VerifyOrExit(tlv != nullptr, error = kErrorNotFound);
aTimestamp = tlv->ReadValueAs<PendingTimestampTlv>();
}
exit:
return error;
}
void Dataset::SetTimestamp(Type aType, const Timestamp &aTimestamp)
{
if (aType == kActive)
{
IgnoreError(Write<ActiveTimestampTlv>(aTimestamp));
}
else
{
IgnoreError(Write<PendingTimestampTlv>(aTimestamp));
}
return (aType == kActive) ? Read<ActiveTimestampTlv>(aTimestamp) : Read<PendingTimestampTlv>(aTimestamp);
}
Error Dataset::WriteTlv(Tlv::Type aType, const void *aValue, uint8_t aLength)
+52 -21
View File
@@ -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 <typename SimpleTlvType> Error Read(typename SimpleTlvType::ValueType &aValue) const
{
const Tlv *tlv = FindTlv(static_cast<Tlv::Type>(SimpleTlvType::kType));
return (tlv == nullptr) ? kErrorNotFound : (aValue = tlv->ReadValueAs<SimpleTlvType>(), 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 <typename UintTlvType> Error Read(typename UintTlvType::UintValueType &aValue) const
{
const Tlv *tlv = FindTlv(static_cast<Tlv::Type>(UintTlvType::kType));
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.
*
@@ -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.
*
+2 -2
View File
@@ -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:
+5 -5
View File
@@ -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<PendingDatasetManager>().Read(pendingDataset));
if ((pendingDataset.GetTimestamp(Dataset::kActive, timestamp) == kErrorNone) &&
if ((pendingDataset.Read<ActiveTimestampTlv>(timestamp) == kErrorNone) &&
(Timestamp::Compare(&timestamp, 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<ActiveTimestampTlv>(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<PendingTimestampTlv>(aTimestamp));
SuccessOrExit(error = DatasetManager::Save(dataset));
StartDelayTimer();
+1 -1
View File
@@ -443,7 +443,7 @@ void PendingDatasetManager::ApplyActiveDataset(const Timestamp &aTimestamp, Coap
IgnoreError(dataset.Write<DelayTimerTlv>(Get<Leader>().GetDelayTimerMinimal()));
dataset.SetTimestamp(Dataset::kPending, aTimestamp);
IgnoreError(dataset.Write<PendingTimestampTlv>(aTimestamp));
IgnoreError(DatasetManager::Save(dataset));
StartDelayTimer();
+2 -2
View File
@@ -139,14 +139,14 @@ void DatasetUpdater::PreparePendingDataset(void)
}
timestamp.AdvanceRandomTicks();
dataset.SetTimestamp(Dataset::kPending, timestamp);
IgnoreError(dataset.Write<PendingTimestampTlv>(timestamp));
}
{
Timestamp timestamp = dataset.FindTlv(Tlv::kActiveTimestamp)->ReadValueAs<ActiveTimestampTlv>();
timestamp.AdvanceRandomTicks();
dataset.SetTimestamp(Dataset::kActive, timestamp);
IgnoreError(dataset.Write<ActiveTimestampTlv>(timestamp));
}
SuccessOrExit(error = Get<PendingDatasetManager>().Save(dataset));