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