Skip to content

Commit

Permalink
[application] refactor constructor to allow flexible initialization (#…
Browse files Browse the repository at this point in the history
…2302)

This commit refactors the constructor of the `Application` class so
that the initialization could be flexible. With this change, we can
choose to **not** initialize other submodules when the co-processor is
an NCP.
  • Loading branch information
Irving-cl authored May 29, 2024
1 parent 140247a commit a05cdc4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 51 deletions.
67 changes: 35 additions & 32 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,48 @@ Application::Application(const std::string &aInterfaceName,
#else
, mBackboneInterfaceName(aBackboneInterfaceNames.empty() ? "" : aBackboneInterfaceNames.front())
#endif
, mHost(mInterfaceName.c_str(), aRadioUrls, mBackboneInterfaceName, /* aDryRun */ false, aEnableAutoAttach)
#if OTBR_ENABLE_MDNS
, mPublisher(Mdns::Publisher::Create([this](Mdns::Publisher::State aState) { this->HandleMdnsState(aState); }))
#endif
#if OTBR_ENABLE_VENDOR_SERVER
, mVendorServer(vendor::VendorServer::newInstance(*this))
#endif
{
mHost = MakeUnique<Ncp::RcpHost>(mInterfaceName.c_str(), aRadioUrls, mBackboneInterfaceName,
/* aDryRun */ false, aEnableAutoAttach);

#if OTBR_ENABLE_BORDER_AGENT
, mBorderAgent(mHost, *mPublisher)
mBorderAgent = MakeUnique<BorderAgent>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
, mBackboneAgent(mHost, aInterfaceName, mBackboneInterfaceName)
mBackboneAgent = MakeUnique<BackboneRouter::BackboneAgent>(*mHost, aInterfaceName, mBackboneInterfaceName);
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
, mAdvertisingProxy(mHost, *mPublisher)
mAdvertisingProxy = MakeUnique<AdvertisingProxy>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
, mDiscoveryProxy(mHost, *mPublisher)
mDiscoveryProxy = MakeUnique<Dnssd::DiscoveryProxy>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_TREL
, mTrelDnssd(mHost, *mPublisher)
mTrelDnssd = MakeUnique<TrelDnssd::TrelDnssd>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_OPENWRT
, mUbusAgent(mHost)
mUbusAgent = MakeUnique<ubus::UBusAgent>(*mHost);
#endif
#if OTBR_ENABLE_REST_SERVER
, mRestWebServer(mHost, aRestListenAddress, aRestListenPort)
mRestWebServer = MakeUnique<rest::RestWebServer>(*mHost, aRestListenAddress, aRestListenPort);
#endif
#if OTBR_ENABLE_DBUS_SERVER && OTBR_ENABLE_BORDER_AGENT
, mDBusAgent(mHost, *mPublisher)
mDBusAgent = MakeUnique<DBus::DBusAgent>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_VENDOR_SERVER
, mVendorServer(vendor::VendorServer::newInstance(*this))
#endif
{
OTBR_UNUSED_VARIABLE(aRestListenAddress);
OTBR_UNUSED_VARIABLE(aRestListenPort);

OT_UNUSED_VARIABLE(aRestListenAddress);
OT_UNUSED_VARIABLE(aRestListenPort);
}

void Application::Init(void)
{
mHost.Init();
mHost->Init();

#if OTBR_ENABLE_MDNS
mPublisher->Start();
Expand All @@ -107,28 +110,28 @@ void Application::Init(void)
// This is for delaying publishing the MeshCoP service until the correct
// vendor name and OUI etc. are correctly set by BorderAgent::SetMeshCopServiceValues()
#if OTBR_STOP_BORDER_AGENT_ON_INIT
mBorderAgent.SetEnabled(false);
mBorderAgent->SetEnabled(false);
#else
mBorderAgent.SetEnabled(true);
mBorderAgent->SetEnabled(true);
#endif
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
mBackboneAgent.Init();
mBackboneAgent->Init();
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy.SetEnabled(true);
mAdvertisingProxy->SetEnabled(true);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy.SetEnabled(true);
mDiscoveryProxy->SetEnabled(true);
#endif
#if OTBR_ENABLE_OPENWRT
mUbusAgent.Init();
mUbusAgent->Init();
#endif
#if OTBR_ENABLE_REST_SERVER
mRestWebServer.Init();
mRestWebServer->Init();
#endif
#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent.Init();
mDBusAgent->Init();
#endif
#if OTBR_ENABLE_VENDOR_SERVER
mVendorServer->Init();
Expand All @@ -138,19 +141,19 @@ void Application::Init(void)
void Application::Deinit(void)
{
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy.SetEnabled(false);
mAdvertisingProxy->SetEnabled(false);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy.SetEnabled(false);
mDiscoveryProxy->SetEnabled(false);
#endif
#if OTBR_ENABLE_BORDER_AGENT
mBorderAgent.SetEnabled(false);
mBorderAgent->SetEnabled(false);
#endif
#if OTBR_ENABLE_MDNS
mPublisher->Stop();
#endif

mHost.Deinit();
mHost->Deinit();
}

otbrError Application::Run(void)
Expand Down Expand Up @@ -233,16 +236,16 @@ void Application::HandleMdnsState(Mdns::Publisher::State aState)
OTBR_UNUSED_VARIABLE(aState);

#if OTBR_ENABLE_BORDER_AGENT
mBorderAgent.HandleMdnsState(aState);
mBorderAgent->HandleMdnsState(aState);
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy.HandleMdnsState(aState);
mAdvertisingProxy->HandleMdnsState(aState);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy.HandleMdnsState(aState);
mDiscoveryProxy->HandleMdnsState(aState);
#endif
#if OTBR_ENABLE_TREL
mTrelDnssd.HandleMdnsState(aState);
mTrelDnssd->HandleMdnsState(aState);
#endif
}

Expand Down
38 changes: 19 additions & 19 deletions src/agent/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Application : private NonCopyable
*
* @returns The OpenThread controller object.
*/
Ncp::RcpHost &GetNcp(void) { return mHost; }
Ncp::RcpHost &GetNcp(void) { return *mHost; }

#if OTBR_ENABLE_MDNS
/**
Expand All @@ -154,7 +154,7 @@ class Application : private NonCopyable
*/
BorderAgent &GetBorderAgent(void)
{
return mBorderAgent;
return *mBorderAgent;
}
#endif

Expand All @@ -166,7 +166,7 @@ class Application : private NonCopyable
*/
BackboneRouter::BackboneAgent &GetBackboneAgent(void)
{
return mBackboneAgent;
return *mBackboneAgent;
}
#endif

Expand All @@ -178,7 +178,7 @@ class Application : private NonCopyable
*/
AdvertisingProxy &GetAdvertisingProxy(void)
{
return mAdvertisingProxy;
return *mAdvertisingProxy;
}
#endif

Expand All @@ -190,7 +190,7 @@ class Application : private NonCopyable
*/
Dnssd::DiscoveryProxy &GetDiscoveryProxy(void)
{
return mDiscoveryProxy;
return *mDiscoveryProxy;
}
#endif

Expand All @@ -202,7 +202,7 @@ class Application : private NonCopyable
*/
TrelDnssd::TrelDnssd &GetTrelDnssd(void)
{
return mTrelDnssd;
return *mTrelDnssd;
}
#endif

Expand All @@ -214,7 +214,7 @@ class Application : private NonCopyable
*/
ubus::UBusAgent &GetUBusAgent(void)
{
return mUbusAgent;
return *mUbusAgent;
}
#endif

Expand All @@ -226,7 +226,7 @@ class Application : private NonCopyable
*/
rest::RestWebServer &GetRestWebServer(void)
{
return mRestWebServer;
return *mRestWebServer;
}
#endif

Expand All @@ -238,7 +238,7 @@ class Application : private NonCopyable
*/
DBus::DBusAgent &GetDBusAgent(void)
{
return mDBusAgent;
return *mDBusAgent;
}
#endif

Expand All @@ -260,34 +260,34 @@ class Application : private NonCopyable
#if __linux__
otbr::Utils::InfraLinkSelector mInfraLinkSelector;
#endif
const char *mBackboneInterfaceName;
Ncp::RcpHost mHost;
const char *mBackboneInterfaceName;
std::unique_ptr<Ncp::RcpHost> mHost;
#if OTBR_ENABLE_MDNS
std::unique_ptr<Mdns::Publisher> mPublisher;
#endif
#if OTBR_ENABLE_BORDER_AGENT
BorderAgent mBorderAgent;
std::unique_ptr<BorderAgent> mBorderAgent;
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
BackboneRouter::BackboneAgent mBackboneAgent;
std::unique_ptr<BackboneRouter::BackboneAgent> mBackboneAgent;
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
AdvertisingProxy mAdvertisingProxy;
std::unique_ptr<AdvertisingProxy> mAdvertisingProxy;
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
Dnssd::DiscoveryProxy mDiscoveryProxy;
std::unique_ptr<Dnssd::DiscoveryProxy> mDiscoveryProxy;
#endif
#if OTBR_ENABLE_TREL
TrelDnssd::TrelDnssd mTrelDnssd;
std::unique_ptr<TrelDnssd::TrelDnssd> mTrelDnssd;
#endif
#if OTBR_ENABLE_OPENWRT
ubus::UBusAgent mUbusAgent;
std::unique_ptr<ubus::UBusAgent> mUbusAgent;
#endif
#if OTBR_ENABLE_REST_SERVER
rest::RestWebServer mRestWebServer;
std::unique_ptr<rest::RestWebServer> mRestWebServer;
#endif
#if OTBR_ENABLE_DBUS_SERVER
DBus::DBusAgent mDBusAgent;
std::unique_ptr<DBus::DBusAgent> mDBusAgent;
#endif
#if OTBR_ENABLE_VENDOR_SERVER
std::shared_ptr<vendor::VendorServer> mVendorServer;
Expand Down

0 comments on commit a05cdc4

Please sign in to comment.