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