From 2cc89cbfcbefd69a4e9d5fb4f10fda24414df774 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Tue, 20 Jul 2021 13:44:12 +0800 Subject: [PATCH] [dataset] remove `mType` from Dataset (#6831) --- src/cli/cli_dataset.cpp | 2 +- src/core/meshcop/dataset.cpp | 24 ++++++++---------- src/core/meshcop/dataset.hpp | 18 +++++++------ src/core/meshcop/dataset_local.cpp | 12 ++++----- src/core/meshcop/dataset_manager.cpp | 32 ++++++++++++------------ src/core/meshcop/dataset_manager_ftd.cpp | 12 ++++----- src/core/meshcop/dataset_updater.cpp | 4 +-- src/core/meshcop/joiner_router.cpp | 2 +- src/lib/spinel/radio_spinel_impl.hpp | 2 +- 9 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/cli/cli_dataset.cpp b/src/cli/cli_dataset.cpp index 440852709..eaec978c8 100644 --- a/src/cli/cli_dataset.cpp +++ b/src/cli/cli_dataset.cpp @@ -876,7 +876,7 @@ otError Dataset::ProcessSet(Arg aArgs[]) } { - MeshCoP::Dataset dataset(datasetType); + MeshCoP::Dataset dataset; MeshCoP::Dataset::Info datasetInfo; uint16_t tlvsLength = MeshCoP::Dataset::kMaxSize; diff --git a/src/core/meshcop/dataset.cpp b/src/core/meshcop/dataset.cpp index 97d21fbd5..a75ab4e19 100644 --- a/src/core/meshcop/dataset.cpp +++ b/src/core/meshcop/dataset.cpp @@ -150,10 +150,9 @@ exit: return isSubset; } -Dataset::Dataset(Type aType) +Dataset::Dataset(void) : mUpdateTime(0) , mLength(0) - , mType(aType) { memset(mTlvs, 0, sizeof(mTlvs)); } @@ -263,12 +262,12 @@ void Dataset::ConvertTo(otOperationalDatasetTlvs &aDataset) const aDataset.mLength = static_cast(mLength); } -void Dataset::Set(const Dataset &aDataset) +void Dataset::Set(Type aType, const Dataset &aDataset) { memcpy(mTlvs, aDataset.mTlvs, aDataset.mLength); mLength = aDataset.mLength; - if (mType == kActive) + if (aType == kActive) { RemoveTlv(Tlv::kPendingTimestamp); RemoveTlv(Tlv::kDelayTimer); @@ -372,11 +371,11 @@ Error Dataset::SetFrom(const Info &aDatasetInfo) return error; } -const Timestamp *Dataset::GetTimestamp(void) const +const Timestamp *Dataset::GetTimestamp(Type aType) const { const Timestamp *timestamp = nullptr; - if (mType == kActive) + if (aType == kActive) { const ActiveTimestampTlv *tlv = GetTlv(); VerifyOrExit(tlv != nullptr); @@ -393,9 +392,9 @@ exit: return timestamp; } -void Dataset::SetTimestamp(const Timestamp &aTimestamp) +void Dataset::SetTimestamp(Type aType, const Timestamp &aTimestamp) { - IgnoreError(SetTlv((mType == kActive) ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp, aTimestamp)); + IgnoreError(SetTlv((aType == kActive) ? Tlv::kActiveTimestamp : Tlv::kPendingTimestamp, aTimestamp)); } Error Dataset::SetTlv(Tlv::Type aType, const void *aValue, uint8_t aLength) @@ -461,7 +460,7 @@ exit: return; } -Error Dataset::AppendMleDatasetTlv(Message &aMessage) const +Error Dataset::AppendMleDatasetTlv(Type aType, Message &aMessage) const { Error error = kErrorNone; Mle::Tlv tlv; @@ -469,7 +468,7 @@ Error Dataset::AppendMleDatasetTlv(Message &aMessage) const VerifyOrExit(mLength > 0); - type = (mType == kActive ? Mle::Tlv::kActiveDataset : Mle::Tlv::kPendingDataset); + type = (aType == kActive ? Mle::Tlv::kActiveDataset : Mle::Tlv::kPendingDataset); tlv.SetType(type); tlv.SetLength(static_cast(mLength) - sizeof(Tlv) - sizeof(Timestamp)); @@ -477,8 +476,8 @@ Error Dataset::AppendMleDatasetTlv(Message &aMessage) const for (const Tlv *cur = GetTlvsStart(); cur < GetTlvsEnd(); cur = cur->GetNext()) { - if (((mType == kActive) && (cur->GetType() == Tlv::kActiveTimestamp)) || - ((mType == kPending) && (cur->GetType() == Tlv::kPendingTimestamp))) + if (((aType == kActive) && (cur->GetType() == Tlv::kActiveTimestamp)) || + ((aType == kPending) && (cur->GetType() == Tlv::kPendingTimestamp))) { ; // skip Active or Pending Timestamp TLV } @@ -608,7 +607,6 @@ void Dataset::ConvertToActive(void) { RemoveTlv(Tlv::kPendingTimestamp); RemoveTlv(Tlv::kDelayTimer); - mType = kActive; } const char *Dataset::TypeToString(Type aType) diff --git a/src/core/meshcop/dataset.hpp b/src/core/meshcop/dataset.hpp index e96b939a2..796b5853d 100644 --- a/src/core/meshcop/dataset.hpp +++ b/src/core/meshcop/dataset.hpp @@ -615,10 +615,8 @@ public: /** * This constructor initializes the object. * - * @param[in] aType The type of the dataset, active or pending. - * */ - explicit Dataset(Type aType); + Dataset(void); /** * This method clears the Dataset. @@ -735,18 +733,21 @@ public: /** * This method returns a reference to the Timestamp. * + * @param[in] aType The type of the dataset, active or pending. + * * @returns A pointer to the Timestamp. * */ - const Timestamp *GetTimestamp(void) const; + const Timestamp *GetTimestamp(Type aType) const; /** * This method sets the Timestamp value. * + * @param[in] aType The type of the dataset, active or pending. * @param[in] aTimestamp A Timestamp. * */ - void SetTimestamp(const Timestamp &aTimestamp); + void SetTimestamp(Type aType, const Timestamp &aTimestamp); /** * This method sets a TLV in the Dataset. @@ -810,10 +811,11 @@ public: * If this Dataset is an Active Dataset, any Pending Timestamp and Delay Timer TLVs will be omitted in the copy * from @p aDataset. * + * @param[in] aType The type of the dataset, active or pending. * @param[in] aDataset The input Dataset. * */ - void Set(const Dataset &aDataset); + void Set(Type aType, const Dataset &aDataset); /** * This method sets the Dataset from a given structure representation. @@ -845,13 +847,14 @@ public: /** * This method 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(Message &aMessage) const; + Error AppendMleDatasetTlv(Type aType, Message &aMessage) const; /** * This method applies the Active or Pending Dataset to the Thread interface. @@ -923,7 +926,6 @@ private: uint8_t mTlvs[kMaxSize]; ///< The Dataset buffer TimeMilli mUpdateTime; ///< Local time last updated uint16_t mLength; ///< The number of valid bytes in @var mTlvs - Type mType; ///< Active or Pending }; /** diff --git a/src/core/meshcop/dataset_local.cpp b/src/core/meshcop/dataset_local.cpp index 1e84d85b8..f304986eb 100644 --- a/src/core/meshcop/dataset_local.cpp +++ b/src/core/meshcop/dataset_local.cpp @@ -77,7 +77,7 @@ Error DatasetLocal::Restore(Dataset &aDataset) SuccessOrExit(error); mSaved = true; - timestamp = aDataset.GetTimestamp(); + timestamp = aDataset.GetTimestamp(mType); if (timestamp != nullptr) { @@ -128,7 +128,7 @@ exit: Error DatasetLocal::Read(Dataset::Info &aDatasetInfo) const { - Dataset dataset(mType); + Dataset dataset; Error error; aDatasetInfo.Clear(); @@ -142,7 +142,7 @@ exit: Error DatasetLocal::Read(otOperationalDatasetTlvs &aDataset) const { - Dataset dataset(mType); + Dataset dataset; Error error; memset(&aDataset, 0, sizeof(aDataset)); @@ -157,7 +157,7 @@ exit: Error DatasetLocal::Save(const Dataset::Info &aDatasetInfo) { Error error; - Dataset dataset(mType); + Dataset dataset; SuccessOrExit(error = dataset.SetFrom(aDatasetInfo)); SuccessOrExit(error = Save(dataset)); @@ -168,7 +168,7 @@ exit: Error DatasetLocal::Save(const otOperationalDatasetTlvs &aDataset) { - Dataset dataset(mType); + Dataset dataset; dataset.SetFrom(aDataset); @@ -194,7 +194,7 @@ Error DatasetLocal::Save(const Dataset &aDataset) otLogInfoMeshCoP("%s dataset set", Dataset::TypeToString(mType)); } - timestamp = aDataset.GetTimestamp(); + timestamp = aDataset.GetTimestamp(mType); if (timestamp != nullptr) { diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 9dd70bc2a..9dafa81d9 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -81,7 +81,7 @@ int DatasetManager::Compare(const Timestamp &aTimestamp) const Error DatasetManager::Restore(void) { Error error; - Dataset dataset(GetType()); + Dataset dataset; const Timestamp *timestamp; mTimer.Stop(); @@ -90,7 +90,7 @@ Error DatasetManager::Restore(void) SuccessOrExit(error = mLocal.Restore(dataset)); - timestamp = dataset.GetTimestamp(); + timestamp = dataset.GetTimestamp(GetType()); if (timestamp != nullptr) { @@ -112,7 +112,7 @@ exit: Error DatasetManager::ApplyConfiguration(void) const { Error error; - Dataset dataset(GetType()); + Dataset dataset; SuccessOrExit(error = Read(dataset)); SuccessOrExit(error = dataset.ApplyConfiguration(GetInstance())); @@ -142,7 +142,7 @@ Error DatasetManager::Save(const Dataset &aDataset) int compare; bool isNetworkkeyUpdated = false; - timestamp = aDataset.GetTimestamp(); + timestamp = aDataset.GetTimestamp(GetType()); if (timestamp != nullptr) { @@ -249,7 +249,7 @@ Error DatasetManager::GetChannelMask(Mac::ChannelMask &aChannelMask) const Error error; const MeshCoP::ChannelMaskTlv *channelMaskTlv; uint32_t mask; - Dataset dataset(GetType()); + Dataset dataset; SuccessOrExit(error = Read(dataset)); @@ -275,7 +275,7 @@ void DatasetManager::SendSet(void) Error error; Coap::Message * message = nullptr; Ip6::MessageInfo messageInfo; - Dataset dataset(GetType()); + Dataset dataset; VerifyOrExit(!mCoapPending, error = kErrorBusy); VerifyOrExit(Get().IsChild() || Get().IsRouter(), error = kErrorInvalidState); @@ -283,7 +283,7 @@ void DatasetManager::SendSet(void) if (IsActiveDataset()) { - Dataset pendingDataset(Dataset::kPending); + Dataset pendingDataset; IgnoreError(Get().Read(pendingDataset)); const ActiveTimestampTlv *tlv = pendingDataset.GetTlv(); @@ -402,7 +402,7 @@ void DatasetManager::SendGetResponse(const Coap::Message & aRequest, { Error error = kErrorNone; Coap::Message *message; - Dataset dataset(GetType()); + Dataset dataset; IgnoreError(Read(dataset)); @@ -451,7 +451,7 @@ exit: Error DatasetManager::AppendDatasetToMessage(const Dataset::Info &aDatasetInfo, Message &aMessage) const { Error error; - Dataset dataset(GetType()); + Dataset dataset; SuccessOrExit(error = dataset.SetFrom(aDatasetInfo)); error = aMessage.AppendBytes(dataset.GetBytes(), dataset.GetSize()); @@ -670,10 +670,10 @@ exit: Error ActiveDataset::Save(const Timestamp &aTimestamp, const Message &aMessage, uint16_t aOffset, uint8_t aLength) { Error error = kErrorNone; - Dataset dataset(GetType()); + Dataset dataset; SuccessOrExit(error = dataset.Set(aMessage, aOffset, aLength)); - dataset.SetTimestamp(aTimestamp); + dataset.SetTimestamp(Dataset::kActive, aTimestamp); IgnoreError(DatasetManager::Save(dataset)); exit: @@ -715,7 +715,7 @@ void PendingDataset::Clear(void) void PendingDataset::ClearNetwork(void) { - Dataset dataset(GetType()); + Dataset dataset; mTimestamp.Init(); mTimestampValid = false; @@ -758,10 +758,10 @@ exit: Error PendingDataset::Save(const Timestamp &aTimestamp, const Message &aMessage, uint16_t aOffset, uint8_t aLength) { Error error = kErrorNone; - Dataset dataset(GetType()); + Dataset dataset; SuccessOrExit(error = dataset.Set(aMessage, aOffset, aLength)); - dataset.SetTimestamp(aTimestamp); + dataset.SetTimestamp(Dataset::kPending, aTimestamp); IgnoreError(DatasetManager::Save(dataset)); StartDelayTimer(); @@ -772,7 +772,7 @@ exit: void PendingDataset::StartDelayTimer(void) { DelayTimerTlv *delayTimer; - Dataset dataset(GetType()); + Dataset dataset; IgnoreError(Read(dataset)); @@ -801,7 +801,7 @@ void PendingDataset::HandleDelayTimer(Timer &aTimer) void PendingDataset::HandleDelayTimer(void) { DelayTimerTlv *delayTimer; - Dataset dataset(GetType()); + Dataset dataset; IgnoreError(Read(dataset)); diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 21d92b1a9..35b38246e 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -61,11 +61,11 @@ namespace MeshCoP { Error DatasetManager::AppendMleDatasetTlv(Message &aMessage) const { - Dataset dataset(GetType()); + Dataset dataset; IgnoreError(Read(dataset)); - return dataset.AppendMleDatasetTlv(aMessage); + return dataset.AppendMleDatasetTlv(GetType(), aMessage); } Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) @@ -79,7 +79,7 @@ Error DatasetManager::HandleSet(Coap::Message &aMessage, const Ip6::MessageInfo bool doesAffectNetworkKey = false; bool hasNetworkKey = false; StateTlv::State state = StateTlv::kReject; - Dataset dataset(GetType()); + Dataset dataset; ActiveTimestampTlv activeTimestamp; PendingTimestampTlv pendingTimestamp; @@ -309,7 +309,7 @@ exit: Error ActiveDataset::GenerateLocal(void) { Error error = kErrorNone; - Dataset dataset(GetType()); + Dataset dataset; VerifyOrExit(Get().IsAttached(), error = kErrorInvalidState); VerifyOrExit(!mLocal.IsTimestampPresent(), error = kErrorAlready); @@ -457,7 +457,7 @@ exit: void PendingDataset::ApplyActiveDataset(const Timestamp &aTimestamp, Coap::Message &aMessage) { uint16_t offset = aMessage.GetOffset(); - Dataset dataset(GetType()); + Dataset dataset; VerifyOrExit(Get().IsAttached()); @@ -474,7 +474,7 @@ void PendingDataset::ApplyActiveDataset(const Timestamp &aTimestamp, Coap::Messa IgnoreError(dataset.SetTlv(Tlv::kDelayTimer, Get().GetDelayTimerMinimal())); // add pending timestamp tlv - dataset.SetTimestamp(aTimestamp); + dataset.SetTimestamp(Dataset::kPending, aTimestamp); IgnoreError(DatasetManager::Save(dataset)); // reset delay timer diff --git a/src/core/meshcop/dataset_updater.cpp b/src/core/meshcop/dataset_updater.cpp index cd7392a7e..bce95055c 100644 --- a/src/core/meshcop/dataset_updater.cpp +++ b/src/core/meshcop/dataset_updater.cpp @@ -105,7 +105,7 @@ void DatasetUpdater::HandleTimer(void) void DatasetUpdater::PreparePendingDataset(void) { - Dataset dataset(Dataset::kPending); + Dataset dataset; MeshCoP::Dataset::Info requestedDataset; Error error; @@ -145,7 +145,7 @@ void DatasetUpdater::PreparePendingDataset(void) } timestamp.AdvanceRandomTicks(); - dataset.SetTimestamp(timestamp); + dataset.SetTimestamp(Dataset::kPending, timestamp); } { diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 041839d16..13dff8ca2 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -314,7 +314,7 @@ Coap::Message *JoinerRouter::PrepareJoinerEntrustMessage(void) { Error error; Coap::Message *message = nullptr; - Dataset dataset(Dataset::kActive); + Dataset dataset; NetworkNameTlv networkName; const Tlv * tlv; diff --git a/src/lib/spinel/radio_spinel_impl.hpp b/src/lib/spinel/radio_spinel_impl.hpp index c7d7f54ea..5a113e52a 100644 --- a/src/lib/spinel/radio_spinel_impl.hpp +++ b/src/lib/spinel/radio_spinel_impl.hpp @@ -601,7 +601,7 @@ otError RadioSpinel::ThreadDatasetHandler(con otOperationalDataset opDataset; bool isActive = ((mWaitingKey == SPINEL_PROP_THREAD_ACTIVE_DATASET) ? true : false); Spinel::Decoder decoder; - MeshCoP::Dataset dataset(isActive ? MeshCoP::Dataset::kActive : MeshCoP::Dataset::kPending); + MeshCoP::Dataset dataset; memset(&opDataset, 0, sizeof(otOperationalDataset)); decoder.Init(aBuffer, aLength);