mirror of
https://github.com/espressif/openthread.git
synced 2026-08-02 09:07:47 +00:00
[cli] add context for the user commands (#5587)
This makes using a class method as the cli handler easier.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -108,6 +108,9 @@ check()
|
||||
|
||||
sudo expect <<EOF | tee "${OT_OUTPUT}" &
|
||||
spawn ${OT_CLI_CMD}
|
||||
send "radiourl\r\n"
|
||||
expect "${RADIO_URL}"
|
||||
expect "Done"
|
||||
send "panid 0xface\r\n"
|
||||
expect "Done"
|
||||
send "ifconfig up\r\n"
|
||||
|
||||
+7
-6
@@ -4196,7 +4196,7 @@ void Interpreter::ProcessLine(char *aBuf, uint16_t aBufLength)
|
||||
{
|
||||
if (strcmp(cmdName, mUserCommands[i].mName) == 0)
|
||||
{
|
||||
mUserCommands[i].mCommand(aArgsLength - 1, &aArgs[1]);
|
||||
mUserCommands[i].mCommand(mUserCommandsContext, aArgsLength - 1, &aArgs[1]);
|
||||
ExitNow();
|
||||
}
|
||||
}
|
||||
@@ -4509,10 +4509,11 @@ void Interpreter::OutputChildTableEntry(const otNetworkDiagChildEntry &aChildEnt
|
||||
}
|
||||
#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;
|
||||
mUserCommandsLength = aLength;
|
||||
mUserCommands = aCommands;
|
||||
mUserCommandsLength = aLength;
|
||||
mUserCommandsContext = aContext;
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
+3
-1
@@ -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;
|
||||
|
||||
+26
-11
@@ -56,6 +56,7 @@
|
||||
#define OT_POSIX_APP_TYPE_NCP 1
|
||||
#define OT_POSIX_APP_TYPE_CLI 2
|
||||
|
||||
#include <openthread/cli.h>
|
||||
#include <openthread/diag.h>
|
||||
#include <openthread/logging.h>
|
||||
#include <openthread/tasklet.h>
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user