• 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 #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(data, 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         case static_cast<int>(PowerMgr::PowerMgrInterfaceCode::IS_RUNNINGLOCK_ENABLED):
207             ret = IsRunningLockEnabledStub(data, reply);
208             break;
209         default:
210             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
211     }
212     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
213     return ret;
214 }
215 
CreateRunningLockStub(MessageParcel & data,MessageParcel & reply)216 int32_t PowerMgrStub::CreateRunningLockStub(MessageParcel& data, MessageParcel& reply)
217 {
218     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
219     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
220     std::unique_ptr<RunningLockInfo> runningLockInfo(data.ReadParcelable<RunningLockInfo>());
221     RETURN_IF_WITH_RET((runningLockInfo == nullptr), E_READ_PARCEL_ERROR);
222     PowerErrors error = CreateRunningLock(remoteObj, *runningLockInfo);
223     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
224     return ERR_OK;
225 }
226 
ReleaseRunningLockStub(MessageParcel & data)227 int32_t PowerMgrStub::ReleaseRunningLockStub(MessageParcel& data)
228 {
229     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
230     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
231     std::string name;
232     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String, name, E_READ_PARCEL_ERROR);
233     ReleaseRunningLock(remoteObj, name);
234     return ERR_OK;
235 }
236 
IsRunningLockTypeSupportedStub(MessageParcel & data,MessageParcel & reply)237 int32_t PowerMgrStub::IsRunningLockTypeSupportedStub(MessageParcel& data, MessageParcel& reply)
238 {
239     auto type = static_cast<uint32_t>(RunningLockType::RUNNINGLOCK_BUTT);
240     bool ret = false;
241     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
242     ret = IsRunningLockTypeSupported(static_cast<RunningLockType>(type));
243     if (!reply.WriteBool(ret)) {
244         POWER_HILOGE(FEATURE_SUSPEND, "WriteBool fail");
245         return E_WRITE_PARCEL_ERROR;
246     }
247     return ERR_OK;
248 }
249 
UpdateWorkSourceStub(MessageParcel & data)250 int32_t PowerMgrStub::UpdateWorkSourceStub(MessageParcel& data)
251 {
252     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
253     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
254     uint32_t size = 0;
255     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, size, E_READ_PARCEL_ERROR);
256     if (size > PARAM_MAX_SIZE) {
257         return E_EXCEED_PARAM_LIMIT;
258     }
259     std::vector<int32_t> workSources(size);
260     for (uint32_t i = 0; i < size; ++i) {
261         RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, workSources[i], E_READ_PARCEL_ERROR);
262     }
263     UpdateWorkSource(remoteObj, workSources);
264     return ERR_OK;
265 }
266 
LockStub(MessageParcel & data,MessageParcel & reply)267 int32_t PowerMgrStub::LockStub(MessageParcel& data, MessageParcel& reply)
268 {
269     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
270     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
271     int32_t timeOutMs = 0;
272     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, timeOutMs, E_READ_PARCEL_ERROR);
273     PowerErrors error = Lock(remoteObj, timeOutMs);
274     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
275     return ERR_OK;
276 }
277 
UnLockStub(MessageParcel & data,MessageParcel & reply)278 int32_t PowerMgrStub::UnLockStub(MessageParcel& data, MessageParcel& reply)
279 {
280     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
281     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
282     std::string name;
283     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, String, name, E_READ_PARCEL_ERROR);
284     PowerErrors error = UnLock(remoteObj, name);
285     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
286     return ERR_OK;
287 }
288 
QueryRunningLockListsStub(MessageParcel & data,MessageParcel & reply)289 int32_t PowerMgrStub::QueryRunningLockListsStub(MessageParcel& data, MessageParcel& reply)
290 {
291     std::map<std::string, RunningLockInfo> runningLockLists;
292     QueryRunningLockLists(runningLockLists);
293     reply.WriteInt32(runningLockLists.size());
294     for (auto it : runningLockLists) {
295         reply.WriteString(it.first);
296         reply.WriteParcelable(&it.second);
297     }
298     return ERR_OK;
299 }
IsUsedStub(MessageParcel & data,MessageParcel & reply)300 int32_t PowerMgrStub::IsUsedStub(MessageParcel& data, MessageParcel& reply)
301 {
302     sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
303     RETURN_IF_WITH_RET((remoteObj == nullptr), E_READ_PARCEL_ERROR);
304     bool ret = IsUsed(remoteObj);
305     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
306     return ERR_OK;
307 }
308 
ProxyRunningLockStub(MessageParcel & data,MessageParcel & reply)309 int32_t PowerMgrStub::ProxyRunningLockStub(MessageParcel& data, MessageParcel& reply)
310 {
311     bool isProxied = false;
312     pid_t uid;
313     pid_t pid;
314     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isProxied, E_READ_PARCEL_ERROR);
315     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, pid, E_READ_PARCEL_ERROR);
316     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, uid, E_READ_PARCEL_ERROR);
317     bool ret = ProxyRunningLock(isProxied, pid, uid);
318     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
319     return ERR_OK;
320 }
321 
ProxyRunningLocksStub(MessageParcel & data,MessageParcel & reply)322 int32_t PowerMgrStub::ProxyRunningLocksStub(MessageParcel& data, MessageParcel& reply)
323 {
324     bool isProxied = false;
325     std::vector<std::pair<pid_t, pid_t>> processInfos {};
326     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isProxied, E_READ_PARCEL_ERROR);
327     int32_t size {0};
328     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, size, E_READ_PARCEL_ERROR);
329     if (size <= 0 || size > MAX_PROXY_RUNNINGLOCK_NUM) {
330         POWER_HILOGW(COMP_FWK, "size exceed limit, size=%{public}d", size);
331         return E_EXCEED_PARAM_LIMIT;
332     }
333     processInfos.resize(size);
334     for (int i = 0; i < size; ++i) {
335         RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, processInfos[i].first, E_READ_PARCEL_ERROR);
336         RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, processInfos[i].second, E_READ_PARCEL_ERROR);
337     }
338     bool ret = ProxyRunningLocks(isProxied, processInfos);
339     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
340     return ERR_OK;
341 }
342 
ResetAllPorxyStub(MessageParcel & data,MessageParcel & reply)343 int32_t PowerMgrStub::ResetAllPorxyStub(MessageParcel& data, MessageParcel& reply)
344 {
345     bool ret = ResetRunningLocks();
346     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
347     return ERR_OK;
348 }
349 
RebootDeviceStub(MessageParcel & data,MessageParcel & reply)350 int32_t PowerMgrStub::RebootDeviceStub(MessageParcel& data, MessageParcel& reply)
351 {
352     std::string reason = Str16ToStr8(data.ReadString16());
353     PowerErrors error = RebootDevice(reason);
354     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
355     return ERR_OK;
356 }
357 
RebootDeviceForDeprecatedStub(MessageParcel & data,MessageParcel & reply)358 int32_t PowerMgrStub::RebootDeviceForDeprecatedStub(MessageParcel& data, MessageParcel& reply)
359 {
360     std::string reason = Str16ToStr8(data.ReadString16());
361     PowerErrors error = RebootDeviceForDeprecated(reason);
362     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
363     return ERR_OK;
364 }
365 
ShutDownDeviceStub(MessageParcel & data,MessageParcel & reply)366 int32_t PowerMgrStub::ShutDownDeviceStub(MessageParcel& data, MessageParcel& reply)
367 {
368     std::string reason = Str16ToStr8(data.ReadString16());
369     PowerErrors error = ShutDownDevice(reason);
370     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
371     return ERR_OK;
372 }
373 
SetSuspendTagStub(MessageParcel & data,MessageParcel & reply)374 int32_t PowerMgrStub::SetSuspendTagStub(MessageParcel& data, MessageParcel& reply)
375 {
376     std::string tag = Str16ToStr8(data.ReadString16());
377     PowerErrors error = SetSuspendTag(tag);
378     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
379     return ERR_OK;
380 }
381 
WakeupDeviceStub(MessageParcel & data,MessageParcel & reply)382 int32_t PowerMgrStub::WakeupDeviceStub(MessageParcel& data, MessageParcel& reply)
383 {
384     int64_t time = 0;
385     uint32_t reason = 0;
386 
387     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
388     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
389     std::string details = Str16ToStr8(data.ReadString16());
390     std::string apiVersion = Str16ToStr8(data.ReadString16());
391 
392     PowerErrors error = WakeupDevice(time, static_cast<WakeupDeviceType>(reason), details, apiVersion);
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     std::string apiVersion = Str16ToStr8(data.ReadString16());
407 
408     PowerErrors error = SuspendDevice(time, static_cast<SuspendDeviceType>(reason), suspendImmed, apiVersion);
409     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
410     return ERR_OK;
411 }
412 
RefreshActivityStub(MessageParcel & data)413 int32_t PowerMgrStub::RefreshActivityStub(MessageParcel& data)
414 {
415     int64_t time = 0;
416     uint32_t type = 0;
417     bool needChangeBacklight = true;
418 
419     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
420     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
421     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, needChangeBacklight, E_READ_PARCEL_ERROR);
422 
423     RefreshActivity(time, static_cast<UserActivityType>(type), needChangeBacklight);
424     return ERR_OK;
425 }
426 
OverrideScreenOffTimeStub(MessageParcel & data,MessageParcel & reply)427 int32_t PowerMgrStub::OverrideScreenOffTimeStub(MessageParcel& data, MessageParcel& reply)
428 {
429     int64_t timeout = 0;
430 
431     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, timeout, E_READ_PARCEL_ERROR);
432     std::string apiVersion = Str16ToStr8(data.ReadString16());
433 
434     PowerErrors error = OverrideScreenOffTime(timeout, apiVersion);
435     if (!reply.WriteInt32(static_cast<int32_t>(error))) {
436         POWER_HILOGE(COMP_FWK, "WriteInt32 fail");
437         return E_WRITE_PARCEL_ERROR;
438     }
439     return ERR_OK;
440 }
441 
RestoreScreenOffTimeStub(MessageParcel & data,MessageParcel & reply)442 int32_t PowerMgrStub::RestoreScreenOffTimeStub(MessageParcel& data, MessageParcel& reply)
443 {
444     std::string apiVersion = Str16ToStr8(data.ReadString16());
445     PowerErrors error = RestoreScreenOffTime(apiVersion);
446     if (!reply.WriteInt32(static_cast<int32_t>(error))) {
447         POWER_HILOGE(COMP_FWK, "WriteInt32 fail");
448         return E_WRITE_PARCEL_ERROR;
449     }
450     return ERR_OK;
451 }
452 
ForceSuspendDeviceStub(MessageParcel & data,MessageParcel & reply)453 int32_t PowerMgrStub::ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& reply)
454 {
455     int64_t time = 0;
456 
457     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
458     std::string apiVersion = Str16ToStr8(data.ReadString16());
459     sptr<IPowerMgrAsync> powerProxy = iface_cast<IPowerMgrAsync>(data.ReadRemoteObject());
460 
461     PowerErrors error = ForceSuspendDevice(time, apiVersion);
462     int result = static_cast<int>(error);
463     if (powerProxy != nullptr) {
464         powerProxy->SendAsyncReply(result);
465     }
466     return ERR_OK;
467 }
468 
GetStateStub(MessageParcel & reply)469 int32_t PowerMgrStub::GetStateStub(MessageParcel& reply)
470 {
471     PowerState ret = GetState();
472     if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
473         POWER_HILOGE(FEATURE_POWER_STATE, "WriteUint32 fail");
474         return E_WRITE_PARCEL_ERROR;
475     }
476     return ERR_OK;
477 }
478 
IsCollaborationScreenOnStub(MessageParcel & reply)479 int32_t PowerMgrStub::IsCollaborationScreenOnStub(MessageParcel& reply)
480 {
481     bool ret = false;
482     ret = IsCollaborationScreenOn();
483     if (!reply.WriteBool(ret)) {
484         POWER_HILOGE(COMP_FWK, "WriteBool fail");
485         return E_WRITE_PARCEL_ERROR;
486     }
487     return ERR_OK;
488 }
489 
IsFoldScreenOnStub(MessageParcel & reply)490 int32_t PowerMgrStub::IsFoldScreenOnStub(MessageParcel& reply)
491 {
492     bool ret = false;
493     ret = IsFoldScreenOn();
494     if (!reply.WriteBool(ret)) {
495         POWER_HILOGE(COMP_FWK, "WriteBool fail");
496         return E_WRITE_PARCEL_ERROR;
497     }
498     return ERR_OK;
499 }
500 
IsScreenOnStub(MessageParcel & data,MessageParcel & reply)501 int32_t PowerMgrStub::IsScreenOnStub(MessageParcel& data, MessageParcel& reply)
502 {
503     bool ret = false;
504     bool needPrintLog = true;
505     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, needPrintLog, E_READ_PARCEL_ERROR);
506     ret = IsScreenOn(needPrintLog);
507     if (!reply.WriteBool(ret)) {
508         POWER_HILOGE(COMP_FWK, "WriteBool fail");
509         return E_WRITE_PARCEL_ERROR;
510     }
511     return ERR_OK;
512 }
513 
RegisterPowerStateCallbackStub(MessageParcel & data)514 int32_t PowerMgrStub::RegisterPowerStateCallbackStub(MessageParcel& data)
515 {
516     sptr<IRemoteObject> obj = data.ReadRemoteObject();
517     bool isSync = true;
518     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isSync, E_READ_PARCEL_ERROR);
519     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
520     sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
521     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
522     RegisterPowerStateCallback(callback, isSync);
523     return ERR_OK;
524 }
525 
UnRegisterPowerStateCallbackStub(MessageParcel & data)526 int32_t PowerMgrStub::UnRegisterPowerStateCallbackStub(MessageParcel& data)
527 {
528     sptr<IRemoteObject> obj = data.ReadRemoteObject();
529     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
530     sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
531     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
532     UnRegisterPowerStateCallback(callback);
533     return ERR_OK;
534 }
535 
RegisterSyncHibernateCallbackStub(MessageParcel & data)536 int32_t PowerMgrStub::RegisterSyncHibernateCallbackStub(MessageParcel& data)
537 {
538     sptr<IRemoteObject> obj = data.ReadRemoteObject();
539     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
540     sptr<ISyncHibernateCallback> callback = iface_cast<ISyncHibernateCallback>(obj);
541     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
542     RegisterSyncHibernateCallback(callback);
543     return ERR_OK;
544 }
545 
UnRegisterSyncHibernateCallbackStub(MessageParcel & data)546 int32_t PowerMgrStub::UnRegisterSyncHibernateCallbackStub(MessageParcel& data)
547 {
548     sptr<IRemoteObject> obj = data.ReadRemoteObject();
549     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
550     sptr<ISyncHibernateCallback> callback = iface_cast<ISyncHibernateCallback>(obj);
551     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
552     UnRegisterSyncHibernateCallback(callback);
553     return ERR_OK;
554 }
555 
RegisterSyncSleepCallbackStub(MessageParcel & data)556 int32_t PowerMgrStub::RegisterSyncSleepCallbackStub(MessageParcel& data)
557 {
558     uint32_t priority;
559     sptr<IRemoteObject> obj = data.ReadRemoteObject();
560     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, priority, E_READ_PARCEL_ERROR);
561     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
562     sptr<ISyncSleepCallback> callback = iface_cast<ISyncSleepCallback>(obj);
563     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
564     RegisterSyncSleepCallback(callback);
565     return ERR_OK;
566 }
567 
UnRegisterSyncSleepCallbackStub(MessageParcel & data)568 int32_t PowerMgrStub::UnRegisterSyncSleepCallbackStub(MessageParcel& data)
569 {
570     sptr<IRemoteObject> obj = data.ReadRemoteObject();
571     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
572     sptr<ISyncSleepCallback> callback = iface_cast<ISyncSleepCallback>(obj);
573     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
574     UnRegisterSyncSleepCallback(callback);
575     return ERR_OK;
576 }
577 
RegisterPowerModeCallbackStub(MessageParcel & data)578 int32_t PowerMgrStub::RegisterPowerModeCallbackStub(MessageParcel& data)
579 {
580     sptr<IRemoteObject> obj = data.ReadRemoteObject();
581     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
582     sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
583     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
584     RegisterPowerModeCallback(callback);
585     return ERR_OK;
586 }
587 
UnRegisterPowerModeCallbackStub(MessageParcel & data)588 int32_t PowerMgrStub::UnRegisterPowerModeCallbackStub(MessageParcel& data)
589 {
590     sptr<IRemoteObject> obj = data.ReadRemoteObject();
591     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
592     sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
593     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
594     UnRegisterPowerModeCallback(callback);
595     return ERR_OK;
596 }
597 
RegisterScreenStateCallbackStub(MessageParcel & data)598 int32_t PowerMgrStub::RegisterScreenStateCallbackStub(MessageParcel& data)
599 {
600     int32_t remainTime = 0;
601     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int32, remainTime, E_READ_PARCEL_ERROR);
602 
603     sptr<IRemoteObject> obj = data.ReadRemoteObject();
604     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
605     sptr<IScreenOffPreCallback> callback = iface_cast<IScreenOffPreCallback>(obj);
606     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
607     RegisterScreenStateCallback(remainTime, callback);
608     return ERR_OK;
609 }
610 
UnRegisterScreenStateCallbackStub(MessageParcel & data)611 int32_t PowerMgrStub::UnRegisterScreenStateCallbackStub(MessageParcel& data)
612 {
613     sptr<IRemoteObject> obj = data.ReadRemoteObject();
614     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
615     sptr<IScreenOffPreCallback> callback = iface_cast<IScreenOffPreCallback>(obj);
616     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
617     UnRegisterScreenStateCallback(callback);
618     return ERR_OK;
619 }
620 
RegisterRunningLockCallbackStub(MessageParcel & data)621 int32_t PowerMgrStub::RegisterRunningLockCallbackStub(MessageParcel& data)
622 {
623     sptr<IRemoteObject> obj = data.ReadRemoteObject();
624     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
625     sptr<IPowerRunninglockCallback> callback = iface_cast<IPowerRunninglockCallback>(obj);
626     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
627     RegisterRunningLockCallback(callback);
628     return ERR_OK;
629 }
630 
UnRegisterRunningLockCallbackStub(MessageParcel & data)631 int32_t PowerMgrStub::UnRegisterRunningLockCallbackStub(MessageParcel& data)
632 {
633     sptr<IRemoteObject> obj = data.ReadRemoteObject();
634     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
635     sptr<IPowerRunninglockCallback> callback = iface_cast<IPowerRunninglockCallback>(obj);
636     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
637     UnRegisterRunningLockCallback(callback);
638     return ERR_OK;
639 }
640 
SetDisplaySuspendStub(MessageParcel & data)641 int32_t PowerMgrStub::SetDisplaySuspendStub(MessageParcel& data)
642 {
643     bool enable = false;
644     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR);
645     SetDisplaySuspend(enable);
646     return ERR_OK;
647 }
648 
HibernateStub(MessageParcel & data,MessageParcel & reply)649 int32_t PowerMgrStub::HibernateStub(MessageParcel& data, MessageParcel& reply)
650 {
651     bool clearMemory = false;
652     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, clearMemory, E_READ_PARCEL_ERROR);
653     std::string apiVersion = Str16ToStr8(data.ReadString16());
654     sptr<IPowerMgrAsync> powerProxy = iface_cast<IPowerMgrAsync>(data.ReadRemoteObject());
655 
656     PowerErrors error = Hibernate(clearMemory, apiVersion);
657     int result = static_cast<int>(error);
658     if (powerProxy != nullptr) {
659         powerProxy->SendAsyncReply(result);
660     }
661     return ERR_OK;
662 }
663 
SetDeviceModeStub(MessageParcel & data,MessageParcel & reply)664 int32_t PowerMgrStub::SetDeviceModeStub(MessageParcel& data, MessageParcel& reply)
665 {
666     uint32_t mode = 0;
667     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, mode, E_READ_PARCEL_ERROR);
668     PowerErrors error = SetDeviceMode(static_cast<PowerMode>(mode));
669     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
670     return ERR_OK;
671 }
672 
GetDeviceModeStub(MessageParcel & reply)673 int32_t PowerMgrStub::GetDeviceModeStub(MessageParcel& reply)
674 {
675     uint32_t ret = 0;
676     ret = static_cast<uint32_t>(GetDeviceMode());
677     if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
678         POWER_HILOGE(FEATURE_POWER_MODE, "WriteUint32 fail");
679         return E_WRITE_PARCEL_ERROR;
680     }
681     return ERR_OK;
682 }
683 
ShellDumpStub(MessageParcel & data,MessageParcel & reply)684 int32_t PowerMgrStub::ShellDumpStub(MessageParcel& data, MessageParcel& reply)
685 {
686     uint32_t argc;
687     std::vector<std::string> args;
688 
689     if (!data.ReadUint32(argc)) {
690         POWER_HILOGE(COMP_FWK, "ReadUint32 fail");
691         return E_READ_PARCEL_ERROR;
692     }
693 
694     if (argc >= PARAM_MAX_NUM) {
695         POWER_HILOGW(COMP_FWK, "params exceed limit, argc=%{public}d", argc);
696         return E_EXCEED_PARAM_LIMIT;
697     }
698 
699     for (uint32_t i = 0; i < argc; i++) {
700         std::string arg = data.ReadString();
701         if (arg.empty()) {
702             POWER_HILOGW(COMP_FWK, "read args fail, arg index=%{public}d", i);
703             return E_READ_PARCEL_ERROR;
704         }
705         args.push_back(arg);
706     }
707 
708     std::string ret = ShellDump(args, argc);
709     if (!reply.WriteString(ret)) {
710         POWER_HILOGE(COMP_FWK, "WriteString fail");
711         return E_WRITE_PARCEL_ERROR;
712     }
713     return ERR_OK;
714 }
715 
IsShutdownCommand(uint32_t code)716 bool PowerMgrStub::IsShutdownCommand(uint32_t code)
717 {
718     return (code >= static_cast<uint32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_START)) &&
719         (code <= static_cast<uint32_t>(PowerMgr::ShutdownClientInterfaceCode::CMD_END));
720 }
721 
IsStandbyStub(MessageParcel & data,MessageParcel & reply)722 int32_t PowerMgrStub::IsStandbyStub(MessageParcel& data, MessageParcel& reply)
723 {
724     bool isStandby = false;
725     PowerErrors ret = IsStandby(isStandby);
726     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(ret), E_WRITE_PARCEL_ERROR);
727     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, isStandby, E_WRITE_PARCEL_ERROR);
728     return ERR_OK;
729 }
730 
SetForceTimingOutStub(MessageParcel & data,MessageParcel & reply)731 int32_t PowerMgrStub::SetForceTimingOutStub(MessageParcel& data, MessageParcel& reply)
732 {
733     POWER_HILOGI(COMP_FWK, "SetForceTimingOutStub msg received");
734     bool enabled = false;
735     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, enabled, E_READ_PARCEL_ERROR);
736     sptr<IRemoteObject> token = data.ReadRemoteObject();
737     if (!token) {
738         POWER_HILOGE(COMP_FWK, "ReadRemoteObject failed, possibly blocked by SeLinux");
739     }
740     PowerErrors ret = SetForceTimingOut(enabled, token);
741     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(ret), E_WRITE_PARCEL_ERROR);
742     return ERR_OK;
743 }
744 
LockScreenAfterTimingOutStub(MessageParcel & data,MessageParcel & reply)745 int32_t PowerMgrStub::LockScreenAfterTimingOutStub(MessageParcel& data, MessageParcel& reply)
746 {
747     bool enabledLockScreen = true;
748     bool checkLock = false;
749     bool sendScreenOffEvent = true;
750     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, enabledLockScreen, E_READ_PARCEL_ERROR);
751     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, checkLock, E_READ_PARCEL_ERROR);
752     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, sendScreenOffEvent, E_READ_PARCEL_ERROR);
753     sptr<IRemoteObject> token = data.ReadRemoteObject();
754     if (!token) {
755         POWER_HILOGI(COMP_FWK, "ReadRemoteObject failed, possibly blocked by SeLinux");
756     }
757     PowerErrors ret = LockScreenAfterTimingOut(enabledLockScreen, checkLock, sendScreenOffEvent, token);
758     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(ret), E_WRITE_PARCEL_ERROR);
759     return ERR_OK;
760 }
761 
IsRunningLockEnabledStub(MessageParcel & data,MessageParcel & reply)762 int32_t PowerMgrStub::IsRunningLockEnabledStub(MessageParcel& data, MessageParcel& reply)
763 {
764     auto type = static_cast<uint32_t>(RunningLockType::RUNNINGLOCK_BUTT);
765     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
766     bool result = false;
767     PowerErrors error = IsRunningLockEnabled(static_cast<RunningLockType>(type), result);
768     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast<int32_t>(error), E_WRITE_PARCEL_ERROR);
769     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, result, E_WRITE_PARCEL_ERROR);
770     return ERR_OK;
771 }
772 } // namespace PowerMgr
773 } // namespace OHOS
774