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