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