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 "device_state_action.h"
17
18 #include <mutex>
19 #include <condition_variable>
20 #include <ipc_skeleton.h>
21 #include "display_power_mgr_client.h"
22 #include "power_log.h"
23 #include "power_state_machine_info.h"
24 #include "system_suspend_controller.h"
25
26 using namespace std;
27
28 namespace OHOS {
29 namespace PowerMgr {
30 using namespace DisplayPowerMgr;
31 using namespace Rosen;
32
DeviceStateAction()33 DeviceStateAction::DeviceStateAction()
34 {
35 dispCallback_ = new DisplayPowerCallback();
36 }
37
~DeviceStateAction()38 DeviceStateAction::~DeviceStateAction()
39 {
40 dispCallback_ = nullptr;
41 }
42
Suspend(int64_t callTimeMs,SuspendDeviceType type,uint32_t flags)43 void DeviceStateAction::Suspend(int64_t callTimeMs, SuspendDeviceType type, uint32_t flags)
44 {
45 // Display is controlled by PowerStateMachine
46 // Don't suspend until GoToSleep is called
47 }
48
ForceSuspend()49 void DeviceStateAction::ForceSuspend()
50 {
51 GoToSleep(nullptr, nullptr, true);
52 }
53
Wakeup(int64_t callTimeMs,WakeupDeviceType type,const string & details,const string & pkgName)54 void DeviceStateAction::Wakeup(int64_t callTimeMs, WakeupDeviceType type, const string& details,
55 const string& pkgName)
56 {
57 SystemSuspendController::GetInstance().Wakeup();
58 }
59
GetDisplayState()60 DisplayState DeviceStateAction::GetDisplayState()
61 {
62 DisplayPowerMgr::DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
63 POWER_HILOGD(FEATURE_POWER_STATE, "Get display state: %{public}d", state);
64 DisplayState ret = DisplayState::DISPLAY_UNKNOWN;
65 switch (state) {
66 case DisplayPowerMgr::DisplayState::DISPLAY_ON:
67 ret = DisplayState::DISPLAY_ON;
68 break;
69 case DisplayPowerMgr::DisplayState::DISPLAY_DIM:
70 ret = DisplayState::DISPLAY_DIM;
71 break;
72 case DisplayPowerMgr::DisplayState::DISPLAY_OFF:
73 ret = DisplayState::DISPLAY_OFF;
74 break;
75 case DisplayPowerMgr::DisplayState::DISPLAY_SUSPEND:
76 ret = DisplayState::DISPLAY_SUSPEND;
77 break;
78 case DisplayPowerMgr::DisplayState::DISPLAY_UNKNOWN:
79 ret = DisplayState::DISPLAY_UNKNOWN;
80 break;
81 default:
82 break;
83 }
84 return ret;
85 }
86
GetDmsReasonByPowerReason(StateChangeReason reason)87 PowerStateChangeReason DeviceStateAction::GetDmsReasonByPowerReason(StateChangeReason reason)
88 {
89 PowerStateChangeReason dmsReason = static_cast<PowerStateChangeReason>(reason);
90 switch (reason) {
91 case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT:
92 dmsReason = PowerStateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT;
93 break;
94 case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_SUCCESS:
95 dmsReason = PowerStateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_SUCCESS;
96 break;
97 case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON:
98 dmsReason = PowerStateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_ON;
99 break;
100 case StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF:
101 dmsReason = PowerStateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF;
102 break;
103 case StateChangeReason::STATE_CHANGE_REASON_POWER_KEY:
104 dmsReason = PowerStateChangeReason::STATE_CHANGE_REASON_POWER_KEY;
105 break;
106 case StateChangeReason::STATE_CHANGE_REASON_TIMEOUT_NO_SCREEN_LOCK:
107 dmsReason = PowerStateChangeReason::STATE_CHANGE_REASON_COLLABORATION;
108 break;
109 default:
110 break;
111 }
112 POWER_HILOGI(FEATURE_POWER_STATE, "The reason to DMS is = %{public}d", static_cast<uint32_t>(dmsReason));
113 return dmsReason;
114 }
115
TryToCancelScreenOff()116 bool DeviceStateAction::TryToCancelScreenOff()
117 {
118 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]ready to call TryToCancelScreenOff");
119 std::unique_lock lock(cancelScreenOffMutex_);
120 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]TryToCancelScreenOff mutex acquired");
121 if (!screenOffStarted_) {
122 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]powerkey screen off not in progress");
123 return false;
124 }
125 interruptingScreenOff_ = true;
126 // block thread until suspendbegin
127 constexpr uint32_t timeoutMs = 300;
128 bool notified = cancelScreenOffCv_.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this] {
129 return cancelScreenOffCvUnblocked_;
130 });
131 if (!notified) {
132 POWER_HILOGW(FEATURE_POWER_STATE, "[UL_POWER]TryToCancelScreenOff wait for response timed out");
133 }
134 if (screenOffInterrupted_) {
135 //not really calling the interface of DMS, interrupted early instead
136 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]Interrupted before calling SuspendBegin");
137 return true;
138 }
139 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]calling TryToCancelScreenOff");
140 screenOffInterrupted_ = DisplayManagerLite::GetInstance().TryToCancelScreenOff();
141 return screenOffInterrupted_;
142 }
143
BeginPowerkeyScreenOff()144 void DeviceStateAction::BeginPowerkeyScreenOff()
145 {
146 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]BeginPowerkeyScreenOff");
147 std::lock_guard lock(cancelScreenOffMutex_);
148 screenOffStarted_ = true;
149 cancelScreenOffCvUnblocked_ = false;
150 interruptingScreenOff_ = false;
151 screenOffInterrupted_ = false;
152 }
153
EndPowerkeyScreenOff()154 void DeviceStateAction::EndPowerkeyScreenOff()
155 {
156 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]EndPowerkeyScreenOff");
157 std::unique_lock lock(cancelScreenOffMutex_);
158 screenOffStarted_ = false;
159 cancelScreenOffCvUnblocked_ = true;
160 interruptingScreenOff_ = false;
161 screenOffInterrupted_ = false;
162 lock.unlock();
163 cancelScreenOffCv_.notify_all();
164 }
165
IsInterruptingScreenOff(PowerStateChangeReason dispReason)166 bool DeviceStateAction::IsInterruptingScreenOff(PowerStateChangeReason dispReason)
167 {
168 bool ret = false;
169 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]IsInterruptingScreenOff, dispReason: %{public}d", dispReason);
170 if (dispReason == PowerStateChangeReason::STATE_CHANGE_REASON_HARD_KEY) {
171 std::unique_lock lock(cancelScreenOffMutex_);
172 if (screenOffStarted_ && interruptingScreenOff_) {
173 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER]powerkey screen off interrupted before SuspendBegin");
174 ret = true;
175 // If there is a powerkey screen off event on going, tell the blocked thread that this function has been
176 // executed and has observed interruptingScreenOff_ set by TryToCancelScreenOff.
177 // The screen off action will be interrupted inside our own process without calling DMS interfaces
178 // and it would be safe for TryToCancelScreenOff to return true.
179 screenOffInterrupted_ = true;
180 } else {
181 // Otherwise calls SuspendBegin before unblocking TryToCancelScreenOff to reduce the possibility for
182 // TryToCancelScreenOff to fail.
183 DisplayManagerLite::GetInstance().SuspendBegin(dispReason);
184 }
185 cancelScreenOffCvUnblocked_ = true;
186 lock.unlock();
187 cancelScreenOffCv_.notify_all();
188 } else {
189 // not in the process of a powerkey screen off, calls SuspendBegin as usual
190 DisplayManagerLite::GetInstance().SuspendBegin(dispReason);
191 }
192 return ret;
193 }
194
SetDisplayState(DisplayState state,StateChangeReason reason)195 uint32_t DeviceStateAction::SetDisplayState(DisplayState state, StateChangeReason reason)
196 {
197 DisplayState currentState = GetDisplayState();
198 if (state == currentState) {
199 POWER_HILOGD(FEATURE_POWER_STATE, "Already in state: %{public}d", static_cast<uint32_t>(state));
200 return ActionResult::SUCCESS;
201 }
202 if (!isRegister_) {
203 isRegister_ = DisplayPowerMgrClient::GetInstance().RegisterCallback(dispCallback_);
204 }
205 if (reason == StateChangeReason::STATE_CHANGE_REASON_PRE_BRIGHT_AUTH_FAIL_SCREEN_OFF) {
206 state = DisplayState::DISPLAY_OFF;
207 currentState = DisplayState::DISPLAY_ON;
208 }
209 DisplayPowerMgr::DisplayState dispState = DisplayPowerMgr::DisplayState::DISPLAY_ON;
210 PowerStateChangeReason dispReason = GetDmsReasonByPowerReason(reason);
211 switch (state) {
212 case DisplayState::DISPLAY_ON: {
213 dispState = DisplayPowerMgr::DisplayState::DISPLAY_ON;
214 if (currentState == DisplayState::DISPLAY_OFF) {
215 std::string identity = IPCSkeleton::ResetCallingIdentity();
216 DisplayManagerLite::GetInstance().WakeUpBegin(dispReason);
217 IPCSkeleton::SetCallingIdentity(identity);
218 }
219 break;
220 }
221 case DisplayState::DISPLAY_DIM:
222 dispState = DisplayPowerMgr::DisplayState::DISPLAY_DIM;
223 break;
224 case DisplayState::DISPLAY_OFF: {
225 dispState = DisplayPowerMgr::DisplayState::DISPLAY_OFF;
226 if (currentState == DisplayState::DISPLAY_ON || currentState == DisplayState::DISPLAY_DIM) {
227 std::string identity = IPCSkeleton::ResetCallingIdentity();
228 // SuspendBegin is processed inside IsInterruptingScreenOff
229 if (IsInterruptingScreenOff(dispReason)) {
230 return ActionResult::FAILED;
231 }
232 IPCSkeleton::SetCallingIdentity(identity);
233 }
234 break;
235 }
236 case DisplayState::DISPLAY_SUSPEND:
237 dispState = DisplayPowerMgr::DisplayState::DISPLAY_SUSPEND;
238 break;
239 default:
240 break;
241 }
242 dispCallback_->notify_ = actionCallback_;
243 bool ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(dispState, reason);
244 POWER_HILOGI(FEATURE_POWER_STATE, "Set display state finished, ret=%{public}d", ret);
245 return ret ? ActionResult::SUCCESS : ActionResult::FAILED;
246 }
247
SetCoordinated(bool coordinated)248 void DeviceStateAction::SetCoordinated(bool coordinated)
249 {
250 coordinated_ = coordinated;
251 bool ret = DisplayPowerMgrClient::GetInstance().SetCoordinated(coordinated_);
252 POWER_HILOGI(FEATURE_POWER_STATE, "Set coordinated=%{public}d, ret=%{public}d", coordinated_, ret);
253 }
254
GoToSleep(const std::function<void ()> onSuspend,const std::function<void ()> onWakeup,bool force)255 uint32_t DeviceStateAction::GoToSleep(const std::function<void()> onSuspend,
256 const std::function<void()> onWakeup, bool force)
257 {
258 SystemSuspendController::GetInstance().Suspend(onSuspend, onWakeup, force);
259 return ActionResult::SUCCESS;
260 }
261
RegisterCallback(std::function<void (uint32_t)> & callback)262 void DeviceStateAction::RegisterCallback(std::function<void(uint32_t)>& callback)
263 {
264 actionCallback_ = callback;
265 }
266
OnDisplayStateChanged(uint32_t displayId,DisplayPowerMgr::DisplayState state,uint32_t reason)267 void DeviceStateAction::DisplayPowerCallback::OnDisplayStateChanged(uint32_t displayId,
268 DisplayPowerMgr::DisplayState state, uint32_t reason)
269 {
270 POWER_HILOGD(FEATURE_POWER_STATE, "Callback: OnDisplayStateChanged");
271 int32_t mainDisp = DisplayPowerMgrClient::GetInstance().GetMainDisplayId();
272 if (mainDisp < 0 || static_cast<uint32_t>(mainDisp) != displayId) {
273 POWER_HILOGI(FEATURE_POWER_STATE, "[UL_POWER] It's not main display, skip!");
274 return;
275 }
276 switch (state) {
277 case DisplayPowerMgr::DisplayState::DISPLAY_ON: {
278 std::string identity = IPCSkeleton::ResetCallingIdentity();
279 DisplayManagerLite::GetInstance().WakeUpEnd();
280 IPCSkeleton::SetCallingIdentity(identity);
281 NotifyDisplayActionDone(DISPLAY_ON_DONE);
282 break;
283 }
284 case DisplayPowerMgr::DisplayState::DISPLAY_OFF: {
285 std::string identity = IPCSkeleton::ResetCallingIdentity();
286 DisplayManagerLite::GetInstance().SuspendEnd();
287 IPCSkeleton::SetCallingIdentity(identity);
288 NotifyDisplayActionDone(DISPLAY_OFF_DONE);
289 break;
290 }
291 default:
292 break;
293 }
294 return;
295 }
296
NotifyDisplayActionDone(uint32_t event)297 void DeviceStateAction::DisplayPowerCallback::NotifyDisplayActionDone(uint32_t event)
298 {
299 std::lock_guard lock(notifyMutex_);
300 if (notify_ != nullptr) {
301 notify_(event);
302 }
303 }
304 } // namespace PowerMgr
305 } // namespace OHOS
306