[posix] add return of co-processor type in otSysInit and allow posix plat run with NCP (#10220)

This commit lets posix `otSysInit` return the Co-processor type by
adding a field in `otPlatformConfig`. (This avoids breaking the
existing code that uses `otSysInit`.) Returning the Co-processor type
allows the app layer has the information and can do
initialization. For example, the dbus server.

The commit also allows `otSysInit` and `otSysDeinit` to be completed
when the co-processor type is NCP. This doesn't mean `ot-daemon` can
work with NCP now. But this is required by further developing NCP
support in `otbr-agent` because these two methods are required. The
native posix app (ot-daemon) will still exit when it detects a
co-processor other than RCP.

When the co-processor type is RCP, `otSysInit` and `otSysDeinit` work
as usual. When the type is NCP, only spinel manager will be
initialized and ot::Instance will not be created.
This commit is contained in:
Li Cao
2024-05-15 10:27:22 -07:00
committed by GitHub
parent 02acc480dd
commit 0cd21e6137
3 changed files with 78 additions and 31 deletions
+8 -2
View File
@@ -64,11 +64,11 @@
#endif
#include <common/code_utils.hpp>
#include <lib/platform/exit_code.h>
#include <lib/platform/reset_util.h>
#include <lib/spinel/coprocessor_type.h>
#include <openthread/openthread-system.h>
#include <openthread/platform/misc.h>
#include "lib/platform/reset_util.h"
/**
* Initializes NCP app.
*
@@ -298,6 +298,12 @@ static otInstance *InitInstance(PosixConfig *aConfig)
VerifyOrDie(instance != NULL, OT_EXIT_FAILURE);
syslog(LOG_INFO, "Thread interface: %s", otSysGetThreadNetifName());
if (aConfig->mPlatformConfig.mCoprocessorType != OT_COPROCESSOR_RCP)
{
printf("Only RCP is supported by posix app now!\n");
exit(OT_EXIT_FAILURE);
}
if (aConfig->mPrintRadioVersion)
{
printf("%s\n", otPlatRadioGetVersionString(instance));
@@ -45,6 +45,7 @@
#include <openthread/instance.h>
#include <openthread/platform/misc.h>
#include "lib/spinel/coprocessor_type.h"
#include "lib/spinel/radio_spinel_metrics.h"
#ifdef __cplusplus
@@ -83,6 +84,8 @@ typedef struct otPlatformConfig
bool mPersistentInterface; ///< Whether persistent the interface
bool mDryRun; ///< If 'DryRun' is set, the posix daemon will exit
///< directly after initialization.
CoprocessorType mCoprocessorType; ///< The co-processor type. This field is used to pass
///< the type to the app layer.
} otPlatformConfig;
/**
+67 -29
View File
@@ -45,6 +45,7 @@
#include <openthread/tasklet.h>
#include <openthread/platform/alarm-milli.h>
#include <openthread/platform/infra_if.h>
#include <openthread/platform/logging.h>
#include <openthread/platform/otns.h>
#include <openthread/platform/radio.h>
@@ -61,6 +62,8 @@
otInstance *gInstance = nullptr;
bool gDryRun = false;
CoprocessorType sCoprocessorType = OT_COPROCESSOR_UNKNOWN;
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE || OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
static void processStateChange(otChangedFlags aFlags, void *aContext)
{
@@ -127,26 +130,11 @@ void otSysSetInfraNetif(const char *aInfraNetifName, int aIcmp6Socket)
}
#endif
void platformInit(otPlatformConfig *aPlatformConfig)
void platformInitRcpMode(otPlatformConfig *aPlatformConfig)
{
CoprocessorType type;
#if OPENTHREAD_POSIX_CONFIG_BACKTRACE_ENABLE
platformBacktraceInit();
#endif
platformAlarmInit(aPlatformConfig->mSpeedUpFactor, aPlatformConfig->mRealTimeSignal);
type = platformSpinelManagerInit(get802154RadioUrl(aPlatformConfig));
if (type != OT_COPROCESSOR_RCP)
{
printf("Only RCP is supported!\n");
exit(OT_EXIT_FAILURE);
}
platformRadioInit(get802154RadioUrl(aPlatformConfig));
// For Dry-Run option, only init the radio.
// For Dry-Run option, only init the co-processor.
VerifyOrExit(!aPlatformConfig->mDryRun);
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
@@ -175,11 +163,45 @@ void platformInit(otPlatformConfig *aPlatformConfig)
ot::Posix::Udp::Get().Init(aPlatformConfig->mInterfaceName);
#endif
#endif
exit:
return;
}
void platformInitNcpMode(otPlatformConfig *aPlatformConfig)
{
// Do nothing now.
OT_UNUSED_VARIABLE(aPlatformConfig);
}
void platformInit(otPlatformConfig *aPlatformConfig)
{
#if OPENTHREAD_POSIX_CONFIG_BACKTRACE_ENABLE
platformBacktraceInit();
#endif
platformAlarmInit(aPlatformConfig->mSpeedUpFactor, aPlatformConfig->mRealTimeSignal);
sCoprocessorType = platformSpinelManagerInit(get802154RadioUrl(aPlatformConfig));
switch (sCoprocessorType)
{
case OT_COPROCESSOR_RCP:
platformInitRcpMode(aPlatformConfig);
break;
case OT_COPROCESSOR_NCP:
platformInitNcpMode(aPlatformConfig);
break;
default:
otPlatLog(OT_LOG_LEVEL_CRIT, OT_LOG_REGION_PLATFORM, "Unknown type of the co-processor!\n");
exit(OT_EXIT_FAILURE);
break;
}
aPlatformConfig->mCoprocessorType = sCoprocessorType;
}
void platformSetUp(otPlatformConfig *aPlatformConfig)
{
OT_UNUSED_VARIABLE(aPlatformConfig);
@@ -233,11 +255,14 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig)
platformInit(aPlatformConfig);
gDryRun = aPlatformConfig->mDryRun;
gInstance = otInstanceInitSingle();
OT_ASSERT(gInstance != nullptr);
gDryRun = aPlatformConfig->mDryRun;
if (sCoprocessorType == OT_COPROCESSOR_RCP)
{
gInstance = otInstanceInitSingle();
OT_ASSERT(gInstance != nullptr);
platformSetUp(aPlatformConfig);
platformSetUp(aPlatformConfig);
}
return gInstance;
}
@@ -270,13 +295,14 @@ exit:
return;
}
void platformDeinit(void)
void platformDeinitRcpMode(void)
{
#if OPENTHREAD_POSIX_VIRTUAL_TIME
virtualTimeDeinit();
#endif
platformRadioDeinit();
platformSpinelManagerDeinit();
sCoprocessorType = OT_COPROCESSOR_UNKNOWN;
// For Dry-Run option, only the radio is initialized.
VerifyOrExit(!gDryRun);
@@ -303,14 +329,26 @@ exit:
return;
}
void platformDeinitNcpMode(void)
{
platformSpinelManagerDeinit();
sCoprocessorType = OT_COPROCESSOR_UNKNOWN;
}
void otSysDeinit(void)
{
OT_ASSERT(gInstance != nullptr);
platformTearDown();
otInstanceFinalize(gInstance);
gInstance = nullptr;
platformDeinit();
if (sCoprocessorType == OT_COPROCESSOR_RCP)
{
OT_ASSERT(gInstance != nullptr);
platformTearDown();
otInstanceFinalize(gInstance);
gInstance = nullptr;
platformDeinitRcpMode();
}
else if (sCoprocessorType == OT_COPROCESSOR_NCP)
{
platformDeinitNcpMode();
}
}
#if OPENTHREAD_POSIX_VIRTUAL_TIME