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 #include "power_mgr_async_reply.h"
18 
19 #include <message_parcel.h>
20 #include <string_ex.h>
21 
22 #include "running_lock_info.h"
23 #include "power_common.h"
24 #include "power_errors.h"
25 #include "power_mgr_ipc_interface_code.h"
26 #include "shutdown_stub_delegator.h"
27 #include "shutdown/shutdown_client_ipc_interface_code.h"
28 #include "xcollie/xcollie.h"
29 
30 using namespace OHOS::HiviewDFX;
31 
32 namespace OHOS {
33 namespace PowerMgr {
34 namespace {
35 constexpr uint32_t PARAM_MAX_NUM = 10;
36 constexpr uint32_t PARAM_MAX_SIZE = 2000;
37 constexpr int32_t MAX_PROXY_RUNNINGLOCK_NUM = 2000;
38 std::unique_ptr<ShutdownStubDelegator> g_shutdownDelegator;
39 std::mutex g_shutdownMutex;
40 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
42 {
43     POWER_HILOGD(COMP_FWK, "code=%{public}u, flags=%{public}d", code, option.GetFlags());
44     std::u16string descriptor = PowerMgrStub::GetDescriptor();
45     std::u16string remoteDescriptor = data.ReadInterfaceToken();
46     if (descriptor != remoteDescriptor) {
47         POWER_HILOGE(COMP_FWK, "Descriptor is not matched");
48         return E_GET_POWER_SERVICE_FAILED;
49     }
50     if (IsShutdownCommand(code)) {
51         {
52             std::lock_guard<std::mutex> lock(g_shutdownMutex);
53             if (g_shutdownDelegator == nullptr) {
54                 g_shutdownDelegator = std::make_unique<ShutdownStubDelegator>(*this);
55                 RETURN_IF_WITH_RET(g_shutdownDelegator == nullptr, ERR_NO_INIT)
56             }
57         }
58         int ret = g_shutdownDelegator->HandleRemoteRequest(code, data, reply, option);
59         if (ret == ERR_INVALID_OPERATION) {
60             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61         }
62         return ret;
63     }
64 
65     const int DFX_DELAY_S = 60;
66     int id = HiviewDFX::XCollie::GetInstance().SetTimer("PowerMgrStub", DFX_DELAY_S, nullptr, nullptr,
67         HiviewDFX::XCOLLIE_FLAG_LOG);
68 
69     int ret = ERR_OK;
70     switch (code) {
71         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::WAKEUP_DEVICE):
72             ret = WakeupDeviceStub(data, reply);
73             break;
74         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SUSPEND_DEVICE):
75             ret = SuspendDeviceStub(data, reply);
76             break;
77         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REFRESH_ACTIVITY):
78             ret = RefreshActivityStub(data);
79             break;
80         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE):
81             ret = RebootDeviceStub(data, reply);
82             break;
83         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REBOOT_DEVICE_FOR_DEPRECATED):
84             ret = RebootDeviceForDeprecatedStub(data, reply);
85             break;
86         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHUTDOWN_DEVICE):
87             ret = ShutDownDeviceStub(data, reply);
88             break;
89         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_SUSPEND_TAG):
90             ret = SetSuspendTagStub(data, reply);
91             break;
92         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::OVERRIDE_DISPLAY_OFF_TIME):
93             ret = OverrideScreenOffTimeStub(data, reply);
94             break;
95         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESTORE_DISPLAY_OFF_TIME):
96             ret = RestoreScreenOffTimeStub(reply);
97             break;
98         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GET_STATE):
99             ret = GetStateStub(reply);
100             break;
101         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_SCREEN_ON):
102             ret = IsScreenOnStub(data, reply);
103             break;
104         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_FOLD_SCREEN_ON):
105             ret = IsFoldScreenOnStub(reply);
106             break;
107         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_COLLABORATION_SCREEN_ON):
108             ret = IsCollaborationScreenOnStub(reply);
109             break;
110         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::FORCE_DEVICE_SUSPEND):
111             ret = ForceSuspendDeviceStub(data, reply);
112             break;
113         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::CREATE_RUNNINGLOCK):
114             ret = CreateRunningLockStub(data, reply);
115             break;
116         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RELEASE_RUNNINGLOCK):
117             ret = ReleaseRunningLockStub(data);
118             break;
119         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_RUNNINGLOCK_TYPE_SUPPORTED):
120             ret = IsRunningLockTypeSupportedStub(data, reply);
121             break;
122         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_LOCK):
123             ret = LockStub(data, reply);
124             break;
125         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_UNLOCK):
126             ret = UnLockStub(data, reply);
127             break;
128         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_QUERY):
129             ret = QueryRunningLockListsStub(data, reply);
130             break;
131         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCK):
132             ret = ProxyRunningLockStub(data, reply);
133             break;
134         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RUNNINGLOCK_ISUSED):
135             ret = IsUsedStub(data, reply);
136             break;
137         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::PROXY_RUNNINGLOCKS):
138             ret = ProxyRunningLocksStub(data, reply);
139             break;
140         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::RESET_RUNNINGLOCKS):
141             ret = ResetAllPorxyStub(data, reply);
142             break;
143         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_STATE_CALLBACK):
144             ret = RegisterPowerStateCallbackStub(data);
145             break;
146         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_STATE_CALLBACK):
147             ret = UnRegisterPowerStateCallbackStub(data);
148             break;
149         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_POWER_MODE_CALLBACK):
150             ret = RegisterPowerModeCallbackStub(data);
151             break;
152         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_POWER_MODE_CALLBACK):
153             ret = UnRegisterPowerModeCallbackStub(data);
154             break;
155         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_DISPLAY_SUSPEND):
156             ret = SetDisplaySuspendStub(data);
157             break;
158         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::HIBERNATE):
159             ret = HibernateStub(data, reply);
160             break;
161         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SETMODE_DEVICE):
162             ret = SetDeviceModeStub(data, reply);
163             break;
164         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::GETMODE_DEVICE):
165             ret = GetDeviceModeStub(reply);
166             break;
167         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SHELL_DUMP):
168             ret = ShellDumpStub(data, reply);
169             break;
170         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_STANDBY):
171             ret = IsStandbyStub(data, reply);
172             break;
173         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_SYNC_HIBERNATE_CALLBACK):
174             ret = RegisterSyncHibernateCallbackStub(data);
175             break;
176         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_SYNC_HIBERNATE_CALLBACK):
177             ret = UnRegisterSyncHibernateCallbackStub(data);
178             break;
179         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_SYNC_SLEEP_CALLBACK):
180             ret = RegisterSyncSleepCallbackStub(data);
181             break;
182         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_SYNC_SLEEP_CALLBACK):
183             ret = UnRegisterSyncSleepCallbackStub(data);
184             break;
185         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::SET_FORCE_TIMING_OUT):
186             ret = SetForceTimingOutStub(data, reply);
187             break;
188         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::LOCK_SCREEN_AFTER_TIMING_OUT):
189             ret = LockScreenAfterTimingOutStub(data, reply);
190             break;
191         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_RUNNINGLOCK_CALLBACK):
192             ret = RegisterRunningLockCallbackStub(data);
193             break;
194         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_RUNNINGLOCK_CALLBACK):
195             ret = UnRegisterRunningLockCallbackStub(data);
196             break;
197         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::REG_SCREEN_OFF_PRE_CALLBACK):
198             ret = RegisterScreenStateCallbackStub(data);
199             break;
200         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UNREG_SCREEN_OFF_PRE_CALLBACK):
201             ret = UnRegisterScreenStateCallbackStub(data);
202             break;
203         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::UPDATE_WORK_SOURCE):
204             ret = UpdateWorkSourceStub(data);
205             break;
206         default:
207             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
208     }
209     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
210     return ret;
211 }
212 
CreateRunningLockStub(MessageParcel & data,MessageParcel & reply)213 int32_t PowerMgrStub::CreateRunningLockStub(MessageParcel& data, MessageParcel& reply)
214 {
215     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
216     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
217     std::unique_ptr<RunningLockInfo> runningLockInfo(data.ReadParcelable<RunningLockInfo>());
218     RETURN_IF_WITH_RET((runningLockInfo == nullptr), E_READ_PARCEL_ERROR);
219     PowerErrors error = CreateRunningLock(remoteObj, *runningLockInfo);
220     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
221     return ERR_OK;
222 }
223 
ReleaseRunningLockStub(MessageParcel & data)224 int32_t PowerMgrStub::ReleaseRunningLockStub(MessageParcel& data)
225 {
226     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
227     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
228     std::string name;
229     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String, name, E_READ_PARCEL_ERROR);
230     ReleaseRunningLock(remoteObj, name);
231     return ERR_OK;
232 }
233 
IsRunningLockTypeSupportedStub(MessageParcel & data,MessageParcel & reply)234 int32_t PowerMgrStub::IsRunningLockTypeSupportedStub(MessageParcel& data, MessageParcel& reply)
235 {
236     auto type = static_cast<uint32_t >(RunningLockType::RUNNINGLOCK_BUTT);
237     bool ret = false;
238     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
239     ret = IsRunningLockTypeSupported(static_cast<RunningLockType>(type));
240     if (!reply.WriteBool(ret)) {
241         POWER_HILOGE(FEATURE_SUSPEND, "WriteBool fail");
242         return E_WRITE_PARCEL_ERROR;
243     }
244     return ERR_OK;
245 }
246 
UpdateWorkSourceStub(MessageParcel & data)247 int32_t PowerMgrStub::UpdateWorkSourceStub(MessageParcel& data)
248 {
249     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
250     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
251     uint32_t size = 0;
252     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, size, E_READ_PARCEL_ERROR);
253     if (size > PARAM_MAX_SIZE) {
254         return E_EXCEED_PARAM_LIMIT;
255     }
256     std::map<int32_t, std::string> workSources;
257     for (uint32_t i = 0; i < size; ++i) {
258         int32_t uid = 0;
259         std::string bundleName;
260         RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, uid, E_READ_PARCEL_ERROR);
261         RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String, bundleName, E_READ_PARCEL_ERROR);
262         workSources[uid] = bundleName;
263     }
264     UpdateWorkSource(remoteObj, workSources);
265     return ERR_OK;
266 }
267 
LockStub(MessageParcel & data,MessageParcel & reply)268 int32_t PowerMgrStub::LockStub(MessageParcel& data, MessageParcel& reply)
269 {
270     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
271     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
272     int32_t timeOutMs = 0;
273     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, timeOutMs, E_READ_PARCEL_ERROR);
274     PowerErrors error = Lock(remoteObj, timeOutMs);
275     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
276     return ERR_OK;
277 }
278 
UnLockStub(MessageParcel & data,MessageParcel & reply)279 int32_t PowerMgrStub::UnLockStub(MessageParcel& data, MessageParcel& reply)
280 {
281     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
282     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
283     std::string name;
284     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String, name, E_READ_PARCEL_ERROR);
285     PowerErrors error = UnLock(remoteObj, name);
286     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
287     return ERR_OK;
288 }
289 
QueryRunningLockListsStub(MessageParcel & data,MessageParcel & reply)290 int32_t PowerMgrStub::QueryRunningLockListsStub(MessageParcel& data, MessageParcel& reply)
291 {
292     std::map<std::string, RunningLockInfo> runningLockLists;
293     QueryRunningLockLists(runningLockLists);
294     reply.WriteInt32(runningLockLists.size());
295     for (auto it : runningLockLists) {
296         reply.WriteString(it.first);
297         reply.WriteParcelable(&it.second);
298     }
299     return ERR_OK;
300 }
IsUsedStub(MessageParcel & data,MessageParcel & reply)301 int32_t PowerMgrStub::IsUsedStub(MessageParcel& data, MessageParcel& reply)
302 {
303     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
304     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
305     bool ret = IsUsed(remoteObj);
306     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
307     return ERR_OK;
308 }
309 
ProxyRunningLockStub(MessageParcel & data,MessageParcel & reply)310 int32_t PowerMgrStub::ProxyRunningLockStub(MessageParcel& data, MessageParcel& reply)
311 {
312     bool isProxied = false;
313     pid_t uid;
314     pid_t pid;
315     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isProxied, E_READ_PARCEL_ERROR);
316     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, pid, E_READ_PARCEL_ERROR);
317     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, uid, E_READ_PARCEL_ERROR);
318     bool ret = ProxyRunningLock(isProxied, pid, uid);
319     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
320     return ERR_OK;
321 }
322 
ProxyRunningLocksStub(MessageParcel & data,MessageParcel & reply)323 int32_t PowerMgrStub::ProxyRunningLocksStub(MessageParcel& data, MessageParcel& reply)
324 {
325     bool isProxied = false;
326     std::vector<std::pair<pid_t, pid_t>> processInfos {};
327     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isProxied, E_READ_PARCEL_ERROR);
328     int32_t size {0};
329     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, size, E_READ_PARCEL_ERROR);
330     if (size <= 0 || size > MAX_PROXY_RUNNINGLOCK_NUM) {
331         POWER_HILOGW(COMP_FWK, "size exceed limit, size=%{public}d", size);
332         return E_EXCEED_PARAM_LIMIT;
333     }
334     processInfos.resize(size);
335     for (int i = 0; i < size; ++i) {
336         RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, processInfos[i].first, E_READ_PARCEL_ERROR);
337         RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, processInfos[i].second, E_READ_PARCEL_ERROR);
338     }
339     bool ret = ProxyRunningLocks(isProxied, processInfos);
340     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
341     return ERR_OK;
342 }
343 
ResetAllPorxyStub(MessageParcel & data,MessageParcel & reply)344 int32_t PowerMgrStub::ResetAllPorxyStub(MessageParcel& data, MessageParcel& reply)
345 {
346     bool ret = ResetRunningLocks();
347     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
348     return ERR_OK;
349 }
350 
RebootDeviceStub(MessageParcel & data,MessageParcel & reply)351 int32_t PowerMgrStub::RebootDeviceStub(MessageParcel& data, MessageParcel& reply)
352 {
353     std::string reason = Str16ToStr8(data.ReadString16());
354     PowerErrors error = RebootDevice(reason);
355     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
356     return ERR_OK;
357 }
358 
RebootDeviceForDeprecatedStub(MessageParcel & data,MessageParcel & reply)359 int32_t PowerMgrStub::RebootDeviceForDeprecatedStub(MessageParcel& data, MessageParcel& reply)
360 {
361     std::string reason = Str16ToStr8(data.ReadString16());
362     PowerErrors error = RebootDeviceForDeprecated(reason);
363     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
364     return ERR_OK;
365 }
366 
ShutDownDeviceStub(MessageParcel & data,MessageParcel & reply)367 int32_t PowerMgrStub::ShutDownDeviceStub(MessageParcel& data, MessageParcel& reply)
368 {
369     std::string reason = Str16ToStr8(data.ReadString16());
370     PowerErrors error = ShutDownDevice(reason);
371     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
372     return ERR_OK;
373 }
374 
SetSuspendTagStub(MessageParcel & data,MessageParcel & reply)375 int32_t PowerMgrStub::SetSuspendTagStub(MessageParcel& data, MessageParcel& reply)
376 {
377     std::string tag = Str16ToStr8(data.ReadString16());
378     PowerErrors error = SetSuspendTag(tag);
379     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
380     return ERR_OK;
381 }
382 
WakeupDeviceStub(MessageParcel & data,MessageParcel & reply)383 int32_t PowerMgrStub::WakeupDeviceStub(MessageParcel& data, MessageParcel& reply)
384 {
385     int64_t time = 0;
386     uint32_t reason = 0;
387 
388     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
389     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
390     std::string details = Str16ToStr8(data.ReadString16());
391 
392     PowerErrors error = WakeupDevice(time, static_cast<WakeupDeviceType>(reason), details);
393     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
394     return ERR_OK;
395 }
396 
SuspendDeviceStub(MessageParcel & data,MessageParcel & reply)397 int32_t PowerMgrStub::SuspendDeviceStub(MessageParcel& data, MessageParcel& reply)
398 {
399     int64_t time = 0;
400     uint32_t reason = 0;
401     bool suspendImmed = true;
402 
403     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
404     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
405     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, suspendImmed, E_READ_PARCEL_ERROR);
406 
407     PowerErrors error = SuspendDevice(time, static_cast<SuspendDeviceType>(reason), suspendImmed);
408     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
409     return ERR_OK;
410 }
411 
RefreshActivityStub(MessageParcel & data)412 int32_t PowerMgrStub::RefreshActivityStub(MessageParcel& data)
413 {
414     int64_t time = 0;
415     uint32_t type = 0;
416     bool needChangeBacklight = true;
417 
418     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
419     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
420     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, needChangeBacklight, E_READ_PARCEL_ERROR);
421 
422     RefreshActivity(time, static_cast<UserActivityType>(type), needChangeBacklight);
423     return ERR_OK;
424 }
425 
OverrideScreenOffTimeStub(MessageParcel & data,MessageParcel & reply)426 int32_t PowerMgrStub::OverrideScreenOffTimeStub(MessageParcel& data, MessageParcel& reply)
427 {
428     int64_t timeout = 0;
429 
430     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, timeout, E_READ_PARCEL_ERROR);
431 
432     PowerErrors error = OverrideScreenOffTime(timeout);
433     if (!reply.WriteInt32(static_cast<int32_t>(error))) {
434         POWER_HILOGE(COMP_FWK, "WriteInt32 fail");
435         return E_WRITE_PARCEL_ERROR;
436     }
437     return ERR_OK;
438 }
439 
RestoreScreenOffTimeStub(MessageParcel & reply)440 int32_t PowerMgrStub::RestoreScreenOffTimeStub(MessageParcel& reply)
441 {
442     PowerErrors error = RestoreScreenOffTime();
443     if (!reply.WriteInt32(static_cast<int32_t>(error))) {
444         POWER_HILOGE(COMP_FWK, "WriteInt32 fail");
445         return E_WRITE_PARCEL_ERROR;
446     }
447     return ERR_OK;
448 }
449 
ForceSuspendDeviceStub(MessageParcel & data,MessageParcel & reply)450 int32_t PowerMgrStub::ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& reply)
451 {
452     int64_t time = 0;
453 
454     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
455     sptr<IPowerMgrAsync> powerProxy = iface_cast<IPowerMgrAsync>(data.ReadRemoteObject());
456 
457     PowerErrors error = ForceSuspendDevice(time);
458     int result = static_cast<int>(error);
459     if (powerProxy != nullptr) {
460         powerProxy->SendAsyncReply(result);
461     }
462     return ERR_OK;
463 }
464 
GetStateStub(MessageParcel & reply)465 int32_t PowerMgrStub::GetStateStub(MessageParcel& reply)
466 {
467     PowerState ret = GetState();
468     if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
469         POWER_HILOGE(FEATURE_POWER_STATE, "WriteUint32 fail");
470         return E_WRITE_PARCEL_ERROR;
471     }
472     return ERR_OK;
473 }
474 
IsCollaborationScreenOnStub(MessageParcel & reply)475 int32_t PowerMgrStub::IsCollaborationScreenOnStub(MessageParcel& reply)
476 {
477     bool ret = false;
478     ret = IsCollaborationScreenOn();
479     if (!reply.WriteBool(ret)) {
480         POWER_HILOGE(COMP_FWK, "WriteBool fail");
481         return E_WRITE_PARCEL_ERROR;
482     }
483     return ERR_OK;
484 }
485 
IsFoldScreenOnStub(MessageParcel & reply)486 int32_t PowerMgrStub::IsFoldScreenOnStub(MessageParcel& reply)
487 {
488     bool ret = false;
489     ret = IsFoldScreenOn();
490     if (!reply.WriteBool(ret)) {
491         POWER_HILOGE(COMP_FWK, "WriteBool fail");
492         return E_WRITE_PARCEL_ERROR;
493     }
494     return ERR_OK;
495 }
496 
IsScreenOnStub(MessageParcel & data,MessageParcel & reply)497 int32_t PowerMgrStub::IsScreenOnStub(MessageParcel& data, MessageParcel& reply)
498 {
499     bool ret = false;
500     bool needPrintLog = true;
501     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, needPrintLog, E_READ_PARCEL_ERROR);
502     ret = IsScreenOn(needPrintLog);
503     if (!reply.WriteBool(ret)) {
504         POWER_HILOGE(COMP_FWK, "WriteBool fail");
505         return E_WRITE_PARCEL_ERROR;
506     }
507     return ERR_OK;
508 }
509 
RegisterPowerStateCallbackStub(MessageParcel & data)510 int32_t PowerMgrStub::RegisterPowerStateCallbackStub(MessageParcel& data)
511 {
512     sptr<IRemoteObject> obj = data.ReadRemoteObject();
513     bool isSync = true;
514     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isSync, E_READ_PARCEL_ERROR);
515     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
516     sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
517     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
518     RegisterPowerStateCallback(callback, isSync);
519     return ERR_OK;
520 }
521 
UnRegisterPowerStateCallbackStub(MessageParcel & data)522 int32_t PowerMgrStub::UnRegisterPowerStateCallbackStub(MessageParcel& data)
523 {
524     sptr<IRemoteObject> obj = data.ReadRemoteObject();
525     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
526     sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
527     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
528     UnRegisterPowerStateCallback(callback);
529     return ERR_OK;
530 }
531 
RegisterSyncHibernateCallbackStub(MessageParcel & data)532 int32_t PowerMgrStub::RegisterSyncHibernateCallbackStub(MessageParcel& data)
533 {
534     sptr<IRemoteObject> obj = data.ReadRemoteObject();
535     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
536     sptr<ISyncHibernateCallback> callback = iface_cast<ISyncHibernateCallback>(obj);
537     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
538     RegisterSyncHibernateCallback(callback);
539     return ERR_OK;
540 }
541 
UnRegisterSyncHibernateCallbackStub(MessageParcel & data)542 int32_t PowerMgrStub::UnRegisterSyncHibernateCallbackStub(MessageParcel& data)
543 {
544     sptr<IRemoteObject> obj = data.ReadRemoteObject();
545     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
546     sptr<ISyncHibernateCallback> callback = iface_cast<ISyncHibernateCallback>(obj);
547     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
548     UnRegisterSyncHibernateCallback(callback);
549     return ERR_OK;
550 }
551 
RegisterSyncSleepCallbackStub(MessageParcel & data)552 int32_t PowerMgrStub::RegisterSyncSleepCallbackStub(MessageParcel& data)
553 {
554     uint32_t priority;
555     sptr<IRemoteObject> obj = data.ReadRemoteObject();
556     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, priority, E_READ_PARCEL_ERROR);
557     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
558     sptr<ISyncSleepCallback> callback = iface_cast<ISyncSleepCallback>(obj);
559     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
560     RegisterSyncSleepCallback(callback);
561     return ERR_OK;
562 }
563 
UnRegisterSyncSleepCallbackStub(MessageParcel & data)564 int32_t PowerMgrStub::UnRegisterSyncSleepCallbackStub(MessageParcel& data)
565 {
566     sptr<IRemoteObject> obj = data.ReadRemoteObject();
567     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
568     sptr<ISyncSleepCallback> callback = iface_cast<ISyncSleepCallback>(obj);
569     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
570     UnRegisterSyncSleepCallback(callback);
571     return ERR_OK;
572 }
573 
RegisterPowerModeCallbackStub(MessageParcel & data)574 int32_t PowerMgrStub::RegisterPowerModeCallbackStub(MessageParcel& data)
575 {
576     sptr<IRemoteObject> obj = data.ReadRemoteObject();
577     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
578     sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
579     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
580     RegisterPowerModeCallback(callback);
581     return ERR_OK;
582 }
583 
UnRegisterPowerModeCallbackStub(MessageParcel & data)584 int32_t PowerMgrStub::UnRegisterPowerModeCallbackStub(MessageParcel& data)
585 {
586     sptr<IRemoteObject> obj = data.ReadRemoteObject();
587     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
588     sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
589     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
590     UnRegisterPowerModeCallback(callback);
591     return ERR_OK;
592 }
593 
RegisterScreenStateCallbackStub(MessageParcel & data)594 int32_t PowerMgrStub::RegisterScreenStateCallbackStub(MessageParcel& data)
595 {
596     int32_t remainTime = 0;
597     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, remainTime, E_READ_PARCEL_ERROR);
598 
599     sptr<IRemoteObject> obj = data.ReadRemoteObject();
600     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
601     sptr<IScreenOffPreCallback> callback = iface_cast<IScreenOffPreCallback>(obj);
602     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
603     RegisterScreenStateCallback(remainTime, callback);
604     return ERR_OK;
605 }
606 
UnRegisterScreenStateCallbackStub(MessageParcel & data)607 int32_t PowerMgrStub::UnRegisterScreenStateCallbackStub(MessageParcel& data)
608 {
609     sptr<IRemoteObject> obj = data.ReadRemoteObject();
610     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
611     sptr<IScreenOffPreCallback> callback = iface_cast<IScreenOffPreCallback>(obj);
612     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
613     UnRegisterScreenStateCallback(callback);
614     return ERR_OK;
615 }
616 
RegisterRunningLockCallbackStub(MessageParcel & data)617 int32_t PowerMgrStub::RegisterRunningLockCallbackStub(MessageParcel& data)
618 {
619     sptr<IRemoteObject> obj = data.ReadRemoteObject();
620     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
621     sptr<IPowerRunninglockCallback> callback = iface_cast<IPowerRunninglockCallback>(obj);
622     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
623     RegisterRunningLockCallback(callback);
624     return ERR_OK;
625 }
626 
UnRegisterRunningLockCallbackStub(MessageParcel & data)627 int32_t PowerMgrStub::UnRegisterRunningLockCallbackStub(MessageParcel& data)
628 {
629     sptr<IRemoteObject> obj = data.ReadRemoteObject();
630     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
631     sptr<IPowerRunninglockCallback> callback = iface_cast<IPowerRunninglockCallback>(obj);
632     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
633     UnRegisterRunningLockCallback(callback);
634     return ERR_OK;
635 }
636 
SetDisplaySuspendStub(MessageParcel & data)637 int32_t PowerMgrStub::SetDisplaySuspendStub(MessageParcel& data)
638 {
639     bool enable = false;
640     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR);
641     SetDisplaySuspend(enable);
642     return ERR_OK;
643 }
644 
HibernateStub(MessageParcel & data,MessageParcel & reply)645 int32_t PowerMgrStub::HibernateStub(MessageParcel& data, MessageParcel& reply)
646 {
647     bool clearMemory = false;
648     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, clearMemory, E_READ_PARCEL_ERROR);
649     sptr<IPowerMgrAsync> powerProxy = iface_cast<IPowerMgrAsync>(data.ReadRemoteObject());
650 
651     PowerErrors error = Hibernate(clearMemory);
652     int result = static_cast<int>(error);
653     if (powerProxy != nullptr) {
654         powerProxy->SendAsyncReply(result);
655     }
656     return ERR_OK;
657 }
658 
SetDeviceModeStub(MessageParcel & data,MessageParcel & reply)659 int32_t PowerMgrStub::SetDeviceModeStub(MessageParcel& data, MessageParcel& reply)
660 {
661     uint32_t mode = 0;
662     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, mode, E_READ_PARCEL_ERROR);
663     PowerErrors error = SetDeviceMode(static_cast<PowerMode>(mode));
664     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
665     return ERR_OK;
666 }
667 
GetDeviceModeStub(MessageParcel & reply)668 int32_t PowerMgrStub::GetDeviceModeStub(MessageParcel& reply)
669 {
670     uint32_t ret = 0;
671     ret = static_cast<uint32_t>(GetDeviceMode());
672     if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
673         POWER_HILOGE(FEATURE_POWER_MODE, "WriteUint32 fail");
674         return E_WRITE_PARCEL_ERROR;
675     }
676     return ERR_OK;
677 }
678 
ShellDumpStub(MessageParcel & data,MessageParcel & reply)679 int32_t PowerMgrStub::ShellDumpStub(MessageParcel& data, MessageParcel& reply)
680 {
681     uint32_t argc;
682     std::vector<std::string> args;
683 
684     if (!data.ReadUint32(argc)) {
685         POWER_HILOGE(COMP_FWK, "ReadUint32 fail");
686         return E_READ_PARCEL_ERROR;
687     }
688 
689     if (argc >= PARAM_MAX_NUM) {
690         POWER_HILOGW(COMP_FWK, "params exceed limit, argc=%{public}d", argc);
691         return E_EXCEED_PARAM_LIMIT;
692     }
693 
694     for (uint32_t i = 0; i < argc; i++) {
695         std::string arg = data.ReadString();
696         if (arg.empty()) {
697             POWER_HILOGW(COMP_FWK, "read args fail, arg index=%{public}d", i);
698             return E_READ_PARCEL_ERROR;
699         }
700         args.push_back(arg);
701     }
702 
703     std::string ret = ShellDump(args, argc);
704     if (!reply.WriteString(ret)) {
705         POWER_HILOGE(COMP_FWK, "WriteString fail");
706         return E_WRITE_PARCEL_ERROR;
707     }
708     return ERR_OK;
709 }
710 
IsShutdownCommand(uint32_t code)711 bool PowerMgrStub::IsShutdownCommand(uint32_t code)
712 {
713     return (code >= static_cast<uint32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_START)) &&
714         (code <= static_cast<uint32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_END));
715 }
716 
IsStandbyStub(MessageParcel & data,MessageParcel & reply)717 int32_t PowerMgrStub::IsStandbyStub(MessageParcel& data, MessageParcel& reply)
718 {
719     bool isStandby = false;
720     PowerErrors ret = IsStandby(isStandby);
721     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(ret), E_WRITE_PARCEL_ERROR);
722     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, isStandby, E_WRITE_PARCEL_ERROR);
723     return ERR_OK;
724 }
725 
SetForceTimingOutStub(MessageParcel & data,MessageParcel & reply)726 int32_t PowerMgrStub::SetForceTimingOutStub(MessageParcel& data, MessageParcel& reply)
727 {
728     POWER_HILOGI(COMP_FWK, "SetForceTimingOutStub msg received");
729     bool enabled = false;
730     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, enabled, E_READ_PARCEL_ERROR);
731     sptr<IRemoteObject> token = data.ReadRemoteObject();
732     if (!token) {
733         POWER_HILOGE(COMP_FWK, "ReadRemoteObject failed, possibly blocked by SeLinux");
734     }
735     PowerErrors ret = SetForceTimingOut(enabled, token);
736     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(ret), E_WRITE_PARCEL_ERROR);
737     return ERR_OK;
738 }
739 
LockScreenAfterTimingOutStub(MessageParcel & data,MessageParcel & reply)740 int32_t PowerMgrStub::LockScreenAfterTimingOutStub(MessageParcel& data, MessageParcel& reply)
741 {
742     bool enabledLockScreen = true;
743     bool checkLock = false;
744     bool sendScreenOffEvent = true;
745     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, enabledLockScreen, E_READ_PARCEL_ERROR);
746     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, checkLock, E_READ_PARCEL_ERROR);
747     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, sendScreenOffEvent, E_READ_PARCEL_ERROR);
748     sptr<IRemoteObject> token = data.ReadRemoteObject();
749     if (!token) {
750         POWER_HILOGI(COMP_FWK, "ReadRemoteObject failed, possibly blocked by SeLinux");
751     }
752     PowerErrors ret = LockScreenAfterTimingOut(enabledLockScreen, checkLock, sendScreenOffEvent, token);
753     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(ret), E_WRITE_PARCEL_ERROR);
754     return ERR_OK;
755 }
756 } // namespace PowerMgr
757 } // namespace OHOS
758