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 "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 #include "ffrt_utils.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( 45 std::shared_ptr<ShutdownController>& shutdownController, std::shared_ptr<PowerStateMachine>& stateMachine); 46 ~SuspendController(); 47 void Init(); 48 void ExecSuspendMonitorByReason(SuspendDeviceType reason); 49 void RegisterSettingsObserver(); 50 void Execute(); 51 void Cancel(); 52 void StopSleep(); 53 void HandleEvent(int64_t delayTime); 54 void CancelEvent(); 55 void HandleAction(SuspendDeviceType reason, uint32_t action); 56 void RecordPowerKeyDown(); 57 bool GetPowerkeyDownWhenScreenOff(); 58 59 void AddCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority); 60 void RemoveCallback(const sptr<ISyncSleepCallback>& callback); 61 void TriggerSyncSleepCallback(bool isWakeup); 62 GetStateMachine()63 std::shared_ptr<PowerStateMachine> GetStateMachine() const 64 { 65 return stateMachine_; 66 } GetLastReason()67 SuspendDeviceType GetLastReason() const 68 { 69 return sleepReason_; 70 } GetLastAction()71 uint32_t GetLastAction() const 72 { 73 return sleepAction_; 74 } 75 void StartSleepTimer(SuspendDeviceType reason, uint32_t action, uint32_t delay); 76 void Reset(); 77 78 private: 79 void ControlListener(SuspendDeviceType reason, uint32_t action, uint32_t delay); 80 void HandleAutoSleep(SuspendDeviceType reason); 81 void HandleForceSleep(SuspendDeviceType reason); 82 void HandleHibernate(SuspendDeviceType reason); 83 void HandleShutdown(SuspendDeviceType reason); 84 85 void TriggerSyncSleepCallbackInner(std::set<sptr<ISyncSleepCallback>>& callbacks, bool isWakeup); 86 static constexpr int32_t FORCE_SLEEP_DELAY_MS = 8000; 87 void SuspendWhenScreenOff(SuspendDeviceType reason, uint32_t action, uint32_t delay); 88 std::vector<SuspendSource> sourceList_; 89 std::map<SuspendDeviceType, std::shared_ptr<SuspendMonitor>> monitorMap_; 90 std::shared_ptr<ShutdownController> shutdownController_; 91 std::shared_ptr<PowerStateMachine> stateMachine_; 92 uint32_t sleepDuration_ {0}; 93 int64_t sleepTime_ {-1}; 94 SuspendDeviceType sleepReason_ {0}; 95 uint32_t sleepAction_ {0}; 96 uint32_t sleepType_ {0}; 97 bool powerkeyDownWhenScreenOff_ = false; 98 std::mutex mutex_; 99 std::shared_ptr<FFRTQueue> queue_; 100 }; 101 102 class SuspendMonitor { 103 public: 104 const static std::shared_ptr<SuspendMonitor> CreateMonitor(SuspendSource& source); 105 106 virtual ~SuspendMonitor() = default; 107 virtual bool Init() = 0; 108 virtual void Cancel() = 0; HandleEvent()109 virtual void HandleEvent() 110 { 111 // do nothing in base class 112 } GetReason()113 SuspendDeviceType GetReason() const 114 { 115 return reason_; 116 } GetAction()117 uint32_t GetAction() const 118 { 119 return action_; 120 } GetDelay()121 uint32_t GetDelay() const 122 { 123 return delayMs_; 124 } RegisterListener(SuspendListener listener)125 void RegisterListener(SuspendListener listener) 126 { 127 listener_ = listener; 128 } 129 Notify()130 void Notify() 131 { 132 if (listener_ == nullptr) { 133 return; 134 } 135 listener_(reason_, action_, delayMs_); 136 } 137 protected: SuspendMonitor(const SuspendSource & source)138 explicit SuspendMonitor(const SuspendSource& source) 139 { 140 reason_ = source.GetReason(); 141 action_ = source.GetAction(); 142 delayMs_ = source.GetDelay(); 143 } 144 145 SuspendDeviceType reason_; 146 uint32_t action_; 147 uint32_t delayMs_; 148 SuspendListener listener_; 149 }; 150 151 class PowerKeySuspendMonitor : public SuspendMonitor { 152 public: PowerKeySuspendMonitor(SuspendSource & source)153 explicit PowerKeySuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 154 ~PowerKeySuspendMonitor() override = default; 155 bool Init() override; 156 void Cancel() override; 157 158 private: 159 static constexpr int32_t LONG_PRESS_DELAY_MS = 3000; 160 static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; 161 int32_t powerkeyReleaseId_ {-1}; 162 }; 163 164 class TimeoutSuspendMonitor : public SuspendMonitor { 165 public: TimeoutSuspendMonitor(SuspendSource & source)166 explicit TimeoutSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 167 ~TimeoutSuspendMonitor() override = default; 168 bool Init() override; 169 void Cancel() override; 170 void HandleEvent() override; 171 }; 172 173 class LidSuspendMonitor : public SuspendMonitor { 174 public: LidSuspendMonitor(SuspendSource & source)175 explicit LidSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 176 ~LidSuspendMonitor() override = default; 177 bool Init() override; 178 void Cancel() override; 179 }; 180 181 class SwitchSuspendMonitor : public SuspendMonitor { 182 public: SwitchSuspendMonitor(SuspendSource & source)183 explicit SwitchSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {} 184 ~SwitchSuspendMonitor() override = default; 185 bool Init() override; 186 void Cancel() override; 187 }; 188 189 } // namespace PowerMgr 190 } // namespace OHOS 191 192 #endif // POWERMGR_SUSPEND_CONTROLLER_H 193