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