• 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 <functional>
20 #include <memory>
21 #include <vector>
22 
23 #include "event_handler.h"
24 #include "power_state_machine.h"
25 #include "sensor_agent.h"
26 #include "shutdown_controller.h"
27 #include "suspend_source_parser.h"
28 #include "suspend_sources.h"
29 
30 namespace OHOS {
31 namespace PowerMgr {
32 
33 using SuspendListener = std::function<void(SuspendDeviceType, uint32_t, uint32_t)>;
34 
35 class SuspendMonitor;
36 class SuspendEventHandler;
37 class SuspendController : public std::enable_shared_from_this<SuspendController> {
38 public:
39     SuspendController(
40         std::shared_ptr<ShutdownController>& shutdownController, std::shared_ptr<PowerStateMachine>& stateMachine);
41     ~SuspendController();
42     void Init();
43     void ExecSuspendMonitorByReason(SuspendDeviceType reason);
44     void RegisterSettingsObserver();
45     void Execute();
46     void Cancel();
47     void StopSleep();
48     void HandleEvent(int64_t delayTime);
49     void CancelEvent();
50     void HandleAction(SuspendDeviceType reason, uint32_t action);
51     void RecordPowerKeyDown();
52     bool GetPowerkeyDownWhenScreenOff();
GetStateMachine()53     std::shared_ptr<PowerStateMachine> GetStateMachine() const
54     {
55         return stateMachine_;
56     }
GetLastReason()57     SuspendDeviceType GetLastReason() const
58     {
59         return sleepReason_;
60     }
GetLastAction()61     uint32_t GetLastAction() const
62     {
63         return sleepAction_;
64     }
65 
66 private:
67     void ControlListener(SuspendDeviceType reason, uint32_t action, uint32_t delay);
68     void StartSleepTimer(SuspendDeviceType reason, uint32_t action, uint32_t delay);
69     void HandleAutoSleep(SuspendDeviceType reason);
70     void HandleForceSleep(SuspendDeviceType reason);
71     void HandleHibernate(SuspendDeviceType reason);
72     void HandleShutdown(SuspendDeviceType reason);
73     std::vector<SuspendSource> sourceList_;
74     std::map<SuspendDeviceType, std::shared_ptr<SuspendMonitor>> monitorMap_;
75     std::shared_ptr<ShutdownController> shutdownController_;
76     std::shared_ptr<PowerStateMachine> stateMachine_;
77     uint32_t sleepDuration_ {0};
78     int64_t sleepTime_ {-1};
79     SuspendDeviceType sleepReason_ {0};
80     uint32_t sleepAction_ {0};
81     bool powerkeyDownWhenScreenOff_ = false;
82 };
83 
84 class SuspendMonitor {
85 public:
86     const static std::shared_ptr<SuspendMonitor> CreateMonitor(SuspendSource& source);
87 
88     virtual ~SuspendMonitor() = default;
89     virtual bool Init() = 0;
90     virtual void Cancel() = 0;
HandleEvent()91     virtual void HandleEvent()
92     {
93         // do nothing in base class
94     }
GetReason()95     SuspendDeviceType GetReason() const
96     {
97         return reason_;
98     }
GetAction()99     uint32_t GetAction() const
100     {
101         return action_;
102     }
GetDelay()103     uint32_t GetDelay() const
104     {
105         return delayMs_;
106     }
RegisterListener(SuspendListener listener)107     void RegisterListener(SuspendListener listener)
108     {
109         listener_ = listener;
110     }
111 
Notify()112     void Notify()
113     {
114         if (listener_ == nullptr) {
115             return;
116         }
117         listener_(reason_, action_, delayMs_);
118     }
119 protected:
SuspendMonitor(const SuspendSource & source)120     explicit SuspendMonitor(const SuspendSource& source)
121     {
122         reason_ = source.GetReason();
123         action_ = source.GetAction();
124         delayMs_ = source.GetDelay();
125     }
126 
127     SuspendDeviceType reason_;
128     uint32_t action_;
129     uint32_t delayMs_;
130     SuspendListener listener_;
131 };
132 
133 class PowerKeySuspendMonitor : public SuspendMonitor {
134 public:
PowerKeySuspendMonitor(SuspendSource & source)135     explicit PowerKeySuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
136     ~PowerKeySuspendMonitor() override = default;
137     bool Init() override;
138     void Cancel() override;
139 
140 private:
141     static constexpr int32_t LONG_PRESS_DELAY_MS = 3000;
142     static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000;
143     int32_t powerkeyReleaseId_ {-1};
144 };
145 
146 class TimeoutSuspendMonitor : public SuspendMonitor {
147 public:
TimeoutSuspendMonitor(SuspendSource & source)148     explicit TimeoutSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
149     ~TimeoutSuspendMonitor() override = default;
150     bool Init() override;
151     void Cancel() override;
152     void HandleEvent() override;
153 };
154 
155 class LidSuspendMonitor : public SuspendMonitor {
156 public:
LidSuspendMonitor(SuspendSource & source)157     explicit LidSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
158     ~LidSuspendMonitor() override = default;
159     bool Init() override;
160     void Cancel() override;
161 };
162 
163 class SwitchSuspendMonitor : public SuspendMonitor {
164 public:
SwitchSuspendMonitor(SuspendSource & source)165     explicit SwitchSuspendMonitor(SuspendSource& source) : SuspendMonitor(source) {}
166     ~SwitchSuspendMonitor() override = default;
167     bool Init() override;
168     void Cancel() override;
169 };
170 
171 } // namespace PowerMgr
172 } // namespace OHOS
173 
174 #endif // POWERMGR_SUSPEND_CONTROLLER_H