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/async_shutdown_callback_stub.h"
17 
18 #include "message_parcel.h"
19 #include "power_common.h"
20 #include "shutdown/async_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 AsyncShutdownCallbackStub::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     int32_t id = HiviewDFX::XCollie::GetInstance().SetTimer(
32         "PowerShutdownCallback", DFX_DELAY_S, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_NOOP);
33     std::u16string descripter = AsyncShutdownCallbackStub::GetDescriptor();
34     std::u16string remoteDescripter = data.ReadInterfaceToken();
35     if (descripter != remoteDescripter) {
36         POWER_HILOGE(COMP_SVC, "Descriptor is not match");
37         HiviewDFX::XCollie::GetInstance().CancelTimer(id);
38         return E_GET_POWER_SERVICE_FAILED;
39     }
40 
41     int32_t ret = ERR_OK;
42     if (code == static_cast<uint32_t>(PowerMgr::AsyncShutdownCallbackInterfaceCode::CMD_ON_ASYNC_SHUTDOWN_OR_REBOOT)) {
43         ret = OnAsyncShutdownOrRebootCallbackStub(data, reply);
44     } else if (code == static_cast<uint32_t>(PowerMgr::AsyncShutdownCallbackInterfaceCode::CMD_ON_ASYNC_SHUTDOWN)) {
45         ret = OnAsyncShutdownCallbackStub(data, reply);
46     } else {
47         ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48     }
49     HiviewDFX::XCollie::GetInstance().CancelTimer(id);
50     return ret;
51 }
52 
OnAsyncShutdownCallbackStub(MessageParcel & data,MessageParcel & reply)53 int32_t AsyncShutdownCallbackStub::OnAsyncShutdownCallbackStub(MessageParcel& data, MessageParcel& reply)
54 {
55     OnAsyncShutdown();
56     return ERR_OK;
57 }
58 
OnAsyncShutdownOrRebootCallbackStub(MessageParcel & data,MessageParcel & reply)59 int32_t AsyncShutdownCallbackStub::OnAsyncShutdownOrRebootCallbackStub(MessageParcel& data, MessageParcel& reply)
60 {
61     bool isReboot;
62     RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, isReboot, E_READ_PARCEL_ERROR);
63     OnAsyncShutdownOrReboot(isReboot);
64     return ERR_OK;
65 }
66 
67 } // namespace PowerMgr
68 } // namespace OHOS
69