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 #include "dm_constants.h"
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 std::lock_guard<std::mutex> autoLock(nodeInfosLock_);
74 CHECK_SIZE_VOID(nodeInfos_);
75 nodeInfos_.push_back(deviceInfo);
76 }
77
ProcessDump(const HidumperFlag & flag,std::string & result)78 int32_t HiDumpHelper::ProcessDump(const HidumperFlag &flag, std::string &result)
79 {
80 LOGI("Process Dump.");
81 int32_t ret = ERR_DM_FAILED;
82 switch (flag) {
83 case HidumperFlag::HIDUMPER_GET_HELP: {
84 ret = ShowHelp(result);
85 break;
86 }
87 case HidumperFlag::HIDUMPER_GET_TRUSTED_LIST: {
88 ret = ShowAllLoadTrustedList(result);
89 break;
90 }
91 default: {
92 ret = ShowIllealInfomation(result);
93 break;
94 }
95 }
96 return ret;
97 }
98
ShowAllLoadTrustedList(std::string & result)99 int32_t HiDumpHelper::ShowAllLoadTrustedList(std::string &result)
100 {
101 LOGI("dump all trusted device List");
102 int32_t ret = DM_OK;
103 std::lock_guard<std::mutex> autoLock(nodeInfosLock_);
104 if (nodeInfos_.size() == 0) {
105 LOGE("dump trusted device list is empty");
106 result.append("dump trusted device list is empty");
107 }
108 for (unsigned int i = 0; i < nodeInfos_.size(); ++i) {
109 result.append("\n{\n deviceId : ").append(GetAnonyString(nodeInfos_[i].deviceId).c_str());
110 result.append("\n{\n deviceName : ").append(GetAnonyString(nodeInfos_[i].deviceName).c_str());
111 result.append("\n{\n networkId : ").append(GetAnonyString(nodeInfos_[i].networkId).c_str());
112 std::string deviceType = GetDeviceType(nodeInfos_[i].deviceTypeId);
113 result.append("\n{\n deviceType : ").append(deviceType);
114 }
115
116 nodeInfos_.clear();
117 LOGI("HiDumpHelper ShowAllLoadTrustedList %{public}s", result.c_str());
118 return ret;
119 }
120
GetDeviceType(int32_t deviceTypeId)121 std::string HiDumpHelper::GetDeviceType(int32_t deviceTypeId)
122 {
123 std::string dmDeviceTypeIdString = "";
124 for (uint32_t i = 0; i < (sizeof(g_dumperDeviceType) / sizeof(g_dumperDeviceType[0])); i++) {
125 if (deviceTypeId == g_dumperDeviceType[i].deviceTypeId) {
126 dmDeviceTypeIdString = g_dumperDeviceType[i].deviceTypeInfo;
127 break;
128 }
129 }
130 return dmDeviceTypeIdString;
131 }
132
ShowHelp(std::string & result)133 int32_t HiDumpHelper::ShowHelp(std::string &result)
134 {
135 LOGI("Show hidumper help");
136 result.append("DistributedHardwareDeviceManager hidumper options:\n");
137 result.append(" -help ");
138 result.append(": show help\n");
139 result.append(" -getTrustlist ");
140 result.append(": show all trusted device list\n\n");
141 return DM_OK;
142 }
143
ShowIllealInfomation(std::string & result)144 int32_t HiDumpHelper::ShowIllealInfomation(std::string &result)
145 {
146 LOGI("ShowIllealInfomation Dump");
147 result.clear();
148 result.append("unrecognized option, -help for help.");
149 return DM_OK;
150 }
151
GetArgsType(const std::vector<std::string> & args,std::vector<HidumperFlag> & Flag)152 int32_t HiDumpHelper::GetArgsType(const std::vector<std::string>& args, std::vector<HidumperFlag> &Flag)
153 {
154 LOGI("HiDumpHelper::GetArgsType");
155 int32_t ret = ERR_DM_FAILED;
156 if (args.empty()) {
157 Flag.push_back(HidumperFlag::HIDUMPER_GET_HELP);
158 return ret;
159 }
160
161 auto flag = MAP_ARGS.find(args[0]);
162 if (flag != MAP_ARGS.end()) {
163 Flag.push_back(flag->second);
164 }
165 return ret;
166 }
167 } // namespace DistributedHardware
168 } // namespace OHOS
169