• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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     DHLOGI("Distributed audio hidumper dump args.size():%d.", args.size());
57     result.clear();
58     int32_t argsSize = static_cast<int32_t>(args.size());
59     for (int32_t i = 0; i < argsSize; i++) {
60         DHLOGI("Distributed audio hidumper dump args[%d]: %s.", i, args.at(i).c_str());
61     }
62 
63     if (args.empty()) {
64         ShowHelp(result);
65         return true;
66     } else if (args.size() > 1) {
67         ShowIllegalInfomation(result);
68         return true;
69     }
70 
71     return ProcessDump(args[0], result) == DH_SUCCESS;
72 }
73 
ProcessDump(const std::string & args,std::string & result)74 int32_t DaudioSinkHidumper::ProcessDump(const std::string &args, std::string &result)
75 {
76     DHLOGI("Process dump.");
77     HidumpFlag hf = HidumpFlag::UNKNOWN;
78     auto operatorIter = ARGS_MAP.find(args);
79     if (operatorIter != ARGS_MAP.end()) {
80         hf = operatorIter->second;
81     }
82 
83     if (hf == HidumpFlag::GET_HELP) {
84         ShowHelp(result);
85         return DH_SUCCESS;
86     }
87     result.clear();
88     switch (hf) {
89         case HidumpFlag::DUMP_AUDIO_DATA_START: {
90             return StartDumpData(result);
91         }
92         case HidumpFlag::DUMP_AUDIO_DATA_STOP: {
93             return StopDumpData(result);
94         }
95         default: {
96             return ShowIllegalInfomation(result);
97         }
98     }
99 }
100 
StartDumpData(std::string & result)101 int32_t DaudioSinkHidumper::StartDumpData(std::string &result)
102 {
103     if (access(DUMP_FILE_PATH.c_str(), 0) < 0) {
104         if (mkdir(DUMP_FILE_PATH.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
105             DHLOGE("Create dir error");
106             return ERR_DH_AUDIO_FAILED;
107         }
108     }
109     DHLOGI("start dump audio data.");
110     result.append("start dump...");
111     dumpAudioDataFlag_ = true;
112     return DH_SUCCESS;
113 }
114 
StopDumpData(std::string & result)115 int32_t DaudioSinkHidumper::StopDumpData(std::string &result)
116 {
117     DHLOGI("stop dump audio data.");
118     result.append("stop dump...");
119     dumpAudioDataFlag_ = false;
120     return DH_SUCCESS;
121 }
122 
QueryDumpDataFlag()123 bool DaudioSinkHidumper::QueryDumpDataFlag()
124 {
125     return dumpAudioDataFlag_;
126 }
127 
ShowHelp(std::string & result)128 void DaudioSinkHidumper::ShowHelp(std::string &result)
129 {
130     DHLOGI("Show help.");
131     result.append("Usage:dump  <command> [options]\n")
132         .append("Description:\n")
133         .append("-h            ")
134         .append(": show help\n")
135         .append("--startDump")
136         .append(": start dump audio data in the system /data/data/daudio\n")
137         .append("--stopDump")
138         .append(": stop dump audio data in the system\n");
139 }
140 
ShowIllegalInfomation(std::string & result)141 int32_t DaudioSinkHidumper::ShowIllegalInfomation(std::string &result)
142 {
143     DHLOGI("Show illegal information.");
144     result.append("unknown command, -h for help.");
145     return DH_SUCCESS;
146 }
147 } // namespace DistributedHardware
148 } // namespace OHOS