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 "daudio_hidumper.h"
17
18 #include "daudio_errorcode.h"
19 #include "daudio_log.h"
20 #include "daudio_util.h"
21
22 #undef DH_LOG_TAG
23 #define DH_LOG_TAG "DaudioHidumper"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 IMPLEMENT_SINGLE_INSTANCE(DaudioHidumper);
28
29 namespace {
30 const std::string ARGS_HELP = "-h";
31 const std::string ARGS_SOURCE_DEVID = "--sourceDevId";
32 const std::string ARGS_SINK_INFO = "--sinkInfo";
33 const std::string ARGS_ABILITY = "--ability";
34
35 const std::map<std::string, HidumpFlag> ARGS_MAP = {
36 { ARGS_HELP, HidumpFlag::GET_HELP },
37 { ARGS_SOURCE_DEVID, HidumpFlag::GET_SOURCE_DEVID },
38 { ARGS_SINK_INFO, HidumpFlag::GET_SINK_INFO },
39 { ARGS_ABILITY, HidumpFlag::GET_ABILITY },
40 };
41 }
42
DaudioHidumper()43 DaudioHidumper::DaudioHidumper()
44 {
45 DHLOGI("Distributed audio hidumper constructed.");
46 }
47
~DaudioHidumper()48 DaudioHidumper::~DaudioHidumper()
49 {
50 DHLOGI("Distributed audio hidumper deconstructed.");
51 }
52
Dump(const std::vector<std::string> & args,std::string & result)53 bool DaudioHidumper::Dump(const std::vector<std::string> &args, std::string &result)
54 {
55 DHLOGI("Distributed audio hidumper dump args.size():%d.", args.size());
56 result.clear();
57 int32_t argsSize = static_cast<int32_t>(args.size());
58 for (int32_t i = 0; i < argsSize; i++) {
59 DHLOGI("Distributed audio hidumper dump args[%d]: %s.", i, args.at(i).c_str());
60 }
61
62 if (args.empty()) {
63 ShowHelp(result);
64 return true;
65 } else if (args.size() > 1) {
66 ShowIllegalInfomation(result);
67 return true;
68 }
69
70 return ProcessDump(args[0], result) == DH_SUCCESS;
71 }
72
ProcessDump(const std::string & args,std::string & result)73 int32_t DaudioHidumper::ProcessDump(const std::string &args, std::string &result)
74 {
75 DHLOGI("Process dump.");
76 HidumpFlag hf = HidumpFlag::UNKNOWN;
77 auto operatorIter = ARGS_MAP.find(args);
78 if (operatorIter != ARGS_MAP.end()) {
79 hf = operatorIter->second;
80 }
81
82 if (hf == HidumpFlag::GET_HELP) {
83 ShowHelp(result);
84 return DH_SUCCESS;
85 }
86 result.clear();
87 switch (hf) {
88 case HidumpFlag::GET_SOURCE_DEVID: {
89 return GetSourceDevId(result);
90 }
91 case HidumpFlag::GET_SINK_INFO: {
92 return GetSinkInfo(result);
93 }
94 case HidumpFlag::GET_ABILITY: {
95 return GetAbilityInfo(result);
96 }
97 default: {
98 return ShowIllegalInfomation(result);
99 }
100 }
101 }
102
GetSourceDevId(std::string & result)103 int32_t DaudioHidumper::GetSourceDevId(std::string &result)
104 {
105 DHLOGI("Get source devId dump.");
106 int32_t ret = GetLocalDeviceNetworkId(g_sourceDevId_);
107 if (ret != DH_SUCCESS) {
108 DHLOGE("Get local network id failed.");
109 result.append("sourceDevId: ").append("");
110 return ret;
111 }
112 result.append("sourceDevId: ").append(GetAnonyString(g_sourceDevId_));
113 return DH_SUCCESS;
114 }
115
GetSinkInfo(std::string & result)116 int32_t DaudioHidumper::GetSinkInfo(std::string &result)
117 {
118 DHLOGI("Get sink info dump.");
119 g_manager = GetAudioManagerFuncs();
120 if (g_manager == nullptr) {
121 return ERR_DH_AUDIO_NULLPTR;
122 }
123 int32_t ret = g_manager->GetAllAdapters(g_manager, &g_devices, &g_deviceNum);
124 if (ret != DH_SUCCESS) {
125 DHLOGE("Get all adapters failed.");
126 return ERR_DH_AUDIO_NULLPTR;
127 }
128 for (int32_t index = 0; index < g_deviceNum; index++) {
129 AudioAdapterDescriptor &desc = g_devices[index];
130 result.append("sinkDevId: ").append(GetAnonyString(desc.adapterName)).append(" portId: ");
131 for (uint32_t i = 0; i < desc.portNum; i++) {
132 result.append(std::to_string(desc.ports[i].portId)).append(" ");
133 }
134 }
135
136 return DH_SUCCESS;
137 }
138
GetAbilityInfo(std::string & result)139 int32_t DaudioHidumper::GetAbilityInfo(std::string &result)
140 {
141 DHLOGI("Obtaining capability information.");
142 std::vector<DHItem> abilityInfo = DAudioHandler::GetInstance().ablityForDump();
143 for (DHItem dhItem : abilityInfo) {
144 if (dhItem.dhId == spkDefault) {
145 result.append("spkAbilityInfo:").append(dhItem.attrs).append(" ");
146 }
147 if (dhItem.dhId == micDefault) {
148 result.append("micAbilityInfo:").append(dhItem.attrs).append(" ");
149 }
150 }
151 return DH_SUCCESS;
152 }
153
ShowHelp(std::string & result)154 void DaudioHidumper::ShowHelp(std::string &result)
155 {
156 DHLOGI("Show help.");
157 result.append("Usage:dump <command> [options]\n")
158 .append("Description:\n")
159 .append("-h ")
160 .append(": show help\n")
161 .append("--sourceDevId ")
162 .append(": dump audio sourceDevId in the system\n")
163 .append("--sinkInfo ")
164 .append(": dump sink info in the system\n")
165 .append("--ability ")
166 .append(": dump current ability of the audio in the system\n");
167 }
168
ShowIllegalInfomation(std::string & result)169 int32_t DaudioHidumper::ShowIllegalInfomation(std::string &result)
170 {
171 DHLOGI("Show illegal information.");
172 result.append("unknown command, -h for help.");
173 return DH_SUCCESS;
174 }
175 } // namespace DistributedHardware
176 } // namespace OHOS