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     { 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 
ShowAllLoadedComps(std::string & result)132 int32_t HidumpHelper::ShowAllLoadedComps(std::string &result)
133 {
134     DHLOGI("Dump all loaded compTypes.");
135     std::set<DHType> loadedCompSource {};
136     std::set<DHType> loadedCompSink {};
137     ComponentManager::GetInstance().DumpLoadedComps(loadedCompSource, loadedCompSink);
138     DHVersion dhVersion;
139     ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
140 
141     result.append("Local loaded components:");
142     result.append("\nSource:");
143     if (!loadedCompSource.empty()) {
144         for (auto compSource : loadedCompSource) {
145             std::string dhTypeStr = "UNKNOWN";
146             auto it = DHTypeStrMap.find(compSource);
147             if (it != DHTypeStrMap.end()) {
148                 dhTypeStr = it->second;
149             }
150             std::string sourceVersion = "";
151             auto iter = dhVersion.compVersions.find(compSource);
152             if (iter != dhVersion.compVersions.end()) {
153                 sourceVersion = iter->second.sourceVersion;
154             }
155             result.append("\n{\n    DHType         : ").append(dhTypeStr);
156             result.append("\n    Version        : ").append(sourceVersion).append("\n},");
157         }
158         if (result.size() >= 1) {
159             result.replace(result.size() - 1, 1, "\n");
160         }
161     }
162 
163     result.append("\nSink:");
164     if (!loadedCompSink.empty()) {
165         for (auto compSink : loadedCompSink) {
166             std::string dhTypeStr = "UNKNOWN";
167             auto it = DHTypeStrMap.find(compSink);
168             if (it != DHTypeStrMap.end()) {
169                 dhTypeStr = it->second;
170             }
171             std::string sinkVersion = "";
172             auto iter = dhVersion.compVersions.find(compSink);
173             if (iter != dhVersion.compVersions.end()) {
174                 sinkVersion = iter->second.sinkVersion;
175             }
176             result.append("\n{\n    DHType         : ").append(dhTypeStr);
177             result.append("\n    Version        : ").append(sinkVersion).append("\n},");
178         }
179         if (result.size() >= 1) {
180             result.replace(result.size() - 1, 1, "\n");
181         }
182     }
183     return DH_FWK_SUCCESS;
184 }
185 
ShowAllEnabledComps(std::string & result)186 int32_t HidumpHelper::ShowAllEnabledComps(std::string &result)
187 {
188     DHLOGI("Dump all enabled comps.");
189     std::set<HidumpCompInfo> compInfoSet {};
190     EnabledCompsDump::GetInstance().Dump(compInfoSet);
191 
192     result.append("All enabled components:");
193     if (compInfoSet.empty()) {
194         return DH_FWK_SUCCESS;
195     }
196 
197     for (auto info : compInfoSet) {
198         std::string dhTypeStr = "UNKNOWN";
199         auto it = DHTypeStrMap.find(info.dhType_);
200         if (it != DHTypeStrMap.end()) {
201             dhTypeStr = it->second;
202         }
203         result.append("\n{");
204         result.append("\n    NetworkId      : ");
205         result.append(GetAnonyString(info.networkId_));
206         result.append("\n    DHType         : ");
207         result.append(dhTypeStr);
208         result.append("\n    DHId           : ");
209         result.append(GetAnonyString(info.dhId_));
210         result.append("\n},");
211     }
212     if (result.size() < 1) {
213         return DH_FWK_SUCCESS;
214     }
215     result.replace(result.size() - 1, 1, "\n");
216     return DH_FWK_SUCCESS;
217 }
218 
ShowAllTaskInfos(std::string & result)219 int32_t HidumpHelper::ShowAllTaskInfos(std::string &result)
220 {
221     DHLOGI("Dump all task infos.");
222     std::vector<TaskDump> taskInfos {};
223     TaskBoard::GetInstance().DumpAllTasks(taskInfos);
224 
225     result.append("All task infos:");
226     if (taskInfos.empty()) {
227         return DH_FWK_SUCCESS;
228     }
229 
230     for (auto taskInfo : taskInfos) {
231         std::string dhTypeStr = "UNKNOWN";
232         auto it = DHTypeStrMap.find(taskInfo.taskParm.dhType);
233         if (it != DHTypeStrMap.end()) {
234             dhTypeStr = it->second;
235         }
236         result.append("\n{");
237         result.append("\n    TaskId     : ");
238         result.append(taskInfo.id);
239         result.append("\n    TaskType   : ");
240         result.append(g_mapTaskType[taskInfo.taskType]);
241         result.append("\n    DHType     : ");
242         result.append(dhTypeStr);
243         result.append("\n    DHId       : ");
244         result.append(GetAnonyString(taskInfo.taskParm.dhId));
245         result.append("\n    TaskState  : ");
246         result.append(g_mapTaskState[taskInfo.taskState]);
247         result.append("\n    TaskStep   : [ ");
248         std::vector<TaskStep> taskSteps = taskInfo.taskSteps;
249         for (auto step : taskSteps) {
250             result.append(g_mapTaskStep[step]);
251             result.append(" ");
252         }
253         result.append("]\n");
254         result.append("},");
255     }
256     if (result.size() < 1) {
257         return DH_FWK_SUCCESS;
258     }
259     result.replace(result.size() - 1, 1, "\n");
260     return DH_FWK_SUCCESS;
261 }
262 
ShowAllCapabilityInfos(std::string & result)263 int32_t HidumpHelper::ShowAllCapabilityInfos(std::string &result)
264 {
265     DHLOGI("Dump all capability infos.");
266     std::vector<CapabilityInfo> capInfos;
267     CapabilityInfoManager::GetInstance()->DumpCapabilityInfos(capInfos);
268 
269     result.append("All capability info of online components :");
270     if (capInfos.empty()) {
271         return DH_FWK_SUCCESS;
272     }
273 
274     for (auto info : capInfos) {
275         std::string dhTypeStr = "UNKNOWN";
276         auto it = DHTypeStrMap.find(info.GetDHType());
277         if (it != DHTypeStrMap.end()) {
278             dhTypeStr = it->second;
279         }
280         result.append("\n{");
281         result.append("\n    DeviceName     : ");
282         result.append(GetAnonyString(info.GetDeviceName()));
283         result.append("\n    DeviceId       : ");
284         result.append(GetAnonyString(info.GetDeviceId()));
285         result.append("\n    DeviceType     : ");
286         result.append(std::to_string(info.GetDeviceType()));
287         result.append("\n    DHType         : ");
288         result.append(dhTypeStr);
289         result.append("\n    DHId           : ");
290         result.append(GetAnonyString(info.GetDHId()));
291         result.append("\n    DHAttrs        :\n");
292         result.append(info.GetDHAttrs());
293         result.append("\n},");
294     }
295     if (result.size() < 1) {
296         return DH_FWK_SUCCESS;
297     }
298     result.replace(result.size() - 1, 1, "\n");
299     return DH_FWK_SUCCESS;
300 }
301 
ShowHelp(std::string & result)302 int32_t HidumpHelper::ShowHelp(std::string &result)
303 {
304     DHLOGI("Show dump help.");
305     result.append("DistributedHardwareFramework dump options:\n");
306     result.append(" -h    ");
307     result.append(": Show help\n");
308     result.append(" -l    ");
309     result.append(": Show all loaded components\n");
310     result.append(" -e    ");
311     result.append(": Show all enabled components\n");
312     result.append(" -t    ");
313     result.append(": Show all tasks\n");
314     result.append(" -c    ");
315     result.append(": Show all Capability info of online components\n\n");
316 
317     return DH_FWK_SUCCESS;
318 }
319 
ShowIllealInfomation(std::string & result)320 int32_t HidumpHelper::ShowIllealInfomation(std::string &result)
321 {
322     DHLOGI("ShowIllealInfomation  Dump.");
323     result.clear();
324     result.append("Unrecognized option, -h for help.");
325     return ERR_DH_FWK_HIDUMP_INVALID_ARGS;
326 }
327 } // namespace DistributedHardware
328 } // namespace OHOS
329