• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_shell_command.h"
17 
18 #include <cerrno>
19 #include <getopt.h>
20 #include <string_ex.h>
21 
22 #include "power_mgr_client.h"
23 #ifdef HAS_DISPLAY_MANAGER_PART
24 #include "display_power_mgr_client.h"
25 #endif
26 
27 extern char *optarg;
28 
29 namespace OHOS {
30 namespace PowerMgr {
31 
32 static const struct option SET_MODE_OPTIONS[] = {
33     {"help", no_argument, nullptr, 'h'},
34 };
35 
36 #ifdef HAS_DISPLAY_MANAGER_PART
37 static const struct option DISPLAY_OPTIONS[] = {
38     {"help", no_argument, nullptr, 'h'},
39     {"restore", no_argument, nullptr, 'r'},
40     {"set", required_argument, nullptr, 's'},
41     {"override", required_argument, nullptr, 'o'},
42     {"boost", required_argument, nullptr, 'b'},
43     {"cancel", no_argument, nullptr, 'c'},
44     {"discount", required_argument, nullptr, 'd'},
45 };
46 #endif
47 
48 static const struct option TIME_OUT_OPTIONS[] = {
49     {"help", no_argument, nullptr, 'h'},
50     {"restore", no_argument, nullptr, 'r'},
51     {"override", required_argument, nullptr, 'o'},
52 };
53 
54 static const std::string HELP_MSG =
55     "usage: power-shell\n"
56     "command list:\n"
57     "  setmode :    Set power mode. \n"
58     "  wakeup  :    Wakeup system and turn screen on. \n"
59     "  suspend :    Suspend system and turn screen off. \n"
60 #ifdef HAS_DISPLAY_MANAGER_PART
61     "  display :    Update or Override display brightness. \n"
62 #endif
63     "  timeout :    Override or Restore screen off time. \n"
64     "  dump    :    Dump power info. \n"
65     "  help    :    Show this help menu. \n";
66 
67 static const std::string SETMODE_HELP_MSG =
68     "usage: power-shell setmode [<options>]\n"
69     "setmode <power mode: (value is as below)> \n"
70     "  600  :  normal mode\n"
71     "  601  :  power save mode\n"
72     "  602  :  performance mode\n"
73     "  603  :  extreme power save mode\n";
74 
75 #ifdef HAS_DISPLAY_MANAGER_PART
76 static const std::string DISPLAY_HELP_MSG =
77     "usage: power-shell display [<options>] 100\n"
78     "display <options are as below> \n"
79     "  -h  :  display help\n"
80     "  -r  :  retore brightness\n"
81     "  -s  :  set brightness\n"
82     "  -o  :  override brightness\n"
83     "  -b  :  timing maximum brightness\n"
84     "  -c  :  cancel the timing maximum brightness\n"
85     "  -d  :  discount brightness\n";
86 #endif
87 
88 static const std::string TIME_OUT_HELP_MSG =
89     "usage: power-shell timeout [<options>] 1000\n"
90     "timeout <options are as below> \n"
91     "  -o  :  override screen off time\n"
92     "  -r  :  restore screen off time\n";
93 
PowerShellCommand(int argc,char * argv[])94 PowerShellCommand::PowerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "power-shell")
95 {}
96 
CreateCommandMap()97 ErrCode PowerShellCommand::CreateCommandMap()
98 {
99     commandMap_ = {
100         {"help", std::bind(&PowerShellCommand::RunAsHelpCommand, this)},
101         {"setmode", std::bind(&PowerShellCommand::RunAsSetModeCommand, this)},
102         {"wakeup", std::bind(&PowerShellCommand::RunAsWakeupCommand, this)},
103         {"suspend", std::bind(&PowerShellCommand::RunAsSuspendCommand, this)},
104 #ifdef HAS_DISPLAY_MANAGER_PART
105         {"display", std::bind(&PowerShellCommand::RunAsDisplayCommand, this)},
106 #endif
107         {"timeout", std::bind(&PowerShellCommand::RunAsTimeOutCommand, this)},
108         {"dump", std::bind(&PowerShellCommand::RunAsDumpCommand, this)},
109     };
110 
111     return ERR_OK;
112 }
113 
CreateMessageMap()114 ErrCode PowerShellCommand::CreateMessageMap()
115 {
116     messageMap_ = {};
117 
118     return ERR_OK;
119 }
120 
init()121 ErrCode PowerShellCommand::init()
122 {
123     return OHOS::ERR_OK;
124 }
125 
RunAsHelpCommand()126 ErrCode PowerShellCommand::RunAsHelpCommand()
127 {
128     resultReceiver_.clear();
129     resultReceiver_.append(HELP_MSG);
130     return ERR_OK;
131 }
132 
RunAsSetModeCommand()133 ErrCode PowerShellCommand::RunAsSetModeCommand()
134 {
135     int ind = 0;
136     int option = getopt_long(argc_, argv_, "h", SET_MODE_OPTIONS, &ind);
137     resultReceiver_.clear();
138     if (option == 'h') {
139         resultReceiver_.append(SETMODE_HELP_MSG);
140         return ERR_OK;
141     }
142     if (argList_.empty()) {
143         resultReceiver_.append("Error! please input your mode value. \n");
144         resultReceiver_.append(SETMODE_HELP_MSG);
145         return ERR_OK;
146     }
147 
148     auto mode = static_cast<uint32_t>(strtol(argList_[0].c_str(), nullptr, 0));
149     resultReceiver_.append("Set Mode: ");
150     resultReceiver_.append(argList_[0]);
151     resultReceiver_.append("\n");
152     PowerMgrClient& client = PowerMgrClient::GetInstance();
153     client.SetDeviceMode(static_cast<PowerMode>(mode));
154     uint32_t result = static_cast<uint32_t>(client.GetDeviceMode());
155     if (result == mode) {
156         resultReceiver_.append("Set Mode Success!\n");
157     } else {
158         resultReceiver_.append("Set Mode Failed, current mode is: ");
159         resultReceiver_.append(std::to_string(result));
160         resultReceiver_.append("\n");
161     }
162 
163     return ERR_OK;
164 }
165 
RunAsWakeupCommand()166 ErrCode PowerShellCommand::RunAsWakeupCommand()
167 {
168     PowerMgrClient& client = PowerMgrClient::GetInstance();
169     std::string detail = "shell";
170     client.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, detail);
171     resultReceiver_.append("WakeupDevice is called\n");
172     return ERR_OK;
173 }
174 
RunAsSuspendCommand()175 ErrCode PowerShellCommand::RunAsSuspendCommand()
176 {
177     PowerMgrClient& client = PowerMgrClient::GetInstance();
178     client.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON);
179     resultReceiver_.append("SuspendDevice is called\n");
180     return ERR_OK;
181 }
182 
PrintDumpFileError(std::string & receiver,const char * path)183 extern "C" void PrintDumpFileError(std::string& receiver, const char* path)
184 {
185     receiver.append("Open Dump file (");
186     receiver.append(path);
187     receiver.append(") failed: ");
188     receiver.append(std::to_string(errno));
189     receiver.append("\n");
190 }
191 
RunAsDumpCommand()192 ErrCode PowerShellCommand::RunAsDumpCommand()
193 {
194     resultReceiver_.clear();
195 
196     PowerMgrClient& client = PowerMgrClient::GetInstance();
197     std::string ret = client.Dump(argList_);
198     resultReceiver_.append("Power Dump result: \n");
199     resultReceiver_.append(ret);
200 
201     return ERR_OK;
202 }
203 
204 #ifdef HAS_DISPLAY_MANAGER_PART
205 using namespace OHOS::DisplayPowerMgr;
RunAsDisplayCommand()206 ErrCode PowerShellCommand::RunAsDisplayCommand()
207 {
208     int ind = 0;
209     int option = getopt_long(argc_, argv_, "hrcs:o:b:d:", DISPLAY_OPTIONS, &ind);
210     resultReceiver_.clear();
211     if (option == 'h') {
212         resultReceiver_.append(DISPLAY_HELP_MSG);
213         return ERR_OK;
214     }
215     if (option == 'r') {
216         bool ret = DisplayPowerMgrClient::GetInstance().RestoreBrightness();
217         resultReceiver_.append("Restore brightness");
218         if (!ret) {
219             resultReceiver_.append(" failed");
220         }
221         resultReceiver_.append("\n");
222         return ERR_OK;
223     }
224     if (option == 'c') {
225         bool ret = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness();
226         resultReceiver_.append("Cancel boost brightness");
227         if (!ret) {
228             resultReceiver_.append(" failed");
229         }
230         resultReceiver_.append("\n");
231         return ERR_OK;
232     }
233     if (!optarg) {
234         resultReceiver_.append("Error! please input your brightness value.\n");
235         resultReceiver_.append(DISPLAY_HELP_MSG);
236         return ERR_OK;
237     }
238     if (option == 's') {
239         auto value = static_cast<uint32_t>(atoi(optarg));
240         bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(value);
241         resultReceiver_.append("Set brightness to ");
242         resultReceiver_.append(std::to_string(value));
243         if (!ret) {
244             resultReceiver_.append(" failed");
245         }
246         resultReceiver_.append("\n");
247         return ERR_OK;
248     }
249     if (option == 'o') {
250         auto value = static_cast<uint32_t>(atoi(optarg));
251         bool ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(value);
252         resultReceiver_.append("Override brightness to ");
253         resultReceiver_.append(std::to_string(value));
254         if (!ret) {
255             resultReceiver_.append(" failed");
256         }
257         resultReceiver_.append("\n");
258         return ERR_OK;
259     }
260     if (option == 'b') {
261         auto value = static_cast<uint32_t>(atoi(optarg));
262         bool ret = DisplayPowerMgrClient::GetInstance().BoostBrightness(value);
263         resultReceiver_.append("Boost brightness timeout ");
264         resultReceiver_.append(std::to_string(value)).append("ms");
265         if (!ret) {
266             resultReceiver_.append(" failed");
267         }
268         resultReceiver_.append("\n");
269         return ERR_OK;
270     }
271     if (option == 'd') {
272         auto discount = static_cast<double>(atof(optarg));
273         bool ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(discount);
274         resultReceiver_.append("Set brightness discount to ");
275         resultReceiver_.append(std::to_string(discount));
276         if (!ret) {
277             resultReceiver_.append(" failed");
278         }
279         resultReceiver_.append("\n");
280         return ERR_OK;
281     }
282     return ERR_OK;
283 }
284 #endif
285 
RunAsTimeOutCommand()286 ErrCode PowerShellCommand::RunAsTimeOutCommand()
287 {
288     int ind = 0;
289     int option = getopt_long(argc_, argv_, "hro:", TIME_OUT_OPTIONS, &ind);
290     resultReceiver_.clear();
291     if (option == 'h') {
292         resultReceiver_.append(TIME_OUT_HELP_MSG);
293         return ERR_OK;
294     }
295     if (option == 'r') {
296         bool ret = PowerMgrClient::GetInstance().RestoreScreenOffTime();
297         resultReceiver_.append("Restore screen off time");
298         if (!ret) {
299             resultReceiver_.append(" failed");
300         }
301         resultReceiver_.append("\n");
302         return ERR_OK;
303     }
304     if (!optarg) {
305         resultReceiver_.append("Error! please input your screen off time.\n");
306         resultReceiver_.append(TIME_OUT_HELP_MSG);
307         return ERR_OK;
308     }
309     auto timeout = static_cast<int64_t>(atoi(optarg));
310     if (option == 'o') {
311         bool ret = PowerMgrClient::GetInstance().OverrideScreenOffTime(timeout);
312         resultReceiver_.append("Override screen off time to ");
313         resultReceiver_.append(std::to_string(timeout));
314         if (!ret) {
315             resultReceiver_.append(" failed");
316         }
317         resultReceiver_.append("\n");
318         return ERR_OK;
319     }
320     return ERR_OK;
321 }
322 } // namespace PowerMgr
323 } // namespace OHOS
324