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