• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "suspend/running_lock_hub.h"
21 
22 namespace OHOS {
23 namespace PowerMgr {
24 namespace {
25 const std::string HDI_SERVICE_NAME = "power_interface_service";
26 constexpr uint32_t RETRY_TIME = 1000;
27 } // namespace
28 using namespace OHOS::HDI::Power::V1_1;
29 
SystemSuspendController()30 SystemSuspendController::SystemSuspendController() {}
31 
32 SystemSuspendController::~SystemSuspendController() = default;
33 
RegisterHdiStatusListener()34 void SystemSuspendController::RegisterHdiStatusListener()
35 {
36     POWER_HILOGD(COMP_SVC, "power rigister Hdi status listener");
37     hdiServiceMgr_ = OHOS::HDI::ServiceManager::V1_0::IServiceManager::Get();
38     if (hdiServiceMgr_ == nullptr) {
39         FFRTTask retryTask = [this] {
40             RegisterHdiStatusListener();
41         };
42         POWER_HILOGW(COMP_SVC, "hdi service manager is nullptr");
43         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, queue_);
44         return;
45     }
46 
47     hdiServStatListener_ = new HdiServiceStatusListener(
48         HdiServiceStatusListener::StatusCallback([&](const OHOS::HDI::ServiceManager::V1_0::ServiceStatus& status) {
49             RETURN_IF(status.serviceName != HDI_SERVICE_NAME || status.deviceClass != DEVICE_CLASS_DEFAULT);
50 
51             if (status.status == SERVIE_STATUS_START) {
52                 FFRTTask task = [this] {
53                     RegisterPowerHdiCallback();
54                 };
55                 FFRTUtils::SubmitTask(task);
56                 POWER_HILOGI(COMP_SVC, "power interface service start");
57             } else if (status.status == SERVIE_STATUS_STOP && powerInterface_) {
58                 powerInterface_ = nullptr;
59                 POWER_HILOGW(COMP_SVC, "power interface service stop, unregister interface");
60             }
61         }));
62 
63     int32_t status = hdiServiceMgr_->RegisterServiceStatusListener(hdiServStatListener_, DEVICE_CLASS_DEFAULT);
64     if (status != ERR_OK) {
65         FFRTTask retryTask = [this] {
66             RegisterHdiStatusListener();
67         };
68         POWER_HILOGW(COMP_SVC, "Register hdi failed");
69         FFRTUtils::SubmitDelayTask(retryTask, RETRY_TIME, queue_);
70     }
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(
98     const std::function<void()>& onSuspend, const std::function<void()>& onWakeup, bool force)
99 {
100     POWER_HILOGI(COMP_SVC, "The hdf interface");
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 }
111 
Wakeup()112 void SystemSuspendController::Wakeup()
113 {
114     if (powerInterface_ == nullptr) {
115         POWER_HILOGE(COMP_SVC, "The hdf interface is null");
116         return;
117     }
118     powerInterface_->StopSuspend();
119 }
120 
FillRunningLockInfo(const RunningLockParam & param)121 OHOS::HDI::Power::V1_1::RunningLockInfo SystemSuspendController::FillRunningLockInfo(const RunningLockParam& param)
122 {
123     OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo {};
124     filledInfo.name = param.name;
125     filledInfo.type = static_cast<OHOS::HDI::Power::V1_1::RunningLockType>(param.type);
126     filledInfo.timeoutMs = param.timeoutMs;
127     filledInfo.uid = param.uid;
128     filledInfo.pid = param.pid;
129     return filledInfo;
130 }
131 
AcquireRunningLock(const RunningLockParam & param)132 int32_t SystemSuspendController::AcquireRunningLock(const RunningLockParam& param)
133 {
134     int32_t status = RUNNINGLOCK_FAILURE;
135     if (powerInterface_ == nullptr) {
136         POWER_HILOGE(COMP_SVC, "The hdf interface is null");
137         return status;
138     }
139     if (param.type == RunningLockType::RUNNINGLOCK_BACKGROUND) {
140         status = powerInterface_->SuspendBlock(param.name);
141     } else {
142         OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo = FillRunningLockInfo(param);
143         status = powerInterface_->HoldRunningLock(filledInfo);
144     }
145     return status;
146 }
147 
ReleaseRunningLock(const RunningLockParam & param)148 int32_t SystemSuspendController::ReleaseRunningLock(const RunningLockParam& param)
149 {
150     int32_t status = RUNNINGLOCK_FAILURE;
151     if (powerInterface_ == nullptr) {
152         POWER_HILOGE(COMP_SVC, "The hdf interface is null");
153         return status;
154     }
155     if (param.type == RunningLockType::RUNNINGLOCK_BACKGROUND) {
156         status = powerInterface_->SuspendUnblock(param.name);
157     } else {
158         OHOS::HDI::Power::V1_1::RunningLockInfo filledInfo = FillRunningLockInfo(param);
159         status = powerInterface_->UnholdRunningLock(filledInfo);
160     }
161     return status;
162 }
163 
Dump(std::string & info)164 void SystemSuspendController::Dump(std::string& info)
165 {
166     if (powerInterface_ == nullptr) {
167         POWER_HILOGE(COMP_SVC, "The hdf interface is null");
168         return;
169     }
170     powerInterface_->PowerDump(info);
171 }
172 
GetWakeupReason(std::string & reason)173 void SystemSuspendController::GetWakeupReason(std::string& reason)
174 {
175     if (powerInterface_ == nullptr) {
176         POWER_HILOGE(COMP_SVC, "The hdf interface is null");
177         return;
178     }
179     powerInterface_->GetWakeupReason(reason);
180 }
181 
OnSuspend()182 int32_t SystemSuspendController::PowerHdfCallback::OnSuspend()
183 {
184     if (onSuspend_ != nullptr) {
185         onSuspend_();
186     }
187     return 0;
188 }
189 
OnWakeup()190 int32_t SystemSuspendController::PowerHdfCallback::OnWakeup()
191 {
192     if (onWakeup_ != nullptr) {
193         onWakeup_();
194     }
195     return 0;
196 }
197 
SetListener(std::function<void ()> & suspend,std::function<void ()> & wakeup)198 void SystemSuspendController::PowerHdfCallback::SetListener(
199     std::function<void()>& suspend, std::function<void()>& wakeup)
200 {
201     onSuspend_ = suspend;
202     onWakeup_ = wakeup;
203 }
204 } // namespace PowerMgr
205 } // namespace OHOS
206