aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbtin Keshavarzian <abtink@google.com>2024-04-18 15:40:51 -0700
committerGitHub <noreply@github.com>2024-04-18 15:40:51 -0700
commit2199db82519b2bc7310320679e252ef3acd63eff (patch)
treec88041449bbd6804f93e36c425e4c26ff3e7eef4
parentfa6477197c61a7e69e47e9e8dd9a032cd38a709b (diff)
downloadopenthread-2199db82519b2bc7310320679e252ef3acd63eff.tar.gz
[dataset] simplify `AppendMleDatasetTlv()` (#10043)
- Inlines `AppendMleDatasetTlv()` in `DatasetManager` (removing from `Dataset` class) - Updates the locally read 'dataset' directly by removing the timestamp before appending it to the message as an MLE TLV value. - Leverages `Read()` which already updates the `DelayTimerTlv` with the remaining delay, eliminating the need to re-calculate and update the delay. - Uses `Tlv::AppendTlv()` to append the MLE TLV with `dataset` bytes as value.
-rw-r--r--src/core/common/tlvs.hpp17
-rw-r--r--src/core/meshcop/dataset.cpp37
-rw-r--r--src/core/meshcop/dataset.hpp12
-rw-r--r--src/core/meshcop/dataset_manager_ftd.cpp11
4 files changed, 25 insertions, 52 deletions
diff --git a/src/core/common/tlvs.hpp b/src/core/common/tlvs.hpp
index fd2d6791f..1acc80694 100644
--- a/src/core/common/tlvs.hpp
+++ b/src/core/common/tlvs.hpp
@@ -530,6 +530,22 @@ public:
*
* On success this method grows the message by the size of the TLV.
*
+ * @param[in] aMessage The message to append to.
+ * @param[in] aType The TLV type to append.
+ * @param[in] aValue A buffer containing the TLV value.
+ * @param[in] aLength The value length (in bytes).
+ *
+ * @retval kErrorNone Successfully appended the TLV to the message.
+ * @retval kErrorNoBufs Insufficient available buffers to grow the message.
+ *
+ */
+ static Error AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength);
+
+ /**
+ * Appends a TLV with a given type and value to a message.
+ *
+ * On success this method grows the message by the size of the TLV.
+ *
* @tparam TlvType The TLV type to append.
*
* @param[in] aMessage A reference to the message to append to.
@@ -687,7 +703,6 @@ private:
};
static Error FindTlv(const Message &aMessage, uint8_t aType, void *aValue, uint16_t aLength);
- static Error AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength);
static Error ReadStringTlv(const Message &aMessage, uint16_t aOffset, uint8_t aMaxStringLength, char *aValue);
static Error FindStringTlv(const Message &aMessage, uint8_t aType, uint8_t aMaxStringLength, char *aValue);
static Error AppendStringTlv(Message &aMessage, uint8_t aType, uint8_t aMaxStringLength, const char *aValue);
diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp
index b40e1527a..7f63cb215 100644
--- a/src/core/meshcop/dataset.cpp
+++ b/src/core/meshcop/dataset.cpp
@@ -450,43 +450,6 @@ exit:
void Dataset::RemoveTlv(Tlv::Type aType) { RemoveTlv(FindTlv(aType)); }
-Error Dataset::AppendMleDatasetTlv(Type aType, Message &aMessage) const
-{
- Error error = kErrorNone;
- Mle::Tlv tlv;
- Mle::Tlv::Type type;
-
- VerifyOrExit(mLength > 0);
-
- type = (aType == kActive ? Mle::Tlv::kActiveDataset : Mle::Tlv::kPendingDataset);
-
- tlv.SetType(type);
- tlv.SetLength(static_cast<uint8_t>(mLength) - sizeof(Tlv) - sizeof(Timestamp));
- SuccessOrExit(error = aMessage.Append(tlv));
-
- for (const Tlv *cur = GetTlvsStart(); cur < GetTlvsEnd(); cur = cur->GetNext())
- {
- if (((aType == kActive) && (cur->GetType() == Tlv::kActiveTimestamp)) ||
- ((aType == kPending) && (cur->GetType() == Tlv::kPendingTimestamp)))
- {
- ; // skip Active or Pending Timestamp TLV
- }
- else if (cur->GetType() == Tlv::kDelayTimer)
- {
- uint32_t remainingDelay = DelayTimerTlv::CalculateRemainingDelay(*cur, mUpdateTime);
-
- SuccessOrExit(error = Tlv::Append<DelayTimerTlv>(aMessage, remainingDelay));
- }
- else
- {
- SuccessOrExit(error = cur->AppendTo(aMessage));
- }
- }
-
-exit:
- return error;
-}
-
void Dataset::RemoveTlv(Tlv *aTlv)
{
if (aTlv != nullptr)
diff --git a/src/core/meshcop/dataset.hpp b/src/core/meshcop/dataset.hpp
index c81173e05..627bbb8fd 100644
--- a/src/core/meshcop/dataset.hpp
+++ b/src/core/meshcop/dataset.hpp
@@ -495,18 +495,6 @@ public:
void SetFrom(const Tlvs &aTlvs);
/**
- * Appends the MLE Dataset TLV but excluding MeshCoP Sub Timestamp TLV.
- *
- * @param[in] aType The type of the dataset, active or pending.
- * @param[in] aMessage A message to append to.
- *
- * @retval kErrorNone Successfully append MLE Dataset TLV without MeshCoP Sub Timestamp TLV.
- * @retval kErrorNoBufs Insufficient available buffers to append the message with MLE Dataset TLV.
- *
- */
- Error AppendMleDatasetTlv(Type aType, Message &aMessage) const;
-
- /**
* Applies the Active or Pending Dataset to the Thread interface.
*
* @param[in] aInstance A reference to the OpenThread instance.
diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp
index d0ac9c346..dfa9053c7 100644
--- a/src/core/meshcop/dataset_manager_ftd.cpp
+++ b/src/core/meshcop/dataset_manager_ftd.cpp
@@ -64,11 +64,18 @@ RegisterLogModule("DatasetManager");
Error DatasetManager::AppendMleDatasetTlv(Message &aMessage) const
{
- Dataset dataset;
+ Mle::Tlv::Type mleTlvType = IsActiveDataset() ? Mle::Tlv::kActiveDataset : Mle::Tlv::kPendingDataset;
+ Dataset dataset;
IgnoreError(Read(dataset));
- return dataset.AppendMleDatasetTlv(GetType(), aMessage);
+ // Remove the Active or Pending Timestamp TLV from Dataset before
+ // appending to the message. The timestamp is appended as its own
+ // MLE TLV to the message.
+
+ dataset.RemoveTlv(IsActiveDataset() ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp);
+
+ return Tlv::AppendTlv(aMessage, mleTlvType, dataset.GetBytes(), static_cast<uint8_t>(dataset.GetSize()));
}
Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)