From 0cd21e6137ec4ec7eaef6e4d08b9c6b0bc39c4dc Mon Sep 17 00:00:00 2001 From: Li Cao Date: Thu, 16 May 2024 01:27:22 +0800 Subject: [PATCH] [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. --- src/posix/main.c | 10 +- .../include/openthread/openthread-system.h | 3 + src/posix/platform/system.cpp | 96 +++++++++++++------ 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/src/posix/main.c b/src/posix/main.c index 026c6c6eb..0bdcd13fa 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -64,11 +64,11 @@ #endif #include #include +#include +#include #include #include -#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)); diff --git a/src/posix/platform/include/openthread/openthread-system.h b/src/posix/platform/include/openthread/openthread-system.h index 7f113492f..9e497b051 100644 --- a/src/posix/platform/include/openthread/openthread-system.h +++ b/src/posix/platform/include/openthread/openthread-system.h @@ -45,6 +45,7 @@ #include #include +#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; /** diff --git a/src/posix/platform/system.cpp b/src/posix/platform/system.cpp index 4cb8dc810..3ac09f4f1 100644 --- a/src/posix/platform/system.cpp +++ b/src/posix/platform/system.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include @@ -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