1 /*
2 * Copyright (c) 2021-2023 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 "system_suspend_controller.h"
17
18 #include "power_common.h"
19 #include "power_log.h"
20 #include "ffrt_utils.h"
21 #include "suspend/running_lock_hub.h"
22
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 const std::string HDI_SERVICE_NAME = "power_interface_service";
27 FFRTQueue g_queue("power_system_suspend_controller");
28 constexpr uint32_t RETRY_TIME = 1000;
29 } // namespace
30 using namespace OHOS::HDI::Power::V1_1;
31
SystemSuspendController()32 SystemSuspendController::SystemSuspendController() {}
33
34 SystemSuspendController::~SystemSuspendController() = default;
35
RegisterHdiStatusListener()36 void SystemSuspendController::RegisterHdiStatusListener()
37 {
38 POWER_HILOGD(COMP_SVC, "power rigister Hdi status listener");
39 hdiServiceMgr_ = OHOS::HDI::ServiceManager::V1_0::IServiceManager::Get();
40 if (hdiServiceMgr_ == nullptr) {
41 FFRTTask retryTask = [this] {
42 RegisterHdiStatusListener();
43 };
44 POWER_HILOGW(COMP_SVC, "hdi service manager is nullptr");
45 FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
46 return;
47 }
48
49 hdiServStatListener_ = new HdiServiceStatusListener(
50 HdiServiceStatusListener::StatusCallback([&](const OHOS::HDI::ServiceManager::V1_0::ServiceStatus& status) {
51 RETURN_IF(status.serviceName != HDI_SERVICE_NAME || status.deviceClass != DEVICE_CLASS_DEFAULT);
52
53 if (status.status == SERVIE_STATUS_START) {
54 FFRTTask task = [this] {
55 RegisterPowerHdiCallback();
56 };
57 FFRTUtils::SubmitTask(task);
58 POWER_HILOGI(COMP_SVC, "power interface service start");
59 } else if (status.status == SERVIE_STATUS_STOP && powerInterface_) {
60 powerInterface_ = nullptr;
61 POWER_HILOGW(COMP_SVC, "power interface service stop, unregister interface");
62 }
63 }));
64
65 int32_t status = hdiServiceMgr_->RegisterServiceStatusListener(hdiServStatListener_, DEVICE_CLASS_DEFAULT);
66 if (status != ERR_OK) {
67 FFRTTask retryTask = [this] {
68 RegisterHdiStatusListener();
69 };
70 POWER_HILOGW(COMP_SVC, "Register hdi failed");
71 FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, g_queue);
72 }
73 }
74
RegisterPowerHdiCallback()75 void SystemSuspendController::RegisterPowerHdiCallback()
76 {
77 POWER_HILOGD(COMP_SVC, "register power hdi callback");
78 if (powerInterface_ == nullptr) {
79 powerInterface_ = IPowerInterface::Get();
80 RETURN_IF_WITH_LOG(powerInterface_ == nullptr, "failed to get power hdi interface");
81 }
82 sptr<IPowerHdiCallback> callback = new PowerHdiCallback();
83 powerInterface_->RegisterCallback(callback);
84 POWER_HILOGD(COMP_SVC, "register power hdi callback end");
85 }
86
UnRegisterPowerHdiCallback()87 void SystemSuspendController::UnRegisterPowerHdiCallback()
88 {
89 POWER_HILOGD(COMP_SVC, "unregister power hdi callback");
90 if (powerInterface_ == nullptr) {
91 powerInterface_ = IPowerInterface::Get();
92 RETURN_IF_WITH_LOG(powerInterface_ == nullptr, "failed to get power hdi interface");
93 }
94 sptr<IPowerHdiCallback> callback = nullptr;
95 powerInterface_->RegisterCallback(callback);
96 POWER_HILOGD(COMP_SVC, "unregister power hdi callback end");
97 }
98
Suspend(const std::function<void ()> & onSuspend,const std::function<void ()> & onWakeup,bool force)99 void SystemSuspendController::Suspend(
100 const std::function<void()>& onSuspend, const std::function<void()>& onWakeup, bool force)
101 {
102 POWER_HILOGI(COMP_SVC, "The hdf interface");
103 if (powerInterface_ == nullptr) {
104 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
105 return;
106 }
107 if (force) {
108 powerInterface_->ForceSuspend();
109 } else {
110 powerInterface_->StartSuspend();
111 }
112 }
113
Wakeup()114 void SystemSuspendController::Wakeup()
115 {
116 if (powerInterface_ == nullptr) {
117 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
118 return;
119 }
120 powerInterface_->StopSuspend();
121 }
122
FillRunningLockInfo(const RunningLockParam & param)123 OHOS::HDI::Power::V1_1::RunningLockInfo SystemSuspendController::FillRunningLockInfo(const RunningLockParam& param)
124 {
125 OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo {};
126 filledInfo.name = param.name;
127 filledInfo.type = static_cast<OHOS::HDI::Power::V1_1::RunningLockType>(param.type);
128 filledInfo.timeoutMs = param.timeoutMs;
129 filledInfo.uid = param.uid;
130 filledInfo.pid = param.pid;
131 return filledInfo;
132 }
133
AcquireRunningLock(const RunningLockParam & param)134 int32_t SystemSuspendController::AcquireRunningLock(const RunningLockParam& param)
135 {
136 int32_t status = RUNNINGLOCK_FAILURE;
137 if (powerInterface_ == nullptr) {
138 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
139 return status;
140 }
141 if (param.type == RunningLockType::RUNNINGLOCK_BACKGROUND) {
142 status = powerInterface_->SuspendBlock(param.name);
143 } else {
144 OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo = FillRunningLockInfo(param);
145 status = powerInterface_->HoldRunningLock(filledInfo);
146 }
147 return status;
148 }
149
ReleaseRunningLock(const RunningLockParam & param)150 int32_t SystemSuspendController::ReleaseRunningLock(const RunningLockParam& param)
151 {
152 int32_t status = RUNNINGLOCK_FAILURE;
153 if (powerInterface_ == nullptr) {
154 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
155 return status;
156 }
157 if (param.type == RunningLockType::RUNNINGLOCK_BACKGROUND) {
158 status = powerInterface_->SuspendUnblock(param.name);
159 } else {
160 OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo = FillRunningLockInfo(param);
161 status = powerInterface_->UnholdRunningLock(filledInfo);
162 }
163 return status;
164 }
165
Dump(std::string & info)166 void SystemSuspendController::Dump(std::string& info)
167 {
168 if (powerInterface_ == nullptr) {
169 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
170 return;
171 }
172 powerInterface_->PowerDump(info);
173 }
174
OnSuspend()175 int32_t SystemSuspendController::PowerHdfCallback::OnSuspend()
176 {
177 if (onSuspend_ != nullptr) {
178 onSuspend_();
179 }
180 return 0;
181 }
182
OnWakeup()183 int32_t SystemSuspendController::PowerHdfCallback::OnWakeup()
184 {
185 if (onWakeup_ != nullptr) {
186 onWakeup_();
187 }
188 return 0;
189 }
190
SetListener(std::function<void ()> & suspend,std::function<void ()> & wakeup)191 void SystemSuspendController::PowerHdfCallback::SetListener(
192 std::function<void()>& suspend, std::function<void()>& wakeup)
193 {
194 onSuspend_ = suspend;
195 onWakeup_ = wakeup;
196 }
197 } // namespace PowerMgr
198 } // namespace OHOS
199