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 <iproxy_server.h>
17 #include <ohos_errno.h>
18
19 #include <pthread.h>
20 #include <unistd.h>
21
22 #include "hilog_wrapper.h"
23 #include "power_manage_feature.h"
24
25 typedef int32_t (*InvokeFunc)(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply);
26 static int32_t AcquireInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply);
27 static int32_t ReleaseInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply);
28 static int32_t IsAnyHoldingInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply);
29 static int32_t SuspendInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply);
30 static int32_t WakeupInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply);
31 static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply);
32
33 static PowerManageFeature g_feature = {
34 POWER_MANAGE_FEATURE_INTERFACE_IMPL,
35 SERVER_IPROXY_IMPL_BEGIN,
36 .Invoke = FeatureInvoke,
37 POWER_MANAGE_INTERFACE_IMPL,
38 IPROXY_END,
39 .identity = { -1, -1, NULL },
40 };
41
42 static InvokeFunc g_invokeFuncs[POWERMANAGE_FUNCID_BUTT] = {
43 AcquireInvoke,
44 ReleaseInvoke,
45 IsAnyHoldingInvoke,
46 SuspendInvoke,
47 WakeupInvoke,
48 };
49
GetPowerManageFeatureImpl(void)50 PowerManageFeature *GetPowerManageFeatureImpl(void)
51 {
52 return &g_feature;
53 }
54
FeatureInvoke(IServerProxy * iProxy,int32_t funcId,void * origin,IpcIo * req,IpcIo * reply)55 static int32_t FeatureInvoke(IServerProxy *iProxy, int32_t funcId, void *origin, IpcIo *req, IpcIo *reply)
56 {
57 if ((iProxy == NULL) || (origin == NULL) || (req == NULL)) {
58 POWER_HILOGE("Invalid parameter");
59 return EC_INVALID;
60 }
61 POWER_HILOGD("Power manage feature invoke function id: %d", funcId);
62 return (funcId >= 0 && funcId < POWERMANAGE_FUNCID_BUTT) ? g_invokeFuncs[funcId](iProxy, origin, req, reply) :
63 EC_FAILURE;
64 }
65
AcquireInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)66 static int32_t AcquireInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
67 {
68 size_t len = 0;
69 void *data = (void *)IpcIoPopFlatObj(req, &len);
70 int32_t timeoutMs = IpcIoPopInt32(req);
71 int32_t ret = OnAcquireRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data, timeoutMs);
72 IpcIoPushInt32(reply, ret);
73 return EC_SUCCESS;
74 }
75
ReleaseInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)76 static int32_t ReleaseInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
77 {
78 size_t len = 0;
79 void *data = (void *)IpcIoPopFlatObj(req, &len);
80 int32_t ret = OnReleaseRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data);
81 IpcIoPushInt32(reply, ret);
82 return EC_SUCCESS;
83 }
84
IsAnyHoldingInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)85 static int32_t IsAnyHoldingInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
86 {
87 BOOL ret = OnIsAnyRunningLockHolding((IUnknown *)iProxy);
88 IpcIoPushBool(reply, ret == TRUE);
89 return EC_SUCCESS;
90 }
91
SuspendInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)92 static int32_t SuspendInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
93 {
94 SuspendDeviceType reason = (SuspendDeviceType)IpcIoPopInt32(req);
95 BOOL suspendImmed = IpcIoPopBool(req) ? TRUE : FALSE;
96 OnSuspendDevice((IUnknown *)iProxy, reason, suspendImmed);
97 return EC_SUCCESS;
98 }
99
WakeupInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)100 static int32_t WakeupInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
101 {
102 WakeupDeviceType reason = (WakeupDeviceType)IpcIoPopInt32(req);
103 size_t len = 0;
104 const char *details = (const char *)IpcIoPopString(req, &len);
105 OnWakeupDevice((IUnknown *)iProxy, reason, details);
106 return EC_SUCCESS;
107 }
108