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 "net_conn_callback_proxy.h"
17 #include "net_conn_constants.h"
18 #include "net_mgr_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
NetConnCallbackProxy(const sptr<IRemoteObject> & impl)22 NetConnCallbackProxy::NetConnCallbackProxy(const sptr<IRemoteObject> &impl)
23     : IRemoteProxy<INetConnCallback>(impl)
24 {}
25 
~NetConnCallbackProxy()26 NetConnCallbackProxy::~NetConnCallbackProxy() {}
27 
NetAvailable(sptr<NetHandle> & netHandle)28 int32_t NetConnCallbackProxy::NetAvailable(sptr<NetHandle> &netHandle)
29 {
30     if (netHandle == nullptr) {
31         NETMGR_LOG_E("netHandle is null");
32         return NETMANAGER_ERR_LOCAL_PTR_NULL;
33     }
34 
35     MessageParcel data;
36     if (!WriteInterfaceToken(data)) {
37         NETMGR_LOG_E("WriteInterfaceToken failed");
38         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
39     }
40 
41     if (!data.WriteInt32(netHandle->GetNetId())) {
42         return NETMANAGER_ERR_WRITE_DATA_FAIL;
43     }
44 
45     sptr<IRemoteObject> remote = Remote();
46     if (remote == nullptr) {
47         NETMGR_LOG_E("Remote is null");
48         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
49     }
50 
51     MessageParcel reply;
52     MessageOption option;
53     option.SetFlags(MessageOption::TF_ASYNC);
54     int32_t ret = remote->SendRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_AVAILABLE),
55                                       data, reply, option);
56     if (ret != ERR_NONE) {
57         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
58     }
59     return ret;
60 }
61 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)62 int32_t NetConnCallbackProxy::NetCapabilitiesChange(
63     sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
64 {
65     MessageParcel data;
66     if (!WriteInterfaceToken(data)) {
67         NETMGR_LOG_E("WriteInterfaceToken failed");
68         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
69     }
70 
71     if (netHandle == nullptr || netAllCap == nullptr) {
72         return NETMANAGER_ERR_LOCAL_PTR_NULL;
73     }
74 
75     if (!data.WriteInt32(netHandle->GetNetId()) || !data.WriteUint32(netAllCap->linkUpBandwidthKbps_) ||
76         !data.WriteUint32(netAllCap->linkDownBandwidthKbps_)) {
77         return NETMANAGER_ERR_WRITE_DATA_FAIL;
78     }
79     uint32_t size = static_cast<uint32_t>(netAllCap->netCaps_.size());
80     if (!data.WriteUint32(size)) {
81         return NETMANAGER_ERR_WRITE_DATA_FAIL;
82     }
83     for (auto netCap : netAllCap->netCaps_) {
84         if (!data.WriteUint32(static_cast<uint32_t>(netCap))) {
85             return NETMANAGER_ERR_WRITE_DATA_FAIL;
86         }
87     }
88     size = static_cast<uint32_t>(netAllCap->bearerTypes_.size());
89     if (!data.WriteUint32(size)) {
90         return NETMANAGER_ERR_WRITE_DATA_FAIL;
91     }
92     for (auto bearerType : netAllCap->bearerTypes_) {
93         if (!data.WriteUint32(static_cast<uint32_t>(bearerType))) {
94             return NETMANAGER_ERR_WRITE_DATA_FAIL;
95         }
96     }
97 
98     sptr<IRemoteObject> remote = Remote();
99     if (remote == nullptr) {
100         NETMGR_LOG_E("Remote is null");
101         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
102     }
103 
104     MessageParcel reply;
105     MessageOption option;
106     option.SetFlags(MessageOption::TF_ASYNC);
107     int32_t ret = remote->SendRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_CAPABILITIES_CHANGE),
108                                       data, reply, option);
109     if (ret != ERR_NONE) {
110         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
111     }
112     return ret;
113 }
114 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)115 int32_t NetConnCallbackProxy::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
116 {
117     if (netHandle == nullptr || info == nullptr) {
118         NETMGR_LOG_E("Input parameter is null");
119         return NETMANAGER_ERR_LOCAL_PTR_NULL;
120     }
121 
122     MessageParcel data;
123     if (!WriteInterfaceToken(data)) {
124         NETMGR_LOG_E("WriteInterfaceToken failed");
125         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
126     }
127 
128     if (!data.WriteInt32(netHandle->GetNetId())) {
129         return NETMANAGER_ERR_WRITE_DATA_FAIL;
130     }
131 
132     if (!info->Marshalling(data)) {
133         NETMGR_LOG_E("proxy Marshalling failed");
134         return NETMANAGER_ERR_WRITE_DATA_FAIL;
135     }
136 
137     sptr<IRemoteObject> remote = Remote();
138     if (remote == nullptr) {
139         NETMGR_LOG_E("Remote is null");
140         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
141     }
142 
143     MessageParcel reply;
144     MessageOption option;
145     option.SetFlags(MessageOption::TF_ASYNC);
146     int32_t ret =
147         remote->SendRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_CONNECTION_PROPERTIES_CHANGE),
148                             data, reply, option);
149     if (ret != ERR_NONE) {
150         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
151     }
152     return ret;
153 }
154 
NetLost(sptr<NetHandle> & netHandle)155 int32_t NetConnCallbackProxy::NetLost(sptr<NetHandle> &netHandle)
156 {
157     if (netHandle == nullptr) {
158         NETMGR_LOG_E("netHandle is null");
159         return NETMANAGER_ERR_LOCAL_PTR_NULL;
160     }
161 
162     MessageParcel data;
163     if (!WriteInterfaceToken(data)) {
164         NETMGR_LOG_E("WriteInterfaceToken failed");
165         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
166     }
167 
168     if (!data.WriteInt32(netHandle->GetNetId())) {
169         return NETMANAGER_ERR_WRITE_DATA_FAIL;
170     }
171 
172     sptr<IRemoteObject> remote = Remote();
173     if (remote == nullptr) {
174         NETMGR_LOG_E("Remote is null");
175         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
176     }
177 
178     MessageParcel reply;
179     MessageOption option;
180     option.SetFlags(MessageOption::TF_ASYNC);
181     int32_t ret = remote->SendRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_LOST),
182                                       data, reply, option);
183     if (ret != ERR_NONE) {
184         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
185     }
186     return ret;
187 }
188 
NetUnavailable()189 int32_t NetConnCallbackProxy::NetUnavailable()
190 {
191     MessageParcel data;
192     if (!WriteInterfaceToken(data)) {
193         NETMGR_LOG_E("WriteInterfaceToken failed");
194         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
195     }
196 
197     sptr<IRemoteObject> remote = Remote();
198     if (remote == nullptr) {
199         NETMGR_LOG_E("Remote is null");
200         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
201     }
202 
203     MessageParcel reply;
204     MessageOption option;
205     option.SetFlags(MessageOption::TF_ASYNC);
206     int32_t ret = remote->SendRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_UNAVAILABLE),
207                                       data, reply, option);
208     if (ret != ERR_NONE) {
209         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
210     }
211     return ret;
212 }
213 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)214 int32_t NetConnCallbackProxy::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
215 {
216     if (netHandle == nullptr) {
217         NETMGR_LOG_E("netHandle is null");
218         return NETMANAGER_ERR_LOCAL_PTR_NULL;
219     }
220 
221     MessageParcel data;
222     if (!WriteInterfaceToken(data)) {
223         NETMGR_LOG_E("WriteInterfaceToken failed");
224         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
225     }
226 
227     if (!data.WriteInt32(netHandle->GetNetId())) {
228         return NETMANAGER_ERR_WRITE_DATA_FAIL;
229     }
230     if (!data.WriteBool(blocked)) {
231         return NETMANAGER_ERR_WRITE_DATA_FAIL;
232     }
233 
234     sptr<IRemoteObject> remote = Remote();
235     if (remote == nullptr) {
236         NETMGR_LOG_E("Remote is null");
237         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
238     }
239     MessageParcel reply;
240     MessageOption option;
241     option.SetFlags(MessageOption::TF_ASYNC);
242     int32_t ret = remote->SendRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_BLOCK_STATUS_CHANGE),
243                                       data, reply, option);
244     if (ret != ERR_NONE) {
245         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
246     }
247     return ret;
248 }
249 
WriteInterfaceToken(MessageParcel & data)250 bool NetConnCallbackProxy::WriteInterfaceToken(MessageParcel &data)
251 {
252     if (!data.WriteInterfaceToken(NetConnCallbackProxy::GetDescriptor())) {
253         NETMGR_LOG_E("WriteInterfaceToken failed");
254         return false;
255     }
256     return true;
257 }
258 
PreAirplaneCallbackProxy(const sptr<IRemoteObject> & impl)259 PreAirplaneCallbackProxy::PreAirplaneCallbackProxy(const sptr<IRemoteObject> &impl)
260     : IRemoteProxy<IPreAirplaneCallback>(impl)
261 {}
262 
PreAirplaneStart()263 int32_t PreAirplaneCallbackProxy::PreAirplaneStart()
264 {
265     NETMGR_LOG_I("PreAirplaneCallbackProxy::PreAirplaneStart()");
266     MessageParcel data;
267     if (!WriteInterfaceToken(data)) {
268         NETMGR_LOG_E("WriteInterfaceToken failed");
269         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
270     }
271 
272     sptr<IRemoteObject> remote = Remote();
273     if (remote == nullptr) {
274         NETMGR_LOG_E("Remote is null");
275         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
276     }
277 
278     MessageParcel reply;
279     MessageOption option;
280     option.SetFlags(MessageOption::TF_ASYNC);
281     int32_t ret = remote->SendRequest(static_cast<uint32_t>(PreAirplaneCallbackInterfaceCode::PRE_AIRPLANE_START),
282                                       data, reply, option);
283     if (ret != ERR_NONE) {
284         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
285     }
286     return ret;
287 }
288 
WriteInterfaceToken(MessageParcel & data)289 bool PreAirplaneCallbackProxy::WriteInterfaceToken(MessageParcel &data)
290 {
291     if (!data.WriteInterfaceToken(PreAirplaneCallbackProxy::GetDescriptor())) {
292         NETMGR_LOG_E("WriteInterfaceToken failed");
293         return false;
294     }
295     return true;
296 }
297 } // namespace NetManagerStandard
298 } // namespace OHOS
299