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