• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "power_mgr_stub.h"
17 
18 #include <message_parcel.h>
19 #include <string_ex.h>
20 
21 #include "running_lock_info.h"
22 #include "power_common.h"
23 #include "power_errors.h"
24 #include "power_mgr_ipc_interface_code.h"
25 #include "shutdown_stub_delegator.h"
26 #include "shutdown/shutdown_client_ipc_interface_code.h"
27 #include "xcollie/xcollie.h"
28 
29 using namespace OHOS::HiviewDFX;
30 
31 namespace OHOS {
32 namespace PowerMgr {
33 namespace {
34 constexpr uint32_t PARAM_MAX_NUM = 10;
35 constexpr int32_t MAX_PROXY_RUNNINGLOCK_NUM = 2000;
36 std::unique_ptr<ShutdownStubDelegator> g_shutdownDelegator;
37 std::mutex g_shutdownMutex;
38 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
40 {
41     POWER_HILOGD(COMP_FWK, "code=%{public}u, flags=%{public}d", code, option.GetFlags());
42     std::u16string descriptor = PowerMgrStub::GetDescriptor();
43     std::u16string remoteDescriptor = data.ReadInterfaceToken();
44     if (descriptor != remoteDescriptor) {
45         POWER_HILOGE(COMP_FWK, "Descriptor is not matched");
46         return E_GET_POWER_SERVICE_FAILED;
47     }
48     if (IsShutdownCommand(code)) {
49         {
50             std::lock_guard<std::mutex> lock(g_shutdownMutex);
51             if (g_shutdownDelegator == nullptr) {
52                 g_shutdownDelegator = std::make_unique<ShutdownStubDelegator>(*this);
53                 RETURN_IF_WITH_RET(g_shutdownDelegator == nullptr, ERR_NO_INIT)
54             }
55         }
56         int ret = g_shutdownDelegator->HandleRemoteRequest(code, data, reply, option);
57         if (ret == ERR_INVALID_OPERATION) {
58             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59         }
60         return ret;
61     }
62 
63     const int DFX_DELAY_MS = 10000;
64     int id = HiviewDFX::XCollie::GetInstance().SetTimer("PowerMgrStub", DFX_DELAY_MS, nullptr, nullptr,
65         HiviewDFX::XCOLLIE_FLAG_NOOP);
66 
67     int ret = ERR_OK;
68     switch (code) {
69         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::WAKEUP_DEVICE):
70             ret = WakeupDeviceStub(data, reply);
71             break;
72         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SUSPEND_DEVICE):
73             ret = SuspendDeviceStub(data, reply);
74             break;
75         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REFRESH_ACTIVITY):
76             ret = RefreshActivityStub(data);
77             break;
78         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE):
79             ret = RebootDeviceStub(data, reply);
80             break;
81         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE_FOR_DEPRECATED):
82             ret = RebootDeviceForDeprecatedStub(data, reply);
83             break;
84         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHUTDOWN_DEVICE):
85             ret = ShutDownDeviceStub(data, reply);
86             break;
87         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::OVERRIDE_DISPLAY_OFF_TIME):
88             ret = OverrideScreenOffTimeStub(data, reply);
89             break;
90         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESTORE_DISPLAY_OFF_TIME):
91             ret = RestoreScreenOffTimeStub(reply);
92             break;
93         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GET_STATE):
94             ret = GetStateStub(reply);
95             break;
96         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_SCREEN_ON):
97             ret = IsScreenOnStub(reply);
98             break;
99         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::FORCE_DEVICE_SUSPEND):
100             ret = ForceSuspendDeviceStub(data, reply);
101             break;
102         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::CREATE_RUNNINGLOCK):
103             ret = CreateRunningLockStub(data, reply);
104             break;
105         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RELEASE_RUNNINGLOCK):
106             ret = ReleaseRunningLockStub(data);
107             break;
108         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_RUNNINGLOCK_TYPE_SUPPORTED):
109             ret = IsRunningLockTypeSupportedStub(data, reply);
110             break;
111         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_LOCK):
112             ret = LockStub(data);
113             break;
114         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_UNLOCK):
115             ret = UnLockStub(data);
116             break;
117         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_QUERY):
118             ret = QueryRunningLockListsStub(data, reply);
119             break;
120         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCK):
121             ret = ProxyRunningLockStub(data, reply);
122             break;
123         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_ISUSED):
124             ret = IsUsedStub(data, reply);
125             break;
126         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCKS):
127             ret = ProxyRunningLocksStub(data, reply);
128             break;
129         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESET_RUNNINGLOCKS):
130             ret = ResetAllPorxyStub(data, reply);
131             break;
132         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_STATE_CALLBACK):
133             ret = RegisterPowerStateCallbackStub(data);
134             break;
135         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_STATE_CALLBACK):
136             ret = UnRegisterPowerStateCallbackStub(data);
137             break;
138         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_MODE_CALLBACK):
139             ret = RegisterPowerModeCallbackStub(data);
140             break;
141         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_MODE_CALLBACK):
142             ret = UnRegisterPowerModeCallbackStub(data);
143             break;
144         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_DISPLAY_SUSPEND):
145             ret = SetDisplaySuspendStub(data);
146             break;
147         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SETMODE_DEVICE):
148             ret = SetDeviceModeStub(data, reply);
149             break;
150         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GETMODE_DEVICE):
151             ret = GetDeviceModeStub(reply);
152             break;
153         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHELL_DUMP):
154             ret = ShellDumpStub(data, reply);
155             break;
156         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_STANDBY):
157             ret = IsStandbyStub(data, reply);
158             break;
159         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_SYNC_SLEEP_CALLBACK):
160             ret = RegisterSyncSleepCallbackStub(data);
161             break;
162         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_SYNC_SLEEP_CALLBACK):
163             ret = UnRegisterSyncSleepCallbackStub(data);
164             break;
165         default:
166             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
167     }
168     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
169     return ret;
170 }
171 
CreateRunningLockStub(MessageParcel & data,MessageParcel & reply)172 int32_t PowerMgrStub::CreateRunningLockStub(MessageParcel& data, MessageParcel& reply)
173 {
174     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
175     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
176     std::unique_ptr<RunningLockInfo> runningLockInfo(data.ReadParcelable<RunningLockInfo>());
177     RETURN_IF_WITH_RET((runningLockInfo == nullptr), E_READ_PARCEL_ERROR);
178     PowerErrors error = CreateRunningLock(remoteObj, *runningLockInfo);
179     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
180     return ERR_OK;
181 }
182 
ReleaseRunningLockStub(MessageParcel & data)183 int32_t PowerMgrStub::ReleaseRunningLockStub(MessageParcel& data)
184 {
185     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
186     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
187     ReleaseRunningLock(remoteObj);
188     return ERR_OK;
189 }
190 
IsRunningLockTypeSupportedStub(MessageParcel & data,MessageParcel & reply)191 int32_t PowerMgrStub::IsRunningLockTypeSupportedStub(MessageParcel& data, MessageParcel& reply)
192 {
193     auto type = static_cast<uint32_t >(RunningLockType::RUNNINGLOCK_BUTT);
194     bool ret = false;
195     READ_PARCEL_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
196     ret = IsRunningLockTypeSupported(static_cast<RunningLockType>(type));
197     if (!reply.WriteBool(ret)) {
198         POWER_HILOGE(FEATURE_SUSPEND, "WriteBool fail");
199         return E_WRITE_PARCEL_ERROR;
200     }
201     return ERR_OK;
202 }
203 
LockStub(MessageParcel & data)204 int32_t PowerMgrStub::LockStub(MessageParcel& data)
205 {
206     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
207     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
208     int32_t timeOutMs = 0;
209     READ_PARCEL_WITH_RET(data, Int32, timeOutMs, E_READ_PARCEL_ERROR);
210     Lock(remoteObj, timeOutMs);
211     return ERR_OK;
212 }
213 
UnLockStub(MessageParcel & data)214 int32_t PowerMgrStub::UnLockStub(MessageParcel& data)
215 {
216     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
217     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
218     UnLock(remoteObj);
219     return ERR_OK;
220 }
221 
QueryRunningLockListsStub(MessageParcel & data,MessageParcel & reply)222 int32_t PowerMgrStub::QueryRunningLockListsStub(MessageParcel& data, MessageParcel& reply)
223 {
224     std::map<std::string, RunningLockInfo> runningLockLists;
225     QueryRunningLockLists(runningLockLists);
226     reply.WriteInt32(runningLockLists.size());
227     for (auto it : runningLockLists) {
228         reply.WriteString(it.first);
229         reply.WriteParcelable(&it.second);
230     }
231     return ERR_OK;
232 }
IsUsedStub(MessageParcel & data,MessageParcel & reply)233 int32_t PowerMgrStub::IsUsedStub(MessageParcel& data, MessageParcel& reply)
234 {
235     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
236     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
237     bool ret = IsUsed(remoteObj);
238     WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
239     return ERR_OK;
240 }
241 
ProxyRunningLockStub(MessageParcel & data,MessageParcel & reply)242 int32_t PowerMgrStub::ProxyRunningLockStub(MessageParcel& data, MessageParcel& reply)
243 {
244     bool isProxied = false;
245     pid_t uid;
246     pid_t pid;
247     READ_PARCEL_WITH_RET(data, Bool, isProxied, E_READ_PARCEL_ERROR);
248     READ_PARCEL_WITH_RET(data, Int32, pid, E_READ_PARCEL_ERROR);
249     READ_PARCEL_WITH_RET(data, Int32, uid, E_READ_PARCEL_ERROR);
250     bool ret = ProxyRunningLock(isProxied, pid, uid);
251     WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
252     return ERR_OK;
253 }
254 
ProxyRunningLocksStub(MessageParcel & data,MessageParcel & reply)255 int32_t PowerMgrStub::ProxyRunningLocksStub(MessageParcel& data, MessageParcel& reply)
256 {
257     bool isProxied = false;
258     std::vector<std::pair<pid_t, pid_t>> processInfos {};
259     READ_PARCEL_WITH_RET(data, Bool, isProxied, E_READ_PARCEL_ERROR);
260     int32_t size {0};
261     READ_PARCEL_WITH_RET(data, Int32, size, E_READ_PARCEL_ERROR);
262     if (size <= 0 || size > MAX_PROXY_RUNNINGLOCK_NUM) {
263         POWER_HILOGW(COMP_FWK, "size exceed limit, size=%{public}d", size);
264         return E_EXCEED_PARAM_LIMIT;
265     }
266     processInfos.resize(size);
267     for (int i = 0; i < size; ++i) {
268         READ_PARCEL_WITH_RET(data, Int32, processInfos[i].first, E_READ_PARCEL_ERROR);
269         READ_PARCEL_WITH_RET(data, Int32, processInfos[i].second, E_READ_PARCEL_ERROR);
270     }
271     bool ret = ProxyRunningLocks(isProxied, processInfos);
272     WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
273     return ERR_OK;
274 }
275 
ResetAllPorxyStub(MessageParcel & data,MessageParcel & reply)276 int32_t PowerMgrStub::ResetAllPorxyStub(MessageParcel& data, MessageParcel& reply)
277 {
278     bool ret = ResetRunningLocks();
279     WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
280     return ERR_OK;
281 }
282 
RebootDeviceStub(MessageParcel & data,MessageParcel & reply)283 int32_t PowerMgrStub::RebootDeviceStub(MessageParcel& data, MessageParcel& reply)
284 {
285     std::string reason = Str16ToStr8(data.ReadString16());
286     PowerErrors error = RebootDevice(reason);
287     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
288     return ERR_OK;
289 }
290 
RebootDeviceForDeprecatedStub(MessageParcel & data,MessageParcel & reply)291 int32_t PowerMgrStub::RebootDeviceForDeprecatedStub(MessageParcel& data, MessageParcel& reply)
292 {
293     std::string reason = Str16ToStr8(data.ReadString16());
294     PowerErrors error = RebootDeviceForDeprecated(reason);
295     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
296     return ERR_OK;
297 }
298 
ShutDownDeviceStub(MessageParcel & data,MessageParcel & reply)299 int32_t PowerMgrStub::ShutDownDeviceStub(MessageParcel& data, MessageParcel& reply)
300 {
301     std::string reason = Str16ToStr8(data.ReadString16());
302     PowerErrors error = ShutDownDevice(reason);
303     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
304     return ERR_OK;
305 }
306 
WakeupDeviceStub(MessageParcel & data,MessageParcel & reply)307 int32_t PowerMgrStub::WakeupDeviceStub(MessageParcel& data, MessageParcel& reply)
308 {
309     int64_t time = 0;
310     uint32_t reason = 0;
311 
312     READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
313     READ_PARCEL_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
314     std::string details = Str16ToStr8(data.ReadString16());
315 
316     PowerErrors error = WakeupDevice(time, static_cast<WakeupDeviceType>(reason), details);
317     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
318     return ERR_OK;
319 }
320 
SuspendDeviceStub(MessageParcel & data,MessageParcel & reply)321 int32_t PowerMgrStub::SuspendDeviceStub(MessageParcel& data, MessageParcel& reply)
322 {
323     int64_t time = 0;
324     uint32_t reason = 0;
325     bool suspendImmed = true;
326 
327     READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
328     READ_PARCEL_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
329     READ_PARCEL_WITH_RET(data, Bool, suspendImmed, E_READ_PARCEL_ERROR);
330 
331     PowerErrors error = SuspendDevice(time, static_cast<SuspendDeviceType>(reason), suspendImmed);
332     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
333     return ERR_OK;
334 }
335 
RefreshActivityStub(MessageParcel & data)336 int32_t PowerMgrStub::RefreshActivityStub(MessageParcel& data)
337 {
338     int64_t time = 0;
339     uint32_t type = 0;
340     bool needChangeBacklight = true;
341 
342     READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
343     READ_PARCEL_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
344     READ_PARCEL_WITH_RET(data, Bool, needChangeBacklight, E_READ_PARCEL_ERROR);
345 
346     RefreshActivity(time, static_cast<UserActivityType>(type), needChangeBacklight);
347     return ERR_OK;
348 }
349 
OverrideScreenOffTimeStub(MessageParcel & data,MessageParcel & reply)350 int32_t PowerMgrStub::OverrideScreenOffTimeStub(MessageParcel& data, MessageParcel& reply)
351 {
352     int64_t timeout = 0;
353 
354     READ_PARCEL_WITH_RET(data, Int64, timeout, E_READ_PARCEL_ERROR);
355 
356     bool ret = OverrideScreenOffTime(timeout);
357     if (!reply.WriteBool(ret)) {
358         POWER_HILOGE(COMP_FWK, "WriteBool fail");
359         return E_WRITE_PARCEL_ERROR;
360     }
361     return ERR_OK;
362 }
363 
RestoreScreenOffTimeStub(MessageParcel & reply)364 int32_t PowerMgrStub::RestoreScreenOffTimeStub(MessageParcel& reply)
365 {
366     bool ret = RestoreScreenOffTime();
367     if (!reply.WriteBool(ret)) {
368         POWER_HILOGE(COMP_FWK, "WriteBool fail");
369         return E_WRITE_PARCEL_ERROR;
370     }
371     return ERR_OK;
372 }
373 
ForceSuspendDeviceStub(MessageParcel & data,MessageParcel & reply)374 int32_t PowerMgrStub::ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& reply)
375 {
376     bool ret = false;
377     int64_t time = 0;
378 
379     READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
380 
381     ret = ForceSuspendDevice(time);
382     if (!reply.WriteBool(ret)) {
383         POWER_HILOGE(FEATURE_SUSPEND, "WriteBool fail");
384         return E_WRITE_PARCEL_ERROR;
385     }
386     return ERR_OK;
387 }
388 
GetStateStub(MessageParcel & reply)389 int32_t PowerMgrStub::GetStateStub(MessageParcel& reply)
390 {
391     PowerState ret = GetState();
392     if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
393         POWER_HILOGE(FEATURE_POWER_STATE, "WriteUint32 fail");
394         return E_WRITE_PARCEL_ERROR;
395     }
396     return ERR_OK;
397 }
398 
IsScreenOnStub(MessageParcel & reply)399 int32_t PowerMgrStub::IsScreenOnStub(MessageParcel& reply)
400 {
401     bool ret = false;
402     ret = IsScreenOn();
403     if (!reply.WriteBool(ret)) {
404         POWER_HILOGE(COMP_FWK, "WriteBool fail");
405         return E_WRITE_PARCEL_ERROR;
406     }
407     return ERR_OK;
408 }
409 
RegisterPowerStateCallbackStub(MessageParcel & data)410 int32_t PowerMgrStub::RegisterPowerStateCallbackStub(MessageParcel& data)
411 {
412     sptr<IRemoteObject> obj = data.ReadRemoteObject();
413     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
414     sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
415     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
416     RegisterPowerStateCallback(callback);
417     return ERR_OK;
418 }
419 
UnRegisterPowerStateCallbackStub(MessageParcel & data)420 int32_t PowerMgrStub::UnRegisterPowerStateCallbackStub(MessageParcel& data)
421 {
422     sptr<IRemoteObject> obj = data.ReadRemoteObject();
423     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
424     sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
425     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
426     UnRegisterPowerStateCallback(callback);
427     return ERR_OK;
428 }
429 
RegisterSyncSleepCallbackStub(MessageParcel & data)430 int32_t PowerMgrStub::RegisterSyncSleepCallbackStub(MessageParcel& data)
431 {
432     uint32_t priority;
433     sptr<IRemoteObject> obj = data.ReadRemoteObject();
434     READ_PARCEL_WITH_RET(data, Uint32, priority, E_READ_PARCEL_ERROR);
435     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
436     sptr<ISyncSleepCallback> callback = iface_cast<ISyncSleepCallback>(obj);
437     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
438     RegisterSyncSleepCallback(callback);
439     return ERR_OK;
440 }
441 
UnRegisterSyncSleepCallbackStub(MessageParcel & data)442 int32_t PowerMgrStub::UnRegisterSyncSleepCallbackStub(MessageParcel& data)
443 {
444     sptr<IRemoteObject> obj = data.ReadRemoteObject();
445     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
446     sptr<ISyncSleepCallback> callback = iface_cast<ISyncSleepCallback>(obj);
447     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
448     UnRegisterSyncSleepCallback(callback);
449     return ERR_OK;
450 }
451 
RegisterPowerModeCallbackStub(MessageParcel & data)452 int32_t PowerMgrStub::RegisterPowerModeCallbackStub(MessageParcel& data)
453 {
454     sptr<IRemoteObject> obj = data.ReadRemoteObject();
455     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
456     sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
457     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
458     RegisterPowerModeCallback(callback);
459     return ERR_OK;
460 }
461 
UnRegisterPowerModeCallbackStub(MessageParcel & data)462 int32_t PowerMgrStub::UnRegisterPowerModeCallbackStub(MessageParcel& data)
463 {
464     sptr<IRemoteObject> obj = data.ReadRemoteObject();
465     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
466     sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
467     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
468     UnRegisterPowerModeCallback(callback);
469     return ERR_OK;
470 }
471 
SetDisplaySuspendStub(MessageParcel & data)472 int32_t PowerMgrStub::SetDisplaySuspendStub(MessageParcel& data)
473 {
474     bool enable = false;
475     READ_PARCEL_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR);
476     SetDisplaySuspend(enable);
477     return ERR_OK;
478 }
479 
SetDeviceModeStub(MessageParcel & data,MessageParcel & reply)480 int32_t PowerMgrStub::SetDeviceModeStub(MessageParcel& data, MessageParcel& reply)
481 {
482     uint32_t mode = 0;
483     READ_PARCEL_WITH_RET(data, Uint32, mode, E_READ_PARCEL_ERROR);
484     PowerErrors error = SetDeviceMode(static_cast<PowerMode>(mode));
485     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
486     return ERR_OK;
487 }
488 
GetDeviceModeStub(MessageParcel & reply)489 int32_t PowerMgrStub::GetDeviceModeStub(MessageParcel& reply)
490 {
491     uint32_t ret = 0;
492     ret = static_cast<uint32_t>(GetDeviceMode());
493     if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
494         POWER_HILOGE(FEATURE_POWER_MODE, "WriteUint32 fail");
495         return E_WRITE_PARCEL_ERROR;
496     }
497     return ERR_OK;
498 }
499 
ShellDumpStub(MessageParcel & data,MessageParcel & reply)500 int32_t PowerMgrStub::ShellDumpStub(MessageParcel& data, MessageParcel& reply)
501 {
502     uint32_t argc;
503     std::vector<std::string> args;
504 
505     if (!data.ReadUint32(argc)) {
506         POWER_HILOGE(COMP_FWK, "ReadUint32 fail");
507         return E_READ_PARCEL_ERROR;
508     }
509 
510     if (argc >= PARAM_MAX_NUM) {
511         POWER_HILOGW(COMP_FWK, "params exceed limit, argc=%{public}d", argc);
512         return E_EXCEED_PARAM_LIMIT;
513     }
514 
515     for (uint32_t i = 0; i < argc; i++) {
516         std::string arg = data.ReadString();
517         if (arg.empty()) {
518             POWER_HILOGW(COMP_FWK, "read args fail, arg index=%{public}d", i);
519             return E_READ_PARCEL_ERROR;
520         }
521         args.push_back(arg);
522     }
523 
524     std::string ret = ShellDump(args, argc);
525     if (!reply.WriteString(ret)) {
526         POWER_HILOGE(COMP_FWK, "WriteString fail");
527         return E_WRITE_PARCEL_ERROR;
528     }
529     return ERR_OK;
530 }
531 
IsShutdownCommand(uint32_t code)532 bool PowerMgrStub::IsShutdownCommand(uint32_t code)
533 {
534     return (code >= static_cast<uint32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_START)) &&
535         (code <= static_cast<uint32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_END));
536 }
537 
IsStandbyStub(MessageParcel & data,MessageParcel & reply)538 int32_t PowerMgrStub::IsStandbyStub(MessageParcel& data, MessageParcel& reply)
539 {
540     bool isStandby = false;
541     PowerErrors ret = IsStandby(isStandby);
542     WRITE_PARCEL_WITH_RET(reply, Int32, static_cast<int32_t>(ret), E_WRITE_PARCEL_ERROR);
543     WRITE_PARCEL_WITH_RET(reply, Bool, isStandby, E_WRITE_PARCEL_ERROR);
544     return ERR_OK;
545 }
546 } // namespace PowerMgr
547 } // namespace OHOS
548