• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <fcntl.h>
20 #include <getopt.h>
21 #include <iostream>
22 #include <string_ex.h>
23 #include <unistd.h>
24 
25 #include "power_mgr_client.h"
26 #include "display_power_mgr_client.h"
27 #include "iservice_registry.h"
28 #include "singleton.h"
29 #include "system_ability_definition.h"
30 
31 extern char *optarg;
32 
33 namespace OHOS {
34 namespace PowerMgr {
35 using namespace OHOS::DisplayPowerMgr;
36 
37 static const struct option SET_MODE_OPTIONS[] = {
38     {"help", no_argument, nullptr, 'h'},
39 };
40 
41 static const struct option DISPLAY_OPTIONS[] = {
42     {"help", no_argument, nullptr, 'h'},
43     {"restore", no_argument, nullptr, 'r'},
44     {"update", required_argument, nullptr, 'u'},
45     {"override", required_argument, nullptr, 'o'},
46 };
47 
48 static const std::string HELP_MSG =
49     "usage: power-shell\n"
50     "command list:\n"
51     "  setmode :    Set power mode. \n"
52     "  wakeup  :    Wakeup system and turn screen on. \n"
53     "  suspend :    Suspend system and turn screen off. \n"
54     "  display :    Update or Override display brightness. \n"
55     "  dump    :    Dump power info. \n"
56     "  help    :    Show this help menu. \n";
57 
58 static const std::string SETMODE_HELP_MSG =
59     "usage: power-shell setmode [<options>]\n"
60     "setmode <power mode: (value is as below)> \n"
61     "  600  :  normal mode\n"
62     "  601  :  power save mode\n"
63     "  602  :  extreme mode\n";
64 
65 static const std::string DISPLAY_HELP_MSG =
66     "usage: power-shell display [<options>] 100\n"
67     "display <options is as below> \n"
68     "  -u  :  update brightness\n"
69     "  -o  :  override brightness\n";
70 
PowerShellCommand(int argc,char * argv[])71 PowerShellCommand::PowerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "power-shell")
72 {}
73 
CreateCommandMap()74 ErrCode PowerShellCommand::CreateCommandMap()
75 {
76     commandMap_ = {
77         {"help", std::bind(&PowerShellCommand::RunAsHelpCommand, this)},
78         {"setmode", std::bind(&PowerShellCommand::RunAsSetModeCommand, this)},
79         {"wakeup", std::bind(&PowerShellCommand::RunAsWakeupCommand, this)},
80         {"suspend", std::bind(&PowerShellCommand::RunAsSuspendCommand, this)},
81         {"display", std::bind(&PowerShellCommand::RunAsDisplayCommand, this)},
82         {"dump", std::bind(&PowerShellCommand::RunAsDumpCommand, this)},
83     };
84 
85     return ERR_OK;
86 }
87 
CreateMessageMap()88 ErrCode PowerShellCommand::CreateMessageMap()
89 {
90     messageMap_ = {};
91 
92     return ERR_OK;
93 }
94 
init()95 ErrCode PowerShellCommand::init()
96 {
97     return OHOS::ERR_OK;
98 }
99 
RunAsHelpCommand()100 ErrCode PowerShellCommand::RunAsHelpCommand()
101 {
102     resultReceiver_.clear();
103     resultReceiver_.append(HELP_MSG);
104     return ERR_OK;
105 }
106 
RunAsSetModeCommand()107 ErrCode PowerShellCommand::RunAsSetModeCommand()
108 {
109     int ind = 0;
110     int option = getopt_long(argc_, argv_, "h", SET_MODE_OPTIONS, &ind);
111     resultReceiver_.clear();
112     if (option == 'h') {
113         resultReceiver_.append(SETMODE_HELP_MSG);
114         return ERR_OK;
115     }
116     if (argList_.empty()) {
117         resultReceiver_.append("Error! please input your mode value. \n");
118         resultReceiver_.append(SETMODE_HELP_MSG);
119         return ERR_OK;
120     }
121 
122     uint32_t mode = static_cast<uint32_t>(atoi(argList_[0].c_str()));
123     resultReceiver_.append("Set Mode: ");
124     resultReceiver_.append(argList_[0]);
125     resultReceiver_.append("\n");
126     PowerMgrClient &client = PowerMgrClient::GetInstance();
127     client.SetDeviceMode(mode);
128     uint32_t result = client.GetDeviceMode();
129     if (result == mode) {
130         resultReceiver_.append("Set Mode Success!");
131     } else {
132         resultReceiver_.append("Set Mode Failed, current mode is: ");
133         resultReceiver_.append(std::to_string(result));
134     }
135 
136     return ERR_OK;
137 }
138 
RunAsWakeupCommand()139 ErrCode PowerShellCommand::RunAsWakeupCommand()
140 {
141     PowerMgrClient &client = PowerMgrClient::GetInstance();
142     std::string detail = "shell";
143     client.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, detail);
144     resultReceiver_.append("WakeupDevice is called");
145     return ERR_OK;
146 }
147 
RunAsSuspendCommand()148 ErrCode PowerShellCommand::RunAsSuspendCommand()
149 {
150     PowerMgrClient &client = PowerMgrClient::GetInstance();
151     std::string detail = "shell";
152     client.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_BUTTON);
153     resultReceiver_.append("SuspendDevice is called");
154     return ERR_OK;
155 }
156 
PrintDumpFileError(std::string & receiver,const char * path)157 extern "C" void PrintDumpFileError(std::string& receiver, const char* path)
158 {
159     receiver.append("Open Dump file (");
160     receiver.append(path);
161     receiver.append(") failed: ");
162     receiver.append(std::to_string(errno));
163     receiver.append("\n");
164 }
165 
RunAsDumpCommand()166 ErrCode PowerShellCommand::RunAsDumpCommand()
167 {
168     resultReceiver_.clear();
169 
170     PowerMgrClient &client = PowerMgrClient::GetInstance();
171     std::string ret = client.Dump(argList_);
172     resultReceiver_.append("Power Dump result: \n");
173     resultReceiver_.append(ret);
174 
175     return ERR_OK;
176 }
177 
RunAsDisplayCommand()178 ErrCode PowerShellCommand::RunAsDisplayCommand()
179 {
180     int ind = 0;
181     int option = getopt_long(argc_, argv_, "hru:o:", DISPLAY_OPTIONS, &ind);
182     resultReceiver_.clear();
183     if (option == 'h') {
184         resultReceiver_.append(DISPLAY_HELP_MSG);
185         return ERR_OK;
186     }
187     if (option == 'r') {
188         bool ret = DisplayPowerMgrClient::GetInstance().RestoreBrightness();
189         resultReceiver_.append("Restore brightness");
190         if (!ret) {
191             resultReceiver_.append(" failed");
192         }
193         resultReceiver_.append("\n");
194         return ERR_OK;
195     }
196     if (!optarg) {
197         resultReceiver_.append("Error! please input your brightness value.\n");
198         resultReceiver_.append(DISPLAY_HELP_MSG);
199         return ERR_OK;
200     }
201     auto value = static_cast<uint32_t>(atoi(optarg));
202     if (option == 'u') {
203         bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(value);
204         resultReceiver_.append("Update brightness to ");
205         resultReceiver_.append(std::to_string(value));
206         if (!ret) {
207             resultReceiver_.append(" failed");
208         }
209         resultReceiver_.append("\n");
210         return ERR_OK;
211     }
212     if (option == 'o') {
213         bool ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(value);
214         resultReceiver_.append("Override brightness to ");
215         resultReceiver_.append(std::to_string(value));
216         if (!ret) {
217             resultReceiver_.append(" failed");
218         }
219         resultReceiver_.append("\n");
220         return ERR_OK;
221     }
222     return ERR_OK;
223 }
224 }
225 }  // namespace OHOS
226