• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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     constexpr int dfxDelayMs = 10000;
47     int id = HiviewDFX::XCollie::GetInstance().SetTimer("DisplayPowerMgrStub", dfxDelayMs, 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         default:
76             ret = RemoteRequest(code, data, reply, option);
77             break;
78     }
79 
80     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
81     return ret;
82 }
83 
RemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)84 int32_t DisplayPowerMgrStub::RemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
85     MessageOption &option)
86 {
87     int32_t ret = ERR_OK;
88     switch (code) {
89         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::RESTORE_BRIGHTNESS):
90             ret = RestoreBrightnessStub(data, reply);
91             break;
92         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_BRIGHTNESS):
93             ret = GetBrightnessStub(data, reply);
94             break;
95         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_DEFAULT_BRIGHTNESS):
96             ret = GetDefaultBrightnessStub(data, reply);
97             break;
98         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_MAX_BRIGHTNESS):
99             ret = GetMaxBrightnessStub(data, reply);
100             break;
101         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_MIN_BRIGHTNESS):
102             ret = GetMinBrightnessStub(data, reply);
103             break;
104         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::ADJUST_BRIGHTNESS):
105             ret = AdjustBrightnessStub(data, reply);
106             break;
107         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::AUTO_ADJUST_BRIGHTNESS):
108             ret = AutoAdjustBrightnessStub(data, reply);
109             break;
110         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::IS_AUTO_ADJUST_BRIGHTNESS):
111             ret = IsAutoAdjustBrightnessStub(data, reply);
112             break;
113         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::REGISTER_CALLBACK):
114             ret = RegisterCallbackStub(data, reply);
115             break;
116         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::BOOST_BRIGHTNESS):
117             ret = BoostBrightnessStub(data, reply);
118             break;
119         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::CANCEL_BOOST_BRIGHTNESS):
120             ret = CancelBoostBrightnessStub(data, reply);
121             break;
122         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::GET_DEVICE_BRIGHTNESS):
123             ret = GetDeviceBrightnessStub(data, reply);
124             break;
125         case static_cast<int32_t>(PowerMgr::DisplayPowerMgrInterfaceCode::SET_COORDINATED):
126             ret = SetCoordinatedStub(data, reply);
127             break;
128         default:
129             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
130             break;
131     }
132 
133     return ret;
134 }
135 
SetDisplayStateStub(MessageParcel & data,MessageParcel & reply)136 int32_t DisplayPowerMgrStub::SetDisplayStateStub(MessageParcel& data, MessageParcel& reply)
137 {
138     uint32_t id = 0;
139     uint32_t state = 0;
140     uint32_t reason = 0;
141 
142     READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
143     READ_PARCEL_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR);
144     READ_PARCEL_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
145 
146     bool ret = SetDisplayState(id, static_cast<DisplayState>(state), reason);
147     if (!reply.WriteBool(ret)) {
148         DISPLAY_HILOGE(COMP_SVC, "Failed to write SetDisplayStateStub return value");
149         return E_WRITE_PARCEL_ERROR;
150     }
151     return ERR_OK;
152 }
153 
GetDisplayStateStub(MessageParcel & data,MessageParcel & reply)154 int32_t DisplayPowerMgrStub::GetDisplayStateStub(MessageParcel& data, MessageParcel& reply)
155 {
156     uint32_t id = 0;
157 
158     READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
159 
160     DisplayState ret = GetDisplayState(id);
161     if (!reply.WriteUint32(static_cast<uint32_t>(ret))) {
162         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDisplayStateStub return value");
163         return E_WRITE_PARCEL_ERROR;
164     }
165     return ERR_OK;
166 }
167 
GetDisplayIdsStub(MessageParcel & data,MessageParcel & reply)168 int32_t DisplayPowerMgrStub::GetDisplayIdsStub(MessageParcel& data, MessageParcel& reply)
169 {
170     std::vector<uint32_t> result = GetDisplayIds();
171     if (!reply.WriteUint32(static_cast<uint32_t>(result.size()))) {
172         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDisplayIdsStub return value");
173         return E_WRITE_PARCEL_ERROR;
174     }
175     for (uint32_t i = 0; i < result.size(); i++) {
176         if (!reply.WriteUint32(static_cast<uint32_t>(result[i]))) {
177             DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDisplayIdsStub");
178         }
179     }
180     return ERR_OK;
181 }
182 
GetMainDisplayIdStub(MessageParcel & data,MessageParcel & reply)183 int32_t DisplayPowerMgrStub::GetMainDisplayIdStub(MessageParcel& data, MessageParcel& reply)
184 {
185     uint32_t result = GetMainDisplayId();
186     if (!reply.WriteUint32(result)) {
187         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetMainDisplayIdStub return value");
188         return E_WRITE_PARCEL_ERROR;
189     }
190     return ERR_OK;
191 }
192 
SetBrightnessStub(MessageParcel & data,MessageParcel & reply)193 int32_t DisplayPowerMgrStub::SetBrightnessStub(MessageParcel& data, MessageParcel& reply)
194 {
195     uint32_t value = 0;
196     uint32_t displayId = 0;
197     bool continuous = false;
198 
199     READ_PARCEL_WITH_RET(data, Uint32, value, E_READ_PARCEL_ERROR);
200     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
201     READ_PARCEL_WITH_RET(data, Bool, continuous, E_READ_PARCEL_ERROR);
202 
203     bool ret = SetBrightness(value, displayId, continuous);
204     if (!reply.WriteBool(ret)) {
205         DISPLAY_HILOGE(COMP_SVC, "Failed to write SetBrightness return value");
206         return E_WRITE_PARCEL_ERROR;
207     }
208     int32_t error = static_cast<int32_t>(GetError());
209     WRITE_PARCEL_WITH_RET(reply, Int32, error, E_WRITE_PARCEL_ERROR);
210     return ERR_OK;
211 }
212 
DiscountBrightnessStub(MessageParcel & data,MessageParcel & reply)213 int32_t DisplayPowerMgrStub::DiscountBrightnessStub(MessageParcel& data, MessageParcel& reply)
214 {
215     double discount = 0;
216     uint32_t displayId = 0;
217 
218     READ_PARCEL_WITH_RET(data, Double, discount, E_READ_PARCEL_ERROR);
219     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
220 
221     bool ret = DiscountBrightness(discount, displayId);
222     if (!reply.WriteBool(ret)) {
223         DISPLAY_HILOGE(COMP_SVC, "Failed to wirte DiscountBrightness return value");
224         return E_WRITE_PARCEL_ERROR;
225     }
226     return ERR_OK;
227 }
228 
OverrideBrightnessStub(MessageParcel & data,MessageParcel & reply)229 int32_t DisplayPowerMgrStub::OverrideBrightnessStub(MessageParcel& data, MessageParcel& reply)
230 {
231     uint32_t value = 0;
232     uint32_t displayId = 0;
233 
234     READ_PARCEL_WITH_RET(data, Uint32, value, E_READ_PARCEL_ERROR);
235     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
236 
237     bool ret = OverrideBrightness(value, displayId);
238     if (!reply.WriteBool(ret)) {
239         DISPLAY_HILOGE(COMP_SVC, "Failed to write OverrideBrightness return value");
240         return E_WRITE_PARCEL_ERROR;
241     }
242     return ERR_OK;
243 }
244 
OverrideDisplayOffDelayStub(MessageParcel & data,MessageParcel & reply)245 int32_t DisplayPowerMgrStub::OverrideDisplayOffDelayStub(MessageParcel& data, MessageParcel& reply)
246 {
247     uint32_t delayMs = 0;
248 
249     READ_PARCEL_WITH_RET(data, Uint32, delayMs, E_READ_PARCEL_ERROR);
250 
251     bool ret = OverrideDisplayOffDelay(delayMs);
252     if (!reply.WriteBool(ret)) {
253         DISPLAY_HILOGE(COMP_SVC, "Failed to write OverrideDisplayOffDelay return value");
254         return E_WRITE_PARCEL_ERROR;
255     }
256     return ERR_OK;
257 }
258 
RestoreBrightnessStub(MessageParcel & data,MessageParcel & reply)259 int32_t DisplayPowerMgrStub::RestoreBrightnessStub(MessageParcel& data, MessageParcel& reply)
260 {
261     uint32_t displayId = 0;
262 
263     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
264 
265     bool ret = RestoreBrightness(displayId);
266     if (!reply.WriteBool(ret)) {
267         DISPLAY_HILOGE(COMP_SVC, "Failed to write RestoreBrightness return value");
268         return E_WRITE_PARCEL_ERROR;
269     }
270     return ERR_OK;
271 }
272 
GetBrightnessStub(MessageParcel & data,MessageParcel & reply)273 int32_t DisplayPowerMgrStub::GetBrightnessStub(MessageParcel& data, MessageParcel& reply)
274 {
275     uint32_t displayId = 0;
276 
277     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
278 
279     uint32_t ret = GetBrightness(displayId);
280     if (!reply.WriteUint32(ret)) {
281         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetBrightness return value");
282         return E_WRITE_PARCEL_ERROR;
283     }
284     return ERR_OK;
285 }
286 
GetDefaultBrightnessStub(MessageParcel & data,MessageParcel & reply)287 int32_t DisplayPowerMgrStub::GetDefaultBrightnessStub(MessageParcel& data, MessageParcel& reply)
288 {
289     uint32_t ret = GetDefaultBrightness();
290     if (!reply.WriteUint32(ret)) {
291         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDefaultBrightness return value");
292         return E_WRITE_PARCEL_ERROR;
293     }
294     return ERR_OK;
295 }
296 
GetMaxBrightnessStub(MessageParcel & data,MessageParcel & reply)297 int32_t DisplayPowerMgrStub::GetMaxBrightnessStub(MessageParcel& data, MessageParcel& reply)
298 {
299     uint32_t ret = GetMaxBrightness();
300     if (!reply.WriteUint32(ret)) {
301         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetMaxBrightness return value");
302         return E_WRITE_PARCEL_ERROR;
303     }
304     return ERR_OK;
305 }
306 
GetMinBrightnessStub(MessageParcel & data,MessageParcel & reply)307 int32_t DisplayPowerMgrStub::GetMinBrightnessStub(MessageParcel& data, MessageParcel& reply)
308 {
309     uint32_t ret = GetMinBrightness();
310     if (!reply.WriteUint32(ret)) {
311         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetMinBrightness return value");
312         return E_WRITE_PARCEL_ERROR;
313     }
314     return ERR_OK;
315 }
316 
AdjustBrightnessStub(MessageParcel & data,MessageParcel & reply)317 int32_t DisplayPowerMgrStub::AdjustBrightnessStub(MessageParcel& data, MessageParcel& reply)
318 {
319     uint32_t id = 0;
320     int32_t value = 0;
321     uint32_t duration = 0;
322 
323     READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
324     READ_PARCEL_WITH_RET(data, Int32, value, E_READ_PARCEL_ERROR);
325     READ_PARCEL_WITH_RET(data, Uint32, duration, E_READ_PARCEL_ERROR);
326 
327     bool ret = AdjustBrightness(id, value, duration);
328     if (!reply.WriteBool(ret)) {
329         DISPLAY_HILOGE(COMP_SVC, "Failed to write AdjustBrightnessStub return value");
330         return E_WRITE_PARCEL_ERROR;
331     }
332     return ERR_OK;
333 }
334 
AutoAdjustBrightnessStub(MessageParcel & data,MessageParcel & reply)335 int32_t DisplayPowerMgrStub::AutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply)
336 {
337     bool enable = 0;
338 
339     READ_PARCEL_WITH_RET(data, Bool, enable, E_READ_PARCEL_ERROR);
340 
341     bool ret = AutoAdjustBrightness(enable);
342     if (!reply.WriteBool(ret)) {
343         DISPLAY_HILOGE(COMP_SVC, "Failed to write AutoAdjustBrightnessStub return value");
344         return E_WRITE_PARCEL_ERROR;
345     }
346     return ERR_OK;
347 }
348 
IsAutoAdjustBrightnessStub(MessageParcel & data,MessageParcel & reply)349 int32_t DisplayPowerMgrStub::IsAutoAdjustBrightnessStub(MessageParcel& data, MessageParcel& reply)
350 {
351     bool ret = IsAutoAdjustBrightness();
352     if (!reply.WriteBool(ret)) {
353         DISPLAY_HILOGE(COMP_SVC, "Failed to write IsAutoAdjustBrightnessStub return value");
354         return E_WRITE_PARCEL_ERROR;
355     }
356     return ERR_OK;
357 }
358 
RegisterCallbackStub(MessageParcel & data,MessageParcel & reply)359 int32_t DisplayPowerMgrStub::RegisterCallbackStub(MessageParcel& data, MessageParcel& reply)
360 {
361     sptr<IRemoteObject> obj = data.ReadRemoteObject();
362     RETURN_IF_WITH_RET((obj == nullptr), E_READ_PARCEL_ERROR);
363     sptr<IDisplayPowerCallback> callback = iface_cast<IDisplayPowerCallback>(obj);
364     RETURN_IF_WITH_RET((callback == nullptr), E_READ_PARCEL_ERROR);
365     bool isSucc = RegisterCallback(callback);
366     WRITE_PARCEL_WITH_RET(reply, Bool, isSucc, E_WRITE_PARCEL_ERROR);
367     return ERR_OK;
368 }
369 
BoostBrightnessStub(MessageParcel & data,MessageParcel & reply)370 int32_t DisplayPowerMgrStub::BoostBrightnessStub(MessageParcel& data, MessageParcel& reply)
371 {
372     int32_t timeoutMs = -1;
373     uint32_t id = 0;
374     READ_PARCEL_WITH_RET(data, Int32, timeoutMs, E_READ_PARCEL_ERROR);
375     READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
376 
377     bool isScuu = BoostBrightness(timeoutMs, id);
378     if (!reply.WriteBool(isScuu)) {
379         DISPLAY_HILOGW(COMP_SVC, "Failed to write BoostBrightness return value");
380         return E_WRITE_PARCEL_ERROR;
381     }
382     return ERR_OK;
383 }
384 
CancelBoostBrightnessStub(MessageParcel & data,MessageParcel & reply)385 int32_t DisplayPowerMgrStub::CancelBoostBrightnessStub(MessageParcel& data, MessageParcel& reply)
386 {
387     uint32_t displayId = 0;
388 
389     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
390 
391     bool isScuu = CancelBoostBrightness(displayId);
392     if (!reply.WriteBool(isScuu)) {
393         DISPLAY_HILOGW(COMP_SVC, "Failed to write CancelBoostBrightness return value");
394         return E_WRITE_PARCEL_ERROR;
395     }
396     return ERR_OK;
397 }
398 
GetDeviceBrightnessStub(MessageParcel & data,MessageParcel & reply)399 int32_t DisplayPowerMgrStub::GetDeviceBrightnessStub(MessageParcel& data, MessageParcel& reply)
400 {
401     uint32_t displayId = 0;
402 
403     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
404 
405     uint32_t ret = GetDeviceBrightness(displayId);
406     if (!reply.WriteUint32(ret)) {
407         DISPLAY_HILOGE(COMP_SVC, "Failed to write GetDeviceBrightness return value");
408         return E_WRITE_PARCEL_ERROR;
409     }
410     return ERR_OK;
411 }
412 
SetCoordinatedStub(MessageParcel & data,MessageParcel & reply)413 int32_t DisplayPowerMgrStub::SetCoordinatedStub(MessageParcel& data, MessageParcel& reply)
414 {
415     bool coordinated = false;
416     uint32_t displayId = 0;
417 
418     READ_PARCEL_WITH_RET(data, Bool, coordinated, E_READ_PARCEL_ERROR);
419     READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR);
420 
421     bool ret = SetCoordinated(coordinated, displayId);
422     if (!reply.WriteBool(ret)) {
423         DISPLAY_HILOGE(COMP_SVC, "Failed to write SetCoordinatedStub return value");
424         return E_WRITE_PARCEL_ERROR;
425     }
426     return ERR_OK;
427 }
428 } // namespace DisplayPowerMgr
429 } // namespace OHOS
430