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