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

[ncp] add initial support of NCP #2283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/thread_control_api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# Copyright (c) 2024, The OpenThread Authors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

name: ThreadControlApi

on:
push:
branches-ignore:
- 'dependabot/**'
pull_request:
branches:
- 'main'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || (github.repository == 'openthread/ot-br-posix' && github.run_id) || github.ref }}
cancel-in-progress: true

jobs:

thread_control_api:
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Bootstrap
env:
BUILD_TARGET: "check"
OTBR_MDNS: "avahi"
run: tests/scripts/bootstrap.sh
- name: Build
env:
OTBR_MDNS: "avahi"
- name: Test Thread Control Apis
run: OTBR_VERBOSE=${RUNNER_DEBUG:-0} script/test thread_control_api
- name: Codecov
uses: codecov/codecov-action@v4
3 changes: 3 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ main()
simulation)
do_simulation
;;
thread_control_api)
top_builddir="${OTBR_TOP_BUILDDIR}" print_result ./tests/scripts/thread_control_api
;;
package)
do_package
;;
Expand Down
82 changes: 58 additions & 24 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "agent/application.hpp"
#include "common/code_utils.hpp"
#include "common/mainloop_manager.hpp"
#include "ncp/controller_openthread_ncp.hpp"
#include "ncp/controller_openthread_rcp.hpp"
#include "utils/infra_link_selector.hpp"

namespace otbr {
Expand All @@ -49,8 +51,6 @@ const struct timeval Application::kPollTimeout = {10, 0};

Application::Application(const std::string &aInterfaceName,
const std::vector<const char *> &aBackboneInterfaceNames,
const std::vector<const char *> &aRadioUrls,
bool aEnableAutoAttach,
const std::string &aRestListenAddress,
int aRestListenPort)
: mInterfaceName(aInterfaceName)
Expand All @@ -60,33 +60,33 @@ Application::Application(const std::string &aInterfaceName,
#else
, mBackboneInterfaceName(aBackboneInterfaceNames.empty() ? "" : aBackboneInterfaceNames.front())
#endif
, mNcp(mInterfaceName.c_str(), aRadioUrls, mBackboneInterfaceName, /* aDryRun */ false, aEnableAutoAttach)
, mCtrlr(nullptr)
#if OTBR_ENABLE_MDNS
, mPublisher(Mdns::Publisher::Create([this](Mdns::Publisher::State aState) { this->HandleMdnsState(aState); }))
#endif
#if OTBR_ENABLE_BORDER_AGENT
, mBorderAgent(mNcp, *mPublisher)
, mBorderAgent(*mPublisher)
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
, mBackboneAgent(mNcp, aInterfaceName, mBackboneInterfaceName)
, mBackboneAgent(aInterfaceName, mBackboneInterfaceName)
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
, mAdvertisingProxy(mNcp, *mPublisher)
, mAdvertisingProxy(*mPublisher)
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
, mDiscoveryProxy(mNcp, *mPublisher)
, mDiscoveryProxy(*mPublisher)
#endif
#if OTBR_ENABLE_TREL
, mTrelDnssd(mNcp, *mPublisher)
, mTrelDnssd(*mPublisher)
#endif
#if OTBR_ENABLE_OPENWRT
, mUbusAgent(mNcp)
, mUbusAgent()
#endif
#if OTBR_ENABLE_REST_SERVER
, mRestWebServer(mNcp, aRestListenAddress, aRestListenPort)
, mRestWebServer(aRestListenAddress, aRestListenPort)
#endif
#if OTBR_ENABLE_DBUS_SERVER && OTBR_ENABLE_BORDER_AGENT
, mDBusAgent(mNcp, *mPublisher)
, mDBusAgent(*mPublisher)
#endif
#if OTBR_ENABLE_VENDOR_SERVER
, mVendorServer(vendor::VendorServer::newInstance(*this))
Expand All @@ -96,9 +96,31 @@ Application::Application(const std::string &aInterfaceName,
OTBR_UNUSED_VARIABLE(aRestListenPort);
}

void Application::Init(void)
void Application::Init(const std::vector<const char *> &aRadioUrls, bool aEnableAutoAttach)

{
mCtrlr = Ncp::IControllerOpenThread::CreateInstance(mInterfaceName.c_str(), aRadioUrls, mBackboneInterfaceName,
false, aEnableAutoAttach);
mCtrlr->Init();

if (mCtrlr->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
InitRcpMode();
}
else if (mCtrlr->GetCoprocessorType() == OT_COPROCESSOR_NCP)
{
InitNcpMode();
}
}

void Application::InitRcpMode(void)
{
mNcp.Init();
Ncp::ControllerOpenThreadRcp *ctrlrRcp = static_cast<Ncp::ControllerOpenThreadRcp *>(mCtrlr);

mBorderAgent.Init(ctrlrRcp);
#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent.Init(ctrlrRcp);
#endif

#if OTBR_ENABLE_MDNS
mPublisher->Start();
Expand All @@ -113,44 +135,56 @@ void Application::Init(void)
#endif
#endif
#if OTBR_ENABLE_BACKBONE_ROUTER
mBackboneAgent.Init();
mBackboneAgent.Init(ctrlrRcp);
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
mAdvertisingProxy.Init(ctrlrRcp);
mAdvertisingProxy.SetEnabled(true);
#endif
#if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
mDiscoveryProxy.Init(ctrlrRcp);
mDiscoveryProxy.SetEnabled(true);
#endif
#if OTBR_ENABLE_TREL
mTrelDnssd.Init(ctrlrRcp);
#endif
#if OTBR_ENABLE_OPENWRT
mUbusAgent.Init();
mUbusAgent.Init(ctrlrRcp);
#endif
#if OTBR_ENABLE_REST_SERVER
mRestWebServer.Init();
#endif
#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent.Init();
mRestWebServer.Init(ctrlrRcp);
#endif
#if OTBR_ENABLE_VENDOR_SERVER
mVendorServer->Init();
#endif
}

void Application::InitNcpMode(void)
{
#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent.Init(mCtrlr);
#endif
}

void Application::Deinit(void)
{
if (mCtrlr->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
#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();
mPublisher->Stop();
#endif
}

mNcp.Deinit();
mCtrlr->Deinit();
}

otbrError Application::Run(void)
Expand Down
25 changes: 10 additions & 15 deletions src/agent/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#if OTBR_ENABLE_BORDER_AGENT
#include "border_agent/border_agent.hpp"
#endif
#include "ncp/ncp_openthread.hpp"
#include "ncp/controller_openthread.hpp"
#if OTBR_ENABLE_BACKBONE_ROUTER
#include "backbone_router/backbone_agent.hpp"
#endif
Expand Down Expand Up @@ -93,24 +93,23 @@ class Application : private NonCopyable
*
* @param[in] aInterfaceName Name of the Thread network interface.
* @param[in] aBackboneInterfaceName Name of the backbone network interface.
* @param[in] aRadioUrls The radio URLs (can be IEEE802.15.4 or TREL radio).
* @param[in] aEnableAutoAttach Whether or not to automatically attach to the saved network.
* @param[in] aRestListenAddress Network address to listen on.
* @param[in] aRestListenPort Network port to listen on.
*
*/
explicit Application(const std::string &aInterfaceName,
const std::vector<const char *> &aBackboneInterfaceNames,
const std::vector<const char *> &aRadioUrls,
bool aEnableAutoAttach,
const std::string &aRestListenAddress,
int aRestListenPort);

/**
* This method initializes the Application instance.
*
* @param[in] aRadioUrls The radio URLs (can be IEEE802.15.4 or TREL radio).
* @param[in] aEnableAutoAttach Whether or not to automatically attach to the saved network.
*
*/
void Init(void);
void Init(const std::vector<const char *> &aRadioUrls, bool aEnableAutoAttach);

/**
* This method de-initializes the Application instance.
Expand All @@ -127,13 +126,6 @@ class Application : private NonCopyable
*/
otbrError Run(void);

/**
* Get the OpenThread controller object the application is using.
*
* @returns The OpenThread controller object.
*/
Ncp::ControllerOpenThread &GetNcp(void) { return mNcp; }

#if OTBR_ENABLE_MDNS
/**
* Get the Publisher object the application is using.
Expand Down Expand Up @@ -256,12 +248,15 @@ class Application : private NonCopyable

static void HandleSignal(int aSignal);

void InitRcpMode(void);
void InitNcpMode(void);

std::string mInterfaceName;
#if __linux__
otbr::Utils::InfraLinkSelector mInfraLinkSelector;
#endif
const char *mBackboneInterfaceName;
Ncp::ControllerOpenThread mNcp;
const char *mBackboneInterfaceName;
Ncp::IControllerOpenThread *mCtrlr;
#if OTBR_ENABLE_MDNS
std::unique_ptr<Mdns::Publisher> mPublisher;
#endif
Expand Down
33 changes: 20 additions & 13 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/ncp_openthread.hpp"
#include "ncp/controller_openthread_rcp.hpp"

static const char kDefaultInterfaceName[] = "wpan0";

Expand Down Expand Up @@ -176,17 +176,20 @@ static otbrLogLevel GetDefaultLogLevel(void)

static void PrintRadioVersionAndExit(const std::vector<const char *> &aRadioUrls)
{
otbr::Ncp::ControllerOpenThread ncpOpenThread{/* aInterfaceName */ "", aRadioUrls, /* aBackboneInterfaceName */ "",
/* aDryRun */ true, /* aEnableAutoAttach */ false};
const char *radioVersion;
otbr::Ncp::IControllerOpenThread *ctrlr =
otbr::Ncp::IControllerOpenThread::CreateInstance(/* aInterfaceName */ "", aRadioUrls,
/* aBackboneInterfaceName */ "",
/* aDryRun */ true, /* aEnableAutoAttach */ false);
const char *coprocessorVersion;

ncpOpenThread.Init();
ctrlr->Init();

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

ncpOpenThread.Deinit();
ctrlr->Deinit();
delete ctrlr;

exit(EXIT_SUCCESS);
}
Expand Down Expand Up @@ -279,7 +282,12 @@ static int realmain(int argc, char *argv[])

otbrLogInit(argv[0], logLevel, verbose, syslogDisable);
otbrLogNotice("Running %s", OTBR_PACKAGE_VERSION);
otbrLogNotice("Thread version: %s", otbr::Ncp::ControllerOpenThread::GetThreadVersion());
// The version is a constant configured during compiling. At this moment, the co-processor type
// is not yet known.
// - If the co-processor type is RCP, the Thread version here will be the actual Thread version.
// - If the co-processor type is NCP, the actual Thread version will be later evaluated after
// initialization. The Thread version printed here may not equal to the actual Thread version.
otbrLogNotice("Thread version on host side: %s", otbr::Ncp::ControllerOpenThreadRcp::GetThreadVersion());
otbrLogNotice("Thread interface: %s", interfaceName);

if (backboneInterfaceNames.empty())
Expand All @@ -300,11 +308,10 @@ static int realmain(int argc, char *argv[])
}

{
otbr::Application app(interfaceName, backboneInterfaceNames, radioUrls, enableAutoAttach, restListenAddress,
restListenPort);
otbr::Application app(interfaceName, backboneInterfaceNames, restListenAddress, restListenPort);

gApp = &app;
app.Init();
app.Init(radioUrls, enableAutoAttach);

ret = app.Run();

Expand Down