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