[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:
Abtin Keshavarzian
2022-10-03 09:32:02 -07:00
committed by GitHub
parent 7e8b482e3c
commit 61d9e72c18
9 changed files with 152 additions and 247 deletions
+30 -28
View File
@@ -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);