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) || (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     uint32_t len = 0;
69     ReadUint32(req, &len);
70     void *data = (void*)ReadBuffer(req, len);
71     int32_t timeoutMs = 0;
72     ReadInt32(req, &timeoutMs);
73     int32_t ret = OnAcquireRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data, timeoutMs);
74     WriteInt32(reply, ret);
75     return EC_SUCCESS;
76 }
77 
ReleaseInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)78 static int32_t ReleaseInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
79 {
80     uint32_t len = 0;
81     ReadUint32(req, &len);
82     void *data = (void*)ReadBuffer(req, len);
83     int32_t ret = OnReleaseRunningLockEntry((IUnknown *)iProxy, (RunningLockEntry *)data);
84     WriteInt32(reply, ret);
85     return EC_SUCCESS;
86 }
87 
IsAnyHoldingInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)88 static int32_t IsAnyHoldingInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
89 {
90     BOOL ret = OnIsAnyRunningLockHolding((IUnknown *)iProxy);
91     WriteBool(reply, ret == TRUE);
92     return EC_SUCCESS;
93 }
94 
SuspendInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)95 static int32_t SuspendInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
96 {
97     int32_t ret = 0;
98     ReadInt32(req, &ret);
99     SuspendDeviceType reason = (SuspendDeviceType)ret;
100     bool ret1 = false;
101     ReadBool(req, &ret1);
102     BOOL suspendImmed = ret1 ? TRUE : FALSE;
103     OnSuspendDevice((IUnknown *)iProxy, reason, suspendImmed);
104     return EC_SUCCESS;
105 }
106 
WakeupInvoke(IServerProxy * iProxy,void * origin,IpcIo * req,IpcIo * reply)107 static int32_t WakeupInvoke(IServerProxy *iProxy, void *origin, IpcIo *req, IpcIo *reply)
108 {
109     int32_t ret = 0;
110     ReadInt32(req, &ret);
111     WakeupDeviceType reason = (WakeupDeviceType)ret;
112     size_t len = 0;
113     const char *details = (const char *)ReadString(req, &len);
114     OnWakeupDevice((IUnknown *)iProxy, reason, details);
115     return EC_SUCCESS;
116 }
117