Skip to content

Commit

Permalink
[controller] refactor controller creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Irving-cl committed Jun 5, 2024
1 parent e2eed3c commit e7f80e8
Show file tree
Hide file tree
Showing 12 changed files with 366 additions and 165 deletions.
4 changes: 4 additions & 0 deletions src/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ add_executable(otbr-agent
vendor.hpp
)

target_link_libraries(openthread-radio-spinel INTERFACE
openthread-spinel-rcp
)

target_link_libraries(otbr-agent PRIVATE
$<$<BOOL:${OTBR_BORDER_AGENT}>:otbr-border-agent>
$<$<BOOL:${OTBR_BACKBONE_ROUTER}>:otbr-backbone-router>
Expand Down
178 changes: 103 additions & 75 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,95 +63,38 @@ Application::Application(const std::string &aInterfaceName,
#if OTBR_ENABLE_MDNS
, mPublisher(Mdns::Publisher::Create([this](Mdns::Publisher::State aState) { this->HandleMdnsState(aState); }))
#endif
#if OTBR_ENABLE_TREL
, mTrelDnssd(*mPublisher)
#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 = MakeUnique<BorderAgent>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
mBackboneAgent = MakeUnique<BackboneRouter::BackboneAgent>(*mHost, aInterfaceName, mBackboneInterfaceName);
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy = MakeUnique<AdvertisingProxy>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy = MakeUnique<Dnssd::DiscoveryProxy>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_TREL
mTrelDnssd = MakeUnique<TrelDnssd::TrelDnssd>(*mHost, *mPublisher);
#endif
#if OTBR_ENABLE_OPENWRT
mUbusAgent = MakeUnique<ubus::UBusAgent>(*mHost);
#endif
#if OTBR_ENABLE_REST_SERVER
mRestWebServer = MakeUnique<rest::RestWebServer>(*mHost, aRestListenAddress, aRestListenPort);
#endif
#if OTBR_ENABLE_DBUS_SERVER && OTBR_ENABLE_BORDER_AGENT
mDBusAgent = MakeUnique<DBus::DBusAgent>(*mHost, *mPublisher);
#endif
mHost = otbr::Ncp::ThreadController::CreateInstance(mInterfaceName.c_str(), aRadioUrls, mBackboneInterfaceName,
/* aDryRun */ false, aEnableAutoAttach);

OT_UNUSED_VARIABLE(aRestListenAddress);
OT_UNUSED_VARIABLE(aRestListenPort);
if (mHost->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
ConstructRcpMode(aRestListenAddress, aRestListenPort);
}
}

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

#if OTBR_ENABLE_MDNS
mPublisher->Start();
#endif
#if OTBR_ENABLE_BORDER_AGENT
// 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);
#else
mBorderAgent->SetEnabled(true);
#endif
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
mBackboneAgent->Init();
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy->SetEnabled(true);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy->SetEnabled(true);
#endif
#if OTBR_ENABLE_OPENWRT
mUbusAgent->Init();
#endif
#if OTBR_ENABLE_REST_SERVER
mRestWebServer->Init();
#endif
#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent->Init();
#endif
#if OTBR_ENABLE_VENDOR_SERVER
mVendorServer->Init();
#endif
if (mHost->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
InitRcpMode();
}
}

void Application::Deinit(void)
{
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy->SetEnabled(false);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy->SetEnabled(false);
#endif
#if OTBR_ENABLE_BORDER_AGENT
mBorderAgent->SetEnabled(false);
#endif
#if OTBR_ENABLE_MDNS
mPublisher->Stop();
#endif
if (mHost->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
DeinitRcpMode();
}

mHost->Deinit();
}
Expand Down Expand Up @@ -245,7 +188,7 @@ void Application::HandleMdnsState(Mdns::Publisher::State aState)
mDiscoveryProxy->HandleMdnsState(aState);
#endif
#if OTBR_ENABLE_TREL
mTrelDnssd->HandleMdnsState(aState);
mTrelDnssd.HandleMdnsState(aState);
#endif
}

Expand All @@ -255,4 +198,89 @@ void Application::HandleSignal(int aSignal)
signal(aSignal, SIG_DFL);
}

void Application::ConstructRcpMode(const std::string &aRestListenAddress, int aRestListenPort)
{
otbr::Ncp::RcpHost &rcpHost = static_cast<otbr::Ncp::RcpHost &>(*mHost);
#if OTBR_ENABLE_BORDER_AGENT
mBorderAgent = MakeUnique<BorderAgent>(rcpHost, *mPublisher);
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
mBackboneAgent = MakeUnique<BackboneRouter::BackboneAgent>(rcpHost, mInterfaceName, mBackboneInterfaceName);
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy = MakeUnique<AdvertisingProxy>(rcpHost, *mPublisher);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy = MakeUnique<Dnssd::DiscoveryProxy>(rcpHost, *mPublisher);
#endif
#if OTBR_ENABLE_OPENWRT
mUbusAgent = MakeUnique<ubus::UBusAgent>(rcpHost);
#endif
#if OTBR_ENABLE_REST_SERVER
mRestWebServer = MakeUnique<rest::RestWebServer>(rcpHost, aRestListenAddress, aRestListenPort);
#endif
#if OTBR_ENABLE_DBUS_SERVER && OTBR_ENABLE_BORDER_AGENT
mDBusAgent = MakeUnique<DBus::DBusAgent>(rcpHost, *mPublisher);
#endif
#if OTBR_ENABLE_TREL
mTrelDnssd.SetHost(rcpHost);
#endif

OT_UNUSED_VARIABLE(aRestListenAddress);
OT_UNUSED_VARIABLE(aRestListenPort);
}

void Application::InitRcpMode(void)
{
#if OTBR_ENABLE_MDNS
mPublisher->Start();
#endif
#if OTBR_ENABLE_BORDER_AGENT
// 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);
#else
mBorderAgent->SetEnabled(true);
#endif
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
mBackboneAgent->Init();
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy->SetEnabled(true);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy->SetEnabled(true);
#endif
#if OTBR_ENABLE_OPENWRT
mUbusAgent->Init();
#endif
#if OTBR_ENABLE_REST_SERVER
mRestWebServer->Init();
#endif
#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent->Init();
#endif
#if OTBR_ENABLE_VENDOR_SERVER
mVendorServer->Init();
#endif
}

void Application::DeinitRcpMode(void)
{
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy->SetEnabled(false);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy->SetEnabled(false);
#endif
#if OTBR_ENABLE_BORDER_AGENT
mBorderAgent->SetEnabled(false);
#endif
#if OTBR_ENABLE_MDNS
mPublisher->Stop();
#endif
}

} // namespace otbr
14 changes: 9 additions & 5 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::ThreadController &GetHost(void) { return *mHost; }

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

Expand Down Expand Up @@ -256,12 +256,16 @@ class Application : private NonCopyable

static void HandleSignal(int aSignal);

void ConstructRcpMode(const std::string &aRestListenAddress, int aRestListenPort);
void InitRcpMode(void);
void DeinitRcpMode(void);

std::string mInterfaceName;
#if __linux__
otbr::Utils::InfraLinkSelector mInfraLinkSelector;
#endif
const char *mBackboneInterfaceName;
std::unique_ptr<Ncp::RcpHost> mHost;
const char *mBackboneInterfaceName;
std::unique_ptr<Ncp::ThreadController> mHost;
#if OTBR_ENABLE_MDNS
std::unique_ptr<Mdns::Publisher> mPublisher;
#endif
Expand All @@ -278,7 +282,7 @@ class Application : private NonCopyable
std::unique_ptr<Dnssd::DiscoveryProxy> mDiscoveryProxy;
#endif
#if OTBR_ENABLE_TREL
std::unique_ptr<TrelDnssd::TrelDnssd> mTrelDnssd;
TrelDnssd::TrelDnssd mTrelDnssd;
#endif
#if OTBR_ENABLE_OPENWRT
std::unique_ptr<ubus::UBusAgent> mUbusAgent;
Expand Down
21 changes: 11 additions & 10 deletions src/agent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#include "common/logging.hpp"
#include "common/mainloop.hpp"
#include "common/types.hpp"
#include "ncp/rcp_host.hpp"
#include "ncp/thread_controller.hpp"

static const char kDefaultInterfaceName[] = "wpan0";

Expand Down Expand Up @@ -176,18 +176,19 @@ static otbrLogLevel GetDefaultLogLevel(void)

static void PrintRadioVersionAndExit(const std::vector<const char *> &aRadioUrls)
{
otbr::Ncp::RcpHost rcpHost{/* aInterfaceName */ "", aRadioUrls,
/* aBackboneInterfaceName */ "",
/* aDryRun */ true, /* aEnableAutoAttach */ false};
const char *radioVersion;
auto host = std::unique_ptr<otbr::Ncp::ThreadController>(
otbr::Ncp::ThreadController::CreateInstance(/* aInterfaceName */ "", aRadioUrls,
/* aBackboneInterfaceName */ "",
/* aDryRun */ true, /* aEnableAutoAttach */ false));
const char *coprocessorVersion;

rcpHost.Init();
host->Init();

radioVersion = otPlatRadioGetVersionString(rcpHost.GetInstance());
otbrLogNotice("Radio version: %s", radioVersion);
printf("%s\n", radioVersion);
coprocessorVersion = host->GetCoprocessorVersion();
otbrLogNotice("Co-processor version: %s", coprocessorVersion);
printf("%s\n", coprocessorVersion);

rcpHost.Deinit();
host->Deinit();

exit(EXIT_SUCCESS);
}
Expand Down
13 changes: 13 additions & 0 deletions src/common/code_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@
} \
} while (false)

/**
* This macro prints the message and terminates the program.
*
* @param[in] aMessage A message (text string) to print on failure.
*
*/
#define DieNow(aMessage) \
do \
{ \
otbrLogEmerg("FAILED %s:%d - %s", __FILE__, __LINE__, aMessage); \
exit(-1); \
} while (false)

/**
* This unconditionally executes @a ... and branches to the local
* label 'exit'.
Expand Down
2 changes: 2 additions & 0 deletions src/ncp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
add_library(otbr-ncp
rcp_host.cpp
rcp_host.hpp
thread_controller.cpp
thread_controller.hpp
)

target_link_libraries(otbr-ncp PRIVATE
Expand Down
Loading

0 comments on commit e7f80e8

Please sign in to comment.