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 "dscreen_hidumper.h"
17
18 #include "dscreen_errcode.h"
19 #include "dscreen_log.h"
20 #include "dscreen_util.h"
21
22 #undef DH_LOG_TAG
23 #define DH_LOG_TAG "DscreenHidumper"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 IMPLEMENT_SINGLE_INSTANCE(DscreenHidumper);
28
29 namespace {
30 const std::string ARGS_HELP = "-h";
31 const std::string ARGS_DUMP_SCREEN_DATA = "--dump";
32 const std::string ARGS_DUMP_SCREEN_DATA_RESTART = "--redump";
33 const std::string ARGS_DUMP_SCREEN_DATA_STOP = "--stopdump";
34
35 const std::map<std::string, HidumpFlag> ARGS_MAP = {
36 { ARGS_HELP, HidumpFlag::GET_HELP },
37 { ARGS_DUMP_SCREEN_DATA, HidumpFlag::DUMP_SCREEN_DATA },
38 { ARGS_DUMP_SCREEN_DATA_RESTART, HidumpFlag::DUMP_SCREEN_DATA_RESTART },
39 { ARGS_DUMP_SCREEN_DATA_STOP, HidumpFlag::DUMP_SCREEN_DATA_STOP },
40 };
41 }
42
DscreenHidumper()43 DscreenHidumper::DscreenHidumper()
44 {
45 DHLOGI("Distributed screen hidumper constructed.");
46 }
47
~DscreenHidumper()48 DscreenHidumper::~DscreenHidumper()
49 {
50 DHLOGI("Distributed screen hidumper deconstructed.");
51 }
52
Dump(const std::vector<std::string> & args,std::string & result)53 bool DscreenHidumper::Dump(const std::vector<std::string> &args, std::string &result)
54 {
55 DHLOGI("Distributed screen 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 screen hidumper dump args[%d]: %s.", i, args.at(i).c_str());
60 }
61
62 if (args.empty()) {
63 ShowHelp(result);
64 return true;
65 }
66
67 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 DscreenHidumper::ProcessDump(const std::string &args, std::string &result)
76 {
77 DHLOGI("Process dump.");
78 HidumpFlag hidumpFlag = HidumpFlag::UNKNOWN;
79 auto operatorIter = ARGS_MAP.find(args);
80 if (operatorIter != ARGS_MAP.end()) {
81 hidumpFlag = operatorIter->second;
82 }
83
84 if (hidumpFlag == HidumpFlag::GET_HELP) {
85 ShowHelp(result);
86 return DH_SUCCESS;
87 }
88 result.clear();
89 switch (hidumpFlag) {
90 case HidumpFlag::DUMP_SCREEN_DATA: {
91 return DumpScreenData(result);
92 }
93 case HidumpFlag::DUMP_SCREEN_DATA_RESTART: {
94 return ReDumpScreenData(result);
95 }
96 case HidumpFlag::DUMP_SCREEN_DATA_STOP: {
97 SetFlagFalse();
98 return DH_SUCCESS;
99 }
100 default: {
101 return ShowIllegalInfomation(result);
102 }
103 }
104 }
105
DumpScreenData(std::string & result)106 int32_t DscreenHidumper::DumpScreenData(std::string &result)
107 {
108 DHLOGI("Dump screen data.");
109
110 if (access(DUMP_FILE_PATH.c_str(), 0) < 0) {
111 if (mkdir(DUMP_FILE_PATH.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
112 DHLOGI("Create dir err.");
113 DHLOGI("dir path : %s", DUMP_FILE_PATH.c_str());
114 return DSCREEN_BAD_VALUE;
115 }
116 }
117
118 if (fileFullFlag_ == false) {
119 result.append("Dump...");
120 hidumperFlag_ = true;
121 } else {
122 result.append("File oversize 300M : stop dump, use parameter \"--redump\" to clear dumpfile and redump");
123 }
124 return DH_SUCCESS;
125 }
126
ReDumpScreenData(std::string & result)127 int32_t DscreenHidumper::ReDumpScreenData(std::string &result)
128 {
129 DHLOGI("Redump screen data.");
130
131 if (access(DUMP_FILE_PATH.c_str(), 0) < 0) {
132 if (mkdir(DUMP_FILE_PATH.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
133 DHLOGI("Create dir err.");
134 DHLOGI("dir path : %s", DUMP_FILE_PATH.c_str());
135 return DSCREEN_BAD_VALUE;
136 }
137 }
138 SetFileFlagFalse();
139 SetReDumpFlagTrue();
140 result.append("ReDumpStart...");
141 hidumperFlag_ = true;
142 SetTransReDumpFlagTrue();
143 return DH_SUCCESS;
144 }
145
GetFlagStatus()146 bool DscreenHidumper::GetFlagStatus()
147 {
148 return hidumperFlag_;
149 }
150
SetFlagFalse()151 void DscreenHidumper::SetFlagFalse()
152 {
153 hidumperFlag_ = false;
154 }
155
GetFileFlag()156 bool DscreenHidumper::GetFileFlag()
157 {
158 return fileFullFlag_;
159 }
160
SetFileFlagFalse()161 void DscreenHidumper::SetFileFlagFalse()
162 {
163 fileFullFlag_ = false;
164 }
165
SetFileFlagTrue()166 void DscreenHidumper::SetFileFlagTrue()
167 {
168 fileFullFlag_ = true;
169 }
170
GetReDumpFlag()171 bool DscreenHidumper::GetReDumpFlag()
172 {
173 return reDumpFlag_;
174 }
175
SetReDumpFlagFalse()176 void DscreenHidumper::SetReDumpFlagFalse()
177 {
178 reDumpFlag_ = false;
179 }
180
SetReDumpFlagTrue()181 void DscreenHidumper::SetReDumpFlagTrue()
182 {
183 reDumpFlag_ = true;
184 }
185
GetTransReDumpFlag()186 bool DscreenHidumper::GetTransReDumpFlag()
187 {
188 return transReDumpFlag_;
189 }
190
SetTransReDumpFlagFalse()191 void DscreenHidumper::SetTransReDumpFlagFalse()
192 {
193 transReDumpFlag_ = false;
194 }
195
SetTransReDumpFlagTrue()196 void DscreenHidumper::SetTransReDumpFlagTrue()
197 {
198 transReDumpFlag_ = true;
199 }
200
ShowHelp(std::string & result)201 void DscreenHidumper::ShowHelp(std::string &result)
202 {
203 DHLOGI("Show help.");
204 result.append("Usage:dump <command> [options]\n")
205 .append("Description:\n")
206 .append("-h ")
207 .append(": show help\n")
208 .append("--dump ")
209 .append(": dump screen data in /data/data/dscreen\n")
210 .append("--redump ")
211 .append(": clear file and restart dump screen data\n")
212 .append("--stopdump ")
213 .append(": stop dump screen data\n");
214 }
215
ShowIllegalInfomation(std::string & result)216 int32_t DscreenHidumper::ShowIllegalInfomation(std::string &result)
217 {
218 DHLOGI("Show illegal information.");
219 result.append("unknown command, -h for help.");
220 return DH_SUCCESS;
221 }
222
SaveFile(std::string file,const VideoData & video)223 void DscreenHidumper::SaveFile(std::string file, const VideoData &video)
224 {
225 DHLOGE("Saving File.");
226 std::string fileName = DUMP_FILE_PATH + "/" + file + std::to_string(video.width) + ")_height(" +
227 std::to_string(video.height) + ")_" + video.format + ".jpg";
228 DHLOGE("fileName = %s", fileName.c_str());
229 if (GetReDumpFlag() == true) {
230 std::remove(fileName.c_str());
231 SetReDumpFlagFalse();
232 }
233 std::ofstream ofs(fileName, std::ios::binary | std::ios::out | std::ios::app);
234
235 if (!ofs.is_open()) {
236 DHLOGE("open file failed.");
237 return;
238 }
239 DHLOGE("open Hidumper SaveFile file success.");
240 ofs.seekp(0, std::ios::end);
241 std::ofstream::pos_type fileSize = ofs.tellp();
242 if (fileSize < 0) {
243 DHLOGE("filesize get err");
244 fileSize = 0;
245 }
246 if ((static_cast<size_t>(fileSize) + video.size) < DUMP_FILE_MAX_SIZE) {
247 SetFileFlagFalse();
248 ofs.write(reinterpret_cast<const char *>(video.data), video.size);
249 } else {
250 SetFileFlagTrue();
251 }
252 ofs.close();
253 }
254 } // namespace DistributedHardware
255 } // namespace OHOS