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