[cli] add context for the user commands (#5587)

This makes using a class method as the cli handler easier.
This commit is contained in:
Jiacheng Guo
2020-10-15 07:58:37 -07:00
committed by GitHub
parent 54b8d29e8f
commit fdb9e43fa0
6 changed files with 46 additions and 22 deletions
+6 -3
View File
@@ -51,8 +51,10 @@ extern "C" {
*/ */
typedef struct otCliCommand typedef struct otCliCommand
{ {
const char *mName; ///< A pointer to the command string. const char *mName; ///< A pointer to the command string.
void (*mCommand)(uint8_t aArgsLength, char *aArgs[]); ///< A function pointer to process the command. void (*mCommand)(void * aContext,
uint8_t aArgsLength,
char * aArgs[]); ///< A function pointer to process the command.
} otCliCommand; } otCliCommand;
/** /**
@@ -109,9 +111,10 @@ void otCliUartInit(otInstance *aInstance);
* *
* @param[in] aUserCommands A pointer to an array with user commands. * @param[in] aUserCommands A pointer to an array with user commands.
* @param[in] aLength @p aUserCommands length. * @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. * Write a number of bytes to the CLI console as a hex string.
+1 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs. * @note This number versions both OpenThread platform and user APIs.
* *
*/ */
#define OPENTHREAD_API_VERSION (34) #define OPENTHREAD_API_VERSION (35)
/** /**
* @addtogroup api-instance * @addtogroup api-instance
+3
View File
@@ -108,6 +108,9 @@ check()
sudo expect <<EOF | tee "${OT_OUTPUT}" & sudo expect <<EOF | tee "${OT_OUTPUT}" &
spawn ${OT_CLI_CMD} spawn ${OT_CLI_CMD}
send "radiourl\r\n"
expect "${RADIO_URL}"
expect "Done"
send "panid 0xface\r\n" send "panid 0xface\r\n"
expect "Done" expect "Done"
send "ifconfig up\r\n" send "ifconfig up\r\n"
+7 -6
View File
@@ -4196,7 +4196,7 @@ void Interpreter::ProcessLine(char *aBuf, uint16_t aBufLength)
{ {
if (strcmp(cmdName, mUserCommands[i].mName) == 0) if (strcmp(cmdName, mUserCommands[i].mName) == 0)
{ {
mUserCommands[i].mCommand(aArgsLength - 1, &aArgs[1]); mUserCommands[i].mCommand(mUserCommandsContext, aArgsLength - 1, &aArgs[1]);
ExitNow(); ExitNow();
} }
} }
@@ -4509,10 +4509,11 @@ void Interpreter::OutputChildTableEntry(const otNetworkDiagChildEntry &aChildEnt
} }
#endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE #endif // OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE
void Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLength) void Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLength, void *aContext)
{ {
mUserCommands = aCommands; mUserCommands = aCommands;
mUserCommandsLength = aLength; mUserCommandsLength = aLength;
mUserCommandsContext = aContext;
} }
Interpreter &Interpreter::GetOwner(OwnerLocator &aOwnerLocator) Interpreter &Interpreter::GetOwner(OwnerLocator &aOwnerLocator)
@@ -4596,9 +4597,9 @@ int Interpreter::OutputFormatV(const char *aFormat, va_list aArguments)
return Output(buf, static_cast<uint16_t>(strlen(buf))); return Output(buf, static_cast<uint16_t>(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) extern "C" void otCliOutputBytes(const uint8_t *aBytes, uint8_t aLength)
+3 -1
View File
@@ -229,9 +229,10 @@ public:
* *
* @param[in] aUserCommands A pointer to an array with user commands. * @param[in] aUserCommands A pointer to an array with user commands.
* @param[in] aLength @p aUserCommands length. * @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: protected:
static Interpreter *sInterpreter; static Interpreter *sInterpreter;
@@ -703,6 +704,7 @@ private:
const otCliCommand *mUserCommands; const otCliCommand *mUserCommands;
uint8_t mUserCommandsLength; uint8_t mUserCommandsLength;
void * mUserCommandsContext;
uint16_t mPingLength; uint16_t mPingLength;
uint16_t mPingCount; uint16_t mPingCount;
uint32_t mPingInterval; uint32_t mPingInterval;
+26 -11
View File
@@ -56,6 +56,7 @@
#define OT_POSIX_APP_TYPE_NCP 1 #define OT_POSIX_APP_TYPE_NCP 1
#define OT_POSIX_APP_TYPE_CLI 2 #define OT_POSIX_APP_TYPE_CLI 2
#include <openthread/cli.h>
#include <openthread/diag.h> #include <openthread/diag.h>
#include <openthread/logging.h> #include <openthread/logging.h>
#include <openthread/tasklet.h> #include <openthread/tasklet.h>
@@ -250,24 +251,30 @@ static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig)
aConfig->mPlatformConfig.mRadioUrl = aArgVector[optind]; 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; 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, "Running %s", otGetVersionString());
syslog(LOG_INFO, "Thread version: %hu", otThreadGetVersion()); 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); atexit(otSysDeinit);
if (config.mPrintRadioVersion) if (aConfig->mPrintRadioVersion)
{ {
printf("%s\n", otPlatRadioGetVersionString(instance)); printf("%s\n", otPlatRadioGetVersionString(instance));
} }
@@ -276,7 +283,7 @@ static otInstance *InitInstance(int aArgCount, char *aArgVector[])
syslog(LOG_INFO, "RCP version: %s", otPlatRadioGetVersionString(instance)); syslog(LOG_INFO, "RCP version: %s", otPlatRadioGetVersionString(instance));
} }
if (config.mIsDryRun) if (aConfig->mIsDryRun)
{ {
exit(OT_EXIT_SUCCESS); exit(OT_EXIT_SUCCESS);
} }
@@ -304,6 +311,10 @@ int main(int argc, char *argv[])
{ {
otInstance *instance; otInstance *instance;
int rval = 0; int rval = 0;
PosixConfig config;
#if OPENTHREAD_POSIX_APP_TYPE == OT_POSIX_APP_TYPE_CLI
otCliCommand radioUrlCommand = {"radiourl", PrintRadioUrl};
#endif
#ifdef __linux__ #ifdef __linux__
// Ensure we terminate this process if the // Ensure we terminate this process if the
@@ -320,7 +331,10 @@ int main(int argc, char *argv[])
execvp(argv[0], 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 #if OPENTHREAD_POSIX_APP_TYPE == OT_POSIX_APP_TYPE_NCP
otNcpInit(instance); otNcpInit(instance);
@@ -330,6 +344,7 @@ int main(int argc, char *argv[])
#else #else
otCliUartInit(instance); otCliUartInit(instance);
#endif #endif
otCliSetUserCommands(&radioUrlCommand, 1, &config.mPlatformConfig);
#endif #endif
while (true) while (true)