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 "display_power_mgr_client.h"
17
18 #include <if_system_ability_manager.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 #include "new"
22 #include "refbase.h"
23 #include "iremote_broker.h"
24 #include "iremote_object.h"
25 #include "display_log.h"
26 #include "display_common.h"
27 #include "display_power_info.h"
28 #include "idisplay_power_callback.h"
29 #include "idisplay_power_mgr.h"
30 #include "power_state_machine_info.h"
31
32 namespace OHOS {
33 namespace DisplayPowerMgr {
34 DisplayPowerMgrClient::DisplayPowerMgrClient() = default;
35 DisplayPowerMgrClient::~DisplayPowerMgrClient() = default;
36 namespace {
37 constexpr int32_t DEFAULT_VALUE = -1;
38 constexpr uint32_t BRIGHTNESS_DEFAULT_PROXY = 0;
39 }
GetProxy()40 sptr<IDisplayPowerMgr> DisplayPowerMgrClient::GetProxy()
41 {
42 std::lock_guard lock(mutex_);
43 if (proxy_ != nullptr) {
44 return proxy_;
45 }
46
47 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 if (sam == nullptr) {
49 DISPLAY_HILOGE(COMP_FWK, "Failed to get system ability manager");
50 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
51 return nullptr;
52 }
53 sptr<IRemoteObject> obj = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID);
54 if (obj == nullptr) {
55 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
56 static uint32_t count = 0;
57 DISPLAY_HILOGE(COMP_FWK, "Failed to get display manager service, count=%{public}u", ++count);
58 return nullptr;
59 }
60 sptr<IRemoteObject::DeathRecipient> dr = new DisplayDeathRecipient(*this);
61 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
62 DISPLAY_HILOGE(COMP_FWK, "Failed to add death recipient");
63 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
64 return nullptr;
65 }
66
67 proxy_ = iface_cast<IDisplayPowerMgr>(obj);
68 deathRecipient_ = dr;
69 DISPLAY_HILOGI(COMP_FWK, "Succeed to connect display manager service, pid=%{public}d", getpid());
70 return proxy_;
71 }
72
OnRemoteDied(const wptr<IRemoteObject> & remote)73 void DisplayPowerMgrClient::OnRemoteDied(const wptr<IRemoteObject>& remote)
74 {
75 if (remote == nullptr) {
76 DISPLAY_HILOGE(COMP_FWK, "remote is nullptr");
77 return;
78 }
79
80 std::lock_guard lock(mutex_);
81 RETURN_IF(proxy_ == nullptr);
82
83 auto serviceRemote = proxy_->AsObject();
84 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
85 serviceRemote->RemoveDeathRecipient(deathRecipient_);
86 proxy_ = nullptr;
87 }
88 }
89
SetDisplayState(DisplayState state,PowerMgr::StateChangeReason reason,uint32_t id)90 bool DisplayPowerMgrClient::SetDisplayState(DisplayState state,
91 PowerMgr::StateChangeReason reason, uint32_t id)
92 {
93 auto proxy = GetProxy();
94 RETURN_IF_WITH_RET(proxy == nullptr, false);
95 bool result = false;
96 auto ret = proxy->SetDisplayState(id, static_cast<uint32_t>(state), static_cast<uint32_t>(reason), result);
97 if (ret != ERR_OK) {
98 DISPLAY_HILOGE(COMP_FWK, "SetDisplayState, ret = %{public}d", ret);
99 return false;
100 }
101 return result;
102 }
103
GetDisplayState(uint32_t id)104 DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id)
105 {
106 auto proxy = GetProxy();
107 if (proxy == nullptr) {
108 return DisplayState::DISPLAY_UNKNOWN;
109 }
110 int32_t displayState = static_cast<int32_t>(DisplayState::DISPLAY_UNKNOWN);
111 auto ret = proxy->GetDisplayState(id, displayState);
112 if (ret != ERR_OK) {
113 DISPLAY_HILOGE(COMP_FWK, "GetDisplayState, ret = %{public}d", ret);
114 return DisplayState::DISPLAY_UNKNOWN;
115 }
116 return static_cast<DisplayState>(displayState);
117 }
118
GetDisplayIds()119 std::vector<uint32_t> DisplayPowerMgrClient::GetDisplayIds()
120 {
121 auto proxy = GetProxy();
122 if (proxy == nullptr) {
123 return {};
124 }
125 std::vector<uint32_t> ids;
126 auto ret = proxy->GetDisplayIds(ids);
127 if (ret != ERR_OK) {
128 DISPLAY_HILOGE(COMP_FWK, "GetDisplayIds, ret = %{public}d", ret);
129 }
130 return ids;
131 }
132
GetMainDisplayId()133 int32_t DisplayPowerMgrClient::GetMainDisplayId()
134 {
135 auto proxy = GetProxy();
136 RETURN_IF_WITH_RET(proxy == nullptr, INVALID_DISPLAY_ID);
137 uint32_t id = DEFAULT_MAIN_DISPLAY_ID;
138 auto ret = proxy->GetMainDisplayId(id);
139 if (ret != ERR_OK) {
140 DISPLAY_HILOGE(COMP_FWK, "GetMainDisplayId, ret = %{public}d", ret);
141 return DEFAULT_MAIN_DISPLAY_ID;
142 }
143 return id;
144 }
145
SetBrightness(uint32_t value,uint32_t displayId,bool continuous)146 bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId, bool continuous)
147 {
148 auto proxy = GetProxy();
149 RETURN_IF_WITH_RET(proxy == nullptr, false);
150 bool result = false;
151 int32_t displayError = ERR_OK;
152 auto ret = proxy->SetBrightness(value, displayId, continuous, result, displayError);
153 if (ret != ERR_OK) {
154 DISPLAY_HILOGE(COMP_FWK, "SetBrightness, ret = %{public}d", ret);
155 return false;
156 }
157 lastError_ = static_cast<DisplayErrors>(displayError);
158 return result;
159 }
160
DiscountBrightness(double discount,uint32_t displayId)161 bool DisplayPowerMgrClient::DiscountBrightness(double discount, uint32_t displayId)
162 {
163 auto proxy = GetProxy();
164 RETURN_IF_WITH_RET(proxy == nullptr, false);
165 bool result = false;
166 auto ret = proxy->DiscountBrightness(discount, displayId, result);
167 if (ret != ERR_OK) {
168 DISPLAY_HILOGE(COMP_FWK, "DiscountBrightness, ret = %{public}d", ret);
169 return false;
170 }
171 return result;
172 }
173
OverrideBrightness(uint32_t value,uint32_t displayId,uint32_t duration)174 bool DisplayPowerMgrClient::OverrideBrightness(uint32_t value, uint32_t displayId, uint32_t duration)
175 {
176 auto proxy = GetProxy();
177 RETURN_IF_WITH_RET(proxy == nullptr, false);
178 bool result = false;
179 auto ret = proxy->OverrideBrightness(value, displayId, duration, result);
180 if (ret != ERR_OK) {
181 DISPLAY_HILOGE(COMP_FWK, "OverrideBrightness, ret = %{public}d", ret);
182 return false;
183 }
184 return result;
185 }
186
OverrideDisplayOffDelay(uint32_t delayMs)187 bool DisplayPowerMgrClient::OverrideDisplayOffDelay(uint32_t delayMs)
188 {
189 auto proxy = GetProxy();
190 RETURN_IF_WITH_RET(proxy == nullptr, false);
191 bool result = false;
192 auto ret = proxy->OverrideDisplayOffDelay(delayMs, result);
193 if (ret != ERR_OK) {
194 DISPLAY_HILOGE(COMP_FWK, "OverrideDisplayOffDelay, ret = %{public}d", ret);
195 return false;
196 }
197 return result;
198 }
199
RestoreBrightness(uint32_t displayId,uint32_t duration)200 bool DisplayPowerMgrClient::RestoreBrightness(uint32_t displayId, uint32_t duration)
201 {
202 auto proxy = GetProxy();
203 RETURN_IF_WITH_RET(proxy == nullptr, false);
204 bool result = false;
205 auto ret = proxy->RestoreBrightness(displayId, duration, result);
206 if (ret != ERR_OK) {
207 DISPLAY_HILOGE(COMP_FWK, "RestoreBrightness, ret = %{public}d", ret);
208 return false;
209 }
210 return result;
211 }
212
GetBrightness(uint32_t displayId)213 uint32_t DisplayPowerMgrClient::GetBrightness(uint32_t displayId)
214 {
215 auto proxy = GetProxy();
216 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_OFF);
217 uint32_t brightness = BRIGHTNESS_OFF;
218 auto ret = proxy->GetBrightness(displayId, brightness);
219 if (ret != ERR_OK) {
220 DISPLAY_HILOGE(COMP_FWK, "GetBrightness, ret = %{public}d", ret);
221 return BRIGHTNESS_OFF;
222 }
223 return brightness;
224 }
225
GetDefaultBrightness()226 uint32_t DisplayPowerMgrClient::GetDefaultBrightness()
227 {
228 auto proxy = GetProxy();
229 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_DEFAULT);
230 uint32_t brightness = BRIGHTNESS_DEFAULT_PROXY;
231 auto ret = proxy->GetDefaultBrightness(brightness);
232 if (ret != ERR_OK) {
233 DISPLAY_HILOGE(COMP_FWK, "GetDefaultBrightness, ret = %{public}d", ret);
234 return BRIGHTNESS_DEFAULT_PROXY;
235 }
236 return brightness;
237 }
238
GetMaxBrightness()239 uint32_t DisplayPowerMgrClient::GetMaxBrightness()
240 {
241 auto proxy = GetProxy();
242 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MAX);
243 uint32_t brightness = BRIGHTNESS_DEFAULT_PROXY;
244 auto ret = proxy->GetMaxBrightness(brightness);
245 if (ret != ERR_OK) {
246 DISPLAY_HILOGE(COMP_FWK, "GetMaxBrightness, ret = %{public}d", ret);
247 return BRIGHTNESS_DEFAULT_PROXY;
248 }
249 return brightness;
250 }
251
GetMinBrightness()252 uint32_t DisplayPowerMgrClient::GetMinBrightness()
253 {
254 auto proxy = GetProxy();
255 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MIN);
256 uint32_t brightness = BRIGHTNESS_DEFAULT_PROXY;
257 auto ret = proxy->GetMinBrightness(brightness);
258 if (ret != ERR_OK) {
259 DISPLAY_HILOGE(COMP_FWK, "GetMinBrightness, ret = %{public}d", ret);
260 return BRIGHTNESS_DEFAULT_PROXY;
261 }
262 return brightness;
263 }
264
AdjustBrightness(uint32_t value,uint32_t duration,uint32_t id)265 bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id)
266 {
267 auto proxy = GetProxy();
268 RETURN_IF_WITH_RET(proxy == nullptr, false);
269 bool result = false;
270 auto ret = proxy->AdjustBrightness(id, value, duration, result);
271 if (ret != ERR_OK) {
272 DISPLAY_HILOGE(COMP_FWK, "AdjustBrightness, ret = %{public}d", ret);
273 return false;
274 }
275 return result;
276 }
277
AutoAdjustBrightness(bool enable)278 bool DisplayPowerMgrClient::AutoAdjustBrightness(bool enable)
279 {
280 auto proxy = GetProxy();
281 RETURN_IF_WITH_RET(proxy == nullptr, false);
282 bool result = false;
283 auto ret = proxy->AutoAdjustBrightness(enable, result);
284 if (ret != ERR_OK) {
285 DISPLAY_HILOGE(COMP_FWK, "AutoAdjustBrightness, ret = %{public}d", ret);
286 return false;
287 }
288 return result;
289 }
290
IsAutoAdjustBrightness()291 bool DisplayPowerMgrClient::IsAutoAdjustBrightness()
292 {
293 auto proxy = GetProxy();
294 RETURN_IF_WITH_RET(proxy == nullptr, false);
295 bool result = false;
296 auto ret = proxy->IsAutoAdjustBrightness(result);
297 if (ret != ERR_OK) {
298 DISPLAY_HILOGE(COMP_FWK, "IsAutoAdjustBrightness, ret = %{public}d", ret);
299 return false;
300 }
301 return result;
302 }
303
SetScreenOnBrightness()304 bool DisplayPowerMgrClient::SetScreenOnBrightness()
305 {
306 auto proxy = GetProxy();
307 RETURN_IF_WITH_RET(proxy == nullptr, false);
308 bool result = false;
309 auto ret = proxy->SetScreenOnBrightness(result);
310 if (ret != ERR_OK) {
311 DISPLAY_HILOGE(COMP_FWK, "SetScreenOnBrightness, ret = %{public}d", ret);
312 return false;
313 }
314 return result;
315 }
316
RegisterCallback(sptr<IDisplayPowerCallback> callback)317 bool DisplayPowerMgrClient::RegisterCallback(sptr<IDisplayPowerCallback> callback)
318 {
319 if (callback == nullptr) {
320 DISPLAY_HILOGE(COMP_FWK, "callback is nullptr");
321 return false;
322 }
323
324 auto proxy = GetProxy();
325 RETURN_IF_WITH_RET(proxy == nullptr, false);
326 bool result = false;
327 auto ret = proxy->RegisterCallback(callback, result);
328 if (ret != ERR_OK) {
329 DISPLAY_HILOGE(COMP_FWK, "RegisterCallback, ret = %{public}d", ret);
330 return false;
331 }
332 return result;
333 }
334
BoostBrightness(int32_t timeoutMs,uint32_t displayId)335 bool DisplayPowerMgrClient::BoostBrightness(int32_t timeoutMs, uint32_t displayId)
336 {
337 auto proxy = GetProxy();
338 RETURN_IF_WITH_RET(proxy == nullptr, false);
339 bool result = false;
340 auto ret = proxy->BoostBrightness(timeoutMs, displayId, result);
341 if (ret != ERR_OK) {
342 DISPLAY_HILOGE(COMP_FWK, "BoostBrightness, ret = %{public}d", ret);
343 return false;
344 }
345 return result;
346 }
347
CancelBoostBrightness(uint32_t displayId)348 bool DisplayPowerMgrClient::CancelBoostBrightness(uint32_t displayId)
349 {
350 auto proxy = GetProxy();
351 RETURN_IF_WITH_RET(proxy == nullptr, false);
352 bool result = false;
353 auto ret = proxy->CancelBoostBrightness(displayId, result);
354 if (ret != ERR_OK) {
355 DISPLAY_HILOGE(COMP_FWK, "CancelBoostBrightness, ret = %{public}d", ret);
356 return false;
357 }
358 return result;
359 }
360
GetDeviceBrightness(uint32_t displayId)361 uint32_t DisplayPowerMgrClient::GetDeviceBrightness(uint32_t displayId)
362 {
363 auto proxy = GetProxy();
364 RETURN_IF_WITH_RET(proxy == nullptr, 0);
365 uint32_t brightness = BRIGHTNESS_OFF;
366 auto ret = proxy->GetDeviceBrightness(displayId, brightness);
367 if (ret != ERR_OK) {
368 DISPLAY_HILOGE(COMP_FWK, "GetDeviceBrightness, ret = %{public}d", ret);
369 return BRIGHTNESS_OFF;
370 }
371 return brightness;
372 }
373
SetCoordinated(bool coordinated,uint32_t displayId)374 bool DisplayPowerMgrClient::SetCoordinated(bool coordinated, uint32_t displayId)
375 {
376 auto proxy = GetProxy();
377 RETURN_IF_WITH_RET(proxy == nullptr, 0);
378 bool result = false;
379 auto ret = proxy->SetCoordinated(coordinated, displayId, result);
380 if (ret != ERR_OK) {
381 DISPLAY_HILOGE(COMP_FWK, "SetCoordinated, ret = %{public}d", ret);
382 return false;
383 }
384 return result;
385 }
386
SetLightBrightnessThreshold(std::vector<int32_t> threshold,sptr<IDisplayBrightnessCallback> callback)387 uint32_t DisplayPowerMgrClient::SetLightBrightnessThreshold(
388 std::vector<int32_t> threshold, sptr<IDisplayBrightnessCallback> callback)
389 {
390 auto proxy = GetProxy();
391 RETURN_IF_WITH_RET(proxy == nullptr, 0);
392 uint32_t result = BRIGHTNESS_DEFAULT_PROXY;
393 auto ret = proxy->SetLightBrightnessThreshold(threshold, callback, result);
394 if (ret != ERR_OK) {
395 DISPLAY_HILOGE(COMP_FWK, "SetLightBrightnessThreshold, ret = %{public}d", ret);
396 return BRIGHTNESS_DEFAULT_PROXY;
397 }
398 return result;
399 }
400
GetError()401 DisplayErrors DisplayPowerMgrClient::GetError()
402 {
403 DisplayErrors tmpError = lastError_;
404 if (lastError_ != DisplayErrors::ERR_OK) {
405 lastError_ = DisplayErrors::ERR_OK;
406 }
407 return tmpError;
408 }
409
SetMaxBrightness(double value,uint32_t enterTestMode)410 bool DisplayPowerMgrClient::SetMaxBrightness(double value, uint32_t enterTestMode)
411 {
412 auto proxy = GetProxy();
413 RETURN_IF_WITH_RET(proxy == nullptr, false);
414 bool result = false;
415 int32_t displayError = ERR_OK;
416 auto ret = proxy->SetMaxBrightness(value, enterTestMode, result, displayError);
417 if (ret != ERR_OK) {
418 DISPLAY_HILOGE(COMP_FWK, "SetMaxBrightness, ret = %{public}d", ret);
419 return false;
420 }
421 lastError_ = static_cast<DisplayErrors>(displayError);
422 return result;
423 }
424
SetMaxBrightnessNit(uint32_t maxNit,uint32_t enterTestMode)425 bool DisplayPowerMgrClient::SetMaxBrightnessNit(uint32_t maxNit, uint32_t enterTestMode)
426 {
427 auto proxy = GetProxy();
428 RETURN_IF_WITH_RET(proxy == nullptr, false);
429 bool result = false;
430 int32_t displayError = ERR_OK;
431 auto ret = proxy->SetMaxBrightnessNit(maxNit, enterTestMode, result, displayError);
432 if (ret != ERR_OK) {
433 DISPLAY_HILOGE(COMP_FWK, "SetMaxBrightnessNit, ret = %{public}d", ret);
434 return false;
435 }
436 lastError_ = static_cast<DisplayErrors>(displayError);
437 return result;
438 }
439
NotifyBrightnessManagerScreenPowerStatus(uint32_t displayId,uint32_t status)440 int DisplayPowerMgrClient::NotifyBrightnessManagerScreenPowerStatus(uint32_t displayId, uint32_t status)
441 {
442 auto proxy = GetProxy();
443 RETURN_IF_WITH_RET(proxy == nullptr, -1); // -1 means failed
444 int32_t result = DEFAULT_VALUE;
445 auto ret = proxy->NotifyScreenPowerStatus(displayId, status, result);
446 if (ret != ERR_OK) {
447 DISPLAY_HILOGE(COMP_FWK, "NotifyBrightnessManagerScreenPowerStatus, ret = %{public}d", ret);
448 return DEFAULT_VALUE;
449 }
450 lastError_ = static_cast<DisplayErrors>(result);
451 return result;
452 }
453
454 } // namespace DisplayPowerMgr
455 } // namespace OHOS
456