1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hidump_helper.h"
17
18 #include <unordered_map>
19
20 #include "capability_info_manager.h"
21 #include "component_loader.h"
22 #include "component_manager.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "task_board.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 IMPLEMENT_SINGLE_INSTANCE(HidumpHelper);
30 namespace {
31 const std::string ARGS_HELP = "-h";
32 const std::string LOADED_COMP_LIST = "-l";
33 const std::string ENABLED_COMP_LIST = "-e";
34 const std::string TASK_LIST = "-t";
35 const std::string CAPABILITY_LIST = "-c";
36
37 const std::unordered_map<std::string, HidumpFlag> MAP_ARGS = {
38 { ARGS_HELP, HidumpFlag::GET_HELP },
39 { LOADED_COMP_LIST, HidumpFlag::GET_LOADED_COMP_LIST },
40 { ENABLED_COMP_LIST, HidumpFlag::GET_ENABLED_COMP_LIST },
41 { TASK_LIST, HidumpFlag::GET_TASK_LIST },
42 { CAPABILITY_LIST, HidumpFlag::GET_CAPABILITY_LIST },
43 };
44
45 std::unordered_map<TaskType, std::string> g_mapTaskType = {
46 { TaskType::UNKNOWN, "UNKNOWN" },
47 { TaskType::ENABLE, "ENABLE" },
48 { TaskType::DISABLE, "DISABLE" },
49 { TaskType::ON_LINE, "ON_LINE" },
50 { TaskType::OFF_LINE, "OFF_LINE" },
51 };
52
53 std::unordered_map<TaskStep, std::string> g_mapTaskStep = {
54 { TaskStep::DO_ENABLE, "DO_ENABLE" },
55 { TaskStep::DO_DISABLE, "DO_DISABLE" },
56 { TaskStep::SYNC_ONLINE_INFO, "SYNC_ONLINE_INFO" },
57 { TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE, "REGISTER_ONLINE_DISTRIBUTED_HARDWARE" },
58 { TaskStep::UNREGISTER_OFFLINE_DISTRIBUTED_HARDWARE, "UNREGISTER_OFFLINE_DISTRIBUTED_HARDWARE" },
59 { TaskStep::CLEAR_OFFLINE_INFO, "CLEAR_OFFLINE_INFO" },
60 { TaskStep::WAIT_UNREGISTGER_COMPLETE, "WAIT_UNREGISTGER_COMPLETE" },
61 };
62
63 std::unordered_map<TaskState, std::string> g_mapTaskState = {
64 { TaskState::INIT, "INIT" },
65 { TaskState::RUNNING, "RUNNING" },
66 { TaskState::SUCCESS, "SUCCESS" },
67 { TaskState::FAIL, "FAIL" },
68 };
69 }
70
Dump(const std::vector<std::string> & args,std::string & result)71 int32_t HidumpHelper::Dump(const std::vector<std::string>& args, std::string &result)
72 {
73 DHLOGI("HidumpHelper dump start.");
74 result.clear();
75 int32_t errCode = ERR_DH_FWK_HIDUMP_ERROR;
76
77 if (args.empty()) {
78 return ProcessDump(HidumpFlag::GET_HELP, result);
79 }
80
81 auto flag = MAP_ARGS.find(args[0]);
82 if ((args.size() > 1) || (flag == MAP_ARGS.end())) {
83 errCode = ProcessDump(HidumpFlag::UNKNOWN, result);
84 } else {
85 errCode = ProcessDump(flag->second, result);
86 }
87
88 return errCode;
89 }
90
ProcessDump(const HidumpFlag & flag,std::string & result)91 int32_t HidumpHelper::ProcessDump(const HidumpFlag &flag, std::string &result)
92 {
93 DHLOGI("Process Dump.");
94 int32_t errCode = ERR_DH_FWK_HIDUMP_ERROR;
95 switch (flag) {
96 case HidumpFlag::GET_HELP: {
97 errCode = ShowHelp(result);
98 break;
99 }
100 case HidumpFlag::GET_LOADED_COMP_LIST: {
101 errCode = ShowAllLoadedComps(result);
102 break;
103 }
104 case HidumpFlag::GET_ENABLED_COMP_LIST : {
105 errCode = ShowAllEnabledComps(result);
106 break;
107 }
108 case HidumpFlag::GET_TASK_LIST : {
109 errCode = ShowAllTaskInfos(result);
110 break;
111 }
112 case HidumpFlag::GET_CAPABILITY_LIST : {
113 errCode = ShowAllCapabilityInfos(result);
114 break;
115 }
116 default: {
117 errCode = ShowIllealInfomation(result);
118 break;
119 }
120 }
121
122 return errCode;
123 }
124
ShowAllLoadedComps(std::string & result)125 int32_t HidumpHelper::ShowAllLoadedComps(std::string &result)
126 {
127 DHLOGI("Dump all loaded compTypes.");
128 std::set<DHType> loadedCompSource {};
129 std::set<DHType> loadedCompSink {};
130 ComponentManager::GetInstance().DumpLoadedComps(loadedCompSource, loadedCompSink);
131 DHVersion dhVersion;
132 ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
133
134 result.append("Local loaded components:");
135 result.append("\nSource:");
136 if (!loadedCompSource.empty()) {
137 for (auto compSource : loadedCompSource) {
138 std::string dhTypeStr = "UNKNOWN";
139 auto it = DHTypeStrMap.find(compSource);
140 if (it != DHTypeStrMap.end()) {
141 dhTypeStr = it->second;
142 }
143 std::string sourceVersion = "";
144 auto iter = dhVersion.compVersions.find(compSource);
145 if (iter != dhVersion.compVersions.end()) {
146 sourceVersion = iter->second.sourceVersion;
147 }
148 result.append("\n{\n DHType : ").append(dhTypeStr);
149 result.append("\n Version : ").append(sourceVersion);
150 result.append("\n},");
151 }
152 result.replace(result.size() - 1, 1, "\n");
153 }
154
155 result.append("\nSink:");
156 if (!loadedCompSink.empty()) {
157 for (auto compSink : loadedCompSink) {
158 std::string dhTypeStr = "UNKNOWN";
159 auto it = DHTypeStrMap.find(compSink);
160 if (it != DHTypeStrMap.end()) {
161 dhTypeStr = it->second;
162 }
163 std::string sinkVersion = "";
164 auto iter = dhVersion.compVersions.find(compSink);
165 if (iter != dhVersion.compVersions.end()) {
166 sinkVersion = iter->second.sinkVersion;
167 }
168 result.append("\n{\n DHType : ").append(dhTypeStr);
169 result.append("\n Version : ").append(sinkVersion);
170 result.append("\n},");
171 }
172 result.replace(result.size() - 1, 1, "\n");
173 }
174 return DH_FWK_SUCCESS;
175 }
176
ShowAllEnabledComps(std::string & result)177 int32_t HidumpHelper::ShowAllEnabledComps(std::string &result)
178 {
179 DHLOGI("Dump all enabled comps.");
180 std::set<HidumpCompInfo> compInfoSet {};
181 EnabledCompsDump::GetInstance().Dump(compInfoSet);
182
183 result.append("All enabled components:");
184 if (compInfoSet.empty()) {
185 return DH_FWK_SUCCESS;
186 }
187
188 for (auto info : compInfoSet) {
189 std::string dhTypeStr = "UNKNOWN";
190 auto it = DHTypeStrMap.find(info.dhType_);
191 if (it != DHTypeStrMap.end()) {
192 dhTypeStr = it->second;
193 }
194 result.append("\n{");
195 result.append("\n NetworkId : ");
196 result.append(GetAnonyString(info.networkId_));
197 result.append("\n DHType : ");
198 result.append(dhTypeStr);
199 result.append("\n DHId : ");
200 result.append(GetAnonyString(info.dhId_));
201 result.append("\n},");
202 }
203 result.replace(result.size() - 1, 1, "\n");
204 return DH_FWK_SUCCESS;
205 }
206
ShowAllTaskInfos(std::string & result)207 int32_t HidumpHelper::ShowAllTaskInfos(std::string &result)
208 {
209 DHLOGI("Dump all task infos.");
210 std::vector<TaskDump> taskInfos {};
211 TaskBoard::GetInstance().DumpAllTasks(taskInfos);
212
213 result.append("All task infos:");
214 if (taskInfos.empty()) {
215 return DH_FWK_SUCCESS;
216 }
217
218 for (auto taskInfo : taskInfos) {
219 std::string dhTypeStr = "UNKNOWN";
220 auto it = DHTypeStrMap.find(taskInfo.taskParm.dhType);
221 if (it != DHTypeStrMap.end()) {
222 dhTypeStr = it->second;
223 }
224 result.append("\n{");
225 result.append("\n TaskId : ");
226 result.append(taskInfo.id);
227 result.append("\n TaskType : ");
228 result.append(g_mapTaskType[taskInfo.taskType]);
229 result.append("\n DHType : ");
230 result.append(dhTypeStr);
231 result.append("\n DHId : ");
232 result.append(GetAnonyString(taskInfo.taskParm.dhId));
233 result.append("\n TaskState : ");
234 result.append(g_mapTaskState[taskInfo.taskState]);
235 result.append("\n TaskStep : [ ");
236 std::vector<TaskStep> taskSteps = taskInfo.taskSteps;
237 for (auto step : taskSteps) {
238 result.append(g_mapTaskStep[step]);
239 result.append(" ");
240 }
241 result.append("]\n");
242 result.append("},");
243 }
244 result.replace(result.size() - 1, 1, "\n");
245 return DH_FWK_SUCCESS;
246 }
247
ShowAllCapabilityInfos(std::string & result)248 int32_t HidumpHelper::ShowAllCapabilityInfos(std::string &result)
249 {
250 DHLOGI("Dump all capability infos.");
251 std::vector<CapabilityInfo> capInfos;
252 CapabilityInfoManager::GetInstance()->DumpCapabilityInfos(capInfos);
253
254 result.append("All capability info of online components :");
255 if (capInfos.empty()) {
256 return DH_FWK_SUCCESS;
257 }
258
259 for (auto info : capInfos) {
260 std::string dhTypeStr = "UNKNOWN";
261 auto it = DHTypeStrMap.find(info.GetDHType());
262 if (it != DHTypeStrMap.end()) {
263 dhTypeStr = it->second;
264 }
265 result.append("\n{");
266 result.append("\n DeviceName : ");
267 result.append(GetAnonyString(info.GetDeviceName()));
268 result.append("\n DeviceId : ");
269 result.append(GetAnonyString(info.GetDeviceId()));
270 result.append("\n DeviceType : ");
271 result.append(std::to_string(info.GetDeviceType()));
272 result.append("\n DHType : ");
273 result.append(dhTypeStr);
274 result.append("\n DHId : ");
275 result.append(GetAnonyString(info.GetDHId()));
276 result.append("\n DHAttrs :\n");
277 result.append(info.GetDHAttrs());
278 result.append("\n},");
279 }
280 result.replace(result.size() - 1, 1, "\n");
281 return DH_FWK_SUCCESS;
282 }
283
ShowHelp(std::string & result)284 int32_t HidumpHelper::ShowHelp(std::string &result)
285 {
286 DHLOGI("Show dump help.");
287 result.append("DistributedHardwareFramework dump options:\n");
288 result.append(" -h ");
289 result.append(": Show help\n");
290 result.append(" -l ");
291 result.append(": Show all loaded components\n");
292 result.append(" -e ");
293 result.append(": Show all enabled components\n");
294 result.append(" -t ");
295 result.append(": Show all tasks\n");
296 result.append(" -c ");
297 result.append(": Show all Capability info of online components\n\n");
298
299 return DH_FWK_SUCCESS;
300 }
301
ShowIllealInfomation(std::string & result)302 int32_t HidumpHelper::ShowIllealInfomation(std::string &result)
303 {
304 DHLOGI("ShowIllealInfomation Dump.");
305 result.clear();
306 result.append("Unrecognized option, -h for help.");
307 return ERR_DH_FWK_HIDUMP_INVALID_ARGS;
308 }
309 } // namespace DistributedHardware
310 } // namespace OHOS
311