From cdeb02b0350b5c33f86ff441e00c47e308bb45e6 Mon Sep 17 00:00:00 2001 From: Li Cao Date: Thu, 6 Jun 2024 23:42:49 +0800 Subject: [PATCH] [posix] extract co-processor init out of otSysInit (#10344) This commit adds a new posix sys API `otSysInitCoprocessor` which only initializes the platform spinel component. (Reset the co-processor and get the type) The intention is to let the app to get the co-processor type without creating the otInstance. Currently only `otSysInit` can be called which creates the otInstance. However in some cases, we don't want to create the instance at the early stage. --- src/posix/main.c | 8 ++-- .../include/openthread/openthread-system.h | 46 ++++++++++++++----- src/posix/platform/system.cpp | 27 +++++++---- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/posix/main.c b/src/posix/main.c index 0bdcd13fa..40bcea70a 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -275,12 +275,14 @@ static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig) for (; optind < aArgCount; optind++) { - VerifyOrDie(aConfig->mPlatformConfig.mRadioUrlNum < OT_ARRAY_LENGTH(aConfig->mPlatformConfig.mRadioUrls), + VerifyOrDie(aConfig->mPlatformConfig.mCoprocessorUrls.mNum < + OT_ARRAY_LENGTH(aConfig->mPlatformConfig.mCoprocessorUrls.mUrls), OT_EXIT_INVALID_ARGUMENTS); - aConfig->mPlatformConfig.mRadioUrls[aConfig->mPlatformConfig.mRadioUrlNum++] = aArgVector[optind]; + aConfig->mPlatformConfig.mCoprocessorUrls.mUrls[aConfig->mPlatformConfig.mCoprocessorUrls.mNum++] = + aArgVector[optind]; } - if (aConfig->mPlatformConfig.mRadioUrlNum == 0) + if (aConfig->mPlatformConfig.mCoprocessorUrls.mNum == 0) { PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS); } diff --git a/src/posix/platform/include/openthread/openthread-system.h b/src/posix/platform/include/openthread/openthread-system.h index 9e497b051..460016e45 100644 --- a/src/posix/platform/include/openthread/openthread-system.h +++ b/src/posix/platform/include/openthread/openthread-system.h @@ -69,25 +69,49 @@ enum OT_PLATFORM_CONFIG_MAX_RADIO_URLS = 2, ///< Max number of Radio URLs. }; +/** + * Represents the Co-processor URLs. + * + */ +typedef struct otPlatformCoprocessorUrls +{ + const char *mUrls[OT_PLATFORM_CONFIG_MAX_RADIO_URLS]; ///< Co-processor URLs. + uint8_t mNum; ///< Number of Co-processor URLs. +} otPlatformCoprocessorUrls; + /** * Represents platform specific configurations. * */ typedef struct otPlatformConfig { - const char *mBackboneInterfaceName; ///< Backbone network interface name. - const char *mInterfaceName; ///< Thread network interface name. - const char *mRadioUrls[OT_PLATFORM_CONFIG_MAX_RADIO_URLS]; ///< Radio URLs. - uint8_t mRadioUrlNum; ///< Number of Radio URLs. - int mRealTimeSignal; ///< The real-time signal for microsecond timer. - uint32_t mSpeedUpFactor; ///< Speed up factor. - 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. + const char *mBackboneInterfaceName; ///< Backbone network interface name. + const char *mInterfaceName; ///< Thread network interface name. + otPlatformCoprocessorUrls mCoprocessorUrls; ///< Coprocessor URLs. + int mRealTimeSignal; ///< The real-time signal for microsecond timer. + uint32_t mSpeedUpFactor; ///< Speed up factor. + 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; +/** + * Initializes the co-processor and the spinel driver. + * + * @note This API will initialize the co-processor by resetting it and return the co-processor type. + * If this API is called, the upcoming call of `otSysInit` won't initialize the co-processor + * and the spinel driver again, unless `otSysDeinit` is called. This API is used to get the + * co-processor type without calling `otSysInit`. + * + * @param[in] aUrls The URLs to initialize the co-processor. + * + * @returns The co-processor type. + * + */ +CoprocessorType otSysInitCoprocessor(otPlatformCoprocessorUrls *aUrls); + /** * Performs all platform-specific initialization of OpenThread's drivers and initializes the OpenThread * instance. diff --git a/src/posix/platform/system.cpp b/src/posix/platform/system.cpp index 3ac09f4f1..8de729c4a 100644 --- a/src/posix/platform/system.cpp +++ b/src/posix/platform/system.cpp @@ -82,20 +82,20 @@ static void processStateChange(otChangedFlags aFlags, void *aContext) } #endif -static const char *get802154RadioUrl(otPlatformConfig *aPlatformConfig) +static const char *get802154RadioUrl(const otPlatformCoprocessorUrls &aUrls) { const char *radioUrl = nullptr; - for (uint8_t i = 0; i < aPlatformConfig->mRadioUrlNum; i++) + for (uint8_t i = 0; i < aUrls.mNum; i++) { - ot::Posix::RadioUrl url(aPlatformConfig->mRadioUrls[i]); + ot::Posix::RadioUrl url(aUrls.mUrls[i]); if (strcmp(url.GetProtocol(), "trel") == 0) { continue; } - radioUrl = aPlatformConfig->mRadioUrls[i]; + radioUrl = aUrls.mUrls[i]; break; } @@ -108,13 +108,13 @@ static const char *getTrelRadioUrl(otPlatformConfig *aPlatformConfig) { const char *radioUrl = nullptr; - for (uint8_t i = 0; i < aPlatformConfig->mRadioUrlNum; i++) + for (uint8_t i = 0; i < aPlatformConfig->mCoprocessorUrls.mNum; i++) { - ot::Posix::RadioUrl url(aPlatformConfig->mRadioUrls[i]); + ot::Posix::RadioUrl url(aPlatformConfig->mCoprocessorUrls.mUrls[i]); if (strcmp(url.GetProtocol(), "trel") == 0) { - radioUrl = aPlatformConfig->mRadioUrls[i]; + radioUrl = aPlatformConfig->mCoprocessorUrls.mUrls[i]; break; } } @@ -132,7 +132,7 @@ void otSysSetInfraNetif(const char *aInfraNetifName, int aIcmp6Socket) void platformInitRcpMode(otPlatformConfig *aPlatformConfig) { - platformRadioInit(get802154RadioUrl(aPlatformConfig)); + platformRadioInit(get802154RadioUrl(aPlatformConfig->mCoprocessorUrls)); // For Dry-Run option, only init the co-processor. VerifyOrExit(!aPlatformConfig->mDryRun); @@ -181,7 +181,10 @@ void platformInit(otPlatformConfig *aPlatformConfig) platformAlarmInit(aPlatformConfig->mSpeedUpFactor, aPlatformConfig->mRealTimeSignal); - sCoprocessorType = platformSpinelManagerInit(get802154RadioUrl(aPlatformConfig)); + if (sCoprocessorType == OT_COPROCESSOR_UNKNOWN) + { + sCoprocessorType = platformSpinelManagerInit(get802154RadioUrl(aPlatformConfig->mCoprocessorUrls)); + } switch (sCoprocessorType) { @@ -249,6 +252,12 @@ exit: return; } +CoprocessorType otSysInitCoprocessor(otPlatformCoprocessorUrls *aUrls) +{ + sCoprocessorType = platformSpinelManagerInit(get802154RadioUrl(*aUrls)); + return sCoprocessorType; +} + otInstance *otSysInit(otPlatformConfig *aPlatformConfig) { OT_ASSERT(gInstance == nullptr);