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