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_proxy_delegator.h"
17 
18 #include "power_common.h"
19 #include "shutdown/shutdown_client_ipc_interface_code.h"
20 
21 namespace OHOS {
22 namespace PowerMgr {
RegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback,ShutdownPriority priority)23 void ShutdownProxyDelegator::RegisterShutdownCallback(
24     const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority)
25 {
26     RETURN_IF((remote_ == nullptr) || (callback == nullptr))
27     MessageParcel data;
28     MessageParcel reply;
29     MessageOption option;
30 
31     if (!data.WriteInterfaceToken(descriptor_)) {
32         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
33         return;
34     }
35 
36     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, RemoteObject, callback->AsObject());
37     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, Uint32, static_cast<uint32_t>(priority));
38 
39     int ret = remote_->SendRequest(
40         static_cast<int>(PowerMgr::ShutdownClientInterfaceCode::CMD_REG_TAKEOVER_SHUTDOWN_CALLBACK),
41         data, reply, option);
42     if (ret != ERR_OK) {
43         POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret=%{public}d", ret);
44     }
45 }
46 
UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback> & callback)47 void ShutdownProxyDelegator::UnRegisterShutdownCallback(const sptr<ITakeOverShutdownCallback>& callback)
48 {
49     RETURN_IF((remote_ == nullptr) || (callback == nullptr))
50     MessageParcel data;
51     MessageParcel reply;
52     MessageOption option;
53 
54     if (!data.WriteInterfaceToken(descriptor_)) {
55         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
56         return;
57     }
58 
59     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, RemoteObject, callback->AsObject());
60 
61     int ret = remote_->SendRequest(
62         static_cast<int>(PowerMgr::ShutdownClientInterfaceCode::CMD_UNREG_TAKEOVER_SHUTDOWN_CALLBACK),
63         data, reply, option);
64     if (ret != ERR_OK) {
65         POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret=%{public}d", ret);
66     }
67 }
68 
RegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback,ShutdownPriority priority)69 void ShutdownProxyDelegator::RegisterShutdownCallback(
70     const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority)
71 {
72     RETURN_IF((remote_ == nullptr) || (callback == nullptr))
73     MessageParcel data;
74     MessageParcel reply;
75     MessageOption option;
76 
77     if (!data.WriteInterfaceToken(descriptor_)) {
78         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
79         return;
80     }
81 
82     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, RemoteObject, callback->AsObject());
83     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, Uint32, static_cast<uint32_t>(priority));
84 
85     int ret = remote_->SendRequest(
86         static_cast<int>(PowerMgr::ShutdownClientInterfaceCode::CMD_REG_ASYNC_SHUTDOWN_CALLBACK),
87         data, reply, option);
88     if (ret != ERR_OK) {
89         POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret=%{public}d", ret);
90     }
91 }
92 
UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback> & callback)93 void ShutdownProxyDelegator::UnRegisterShutdownCallback(const sptr<IAsyncShutdownCallback>& callback)
94 {
95     RETURN_IF((remote_ == nullptr) || (callback == nullptr))
96     MessageParcel data;
97     MessageParcel reply;
98     MessageOption option;
99 
100     if (!data.WriteInterfaceToken(descriptor_)) {
101         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
102         return;
103     }
104 
105     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, RemoteObject, callback->AsObject());
106 
107     int ret = remote_->SendRequest(
108         static_cast<int>(PowerMgr::ShutdownClientInterfaceCode::CMD_UNREG_ASYNC_SHUTDOWN_CALLBACK),
109         data, reply, option);
110     if (ret != ERR_OK) {
111         POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret=%{public}d", ret);
112     }
113 }
114 
RegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback,ShutdownPriority priority)115 void ShutdownProxyDelegator::RegisterShutdownCallback(
116     const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority)
117 {
118     RETURN_IF((remote_ == nullptr) || (callback == nullptr))
119     MessageParcel data;
120     MessageParcel reply;
121     MessageOption option;
122 
123     if (!data.WriteInterfaceToken(descriptor_)) {
124         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
125         return;
126     }
127 
128     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, RemoteObject, callback->AsObject());
129     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, Uint32, static_cast<uint32_t>(priority));
130 
131     int ret = remote_->SendRequest(
132         static_cast<int>(PowerMgr::ShutdownClientInterfaceCode::CMD_REG_SYNC_SHUTDOWN_CALLBACK),
133         data, reply, option);
134     if (ret != ERR_OK) {
135         POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret=%{public}d", ret);
136     }
137 }
138 
UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback> & callback)139 void ShutdownProxyDelegator::UnRegisterShutdownCallback(const sptr<ISyncShutdownCallback>& callback)
140 {
141     RETURN_IF((remote_ == nullptr) || (callback == nullptr))
142     MessageParcel data;
143     MessageParcel reply;
144     MessageOption option;
145 
146     if (!data.WriteInterfaceToken(descriptor_)) {
147         POWER_HILOGE(FEATURE_SHUTDOWN, "Write descriptor failed");
148         return;
149     }
150 
151     RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(data, RemoteObject, callback->AsObject());
152 
153     int ret = remote_->SendRequest(
154         static_cast<int>(PowerMgr::ShutdownClientInterfaceCode::CMD_UNREG_SYNC_SHUTDOWN_CALLBACK),
155         data, reply, option);
156     if (ret != ERR_OK) {
157         POWER_HILOGE(FEATURE_SHUTDOWN, "SendRequest is failed, ret=%{public}d", ret);
158     }
159 }
160 } // namespace PowerMgr
161 } // namespace OHOS
162