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_manage_feature.h"
17 
18 #include <feature.h>
19 #include <ohos_init.h>
20 #include <samgr_lite.h>
21 
22 #include "hilog_wrapper.h"
23 #include "power/suspend_controller.h"
24 #include "power_mgr.h"
25 #include "running_lock_mgr.h"
26 
GetPowerManageFeatureName(Feature * feature)27 const char *GetPowerManageFeatureName(Feature *feature)
28 {
29     (void)feature;
30     return POWER_MANAGE_FEATURE;
31 }
32 
OnPowerManageFeatureInitialize(Feature * feature,Service * parent,Identity identity)33 void OnPowerManageFeatureInitialize(Feature *feature, Service *parent, Identity identity)
34 {
35     if (feature == NULL) {
36         POWER_HILOGE("Invalid feature");
37         return;
38     }
39 
40     PowerManageFeature *f = (PowerManageFeature *)feature;
41     f->identity = identity;
42     POWER_HILOGI("Init power manage feature done");
43 }
44 
OnPowerManageFeatureStop(Feature * feature,Identity identity)45 void OnPowerManageFeatureStop(Feature *feature, Identity identity)
46 {
47     (void)feature;
48     (void)identity;
49     PowerManageFeature *f = GetPowerManageFeatureImpl();
50     if (f != NULL) {
51         f->identity.queueId = NULL;
52         f->identity.featureId = -1;
53         f->identity.serviceId = -1;
54     }
55 }
56 
OnPowerManageFeatureMessage(Feature * feature,Request * request)57 BOOL OnPowerManageFeatureMessage(Feature *feature, Request *request)
58 {
59     return ((feature != NULL) && (request != NULL)) ? TRUE : FALSE;
60 }
61 
OnAcquireRunningLockEntry(IUnknown * iUnknown,RunningLockEntry * entry,int32_t timeoutMs)62 int32_t OnAcquireRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs)
63 {
64     (void)iUnknown;
65     return RunningLockMgrAcquireEntry(entry, timeoutMs);
66 }
67 
OnReleaseRunningLockEntry(IUnknown * iUnknown,RunningLockEntry * entry)68 int32_t OnReleaseRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry)
69 {
70     (void)iUnknown;
71     return RunningLockMgrReleaseEntry(entry);
72 }
73 
OnIsAnyRunningLockHolding(IUnknown * iUnknown)74 BOOL OnIsAnyRunningLockHolding(IUnknown *iUnknown)
75 {
76     (void)iUnknown;
77     return RunningLockMgrIsAnyLockHolding();
78 }
79 
OnSuspendDevice(IUnknown * iUnknown,SuspendDeviceType reason,BOOL suspendImmed)80 void OnSuspendDevice(IUnknown *iUnknown, SuspendDeviceType reason, BOOL suspendImmed)
81 {
82     /*
83      * It should check if the calling pid has permission to suspend device
84      */
85     (void)iUnknown;
86     POWER_HILOGI("Suspending device, reason: %{public}d, suspendImmed: %{public}d", reason, suspendImmed);
87     EnableSuspend();
88 }
89 
OnWakeupDevice(IUnknown * iUnknown,WakeupDeviceType reason,const char * details)90 void OnWakeupDevice(IUnknown *iUnknown, WakeupDeviceType reason, const char* details)
91 {
92     /*
93      * It should check if the calling pid has permission to wakeup device
94      */
95     (void)iUnknown;
96     POWER_HILOGI("Waking up device, reason: %{public}d", reason);
97     DisableSuspend();
98 }
99 
Init(void)100 static void Init(void)
101 {
102     SamgrLite *sam = SAMGR_GetInstance();
103     if (sam == NULL) {
104         POWER_HILOGE("Failed to get samgr");
105         return;
106     }
107 
108     PowerManageFeature *feature = GetPowerManageFeatureImpl();
109     if (feature == NULL) {
110         POWER_HILOGE("Failed to get power manage feature");
111         return;
112     }
113     BOOL result = sam->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)feature);
114     if (result == FALSE) {
115         POWER_HILOGE("Failed to register power manage feature");
116         return;
117     }
118     result = sam->RegisterFeatureApi(POWER_MANAGE_SERVICE, POWER_MANAGE_FEATURE, GET_IUNKNOWN(*feature));
119     if (result == FALSE) {
120         POWER_HILOGE("Failed to register power manage feature api");
121         return;
122     }
123     RunningLockMgrInit();
124     SuspendControllerInit();
125     POWER_HILOGI("Succeed to register power manage feature");
126 }
127 SYS_FEATURE_INIT(Init);
128