• 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_MGR_SERVICE_H
17 #define POWERMGR_POWER_MGR_SERVICE_H
18 
19 #include <iremote_object.h>
20 #include <system_ability.h>
21 
22 #include "actions/idevice_power_action.h"
23 #include "ipower_mgr.h"
24 #include "power_mgr_notify.h"
25 #include "power_mgr_stub.h"
26 #include "power_mode_module.h"
27 #include "power_save_mode.h"
28 #include "power_state_machine.h"
29 #include "running_lock_mgr.h"
30 #include "shutdown_controller.h"
31 #include "shutdown_dialog.h"
32 #include "sp_singleton.h"
33 #include "suspend_controller.h"
34 #include "wakeup_controller.h"
35 
36 #ifdef POWER_MANAGER_WAKEUP_ACTION
37 #include "wakeup_action_controller.h"
38 #endif
39 
40 namespace OHOS {
41 namespace PowerMgr {
42 class RunningLockMgr;
43 class PowerMgrService final : public SystemAbility, public PowerMgrStub {
44     DECLARE_SYSTEM_ABILITY(PowerMgrService)
45     DECLARE_DELAYED_SP_SINGLETON(PowerMgrService);
46 
47 public:
48     virtual void OnStart() override;
49     virtual void OnStop() override;
50     virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
51     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
52     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
53     virtual PowerErrors RebootDevice(const std::string& reason) override;
54     virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override;
55     virtual PowerErrors ShutDownDevice(const std::string& reason) override;
56     virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override;
57     virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override;
58     virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override;
59     virtual bool OverrideScreenOffTime(int64_t timeout) override;
60     virtual bool RestoreScreenOffTime() override;
61     virtual PowerState GetState() override;
62     virtual bool IsScreenOn() override;
63     virtual bool ForceSuspendDevice(int64_t callTimeMs) override;
64     virtual PowerErrors CreateRunningLock(
65         const sptr<IRemoteObject>& remoteObj, const RunningLockInfo& runningLockInfo) override;
66     virtual bool ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj) override;
67     virtual bool IsRunningLockTypeSupported(RunningLockType type) override;
68     virtual bool Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMS) override;
69     virtual bool UnLock(const sptr<IRemoteObject>& remoteObj) override;
70     virtual bool QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists) override;
71     virtual void ForceUnLock(const sptr<IRemoteObject>& remoteObj);
72     virtual bool IsUsed(const sptr<IRemoteObject>& remoteObj) override;
73     virtual bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid) override;
74     virtual bool ProxyRunningLocks(bool isProxied,
75         const std::vector<std::pair<pid_t, pid_t>>& processInfos) override;
76     virtual bool ResetRunningLocks() override;
77     virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
78     virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override;
79 
80     virtual bool RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority) override;
81     virtual bool UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback) override;
82 
83     virtual bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;
84     virtual bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override;
85     virtual bool SetDisplaySuspend(bool enable) override;
86     virtual PowerErrors SetDeviceMode(const PowerMode& mode) override;
87     virtual PowerMode GetDeviceMode() override;
88     virtual std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override;
89     virtual PowerErrors IsStandby(bool& isStandby) override;
90 
91     void RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority) override;
92     void UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback) override;
93 
94     void RegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority) override;
95     void UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback) override;
96     void RegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority) override;
97     void UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback) override;
98 
99     void HandleKeyEvent(int32_t keyCode);
100     void HandlePointEvent(int32_t type);
101     void KeyMonitorCancel();
102     void SwitchSubscriberInit();
103     void SwitchSubscriberCancel();
104     void HallSensorSubscriberInit();
105     void HallSensorSubscriberCancel();
106     void InputMonitorInit();
107     void InputMonitorCancel();
108     bool CheckDialogFlag();
109     bool CheckDialogAndShuttingDown();
110     void SuspendControllerInit();
111     void WakeupControllerInit();
112 #ifdef POWER_MANAGER_WAKEUP_ACTION
113     void WakeupActionControllerInit();
114 #endif
115     void VibratorInit();
116     void Reset();
117 
GetRunningLockMgr()118     std::shared_ptr<RunningLockMgr> GetRunningLockMgr() const
119     {
120         return runningLockMgr_;
121     }
122 
GetPowerStateMachine()123     std::shared_ptr<PowerStateMachine> GetPowerStateMachine() const
124     {
125         return powerStateMachine_;
126     }
127 
GetPowerMgrNotify()128     std::shared_ptr<PowerMgrNotify> GetPowerMgrNotify() const
129     {
130         return powerMgrNotify_;
131     }
132 
GetSuspendController()133     std::shared_ptr<SuspendController> GetSuspendController() const
134     {
135         return suspendController_;
136     }
GetWakeupController()137     std::shared_ptr<WakeupController> GetWakeupController() const
138     {
139         return wakeupController_;
140     }
141 #ifdef POWER_MANAGER_WAKEUP_ACTION
GetWakeupActionController()142     std::shared_ptr<WakeupActionController> GetWakeupActionController() const
143     {
144         return wakeupActionController_;
145     }
146 #endif
GetShutdownDialog()147     ShutdownDialog& GetShutdownDialog()
148     {
149         return shutdownDialog_;
150     }
IsServiceReady()151     bool IsServiceReady() const
152     {
153         return ready_;
154     }
SetDisplayOffTime(int64_t time)155     void SetDisplayOffTime(int64_t time)
156     {
157         powerStateMachine_->SetDisplayOffTime(time);
158     }
SetSleepTime(int64_t time)159     void SetSleepTime(int64_t time)
160     {
161         powerStateMachine_->SetSleepTime(time);
162     }
163 
EnableMock(IDeviceStateAction * powerState,IDeviceStateAction * shutdownState,IDevicePowerAction * powerAction,IRunningLockAction * lockAction)164     void EnableMock(IDeviceStateAction* powerState, IDeviceStateAction* shutdownState, IDevicePowerAction* powerAction,
165         IRunningLockAction* lockAction)
166     {
167         POWER_HILOGI(LABEL_TEST, "Service EnableMock:%{public}d", mockCount_++);
168         runningLockMgr_->EnableMock(lockAction);
169         powerStateMachine_->EnableMock(powerState);
170         shutdownController_->EnableMock(powerAction, shutdownState);
171     }
MockProximity(uint32_t status)172     void MockProximity(uint32_t status)
173     {
174         POWER_HILOGI(LABEL_TEST, "MockProximity: fun is start");
175         runningLockMgr_->SetProximity(status);
176         POWER_HILOGI(LABEL_TEST, "MockProximity: fun is end");
177     }
MockSystemWakeup()178     void MockSystemWakeup()
179     {
180         PowerStateMachine::onWakeup();
181     }
GetShutdownController()182     std::shared_ptr<ShutdownController> GetShutdownController()
183     {
184         return shutdownController_;
185     }
186 
187     std::shared_ptr<SuspendController> suspendController_ = nullptr;
188     std::shared_ptr<WakeupController> wakeupController_ = nullptr;
189 #ifdef POWER_MANAGER_WAKEUP_ACTION
190     std::shared_ptr<WakeupActionController> wakeupActionController_ = nullptr;
191 #endif
192 
193 private:
194     class WakeupRunningLock {
195     public:
196         static void Create();
197         static void Lock();
198         static void Unlock();
199 
200     private:
201         WakeupRunningLock() = default;
202         ~WakeupRunningLock() = default;
203 
204         static sptr<RunningLockTokenStub> token_;
205         static const int32_t TIMEOUT = 10000; // 10seconds
206     };
207 
208     static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000;
209     static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000;
210     static constexpr int32_t HALL_REPORT_INTERVAL = 0;
211     static constexpr uint32_t HALL_SAMPLING_RATE = 100000000;
212     bool Init();
213     bool PowerStateMachineInit();
214     void NotifyRunningLockChanged(bool isUnLock);
215     std::string GetBundleNameByUid(const int32_t uid);
216     RunningLockParam FillRunningLockParam(const RunningLockInfo& info, int32_t timeOutMS = -1);
217     static void RegisterBootCompletedCallback();
218 
GetPowerModeModule()219     inline PowerModeModule& GetPowerModeModule()
220     {
221         return powerModeModule_;
222     }
223 
224 #ifdef HAS_SENSORS_SENSOR_PART
225     bool IsSupportSensor(SensorTypeId);
226     static void HallSensorCallback(SensorEvent* event);
227     SensorUser sensorUser_;
228 #endif
229 
230     bool ready_ {false};
231     static std::atomic_bool isBootCompleted_;
232     std::mutex wakeupMutex_;
233     std::mutex suspendMutex_;
234     std::mutex stateMutex_;
235     std::mutex shutdownMutex_;
236     std::mutex modeMutex_;
237     std::mutex screenMutex_;
238     std::mutex dumpMutex_;
239     std::mutex lockMutex_;
240     std::shared_ptr<RunningLockMgr> runningLockMgr_;
241     std::shared_ptr<PowerStateMachine> powerStateMachine_;
242     std::shared_ptr<PowerMgrNotify> powerMgrNotify_;
243     std::shared_ptr<ShutdownController> shutdownController_;
244     PowerModeModule powerModeModule_;
245     ShutdownDialog shutdownDialog_;
246     uint32_t mockCount_ {0};
247     int32_t switchId_ {0};
248     int32_t doubleClickId_ {0};
249     int32_t monitorId_ {0};
250     int32_t inputMonitorId_ {-1};
251 };
252 
253 #ifdef HAS_MULTIMODALINPUT_INPUT_PART
254 class PowerMgrInputMonitor : public IInputEventConsumer {
255 public:
256     virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const;
257     virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
258     virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const;
259 };
260 #endif
261 } // namespace PowerMgr
262 } // namespace OHOS
263 #endif // POWERMGR_POWER_MGR_SERVICE_H
264