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