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