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_stub.h"
17
18 #include <message_parcel.h>
19 #include "errors.h"
20 #include "ipc_object_stub.h"
21 #include "iremote_broker.h"
22 #include "iremote_object.h"
23 #include "idisplay_power_callback.h"
24 #include "display_log.h"
25 #include "display_common.h"
26 #include "display_mgr_errors.h"
27 #include "display_power_info.h"
28 #include "display_power_mgr_ipc_interface_code.h"
29 #include "xcollie/xcollie.h"
30 #include "xcollie/xcollie_define.h"
31
32 namespace OHOS {
33 namespace DisplayPowerMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t DisplayPowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
35 MessageOption &option)
36 {
37 DISPLAY_HILOGD(COMP_SVC, "DisplayPowerMgrStub::OnRemoteRequest, cmd = %d, flags= %d",
38 code, option.GetFlags());
39 std::u16string descripter = DisplayPowerMgrStub::GetDescriptor();
40 std::u16string remoteDescripter = data.ReadInterfaceToken();
41 if (descripter != remoteDescripter) {
42 DISPLAY_HILOGE(COMP_SVC, "DisplayPowerMgrStub::OnRemoteRequest failed, descriptor is not matched!");
43 return E_GET_POWER_SERVICE_FAILED;
44 }
45
46 const int DFX_DELAY_MS = 10000;
47 int id = HiviewDFX::XCollie::GetInstance().SetTimer("DisplayPowerMgrStub", DFX_DELAY_MS, nullptr, nullptr,
48 HiviewDFX::XCOLLIE_FLAG_NOOP);
49 int32_t ret = ERR_OK;
50 switch (code) {
51 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::SET_DISPLAY_STATE):
52 ret = SetDisplayStateStub(data, reply);
53 break;
54 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_DISPLAY_STATE):
55 ret = GetDisplayStateStub(data, reply);
56 break;
57 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_DISPLAY_IDS):
58 ret = GetDisplayIdsStub(data, reply);
59 break;
60 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_MAIN_DISPLAY_ID):
61 ret = GetMainDisplayIdStub(data, reply);
62 break;
63 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::SET_BRIGHTNESS):
64 ret = SetBrightnessStub(data, reply);
65 break;
66 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::DISCOUNT_BRIGHTNESS):
67 ret = DiscountBrightnessStub(data, reply);
68 break;
69 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::OVERRIDE_BRIGHTNESS):
70 ret = OverrideBrightnessStub(data, reply);
71 break;
72 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::OVERRIDE_DISPLAY_OFF_DELAY):
73 ret = OverrideDisplayOffDelayStub(data, reply);
74 break;
75 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::RESTORE_BRIGHTNESS):
76 ret = RestoreBrightnessStub(data, reply);
77 break;
78 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_BRIGHTNESS):
79 ret = GetBrightnessStub(data, reply);
80 break;
81 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_DEFAULT_BRIGHTNESS):
82 ret = GetDefaultBrightnessStub(data, reply);
83 break;
84 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_MAX_BRIGHTNESS):
85 ret = GetMaxBrightnessStub(data, reply);
86 break;
87 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_MIN_BRIGHTNESS):
88 ret = GetMinBrightnessStub(data, reply);
89 break;
90 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::ADJUST_BRIGHTNESS):
91 ret = AdjustBrightnessStub(data, reply);
92 break;
93 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::AUTO_ADJUST_BRIGHTNESS):
94 ret = AutoAdjustBrightnessStub(data, reply);
95 break;
96 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::IS_AUTO_ADJUST_BRIGHTNESS):
97 ret = IsAutoAdjustBrightnessStub(data, reply);
98 break;
99 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::REGISTER_CALLBACK):
100 ret = RegisterCallbackStub(data, reply);
101 break;
102 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::BOOST_BRIGHTNESS):
103 ret = BoostBrightnessStub(data, reply);
104 break;
105 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::CANCEL_BOOST_BRIGHTNESS):
106 ret = CancelBoostBrightnessStub(data, reply);
107 break;
108 case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_DEVICE_BRIGHTNESS):
109 ret = GetDeviceBrightnessStub(data, reply);
110 break;
111 default:
112 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
113 break;
114 }
115
116 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
117 return ret;
118 }
119
SetDisplayStateStub(MessageParcel & data,MessageParcel & reply)120 int32_t DisplayPowerMgrStub::SetDisplayStateStub(MessageParcel& data, MessageParcel& reply)
121 {
122 uint32_t id = 0;
123 uint32_t state = 0;
124 uint32_t reason = 0;
125
126 READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
127 READ_PARCEL_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR);
128 READ_PARCEL_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
129
130 bool ret = SetDisplayState(id, static_cast<DisplayState>(state), reason);
131 if (!reply.WriteBool(ret)) {
132 DISPLAY_HILOGE(COMP_SVC, "Failed to write SetDisplayStateStub return value");
133 return E_WRITE_PARCEL_ERROR;
134 }
135 return ERR_OK;
136 }
137
GetDisplayStateStub(MessageParcel & data,MessageParcel & reply)138 int32_t DisplayPowerMgrStub::GetDisplayStateStub(MessageParcel& data, MessageParcel& reply)
139 {
140 uint32_t id = 0;
141
142 READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
143
144 DisplayState ret = GetDisplayState(id);
145 if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
146 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDisplayStateStub return value");
147 return E_WRITE_PARCEL_ERROR;
148 }
149 return ERR_OK;
150 }
151
GetDisplayIdsStub(MessageParcel & data,MessageParcel & reply)152 int32_t DisplayPowerMgrStub::GetDisplayIdsStub(MessageParcel& data, MessageParcel& reply)
153 {
154 std::vector<uint32_t> result = GetDisplayIds();
155 if (!reply.WriteUint32(static_cast<uint32_t>(result.size()))) {
156 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDisplayIdsStub return value");
157 return E_WRITE_PARCEL_ERROR;
158 }
159 for (uint32_t i = 0; i < result.size(); i++) {
160 if (!reply.WriteUint32(static_cast<uint32_t>(result[i]))) {
161 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDisplayIdsStub");
162 }
163 }
164 return ERR_OK;
165 }
166
GetMainDisplayIdStub(MessageParcel & data,MessageParcel & reply)167 int32_t DisplayPowerMgrStub::GetMainDisplayIdStub(MessageParcel& data, MessageParcel& reply)
168 {
169 uint32_t result = GetMainDisplayId();
170 if (!reply.WriteUint32(result)) {
171 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetMainDisplayIdStub return value");
172 return E_WRITE_PARCEL_ERROR;
173 }
174 return ERR_OK;
175 }
176
SetBrightnessStub(MessageParcel & data,MessageParcel & reply)177 int32_t DisplayPowerMgrStub::SetBrightnessStub(MessageParcel& data, MessageParcel& reply)
178 {
179 uint32_t value = 0;
180 uint32_t displayId = 0;
181
182 READ_PARCEL_WITH_RET(data, Uint32, value, E_READ_PARCEL_ERROR);
183 READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
184
185 bool ret = SetBrightness(value, displayId);
186 if (!reply.WriteBool(ret)) {
187 DISPLAY_HILOGE(COMP_SVC, "Failed to write SetBrightness return value");
188 return E_WRITE_PARCEL_ERROR;
189 }
190 int32_t error = static_cast<int32_t>(GetError());
191 WRITE_PARCEL_WITH_RET(reply, Int32, error, E_WRITE_PARCEL_ERROR);
192 return ERR_OK;
193 }
194
DiscountBrightnessStub(MessageParcel & data,MessageParcel & reply)195 int32_t DisplayPowerMgrStub::DiscountBrightnessStub(MessageParcel& data, MessageParcel& reply)
196 {
197 double discount = 0;
198 uint32_t displayId = 0;
199
200 READ_PARCEL_WITH_RET(data, Double, discount, E_READ_PARCEL_ERROR);
201 READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
202
203 bool ret = DiscountBrightness(discount, displayId);
204 if (!reply.WriteBool(ret)) {
205 DISPLAY_HILOGE(COMP_SVC, "Failed to wirte DiscountBrightness return value");
206 return E_WRITE_PARCEL_ERROR;
207 }
208 return ERR_OK;
209 }
210
OverrideBrightnessStub(MessageParcel & data,MessageParcel & reply)211 int32_t DisplayPowerMgrStub::OverrideBrightnessStub(MessageParcel& data, MessageParcel& reply)
212 {
213 uint32_t value = 0;
214 uint32_t displayId = 0;
215
216 READ_PARCEL_WITH_RET(data, Uint32, value, E_READ_PARCEL_ERROR);
217 READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
218
219 bool ret = OverrideBrightness(value, displayId);
220 if (!reply.WriteBool(ret)) {
221 DISPLAY_HILOGE(COMP_SVC, "Failed to write OverrideBrightness return value");
222 return E_WRITE_PARCEL_ERROR;
223 }
224 return ERR_OK;
225 }
226
OverrideDisplayOffDelayStub(MessageParcel & data,MessageParcel & reply)227 int32_t DisplayPowerMgrStub::OverrideDisplayOffDelayStub(MessageParcel& data, MessageParcel& reply)
228 {
229 uint32_t delayMs = 0;
230
231 READ_PARCEL_WITH_RET(data, Uint32, delayMs, E_READ_PARCEL_ERROR);
232
233 bool ret = OverrideDisplayOffDelay(delayMs);
234 if (!reply.WriteBool(ret)) {
235 DISPLAY_HILOGE(COMP_SVC, "Failed to write OverrideDisplayOffDelay return value");
236 return E_WRITE_PARCEL_ERROR;
237 }
238 return ERR_OK;
239 }
240
RestoreBrightnessStub(MessageParcel & data,MessageParcel & reply)241 int32_t DisplayPowerMgrStub::RestoreBrightnessStub(MessageParcel& data, MessageParcel& reply)
242 {
243 uint32_t displayId = 0;
244
245 READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
246
247 bool ret = RestoreBrightness(displayId);
248 if (!reply.WriteBool(ret)) {
249 DISPLAY_HILOGE(COMP_SVC, "Failed to write RestoreBrightness return value");
250 return E_WRITE_PARCEL_ERROR;
251 }
252 return ERR_OK;
253 }
254
GetBrightnessStub(MessageParcel & data,MessageParcel & reply)255 int32_t DisplayPowerMgrStub::GetBrightnessStub(MessageParcel& data, MessageParcel& reply)
256 {
257 uint32_t displayId = 0;
258
259 READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
260
261 uint32_t ret = GetBrightness(displayId);
262 if (!reply.WriteUint32(ret)) {
263 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetBrightness return value");
264 return E_WRITE_PARCEL_ERROR;
265 }
266 return ERR_OK;
267 }
268
GetDefaultBrightnessStub(MessageParcel & data,MessageParcel & reply)269 int32_t DisplayPowerMgrStub::GetDefaultBrightnessStub(MessageParcel& data, MessageParcel& reply)
270 {
271 uint32_t ret = GetDefaultBrightness();
272 if (!reply.WriteUint32(ret)) {
273 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDefaultBrightness return value");
274 return E_WRITE_PARCEL_ERROR;
275 }
276 return ERR_OK;
277 }
278
GetMaxBrightnessStub(MessageParcel & data,MessageParcel & reply)279 int32_t DisplayPowerMgrStub::GetMaxBrightnessStub(MessageParcel& data, MessageParcel& reply)
280 {
281 uint32_t ret = GetMaxBrightness();
282 if (!reply.WriteUint32(ret)) {
283 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetMaxBrightness return value");
284 return E_WRITE_PARCEL_ERROR;
285 }
286 return ERR_OK;
287 }
288
GetMinBrightnessStub(MessageParcel & data,MessageParcel & reply)289 int32_t DisplayPowerMgrStub::GetMinBrightnessStub(MessageParcel& data, MessageParcel& reply)
290 {
291 uint32_t ret = GetMinBrightness();
292 if (!reply.WriteUint32(ret)) {
293 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetMinBrightness return value");
294 return E_WRITE_PARCEL_ERROR;
295 }
296 return ERR_OK;
297 }
298
AdjustBrightnessStub(MessageParcel & data,MessageParcel & reply)299 int32_t DisplayPowerMgrStub::AdjustBrightnessStub(MessageParcel& data, MessageParcel& reply)
300 {
301 uint32_t id = 0;
302 int32_t value = 0;
303 uint32_t duration = 0;
304
305 READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
306 READ_PARCEL_WITH_RET(data, Int32, value, E_READ_PARCEL_ERROR);
307 READ_PARCEL_WITH_RET(data, Uint32, duration, E_READ_PARCEL_ERROR);
308
309 bool ret = AdjustBrightness(id, value, duration);
310 if (!reply.WriteBool(ret)) {
311 DISPLAY_HILOGE(COMP_SVC, "Failed to write AdjustBrightnessStub return value");
312 return E_WRITE_PARCEL_ERROR;
313 }
314 return ERR_OK;
315 }
316
AutoAdjustBrightnessStub(MessageParcel & data,MessageParcel & reply)317 int32_t DisplayPowerMgrStub::AutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply)
318 {
319 bool enable = 0;
320
321 READ_PARCEL_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR);
322
323 bool ret = AutoAdjustBrightness(enable);
324 if (!reply.WriteBool(ret)) {
325 DISPLAY_HILOGE(COMP_SVC, "Failed to write AutoAdjustBrightnessStub return value");
326 return E_WRITE_PARCEL_ERROR;
327 }
328 return ERR_OK;
329 }
330
IsAutoAdjustBrightnessStub(MessageParcel & data,MessageParcel & reply)331 int32_t DisplayPowerMgrStub::IsAutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply)
332 {
333 bool ret = IsAutoAdjustBrightness();
334 if (!reply.WriteBool(ret)) {
335 DISPLAY_HILOGE(COMP_SVC, "Failed to write IsAutoAdjustBrightnessStub return value");
336 return E_WRITE_PARCEL_ERROR;
337 }
338 return ERR_OK;
339 }
340
RegisterCallbackStub(MessageParcel & data,MessageParcel & reply)341 int32_t DisplayPowerMgrStub::RegisterCallbackStub(MessageParcel& data, MessageParcel& reply)
342 {
343 sptr<IRemoteObject> obj = data.ReadRemoteObject();
344 RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
345 sptr<IDisplayPowerCallback> callback = iface_cast<IDisplayPowerCallback>(obj);
346 RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
347 bool isSucc = RegisterCallback(callback);
348 WRITE_PARCEL_WITH_RET(reply, Bool, isSucc, E_WRITE_PARCEL_ERROR);
349 return ERR_OK;
350 }
351
BoostBrightnessStub(MessageParcel & data,MessageParcel & reply)352 int32_t DisplayPowerMgrStub::BoostBrightnessStub(MessageParcel& data, MessageParcel& reply)
353 {
354 int32_t timeoutMs = -1;
355 uint32_t id = 0;
356 READ_PARCEL_WITH_RET(data, Int32, timeoutMs, E_READ_PARCEL_ERROR);
357 READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
358
359 bool isScuu = BoostBrightness(timeoutMs, id);
360 if (!reply.WriteBool(isScuu)) {
361 DISPLAY_HILOGW(COMP_SVC, "Failed to write BoostBrightness return value");
362 return E_WRITE_PARCEL_ERROR;
363 }
364 return ERR_OK;
365 }
366
CancelBoostBrightnessStub(MessageParcel & data,MessageParcel & reply)367 int32_t DisplayPowerMgrStub::CancelBoostBrightnessStub(MessageParcel& data, MessageParcel& reply)
368 {
369 uint32_t displayId = 0;
370
371 READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
372
373 bool isScuu = CancelBoostBrightness(displayId);
374 if (!reply.WriteBool(isScuu)) {
375 DISPLAY_HILOGW(COMP_SVC, "Failed to write CancelBoostBrightness return value");
376 return E_WRITE_PARCEL_ERROR;
377 }
378 return ERR_OK;
379 }
380
GetDeviceBrightnessStub(MessageParcel & data,MessageParcel & reply)381 int32_t DisplayPowerMgrStub::GetDeviceBrightnessStub(MessageParcel& data, MessageParcel& reply)
382 {
383 uint32_t displayId = 0;
384
385 READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
386
387 uint32_t ret = GetDeviceBrightness(displayId);
388 if (!reply.WriteUint32(ret)) {
389 DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDeviceBrightness return value");
390 return E_WRITE_PARCEL_ERROR;
391 }
392 return ERR_OK;
393 }
394 } // namespace DisplayPowerMgr
395 } // namespace OHOS
396