From c1add348a6c78f601530fb6f0d16468f8a0e2970 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 20 May 2024 08:56:43 -0700 Subject: [PATCH] [dataset-manager] combine `protected` and `private` sections (#10267) This commit combines the `protected` and `private` sections of `DatasetManager`, adding `ActiveDatasetManager` and `PendingDatasetManager` as `friend`s. This simplifies the code and aligns with the goal of having more functions handled by the `DatasetManager` base class. --- src/core/meshcop/dataset_manager.hpp | 125 +++++++---------------- src/core/meshcop/dataset_manager_ftd.cpp | 9 ++ 2 files changed, 45 insertions(+), 89 deletions(-) diff --git a/src/core/meshcop/dataset_manager.hpp b/src/core/meshcop/dataset_manager.hpp index 7827d4ebd..b106ded32 100644 --- a/src/core/meshcop/dataset_manager.hpp +++ b/src/core/meshcop/dataset_manager.hpp @@ -51,10 +51,12 @@ namespace ot { namespace MeshCoP { +class ActiveDatasetManager; class PendingDatasetManager; class DatasetManager : public InstanceLocator { + friend class ActiveDatasetManager; friend class PendingDatasetManager; public: @@ -249,83 +251,11 @@ public: Error AppendMleDatasetTlv(Message &aMessage) const; #endif -protected: - /** - * Initializes the object. - * - * @param[in] aInstance A reference to the OpenThread instance. - * @param[in] aType Dataset type, Active or Pending. - * @param[in] aTimerHandler The registration timer handler. - * - */ - DatasetManager(Instance &aInstance, Dataset::Type aType, TimerMilli::Handler aTimerHandler); - - /** - * Gets the Operational Dataset type (Active or Pending). - * - * @returns The Operational Dataset type. - * - */ - Dataset::Type GetType(void) const { return mLocal.GetType(); } - - /** - * Clears the Operational Dataset. - * - */ - void Clear(void); - - /** - * Handles a MGMT_GET request message. - * - * @param[in] aMessage The CoAP message buffer. - * @param[in] aMessageInfo The message info. - * - */ - void HandleGet(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) const; - - /** - * Compares the partition's Operational Dataset with that stored in non-volatile memory. - * - * If the partition's Operational Dataset is newer, the non-volatile storage is updated. - * If the partition's Operational Dataset is older, the registration process is started. - * - */ - void HandleNetworkUpdate(void); - - /** - * Initiates a network data registration message with the Leader. - * - */ - void HandleTimer(void); - -#if OPENTHREAD_FTD - enum MgmtCommand : uint8_t ///< A MGMT command type. - { - kMgmtSet, ///< MGMT_SET command - kMgmtReplace, ///< MGMT_REPLACE command - }; - - /** - * Handles the MGMT_SET or MGMT_REPLACE request message. - * - * - * @param[in] aCommand The MGMT command type (MGMT_REPLACE or MGMT_SET). - * @param[in] aMessage The CoAP message buffer. - * @param[in] aMessageInfo The message info. - * - * @retval kErrorNone The request message was handled successfully. - * @retval kErrorDrop The request message was dropped. - * - */ - Error HandleSetOrReplace(MgmtCommand aCommand, const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); -#endif - - DatasetLocal mLocal; - Timestamp mTimestamp; - bool mTimestampValid : 1; - private: - static constexpr uint8_t kMaxGetTypes = 64; // Max number of types in MGMT_GET.req + static constexpr uint8_t kMaxGetTypes = 64; // Max number of types in MGMT_GET.req + static constexpr uint32_t kSendSetDelay = 5000; // in msec. + + using Type = Dataset::Type; class TlvList : public Array { @@ -335,6 +265,12 @@ private: }; #if OPENTHREAD_FTD + enum MgmtCommand : uint8_t + { + kMgmtSet, + kMgmtReplace, + }; + struct RequestInfo : Clearable // Info from a MGMT_SET or MGMT_REPLACE request. { Dataset mDataset; @@ -343,14 +279,15 @@ private: bool mAffectsNetworkKey; }; #endif - static void HandleMgmtSetResponse(void *aContext, - otMessage *aMessage, - const otMessageInfo *aMessageInfo, - Error aError); - void HandleMgmtSetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aError); - bool IsActiveDataset(void) const { return GetType() == Dataset::kActive; } - bool IsPendingDataset(void) const { return GetType() == Dataset::kPending; } + DatasetManager(Instance &aInstance, Type aType, TimerMilli::Handler aTimerHandler); + + Type GetType(void) const { return mLocal.GetType(); } + bool IsActiveDataset(void) const { return mLocal.GetType() == Dataset::kActive; } + bool IsPendingDataset(void) const { return mLocal.GetType() == Dataset::kPending; } + void Clear(void); + void HandleGet(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo) const; + void HandleTimer(void); Error Save(const Dataset &aDataset, bool aAllowOlderTimestamp); void SignalDatasetChange(void) const; void SyncLocalWithLeader(const Dataset &aDataset); @@ -358,23 +295,31 @@ private: void SendGetResponse(const Coap::Message &aRequest, const Ip6::MessageInfo &aMessageInfo, const TlvList &aTlvList) const; + void HandleMgmtSetResponse(Coap::Message *aMessage, const Ip6::MessageInfo *aMessageInfo, Error aError); + + static void HandleMgmtSetResponse(void *aContext, + otMessage *aMessage, + const otMessageInfo *aMessageInfo, + Error aError); #if OPENTHREAD_FTD + Error HandleSetOrReplace(MgmtCommand aCommand, const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); Error ProcessSetOrReplaceRequest(MgmtCommand aCommand, const Coap::Message &aMessage, RequestInfo &aInfo) const; void SendSetOrReplaceResponse(const Coap::Message &aRequest, const Ip6::MessageInfo &aMessageInfo, StateTlv::State aState); #endif - static constexpr uint8_t kMaxDatasetTlvs = 16; // Maximum number of TLVs in a Dataset. - static constexpr uint32_t kSendSetDelay = 5000; // Milliseconds - - bool mMgmtPending : 1; - TimerMilli mTimer; - + DatasetLocal mLocal; + Timestamp mTimestamp; + bool mTimestampValid : 1; + bool mMgmtPending : 1; + TimerMilli mTimer; Callback mMgmtSetCallback; }; +//---------------------------------------------------------------------------------------------------------------------- + class ActiveDatasetManager : public DatasetManager, private NonCopyable { friend class Tmf::Agent; @@ -469,6 +414,8 @@ DeclareTmfHandler(ActiveDatasetManager, kUriActiveSet); DeclareTmfHandler(ActiveDatasetManager, kUriActiveReplace); #endif +//---------------------------------------------------------------------------------------------------------------------- + class PendingDatasetManager : public DatasetManager, private NonCopyable { friend class Tmf::Agent; diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 8ca169e8e..97e190f30 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -62,6 +62,9 @@ namespace MeshCoP { RegisterLogModule("DatasetManager"); +//---------------------------------------------------------------------------------------------------------------------- +// DatasetManager + Error DatasetManager::AppendMleDatasetTlv(Message &aMessage) const { Mle::Tlv::Type mleTlvType = IsActiveDataset() ? Mle::Tlv::kActiveDataset : Mle::Tlv::kPendingDataset; @@ -286,6 +289,9 @@ exit: FreeMessageOnError(message, error); } +//---------------------------------------------------------------------------------------------------------------------- +// ActiveDatasetManager + #if OPENTHREAD_CONFIG_OPERATIONAL_DATASET_AUTO_INIT Error ActiveDatasetManager::GenerateLocal(void) { @@ -410,6 +416,9 @@ exit: return; } +//---------------------------------------------------------------------------------------------------------------------- +// PendingDatasetManager + void PendingDatasetManager::StartLeader(void) { StartDelayTimer(); } template <>