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_constants.h"
19 #include "daudio_errorcode.h"
20 #include "daudio_log.h"
21 #include "daudio_util.h"
22
23 #undef DH_LOG_TAG
24 #define DH_LOG_TAG "DaudioHidumper"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 IMPLEMENT_SINGLE_INSTANCE(DaudioHidumper);
29
30 namespace {
31 const std::string ARGS_HELP = "-h";
32 const std::string ARGS_SOURCE_DEVID = "--sourceDevId";
33 const std::string ARGS_SINK_INFO = "--sinkInfo";
34 const std::string ARGS_ABILITY = "--ability";
35 const std::string ARGS_DUMP_AUDIO_DATA_START = "--startDump";
36 const std::string ARGS_DUMP_AUDIO_DATA_STOP = "--stopDump";
37
38 const std::map<std::string, HidumpFlag> ARGS_MAP = {
39 { ARGS_HELP, HidumpFlag::GET_HELP },
40 { ARGS_SOURCE_DEVID, HidumpFlag::GET_SOURCE_DEVID },
41 { ARGS_SINK_INFO, HidumpFlag::GET_SINK_INFO },
42 { ARGS_ABILITY, HidumpFlag::GET_ABILITY },
43 { ARGS_DUMP_AUDIO_DATA_START, HidumpFlag::DUMP_AUDIO_DATA_START },
44 { ARGS_DUMP_AUDIO_DATA_STOP, HidumpFlag::DUMP_AUDIO_DATA_STOP },
45 };
46 }
47
DaudioHidumper()48 DaudioHidumper::DaudioHidumper()
49 {
50 DHLOGI("Distributed audio hidumper constructed.");
51 }
52
~DaudioHidumper()53 DaudioHidumper::~DaudioHidumper()
54 {
55 DHLOGI("Distributed audio hidumper deconstructed.");
56 }
57
Dump(const std::vector<std::string> & args,std::string & result)58 bool DaudioHidumper::Dump(const std::vector<std::string> &args, std::string &result)
59 {
60 DHLOGI("Distributed audio hidumper dump args.size():%d.", args.size());
61 result.clear();
62 int32_t argsSize = static_cast<int32_t>(args.size());
63 for (int32_t i = 0; i < argsSize; i++) {
64 DHLOGI("Distributed audio hidumper dump args[%d]: %s.", i, args.at(i).c_str());
65 }
66
67 if (args.empty()) {
68 ShowHelp(result);
69 return true;
70 } else if (args.size() > 1) {
71 ShowIllegalInfomation(result);
72 return true;
73 }
74
75 return ProcessDump(args[0], result) == DH_SUCCESS;
76 }
77
ProcessDump(const std::string & args,std::string & result)78 int32_t DaudioHidumper::ProcessDump(const std::string &args, std::string &result)
79 {
80 DHLOGI("Process dump.");
81 HidumpFlag hf = HidumpFlag::UNKNOWN;
82 auto operatorIter = ARGS_MAP.find(args);
83 if (operatorIter != ARGS_MAP.end()) {
84 hf = operatorIter->second;
85 }
86
87 if (hf == HidumpFlag::GET_HELP) {
88 ShowHelp(result);
89 return DH_SUCCESS;
90 }
91 result.clear();
92 switch (hf) {
93 case HidumpFlag::GET_SOURCE_DEVID: {
94 return GetSourceDevId(result);
95 }
96 case HidumpFlag::GET_SINK_INFO: {
97 return GetSinkInfo(result);
98 }
99 case HidumpFlag::GET_ABILITY: {
100 return GetAbilityInfo(result);
101 }
102 case HidumpFlag::DUMP_AUDIO_DATA_START: {
103 return StartDumpData(result);
104 }
105 case HidumpFlag::DUMP_AUDIO_DATA_STOP: {
106 return StopDumpData(result);
107 }
108 default: {
109 return ShowIllegalInfomation(result);
110 }
111 }
112 }
113
GetSourceDevId(std::string & result)114 int32_t DaudioHidumper::GetSourceDevId(std::string &result)
115 {
116 DHLOGI("Get source devId dump.");
117 std::string sourceDevId = "";
118 int32_t ret = GetLocalDeviceNetworkId(sourceDevId);
119 if (ret != DH_SUCCESS) {
120 DHLOGE("Get local network id failed.");
121 result.append("sourceDevId: ").append("");
122 return ret;
123 }
124 result.append("sourceDevId: ").append(GetAnonyString(sourceDevId));
125 return DH_SUCCESS;
126 }
127
GetSinkInfo(std::string & result)128 int32_t DaudioHidumper::GetSinkInfo(std::string &result)
129 {
130 DHLOGI("Get sink info dump.");
131 audioManager_ = GetAudioManagerFuncs();
132 if (audioManager_ == nullptr) {
133 return ERR_DH_AUDIO_NULLPTR;
134 }
135 int32_t ret = audioManager_->GetAllAdapters(audioManager_, &adapterdesc_, &g_deviceNum);
136 if (ret != DH_SUCCESS) {
137 DHLOGE("Get all adapters failed.");
138 return ERR_DH_AUDIO_NULLPTR;
139 }
140 for (int32_t index = 0; index < g_deviceNum; index++) {
141 AudioAdapterDescriptor &desc = adapterdesc_[index];
142 result.append("sinkDevId: ").append(GetAnonyString(desc.adapterName)).append(" portId: ");
143 for (uint32_t i = 0; i < desc.portNum; i++) {
144 result.append(std::to_string(desc.ports[i].portId)).append(" ");
145 }
146 }
147
148 return DH_SUCCESS;
149 }
150
GetAbilityInfo(std::string & result)151 int32_t DaudioHidumper::GetAbilityInfo(std::string &result)
152 {
153 DHLOGI("Obtaining capability information.");
154 std::vector<DHItem> abilityInfo = DAudioHandler::GetInstance().ablityForDump();
155 for (DHItem dhItem : abilityInfo) {
156 if (dhItem.dhId == DEFAULT_SPK_DHID) {
157 result.append("spkAbilityInfo:").append(dhItem.attrs).append(" ");
158 }
159 if (dhItem.dhId == DEFAULT_MIC_DHID) {
160 result.append("micAbilityInfo:").append(dhItem.attrs).append(" ");
161 }
162 }
163 return DH_SUCCESS;
164 }
165
StartDumpData(std::string & result)166 int32_t DaudioHidumper::StartDumpData(std::string &result)
167 {
168 if (access(DUMP_FILE_PATH.c_str(), 0) < 0) {
169 if (mkdir(DUMP_FILE_PATH.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
170 DHLOGE("Create dir error");
171 return ERR_DH_AUDIO_FAILED;
172 }
173 }
174 DHLOGI("Start dump audio data.");
175 result.append("start dump...");
176 dumpAudioDataFlag_ = true;
177 return DH_SUCCESS;
178 }
179
StopDumpData(std::string & result)180 int32_t DaudioHidumper::StopDumpData(std::string &result)
181 {
182 DHLOGI("Stop dump audio data.");
183 result.append("stop dump...");
184 dumpAudioDataFlag_ = false;
185 return DH_SUCCESS;
186 }
187
QueryDumpDataFlag()188 bool DaudioHidumper::QueryDumpDataFlag()
189 {
190 return dumpAudioDataFlag_;
191 }
192
ShowHelp(std::string & result)193 void DaudioHidumper::ShowHelp(std::string &result)
194 {
195 DHLOGI("Show help.");
196 result.append("Usage:dump <command> [options]\n")
197 .append("Description:\n")
198 .append("-h ")
199 .append(": show help\n")
200 .append("--sourceDevId ")
201 .append(": dump audio sourceDevId in the system\n")
202 .append("--sinkInfo ")
203 .append(": dump sink info in the system\n")
204 .append("--ability ")
205 .append(": dump current ability of the audio in the system\n")
206 .append("--startDump")
207 .append(": start dump audio data in the system /data/data/daudio\n")
208 .append("--stopDump")
209 .append(": stop dump audio data in the system\n");
210 }
211
ShowIllegalInfomation(std::string & result)212 int32_t DaudioHidumper::ShowIllegalInfomation(std::string &result)
213 {
214 DHLOGI("Show illegal information.");
215 result.append("unknown command, -h for help.");
216 return DH_SUCCESS;
217 }
218 } // namespace DistributedHardware
219 } // namespace OHOS