• 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 "dm_hidumper.h"
17 
18 #include <unordered_map>       // for __hash_map_const_iterator, unordered_map
19 #include <utility>             // for pair
20 
21 #include "dm_anonymous.h"      // for GetAnonyString
22 #include "dm_log.h"            // for LOGI, LOGE
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 IMPLEMENT_SINGLE_INSTANCE(HiDumpHelper);
27 constexpr int32_t DM_OK = 0;
28 constexpr int32_t ERR_DM_FAILED = -20000;
HiDump(const std::vector<std::string> & args,std::string & result)29 int32_t HiDumpHelper::HiDump(const std::vector<std::string>& args, std::string &result)
30 {
31     LOGI("HiDumpHelper start.");
32     result.clear();
33     int32_t errCode = ERR_DM_FAILED;
34 
35     if (args.empty()) {
36         return ProcessDump(HidumperFlag::HIDUMPER_GET_HELP, result);
37     }
38     auto flag = MAP_ARGS.find(args[0]);
39     if ((args.size() > 1) || (flag == MAP_ARGS.end())) {
40         errCode = ProcessDump(HidumperFlag::HIDUMPER_UNKNOWN, result);
41     } else {
42         errCode = ProcessDump(flag->second, result);
43     }
44     return errCode;
45 }
46 
SetNodeInfo(const DmDeviceInfo & deviceInfo)47 void HiDumpHelper::SetNodeInfo(const DmDeviceInfo& deviceInfo)
48 {
49     LOGI("HiDumpHelper::SetNodeInfo");
50     nodeInfos_.push_back(deviceInfo);
51 }
52 
ProcessDump(const HidumperFlag & flag,std::string & result)53 int32_t HiDumpHelper::ProcessDump(const HidumperFlag &flag, std::string &result)
54 {
55     LOGI("Process Dump.");
56     int32_t ret = ERR_DM_FAILED;
57     switch (flag) {
58         case HidumperFlag::HIDUMPER_GET_HELP: {
59             ret = ShowHelp(result);
60             break;
61         }
62         case HidumperFlag::HIDUMPER_GET_TRUSTED_LIST: {
63             ret = ShowAllLoadTrustedList(result);
64             break;
65         }
66         default: {
67             ret = ShowIllealInfomation(result);
68             break;
69         }
70     }
71     return ret;
72 }
73 
ShowAllLoadTrustedList(std::string & result)74 int32_t HiDumpHelper::ShowAllLoadTrustedList(std::string &result)
75 {
76     LOGI("dump all trusted device List");
77     int32_t ret = DM_OK;
78 
79     if (nodeInfos_.size() == 0) {
80         LOGE("dump trusted device list is empty");
81         result.append("dump trusted device list is empty");
82     }
83     for (unsigned int i = 0; i < nodeInfos_.size(); ++i) {
84         result.append("\n{\n    deviceId          : ").append(GetAnonyString(nodeInfos_[i].deviceId).c_str());
85         result.append("\n{\n    deviceName        : ").append(nodeInfos_[i].deviceName);
86         result.append("\n{\n    networkId         : ").append(GetAnonyString(nodeInfos_[i].networkId).c_str());
87         std::string deviceType = GetDeviceType(nodeInfos_[i].deviceTypeId);
88         result.append("\n{\n    deviceType        : ").append(deviceType);
89     }
90 
91     nodeInfos_.clear();
92     LOGI("HiDumpHelper ShowAllLoadTrustedList %s", result.c_str());
93     return ret;
94 }
95 
GetDeviceType(int32_t deviceTypeId)96 std::string HiDumpHelper::GetDeviceType(int32_t deviceTypeId)
97 {
98     std::string dmDeviceTypeIdString = "";
99     for (uint32_t i = 0; i < (sizeof(dumperDeviceType) / sizeof(dumperDeviceType[0])); i++) {
100         if (deviceTypeId == dumperDeviceType[i].deviceTypeId) {
101             dmDeviceTypeIdString = dumperDeviceType[i].deviceTypeInfo;
102             break;
103         }
104     }
105     return dmDeviceTypeIdString;
106 }
107 
ShowHelp(std::string & result)108 int32_t HiDumpHelper::ShowHelp(std::string &result)
109 {
110     LOGI("Show hidumper help");
111     result.append("DistributedHardwareDeviceManager hidumper options:\n");
112     result.append(" -help                    ");
113     result.append(": show help\n");
114     result.append(" -getTrustlist            ");
115     result.append(": show all trusted device list\n\n");
116     return DM_OK;
117 }
118 
ShowIllealInfomation(std::string & result)119 int32_t HiDumpHelper::ShowIllealInfomation(std::string &result)
120 {
121     LOGI("ShowIllealInfomation Dump");
122     result.clear();
123     result.append("unrecognized option, -help for help.");
124     return DM_OK;
125 }
126 
GetArgsType(const std::vector<std::string> & args,std::vector<HidumperFlag> & Flag)127 int32_t HiDumpHelper::GetArgsType(const std::vector<std::string>& args, std::vector<HidumperFlag> &Flag)
128 {
129     LOGI("HiDumpHelper::GetArgsType");
130     int32_t ret = ERR_DM_FAILED;
131     if (args.empty()) {
132         Flag.push_back(HidumperFlag::HIDUMPER_GET_HELP);
133         return ret;
134     }
135 
136     auto flag = MAP_ARGS.find(args[0]);
137     if (flag != MAP_ARGS.end()) {
138         Flag.push_back(flag->second);
139     }
140     return ret;
141 }
142 } // namespace DistributedHardware
143 } // namespace OHOS
144