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