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 namespace OHOS { 37 namespace PowerMgr { 38 class RunningLockMgr; 39 class PowerMgrService final : public SystemAbility, public PowerMgrStub { 40 DECLARE_SYSTEM_ABILITY(PowerMgrService) 41 DECLARE_DELAYED_SP_SINGLETON(PowerMgrService); 42 43 public: 44 virtual void OnStart() override; 45 virtual void OnStop() override; 46 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 47 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 48 virtual PowerErrors RebootDevice(const std::string& reason) override; 49 virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override; 50 virtual PowerErrors ShutDownDevice(const std::string& reason) override; 51 virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override; 52 virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override; 53 virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override; 54 virtual bool OverrideScreenOffTime(int64_t timeout) override; 55 virtual bool RestoreScreenOffTime() override; 56 virtual PowerState GetState() override; 57 virtual bool IsScreenOn() override; 58 virtual bool ForceSuspendDevice(int64_t callTimeMs) override; 59 virtual PowerErrors CreateRunningLock( 60 const sptr<IRemoteObject>& remoteObj, const RunningLockInfo& runningLockInfo) override; 61 virtual bool ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj) override; 62 virtual bool IsRunningLockTypeSupported(RunningLockType type) override; 63 virtual bool Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMS) override; 64 virtual bool UnLock(const sptr<IRemoteObject>& remoteObj) override; 65 virtual void ForceUnLock(const sptr<IRemoteObject>& remoteObj); 66 virtual bool IsUsed(const sptr<IRemoteObject>& remoteObj) override; 67 virtual bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid) override; 68 virtual bool ProxyRunningLocks(bool isProxied, 69 const std::vector<std::pair<pid_t, pid_t>>& processInfos) override; 70 virtual bool ResetRunningLocks() override; 71 virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override; 72 virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override; 73 virtual bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override; 74 virtual bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override; 75 virtual bool SetDisplaySuspend(bool enable) override; 76 virtual PowerErrors SetDeviceMode(const PowerMode& mode) override; 77 virtual PowerMode GetDeviceMode() override; 78 virtual std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override; 79 virtual PowerErrors IsStandby(bool& isStandby) override; 80 81 void RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority) override; 82 void UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback) override; 83 84 void RegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority) override; 85 void UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback) override; 86 void RegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority) override; 87 void UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback) override; 88 89 void HandleKeyEvent(int32_t keyCode); 90 void HandlePointEvent(int32_t type); 91 void KeyMonitorCancel(); 92 void SwitchSubscriberInit(); 93 void SwitchSubscriberCancel(); 94 void HallSensorSubscriberInit(); 95 void HallSensorSubscriberCancel(); 96 bool CheckDialogAndShuttingDown(); 97 void SuspendControllerInit(); 98 void WakeupControllerInit(); 99 GetRunningLockMgr()100 std::shared_ptr<RunningLockMgr> GetRunningLockMgr() const 101 { 102 return runningLockMgr_; 103 } 104 GetPowerStateMachine()105 std::shared_ptr<PowerStateMachine> GetPowerStateMachine() const 106 { 107 return powerStateMachine_; 108 } 109 GetPowerMgrNotify()110 std::shared_ptr<PowerMgrNotify> GetPowerMgrNotify() const 111 { 112 return powerMgrNotify_; 113 } 114 GetSuspendController()115 std::shared_ptr<SuspendController> GetSuspendController() const 116 { 117 return suspendController_; 118 } GetWakeupController()119 std::shared_ptr<WakeupController> GetWakeupController() const 120 { 121 return wakeupController_; 122 } GetShutdownDialog()123 ShutdownDialog& GetShutdownDialog() 124 { 125 return shutdownDialog_; 126 } IsServiceReady()127 bool IsServiceReady() const 128 { 129 return ready_; 130 } SetDisplayOffTime(int64_t time)131 void SetDisplayOffTime(int64_t time) 132 { 133 powerStateMachine_->SetDisplayOffTime(time); 134 } SetSleepTime(int64_t time)135 void SetSleepTime(int64_t time) 136 { 137 powerStateMachine_->SetSleepTime(time); 138 } 139 EnableMock(IDeviceStateAction * powerState,IDeviceStateAction * shutdownState,IDevicePowerAction * powerAction,IRunningLockAction * lockAction)140 void EnableMock(IDeviceStateAction* powerState, IDeviceStateAction* shutdownState, IDevicePowerAction* powerAction, 141 IRunningLockAction* lockAction) 142 { 143 POWER_HILOGI(LABEL_TEST, "Service EnableMock:%{public}d", mockCount_++); 144 runningLockMgr_->EnableMock(lockAction); 145 powerStateMachine_->EnableMock(powerState); 146 shutdownController_->EnableMock(powerAction, shutdownState); 147 } MockProximity(uint32_t status)148 void MockProximity(uint32_t status) 149 { 150 POWER_HILOGI(LABEL_TEST, "MockProximity: fun is start"); 151 runningLockMgr_->SetProximity(status); 152 POWER_HILOGI(LABEL_TEST, "MockProximity: fun is end"); 153 } MockSystemWakeup()154 void MockSystemWakeup() 155 { 156 PowerStateMachine::onWakeup(); 157 } GetShutdownController()158 std::shared_ptr<ShutdownController> GetShutdownController() 159 { 160 return shutdownController_; 161 } 162 163 std::shared_ptr<SuspendController> suspendController_ = nullptr; 164 std::shared_ptr<WakeupController> wakeupController_ = nullptr; 165 166 private: 167 class WakeupRunningLock { 168 public: 169 static void Create(); 170 static void Lock(); 171 static void Unlock(); 172 173 private: 174 WakeupRunningLock() = default; 175 ~WakeupRunningLock() = default; 176 177 static sptr<RunningLockTokenStub> token_; 178 static const int32_t TIMEOUT = 10000; // 10seconds 179 }; 180 181 static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; 182 static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000; 183 static constexpr int32_t HALL_REPORT_INTERVAL = 0; 184 static constexpr uint32_t HALL_SAMPLING_RATE = 100000000; 185 bool Init(); 186 bool PowerStateMachineInit(); 187 void NotifyRunningLockChanged(bool isUnLock); 188 RunningLockParam FillRunningLockParam(const RunningLockInfo& info, int32_t timeOutMS = -1); 189 bool IsSupportSensor(SensorTypeId); 190 static void HallSensorCallback(SensorEvent* event); 191 static void RegisterBootCompletedCallback(); 192 GetPowerModeModule()193 inline PowerModeModule& GetPowerModeModule() 194 { 195 return powerModeModule_; 196 } 197 198 bool ready_ {false}; 199 static std::atomic_bool isBootCompleted_; 200 std::mutex wakeupMutex_; 201 std::mutex suspendMutex_; 202 std::mutex stateMutex_; 203 std::mutex shutdownMutex_; 204 std::mutex modeMutex_; 205 std::mutex screenMutex_; 206 std::mutex dumpMutex_; 207 std::mutex lockMutex_; 208 std::shared_ptr<RunningLockMgr> runningLockMgr_; 209 std::shared_ptr<PowerStateMachine> powerStateMachine_; 210 std::shared_ptr<PowerMgrNotify> powerMgrNotify_; 211 std::shared_ptr<ShutdownController> shutdownController_; 212 PowerModeModule powerModeModule_; 213 ShutdownDialog shutdownDialog_; 214 uint32_t mockCount_ {0}; 215 int32_t switchId_ {0}; 216 int32_t doubleClickId_ {0}; 217 int32_t monitorId_ {0}; 218 SensorUser sensorUser_; 219 }; 220 } // namespace PowerMgr 221 } // namespace OHOS 222 #endif // POWERMGR_POWER_MGR_SERVICE_H 223