1 /*
2  * Copyright (c) 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 "shutdown/takeover_shutdown_callback_stub.h"
17 
18 #include "message_parcel.h"
19 #include "power_common.h"
20 #include "shutdown/takeover_shutdown_callback_ipc_interface_code.h"
21 #include "xcollie/xcollie.h"
22 #include "xcollie/xcollie_define.h"
23 
24 namespace OHOS {
25 namespace PowerMgr {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)26 int TakeOverShutdownCallbackStub::OnRemoteRequest(
27     uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
28 {
29     POWER_HILOGD(FEATURE_SHUTDOWN, "cmd=%{public}d, flags=%{public}d", code, option.GetFlags());
30     const int32_t DFX_DELAY_S = 60;
31     const int32_t INVALID_ID = 0;
32     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(
33         "PowerShutdownCallback", DFX_DELAY_S, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
34     if (id <= INVALID_ID) {
35         POWER_HILOGE(COMP_SVC, "SetTimer failed");
36     }
37     std::u16string descripter = TakeOverShutdownCallbackStub::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     int32_t ret = ERR_OK;
46     if (code == static_cast<uint32_t>(PowerMgr::TakeoverShutdownCallbackInterfaceCode::CMD_ON_TAKEOVER_SHUTDOWN)) {
47         ret = OnTakeOverShutdownCallbackStub(data, reply);
48     } else {
49         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50     }
51     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
52     return ret;
53 }
54 
OnTakeOverShutdownCallbackStub(MessageParcel & data,MessageParcel & reply)55 int32_t TakeOverShutdownCallbackStub::OnTakeOverShutdownCallbackStub(MessageParcel& data, MessageParcel& reply)
56 {
57     bool isReboot;
58     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isReboot, E_READ_PARCEL_ERROR);
59     int32_t isTakeOver = OnTakeOverShutdown(isReboot);
60     RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Bool, isTakeOver, E_WRITE_PARCEL_ERROR);
61     return ERR_OK;
62 }
63 
64 } // namespace PowerMgr
65 } // namespace OHOS
66