1 /* 2 * Copyright (c) 2021-2022 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_POWER_STATE_MACHINE_H 17 #define POWERMGR_POWER_STATE_MACHINE_H 18 19 #include <set> 20 21 #include <singleton.h> 22 23 #include "actions/idevice_state_action.h" 24 #include "ipower_state_callback.h" 25 #include "power_common.h" 26 #include "power_mgr_monitor.h" 27 #include "power_state_machine_info.h" 28 #include "running_lock_info.h" 29 30 #define DEFAULT_DISPLAY_OFF_TIME 30000 31 #define DEFAULT_SLEEP_TIME 5000 32 33 namespace OHOS { 34 namespace PowerMgr { 35 class RunningLockMgr; 36 class PowerMgrService; 37 38 struct ScreenState { 39 DisplayState state; 40 int64_t lastOnTime; 41 int64_t lastOffTime; 42 }; 43 44 struct DevicePowerState { 45 ScreenState screenState; 46 // record the last time when get wakeup event from A side 47 int64_t lastWakeupEventTime; 48 // record the last time when calling func RefreshActivityInner 49 int64_t lastRefreshActivityTime; 50 // record the last time when calling func WakeupDeviceInner 51 int64_t lastWakeupDeviceTime; 52 // record the last time when calling func SuspendDeviceInner 53 int64_t lastSuspendDeviceTime; 54 }; 55 56 enum class TransitResult { 57 SUCCESS = 0, 58 ALREADY_IN_STATE = 1, 59 LOCKING = 2, 60 HDI_ERR = 3, 61 DISPLAY_ON_ERR = 4, 62 DISPLAY_OFF_ERR = 5, 63 OTHER_ERR = 99 64 }; 65 66 class PowerStateMachine : public std::enable_shared_from_this<PowerStateMachine> { 67 public: 68 explicit PowerStateMachine(const wptr<PowerMgrService>& pms); 69 ~PowerStateMachine(); 70 71 static void onSuspend(); 72 static void onWakeup(); 73 74 bool Init(); 75 void InitState(); 76 void SuspendDeviceInner(pid_t pid, int64_t callTimeMs, SuspendDeviceType type, bool suspendImmed, 77 bool ignoreScreenState = false); 78 void WakeupDeviceInner(pid_t pid, int64_t callTimeMs, WakeupDeviceType type, const std::string& details, 79 const std::string& pkgName); 80 void RefreshActivityInner(pid_t pid, int64_t callTimeMs, UserActivityType type, bool needChangeBacklight); 81 bool CheckRefreshTime(); 82 bool OverrideScreenOffTimeInner(int64_t timeout); 83 bool RestoreScreenOffTimeInner(); 84 void ReceiveScreenEvent(bool isScreenOn); 85 bool IsScreenOn(); GetState()86 PowerState GetState() 87 { 88 return currentState_; 89 }; 90 bool ForceSuspendDeviceInner(pid_t pid, int64_t callTimeMs); 91 void RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback); 92 void UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback); 93 void SetDelayTimer(int64_t delayTime, int32_t event); 94 void CancelDelayTimer(int32_t event); 95 void ResetInactiveTimer(); 96 void ResetSleepTimer(); 97 void HandleDelayTimer(int32_t event); 98 bool SetState(PowerState state, StateChangeReason reason, bool force = false); 99 void SetDisplaySuspend(bool enable); 100 void ActionCallback(uint32_t event); 101 102 // only use for test GetLastSuspendDeviceTime()103 int64_t GetLastSuspendDeviceTime() const 104 { 105 return mDeviceState_.lastSuspendDeviceTime; 106 } GetLastWakeupDeviceTime()107 int64_t GetLastWakeupDeviceTime() const 108 { 109 return mDeviceState_.lastWakeupDeviceTime; 110 } GetLastRefreshActivityTime()111 int64_t GetLastRefreshActivityTime() const 112 { 113 return mDeviceState_.lastRefreshActivityTime; 114 } GetLastWakeupEventTime()115 int64_t GetLastWakeupEventTime() const 116 { 117 return mDeviceState_.lastWakeupEventTime; 118 } 119 class PowerStateCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 120 public: 121 PowerStateCallbackDeathRecipient() = default; 122 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 123 virtual ~PowerStateCallbackDeathRecipient() = default; 124 }; 125 void DumpInfo(std::string& result); 126 void EnableMock(IDeviceStateAction* mockAction); 127 int64_t GetDisplayOffTime(); 128 void SetDisplayOffTime(int64_t time, bool needUpdateSetting = true); 129 static void RegisterDisplayOffTimeObserver(); 130 static void UnregisterDisplayOffTimeObserver(); 131 void SetSleepTime(int64_t time); 132 private: 133 class StateController { 134 public: StateController(PowerState state,std::shared_ptr<PowerStateMachine> owner,std::function<TransitResult (StateChangeReason)> action)135 StateController(PowerState state, std::shared_ptr<PowerStateMachine> owner, 136 std::function<TransitResult(StateChangeReason)> action) 137 : state_(state), owner_(owner), action_(action) {} 138 ~StateController() = default; GetState()139 PowerState GetState() 140 { 141 return state_; 142 } 143 TransitResult TransitTo(StateChangeReason reason, bool ignoreLock = false); 144 void RecordFailure(PowerState from, StateChangeReason trigger, TransitResult failReason); 145 StateChangeReason lastReason_; 146 int64_t lastTime_ {0}; 147 PowerState failFrom_; 148 StateChangeReason failTrigger_; 149 std::string failReasion_; 150 int64_t failTime_ {0}; 151 protected: 152 bool CheckState(); 153 PowerState state_; 154 std::weak_ptr<PowerStateMachine> owner_; 155 std::function<TransitResult(StateChangeReason)> action_; 156 }; 157 158 struct classcomp { operatorclasscomp159 bool operator() (const sptr<IPowerStateCallback>& l, const sptr<IPowerStateCallback>& r) const 160 { 161 return l->AsObject() < r->AsObject(); 162 } 163 }; 164 void InitStateMap(); 165 void EmplaceAwake(); 166 void EmplaceInactive(); 167 void EmplaceSleep(); 168 void NotifyPowerStateChanged(PowerState state); 169 void SendEventToPowerMgrNotify(PowerState state, int64_t callTime); 170 bool CheckRunningLock(PowerState state); 171 int64_t GetSleepTime(); 172 void HandleActivityTimeout(); 173 void HandleActivityOffTimeout(); 174 void HandleActivitySleepTimeout(); 175 void HandleSystemWakeup(); 176 StateChangeReason GetReasonByUserActivity(UserActivityType type); 177 StateChangeReason GetReasonByWakeType(WakeupDeviceType type); 178 StateChangeReason GetReasionBySuspendType(SuspendDeviceType type); 179 180 const wptr<PowerMgrService> pms_; 181 PowerMgrMonitor powerMgrMonitor_; 182 PowerState currentState_; 183 std::map<PowerState, std::shared_ptr<std::vector<RunningLockType>>> lockMap_; 184 std::map<PowerState, std::shared_ptr<StateController>> controllerMap_; 185 std::mutex mutex_; 186 DevicePowerState mDeviceState_; 187 sptr<IRemoteObject::DeathRecipient> powerStateCBDeathRecipient_; 188 std::set<const sptr<IPowerStateCallback>, classcomp> powerStateListeners_; 189 std::unique_ptr<IDeviceStateAction> stateAction_; 190 std::atomic<int64_t> displayOffTime_ {DEFAULT_DISPLAY_OFF_TIME}; 191 int64_t sleepTime_ {DEFAULT_SLEEP_TIME}; 192 bool enableDisplaySuspend_ {false}; 193 bool isScreenOffTimeOverride_ { false }; 194 int64_t beforeOverrideTime_ { -1 }; 195 }; 196 } // namespace PowerMgr 197 } // namespace OHOS 198 #endif // POWERMGR_POWER_STATE_MACHINE_H 199