From 6a9488b9c4d2bdf52e7f79cd358e127bf9443650 Mon Sep 17 00:00:00 2001 From: Kangping Dong Date: Mon, 4 Sep 2023 20:32:47 +0800 Subject: [PATCH] [utils] make generic VendorServer (#1995) This commit changes VendorServer to an abstract class so that it can be extended to have different implementations. Fulfills request from https://github.com/openthread/ot-br-posix/pull/1967#issuecomment-1702407419 Change-Id: Ic322efbee9b58d7a2743c72a511572ec7ebfb262 --- src/agent/application.cpp | 4 ++-- src/agent/application.hpp | 2 +- src/agent/vendor.hpp | 25 +++++++++++++------------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/agent/application.cpp b/src/agent/application.cpp index 6d7499676bc..c9a5598f2ff 100644 --- a/src/agent/application.cpp +++ b/src/agent/application.cpp @@ -77,7 +77,7 @@ Application::Application(const std::string &aInterfaceName, , mDBusAgent(mNcp, mBorderAgent.GetPublisher()) #endif #if OTBR_ENABLE_VENDOR_SERVER - , mVendorServer(mNcp) + , mVendorServer(vendor::VendorServer::newInstance(mNcp)) #endif { OTBR_UNUSED_VARIABLE(aRestListenAddress); @@ -104,7 +104,7 @@ void Application::Init(void) mDBusAgent.Init(); #endif #if OTBR_ENABLE_VENDOR_SERVER - mVendorServer.Init(); + mVendorServer->Init(); #endif } diff --git a/src/agent/application.hpp b/src/agent/application.hpp index 9bfeee33e66..4d392c33063 100644 --- a/src/agent/application.hpp +++ b/src/agent/application.hpp @@ -147,7 +147,7 @@ class Application : private NonCopyable DBus::DBusAgent mDBusAgent; #endif #if OTBR_ENABLE_VENDOR_SERVER - vendor::VendorServer mVendorServer; + std::shared_ptr mVendorServer; #endif static std::atomic_bool sShouldTerminate; diff --git a/src/agent/vendor.hpp b/src/agent/vendor.hpp index 833072225ed..61be192522d 100644 --- a/src/agent/vendor.hpp +++ b/src/agent/vendor.hpp @@ -41,29 +41,30 @@ namespace otbr { namespace vendor { +/** + * An interface for customized behavior depending on Openthread API and state. + */ class VendorServer { public: + ~VendorServer(void) = default; + /** - * The constructor of vendor server. - * - * @param[in] aNcp A reference to the NCP controller. + * Creates a new instance of VendorServer. * + * Custom vendor servers should implement this method to return an object of the derived class. */ - VendorServer(otbr::Ncp::ControllerOpenThread &aNcp) - : mNcp(aNcp) - { - } + static std::shared_ptr newInstance(otbr::Ncp::ControllerOpenThread &aNcp); /** - * This method initializes the vendor server. + * Initializes the vendor server. * + * This will be called by `Application::Init()` after OpenThread instance and other built-in + * servers have been created and initialized. */ - void Init(void); - -private: - otbr::Ncp::ControllerOpenThread &mNcp; + virtual void Init(void) = 0; }; + } // namespace vendor } // namespace otbr #endif // OTBR_AGENT_VENDOR_HPP_