From fdb9e43fa0fb22fc125fd3d7e1d2780b1042de1c Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Thu, 15 Oct 2020 22:58:37 +0800 Subject: [PATCH] [cli] add context for the user commands (#5587) This makes using a class method as the cli handler easier. --- include/openthread/cli.h | 9 ++++++--- include/openthread/instance.h | 2 +- script/check-posix-pty | 3 +++ src/cli/cli.cpp | 13 ++++++------ src/cli/cli.hpp | 4 +++- src/posix/main.c | 37 ++++++++++++++++++++++++----------- 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/include/openthread/cli.h b/include/openthread/cli.h index 46817ea6c..8b640427e 100644 --- a/include/openthread/cli.h +++ b/include/openthread/cli.h @@ -51,8 +51,10 @@ extern "C" { */ typedef struct otCliCommand { - const char *mName; ///< A pointer to the command string. - void (*mCommand)(uint8_t aArgsLength, char *aArgs[]); ///< A function pointer to process the command. + const char *mName; ///< A pointer to the command string. + void (*mCommand)(void * aContext, + uint8_t aArgsLength, + char * aArgs[]); ///< A function pointer to process the command. } otCliCommand; /** @@ -109,9 +111,10 @@ void otCliUartInit(otInstance *aInstance); * * @param[in] aUserCommands A pointer to an array with user commands. * @param[in] aLength @p aUserCommands length. + * @param[in] aContext @p The context passed to the handler. * */ -void otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength); +void otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext); /** * Write a number of bytes to the CLI console as a hex string. diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 8cbefe02d..622e4999d 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (34) +#define OPENTHREAD_API_VERSION (35) /** * @addtogroup api-instance diff --git a/script/check-posix-pty b/script/check-posix-pty index 8c6d4720f..1143ee864 100755 --- a/script/check-posix-pty +++ b/script/check-posix-pty @@ -108,6 +108,9 @@ check() sudo expect <(strlen(buf))); } -extern "C" void otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength) +extern "C" void otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext) { - Interpreter::GetInterpreter().SetUserCommands(aUserCommands, aLength); + Interpreter::GetInterpreter().SetUserCommands(aUserCommands, aLength, aContext); } extern "C" void otCliOutputBytes(const uint8_t *aBytes, uint8_t aLength) diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 85dc8f66c..a568ba341 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -229,9 +229,10 @@ public: * * @param[in] aUserCommands A pointer to an array with user commands. * @param[in] aLength @p aUserCommands length. + * @param[in] aContext @p aUserCommands length. * */ - void SetUserCommands(const otCliCommand *aCommands, uint8_t aLength); + void SetUserCommands(const otCliCommand *aCommands, uint8_t aLength, void *aContext); protected: static Interpreter *sInterpreter; @@ -703,6 +704,7 @@ private: const otCliCommand *mUserCommands; uint8_t mUserCommandsLength; + void * mUserCommandsContext; uint16_t mPingLength; uint16_t mPingCount; uint32_t mPingInterval; diff --git a/src/posix/main.c b/src/posix/main.c index 054abe819..8f1833396 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -56,6 +56,7 @@ #define OT_POSIX_APP_TYPE_NCP 1 #define OT_POSIX_APP_TYPE_CLI 2 +#include #include #include #include @@ -250,24 +251,30 @@ static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig) aConfig->mPlatformConfig.mRadioUrl = aArgVector[optind]; } -static otInstance *InitInstance(int aArgCount, char *aArgVector[]) +#if OPENTHREAD_POSIX_APP_TYPE == OT_POSIX_APP_TYPE_CLI +static void PrintRadioUrl(void *aContext, uint8_t aArgsLength, char *aArgs[]) +{ + (void)aArgsLength; + (void)aArgs; + + otPlatformConfig *config = (otPlatformConfig *)aContext; + otCliOutputFormat("%s\r\nDone\r\n", config->mRadioUrl); +} +#endif // OPENTHREAD_POSIX_APP_TYPE == OT_POSIX_APP_TYPE_CLI + +static otInstance *InitInstance(PosixConfig *aConfig) { - PosixConfig config; otInstance *instance = NULL; - ParseArg(aArgCount, aArgVector, &config); - - openlog(aArgVector[0], LOG_PID | (config.mIsVerbose ? LOG_PERROR : 0), LOG_DAEMON); - setlogmask(setlogmask(0) & LOG_UPTO(LOG_DEBUG)); syslog(LOG_INFO, "Running %s", otGetVersionString()); syslog(LOG_INFO, "Thread version: %hu", otThreadGetVersion()); - IgnoreError(otLoggingSetLevel(config.mLogLevel)); + IgnoreError(otLoggingSetLevel(aConfig->mLogLevel)); - instance = otSysInit(&config.mPlatformConfig); + instance = otSysInit(&aConfig->mPlatformConfig); atexit(otSysDeinit); - if (config.mPrintRadioVersion) + if (aConfig->mPrintRadioVersion) { printf("%s\n", otPlatRadioGetVersionString(instance)); } @@ -276,7 +283,7 @@ static otInstance *InitInstance(int aArgCount, char *aArgVector[]) syslog(LOG_INFO, "RCP version: %s", otPlatRadioGetVersionString(instance)); } - if (config.mIsDryRun) + if (aConfig->mIsDryRun) { exit(OT_EXIT_SUCCESS); } @@ -304,6 +311,10 @@ int main(int argc, char *argv[]) { otInstance *instance; int rval = 0; + PosixConfig config; +#if OPENTHREAD_POSIX_APP_TYPE == OT_POSIX_APP_TYPE_CLI + otCliCommand radioUrlCommand = {"radiourl", PrintRadioUrl}; +#endif #ifdef __linux__ // Ensure we terminate this process if the @@ -320,7 +331,10 @@ int main(int argc, char *argv[]) execvp(argv[0], argv); } - instance = InitInstance(argc, argv); + ParseArg(argc, argv, &config); + openlog(argv[0], LOG_PID | (config.mIsVerbose ? LOG_PERROR : 0), LOG_DAEMON); + setlogmask(setlogmask(0) & LOG_UPTO(LOG_DEBUG)); + instance = InitInstance(&config); #if OPENTHREAD_POSIX_APP_TYPE == OT_POSIX_APP_TYPE_NCP otNcpInit(instance); @@ -330,6 +344,7 @@ int main(int argc, char *argv[]) #else otCliUartInit(instance); #endif + otCliSetUserCommands(&radioUrlCommand, 1, &config.mPlatformConfig); #endif while (true)