1 /*
2 * Copyright (c) 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 #include "screen_session_dumper.h"
16
17 #include <csignal>
18 #include <fstream>
19 #include <transaction/rs_interfaces.h>
20
21 #include "unique_fd.h"
22 #include "screen_session_manager.h"
23 #include "session_permission.h"
24 #include "screen_rotation_property.h"
25 #include "screen_sensor_connector.h"
26 #include "parameters.h"
27 #include "fold_screen_controller/fold_screen_sensor_manager.h"
28 #include "fold_screen_state_internel.h"
29 #include "window_helper.h"
30 #include "fold_screen_controller/secondary_fold_sensor_manager.h"
31
32 namespace OHOS {
33 namespace Rosen {
34 namespace {
35 constexpr int LINE_WIDTH = 30;
36 constexpr int DUMPER_PARAM_INDEX_ONE = 1;
37 constexpr int DUMPER_PARAM_INDEX_TWO = 2;
38 constexpr int DUMPER_PARAM_INDEX_THREE = 3;
39 const std::string ARG_DUMP_HELP = "-h";
40 const std::string ARG_DUMP_ALL = "-a";
41 const std::string ARG_DUMP_FOLD_STATUS = "-f";
42
43 constexpr int MOTION_SENSOR_PARAM_SIZE = 2;
44 const std::string STATUS_FOLD_HALF = "-z";
45 const std::string STATUS_EXPAND = "-y";
46 const std::string STATUS_FOLD = "-p";
47 const std::string ARG_SET_ROTATION_SENSOR = "-motion"; // rotation event inject
48 const std::string ARG_SET_ROTATION_LOCK = "-rotationlock";
49 const std::string ARG_FOLD_DISPLAY_FULL = "-f";
50 const std::string ARG_FOLD_DISPLAY_MAIN = "-m";
51 const std::string ARG_FOLD_DISPLAY_GLOBALL_FULL = "-g";
52 const std::string ARG_FOLD_DISPLAY_SUB = "-sub";
53 const std::string ARG_FOLD_DISPLAY_COOR = "-coor";
54 const std::vector<std::string> displayModeCommands = {"-f", "-m", "-sub", "-coor", "-g"};
55 const std::string ARG_LOCK_FOLD_DISPLAY_STATUS = "-l";
56 const std::string ARG_UNLOCK_FOLD_DISPLAY_STATUS = "-u";
57 const std::string ARG_SET_ON_TENT_MODE = "-ontent";
58 const std::string ARG_SET_OFF_TENT_MODE = "-offtent";
59 const std::string ARG_SET_HOVER_STATUS = "-hoverstatus";
60 const std::string ARG_SET_POSTURE_HALL = "-posture";
61 const std::string ARG_SET_POSTURE_HALL_STATUS = "-registerhall"; // 关闭开合sensor报值
62 const std::string ARG_CHANGE_OUTER_CMD = "outer";
63 const std::string ARG_SET_SECONDARY_FOLD_STATUS = "-secondary";
64 const char SECONDARY_DUMPER_VALUE_BOUNDARY[] = "mfg";
65 constexpr size_t SECONDARY_FOLD_STATUS_INDEX_M = 0;
66 constexpr size_t SECONDARY_FOLD_STATUS_INDEX_F = 1;
67 constexpr size_t SECONDARY_FOLD_STATUS_INDEX_G = 2;
68 constexpr size_t SECONDARY_FOLD_STATUS_COMMAND_NUM = 2;
69 constexpr uint16_t HALL_EXT_DATA_FLAG = 26;
70 }
71
GetProcessNameByPid(int32_t pid)72 static std::string GetProcessNameByPid(int32_t pid)
73 {
74 std::string filePath = "/proc/" + std::to_string(pid) + "/comm";
75 char tmpPath[PATH_MAX] = { 0 };
76 if (!realpath(filePath.c_str(), tmpPath)) {
77 return "UNKNOWN";
78 }
79 std::ifstream infile(filePath);
80 if (!infile.is_open()) {
81 return "UNKNOWN";
82 }
83 std::string processName = "UNKNOWN";
84 std::getline(infile, processName);
85 infile.close();
86 return processName;
87 }
88
ScreenSessionDumper(int fd,const std::vector<std::u16string> & args)89 ScreenSessionDumper::ScreenSessionDumper(int fd, const std::vector<std::u16string>& args)
90 : fd_(fd)
91 {
92 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> cv;
93 std::string info;
94 for (auto& u16str: args) {
95 std::string arg = cv.to_bytes(u16str);
96 params_.emplace_back(arg);
97 info += arg;
98 }
99 TLOGI(WmsLogTag::DMS, "input args: [%{public}s]", info.c_str());
100 }
101
IsNumber(std::string str)102 bool ScreenSessionDumper::IsNumber(std::string str)
103 {
104 if (str.size() == 0) {
105 return false;
106 }
107 for (int32_t i = 0; i < static_cast<int32_t>(str.size()); i++) {
108 if (str.at(i) < '0' || str.at(i) > '9') {
109 return false;
110 }
111 }
112 return true;
113 }
114
OutputDumpInfo()115 void ScreenSessionDumper::OutputDumpInfo()
116 {
117 if (fd_ < 0) {
118 TLOGE(WmsLogTag::DMS, "invalid fd: %{public}d", fd_);
119 return;
120 }
121
122 static_cast<void>(signal(SIGPIPE, SIG_IGN)); // ignore SIGPIPE crash
123 int ret = dprintf(fd_, "%s\n", dumpInfo_.c_str());
124 if (ret < 0) {
125 TLOGE(WmsLogTag::DMS, "dprintf error. ret: %{public}d", ret);
126 return;
127 }
128 dumpInfo_.clear();
129 }
130
131
ExcuteDumpCmd()132 void ScreenSessionDumper::ExcuteDumpCmd()
133 {
134 if (params_.empty() || params_[0] == ARG_DUMP_HELP) { //help command
135 ShowHelpInfo();
136 }
137
138 if (!(SessionPermission::IsSACalling() || SessionPermission::IsStartByHdcd())) {
139 TLOGE(WmsLogTag::DMS, "dump permission denied!");
140 return;
141 }
142 if (!params_.empty() && params_[0] == ARG_DUMP_ALL) { // dump all info command
143 ShowAllScreenInfo();
144 } else if (!params_.empty() && params_[0] == ARG_DUMP_FOLD_STATUS) { // dump fold status command
145 DumpFoldStatus();
146 }
147 ExcuteInjectCmd();
148 OutputDumpInfo();
149 }
150
ExcuteInjectCmd()151 void ScreenSessionDumper::ExcuteInjectCmd()
152 {
153 bool isDeveloperMode = system::GetBoolParameter("const.security.developermode.state", false);
154 if (isDeveloperMode) {
155 if (params_.size() == 1 && IsValidDisplayModeCommand(params_[0])) {
156 int errCode = SetFoldDisplayMode();
157 if (errCode != 0) {
158 ShowIllegalArgsInfo();
159 }
160 return;
161 }
162 }
163
164 bool isDebugMode = system::GetBoolParameter("dms.hidumper.supportdebug", false);
165 if (!isDebugMode) {
166 TLOGI(WmsLogTag::DMS, "Can't use DMS hidumper inject methods.");
167 dumpInfo_.append("dms.hidumper.supportdebug false\n");
168 return;
169 }
170 if (params_[0] == STATUS_FOLD_HALF || params_[0] == STATUS_EXPAND || params_[0] == STATUS_FOLD) {
171 ShowNotifyFoldStatusChangedInfo();
172 return;
173 } else if (params_[0].find(ARG_SET_ROTATION_SENSOR) != std::string::npos) {
174 SetMotionSensorvalue(params_[0]);
175 return;
176 } else if (params_[0].find(ARG_SET_ROTATION_LOCK) != std::string::npos) {
177 SetRotationLockedvalue(params_[0]);
178 return;
179 } else if (params_.size() == 1 && (params_[0] == ARG_LOCK_FOLD_DISPLAY_STATUS
180 || params_[0] == ARG_UNLOCK_FOLD_DISPLAY_STATUS)) {
181 int errCode = SetFoldStatusLocked();
182 if (errCode != 0) {
183 ShowIllegalArgsInfo();
184 }
185 return;
186 }
187 ExcuteInjectCmd2();
188 }
189
ExcuteInjectCmd2()190 void ScreenSessionDumper::ExcuteInjectCmd2()
191 {
192 if (params_[0].find(ARG_SET_ON_TENT_MODE) != std::string::npos ||
193 params_[0].find(ARG_SET_OFF_TENT_MODE) != std::string::npos) {
194 SetEnterOrExitTentMode(params_[0]);
195 } else if (params_[0].find(ARG_SET_HOVER_STATUS) != std::string::npos) {
196 SetHoverStatusChange(params_[0]);
197 } else if (params_[0].find(ARG_SET_POSTURE_HALL) != std::string::npos) {
198 SetHallAndPostureValue(params_[0]);
199 } else if (params_[0].find(ARG_SET_POSTURE_HALL_STATUS) != std::string::npos) {
200 SetHallAndPostureStatus(params_[0]);
201 } else if (params_[0].find(ARG_SET_SECONDARY_FOLD_STATUS) != std::string::npos) {
202 SetSecondaryStatusChange(params_[0]);
203 }
204 }
205
DumpEventTracker(EventTracker & tracker)206 void ScreenSessionDumper::DumpEventTracker(EventTracker& tracker)
207 {
208 std::ostringstream oss;
209 auto recordInfos = tracker.GetRecordInfos();
210 oss << "-------------- DMS KEY EVENTS LIST --------------" << std::endl;
211 for (const auto& info : recordInfos) {
212 oss << std::left << "[" << tracker.formatTimestamp(info.timestamp).c_str()
213 << "]: " << info.info.c_str() << std::endl;
214 }
215 dumpInfo_.append(oss.str());
216 }
217
DumpMultiUserInfo(std::vector<int32_t> oldScbPids,int32_t userId,int32_t ScbPid)218 void ScreenSessionDumper::DumpMultiUserInfo(std::vector<int32_t> oldScbPids, int32_t userId, int32_t ScbPid)
219 {
220 std::ostringstream oss;
221 oss << "-------------- DMS Multi User Info --------------" << std::endl;
222 oss << std::left << "[oldScbPid:] ";
223 for (auto oldScbPid : oldScbPids) {
224 oss << oldScbPid << " ";
225 }
226 oss << std::endl;
227 oss << std::left << "[userId:] " << userId << std::endl;
228 oss << std::left << "[ScbPid:] " << ScbPid << std::endl;
229 dumpInfo_.append(oss.str());
230 }
231
DumpFreezedPidList(std::set<int32_t> pidList)232 void ScreenSessionDumper::DumpFreezedPidList(std::set<int32_t> pidList)
233 {
234 std::ostringstream oss;
235 oss << "-------------- DMS FREEZED PID LIST --------------" << std::endl;
236 for (auto pid : pidList) {
237 oss << std::left << "[PID: " << pid << "]: "
238 << " [" << GetProcessNameByPid(pid) << "]"<< std::endl;
239 }
240 dumpInfo_.append(oss.str());
241 }
242
ShowHelpInfo()243 void ScreenSessionDumper::ShowHelpInfo()
244 {
245 dumpInfo_.append("Usage:\n")
246 .append(" -h ")
247 .append("|help text for the tool\n")
248 .append(" -a ")
249 .append("|dump all screen information in the system\n")
250 .append(" -z ")
251 .append("|switch to fold half status\n")
252 .append(" -y ")
253 .append("|switch to expand status\n")
254 .append(" -p ")
255 .append("|switch to fold status\n")
256 .append(" -f ")
257 .append("|get to fold status\n");
258 }
259
ShowAllScreenInfo()260 void ScreenSessionDumper::ShowAllScreenInfo()
261 {
262 std::vector<ScreenId> screenIds = ScreenSessionManager::GetInstance().GetAllScreenIds();
263 for (auto screenId : screenIds) {
264 std::ostringstream oss;
265 oss << "---------------- Screen ID: " << screenId << " ----------------" << std::endl;
266 dumpInfo_.append(oss.str());
267 DumpFoldStatus();
268 DumpTentMode();
269 DumpScreenSessionById(screenId);
270 DumpRsInfoById(screenId);
271 DumpCutoutInfoById(screenId);
272 DumpScreenInfoById(screenId);
273 DumpScreenPropertyById(screenId);
274 }
275 }
276
DumpFoldStatus()277 void ScreenSessionDumper::DumpFoldStatus()
278 {
279 std::ostringstream oss;
280 auto foldStatus = ScreenSessionManager::GetInstance().GetFoldStatus();
281 std::string status = "";
282 switch (foldStatus) {
283 case FoldStatus::EXPAND: {
284 status = "EXPAND";
285 break;
286 }
287 case FoldStatus::FOLDED: {
288 status = "FOLDED";
289 break;
290 }
291 case FoldStatus::HALF_FOLD: {
292 status = "HALF_FOLD";
293 break;
294 }
295 default: {
296 status = "UNKNOWN";
297 break;
298 }
299 }
300 oss << std::left << std::setw(LINE_WIDTH) << "FoldStatus: "
301 << status << std::endl;
302 dumpInfo_.append(oss.str());
303 }
304
DumpTentMode()305 void ScreenSessionDumper::DumpTentMode()
306 {
307 std::ostringstream oss;
308 bool isTentMode = ScreenSessionManager::GetInstance().GetTentMode();
309 std::string status = "";
310 if (isTentMode) {
311 status = "TRUE";
312 } else {
313 status = "FALSE";
314 }
315 oss << std::left << std::setw(LINE_WIDTH) << "TentMode: "
316 << status << std::endl;
317 dumpInfo_.append(oss.str());
318 }
319
DumpScreenSessionById(ScreenId id)320 void ScreenSessionDumper::DumpScreenSessionById(ScreenId id)
321 {
322 std::ostringstream oss;
323 oss << "[SCREEN SESSION]" << std::endl;
324 auto screenSession = ScreenSessionManager::GetInstance().GetScreenSession(id);
325 if (screenSession == nullptr) {
326 TLOGE(WmsLogTag::DMS, "screenSession nullptr. screen id: %{public}" PRIu64"", id);
327 return;
328 }
329 oss << std::left << std::setw(LINE_WIDTH) << "Name: "
330 << screenSession->GetName() << std::endl;
331 oss << std::left << std::setw(LINE_WIDTH) << "RSScreenId: "
332 << screenSession->GetRSScreenId() << std::endl;
333 sptr<SupportedScreenModes> activeModes = screenSession->GetActiveScreenMode();
334 if (activeModes != nullptr) {
335 oss << std::left << std::setw(LINE_WIDTH) << "activeModes<id, W, H, RS>: "
336 << activeModes->id_ << ", " << activeModes->width_ << ", "
337 << activeModes->height_ << ", " << activeModes->refreshRate_ << std::endl;
338 }
339 oss << std::left << std::setw(LINE_WIDTH) << "SourceMode: "
340 << static_cast<int32_t>(screenSession->GetSourceMode()) << std::endl;
341 oss << std::left << std::setw(LINE_WIDTH) << "ScreenCombination: "
342 << static_cast<int32_t>(screenSession->GetScreenCombination()) << std::endl;
343 oss << std::left << std::setw(LINE_WIDTH) << "Orientation: "
344 << static_cast<int32_t>(screenSession->GetOrientation()) << std::endl;
345 oss << std::left << std::setw(LINE_WIDTH) << "Rotation: "
346 << static_cast<int32_t>(screenSession->GetRotation()) << std::endl;
347 oss << std::left << std::setw(LINE_WIDTH) << "ScreenRequestedOrientation: "
348 << static_cast<int32_t>(screenSession->GetScreenRequestedOrientation()) << std::endl;
349 dumpInfo_.append(oss.str());
350 }
351
DumpRsInfoById(ScreenId id)352 void ScreenSessionDumper::DumpRsInfoById(ScreenId id)
353 {
354 std::ostringstream oss;
355 oss << "[RS INFO]" << std::endl;
356 auto screenSession = ScreenSessionManager::GetInstance().GetScreenSession(id);
357 if (screenSession == nullptr) {
358 TLOGE(WmsLogTag::DMS, "screenSession nullptr. screen id: %{public}" PRIu64"", id);
359 return;
360 }
361 std::vector<ScreenColorGamut> colorGamuts;
362 DMError ret = screenSession->GetScreenSupportedColorGamuts(colorGamuts);
363 if (ret == DMError::DM_OK && colorGamuts.size() > 0) {
364 oss << std::left << std::setw(LINE_WIDTH) << "SupportedColorGamuts: ";
365 for (uint32_t i = 0; i < colorGamuts.size() - 1; i++) {
366 oss << static_cast<int32_t>(colorGamuts[i]) << ", ";
367 }
368 oss << static_cast<int32_t>(colorGamuts[colorGamuts.size() - 1]) << std::endl;
369 }
370 ScreenColorGamut colorGamut;
371 ret = screenSession->GetScreenColorGamut(colorGamut);
372 if (ret == DMError::DM_OK) {
373 oss << std::left << std::setw(LINE_WIDTH) << "ScreenColorGamut: "
374 << static_cast<int32_t>(colorGamut) << std::endl;
375 }
376 ScreenGamutMap gamutMap;
377 ret = screenSession->GetScreenGamutMap(gamutMap);
378 if (ret == DMError::DM_OK) {
379 oss << std::left << std::setw(LINE_WIDTH) << "ScreenGamutMap: "
380 << static_cast<int32_t>(gamutMap) << std::endl;
381 }
382 GraphicPixelFormat pixelFormat;
383 ret = screenSession->GetPixelFormat(pixelFormat);
384 if (ret == DMError::DM_OK) {
385 oss << std::left << std::setw(LINE_WIDTH) << "GraphicPixelFormat: "
386 << static_cast<int32_t>(pixelFormat) << std::endl;
387 }
388 dumpInfo_.append(oss.str());
389 DumpRsInfoById01(screenSession); // 拆分函数,避免函数过长
390 }
391
DumpRsInfoById01(sptr<ScreenSession> screenSession)392 void ScreenSessionDumper::DumpRsInfoById01(sptr<ScreenSession> screenSession)
393 {
394 std::ostringstream oss;
395 std::vector<ScreenHDRFormat> hdrFormats;
396 DMError ret = screenSession->GetSupportedHDRFormats(hdrFormats);
397 if (ret == DMError::DM_OK && hdrFormats.size() > 0) {
398 oss << std::left << std::setw(LINE_WIDTH) << "SupportedScreenHDRFormat: ";
399 for (uint32_t i = 0; i < hdrFormats.size() - 1; i++) {
400 oss << static_cast<int32_t>(hdrFormats[i]) << ", ";
401 }
402 oss << static_cast<int32_t>(hdrFormats[hdrFormats.size() - 1]) << std::endl;
403 }
404 ScreenHDRFormat hdrFormat;
405 ret = screenSession->GetScreenHDRFormat(hdrFormat);
406 if (ret == DMError::DM_OK) {
407 oss << std::left << std::setw(LINE_WIDTH) << "ScreenHDRFormat: "
408 << static_cast<int32_t>(hdrFormat) << std::endl;
409 }
410 std::vector<GraphicCM_ColorSpaceType> colorSpaces;
411 ret = screenSession->GetSupportedColorSpaces(colorSpaces);
412 if (ret == DMError::DM_OK && colorSpaces.size() > 0) {
413 oss << std::left << std::setw(LINE_WIDTH) << "SupportedColorSpaces: ";
414 for (uint32_t i = 0; i < colorSpaces.size() - 1; i++) {
415 oss << static_cast<int32_t>(colorSpaces[i]) << ", ";
416 }
417 oss << static_cast<int32_t>(colorSpaces[colorSpaces.size() - 1]) << std::endl;
418 }
419 GraphicCM_ColorSpaceType colorSpace;
420 ret = screenSession->GetScreenColorSpace(colorSpace);
421 if (ret == DMError::DM_OK) {
422 oss << std::left << std::setw(LINE_WIDTH) << "ScreenColorSpace: "
423 << static_cast<int32_t>(colorSpace) << std::endl;
424 }
425 dumpInfo_.append(oss.str());
426 }
427
DumpCutoutInfoById(ScreenId id)428 void ScreenSessionDumper::DumpCutoutInfoById(ScreenId id)
429 {
430 std::ostringstream oss;
431 oss << "[CUTOUT INFO]" << std::endl;
432 sptr<CutoutInfo> cutoutInfo = ScreenSessionManager::GetInstance().GetCutoutInfo(id);
433 if (cutoutInfo == nullptr) {
434 TLOGE(WmsLogTag::DMS, "cutoutInfo nullptr. screen id: %{public}" PRIu64"", id);
435 return;
436 }
437 oss << std::left << std::setw(LINE_WIDTH) << "WaterFall_L<X,Y,W,H>: "
438 << cutoutInfo->GetWaterfallDisplayAreaRects().left.posX_ << ", "
439 << cutoutInfo->GetWaterfallDisplayAreaRects().left.posY_ << ", "
440 << cutoutInfo->GetWaterfallDisplayAreaRects().left.width_ << ", "
441 << cutoutInfo->GetWaterfallDisplayAreaRects().left.height_ << std::endl;
442 oss << std::left << std::setw(LINE_WIDTH) << "WaterFall_T<X,Y,W,H>: "
443 << cutoutInfo->GetWaterfallDisplayAreaRects().top.posX_ << ", "
444 << cutoutInfo->GetWaterfallDisplayAreaRects().top.posY_ << ", "
445 << cutoutInfo->GetWaterfallDisplayAreaRects().top.width_ << ", "
446 << cutoutInfo->GetWaterfallDisplayAreaRects().top.height_ << std::endl;
447 oss << std::left << std::setw(LINE_WIDTH) << "WaterFall_R<X,Y,W,H>: "
448 << cutoutInfo->GetWaterfallDisplayAreaRects().right.posX_ << ", "
449 << cutoutInfo->GetWaterfallDisplayAreaRects().right.posY_ << ", "
450 << cutoutInfo->GetWaterfallDisplayAreaRects().right.width_ << ", "
451 << cutoutInfo->GetWaterfallDisplayAreaRects().right.height_ << std::endl;
452 oss << std::left << std::setw(LINE_WIDTH) << "WaterFall_B<X,Y,W,H>: "
453 << cutoutInfo->GetWaterfallDisplayAreaRects().bottom.posX_ << ", "
454 << cutoutInfo->GetWaterfallDisplayAreaRects().bottom.posY_ << ", "
455 << cutoutInfo->GetWaterfallDisplayAreaRects().bottom.width_ << ", "
456 << cutoutInfo->GetWaterfallDisplayAreaRects().bottom.height_ << std::endl;
457
458 std::vector<DMRect> boundingRects = cutoutInfo->GetBoundingRects();
459 oss << std::left << std::setw(LINE_WIDTH) << "BoundingRects<X,Y,W,H>: ";
460 for (auto rect : boundingRects) {
461 oss << "[" << rect.posX_ << ", " << rect.posY_ << ", " << rect.width_ << ", " << rect.height_ << "] ";
462 }
463 oss << std::endl;
464 dumpInfo_.append(oss.str());
465 }
466
DumpScreenInfoById(ScreenId id)467 void ScreenSessionDumper::DumpScreenInfoById(ScreenId id)
468 {
469 std::ostringstream oss;
470 oss << "[SCREEN INFO]" << std::endl;
471 auto screenInfo = ScreenSessionManager::GetInstance().GetScreenInfoById(id);
472 if (screenInfo == nullptr) {
473 TLOGE(WmsLogTag::DMS, "screenInfo nullptr. screen id: %{public}" PRIu64"", id);
474 return;
475 }
476 oss << std::left << std::setw(LINE_WIDTH) << "VirtualWidth: "
477 << screenInfo->GetVirtualWidth() << std::endl;
478 oss << std::left << std::setw(LINE_WIDTH) << "VirtualHeight: "
479 << screenInfo->GetVirtualHeight() << std::endl;
480 oss << std::left << std::setw(LINE_WIDTH) << "LastParentId: "
481 << screenInfo->GetLastParentId() << std::endl;
482 oss << std::left << std::setw(LINE_WIDTH) << "ParentId: "
483 << screenInfo->GetParentId() << std::endl;
484 oss << std::left << std::setw(LINE_WIDTH) << "IsScreenGroup: "
485 << screenInfo->GetIsScreenGroup() << std::endl;
486 oss << std::left << std::setw(LINE_WIDTH) << "VirtualPixelRatio: "
487 << screenInfo->GetVirtualPixelRatio() << std::endl;
488 oss << std::left << std::setw(LINE_WIDTH) << "Rotation: "
489 << static_cast<int32_t>(screenInfo->GetRotation()) << std::endl;
490 oss << std::left << std::setw(LINE_WIDTH) << "Orientation: "
491 << static_cast<int32_t>(screenInfo->GetOrientation()) << std::endl;
492 oss << std::left << std::setw(LINE_WIDTH) << "SourceMode: "
493 << static_cast<int32_t>(screenInfo->GetSourceMode()) << std::endl;
494 oss << std::left << std::setw(LINE_WIDTH) << "ScreenType: "
495 << static_cast<int32_t>(screenInfo->GetType()) << std::endl;
496 dumpInfo_.append(oss.str());
497 }
498
DumpScreenPropertyById(ScreenId id)499 void ScreenSessionDumper::DumpScreenPropertyById(ScreenId id)
500 {
501 std::ostringstream oss;
502 oss << "[SCREEN PROPERTY]" << std::endl;
503 ScreenProperty screenProperty = ScreenSessionManager::GetInstance().GetScreenProperty(id);
504
505 oss << std::left << std::setw(LINE_WIDTH) << "Rotation: " << screenProperty.GetRotation() << std::endl;
506 oss << std::left << std::setw(LINE_WIDTH) << "Density: " << screenProperty.GetDensity() << std::endl;
507 oss << std::left << std::setw(LINE_WIDTH) << "DensityInCurResolution: "
508 << screenProperty.GetDensityInCurResolution() << std::endl;
509 oss << std::left << std::setw(LINE_WIDTH) << "PhyWidth: " << screenProperty.GetPhyWidth() << std::endl;
510 oss << std::left << std::setw(LINE_WIDTH) << "PhyHeight: " << screenProperty.GetPhyHeight() << std::endl;
511 oss << std::left << std::setw(LINE_WIDTH) << "RefreshRate: " << screenProperty.GetRefreshRate() << std::endl;
512 oss << std::left << std::setw(LINE_WIDTH) << "VirtualPixelRatio: "
513 << screenProperty.GetVirtualPixelRatio() << std::endl;
514 oss << std::left << std::setw(LINE_WIDTH) << "ScreenRotation: "
515 << static_cast<int32_t>(screenProperty.GetRotation()) << std::endl;
516 oss << std::left << std::setw(LINE_WIDTH) << "Orientation: "
517 << static_cast<int32_t>(screenProperty.GetOrientation()) << std::endl;
518 oss << std::left << std::setw(LINE_WIDTH) << "DisplayOrientation: "
519 << static_cast<int32_t>(screenProperty.GetDisplayOrientation()) << std::endl;
520 oss << std::left << std::setw(LINE_WIDTH) << "GetScreenType: "
521 << static_cast<int32_t>(screenProperty.GetScreenType()) << std::endl;
522 oss << std::left << std::setw(LINE_WIDTH) << "ReqOrientation: "
523 << static_cast<int32_t>(screenProperty.GetScreenRequestedOrientation()) << std::endl;
524 oss << std::left << std::setw(LINE_WIDTH) << "DPI<X, Y>: " << screenProperty.GetXDpi()
525 << ", " << screenProperty.GetYDpi() << std::endl;
526 oss << std::left << std::setw(LINE_WIDTH) << "Offset<X, Y>: " << screenProperty.GetOffsetX()
527 << ", " << screenProperty.GetOffsetY() << std::endl;
528 oss << std::left << std::setw(LINE_WIDTH) << "Bounds<L,T,W,H>: "
529 << screenProperty.GetBounds().rect_.GetLeft() << ", "
530 << screenProperty.GetBounds().rect_.GetTop() << ", "
531 << screenProperty.GetBounds().rect_.GetWidth() << ", "
532 << screenProperty.GetBounds().rect_.GetHeight() << ", " << std::endl;
533 oss << std::left << std::setw(LINE_WIDTH) << "PhyBounds<L,T,W,H>: "
534 << screenProperty.GetPhyBounds().rect_.GetLeft() << ", "
535 << screenProperty.GetPhyBounds().rect_.GetTop() << ", "
536 << screenProperty.GetPhyBounds().rect_.GetWidth() << ", "
537 << screenProperty.GetPhyBounds().rect_.GetHeight() << ", " << std::endl;
538 oss << std::left << std::setw(LINE_WIDTH) << "AvailableArea<X,Y,W,H> "
539 << screenProperty.GetAvailableArea().posX_ << ", "
540 << screenProperty.GetAvailableArea().posY_ << ", "
541 << screenProperty.GetAvailableArea().width_ << ", "
542 << screenProperty.GetAvailableArea().height_ << ", " << std::endl;
543 oss << std::left << std::setw(LINE_WIDTH) << "DefaultDeviceRotationOffset "
544 << screenProperty.GetDefaultDeviceRotationOffset() << std::endl;
545 dumpInfo_.append(oss.str());
546 }
547
548 /*
549 * hidumper inject methods
550 */
ShowNotifyFoldStatusChangedInfo()551 void ScreenSessionDumper::ShowNotifyFoldStatusChangedInfo()
552 {
553 TLOGI(WmsLogTag::DMS, "params_: [%{public}s]", params_[0].c_str());
554 int errCode = ScreenSessionManager::GetInstance().NotifyFoldStatusChanged(params_[0]);
555 if (errCode != 0) {
556 ShowIllegalArgsInfo();
557 } else {
558 std::ostringstream oss;
559 oss << "currentFoldStatus is: "
560 << static_cast<uint32_t>(ScreenSessionManager::GetInstance().GetFoldStatus())
561 << std::endl;
562 dumpInfo_.append(oss.str());
563 }
564 }
565
ShowIllegalArgsInfo()566 void ScreenSessionDumper::ShowIllegalArgsInfo()
567 {
568 dumpInfo_.append("The arguments are illegal and you can enter '-h' for help.");
569 }
570
SetMotionSensorvalue(std::string input)571 void ScreenSessionDumper::SetMotionSensorvalue(std::string input)
572 {
573 size_t commaPos = input.find_last_of(',');
574 if ((commaPos != std::string::npos) && (input.substr(0, commaPos) == ARG_SET_ROTATION_SENSOR)) {
575 std::string valueStr = input.substr(commaPos + 1, MOTION_SENSOR_PARAM_SIZE);
576 if (valueStr.size() == 1 && !std::isdigit(valueStr[0])) {
577 return;
578 }
579 if (valueStr.size() == MOTION_SENSOR_PARAM_SIZE && valueStr != "-1") {
580 return;
581 }
582 int32_t value = static_cast<uint32_t>(std::stoi(valueStr));
583 if (value < static_cast<int32_t>(DeviceRotation::INVALID) ||
584 value > static_cast<int32_t>(DeviceRotation::ROTATION_LANDSCAPE_INVERTED)) {
585 TLOGE(WmsLogTag::DMS, "params is invalid: %{public}d", value);
586 return;
587 }
588 ScreenRotationProperty::HandleSensorEventInput(static_cast<DeviceRotation>(value));
589 TLOGI(WmsLogTag::DMS, "mock motion sensor: %{public}d", value);
590 }
591 }
592
SetRotationLockedvalue(std::string input)593 void ScreenSessionDumper::SetRotationLockedvalue(std::string input)
594 {
595 size_t commaPos = input.find_last_of(',');
596 if ((commaPos != std::string::npos) && (input.substr(0, commaPos) == ARG_SET_ROTATION_LOCK)) {
597 std::string valueStr = input.substr(commaPos + 1);
598 if (valueStr.size() != 1) {
599 return;
600 }
601 if (!std::isdigit(valueStr[0])) {
602 return;
603 }
604 int32_t value = std::stoi(valueStr);
605 ScreenSessionManager::GetInstance().SetScreenRotationLocked(static_cast<bool>(value));
606 TLOGI(WmsLogTag::DMS, "mock rotation locked: %{public}d", value);
607 }
608 }
609
IsValidDisplayModeCommand(std::string command)610 bool ScreenSessionDumper::IsValidDisplayModeCommand(std::string command)
611 {
612 if (std::find(displayModeCommands.begin(), displayModeCommands.end(), command) != displayModeCommands.end()) {
613 return true;
614 }
615 return false;
616 }
617
SetFoldDisplayMode()618 int ScreenSessionDumper::SetFoldDisplayMode()
619 {
620 std::string modeParam = params_[0];
621 if (modeParam.empty()) {
622 return -1;
623 }
624 FoldDisplayMode displayMode = FoldDisplayMode::UNKNOWN;
625 if (modeParam == ARG_FOLD_DISPLAY_FULL) {
626 displayMode = FoldDisplayMode::FULL;
627 } else if (modeParam == ARG_FOLD_DISPLAY_MAIN) {
628 displayMode = FoldDisplayMode::MAIN;
629 } else if (modeParam == ARG_FOLD_DISPLAY_SUB) {
630 displayMode = FoldDisplayMode::SUB;
631 } else if (modeParam == ARG_FOLD_DISPLAY_COOR) {
632 displayMode = FoldDisplayMode::COORDINATION;
633 } else {
634 TLOGW(WmsLogTag::DMS, "SetFoldDisplayMode mode not support");
635 return -1;
636 }
637 ScreenSessionManager::GetInstance().SetFoldDisplayMode(displayMode);
638 return 0;
639 }
640
SetFoldStatusLocked()641 int ScreenSessionDumper::SetFoldStatusLocked()
642 {
643 std::string lockParam = params_[0];
644 if (lockParam.empty()) {
645 return -1;
646 }
647 bool lockDisplayStatus = false;
648 if (lockParam == ARG_LOCK_FOLD_DISPLAY_STATUS) {
649 lockDisplayStatus = true;
650 } else if (lockParam == ARG_UNLOCK_FOLD_DISPLAY_STATUS) {
651 lockDisplayStatus = false;
652 } else {
653 TLOGW(WmsLogTag::DMS, "SetFoldStatusLocked status not support");
654 return -1;
655 }
656 ScreenSessionManager::GetInstance().SetFoldStatusLocked(lockDisplayStatus);
657 return 0;
658 }
659
SetEnterOrExitTentMode(std::string input)660 void ScreenSessionDumper::SetEnterOrExitTentMode(std::string input)
661 {
662 if (input == ARG_SET_ON_TENT_MODE) {
663 ScreenSessionManager::GetInstance().OnTentModeChanged(true);
664 } else if (input == ARG_SET_OFF_TENT_MODE) {
665 ScreenSessionManager::GetInstance().OnTentModeChanged(false);
666 }
667 }
668
SetHoverStatusChange(std::string input)669 void ScreenSessionDumper::SetHoverStatusChange(std::string input)
670 {
671 size_t commaPos = input.find_last_of(',');
672 auto screenSession = ScreenSessionManager::GetInstance().GetDefaultScreenSession();
673 if (screenSession == nullptr) {
674 TLOGE(WmsLogTag::DMS, "screenSession is nullptr");
675 return;
676 }
677 if ((commaPos != std::string::npos) && (input.substr(0, commaPos) == ARG_SET_HOVER_STATUS)) {
678 std::string valueStr = input.substr(commaPos + 1);
679 if (valueStr.size() != 1) {
680 dumpInfo_.append("[error]: the value is too long");
681 return;
682 }
683 if (!std::isdigit(valueStr[0])) {
684 dumpInfo_.append("[error]: value is not a number");
685 return;
686 }
687 int32_t value = std::stoi(valueStr);
688 if ((value < static_cast<int32_t>(DeviceHoverStatus::INVALID)) ||
689 (value > static_cast<int32_t>(DeviceHoverStatus::CAMERA_STATUS_CANCEL))) {
690 TLOGE(WmsLogTag::DMS, "params is invalid: %{public}d", value);
691 return;
692 }
693 screenSession->HoverStatusChange(value);
694 TLOGI(WmsLogTag::DMS, "SetHoverStatusChange: %{public}d", value);
695 }
696 }
697
SetHallAndPostureValue(std::string input)698 void ScreenSessionDumper::SetHallAndPostureValue(std::string input)
699 {
700 std::string token;
701 std::istringstream ss(input);
702 std::vector<std::string> tokens;
703 while (std::getline(ss, token, ',')) {
704 tokens.push_back(token);
705 }
706 if (tokens.size() != DUMPER_PARAM_INDEX_THREE && tokens[0] != ARG_SET_POSTURE_HALL) {
707 TLOGE(WmsLogTag::DMS, "param error: %{public}s", input.c_str());
708 return;
709 }
710 if (!IsNumber(tokens[DUMPER_PARAM_INDEX_ONE]) || !IsNumber(tokens[DUMPER_PARAM_INDEX_TWO])) {
711 TLOGE(WmsLogTag::DMS, "param error: %{public}s", input.c_str());
712 return;
713 }
714 int hallVal = stoi(tokens[DUMPER_PARAM_INDEX_ONE]);
715 int postureVal = stoi(tokens[DUMPER_PARAM_INDEX_TWO]);
716 FoldScreenSensorManager::ExtHallData hallData = {
717 .flag = (1 << 1),
718 .hall = hallVal,
719 };
720 PostureData postureData = {
721 .angle = postureVal,
722 };
723 SensorEvent hallEvent = {
724 .data = reinterpret_cast<uint8_t *>(&hallData),
725 .dataLen = sizeof(FoldScreenSensorManager::ExtHallData),
726 };
727 SensorEvent postureEvent = {
728 .data = reinterpret_cast<uint8_t *>(&postureData),
729 .dataLen = sizeof(PostureData),
730 };
731 FoldScreenSensorManager::GetInstance().HandleHallData(&hallEvent);
732 FoldScreenSensorManager::GetInstance().HandlePostureData(&postureEvent);
733 TLOGI(WmsLogTag::DMS, "mock posture: %{public}d, hall: %{public}d ", postureVal, hallVal);
734 }
735
SetHallAndPostureStatus(std::string input)736 void ScreenSessionDumper::SetHallAndPostureStatus(std::string input)
737 {
738 size_t commaPos = input.find_last_of(',');
739 if ((commaPos != std::string::npos) && (input.substr(0, commaPos) == ARG_SET_POSTURE_HALL_STATUS)) {
740 std::string valueStr = input.substr(commaPos + 1, DUMPER_PARAM_INDEX_ONE);
741 if (valueStr.size() != DUMPER_PARAM_INDEX_ONE && !std::isdigit(valueStr[0])) {
742 return;
743 }
744 int32_t value = std::stoi(valueStr);
745 if (value) {
746 FoldScreenSensorManager::GetInstance().RegisterHallCallback();
747 FoldScreenSensorManager::GetInstance().RegisterPostureCallback();
748 ScreenSensorConnector::SubscribeRotationSensor();
749 } else {
750 FoldScreenSensorManager::GetInstance().UnRegisterHallCallback();
751 FoldScreenSensorManager::GetInstance().UnRegisterPostureCallback();
752 ScreenSensorConnector::UnsubscribeRotationSensor();
753 }
754 TLOGI(WmsLogTag::DMS, "hall and posture register status: %{public}d", value);
755 }
756 }
757
SetSecondaryStatusChange(const std::string & input)758 void ScreenSessionDumper::SetSecondaryStatusChange(const std::string &input)
759 {
760 if (!FoldScreenStateInternel::IsSecondaryDisplayFoldDevice()) {
761 TLOGD(WmsLogTag::DMS, "not secondary device");
762 return;
763 }
764 TLOGI(WmsLogTag::DMS, "secondary input: %{public}s", input.c_str());
765 size_t commaPos = input.find(',');
766 if (!((commaPos != std::string::npos) && (input.substr(0, commaPos) == ARG_SET_SECONDARY_FOLD_STATUS))) {
767 return;
768 }
769 std::string valueStr = input.substr(commaPos + 1, 1);
770 if (valueStr.empty()) {
771 return;
772 }
773 char ch = valueStr[0];
774 TLOGI(WmsLogTag::DMS, "value: %{public}c", ch);
775 const char *end = SECONDARY_DUMPER_VALUE_BOUNDARY + sizeof(SECONDARY_DUMPER_VALUE_BOUNDARY); // mfg
776 const char *result = std::find(SECONDARY_DUMPER_VALUE_BOUNDARY, end, ch);
777 if (result == end) {
778 if (ch == 'p') { // sensor
779 TriggerSecondarySensor(input.substr(commaPos + 1, input.size() - commaPos - 1));
780 } else if (ch == 'z') { // foldstatus
781 TriggerSecondaryFoldStatus(input.substr(commaPos + 1, input.size() - commaPos - 1));
782 } else {
783 TLOGE(WmsLogTag::DMS, "command not supported.");
784 }
785 return;
786 }
787
788 FoldDisplayMode displayMode = FoldDisplayMode::UNKNOWN;
789 if (ch == SECONDARY_DUMPER_VALUE_BOUNDARY[SECONDARY_FOLD_STATUS_INDEX_M]) {
790 displayMode = FoldDisplayMode::FULL;
791 } else if (ch == SECONDARY_DUMPER_VALUE_BOUNDARY[SECONDARY_FOLD_STATUS_INDEX_F]) {
792 displayMode = FoldDisplayMode::MAIN;
793 } else if (ch == SECONDARY_DUMPER_VALUE_BOUNDARY[SECONDARY_FOLD_STATUS_INDEX_G]) {
794 displayMode = FoldDisplayMode::GLOBAL_FULL;
795 } else {
796 TLOGW(WmsLogTag::DMS, "SetFoldDisplayMode mode is not supported.");
797 return;
798 }
799 ScreenSessionManager::GetInstance().SetFoldDisplayMode(displayMode);
800 }
801
IsAllCharDigit(const std::string & firstPostureStr)802 bool ScreenSessionDumper::IsAllCharDigit(const std::string &firstPostureStr)
803 {
804 for (size_t i = 0; i < firstPostureStr.size(); i++) {
805 if (!std::isdigit(firstPostureStr[i])) {
806 TLOGW(WmsLogTag::DMS, "%{public}s is not number", firstPostureStr.c_str());
807 return false;
808 }
809 }
810 return true;
811 }
812
GetPostureAndHall(std::vector<std::string> strVec,std::vector<float> & postures,std::vector<uint16_t> & halls)813 bool ScreenSessionDumper::GetPostureAndHall(std::vector<std::string> strVec,
814 std::vector<float> &postures, std::vector<uint16_t> &halls)
815 {
816 for (std::string str : strVec) {
817 size_t index = str.find(":");
818 if (index == std::string::npos) {
819 return false;
820 }
821 std::string key = str.substr(0, index);
822 std::string value = str.substr(index + 1, str.size() - index - 1);
823 index = value.find(",");
824 if (index == std::string::npos) {
825 return false;
826 }
827 std::vector<std::string> posturesStrList = WindowHelper::Split(value, ",");
828 std::string firstPostureStr;
829 std::string secondPostureStr;
830 std::string thirdPostureStr;
831 if (posturesStrList.size() == DUMPER_PARAM_INDEX_THREE) {
832 firstPostureStr = posturesStrList[0];
833 secondPostureStr = posturesStrList[1];
834 thirdPostureStr = posturesStrList[DUMPER_PARAM_INDEX_TWO];
835 if (!WindowHelper::IsFloatingNumber(firstPostureStr) ||
836 !WindowHelper::IsFloatingNumber(secondPostureStr)) {
837 TLOGW(WmsLogTag::DMS, "posture should be a float number");
838 return false;
839 }
840 if (thirdPostureStr != "0" && thirdPostureStr != "1") {
841 TLOGW(WmsLogTag::DMS, "third posture is not 0 or 1");
842 return false;
843 }
844 postures.emplace_back(std::stof(firstPostureStr));
845 postures.emplace_back(std::stof(secondPostureStr));
846 postures.emplace_back(std::stof(thirdPostureStr));
847 } else if (posturesStrList.size() == DUMPER_PARAM_INDEX_TWO) {
848 firstPostureStr = posturesStrList[0];
849 secondPostureStr = posturesStrList[1];
850 if (!IsAllCharDigit(firstPostureStr) || !IsAllCharDigit(secondPostureStr)) {
851 return false;
852 }
853 halls.emplace_back(static_cast<uint16_t>(std::stoi(firstPostureStr)));
854 halls.emplace_back(static_cast<uint16_t>(std::stoi(secondPostureStr)));
855 } else {
856 TLOGW(WmsLogTag::DMS, "sensor command error");
857 return false;
858 }
859 }
860 return true;
861 }
862
TriggerSecondarySensor(const std::string & valueStr)863 void ScreenSessionDumper::TriggerSecondarySensor(const std::string &valueStr)
864 {
865 TLOGI(WmsLogTag::DMS, "%{public}s", valueStr.c_str());
866 std::vector<std::string> strVec = WindowHelper::Split(valueStr, "/");
867 std::vector<float> postures;
868 std::vector<uint16_t> halls;
869 if (!GetPostureAndHall(strVec, postures, halls)) {
870 TLOGW(WmsLogTag::DMS, "GetPostureAndHall failed");
871 return;
872 }
873 FoldScreenSensorManager::ExtHallData hallData = {
874 .flag = HALL_EXT_DATA_FLAG,
875 .hall = halls[0],
876 .hallAb = halls[1],
877 };
878 FoldScreenSensorManager::PostureDataSecondary postureData = {
879 .postureBc = postures[0],
880 .postureAb = postures[1],
881 .postureAbAnti = postures[DUMPER_PARAM_INDEX_TWO],
882 };
883 SensorEvent hallEvent = {
884 .data = reinterpret_cast<uint8_t *>(&hallData),
885 .dataLen = sizeof(FoldScreenSensorManager::ExtHallData),
886 };
887 SensorEvent postureEvent = {
888 .data = reinterpret_cast<uint8_t *>(&postureData),
889 .dataLen = sizeof(FoldScreenSensorManager::PostureDataSecondary),
890 };
891 TLOGI(WmsLogTag::DMS, "mock secondary sensor: %{public}s, %{public}s",
892 FoldScreenStateInternel::TransVec2Str(postures, "angle").c_str(),
893 FoldScreenStateInternel::TransVec2Str(halls, "hall").c_str());
894 SecondaryFoldSensorManager::GetInstance().HandleHallDataExt(&hallEvent);
895 SecondaryFoldSensorManager::GetInstance().HandlePostureData(&postureEvent);
896 }
897
TriggerSecondaryFoldStatus(const std::string & valueStr)898 void ScreenSessionDumper::TriggerSecondaryFoldStatus(const std::string &valueStr)
899 {
900 std::vector<std::string> strVec = WindowHelper::Split(valueStr, "=");
901 if (strVec.size() != SECONDARY_FOLD_STATUS_COMMAND_NUM) {
902 TLOGW(WmsLogTag::DMS, "secondary foldstatus command miss '=' or has extra '='.");
903 return;
904 }
905 const std::string &foldStatusStr = strVec[1];
906 for (size_t i = 0; i < foldStatusStr.size(); i++) {
907 if (!std::isdigit(foldStatusStr[i])) {
908 TLOGW(WmsLogTag::DMS, "secondary foldstatus command contains characters that are not numbers.");
909 return;
910 }
911 }
912 uint32_t foldStatus = static_cast<uint32_t>(std::stoi(foldStatusStr));
913 switch (foldStatus) {
914 case static_cast<uint32_t>(FoldStatus::EXPAND) :
915 case static_cast<uint32_t>(FoldStatus::FOLDED) :
916 case static_cast<uint32_t>(FoldStatus::HALF_FOLD) :
917 case static_cast<uint32_t>(FoldStatus::FOLD_STATE_EXPAND_WITH_SECOND_EXPAND) :
918 case static_cast<uint32_t>(FoldStatus::FOLD_STATE_EXPAND_WITH_SECOND_HALF_FOLDED) :
919 case static_cast<uint32_t>(FoldStatus::FOLD_STATE_FOLDED_WITH_SECOND_EXPAND) :
920 case static_cast<uint32_t>(FoldStatus::FOLD_STATE_FOLDED_WITH_SECOND_HALF_FOLDED) :
921 case static_cast<uint32_t>(FoldStatus::FOLD_STATE_HALF_FOLDED_WITH_SECOND_EXPAND) :
922 case static_cast<uint32_t>(FoldStatus::FOLD_STATE_HALF_FOLDED_WITH_SECOND_HALF_FOLDED) : {
923 break;
924 }
925 default: {
926 TLOGW(WmsLogTag::DMS, "secondary foldstatus is out of range.");
927 return;
928 }
929 }
930 TLOGI(WmsLogTag::DMS, "change fold status, %{public}s", foldStatusStr.c_str());
931 ScreenSessionManager::GetInstance().TriggerFoldStatusChange(static_cast<FoldStatus>(foldStatus));
932 }
933 } // Rosen
934 } // OHOS