Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[controller] allows polymorphism of the controller #2309

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
177 changes: 102 additions & 75 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,98 +60,40 @@ Application::Application(const std::string &aInterfaceName,
#else
, mBackboneInterfaceName(aBackboneInterfaceNames.empty() ? "" : aBackboneInterfaceNames.front())
#endif
, mHost(Ncp::ThreadController::Create(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 = 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

OT_UNUSED_VARIABLE(aRestListenAddress);
OT_UNUSED_VARIABLE(aRestListenPort);
if (mHost->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
CreateRcpMode(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 @@ -255,4 +197,89 @@ void Application::HandleSignal(int aSignal)
signal(aSignal, SIG_DFL);
}

void Application::CreateRcpMode(const std::string &aRestListenAddress, int aRestListenPort)
{
otbr::Ncp::RcpHost &rcpHost = static_cast<otbr::Ncp::RcpHost &>(*mHost);
superwhd marked this conversation as resolved.
Show resolved Hide resolved
#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_TREL
mTrelDnssd = MakeUnique<TrelDnssd::TrelDnssd>(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

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
10 changes: 7 additions & 3 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 @@ -256,12 +256,16 @@ class Application : private NonCopyable

static void HandleSignal(int aSignal);

void CreateRcpMode(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 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::Create(/* 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.
*
*/
#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
30 changes: 0 additions & 30 deletions src/ncp/rcp_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,36 +164,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 Down
22 changes: 15 additions & 7 deletions src/ncp/rcp_host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ class RcpHost : public MainloopProcessor, public ThreadController
bool aEnableAutoAttach);

/**
* This method initialize the NCP controller.
* This method initialize the Thread controller.
*
*/
void Init(void);
void Init(void) override;

/**
* This method deinitialize the NCP controller.
* This method deinitialize the Thread controller.
*
*/
void Deinit(void);
void Deinit(void) override;

/**
* Returns an OpenThread instance.
Expand Down Expand Up @@ -194,9 +194,19 @@ class RcpHost : public MainloopProcessor, public ThreadController

~RcpHost(void) override;

// Thread Control APIs
// Thread Control virtual methods
void GetDeviceRole(const DeviceRoleHandler aHandler) override;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in a future PR: make this parameter either a non-const value or a const reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SG. Let's do it in a seperate PR.


CoprocessorType GetCoprocessorType(void) override
{
return OT_COPROCESSOR_RCP;
}

const char *GetCoprocessorVersion(void) override
{
return otPlatRadioGetVersionString(mInstance);
}

private:
static void HandleStateChanged(otChangedFlags aFlags, void *aContext)
{
Expand All @@ -220,8 +230,6 @@ class RcpHost : public MainloopProcessor, public ThreadController
bool IsAutoAttachEnabled(void);
void DisableAutoAttach(void);

static otLogLevel ConvertToOtLogLevel(otbrLogLevel aLevel);

otError SetOtbrAndOtLogLevel(otbrLogLevel aLevel);

otInstance *mInstance;
Expand Down
Loading
Loading