mirror of
https://github.com/espressif/openthread.git
synced 2026-08-19 08:59:52 +00:00
[cli] update sub-modules to use Process<Cmd("cmd")> method (#8224)
This commit updates CLI `SrpServer`, `CoapSecure`, `UdpExample`, and
`TcpExample` to use the template `Process<Cmd("cmd")>` model. This
helps make them similar to other CLI modules.
This commit is contained in:
+41
-35
@@ -46,8 +46,6 @@
|
||||
namespace ot {
|
||||
namespace Cli {
|
||||
|
||||
constexpr CoapSecure::Command CoapSecure::sCommands[];
|
||||
|
||||
CoapSecure::CoapSecure(Output &aOutput)
|
||||
: OutputWrapper(aOutput)
|
||||
, mShutdownFlag(false)
|
||||
@@ -92,19 +90,7 @@ void CoapSecure::PrintPayload(otMessage *aMessage)
|
||||
OutputLine("");
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessHelp(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
for (const Command &command : sCommands)
|
||||
{
|
||||
OutputLine(command.mName);
|
||||
}
|
||||
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessResource(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("resource")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -142,7 +128,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessSet(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("set")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -161,7 +147,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessStart(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("start")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
bool verifyPeerCert = true;
|
||||
@@ -191,7 +177,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessStop(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("stop")>(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
@@ -214,22 +200,22 @@ otError CoapSecure::ProcessStop(Arg aArgs[])
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessGet(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("get")>(Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgs, OT_COAP_CODE_GET);
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessPost(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("post")>(Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgs, OT_COAP_CODE_POST);
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessPut(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("put")>(Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgs, OT_COAP_CODE_PUT);
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessDelete(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("delete")>(Arg aArgs[])
|
||||
{
|
||||
return ProcessRequest(aArgs, OT_COAP_CODE_DELETE);
|
||||
}
|
||||
@@ -384,7 +370,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessConnect(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("connect")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
otSockAddr sockaddr;
|
||||
@@ -404,7 +390,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError CoapSecure::ProcessDisconnect(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("disconnect")>(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
@@ -414,7 +400,7 @@ otError CoapSecure::ProcessDisconnect(Arg aArgs[])
|
||||
}
|
||||
|
||||
#ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
|
||||
otError CoapSecure::ProcessPsk(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("psk")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
uint16_t length;
|
||||
@@ -440,7 +426,7 @@ exit:
|
||||
#endif // MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
|
||||
|
||||
#ifdef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
|
||||
otError CoapSecure::ProcessX509(Arg aArgs[])
|
||||
template <> otError CoapSecure::Process<Cmd("x509")>(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
@@ -459,17 +445,37 @@ otError CoapSecure::ProcessX509(Arg aArgs[])
|
||||
|
||||
otError CoapSecure::Process(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_INVALID_ARGS;
|
||||
const Command *command;
|
||||
|
||||
if (aArgs[0].IsEmpty())
|
||||
{
|
||||
IgnoreError(ProcessHelp(aArgs));
|
||||
ExitNow();
|
||||
#define CmdEntry(aCommandString) \
|
||||
{ \
|
||||
aCommandString, &CoapSecure::Process<Cmd(aCommandString)> \
|
||||
}
|
||||
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), sCommands);
|
||||
VerifyOrExit(command != nullptr, error = OT_ERROR_INVALID_COMMAND);
|
||||
static constexpr Command kCommands[] = {
|
||||
CmdEntry("connect"), CmdEntry("delete"), CmdEntry("disconnect"), CmdEntry("get"), CmdEntry("post"),
|
||||
#ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
|
||||
CmdEntry("psk"),
|
||||
#endif
|
||||
CmdEntry("put"), CmdEntry("resource"), CmdEntry("set"), CmdEntry("start"), CmdEntry("stop"),
|
||||
#ifdef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
|
||||
CmdEntry("x509"),
|
||||
#endif
|
||||
};
|
||||
|
||||
#undef CmdEntry
|
||||
|
||||
static_assert(BinarySearch::IsSorted(kCommands), "kCommands is not sorted");
|
||||
|
||||
otError error = OT_ERROR_INVALID_COMMAND;
|
||||
const Command *command;
|
||||
|
||||
if (aArgs[0].IsEmpty() || (aArgs[0] == "help"))
|
||||
{
|
||||
OutputCommandTable(kCommands);
|
||||
ExitNow(error = aArgs[0].IsEmpty() ? OT_ERROR_INVALID_ARGS : OT_ERROR_NONE);
|
||||
}
|
||||
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), kCommands);
|
||||
VerifyOrExit(command != nullptr);
|
||||
|
||||
error = (this->*command->mHandler)(aArgs + 1);
|
||||
|
||||
|
||||
@@ -96,19 +96,7 @@ private:
|
||||
|
||||
void PrintPayload(otMessage *aMessage);
|
||||
|
||||
otError ProcessConnect(Arg aArgs[]);
|
||||
otError ProcessDelete(Arg aArgs[]);
|
||||
otError ProcessDisconnect(Arg aArgs[]);
|
||||
otError ProcessGet(Arg aArgs[]);
|
||||
otError ProcessHelp(Arg aArgs[]);
|
||||
otError ProcessPost(Arg aArgs[]);
|
||||
otError ProcessPsk(Arg aArgs[]);
|
||||
otError ProcessPut(Arg aArgs[]);
|
||||
otError ProcessResource(Arg aArgs[]);
|
||||
otError ProcessSet(Arg aArgs[]);
|
||||
otError ProcessStart(Arg aArgs[]);
|
||||
otError ProcessStop(Arg aArgs[]);
|
||||
otError ProcessX509(Arg aArgs[]);
|
||||
template <CommandId kCommandId> otError Process(Arg aArgs[]);
|
||||
|
||||
otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode);
|
||||
|
||||
@@ -149,28 +137,6 @@ private:
|
||||
static void HandleConnected(bool aConnected, void *aContext);
|
||||
void HandleConnected(bool aConnected);
|
||||
|
||||
static constexpr Command sCommands[] = {
|
||||
{"connect", &CoapSecure::ProcessConnect},
|
||||
{"delete", &CoapSecure::ProcessDelete},
|
||||
{"disconnect", &CoapSecure::ProcessDisconnect},
|
||||
{"get", &CoapSecure::ProcessGet},
|
||||
{"help", &CoapSecure::ProcessHelp},
|
||||
{"post", &CoapSecure::ProcessPost},
|
||||
#ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
|
||||
{"psk", &CoapSecure::ProcessPsk},
|
||||
#endif
|
||||
{"put", &CoapSecure::ProcessPut},
|
||||
{"resource", &CoapSecure::ProcessResource},
|
||||
{"set", &CoapSecure::ProcessSet},
|
||||
{"start", &CoapSecure::ProcessStart},
|
||||
{"stop", &CoapSecure::ProcessStop},
|
||||
#ifdef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
|
||||
{"x509", &CoapSecure::ProcessX509},
|
||||
#endif
|
||||
};
|
||||
|
||||
static_assert(BinarySearch::IsSorted(sCommands), "Command Table is not sorted");
|
||||
|
||||
#if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE
|
||||
otCoapBlockwiseResource mResource;
|
||||
#else
|
||||
|
||||
+49
-40
@@ -43,29 +43,7 @@
|
||||
namespace ot {
|
||||
namespace Cli {
|
||||
|
||||
constexpr SrpServer::Command SrpServer::sCommands[];
|
||||
|
||||
otError SrpServer::Process(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_INVALID_COMMAND;
|
||||
const Command *command;
|
||||
|
||||
if (aArgs[0].IsEmpty())
|
||||
{
|
||||
IgnoreError(ProcessHelp(aArgs));
|
||||
ExitNow();
|
||||
}
|
||||
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), sCommands);
|
||||
VerifyOrExit(command != nullptr);
|
||||
|
||||
error = (this->*command->mHandler)(aArgs + 1);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessAddrMode(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("addrmode")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_INVALID_ARGS;
|
||||
|
||||
@@ -97,7 +75,7 @@ otError SrpServer::ProcessAddrMode(Arg aArgs[])
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
|
||||
otError SrpServer::ProcessAuto(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("auto")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -137,7 +115,7 @@ exit:
|
||||
}
|
||||
#endif
|
||||
|
||||
otError SrpServer::ProcessDomain(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("domain")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -153,7 +131,7 @@ otError SrpServer::ProcessDomain(Arg aArgs[])
|
||||
return error;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessState(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("state")>(Arg aArgs[])
|
||||
{
|
||||
static const char *const kStateStrings[] = {
|
||||
"disabled", // (0) OT_SRP_SERVER_STATE_DISABLED
|
||||
@@ -172,7 +150,7 @@ otError SrpServer::ProcessState(Arg aArgs[])
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessEnable(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("enable")>(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
@@ -181,7 +159,7 @@ otError SrpServer::ProcessEnable(Arg aArgs[])
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessDisable(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("disable")>(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
@@ -190,7 +168,7 @@ otError SrpServer::ProcessDisable(Arg aArgs[])
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessTtl(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("ttl")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otSrpServerTtlConfig ttlConfig;
|
||||
@@ -214,7 +192,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessLease(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("lease")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otSrpServerLeaseConfig leaseConfig;
|
||||
@@ -242,7 +220,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessHost(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("host")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
const otSrpServerHost *host;
|
||||
@@ -304,7 +282,7 @@ void SrpServer::OutputHostAddresses(const otSrpServerHost *aHost)
|
||||
OutputFormat("]");
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessService(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("service")>(Arg aArgs[])
|
||||
{
|
||||
static constexpr char *kAnyServiceName = nullptr;
|
||||
static constexpr char *kAnyInstanceName = nullptr;
|
||||
@@ -373,7 +351,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessSeqNum(Arg aArgs[])
|
||||
template <> otError SrpServer::Process<Cmd("seqnum")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -393,16 +371,47 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError SrpServer::ProcessHelp(Arg aArgs[])
|
||||
otError SrpServer::Process(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
for (const Command &command : sCommands)
|
||||
{
|
||||
OutputLine(command.mName);
|
||||
#define CmdEntry(aCommandString) \
|
||||
{ \
|
||||
aCommandString, &SrpServer::Process<Cmd(aCommandString)> \
|
||||
}
|
||||
|
||||
return OT_ERROR_NONE;
|
||||
static constexpr Command kCommands[] = {
|
||||
CmdEntry("addrmode"),
|
||||
#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
|
||||
CmdEntry("auto"),
|
||||
#endif
|
||||
CmdEntry("disable"),
|
||||
CmdEntry("domain"),
|
||||
CmdEntry("enable"),
|
||||
CmdEntry("host"),
|
||||
CmdEntry("lease"),
|
||||
CmdEntry("seqnum"),
|
||||
CmdEntry("service"),
|
||||
CmdEntry("state"),
|
||||
CmdEntry("ttl"),
|
||||
};
|
||||
|
||||
static_assert(BinarySearch::IsSorted(kCommands), "kCommands is not sorted");
|
||||
|
||||
otError error = OT_ERROR_INVALID_COMMAND;
|
||||
const Command *command;
|
||||
|
||||
if (aArgs[0].IsEmpty() || (aArgs[0] == "help"))
|
||||
{
|
||||
OutputCommandTable(kCommands);
|
||||
ExitNow(error = aArgs[0].IsEmpty() ? error : OT_ERROR_NONE);
|
||||
}
|
||||
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), kCommands);
|
||||
VerifyOrExit(command != nullptr);
|
||||
|
||||
error = (this->*command->mHandler)(aArgs + 1);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
} // namespace Cli
|
||||
|
||||
@@ -81,41 +81,9 @@ private:
|
||||
|
||||
using Command = CommandEntry<SrpServer>;
|
||||
|
||||
otError ProcessAddrMode(Arg aArgs[]);
|
||||
#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
|
||||
otError ProcessAuto(Arg aArgs[]);
|
||||
#endif
|
||||
otError ProcessDomain(Arg aArgs[]);
|
||||
otError ProcessState(Arg aArgs[]);
|
||||
otError ProcessEnable(Arg aArgs[]);
|
||||
otError ProcessDisable(Arg aArgs[]);
|
||||
otError ProcessLease(Arg aArgs[]);
|
||||
otError ProcessHost(Arg aArgs[]);
|
||||
otError ProcessService(Arg aArgs[]);
|
||||
otError ProcessSeqNum(Arg aArgs[]);
|
||||
otError ProcessTtl(Arg aArgs[]);
|
||||
otError ProcessHelp(Arg aArgs[]);
|
||||
template <CommandId kCommandId> otError Process(Arg aArgs[]);
|
||||
|
||||
void OutputHostAddresses(const otSrpServerHost *aHost);
|
||||
|
||||
static constexpr Command sCommands[] = {
|
||||
{"addrmode", &SrpServer::ProcessAddrMode},
|
||||
#if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
|
||||
{"auto", &SrpServer::ProcessAuto},
|
||||
#endif
|
||||
{"disable", &SrpServer::ProcessDisable},
|
||||
{"domain", &SrpServer::ProcessDomain},
|
||||
{"enable", &SrpServer::ProcessEnable},
|
||||
{"help", &SrpServer::ProcessHelp},
|
||||
{"host", &SrpServer::ProcessHost},
|
||||
{"lease", &SrpServer::ProcessLease},
|
||||
{"seqnum", &SrpServer::ProcessSeqNum},
|
||||
{"service", &SrpServer::ProcessService},
|
||||
{"state", &SrpServer::ProcessState},
|
||||
{"ttl", &SrpServer::ProcessTtl},
|
||||
};
|
||||
|
||||
static_assert(BinarySearch::IsSorted(sCommands), "Command Table is not sorted");
|
||||
};
|
||||
|
||||
} // namespace Cli
|
||||
|
||||
+30
-28
@@ -49,8 +49,6 @@
|
||||
namespace ot {
|
||||
namespace Cli {
|
||||
|
||||
constexpr TcpExample::Command TcpExample::sCommands[];
|
||||
|
||||
TcpExample::TcpExample(Output &aOutput)
|
||||
: OutputWrapper(aOutput)
|
||||
, mInitialized(false)
|
||||
@@ -62,19 +60,7 @@ TcpExample::TcpExample(Output &aOutput)
|
||||
{
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessHelp(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
for (const Command &command : sCommands)
|
||||
{
|
||||
OutputLine(command.mName);
|
||||
}
|
||||
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessInit(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("init")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
size_t receiveBufferSize;
|
||||
@@ -163,7 +149,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessDeinit(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("deinit")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otError endpointError;
|
||||
@@ -190,7 +176,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessBind(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("bind")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
otSockAddr sockaddr;
|
||||
@@ -207,7 +193,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessConnect(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("connect")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
otSockAddr sockaddr;
|
||||
@@ -233,7 +219,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessSend(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("send")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
|
||||
@@ -265,7 +251,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessBenchmark(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("benchmark")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -317,7 +303,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessSendEnd(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("sendend")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
|
||||
@@ -330,7 +316,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessAbort(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("abort")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
|
||||
@@ -344,7 +330,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessListen(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("listen")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
otSockAddr sockaddr;
|
||||
@@ -362,7 +348,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError TcpExample::ProcessStopListening(Arg aArgs[])
|
||||
template <> otError TcpExample::Process<Cmd("stoplistening")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
|
||||
@@ -377,13 +363,29 @@ exit:
|
||||
|
||||
otError TcpExample::Process(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_INVALID_ARGS;
|
||||
#define CmdEntry(aCommandString) \
|
||||
{ \
|
||||
aCommandString, &TcpExample::Process<Cmd(aCommandString)> \
|
||||
}
|
||||
|
||||
static constexpr Command kCommands[] = {
|
||||
CmdEntry("abort"), CmdEntry("benchmark"), CmdEntry("bind"), CmdEntry("connect"), CmdEntry("deinit"),
|
||||
CmdEntry("init"), CmdEntry("listen"), CmdEntry("send"), CmdEntry("sendend"), CmdEntry("stoplistening"),
|
||||
};
|
||||
|
||||
static_assert(BinarySearch::IsSorted(kCommands), "kCommands is not sorted");
|
||||
|
||||
otError error = OT_ERROR_INVALID_COMMAND;
|
||||
const Command *command;
|
||||
|
||||
VerifyOrExit(!aArgs[0].IsEmpty(), IgnoreError(ProcessHelp(nullptr)));
|
||||
if (aArgs[0].IsEmpty() || (aArgs[0] == "help"))
|
||||
{
|
||||
OutputCommandTable(kCommands);
|
||||
ExitNow(error = aArgs[0].IsEmpty() ? error : OT_ERROR_NONE);
|
||||
}
|
||||
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), sCommands);
|
||||
VerifyOrExit(command != nullptr, error = OT_ERROR_INVALID_COMMAND);
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), kCommands);
|
||||
VerifyOrExit(command != nullptr);
|
||||
|
||||
error = (this->*command->mHandler)(aArgs + 1);
|
||||
|
||||
|
||||
+1
-27
@@ -74,17 +74,7 @@ public:
|
||||
private:
|
||||
using Command = CommandEntry<TcpExample>;
|
||||
|
||||
otError ProcessHelp(Arg aArgs[]);
|
||||
otError ProcessInit(Arg aArgs[]);
|
||||
otError ProcessDeinit(Arg aArgs[]);
|
||||
otError ProcessBind(Arg aArgs[]);
|
||||
otError ProcessConnect(Arg aArgs[]);
|
||||
otError ProcessSend(Arg aArgs[]);
|
||||
otError ProcessBenchmark(Arg aArgs[]);
|
||||
otError ProcessSendEnd(Arg aArgs[]);
|
||||
otError ProcessAbort(Arg aArgs[]);
|
||||
otError ProcessListen(Arg aArgs[]);
|
||||
otError ProcessStopListening(Arg aArgs[]);
|
||||
template <CommandId kCommandId> otError Process(Arg aArgs[]);
|
||||
|
||||
otError ContinueBenchmarkCircularSend(void);
|
||||
void CompleteBenchmark(void);
|
||||
@@ -117,22 +107,6 @@ private:
|
||||
otTcpEndpoint ** aAcceptInto);
|
||||
void HandleTcpAcceptDone(otTcpListener *aListener, otTcpEndpoint *aEndpoint, const otSockAddr *aPeer);
|
||||
|
||||
static constexpr Command sCommands[] = {
|
||||
{"abort", &TcpExample::ProcessAbort},
|
||||
{"benchmark", &TcpExample::ProcessBenchmark},
|
||||
{"bind", &TcpExample::ProcessBind},
|
||||
{"connect", &TcpExample::ProcessConnect},
|
||||
{"deinit", &TcpExample::ProcessDeinit},
|
||||
{"help", &TcpExample::ProcessHelp},
|
||||
{"init", &TcpExample::ProcessInit},
|
||||
{"listen", &TcpExample::ProcessListen},
|
||||
{"send", &TcpExample::ProcessSend},
|
||||
{"sendend", &TcpExample::ProcessSendEnd},
|
||||
{"stoplistening", &TcpExample::ProcessStopListening},
|
||||
};
|
||||
|
||||
static_assert(BinarySearch::IsSorted(sCommands), "Command Table is not sorted");
|
||||
|
||||
otTcpEndpoint mEndpoint;
|
||||
otTcpListener mListener;
|
||||
|
||||
|
||||
+27
-29
@@ -43,8 +43,6 @@
|
||||
namespace ot {
|
||||
namespace Cli {
|
||||
|
||||
constexpr UdpExample::Command UdpExample::sCommands[];
|
||||
|
||||
UdpExample::UdpExample(Output &aOutput)
|
||||
: OutputWrapper(aOutput)
|
||||
, mLinkSecurityEnabled(true)
|
||||
@@ -52,19 +50,7 @@ UdpExample::UdpExample(Output &aOutput)
|
||||
memset(&mSocket, 0, sizeof(mSocket));
|
||||
}
|
||||
|
||||
otError UdpExample::ProcessHelp(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
for (const Command &command : sCommands)
|
||||
{
|
||||
OutputLine(command.mName);
|
||||
}
|
||||
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError UdpExample::ProcessBind(Arg aArgs[])
|
||||
template <> otError UdpExample::Process<Cmd("bind")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
otSockAddr sockaddr;
|
||||
@@ -91,7 +77,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError UdpExample::ProcessConnect(Arg aArgs[])
|
||||
template <> otError UdpExample::Process<Cmd("connect")>(Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
otSockAddr sockaddr;
|
||||
@@ -114,14 +100,14 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError UdpExample::ProcessClose(Arg aArgs[])
|
||||
template <> otError UdpExample::Process<Cmd("close")>(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
return otUdpClose(GetInstancePtr(), &mSocket);
|
||||
}
|
||||
|
||||
otError UdpExample::ProcessOpen(Arg aArgs[])
|
||||
template <> otError UdpExample::Process<Cmd("open")>(Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
|
||||
@@ -134,7 +120,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError UdpExample::ProcessSend(Arg aArgs[])
|
||||
template <> otError UdpExample::Process<Cmd("send")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otMessage * message = nullptr;
|
||||
@@ -211,7 +197,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError UdpExample::ProcessLinkSecurity(Arg aArgs[])
|
||||
template <> otError UdpExample::Process<Cmd("linksecurity")>(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -286,17 +272,29 @@ exit:
|
||||
|
||||
otError UdpExample::Process(Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_INVALID_ARGS;
|
||||
const Command *command;
|
||||
|
||||
if (aArgs[0].IsEmpty())
|
||||
{
|
||||
IgnoreError(ProcessHelp(aArgs));
|
||||
ExitNow();
|
||||
#define CmdEntry(aCommandString) \
|
||||
{ \
|
||||
aCommandString, &UdpExample::Process<Cmd(aCommandString)> \
|
||||
}
|
||||
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), sCommands);
|
||||
VerifyOrExit(command != nullptr, error = OT_ERROR_INVALID_COMMAND);
|
||||
static constexpr Command kCommands[] = {
|
||||
CmdEntry("bind"), CmdEntry("close"), CmdEntry("connect"),
|
||||
CmdEntry("linksecurity"), CmdEntry("open"), CmdEntry("send"),
|
||||
};
|
||||
|
||||
static_assert(BinarySearch::IsSorted(kCommands), "kCommands is not sorted");
|
||||
|
||||
otError error = OT_ERROR_INVALID_COMMAND;
|
||||
const Command *command;
|
||||
|
||||
if (aArgs[0].IsEmpty() || (aArgs[0] == "help"))
|
||||
{
|
||||
OutputCommandTable(kCommands);
|
||||
ExitNow(error = aArgs[0].IsEmpty() ? error : OT_ERROR_NONE);
|
||||
}
|
||||
|
||||
command = BinarySearch::Find(aArgs[0].GetCString(), kCommands);
|
||||
VerifyOrExit(command != nullptr);
|
||||
|
||||
error = (this->*command->mHandler)(aArgs + 1);
|
||||
|
||||
|
||||
+1
-19
@@ -71,13 +71,7 @@ public:
|
||||
private:
|
||||
using Command = CommandEntry<UdpExample>;
|
||||
|
||||
otError ProcessHelp(Arg aArgs[]);
|
||||
otError ProcessBind(Arg aArgs[]);
|
||||
otError ProcessClose(Arg aArgs[]);
|
||||
otError ProcessConnect(Arg aArgs[]);
|
||||
otError ProcessOpen(Arg aArgs[]);
|
||||
otError ProcessSend(Arg aArgs[]);
|
||||
otError ProcessLinkSecurity(Arg aArgs[]);
|
||||
template <CommandId kCommandId> otError Process(Arg aArgs[]);
|
||||
|
||||
static otError PrepareAutoGeneratedPayload(otMessage &aMessage, uint16_t aPayloadLength);
|
||||
static otError PrepareHexStringPaylod(otMessage &aMessage, const char *aHexString);
|
||||
@@ -85,18 +79,6 @@ private:
|
||||
static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo);
|
||||
void HandleUdpReceive(otMessage *aMessage, const otMessageInfo *aMessageInfo);
|
||||
|
||||
static constexpr Command sCommands[] = {
|
||||
{"bind", &UdpExample::ProcessBind},
|
||||
{"close", &UdpExample::ProcessClose},
|
||||
{"connect", &UdpExample::ProcessConnect},
|
||||
{"help", &UdpExample::ProcessHelp},
|
||||
{"linksecurity", &UdpExample::ProcessLinkSecurity},
|
||||
{"open", &UdpExample::ProcessOpen},
|
||||
{"send", &UdpExample::ProcessSend},
|
||||
};
|
||||
|
||||
static_assert(BinarySearch::IsSorted(sCommands), "Command Table is not sorted");
|
||||
|
||||
bool mLinkSecurityEnabled;
|
||||
otUdpSocket mSocket;
|
||||
};
|
||||
|
||||
@@ -68,7 +68,7 @@ expect "Error 6: Parse"
|
||||
send "udp send -x something_invalid\n"
|
||||
expect "Error 7: InvalidArgs"
|
||||
send "udp\n"
|
||||
expect "Error 7: InvalidArgs"
|
||||
expect "Error 35: InvalidCommand"
|
||||
|
||||
send "udp linksecurity\n"
|
||||
expect "Enabled"
|
||||
|
||||
Reference in New Issue
Block a user