• 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 "device_state_action.h"
17 
18 #include <ipc_skeleton.h>
19 #include "display_manager.h"
20 #include "display_power_mgr_client.h"
21 #include "power_log.h"
22 #include "power_state_machine_info.h"
23 #include "system_suspend_controller.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace PowerMgr {
29 using namespace DisplayPowerMgr;
30 using namespace Rosen;
31 
DeviceStateAction()32 DeviceStateAction::DeviceStateAction()
33 {
34     dispCallback_ = new DisplayPowerCallback();
35 }
36 
~DeviceStateAction()37 DeviceStateAction::~DeviceStateAction()
38 {
39     dispCallback_ = nullptr;
40 }
41 
Suspend(int64_t callTimeMs,SuspendDeviceType type,uint32_t flags)42 void DeviceStateAction::Suspend(int64_t callTimeMs, SuspendDeviceType type, uint32_t flags)
43 {
44     // Display is controlled by PowerStateMachine
45     // Don't suspend until GoToSleep is called
46 }
47 
ForceSuspend()48 void DeviceStateAction::ForceSuspend()
49 {
50     GoToSleep(nullptr, nullptr, true);
51 }
52 
Wakeup(int64_t callTimeMs,WakeupDeviceType type,const string & details,const string & pkgName)53 void DeviceStateAction::Wakeup(int64_t callTimeMs, WakeupDeviceType type, const string& details,
54     const string& pkgName)
55 {
56     SystemSuspendController::GetInstance().Wakeup();
57 }
58 
GetDisplayState()59 DisplayState DeviceStateAction::GetDisplayState()
60 {
61     DisplayPowerMgr::DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState();
62     POWER_HILOGD(FEATURE_POWER_STATE, "Get display state: %{public}d", state);
63     DisplayState ret = DisplayState::DISPLAY_UNKNOWN;
64     switch (state) {
65         case DisplayPowerMgr::DisplayState::DISPLAY_ON:
66             ret = DisplayState::DISPLAY_ON;
67             break;
68         case DisplayPowerMgr::DisplayState::DISPLAY_DIM:
69             ret = DisplayState::DISPLAY_DIM;
70             break;
71         case DisplayPowerMgr::DisplayState::DISPLAY_OFF:
72             ret = DisplayState::DISPLAY_OFF;
73             break;
74         case DisplayPowerMgr::DisplayState::DISPLAY_SUSPEND:
75             ret = DisplayState::DISPLAY_SUSPEND;
76             break;
77         case DisplayPowerMgr::DisplayState::DISPLAY_UNKNOWN:
78             ret = DisplayState::DISPLAY_UNKNOWN;
79             break;
80         default:
81             break;
82     }
83     return ret;
84 }
85 
SetDisplayState(const DisplayState state,StateChangeReason reason)86 uint32_t DeviceStateAction::SetDisplayState(const DisplayState state, StateChangeReason reason)
87 {
88     POWER_HILOGD(FEATURE_POWER_STATE, "Action: SetDisplayState: DisplayState=%{public}d, StateChangeReason=%{public}d",
89         static_cast<uint32_t>(state), static_cast<uint32_t>(reason));
90 
91     DisplayState currentState = GetDisplayState();
92     if (state == currentState) {
93         POWER_HILOGD(FEATURE_POWER_STATE, "Already in state: %{public}d", static_cast<uint32_t>(state));
94         return ActionResult::SUCCESS;
95     }
96 
97     if (!isRegister_) {
98         isRegister_ = DisplayPowerMgrClient::GetInstance().RegisterCallback(dispCallback_);
99         POWER_HILOGI(FEATURE_POWER_STATE, "Register Callback is %{public}d", isRegister_);
100     }
101 
102     DisplayPowerMgr::DisplayState dispState = DisplayPowerMgr::DisplayState::DISPLAY_ON;
103     switch (state) {
104         case DisplayState::DISPLAY_ON: {
105             dispState = DisplayPowerMgr::DisplayState::DISPLAY_ON;
106             if (currentState == DisplayState::DISPLAY_OFF && reason != StateChangeReason::STATE_CHANGE_REASON_SENSOR) {
107                 std::string identity = IPCSkeleton::ResetCallingIdentity();
108                 DisplayManager::GetInstance().WakeUpBegin(PowerStateChangeReason::POWER_BUTTON);
109                 IPCSkeleton::SetCallingIdentity(identity);
110             }
111             break;
112         }
113         case DisplayState::DISPLAY_DIM:
114             dispState = DisplayPowerMgr::DisplayState::DISPLAY_DIM;
115             break;
116         case DisplayState::DISPLAY_OFF: {
117             dispState = DisplayPowerMgr::DisplayState::DISPLAY_OFF;
118             if ((currentState == DisplayState::DISPLAY_ON || currentState == DisplayState::DISPLAY_DIM) &&
119                 reason != StateChangeReason::STATE_CHANGE_REASON_SENSOR) {
120                 std::string identity = IPCSkeleton::ResetCallingIdentity();
121                 DisplayManager::GetInstance().SuspendBegin(PowerStateChangeReason::POWER_BUTTON);
122                 IPCSkeleton::SetCallingIdentity(identity);
123             }
124             break;
125         }
126         case DisplayState::DISPLAY_SUSPEND:
127             dispState = DisplayPowerMgr::DisplayState::DISPLAY_SUSPEND;
128             break;
129         default:
130             break;
131     }
132     dispCallback_->notify_ = actionCallback_;
133     bool ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(dispState, reason);
134     POWER_HILOGI(FEATURE_POWER_STATE, "Set display state finished, ret: %{public}d", ret);
135     return ret ? ActionResult::SUCCESS : ActionResult::FAILED;
136 }
137 
GoToSleep(const std::function<void ()> onSuspend,const std::function<void ()> onWakeup,bool force)138 uint32_t DeviceStateAction::GoToSleep(const std::function<void()> onSuspend,
139     const std::function<void()> onWakeup, bool force)
140 {
141     SystemSuspendController::GetInstance().Suspend(onSuspend, onWakeup, force);
142     return ActionResult::SUCCESS;
143 }
144 
RegisterCallback(std::function<void (uint32_t)> & callback)145 void DeviceStateAction::RegisterCallback(std::function<void(uint32_t)>& callback)
146 {
147     actionCallback_ = callback;
148 }
149 
OnDisplayStateChanged(uint32_t displayId,DisplayPowerMgr::DisplayState state,uint32_t reason)150 void DeviceStateAction::DisplayPowerCallback::OnDisplayStateChanged(uint32_t displayId,
151     DisplayPowerMgr::DisplayState state, uint32_t reason)
152 {
153     POWER_HILOGD(FEATURE_POWER_STATE, "Callback: OnDisplayStateChanged");
154     int32_t mainDisp = DisplayPowerMgrClient::GetInstance().GetMainDisplayId();
155     if (mainDisp < 0 || static_cast<uint32_t>(mainDisp) != displayId) {
156         POWER_HILOGI(FEATURE_POWER_STATE, "It's not main display, skip!");
157         return;
158     }
159     switch (state) {
160         case DisplayPowerMgr::DisplayState::DISPLAY_ON: {
161             if (StateChangeReason(reason) != StateChangeReason::STATE_CHANGE_REASON_SENSOR) {
162                 std::string identity = IPCSkeleton::ResetCallingIdentity();
163                 DisplayManager::GetInstance().WakeUpEnd();
164                 IPCSkeleton::SetCallingIdentity(identity);
165             }
166             NotifyDisplayActionDone(DISPLAY_ON_DONE);
167             break;
168         }
169         case DisplayPowerMgr::DisplayState::DISPLAY_OFF: {
170             if (StateChangeReason(reason) != StateChangeReason::STATE_CHANGE_REASON_SENSOR) {
171                 std::string identity = IPCSkeleton::ResetCallingIdentity();
172                 DisplayManager::GetInstance().SuspendEnd();
173                 IPCSkeleton::SetCallingIdentity(identity);
174             }
175             NotifyDisplayActionDone(DISPLAY_OFF_DONE);
176             break;
177         }
178         default:
179             break;
180     }
181     return;
182 }
183 
NotifyDisplayActionDone(uint32_t event)184 void DeviceStateAction::DisplayPowerCallback::NotifyDisplayActionDone(uint32_t event)
185 {
186     std::lock_guard lock(notifyMutex_);
187     if (notify_ != nullptr) {
188         notify_(event);
189     }
190 }
191 } // namespace PowerMgr
192 } // namespace OHOS
193