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