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 #ifdef HAS_DISPLAY_MANAGER_PART
38 static const struct option DISPLAY_OPTIONS[] = {
39 {"help", no_argument, nullptr, 'h'},
40 {"restore", no_argument, nullptr, 'r'},
41 {"set", required_argument, nullptr, 's'},
42 {"override", required_argument, nullptr, 'o'},
43 {"boost", required_argument, nullptr, 'b'},
44 {"cancel", no_argument, nullptr, 'c'},
45 {"discount", required_argument, nullptr, 'd'},
46 {"override delay", required_argument, nullptr, 'e'},
47 };
48 #endif
49
50 static const struct option TIME_OUT_OPTIONS[] = {
51 {"help", no_argument, nullptr, 'h'},
52 {"restore", no_argument, nullptr, 'r'},
53 {"override", required_argument, nullptr, 'o'},
54 };
55
56 static const std::string HELP_MSG =
57 "usage: power-shell\n"
58 "command list:\n"
59 " setmode : Set power mode. \n"
60 " wakeup : Wakeup system and turn screen on. \n"
61 " suspend : Suspend system and turn screen off. \n"
62 #ifdef HAS_DISPLAY_MANAGER_PART
63 " display : Update or Override display brightness. \n"
64 #endif
65 " timeout : Override or Restore screen off time. \n"
66 " dump : Dump power info. \n"
67 " help : Show this help menu. \n";
68
69 static const std::string SETMODE_HELP_MSG =
70 "usage: power-shell setmode [<options>]\n"
71 "setmode <power mode: (value is as below)> \n"
72 " 600 : normal mode\n"
73 " 601 : power save mode\n"
74 " 602 : performance mode\n"
75 " 603 : extreme power save mode\n";
76
77 #ifdef HAS_DISPLAY_MANAGER_PART
78 static const std::string DISPLAY_HELP_MSG =
79 "usage: power-shell display [<options>] 100\n"
80 "display <options are as below> \n"
81 " -h : display help\n"
82 " -r : retore brightness\n"
83 " -s : set brightness\n"
84 " -o : override brightness\n"
85 " -b : timing maximum brightness\n"
86 " -c : cancel the timing maximum brightness\n"
87 " -d : discount brightness\n"
88 " -e : set screenoff delay\n";
89 #endif
90
91 static const std::string TIME_OUT_HELP_MSG =
92 "usage: power-shell timeout [<options>] 1000\n"
93 "timeout <options are as below> \n"
94 " -o : override screen off time\n"
95 " -r : restore screen off time\n";
96
PowerShellCommand(int argc,char * argv[])97 PowerShellCommand::PowerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, "power-shell")
98 {}
99
CreateCommandMap()100 ErrCode PowerShellCommand::CreateCommandMap()
101 {
102 commandMap_ = {
103 {"help", std::bind(&PowerShellCommand::RunAsHelpCommand, this)},
104 {"setmode", std::bind(&PowerShellCommand::RunAsSetModeCommand, this)},
105 {"wakeup", std::bind(&PowerShellCommand::RunAsWakeupCommand, this)},
106 {"suspend", std::bind(&PowerShellCommand::RunAsSuspendCommand, this)},
107 #ifdef HAS_DISPLAY_MANAGER_PART
108 {"display", std::bind(&PowerShellCommand::RunAsDisplayCommand, this)},
109 #endif
110 {"timeout", std::bind(&PowerShellCommand::RunAsTimeOutCommand, this)},
111 {"dump", std::bind(&PowerShellCommand::RunAsDumpCommand, this)},
112 };
113
114 #ifdef HAS_DISPLAY_MANAGER_PART
115 commandDisplay_ = {
116 {'h', std::bind(&PowerShellCommand::RunAsDisplayCommandHelp, this)},
117 {'r', std::bind(&PowerShellCommand::RunAsDisplayCommandRestore, this)},
118 {'s', std::bind(&PowerShellCommand::RunAsDisplayCommandSetValue, this)},
119 {'o', std::bind(&PowerShellCommand::RunAsDisplayCommandOverride, this)},
120 {'b', std::bind(&PowerShellCommand::RunAsDisplayCommandBoost, this)},
121 {'c', std::bind(&PowerShellCommand::RunAsDisplayCommandCancelBoost, this)},
122 {'d', std::bind(&PowerShellCommand::RunAsDisplayCommandDiscount, this)},
123 {'e', std::bind(&PowerShellCommand::RunAsDisplayCommandDelay, this)},
124 };
125 #endif
126
127 return ERR_OK;
128 }
129
CreateMessageMap()130 ErrCode PowerShellCommand::CreateMessageMap()
131 {
132 messageMap_ = {};
133
134 return ERR_OK;
135 }
136
init()137 ErrCode PowerShellCommand::init()
138 {
139 return OHOS::ERR_OK;
140 }
141
RunAsHelpCommand()142 ErrCode PowerShellCommand::RunAsHelpCommand()
143 {
144 resultReceiver_.clear();
145 resultReceiver_.append(HELP_MSG);
146 return ERR_OK;
147 }
148
RunAsSetModeCommand()149 ErrCode PowerShellCommand::RunAsSetModeCommand()
150 {
151 int ind = 0;
152 int option = getopt_long(argc_, argv_, "h", SET_MODE_OPTIONS, &ind);
153 resultReceiver_.clear();
154 if (option == 'h') {
155 resultReceiver_.append(SETMODE_HELP_MSG);
156 return ERR_OK;
157 }
158 if (argList_.empty()) {
159 resultReceiver_.append("Error! please input your mode value. \n");
160 resultReceiver_.append(SETMODE_HELP_MSG);
161 return ERR_OK;
162 }
163
164 auto mode = static_cast<uint32_t>(strtol(argList_[0].c_str(), nullptr, 0));
165 resultReceiver_.append("Set Mode: ");
166 resultReceiver_.append(argList_[0]);
167 resultReceiver_.append("\n");
168 PowerMgrClient& client = PowerMgrClient::GetInstance();
169 client.SetDeviceMode(static_cast<PowerMode>(mode));
170 uint32_t result = static_cast<uint32_t>(client.GetDeviceMode());
171 if (result == mode) {
172 resultReceiver_.append("Set Mode Success!\n");
173 } else {
174 resultReceiver_.append("Set Mode Failed, current mode is: ");
175 resultReceiver_.append(std::to_string(result));
176 resultReceiver_.append("\n");
177 }
178
179 return ERR_OK;
180 }
181
RunAsWakeupCommand()182 ErrCode PowerShellCommand::RunAsWakeupCommand()
183 {
184 PowerMgrClient& client = PowerMgrClient::GetInstance();
185 std::string detail = "shell";
186 client.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_POWER_BUTTON, detail);
187 resultReceiver_.append("WakeupDevice is called\n");
188 return ERR_OK;
189 }
190
RunAsSuspendCommand()191 ErrCode PowerShellCommand::RunAsSuspendCommand()
192 {
193 PowerMgrClient& client = PowerMgrClient::GetInstance();
194 client.SuspendDevice(SuspendDeviceType::SUSPEND_DEVICE_REASON_POWER_KEY);
195 resultReceiver_.append("SuspendDevice is called\n");
196 return ERR_OK;
197 }
198
PrintDumpFileError(std::string & receiver,const char * path)199 extern "C" void PrintDumpFileError(std::string& receiver, const char* path)
200 {
201 receiver.append("Open Dump file (");
202 receiver.append(path);
203 receiver.append(") failed: ");
204 receiver.append(std::to_string(errno));
205 receiver.append("\n");
206 }
207
RunAsDumpCommand()208 ErrCode PowerShellCommand::RunAsDumpCommand()
209 {
210 resultReceiver_.clear();
211
212 PowerMgrClient& client = PowerMgrClient::GetInstance();
213 std::string ret = client.Dump(argList_);
214 resultReceiver_.append("Power Dump result: \n");
215 resultReceiver_.append(ret);
216
217 return ERR_OK;
218 }
219
220 #ifdef HAS_DISPLAY_MANAGER_PART
221 using namespace OHOS::DisplayPowerMgr;
DisplayOptargEmpty()222 bool PowerShellCommand::DisplayOptargEmpty()
223 {
224 if (!optarg) {
225 resultReceiver_.append("Error! please input your brightness value.\n");
226 resultReceiver_.append(DISPLAY_HELP_MSG);
227 return true;
228 }
229 return false;
230 }
231
RunAsDisplayCommandHelp()232 ErrCode PowerShellCommand::RunAsDisplayCommandHelp()
233 {
234 resultReceiver_.append(DISPLAY_HELP_MSG);
235 return ERR_OK;
236 }
237
RunAsDisplayCommandOverride()238 ErrCode PowerShellCommand::RunAsDisplayCommandOverride()
239 {
240 if (DisplayOptargEmpty()) {
241 return ERR_OK;
242 }
243 int32_t value = 0;
244 StrToInt(optarg, value);
245 bool ret = DisplayPowerMgrClient::GetInstance().OverrideBrightness(static_cast<uint32_t>(value));
246 resultReceiver_.append("Override brightness to ");
247 resultReceiver_.append(std::to_string(value));
248 if (!ret) {
249 resultReceiver_.append(" failed");
250 }
251 resultReceiver_.append("\n");
252 return ERR_OK;
253 }
254
RunAsDisplayCommandRestore()255 ErrCode PowerShellCommand::RunAsDisplayCommandRestore()
256 {
257 bool ret = DisplayPowerMgrClient::GetInstance().RestoreBrightness();
258 resultReceiver_.append("Restore brightness");
259 if (!ret) {
260 resultReceiver_.append(" failed");
261 }
262 resultReceiver_.append("\n");
263 return ERR_OK;
264 }
265
RunAsDisplayCommandBoost()266 ErrCode PowerShellCommand::RunAsDisplayCommandBoost()
267 {
268 if (DisplayOptargEmpty()) {
269 return ERR_OK;
270 }
271 int32_t value = 0;
272 StrToInt(optarg, value);
273 bool ret = DisplayPowerMgrClient::GetInstance().BoostBrightness(static_cast<uint32_t>(value));
274 resultReceiver_.append("Boost brightness timeout ");
275 resultReceiver_.append(std::to_string(value)).append("ms");
276 if (!ret) {
277 resultReceiver_.append(" failed");
278 }
279 resultReceiver_.append("\n");
280 return ERR_OK;
281 }
282
RunAsDisplayCommandCancelBoost()283 ErrCode PowerShellCommand::RunAsDisplayCommandCancelBoost()
284 {
285 bool ret = DisplayPowerMgrClient::GetInstance().CancelBoostBrightness();
286 resultReceiver_.append("Cancel boost brightness");
287 if (!ret) {
288 resultReceiver_.append(" failed");
289 }
290 resultReceiver_.append("\n");
291 return ERR_OK;
292 }
293
RunAsDisplayCommandSetValue()294 ErrCode PowerShellCommand::RunAsDisplayCommandSetValue()
295 {
296 if (DisplayOptargEmpty()) {
297 return ERR_OK;
298 }
299 int32_t value = 0;
300 StrToInt(optarg, value);
301 bool ret = DisplayPowerMgrClient::GetInstance().SetBrightness(static_cast<uint32_t>(value));
302 resultReceiver_.append("Set brightness to ");
303 resultReceiver_.append(std::to_string(value));
304 if (!ret) {
305 resultReceiver_.append(" failed");
306 }
307 resultReceiver_.append("\n");
308 return ERR_OK;
309 }
310
RunAsDisplayCommandDiscount()311 ErrCode PowerShellCommand::RunAsDisplayCommandDiscount()
312 {
313 if (DisplayOptargEmpty()) {
314 return ERR_OK;
315 }
316 std::stringstream fstr(optarg);
317 double discount = 0;
318 fstr >> discount;
319 bool ret = DisplayPowerMgrClient::GetInstance().DiscountBrightness(discount);
320 resultReceiver_.append("Set brightness discount to ");
321 resultReceiver_.append(std::to_string(discount));
322 if (!ret) {
323 resultReceiver_.append(" failed");
324 }
325 resultReceiver_.append("\n");
326 return ERR_OK;
327 }
328
RunAsDisplayCommandDelay()329 ErrCode PowerShellCommand::RunAsDisplayCommandDelay()
330 {
331 if (DisplayOptargEmpty()) {
332 return ERR_OK;
333 }
334 int32_t value = 0;
335 StrToInt(optarg, value);
336 bool ret = DisplayPowerMgrClient::GetInstance().OverrideDisplayOffDelay(static_cast<uint32_t>(value));
337 resultReceiver_.append("Override delay time to ");
338 resultReceiver_.append(std::to_string(value));
339 if (!ret) {
340 resultReceiver_.append(" failed");
341 }
342 resultReceiver_.append("\n");
343 return ERR_OK;
344 }
345
RunAsDisplayCommand()346 ErrCode PowerShellCommand::RunAsDisplayCommand()
347 {
348 int ind = 0;
349 int option = getopt_long(argc_, argv_, "hrcs:o:b:d:e:", DISPLAY_OPTIONS, &ind);
350 resultReceiver_.clear();
351 auto item = commandDisplay_.find(option);
352 if (item != commandDisplay_.end()) {
353 return item->second();
354 }
355 resultReceiver_.append(DISPLAY_HELP_MSG);
356 return ERR_OK;
357 }
358 #endif
359
RunAsTimeOutCommand()360 ErrCode PowerShellCommand::RunAsTimeOutCommand()
361 {
362 int ind = 0;
363 int option = getopt_long(argc_, argv_, "hro:", TIME_OUT_OPTIONS, &ind);
364 resultReceiver_.clear();
365 if (option == 'h') {
366 resultReceiver_.append(TIME_OUT_HELP_MSG);
367 return ERR_OK;
368 }
369 if (option == 'r') {
370 bool ret = PowerMgrClient::GetInstance().RestoreScreenOffTime();
371 resultReceiver_.append("Restore screen off time");
372 if (!ret) {
373 resultReceiver_.append(" failed");
374 }
375 resultReceiver_.append("\n");
376 return ERR_OK;
377 }
378 if (!optarg) {
379 resultReceiver_.append("Error! please input your screen off time.\n");
380 resultReceiver_.append(TIME_OUT_HELP_MSG);
381 return ERR_OK;
382 }
383 if (option == 'o') {
384 int32_t timeout = 0;
385 StrToInt(optarg, timeout);
386 bool ret = PowerMgrClient::GetInstance().OverrideScreenOffTime(static_cast<int64_t>(timeout));
387 resultReceiver_.append("Override screen off time to ");
388 resultReceiver_.append(std::to_string(timeout));
389 if (!ret) {
390 resultReceiver_.append(" failed");
391 }
392 resultReceiver_.append("\n");
393 return ERR_OK;
394 }
395 return ERR_OK;
396 }
397 } // namespace PowerMgr
398 } // namespace OHOS
399