1 /*
2 * Copyright (c) 2023-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 <unistd.h>
17
18 #include "daudio_sink_hidumper.h"
19
20 #include "daudio_constants.h"
21 #include "daudio_errorcode.h"
22 #include "daudio_log.h"
23 #include "daudio_util.h"
24
25 #undef DH_LOG_TAG
26 #define DH_LOG_TAG "DaudioSinkHidumper"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 IMPLEMENT_SINGLE_INSTANCE(DaudioSinkHidumper);
31
32 namespace {
33 const std::string ARGS_HELP = "-h";
34 const std::string ARGS_DUMP_AUDIO_DATA_START = "--startDump";
35 const std::string ARGS_DUMP_AUDIO_DATA_STOP = "--stopDump";
36
37 const std::map<std::string, HidumpFlag> ARGS_MAP = {
38 { ARGS_HELP, HidumpFlag::GET_HELP },
39 { ARGS_DUMP_AUDIO_DATA_START, HidumpFlag::DUMP_AUDIO_DATA_START },
40 { ARGS_DUMP_AUDIO_DATA_STOP, HidumpFlag::DUMP_AUDIO_DATA_STOP },
41 };
42 }
43
DaudioSinkHidumper()44 DaudioSinkHidumper::DaudioSinkHidumper()
45 {
46 DHLOGI("Distributed audio hidumper constructed.");
47 }
48
~DaudioSinkHidumper()49 DaudioSinkHidumper::~DaudioSinkHidumper()
50 {
51 DHLOGI("Distributed audio hidumper deconstructed.");
52 }
53
Dump(const std::vector<std::string> & args,std::string & result)54 bool DaudioSinkHidumper::Dump(const std::vector<std::string> &args, std::string &result)
55 {
56 result.clear();
57 int32_t argsSize = static_cast<int32_t>(args.size());
58 if (argsSize > DUMP_MAX_SIZE) {
59 DHLOGE("Dump input is invalid");
60 return false;
61 }
62 DHLOGI("Distributed audio hidumper dump args.size():%{public}d", argsSize);
63 for (int32_t i = 0; i < argsSize; i++) {
64 DHLOGD("Distributed audio hidumper dump args[%{public}d]: %{public}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 DaudioSinkHidumper::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::DUMP_AUDIO_DATA_START: {
94 return StartDumpData(result);
95 }
96 case HidumpFlag::DUMP_AUDIO_DATA_STOP: {
97 return StopDumpData(result);
98 }
99 default: {
100 return ShowIllegalInfomation(result);
101 }
102 }
103 }
104
StartDumpData(std::string & result)105 int32_t DaudioSinkHidumper::StartDumpData(std::string &result)
106 {
107 if (access(DUMP_FILE_PATH.c_str(), 0) < 0) {
108 if (mkdir(DUMP_FILE_PATH.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
109 DHLOGE("Create dir error");
110 return ERR_DH_AUDIO_FAILED;
111 }
112 }
113 DHLOGI("start dump audio data.");
114 result.append("start dump...");
115 dumpAudioDataFlag_ = true;
116 return DH_SUCCESS;
117 }
118
StopDumpData(std::string & result)119 int32_t DaudioSinkHidumper::StopDumpData(std::string &result)
120 {
121 DHLOGI("stop dump audio data.");
122 result.append("stop dump...");
123 dumpAudioDataFlag_ = false;
124 return DH_SUCCESS;
125 }
126
QueryDumpDataFlag()127 bool DaudioSinkHidumper::QueryDumpDataFlag()
128 {
129 return dumpAudioDataFlag_;
130 }
131
ShowHelp(std::string & result)132 void DaudioSinkHidumper::ShowHelp(std::string &result)
133 {
134 DHLOGI("Show help.");
135 result.append("Usage:dump <command> [options]\n")
136 .append("Description:\n")
137 .append("-h ")
138 .append(": show help\n")
139 .append("--startDump")
140 .append(": start dump audio data in the system /data/data/daudio\n")
141 .append("--stopDump")
142 .append(": stop dump audio data in the system\n");
143 }
144
ShowIllegalInfomation(std::string & result)145 int32_t DaudioSinkHidumper::ShowIllegalInfomation(std::string &result)
146 {
147 DHLOGI("Show illegal information.");
148 result.append("unknown command, -h for help.");
149 return DH_SUCCESS;
150 }
151 } // namespace DistributedHardware
152 } // namespace OHOS