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