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_mgr_proxy.h"
17
18 #include <message_parcel.h>
19 #include <string_ex.h>
20 #include "message_option.h"
21 #include "shutdown_proxy_delegator.h"
22 #include "power_log.h"
23 #include "power_common.h"
24 #include "power_mgr_ipc_interface_code.h"
25
26 namespace OHOS {
27 namespace PowerMgr {
CreateRunningLock(const sptr<IRemoteObject> & remoteObj,const RunningLockInfo & runningLockInfo)28 PowerErrors PowerMgrProxy::CreateRunningLock(const sptr<IRemoteObject>& remoteObj,
29 const RunningLockInfo& runningLockInfo)
30 {
31 sptr<IRemoteObject> remote = Remote();
32 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
33
34 MessageParcel data;
35 MessageParcel reply;
36 MessageOption option;
37
38 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
39 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
40 return PowerErrors::ERR_CONNECTION_FAIL;
41 }
42
43 WRITE_PARCEL_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), PowerErrors::ERR_CONNECTION_FAIL);
44 WRITE_PARCEL_WITH_RET(data, Parcelable, &runningLockInfo, PowerErrors::ERR_CONNECTION_FAIL);
45
46 int ret = remote->SendRequest(
47 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::CREATE_RUNNINGLOCK),
48 data, reply, option);
49 if (ret != ERR_OK) {
50 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
51 return PowerErrors::ERR_CONNECTION_FAIL;
52 }
53 int32_t error;
54 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
55 return static_cast<PowerErrors>(error);
56 }
57
ReleaseRunningLock(const sptr<IRemoteObject> & remoteObj)58 bool PowerMgrProxy::ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj)
59 {
60 sptr<IRemoteObject> remote = Remote();
61 RETURN_IF_WITH_RET(remote == nullptr, false);
62
63 MessageParcel data;
64 MessageParcel reply;
65 MessageOption option;
66
67 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
68 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
69 return false;
70 }
71
72 WRITE_PARCEL_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), false);
73
74 int ret = remote->SendRequest(
75 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RELEASE_RUNNINGLOCK),
76 data, reply, option);
77 if (ret != ERR_OK) {
78 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
79 return false;
80 }
81 return true;
82 }
83
IsRunningLockTypeSupported(RunningLockType type)84 bool PowerMgrProxy::IsRunningLockTypeSupported(RunningLockType type)
85 {
86 sptr<IRemoteObject> remote = Remote();
87 RETURN_IF_WITH_RET(remote == nullptr, false);
88
89 bool result = false;
90 MessageParcel data;
91 MessageParcel reply;
92 MessageOption option;
93
94 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
95 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
96 return result;
97 }
98
99 WRITE_PARCEL_WITH_RET(data, Uint32, static_cast<uint32_t>(type), false);
100 int ret = remote->SendRequest(
101 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_RUNNINGLOCK_TYPE_SUPPORTED),
102 data, reply, option);
103 if (ret != ERR_OK) {
104 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
105 return result;
106 }
107
108 if (!reply.ReadBool(result)) {
109 POWER_HILOGE(FEATURE_RUNNING_LOCK, "ReadBool fail");
110 }
111
112 return result;
113 }
114
Lock(const sptr<IRemoteObject> & remoteObj,int32_t timeOutMs)115 bool PowerMgrProxy::Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMs)
116 {
117 sptr<IRemoteObject> remote = Remote();
118 RETURN_IF_WITH_RET(remote == nullptr, false);
119
120 MessageParcel data;
121 MessageParcel reply;
122 MessageOption option;
123
124 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
125 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
126 return false;
127 }
128
129 WRITE_PARCEL_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), false);
130 WRITE_PARCEL_WITH_RET(data, Int32, timeOutMs, false);
131
132 int ret = remote->SendRequest(
133 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_LOCK),
134 data, reply, option);
135 if (ret != ERR_OK) {
136 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
137 return false;
138 }
139 return true;
140 }
141
UnLock(const sptr<IRemoteObject> & remoteObj)142 bool PowerMgrProxy::UnLock(const sptr<IRemoteObject>& remoteObj)
143 {
144 sptr<IRemoteObject> remote = Remote();
145 RETURN_IF_WITH_RET(remote == nullptr, false);
146
147 MessageParcel data;
148 MessageParcel reply;
149 MessageOption option;
150
151 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
152 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
153 return false;
154 }
155
156 WRITE_PARCEL_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), false);
157
158 int ret = remote->SendRequest(
159 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_UNLOCK),
160 data, reply, option);
161 if (ret != ERR_OK) {
162 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
163 return false;
164 }
165 return true;
166 }
167
IsUsed(const sptr<IRemoteObject> & remoteObj)168 bool PowerMgrProxy::IsUsed(const sptr<IRemoteObject>& remoteObj)
169 {
170 sptr<IRemoteObject> remote = Remote();
171 RETURN_IF_WITH_RET(remote == nullptr, false);
172
173 MessageParcel data;
174 MessageParcel reply;
175 MessageOption option;
176
177 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
178 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
179 return false;
180 }
181
182 WRITE_PARCEL_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), false);
183 int ret = remote->SendRequest(
184 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_ISUSED),
185 data, reply, option);
186 if (ret != ERR_OK) {
187 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
188 return false;
189 }
190 bool used = false;
191 READ_PARCEL_WITH_RET(reply, Bool, used, false);
192 return used;
193 }
194
ProxyRunningLock(bool isProxied,pid_t pid,pid_t uid)195 bool PowerMgrProxy::ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)
196 {
197 sptr<IRemoteObject> remote = Remote();
198 RETURN_IF_WITH_RET(remote == nullptr, false);
199
200 MessageParcel data;
201 MessageParcel reply;
202 MessageOption option;
203
204 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
205 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
206 return false;
207 }
208
209 WRITE_PARCEL_WITH_RET(data, Bool, isProxied, false);
210 WRITE_PARCEL_WITH_RET(data, Int32, pid, false);
211 WRITE_PARCEL_WITH_RET(data, Int32, uid, false);
212 int ret = remote->SendRequest(
213 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCK),
214 data, reply, option);
215 if (ret != ERR_OK) {
216 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
217 return false;
218 }
219 bool succ = false;
220 READ_PARCEL_WITH_RET(reply, Bool, succ, false);
221 return succ;
222 }
223
ProxyRunningLocks(bool isProxied,const std::vector<std::pair<pid_t,pid_t>> & processInfos)224 bool PowerMgrProxy::ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)
225 {
226 sptr<IRemoteObject> remote = Remote();
227 RETURN_IF_WITH_RET(remote == nullptr, false);
228
229 MessageParcel data;
230 MessageParcel reply;
231 MessageOption option;
232
233 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
234 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
235 return false;
236 }
237
238 size_t size = processInfos.size();
239 WRITE_PARCEL_WITH_RET(data, Bool, isProxied, false);
240 WRITE_PARCEL_WITH_RET(data, Int32, size, false);
241 for (size_t i = 0; i < size; ++i) {
242 WRITE_PARCEL_WITH_RET(data, Int32, processInfos[i].first, false);
243 WRITE_PARCEL_WITH_RET(data, Int32, processInfos[i].second, false);
244 }
245 int ret = remote->SendRequest(static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCKS),
246 data, reply, option);
247 if (ret != ERR_OK) {
248 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
249 return false;
250 }
251 bool succ = false;
252 READ_PARCEL_WITH_RET(reply, Bool, succ, false);
253 return succ;
254 }
255
ResetRunningLocks()256 bool PowerMgrProxy::ResetRunningLocks()
257 {
258 sptr<IRemoteObject> remote = Remote();
259 RETURN_IF_WITH_RET(remote == nullptr, false);
260
261 MessageParcel data;
262 MessageParcel reply;
263 MessageOption option;
264
265 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
266 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
267 return false;
268 }
269
270 int ret = remote->SendRequest(static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESET_RUNNINGLOCKS),
271 data, reply, option);
272 if (ret != ERR_OK) {
273 POWER_HILOGE(FEATURE_RUNNING_LOCK, "SendRequest is failed, ret: %{public}d", ret);
274 return false;
275 }
276 bool succ = false;
277 READ_PARCEL_WITH_RET(reply, Bool, succ, false);
278 return succ;
279 }
280
RebootDevice(const std::string & reason)281 PowerErrors PowerMgrProxy::RebootDevice(const std::string& reason)
282 {
283 sptr<IRemoteObject> remote = Remote();
284 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
285
286 MessageParcel data;
287 MessageParcel reply;
288 MessageOption option;
289
290 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
291 POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
292 return PowerErrors::ERR_CONNECTION_FAIL;
293 }
294
295 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(reason), PowerErrors::ERR_CONNECTION_FAIL);
296
297 int ret = remote->SendRequest(
298 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE), data, reply, option);
299 if (ret != ERR_OK) {
300 POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret: %{public}d", ret);
301 return PowerErrors::ERR_CONNECTION_FAIL;
302 }
303 int32_t error;
304 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
305 return static_cast<PowerErrors>(error);
306 }
307
RebootDeviceForDeprecated(const std::string & reason)308 PowerErrors PowerMgrProxy::RebootDeviceForDeprecated(const std::string& reason)
309 {
310 sptr<IRemoteObject> remote = Remote();
311 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
312
313 MessageParcel data;
314 MessageParcel reply;
315 MessageOption option;
316
317 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
318 POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
319 return PowerErrors::ERR_CONNECTION_FAIL;
320 }
321
322 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(reason), PowerErrors::ERR_CONNECTION_FAIL);
323
324 int ret = remote->SendRequest(
325 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE_FOR_DEPRECATED),
326 data, reply, option);
327 if (ret != ERR_OK) {
328 POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret: %{public}d", ret);
329 return PowerErrors::ERR_CONNECTION_FAIL;
330 }
331 int32_t error;
332 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
333 return static_cast<PowerErrors>(error);
334 }
335
ShutDownDevice(const std::string & reason)336 PowerErrors PowerMgrProxy::ShutDownDevice(const std::string& reason)
337 {
338 sptr<IRemoteObject> remote = Remote();
339 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
340
341 MessageParcel data;
342 MessageParcel reply;
343 MessageOption option;
344
345 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
346 POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
347 return PowerErrors::ERR_CONNECTION_FAIL;
348 }
349
350 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(reason), PowerErrors::ERR_CONNECTION_FAIL);
351
352 int ret = remote->SendRequest(
353 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHUTDOWN_DEVICE), data, reply, option);
354 if (ret != ERR_OK) {
355 POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret: %{public}d", ret);
356 return PowerErrors::ERR_CONNECTION_FAIL;
357 }
358 int32_t error;
359 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
360 return static_cast<PowerErrors>(error);
361 }
362
SuspendDevice(int64_t callTimeMs,SuspendDeviceType reason,bool suspendImmed)363 PowerErrors PowerMgrProxy::SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed)
364 {
365 sptr<IRemoteObject> remote = Remote();
366 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
367
368 MessageParcel data;
369 MessageParcel reply;
370 MessageOption option;
371
372 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
373 POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
374 return PowerErrors::ERR_CONNECTION_FAIL;
375 }
376
377 WRITE_PARCEL_WITH_RET(data, Int64, callTimeMs, PowerErrors::ERR_CONNECTION_FAIL);
378 WRITE_PARCEL_WITH_RET(data, Uint32, static_cast<uint32_t>(reason), PowerErrors::ERR_CONNECTION_FAIL);
379 WRITE_PARCEL_WITH_RET(data, Bool, suspendImmed, PowerErrors::ERR_CONNECTION_FAIL);
380
381 int ret = remote->SendRequest(
382 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SUSPEND_DEVICE), data, reply, option);
383 if (ret != ERR_OK) {
384 POWER_HILOGE(FEATURE_SUSPEND, "SendRequest is failed, ret: %{public}d", ret);
385 return PowerErrors::ERR_CONNECTION_FAIL;
386 }
387 int32_t error;
388 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
389 return static_cast<PowerErrors>(error);
390 }
391
WakeupDevice(int64_t callTimeMs,WakeupDeviceType reason,const std::string & details)392 PowerErrors PowerMgrProxy::WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details)
393 {
394 sptr<IRemoteObject> remote = Remote();
395 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
396
397 MessageParcel data;
398 MessageParcel reply;
399 MessageOption option;
400
401 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
402 POWER_HILOGE(FEATURE_WAKEUP, "Write descriptor failed");
403 return PowerErrors::ERR_CONNECTION_FAIL;
404 }
405
406 WRITE_PARCEL_WITH_RET(data, Int64, callTimeMs, PowerErrors::ERR_CONNECTION_FAIL);
407 WRITE_PARCEL_WITH_RET(data, Uint32, static_cast<uint32_t>(reason), PowerErrors::ERR_CONNECTION_FAIL);
408 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(details), PowerErrors::ERR_CONNECTION_FAIL);
409
410 int ret = remote->SendRequest(
411 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::WAKEUP_DEVICE), data, reply, option);
412 if (ret != ERR_OK) {
413 POWER_HILOGE(FEATURE_WAKEUP, "SendRequest is failed, ret: %{public}d", ret);
414 return PowerErrors::ERR_CONNECTION_FAIL;
415 }
416 int32_t error;
417 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
418 return static_cast<PowerErrors>(error);
419 }
420
RefreshActivity(int64_t callTimeMs,UserActivityType type,bool needChangeBacklight)421 bool PowerMgrProxy::RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight)
422 {
423 sptr<IRemoteObject> remote = Remote();
424 RETURN_IF_WITH_RET(remote == nullptr, false);
425
426 MessageParcel data;
427 MessageParcel reply;
428 MessageOption option;
429
430 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
431 POWER_HILOGE(FEATURE_ACTIVITY, "Write descriptor failed");
432 return false;
433 }
434
435 WRITE_PARCEL_WITH_RET(data, Int64, callTimeMs, false);
436 WRITE_PARCEL_WITH_RET(data, Uint32, static_cast<uint32_t>(type), false);
437 WRITE_PARCEL_WITH_RET(data, Bool, needChangeBacklight, false);
438
439 int ret = remote->SendRequest(
440 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REFRESH_ACTIVITY), data, reply, option);
441 if (ret != ERR_OK) {
442 POWER_HILOGE(FEATURE_ACTIVITY, "SendRequest is failed, ret: %{public}d", ret);
443 return false;
444 }
445 return true;
446 }
447
OverrideScreenOffTime(int64_t timeout)448 bool PowerMgrProxy::OverrideScreenOffTime(int64_t timeout)
449 {
450 sptr<IRemoteObject> remote = Remote();
451 RETURN_IF_WITH_RET(remote == nullptr, false);
452
453 bool result = false;
454 MessageParcel data;
455 MessageParcel reply;
456 MessageOption option;
457
458 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
459 POWER_HILOGE(COMP_SVC, "Write descriptor failed");
460 return result;
461 }
462
463 WRITE_PARCEL_WITH_RET(data, Int64, timeout, false);
464
465 int ret = remote->SendRequest(
466 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::OVERRIDE_DISPLAY_OFF_TIME),
467 data, reply, option);
468 if (ret != ERR_OK) {
469 POWER_HILOGE(COMP_SVC, "SendRequest is failed, ret: %{public}d", ret);
470 return result;
471 }
472 if (!reply.ReadBool(result)) {
473 POWER_HILOGE(COMP_SVC, "ReadBool fail");
474 }
475
476 return result;
477 }
478
RestoreScreenOffTime()479 bool PowerMgrProxy::RestoreScreenOffTime()
480 {
481 sptr<IRemoteObject> remote = Remote();
482 RETURN_IF_WITH_RET(remote == nullptr, false);
483
484 bool result = false;
485 MessageParcel data;
486 MessageParcel reply;
487 MessageOption option;
488
489 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
490 POWER_HILOGE(COMP_FWK, "Write descriptor failed");
491 return result;
492 }
493
494 int ret = remote->SendRequest(
495 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESTORE_DISPLAY_OFF_TIME),
496 data, reply, option);
497 if (ret != ERR_OK) {
498 POWER_HILOGE(COMP_FWK, "SendRequest is failed, ret: %{public}d", ret);
499 return result;
500 }
501 if (!reply.ReadBool(result)) {
502 POWER_HILOGE(COMP_FWK, "ReadBool fail");
503 }
504
505 return result;
506 }
507
ForceSuspendDevice(int64_t callTimeMs)508 bool PowerMgrProxy::ForceSuspendDevice(int64_t callTimeMs)
509 {
510 sptr<IRemoteObject> remote = Remote();
511 RETURN_IF_WITH_RET(remote == nullptr, false);
512
513 bool result = false;
514 MessageParcel data;
515 MessageParcel reply;
516 MessageOption option;
517
518 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
519 POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
520 return result;
521 }
522
523 WRITE_PARCEL_WITH_RET(data, Int64, callTimeMs, false);
524
525 int ret = remote->SendRequest(
526 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::FORCE_DEVICE_SUSPEND), data, reply, option);
527 if (ret != ERR_OK) {
528 POWER_HILOGE(FEATURE_SUSPEND, "SendRequest is failed, ret: %{public}d", ret);
529 return result;
530 }
531 if (!reply.ReadBool(result)) {
532 POWER_HILOGE(FEATURE_SUSPEND, "ReadBool fail");
533 }
534
535 return result;
536 }
537
GetState()538 PowerState PowerMgrProxy::GetState()
539 {
540 sptr<IRemoteObject> remote = Remote();
541 RETURN_IF_WITH_RET(remote == nullptr, PowerState::UNKNOWN);
542
543 uint32_t result = 0;
544 MessageParcel data;
545 MessageParcel reply;
546 MessageOption option;
547
548 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
549 POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
550 return PowerState::UNKNOWN;
551 }
552
553 int ret = remote->SendRequest(
554 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GET_STATE), data, reply, option);
555 if (ret != ERR_OK) {
556 POWER_HILOGE(FEATURE_POWER_STATE, "SendRequest is failed, ret: %{public}d", ret);
557 return PowerState::UNKNOWN;
558 }
559 if (!reply.ReadUint32(result)) {
560 POWER_HILOGE(FEATURE_POWER_STATE, "ReadUint32 failed");
561 return PowerState::UNKNOWN;
562 }
563
564 return static_cast<PowerState>(result);
565 }
566
IsScreenOn()567 bool PowerMgrProxy::IsScreenOn()
568 {
569 sptr<IRemoteObject> remote = Remote();
570 RETURN_IF_WITH_RET(remote == nullptr, false);
571
572 bool result = false;
573 MessageParcel data;
574 MessageParcel reply;
575 MessageOption option;
576
577 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
578 POWER_HILOGE(COMP_FWK, "Write descriptor failed");
579 return result;
580 }
581
582 int ret = remote->SendRequest(
583 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_SCREEN_ON), data, reply, option);
584 if (ret != ERR_OK) {
585 POWER_HILOGE(COMP_FWK, "SendRequest is failed, ret: %{public}d", ret);
586 return result;
587 }
588 if (!reply.ReadBool(result)) {
589 POWER_HILOGE(COMP_FWK, "ReadBool fail");
590 }
591
592 return result;
593 }
594
RegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback)595 bool PowerMgrProxy::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
596 {
597 sptr<IRemoteObject> remote = Remote();
598 RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
599
600 MessageParcel data;
601 MessageParcel reply;
602 MessageOption option;
603
604 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
605 POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
606 return false;
607 }
608
609 WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
610
611 int ret = remote->SendRequest(
612 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_STATE_CALLBACK),
613 data, reply, option);
614 if (ret != ERR_OK) {
615 POWER_HILOGE(FEATURE_POWER_STATE, "SendRequest is failed, ret: %{public}d", ret);
616 return false;
617 }
618 return true;
619 }
620
UnRegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback)621 bool PowerMgrProxy::UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
622 {
623 sptr<IRemoteObject> remote = Remote();
624 RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
625
626 MessageParcel data;
627 MessageParcel reply;
628 MessageOption option;
629
630 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
631 POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
632 return false;
633 }
634
635 WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
636
637 int ret = remote->SendRequest(
638 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_STATE_CALLBACK),
639 data, reply, option);
640 if (ret != ERR_OK) {
641 POWER_HILOGE(FEATURE_POWER_STATE, "SendRequest is failed, ret: %{public}d", ret);
642 return false;
643 }
644 return true;
645 }
646
RegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)647 bool PowerMgrProxy::RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
648 {
649 sptr<IRemoteObject> remote = Remote();
650 RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
651
652 MessageParcel data;
653 MessageParcel reply;
654 MessageOption option;
655
656 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
657 POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
658 return false;
659 }
660
661 WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
662
663 int ret = remote->SendRequest(
664 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_MODE_CALLBACK),
665 data, reply, option);
666 if (ret != ERR_OK) {
667 POWER_HILOGE(FEATURE_POWER_MODE, "SendRequest is failed, ret: %{public}d", ret);
668 return false;
669 }
670 return true;
671 }
672
UnRegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)673 bool PowerMgrProxy::UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
674 {
675 sptr<IRemoteObject> remote = Remote();
676 RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
677
678 MessageParcel data;
679 MessageParcel reply;
680 MessageOption option;
681
682 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
683 POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
684 return false;
685 }
686
687 WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
688
689 int ret = remote->SendRequest(
690 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_MODE_CALLBACK),
691 data, reply, option);
692 if (ret != ERR_OK) {
693 POWER_HILOGE(FEATURE_POWER_MODE, "SendRequest is failed, ret: %{public}d", ret);
694 return false;
695 }
696 return true;
697 }
698
SetDisplaySuspend(bool enable)699 bool PowerMgrProxy::SetDisplaySuspend(bool enable)
700 {
701 sptr<IRemoteObject> remote = Remote();
702 RETURN_IF_WITH_RET(remote == nullptr, false);
703
704 MessageParcel data;
705 MessageParcel reply;
706 MessageOption option;
707
708 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
709 POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
710 return false;
711 }
712
713 WRITE_PARCEL_WITH_RET(data, Bool, enable, false);
714
715 int ret = remote->SendRequest(
716 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_DISPLAY_SUSPEND),
717 data, reply, option);
718 if (ret != ERR_OK) {
719 POWER_HILOGE(FEATURE_SUSPEND, "SendRequest is failed, ret: %{public}d", ret);
720 return false;
721 }
722 return true;
723 }
724
SetDeviceMode(const PowerMode & mode)725 PowerErrors PowerMgrProxy::SetDeviceMode(const PowerMode& mode)
726 {
727 sptr<IRemoteObject> remote = Remote();
728 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
729
730 MessageParcel data;
731 MessageParcel reply;
732 MessageOption option;
733
734 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
735 POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
736 return PowerErrors::ERR_CONNECTION_FAIL;
737 }
738
739 WRITE_PARCEL_WITH_RET(data, Uint32, static_cast<uint32_t>(mode), PowerErrors::ERR_CONNECTION_FAIL);
740
741 int ret = remote->SendRequest(
742 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SETMODE_DEVICE), data, reply, option);
743 if (ret != ERR_OK) {
744 POWER_HILOGE(FEATURE_POWER_MODE, "SendRequest is failed, ret: %{public}d", ret);
745 return PowerErrors::ERR_CONNECTION_FAIL;
746 }
747 int32_t error;
748 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
749 return static_cast<PowerErrors>(error);
750 }
751
GetDeviceMode()752 PowerMode PowerMgrProxy::GetDeviceMode()
753 {
754 sptr<IRemoteObject> remote = Remote();
755 RETURN_IF_WITH_RET(remote == nullptr, static_cast<PowerMode>(false));
756
757 MessageParcel data;
758 MessageParcel reply;
759 MessageOption option;
760
761 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
762 POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
763 return PowerMode::NORMAL_MODE;
764 }
765
766 int ret = remote->SendRequest(
767 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GETMODE_DEVICE),
768 data, reply, option);
769 if (ret != ERR_OK) {
770 POWER_HILOGE(FEATURE_POWER_MODE, "SendRequest is failed, ret: %{public}d", ret);
771 return PowerMode::NORMAL_MODE;
772 }
773
774 uint32_t used = static_cast<uint32_t>(PowerMode::NORMAL_MODE);
775 if (!reply.ReadUint32(used)) {
776 POWER_HILOGE(FEATURE_POWER_MODE, "ReadUint32 fail");
777 }
778 return static_cast<PowerMode>(used);
779 }
780
ShellDump(const std::vector<std::string> & args,uint32_t argc)781 std::string PowerMgrProxy::ShellDump(const std::vector<std::string>& args, uint32_t argc)
782 {
783 sptr<IRemoteObject> remote = Remote();
784 std::string result = "remote error";
785 RETURN_IF_WITH_RET(remote == nullptr, result);
786
787 MessageParcel data;
788 MessageParcel reply;
789 MessageOption option;
790
791 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
792 POWER_HILOGE(COMP_FWK, "Write descriptor failed");
793 return result;
794 }
795
796 data.WriteUint32(argc);
797 for (uint32_t i = 0; i < argc; i++) {
798 data.WriteString(args[i]);
799 }
800 int ret = remote->SendRequest(
801 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHELL_DUMP), data, reply, option);
802 if (ret != ERR_OK) {
803 POWER_HILOGE(COMP_FWK, "SendRequest is failed, ret: %{public}d", ret);
804 return result;
805 }
806 result = reply.ReadString();
807
808 return result;
809 }
810
IsStandby(bool & isStandby)811 PowerErrors PowerMgrProxy::IsStandby(bool& isStandby)
812 {
813 sptr<IRemoteObject> remote = Remote();
814 RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
815
816 MessageParcel data;
817 MessageParcel reply;
818 MessageOption option;
819
820 if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
821 POWER_HILOGE(COMP_FWK, "Write descriptor failed");
822 return PowerErrors::ERR_CONNECTION_FAIL;
823 }
824
825 int32_t ret = remote->SendRequest(
826 static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_STANDBY), data, reply, option);
827 if (ret != ERR_OK) {
828 POWER_HILOGE(COMP_FWK, "SendRequest is failed, ret: %{public}d", ret);
829 return PowerErrors::ERR_CONNECTION_FAIL;
830 }
831
832 int32_t error;
833
834 READ_PARCEL_WITH_RET(reply, Int32, error, PowerErrors::ERR_CONNECTION_FAIL);
835 READ_PARCEL_WITH_RET(reply, Bool, isStandby, PowerErrors::ERR_CONNECTION_FAIL);
836
837 return static_cast<PowerErrors>(error);
838 }
839
RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback,ShutdownPriority priority)840 void PowerMgrProxy::RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority)
841 {
842 auto delegator = std::make_unique<ShutdownProxyDelegator>(Remote(), PowerMgrProxy::GetDescriptor());
843 delegator->RegisterShutdownCallback(callback, priority);
844 }
845
UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback)846 void PowerMgrProxy::UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback)
847 {
848 auto delegator = std::make_unique<ShutdownProxyDelegator>(Remote(), PowerMgrProxy::GetDescriptor());
849 delegator->UnRegisterShutdownCallback(callback);
850 }
851
RegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback,ShutdownPriority priority)852 void PowerMgrProxy::RegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority)
853 {
854 auto delegator = std::make_unique<ShutdownProxyDelegator>(Remote(), PowerMgrProxy::GetDescriptor());
855 delegator->RegisterShutdownCallback(callback, priority);
856 }
857
UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback)858 void PowerMgrProxy::UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback)
859 {
860 auto delegator = std::make_unique<ShutdownProxyDelegator>(Remote(), PowerMgrProxy::GetDescriptor());
861 delegator->UnRegisterShutdownCallback(callback);
862 }
863
RegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback,ShutdownPriority priority)864 void PowerMgrProxy::RegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority)
865 {
866 auto delegator = std::make_unique<ShutdownProxyDelegator>(Remote(), PowerMgrProxy::GetDescriptor());
867 delegator->RegisterShutdownCallback(callback, priority);
868 }
869
UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback)870 void PowerMgrProxy::UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback)
871 {
872 auto delegator = std::make_unique<ShutdownProxyDelegator>(Remote(), PowerMgrProxy::GetDescriptor());
873 delegator->UnRegisterShutdownCallback(callback);
874 }
875 } // namespace PowerMgr
876 } // namespace OHOS
877