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 "ffrt_utils.h"
26 #include "power_state_machine.h"
27 #ifdef HAS_SENSORS_SENSOR_PART
28 #include "sensor_agent.h"
29 #endif
30 #include "shutdown_controller.h"
31 #include "suspend_source_parser.h"
32 #include "suspend_sources.h"
33 #include "sleep_callback_holder.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 UnregisterSettingsObserver();
51     void Execute();
52     void Cancel();
53     void StopSleep();
54     void HandleEvent(int64_t delayTime);
55     void CancelEvent();
56     void HandleAction(SuspendDeviceType reason, uint32_t action);
57     void RecordPowerKeyDown(bool interrupting = false);
58     bool GetPowerkeyDownWhenScreenOff();
59 
60     void AddCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority);
61     void RemoveCallback(const sptr<ISyncSleepCallback>& callback);
62     void TriggerSyncSleepCallback(bool isWakeup);
63     void UpdateSuspendSources();
64 
GetStateMachine()65     std::shared_ptr<PowerStateMachine> GetStateMachine() const
66     {
67         return stateMachine_;
68     }
GetLastReason()69     SuspendDeviceType GetLastReason() const
70     {
71         return sleepReason_;
72     }
GetLastAction()73     uint32_t GetLastAction() const
74     {
75         return sleepAction_;
76     }
77     void StartSleepTimer(SuspendDeviceType reason, uint32_t action, uint32_t delay);
78     void Reset();
79 
80 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST
SetForceSleepingFlag(bool isForceSleeping)81     void SetForceSleepingFlag(bool isForceSleeping)
82     {
83         forceSleeping_.store(isForceSleeping, std::memory_order_relaxed);
84     }
GetForceSleepingFlag()85     bool GetForceSleepingFlag()
86     {
87         return forceSleeping_.load();
88     }
89 #endif
90 
91 private:
92     void ControlListener(SuspendDeviceType reason, uint32_t action, uint32_t delay);
93     void HandleAutoSleep(SuspendDeviceType reason);
94     void HandleForceSleep(SuspendDeviceType reason);
95     void HandleHibernate(SuspendDeviceType reason);
96     void HandleShutdown(SuspendDeviceType reason);
97 
98     void TriggerSyncSleepCallbackInner(
99         SleepCallbackHolder::SleepCallbackContainerType& callbacks, const std::string& priority, bool isWakeup);
100     static constexpr int32_t FORCE_SLEEP_DELAY_MS = 8000;
101     void SuspendWhenScreenOff(SuspendDeviceType reason, uint32_t action, uint32_t delay);
102     std::vector<SuspendSource> sourceList_;
103     std::map<SuspendDeviceType, std::shared_ptr<SuspendMonitor>> monitorMap_;
104     std::shared_ptr<ShutdownController> shutdownController_;
105     std::shared_ptr<PowerStateMachine> stateMachine_;
106     uint32_t sleepDuration_ {0};
107     int64_t sleepTime_ {-1};
108     SuspendDeviceType sleepReason_ {0};
109     uint32_t sleepAction_ {0};
110     uint32_t sleepType_ {0};
111     bool powerkeyDownWhenScreenOff_ = false;
112     std::mutex mutex_;
113     std::shared_ptr<FFRTTimer> ffrtTimer_;
114     FFRTMutexMap ffrtMutexMap_;
115 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST
116     std::atomic<bool> forceSleeping_ {false};
117 #endif
118 };
119 
120 class SuspendMonitor {
121 public:
122     const static std::shared_ptr<SuspendMonitor> CreateMonitor(SuspendSource& source);
123 
124     virtual ~SuspendMonitor() = default;
125     virtual bool Init() = 0;
126     virtual void Cancel() = 0;
HandleEvent()127     virtual void HandleEvent()
128     {
129         // do nothing in base class
130     }
GetReason()131     SuspendDeviceType GetReason() const
132     {
133         return reason_;
134     }
GetAction()135     uint32_t GetAction() const
136     {
137         return action_;
138     }
GetDelay()139     uint32_t GetDelay() const
140     {
141         return delayMs_;
142     }
RegisterListener(SuspendListener listener)143     void RegisterListener(SuspendListener listener)
144     {
145         listener_ = listener;
146     }
147 
Notify()148     void Notify()
149     {
150         if (listener_ == nullptr) {
151             return;
152         }
153         listener_(reason_, action_, delayMs_);
154     }
155 protected:
SuspendMonitor(const SuspendSource & source)156     explicit SuspendMonitor(const SuspendSource& source)
157     {
158         reason_ = source.GetReason();
159         action_ = source.GetAction();
160         delayMs_ = source.GetDelay();
161     }
162 
163     SuspendDeviceType reason_;
164     uint32_t action_;
165     uint32_t delayMs_;
166     SuspendListener listener_;
167 };
168 
169 class PowerKeySuspendMonitor : public SuspendMonitor {
170 public:
PowerKeySuspendMonitor(SuspendSource & source)171     explicit PowerKeySuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
172     ~PowerKeySuspendMonitor() override = default;
173     bool Init() override;
174     void Cancel() override;
175     static inline std::atomic<bool> powerkeyScreenOff_ {false};
176 private:
177     void BeginPowerkeyScreenOff() const;
178     void EndPowerkeyScreenOff() const;
179     static constexpr int32_t LONG_PRESS_DELAY_MS = 3000;
180     static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000;
181     int32_t powerkeyReleaseId_ {-1};
182 };
183 
184 class TimeoutSuspendMonitor : public SuspendMonitor {
185 public:
TimeoutSuspendMonitor(SuspendSource & source)186     explicit TimeoutSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
187     ~TimeoutSuspendMonitor() override = default;
188     bool Init() override;
189     void Cancel() override;
190     void HandleEvent() override;
191 };
192 
193 class LidSuspendMonitor : public SuspendMonitor {
194 public:
LidSuspendMonitor(SuspendSource & source)195     explicit LidSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
196     ~LidSuspendMonitor() override = default;
197     bool Init() override;
198     void Cancel() override;
199 };
200 
201 class SwitchSuspendMonitor : public SuspendMonitor {
202 public:
SwitchSuspendMonitor(SuspendSource & source)203     explicit SwitchSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
204     ~SwitchSuspendMonitor() override = default;
205     bool Init() override;
206     void Cancel() override;
207 };
208 
209 } // namespace PowerMgr
210 } // namespace OHOS
211 
212 #endif // POWERMGR_SUSPEND_CONTROLLER_H
213