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