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_POWER_MGR_CLIENT_H 17 #define POWERMGR_POWER_MGR_CLIENT_H 18 19 #include <string> 20 #include <singleton.h> 21 22 #include "power_state_machine_info.h" 23 #include "running_lock.h" 24 #include "suspend/itake_over_suspend_callback.h" 25 #include "isync_sleep_callback.h" 26 27 namespace OHOS { 28 namespace PowerMgr { 29 class PowerMgrClient final { 30 public: 31 static PowerMgrClient& GetInstance(); 32 virtual ~PowerMgrClient(); 33 static const uint32_t CONNECT_RETRY_COUNT = 5; 34 static const uint32_t CONNECT_RETRY_MS = 800000; 35 /** 36 * Reboot the device. 37 * 38 * @param reason The reason for rebooting the device. e.g.updater 39 */ 40 PowerErrors RebootDevice(const std::string& reason); 41 PowerErrors RebootDeviceForDeprecated(const std::string& reason); 42 43 /** 44 * Shut down the device. 45 * 46 * @param reason The reason for shutting down the device. 47 * 48 */ 49 PowerErrors ShutDownDevice(const std::string& reason); 50 51 /** 52 * @brief Set suspend tag before suspend. 53 * The special sleep mode supported by the kernel and hardware is triggered by setting a special 54 * suspend tag and then triggering suspend. If the suspend tag is not set, the standard S3 sleep 55 * mode is triggered when suspend. 56 * 57 * @param tag Suspend tag. 58 */ 59 PowerErrors SetSuspendTag(const std::string& tag); 60 61 /** 62 * Suspend device and set screen off. 63 * 64 * @param reason The reason why will you suspend the device, such as timeout/powerkey/forcesuspend and so on. 65 */ 66 PowerErrors SuspendDevice(SuspendDeviceType reason = SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, 67 bool suspendImmed = false, const std::string& apiVersion = "-1"); 68 69 /** 70 * Wake up the device and set the screen on. 71 * 72 * @param reason The reason for waking up the device, such as powerkey/plugin/application. 73 */ 74 PowerErrors WakeupDevice(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, 75 const std::string& detail = std::string("app call"), const std::string& apiVersion = "-1"); 76 77 /** 78 * Wake up the device and set the screen on async. 79 * 80 * @param reason The reason for waking up the device, such as powerkey/plugin/application. 81 */ 82 void WakeupDeviceAsync(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, 83 const std::string& detail = std::string("app call")); 84 85 /** 86 * Refresh the screentimeout time, and keep the screen on. RefreshActivity works only when the screen is on. 87 * 88 * @param type The RefreshActivity type, such as touch/button/accessibility and so on. 89 * @param needChangeBacklight Whether to change the backlight state, for example, from DIM to BRIGHT. 90 * Set it to false if you don't want to change the backlight state. 91 */ 92 bool RefreshActivity(UserActivityType type = UserActivityType::USER_ACTIVITY_TYPE_OTHER); 93 94 /** 95 * Refresh the screentimeout time, and keep the screen on. RefreshActivity works only when the screen is on. 96 * 97 * @param type The RefreshActivity type, such as touch/button/accessibility and so on. 98 * @param refreshReason The reason to refresh the screentimeout time. 99 * @return PowerErrors::ERR_OK if the call success, otherwise return error code. 100 */ 101 PowerErrors RefreshActivity(UserActivityType type, const std::string& refreshReason); 102 103 /** 104 * Windows overwrite timeout 105 * @param timeout Specifies the timeout duration. 106 */ 107 PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1"); 108 109 /** 110 * Windows restores timeout 111 */ 112 PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1"); 113 114 /** 115 * Check whether the device screen is on. The result may be true or false, depending on the system state. 116 */ 117 bool IsScreenOn(bool needPrintLog = true); 118 119 /** 120 * Check whether the fold device screen is on. The result may be true or false, depending on the system state. 121 */ 122 bool IsFoldScreenOn(); 123 124 /** 125 * Check whether the collaboration device screen is on. 126 * The result may be true or false, depending on the system state. 127 */ 128 bool IsCollaborationScreenOn(); 129 130 /** 131 * Check whether the device is in force sleep. The result may be true or false, depending on the system state. 132 */ 133 bool IsForceSleeping(); 134 135 /** 136 * Get Power state. The result is PowerState type. 137 */ 138 PowerState GetState(); 139 140 /** 141 * Forcibly suspend the device into deepsleep, and return the suspend result. 142 */ 143 PowerErrors ForceSuspendDevice(const std::string& apiVersion = "-1"); 144 145 /** 146 * Check whether the type of running lock is supported 147 */ 148 bool IsRunningLockTypeSupported(RunningLockType type); 149 150 /** 151 * Enable/disable display suspend state 152 */ 153 bool SetDisplaySuspend(bool enable); 154 155 /** 156 * Hibernate the device. 157 * @param clearMemory Indicates whether to clear the memory before the device hibernates. 158 * @param reasonn The reason for hibernate the device. 159 */ 160 PowerErrors Hibernate(bool clearMemory, const std::string& reason = "", const std::string& apiVersion = "-1"); 161 162 /* Set the device mode. 163 * 164 * @param set The mode the device. 165 */ 166 PowerErrors SetDeviceMode(const PowerMode mode); 167 168 /** 169 * Get the device mode. 170 * 171 * @param Get The mode the device. 172 */ 173 PowerMode GetDeviceMode(); 174 175 /** 176 * Check if the device has entered standby mode. 177 */ 178 PowerErrors IsStandby(bool& isStandby); 179 180 /** 181 * Query the list of lock information. 182 */ 183 bool QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists); 184 185 PowerErrors SetForceTimingOut(bool enabled); 186 PowerErrors LockScreenAfterTimingOut(bool enabledLockScreen, bool checkLock, bool sendScreenOffEvent = true); 187 188 std::shared_ptr<RunningLock> CreateRunningLock(const std::string& name, RunningLockType type); 189 bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid); 190 bool ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos); 191 bool ResetRunningLocks(); 192 bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback, bool isSync = true); 193 bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback); 194 bool RegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback); 195 bool UnRegisterSyncHibernateCallback(const sptr<ISyncHibernateCallback>& callback); 196 bool RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority); 197 bool UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback); 198 bool RegisterSuspendTakeoverCallback( 199 const sptr<ITakeOverSuspendCallback>& callback, TakeOverSuspendPriority priority); 200 bool UnRegisterSuspendTakeoverCallback(const sptr<ITakeOverSuspendCallback>& callback); 201 bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback); 202 bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback); 203 void RecoverRunningLocks(); 204 bool RegisterScreenStateCallback(int32_t remainTime, const sptr<IScreenOffPreCallback>& callback); 205 bool UnRegisterScreenStateCallback(const sptr<IScreenOffPreCallback>& callback); 206 bool RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback); 207 bool UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback); 208 std::string Dump(const std::vector<std::string>& args); 209 PowerErrors GetError(); 210 211 /** 212 * Check whether the type of running lock is enabled 213 * @param type The type of running lock for query if it is enabled 214 * @param result The result of whether the type of running lock is enabled 215 * @return PowerErrors::ERR_OK if the call success, otherwise return error code 216 */ 217 PowerErrors IsRunningLockEnabled(const RunningLockType type, bool& result); 218 219 #ifndef POWERMGR_SERVICE_DEATH_UT 220 private: 221 #endif 222 class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient { 223 public: PowerMgrDeathRecipient(PowerMgrClient & client)224 explicit PowerMgrDeathRecipient(PowerMgrClient& client) : client_(client) {} 225 ~PowerMgrDeathRecipient() = default; 226 void OnRemoteDied(const wptr<IRemoteObject>& remote); 227 228 private: 229 DISALLOW_COPY_AND_MOVE(PowerMgrDeathRecipient); 230 PowerMgrClient& client_; 231 }; 232 233 ErrCode Connect(); 234 sptr<IPowerMgr> GetPowerMgrProxy(); 235 void ResetProxy(const wptr<IRemoteObject>& remote); 236 sptr<IPowerMgr> proxy_ {nullptr}; 237 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr}; 238 std::mutex mutex_; 239 static std::vector<std::weak_ptr<RunningLock>> runningLocks_; 240 static std::mutex runningLocksMutex_; 241 sptr<IRemoteObject> token_ {nullptr}; 242 PowerErrors error_ = PowerErrors::ERR_OK; 243 244 private: 245 PowerMgrClient(); 246 DISALLOW_COPY_AND_MOVE(PowerMgrClient); 247 }; 248 } // namespace PowerMgr 249 } // namespace OHOS 250 #endif // POWERMGR_POWER_MGR_CLIENT_H 251