2 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 /* TIZEN_CHIP_MODIFY */
28 #if CONFIG_DEVICE_LAYER
29 #include <platform/CHIPDeviceLayer.h>
32 #include <support/CHIPMem.h>
33 #include <support/CodeUtils.h>
35 void Commands::Register(const char * clusterName, commands_list commandsList)
37 for (auto & command : commandsList)
39 mClusters[clusterName].push_back(std::move(command));
43 int Commands::Run(NodeId localId, NodeId remoteId, int argc, char ** argv)
45 CHIP_ERROR err = CHIP_NO_ERROR;
46 PersistentStorage storage;
48 err = chip::Platform::MemoryInit();
49 VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init Memory failure: %s", chip::ErrorStr(err)));
51 #if (CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE) || \
52 (CHIP_DEVICE_LAYER_TARGET_TIZEN && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE)
53 // By default, Linux device is configured as a BLE peripheral while the controller needs a BLE central.
54 SuccessOrExit(err = chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(/* BLE adapter ID */ 0, /* BLE central */ true));
58 VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init Storage failure: %s", chip::ErrorStr(err)));
60 chip::Logging::SetLogFilter(storage.GetLoggingLevel());
62 err = RunCommand(storage, localId, remoteId, argc, argv);
66 return (err == CHIP_NO_ERROR) ? EXIT_SUCCESS : EXIT_FAILURE;
69 CHIP_ERROR Commands::RunCommand(PersistentStorage & storage, NodeId localId, NodeId remoteId, int argc, char ** argv)
71 CHIP_ERROR err = CHIP_NO_ERROR;
72 std::map<std::string, CommandsVector>::iterator cluster;
73 Command * command = nullptr;
77 ChipLogError(chipTool, "Missing cluster name");
78 ShowClusters(argv[0]);
79 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
82 cluster = GetCluster(argv[1]);
83 if (cluster == mClusters.end())
85 ChipLogError(chipTool, "Unknown cluster: %s", argv[1]);
86 ShowClusters(argv[0]);
87 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
92 ChipLogError(chipTool, "Missing command name");
93 ShowCluster(argv[0], argv[1], cluster->second);
94 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
97 if (!IsGlobalCommand(argv[2]))
99 command = GetCommand(cluster->second, argv[2]);
100 if (command == nullptr)
102 ChipLogError(chipTool, "Unknown command: %s", argv[2]);
103 ShowCluster(argv[0], argv[1], cluster->second);
104 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
111 ChipLogError(chipTool, "Missing attribute name");
112 ShowClusterAttributes(argv[0], argv[1], argv[2], cluster->second);
113 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
116 command = GetGlobalCommand(cluster->second, argv[2], argv[3]);
117 if (command == nullptr)
119 ChipLogError(chipTool, "Unknown attribute: %s", argv[3]);
120 ShowClusterAttributes(argv[0], argv[1], argv[2], cluster->second);
121 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
125 if (!command->InitArguments(argc - 3, &argv[3]))
127 ShowCommand(argv[0], argv[1], command);
128 ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
131 err = command->Run(storage, localId, remoteId);
132 if (err != CHIP_NO_ERROR)
134 ChipLogError(chipTool, "Run command failure: %s", chip::ErrorStr(err));
142 std::map<std::string, Commands::CommandsVector>::iterator Commands::GetCluster(std::string clusterName)
144 for (auto & cluster : mClusters)
146 std::string key(cluster.first);
147 std::transform(key.begin(), key.end(), key.begin(), ::tolower);
148 if (key.compare(clusterName) == 0)
150 return mClusters.find(cluster.first);
154 return mClusters.end();
157 Command * Commands::GetCommand(CommandsVector & commands, std::string commandName)
159 for (auto & command : commands)
161 if (commandName.compare(command->GetName()) == 0)
163 return command.get();
170 Command * Commands::GetGlobalCommand(CommandsVector & commands, std::string commandName, std::string attributeName)
172 for (auto & command : commands)
174 if (commandName.compare(command->GetName()) == 0 && attributeName.compare(command->GetAttribute()) == 0)
176 return command.get();
183 bool Commands::IsGlobalCommand(std::string commandName) const
185 return commandName.compare("read") == 0 || commandName.compare("write") == 0 || commandName.compare("report") == 0;
188 void Commands::ShowClusters(std::string executable)
190 fprintf(stderr, "Usage:\n");
191 fprintf(stderr, " %s cluster_name command_name [param1 param2 ...]\n", executable.c_str());
192 fprintf(stderr, "\n");
193 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
194 fprintf(stderr, " | Clusters: |\n");
195 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
196 for (auto & cluster : mClusters)
198 std::string clusterName(cluster.first);
199 std::transform(clusterName.begin(), clusterName.end(), clusterName.begin(),
200 [](unsigned char c) { return std::tolower(c); });
201 fprintf(stderr, " | * %-82s|\n", clusterName.c_str());
203 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
206 void Commands::ShowCluster(std::string executable, std::string clusterName, CommandsVector & commands)
208 fprintf(stderr, "Usage:\n");
209 fprintf(stderr, " %s %s command_name [param1 param2 ...]\n", executable.c_str(), clusterName.c_str());
210 fprintf(stderr, "\n");
211 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
212 fprintf(stderr, " | Commands: |\n");
213 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
214 bool readCommand = false;
215 bool writeCommand = false;
216 bool reportCommand = false;
217 for (auto & command : commands)
219 bool shouldPrint = true;
221 if (IsGlobalCommand(command->GetName()))
223 if (strcmp(command->GetName(), "read") == 0 && readCommand == false)
227 else if (strcmp(command->GetName(), "write") == 0 && writeCommand == false)
231 else if (strcmp(command->GetName(), "report") == 0 && reportCommand == false)
233 reportCommand = true;
243 fprintf(stderr, " | * %-82s|\n", command->GetName());
246 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
249 void Commands::ShowClusterAttributes(std::string executable, std::string clusterName, std::string commandName,
250 CommandsVector & commands)
252 fprintf(stderr, "Usage:\n");
253 fprintf(stderr, " %s %s %s attribute-name [param1 param2 ...]\n", executable.c_str(), clusterName.c_str(),
254 commandName.c_str());
255 fprintf(stderr, "\n");
256 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
257 fprintf(stderr, " | Attributes: |\n");
258 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
259 for (auto & command : commands)
261 if (commandName.compare(command->GetName()) == 0)
263 fprintf(stderr, " | * %-82s|\n", command->GetAttribute());
266 fprintf(stderr, " +-------------------------------------------------------------------------------------+\n");
269 void Commands::ShowCommand(std::string executable, std::string clusterName, Command * command)
271 fprintf(stderr, "Usage:\n");
273 std::string arguments = "";
274 arguments += command->GetName();
276 size_t argumentsCount = command->GetArgumentsCount();
277 for (size_t i = 0; i < argumentsCount; i++)
280 arguments += command->GetArgumentName(i);
282 fprintf(stderr, " %s %s %s\n", executable.c_str(), clusterName.c_str(), arguments.c_str());