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 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
18 #include "hisysevent.h"
19 #endif
20 #include "power_common.h"
21 #include "power_log.h"
22 #include "suspend/running_lock_hub.h"
23
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 const std::string HDI_SERVICE_NAME = "power_interface_service";
28 constexpr uint32_t RETRY_TIME = 1000;
29 constexpr int32_t ERR_FAILED = -1;
30 using namespace OHOS::HDI::Power;
31 using namespace OHOS::HDI::Power::V1_2;
32 } // namespace
33
SystemSuspendController()34 SystemSuspendController::SystemSuspendController() {}
35
36 SystemSuspendController::~SystemSuspendController() = default;
37
RegisterHdiStatusListener()38 void SystemSuspendController::RegisterHdiStatusListener()
39 {
40 POWER_HILOGD(COMP_SVC, "power rigister Hdi status listener");
41 hdiServiceMgr_ = OHOS::HDI::ServiceManager::V1_0::IServiceManager::Get();
42 if (hdiServiceMgr_ == nullptr) {
43 FFRTTask retryTask = [this] {
44 RegisterHdiStatusListener();
45 };
46 POWER_HILOGW(COMP_SVC, "hdi service manager is nullptr");
47 FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, queue_);
48 return;
49 }
50
51 hdiServStatListener_ = new HdiServiceStatusListener(
52 HdiServiceStatusListener::StatusCallback([&](const OHOS::HDI::ServiceManager::V1_0::ServiceStatus& status) {
53 RETURN_IF(status.serviceName != HDI_SERVICE_NAME || status.deviceClass != DEVICE_CLASS_DEFAULT);
54
55 if (status.status == SERVIE_STATUS_START) {
56 FFRTTask task = [this] {
57 RegisterPowerHdiCallback();
58 };
59 FFRTUtils::SubmitTask(task);
60 POWER_HILOGI(COMP_SVC, "power interface service start");
61 } else if (status.status == SERVIE_STATUS_STOP) {
62 std::lock_guard lock(interfaceMutex_);
63 if (powerInterface_) {
64 powerInterface_ = nullptr;
65 }
66 POWER_HILOGW(COMP_SVC, "power interface service stop, unregister interface");
67 }
68 }));
69
70 int32_t status = hdiServiceMgr_->RegisterServiceStatusListener(hdiServStatListener_, DEVICE_CLASS_DEFAULT);
71 if (status != ERR_OK) {
72 FFRTTask retryTask = [this] {
73 RegisterHdiStatusListener();
74 };
75 POWER_HILOGW(COMP_SVC, "Register hdi failed");
76 FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, queue_);
77 }
78 }
79
GetPowerInterface()80 sptr<V1_3::IPowerInterface> SystemSuspendController::GetPowerInterface()
81 {
82 std::lock_guard lock(interfaceMutex_);
83 if (powerInterface_ == nullptr) {
84 powerInterface_ = V1_3::IPowerInterface::Get();
85 RETURN_IF_WITH_RET(powerInterface_ == nullptr, nullptr);
86 }
87 return powerInterface_;
88 }
89
RegisterPowerHdiCallback()90 void SystemSuspendController::RegisterPowerHdiCallback()
91 {
92 POWER_HILOGD(COMP_SVC, "register power hdi callback");
93 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
94 if (powerInterface == nullptr) {
95 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
96 return;
97 }
98 #ifdef POWER_MANAGER_ENABLE_SUSPEND_WITH_TAG
99 powerCallbackExt_ = new PowerHdiCallbackExt();
100 powerInterface->RegisterPowerCallbackExt(powerCallbackExt_);
101 #else
102 sptr<IPowerHdiCallback> callback = new PowerHdiCallback();
103 powerInterface->RegisterCallback(callback);
104 #endif
105 POWER_HILOGD(COMP_SVC, "register power hdi callback end");
106 }
107
UnRegisterPowerHdiCallback()108 void SystemSuspendController::UnRegisterPowerHdiCallback()
109 {
110 POWER_HILOGD(COMP_SVC, "unregister power hdi callback");
111 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
112 if (powerInterface == nullptr) {
113 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
114 return;
115 }
116 #ifdef POWER_MANAGER_ENABLE_SUSPEND_WITH_TAG
117 powerInterface->UnRegisterPowerCallbackExt(powerCallbackExt_);
118 powerCallbackExt_ = nullptr;
119 #else
120 sptr<IPowerHdiCallback> callback = nullptr;
121 powerInterface->RegisterCallback(callback);
122 #endif
123 POWER_HILOGD(COMP_SVC, "unregister power hdi callback end");
124 }
125
SetSuspendTag(const std::string & tag)126 void SystemSuspendController::SetSuspendTag(const std::string& tag)
127 {
128 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
129 if (powerInterface == nullptr) {
130 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
131 return;
132 }
133 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
134 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "SET_SUSPEND_TAG", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
135 "TAG", tag);
136 #endif
137 powerInterface->SetSuspendTag(tag);
138 }
139
AllowAutoSleep()140 void SystemSuspendController::AllowAutoSleep()
141 {
142 allowSleepTask_ = true;
143 }
144
DisallowAutoSleep()145 void SystemSuspendController::DisallowAutoSleep()
146 {
147 allowSleepTask_ = false;
148 }
149
Suspend(const std::function<void ()> & onSuspend,const std::function<void ()> & onWakeup,bool force)150 void SystemSuspendController::Suspend(
151 const std::function<void()>& onSuspend, const std::function<void()>& onWakeup, bool force)
152 {
153 std::lock_guard lock(mutex_);
154 POWER_HILOGI(COMP_SVC, "The hdf interface, force=%{public}u", static_cast<uint32_t>(force));
155 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
156 if (powerInterface == nullptr) {
157 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
158 return;
159 }
160 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
161 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "DO_SUSPEND", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
162 "TYPE", static_cast<int32_t>(1));
163 #endif
164 if (force) {
165 powerInterface->ForceSuspend();
166 } else if (allowSleepTask_.load()) {
167 powerInterface->StartSuspend();
168 }
169 }
170
Wakeup()171 void SystemSuspendController::Wakeup()
172 {
173 std::lock_guard lock(mutex_);
174 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
175 if (powerInterface == nullptr) {
176 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
177 return;
178 }
179 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
180 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "DO_SUSPEND", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
181 "TYPE", static_cast<int32_t>(0));
182 #endif
183 powerInterface->StopSuspend();
184 }
185
Hibernate()186 bool SystemSuspendController::Hibernate()
187 {
188 POWER_HILOGI(COMP_SVC, "SystemSuspendController hibernate begin.");
189 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
190 if (powerInterface == nullptr) {
191 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
192 return false;
193 }
194 #ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
195 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::POWER, "DO_HIBERNATE",
196 HiviewDFX::HiSysEvent::EventType::BEHAVIOR);
197 #endif
198 int32_t ret = powerInterface->Hibernate();
199 if (ret != HDF_SUCCESS) {
200 POWER_HILOGE(COMP_SVC, "SystemSuspendController hibernate failed.");
201 return false;
202 }
203 POWER_HILOGI(COMP_SVC, "SystemSuspendController hibernate end.");
204 return true;
205 }
206
FillRunningLockInfo(const RunningLockParam & param)207 OHOS::HDI::Power::V1_2::RunningLockInfo SystemSuspendController::FillRunningLockInfo(const RunningLockParam& param)
208 {
209 OHOS::HDI::Power::V1_2::RunningLockInfo filledInfo {};
210 filledInfo.name = param.name;
211 filledInfo.type = static_cast<OHOS::HDI::Power::V1_2::RunningLockType>(param.type);
212 filledInfo.timeoutMs = param.timeoutMs;
213 filledInfo.uid = param.uid;
214 filledInfo.pid = param.pid;
215 return filledInfo;
216 }
217
AcquireRunningLock(const RunningLockParam & param)218 int32_t SystemSuspendController::AcquireRunningLock(const RunningLockParam& param)
219 {
220 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
221 int32_t status = RUNNINGLOCK_FAILURE;
222 if (powerInterface == nullptr) {
223 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
224 return status;
225 }
226 OHOS::HDI::Power::V1_2::RunningLockInfo filledInfo = FillRunningLockInfo(param);
227 status = powerInterface->HoldRunningLockExt(filledInfo,
228 param.lockid, param.bundleName);
229 return status;
230 }
231
ReleaseRunningLock(const RunningLockParam & param)232 int32_t SystemSuspendController::ReleaseRunningLock(const RunningLockParam& param)
233 {
234 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
235 int32_t status = RUNNINGLOCK_FAILURE;
236 if (powerInterface == nullptr) {
237 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
238 return status;
239 }
240 OHOS::HDI::Power::V1_2::RunningLockInfo filledInfo = FillRunningLockInfo(param);
241 status = powerInterface->UnholdRunningLockExt(filledInfo,
242 param.lockid, param.bundleName);
243 return status;
244 }
245
Dump(std::string & info)246 void SystemSuspendController::Dump(std::string& info)
247 {
248 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
249 if (powerInterface == nullptr) {
250 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
251 return;
252 }
253 powerInterface->PowerDump(info);
254 }
255
GetWakeupReason(std::string & reason)256 void SystemSuspendController::GetWakeupReason(std::string& reason)
257 {
258 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
259 if (powerInterface == nullptr) {
260 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
261 return;
262 }
263 powerInterface->GetWakeupReason(reason);
264 }
265
SetPowerConfig(const std::string & sceneName,const std::string & value)266 int32_t SystemSuspendController::SetPowerConfig(const std::string& sceneName, const std::string& value)
267 {
268 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
269 if (powerInterface == nullptr) {
270 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
271 return ERR_FAILED;
272 }
273 return powerInterface->SetPowerConfig(sceneName, value);
274 }
275
GetPowerConfig(const std::string & sceneName,std::string & value)276 int32_t SystemSuspendController::GetPowerConfig(const std::string& sceneName, std::string& value)
277 {
278 sptr<V1_3::IPowerInterface> powerInterface = GetPowerInterface();
279 if (powerInterface == nullptr) {
280 POWER_HILOGE(COMP_SVC, "The hdf interface is null");
281 return ERR_FAILED;
282 }
283 return powerInterface->GetPowerConfig(sceneName, value);
284 }
285
OnSuspend()286 int32_t SystemSuspendController::PowerHdfCallback::OnSuspend()
287 {
288 if (onSuspend_ != nullptr) {
289 onSuspend_();
290 }
291 return 0;
292 }
293
OnWakeup()294 int32_t SystemSuspendController::PowerHdfCallback::OnWakeup()
295 {
296 if (onWakeup_ != nullptr) {
297 onWakeup_();
298 }
299 return 0;
300 }
301
SetListener(std::function<void ()> & suspend,std::function<void ()> & wakeup)302 void SystemSuspendController::PowerHdfCallback::SetListener(
303 std::function<void()>& suspend, std::function<void()>& wakeup)
304 {
305 onSuspend_ = suspend;
306 onWakeup_ = wakeup;
307 }
308 } // namespace PowerMgr
309 } // namespace OHOS
310