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_callback_stub.h"
17 
18 #include <message_parcel.h>
19 #include "errors.h"
20 #include "display_common.h"
21 #include "display_log.h"
22 #include "display_mgr_errors.h"
23 #include "display_power_info.h"
24 #include "display_power_callback_ipc_interface_code.h"
25 #include "idisplay_power_callback.h"
26 #include "ipc_object_stub.h"
27 #include "message_option.h"
28 #include "xcollie/xcollie.h"
29 #include "xcollie/xcollie_define.h"
30 
31 namespace OHOS {
32 namespace DisplayPowerMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t DisplayPowerCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34     MessageOption &option)
35 {
36     DISPLAY_HILOGD(COMP_SVC, "DisplayPowerCallbackStub::OnRemoteRequest, cmd = %d, flags= %d",
37         code, option.GetFlags());
38     std::u16string descripter = DisplayPowerCallbackStub::GetDescriptor();
39     std::u16string remoteDescripter = data.ReadInterfaceToken();
40     if (descripter != remoteDescripter) {
41         DISPLAY_HILOGE(COMP_SVC, "descriptor is not matched!");
42         return E_GET_POWER_SERVICE_FAILED;
43     }
44 
45     const int DFX_DELAY_S = 60;
46     int id = HiviewDFX::XCollie::GetInstance().SetTimer("DisplayPowerCallbackStub", DFX_DELAY_S, nullptr, nullptr,
47         HiviewDFX::XCOLLIE_FLAG_LOG);
48     int32_t ret = ERR_OK;
49     if (code == static_cast<uint32_t>(PowerMgr::DisplayPowerCallbackInterfaceCode::ON_DISPLAY_STATE_CHANGED)) {
50         ret = OnDisplayStateChangedStub(data, reply);
51     } else {
52         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
53     }
54     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
55     return ret;
56 }
57 
OnDisplayStateChangedStub(MessageParcel & data,MessageParcel & reply)58 int32_t DisplayPowerCallbackStub::OnDisplayStateChangedStub(MessageParcel& data, MessageParcel& reply)
59 {
60     uint32_t id = 0;
61     uint32_t state = 0;
62     uint32_t reason = 0;
63 
64     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR);
65     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR);
66     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR);
67 
68     OnDisplayStateChanged(id, static_cast<DisplayState>(state), reason);
69     return ERR_OK;
70 }
71 } // namespace DisplayPowerMgr
72 } // namespace OHOS
73