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_mode_callback_stub.h"
17 
18 #include <message_parcel.h>
19 #include "errors.h"
20 #include "ipc_object_stub.h"
21 #include "power_common.h"
22 #include "power_log.h"
23 #include "power_mgr_errors.h"
24 #include "power_mode_callback_ipc_interface_code.h"
25 #include "xcollie/xcollie.h"
26 #include "xcollie/xcollie_define.h"
27 
28 namespace OHOS {
29 namespace PowerMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int PowerModeCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
31     MessageOption& option)
32 {
33     POWER_HILOGD(COMP_SVC, "cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
34     const int DFX_DELAY_S = 60;
35     int id = HiviewDFX::XCollie::GetInstance().SetTimer("PowerModeCallbackStub", DFX_DELAY_S, nullptr, nullptr,
36         HiviewDFX::XCOLLIE_FLAG_LOG);
37     std::u16string descripter = PowerModeCallbackStub::GetDescriptor();
38     std::u16string remoteDescripter = data.ReadInterfaceToken();
39     if (descripter != remoteDescripter) {
40         POWER_HILOGE(COMP_SVC, "Descriptor is not match");
41         HiviewDFX::XCollie::GetInstance().CancelTimer(id);
42         return E_GET_POWER_SERVICE_FAILED;
43     }
44 
45     int ret = ERR_OK;
46     if (code == static_cast<uint32_t>(PowerMgr::PowerModeCallbackInterfaceCode::POWER_MODE_CHANGED)) {
47         ret = OnPowerModeCallbackStub(data);
48     } else {
49         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50     }
51     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
52     return ret;
53 }
54 
OnPowerModeCallbackStub(MessageParcel & data)55 int32_t PowerModeCallbackStub::OnPowerModeCallbackStub(MessageParcel& data)
56 {
57     uint32_t mode;
58     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, mode, E_READ_PARCEL_ERROR);
59     OnPowerModeChanged(static_cast<PowerMode>(mode));
60     return ERR_OK;
61 }
62 } // namespace PowerMgr
63 } // namespace OHOS
64