Skip to content

Commit

Permalink
[ncp] add initial support of NCP
Browse files Browse the repository at this point in the history
  • Loading branch information
Irving-cl committed May 11, 2024
1 parent 4a8fd13 commit 573911d
Show file tree
Hide file tree
Showing 10 changed files with 641 additions and 45 deletions.
23 changes: 16 additions & 7 deletions src/agent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ void Application::Init(void)
{
mNcp.Init();

#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent.Init();
#endif

VerifyOrExit(mNcp.GetCoprocessorType() == OT_COPROCESSOR_RCP);

#if OTBR_ENABLE_MDNS
mPublisher->Start();
#endif
Expand Down Expand Up @@ -127,28 +133,31 @@ void Application::Init(void)
#if OTBR_ENABLE_REST_SERVER
mRestWebServer.Init();
#endif
#if OTBR_ENABLE_DBUS_SERVER
mDBusAgent.Init();
#endif
#if OTBR_ENABLE_VENDOR_SERVER
mVendorServer->Init();
#endif

exit:
return;
}

void Application::Deinit(void)
{
if (mNcp.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();
}
Expand Down
59 changes: 54 additions & 5 deletions src/dbus/server/dbus_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ DBusObject::DBusObject(DBusConnection *aConnection, const std::string &aObjectPa
{
}

otbrError DBusObject::Init(void)
otbrError DBusObject::Init(bool aAsyncPropertyHandler)
{
otbrError error = OTBR_ERROR_NONE;
DBusObjectPathVTable vTable;
Expand All @@ -60,10 +60,18 @@ otbrError DBusObject::Init(void)

VerifyOrExit(dbus_connection_register_object_path(mConnection, mObjectPath.c_str(), &vTable, this),
error = OTBR_ERROR_DBUS);
RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_GET_METHOD,
std::bind(&DBusObject::GetPropertyMethodHandler, this, _1));
RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_SET_METHOD,
std::bind(&DBusObject::SetPropertyMethodHandler, this, _1));
if (aAsyncPropertyHandler)
{
RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_GET_METHOD,
std::bind(&DBusObject::AsyncGetPropertyMethodHandler, this, _1));
}
else
{
RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_GET_METHOD,
std::bind(&DBusObject::GetPropertyMethodHandler, this, _1));
RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_SET_METHOD,
std::bind(&DBusObject::SetPropertyMethodHandler, this, _1));
}
RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_GET_ALL_METHOD,
std::bind(&DBusObject::GetAllPropertiesMethodHandler, this, _1));

Expand Down Expand Up @@ -98,6 +106,13 @@ void DBusObject::RegisterSetPropertyHandler(const std::string &aInterfac
mSetPropertyHandlers.emplace(fullPath, aHandler);
}

void DBusObject::RegisterAsyncGetPropertyHandler(const std::string &aInterfaceName,
const std::string &aPropertyName,
const AsyncPropertyHandlerType &aHandler)
{
mAsyncGetPropertyHandlers[aInterfaceName].emplace(aPropertyName, aHandler);
}

DBusHandlerResult DBusObject::sMessageHandler(DBusConnection *aConnection, DBusMessage *aMessage, void *aData)
{
DBusObject *server = reinterpret_cast<DBusObject *>(aData);
Expand Down Expand Up @@ -181,6 +196,40 @@ void DBusObject::GetPropertyMethodHandler(DBusRequest &aRequest)
}
}

void DBusObject::AsyncGetPropertyMethodHandler(DBusRequest &aRequest)
{
DBusMessageIter iter;
std::string interfaceName;
otError error = OT_ERROR_NONE;
std::string propertyName;

VerifyOrExit(dbus_message_iter_init(aRequest.GetMessage(), &iter), error = OT_ERROR_FAILED);
VerifyOrExit(DBusMessageExtract(&iter, interfaceName) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);
VerifyOrExit(DBusMessageExtract(&iter, propertyName) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);

{
auto propertyIter = mAsyncGetPropertyHandlers.find(interfaceName);

otbrLogDebug("AsyncGetProperty %s.%s", interfaceName.c_str(), propertyName.c_str());
VerifyOrExit(propertyIter != mAsyncGetPropertyHandlers.end(), error = OT_ERROR_NOT_FOUND);
{
auto &interfaceHandlers = propertyIter->second;
auto interfaceIter = interfaceHandlers.find(propertyName);

VerifyOrExit(interfaceIter != interfaceHandlers.end(), error = OT_ERROR_NOT_FOUND);
(interfaceIter->second)(aRequest);
}
}

exit:
if (error != OT_ERROR_NONE)
{
otbrLogWarning("GetProperty %s.%s error:%s", interfaceName.c_str(), propertyName.c_str(),
ConvertToDBusErrorName(error));
aRequest.ReplyOtResult(error);
}
}

void DBusObject::GetAllPropertiesMethodHandler(DBusRequest &aRequest)
{
UniqueDBusMessage reply{dbus_message_new_method_return(aRequest.GetMessage())};
Expand Down
38 changes: 22 additions & 16 deletions src/dbus/server/dbus_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ namespace DBus {
class DBusObject : private NonCopyable
{
public:
using MethodHandlerType = std::function<void(DBusRequest &)>;

using PropertyHandlerType = std::function<otError(DBusMessageIter &)>;
using MethodHandlerType = std::function<void(DBusRequest &)>;
using AsyncPropertyHandlerType = std::function<void(DBusRequest &)>;
using PropertyHandlerType = std::function<otError(DBusMessageIter &)>;

/**
* The constructor of a d-bus object.
Expand All @@ -78,17 +78,6 @@ class DBusObject : private NonCopyable
*/
DBusObject(DBusConnection *aConnection, const std::string &aObjectPath);

/**
* This method initializes the d-bus object.
*
* This method will register the object to the d-bus library.
*
* @retval OTBR_ERROR_NONE Successfully registered the object.
* @retval OTBR_ERROR_DBUS Failed to ragister an object.
*
*/
virtual otbrError Init(void);

/**
* This method registers the method handler.
*
Expand Down Expand Up @@ -125,6 +114,18 @@ class DBusObject : private NonCopyable
const std::string &aPropertyName,
const PropertyHandlerType &aHandler);

/**
* This method registers the async get handler for a property.
*
* @param[in] aInterfaceName The interface name.
* @param[in] aPropertyName The property name.
* @param[in] aHandler The method handler.
*
*/
virtual void RegisterAsyncGetPropertyHandler(const std::string &aInterfaceName,
const std::string &aPropertyName,
const AsyncPropertyHandlerType &aHandler);

/**
* This method sends a signal.
*
Expand Down Expand Up @@ -220,10 +221,13 @@ class DBusObject : private NonCopyable
*/
void Flush(void);

otbrError Init(bool aAsyncPropertyHandler = false);

private:
void GetAllPropertiesMethodHandler(DBusRequest &aRequest);
void GetPropertyMethodHandler(DBusRequest &aRequest);
void SetPropertyMethodHandler(DBusRequest &aRequest);
void AsyncGetPropertyMethodHandler(DBusRequest &aRequest);

static DBusHandlerResult sMessageHandler(DBusConnection *aConnection, DBusMessage *aMessage, void *aData);
DBusHandlerResult MessageHandler(DBusConnection *aConnection, DBusMessage *aMessage);
Expand All @@ -233,8 +237,10 @@ class DBusObject : private NonCopyable
std::unordered_map<std::string, MethodHandlerType> mMethodHandlers;
std::unordered_map<std::string, std::unordered_map<std::string, PropertyHandlerType>> mGetPropertyHandlers;
std::unordered_map<std::string, PropertyHandlerType> mSetPropertyHandlers;
DBusConnection *mConnection;
std::string mObjectPath;
std::unordered_map<std::string, std::unordered_map<std::string, AsyncPropertyHandlerType>>
mAsyncGetPropertyHandlers;
DBusConnection *mConnection;
std::string mObjectPath;
};

} // namespace DBus
Expand Down
41 changes: 34 additions & 7 deletions src/dbus/server/dbus_thread_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,27 @@ DBusThreadObject::DBusThreadObject(DBusConnection *aConnection,

otbrError DBusThreadObject::Init(void)
{
otbrError error = OTBR_ERROR_NONE;
auto threadHelper = mNcp->GetThreadHelper();
otbrError error = OTBR_ERROR_NONE;
bool asyncPropHandler = mNcp->GetCoprocessorType() == OT_COPROCESSOR_NCP;
SuccessOrExit(error = DBusObject::Init(asyncPropHandler));

SuccessOrExit(error = DBusObject::Init());
if (mNcp->GetCoprocessorType() == OT_COPROCESSOR_RCP)
{
InitCoreMode();
}
else if (mNcp->GetCoprocessorType() == OT_COPROCESSOR_NCP)
{
InitOffloadMode();
}

SuccessOrExit(error = Signal(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_SIGNAL_READY, std::make_tuple()));
exit:
return error;
}

void DBusThreadObject::InitCoreMode(void)
{
auto threadHelper = mNcp->GetThreadHelper();

threadHelper->AddDeviceRoleHandler(std::bind(&DBusThreadObject::DeviceRoleHandler, this, _1));
threadHelper->AddActiveDatasetChangeHandler(std::bind(&DBusThreadObject::ActiveDatasetChangeHandler, this, _1));
Expand Down Expand Up @@ -279,11 +296,12 @@ otbrError DBusThreadObject::Init(void)
std::bind(&DBusThreadObject::GetTelemetryDataHandler, this, _1));
RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_CAPABILITIES,
std::bind(&DBusThreadObject::GetCapabilitiesHandler, this, _1));
}

SuccessOrExit(error = Signal(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_SIGNAL_READY, std::make_tuple()));

exit:
return error;
void DBusThreadObject::InitOffloadMode(void)
{
RegisterAsyncGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_DEVICE_ROLE,
std::bind(&DBusThreadObject::AsyncGetDeviceRoleHandler, this, _1));
}

void DBusThreadObject::DeviceRoleHandler(otDeviceRole aDeviceRole)
Expand Down Expand Up @@ -2011,6 +2029,15 @@ otError DBusThreadObject::GetDnsUpstreamQueryState(DBusMessageIter &aIter)
#endif
}

void DBusThreadObject::AsyncGetDeviceRoleHandler(DBusRequest &aRequest)
{
mNcp->GetDeviceRole([aRequest](otError aError, otDeviceRole aRole) mutable {
std::string roleName = (aError == OT_ERROR_NONE) ? GetDeviceRoleName(aRole) : "Error getting device role!";

aRequest.Reply(std::tie(roleName));
});
}

static_assert(OTBR_SRP_SERVER_STATE_DISABLED == static_cast<uint8_t>(OT_SRP_SERVER_STATE_DISABLED),
"OTBR_SRP_SERVER_STATE_DISABLED value is incorrect");
static_assert(OTBR_SRP_SERVER_STATE_RUNNING == static_cast<uint8_t>(OT_SRP_SERVER_STATE_RUNNING),
Expand Down
7 changes: 6 additions & 1 deletion src/dbus/server/dbus_thread_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ class DBusThreadObject : public DBusObject
otbr::Ncp::ControllerOpenThread *aNcp,
Mdns::Publisher *aPublisher);

otbrError Init(void) override;
otbrError Init(void);

void RegisterGetPropertyHandler(const std::string &aInterfaceName,
const std::string &aPropertyName,
const PropertyHandlerType &aHandler) override;

private:
void InitCoreMode(void);
void InitOffloadMode(void);

void DeviceRoleHandler(otDeviceRole aDeviceRole);
void ActiveDatasetChangeHandler(const otOperationalDatasetTlvs &aDatasetTlvs);
void NcpResetHandler(void);
Expand Down Expand Up @@ -175,6 +178,8 @@ class DBusThreadObject : public DBusObject
otError GetTelemetryDataHandler(DBusMessageIter &aIter);
otError GetCapabilitiesHandler(DBusMessageIter &aIter);

void AsyncGetDeviceRoleHandler(DBusRequest &aRequest);

void ReplyScanResult(DBusRequest &aRequest, otError aError, const std::vector<otActiveScanResult> &aResult);
void ReplyEnergyScanResult(DBusRequest &aRequest, otError aError, const std::vector<otEnergyScanResult> &aResult);

Expand Down
3 changes: 3 additions & 0 deletions src/ncp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
add_library(otbr-ncp
ncp_openthread.cpp
ncp_openthread.hpp
ncp_spinel.cpp
ncp_spinel.hpp
)

target_link_libraries(otbr-ncp PRIVATE
otbr-common
$<$<BOOL:${OTBR_FEATURE_FLAGS}>:otbr-proto>
$<$<BOOL:${OTBR_TELEMETRY_DATA_API}>:otbr-proto>
)

Loading

0 comments on commit 573911d

Please sign in to comment.