From 7d6580ee3cbfd031f244310948eead1ea3ec7110 Mon Sep 17 00:00:00 2001 From: Li Cao Date: Sat, 23 Mar 2024 01:35:46 +0800 Subject: [PATCH] [cli] move `Process{Get/Set/Enable/Disable}` methods to Utils class (#9951) This commit moves the `Process{Get/Set/Enable/Disable}` methods to the Output class and rename the `Output` class to `Utils`. The purpose is to reduce code size in the Interpreter class to make the class focus more on the command handling and easier for further refactoring. The commit also removes the alias typedef of `Arg` in each cli module because they are unnecessary. Having one definition in the parent class is enough. --- src/cli/BUILD.gn | 4 +- src/cli/CMakeLists.txt | 2 +- src/cli/cli.cpp | 96 +------------- src/cli/cli.hpp | 147 +-------------------- src/cli/cli_bbr.cpp | 3 +- src/cli/cli_bbr.hpp | 8 +- src/cli/cli_br.cpp | 2 +- src/cli/cli_br.hpp | 8 +- src/cli/cli_coap.cpp | 2 +- src/cli/cli_coap.hpp | 6 +- src/cli/cli_coap_secure.cpp | 2 +- src/cli/cli_coap_secure.hpp | 6 +- src/cli/cli_commissioner.hpp | 8 +- src/cli/cli_dataset.hpp | 8 +- src/cli/cli_dns.cpp | 3 +- src/cli/cli_dns.hpp | 8 +- src/cli/cli_history.hpp | 8 +- src/cli/cli_joiner.hpp | 8 +- src/cli/cli_link_metrics.cpp | 4 +- src/cli/cli_link_metrics.hpp | 6 +- src/cli/cli_mac_filter.hpp | 8 +- src/cli/cli_network_data.cpp | 2 +- src/cli/cli_network_data.hpp | 6 +- src/cli/cli_ping.cpp | 4 +- src/cli/cli_ping.hpp | 6 +- src/cli/cli_srp_client.cpp | 19 ++- src/cli/cli_srp_client.hpp | 6 +- src/cli/cli_srp_server.cpp | 3 +- src/cli/cli_srp_server.hpp | 8 +- src/cli/cli_tcat.cpp | 2 +- src/cli/cli_tcat.hpp | 8 +- src/cli/cli_tcp.cpp | 2 +- src/cli/cli_tcp.hpp | 6 +- src/cli/cli_udp.cpp | 4 +- src/cli/cli_udp.hpp | 6 +- src/cli/{cli_output.cpp => cli_utils.cpp} | 146 ++++++++++++++++---- src/cli/{cli_output.hpp => cli_utils.hpp} | 154 +++++++++++++++++++++- src/cli/radio.cmake | 2 +- 38 files changed, 345 insertions(+), 386 deletions(-) rename src/cli/{cli_output.cpp => cli_utils.cpp} (71%) rename src/cli/{cli_output.hpp => cli_utils.hpp} (76%) diff --git a/src/cli/BUILD.gn b/src/cli/BUILD.gn index 318a4d0b9..b74577af1 100644 --- a/src/cli/BUILD.gn +++ b/src/cli/BUILD.gn @@ -55,8 +55,6 @@ openthread_cli_sources = [ "cli_mac_filter.hpp", "cli_network_data.cpp", "cli_network_data.hpp", - "cli_output.cpp", - "cli_output.hpp", "cli_ping.cpp", "cli_ping.hpp", "cli_srp_client.cpp", @@ -67,6 +65,8 @@ openthread_cli_sources = [ "cli_tcp.hpp", "cli_udp.cpp", "cli_udp.hpp", + "cli_utils.cpp", + "cli_utils.hpp", "x509_cert_key.hpp", ] diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index f36439f26..9dfee4030 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -45,13 +45,13 @@ set(COMMON_SOURCES cli_link_metrics.cpp cli_mac_filter.cpp cli_network_data.cpp - cli_output.cpp cli_ping.cpp cli_srp_client.cpp cli_srp_server.cpp cli_tcat.cpp cli_tcp.cpp cli_udp.cpp + cli_utils.cpp ) set(OT_CLI_VENDOR_EXTENSION "" CACHE STRING "Path to CMake file to define and link cli vendor extension") diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 5f5642e15..28de72475 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -99,7 +99,7 @@ static OT_DEFINE_ALIGNED_VAR(sInterpreterRaw, sizeof(Interpreter), uint64_t); Interpreter::Interpreter(Instance *aInstance, otCliOutputCallback aCallback, void *aContext) : OutputImplementer(aCallback, aContext) - , Output(aInstance, *this) + , Utils(aInstance, *this) , mCommandIsPending(false) , mInternalDebugCommand(false) , mTimer(*aInstance, HandleTimer, this) @@ -337,7 +337,7 @@ void Interpreter::ProcessLine(char *aBuf) VerifyOrExit(StringLength(aBuf, kMaxLineLength) <= kMaxLineLength - 1, error = OT_ERROR_PARSE); } - SuccessOrExit(error = Utils::CmdLineParser::ParseCmd(aBuf, args, kMaxArgs)); + SuccessOrExit(error = ot::Utils::CmdLineParser::ParseCmd(aBuf, args, kMaxArgs)); VerifyOrExit(!args[0].IsEmpty(), mCommandIsPending = false); if (!mInternalDebugCommand) @@ -408,26 +408,6 @@ otError Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLen return error; } -otError Interpreter::ParseEnableOrDisable(const Arg &aArg, bool &aEnable) -{ - otError error = OT_ERROR_NONE; - - if (aArg == "enable") - { - aEnable = true; - } - else if (aArg == "disable") - { - aEnable = false; - } - else - { - error = OT_ERROR_INVALID_COMMAND; - } - - return error; -} - #if OPENTHREAD_FTD || OPENTHREAD_MTD otError Interpreter::ParseJoinerDiscerner(Arg &aArg, otJoinerDiscerner &aDiscerner) @@ -441,7 +421,7 @@ otError Interpreter::ParseJoinerDiscerner(Arg &aArg, otJoinerDiscerner &aDiscern VerifyOrExit(separator != nullptr, error = OT_ERROR_NOT_FOUND); - SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(separator + 1, aDiscerner.mLength)); + SuccessOrExit(error = ot::Utils::CmdLineParser::ParseAsUint8(separator + 1, aDiscerner.mLength)); VerifyOrExit(aDiscerner.mLength > 0 && aDiscerner.mLength <= 64, error = OT_ERROR_INVALID_ARGS); *separator = '\0'; error = aArg.ParseAsUint64(aDiscerner.mValue); @@ -8325,76 +8305,6 @@ void Interpreter::Initialize(otInstance *aInstance, otCliOutputCallback aCallbac Interpreter::sInterpreter = new (&sInterpreterRaw) Interpreter(instance, aCallback, aContext); } -otError Interpreter::ProcessEnableDisable(Arg aArgs[], SetEnabledHandler aSetEnabledHandler) -{ - otError error = OT_ERROR_NONE; - bool enable; - - if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) - { - aSetEnabledHandler(GetInstancePtr(), enable); - } - else - { - error = OT_ERROR_INVALID_COMMAND; - } - - return error; -} - -otError Interpreter::ProcessEnableDisable(Arg aArgs[], SetEnabledHandlerFailable aSetEnabledHandler) -{ - otError error = OT_ERROR_NONE; - bool enable; - - if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) - { - error = aSetEnabledHandler(GetInstancePtr(), enable); - } - else - { - error = OT_ERROR_INVALID_COMMAND; - } - - return error; -} - -otError Interpreter::ProcessEnableDisable(Arg aArgs[], - IsEnabledHandler aIsEnabledHandler, - SetEnabledHandler aSetEnabledHandler) -{ - otError error = OT_ERROR_NONE; - - if (aArgs[0].IsEmpty()) - { - OutputEnabledDisabledStatus(aIsEnabledHandler(GetInstancePtr())); - } - else - { - error = ProcessEnableDisable(aArgs, aSetEnabledHandler); - } - - return error; -} - -otError Interpreter::ProcessEnableDisable(Arg aArgs[], - IsEnabledHandler aIsEnabledHandler, - SetEnabledHandlerFailable aSetEnabledHandler) -{ - otError error = OT_ERROR_NONE; - - if (aArgs[0].IsEmpty()) - { - OutputEnabledDisabledStatus(aIsEnabledHandler(GetInstancePtr())); - } - else - { - error = ProcessEnableDisable(aArgs, aSetEnabledHandler); - } - - return error; -} - void Interpreter::OutputPrompt(void) { #if OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 8cb53f4a3..75ea62975 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -68,13 +68,13 @@ #include "cli/cli_link_metrics.hpp" #include "cli/cli_mac_filter.hpp" #include "cli/cli_network_data.hpp" -#include "cli/cli_output.hpp" #include "cli/cli_ping.hpp" #include "cli/cli_srp_client.hpp" #include "cli/cli_srp_server.hpp" #include "cli/cli_tcat.hpp" #include "cli/cli_tcp.hpp" #include "cli/cli_udp.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_COAP_API_ENABLE #include "cli/cli_coap.hpp" #endif @@ -108,7 +108,7 @@ extern "C" void otCliOutputFormat(const char *aFmt, ...); * Implements the CLI interpreter. * */ -class Interpreter : public OutputImplementer, public Output +class Interpreter : public OutputImplementer, public Utils { #if OPENTHREAD_FTD || OPENTHREAD_MTD friend class Br; @@ -128,8 +128,6 @@ class Interpreter : public OutputImplementer, public Output friend void otCliOutputFormat(const char *aFmt, ...); public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * @@ -178,19 +176,6 @@ public: */ void ProcessLine(char *aBuf); - /** - * Checks a given argument string against "enable" or "disable" commands. - * - * @param[in] aArg The argument string to parse. - * @param[out] aEnable Boolean variable to return outcome on success. - * Set to TRUE for "enable" command, and FALSE for "disable" command. - * - * @retval OT_ERROR_NONE Successfully parsed the @p aString and updated @p aEnable. - * @retval OT_ERROR_INVALID_COMMAND The @p aString is not "enable" or "disable" command. - * - */ - static otError ParseEnableOrDisable(const Arg &aArg, bool &aEnable); - /** * Adds commands to the user command table. * @@ -289,94 +274,6 @@ private: using Command = CommandEntry; - template using GetHandler = ValueType (&)(otInstance *); - template using SetHandler = void (&)(otInstance *, ValueType); - template using SetHandlerFailable = otError (&)(otInstance *, ValueType); - using IsEnabledHandler = bool (&)(otInstance *); - using SetEnabledHandler = void (&)(otInstance *, bool); - using SetEnabledHandlerFailable = otError (&)(otInstance *, bool); - - // Returns format string to output a `ValueType` (e.g., "%u" for `uint16_t`). - template static constexpr const char *FormatStringFor(void); - - // General template implementation. - // Specializations for `uint32_t` and `int32_t` are added at the end. - template otError ProcessGet(Arg aArgs[], GetHandler aGetHandler) - { - static_assert( - TypeTraits::IsSame::kValue || TypeTraits::IsSame::kValue || - TypeTraits::IsSame::kValue || TypeTraits::IsSame::kValue || - TypeTraits::IsSame::kValue, - "ValueType must be an 8, 16 `int` or `uint` type, or a `const char *`"); - - otError error = OT_ERROR_NONE; - - VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - OutputLine(FormatStringFor(), aGetHandler(GetInstancePtr())); - - exit: - return error; - } - - template otError ProcessSet(Arg aArgs[], SetHandler aSetHandler) - { - otError error; - ValueType value; - - SuccessOrExit(error = aArgs[0].ParseAs(value)); - VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - - aSetHandler(GetInstancePtr(), value); - - exit: - return error; - } - - template otError ProcessSet(Arg aArgs[], SetHandlerFailable aSetHandler) - { - otError error; - ValueType value; - - SuccessOrExit(error = aArgs[0].ParseAs(value)); - VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - - error = aSetHandler(GetInstancePtr(), value); - - exit: - return error; - } - - template - otError ProcessGetSet(Arg aArgs[], GetHandler aGetHandler, SetHandler aSetHandler) - { - otError error = ProcessGet(aArgs, aGetHandler); - - VerifyOrExit(error != OT_ERROR_NONE); - error = ProcessSet(aArgs, aSetHandler); - - exit: - return error; - } - - template - otError ProcessGetSet(Arg aArgs[], GetHandler aGetHandler, SetHandlerFailable aSetHandler) - { - otError error = ProcessGet(aArgs, aGetHandler); - - VerifyOrExit(error != OT_ERROR_NONE); - error = ProcessSet(aArgs, aSetHandler); - - exit: - return error; - } - - otError ProcessEnableDisable(Arg aArgs[], SetEnabledHandler aSetEnabledHandler); - otError ProcessEnableDisable(Arg aArgs[], SetEnabledHandlerFailable aSetEnabledHandler); - otError ProcessEnableDisable(Arg aArgs[], IsEnabledHandler aIsEnabledHandler, SetEnabledHandler aSetEnabledHandler); - otError ProcessEnableDisable(Arg aArgs[], - IsEnabledHandler aIsEnabledHandler, - SetEnabledHandlerFailable aSetEnabledHandler); - void OutputPrompt(void); void OutputResult(otError aError); @@ -604,46 +501,6 @@ private: #endif }; -// Specializations of `FormatStringFor()` - -template <> inline constexpr const char *Interpreter::FormatStringFor(void) { return "%u"; } - -template <> inline constexpr const char *Interpreter::FormatStringFor(void) { return "%u"; } - -template <> inline constexpr const char *Interpreter::FormatStringFor(void) { return "%lu"; } - -template <> inline constexpr const char *Interpreter::FormatStringFor(void) { return "%d"; } - -template <> inline constexpr const char *Interpreter::FormatStringFor(void) { return "%d"; } - -template <> inline constexpr const char *Interpreter::FormatStringFor(void) { return "%ld"; } - -template <> inline constexpr const char *Interpreter::FormatStringFor(void) { return "%s"; } - -// Specialization of ProcessGet<> for `uint32_t` and `int32_t` - -template <> inline otError Interpreter::ProcessGet(Arg aArgs[], GetHandler aGetHandler) -{ - otError error = OT_ERROR_NONE; - - VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - OutputLine(FormatStringFor(), ToUlong(aGetHandler(GetInstancePtr()))); - -exit: - return error; -} - -template <> inline otError Interpreter::ProcessGet(Arg aArgs[], GetHandler aGetHandler) -{ - otError error = OT_ERROR_NONE; - - VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - OutputLine(FormatStringFor(), static_cast(aGetHandler(GetInstancePtr()))); - -exit: - return error; -} - } // namespace Cli } // namespace ot diff --git a/src/cli/cli_bbr.cpp b/src/cli/cli_bbr.cpp index d37d3dfcc..0f86b2441 100644 --- a/src/cli/cli_bbr.cpp +++ b/src/cli/cli_bbr.cpp @@ -290,8 +290,7 @@ template <> otError Bbr::Process(Arg aArgs[]) */ template <> otError Bbr::Process(Arg aArgs[]) { - return Interpreter::GetInterpreter().ProcessGetSet(aArgs, otBackboneRouterGetRegistrationJitter, - otBackboneRouterSetRegistrationJitter); + return ProcessGetSet(aArgs, otBackboneRouterGetRegistrationJitter, otBackboneRouterSetRegistrationJitter); } /** diff --git a/src/cli/cli_bbr.hpp b/src/cli/cli_bbr.hpp index 40ed979f6..120518ac9 100644 --- a/src/cli/cli_bbr.hpp +++ b/src/cli/cli_bbr.hpp @@ -42,7 +42,7 @@ #include #include "cli/cli_config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" namespace ot { namespace Cli { @@ -51,11 +51,9 @@ namespace Cli { * Implements the BBR CLI interpreter. * */ -class Bbr : private Output +class Bbr : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor. * @@ -64,7 +62,7 @@ public: * */ Bbr(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_br.cpp b/src/cli/cli_br.cpp index 9c77c7d07..85590b925 100644 --- a/src/cli/cli_br.cpp +++ b/src/cli/cli_br.cpp @@ -455,7 +455,7 @@ template <> otError Br::Process(Arg aArgs[]) * #otBorderRoutingDhcp6PdSetEnabled * */ - if (Interpreter::GetInterpreter().ProcessEnableDisable(aArgs, otBorderRoutingDhcp6PdSetEnabled) == OT_ERROR_NONE) + if (ProcessEnableDisable(aArgs, otBorderRoutingDhcp6PdSetEnabled) == OT_ERROR_NONE) { } /** diff --git a/src/cli/cli_br.hpp b/src/cli/cli_br.hpp index 01fcdcf94..5a877a84d 100644 --- a/src/cli/cli_br.hpp +++ b/src/cli/cli_br.hpp @@ -39,7 +39,7 @@ #include #include "cli/cli_config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE @@ -50,11 +50,9 @@ namespace Cli { * Implements the Border Router CLI interpreter. * */ -class Br : private Output +class Br : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * @@ -63,7 +61,7 @@ public: * */ Br(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_coap.cpp b/src/cli/cli_coap.cpp index 9464dff07..b19d820f9 100644 --- a/src/cli/cli_coap.cpp +++ b/src/cli/cli_coap.cpp @@ -45,7 +45,7 @@ namespace ot { namespace Cli { Coap::Coap(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) , mUseDefaultRequestTxParameters(true) , mUseDefaultResponseTxParameters(true) #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE diff --git a/src/cli/cli_coap.hpp b/src/cli/cli_coap.hpp index 2b4184a51..95ce7b94a 100644 --- a/src/cli/cli_coap.hpp +++ b/src/cli/cli_coap.hpp @@ -40,7 +40,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" namespace ot { namespace Cli { @@ -49,11 +49,9 @@ namespace Cli { * Implements the CLI CoAP server and client. * */ -class Coap : private Output +class Coap : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index e238fbbd0..2ff4f6819 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -47,7 +47,7 @@ namespace ot { namespace Cli { CoapSecure::CoapSecure(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) , mShutdownFlag(false) , mUseCertificate(false) , mPskLength(0) diff --git a/src/cli/cli_coap_secure.hpp b/src/cli/cli_coap_secure.hpp index a09dfed02..92a7a95e5 100644 --- a/src/cli/cli_coap_secure.hpp +++ b/src/cli/cli_coap_secure.hpp @@ -42,7 +42,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #ifndef CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER #define CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER 0 @@ -55,11 +55,9 @@ namespace Cli { * Implements the CLI CoAP Secure server and client. * */ -class CoapSecure : private Output +class CoapSecure : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * diff --git a/src/cli/cli_commissioner.hpp b/src/cli/cli_commissioner.hpp index 162c6da5b..a5feac4cc 100644 --- a/src/cli/cli_commissioner.hpp +++ b/src/cli/cli_commissioner.hpp @@ -38,7 +38,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD @@ -49,11 +49,9 @@ namespace Cli { * Implements the Commissioner CLI interpreter. * */ -class Commissioner : private Output +class Commissioner : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * @@ -62,7 +60,7 @@ public: * */ Commissioner(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_dataset.hpp b/src/cli/cli_dataset.hpp index 858786231..06055e34a 100644 --- a/src/cli/cli_dataset.hpp +++ b/src/cli/cli_dataset.hpp @@ -40,7 +40,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" namespace ot { namespace Cli { @@ -49,13 +49,11 @@ namespace Cli { * Implements the Dataset CLI interpreter. * */ -class Dataset : private Output +class Dataset : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - Dataset(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_dns.cpp b/src/cli/cli_dns.cpp index e5dff9a13..5148bb36b 100644 --- a/src/cli/cli_dns.cpp +++ b/src/cli/cli_dns.cpp @@ -679,8 +679,7 @@ template <> otError Dns::Process(Arg aArgs[]) * @par api_copy * #otDnssdUpstreamQuerySetEnabled */ - error = Interpreter::GetInterpreter().ProcessEnableDisable(aArgs + 1, otDnssdUpstreamQueryIsEnabled, - otDnssdUpstreamQuerySetEnabled); + error = ProcessEnableDisable(aArgs + 1, otDnssdUpstreamQueryIsEnabled, otDnssdUpstreamQuerySetEnabled); } #endif // OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE else diff --git a/src/cli/cli_dns.hpp b/src/cli/cli_dns.hpp index b9f167939..bf31ed76e 100644 --- a/src/cli/cli_dns.hpp +++ b/src/cli/cli_dns.hpp @@ -54,7 +54,7 @@ #include #include "cli/cli_config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" namespace ot { namespace Cli { @@ -63,11 +63,9 @@ namespace Cli { * Implements the DNS CLI interpreter. * */ -class Dns : private Output +class Dns : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor. * @@ -76,7 +74,7 @@ public: * */ Dns(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_history.hpp b/src/cli/cli_history.hpp index 90f832fbf..6958b0429 100644 --- a/src/cli/cli_history.hpp +++ b/src/cli/cli_history.hpp @@ -39,7 +39,7 @@ #include #include "cli/cli_config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_HISTORY_TRACKER_ENABLE @@ -50,11 +50,9 @@ namespace Cli { * Implements the History Tracker CLI interpreter. * */ -class History : private Output +class History : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * @@ -63,7 +61,7 @@ public: * */ History(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_joiner.hpp b/src/cli/cli_joiner.hpp index e45dbf8db..297525c74 100644 --- a/src/cli/cli_joiner.hpp +++ b/src/cli/cli_joiner.hpp @@ -38,7 +38,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_JOINER_ENABLE @@ -49,11 +49,9 @@ namespace Cli { * Implements the Joiner CLI interpreter. * */ -class Joiner : private Output +class Joiner : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * @@ -62,7 +60,7 @@ public: * */ Joiner(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_link_metrics.cpp b/src/cli/cli_link_metrics.cpp index 93cc8c575..a12515d1b 100644 --- a/src/cli/cli_link_metrics.cpp +++ b/src/cli/cli_link_metrics.cpp @@ -36,7 +36,7 @@ #include #include "cli/cli.hpp" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #include "common/code_utils.hpp" #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE @@ -45,7 +45,7 @@ namespace ot { namespace Cli { LinkMetrics::LinkMetrics(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) , mLinkMetricsQueryInProgress(false) { } diff --git a/src/cli/cli_link_metrics.hpp b/src/cli/cli_link_metrics.hpp index d62be780e..40b67c604 100644 --- a/src/cli/cli_link_metrics.hpp +++ b/src/cli/cli_link_metrics.hpp @@ -38,7 +38,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE @@ -50,11 +50,9 @@ namespace Cli { * */ -class LinkMetrics : private Output +class LinkMetrics : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * diff --git a/src/cli/cli_mac_filter.hpp b/src/cli/cli_mac_filter.hpp index 1394d4fa0..0f03b1b5b 100644 --- a/src/cli/cli_mac_filter.hpp +++ b/src/cli/cli_mac_filter.hpp @@ -41,7 +41,7 @@ #include #include "cli/cli_config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" namespace ot { namespace Cli { @@ -50,11 +50,9 @@ namespace Cli { * Implements the MAC Filter CLI interpreter. * */ -class MacFilter : private Output +class MacFilter : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor. * @@ -63,7 +61,7 @@ public: * */ MacFilter(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_network_data.cpp b/src/cli/cli_network_data.cpp index 85c3e262a..5f5c63293 100644 --- a/src/cli/cli_network_data.cpp +++ b/src/cli/cli_network_data.cpp @@ -44,7 +44,7 @@ namespace ot { namespace Cli { NetworkData::NetworkData(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { #if OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL mFullCallbackWasCalled = false; diff --git a/src/cli/cli_network_data.hpp b/src/cli/cli_network_data.hpp index 15af65084..d7d659614 100644 --- a/src/cli/cli_network_data.hpp +++ b/src/cli/cli_network_data.hpp @@ -38,7 +38,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" namespace ot { namespace Cli { @@ -47,11 +47,9 @@ namespace Cli { * Implements the Network Data CLI. * */ -class NetworkData : private Output +class NetworkData : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * This constant specifies the string size for representing Network Data prefix/route entry flags. * diff --git a/src/cli/cli_ping.cpp b/src/cli/cli_ping.cpp index 7d8825908..1111e3d1f 100644 --- a/src/cli/cli_ping.cpp +++ b/src/cli/cli_ping.cpp @@ -36,7 +36,7 @@ #include #include "cli/cli.hpp" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #include "common/code_utils.hpp" #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE @@ -45,7 +45,7 @@ namespace ot { namespace Cli { PingSender::PingSender(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) , mPingIsAsync(false) { } diff --git a/src/cli/cli_ping.hpp b/src/cli/cli_ping.hpp index e517506bd..6504412a4 100644 --- a/src/cli/cli_ping.hpp +++ b/src/cli/cli_ping.hpp @@ -38,7 +38,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE @@ -50,11 +50,9 @@ namespace Cli { * */ -class PingSender : private Output +class PingSender : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * diff --git a/src/cli/cli_srp_client.cpp b/src/cli/cli_srp_client.cpp index d90947bc1..a5301f390 100644 --- a/src/cli/cli_srp_client.cpp +++ b/src/cli/cli_srp_client.cpp @@ -58,7 +58,7 @@ exit: } SrpClient::SrpClient(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) , mCallbackEnabled(false) { otSrpClientSetCallback(GetInstancePtr(), SrpClient::HandleCallback, this); @@ -453,7 +453,7 @@ exit: */ template <> otError SrpClient::Process(Arg aArgs[]) { - return Interpreter::GetInterpreter().ProcessGetSet(aArgs, otSrpClientGetLeaseInterval, otSrpClientSetLeaseInterval); + return ProcessGetSet(aArgs, otSrpClientGetLeaseInterval, otSrpClientSetLeaseInterval); } /** @@ -475,8 +475,7 @@ template <> otError SrpClient::Process(Arg aArgs[]) */ template <> otError SrpClient::Process(Arg aArgs[]) { - return Interpreter::GetInterpreter().ProcessGetSet(aArgs, otSrpClientGetKeyLeaseInterval, - otSrpClientSetKeyLeaseInterval); + return ProcessGetSet(aArgs, otSrpClientGetKeyLeaseInterval, otSrpClientSetKeyLeaseInterval); } template <> otError SrpClient::Process(Arg aArgs[]) @@ -577,9 +576,9 @@ template <> otError SrpClient::Process(Arg aArgs[]) * * --> [@ca{weight}] [@ca{txt}] * The `servicename` parameter can optionally include a list of service subtype labels that are * separated by commas. The examples here use generic naming. The `priority` and `weight` (both are `uint16_t` - * values) parameters are optional, and if not provided zero is used. The optional `txt` parameter sets the TXT data - * associated with the service. The `txt` value must be in hex-string format and is treated as an already encoded - * TXT data byte sequence. + * values) parameters are optional, and if not provided zero is used. The optional `txt` parameter sets the TXT + * data associated with the service. The `txt` value must be in hex-string format and is treated as an already + * encoded TXT data byte sequence. * @par * Adds a service with a given instance name, service name, and port number. * @moreinfo{@srp}. @@ -664,8 +663,8 @@ template <> otError SrpClient::Process(Arg aArgs[]) { // `key [enable/disable]` - error = Interpreter::GetInterpreter().ProcessEnableDisable(aArgs + 1, otSrpClientIsServiceKeyRecordEnabled, - otSrpClientSetServiceKeyRecordEnabled); + error = ProcessEnableDisable(aArgs + 1, otSrpClientIsServiceKeyRecordEnabled, + otSrpClientSetServiceKeyRecordEnabled); } #endif // OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE else @@ -929,7 +928,7 @@ exit: */ template <> otError SrpClient::Process(Arg aArgs[]) { - return Interpreter::GetInterpreter().ProcessGetSet(aArgs, otSrpClientGetTtl, otSrpClientSetTtl); + return ProcessGetSet(aArgs, otSrpClientGetTtl, otSrpClientSetTtl); } void SrpClient::HandleCallback(otError aError, diff --git a/src/cli/cli_srp_client.hpp b/src/cli/cli_srp_client.hpp index 93e7fe35f..85564cd91 100644 --- a/src/cli/cli_srp_client.hpp +++ b/src/cli/cli_srp_client.hpp @@ -40,7 +40,7 @@ #include #include "cli/cli_config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE @@ -51,11 +51,9 @@ namespace Cli { * Implements the SRP Client CLI interpreter. * */ -class SrpClient : private Output +class SrpClient : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * diff --git a/src/cli/cli_srp_server.cpp b/src/cli/cli_srp_server.cpp index ee61a339f..5e71a4a24 100644 --- a/src/cli/cli_srp_server.cpp +++ b/src/cli/cli_srp_server.cpp @@ -121,8 +121,7 @@ template <> otError SrpServer::Process(Arg aArgs[]) */ template <> otError SrpServer::Process(Arg aArgs[]) { - return Interpreter::GetInterpreter().ProcessEnableDisable(aArgs, otSrpServerIsAutoEnableMode, - otSrpServerSetAutoEnableMode); + return ProcessEnableDisable(aArgs, otSrpServerIsAutoEnableMode, otSrpServerSetAutoEnableMode); } #endif diff --git a/src/cli/cli_srp_server.hpp b/src/cli/cli_srp_server.hpp index 344d3c2bb..b110eb896 100644 --- a/src/cli/cli_srp_server.hpp +++ b/src/cli/cli_srp_server.hpp @@ -38,7 +38,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE @@ -49,11 +49,9 @@ namespace Cli { * Implements the SRP Server CLI interpreter. * */ -class SrpServer : private Output +class SrpServer : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * @@ -62,7 +60,7 @@ public: * */ SrpServer(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_tcat.cpp b/src/cli/cli_tcat.cpp index 7845be89b..1ef73a561 100644 --- a/src/cli/cli_tcat.cpp +++ b/src/cli/cli_tcat.cpp @@ -28,7 +28,7 @@ #include "openthread-core-config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #include "cli/cli_tcat.hpp" diff --git a/src/cli/cli_tcat.hpp b/src/cli/cli_tcat.hpp index 91b36cc4b..697cba183 100644 --- a/src/cli/cli_tcat.hpp +++ b/src/cli/cli_tcat.hpp @@ -33,7 +33,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE && OPENTHREAD_CONFIG_CLI_BLE_SECURE_ENABLE @@ -45,11 +45,9 @@ namespace Cli { * Implements the Tcat CLI interpreter. * */ -class Tcat : private Output +class Tcat : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * @@ -58,7 +56,7 @@ public: * */ Tcat(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) { } diff --git a/src/cli/cli_tcp.cpp b/src/cli/cli_tcp.cpp index 90ce1aaef..b891c2df6 100644 --- a/src/cli/cli_tcp.cpp +++ b/src/cli/cli_tcp.cpp @@ -61,7 +61,7 @@ const int TcpExample::sCipherSuites[] = {MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8, #endif TcpExample::TcpExample(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) , mInitialized(false) , mEndpointConnected(false) , mEndpointConnectedFastOpen(false) diff --git a/src/cli/cli_tcp.hpp b/src/cli/cli_tcp.hpp index 50e728025..e658edee0 100644 --- a/src/cli/cli_tcp.hpp +++ b/src/cli/cli_tcp.hpp @@ -49,7 +49,7 @@ #endif #include "cli/cli_config.h" -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" #include "common/time.hpp" namespace ot { @@ -59,11 +59,9 @@ namespace Cli { * Implements a CLI-based TCP example. * */ -class TcpExample : private Output +class TcpExample : private Utils { public: - using Arg = Utils::CmdLineParser::Arg; - /** * Constructor * diff --git a/src/cli/cli_udp.cpp b/src/cli/cli_udp.cpp index 9f8afdf2a..299be12ef 100644 --- a/src/cli/cli_udp.cpp +++ b/src/cli/cli_udp.cpp @@ -44,7 +44,7 @@ namespace ot { namespace Cli { UdpExample::UdpExample(otInstance *aInstance, OutputImplementer &aOutputImplementer) - : Output(aInstance, aOutputImplementer) + : Utils(aInstance, aOutputImplementer) , mLinkSecurityEnabled(true) { ClearAllBytes(mSocket); @@ -408,7 +408,7 @@ otError UdpExample::PrepareHexStringPayload(otMessage &aMessage, const char *aHe while (!done) { length = sizeof(buf); - error = Utils::CmdLineParser::ParseAsHexStringSegment(aHexString, length, buf); + error = ot::Utils::CmdLineParser::ParseAsHexStringSegment(aHexString, length, buf); VerifyOrExit((error == OT_ERROR_NONE) || (error == OT_ERROR_PENDING)); done = (error == OT_ERROR_NONE); diff --git a/src/cli/cli_udp.hpp b/src/cli/cli_udp.hpp index 0c15c8070..d1c70508d 100644 --- a/src/cli/cli_udp.hpp +++ b/src/cli/cli_udp.hpp @@ -38,7 +38,7 @@ #include -#include "cli/cli_output.hpp" +#include "cli/cli_utils.hpp" namespace ot { namespace Cli { @@ -47,11 +47,9 @@ namespace Cli { * Implements a CLI-based UDP example. * */ -class UdpExample : private Output +class UdpExample : private Utils { public: - typedef Utils::CmdLineParser::Arg Arg; - /** * Constructor * diff --git a/src/cli/cli_output.cpp b/src/cli/cli_utils.cpp similarity index 71% rename from src/cli/cli_output.cpp rename to src/cli/cli_utils.cpp index c70eacdd2..f2f53593d 100644 --- a/src/cli/cli_output.cpp +++ b/src/cli/cli_utils.cpp @@ -31,7 +31,7 @@ * This file contains implementation of the CLI output module. */ -#include "cli_output.hpp" +#include "cli_utils.hpp" #include #include @@ -48,7 +48,7 @@ namespace ot { namespace Cli { -const char Output::kUnknownString[] = "unknown"; +const char Utils::kUnknownString[] = "unknown"; OutputImplementer::OutputImplementer(otCliOutputCallback aCallback, void *aCallbackContext) : mCallback(aCallback) @@ -60,7 +60,7 @@ OutputImplementer::OutputImplementer(otCliOutputCallback aCallback, void *aCallb { } -void Output::OutputFormat(const char *aFormat, ...) +void Utils::OutputFormat(const char *aFormat, ...) { va_list args; @@ -69,7 +69,7 @@ void Output::OutputFormat(const char *aFormat, ...) va_end(args); } -void Output::OutputFormat(uint8_t aIndentSize, const char *aFormat, ...) +void Utils::OutputFormat(uint8_t aIndentSize, const char *aFormat, ...) { va_list args; @@ -80,7 +80,7 @@ void Output::OutputFormat(uint8_t aIndentSize, const char *aFormat, ...) va_end(args); } -void Output::OutputLine(const char *aFormat, ...) +void Utils::OutputLine(const char *aFormat, ...) { va_list args; @@ -91,7 +91,7 @@ void Output::OutputLine(const char *aFormat, ...) OutputNewLine(); } -void Output::OutputLine(uint8_t aIndentSize, const char *aFormat, ...) +void Utils::OutputLine(uint8_t aIndentSize, const char *aFormat, ...) { va_list args; @@ -104,11 +104,11 @@ void Output::OutputLine(uint8_t aIndentSize, const char *aFormat, ...) OutputNewLine(); } -void Output::OutputNewLine(void) { OutputFormat("\r\n"); } +void Utils::OutputNewLine(void) { OutputFormat("\r\n"); } -void Output::OutputSpaces(uint8_t aCount) { OutputFormat("%*s", aCount, ""); } +void Utils::OutputSpaces(uint8_t aCount) { OutputFormat("%*s", aCount, ""); } -void Output::OutputBytes(const uint8_t *aBytes, uint16_t aLength) +void Utils::OutputBytes(const uint8_t *aBytes, uint16_t aLength) { for (uint16_t i = 0; i < aLength; i++) { @@ -116,13 +116,13 @@ void Output::OutputBytes(const uint8_t *aBytes, uint16_t aLength) } } -void Output::OutputBytesLine(const uint8_t *aBytes, uint16_t aLength) +void Utils::OutputBytesLine(const uint8_t *aBytes, uint16_t aLength) { OutputBytes(aBytes, aLength); OutputNewLine(); } -const char *Output::Uint64ToString(uint64_t aUint64, Uint64StringBuffer &aBuffer) +const char *Utils::Uint64ToString(uint64_t aUint64, Uint64StringBuffer &aBuffer) { char *cur = &aBuffer.mChars[Uint64StringBuffer::kSize - 1]; @@ -143,24 +143,24 @@ const char *Output::Uint64ToString(uint64_t aUint64, Uint64StringBuffer &aBuffer return cur; } -void Output::OutputUint64(uint64_t aUint64) +void Utils::OutputUint64(uint64_t aUint64) { Uint64StringBuffer buffer; OutputFormat("%s", Uint64ToString(aUint64, buffer)); } -void Output::OutputUint64Line(uint64_t aUint64) +void Utils::OutputUint64Line(uint64_t aUint64) { OutputUint64(aUint64); OutputNewLine(); } -void Output::OutputEnabledDisabledStatus(bool aEnabled) { OutputLine(aEnabled ? "Enabled" : "Disabled"); } +void Utils::OutputEnabledDisabledStatus(bool aEnabled) { OutputLine(aEnabled ? "Enabled" : "Disabled"); } #if OPENTHREAD_FTD || OPENTHREAD_MTD -void Output::OutputIp6Address(const otIp6Address &aAddress) +void Utils::OutputIp6Address(const otIp6Address &aAddress) { char string[OT_IP6_ADDRESS_STRING_SIZE]; @@ -169,13 +169,13 @@ void Output::OutputIp6Address(const otIp6Address &aAddress) return OutputFormat("%s", string); } -void Output::OutputIp6AddressLine(const otIp6Address &aAddress) +void Utils::OutputIp6AddressLine(const otIp6Address &aAddress) { OutputIp6Address(aAddress); OutputNewLine(); } -void Output::OutputIp6Prefix(const otIp6Prefix &aPrefix) +void Utils::OutputIp6Prefix(const otIp6Prefix &aPrefix) { char string[OT_IP6_PREFIX_STRING_SIZE]; @@ -184,25 +184,25 @@ void Output::OutputIp6Prefix(const otIp6Prefix &aPrefix) OutputFormat("%s", string); } -void Output::OutputIp6PrefixLine(const otIp6Prefix &aPrefix) +void Utils::OutputIp6PrefixLine(const otIp6Prefix &aPrefix) { OutputIp6Prefix(aPrefix); OutputNewLine(); } -void Output::OutputIp6Prefix(const otIp6NetworkPrefix &aPrefix) +void Utils::OutputIp6Prefix(const otIp6NetworkPrefix &aPrefix) { OutputFormat("%x:%x:%x:%x::/64", (aPrefix.m8[0] << 8) | aPrefix.m8[1], (aPrefix.m8[2] << 8) | aPrefix.m8[3], (aPrefix.m8[4] << 8) | aPrefix.m8[5], (aPrefix.m8[6] << 8) | aPrefix.m8[7]); } -void Output::OutputIp6PrefixLine(const otIp6NetworkPrefix &aPrefix) +void Utils::OutputIp6PrefixLine(const otIp6NetworkPrefix &aPrefix) { OutputIp6Prefix(aPrefix); OutputNewLine(); } -void Output::OutputSockAddr(const otSockAddr &aSockAddr) +void Utils::OutputSockAddr(const otSockAddr &aSockAddr) { char string[OT_IP6_SOCK_ADDR_STRING_SIZE]; @@ -211,13 +211,13 @@ void Output::OutputSockAddr(const otSockAddr &aSockAddr) return OutputFormat("%s", string); } -void Output::OutputSockAddrLine(const otSockAddr &aSockAddr) +void Utils::OutputSockAddrLine(const otSockAddr &aSockAddr) { OutputSockAddr(aSockAddr); OutputNewLine(); } -void Output::OutputDnsTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLength) +void Utils::OutputDnsTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLength) { otDnsTxtEntry entry; otDnsTxtEntryIterator iterator; @@ -262,7 +262,7 @@ void Output::OutputDnsTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLength) OutputFormat("]"); } -const char *Output::PercentageToString(uint16_t aValue, PercentageStringBuffer &aBuffer) +const char *Utils::PercentageToString(uint16_t aValue, PercentageStringBuffer &aBuffer) { uint32_t scaledValue = aValue; StringWriter writer(aBuffer.mChars, sizeof(aBuffer.mChars)); @@ -275,7 +275,7 @@ const char *Output::PercentageToString(uint16_t aValue, PercentageStringBuffer & #endif // OPENTHREAD_FTD || OPENTHREAD_MTD -void Output::OutputFormatV(const char *aFormat, va_list aArguments) { mImplementer.OutputV(aFormat, aArguments); } +void Utils::OutputFormatV(const char *aFormat, va_list aArguments) { mImplementer.OutputV(aFormat, aArguments); } void OutputImplementer::OutputV(const char *aFormat, va_list aArguments) { @@ -370,7 +370,7 @@ exit: } #if OPENTHREAD_CONFIG_CLI_LOG_INPUT_OUTPUT_ENABLE -void Output::LogInput(const Arg *aArgs) +void Utils::LogInput(const Arg *aArgs) { String inputString; @@ -383,7 +383,7 @@ void Output::LogInput(const Arg *aArgs) } #endif -void Output::OutputTableHeader(uint8_t aNumColumns, const char *const aTitles[], const uint8_t aWidths[]) +void Utils::OutputTableHeader(uint8_t aNumColumns, const char *const aTitles[], const uint8_t aWidths[]) { for (uint8_t index = 0; index < aNumColumns; index++) { @@ -412,7 +412,7 @@ void Output::OutputTableHeader(uint8_t aNumColumns, const char *const aTitles[], OutputTableSeparator(aNumColumns, aWidths); } -void Output::OutputTableSeparator(uint8_t aNumColumns, const uint8_t aWidths[]) +void Utils::OutputTableSeparator(uint8_t aNumColumns, const uint8_t aWidths[]) { for (uint8_t index = 0; index < aNumColumns; index++) { @@ -427,5 +427,95 @@ void Output::OutputTableSeparator(uint8_t aNumColumns, const uint8_t aWidths[]) OutputLine("+"); } +otError Utils::ParseEnableOrDisable(const Arg &aArg, bool &aEnable) +{ + otError error = OT_ERROR_NONE; + + if (aArg == "enable") + { + aEnable = true; + } + else if (aArg == "disable") + { + aEnable = false; + } + else + { + error = OT_ERROR_INVALID_COMMAND; + } + + return error; +} + +otError Utils::ProcessEnableDisable(Arg aArgs[], SetEnabledHandler aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + bool enable; + + if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) + { + aSetEnabledHandler(GetInstancePtr(), enable); + } + else + { + error = OT_ERROR_INVALID_COMMAND; + } + + return error; +} + +otError Utils::ProcessEnableDisable(Arg aArgs[], SetEnabledHandlerFailable aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + bool enable; + + if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) + { + error = aSetEnabledHandler(GetInstancePtr(), enable); + } + else + { + error = OT_ERROR_INVALID_COMMAND; + } + + return error; +} + +otError Utils::ProcessEnableDisable(Arg aArgs[], + IsEnabledHandler aIsEnabledHandler, + SetEnabledHandler aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + + if (aArgs[0].IsEmpty()) + { + OutputEnabledDisabledStatus(aIsEnabledHandler(GetInstancePtr())); + } + else + { + error = ProcessEnableDisable(aArgs, aSetEnabledHandler); + } + + return error; +} + +otError Utils::ProcessEnableDisable(Arg aArgs[], + IsEnabledHandler aIsEnabledHandler, + SetEnabledHandlerFailable aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + + if (aArgs[0].IsEmpty()) + { + OutputEnabledDisabledStatus(aIsEnabledHandler(GetInstancePtr())); + } + else + { + error = ProcessEnableDisable(aArgs, aSetEnabledHandler); + } + + return error; +} + } // namespace Cli } // namespace ot diff --git a/src/cli/cli_output.hpp b/src/cli/cli_utils.hpp similarity index 76% rename from src/cli/cli_output.hpp rename to src/cli/cli_utils.hpp index 3029108cf..ebf9cc14a 100644 --- a/src/cli/cli_output.hpp +++ b/src/cli/cli_utils.hpp @@ -70,7 +70,7 @@ constexpr static CommandId Cmd(const char *aString) return (aString[0] == '\0') ? 0 : (static_cast(aString[0]) + Cmd(aString + 1) * 255u); } -class Output; +class Utils; /** * Implements the basic output functions. @@ -78,7 +78,7 @@ class Output; */ class OutputImplementer { - friend class Output; + friend class Utils; public: /** @@ -111,13 +111,13 @@ private: }; /** - * Provides CLI output helper methods. + * Provides CLI helper methods. * */ -class Output +class Utils { public: - typedef Utils::CmdLineParser::Arg Arg; ///< An argument + typedef ot::Utils::CmdLineParser::Arg Arg; ///< An argument /** * Represent a CLI command table entry, mapping a command with `aName` to a handler method. @@ -190,7 +190,7 @@ public: * @param[in] aImplementer An `OutputImplementer`. * */ - Output(otInstance *aInstance, OutputImplementer &aImplementer) + Utils(otInstance *aInstance, OutputImplementer &aImplementer) : mInstance(aInstance) , mImplementer(aImplementer) { @@ -543,6 +543,108 @@ public: memset(reinterpret_cast(&aObject), 0, sizeof(ObjectType)); } + // Definitions of handlers to process Get/Set/Enable/Disable. + template using GetHandler = ValueType (&)(otInstance *); + template using SetHandler = void (&)(otInstance *, ValueType); + template using SetHandlerFailable = otError (&)(otInstance *, ValueType); + using IsEnabledHandler = bool (&)(otInstance *); + using SetEnabledHandler = void (&)(otInstance *, bool); + using SetEnabledHandlerFailable = otError (&)(otInstance *, bool); + + // Returns format string to output a `ValueType` (e.g., "%u" for `uint16_t`). + template static constexpr const char *FormatStringFor(void); + + /** + * Checks a given argument string against "enable" or "disable" commands. + * + * @param[in] aArg The argument string to parse. + * @param[out] aEnable Boolean variable to return outcome on success. + * Set to TRUE for "enable" command, and FALSE for "disable" command. + * + * @retval OT_ERROR_NONE Successfully parsed the @p aString and updated @p aEnable. + * @retval OT_ERROR_INVALID_COMMAND The @p aString is not "enable" or "disable" command. + * + */ + static otError ParseEnableOrDisable(const Arg &aArg, bool &aEnable); + + // General template implementation. + // Specializations for `uint32_t` and `int32_t` are added at the end. + template otError ProcessGet(Arg aArgs[], GetHandler aGetHandler) + { + static_assert( + TypeTraits::IsSame::kValue || TypeTraits::IsSame::kValue || + TypeTraits::IsSame::kValue || TypeTraits::IsSame::kValue || + TypeTraits::IsSame::kValue, + "ValueType must be an 8, 16 `int` or `uint` type, or a `const char *`"); + + otError error = OT_ERROR_NONE; + + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + OutputLine(FormatStringFor(), aGetHandler(GetInstancePtr())); + + exit: + return error; + } + + template otError ProcessSet(Arg aArgs[], SetHandler aSetHandler) + { + otError error; + ValueType value; + + SuccessOrExit(error = aArgs[0].ParseAs(value)); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + + aSetHandler(GetInstancePtr(), value); + + exit: + return error; + } + + template otError ProcessSet(Arg aArgs[], SetHandlerFailable aSetHandler) + { + otError error; + ValueType value; + + SuccessOrExit(error = aArgs[0].ParseAs(value)); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + + error = aSetHandler(GetInstancePtr(), value); + + exit: + return error; + } + + template + otError ProcessGetSet(Arg aArgs[], GetHandler aGetHandler, SetHandler aSetHandler) + { + otError error = ProcessGet(aArgs, aGetHandler); + + VerifyOrExit(error != OT_ERROR_NONE); + error = ProcessSet(aArgs, aSetHandler); + + exit: + return error; + } + + template + otError ProcessGetSet(Arg aArgs[], GetHandler aGetHandler, SetHandlerFailable aSetHandler) + { + otError error = ProcessGet(aArgs, aGetHandler); + + VerifyOrExit(error != OT_ERROR_NONE); + error = ProcessSet(aArgs, aSetHandler); + + exit: + return error; + } + + otError ProcessEnableDisable(Arg aArgs[], SetEnabledHandler aSetEnabledHandler); + otError ProcessEnableDisable(Arg aArgs[], SetEnabledHandlerFailable aSetEnabledHandler); + otError ProcessEnableDisable(Arg aArgs[], IsEnabledHandler aIsEnabledHandler, SetEnabledHandler aSetEnabledHandler); + otError ProcessEnableDisable(Arg aArgs[], + IsEnabledHandler aIsEnabledHandler, + SetEnabledHandlerFailable aSetEnabledHandler); + protected: void OutputFormatV(const char *aFormat, va_list aArguments); @@ -562,6 +664,46 @@ private: OutputImplementer &mImplementer; }; +// Specializations of `FormatStringFor()` + +template <> inline constexpr const char *Utils::FormatStringFor(void) { return "%u"; } + +template <> inline constexpr const char *Utils::FormatStringFor(void) { return "%u"; } + +template <> inline constexpr const char *Utils::FormatStringFor(void) { return "%lu"; } + +template <> inline constexpr const char *Utils::FormatStringFor(void) { return "%d"; } + +template <> inline constexpr const char *Utils::FormatStringFor(void) { return "%d"; } + +template <> inline constexpr const char *Utils::FormatStringFor(void) { return "%ld"; } + +template <> inline constexpr const char *Utils::FormatStringFor(void) { return "%s"; } + +// Specialization of ProcessGet<> for `uint32_t` and `int32_t` + +template <> inline otError Utils::ProcessGet(Arg aArgs[], GetHandler aGetHandler) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + OutputLine(FormatStringFor(), ToUlong(aGetHandler(GetInstancePtr()))); + +exit: + return error; +} + +template <> inline otError Utils::ProcessGet(Arg aArgs[], GetHandler aGetHandler) +{ + otError error = OT_ERROR_NONE; + + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + OutputLine(FormatStringFor(), static_cast(aGetHandler(GetInstancePtr()))); + +exit: + return error; +} + } // namespace Cli } // namespace ot diff --git a/src/cli/radio.cmake b/src/cli/radio.cmake index 449f99612..47652d2c6 100644 --- a/src/cli/radio.cmake +++ b/src/cli/radio.cmake @@ -45,7 +45,7 @@ target_include_directories(openthread-cli-radio PUBLIC ${OT_PUBLIC_INCLUDES} PRI target_sources(openthread-cli-radio PRIVATE cli.cpp - cli_output.cpp + cli_utils.cpp ) if(NOT DEFINED OT_MBEDTLS_RCP)