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