diff --git a/src/posix/main.c b/src/posix/main.c index dbff53d9c..52b1e23c5 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -62,6 +62,15 @@ #endif #include +typedef struct PosixConfig +{ + otPlatformConfig mPlatformConfig; ///< Platform configuration. + otLogLevel mLogLevel; ///< Debug level of logging. + bool mIsDryRun; ///< Dry run. + bool mPrintRadioVersion; ///< Whether to print radio firmware version. + bool mIsVerbose; ///< Whether to print log to stderr. +} PosixConfig; + static jmp_buf gResetJump; void __gcov_flush(); @@ -94,19 +103,13 @@ static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode) exit(aExitCode); } -static otInstance *InitInstance(int aArgCount, char *aArgVector[]) +static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig) { - otPlatformConfig config; - otInstance * instance = NULL; - bool isDryRun = false; - bool printRadioVersion = false; - bool isVerbose = false; - int logLevel = OT_LOG_LEVEL_CRIT; + memset(aConfig, 0, sizeof(PosixConfig)); - memset(&config, 0, sizeof(config)); - - config.mSpeedUpFactor = 1; - config.mResetRadio = true; + aConfig->mPlatformConfig.mSpeedUpFactor = 1; + aConfig->mPlatformConfig.mResetRadio = true; + aConfig->mLogLevel = OT_LOG_LEVEL_CRIT; optind = 1; @@ -123,23 +126,24 @@ static otInstance *InitInstance(int aArgCount, char *aArgVector[]) switch (option) { case 'd': - logLevel = atoi(optarg); + aConfig->mLogLevel = (otLogLevel)atoi(optarg); break; case 'h': PrintUsage(aArgVector[0], stdout, OT_EXIT_SUCCESS); break; case 'I': - config.mInterfaceName = optarg; + aConfig->mPlatformConfig.mInterfaceName = optarg; break; case 'n': - isDryRun = true; + aConfig->mIsDryRun = true; break; case 's': { - char *endptr = NULL; - config.mSpeedUpFactor = (uint32_t)strtol(optarg, &endptr, 0); + char *endptr = NULL; - if (*endptr != '\0' || config.mSpeedUpFactor == 0) + aConfig->mPlatformConfig.mSpeedUpFactor = (uint32_t)strtol(optarg, &endptr, 0); + + if (*endptr != '\0' || aConfig->mPlatformConfig.mSpeedUpFactor == 0) { fprintf(stderr, "Invalid value for TimerSpeedUpFactor: %s\n", optarg); exit(OT_EXIT_INVALID_ARGUMENTS); @@ -147,17 +151,17 @@ static otInstance *InitInstance(int aArgCount, char *aArgVector[]) break; } case 'v': - isVerbose = true; + aConfig->mIsVerbose = true; break; case 0: if (!strcmp(kOptions[index].name, "radio-version")) { - printRadioVersion = true; + aConfig->mPrintRadioVersion = true; } else if (!strcmp(kOptions[index].name, "no-reset")) { - config.mResetRadio = false; + aConfig->mPlatformConfig.mResetRadio = false; } break; case '?': @@ -169,36 +173,44 @@ static otInstance *InitInstance(int aArgCount, char *aArgVector[]) } } -#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED - openlog(aArgVector[0], LOG_PID | (isVerbose ? LOG_PERROR : 0), LOG_DAEMON); - setlogmask(setlogmask(0) & LOG_UPTO(LOG_DEBUG)); -#endif - if (optind >= aArgCount) { PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS); } - config.mRadioFile = aArgVector[optind]; + aConfig->mPlatformConfig.mRadioFile = aArgVector[optind]; if (optind + 1 < aArgCount) { - config.mRadioConfig = aArgVector[optind + 1]; + aConfig->mPlatformConfig.mRadioConfig = aArgVector[optind + 1]; } +} - instance = otSysInit(&config); +static otInstance *InitInstance(int aArgCount, char *aArgVector[]) +{ + PosixConfig config; + otInstance *instance = NULL; - if (printRadioVersion) + ParseArg(aArgCount, aArgVector, &config); + +#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED + openlog(aArgVector[0], LOG_PID | (config.mIsVerbose ? LOG_PERROR : 0), LOG_DAEMON); + setlogmask(setlogmask(0) & LOG_UPTO(LOG_DEBUG)); +#endif + + instance = otSysInit(&config.mPlatformConfig); + + if (config.mPrintRadioVersion) { printf("%s\n", otPlatRadioGetVersionString(instance)); } - if (isDryRun) + if (config.mIsDryRun) { exit(OT_EXIT_SUCCESS); } - otLoggingSetLevel(logLevel); + otLoggingSetLevel(config.mLogLevel); return instance; } diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index 504b010e5..c3bc6562e 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -130,30 +130,30 @@ HdlcInterface::HdlcInterface(Callbacks &aCallbacks) { } -otError HdlcInterface::Init(const char *aRadioFile, const char *aRadioConfig) +otError HdlcInterface::Init(const otPlatformConfig &aPlatformConfig) { otError error = OT_ERROR_NONE; struct stat st; VerifyOrExit(mSockFd == -1, error = OT_ERROR_ALREADY); - VerifyOrDie(stat(aRadioFile, &st) == 0, OT_EXIT_INVALID_ARGUMENTS); + VerifyOrDie(stat(aPlatformConfig.mRadioFile, &st) == 0, OT_EXIT_INVALID_ARGUMENTS); if (S_ISCHR(st.st_mode)) { - mSockFd = OpenFile(aRadioFile, aRadioConfig); + mSockFd = OpenFile(aPlatformConfig.mRadioFile, aPlatformConfig.mRadioConfig); VerifyOrExit(mSockFd != -1, error = OT_ERROR_INVALID_ARGS); } #if OPENTHREAD_CONFIG_POSIX_APP_ENABLE_PTY_DEVICE else if (S_ISREG(st.st_mode)) { - mSockFd = ForkPty(aRadioFile, aRadioConfig); + mSockFd = ForkPty(aPlatformConfig.mRadioFile, aPlatformConfig.mRadioConfig); VerifyOrExit(mSockFd != -1, error = OT_ERROR_INVALID_ARGS); } #endif // OPENTHREAD_CONFIG_POSIX_APP_ENABLE_PTY_DEVICE else { - otLogCritPlat("Radio file '%s' not supported", aRadioFile); + otLogCritPlat("Radio file '%s' not supported", aPlatformConfig.mRadioFile); ExitNow(error = OT_ERROR_INVALID_ARGS); } diff --git a/src/posix/platform/hdlc_interface.hpp b/src/posix/platform/hdlc_interface.hpp index 6da08ec93..9d113ad1b 100644 --- a/src/posix/platform/hdlc_interface.hpp +++ b/src/posix/platform/hdlc_interface.hpp @@ -103,16 +103,14 @@ public: * * @note This method should be called before reading and sending frames to the interface. * - * - * @param[in] aRadioFile The path to either a UART device or an executable. - * @param[in] aRadioConfig Parameters to be given to the device or executable. + * @param[in] aPlatformConfig Platform configuration structure. * * @retval OT_ERROR_NONE The interface is initialized successfully * @retval OT_ERROR_ALREADY The interface is already initialized. * @retval OT_ERROR_INVALID_ARGS The UART device or executable cannot be found or failed to open/run. * */ - otError Init(const char *aRadioFile, const char *aRadioConfig); + otError Init(const otPlatformConfig &aPlatformConfig); /** * This method deinitializes the interface to the RCP. diff --git a/src/posix/platform/openthread-system.h b/src/posix/platform/openthread-system.h index 3c1d79733..7ae5a9514 100644 --- a/src/posix/platform/openthread-system.h +++ b/src/posix/platform/openthread-system.h @@ -90,12 +90,12 @@ enum */ typedef struct otPlatformConfig { - uint64_t mNodeId; /// Unique node ID. - const char *mInterfaceName; /// Thread network interface name. - const char *mRadioFile; /// Radio file path. - const char *mRadioConfig; /// Radio configurations. - uint32_t mSpeedUpFactor; /// Speed up factor. - bool mResetRadio; /// Whether to reset RCP when initializing. + uint64_t mNodeId; ///< Unique node ID. + uint32_t mSpeedUpFactor; ///< Speed up factor. + const char *mInterfaceName; ///< Thread network interface name. + const char *mRadioFile; ///< Radio file path. + const char *mRadioConfig; ///< Radio configurations. + bool mResetRadio; ///< Whether to reset RCP when initializing. } otPlatformConfig; /** @@ -104,7 +104,7 @@ typedef struct otPlatformConfig * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function * when initialization of OpenThread's drivers is most appropriate. * - * @param[in] aPlatformConfig Argument vector. + * @param[in] aPlatformConfig Platform configuration structure. * * @returns A pointer to the OpenThread instance. * diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 9c04c3fdf..d2a19cc0d 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -192,15 +192,13 @@ void platformAlarmAdvanceNow(uint64_t aDelta); /** * This function initializes the radio service used by OpenThread. * - * @note Even when @p aReset is false, a reset event (i.e. a PROP_LAST_STATUS between + * @note Even when @p aPlatformConfig->mResetRadio is false, a reset event (i.e. a PROP_LAST_STATUS between * [SPINEL_STATUS_RESET__BEGIN, SPINEL_STATUS_RESET__END]) is still expected from RCP. * - * @param[in] aRadioFile A pointer to the radio file. - * @param[in] aRadioConfig A pointer to the radio config. - * @param[in] aReset Whether to reset RCP when initializing. + * @param[in] aPlatformConfig Platform configuration structure. * */ -void platformRadioInit(const char *aRadioFile, const char *aRadioConfig, bool aReset); +void platformRadioInit(const otPlatformConfig *aPlatformConfig); /** * This function shuts down the radio service used by OpenThread. diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index 23041da15..8a2f4a5e6 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -186,13 +186,13 @@ RadioSpinel::RadioSpinel(void) mVersion[0] = '\0'; } -void RadioSpinel::Init(const char *aRadioFile, const char *aRadioConfig, bool aReset) +void RadioSpinel::Init(const otPlatformConfig &aPlatformConfig) { otError error = OT_ERROR_NONE; - SuccessOrExit(error = mHdlcInterface.Init(aRadioFile, aRadioConfig)); + SuccessOrExit(error = mHdlcInterface.Init(aPlatformConfig)); - if (aReset) + if (aPlatformConfig.mResetRadio) { SuccessOrExit(error = SendReset()); } @@ -1437,9 +1437,9 @@ void otPlatRadioSetPromiscuous(otInstance *aInstance, bool aEnable) OT_UNUSED_VARIABLE(aInstance); } -void platformRadioInit(const char *aRadioFile, const char *aRadioConfig, bool aReset) +void platformRadioInit(const otPlatformConfig *aPlatformConfig) { - sRadioSpinel.Init(aRadioFile, aRadioConfig, aReset); + sRadioSpinel.Init(*aPlatformConfig); } void platformRadioDeinit(void) diff --git a/src/posix/platform/radio_spinel.hpp b/src/posix/platform/radio_spinel.hpp index 320896e7a..6b03dc602 100644 --- a/src/posix/platform/radio_spinel.hpp +++ b/src/posix/platform/radio_spinel.hpp @@ -55,12 +55,10 @@ public: /** * Initialize this radio transceiver. * - * @param[in] aRadioFile The path to either a uart device or an executable. - * @param[in] aRadioConfig Parameters given to the device or executable. - * @param[in] aReset Whether to reset RCP when initializing. + * @param[in] aPlatformConfig Platform configuration structure. * */ - void Init(const char *aRadioFile, const char *aRadioConfig, bool aReset); + void Init(const otPlatformConfig &aPlatformConfig); /** * Deinitialize this radio transceiver. diff --git a/src/posix/platform/system.c b/src/posix/platform/system.c index 36aa920a3..8fbc117c1 100644 --- a/src/posix/platform/system.c +++ b/src/posix/platform/system.c @@ -52,7 +52,7 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig) platformSimInit(); #endif platformAlarmInit(aPlatformConfig->mSpeedUpFactor); - platformRadioInit(aPlatformConfig->mRadioFile, aPlatformConfig->mRadioConfig, aPlatformConfig->mResetRadio); + platformRadioInit(aPlatformConfig); platformRandomInit(); instance = otInstanceInitSingle();