Skip to content

Commit

Permalink
[utils] make generic VendorServer (#1995)
Browse files Browse the repository at this point in the history
This commit changes VendorServer to an abstract class so that it
can be extended to have different implementations.

Fulfills request from #1967 (comment)

Change-Id: Ic1b41b8b60e5953142854f761e377e080f1937fc
  • Loading branch information
wgtdkp authored and jwhui committed Sep 6, 2023
1 parent 1b2aeb7 commit 208891f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -104,7 +104,7 @@ void Application::Init(void)
mDBusAgent.Init();
#endif
#if OTBR_ENABLE_VENDOR_SERVER
mVendorServer.Init();
mVendorServer->Init();
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/agent/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Application : private NonCopyable
DBus::DBusAgent mDBusAgent;
#endif
#if OTBR_ENABLE_VENDOR_SERVER
vendor::VendorServer mVendorServer;
std::shared_ptr<vendor::VendorServer> mVendorServer;
#endif

static std::atomic_bool sShouldTerminate;
Expand Down
27 changes: 16 additions & 11 deletions src/agent/vendor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,34 @@
namespace otbr {
namespace vendor {

/**
* An interface for customized behavior depending on OpenThread API and state.
*/
class VendorServer
{
public:
virtual ~VendorServer(void) = default;

/**
* The constructor of vendor server.
* Creates a new instance of VendorServer.
*
* @param[in] aNcp A reference to the NCP controller.
* Custom vendor servers should implement this method to return an object of the derived class.
*
* @param[in] aNcp The OpenThread controller object.
*
* @returns New derived VendorServer instance.
*/
VendorServer(otbr::Ncp::ControllerOpenThread &aNcp)
: mNcp(aNcp)
{
}
static std::shared_ptr<VendorServer> 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_
12 changes: 4 additions & 8 deletions src/ncp/ncp_openthread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,12 @@ class ControllerOpenThread : public MainloopProcessor
void Deinit(void);

/**
* This method get mInstance pointer.
*
* @retval The pointer of mInstance.
* Returns an OpenThread instance.
*
* @retval Non-null OpenThread instance if `ControllerOpenThread::Init()` has been called.
* Otherwise, it's guaranteed to be `null`
*/
otInstance *GetInstance(void)
{
assert(mInstance != nullptr);
return mInstance;
}
otInstance *GetInstance(void) { return mInstance; }

/**
* This method gets the thread functionality helper.
Expand Down

0 comments on commit 208891f

Please sign in to comment.