• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_POWER_STATE_MACHINE_H
17 #define POWERMGR_POWER_STATE_MACHINE_H
18 
19 #include <set>
20 #include <map>
21 #include <singleton.h>
22 
23 #include "actions/idevice_state_action.h"
24 #include "ffrt_utils.h"
25 #include "ipower_state_callback.h"
26 #include "power_common.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     enum {
72         CHECK_USER_ACTIVITY_TIMEOUT_MSG = 0,
73         CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG,
74     };
75 
76     static void onSuspend();
77     static void onWakeup();
78 
79     bool Init();
80     void InitState();
81     void SuspendDeviceInner(
82         pid_t pid, int64_t callTimeMs, SuspendDeviceType type, bool suspendImmed, bool ignoreScreenState = false);
83     void WakeupDeviceInner(
84         pid_t pid, int64_t callTimeMs, WakeupDeviceType type, const std::string& details, const std::string& pkgName);
85     void RefreshActivityInner(pid_t pid, int64_t callTimeMs, UserActivityType type, bool needChangeBacklight);
86     bool CheckRefreshTime();
87     bool OverrideScreenOffTimeInner(int64_t timeout);
88     bool RestoreScreenOffTimeInner();
89     void OverrideScreenOffTimeCoordinated();
90     void RestoreScreenOffTimeCoordinated();
91     void ReceiveScreenEvent(bool isScreenOn);
92     bool IsScreenOn();
93     void Reset();
94 
GetState()95     PowerState GetState()
96     {
97         return currentState_;
98     };
GetStateAction()99     const std::shared_ptr<IDeviceStateAction>& GetStateAction()
100     {
101         return stateAction_;
102     };
103     bool ForceSuspendDeviceInner(pid_t pid, int64_t callTimeMs);
104     void RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
105     void UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
106     void SetDelayTimer(int64_t delayTime, int32_t event);
107     void CancelDelayTimer(int32_t event);
108     void ResetInactiveTimer();
109     void ResetSleepTimer();
110     void SetAutoSuspend(SuspendDeviceType type, uint32_t delay);
111     bool SetState(PowerState state, StateChangeReason reason, bool force = false);
112     void SetDisplaySuspend(bool enable);
113     StateChangeReason GetReasonByUserActivity(UserActivityType type);
114     StateChangeReason GetReasonByWakeType(WakeupDeviceType type);
115     StateChangeReason GetReasionBySuspendType(SuspendDeviceType type);
116 
117     // only use for test
GetLastSuspendDeviceTime()118     int64_t GetLastSuspendDeviceTime() const
119     {
120         return mDeviceState_.lastSuspendDeviceTime;
121     }
GetLastWakeupDeviceTime()122     int64_t GetLastWakeupDeviceTime() const
123     {
124         return mDeviceState_.lastWakeupDeviceTime;
125     }
GetLastRefreshActivityTime()126     int64_t GetLastRefreshActivityTime() const
127     {
128         return mDeviceState_.lastRefreshActivityTime;
129     }
GetLastWakeupEventTime()130     int64_t GetLastWakeupEventTime() const
131     {
132         return mDeviceState_.lastWakeupEventTime;
133     }
134     class PowerStateCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
135     public:
136         PowerStateCallbackDeathRecipient() = default;
137         virtual void OnRemoteDied(const wptr<IRemoteObject>& remote);
138         virtual ~PowerStateCallbackDeathRecipient() = default;
139     };
140     void DumpInfo(std::string& result);
141     void EnableMock(IDeviceStateAction* mockAction);
142     int64_t GetDisplayOffTime();
143     void SetDisplayOffTime(int64_t time, bool needUpdateSetting = true);
144     static void RegisterDisplayOffTimeObserver();
145     static void UnregisterDisplayOffTimeObserver();
146     void SetSleepTime(int64_t time);
147 
148 private:
149     class StateController {
150     public:
StateController(PowerState state,std::shared_ptr<PowerStateMachine> owner,std::function<TransitResult (StateChangeReason)> action)151         StateController(PowerState state, std::shared_ptr<PowerStateMachine> owner,
152             std::function<TransitResult(StateChangeReason)> action)
153             : state_(state),
154             owner_(owner), action_(action)
155         {
156         }
157         ~StateController() = default;
GetState()158         PowerState GetState()
159         {
160             return state_;
161         }
162         TransitResult TransitTo(StateChangeReason reason, bool ignoreLock = false);
163         void RecordFailure(PowerState from, StateChangeReason trigger, TransitResult failReason);
164         StateChangeReason lastReason_;
165         int64_t lastTime_ {0};
166         PowerState failFrom_;
167         StateChangeReason failTrigger_;
168         std::string failReasion_;
169         int64_t failTime_ {0};
170 
171     protected:
172         bool CheckState();
173         void MatchState(PowerState& currentState, DisplayState state);
174         void CorrectState(PowerState& currentState, PowerState correctState, DisplayState state);
175         PowerState state_;
176         std::weak_ptr<PowerStateMachine> owner_;
177         std::function<TransitResult(StateChangeReason)> action_;
178     };
179 
180     struct classcomp {
operatorclasscomp181         bool operator()(const sptr<IPowerStateCallback>& l, const sptr<IPowerStateCallback>& r) const
182         {
183             return l->AsObject() < r->AsObject();
184         }
185     };
186     void InitStateMap();
187     void EmplaceAwake();
188     void EmplaceFreeze();
189     void EmplaceInactive();
190     void EmplaceStandBy();
191     void EmplaceDoze();
192     void EmplaceSleep();
193     void EmplaceHibernate();
194     void EmplaceShutdown();
195     void NotifyPowerStateChanged(PowerState state);
196     void SendEventToPowerMgrNotify(PowerState state, int64_t callTime);
197     bool CheckRunningLock(PowerState state);
198     bool IsRunningLockEnabled(RunningLockType type);
199     int64_t GetSleepTime();
200     void HandleActivityTimeout();
201     void HandleActivitySleepTimeout();
202     void HandleSystemWakeup();
203     void AppendDumpInfo(std::string& result, std::string& reason, std::string& time);
204 
205     const wptr<PowerMgrService> pms_;
206     PowerState currentState_;
207     std::map<PowerState, std::shared_ptr<std::vector<RunningLockType>>> lockMap_;
208     std::map<PowerState, std::shared_ptr<StateController>> controllerMap_;
209     std::mutex mutex_;
210     std::mutex ffrtMutex_;
211     DevicePowerState mDeviceState_;
212     sptr<IRemoteObject::DeathRecipient> powerStateCBDeathRecipient_;
213     std::set<const sptr<IPowerStateCallback>, classcomp> powerStateListeners_;
214     std::shared_ptr<IDeviceStateAction> stateAction_;
215 
216 private:
217     std::atomic<int64_t> displayOffTime_ {DEFAULT_DISPLAY_OFF_TIME};
218     int64_t sleepTime_ {DEFAULT_SLEEP_TIME};
219     bool enableDisplaySuspend_ {false};
220     bool isScreenOffTimeOverride_ {false};
221     int64_t beforeOverrideTime_ {-1};
222     std::shared_ptr<FFRTQueue> queue_;
223     FFRTHandle userActivityTimeoutHandle_ {nullptr};
224     bool isCoordinatedOverride_ {false};
225 };
226 } // namespace PowerMgr
227 } // namespace OHOS
228 #endif // POWERMGR_POWER_STATE_MACHINE_H
229