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