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 <string_ex.h>
20 #include <sstream>
21 #include "parameters.h"
22 #include "power_mgr_client.h"
23 #ifdef HAS_DISPLAY_MANAGER_PART
24 #include "display_power_mgr_client.h"
25 #endif
26 
27 namespace OHOS {
28 namespace PowerMgr {
29 
IsDeveloperMode()30 bool PowerShellCommand::IsDeveloperMode()
31 {
32     return OHOS::system::GetBoolParameter("const.security.developermode.state", true);
33 }
34 
35 static const struct option SET_MODE_OPTIONS[] = {
36     {"help", no_argument, nullptr, 'h'},
37 };
38 
39 #ifndef POWER_SHELL_USER
40 #ifdef HAS_DISPLAY_MANAGER_PART
41 static const struct option DISPLAY_OPTIONS[] = {
42     {"help", no_argument, nullptr, 'h'},
43     {"restore", no_argument, nullptr, 'r'},
44     {"set", required_argument, nullptr, 's'},
45     {"override", required_argument, nullptr, 'o'},
46     {"boost", required_argument, nullptr, 'b'},
47     {"cancel", no_argument, nullptr, 'c'},
48     {"discount", required_argument, nullptr, 'd'},
49 };
50 #endif
51 
52 static const struct option PROXYLOCK_OPTIONS[] = {
53     {"help", no_argument, nullptr, 'h'},
54     {"proxy", required_argument, nullptr, 'p'},
55     {"uproxy", required_argument, nullptr, 'u'},
56 };
57 
58 static const struct option HIBERNATE_OPTIONS[] = {
59     {"help", no_argument, nullptr, 'h'},
60 };
61 #endif
62 
63 static const struct option TIME_OUT_OPTIONS[] = {
64     {"help", no_argument, nullptr, 'h'},
65     {"restore", no_argument, nullptr, 'r'},
66     {"override", required_argument, nullptr, 'o'},
67 };
68 
69 static const struct option WAKE_UP_TYPES[] = {
70     {"pre_bright", no_argument, nullptr, 'a'},
71     {"pre_bright_auth_success", no_argument, nullptr, 'b'},
72     {"pre_bright_auth_fail_screen_on", no_argument, nullptr, 'c'},
73     {"pre_bright_auth_fail_screen_off", no_argument, nullptr, 'd'},
74     {"default", no_argument, nullptr, 'e'},
75 };
76 
77 static const std::string HELP_MSG =
78     "usage: power-shell\n"
79     "command list:\n"
80     "  setmode :    Set power mode. \n"
81     "  wakeup  :    Wakeup system and turn screen on. \n"
82     "  suspend :    Suspend system and turn screen off. \n"
83 #ifndef POWER_SHELL_USER
84     "  lock    :    Query running lock lists by bundle app. \n"
85     "  proxylock :    Proxy running lock by app uid. \n"
86     "  hibernate :    hibernate the device. \n"
87 #ifdef HAS_DISPLAY_MANAGER_PART
88     "  display :    Update or Override display brightness. \n"
89 #endif
90     "  dump    :    Dump power info. \n"
91 #endif
92     "  timeout :    Override or Restore screen off time. \n"
93     "  help    :    Show this help menu. \n";
94 
95 static const std::string SETMODE_HELP_MSG =
96     "usage: power-shell setmode [<options>]\n"
97     "setmode <power mode: (value is as below)> \n"
98     "  600  :  normal mode\n"
99     "  601  :  power save mode\n"
100     "  602  :  performance mode\n"
101     "  603  :  extreme power save mode\n";
102 
103 #ifndef POWER_SHELL_USER
104 #ifdef HAS_DISPLAY_MANAGER_PART
105 static const std::string DISPLAY_HELP_MSG =
106     "usage: power-shell display [<options>] 100\n"
107     "display <options are as below> \n"
108     "  -h  :  display help\n"
109     "  -r  :  retore brightness\n"
110     "  -s  :  set brightness\n"
111     "  -o  :  override brightness\n"
112     "  -b  :  timing maximum brightness\n"
113     "  -c  :  cancel the timing maximum brightness\n"
114     "  -d  :  discount brightness\n";
115 #endif
116 
117 static const std::string PROXYLOCK_HELP_MSG =
118     "usage: power-shell proxylock [<options>] 20020041\n"
119     "proxylock <options are as below> \n"
120     "  -p  :  proxy runninglock\n"
121     "  -u  :  unproxy runninglock\n";
122 
123 static const std::string HIBERNATE_HELP_MSG =
124     "usage: power-shell hibernate [<options>]\n"
125     "  hibernate <options are as below> \n"
126     "  the default option is false\n"
127     "  true  :  clear memory before hibernate\n"
128     "  false :  skip clearing memory before hibernate\n";
129 #endif
130 
131 static const std::string TIME_OUT_HELP_MSG =
132     "usage: power-shell timeout [<options>] 1000\n"
133     "timeout <options are as below> \n"
134     "  -o  :  override screen off time\n"
135     "  -r  :  restore screen off time\n";
136 
PowerShellCommand(int argc,char * argv[])137 PowerShellCommand::PowerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "power-shell")
138 {}
139 
CreateCommandMap()140 ErrCode PowerShellCommand::CreateCommandMap()
141 {
142     commandMap_ = {
143         {"help", [this]() -> ErrCode { return this->RunAsHelpCommand(); }},
144         {"setmode", [this]() -> ErrCode { return this->RunAsSetModeCommand(); }},
145         {"wakeup", [this]() -> ErrCode { return this->RunAsWakeupCommand(); }},
146         {"suspend", [this]() -> ErrCode { return this->RunAsSuspendCommand(); }},
147 #ifndef POWER_SHELL_USER
148         {"hibernate", [this]() -> ErrCode { return this->RunAsHibernateCommand(); }},
149         {"lock", [this]() -> ErrCode { return this->RunAsQueryLockCommand(); }},
150         {"proxylock", [this]() -> ErrCode { return this->RunAsProxyLockCommand(); }},
151         {"forcetimeout", [this]() -> ErrCode { return this->RunAsForceTimeOutCommand(); }},
152         {"timeoutscreenlock", [this]() -> ErrCode { return this->RunAsTimeOutScreenLockCommand(); }},
153 #ifdef HAS_DISPLAY_MANAGER_PART
154         {"display", [this]() -> ErrCode { return this->RunAsDisplayCommand(); }},
155 #endif
156         {"dump", [this]() -> ErrCode { return this->RunAsDumpCommand(); }},
157 #endif
158         {"timeout", [this]() -> ErrCode { return this->RunAsTimeOutCommand(); }},
159     };
160 
161 #ifndef POWER_SHELL_USER
162 #ifdef HAS_DISPLAY_MANAGER_PART
163     commandDisplay_ = {
164         {'h', [this]() -> ErrCode { return this->RunAsDisplayCommandHelp(); }},
165         {'r', [this]() -> ErrCode { return this->RunAsDisplayCommandRestore(); }},
166         {'s', [this]() -> ErrCode { return this->RunAsDisplayCommandSetValue(); }},
167         {'o', [this]() -> ErrCode { return this->RunAsDisplayCommandOverride(); }},
168         {'b', [this]() -> ErrCode { return this->RunAsDisplayCommandBoost(); }},
169         {'c', [this]() -> ErrCode { return this->RunAsDisplayCommandCancelBoost(); }},
170         {'d', [this]() -> ErrCode { return this->RunAsDisplayCommandDiscount(); }},
171     };
172 #endif
173 #endif
174 
175     return ERR_OK;
176 }
177 
RunAsForceTimeOutCommand()178 ErrCode PowerShellCommand::RunAsForceTimeOutCommand()
179 {
180     if (!IsDeveloperMode()) {
181         return ERR_PERMISSION_DENIED;
182     }
183     bool enabled = argList_[0][0] - '0';
184     PowerMgrClient& client = PowerMgrClient::GetInstance();
185     client.SetForceTimingOut(enabled);
186     return ERR_OK;
187 }
188 
RunAsTimeOutScreenLockCommand()189 ErrCode PowerShellCommand::RunAsTimeOutScreenLockCommand()
190 {
191     if (!IsDeveloperMode()) {
192         return ERR_PERMISSION_DENIED;
193     }
194     resultReceiver_.clear();
195     auto parameterCount = argList_.size();
196     constexpr size_t MIN_PARAMETER_COUNT = 2;
197     if (parameterCount < MIN_PARAMETER_COUNT) {
198         resultReceiver_.append("too few arguments \n");
199         return ERR_OK;
200     }
201     PowerMgrClient& client = PowerMgrClient::GetInstance();
202     bool enableLockScreen = argList_[0][0] - '0';
203     bool checkScreenOnLock = argList_[1][0] - '0';
204     if (parameterCount == MIN_PARAMETER_COUNT) {
205         client.LockScreenAfterTimingOut(enableLockScreen, checkScreenOnLock);
206         return ERR_OK;
207     }
208     bool sendScreenOffEvent = argList_[2][0] - '0';
209     client.LockScreenAfterTimingOut(enableLockScreen, checkScreenOnLock, sendScreenOffEvent);
210     return ERR_OK;
211 }
212 
CreateMessageMap()213 ErrCode PowerShellCommand::CreateMessageMap()
214 {
215     messageMap_ = {};
216 
217     return ERR_OK;
218 }
219 
init()220 ErrCode PowerShellCommand::init()
221 {
222     return OHOS::ERR_OK;
223 }
224 
RunAsHelpCommand()225 ErrCode PowerShellCommand::RunAsHelpCommand()
226 {
227     if (!IsDeveloperMode()) {
228         return ERR_PERMISSION_DENIED;
229     }
230     resultReceiver_.clear();
231     resultReceiver_.append(HELP_MSG);
232     return ERR_OK;
233 }
234 
RunAsSetModeCommand()235 ErrCode PowerShellCommand::RunAsSetModeCommand()
236 {
237     if (!IsDeveloperMode()) {
238         return ERR_PERMISSION_DENIED;
239     }
240     int ind = 0;
241     int option = getopt_long(argc_, argv_, "h", SET_MODE_OPTIONS, &ind);
242     resultReceiver_.clear();
243     if (option == 'h') {
244         resultReceiver_.append(SETMODE_HELP_MSG);
245         return ERR_OK;
246     }
247     if (argList_.empty()) {
248         resultReceiver_.append("Error! please input your mode value. \n");
249         resultReceiver_.append(SETMODE_HELP_MSG);
250         return ERR_OK;
251     }
252 
253     auto mode = static_cast<uint32_t>(strtol(argList_[0].c_str(), nullptr, 0));
254     resultReceiver_.append("Set Mode: ");
255     resultReceiver_.append(argList_[0]);
256     resultReceiver_.append("\n");
257     PowerMgrClient& client = PowerMgrClient::GetInstance();
258     client.SetDeviceMode(static_cast<PowerMode>(mode));
259     uint32_t result = static_cast<uint32_t>(client.GetDeviceMode());
260     if (result == mode) {
261         resultReceiver_.append("Set Mode Success!\n");
262     } else {
263         resultReceiver_.append("Set Mode Failed, current mode is: ");
264         resultReceiver_.append(std::to_string(result));
265         resultReceiver_.append("\n");
266     }
267 
268     return ERR_OK;
269 }
270 
RunAsWakeupCommand()271 ErrCode PowerShellCommand::RunAsWakeupCommand()
272 {
273     if (!IsDeveloperMode()) {
274         return ERR_PERMISSION_DENIED;
275     }
276     int ind = 0;
277     int option = getopt_long(argc_, argv_, "abcde", WAKE_UP_TYPES, &ind);
278     resultReceiver_.clear();
279     PowerMgrClient& client = PowerMgrClient::GetInstance();
280     std::string detail = "shell";
281     if (option == 'a') {
282         detail = "pre_bright";
283         resultReceiver_.append("pre_bright is called\n");
284     }
285     if (option == 'b') {
286         detail = "pre_bright_auth_success";
287         resultReceiver_.append("pre_bright_auth_success is called\n");
288     }
289     if (option == 'c') {
290         detail = "pre_bright_auth_fail_screen_on";
291         resultReceiver_.append("pre_bright_auth_fail_screen_on is called\n");
292     }
293     if (option == 'd') {
294         detail = "pre_bright_auth_fail_screen_off";
295         resultReceiver_.append("pre_bright_auth_fail_screen_off is called\n");
296     }
297     if (option == 'e') {
298         resultReceiver_.append("default is called\n");
299         detail = "shell";
300     }
301     client.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail);
302     resultReceiver_.append("WakeupDevice is called\n");
303     return ERR_OK;
304 }
305 
RunAsSuspendCommand()306 ErrCode PowerShellCommand::RunAsSuspendCommand()
307 {
308     if (!IsDeveloperMode()) {
309         return ERR_PERMISSION_DENIED;
310     }
311     PowerMgrClient& client = PowerMgrClient::GetInstance();
312     client.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY);
313     resultReceiver_.append("SuspendDevice is called\n");
314     return ERR_OK;
315 }
316 
317 #ifndef POWER_SHELL_USER
RunAsHibernateCommand()318 ErrCode PowerShellCommand::RunAsHibernateCommand()
319 {
320     if (!IsDeveloperMode()) {
321         return ERR_PERMISSION_DENIED;
322     }
323     int ind = 0;
324     int option = getopt_long(argc_, argv_, "h", HIBERNATE_OPTIONS, &ind);
325     resultReceiver_.clear();
326     if (option == 'h') {
327         resultReceiver_.append(HIBERNATE_HELP_MSG);
328         return ERR_OK;
329     }
330     bool clearMemory = false;
331     if (!argList_.empty()) {
332         if (strcmp(argList_[0].c_str(), "false") == 0) {
333             clearMemory = false;
334         } else if (strcmp(argList_[0].c_str(), "true") == 0) {
335             clearMemory = true;
336         } else {
337             resultReceiver_.append("Error! please input your option value. \n");
338             resultReceiver_.append(HIBERNATE_HELP_MSG);
339             return ERR_OK;
340         }
341     }
342 
343     PowerMgrClient& client = PowerMgrClient::GetInstance();
344     client.Hibernate(clearMemory);
345     if (clearMemory) {
346         resultReceiver_.append("Hibernate true is called\n");
347     } else {
348         resultReceiver_.append("Hibernate false is called\n");
349     }
350     return ERR_OK;
351 }
352 
GetBundleRunningLockTypeString(RunningLockType type)353 static const std::string GetBundleRunningLockTypeString(RunningLockType type)
354 {
355     switch (type) {
356         case RunningLockType::RUNNINGLOCK_SCREEN:
357             return "SCREEN";
358         case RunningLockType::RUNNINGLOCK_BACKGROUND:
359             return "BACKGROUND";
360         case RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL:
361             return "PROXIMITY_SCREEN_CONTROL";
362         case RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE:
363             return "BACKGROUND_PHONE";
364         case RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION:
365             return "BACKGROUND_NOTIFICATION";
366         case RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO:
367             return "BACKGROUND_AUDIO";
368         case RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT:
369             return "BACKGROUND_SPORT";
370         case RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION:
371             return "BACKGROUND_NAVIGATION";
372         case RunningLockType::RUNNINGLOCK_BACKGROUND_TASK:
373             return "BACKGROUND_TASK";
374         case RunningLockType::RUNNINGLOCK_BUTT:
375             return "BUTT";
376         default:
377             break;
378     }
379 
380     return "UNKNOWN";
381 }
382 
RunAsQueryLockCommand()383 ErrCode PowerShellCommand::RunAsQueryLockCommand()
384 {
385     if (!IsDeveloperMode()) {
386         return ERR_PERMISSION_DENIED;
387     }
388     PowerMgrClient& client = PowerMgrClient::GetInstance();
389     std::map<std::string, RunningLockInfo> runningLockLists;
390     bool ret = client.QueryRunningLockLists(runningLockLists);
391     if (!ret) {
392         resultReceiver_.append("failed.\n");
393         return ERR_OK;
394     }
395     resultReceiver_.append("The locking application information is as follows:\n");
396     uint32_t mapSize = static_cast<uint32_t>(runningLockLists.size());
397     resultReceiver_.append("The nums of holding lock by bundle app is ");
398     resultReceiver_.append(std::to_string(mapSize));
399     resultReceiver_.append(".\n");
400     int counter = 0;
401     for (auto it : runningLockLists) {
402         counter++;
403         resultReceiver_.append(std::to_string(counter));
404         resultReceiver_.append(". bundleName=");
405         resultReceiver_.append(it.second.bundleName);
406         resultReceiver_.append(" name=");
407         resultReceiver_.append(it.second.name);
408         resultReceiver_.append(" type=");
409         resultReceiver_.append(GetBundleRunningLockTypeString(it.second.type));
410         resultReceiver_.append(" pid=");
411         resultReceiver_.append(std::to_string(it.second.pid));
412         resultReceiver_.append(" uid=");
413         resultReceiver_.append(std::to_string(it.second.uid));
414         resultReceiver_.append(".\n");
415     }
416     return ERR_OK;
417 }
418 
RunAsProxyLockCommand()419 ErrCode PowerShellCommand::RunAsProxyLockCommand()
420 {
421     if (!IsDeveloperMode()) {
422         return ERR_PERMISSION_DENIED;
423     }
424     int ind = 0;
425     int option = getopt_long(argc_, argv_, "hp:u:", PROXYLOCK_OPTIONS, &ind);
426     resultReceiver_.clear();
427     if (option == 'h') {
428         resultReceiver_.append(PROXYLOCK_HELP_MSG);
429         return ERR_OK;
430     }
431     if (!optarg) {
432         resultReceiver_.append("Error! please input your app uid.\n");
433         resultReceiver_.append(PROXYLOCK_HELP_MSG);
434         return ERR_OK;
435     }
436     int32_t uid = 0;
437     StrToInt(optarg, uid);
438     if (option == 'p') {
439         bool ret = PowerMgrClient::GetInstance().ProxyRunningLock(true, INT32_MAX, uid);
440         resultReceiver_.append("proxy runninglock for");
441         resultReceiver_.append(std::to_string(uid));
442         if (!ret) {
443             resultReceiver_.append(" failed");
444         }
445         resultReceiver_.append("\n");
446         return ERR_OK;
447     }
448     if (option == 'u') {
449         bool ret = PowerMgrClient::GetInstance().ProxyRunningLock(false, INT32_MAX, uid);
450         resultReceiver_.append("unproxy runninglock for");
451         resultReceiver_.append(std::to_string(uid));
452         if (!ret) {
453             resultReceiver_.append(" failed");
454         }
455         resultReceiver_.append("\n");
456         return ERR_OK;
457     }
458     return ERR_OK;
459 }
460 
PrintDumpFileError(std::string & receiver,const char * path)461 extern "C" void PrintDumpFileError(std::string& receiver, const char* path)
462 {
463     receiver.append("Open Dump file (");
464     receiver.append(path);
465     receiver.append(") failed: ");
466     receiver.append(std::to_string(errno));
467     receiver.append("\n");
468 }
469 
RunAsDumpCommand()470 ErrCode PowerShellCommand::RunAsDumpCommand()
471 {
472     if (!IsDeveloperMode()) {
473         return ERR_PERMISSION_DENIED;
474     }
475     resultReceiver_.clear();
476 
477     PowerMgrClient& client = PowerMgrClient::GetInstance();
478     std::string ret = client.Dump(argList_);
479     resultReceiver_.append("Power Dump result: \n");
480     resultReceiver_.append(ret);
481 
482     return ERR_OK;
483 }
484 
485 #ifdef HAS_DISPLAY_MANAGER_PART
486 using namespace OHOS::DisplayPowerMgr;
DisplayOptargEmpty()487 bool PowerShellCommand::DisplayOptargEmpty()
488 {
489     if (!optarg) {
490         resultReceiver_.append("Error! please input your brightness value.\n");
491         resultReceiver_.append(DISPLAY_HELP_MSG);
492         return true;
493     }
494     return false;
495 }
496 
RunAsDisplayCommandHelp()497 ErrCode PowerShellCommand::RunAsDisplayCommandHelp()
498 {
499     if (!IsDeveloperMode()) {
500         return ERR_PERMISSION_DENIED;
501     }
502     resultReceiver_.append(DISPLAY_HELP_MSG);
503     return ERR_OK;
504 }
505 
RunAsDisplayCommandOverride()506 ErrCode PowerShellCommand::RunAsDisplayCommandOverride()
507 {
508     if (!IsDeveloperMode()) {
509         return ERR_PERMISSION_DENIED;
510     }
511     if (DisplayOptargEmpty()) {
512         return ERR_OK;
513     }
514     int32_t value = 0;
515     StrToInt(optarg, value);
516     bool ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(static_cast<uint32_t>(value));
517     resultReceiver_.append("Override brightness to ");
518     resultReceiver_.append(std::to_string(value));
519     if (!ret) {
520         resultReceiver_.append(" failed");
521     }
522     resultReceiver_.append("\n");
523     return ERR_OK;
524 }
525 
RunAsDisplayCommandRestore()526 ErrCode PowerShellCommand::RunAsDisplayCommandRestore()
527 {
528     if (!IsDeveloperMode()) {
529         return ERR_PERMISSION_DENIED;
530     }
531     bool ret = DisplayPowerMgrClient::GetInstance().RestoreBrightness();
532     resultReceiver_.append("Restore brightness");
533     if (!ret) {
534         resultReceiver_.append(" failed");
535     }
536     resultReceiver_.append("\n");
537     return ERR_OK;
538 }
539 
RunAsDisplayCommandBoost()540 ErrCode PowerShellCommand::RunAsDisplayCommandBoost()
541 {
542     if (!IsDeveloperMode()) {
543         return ERR_PERMISSION_DENIED;
544     }
545     if (DisplayOptargEmpty()) {
546         return ERR_OK;
547     }
548     int32_t value = 0;
549     StrToInt(optarg, value);
550     bool ret = DisplayPowerMgrClient::GetInstance().BoostBrightness(static_cast<uint32_t>(value));
551     resultReceiver_.append("Boost brightness timeout ");
552     resultReceiver_.append(std::to_string(value)).append("ms");
553     if (!ret) {
554         resultReceiver_.append(" failed");
555     }
556     resultReceiver_.append("\n");
557     return ERR_OK;
558 }
559 
RunAsDisplayCommandCancelBoost()560 ErrCode PowerShellCommand::RunAsDisplayCommandCancelBoost()
561 {
562     if (!IsDeveloperMode()) {
563         return ERR_PERMISSION_DENIED;
564     }
565     bool ret = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness();
566     resultReceiver_.append("Cancel boost brightness");
567     if (!ret) {
568         resultReceiver_.append(" failed");
569     }
570     resultReceiver_.append("\n");
571     return ERR_OK;
572 }
573 
RunAsDisplayCommandSetValue()574 ErrCode PowerShellCommand::RunAsDisplayCommandSetValue()
575 {
576     if (!IsDeveloperMode()) {
577         return ERR_PERMISSION_DENIED;
578     }
579     if (DisplayOptargEmpty()) {
580         return ERR_OK;
581     }
582     int32_t value = 0;
583     StrToInt(optarg, value);
584     bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(static_cast<uint32_t>(value));
585     resultReceiver_.append("Set brightness to ");
586     resultReceiver_.append(std::to_string(value));
587     if (!ret) {
588         resultReceiver_.append(" failed");
589     }
590     resultReceiver_.append("\n");
591     return ERR_OK;
592 }
593 
RunAsDisplayCommandDiscount()594 ErrCode PowerShellCommand::RunAsDisplayCommandDiscount()
595 {
596     if (!IsDeveloperMode()) {
597         return ERR_PERMISSION_DENIED;
598     }
599     if (DisplayOptargEmpty()) {
600         return ERR_OK;
601     }
602     std::stringstream fstr(optarg);
603     double discount = 0;
604     fstr >> discount;
605     bool ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(discount);
606     resultReceiver_.append("Set brightness discount to ");
607     resultReceiver_.append(std::to_string(discount));
608     if (!ret) {
609         resultReceiver_.append(" failed");
610     }
611     resultReceiver_.append("\n");
612     return ERR_OK;
613 }
614 
RunAsDisplayCommand()615 ErrCode PowerShellCommand::RunAsDisplayCommand()
616 {
617     if (!IsDeveloperMode()) {
618         return ERR_PERMISSION_DENIED;
619     }
620     int ind = 0;
621     int option = getopt_long(argc_, argv_, "hrcs:o:b:d:", DISPLAY_OPTIONS, &ind);
622     resultReceiver_.clear();
623     auto item = commandDisplay_.find(option);
624     if (item != commandDisplay_.end()) {
625         return item->second();
626     }
627     resultReceiver_.append(DISPLAY_HELP_MSG);
628     return ERR_OK;
629 }
630 #endif
631 #endif
632 
RunAsTimeOutCommand()633 ErrCode PowerShellCommand::RunAsTimeOutCommand()
634 {
635     if (!IsDeveloperMode()) {
636         return ERR_PERMISSION_DENIED;
637     }
638     int ind = 0;
639     int option = getopt_long(argc_, argv_, "hro:", TIME_OUT_OPTIONS, &ind);
640     resultReceiver_.clear();
641     if (option == 'h') {
642         resultReceiver_.append(TIME_OUT_HELP_MSG);
643         return ERR_OK;
644     }
645     if (option == 'r') {
646         int ret = (int)PowerMgrClient::GetInstance().RestoreScreenOffTime();
647         resultReceiver_.append("Restore screen off time");
648         if (ret != ERR_OK) {
649             resultReceiver_.append(" failed");
650         }
651         resultReceiver_.append("\n");
652         return ERR_OK;
653     }
654     if (!optarg) {
655         resultReceiver_.append("Error! please input your screen off time.\n");
656         resultReceiver_.append(TIME_OUT_HELP_MSG);
657         return ERR_OK;
658     }
659     if (option == 'o') {
660         int32_t timeout = 0;
661         StrToInt(optarg, timeout);
662         int ret = (int)PowerMgrClient::GetInstance().OverrideScreenOffTime(static_cast<int64_t>(timeout));
663         resultReceiver_.append("Override screen off time to ");
664         resultReceiver_.append(std::to_string(timeout));
665         if (ret != ERR_OK) {
666             resultReceiver_.append(" failed");
667         }
668         resultReceiver_.append("\n");
669         return ERR_OK;
670     }
671     return ERR_OK;
672 }
673 } // namespace PowerMgr
674 } // namespace OHOS
675