• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <singleton.h>
22 
23 #include "power_state_machine_info.h"
24 #include "running_lock.h"
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 class PowerMgrClient final : public DelayedRefSingleton<PowerMgrClient> {
29     DECLARE_DELAYED_REF_SINGLETON(PowerMgrClient)
30 
31 public:
32     DISALLOW_COPY_AND_MOVE(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      * Suspend device and set screen off.
53      *
54      * @param reason The reason why will you suspend the device, such as timeout/powerkey/forcesuspend and so on.
55      */
56     PowerErrors SuspendDevice(SuspendDeviceType reason = SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION,
57         bool suspendImmed = false);
58 
59     /**
60      * Wake up the device and set the screen on.
61      *
62      * @param reason The reason for waking up the device, such as powerkey/plugin/application.
63      */
64     PowerErrors WakeupDevice(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION,
65         const std::string& detail = std::string("app call"));
66 
67     /**
68      * Refresh the screentimeout time, and keep the screen on. RefreshActivity works only when the screen is on.
69      *
70      * @param type The RefreshActivity type, such as touch/button/accessibility and so on.
71      * @param needChangeBacklight Whether to change the backlight state, for example, from DIM to BRIGHT.
72      *                            Set it to false if you don't want to change the backlight state.
73      */
74     bool RefreshActivity(UserActivityType type = UserActivityType::USER_ACTIVITY_TYPE_OTHER);
75 
76     /**
77      * Windows overwrite timeout
78      * @param timeout Specifies the timeout duration.
79      */
80     bool OverrideScreenOffTime(int64_t timeout);
81 
82     /**
83      * Windows restores timeout
84      */
85     bool RestoreScreenOffTime();
86 
87     /**
88      * Check whether the device screen is on. The result may be true or false, depending on the system state.
89      */
90     bool IsScreenOn();
91 
92     /**
93      * Get Power state. The result is PowerState type.
94      */
95     PowerState GetState();
96 
97     /**
98      * Forcibly suspend the device into deepsleep, and return the suspend result.
99      */
100     bool ForceSuspendDevice();
101 
102     /**
103      * Check whether the type of running lock is supported
104      */
105     bool IsRunningLockTypeSupported(RunningLockType type);
106 
107     /**
108      * Enable/disable display suspend state
109      */
110     bool SetDisplaySuspend(bool enable);
111 
112     /* Set the device mode.
113      *
114      * @param set The mode the device.
115      */
116     PowerErrors SetDeviceMode(const PowerMode mode);
117 
118     /**
119      * Get the device mode.
120      *
121      * @param Get The mode the device.
122      */
123     PowerMode GetDeviceMode();
124 
125     /**
126      * Check if the device has entered standby mode.
127      */
128     PowerErrors IsStandby(bool& isStandby);
129 
130     /**
131      * Query the list of lock information.
132      */
133     bool QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists);
134 
135     std::shared_ptr<RunningLock> CreateRunningLock(const std::string& name, RunningLockType type);
136     bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid);
137     bool ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos);
138     bool ResetRunningLocks();
139     bool RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
140     bool UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback);
141     bool RegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority);
142     bool UnRegisterSyncSleepCallback(const sptr<ISyncSleepCallback>& callback);
143     bool RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback);
144     bool UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback);
145     void RecoverRunningLocks();
146     std::string Dump(const std::vector<std::string>& args);
147     PowerErrors GetError();
148 
149 #ifndef POWERMGR_SERVICE_DEATH_UT
150 private:
151 #endif
152     class PowerMgrDeathRecipient : public IRemoteObject::DeathRecipient {
153     public:
PowerMgrDeathRecipient(PowerMgrClient & client)154         explicit PowerMgrDeathRecipient(PowerMgrClient& client) : client_(client) {}
155         ~PowerMgrDeathRecipient() = default;
156         void OnRemoteDied(const wptr<IRemoteObject>& remote);
157 
158     private:
159         DISALLOW_COPY_AND_MOVE(PowerMgrDeathRecipient);
160         PowerMgrClient& client_;
161     };
162 
163     ErrCode Connect();
164     void ResetProxy(const wptr<IRemoteObject>& remote);
165     sptr<IPowerMgr> proxy_ {nullptr};
166     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr};
167     std::mutex mutex_;
168     static std::vector<std::weak_ptr<RunningLock>> runningLocks_;
169     static std::mutex runningLocksMutex_;
170     PowerErrors error_ = PowerErrors::ERR_OK;
171 };
172 } // namespace PowerMgr
173 } // namespace OHOS
174 #endif // POWERMGR_POWER_MGR_CLIENT_H
175