From 61d9e72c18588d6ee3a9b9a2720f65e7d39bdb87 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 3 Oct 2022 09:32:02 -0700 Subject: [PATCH] [cli] update sub-modules to use `Process` method (#8224) This commit updates CLI `SrpServer`, `CoapSecure`, `UdpExample`, and `TcpExample` to use the template `Process` model. This helps make them similar to other CLI modules. --- src/cli/cli_coap_secure.cpp | 76 ++++++++++++++------------- src/cli/cli_coap_secure.hpp | 36 +------------ src/cli/cli_srp_server.cpp | 89 ++++++++++++++++++-------------- src/cli/cli_srp_server.hpp | 34 +----------- src/cli/cli_tcp.cpp | 58 +++++++++++---------- src/cli/cli_tcp.hpp | 28 +--------- src/cli/cli_udp.cpp | 56 ++++++++++---------- src/cli/cli_udp.hpp | 20 +------ tests/scripts/expect/cli-udp.exp | 2 +- 9 files changed, 152 insertions(+), 247 deletions(-) diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index aae479829..e7fbc3609 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -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(Arg aArgs[]) { otError error = OT_ERROR_NONE; @@ -142,7 +128,7 @@ exit: return error; } -otError CoapSecure::ProcessSet(Arg aArgs[]) +template <> otError CoapSecure::Process(Arg aArgs[]) { otError error = OT_ERROR_NONE; @@ -161,7 +147,7 @@ exit: return error; } -otError CoapSecure::ProcessStart(Arg aArgs[]) +template <> otError CoapSecure::Process(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(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(Arg aArgs[]) { return ProcessRequest(aArgs, OT_COAP_CODE_GET); } -otError CoapSecure::ProcessPost(Arg aArgs[]) +template <> otError CoapSecure::Process(Arg aArgs[]) { return ProcessRequest(aArgs, OT_COAP_CODE_POST); } -otError CoapSecure::ProcessPut(Arg aArgs[]) +template <> otError CoapSecure::Process(Arg aArgs[]) { return ProcessRequest(aArgs, OT_COAP_CODE_PUT); } -otError CoapSecure::ProcessDelete(Arg aArgs[]) +template <> otError CoapSecure::Process(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(Arg aArgs[]) { otError error; otSockAddr sockaddr; @@ -404,7 +390,7 @@ exit: return error; } -otError CoapSecure::ProcessDisconnect(Arg aArgs[]) +template <> otError CoapSecure::Process(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(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(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 \ } - 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); diff --git a/src/cli/cli_coap_secure.hpp b/src/cli/cli_coap_secure.hpp index 3bd487290..ed24a33c6 100644 --- a/src/cli/cli_coap_secure.hpp +++ b/src/cli/cli_coap_secure.hpp @@ -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 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 diff --git a/src/cli/cli_srp_server.cpp b/src/cli/cli_srp_server.cpp index 474b950ab..3643fe215 100644 --- a/src/cli/cli_srp_server.cpp +++ b/src/cli/cli_srp_server.cpp @@ -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(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(Arg aArgs[]) { otError error = OT_ERROR_NONE; @@ -137,7 +115,7 @@ exit: } #endif -otError SrpServer::ProcessDomain(Arg aArgs[]) +template <> otError SrpServer::Process(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(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(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(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(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(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(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(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(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 \ } - 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 diff --git a/src/cli/cli_srp_server.hpp b/src/cli/cli_srp_server.hpp index 1a29c9e95..fcc9d8db3 100644 --- a/src/cli/cli_srp_server.hpp +++ b/src/cli/cli_srp_server.hpp @@ -81,41 +81,9 @@ private: using Command = CommandEntry; - 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 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 diff --git a/src/cli/cli_tcp.cpp b/src/cli/cli_tcp.cpp index 79ece00d7..19a6dbcc9 100644 --- a/src/cli/cli_tcp.cpp +++ b/src/cli/cli_tcp.cpp @@ -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(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(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(Arg aArgs[]) { otError error; otSockAddr sockaddr; @@ -207,7 +193,7 @@ exit: return error; } -otError TcpExample::ProcessConnect(Arg aArgs[]) +template <> otError TcpExample::Process(Arg aArgs[]) { otError error; otSockAddr sockaddr; @@ -233,7 +219,7 @@ exit: return error; } -otError TcpExample::ProcessSend(Arg aArgs[]) +template <> otError TcpExample::Process(Arg aArgs[]) { otError error; @@ -265,7 +251,7 @@ exit: return error; } -otError TcpExample::ProcessBenchmark(Arg aArgs[]) +template <> otError TcpExample::Process(Arg aArgs[]) { otError error = OT_ERROR_NONE; @@ -317,7 +303,7 @@ exit: return error; } -otError TcpExample::ProcessSendEnd(Arg aArgs[]) +template <> otError TcpExample::Process(Arg aArgs[]) { otError error; @@ -330,7 +316,7 @@ exit: return error; } -otError TcpExample::ProcessAbort(Arg aArgs[]) +template <> otError TcpExample::Process(Arg aArgs[]) { otError error; @@ -344,7 +330,7 @@ exit: return error; } -otError TcpExample::ProcessListen(Arg aArgs[]) +template <> otError TcpExample::Process(Arg aArgs[]) { otError error; otSockAddr sockaddr; @@ -362,7 +348,7 @@ exit: return error; } -otError TcpExample::ProcessStopListening(Arg aArgs[]) +template <> otError TcpExample::Process(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 \ + } + + 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); diff --git a/src/cli/cli_tcp.hpp b/src/cli/cli_tcp.hpp index d450a37d0..c0409cba9 100644 --- a/src/cli/cli_tcp.hpp +++ b/src/cli/cli_tcp.hpp @@ -74,17 +74,7 @@ public: private: using Command = CommandEntry; - 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 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; diff --git a/src/cli/cli_udp.cpp b/src/cli/cli_udp.cpp index 22b39cc17..a75620247 100644 --- a/src/cli/cli_udp.cpp +++ b/src/cli/cli_udp.cpp @@ -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(Arg aArgs[]) { otError error; otSockAddr sockaddr; @@ -91,7 +77,7 @@ exit: return error; } -otError UdpExample::ProcessConnect(Arg aArgs[]) +template <> otError UdpExample::Process(Arg aArgs[]) { otError error; otSockAddr sockaddr; @@ -114,14 +100,14 @@ exit: return error; } -otError UdpExample::ProcessClose(Arg aArgs[]) +template <> otError UdpExample::Process(Arg aArgs[]) { OT_UNUSED_VARIABLE(aArgs); return otUdpClose(GetInstancePtr(), &mSocket); } -otError UdpExample::ProcessOpen(Arg aArgs[]) +template <> otError UdpExample::Process(Arg aArgs[]) { OT_UNUSED_VARIABLE(aArgs); @@ -134,7 +120,7 @@ exit: return error; } -otError UdpExample::ProcessSend(Arg aArgs[]) +template <> otError UdpExample::Process(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(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 \ } - 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); diff --git a/src/cli/cli_udp.hpp b/src/cli/cli_udp.hpp index bd8ba5462..99701b399 100644 --- a/src/cli/cli_udp.hpp +++ b/src/cli/cli_udp.hpp @@ -71,13 +71,7 @@ public: private: using Command = CommandEntry; - 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 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; }; diff --git a/tests/scripts/expect/cli-udp.exp b/tests/scripts/expect/cli-udp.exp index 071117252..09e76d137 100755 --- a/tests/scripts/expect/cli-udp.exp +++ b/tests/scripts/expect/cli-udp.exp @@ -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"