From 1103155a857fea5f38f3a93d2f2adeaf06bfddcc Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 15 Dec 2021 15:18:59 +0100 Subject: [PATCH] [chip-tool] Optional argument parsing does not work after #12732 (#13028) --- examples/chip-tool/README.md | 2 +- .../chip-tool/commands/common/CHIPCommand.cpp | 31 +++++ .../chip-tool/commands/common/CHIPCommand.h | 17 ++- .../chip-tool/commands/common/Commands.cpp | 123 +++--------------- examples/chip-tool/commands/common/Commands.h | 2 +- 5 files changed, 69 insertions(+), 106 deletions(-) diff --git a/examples/chip-tool/README.md b/examples/chip-tool/README.md index cbe544a80291fc..b0a8a513e204e9 100644 --- a/examples/chip-tool/README.md +++ b/examples/chip-tool/README.md @@ -48,7 +48,7 @@ traces should go: For example: ``` -out/with_trace/chip-tool --trace_file trace.log pairing +out/with_trace/chip-tool pairing --trace_file trace.log ``` ## Using the Client to commission a device diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index b8568da129a48e..8d64fd7b454154 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -19,6 +19,7 @@ #include "CHIPCommand.h" #include +#include #include #include #include @@ -27,6 +28,10 @@ #include #include +#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED +#include "TraceHandlers.h" +#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED + using DeviceControllerFactory = chip::Controller::DeviceControllerFactory; constexpr chip::FabricId kIdentityAlphaFabricId = 1; @@ -35,6 +40,8 @@ constexpr chip::FabricId kIdentityGammaFabricId = 3; CHIP_ERROR CHIPCommand::Run() { + StartTracing(); + #if CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE // By default, Linux device is configured as a BLE peripheral while the controller needs a BLE central. ReturnLogErrorOnFailure(chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(0, true)); @@ -62,9 +69,33 @@ CHIP_ERROR CHIPCommand::Run() // ReturnLogErrorOnFailure(ShutdownCommissioner(GetIdentity())); + StopTracing(); return CHIP_NO_ERROR; } +void CHIPCommand::StartTracing() +{ +#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED + chip::trace::InitTrace(); + + if (mTraceFile.HasValue()) + { + chip::trace::SetTraceStream(new chip::trace::TraceStreamFile(mTraceFile.Value())); + } + else if (mTraceLog.HasValue() && mTraceLog.Value() == true) + { + chip::trace::SetTraceStream(new chip::trace::TraceStreamLog()); + } +#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED +} + +void CHIPCommand::StopTracing() +{ +#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED + chip::trace::DeInitTrace(); +#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED +} + void CHIPCommand::SetIdentity(const char * identity) { std::string name = std::string(identity); diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index f1aeadbbffea81..efc6396311e41e 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -41,7 +41,14 @@ class CHIPCommand : public Command using NodeId = ::chip::NodeId; using PeerAddress = ::chip::Transport::PeerAddress; - CHIPCommand(const char * commandName) : Command(commandName) { AddArgument("commissioner-name", &mCommissionerName); } + CHIPCommand(const char * commandName) : Command(commandName) + { + AddArgument("commissioner-name", &mCommissionerName); +#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED + AddArgument("trace_file", &mTraceFile); + AddArgument("trace_log", 0, 1, &mTraceLog); +#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED + } /////////// Command Interface ///////// CHIP_ERROR Run() override; @@ -100,4 +107,12 @@ class CHIPCommand : public Command std::mutex cvWaitingForResponseMutex; bool mWaitingForResponse{ true }; #endif // CONFIG_USE_SEPARATE_EVENTLOOP + + void StartTracing(); + void StopTracing(); + +#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED + chip::Optional mTraceFile; + chip::Optional mTraceLog; +#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED }; diff --git a/examples/chip-tool/commands/common/Commands.cpp b/examples/chip-tool/commands/common/Commands.cpp index f289c4ef52fba9..555c03a1133071 100644 --- a/examples/chip-tool/commands/common/Commands.cpp +++ b/examples/chip-tool/commands/common/Commands.cpp @@ -26,79 +26,6 @@ #include #include -#include - -#include - -#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED -#include "TraceHandlers.h" -#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED - -namespace { - -#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED -enum -{ - kOptionTraceFile = 0x1000, - kOptionTraceLog, -}; - -bool HandleTraceOptions(const char * program, chip::ArgParser::OptionSet * options, int identifier, const char * name, - const char * value) -{ - switch (identifier) - { - case kOptionTraceLog: - chip::trace::SetTraceStream(new chip::trace::TraceStreamLog()); - return true; - case kOptionTraceFile: - chip::trace::SetTraceStream(new chip::trace::TraceStreamFile(value)); - return true; - default: - chip::ArgParser::PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", program, name); - return false; - } -} - -chip::ArgParser::OptionDef traceCmdLineOptionDefs[] = { { "trace_file", chip::ArgParser::kArgumentRequired, kOptionTraceFile }, - { "trace_log", chip::ArgParser::kNoArgument, kOptionTraceLog }, - {} }; - -const char * traceOptionHelp = " --trace_file \n" - " Output trace data to the specified file.\n" - " --trace_log\n" - " Output trace data to the log stream.\n" - "\n"; -chip::ArgParser::OptionSet traceCmdLineOptions = { HandleTraceOptions, traceCmdLineOptionDefs, "TRACE OPTIONS", traceOptionHelp }; -#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED - -const char * kAppName = "chip-tool"; -const char * kUsage = "Usage: chip-tool [options] cluster command_name [param1] [param2]"; -const char * kVersion = nullptr; // Unknown -const char * kDescription = "A command line tool that uses Matter to send messages to a Matter server."; -chip::ArgParser::HelpOptions helpOptions(kAppName, kUsage, kVersion, kDescription); - -chip::ArgParser::OptionSet * allOptions[] = { -#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED - &traceCmdLineOptions, -#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED - &helpOptions, nullptr -}; - -int gPositionalArgc = 0; -char ** gPositionalArgv = nullptr; -const char * gProgramName = nullptr; - -bool GetPositionalArgs(const char * prog, int argc, char * argv[]) -{ - gProgramName = prog; - gPositionalArgc = argc; - gPositionalArgv = argv; - return true; -} - -} // namespace - void Commands::Register(const char * clusterName, commands_list commandsList) { for (auto & command : commandsList) @@ -119,81 +46,71 @@ int Commands::Run(int argc, char ** argv) chip::Logging::SetLogFilter(mStorage.GetLoggingLevel()); - VerifyOrExit(chip::ArgParser::ParseArgs("chip-tool", argc, argv, allOptions, GetPositionalArgs), - ChipLogError(chipTool, "Error parsing arguments")); - -#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED - chip::trace::InitTrace(); -#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED - - err = RunCommand(); + err = RunCommand(argc, argv); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(err))); exit: -#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED - chip::trace::DeInitTrace(); -#endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED return (err == CHIP_NO_ERROR) ? EXIT_SUCCESS : EXIT_FAILURE; } -CHIP_ERROR Commands::RunCommand() +CHIP_ERROR Commands::RunCommand(int argc, char ** argv) { std::map::iterator cluster; Command * command = nullptr; - if (gPositionalArgc <= 0) + if (argc <= 1) { ChipLogError(chipTool, "Missing cluster name"); - ShowClusters(gProgramName); + ShowClusters(argv[0]); return CHIP_ERROR_INVALID_ARGUMENT; } - cluster = GetCluster(gPositionalArgv[0]); + cluster = GetCluster(argv[1]); if (cluster == mClusters.end()) { - ChipLogError(chipTool, "Unknown cluster: %s", gPositionalArgv[1]); - ShowClusters(gProgramName); + ChipLogError(chipTool, "Unknown cluster: %s", argv[1]); + ShowClusters(argv[0]); return CHIP_ERROR_INVALID_ARGUMENT; } - if (gPositionalArgc <= 1) + if (argc <= 2) { ChipLogError(chipTool, "Missing command name"); - ShowCluster(gProgramName, gPositionalArgv[0], cluster->second); + ShowCluster(argv[0], argv[1], cluster->second); return CHIP_ERROR_INVALID_ARGUMENT; } - if (!IsGlobalCommand(gPositionalArgv[1])) + if (!IsGlobalCommand(argv[2])) { - command = GetCommand(cluster->second, gPositionalArgv[1]); + command = GetCommand(cluster->second, argv[2]); if (command == nullptr) { - ChipLogError(chipTool, "Unknown command: %s", gPositionalArgv[1]); - ShowCluster(gProgramName, gPositionalArgv[0], cluster->second); + ChipLogError(chipTool, "Unknown command: %s", argv[2]); + ShowCluster(argv[0], argv[1], cluster->second); return CHIP_ERROR_INVALID_ARGUMENT; } } else { - if (gPositionalArgc <= 2) + if (argc <= 3) { ChipLogError(chipTool, "Missing attribute name"); - ShowClusterAttributes(gProgramName, gPositionalArgv[0], gPositionalArgv[1], cluster->second); + ShowClusterAttributes(argv[0], argv[1], argv[2], cluster->second); return CHIP_ERROR_INVALID_ARGUMENT; } - command = GetGlobalCommand(cluster->second, gPositionalArgv[1], gPositionalArgv[2]); + command = GetGlobalCommand(cluster->second, argv[2], argv[3]); if (command == nullptr) { - ChipLogError(chipTool, "Unknown attribute: %s", gPositionalArgv[2]); - ShowClusterAttributes(gProgramName, gPositionalArgv[0], gPositionalArgv[1], cluster->second); + ChipLogError(chipTool, "Unknown attribute: %s", argv[3]); + ShowClusterAttributes(argv[0], argv[1], argv[2], cluster->second); return CHIP_ERROR_INVALID_ARGUMENT; } } - if (!command->InitArguments(gPositionalArgc - 2, &gPositionalArgv[2])) + if (!command->InitArguments(argc - 3, &argv[3])) { - ShowCommand(gProgramName, gPositionalArgv[0], command); + ShowCommand(argv[0], argv[1], command); return CHIP_ERROR_INVALID_ARGUMENT; } diff --git a/examples/chip-tool/commands/common/Commands.h b/examples/chip-tool/commands/common/Commands.h index 2196eaf1ce5f02..e96d97ea9b0d3e 100644 --- a/examples/chip-tool/commands/common/Commands.h +++ b/examples/chip-tool/commands/common/Commands.h @@ -31,7 +31,7 @@ class Commands int Run(int argc, char ** argv); private: - CHIP_ERROR RunCommand(); + CHIP_ERROR RunCommand(int argc, char ** argv); std::map::iterator GetCluster(std::string clusterName); Command * GetCommand(CommandsVector & commands, std::string commandName);