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 1, 2024
1 parent e2eed3c commit 7873437
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 161 deletions.
173 changes: 101 additions & 72 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,21 @@ 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);
mHost = std::unique_ptr<otbr::Ncp::ThreadController>(
otbr::Ncp::ThreadController::CreateInstance(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
if (mHost->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
ConstructRcpMode();
}

OT_UNUSED_VARIABLE(aRestListenAddress);
OT_UNUSED_VARIABLE(aRestListenPort);
Expand All @@ -103,55 +87,18 @@ 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 +192,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 +202,86 @@ void Application::HandleSignal(int aSignal)
signal(aSignal, SIG_DFL);
}

void Application::ConstructRcpMode(void)
{
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, aInterfaceName, 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
}

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(void);
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
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
62 changes: 12 additions & 50 deletions src/ncp/rcp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,11 @@ static const uint16_t kThreadVersion12 = 3; ///< Thread Version 1.2
static const uint16_t kThreadVersion13 = 4; ///< Thread Version 1.3
static const uint16_t kThreadVersion14 = 5; ///< Thread Version 1.4

RcpHost::RcpHost(const char *aInterfaceName,
const std::vector<const char *> &aRadioUrls,
const char *aBackboneInterfaceName,
bool aDryRun,
bool aEnableAutoAttach)
: mInstance(nullptr)
RcpHost::RcpHost(otInstance *aInstance, const otPlatformConfig &aConfig, bool aEnableAutoAttach)
: mInstance(aInstance)
, mConfig(aConfig)
, mEnableAutoAttach(aEnableAutoAttach)
{
VerifyOrDie(aRadioUrls.size() <= OT_PLATFORM_CONFIG_MAX_RADIO_URLS, "Too many Radio URLs!");

memset(&mConfig, 0, sizeof(mConfig));

mConfig.mInterfaceName = aInterfaceName;
mConfig.mBackboneInterfaceName = aBackboneInterfaceName;
mConfig.mDryRun = aDryRun;

for (const char *url : aRadioUrls)
{
mConfig.mRadioUrls[mConfig.mRadioUrlNum++] = url;
}
mConfig.mSpeedUpFactor = 1;
}

RcpHost::~RcpHost(void)
Expand Down Expand Up @@ -164,36 +148,6 @@ otbrLogLevel ConvertProtoToOtbrLogLevel(ProtoLogLevel aProtoLogLevel)
}
#endif

otLogLevel RcpHost::ConvertToOtLogLevel(otbrLogLevel aLevel)
{
otLogLevel level;

switch (aLevel)
{
case OTBR_LOG_EMERG:
case OTBR_LOG_ALERT:
case OTBR_LOG_CRIT:
level = OT_LOG_LEVEL_CRIT;
break;
case OTBR_LOG_ERR:
case OTBR_LOG_WARNING:
level = OT_LOG_LEVEL_WARN;
break;
case OTBR_LOG_NOTICE:
level = OT_LOG_LEVEL_NOTE;
break;
case OTBR_LOG_INFO:
level = OT_LOG_LEVEL_INFO;
break;
case OTBR_LOG_DEBUG:
default:
level = OT_LOG_LEVEL_DEBG;
break;
}

return level;
}

otError RcpHost::SetOtbrAndOtLogLevel(otbrLogLevel aLevel)
{
otError error = OT_ERROR_NONE;
Expand All @@ -213,7 +167,15 @@ void RcpHost::Init(void)

VerifyOrExit(otLoggingSetLevel(level) == OT_ERROR_NONE, error = OTBR_ERROR_OPENTHREAD);

mInstance = otSysInit(&mConfig);
// There are two cases:
// 1. The `Init` is called after `CreateInstance`. In this case, `otSysInit` has been called
// and an otInstance has been created. So there is no need to create an instance again.
// 2. `Reset` is called. `Deinit` is called first and then `Init` is called again. In this
// case need to call `otSysInit` to reset the co-processor and creat an otInstance again.
if (mInstance == nullptr)
{
mInstance = otSysInit(&mConfig);
}
assert(mInstance != nullptr);

{
Expand Down
Loading

0 comments on commit 7873437

Please sign in to comment.