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 "power_state_callback_stub.h"
17
18 #include <message_parcel.h>
19
20 #include "power_common.h"
21 #include "power_state_callback_ipc_interface_code.h"
22 #include "power_state_callback_proxy.h"
23 #include "xcollie/xcollie.h"
24
25 namespace OHOS {
26 namespace PowerMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)27 int PowerStateCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
28 MessageOption &option)
29 {
30 POWER_HILOGD(COMP_SVC, "cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
31 const int DFX_DELAY_S = 60;
32 int id = HiviewDFX::XCollie::GetInstance().SetTimer("PowerStateCallbackStub", DFX_DELAY_S, nullptr, nullptr,
33 HiviewDFX::XCOLLIE_FLAG_LOG);
34 std::u16string descripter = PowerStateCallbackStub::GetDescriptor();
35 std::u16string remoteDescripter = data.ReadInterfaceToken();
36 if (descripter != remoteDescripter) {
37 POWER_HILOGE(COMP_SVC, "Descriptor is not match");
38 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
39 return E_GET_POWER_SERVICE_FAILED;
40 }
41
42 int ret = ERR_OK;
43 if (code == static_cast<uint32_t>(PowerMgr::PowerStateCallbackInterfaceCode::POWER_STATE_CHANGED)) {
44 ret = OnPowerStateChangedStub(data);
45 } else if (code == static_cast<uint32_t>(PowerMgr::PowerStateCallbackInterfaceCode::ASYNC_POWER_STATE_CHANGED)) {
46 ret = OnAsyncPowerStateChangedStub(data);
47 } else {
48 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49 }
50 HiviewDFX::XCollie::GetInstance().CancelTimer(id);
51 return ret;
52 }
53
OnPowerStateChangedStub(MessageParcel & data)54 int32_t PowerStateCallbackStub::OnPowerStateChangedStub(MessageParcel& data)
55 {
56 uint32_t type;
57 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
58 OnPowerStateChanged(static_cast<PowerState>(type));
59 return ERR_OK;
60 }
61
OnAsyncPowerStateChangedStub(MessageParcel & data)62 int32_t PowerStateCallbackStub::OnAsyncPowerStateChangedStub(MessageParcel& data)
63 {
64 uint32_t type;
65 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, type, E_READ_PARCEL_ERROR);
66 OnAsyncPowerStateChanged(static_cast<PowerState>(type));
67 return ERR_OK;
68 }
69 } // namespace PowerMgr
70 } // namespace OHOS
71