1 /*
2 * Copyright (c) 2021-2022 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_proxy.h"
17
18 #include "errors.h"
19 #include "message_option.h"
20 #include "message_parcel.h"
21 #include "display_log.h"
22 #include "display_common.h"
23
24 namespace OHOS {
25 namespace DisplayPowerMgr {
SetDisplayState(uint32_t id,DisplayState state,uint32_t reason)26 bool DisplayPowerMgrProxy::SetDisplayState(uint32_t id, DisplayState state, uint32_t reason)
27 {
28 sptr<IRemoteObject> remote = Remote();
29 RETURN_IF_WITH_RET(remote == nullptr, false);
30
31 bool result = false;
32 MessageParcel data;
33 MessageParcel reply;
34 MessageOption option;
35
36 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
37 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
38 return result;
39 }
40
41 WRITE_PARCEL_WITH_RET(data, Uint32, id, false);
42 WRITE_PARCEL_WITH_RET(data, Uint32, static_cast<uint32_t>(state), false);
43 WRITE_PARCEL_WITH_RET(data, Uint32, reason, false);
44
45 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::SET_DISPLAY_STATE),
46 data, reply, option);
47 if (ret != ERR_OK) {
48 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
49 return result;
50 }
51
52 if (!reply.ReadBool(result)) {
53 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
54 return result;
55 }
56
57 return result;
58 }
59
GetDisplayState(uint32_t id)60 DisplayState DisplayPowerMgrProxy::GetDisplayState(uint32_t id)
61 {
62 sptr<IRemoteObject> remote = Remote();
63 RETURN_IF_WITH_RET(remote == nullptr, DisplayState::DISPLAY_UNKNOWN);
64
65 uint32_t result = 0;
66 MessageParcel data;
67 MessageParcel reply;
68 MessageOption option;
69
70 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
71 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
72 return DisplayState::DISPLAY_UNKNOWN;
73 }
74
75 WRITE_PARCEL_WITH_RET(data, Uint32, id, DisplayState::DISPLAY_UNKNOWN);
76
77 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_DISPLAY_STATE),
78 data, reply, option);
79 if (ret != ERR_OK) {
80 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
81 return DisplayState::DISPLAY_UNKNOWN;
82 }
83
84 if (!reply.ReadUint32(result)) {
85 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
86 return DisplayState::DISPLAY_UNKNOWN;
87 }
88
89 return static_cast<DisplayState>(result);
90 }
91
GetDisplayIds()92 std::vector<uint32_t> DisplayPowerMgrProxy::GetDisplayIds()
93 {
94 sptr<IRemoteObject> remote = Remote();
95 std::vector<uint32_t> result;
96
97 RETURN_IF_WITH_RET(remote == nullptr, result);
98
99 uint32_t count = 0;
100 MessageParcel data;
101 MessageParcel reply;
102 MessageOption option;
103
104 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
105 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
106 return result;
107 }
108
109 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_DISPLAY_IDS),
110 data, reply, option);
111 if (ret != ERR_OK) {
112 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
113 return result;
114 }
115
116 if (!reply.ReadUint32(count)) {
117 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
118 return result;
119 }
120
121 for (uint32_t i = 0; i < count; i++) {
122 uint32_t value;
123 if (reply.ReadUint32(value)) {
124 result.push_back(value);
125 } else {
126 DISPLAY_HILOGE(COMP_FWK, "read value fail: %{public}d", i);
127 }
128 }
129
130 return result;
131 }
132
GetMainDisplayId()133 uint32_t DisplayPowerMgrProxy::GetMainDisplayId()
134 {
135 sptr<IRemoteObject> remote = Remote();
136 uint32_t result = 0;
137
138 RETURN_IF_WITH_RET(remote == nullptr, result);
139
140 MessageParcel data;
141 MessageParcel reply;
142 MessageOption option;
143
144 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
145 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
146 return result;
147 }
148
149 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_MAIN_DISPLAY_ID),
150 data, reply, option);
151 if (ret != ERR_OK) {
152 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
153 return result;
154 }
155
156 if (!reply.ReadUint32(result)) {
157 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
158 return result;
159 }
160
161 return result;
162 }
163
SetBrightness(uint32_t value,uint32_t displayId)164 bool DisplayPowerMgrProxy::SetBrightness(uint32_t value, uint32_t displayId)
165 {
166 sptr<IRemoteObject> remote = Remote();
167 RETURN_IF_WITH_RET(remote == nullptr, false);
168
169 bool result = false;
170 MessageParcel data;
171 MessageParcel reply;
172 MessageOption option;
173
174 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
175 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
176 return result;
177 }
178
179 WRITE_PARCEL_WITH_RET(data, Uint32, value, false);
180 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
181
182 int ret = remote->SendRequest(static_cast<int32_t>(IDisplayPowerMgr::SET_BRIGHTNESS),
183 data, reply, option);
184 if (ret != ERR_OK) {
185 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
186 return result;
187 }
188
189 if (!reply.ReadBool(result)) {
190 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
191 return result;
192 }
193 int32_t error;
194 READ_PARCEL_WITH_RET(reply, Int32, error, result);
195 lastError_ = static_cast<DisplayErrors>(error);
196
197 return result;
198 }
199
DiscountBrightness(double discount,uint32_t displayId)200 bool DisplayPowerMgrProxy::DiscountBrightness(double discount, uint32_t displayId)
201 {
202 sptr<IRemoteObject> remote = Remote();
203 RETURN_IF_WITH_RET(remote == nullptr, false);
204
205 bool result = false;
206 MessageParcel data;
207 MessageParcel reply;
208 MessageOption option;
209
210 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
211 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
212 return result;
213 }
214
215 WRITE_PARCEL_WITH_RET(data, Double, discount, false);
216 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
217
218 int ret = remote->SendRequest(static_cast<int32_t>(IDisplayPowerMgr::DISCOUNT_BRIGHTNESS),
219 data, reply, option);
220 if (ret != ERR_OK) {
221 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
222 return false;
223 }
224
225 if (!reply.ReadBool(result)) {
226 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
227 return false;
228 }
229
230 return result;
231 }
232
OverrideBrightness(uint32_t value,uint32_t displayId)233 bool DisplayPowerMgrProxy::OverrideBrightness(uint32_t value, uint32_t displayId)
234 {
235 sptr<IRemoteObject> remote = Remote();
236 RETURN_IF_WITH_RET(remote == nullptr, false);
237
238 bool result = false;
239 MessageParcel data;
240 MessageParcel reply;
241 MessageOption option;
242
243 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
244 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
245 return result;
246 }
247
248 WRITE_PARCEL_WITH_RET(data, Uint32, value, false);
249 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
250
251 int ret = remote->SendRequest(static_cast<int32_t>(IDisplayPowerMgr::OVERRIDE_BRIGHTNESS), data, reply, option);
252 if (ret != ERR_OK) {
253 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
254 return result;
255 }
256
257 if (!reply.ReadBool(result)) {
258 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
259 return result;
260 }
261
262 return result;
263 }
264
RestoreBrightness(uint32_t displayId)265 bool DisplayPowerMgrProxy::RestoreBrightness(uint32_t displayId)
266 {
267 sptr<IRemoteObject> remote = Remote();
268 uint32_t result = 0;
269
270 RETURN_IF_WITH_RET(remote == nullptr, result);
271
272 MessageParcel data;
273 MessageParcel reply;
274 MessageOption option;
275
276 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
277 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
278 return result;
279 }
280
281 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
282
283 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::RESTORE_BRIGHTNESS),
284 data, reply, option);
285 if (ret != ERR_OK) {
286 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
287 return result;
288 }
289
290 if (!reply.ReadUint32(result)) {
291 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
292 return result;
293 }
294
295 return result;
296 }
297
GetBrightness(uint32_t displayId)298 uint32_t DisplayPowerMgrProxy::GetBrightness(uint32_t displayId)
299 {
300 sptr<IRemoteObject> remote = Remote();
301 uint32_t result = 0;
302
303 RETURN_IF_WITH_RET(remote == nullptr, result);
304
305 MessageParcel data;
306 MessageParcel reply;
307 MessageOption option;
308
309 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
310 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
311 return result;
312 }
313
314 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
315
316 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_BRIGHTNESS),
317 data, reply, option);
318 if (ret != ERR_OK) {
319 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
320 return result;
321 }
322
323 if (!reply.ReadUint32(result)) {
324 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
325 return result;
326 }
327
328 return result;
329 }
330
GetDefaultBrightness()331 uint32_t DisplayPowerMgrProxy::GetDefaultBrightness()
332 {
333 sptr<IRemoteObject> remote = Remote();
334 uint32_t result = 0;
335
336 RETURN_IF_WITH_RET(remote == nullptr, result);
337
338 MessageParcel data;
339 MessageParcel reply;
340 MessageOption option;
341
342 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
343 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
344 return result;
345 }
346
347 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_DEFAULT_BRIGHTNESS),
348 data, reply, option);
349 if (ret != ERR_OK) {
350 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
351 return result;
352 }
353
354 if (!reply.ReadUint32(result)) {
355 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
356 return result;
357 }
358
359 return result;
360 }
361
GetMaxBrightness()362 uint32_t DisplayPowerMgrProxy::GetMaxBrightness()
363 {
364 sptr<IRemoteObject> remote = Remote();
365 uint32_t result = 0;
366
367 RETURN_IF_WITH_RET(remote == nullptr, result);
368
369 MessageParcel data;
370 MessageParcel reply;
371 MessageOption option;
372
373 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
374 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
375 return result;
376 }
377
378 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_MAX_BRIGHTNESS),
379 data, reply, option);
380 if (ret != ERR_OK) {
381 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
382 return result;
383 }
384
385 if (!reply.ReadUint32(result)) {
386 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
387 return result;
388 }
389
390 return result;
391 }
392
GetMinBrightness()393 uint32_t DisplayPowerMgrProxy::GetMinBrightness()
394 {
395 sptr<IRemoteObject> remote = Remote();
396 uint32_t result = 0;
397
398 RETURN_IF_WITH_RET(remote == nullptr, result);
399
400 MessageParcel data;
401 MessageParcel reply;
402 MessageOption option;
403
404 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
405 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
406 return result;
407 }
408
409 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_MIN_BRIGHTNESS),
410 data, reply, option);
411 if (ret != ERR_OK) {
412 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
413 return result;
414 }
415
416 if (!reply.ReadUint32(result)) {
417 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
418 return result;
419 }
420
421 return result;
422 }
423
AdjustBrightness(uint32_t id,int32_t value,uint32_t duration)424 bool DisplayPowerMgrProxy::AdjustBrightness(uint32_t id, int32_t value, uint32_t duration)
425 {
426 sptr<IRemoteObject> remote = Remote();
427 RETURN_IF_WITH_RET(remote == nullptr, false);
428
429 bool result = false;
430 MessageParcel data;
431 MessageParcel reply;
432 MessageOption option;
433
434 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
435 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
436 return result;
437 }
438
439 WRITE_PARCEL_WITH_RET(data, Uint32, id, false);
440 WRITE_PARCEL_WITH_RET(data, Int32, value, false);
441 WRITE_PARCEL_WITH_RET(data, Int32, duration, false);
442
443 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::ADJUST_BRIGHTNESS),
444 data, reply, option);
445 if (ret != ERR_OK) {
446 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
447 return result;
448 }
449
450 if (!reply.ReadBool(result)) {
451 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
452 return result;
453 }
454
455 return result;
456 }
457
AutoAdjustBrightness(bool enable)458 bool DisplayPowerMgrProxy::AutoAdjustBrightness(bool enable)
459 {
460 sptr<IRemoteObject> remote = Remote();
461 RETURN_IF_WITH_RET(remote == nullptr, false);
462
463 bool result = false;
464 MessageParcel data;
465 MessageParcel reply;
466 MessageOption option;
467
468 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
469 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
470 return result;
471 }
472
473 WRITE_PARCEL_WITH_RET(data, Bool, enable, false);
474
475 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::AUTO_ADJUST_BRIGHTNESS),
476 data, reply, option);
477 if (ret != ERR_OK) {
478 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
479 return result;
480 }
481
482 if (!reply.ReadBool(result)) {
483 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
484 return result;
485 }
486
487 return result;
488 }
489
IsAutoAdjustBrightness()490 bool DisplayPowerMgrProxy::IsAutoAdjustBrightness()
491 {
492 sptr<IRemoteObject> remote = Remote();
493 RETURN_IF_WITH_RET(remote == nullptr, false);
494
495 bool result = false;
496 MessageParcel data;
497 MessageParcel reply;
498 MessageOption option;
499
500 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
501 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
502 return result;
503 }
504
505 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::IS_AUTO_ADJUST_BRIGHTNESS),
506 data, reply, option);
507 if (ret != ERR_OK) {
508 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
509 return result;
510 }
511
512 if (!reply.ReadBool(result)) {
513 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
514 return result;
515 }
516
517 return result;
518 }
519
RegisterCallback(sptr<IDisplayPowerCallback> callback)520 bool DisplayPowerMgrProxy::RegisterCallback(sptr<IDisplayPowerCallback> callback)
521 {
522 sptr<IRemoteObject> remote = Remote();
523 RETURN_IF_WITH_RET(remote == nullptr, false);
524
525 bool result = false;
526 MessageParcel data;
527 MessageParcel reply;
528 MessageOption option;
529
530 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
531 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
532 return result;
533 }
534
535 WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
536
537 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::REGISTER_CALLBACK),
538 data, reply, option);
539 if (ret != ERR_OK) {
540 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
541 return result;
542 }
543
544 if (!reply.ReadBool(result)) {
545 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
546 return result;
547 }
548
549 return result;
550 }
551
BoostBrightness(int32_t timeoutMs,uint32_t displayId)552 bool DisplayPowerMgrProxy::BoostBrightness(int32_t timeoutMs, uint32_t displayId)
553 {
554 sptr<IRemoteObject> remote = Remote();
555 RETURN_IF_WITH_RET(remote == nullptr, false);
556
557 bool result = false;
558 MessageParcel data;
559 MessageParcel reply;
560 MessageOption option;
561
562 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
563 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
564 return result;
565 }
566
567 WRITE_PARCEL_WITH_RET(data, Int32, timeoutMs, false);
568 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
569
570 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::BOOST_BRIGHTNESS),
571 data, reply, option);
572 if (ret != ERR_OK) {
573 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
574 return result;
575 }
576
577 if (!reply.ReadBool(result)) {
578 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
579 return result;
580 }
581
582 return result;
583 }
584
CancelBoostBrightness(uint32_t displayId)585 bool DisplayPowerMgrProxy::CancelBoostBrightness(uint32_t displayId)
586 {
587 sptr<IRemoteObject> remote = Remote();
588 RETURN_IF_WITH_RET(remote == nullptr, false);
589
590 bool result = false;
591 MessageParcel data;
592 MessageParcel reply;
593 MessageOption option;
594
595 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
596 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
597 return result;
598 }
599 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
600
601 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::CANCEL_BOOST_BRIGHTNESS),
602 data, reply, option);
603 if (ret != ERR_OK) {
604 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
605 return result;
606 }
607
608 if (!reply.ReadBool(result)) {
609 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
610 return result;
611 }
612
613 return result;
614 }
615
GetDeviceBrightness(uint32_t displayId)616 uint32_t DisplayPowerMgrProxy::GetDeviceBrightness(uint32_t displayId)
617 {
618 sptr<IRemoteObject> remote = Remote();
619 uint32_t result = 0;
620
621 RETURN_IF_WITH_RET(remote == nullptr, result);
622
623 MessageParcel data;
624 MessageParcel reply;
625 MessageOption option;
626
627 if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) {
628 DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!");
629 return result;
630 }
631
632 WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false);
633
634 int ret = remote->SendRequest(static_cast<int>(IDisplayPowerMgr::GET_DEVICE_BRIGHTNESS),
635 data, reply, option);
636 if (ret != ERR_OK) {
637 DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret);
638 return result;
639 }
640
641 if (!reply.ReadUint32(result)) {
642 DISPLAY_HILOGE(COMP_FWK, "Readback fail!");
643 return result;
644 }
645
646 return result;
647 }
648
GetError()649 DisplayErrors DisplayPowerMgrProxy::GetError()
650 {
651 return lastError_;
652 }
653 } // namespace DisplayPowerMgr
654 } // namespace OHOS
655