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