1cb69b360Sopenharmony_ci/*
2cb69b360Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3cb69b360Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb69b360Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb69b360Sopenharmony_ci * You may obtain a copy of the License at
6cb69b360Sopenharmony_ci *
7cb69b360Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb69b360Sopenharmony_ci *
9cb69b360Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb69b360Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb69b360Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb69b360Sopenharmony_ci * See the License for the specific language governing permissions and
13cb69b360Sopenharmony_ci * limitations under the License.
14cb69b360Sopenharmony_ci */
15cb69b360Sopenharmony_ci
16cb69b360Sopenharmony_ci#include "power_manage_feature.h"
17cb69b360Sopenharmony_ci
18cb69b360Sopenharmony_ci#include <feature.h>
19cb69b360Sopenharmony_ci#include <ohos_init.h>
20cb69b360Sopenharmony_ci#include <samgr_lite.h>
21cb69b360Sopenharmony_ci
22cb69b360Sopenharmony_ci#include "hilog_wrapper.h"
23cb69b360Sopenharmony_ci#include "power/suspend_controller.h"
24cb69b360Sopenharmony_ci#include "power_mgr.h"
25cb69b360Sopenharmony_ci#include "running_lock_mgr.h"
26cb69b360Sopenharmony_ci
27cb69b360Sopenharmony_ciconst char *GetPowerManageFeatureName(Feature *feature)
28cb69b360Sopenharmony_ci{
29cb69b360Sopenharmony_ci    (void)feature;
30cb69b360Sopenharmony_ci    return POWER_MANAGE_FEATURE;
31cb69b360Sopenharmony_ci}
32cb69b360Sopenharmony_ci
33cb69b360Sopenharmony_civoid OnPowerManageFeatureInitialize(Feature *feature, Service *parent, Identity identity)
34cb69b360Sopenharmony_ci{
35cb69b360Sopenharmony_ci    if (feature == NULL) {
36cb69b360Sopenharmony_ci        POWER_HILOGE("Invalid feature");
37cb69b360Sopenharmony_ci        return;
38cb69b360Sopenharmony_ci    }
39cb69b360Sopenharmony_ci
40cb69b360Sopenharmony_ci    PowerManageFeature *f = (PowerManageFeature *)feature;
41cb69b360Sopenharmony_ci    f->identity = identity;
42cb69b360Sopenharmony_ci    POWER_HILOGI("Init power manage feature done");
43cb69b360Sopenharmony_ci}
44cb69b360Sopenharmony_ci
45cb69b360Sopenharmony_civoid OnPowerManageFeatureStop(Feature *feature, Identity identity)
46cb69b360Sopenharmony_ci{
47cb69b360Sopenharmony_ci    (void)feature;
48cb69b360Sopenharmony_ci    (void)identity;
49cb69b360Sopenharmony_ci    PowerManageFeature *f = GetPowerManageFeatureImpl();
50cb69b360Sopenharmony_ci    if (f != NULL) {
51cb69b360Sopenharmony_ci        f->identity.queueId = NULL;
52cb69b360Sopenharmony_ci        f->identity.featureId = -1;
53cb69b360Sopenharmony_ci        f->identity.serviceId = -1;
54cb69b360Sopenharmony_ci    }
55cb69b360Sopenharmony_ci}
56cb69b360Sopenharmony_ci
57cb69b360Sopenharmony_ciBOOL OnPowerManageFeatureMessage(Feature *feature, Request *request)
58cb69b360Sopenharmony_ci{
59cb69b360Sopenharmony_ci    return ((feature != NULL) && (request != NULL)) ? TRUE : FALSE;
60cb69b360Sopenharmony_ci}
61cb69b360Sopenharmony_ci
62cb69b360Sopenharmony_ciint32_t OnAcquireRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry, int32_t timeoutMs)
63cb69b360Sopenharmony_ci{
64cb69b360Sopenharmony_ci    (void)iUnknown;
65cb69b360Sopenharmony_ci    return RunningLockMgrAcquireEntry(entry, timeoutMs);
66cb69b360Sopenharmony_ci}
67cb69b360Sopenharmony_ci
68cb69b360Sopenharmony_ciint32_t OnReleaseRunningLockEntry(IUnknown *iUnknown, RunningLockEntry *entry)
69cb69b360Sopenharmony_ci{
70cb69b360Sopenharmony_ci    (void)iUnknown;
71cb69b360Sopenharmony_ci    return RunningLockMgrReleaseEntry(entry);
72cb69b360Sopenharmony_ci}
73cb69b360Sopenharmony_ci
74cb69b360Sopenharmony_ciBOOL OnIsAnyRunningLockHolding(IUnknown *iUnknown)
75cb69b360Sopenharmony_ci{
76cb69b360Sopenharmony_ci    (void)iUnknown;
77cb69b360Sopenharmony_ci    return RunningLockMgrIsAnyLockHolding();
78cb69b360Sopenharmony_ci}
79cb69b360Sopenharmony_ci
80cb69b360Sopenharmony_civoid OnSuspendDevice(IUnknown *iUnknown, SuspendDeviceType reason, BOOL suspendImmed)
81cb69b360Sopenharmony_ci{
82cb69b360Sopenharmony_ci    /*
83cb69b360Sopenharmony_ci     * It should check if the calling pid has permission to suspend device
84cb69b360Sopenharmony_ci     */
85cb69b360Sopenharmony_ci    (void)iUnknown;
86cb69b360Sopenharmony_ci    POWER_HILOGI("Suspending device, reason: %{public}d, suspendImmed: %{public}d", reason, suspendImmed);
87cb69b360Sopenharmony_ci    EnableSuspend();
88cb69b360Sopenharmony_ci}
89cb69b360Sopenharmony_ci
90cb69b360Sopenharmony_civoid OnWakeupDevice(IUnknown *iUnknown, WakeupDeviceType reason, const char* details)
91cb69b360Sopenharmony_ci{
92cb69b360Sopenharmony_ci    /*
93cb69b360Sopenharmony_ci     * It should check if the calling pid has permission to wakeup device
94cb69b360Sopenharmony_ci     */
95cb69b360Sopenharmony_ci    (void)iUnknown;
96cb69b360Sopenharmony_ci    POWER_HILOGI("Waking up device, reason: %{public}d", reason);
97cb69b360Sopenharmony_ci    DisableSuspend();
98cb69b360Sopenharmony_ci}
99cb69b360Sopenharmony_ci
100cb69b360Sopenharmony_cistatic void Init(void)
101cb69b360Sopenharmony_ci{
102cb69b360Sopenharmony_ci    SamgrLite *sam = SAMGR_GetInstance();
103cb69b360Sopenharmony_ci    if (sam == NULL) {
104cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to get samgr");
105cb69b360Sopenharmony_ci        return;
106cb69b360Sopenharmony_ci    }
107cb69b360Sopenharmony_ci
108cb69b360Sopenharmony_ci    PowerManageFeature *feature = GetPowerManageFeatureImpl();
109cb69b360Sopenharmony_ci    if (feature == NULL) {
110cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to get power manage feature");
111cb69b360Sopenharmony_ci        return;
112cb69b360Sopenharmony_ci    }
113cb69b360Sopenharmony_ci    BOOL result = sam->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)feature);
114cb69b360Sopenharmony_ci    if (result == FALSE) {
115cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to register power manage feature");
116cb69b360Sopenharmony_ci        return;
117cb69b360Sopenharmony_ci    }
118cb69b360Sopenharmony_ci    result = sam->RegisterFeatureApi(POWER_MANAGE_SERVICE, POWER_MANAGE_FEATURE, GET_IUNKNOWN(*feature));
119cb69b360Sopenharmony_ci    if (result == FALSE) {
120cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to register power manage feature api");
121cb69b360Sopenharmony_ci        return;
122cb69b360Sopenharmony_ci    }
123cb69b360Sopenharmony_ci    RunningLockMgrInit();
124cb69b360Sopenharmony_ci    SuspendControllerInit();
125cb69b360Sopenharmony_ci    POWER_HILOGI("Succeed to register power manage feature");
126cb69b360Sopenharmony_ci}
127cb69b360Sopenharmony_ciSYS_FEATURE_INIT(Init);
128