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 #include "power_mgr_client.h"
17
18 #include <cinttypes>
19 #include <mutex>
20 #include <memory>
21 #include <vector>
22 #include <datetime_ex.h>
23 #include <if_system_ability_manager.h>
24 #include <iservice_registry.h>
25 #include <system_ability_definition.h>
26 #include "new"
27 #include "refbase.h"
28 #include "ipower_mgr.h"
29 #include "ipower_mode_callback.h"
30 #include "ipower_state_callback.h"
31 #include "iremote_broker.h"
32 #include "iremote_object.h"
33 #include "power_log.h"
34 #include "power_common.h"
35 #include "running_lock_info.h"
36
37 namespace OHOS {
38 namespace PowerMgr {
PowerMgrClient()39 PowerMgrClient::PowerMgrClient() {}
~PowerMgrClient()40 PowerMgrClient::~PowerMgrClient()
41 {
42 if (proxy_ != nullptr) {
43 auto remoteObject = proxy_->AsObject();
44 if (remoteObject != nullptr) {
45 remoteObject->RemoveDeathRecipient(deathRecipient_);
46 }
47 }
48 }
49
Connect()50 ErrCode PowerMgrClient::Connect()
51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 if (proxy_ != nullptr) {
54 return ERR_OK;
55 }
56
57 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (sam == nullptr) {
59 POWER_HILOGE(COMP_FWK, "Failed to obtain SystemAbilityMgr");
60 return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
61 }
62 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(POWER_MANAGER_SERVICE_ID);
63 if (remoteObject_ == nullptr) {
64 POWER_HILOGE(COMP_FWK, "Check SystemAbility failed");
65 return E_GET_POWER_SERVICE_FAILED;
66 }
67
68 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new PowerMgrDeathRecipient());
69 if (deathRecipient_ == nullptr) {
70 POWER_HILOGE(COMP_FWK, "Failed to create PowerMgrDeathRecipient");
71 return ERR_NO_MEMORY;
72 }
73 if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
74 POWER_HILOGE(COMP_FWK, "Add death recipient to PowerMgr service failed");
75 return E_ADD_DEATH_RECIPIENT_FAILED;
76 }
77
78 proxy_ = iface_cast<IPowerMgr>(remoteObject_);
79 POWER_HILOGI(COMP_FWK, "Connecting PowerMgrService success");
80 return ERR_OK;
81 }
82
ResetProxy(const wptr<IRemoteObject> & remote)83 void PowerMgrClient::ResetProxy(const wptr<IRemoteObject>& remote)
84 {
85 std::lock_guard<std::mutex> lock(mutex_);
86 RETURN_IF(proxy_ == nullptr);
87
88 auto serviceRemote = proxy_->AsObject();
89 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
90 serviceRemote->RemoveDeathRecipient(deathRecipient_);
91 proxy_ = nullptr;
92 }
93 }
94
OnRemoteDied(const wptr<IRemoteObject> & remote)95 void PowerMgrClient::PowerMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
96 {
97 if (remote == nullptr) {
98 POWER_HILOGE(COMP_FWK, "OnRemoteDied failed, remote is nullptr");
99 return;
100 }
101
102 PowerMgrClient::GetInstance().ResetProxy(remote);
103 POWER_HILOGW(COMP_FWK, "Recv death notice");
104 }
105
RebootDevice(const std::string & reason)106 PowerErrors PowerMgrClient::RebootDevice(const std::string& reason)
107 {
108 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
109 return proxy_->RebootDevice(reason);
110 }
111
RebootDeviceForDeprecated(const std::string & reason)112 PowerErrors PowerMgrClient::RebootDeviceForDeprecated(const std::string& reason)
113 {
114 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
115 return proxy_->RebootDeviceForDeprecated(reason);
116 }
117
ShutDownDevice(const std::string & reason)118 PowerErrors PowerMgrClient::ShutDownDevice(const std::string& reason)
119 {
120 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
121 return proxy_->ShutDownDevice(reason);
122 }
123
SuspendDevice(SuspendDeviceType reason,bool suspendImmed)124 PowerErrors PowerMgrClient::SuspendDevice(SuspendDeviceType reason, bool suspendImmed)
125 {
126 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
127 POWER_HILOGD(FEATURE_SUSPEND, " Calling SuspendDevice success");
128 return proxy_->SuspendDevice(GetTickCount(), reason, suspendImmed);
129 }
130
WakeupDevice(WakeupDeviceType reason,const std::string & detail)131 PowerErrors PowerMgrClient::WakeupDevice(WakeupDeviceType reason, const std::string& detail)
132 {
133 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
134 POWER_HILOGD(FEATURE_WAKEUP, " Calling WakeupDevice success");
135 return proxy_->WakeupDevice(GetTickCount(), reason, detail);
136 }
137
RefreshActivity(UserActivityType type)138 bool PowerMgrClient::RefreshActivity(UserActivityType type)
139 {
140 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
141 bool ret = proxy_->RefreshActivity(GetTickCount(), type, true);
142 POWER_HILOGD(FEATURE_ACTIVITY, "Calling RefreshActivity Success");
143 return ret;
144 }
145
OverrideScreenOffTime(int64_t timeout)146 bool PowerMgrClient::OverrideScreenOffTime(int64_t timeout)
147 {
148 if (timeout <= 0) {
149 POWER_HILOGW(COMP_FWK, "Invalid timeout, timeout=%{public}" PRId64 "", timeout);
150 return false;
151 }
152 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
153 bool ret = proxy_->OverrideScreenOffTime(timeout);
154 POWER_HILOGD(COMP_FWK, "Calling OverrideScreenOffTime Success");
155 return ret;
156 }
157
RestoreScreenOffTime()158 bool PowerMgrClient::RestoreScreenOffTime()
159 {
160 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
161 bool ret = proxy_->RestoreScreenOffTime();
162 POWER_HILOGD(COMP_FWK, "Calling RestoreScreenOffTime Success");
163 return ret;
164 }
165
IsRunningLockTypeSupported(RunningLockType type)166 bool PowerMgrClient::IsRunningLockTypeSupported(RunningLockType type)
167 {
168 POWER_HILOGD(FEATURE_RUNNING_LOCK, "RunningLockType=%{public}u", type);
169 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
170 return proxy_->IsRunningLockTypeSupported(type);
171 }
172
ForceSuspendDevice()173 bool PowerMgrClient::ForceSuspendDevice()
174 {
175 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
176 bool ret = proxy_->ForceSuspendDevice(GetTickCount());
177 POWER_HILOGD(FEATURE_SUSPEND, "Calling ForceSuspendDevice Success");
178 return ret;
179 }
180
IsScreenOn()181 bool PowerMgrClient::IsScreenOn()
182 {
183 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
184 bool ret = false;
185 ret = proxy_->IsScreenOn();
186 POWER_HILOGD(COMP_FWK, "Calling IsScreenOn Success");
187 return ret;
188 }
189
GetState()190 PowerState PowerMgrClient::GetState()
191 {
192 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerState::UNKNOWN);
193 return proxy_->GetState();
194 }
195
CreateRunningLock(const std::string & name,RunningLockType type)196 std::shared_ptr<RunningLock> PowerMgrClient::CreateRunningLock(const std::string& name, RunningLockType type)
197 {
198 RETURN_IF_WITH_RET(Connect() != ERR_OK, nullptr);
199
200 uint32_t nameLen = (name.size() > RunningLock::MAX_NAME_LEN) ? RunningLock::MAX_NAME_LEN : name.size();
201 std::shared_ptr<RunningLock> runningLock = std::make_shared<RunningLock>(proxy_, name.substr(0, nameLen), type);
202 if (runningLock == nullptr) {
203 POWER_HILOGE(FEATURE_RUNNING_LOCK, "Failed to create RunningLock record");
204 return nullptr;
205 }
206 auto error = runningLock->Init();
207 if (error != PowerErrors::ERR_OK) {
208 POWER_HILOGE(FEATURE_RUNNING_LOCK, "RunningLock init failed");
209 error_ = error;
210 return nullptr;
211 }
212 POWER_HILOGI(FEATURE_RUNNING_LOCK, "name: %{public}s, type = %{public}d", name.c_str(), type);
213 return runningLock;
214 }
215
ProxyRunningLock(bool isProxied,pid_t pid,pid_t uid)216 bool PowerMgrClient::ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid)
217 {
218 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
219 return proxy_->ProxyRunningLock(isProxied, pid, uid);
220 }
221
ProxyRunningLocks(bool isProxied,const std::vector<std::pair<pid_t,pid_t>> & processInfos)222 bool PowerMgrClient::ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos)
223 {
224 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
225 return proxy_->ProxyRunningLocks(isProxied, processInfos);
226 }
227
ResetRunningLocks()228 bool PowerMgrClient::ResetRunningLocks()
229 {
230 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
231 return proxy_->ResetRunningLocks();
232 }
233
RegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback)234 bool PowerMgrClient::RegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
235 {
236 RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
237 bool ret = proxy_->RegisterPowerStateCallback(callback);
238 return ret;
239 }
240
UnRegisterPowerStateCallback(const sptr<IPowerStateCallback> & callback)241 bool PowerMgrClient::UnRegisterPowerStateCallback(const sptr<IPowerStateCallback>& callback)
242 {
243 RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
244 bool ret = proxy_->UnRegisterPowerStateCallback(callback);
245 return ret;
246 }
247
RegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)248 bool PowerMgrClient::RegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
249 {
250 RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
251 bool ret = proxy_->RegisterPowerModeCallback(callback);
252 return ret;
253 }
254
UnRegisterPowerModeCallback(const sptr<IPowerModeCallback> & callback)255 bool PowerMgrClient::UnRegisterPowerModeCallback(const sptr<IPowerModeCallback>& callback)
256 {
257 RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
258 bool ret = proxy_->UnRegisterPowerModeCallback(callback);
259 return ret;
260 }
261
SetDisplaySuspend(bool enable)262 bool PowerMgrClient::SetDisplaySuspend(bool enable)
263 {
264 RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
265 bool ret = proxy_->SetDisplaySuspend(enable);
266 return ret;
267 }
268
SetDeviceMode(const PowerMode mode)269 PowerErrors PowerMgrClient::SetDeviceMode(const PowerMode mode)
270 {
271 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
272 return proxy_->SetDeviceMode(mode);
273 }
274
GetDeviceMode()275 PowerMode PowerMgrClient::GetDeviceMode()
276 {
277 RETURN_IF_WITH_RET(Connect() != ERR_OK, static_cast<PowerMode>(0));
278 return static_cast<PowerMode>(proxy_->GetDeviceMode());
279 }
280
Dump(const std::vector<std::string> & args)281 std::string PowerMgrClient::Dump(const std::vector<std::string>& args)
282 {
283 std::string error = "can't connect service";
284 RETURN_IF_WITH_RET(Connect() != ERR_OK, error);
285 return proxy_->ShellDump(args, args.size());
286 }
287
GetError()288 PowerErrors PowerMgrClient::GetError()
289 {
290 auto temp = error_;
291 error_ = PowerErrors::ERR_OK;
292 return temp;
293 }
294
IsStandby(bool & isStandby)295 PowerErrors PowerMgrClient::IsStandby(bool& isStandby)
296 {
297 RETURN_IF_WITH_RET(Connect() != ERR_OK, PowerErrors::ERR_CONNECTION_FAIL);
298 PowerErrors ret = proxy_->IsStandby(isStandby);
299 return ret;
300 }
301 } // namespace PowerMgr
302 } // namespace OHOS
303