1 /* 2 * Copyright (c) 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 #ifndef POWERMGR_SUSPEND_CONTROLLER_H 17 #define POWERMGR_SUSPEND_CONTROLLER_H 18 19 #include <cinttypes> 20 #include <functional> 21 #include <memory> 22 #include <vector> 23 24 #include "ffrt_utils.h" 25 #include "power_state_machine.h" 26 #ifdef HAS_SENSORS_SENSOR_PART 27 #include "sensor_agent.h" 28 #endif 29 #include "shutdown_controller.h" 30 #include "suspend_source_parser.h" 31 #include "suspend_sources.h" 32 #include "sleep_callback_holder.h" 33 #ifdef HAS_MULTIMODALINPUT_INPUT_PART 34 #include "i_input_event_consumer.h" 35 #endif 36 #ifdef POWER_MANAGER_TAKEOVER_SUSPEND 37 #include "suspend_takeover_callback_holder.h" 38 #endif 39 40 namespace OHOS { 41 namespace PowerMgr { 42 43 using SuspendListener = std::function<void(SuspendDeviceType, uint32_t, uint32_t)>; 44 class SuspendMonitor; 45 class SuspendEventHandler; 46 class SuspendController : public std::enable_shared_from_this<SuspendController> { 47 public: 48 SuspendController(const std::shared_ptr<ShutdownController>& shutdownController, 49 const std::shared_ptr<PowerStateMachine>& stateMachine, const std::shared_ptr<FFRTTimer>& ffrtTimer_); 50 ~SuspendController(); 51 void Init(); 52 void ExecSuspendMonitorByReason(SuspendDeviceType reason); 53 void RegisterSettingsObserver(); 54 void UnregisterSettingsObserver(); 55 void Execute(); 56 void Cancel(); 57 void StopSleep(); 58 void HandleEvent(int64_t delayTime); 59 void CancelEvent(); 60 void HandleAction(SuspendDeviceType reason, uint32_t action); 61 void RecordPowerKeyDown(bool interrupting = false); 62 bool GetPowerkeyDownWhenScreenOff(); 63 64 void AddCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority); 65 void AddCallback(const sptr<ITakeOverSuspendCallback>& callback, TakeOverSuspendPriority priority); 66 void RemoveCallback(const sptr<ISyncSleepCallback>& callback); 67 void RemoveCallback(const sptr<ITakeOverSuspendCallback>& callback); 68 void TriggerSyncSleepCallback(bool isWakeup); 69 bool TriggerTakeOverSuspendCallback(SuspendDeviceType type); 70 void UpdateSuspendSources(); 71 GetStateMachine()72 std::shared_ptr<PowerStateMachine> GetStateMachine() const 73 { 74 return stateMachine_; 75 } GetLastReason()76 SuspendDeviceType GetLastReason() const 77 { 78 return sleepReason_; 79 } GetLastAction()80 uint32_t GetLastAction() const 81 { 82 return sleepAction_; 83 } 84 std::shared_ptr<SuspendMonitor> GetSpecifiedSuspendMonitor(SuspendDeviceType type) const; 85 #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT 86 void PowerOffInternalScreen(SuspendDeviceType type); 87 void PowerOffAllScreens(SuspendDeviceType type); 88 #endif 89 void StartSleepTimer(SuspendDeviceType reason, uint32_t action, uint32_t delay); 90 void Reset(); 91 92 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST SetForceSleepingFlag(bool isForceSleeping)93 void SetForceSleepingFlag(bool isForceSleeping) 94 { 95 forceSleeping_.store(isForceSleeping, std::memory_order_relaxed); 96 } GetForceSleepingFlag()97 bool GetForceSleepingFlag() 98 { 99 return forceSleeping_.load(); 100 } 101 #endif 102 #ifdef POWER_MANAGER_WAKEUP_ACTION 103 bool GetLowCapacityPowerKeyFlag(); 104 void SetLowCapacityPowerKeyFlag(bool flag); 105 #endif 106 107 private: 108 void ControlListener(SuspendDeviceType reason, uint32_t action, uint32_t delay); 109 void HandleAutoSleep(SuspendDeviceType reason); 110 void HandleForceSleep(SuspendDeviceType reason); 111 void HandleHibernate(SuspendDeviceType reason); 112 void HandleShutdown(SuspendDeviceType reason); 113 #ifdef POWER_MANAGER_ENABLE_EXTERNAL_SCREEN_MANAGEMENT 114 bool IsPowerOffInernalScreenOnlyScene(SuspendDeviceType reason, SuspendAction action, bool isScreenOn) const; 115 void ProcessPowerOffInternalScreenOnly(const sptr<PowerMgrService>& pms, SuspendDeviceType reason); 116 #endif 117 118 void TriggerSyncSleepCallbackInner( 119 SleepCallbackHolder::SleepCallbackContainerType& callbacks, const std::string& priority, bool isWakeup); 120 #ifdef POWER_MANAGER_TAKEOVER_SUSPEND 121 bool TriggerTakeOverSuspendCallbackInner( 122 TakeOverSuspendCallbackHolder::TakeoverSuspendCallbackContainerType& callbacks, 123 const std::string& priority, SuspendDeviceType type); 124 #endif 125 static constexpr int32_t FORCE_SLEEP_DELAY_MS = 8000; 126 void SuspendWhenScreenOff(SuspendDeviceType reason, uint32_t action, uint32_t delay); 127 bool NeedToSkipCurrentSuspend(SuspendDeviceType reason, uint32_t action, uint32_t delay); 128 std::vector<SuspendSource> sourceList_; 129 std::map<SuspendDeviceType, std::shared_ptr<SuspendMonitor>> monitorMap_; 130 std::shared_ptr<ShutdownController> shutdownController_; 131 std::shared_ptr<PowerStateMachine> stateMachine_; 132 uint32_t sleepDuration_ {0}; 133 int64_t sleepTime_ {-1}; 134 SuspendDeviceType sleepReason_ {0}; 135 uint32_t sleepAction_ {0}; 136 uint32_t sleepType_ {0}; 137 bool powerkeyDownWhenScreenOff_ = false; 138 std::mutex mutex_; 139 std::mutex sleepCbMutex_; 140 std::mutex suspendMutex_; 141 std::shared_ptr<FFRTTimer> ffrtTimer_; 142 FFRTMutexMap ffrtMutexMap_; 143 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST 144 std::atomic<bool> forceSleeping_ {false}; 145 #endif 146 sptr<IPowerStateCallback> suspendPowerStateCallback_ {nullptr}; 147 #ifdef POWER_MANAGER_WAKEUP_ACTION 148 std::atomic<bool> isLowCapacityPowerKey_ {false}; 149 #endif 150 }; 151 152 class SuspendMonitor { 153 public: 154 const static std::shared_ptr<SuspendMonitor> CreateMonitor(SuspendSource& source); 155 156 virtual ~SuspendMonitor() = default; 157 virtual bool Init() = 0; 158 virtual void Cancel() = 0; HandleEvent()159 virtual void HandleEvent() 160 { 161 // do nothing in base class 162 } GetReason()163 SuspendDeviceType GetReason() const 164 { 165 return reason_; 166 } GetAction()167 uint32_t GetAction() const 168 { 169 return action_; 170 } GetDelay()171 uint32_t GetDelay() const 172 { 173 return delayMs_; 174 } RegisterListener(SuspendListener listener)175 void RegisterListener(SuspendListener listener) 176 { 177 listener_ = listener; 178 } 179 Notify()180 void Notify() 181 { 182 if (listener_ == nullptr) { 183 return; 184 } 185 listener_(reason_, action_, delayMs_); 186 } 187 protected: SuspendMonitor(const SuspendSource & source)188 explicit SuspendMonitor(const SuspendSource& source) 189 { 190 reason_ = source.GetReason(); 191 action_ = source.GetAction(); 192 delayMs_ = source.GetDelay(); 193 } 194 195 SuspendDeviceType reason_; 196 uint32_t action_; 197 uint32_t delayMs_; 198 SuspendListener listener_; 199 }; 200 201 class PowerKeySuspendMonitor : public SuspendMonitor, public std::enable_shared_from_this<PowerKeySuspendMonitor> { 202 public: PowerKeySuspendMonitor(SuspendSource & source)203 explicit PowerKeySuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 204 ~PowerKeySuspendMonitor() override = default; 205 bool Init() override; 206 void Cancel() override; 207 static inline std::atomic<bool> powerkeyScreenOff_ {false}; 208 private: 209 void BeginPowerkeyScreenOff() const; 210 void EndPowerkeyScreenOff() const; 211 void ReceivePowerkeyCallback(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent); 212 static constexpr int32_t LONG_PRESS_DELAY_MS = 3000; 213 static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; 214 int32_t powerkeyReleaseId_ {-1}; 215 }; 216 217 class TimeoutSuspendMonitor : public SuspendMonitor { 218 public: TimeoutSuspendMonitor(SuspendSource & source)219 explicit TimeoutSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 220 ~TimeoutSuspendMonitor() override = default; 221 bool Init() override; 222 void Cancel() override; 223 void HandleEvent() override; 224 }; 225 226 class LidSuspendMonitor : public SuspendMonitor { 227 public: LidSuspendMonitor(SuspendSource & source)228 explicit LidSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 229 ~LidSuspendMonitor() override = default; 230 bool Init() override; 231 void Cancel() override; 232 }; 233 234 class SwitchSuspendMonitor : public SuspendMonitor { 235 public: SwitchSuspendMonitor(SuspendSource & source)236 explicit SwitchSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 237 ~SwitchSuspendMonitor() override = default; 238 bool Init() override; 239 void Cancel() override; 240 }; 241 242 class TPCoverSuspendMonitor : public SuspendMonitor, public std::enable_shared_from_this<TPCoverSuspendMonitor> { 243 public: TPCoverSuspendMonitor(SuspendSource & source)244 explicit TPCoverSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 245 ~TPCoverSuspendMonitor() override = default; 246 bool Init() override; 247 void Cancel() override; 248 int32_t TPCoverReleaseId_ {-1}; 249 }; 250 251 } // namespace PowerMgr 252 } // namespace OHOS 253 254 #endif // POWERMGR_SUSPEND_CONTROLLER_H 255