• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "anonymous_string.h"
21 #include "capability_info_manager.h"
22 #include "component_loader.h"
23 #include "component_manager.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "task_board.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 IMPLEMENT_SINGLE_INSTANCE(HidumpHelper);
31 namespace {
32 const std::string ARGS_HELP = "-h";
33 const std::string LOADED_COMP_LIST = "-l";
34 const std::string ENABLED_COMP_LIST = "-e";
35 const std::string TASK_LIST = "-t";
36 const std::string CAPABILITY_LIST = "-c";
37 
38 const std::unordered_map<std::string, HidumpFlag> MAP_ARGS = {
39     { ARGS_HELP, HidumpFlag::GET_HELP },
40     { LOADED_COMP_LIST, HidumpFlag::GET_LOADED_COMP_LIST },
41     { ENABLED_COMP_LIST, HidumpFlag::GET_ENABLED_COMP_LIST },
42     { TASK_LIST, HidumpFlag::GET_TASK_LIST },
43     { CAPABILITY_LIST, HidumpFlag::GET_CAPABILITY_LIST },
44 };
45 
46 std::unordered_map<TaskType, std::string> g_mapTaskType = {
47     { TaskType::UNKNOWN, "UNKNOWN" },
48     { TaskType::ENABLE, "ENABLE" },
49     { TaskType::DISABLE, "DISABLE" },
50     { TaskType::ON_LINE, "ON_LINE" },
51     { TaskType::OFF_LINE, "OFF_LINE" },
52     { TaskType::META_ENABLE, "META_ENABLE" },
53     { TaskType::META_DISABLE, "META_DISABLE" },
54 };
55 
56 std::unordered_map<TaskStep, std::string> g_mapTaskStep = {
57     { TaskStep::DO_ENABLE, "DO_ENABLE" },
58     { TaskStep::DO_DISABLE, "DO_DISABLE" },
59     { TaskStep::SYNC_ONLINE_INFO, "SYNC_ONLINE_INFO" },
60     { TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE, "REGISTER_ONLINE_DISTRIBUTED_HARDWARE" },
61     { TaskStep::UNREGISTER_OFFLINE_DISTRIBUTED_HARDWARE, "UNREGISTER_OFFLINE_DISTRIBUTED_HARDWARE" },
62     { TaskStep::CLEAR_OFFLINE_INFO, "CLEAR_OFFLINE_INFO" },
63     { TaskStep::WAIT_UNREGISTGER_COMPLETE, "WAIT_UNREGISTGER_COMPLETE" },
64     { TaskStep::META_ENABLE_TASK, "META_ENABLE_TASK" },
65     { TaskStep::META_DISABLE_TASK, "META_DISABLE_TASK" },
66     { TaskStep::DO_MODEM_META_ENABLE, "DO_MODEM_META_ENABLE" },
67     { TaskStep::DO_MODEM_META_DISABLE, "DO_MODEM_META_DISABLE" },
68 };
69 
70 std::unordered_map<TaskState, std::string> g_mapTaskState = {
71     { TaskState::INIT, "INIT" },
72     { TaskState::RUNNING, "RUNNING" },
73     { TaskState::SUCCESS, "SUCCESS" },
74     { TaskState::FAIL, "FAIL" },
75 };
76 }
77 
Dump(const std::vector<std::string> & args,std::string & result)78 int32_t HidumpHelper::Dump(const std::vector<std::string>& args, std::string &result)
79 {
80     DHLOGI("HidumpHelper dump start.");
81     result.clear();
82     int32_t errCode = ERR_DH_FWK_HIDUMP_ERROR;
83 
84     if (args.empty()) {
85         return ProcessDump(HidumpFlag::GET_HELP, result);
86     }
87 
88     auto flag = MAP_ARGS.find(args[0]);
89     if ((args.size() > 1) || (flag == MAP_ARGS.end())) {
90         errCode = ProcessDump(HidumpFlag::UNKNOWN, result);
91     } else {
92         errCode = ProcessDump(flag->second, result);
93     }
94 
95     return errCode;
96 }
97 
ProcessDump(const HidumpFlag & flag,std::string & result)98 int32_t HidumpHelper::ProcessDump(const HidumpFlag &flag, std::string &result)
99 {
100     DHLOGI("Process Dump.");
101     int32_t errCode = ERR_DH_FWK_HIDUMP_ERROR;
102     switch (flag) {
103         case HidumpFlag::GET_HELP: {
104             errCode = ShowHelp(result);
105             break;
106         }
107         case HidumpFlag::GET_LOADED_COMP_LIST: {
108             errCode = ShowAllLoadedComps(result);
109             break;
110         }
111         case HidumpFlag::GET_ENABLED_COMP_LIST : {
112             errCode = ShowAllEnabledComps(result);
113             break;
114         }
115         case HidumpFlag::GET_TASK_LIST : {
116             errCode = ShowAllTaskInfos(result);
117             break;
118         }
119         case HidumpFlag::GET_CAPABILITY_LIST : {
120             errCode = ShowAllCapabilityInfos(result);
121             break;
122         }
123         default: {
124             errCode = ShowIllealInfomation(result);
125             break;
126         }
127     }
128 
129     return errCode;
130 }
131 
ShowLoadCompSource(const std::set<DHType> & loadedCompSource,const DHVersion & dhVersion,std::string & result)132 void HidumpHelper::ShowLoadCompSource(const std::set<DHType> &loadedCompSource, const DHVersion &dhVersion,
133     std::string &result)
134 {
135     if (!loadedCompSource.empty()) {
136         for (auto compSource : loadedCompSource) {
137             std::string dhTypeStr = "UNKNOWN";
138             auto it = DHTypeStrMap.find(compSource);
139             if (it != DHTypeStrMap.end()) {
140                 dhTypeStr = it->second;
141             }
142             std::string sourceVersion = "";
143             auto iter = dhVersion.compVersions.find(compSource);
144             if (iter != dhVersion.compVersions.end()) {
145                 sourceVersion = iter->second.sourceVersion;
146             }
147             result.append("\n{\n    DHType         : ").append(dhTypeStr);
148             result.append("\n    Version        : ").append(sourceVersion).append("\n},");
149         }
150         if (result.size() >= 1) {
151             result.replace(result.size() - 1, 1, "\n");
152         }
153     }
154 }
155 
ShowLoadCompSink(const std::set<DHType> & loadedCompSink,const DHVersion & dhVersion,std::string & result)156 void HidumpHelper::ShowLoadCompSink(const std::set<DHType> &loadedCompSink, const DHVersion &dhVersion,
157     std::string &result)
158 {
159     if (!loadedCompSink.empty()) {
160         for (auto compSink : loadedCompSink) {
161             std::string dhTypeStr = "UNKNOWN";
162             auto it = DHTypeStrMap.find(compSink);
163             if (it != DHTypeStrMap.end()) {
164                 dhTypeStr = it->second;
165             }
166             std::string sinkVersion = "";
167             auto iter = dhVersion.compVersions.find(compSink);
168             if (iter != dhVersion.compVersions.end()) {
169                 sinkVersion = iter->second.sinkVersion;
170             }
171             result.append("\n{\n    DHType         : ").append(dhTypeStr);
172             result.append("\n    Version        : ").append(sinkVersion).append("\n},");
173         }
174         if (result.size() >= 1) {
175             result.replace(result.size() - 1, 1, "\n");
176         }
177     }
178 }
179 
ShowAllLoadedComps(std::string & result)180 int32_t HidumpHelper::ShowAllLoadedComps(std::string &result)
181 {
182     DHLOGI("Dump all loaded compTypes.");
183     std::set<DHType> loadedCompSource {};
184     std::set<DHType> loadedCompSink {};
185     ComponentManager::GetInstance().DumpLoadedCompsource(loadedCompSource);
186     ComponentManager::GetInstance().DumpLoadedCompsink(loadedCompSink);
187     DHVersion dhVersion;
188     ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
189 
190     result.append("Local loaded components:");
191     result.append("\nSource:");
192     ShowLoadCompSource(loadedCompSource, dhVersion, result);
193     result.append("\nSink:");
194     ShowLoadCompSink(loadedCompSink, dhVersion, result);
195     return DH_FWK_SUCCESS;
196 }
197 
ShowAllEnabledComps(std::string & result)198 int32_t HidumpHelper::ShowAllEnabledComps(std::string &result)
199 {
200     DHLOGI("Dump all enabled comps.");
201     std::set<HidumpCompInfo> compInfoSet {};
202     EnabledCompsDump::GetInstance().Dump(compInfoSet);
203 
204     result.append("All enabled components:");
205     if (compInfoSet.empty()) {
206         return DH_FWK_SUCCESS;
207     }
208 
209     for (auto info : compInfoSet) {
210         std::string dhTypeStr = "UNKNOWN";
211         auto it = DHTypeStrMap.find(info.dhType_);
212         if (it != DHTypeStrMap.end()) {
213             dhTypeStr = it->second;
214         }
215         result.append("\n{");
216         result.append("\n    NetworkId      : ");
217         result.append(GetAnonyString(info.networkId_));
218         result.append("\n    DHType         : ");
219         result.append(dhTypeStr);
220         result.append("\n    DHId           : ");
221         result.append(GetAnonyString(info.dhId_));
222         result.append("\n},");
223     }
224     if (result.size() < 1) {
225         return DH_FWK_SUCCESS;
226     }
227     result.replace(result.size() - 1, 1, "\n");
228     return DH_FWK_SUCCESS;
229 }
230 
ShowAllTaskInfos(std::string & result)231 int32_t HidumpHelper::ShowAllTaskInfos(std::string &result)
232 {
233     DHLOGI("Dump all task infos.");
234     std::vector<TaskDump> taskInfos {};
235     TaskBoard::GetInstance().DumpAllTasks(taskInfos);
236 
237     result.append("All task infos:");
238     if (taskInfos.empty()) {
239         return DH_FWK_SUCCESS;
240     }
241 
242     for (auto taskInfo : taskInfos) {
243         std::string dhTypeStr = "UNKNOWN";
244         auto it = DHTypeStrMap.find(taskInfo.taskParm.dhType);
245         if (it != DHTypeStrMap.end()) {
246             dhTypeStr = it->second;
247         }
248         result.append("\n{");
249         result.append("\n    TaskId     : ");
250         result.append(taskInfo.id);
251         result.append("\n    TaskType   : ");
252         result.append(g_mapTaskType[taskInfo.taskType]);
253         result.append("\n    DHType     : ");
254         result.append(dhTypeStr);
255         result.append("\n    DHId       : ");
256         result.append(GetAnonyString(taskInfo.taskParm.dhId));
257         result.append("\n    TaskState  : ");
258         result.append(g_mapTaskState[taskInfo.taskState]);
259         result.append("\n    TaskStep   : [ ");
260         std::vector<TaskStep> taskSteps = taskInfo.taskSteps;
261         for (auto step : taskSteps) {
262             result.append(g_mapTaskStep[step]);
263             result.append(" ");
264         }
265         result.append("]\n");
266         result.append("},");
267     }
268     if (result.size() < 1) {
269         return DH_FWK_SUCCESS;
270     }
271     result.replace(result.size() - 1, 1, "\n");
272     return DH_FWK_SUCCESS;
273 }
274 
ShowAllCapabilityInfos(std::string & result)275 int32_t HidumpHelper::ShowAllCapabilityInfos(std::string &result)
276 {
277     DHLOGI("Dump all capability infos.");
278     std::vector<CapabilityInfo> capInfos;
279     CapabilityInfoManager::GetInstance()->DumpCapabilityInfos(capInfos);
280 
281     result.append("All capability info of online components :");
282     if (capInfos.empty()) {
283         return DH_FWK_SUCCESS;
284     }
285 
286     for (auto info : capInfos) {
287         std::string dhTypeStr = "UNKNOWN";
288         auto it = DHTypeStrMap.find(info.GetDHType());
289         if (it != DHTypeStrMap.end()) {
290             dhTypeStr = it->second;
291         }
292         result.append("\n{");
293         result.append("\n    DeviceName     : ");
294         result.append(GetAnonyString(info.GetDeviceName()));
295         result.append("\n    DeviceId       : ");
296         result.append(GetAnonyString(info.GetDeviceId()));
297         result.append("\n    DeviceType     : ");
298         result.append(std::to_string(info.GetDeviceType()));
299         result.append("\n    DHType         : ");
300         result.append(dhTypeStr);
301         result.append("\n    DHId           : ");
302         result.append(GetAnonyString(info.GetDHId()));
303         result.append("\n    DHAttrs        :\n");
304         result.append(info.GetDHAttrs());
305         result.append("\n},");
306     }
307     if (result.size() < 1) {
308         return DH_FWK_SUCCESS;
309     }
310     result.replace(result.size() - 1, 1, "\n");
311     return DH_FWK_SUCCESS;
312 }
313 
ShowHelp(std::string & result)314 int32_t HidumpHelper::ShowHelp(std::string &result)
315 {
316     DHLOGI("Show dump help.");
317     result.append("DistributedHardwareFramework dump options:\n");
318     result.append(" -h    ");
319     result.append(": Show help\n");
320     result.append(" -l    ");
321     result.append(": Show all loaded components\n");
322     result.append(" -e    ");
323     result.append(": Show all enabled components\n");
324     result.append(" -t    ");
325     result.append(": Show all tasks\n");
326     result.append(" -c    ");
327     result.append(": Show all Capability info of online components\n\n");
328 
329     return DH_FWK_SUCCESS;
330 }
331 
ShowIllealInfomation(std::string & result)332 int32_t HidumpHelper::ShowIllealInfomation(std::string &result)
333 {
334     DHLOGI("ShowIllealInfomation  Dump.");
335     result.clear();
336     result.append("Unrecognized option, -h for help.");
337     return ERR_DH_FWK_HIDUMP_INVALID_ARGS;
338 }
339 } // namespace DistributedHardware
340 } // namespace OHOS
341