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 "powerms_event_handler.h" 25 #include "power_mgr_notify.h" 26 #include "power_mgr_stub.h" 27 #include "power_state_machine.h" 28 #include "running_lock_mgr.h" 29 #include "shutdown_service.h" 30 #include "sp_singleton.h" 31 #include "power_mode_module.h" 32 #include "power_save_mode.h" 33 34 namespace OHOS { 35 namespace PowerMgr { 36 class RunningLockMgr; 37 class PowerMgrService final : public SystemAbility, public PowerMgrStub { 38 DECLARE_SYSTEM_ABILITY(PowerMgrService) 39 DECLARE_DELAYED_SP_SINGLETON(PowerMgrService); 40 41 public: 42 virtual void OnStart() override; 43 virtual void OnStop() override; 44 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 45 virtual PowerErrors RebootDevice(const std::string& reason) override; 46 virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override; 47 virtual PowerErrors ShutDownDevice(const std::string& reason) override; 48 virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, 49 bool suspendImmed) override; 50 virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, 51 const std::string& details) override; 52 virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, 53 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(const sptr<IRemoteObject>& remoteObj, 60 const RunningLockInfo& runningLockInfo) override; 61 virtual bool ReleaseRunningLock(const sptr<IRemoteObject>& remoteObj) override; 62 virtual bool IsRunningLockTypeSupported(uint32_t type) override; 63 virtual bool Lock(const sptr<IRemoteObject>& remoteObj, 64 const RunningLockInfo& runningLockInfo, uint32_t timeOutMS) override; 65 virtual bool UnLock(const sptr<IRemoteObject>& remoteObj) override; 66 virtual void ForceUnLock(const sptr<IRemoteObject>& remoteObj); 67 virtual bool IsUsed(const sptr<IRemoteObject>& remoteObj) override; 68 virtual bool SetWorkTriggerList(const sptr<IRemoteObject>& remoteObj, 69 const WorkTriggerList& workTriggerList) override; 70 virtual bool ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid) override; 71 virtual bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override; 72 virtual bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback) override; 73 virtual bool RegisterShutdownCallback(IShutdownCallback::ShutdownPriority priority, 74 const sptr<IShutdownCallback>& callback) override; 75 virtual bool UnRegisterShutdownCallback(const sptr<IShutdownCallback>& callback) override; 76 virtual bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override; 77 virtual bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback) override; 78 virtual bool SetDisplaySuspend(bool enable) override; 79 virtual PowerErrors SetDeviceMode(const PowerMode& mode) override; 80 virtual PowerMode GetDeviceMode() override; 81 virtual std::string ShellDump(const std::vector<std::string>& args, uint32_t argc) override; 82 83 void HandleShutdownRequest(); 84 void HandleKeyEvent(int32_t keyCode); 85 void HandlePointEvent(int32_t type); 86 void NotifyDisplayActionDone(uint32_t event); 87 void KeyMonitorInit(); 88 void KeyMonitorCancel(); 89 void HallSensorSubscriberInit(); 90 void HallSensorSubscriberCancel(); 91 bool ShowPowerDialog(); GetHandler()92 std::shared_ptr<PowermsEventHandler> GetHandler() const 93 { 94 return handler_; 95 } 96 GetRunningLockMgr()97 std::shared_ptr<RunningLockMgr> GetRunningLockMgr() const 98 { 99 return runningLockMgr_; 100 } 101 GetPowerStateMachine()102 std::shared_ptr<PowerStateMachine> GetPowerStateMachine() const 103 { 104 return powerStateMachine_; 105 } 106 GetPowerMgrNotify()107 std::shared_ptr<PowerMgrNotify> GetPowerMgrNotify() const 108 { 109 return powerMgrNotify_; 110 } 111 IsServiceReady()112 bool IsServiceReady() const 113 { 114 return ready_; 115 } SetDisplayOffTime(int64_t time)116 void SetDisplayOffTime(int64_t time) 117 { 118 powerStateMachine_->SetDisplayOffTime(time); 119 } SetSleepTime(int64_t time)120 void SetSleepTime(int64_t time) 121 { 122 powerStateMachine_->SetSleepTime(time); 123 } 124 void HandleScreenOnTimeout(); 125 EnableMock(IDeviceStateAction * powerState,IDeviceStateAction * shutdownState,IDevicePowerAction * powerAction,IRunningLockAction * lockAction)126 void EnableMock(IDeviceStateAction* powerState, IDeviceStateAction* shutdownState, 127 IDevicePowerAction* powerAction, IRunningLockAction* lockAction) 128 { 129 POWER_HILOGE(LABEL_TEST, "Service EnableMock:%{public}d", mockCount_++); 130 runningLockMgr_->EnableMock(lockAction); 131 powerStateMachine_->EnableMock(powerState); 132 shutdownService_.EnableMock(powerAction, shutdownState); 133 } MockProximity(uint32_t status)134 void MockProximity(uint32_t status) 135 { 136 POWER_HILOGE(LABEL_TEST, "MockProximity: fun is start"); 137 runningLockMgr_->SetProximity(status); 138 POWER_HILOGE(LABEL_TEST, "MockProximity: fun is end"); 139 } MockSystemWakeup()140 void MockSystemWakeup() 141 { 142 PowerStateMachine::onWakeup(); 143 } 144 private: 145 static constexpr int32_t LONG_PRESS_DELAY_MS = 3000; 146 static constexpr int32_t POWER_KEY_PRESS_DELAY_MS = 10000; 147 static constexpr int32_t INIT_KEY_MONITOR_DELAY_MS = 1000; 148 static constexpr int32_t HALL_REPORT_INTERVAL = 0; 149 static constexpr uint32_t HALL_SAMPLING_RATE = 100000000; 150 bool Init(); 151 bool PowerStateMachineInit(); 152 void HandlePowerKeyUp(); 153 void NotifyRunningLockChanged(bool isUnLock); 154 void FillUserIPCInfo(UserIPCInfo &userIPCinfo); 155 bool IsSupportSensor(SensorTypeId); 156 static void HallSensorCallback(SensorEvent* event); 157 static void RegisterBootCompletedCallback(); 158 GetPowerModeModule()159 inline PowerModeModule& GetPowerModeModule() 160 { 161 return powerModeModule_; 162 } 163 164 bool ready_ {false}; 165 std::mutex mutex_; 166 std::mutex lockMutex_; 167 std::shared_ptr<RunningLockMgr> runningLockMgr_; 168 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 169 std::shared_ptr<PowermsEventHandler> handler_; 170 std::shared_ptr<PowerStateMachine> powerStateMachine_; 171 std::shared_ptr<PowerMgrNotify> powerMgrNotify_; 172 ShutdownService shutdownService_; 173 PowerModeModule powerModeModule_; 174 bool powerkeyPressed_ {false}; 175 uint32_t mockCount_ {0}; 176 bool isDialogstatus_ {false}; 177 int32_t powerkeyLongPressId_ {0}; 178 int32_t powerkeyShortPressId_ {0}; 179 int32_t powerkeyReleaseId_ {0}; 180 int32_t doubleClickId_ {0}; 181 int32_t monitorId_ {0}; 182 SensorUser sensorUser_; 183 }; 184 } // namespace PowerMgr 185 } // namespace OHOS 186 #endif // POWERMGR_POWER_MGR_SERVICE_H 187