1 /*
2 * Copyright (c) 2021 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 "power_common.h"
22 #include "power_mgr_proxy.h"
23 #include "xcollie.h"
24
25 using namespace OHOS::HiviewDFX;
26
27 namespace OHOS {
28 namespace PowerMgr {
29 namespace {
30 constexpr uint32_t PARAM_MAX_NUM = 10;
31 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
33 MessageParcel &reply, MessageOption &option)
34 {
35 POWER_HILOGD(MODULE_SERVICE,
36 "PowerMgrStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d", code, option.GetFlags());
37 std::u16string descripter = PowerMgrStub::GetDescriptor();
38 std::u16string remoteDescripter = data.ReadInterfaceToken();
39 if (descripter != remoteDescripter) {
40 POWER_HILOGE(MODULE_SERVICE,
41 "PowerMgrStub::OnRemoteRequest failed, descriptor is not matched!");
42 return E_GET_POWER_SERVICE_FAILED;
43 }
44 const int DFX_DELAY_MS = 10000;
45 int id = HiviewDFX::XCollie::GetInstance().SetTimer("PowerMgrStub", DFX_DELAY_MS, nullptr, nullptr,
46 HiviewDFX::XCOLLIE_FLAG_NOOP);
47
48 int ret = ERR_OK;
49 switch (code) {
50 case static_cast<int>(IPowerMgr::WAKEUP_DEVICE):
51 ret = WakeupDeviceStub(data);
52 break;
53 case static_cast<int>(IPowerMgr::SUSPEND_DEVICE):
54 ret = SuspendDeviceStub(data);
55 break;
56 case static_cast<int>(IPowerMgr::REFRESH_ACTIVITY):
57 ret = RefreshActivityStub(data);
58 break;
59 case static_cast<int>(IPowerMgr::REBOOT_DEVICE):
60 ret = RebootDeviceStub(data);
61 break;
62 case static_cast<int>(IPowerMgr::SHUTDOWN_DEVICE):
63 ret = ShutDownDeviceStub(data);
64 break;
65 case static_cast<int>(IPowerMgr::GET_STATE):
66 ret = GetStateStub(reply);
67 break;
68 case static_cast<int>(IPowerMgr::IS_SCREEN_ON):
69 ret = IsScreeOnStub(reply);
70 break;
71 case static_cast<int>(IPowerMgr::FORCE_DEVICE_SUSPEND):
72 ret = ForceSuspendDeviceStub(data, reply);
73 break;
74 case static_cast<int>(IPowerMgr::CREATE_RUNNINGLOCK):
75 ret = CreateRunningLockStub(data);
76 break;
77 case static_cast<int>(IPowerMgr::RELEASE_RUNNINGLOCK):
78 ret = ReleaseRunningLockStub(data);
79 break;
80 case static_cast<int>(IPowerMgr::IS_RUNNINGLOCK_TYPE_SUPPORTED):
81 ret = IsRunningLockTypeSupportedStub(data);
82 break;
83 case static_cast<int>(IPowerMgr::RUNNINGLOCK_LOCK):
84 ret = LockStub(data);
85 break;
86 case static_cast<int>(IPowerMgr::RUNNINGLOCK_UNLOCK):
87 ret = UnLockStub(data);
88 break;
89 case static_cast<int>(IPowerMgr::RUNNINGLOCK_SET_WORK_TRIGGER_LIST):
90 ret = SetWorkTriggerListStub(data);
91 break;
92 case static_cast<int>(IPowerMgr::PROXY_RUNNINGLOCK):
93 ret = ProxyRunningLockStub(data);
94 break;
95 case static_cast<int>(IPowerMgr::RUNNINGLOCK_ISUSED):
96 ret = IsUsedStub(data, reply);
97 break;
98 case static_cast<int>(IPowerMgr::REG_POWER_STATE_CALLBACK):
99 ret = RegisterPowerStateCallbackStub(data);
100 break;
101 case static_cast<int>(IPowerMgr::UNREG_POWER_STATE_CALLBACK):
102 ret = UnRegisterPowerStateCallbackStub(data);
103 break;
104 case static_cast<int>(IPowerMgr::REG_SHUTDOWN_CALLBACK):
105 ret = RegisterShutdownCallbackStub(data);
106 break;
107 case static_cast<int>(IPowerMgr::UNREG_SHUTDOWN_CALLBACK):
108 ret = UnRegisterShutdownCallbackStub(data);
109 break;
110 case static_cast<int>(IPowerMgr::REG_POWER_MODE_CALLBACK):
111 ret = RegisterPowerModeCallbackStub(data);
112 break;
113 case static_cast<int>(IPowerMgr::UNREG_POWER_MODE_CALLBACK):
114 ret = UnRegisterPowerModeCallbackStub(data);
115 break;
116 case static_cast<int>(IPowerMgr::SET_DISPLAY_SUSPEND):
117 ret = SetDisplaySuspendStub(data);
118 break;
119 case static_cast<int>(IPowerMgr::SETMODE_DEVICE):
120 ret = SetDeviceModeStub(data);
121 break;
122 case static_cast<int>(IPowerMgr::GETMODE_DEVICE):
123 ret = GetDeviceModeStub(reply);
124 break;
125 case static_cast<int>(IPowerMgr::SHELL_DUMP):
126 ret = ShellDumpStub(data, reply);
127 break;
128 default:
129 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
130 }
131 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
132 return ret;
133 }
134
CreateRunningLockStub(MessageParcel & data)135 int32_t PowerMgrStub::CreateRunningLockStub(MessageParcel& data)
136 {
137 sptr<IRemoteObject> token = data.ReadRemoteObject();
138 RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR);
139 std::unique_ptr<RunningLockInfo> runningLockInfo(data.ReadParcelable<RunningLockInfo>());
140 RETURN_IF_WITH_RET((runningLockInfo == nullptr), E_READ_PARCEL_ERROR);
141 CreateRunningLock(token, *runningLockInfo);
142 return ERR_OK;
143 }
144
ReleaseRunningLockStub(MessageParcel & data)145 int32_t PowerMgrStub::ReleaseRunningLockStub(MessageParcel& data)
146 {
147 sptr<IRemoteObject> token = data.ReadRemoteObject();
148 RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR);
149 ReleaseRunningLock(token);
150 return ERR_OK;
151 }
152
IsRunningLockTypeSupportedStub(MessageParcel & data)153 int32_t PowerMgrStub::IsRunningLockTypeSupportedStub(MessageParcel& data)
154 {
155 uint32_t type = 0;
156 READ_PARCEL_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
157 IsRunningLockTypeSupported(type);
158 return ERR_OK;
159 }
160
LockStub(MessageParcel & data)161 int32_t PowerMgrStub::LockStub(MessageParcel& data)
162 {
163 sptr<IRemoteObject> token = data.ReadRemoteObject();
164 RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR);
165 std::unique_ptr<RunningLockInfo> runningLockInfo(data.ReadParcelable<RunningLockInfo>());
166 RETURN_IF_WITH_RET((runningLockInfo == nullptr), E_READ_PARCEL_ERROR);
167 uint32_t timeOutMs = 0;
168 READ_PARCEL_WITH_RET(data, Uint32, timeOutMs, E_READ_PARCEL_ERROR);
169 Lock(token, *runningLockInfo, timeOutMs);
170 return ERR_OK;
171 }
172
UnLockStub(MessageParcel & data)173 int32_t PowerMgrStub::UnLockStub(MessageParcel& data)
174 {
175 sptr<IRemoteObject> token = data.ReadRemoteObject();
176 RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR);
177 UnLock(token);
178 return ERR_OK;
179 }
180
IsUsedStub(MessageParcel & data,MessageParcel & reply)181 int32_t PowerMgrStub::IsUsedStub(MessageParcel& data, MessageParcel& reply)
182 {
183 sptr<IRemoteObject> token = data.ReadRemoteObject();
184 RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR);
185 bool ret = IsUsed(token);
186 WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR);
187 return ERR_OK;
188 }
189
SetWorkTriggerListStub(MessageParcel & data)190 int32_t PowerMgrStub::SetWorkTriggerListStub(MessageParcel& data)
191 {
192 sptr<IRemoteObject> token = data.ReadRemoteObject();
193 RETURN_IF_WITH_RET((token == nullptr), E_READ_PARCEL_ERROR);
194 WorkTriggerList workTriggerList;
195 RunningLockInfo::ReadFromParcelWorkTriggerList(data, workTriggerList);
196 SetWorkTriggerList(token, workTriggerList);
197 return ERR_OK;
198 }
199
ProxyRunningLockStub(MessageParcel & data)200 int32_t PowerMgrStub::ProxyRunningLockStub(MessageParcel& data)
201 {
202 bool proxyLock = false;
203 pid_t uid;
204 pid_t pid;
205 READ_PARCEL_WITH_RET(data, Bool, proxyLock, E_READ_PARCEL_ERROR);
206 READ_PARCEL_WITH_RET(data, Int32, uid, E_READ_PARCEL_ERROR);
207 READ_PARCEL_WITH_RET(data, Int32, pid, E_READ_PARCEL_ERROR);
208 ProxyRunningLock(proxyLock, uid, pid);
209 return ERR_OK;
210 }
211
RebootDeviceStub(MessageParcel & data)212 int32_t PowerMgrStub::RebootDeviceStub(MessageParcel& data)
213 {
214 std::string reason = Str16ToStr8(data.ReadString16());
215 RebootDevice(reason);
216 return ERR_OK;
217 }
218
ShutDownDeviceStub(MessageParcel & data)219 int32_t PowerMgrStub::ShutDownDeviceStub(MessageParcel& data)
220 {
221 std::string reason = Str16ToStr8(data.ReadString16());
222 ShutDownDevice(reason);
223 return ERR_OK;
224 }
225
WakeupDeviceStub(MessageParcel & data)226 int32_t PowerMgrStub::WakeupDeviceStub(MessageParcel& data)
227 {
228 int64_t time = 0;
229 uint32_t reason = 0;
230
231 READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
232 READ_PARCEL_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
233 std::string details = Str16ToStr8(data.ReadString16());
234
235 WakeupDevice(time, static_cast<WakeupDeviceType>(reason), details);
236 return ERR_OK;
237 }
238
SuspendDeviceStub(MessageParcel & data)239 int32_t PowerMgrStub::SuspendDeviceStub(MessageParcel& data)
240 {
241 int64_t time = 0;
242 uint32_t reason = 0;
243 bool suspendImmed = true;
244
245 READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
246 READ_PARCEL_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
247 READ_PARCEL_WITH_RET(data, Bool, suspendImmed, E_READ_PARCEL_ERROR);
248
249 SuspendDevice(time, static_cast<SuspendDeviceType>(reason), suspendImmed);
250 return ERR_OK;
251 }
252
RefreshActivityStub(MessageParcel & data)253 int32_t PowerMgrStub::RefreshActivityStub(MessageParcel& data)
254 {
255 int64_t time = 0;
256 uint32_t type = 0;
257 bool needChangeBacklight = true;
258
259 READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
260 READ_PARCEL_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
261 READ_PARCEL_WITH_RET(data, Bool, needChangeBacklight, E_READ_PARCEL_ERROR);
262
263 RefreshActivity(time, static_cast<UserActivityType>(type), needChangeBacklight);
264 return ERR_OK;
265 }
266
ForceSuspendDeviceStub(MessageParcel & data,MessageParcel & reply)267 int32_t PowerMgrStub::ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& reply)
268 {
269 bool ret = false;
270 int64_t time = 0;
271
272 READ_PARCEL_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR);
273
274 ret = ForceSuspendDevice(time);
275 if (!reply.WriteBool(ret)) {
276 POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: ForceSuspendDevice Writeback Fail!");
277 return E_WRITE_PARCEL_ERROR;
278 }
279 return ERR_OK;
280 }
281
GetStateStub(MessageParcel & reply)282 int32_t PowerMgrStub::GetStateStub(MessageParcel& reply)
283 {
284 PowerState ret = GetState();
285 if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
286 POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: GetStateStub Writeback Fail!");
287 return E_WRITE_PARCEL_ERROR;
288 }
289 return ERR_OK;
290 }
291
IsScreeOnStub(MessageParcel & reply)292 int32_t PowerMgrStub::IsScreeOnStub(MessageParcel& reply)
293 {
294 bool ret = false;
295 ret = IsScreenOn();
296 if (!reply.WriteBool(ret)) {
297 POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: IsScreenOn Writeback Fail!");
298 return E_WRITE_PARCEL_ERROR;
299 }
300 return ERR_OK;
301 }
302
RegisterPowerStateCallbackStub(MessageParcel & data)303 int32_t PowerMgrStub::RegisterPowerStateCallbackStub(MessageParcel& data)
304 {
305 sptr<IRemoteObject> obj = data.ReadRemoteObject();
306 RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
307 sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
308 RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
309 RegisterPowerStateCallback(callback);
310 return ERR_OK;
311 }
312
UnRegisterPowerStateCallbackStub(MessageParcel & data)313 int32_t PowerMgrStub::UnRegisterPowerStateCallbackStub(MessageParcel& data)
314 {
315 sptr<IRemoteObject> obj = data.ReadRemoteObject();
316 RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
317 sptr<IPowerStateCallback> callback = iface_cast<IPowerStateCallback>(obj);
318 RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
319 UnRegisterPowerStateCallback(callback);
320 return ERR_OK;
321 }
322
RegisterShutdownCallbackStub(MessageParcel & data)323 int32_t PowerMgrStub::RegisterShutdownCallbackStub(MessageParcel& data)
324 {
325 uint32_t priority;
326 READ_PARCEL_WITH_RET(data, Uint32, priority, E_READ_PARCEL_ERROR);
327 sptr<IRemoteObject> obj = data.ReadRemoteObject();
328 RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
329 sptr<IShutdownCallback> callback = iface_cast<IShutdownCallback>(obj);
330 RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
331 RegisterShutdownCallback(static_cast<IShutdownCallback::ShutdownPriority>(priority), callback);
332 return ERR_OK;
333 }
334
UnRegisterShutdownCallbackStub(MessageParcel & data)335 int32_t PowerMgrStub::UnRegisterShutdownCallbackStub(MessageParcel& data)
336 {
337 sptr<IRemoteObject> obj = data.ReadRemoteObject();
338 RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
339 sptr<IShutdownCallback> callback = iface_cast<IShutdownCallback>(obj);
340 RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
341 UnRegisterShutdownCallback(callback);
342 return ERR_OK;
343 }
344
RegisterPowerModeCallbackStub(MessageParcel & data)345 int32_t PowerMgrStub::RegisterPowerModeCallbackStub(MessageParcel& data)
346 {
347 sptr<IRemoteObject> obj = data.ReadRemoteObject();
348 RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
349 sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
350 RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
351 RegisterPowerModeCallback(callback);
352 return ERR_OK;
353 }
354
UnRegisterPowerModeCallbackStub(MessageParcel & data)355 int32_t PowerMgrStub::UnRegisterPowerModeCallbackStub(MessageParcel& data)
356 {
357 sptr<IRemoteObject> obj = data.ReadRemoteObject();
358 RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
359 sptr<IPowerModeCallback> callback = iface_cast<IPowerModeCallback>(obj);
360 RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
361 UnRegisterPowerModeCallback(callback);
362 return ERR_OK;
363 }
364
SetDisplaySuspendStub(MessageParcel & data)365 int32_t PowerMgrStub::SetDisplaySuspendStub(MessageParcel& data)
366 {
367 bool enable = false;
368 READ_PARCEL_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR);
369 SetDisplaySuspend(enable);
370 return ERR_OK;
371 }
372
SetDeviceModeStub(MessageParcel & data)373 int32_t PowerMgrStub::SetDeviceModeStub(MessageParcel& data)
374 {
375 uint32_t mode = 0;
376 READ_PARCEL_WITH_RET(data, Uint32, mode, E_READ_PARCEL_ERROR);
377 SetDeviceMode(mode);
378 return ERR_OK;
379 }
380
GetDeviceModeStub(MessageParcel & reply)381 int32_t PowerMgrStub::GetDeviceModeStub(MessageParcel& reply)
382 {
383 uint32_t ret = 0;
384 ret = GetDeviceMode();
385 POWER_HILOGD(MODULE_SERVICE, "PowerMgrStub::GetDeviceModeStub, cmd = %{public}u.", ret);
386 if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
387 POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: Get device mode Fail!");
388 return E_WRITE_PARCEL_ERROR;
389 }
390 return ERR_OK;
391 }
392
ShellDumpStub(MessageParcel & data,MessageParcel & reply)393 int32_t PowerMgrStub::ShellDumpStub(MessageParcel& data, MessageParcel& reply)
394 {
395 uint32_t argc;
396 std::vector<std::string> args;
397
398 if (!data.ReadUint32(argc)) {
399 POWER_HILOGE(MODULE_INNERKIT, "Readback fail!");
400 return E_READ_PARCEL_ERROR;
401 }
402
403 if (argc >= PARAM_MAX_NUM) {
404 POWER_HILOGW(MODULE_INNERKIT, "params exceed limit, argc=%{public}u", argc);
405 return E_EXCEED_PARAM_LIMIT;
406 }
407
408 for (uint32_t i = 0; i < argc; i++) {
409 std::string arg = data.ReadString();
410 if (arg.empty()) {
411 POWER_HILOGW(MODULE_INNERKIT, "read args fail, arg index=%{public}u", i);
412 return E_READ_PARCEL_ERROR;
413 }
414 args.push_back(arg);
415 }
416
417 std::string ret = ShellDump(args, argc);
418 if (!reply.WriteString(ret)) {
419 POWER_HILOGE(MODULE_SERVICE, "PowerMgrStub:: Dump Writeback Fail!");
420 return E_WRITE_PARCEL_ERROR;
421 }
422 return ERR_OK;
423 }
424 } // namespace PowerMgr
425 } // namespace OHOS
426