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 #include "power_mgr_async_reply.h"
18 
19 #include <message_parcel.h>
20 #include <string_ex.h>
21 #include "message_option.h"
22 #include "shutdown_proxy_delegator.h"
23 #include "power_log.h"
24 #include "power_common.h"
25 #include "power_mgr_ipc_interface_code.h"
26 #include "running_lock_info.h"
27 
28 namespace OHOS {
29 namespace PowerMgr {
CreateRunningLock(const sptr<IRemoteObject> & remoteObj,const RunningLockInfo & runningLockInfo)30 PowerErrors PowerMgrProxy::CreateRunningLock(const sptr<IRemoteObject>& remoteObj,
31     const RunningLockInfo& runningLockInfo)
32 {
33     sptr<IRemoteObject> remote = Remote();
34     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
35 
36     MessageParcel data;
37     MessageParcel reply;
38     MessageOption option;
39 
40     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
41         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
42         return PowerErrors::ERR_CONNECTION_FAIL;
43     }
44 
45     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), PowerErrors::ERR_CONNECTION_FAIL);
46     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Parcelable, &runningLockInfo, PowerErrors::ERR_CONNECTION_FAIL);
47 
48     int ret = remote->SendRequest(
49         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::CREATE_RUNNINGLOCK),
50         data, reply, option);
51     if (ret != ERR_OK) {
52         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
53         return PowerErrors::ERR_CONNECTION_FAIL;
54     }
55     int32_t error;
56     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
57     return static_cast<PowerErrors>(error);
58 }
59 
ReleaseRunningLock(const sptr<IRemoteObject> & remoteObj,const std::string & name)60 bool PowerMgrProxy::ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj, const std::string& name)
61 {
62     sptr<IRemoteObject> remote = Remote();
63     RETURN_IF_WITH_RET(remote == nullptr, false);
64 
65     MessageParcel data;
66     MessageParcel reply;
67     MessageOption option;
68 
69     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
70         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
71         return false;
72     }
73 
74     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), false);
75     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String, name, false);
76 
77     int ret = remote->SendRequest(
78         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RELEASE_RUNNINGLOCK),
79         data, reply, option);
80     if (ret != ERR_OK) {
81         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
82         return false;
83     }
84     return true;
85 }
86 
IsRunningLockTypeSupported(RunningLockType type)87 bool PowerMgrProxy::IsRunningLockTypeSupported(RunningLockType type)
88 {
89     sptr<IRemoteObject> remote = Remote();
90     RETURN_IF_WITH_RET(remote == nullptr, false);
91 
92     bool result = false;
93     MessageParcel data;
94     MessageParcel reply;
95     MessageOption option;
96 
97     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
98         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
99         return result;
100     }
101 
102     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32, static_cast<uint32_t>(type), false);
103     int ret = remote->SendRequest(
104         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_RUNNINGLOCK_TYPE_SUPPORTED),
105         data, reply, option);
106     if (ret != ERR_OK) {
107         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
108         return result;
109     }
110 
111     if (!reply.ReadBool(result)) {
112         POWER_HILOGE(FEATURE_RUNNING_LOCK, "ReadBool fail");
113     }
114 
115     return result;
116 }
117 
UpdateWorkSource(const sptr<IRemoteObject> & remoteObj,const std::map<int32_t,std::string> & workSources)118 bool PowerMgrProxy::UpdateWorkSource(const sptr<IRemoteObject>& remoteObj,
119     const std::map<int32_t, std::string>& workSources)
120 {
121     sptr<IRemoteObject> remote = Remote();
122     RETURN_IF_WITH_RET(remote == nullptr, false);
123 
124     MessageParcel data;
125     MessageParcel reply;
126     MessageOption option = { MessageOption::TF_ASYNC };
127 
128     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
129         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
130         return false;
131     }
132 
133     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), false);
134     uint32_t size = workSources.size();
135     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32, size, false);
136     for (const auto& wks : workSources) {
137         RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, wks.first, false);
138         RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String, wks.second, false);
139     }
140 
141     int ret = remote->SendRequest(
142         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UPDATE_WORK_SOURCE),
143         data, reply, option);
144     if (ret != ERR_OK) {
145         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
146         return false;
147     }
148     return true;
149 }
150 
Lock(const sptr<IRemoteObject> & remoteObj,int32_t timeOutMs)151 PowerErrors PowerMgrProxy::Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMs)
152 {
153     sptr<IRemoteObject> remote = Remote();
154     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
155 
156     MessageParcel data;
157     MessageParcel reply;
158     MessageOption option;
159 
160     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
161         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
162         return PowerErrors::ERR_CONNECTION_FAIL;
163     }
164 
165     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(
166         data, RemoteObject, remoteObj.GetRefPtr(), PowerErrors::ERR_CONNECTION_FAIL);
167     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, timeOutMs, PowerErrors::ERR_CONNECTION_FAIL);
168 
169     int ret = remote->SendRequest(
170         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_LOCK),
171         data, reply, option);
172     if (ret != ERR_OK) {
173         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
174         return PowerErrors::ERR_CONNECTION_FAIL;
175     }
176     int32_t error;
177     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_CONNECTION_FAIL);
178     return static_cast<PowerErrors>(error);
179 }
180 
UnLock(const sptr<IRemoteObject> & remoteObj,const std::string & name)181 PowerErrors PowerMgrProxy::UnLock(const sptr<IRemoteObject>& remoteObj, const std::string& name)
182 {
183     sptr<IRemoteObject> remote = Remote();
184     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
185 
186     MessageParcel data;
187     MessageParcel reply;
188     MessageOption option;
189 
190     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
191         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
192         return PowerErrors::ERR_CONNECTION_FAIL;
193     }
194 
195     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(
196         data, RemoteObject, remoteObj.GetRefPtr(), PowerErrors::ERR_CONNECTION_FAIL);
197     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String, name, PowerErrors::ERR_CONNECTION_FAIL);
198 
199     int ret = remote->SendRequest(
200         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_UNLOCK),
201         data, reply, option);
202     if (ret != ERR_OK) {
203         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
204         return PowerErrors::ERR_CONNECTION_FAIL;
205     }
206     int32_t error;
207     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_CONNECTION_FAIL);
208     return static_cast<PowerErrors>(error);
209 }
210 
QueryRunningLockLists(std::map<std::string,RunningLockInfo> & runningLockLists)211 bool PowerMgrProxy::QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists)
212 {
213     sptr<IRemoteObject> remote = Remote();
214     RETURN_IF_WITH_RET(remote == nullptr, false);
215 
216     MessageParcel data;
217     MessageParcel reply;
218     MessageOption option;
219 
220     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
221         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
222         return false;
223     }
224 
225     int ret = remote->SendRequest(
226         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_QUERY),
227         data, reply, option);
228     if (ret != ERR_OK) {
229         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
230         return false;
231     }
232     int32_t num = reply.ReadInt32();
233     for (int i = 0; i < num; i++) {
234         std::string key = reply.ReadString();
235         RunningLockInfo* info = reply.ReadParcelable<RunningLockInfo>();
236         if (info != nullptr) {
237             runningLockLists.insert(std::pair<std::string, RunningLockInfo>(key, *info));
238             delete info;
239         }
240     }
241     return true;
242 }
243 
IsUsed(const sptr<IRemoteObject> & remoteObj)244 bool PowerMgrProxy::IsUsed(const sptr<IRemoteObject>& remoteObj)
245 {
246     sptr<IRemoteObject> remote = Remote();
247     RETURN_IF_WITH_RET(remote == nullptr, false);
248 
249     MessageParcel data;
250     MessageParcel reply;
251     MessageOption option;
252 
253     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
254         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
255         return false;
256     }
257 
258     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, remoteObj.GetRefPtr(), false);
259     int ret = remote->SendRequest(
260         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_ISUSED),
261         data, reply, option);
262     if (ret != ERR_OK) {
263         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
264         return false;
265     }
266     bool used = false;
267     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Bool, used, false);
268     return used;
269 }
270 
ProxyRunningLock(bool isProxied,pid_t pid,pid_t uid)271 bool PowerMgrProxy::ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)
272 {
273     sptr<IRemoteObject> remote = Remote();
274     RETURN_IF_WITH_RET(remote == nullptr, false);
275 
276     MessageParcel data;
277     MessageParcel reply;
278     MessageOption option;
279 
280     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
281         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
282         return false;
283     }
284 
285     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, isProxied, false);
286     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, pid, false);
287     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, uid, false);
288     int ret = remote->SendRequest(
289         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCK),
290         data, reply, option);
291     if (ret != ERR_OK) {
292         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
293         return false;
294     }
295     bool succ = false;
296     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Bool, succ, false);
297     return succ;
298 }
299 
ProxyRunningLocks(bool isProxied,const std::vector<std::pair<pid_t,pid_t>> & processInfos)300 bool PowerMgrProxy::ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)
301 {
302     sptr<IRemoteObject> remote = Remote();
303     RETURN_IF_WITH_RET(remote == nullptr, false);
304 
305     MessageParcel data;
306     MessageParcel reply;
307     MessageOption option;
308 
309     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
310         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
311         return false;
312     }
313 
314     size_t size = processInfos.size();
315     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, isProxied, false);
316     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, size, false);
317     for (size_t i = 0; i < size; ++i) {
318         RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, processInfos[i].first, false);
319         RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, processInfos[i].second, false);
320     }
321     int ret = remote->SendRequest(static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCKS),
322         data, reply, option);
323     if (ret != ERR_OK) {
324         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
325         return false;
326     }
327     bool succ = false;
328     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Bool, succ, false);
329     return succ;
330 }
331 
ResetRunningLocks()332 bool PowerMgrProxy::ResetRunningLocks()
333 {
334     sptr<IRemoteObject> remote = Remote();
335     RETURN_IF_WITH_RET(remote == nullptr, false);
336 
337     MessageParcel data;
338     MessageParcel reply;
339     MessageOption option;
340 
341     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
342         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
343         return false;
344     }
345 
346     int ret = remote->SendRequest(static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESET_RUNNINGLOCKS),
347         data, reply, option);
348     if (ret != ERR_OK) {
349         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
350         return false;
351     }
352     bool succ = false;
353     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Bool, succ, false);
354     return succ;
355 }
356 
RebootDevice(const std::string & reason)357 PowerErrors PowerMgrProxy::RebootDevice(const std::string& reason)
358 {
359     sptr<IRemoteObject> remote = Remote();
360     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
361 
362     MessageParcel data;
363     MessageParcel reply;
364     MessageOption option;
365 
366     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
367         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
368         return PowerErrors::ERR_CONNECTION_FAIL;
369     }
370 
371     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(reason), PowerErrors::ERR_CONNECTION_FAIL);
372 
373     int ret = remote->SendRequest(
374         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE), data, reply, option);
375     if (ret != ERR_OK) {
376         POWER_HILOGE(FEATURE_SHUTDOWN, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
377         return PowerErrors::ERR_CONNECTION_FAIL;
378     }
379     int32_t error;
380     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
381     return static_cast<PowerErrors>(error);
382 }
383 
RebootDeviceForDeprecated(const std::string & reason)384 PowerErrors PowerMgrProxy::RebootDeviceForDeprecated(const std::string& reason)
385 {
386     sptr<IRemoteObject> remote = Remote();
387     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
388 
389     MessageParcel data;
390     MessageParcel reply;
391     MessageOption option;
392 
393     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
394         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
395         return PowerErrors::ERR_CONNECTION_FAIL;
396     }
397 
398     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(reason), PowerErrors::ERR_CONNECTION_FAIL);
399 
400     int ret = remote->SendRequest(
401         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE_FOR_DEPRECATED),
402         data, reply, option);
403     if (ret != ERR_OK) {
404         POWER_HILOGE(FEATURE_SHUTDOWN, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
405         return PowerErrors::ERR_CONNECTION_FAIL;
406     }
407     int32_t error;
408     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
409     return static_cast<PowerErrors>(error);
410 }
411 
ShutDownDevice(const std::string & reason)412 PowerErrors PowerMgrProxy::ShutDownDevice(const std::string& reason)
413 {
414     sptr<IRemoteObject> remote = Remote();
415     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
416 
417     MessageParcel data;
418     MessageParcel reply;
419     MessageOption option;
420 
421     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
422         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
423         return PowerErrors::ERR_CONNECTION_FAIL;
424     }
425 
426     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(reason), PowerErrors::ERR_CONNECTION_FAIL);
427 
428     int ret = remote->SendRequest(
429         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHUTDOWN_DEVICE), data, reply, option);
430     if (ret != ERR_OK) {
431         POWER_HILOGE(FEATURE_SHUTDOWN, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
432         return PowerErrors::ERR_CONNECTION_FAIL;
433     }
434     int32_t error;
435     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
436     return static_cast<PowerErrors>(error);
437 }
438 
SetSuspendTag(const std::string & tag)439 PowerErrors PowerMgrProxy::SetSuspendTag(const std::string& tag)
440 {
441     sptr<IRemoteObject> remote = Remote();
442     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
443 
444     MessageParcel data;
445     MessageParcel reply;
446     MessageOption option;
447 
448     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
449         POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
450         return PowerErrors::ERR_CONNECTION_FAIL;
451     }
452 
453     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(tag), PowerErrors::ERR_CONNECTION_FAIL);
454 
455     int ret = remote->SendRequest(
456         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_SUSPEND_TAG), data, reply, option);
457     if (ret != ERR_OK) {
458         POWER_HILOGE(FEATURE_SUSPEND, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
459         return PowerErrors::ERR_CONNECTION_FAIL;
460     }
461     int32_t error;
462     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
463     return static_cast<PowerErrors>(error);
464 }
465 
SuspendDevice(int64_t callTimeMs,SuspendDeviceType reason,bool suspendImmed)466 PowerErrors PowerMgrProxy::SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed)
467 {
468     sptr<IRemoteObject> remote = Remote();
469     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
470 
471     MessageParcel data;
472     MessageParcel reply;
473     MessageOption option;
474 
475     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
476         POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
477         return PowerErrors::ERR_CONNECTION_FAIL;
478     }
479 
480     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int64, callTimeMs, PowerErrors::ERR_CONNECTION_FAIL);
481     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32,
482         static_cast<uint32_t>(reason), PowerErrors::ERR_CONNECTION_FAIL);
483     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, suspendImmed, PowerErrors::ERR_CONNECTION_FAIL);
484 
485     int ret = remote->SendRequest(
486         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SUSPEND_DEVICE), data, reply, option);
487     if (ret != ERR_OK) {
488         POWER_HILOGE(FEATURE_SUSPEND, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
489         return PowerErrors::ERR_CONNECTION_FAIL;
490     }
491     int32_t error;
492     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
493     return static_cast<PowerErrors>(error);
494 }
495 
WakeupDevice(int64_t callTimeMs,WakeupDeviceType reason,const std::string & details)496 PowerErrors PowerMgrProxy::WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details)
497 {
498     sptr<IRemoteObject> remote = Remote();
499     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
500 
501     MessageParcel data;
502     MessageParcel reply;
503     MessageOption option;
504 
505     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
506         POWER_HILOGE(FEATURE_WAKEUP, "Write descriptor failed");
507         return PowerErrors::ERR_CONNECTION_FAIL;
508     }
509 
510     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int64, callTimeMs, PowerErrors::ERR_CONNECTION_FAIL);
511     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32,
512         static_cast<uint32_t>(reason), PowerErrors::ERR_CONNECTION_FAIL);
513     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(details), PowerErrors::ERR_CONNECTION_FAIL);
514 
515     int ret = remote->SendRequest(
516         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::WAKEUP_DEVICE), data, reply, option);
517     if (ret != ERR_OK) {
518         POWER_HILOGE(FEATURE_WAKEUP, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
519         return PowerErrors::ERR_CONNECTION_FAIL;
520     }
521     int32_t error;
522     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
523     return static_cast<PowerErrors>(error);
524 }
525 
WakeupDeviceAsync(int64_t callTimeMs,WakeupDeviceType reason,const std::string & details)526 void PowerMgrProxy::WakeupDeviceAsync(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details)
527 {
528     sptr<IRemoteObject> remote = Remote();
529     RETURN_IF(remote == nullptr);
530 
531     MessageParcel data;
532     MessageParcel reply;
533     MessageOption option = { MessageOption::TF_ASYNC };
534 
535     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
536         POWER_HILOGE(FEATURE_WAKEUP, "Write descriptor failed");
537         return;
538     }
539 
540     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, Int64, callTimeMs);
541     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, Uint32, static_cast<uint32_t>(reason));
542     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, String16, Str8ToStr16(details));
543 
544     int ret = remote->SendRequest(
545         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::WAKEUP_DEVICE), data, reply, option);
546     if (ret != ERR_OK) {
547         POWER_HILOGE(FEATURE_WAKEUP, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
548         return;
549     }
550 }
551 
RefreshActivity(int64_t callTimeMs,UserActivityType type,bool needChangeBacklight)552 bool PowerMgrProxy::RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight)
553 {
554     sptr<IRemoteObject> remote = Remote();
555     RETURN_IF_WITH_RET(remote == nullptr, false);
556 
557     MessageParcel data;
558     MessageParcel reply;
559     MessageOption option;
560 
561     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
562         POWER_HILOGE(FEATURE_ACTIVITY, "Write descriptor failed");
563         return false;
564     }
565 
566     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int64, callTimeMs, false);
567     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32, static_cast<uint32_t>(type), false);
568     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, needChangeBacklight, false);
569 
570     int ret = remote->SendRequest(
571         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REFRESH_ACTIVITY), data, reply, option);
572     if (ret != ERR_OK) {
573         POWER_HILOGE(FEATURE_ACTIVITY, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
574         return false;
575     }
576     return true;
577 }
578 
OverrideScreenOffTime(int64_t timeout)579 PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout)
580 {
581     sptr<IRemoteObject> remote = Remote();
582     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
583 
584     MessageParcel data;
585     MessageParcel reply;
586     MessageOption option;
587 
588     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
589         POWER_HILOGE(COMP_SVC, "Write descriptor failed");
590         return PowerErrors::ERR_CONNECTION_FAIL;
591     }
592 
593     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int64, timeout, PowerErrors::ERR_CONNECTION_FAIL);
594 
595     int ret = remote->SendRequest(
596         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::OVERRIDE_DISPLAY_OFF_TIME),
597         data, reply, option);
598     if (ret != ERR_OK) {
599         POWER_HILOGE(COMP_SVC, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
600         return PowerErrors::ERR_CONNECTION_FAIL;
601     }
602 
603     int32_t error;
604     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
605     return static_cast<PowerErrors>(error);
606 }
607 
RestoreScreenOffTime()608 PowerErrors PowerMgrProxy::RestoreScreenOffTime()
609 {
610     sptr<IRemoteObject> remote = Remote();
611     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
612 
613     MessageParcel data;
614     MessageParcel reply;
615     MessageOption option;
616 
617     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
618         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
619         return PowerErrors::ERR_CONNECTION_FAIL;
620     }
621 
622     int ret = remote->SendRequest(
623         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESTORE_DISPLAY_OFF_TIME),
624         data, reply, option);
625     if (ret != ERR_OK) {
626         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
627         return PowerErrors::ERR_CONNECTION_FAIL;
628     }
629 
630     int32_t error;
631     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
632     return static_cast<PowerErrors>(error);
633 }
634 
ForceSuspendDevice(int64_t callTimeMs)635 PowerErrors PowerMgrProxy::ForceSuspendDevice(int64_t callTimeMs)
636 {
637     sptr<IRemoteObject> remote = Remote();
638     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
639 
640     MessageParcel data;
641     MessageParcel reply;
642     MessageOption option = { MessageOption::TF_ASYNC };
643 
644     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
645         POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
646         return PowerErrors::ERR_CONNECTION_FAIL;
647     }
648 
649     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int64, callTimeMs, PowerErrors::ERR_CONNECTION_FAIL);
650     sptr<PowerMgrStubAsync> asyncCallback = new PowerMgrStubAsync();
651     data.WriteRemoteObject(asyncCallback->AsObject());
652 
653     int ret = remote->SendRequest(
654         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::FORCE_DEVICE_SUSPEND), data, reply, option);
655     if (ret != ERR_OK) {
656         POWER_HILOGE(FEATURE_SUSPEND, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
657         return PowerErrors::ERR_CONNECTION_FAIL;
658     }
659 
660     int waitTime = 100;
661     int error = asyncCallback->WaitForAsyncReply(waitTime);
662     return static_cast<PowerErrors>(error);
663 }
664 
GetState()665 PowerState PowerMgrProxy::GetState()
666 {
667     sptr<IRemoteObject> remote = Remote();
668     RETURN_IF_WITH_RET(remote == nullptr, PowerState::UNKNOWN);
669 
670     uint32_t result = 0;
671     MessageParcel data;
672     MessageParcel reply;
673     MessageOption option;
674 
675     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
676         POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
677         return PowerState::UNKNOWN;
678     }
679 
680     int ret = remote->SendRequest(
681         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GET_STATE), data, reply, option);
682     if (ret != ERR_OK) {
683         POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
684         return PowerState::UNKNOWN;
685     }
686     if (!reply.ReadUint32(result)) {
687         POWER_HILOGE(FEATURE_POWER_STATE, "ReadUint32 failed");
688         return PowerState::UNKNOWN;
689     }
690 
691     return static_cast<PowerState>(result);
692 }
693 
IsScreenOn(bool needPrintLog)694 bool PowerMgrProxy::IsScreenOn(bool needPrintLog)
695 {
696     sptr<IRemoteObject> remote = Remote();
697     RETURN_IF_WITH_RET(remote == nullptr, false);
698 
699     bool result = false;
700     MessageParcel data;
701     MessageParcel reply;
702     MessageOption option;
703 
704     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
705         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
706         return result;
707     }
708     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, needPrintLog, false);
709 
710     int ret = remote->SendRequest(
711         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_SCREEN_ON), data, reply, option);
712     if (ret != ERR_OK) {
713         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
714         return result;
715     }
716 
717     if (!reply.ReadBool(result)) {
718         POWER_HILOGE(COMP_FWK, "Read IsScreenOn failed");
719     }
720 
721     return result;
722 }
723 
IsFoldScreenOn()724 bool PowerMgrProxy::IsFoldScreenOn()
725 {
726     sptr<IRemoteObject> remote = Remote();
727     RETURN_IF_WITH_RET(remote == nullptr, false);
728 
729     bool result = false;
730     MessageParcel data;
731     MessageParcel reply;
732     MessageOption option;
733 
734     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
735         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
736         return result;
737     }
738 
739     int ret = remote->SendRequest(
740         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_FOLD_SCREEN_ON), data, reply, option);
741     if (ret != ERR_OK) {
742         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
743         return result;
744     }
745 
746     if (!reply.ReadBool(result)) {
747         POWER_HILOGE(COMP_FWK, "Read IsFoldScreenOn failed");
748     }
749 
750     return result;
751 }
752 
IsCollaborationScreenOn()753 bool PowerMgrProxy::IsCollaborationScreenOn()
754 {
755     sptr<IRemoteObject> remote = Remote();
756     RETURN_IF_WITH_RET(remote == nullptr, false);
757 
758     bool result = false;
759     MessageParcel data;
760     MessageParcel reply;
761     MessageOption option;
762 
763     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
764         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
765         return result;
766     }
767 
768     int ret = remote->SendRequest(
769         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_COLLABORATION_SCREEN_ON), data, reply, option);
770     if (ret != ERR_OK) {
771         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
772         return result;
773     }
774 
775     if (!reply.ReadBool(result)) {
776         POWER_HILOGE(COMP_FWK, "Read IsCollaborationScreenOn failed");
777     }
778 
779     return result;
780 }
781 
RegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback,bool isSync)782 bool PowerMgrProxy::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync)
783 {
784     sptr<IRemoteObject> remote = Remote();
785     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
786 
787     MessageParcel data;
788     MessageParcel reply;
789     MessageOption option;
790 
791     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
792         POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
793         return false;
794     }
795 
796     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
797     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, isSync, false);
798 
799     int ret = remote->SendRequest(
800         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_STATE_CALLBACK),
801         data, reply, option);
802     if (ret != ERR_OK) {
803         POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
804         return false;
805     }
806     return true;
807 }
808 
UnRegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback)809 bool PowerMgrProxy::UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
810 {
811     sptr<IRemoteObject> remote = Remote();
812     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
813 
814     MessageParcel data;
815     MessageParcel reply;
816     MessageOption option;
817 
818     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
819         POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
820         return false;
821     }
822 
823     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
824 
825     int ret = remote->SendRequest(
826         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_STATE_CALLBACK),
827         data, reply, option);
828     if (ret != ERR_OK) {
829         POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
830         return false;
831     }
832     return true;
833 }
834 
RegisterSyncSleepCallback(const sptr<ISyncSleepCallback> & callback,SleepPriority priority)835 bool PowerMgrProxy::RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority)
836 {
837     sptr<IRemoteObject> remote = Remote();
838     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
839 
840     MessageParcel data;
841     MessageParcel reply;
842     MessageOption option;
843 
844     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
845         POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
846         return false;
847     }
848 
849     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
850     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32, static_cast<uint32_t>(priority), false);
851 
852     int ret = remote->SendRequest(
853         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_SYNC_SLEEP_CALLBACK),
854         data, reply, option);
855     if (ret != ERR_OK) {
856         POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
857         return false;
858     }
859     return true;
860 }
861 
862 
UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback> & callback)863 bool PowerMgrProxy::UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback)
864 {
865     sptr<IRemoteObject> remote = Remote();
866     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
867 
868     MessageParcel data;
869     MessageParcel reply;
870     MessageOption option;
871 
872     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
873         POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
874         return false;
875     }
876 
877     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
878 
879     int ret = remote->SendRequest(
880         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_SYNC_SLEEP_CALLBACK),
881         data, reply, option);
882     if (ret != ERR_OK) {
883         POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
884         return false;
885     }
886     return true;
887 }
888 
RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & callback)889 bool PowerMgrProxy::RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback)
890 {
891     sptr<IRemoteObject> remote = Remote();
892     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
893 
894     MessageParcel data;
895     MessageParcel reply;
896     MessageOption option;
897 
898     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
899         POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
900         return false;
901     }
902 
903     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
904 
905     int ret = remote->SendRequest(
906         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_SYNC_HIBERNATE_CALLBACK),
907         data, reply, option);
908     if (ret != ERR_OK) {
909         POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
910         return false;
911     }
912     return true;
913 }
914 
915 
UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback> & callback)916 bool PowerMgrProxy::UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback)
917 {
918     sptr<IRemoteObject> remote = Remote();
919     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
920 
921     MessageParcel data;
922     MessageParcel reply;
923     MessageOption option;
924 
925     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
926         POWER_HILOGE(FEATURE_POWER_STATE, "Write descriptor failed");
927         return false;
928     }
929 
930     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
931 
932     int ret = remote->SendRequest(
933         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_SYNC_HIBERNATE_CALLBACK),
934         data, reply, option);
935     if (ret != ERR_OK) {
936         POWER_HILOGE(FEATURE_POWER_STATE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
937         return false;
938     }
939     return true;
940 }
941 
RegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)942 bool PowerMgrProxy::RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
943 {
944     sptr<IRemoteObject> remote = Remote();
945     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
946 
947     MessageParcel data;
948     MessageParcel reply;
949     MessageOption option;
950 
951     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
952         POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
953         return false;
954     }
955 
956     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
957 
958     int ret = remote->SendRequest(
959         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_MODE_CALLBACK),
960         data, reply, option);
961     if (ret != ERR_OK) {
962         POWER_HILOGE(FEATURE_POWER_MODE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
963         return false;
964     }
965     return true;
966 }
967 
UnRegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)968 bool PowerMgrProxy::UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
969 {
970     sptr<IRemoteObject> remote = Remote();
971     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
972 
973     MessageParcel data;
974     MessageParcel reply;
975     MessageOption option;
976 
977     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
978         POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
979         return false;
980     }
981 
982     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
983 
984     int ret = remote->SendRequest(
985         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_MODE_CALLBACK),
986         data, reply, option);
987     if (ret != ERR_OK) {
988         POWER_HILOGE(FEATURE_POWER_MODE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
989         return false;
990     }
991     return true;
992 }
993 
RegisterScreenStateCallback(int32_t remainTime,const sptr<IScreenOffPreCallback> & callback)994 bool PowerMgrProxy::RegisterScreenStateCallback(int32_t remainTime, const sptr<IScreenOffPreCallback>& callback)
995 {
996     sptr<IRemoteObject> remote = Remote();
997     RETURN_IF_WITH_RET((remote == nullptr) || (remainTime <= 0) || (callback == nullptr), false);
998     MessageParcel data;
999     MessageParcel reply;
1000     MessageOption option;
1001     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1002         POWER_HILOGE(FEATURE_SCREEN_OFF_PRE, "Write descriptor failed");
1003         return false;
1004     }
1005 
1006     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int32, remainTime, false);
1007     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
1008 
1009     int ret = remote->SendRequest(
1010         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_SCREEN_OFF_PRE_CALLBACK),
1011         data, reply, option);
1012     if (ret != ERR_OK) {
1013         POWER_HILOGE(FEATURE_SCREEN_OFF_PRE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1014         return false;
1015     }
1016     return true;
1017 }
1018 
UnRegisterScreenStateCallback(const sptr<IScreenOffPreCallback> & callback)1019 bool PowerMgrProxy::UnRegisterScreenStateCallback(const sptr<IScreenOffPreCallback>& callback)
1020 {
1021     sptr<IRemoteObject> remote = Remote();
1022     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
1023 
1024     MessageParcel data;
1025     MessageParcel reply;
1026     MessageOption option;
1027 
1028     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1029         POWER_HILOGE(FEATURE_SCREEN_OFF_PRE, "Write descriptor failed");
1030         return false;
1031     }
1032 
1033     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
1034 
1035     int ret = remote->SendRequest(
1036         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_SCREEN_OFF_PRE_CALLBACK),
1037         data, reply, option);
1038     if (ret != ERR_OK) {
1039         POWER_HILOGE(FEATURE_SCREEN_OFF_PRE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1040         return false;
1041     }
1042     return true;
1043 }
1044 
RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback> & callback)1045 bool PowerMgrProxy::RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
1046 {
1047     sptr<IRemoteObject> remote = Remote();
1048     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
1049 
1050     MessageParcel data;
1051     MessageParcel reply;
1052     MessageOption option;
1053     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1054         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
1055         return false;
1056     }
1057 
1058     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
1059 
1060     int ret = remote->SendRequest(
1061         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_RUNNINGLOCK_CALLBACK),
1062         data, reply, option);
1063     if (ret != ERR_OK) {
1064         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1065         return false;
1066     }
1067     return true;
1068 }
1069 
UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback> & callback)1070 bool PowerMgrProxy::UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback)
1071 {
1072     sptr<IRemoteObject> remote = Remote();
1073     RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
1074 
1075     MessageParcel data;
1076     MessageParcel reply;
1077     MessageOption option;
1078 
1079     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1080         POWER_HILOGE(FEATURE_RUNNING_LOCK, "Write descriptor failed");
1081         return false;
1082     }
1083 
1084     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, callback->AsObject(), false);
1085 
1086     int ret = remote->SendRequest(
1087         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_RUNNINGLOCK_CALLBACK),
1088         data, reply, option);
1089     if (ret != ERR_OK) {
1090         POWER_HILOGE(FEATURE_RUNNING_LOCK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1091         return false;
1092     }
1093     return true;
1094 }
1095 
SetDisplaySuspend(bool enable)1096 bool PowerMgrProxy::SetDisplaySuspend(bool enable)
1097 {
1098     sptr<IRemoteObject> remote = Remote();
1099     RETURN_IF_WITH_RET(remote == nullptr, false);
1100 
1101     MessageParcel data;
1102     MessageParcel reply;
1103     MessageOption option;
1104 
1105     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1106         POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
1107         return false;
1108     }
1109 
1110     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, enable, false);
1111 
1112     int ret = remote->SendRequest(
1113         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_DISPLAY_SUSPEND),
1114         data, reply, option);
1115     if (ret != ERR_OK) {
1116         POWER_HILOGE(FEATURE_SUSPEND, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1117         return false;
1118     }
1119     return true;
1120 }
1121 
Hibernate(bool clearMemory)1122 PowerErrors PowerMgrProxy::Hibernate(bool clearMemory)
1123 {
1124     sptr<IRemoteObject> remote = Remote();
1125     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
1126 
1127     MessageParcel data;
1128     MessageParcel reply;
1129     MessageOption option = { MessageOption::TF_ASYNC };
1130 
1131     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1132         POWER_HILOGE(FEATURE_SUSPEND, "Write descriptor failed");
1133         return PowerErrors::ERR_CONNECTION_FAIL;
1134     }
1135 
1136     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, clearMemory, PowerErrors::ERR_CONNECTION_FAIL);
1137     sptr<PowerMgrStubAsync> asyncCallback = new PowerMgrStubAsync();
1138     data.WriteRemoteObject(asyncCallback->AsObject());
1139 
1140     int ret = remote->SendRequest(
1141         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::HIBERNATE),
1142         data, reply, option);
1143     if (ret != ERR_OK) {
1144         POWER_HILOGE(FEATURE_SUSPEND, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1145         return PowerErrors::ERR_CONNECTION_FAIL;
1146     }
1147 
1148     int waitTime = 100;
1149     int error = asyncCallback->WaitForAsyncReply(waitTime);
1150     return static_cast<PowerErrors>(error);
1151 }
1152 
SetDeviceMode(const PowerMode & mode)1153 PowerErrors PowerMgrProxy::SetDeviceMode(const PowerMode& mode)
1154 {
1155     sptr<IRemoteObject> remote = Remote();
1156     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
1157 
1158     MessageParcel data;
1159     MessageParcel reply;
1160     MessageOption option;
1161 
1162     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1163         POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
1164         return PowerErrors::ERR_CONNECTION_FAIL;
1165     }
1166 
1167     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32, static_cast<uint32_t>(mode), PowerErrors::ERR_CONNECTION_FAIL);
1168 
1169     int ret = remote->SendRequest(
1170         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SETMODE_DEVICE), data, reply, option);
1171     if (ret != ERR_OK) {
1172         POWER_HILOGE(FEATURE_POWER_MODE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1173         return PowerErrors::ERR_CONNECTION_FAIL;
1174     }
1175     int32_t error;
1176     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_OK);
1177     return static_cast<PowerErrors>(error);
1178 }
1179 
GetDeviceMode()1180 PowerMode PowerMgrProxy::GetDeviceMode()
1181 {
1182     sptr<IRemoteObject> remote = Remote();
1183     RETURN_IF_WITH_RET(remote == nullptr, static_cast<PowerMode>(false));
1184 
1185     MessageParcel data;
1186     MessageParcel reply;
1187     MessageOption option;
1188 
1189     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1190         POWER_HILOGE(FEATURE_POWER_MODE, "Write descriptor failed");
1191         return PowerMode::NORMAL_MODE;
1192     }
1193 
1194     int ret = remote->SendRequest(
1195         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GETMODE_DEVICE),
1196         data, reply, option);
1197     if (ret != ERR_OK) {
1198         POWER_HILOGE(FEATURE_POWER_MODE, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1199         return PowerMode::NORMAL_MODE;
1200     }
1201 
1202     uint32_t used = static_cast<uint32_t>(PowerMode::NORMAL_MODE);
1203     if (!reply.ReadUint32(used)) {
1204         POWER_HILOGE(FEATURE_POWER_MODE, "ReadUint32 fail");
1205     }
1206     return static_cast<PowerMode>(used);
1207 }
1208 
ShellDump(const std::vector<std::string> & args,uint32_t argc)1209 std::string PowerMgrProxy::ShellDump(const std::vector<std::string>& args, uint32_t argc)
1210 {
1211     sptr<IRemoteObject> remote = Remote();
1212     std::string result = "remote error";
1213     RETURN_IF_WITH_RET(remote == nullptr, result);
1214 
1215     MessageParcel data;
1216     MessageParcel reply;
1217     MessageOption option;
1218 
1219     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1220         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
1221         return result;
1222     }
1223 
1224     data.WriteUint32(argc);
1225     for (uint32_t i = 0; i < argc; i++) {
1226         data.WriteString(args[i]);
1227     }
1228     int ret = remote->SendRequest(
1229         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHELL_DUMP), data, reply, option);
1230     if (ret != ERR_OK) {
1231         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1232         return result;
1233     }
1234     result = reply.ReadString();
1235 
1236     return result;
1237 }
1238 
IsStandby(bool & isStandby)1239 PowerErrors PowerMgrProxy::IsStandby(bool& isStandby)
1240 {
1241     sptr<IRemoteObject> remote = Remote();
1242     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
1243 
1244     MessageParcel data;
1245     MessageParcel reply;
1246     MessageOption option;
1247 
1248     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1249         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
1250         return PowerErrors::ERR_CONNECTION_FAIL;
1251     }
1252 
1253     int32_t ret = remote->SendRequest(
1254         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_STANDBY), data, reply, option);
1255     if (ret != ERR_OK) {
1256         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1257         return PowerErrors::ERR_CONNECTION_FAIL;
1258     }
1259 
1260     int32_t error;
1261 
1262     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_CONNECTION_FAIL);
1263     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Bool, isStandby, PowerErrors::ERR_CONNECTION_FAIL);
1264 
1265     return static_cast<PowerErrors>(error);
1266 }
1267 
SetForceTimingOut(bool enabled,const sptr<IRemoteObject> & token)1268 PowerErrors PowerMgrProxy::SetForceTimingOut(bool enabled, const sptr<IRemoteObject>& token)
1269 {
1270     sptr<IRemoteObject> remote = Remote();
1271     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
1272 
1273     MessageParcel data;
1274     MessageParcel reply;
1275     MessageOption option;
1276 
1277     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1278         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
1279         return PowerErrors::ERR_CONNECTION_FAIL;
1280     }
1281     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool,
1282         static_cast<uint32_t>(enabled), PowerErrors::ERR_CONNECTION_FAIL);
1283     if (token.GetRefPtr() == nullptr) {
1284         POWER_HILOGE(COMP_FWK, "token nullptr");
1285     }
1286     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, token, PowerErrors::ERR_CONNECTION_FAIL);
1287     int32_t ret = remote->SendRequest(
1288         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_FORCE_TIMING_OUT), data, reply, option);
1289     if (ret != ERR_OK) {
1290         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1291         return PowerErrors::ERR_CONNECTION_FAIL;
1292     }
1293     int32_t error;
1294     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_CONNECTION_FAIL);
1295     return static_cast<PowerErrors>(error);
1296 }
1297 
LockScreenAfterTimingOut(bool enabledLockScreen,bool checkLock,bool sendScreenOffEvent,const sptr<IRemoteObject> & token)1298 PowerErrors PowerMgrProxy::LockScreenAfterTimingOut(
1299     bool enabledLockScreen, bool checkLock, bool sendScreenOffEvent, const sptr<IRemoteObject>& token)
1300 {
1301     sptr<IRemoteObject> remote = Remote();
1302     RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL);
1303 
1304     MessageParcel data;
1305     MessageParcel reply;
1306     MessageOption option;
1307 
1308     if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
1309         POWER_HILOGE(COMP_FWK, "Write descriptor failed");
1310         return PowerErrors::ERR_CONNECTION_FAIL;
1311     }
1312     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, enabledLockScreen, PowerErrors::ERR_CONNECTION_FAIL);
1313     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, checkLock, PowerErrors::ERR_CONNECTION_FAIL);
1314     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, sendScreenOffEvent, PowerErrors::ERR_CONNECTION_FAIL);
1315     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, RemoteObject, token.GetRefPtr(), PowerErrors::ERR_CONNECTION_FAIL);
1316 
1317     int32_t ret = remote->SendRequest(
1318         static_cast<int>(PowerMgr::PowerMgrInterfaceCode::LOCK_SCREEN_AFTER_TIMING_OUT), data, reply, option);
1319     if (ret != ERR_OK) {
1320         POWER_HILOGE(COMP_FWK, "%{public}s: SendRequest failed with ret=%{public}d", __func__, ret);
1321         return PowerErrors::ERR_CONNECTION_FAIL;
1322     }
1323     int32_t error;
1324     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, error, PowerErrors::ERR_CONNECTION_FAIL);
1325     return static_cast<PowerErrors>(error);
1326 }
1327 
RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback,ShutdownPriority priority)1328 void PowerMgrProxy::RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority)
1329 {
1330     sptr<IRemoteObject> remote = Remote();
1331     RETURN_IF(remote == nullptr);
1332     auto delegator = std::make_unique<ShutdownProxyDelegator>(remote, PowerMgrProxy::GetDescriptor());
1333     delegator->RegisterShutdownCallback(callback, priority);
1334 }
1335 
UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback)1336 void PowerMgrProxy::UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback)
1337 {
1338     sptr<IRemoteObject> remote = Remote();
1339     RETURN_IF(remote == nullptr);
1340     auto delegator = std::make_unique<ShutdownProxyDelegator>(remote, PowerMgrProxy::GetDescriptor());
1341     delegator->UnRegisterShutdownCallback(callback);
1342 }
1343 
RegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback,ShutdownPriority priority)1344 void PowerMgrProxy::RegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority)
1345 {
1346     sptr<IRemoteObject> remote = Remote();
1347     RETURN_IF(remote == nullptr);
1348     auto delegator = std::make_unique<ShutdownProxyDelegator>(remote, PowerMgrProxy::GetDescriptor());
1349     delegator->RegisterShutdownCallback(callback, priority);
1350 }
1351 
UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback)1352 void PowerMgrProxy::UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback)
1353 {
1354     sptr<IRemoteObject> remote = Remote();
1355     RETURN_IF(remote == nullptr);
1356     auto delegator = std::make_unique<ShutdownProxyDelegator>(remote, PowerMgrProxy::GetDescriptor());
1357     delegator->UnRegisterShutdownCallback(callback);
1358 }
1359 
RegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback,ShutdownPriority priority)1360 void PowerMgrProxy::RegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority)
1361 {
1362     sptr<IRemoteObject> remote = Remote();
1363     RETURN_IF(remote == nullptr);
1364     auto delegator = std::make_unique<ShutdownProxyDelegator>(remote, PowerMgrProxy::GetDescriptor());
1365     delegator->RegisterShutdownCallback(callback, priority);
1366 }
1367 
UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback)1368 void PowerMgrProxy::UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback)
1369 {
1370     sptr<IRemoteObject> remote = Remote();
1371     RETURN_IF(remote == nullptr);
1372     auto delegator = std::make_unique<ShutdownProxyDelegator>(remote, PowerMgrProxy::GetDescriptor());
1373     delegator->UnRegisterShutdownCallback(callback);
1374 }
1375 } // namespace PowerMgr
1376 } // namespace OHOS
1377