1 /*
2  * Copyright (c) 2021-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 "power_mgr_dumper.h"
17 
18 #include "system_suspend_controller.h"
19 
20 namespace OHOS {
21 namespace PowerMgr {
22 namespace {
23 const std::string ARGS_ALL = "-a";
24 const std::string ARGS_HELP = "-h";
25 const std::string ARGS_RUNNINGLOCK = "-r";
26 const std::string ARGS_STATE = "-s";
27 const std::string ARGS_DIALOG = "-d";
28 const std::string ARGS_REG_KEY = "-k";
29 const std::string ARGS_On = "-t";
30 const std::string ARGS_Off = "-f";
31 }
32 
Dump(const std::vector<std::string> & args,std::string & result)33 bool PowerMgrDumper::Dump(const std::vector<std::string>& args, std::string& result)
34 {
35     result.clear();
36     auto argc = args.size();
37     if ((argc == 0) || (args[0] == ARGS_HELP)) {
38         ShowUsage(result);
39         return true;
40     }
41     auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
42     if (pms == nullptr) {
43         return true;
44     }
45     if (DumpArg(pms, args[0])) {
46         return true;
47     }
48     for (auto it = args.begin(); it != args.end(); it++) {
49         if (*it == ARGS_RUNNINGLOCK) {
50             auto runningLockMgr = pms->GetRunningLockMgr();
51             if (runningLockMgr == nullptr) {
52                 continue;
53             }
54             runningLockMgr->DumpInfo(result);
55         } else if (*it == ARGS_STATE) {
56             auto stateMachine = pms->GetPowerStateMachine();
57             if (stateMachine == nullptr) {
58                 continue;
59             }
60             stateMachine->DumpInfo(result);
61         } else if (*it == ARGS_ALL) {
62             result.clear();
63             auto stateMachine = pms->GetPowerStateMachine();
64             if (stateMachine == nullptr) {
65                 continue;
66             }
67             stateMachine->DumpInfo(result);
68             auto runningLockMgr = pms->GetRunningLockMgr();
69             if (runningLockMgr == nullptr) {
70                 continue;
71             }
72             runningLockMgr->DumpInfo(result);
73             break;
74         }
75     }
76     return true;
77 }
78 
DumpArg(const sptr<PowerMgrService> & pms,const std::string & arg)79 bool PowerMgrDumper::DumpArg(const sptr<PowerMgrService>& pms, const std::string& arg)
80 {
81     if (arg == ARGS_DIALOG) {
82         pms->GetShutdownDialog().ConnectSystemUi();
83         return true;
84     }
85     if (arg == ARGS_REG_KEY) {
86         pms->GetShutdownDialog().KeyMonitorInit();
87         return true;
88     }
89     if (arg == ARGS_On) {
90         pms->KeepScreenOn(true);
91         return true;
92     }
93     if (arg == ARGS_Off) {
94         pms->KeepScreenOn(false);
95         return true;
96     }
97     return false;
98 }
99 
ShowUsage(std::string & result)100 void PowerMgrDumper::ShowUsage(std::string& result)
101 {
102     result.append("Power manager dump options:\n")
103         .append("  [-h] [-runninglock]\n")
104         .append("  description of the cmd option:\n")
105         .append("    -a: show dump info of all power modules.\n")
106         .append("    -h: show this help.\n")
107         .append("    -r: show the information of runninglock.\n")
108         .append("    -s: show the information of power state machine.\n")
109         .append("    -d: show power off dialog.\n");
110 }
111 } // namespace PowerMgr
112 } // namespace OHOS
113