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