diff --git a/src/core/common/settings.cpp b/src/core/common/settings.cpp index 06f672ba7..c69b32261 100644 --- a/src/core/common/settings.cpp +++ b/src/core/common/settings.cpp @@ -210,9 +210,14 @@ void Settings::Wipe(void) LogInfo("Wiped all info"); } -Error Settings::SaveOperationalDataset(bool aIsActive, const MeshCoP::Dataset &aDataset) +Settings::Key Settings::KeyForDatasetType(MeshCoP::Dataset::Type aType) { - Key key = (aIsActive ? kKeyActiveDataset : kKeyPendingDataset); + return (aType == MeshCoP::Dataset::kActive) ? kKeyActiveDataset : kKeyPendingDataset; +} + +Error Settings::SaveOperationalDataset(MeshCoP::Dataset::Type aType, const MeshCoP::Dataset &aDataset) +{ + Key key = KeyForDatasetType(aType); Error error = Get().Set(key, aDataset.GetBytes(), aDataset.GetSize()); Log(kActionSave, error, key); @@ -220,13 +225,12 @@ Error Settings::SaveOperationalDataset(bool aIsActive, const MeshCoP::Dataset &a return error; } -Error Settings::ReadOperationalDataset(bool aIsActive, MeshCoP::Dataset &aDataset) const +Error Settings::ReadOperationalDataset(MeshCoP::Dataset::Type aType, MeshCoP::Dataset &aDataset) const { Error error = kErrorNone; uint16_t length = MeshCoP::Dataset::kMaxSize; - SuccessOrExit(error = Get().Get(aIsActive ? kKeyActiveDataset : kKeyPendingDataset, - aDataset.GetBytes(), &length)); + SuccessOrExit(error = Get().Get(KeyForDatasetType(aType), aDataset.GetBytes(), &length)); VerifyOrExit(length <= MeshCoP::Dataset::kMaxSize, error = kErrorNotFound); aDataset.SetSize(length); @@ -235,9 +239,9 @@ exit: return error; } -Error Settings::DeleteOperationalDataset(bool aIsActive) +Error Settings::DeleteOperationalDataset(MeshCoP::Dataset::Type aType) { - Key key = (aIsActive ? kKeyActiveDataset : kKeyPendingDataset); + Key key = KeyForDatasetType(aType); Error error = Get().Delete(key); Log(kActionDelete, error, key); diff --git a/src/core/common/settings.hpp b/src/core/common/settings.hpp index da227f5e8..1758feba2 100644 --- a/src/core/common/settings.hpp +++ b/src/core/common/settings.hpp @@ -47,16 +47,13 @@ #include "common/settings_driver.hpp" #include "crypto/ecdsa.hpp" #include "mac/mac_types.hpp" +#include "meshcop/dataset.hpp" #include "net/ip6_address.hpp" #include "utils/flash.hpp" #include "utils/slaac_address.hpp" namespace ot { -namespace MeshCoP { -class Dataset; -} - class Settings; /** @@ -799,38 +796,38 @@ public: /** * This method saves the Operational Dataset (active or pending). * - * @param[in] aIsActive Indicates whether Dataset is active or pending. + * @param[in] aType The Dataset type (active or pending) to save. * @param[in] aDataset A reference to a `Dataset` object to be saved. * * @retval kErrorNone Successfully saved the Dataset. * @retval kErrorNotImplemented The platform does not implement settings functionality. * */ - Error SaveOperationalDataset(bool aIsActive, const MeshCoP::Dataset &aDataset); + Error SaveOperationalDataset(MeshCoP::Dataset::Type aType, const MeshCoP::Dataset &aDataset); /** * This method reads the Operational Dataset (active or pending). * - * @param[in] aIsActive Indicates whether Dataset is active or pending. - * @param[out] aDataset A reference to a `Dataset` object to output the read content. + * @param[in] aType The Dataset type (active or pending) to read. + * @param[out] aDataset A reference to a `Dataset` object to output the read content. * * @retval kErrorNone Successfully read the Dataset. * @retval kErrorNotFound No corresponding value in the setting store. * @retval kErrorNotImplemented The platform does not implement settings functionality. * */ - Error ReadOperationalDataset(bool aIsActive, MeshCoP::Dataset &aDataset) const; + Error ReadOperationalDataset(MeshCoP::Dataset::Type aType, MeshCoP::Dataset &aDataset) const; /** * This method deletes the Operational Dataset (active/pending) from settings. * - * @param[in] aIsActive Indicates whether Dataset is active or pending. + * @param[in] aType The Dataset type (active or pending) to delete. * * @retval kErrorNone Successfully deleted the Dataset. * @retval kErrorNotImplemented The platform does not implement settings functionality. * */ - Error DeleteOperationalDataset(bool aIsActive); + Error DeleteOperationalDataset(MeshCoP::Dataset::Type aType); /** * This template method reads a specified settings entry. @@ -1114,6 +1111,8 @@ private: }; #endif + static Key KeyForDatasetType(MeshCoP::Dataset::Type aType); + Error ReadEntry(Key aKey, void *aValue, uint16_t aMaxLength) const; Error SaveEntry(Key aKey, const void *aValue, void *aPrev, uint16_t aLength); Error DeleteEntry(Key aKey); diff --git a/src/core/meshcop/dataset_local.cpp b/src/core/meshcop/dataset_local.cpp index 4607ca0ab..dfcb07713 100644 --- a/src/core/meshcop/dataset_local.cpp +++ b/src/core/meshcop/dataset_local.cpp @@ -66,7 +66,7 @@ void DatasetLocal::Clear(void) #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE DestroySecurelyStoredKeys(); #endif - IgnoreError(Get().DeleteOperationalDataset(IsActive())); + IgnoreError(Get().DeleteOperationalDataset(mType)); mTimestamp.Clear(); mTimestampPresent = false; mSaved = false; @@ -94,7 +94,7 @@ Error DatasetLocal::Read(Dataset &aDataset) const uint32_t elapsed; Error error; - error = Get().ReadOperationalDataset(IsActive(), aDataset); + error = Get().ReadOperationalDataset(mType, aDataset); VerifyOrExit(error == kErrorNone, aDataset.mLength = 0); #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE @@ -189,7 +189,7 @@ Error DatasetLocal::Save(const Dataset &aDataset) if (aDataset.GetSize() == 0) { // do not propagate error back - IgnoreError(Get().DeleteOperationalDataset(IsActive())); + IgnoreError(Get().DeleteOperationalDataset(mType)); mSaved = false; LogInfo("%s dataset deleted", Dataset::TypeToString(mType)); } @@ -201,9 +201,9 @@ Error DatasetLocal::Save(const Dataset &aDataset) dataset.Set(GetType(), aDataset); MoveKeysToSecureStorage(dataset); - SuccessOrExit(error = Get().SaveOperationalDataset(IsActive(), dataset)); + SuccessOrExit(error = Get().SaveOperationalDataset(mType, dataset)); #else - SuccessOrExit(error = Get().SaveOperationalDataset(IsActive(), aDataset)); + SuccessOrExit(error = Get().SaveOperationalDataset(mType, aDataset)); #endif mSaved = true; @@ -320,7 +320,7 @@ void DatasetLocal::EmplaceSecurelyStoredKeys(Dataset &aDataset) const dataset.Set(GetType(), aDataset); MoveKeysToSecureStorage(dataset); - SuccessOrAssert(error = Get().SaveOperationalDataset(IsActive(), dataset)); + SuccessOrAssert(error = Get().SaveOperationalDataset(mType, dataset)); } } #endif