1 /*
2 * Copyright (c) 2021-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 "power_shell_command.h"
17
18 #include <cerrno>
19 #include <getopt.h>
20 #include <string_ex.h>
21 #include <sstream>
22
23 #include "power_mgr_client.h"
24 #ifdef HAS_DISPLAY_MANAGER_PART
25 #include "display_power_mgr_client.h"
26 #endif
27
28 extern char *optarg;
29
30 namespace OHOS {
31 namespace PowerMgr {
32
33 static const struct option SET_MODE_OPTIONS[] = {
34 {"help", no_argument, nullptr, 'h'},
35 };
36
37 #ifndef POWER_SHELL_USER
38 #ifdef HAS_DISPLAY_MANAGER_PART
39 static const struct option DISPLAY_OPTIONS[] = {
40 {"help", no_argument, nullptr, 'h'},
41 {"restore", no_argument, nullptr, 'r'},
42 {"set", required_argument, nullptr, 's'},
43 {"override", required_argument, nullptr, 'o'},
44 {"boost", required_argument, nullptr, 'b'},
45 {"cancel", no_argument, nullptr, 'c'},
46 {"discount", required_argument, nullptr, 'd'},
47 {"override delay", required_argument, nullptr, 'e'},
48 };
49 #endif
50 #endif
51
52 static const struct option TIME_OUT_OPTIONS[] = {
53 {"help", no_argument, nullptr, 'h'},
54 {"restore", no_argument, nullptr, 'r'},
55 {"override", required_argument, nullptr, 'o'},
56 };
57
58 static const std::string HELP_MSG =
59 "usage: power-shell\n"
60 "command list:\n"
61 " setmode : Set power mode. \n"
62 " wakeup : Wakeup system and turn screen on. \n"
63 " suspend : Suspend system and turn screen off. \n"
64 #ifndef POWER_SHELL_USER
65 " lock : Query running lock lists by bundle app. \n"
66 #ifdef HAS_DISPLAY_MANAGER_PART
67 " display : Update or Override display brightness. \n"
68 #endif
69 " dump : Dump power info. \n"
70 #endif
71 " timeout : Override or Restore screen off time. \n"
72 " help : Show this help menu. \n";
73
74 static const std::string SETMODE_HELP_MSG =
75 "usage: power-shell setmode [<options>]\n"
76 "setmode <power mode: (value is as below)> \n"
77 " 600 : normal mode\n"
78 " 601 : power save mode\n"
79 " 602 : performance mode\n"
80 " 603 : extreme power save mode\n";
81
82 #ifndef POWER_SHELL_USER
83 #ifdef HAS_DISPLAY_MANAGER_PART
84 static const std::string DISPLAY_HELP_MSG =
85 "usage: power-shell display [<options>] 100\n"
86 "display <options are as below> \n"
87 " -h : display help\n"
88 " -r : retore brightness\n"
89 " -s : set brightness\n"
90 " -o : override brightness\n"
91 " -b : timing maximum brightness\n"
92 " -c : cancel the timing maximum brightness\n"
93 " -d : discount brightness\n"
94 " -e : set screenoff delay\n";
95 #endif
96 #endif
97
98 static const std::string TIME_OUT_HELP_MSG =
99 "usage: power-shell timeout [<options>] 1000\n"
100 "timeout <options are as below> \n"
101 " -o : override screen off time\n"
102 " -r : restore screen off time\n";
103
PowerShellCommand(int argc,char * argv[])104 PowerShellCommand::PowerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "power-shell")
105 {}
106
CreateCommandMap()107 ErrCode PowerShellCommand::CreateCommandMap()
108 {
109 commandMap_ = {
110 {"help", std::bind(&PowerShellCommand::RunAsHelpCommand, this)},
111 {"setmode", std::bind(&PowerShellCommand::RunAsSetModeCommand, this)},
112 {"wakeup", std::bind(&PowerShellCommand::RunAsWakeupCommand, this)},
113 {"suspend", std::bind(&PowerShellCommand::RunAsSuspendCommand, this)},
114 #ifndef POWER_SHELL_USER
115 {"lock", std::bind(&PowerShellCommand::RunAsQueryLockCommand, this)},
116 #ifdef HAS_DISPLAY_MANAGER_PART
117 {"display", std::bind(&PowerShellCommand::RunAsDisplayCommand, this)},
118 #endif
119 {"dump", std::bind(&PowerShellCommand::RunAsDumpCommand, this)},
120 #endif
121 {"timeout", std::bind(&PowerShellCommand::RunAsTimeOutCommand, this)},
122 };
123
124 #ifndef POWER_SHELL_USER
125 #ifdef HAS_DISPLAY_MANAGER_PART
126 commandDisplay_ = {
127 {'h', std::bind(&PowerShellCommand::RunAsDisplayCommandHelp, this)},
128 {'r', std::bind(&PowerShellCommand::RunAsDisplayCommandRestore, this)},
129 {'s', std::bind(&PowerShellCommand::RunAsDisplayCommandSetValue, this)},
130 {'o', std::bind(&PowerShellCommand::RunAsDisplayCommandOverride, this)},
131 {'b', std::bind(&PowerShellCommand::RunAsDisplayCommandBoost, this)},
132 {'c', std::bind(&PowerShellCommand::RunAsDisplayCommandCancelBoost, this)},
133 {'d', std::bind(&PowerShellCommand::RunAsDisplayCommandDiscount, this)},
134 {'e', std::bind(&PowerShellCommand::RunAsDisplayCommandDelay, this)},
135 };
136 #endif
137 #endif
138
139 return ERR_OK;
140 }
141
CreateMessageMap()142 ErrCode PowerShellCommand::CreateMessageMap()
143 {
144 messageMap_ = {};
145
146 return ERR_OK;
147 }
148
init()149 ErrCode PowerShellCommand::init()
150 {
151 return OHOS::ERR_OK;
152 }
153
RunAsHelpCommand()154 ErrCode PowerShellCommand::RunAsHelpCommand()
155 {
156 resultReceiver_.clear();
157 resultReceiver_.append(HELP_MSG);
158 return ERR_OK;
159 }
160
RunAsSetModeCommand()161 ErrCode PowerShellCommand::RunAsSetModeCommand()
162 {
163 int ind = 0;
164 int option = getopt_long(argc_, argv_, "h", SET_MODE_OPTIONS, &ind);
165 resultReceiver_.clear();
166 if (option == 'h') {
167 resultReceiver_.append(SETMODE_HELP_MSG);
168 return ERR_OK;
169 }
170 if (argList_.empty()) {
171 resultReceiver_.append("Error! please input your mode value. \n");
172 resultReceiver_.append(SETMODE_HELP_MSG);
173 return ERR_OK;
174 }
175
176 auto mode = static_cast<uint32_t>(strtol(argList_[0].c_str(), nullptr, 0));
177 resultReceiver_.append("Set Mode: ");
178 resultReceiver_.append(argList_[0]);
179 resultReceiver_.append("\n");
180 PowerMgrClient& client = PowerMgrClient::GetInstance();
181 client.SetDeviceMode(static_cast<PowerMode>(mode));
182 uint32_t result = static_cast<uint32_t>(client.GetDeviceMode());
183 if (result == mode) {
184 resultReceiver_.append("Set Mode Success!\n");
185 } else {
186 resultReceiver_.append("Set Mode Failed, current mode is: ");
187 resultReceiver_.append(std::to_string(result));
188 resultReceiver_.append("\n");
189 }
190
191 return ERR_OK;
192 }
193
RunAsWakeupCommand()194 ErrCode PowerShellCommand::RunAsWakeupCommand()
195 {
196 PowerMgrClient& client = PowerMgrClient::GetInstance();
197 std::string detail = "shell";
198 client.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, detail);
199 resultReceiver_.append("WakeupDevice is called\n");
200 return ERR_OK;
201 }
202
RunAsSuspendCommand()203 ErrCode PowerShellCommand::RunAsSuspendCommand()
204 {
205 PowerMgrClient& client = PowerMgrClient::GetInstance();
206 client.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY);
207 resultReceiver_.append("SuspendDevice is called\n");
208 return ERR_OK;
209 }
210
211 #ifndef POWER_SHELL_USER
GetBundleRunningLockTypeString(RunningLockType type)212 static const std::string GetBundleRunningLockTypeString(RunningLockType type)
213 {
214 switch (type) {
215 case RunningLockType::RUNNINGLOCK_SCREEN:
216 return "SCREEN";
217 case RunningLockType::RUNNINGLOCK_BACKGROUND:
218 return "BACKGROUND";
219 case RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL:
220 return "PROXIMITY_SCREEN_CONTROL";
221 case RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE:
222 return "BACKGROUND_PHONE";
223 case RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION:
224 return "BACKGROUND_NOTIFICATION";
225 case RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO:
226 return "BACKGROUND_AUDIO";
227 case RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT:
228 return "BACKGROUND_SPORT";
229 case RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION:
230 return "BACKGROUND_NAVIGATION";
231 case RunningLockType::RUNNINGLOCK_BACKGROUND_TASK:
232 return "BACKGROUND_TASK";
233 case RunningLockType::RUNNINGLOCK_BUTT:
234 return "BUTT";
235 default:
236 break;
237 }
238
239 return "UNKNOWN";
240 }
241
RunAsQueryLockCommand()242 ErrCode PowerShellCommand::RunAsQueryLockCommand()
243 {
244 PowerMgrClient& client = PowerMgrClient::GetInstance();
245 std::map<std::string, RunningLockInfo> runningLockLists;
246 bool ret = client.QueryRunningLockLists(runningLockLists);
247 if (!ret) {
248 resultReceiver_.append("failed.\n");
249 return ERR_OK;
250 }
251 resultReceiver_.append("The locking application information is as follows:\n");
252 int mapSize = runningLockLists.size();
253 resultReceiver_.append("The nums of holding lock by bundle app is ");
254 resultReceiver_.append(std::to_string(mapSize));
255 resultReceiver_.append(".\n");
256 int counter = 0;
257 for (auto it : runningLockLists) {
258 counter++;
259 resultReceiver_.append(std::to_string(counter));
260 resultReceiver_.append(". bundleName=");
261 resultReceiver_.append(it.first);
262 resultReceiver_.append(" name=");
263 resultReceiver_.append(it.second.name);
264 resultReceiver_.append(" type=");
265 resultReceiver_.append(GetBundleRunningLockTypeString(it.second.type));
266 resultReceiver_.append(" pid=");
267 resultReceiver_.append(std::to_string(it.second.pid));
268 resultReceiver_.append(" uid=");
269 resultReceiver_.append(std::to_string(it.second.uid));
270 resultReceiver_.append(".\n");
271 }
272 return ERR_OK;
273 }
274
PrintDumpFileError(std::string & receiver,const char * path)275 extern "C" void PrintDumpFileError(std::string& receiver, const char* path)
276 {
277 receiver.append("Open Dump file (");
278 receiver.append(path);
279 receiver.append(") failed: ");
280 receiver.append(std::to_string(errno));
281 receiver.append("\n");
282 }
283
RunAsDumpCommand()284 ErrCode PowerShellCommand::RunAsDumpCommand()
285 {
286 resultReceiver_.clear();
287
288 PowerMgrClient& client = PowerMgrClient::GetInstance();
289 std::string ret = client.Dump(argList_);
290 resultReceiver_.append("Power Dump result: \n");
291 resultReceiver_.append(ret);
292
293 return ERR_OK;
294 }
295
296 #ifdef HAS_DISPLAY_MANAGER_PART
297 using namespace OHOS::DisplayPowerMgr;
DisplayOptargEmpty()298 bool PowerShellCommand::DisplayOptargEmpty()
299 {
300 if (!optarg) {
301 resultReceiver_.append("Error! please input your brightness value.\n");
302 resultReceiver_.append(DISPLAY_HELP_MSG);
303 return true;
304 }
305 return false;
306 }
307
RunAsDisplayCommandHelp()308 ErrCode PowerShellCommand::RunAsDisplayCommandHelp()
309 {
310 resultReceiver_.append(DISPLAY_HELP_MSG);
311 return ERR_OK;
312 }
313
RunAsDisplayCommandOverride()314 ErrCode PowerShellCommand::RunAsDisplayCommandOverride()
315 {
316 if (DisplayOptargEmpty()) {
317 return ERR_OK;
318 }
319 int32_t value = 0;
320 StrToInt(optarg, value);
321 bool ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(static_cast<uint32_t>(value));
322 resultReceiver_.append("Override brightness to ");
323 resultReceiver_.append(std::to_string(value));
324 if (!ret) {
325 resultReceiver_.append(" failed");
326 }
327 resultReceiver_.append("\n");
328 return ERR_OK;
329 }
330
RunAsDisplayCommandRestore()331 ErrCode PowerShellCommand::RunAsDisplayCommandRestore()
332 {
333 bool ret = DisplayPowerMgrClient::GetInstance().RestoreBrightness();
334 resultReceiver_.append("Restore brightness");
335 if (!ret) {
336 resultReceiver_.append(" failed");
337 }
338 resultReceiver_.append("\n");
339 return ERR_OK;
340 }
341
RunAsDisplayCommandBoost()342 ErrCode PowerShellCommand::RunAsDisplayCommandBoost()
343 {
344 if (DisplayOptargEmpty()) {
345 return ERR_OK;
346 }
347 int32_t value = 0;
348 StrToInt(optarg, value);
349 bool ret = DisplayPowerMgrClient::GetInstance().BoostBrightness(static_cast<uint32_t>(value));
350 resultReceiver_.append("Boost brightness timeout ");
351 resultReceiver_.append(std::to_string(value)).append("ms");
352 if (!ret) {
353 resultReceiver_.append(" failed");
354 }
355 resultReceiver_.append("\n");
356 return ERR_OK;
357 }
358
RunAsDisplayCommandCancelBoost()359 ErrCode PowerShellCommand::RunAsDisplayCommandCancelBoost()
360 {
361 bool ret = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness();
362 resultReceiver_.append("Cancel boost brightness");
363 if (!ret) {
364 resultReceiver_.append(" failed");
365 }
366 resultReceiver_.append("\n");
367 return ERR_OK;
368 }
369
RunAsDisplayCommandSetValue()370 ErrCode PowerShellCommand::RunAsDisplayCommandSetValue()
371 {
372 if (DisplayOptargEmpty()) {
373 return ERR_OK;
374 }
375 int32_t value = 0;
376 StrToInt(optarg, value);
377 bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(static_cast<uint32_t>(value));
378 resultReceiver_.append("Set brightness to ");
379 resultReceiver_.append(std::to_string(value));
380 if (!ret) {
381 resultReceiver_.append(" failed");
382 }
383 resultReceiver_.append("\n");
384 return ERR_OK;
385 }
386
RunAsDisplayCommandDiscount()387 ErrCode PowerShellCommand::RunAsDisplayCommandDiscount()
388 {
389 if (DisplayOptargEmpty()) {
390 return ERR_OK;
391 }
392 std::stringstream fstr(optarg);
393 double discount = 0;
394 fstr >> discount;
395 bool ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(discount);
396 resultReceiver_.append("Set brightness discount to ");
397 resultReceiver_.append(std::to_string(discount));
398 if (!ret) {
399 resultReceiver_.append(" failed");
400 }
401 resultReceiver_.append("\n");
402 return ERR_OK;
403 }
404
RunAsDisplayCommandDelay()405 ErrCode PowerShellCommand::RunAsDisplayCommandDelay()
406 {
407 if (DisplayOptargEmpty()) {
408 return ERR_OK;
409 }
410 int32_t value = 0;
411 StrToInt(optarg, value);
412 bool ret = DisplayPowerMgrClient::GetInstance().OverrideDisplayOffDelay(static_cast<uint32_t>(value));
413 resultReceiver_.append("Override delay time to ");
414 resultReceiver_.append(std::to_string(value));
415 if (!ret) {
416 resultReceiver_.append(" failed");
417 }
418 resultReceiver_.append("\n");
419 return ERR_OK;
420 }
421
RunAsDisplayCommand()422 ErrCode PowerShellCommand::RunAsDisplayCommand()
423 {
424 int ind = 0;
425 int option = getopt_long(argc_, argv_, "hrcs:o:b:d:e:", DISPLAY_OPTIONS, &ind);
426 resultReceiver_.clear();
427 auto item = commandDisplay_.find(option);
428 if (item != commandDisplay_.end()) {
429 return item->second();
430 }
431 resultReceiver_.append(DISPLAY_HELP_MSG);
432 return ERR_OK;
433 }
434 #endif
435 #endif
436
RunAsTimeOutCommand()437 ErrCode PowerShellCommand::RunAsTimeOutCommand()
438 {
439 int ind = 0;
440 int option = getopt_long(argc_, argv_, "hro:", TIME_OUT_OPTIONS, &ind);
441 resultReceiver_.clear();
442 if (option == 'h') {
443 resultReceiver_.append(TIME_OUT_HELP_MSG);
444 return ERR_OK;
445 }
446 if (option == 'r') {
447 bool ret = PowerMgrClient::GetInstance().RestoreScreenOffTime();
448 resultReceiver_.append("Restore screen off time");
449 if (!ret) {
450 resultReceiver_.append(" failed");
451 }
452 resultReceiver_.append("\n");
453 return ERR_OK;
454 }
455 if (!optarg) {
456 resultReceiver_.append("Error! please input your screen off time.\n");
457 resultReceiver_.append(TIME_OUT_HELP_MSG);
458 return ERR_OK;
459 }
460 if (option == 'o') {
461 int32_t timeout = 0;
462 StrToInt(optarg, timeout);
463 bool ret = PowerMgrClient::GetInstance().OverrideScreenOffTime(static_cast<int64_t>(timeout));
464 resultReceiver_.append("Override screen off time to ");
465 resultReceiver_.append(std::to_string(timeout));
466 if (!ret) {
467 resultReceiver_.append(" failed");
468 }
469 resultReceiver_.append("\n");
470 return ERR_OK;
471 }
472 return ERR_OK;
473 }
474 } // namespace PowerMgr
475 } // namespace OHOS
476