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