179a732c7Sopenharmony_ci/* 279a732c7Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License. 579a732c7Sopenharmony_ci * You may obtain a copy of the License at 679a732c7Sopenharmony_ci * 779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 879a732c7Sopenharmony_ci * 979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and 1379a732c7Sopenharmony_ci * limitations under the License. 1479a732c7Sopenharmony_ci */ 1579a732c7Sopenharmony_ci 1679a732c7Sopenharmony_ci#include "dm_hidumper.h" 1779a732c7Sopenharmony_ci 1879a732c7Sopenharmony_ci#include <unordered_map> // for __hash_map_const_iterator, unordered_map 1979a732c7Sopenharmony_ci#include <utility> // for pair 2079a732c7Sopenharmony_ci 2179a732c7Sopenharmony_ci#include "dm_anonymous.h" // for GetAnonyString 2279a732c7Sopenharmony_ci#include "dm_log.h" // for LOGI, LOGE 2379a732c7Sopenharmony_ci 2479a732c7Sopenharmony_cinamespace OHOS { 2579a732c7Sopenharmony_cinamespace DistributedHardware { 2679a732c7Sopenharmony_ciDM_IMPLEMENT_SINGLE_INSTANCE(HiDumpHelper); 2779a732c7Sopenharmony_ciconstexpr int32_t DM_OK = 0; 2879a732c7Sopenharmony_ciconstexpr int32_t ERR_DM_FAILED = 96929744; 2979a732c7Sopenharmony_cinamespace { 3079a732c7Sopenharmony_cistatic DumperInfo g_dumperDeviceType[] = { 3179a732c7Sopenharmony_ci {DEVICE_TYPE_UNKNOWN, "DEVICE_TYPE_UNKNOWN"}, 3279a732c7Sopenharmony_ci {DEVICE_TYPE_WIFI_CAMERA, "DEVICE_TYPE_WIFI_CAMERA"}, 3379a732c7Sopenharmony_ci {DEVICE_TYPE_AUDIO, "DEVICE_TYPE_AUDIO"}, 3479a732c7Sopenharmony_ci {DEVICE_TYPE_PC, "DEVICE_TYPE_PC"}, 3579a732c7Sopenharmony_ci {DEVICE_TYPE_PHONE, "DEVICE_TYPE_PHONE"}, 3679a732c7Sopenharmony_ci {DEVICE_TYPE_PAD, "DEVICE_TYPE_PAD"}, 3779a732c7Sopenharmony_ci {DEVICE_TYPE_WATCH, "DEVICE_TYPE_WATCH"}, 3879a732c7Sopenharmony_ci {DEVICE_TYPE_CAR, "DEVICE_TYPE_CAR"}, 3979a732c7Sopenharmony_ci {DEVICE_TYPE_TV, "DEVICE_TYPE_TV"}, 4079a732c7Sopenharmony_ci}; 4179a732c7Sopenharmony_ci} // namespace 4279a732c7Sopenharmony_ciint32_t HiDumpHelper::HiDump(const std::vector<std::string>& args, std::string &result) 4379a732c7Sopenharmony_ci{ 4479a732c7Sopenharmony_ci LOGI("HiDumpHelper start."); 4579a732c7Sopenharmony_ci result.clear(); 4679a732c7Sopenharmony_ci int32_t errCode = ERR_DM_FAILED; 4779a732c7Sopenharmony_ci 4879a732c7Sopenharmony_ci if (args.empty()) { 4979a732c7Sopenharmony_ci return ProcessDump(HidumperFlag::HIDUMPER_GET_HELP, result); 5079a732c7Sopenharmony_ci } 5179a732c7Sopenharmony_ci auto flag = MAP_ARGS.find(args[0]); 5279a732c7Sopenharmony_ci if ((args.size() > 1) || (flag == MAP_ARGS.end())) { 5379a732c7Sopenharmony_ci errCode = ProcessDump(HidumperFlag::HIDUMPER_UNKNOWN, result); 5479a732c7Sopenharmony_ci } else { 5579a732c7Sopenharmony_ci errCode = ProcessDump(flag->second, result); 5679a732c7Sopenharmony_ci } 5779a732c7Sopenharmony_ci return errCode; 5879a732c7Sopenharmony_ci} 5979a732c7Sopenharmony_ci 6079a732c7Sopenharmony_civoid HiDumpHelper::SetNodeInfo(const DmDeviceInfo& deviceInfo) 6179a732c7Sopenharmony_ci{ 6279a732c7Sopenharmony_ci LOGI("HiDumpHelper::SetNodeInfo"); 6379a732c7Sopenharmony_ci nodeInfos_.push_back(deviceInfo); 6479a732c7Sopenharmony_ci} 6579a732c7Sopenharmony_ci 6679a732c7Sopenharmony_ciint32_t HiDumpHelper::ProcessDump(const HidumperFlag &flag, std::string &result) 6779a732c7Sopenharmony_ci{ 6879a732c7Sopenharmony_ci LOGI("Process Dump."); 6979a732c7Sopenharmony_ci int32_t ret = ERR_DM_FAILED; 7079a732c7Sopenharmony_ci switch (flag) { 7179a732c7Sopenharmony_ci case HidumperFlag::HIDUMPER_GET_HELP: { 7279a732c7Sopenharmony_ci ret = ShowHelp(result); 7379a732c7Sopenharmony_ci break; 7479a732c7Sopenharmony_ci } 7579a732c7Sopenharmony_ci case HidumperFlag::HIDUMPER_GET_TRUSTED_LIST: { 7679a732c7Sopenharmony_ci ret = ShowAllLoadTrustedList(result); 7779a732c7Sopenharmony_ci break; 7879a732c7Sopenharmony_ci } 7979a732c7Sopenharmony_ci default: { 8079a732c7Sopenharmony_ci ret = ShowIllealInfomation(result); 8179a732c7Sopenharmony_ci break; 8279a732c7Sopenharmony_ci } 8379a732c7Sopenharmony_ci } 8479a732c7Sopenharmony_ci return ret; 8579a732c7Sopenharmony_ci} 8679a732c7Sopenharmony_ci 8779a732c7Sopenharmony_ciint32_t HiDumpHelper::ShowAllLoadTrustedList(std::string &result) 8879a732c7Sopenharmony_ci{ 8979a732c7Sopenharmony_ci LOGI("dump all trusted device List"); 9079a732c7Sopenharmony_ci int32_t ret = DM_OK; 9179a732c7Sopenharmony_ci 9279a732c7Sopenharmony_ci if (nodeInfos_.size() == 0) { 9379a732c7Sopenharmony_ci LOGE("dump trusted device list is empty"); 9479a732c7Sopenharmony_ci result.append("dump trusted device list is empty"); 9579a732c7Sopenharmony_ci } 9679a732c7Sopenharmony_ci for (unsigned int i = 0; i < nodeInfos_.size(); ++i) { 9779a732c7Sopenharmony_ci result.append("\n{\n deviceId : ").append(GetAnonyString(nodeInfos_[i].deviceId).c_str()); 9879a732c7Sopenharmony_ci result.append("\n{\n deviceName : ").append(GetAnonyString(nodeInfos_[i].deviceName).c_str()); 9979a732c7Sopenharmony_ci result.append("\n{\n networkId : ").append(GetAnonyString(nodeInfos_[i].networkId).c_str()); 10079a732c7Sopenharmony_ci std::string deviceType = GetDeviceType(nodeInfos_[i].deviceTypeId); 10179a732c7Sopenharmony_ci result.append("\n{\n deviceType : ").append(deviceType); 10279a732c7Sopenharmony_ci } 10379a732c7Sopenharmony_ci 10479a732c7Sopenharmony_ci nodeInfos_.clear(); 10579a732c7Sopenharmony_ci LOGI("HiDumpHelper ShowAllLoadTrustedList %{public}s", result.c_str()); 10679a732c7Sopenharmony_ci return ret; 10779a732c7Sopenharmony_ci} 10879a732c7Sopenharmony_ci 10979a732c7Sopenharmony_cistd::string HiDumpHelper::GetDeviceType(int32_t deviceTypeId) 11079a732c7Sopenharmony_ci{ 11179a732c7Sopenharmony_ci std::string dmDeviceTypeIdString = ""; 11279a732c7Sopenharmony_ci for (uint32_t i = 0; i < (sizeof(g_dumperDeviceType) / sizeof(g_dumperDeviceType[0])); i++) { 11379a732c7Sopenharmony_ci if (deviceTypeId == g_dumperDeviceType[i].deviceTypeId) { 11479a732c7Sopenharmony_ci dmDeviceTypeIdString = g_dumperDeviceType[i].deviceTypeInfo; 11579a732c7Sopenharmony_ci break; 11679a732c7Sopenharmony_ci } 11779a732c7Sopenharmony_ci } 11879a732c7Sopenharmony_ci return dmDeviceTypeIdString; 11979a732c7Sopenharmony_ci} 12079a732c7Sopenharmony_ci 12179a732c7Sopenharmony_ciint32_t HiDumpHelper::ShowHelp(std::string &result) 12279a732c7Sopenharmony_ci{ 12379a732c7Sopenharmony_ci LOGI("Show hidumper help"); 12479a732c7Sopenharmony_ci result.append("DistributedHardwareDeviceManager hidumper options:\n"); 12579a732c7Sopenharmony_ci result.append(" -help "); 12679a732c7Sopenharmony_ci result.append(": show help\n"); 12779a732c7Sopenharmony_ci result.append(" -getTrustlist "); 12879a732c7Sopenharmony_ci result.append(": show all trusted device list\n\n"); 12979a732c7Sopenharmony_ci return DM_OK; 13079a732c7Sopenharmony_ci} 13179a732c7Sopenharmony_ci 13279a732c7Sopenharmony_ciint32_t HiDumpHelper::ShowIllealInfomation(std::string &result) 13379a732c7Sopenharmony_ci{ 13479a732c7Sopenharmony_ci LOGI("ShowIllealInfomation Dump"); 13579a732c7Sopenharmony_ci result.clear(); 13679a732c7Sopenharmony_ci result.append("unrecognized option, -help for help."); 13779a732c7Sopenharmony_ci return DM_OK; 13879a732c7Sopenharmony_ci} 13979a732c7Sopenharmony_ci 14079a732c7Sopenharmony_ciint32_t HiDumpHelper::GetArgsType(const std::vector<std::string>& args, std::vector<HidumperFlag> &Flag) 14179a732c7Sopenharmony_ci{ 14279a732c7Sopenharmony_ci LOGI("HiDumpHelper::GetArgsType"); 14379a732c7Sopenharmony_ci int32_t ret = ERR_DM_FAILED; 14479a732c7Sopenharmony_ci if (args.empty()) { 14579a732c7Sopenharmony_ci Flag.push_back(HidumperFlag::HIDUMPER_GET_HELP); 14679a732c7Sopenharmony_ci return ret; 14779a732c7Sopenharmony_ci } 14879a732c7Sopenharmony_ci 14979a732c7Sopenharmony_ci auto flag = MAP_ARGS.find(args[0]); 15079a732c7Sopenharmony_ci if (flag != MAP_ARGS.end()) { 15179a732c7Sopenharmony_ci Flag.push_back(flag->second); 15279a732c7Sopenharmony_ci } 15379a732c7Sopenharmony_ci return ret; 15479a732c7Sopenharmony_ci} 15579a732c7Sopenharmony_ci} // namespace DistributedHardware 15679a732c7Sopenharmony_ci} // namespace OHOS 157