• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "net_conn_service_proxy.h"
16 
17 #include "net_conn_constants.h"
18 #include "net_manager_constants.h"
19 #include "net_mgr_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
23 static constexpr uint32_t MAX_IFACE_NUM = 16;
24 static constexpr uint32_t MAX_NET_CAP_NUM = 32;
25 
NetConnServiceProxy(const sptr<IRemoteObject> & impl)26 NetConnServiceProxy::NetConnServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<INetConnService>(impl) {}
27 
~NetConnServiceProxy()28 NetConnServiceProxy::~NetConnServiceProxy() {}
29 
SystemReady()30 int32_t NetConnServiceProxy::SystemReady()
31 {
32     MessageParcel data;
33     MessageParcel reply;
34     MessageOption option;
35     if (!WriteInterfaceToken(data)) {
36         NETMGR_LOG_E("WriteInterfaceToken failed");
37         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
38     }
39 
40     sptr<IRemoteObject> remote = Remote();
41     if (remote == nullptr) {
42         NETMGR_LOG_E("Remote is null");
43         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
44     }
45     int32_t error =
46         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SYSTEM_READY), data, reply, option);
47     if (error != ERR_NONE) {
48         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
49         return NETMANAGER_ERR_OPERATION_FAILED;
50     }
51     return NETMANAGER_SUCCESS;
52 }
53 
SetInternetPermission(uint32_t uid,uint8_t allow)54 int32_t NetConnServiceProxy::SetInternetPermission(uint32_t uid, uint8_t allow)
55 {
56     MessageParcel data;
57     MessageParcel reply;
58     MessageOption option;
59     if (!WriteInterfaceToken(data)) {
60         NETMGR_LOG_E("WriteInterfaceToken failed");
61         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
62     }
63 
64     NETMGR_LOG_D("proxy SetInternetPermission [%{public}u %{public}hhu]", uid, allow);
65     if (!data.WriteUint32(uid)) {
66         return NETMANAGER_ERR_WRITE_DATA_FAIL;
67     }
68     if (!data.WriteUint8(allow)) {
69         return NETMANAGER_ERR_WRITE_DATA_FAIL;
70     }
71     sptr<IRemoteObject> remote = Remote();
72     if (remote == nullptr) {
73         NETMGR_LOG_E("Remote is null");
74         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
75     }
76     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_INTERNET_PERMISSION), data,
77                                         reply, option);
78     if (error != ERR_NONE) {
79         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
80         return NETMANAGER_ERR_OPERATION_FAILED;
81     }
82 
83     return reply.ReadInt32();
84 }
85 
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)86 int32_t NetConnServiceProxy::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
87                                                  const std::set<NetCap> &netCaps, uint32_t &supplierId)
88 {
89     MessageParcel data;
90     MessageParcel reply;
91     MessageOption option;
92     if (!WriteInterfaceToken(data)) {
93         NETMGR_LOG_E("WriteInterfaceToken failed");
94         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
95     }
96 
97     if (!data.WriteUint32(static_cast<uint32_t>(bearerType))) {
98         return NETMANAGER_ERR_WRITE_DATA_FAIL;
99     }
100 
101     if (!data.WriteString(ident)) {
102         return NETMANAGER_ERR_WRITE_DATA_FAIL;
103     }
104 
105     uint32_t size = static_cast<uint32_t>(netCaps.size());
106     if (!data.WriteUint32(size)) {
107         return NETMANAGER_ERR_WRITE_DATA_FAIL;
108     }
109     for (auto netCap : netCaps) {
110         if (!data.WriteUint32(static_cast<uint32_t>(netCap))) {
111             return NETMANAGER_ERR_WRITE_DATA_FAIL;
112         }
113     }
114 
115     sptr<IRemoteObject> remote = Remote();
116     if (remote == nullptr) {
117         NETMGR_LOG_E("Remote is null");
118         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
119     }
120     int32_t error =
121         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REG_NET_SUPPLIER), data, reply, option);
122     if (error != ERR_NONE) {
123         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
124         return NETMANAGER_ERR_OPERATION_FAILED;
125     }
126 
127     int32_t ret;
128     if (!reply.ReadInt32(ret)) {
129         return NETMANAGER_ERR_READ_REPLY_FAIL;
130     }
131     if (ret == NETMANAGER_SUCCESS) {
132         if (!reply.ReadUint32(supplierId)) {
133             return NETMANAGER_ERR_READ_REPLY_FAIL;
134         }
135     }
136     return ret;
137 }
138 
UnregisterNetSupplier(uint32_t supplierId)139 int32_t NetConnServiceProxy::UnregisterNetSupplier(uint32_t supplierId)
140 {
141     MessageParcel data;
142     MessageParcel reply;
143     MessageOption option;
144     if (!WriteInterfaceToken(data)) {
145         NETMGR_LOG_E("WriteInterfaceToken failed");
146         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
147     }
148 
149     NETMGR_LOG_D("proxy supplierId[%{public}d]", supplierId);
150     if (!data.WriteUint32(supplierId)) {
151         return NETMANAGER_ERR_WRITE_DATA_FAIL;
152     }
153     sptr<IRemoteObject> remote = Remote();
154     if (remote == nullptr) {
155         NETMGR_LOG_E("Remote is null");
156         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
157     }
158     int32_t error =
159         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UNREG_NETWORK), data, reply, option);
160     if (error != ERR_NONE) {
161         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
162         return NETMANAGER_ERR_OPERATION_FAILED;
163     }
164 
165     return reply.ReadInt32();
166 }
167 
RegisterNetSupplierCallback(uint32_t supplierId,const sptr<INetSupplierCallback> & callback)168 int32_t NetConnServiceProxy::RegisterNetSupplierCallback(uint32_t supplierId,
169                                                          const sptr<INetSupplierCallback> &callback)
170 {
171     if (callback == nullptr) {
172         NETMGR_LOG_E("The parameter of callback is nullptr");
173         return NETMANAGER_ERR_LOCAL_PTR_NULL;
174     }
175 
176     MessageParcel dataParcel;
177     if (!WriteInterfaceToken(dataParcel)) {
178         NETMGR_LOG_E("WriteInterfaceToken failed");
179         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
180     }
181     dataParcel.WriteUint32(supplierId);
182     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
183 
184     sptr<IRemoteObject> remote = Remote();
185     if (remote == nullptr) {
186         NETMGR_LOG_E("Remote is null");
187         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
188     }
189 
190     MessageOption option;
191     MessageParcel replyParcel;
192     int32_t retCode =
193         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_SUPPLIER_CALLBACK), dataParcel,
194                             replyParcel, option);
195     NETMGR_LOG_I("SendRequest retCode:[%{public}d]", retCode);
196     if (retCode != ERR_NONE) {
197         return NETMANAGER_ERR_OPERATION_FAILED;
198     }
199     return replyParcel.ReadInt32();
200 }
201 
RegisterNetConnCallback(const sptr<INetConnCallback> & callback)202 int32_t NetConnServiceProxy::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
203 {
204     if (callback == nullptr) {
205         NETMGR_LOG_E("The parameter of callback is nullptr");
206         return NETMANAGER_ERR_LOCAL_PTR_NULL;
207     }
208 
209     MessageParcel dataParcel;
210     if (!WriteInterfaceToken(dataParcel)) {
211         NETMGR_LOG_E("WriteInterfaceToken failed");
212         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
213     }
214     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
215 
216     sptr<IRemoteObject> remote = Remote();
217     if (remote == nullptr) {
218         NETMGR_LOG_E("Remote is null");
219         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
220     }
221 
222     MessageOption option;
223     MessageParcel replyParcel;
224     int32_t retCode = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_CONN_CALLBACK),
225                                           dataParcel, replyParcel, option);
226     NETMGR_LOG_D("SendRequest retCode:[%{public}d]", retCode);
227     if (retCode != ERR_NONE) {
228         return NETMANAGER_ERR_OPERATION_FAILED;
229     }
230     return replyParcel.ReadInt32();
231 }
232 
RegisterNetConnCallback(const sptr<NetSpecifier> & netSpecifier,const sptr<INetConnCallback> & callback,const uint32_t & timeoutMS)233 int32_t NetConnServiceProxy::RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier,
234                                                      const sptr<INetConnCallback> &callback, const uint32_t &timeoutMS)
235 {
236     if (netSpecifier == nullptr || callback == nullptr) {
237         NETMGR_LOG_E("The parameter of netSpecifier or callback is nullptr");
238         return NETMANAGER_ERR_LOCAL_PTR_NULL;
239     }
240 
241     MessageParcel dataParcel;
242     if (!WriteInterfaceToken(dataParcel)) {
243         NETMGR_LOG_E("WriteInterfaceToken failed");
244         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
245     }
246     netSpecifier->Marshalling(dataParcel);
247     dataParcel.WriteUint32(timeoutMS);
248     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
249 
250     sptr<IRemoteObject> remote = Remote();
251     if (remote == nullptr) {
252         NETMGR_LOG_E("Remote is null");
253         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
254     }
255 
256     MessageOption option;
257     MessageParcel replyParcel;
258     int32_t retCode =
259         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_CONN_CALLBACK_BY_SPECIFIER),
260                             dataParcel, replyParcel, option);
261     NETMGR_LOG_D("SendRequest retCode:[%{public}d]", retCode);
262     if (retCode != ERR_NONE) {
263         return NETMANAGER_ERR_OPERATION_FAILED;
264     }
265     return replyParcel.ReadInt32();
266 }
267 
UnregisterNetConnCallback(const sptr<INetConnCallback> & callback)268 int32_t NetConnServiceProxy::UnregisterNetConnCallback(const sptr<INetConnCallback> &callback)
269 {
270     if (callback == nullptr) {
271         NETMGR_LOG_E("The parameter of callback is nullptr");
272         return NETMANAGER_ERR_LOCAL_PTR_NULL;
273     }
274 
275     MessageParcel dataParcel;
276     if (!WriteInterfaceToken(dataParcel)) {
277         NETMGR_LOG_E("WriteInterfaceToken failed");
278         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
279     }
280     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
281 
282     sptr<IRemoteObject> remote = Remote();
283     if (remote == nullptr) {
284         NETMGR_LOG_E("Remote is null");
285         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
286     }
287 
288     MessageOption option;
289     MessageParcel replyParcel;
290     int32_t retCode = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UNREGISTER_NET_CONN_CALLBACK),
291                                           dataParcel, replyParcel, option);
292     NETMGR_LOG_D("SendRequest retCode:[%{public}d]", retCode);
293     if (retCode != ERR_NONE) {
294         return NETMANAGER_ERR_OPERATION_FAILED;
295     }
296     return replyParcel.ReadInt32();
297 }
298 
UpdateNetStateForTest(const sptr<NetSpecifier> & netSpecifier,int32_t netState)299 int32_t NetConnServiceProxy::UpdateNetStateForTest(const sptr<NetSpecifier> &netSpecifier, int32_t netState)
300 {
301     NETMGR_LOG_I("Test NetConnServiceProxy::UpdateNetStateForTest(), begin");
302     if (netSpecifier == nullptr) {
303         NETMGR_LOG_E("The parameter of netSpecifier is nullptr");
304         return NETMANAGER_ERR_LOCAL_PTR_NULL;
305     }
306 
307     MessageParcel dataParcel;
308     if (!WriteInterfaceToken(dataParcel)) {
309         NETMGR_LOG_E("WriteInterfaceToken failed");
310         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
311     }
312     netSpecifier->Marshalling(dataParcel);
313 
314     if (!dataParcel.WriteInt32(netState)) {
315         return NETMANAGER_ERR_WRITE_DATA_FAIL;
316     }
317 
318     sptr<IRemoteObject> remote = Remote();
319     if (remote == nullptr) {
320         NETMGR_LOG_E("Remote is null");
321         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
322     }
323 
324     MessageOption option;
325     MessageParcel replyParcel;
326     int32_t retCode = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UPDATE_NET_STATE_FOR_TEST),
327                                           dataParcel, replyParcel, option);
328     NETMGR_LOG_I("NetConnServiceProxy::UpdateNetStateForTest(), SendRequest retCode:[%{public}d]", retCode);
329     if (retCode != ERR_NONE) {
330         return NETMANAGER_ERR_OPERATION_FAILED;
331     }
332     return replyParcel.ReadInt32();
333 }
334 
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)335 int32_t NetConnServiceProxy::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
336 {
337     if (netSupplierInfo == nullptr) {
338         NETMGR_LOG_E("netSupplierInfo is null");
339         return NETMANAGER_ERR_LOCAL_PTR_NULL;
340     }
341 
342     MessageParcel data;
343     MessageParcel reply;
344     MessageOption option;
345     if (!WriteInterfaceToken(data)) {
346         NETMGR_LOG_E("WriteInterfaceToken failed");
347         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
348     }
349 
350     NETMGR_LOG_D("proxy supplierId[%{public}d]", supplierId);
351     if (!data.WriteUint32(supplierId)) {
352         return NETMANAGER_ERR_WRITE_DATA_FAIL;
353     }
354     NETMGR_LOG_D("proxy supplierId[%{public}d] Marshalling success", supplierId);
355     if (!netSupplierInfo->Marshalling(data)) {
356         NETMGR_LOG_E("proxy Marshalling failed");
357         return NETMANAGER_ERR_WRITE_DATA_FAIL;
358     }
359     NETMGR_LOG_D("proxy Marshalling success");
360 
361     sptr<IRemoteObject> remote = Remote();
362     if (remote == nullptr) {
363         NETMGR_LOG_E("Remote is null");
364         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
365     }
366     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_NET_SUPPLIER_INFO), data,
367                                         reply, option);
368     if (error != ERR_NONE) {
369         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
370         return NETMANAGER_ERR_OPERATION_FAILED;
371     }
372     NETMGR_LOG_D("UpdateNetSupplierInfo out.");
373     return reply.ReadInt32();
374 }
375 
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)376 int32_t NetConnServiceProxy::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
377 {
378     if (netLinkInfo == nullptr) {
379         NETMGR_LOG_E("netLinkInfo is null");
380         return NETMANAGER_ERR_LOCAL_PTR_NULL;
381     }
382 
383     MessageParcel data;
384     MessageParcel reply;
385     MessageOption option;
386     if (!WriteInterfaceToken(data)) {
387         NETMGR_LOG_E("WriteInterfaceToken failed");
388         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
389     }
390 
391     if (!data.WriteUint32(supplierId)) {
392         return NETMANAGER_ERR_WRITE_DATA_FAIL;
393     }
394 
395     if (!netLinkInfo->Marshalling(data)) {
396         NETMGR_LOG_E("proxy Marshalling failed");
397         return NETMANAGER_ERR_WRITE_DATA_FAIL;
398     }
399 
400     sptr<IRemoteObject> remote = Remote();
401     if (remote == nullptr) {
402         NETMGR_LOG_E("Remote is null");
403         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
404     }
405     int32_t error =
406         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_NET_LINK_INFO), data, reply, option);
407     if (error != ERR_NONE) {
408         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
409         return NETMANAGER_ERR_OPERATION_FAILED;
410     }
411 
412     return reply.ReadInt32();
413 }
414 
RegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)415 int32_t NetConnServiceProxy::RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
416 {
417     if (callback == nullptr) {
418         NETMGR_LOG_E("The parameter of callback is nullptr");
419         return NETMANAGER_ERR_LOCAL_PTR_NULL;
420     }
421 
422     MessageParcel dataParcel;
423     if (!WriteInterfaceToken(dataParcel)) {
424         NETMGR_LOG_E("WriteInterfaceToken failed");
425         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
426     }
427     if (!dataParcel.WriteInt32(netId)) {
428         return NETMANAGER_ERR_WRITE_DATA_FAIL;
429     }
430     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
431 
432     sptr<IRemoteObject> remote = Remote();
433     if (remote == nullptr) {
434         NETMGR_LOG_E("Remote is null");
435         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
436     }
437     MessageParcel replyParcel;
438     MessageOption option;
439     int32_t error =
440         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_DETECTION_RET_CALLBACK),
441                             dataParcel, replyParcel, option);
442     if (error != ERR_NONE) {
443         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
444         return NETMANAGER_ERR_OPERATION_FAILED;
445     }
446     return replyParcel.ReadInt32();
447 }
448 
UnRegisterNetDetectionCallback(int32_t netId,const sptr<INetDetectionCallback> & callback)449 int32_t NetConnServiceProxy::UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback)
450 {
451     if (callback == nullptr) {
452         NETMGR_LOG_E("The parameter of callback is nullptr");
453         return NETMANAGER_ERR_LOCAL_PTR_NULL;
454     }
455 
456     MessageParcel dataParcel;
457     if (!WriteInterfaceToken(dataParcel)) {
458         NETMGR_LOG_E("WriteInterfaceToken failed");
459         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
460     }
461     if (!dataParcel.WriteInt32(netId)) {
462         return NETMANAGER_ERR_WRITE_DATA_FAIL;
463     }
464     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
465 
466     sptr<IRemoteObject> remote = Remote();
467     if (remote == nullptr) {
468         NETMGR_LOG_E("Remote is null");
469         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
470     }
471     MessageParcel replyParcel;
472     MessageOption option;
473     int32_t error =
474         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_UNREGISTER_NET_DETECTION_RET_CALLBACK),
475                             dataParcel, replyParcel, option);
476     if (error != ERR_NONE) {
477         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
478         return NETMANAGER_ERR_OPERATION_FAILED;
479     }
480     return replyParcel.ReadInt32();
481 }
482 
NetDetection(int32_t netId)483 int32_t NetConnServiceProxy::NetDetection(int32_t netId)
484 {
485     MessageParcel dataParcel;
486     if (!WriteInterfaceToken(dataParcel)) {
487         NETMGR_LOG_E("WriteInterfaceToken failed");
488         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
489     }
490     if (!dataParcel.WriteInt32(netId)) {
491         return NETMANAGER_ERR_WRITE_DATA_FAIL;
492     }
493 
494     sptr<IRemoteObject> remote = Remote();
495     if (remote == nullptr) {
496         NETMGR_LOG_E("Remote is null");
497         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
498     }
499     MessageParcel replyParcel;
500     MessageOption option;
501     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_NET_DETECTION), dataParcel,
502                                         replyParcel, option);
503     if (error != ERR_NONE) {
504         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
505         return NETMANAGER_ERR_OPERATION_FAILED;
506     }
507     return replyParcel.ReadInt32();
508 }
509 
GetIfaceNames(NetBearType bearerType,std::list<std::string> & ifaceNames)510 int32_t NetConnServiceProxy::GetIfaceNames(NetBearType bearerType, std::list<std::string> &ifaceNames)
511 {
512     MessageParcel data;
513     if (!WriteInterfaceToken(data)) {
514         NETMGR_LOG_E("WriteInterfaceToken failed");
515         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
516     }
517 
518     if (!data.WriteUint32(bearerType)) {
519         return NETMANAGER_ERR_WRITE_DATA_FAIL;
520     }
521 
522     sptr<IRemoteObject> remote = Remote();
523     if (remote == nullptr) {
524         NETMGR_LOG_E("Remote is null");
525         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
526     }
527 
528     MessageParcel reply;
529     MessageOption option;
530     int32_t error =
531         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_IFACE_NAMES), data, reply, option);
532     if (error != ERR_NONE) {
533         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
534         return NETMANAGER_ERR_OPERATION_FAILED;
535     }
536 
537     int32_t ret = NETMANAGER_SUCCESS;
538     if (!reply.ReadInt32(ret)) {
539         return NETMANAGER_ERR_READ_REPLY_FAIL;
540     }
541     if (ret == NETMANAGER_SUCCESS) {
542         uint32_t size = 0;
543         if (!reply.ReadUint32(size)) {
544             return NETMANAGER_ERR_READ_REPLY_FAIL;
545         }
546         size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
547         for (uint32_t i = 0; i < size; ++i) {
548             std::string value;
549             if (!reply.ReadString(value)) {
550                 return NETMANAGER_ERR_READ_REPLY_FAIL;
551             }
552             ifaceNames.push_back(value);
553         }
554     }
555     return ret;
556 }
557 
GetIfaceNameByType(NetBearType bearerType,const std::string & ident,std::string & ifaceName)558 int32_t NetConnServiceProxy::GetIfaceNameByType(NetBearType bearerType, const std::string &ident,
559                                                 std::string &ifaceName)
560 {
561     MessageParcel data;
562     if (!WriteInterfaceToken(data)) {
563         NETMGR_LOG_E("WriteInterfaceToken failed");
564         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
565     }
566 
567     uint32_t netType = static_cast<NetBearType>(bearerType);
568     if (!data.WriteUint32(netType)) {
569         return NETMANAGER_ERR_WRITE_DATA_FAIL;
570     }
571 
572     if (!data.WriteString(ident)) {
573         return NETMANAGER_ERR_WRITE_DATA_FAIL;
574     }
575 
576     sptr<IRemoteObject> remote = Remote();
577     if (remote == nullptr) {
578         NETMGR_LOG_E("Remote is null");
579         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
580     }
581     MessageParcel reply;
582     MessageOption option;
583     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_IFACENAME_BY_TYPE), data,
584                                         reply, option);
585     if (error != ERR_NONE) {
586         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
587         return NETMANAGER_ERR_OPERATION_FAILED;
588     }
589 
590     int32_t ret = 0;
591     if (!reply.ReadInt32(ret)) {
592         return NETMANAGER_ERR_READ_REPLY_FAIL;
593     }
594     if (ret == NETMANAGER_SUCCESS) {
595         if (!reply.ReadString(ifaceName)) {
596             return NETMANAGER_ERR_READ_REPLY_FAIL;
597         }
598     }
599     return ret;
600 }
601 
WriteInterfaceToken(MessageParcel & data)602 bool NetConnServiceProxy::WriteInterfaceToken(MessageParcel &data)
603 {
604     if (!data.WriteInterfaceToken(NetConnServiceProxy::GetDescriptor())) {
605         NETMGR_LOG_E("WriteInterfaceToken failed");
606         return false;
607     }
608     return true;
609 }
610 
GetDefaultNet(int32_t & netId)611 int32_t NetConnServiceProxy::GetDefaultNet(int32_t &netId)
612 {
613     MessageParcel dataParcel;
614     if (!WriteInterfaceToken(dataParcel)) {
615         NETMGR_LOG_E("WriteInterfaceToken failed");
616         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
617     }
618 
619     MessageOption option;
620     MessageParcel replyParcel;
621     sptr<IRemoteObject> remote = Remote();
622     if (remote == nullptr) {
623         NETMGR_LOG_E("Remote is null");
624         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
625     }
626 
627     int32_t errCode = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GETDEFAULTNETWORK),
628                                           dataParcel, replyParcel, option);
629     NETMGR_LOG_D("SendRequest errcode:[%{public}d]", errCode);
630     if (errCode != ERR_NONE) {
631         return NETMANAGER_ERR_OPERATION_FAILED;
632     }
633     int32_t ret = 0;
634     if (!replyParcel.ReadInt32(ret)) {
635         return NETMANAGER_ERR_READ_REPLY_FAIL;
636     }
637     if (ret == NETMANAGER_SUCCESS) {
638         if (!replyParcel.ReadInt32(netId)) {
639             return NETMANAGER_ERR_READ_REPLY_FAIL;
640         }
641     }
642     return ret;
643 }
644 
HasDefaultNet(bool & flag)645 int32_t NetConnServiceProxy::HasDefaultNet(bool &flag)
646 {
647     MessageParcel dataParcel;
648     if (!WriteInterfaceToken(dataParcel)) {
649         NETMGR_LOG_E("WriteInterfaceToken failed");
650         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
651     }
652 
653     MessageOption option;
654     MessageParcel replyParcel;
655     sptr<IRemoteObject> remote = Remote();
656     if (remote == nullptr) {
657         NETMGR_LOG_E("Remote is null");
658         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
659     }
660     int32_t retCode = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_HASDEFAULTNET), dataParcel,
661                                           replyParcel, option);
662     NETMGR_LOG_D("SendRequest retCode:[%{public}d]", retCode);
663     if (retCode != ERR_NONE) {
664         return NETMANAGER_ERR_OPERATION_FAILED;
665     }
666 
667     int32_t ret = 0;
668     if (!replyParcel.ReadInt32(ret)) {
669         return NETMANAGER_ERR_READ_REPLY_FAIL;
670     }
671     if (ret == NETMANAGER_SUCCESS) {
672         if (!replyParcel.ReadBool(flag)) {
673             return NETMANAGER_ERR_READ_REPLY_FAIL;
674         }
675     }
676     return ret;
677 }
678 
GetSpecificNet(NetBearType bearerType,std::list<int32_t> & netIdList)679 int32_t NetConnServiceProxy::GetSpecificNet(NetBearType bearerType, std::list<int32_t> &netIdList)
680 {
681     MessageParcel data;
682     if (!WriteInterfaceToken(data)) {
683         NETMGR_LOG_E("WriteInterfaceToken failed");
684         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
685     }
686 
687     uint32_t type = static_cast<uint32_t>(bearerType);
688     if (!data.WriteUint32(type)) {
689         return NETMANAGER_ERR_WRITE_DATA_FAIL;
690     }
691 
692     sptr<IRemoteObject> remote = Remote();
693     if (remote == nullptr) {
694         NETMGR_LOG_E("Remote is null");
695         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
696     }
697 
698     MessageParcel reply;
699     MessageOption option;
700     int32_t error =
701         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_SPECIFIC_NET), data, reply, option);
702     if (error != ERR_NONE) {
703         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
704         return NETMANAGER_ERR_OPERATION_FAILED;
705     }
706 
707     int32_t ret = NETMANAGER_SUCCESS;
708     if (!reply.ReadInt32(ret)) {
709         return NETMANAGER_ERR_READ_REPLY_FAIL;
710     }
711     if (ret == NETMANAGER_SUCCESS) {
712         uint32_t size = 0;
713         if (!reply.ReadUint32(size)) {
714             return NETMANAGER_ERR_READ_REPLY_FAIL;
715         }
716         size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
717         for (uint32_t i = 0; i < size; ++i) {
718             uint32_t value;
719             if (!reply.ReadUint32(value)) {
720                 return NETMANAGER_ERR_READ_REPLY_FAIL;
721             }
722             netIdList.push_back(value);
723         }
724     }
725     return ret;
726 }
727 
GetAllNets(std::list<int32_t> & netIdList)728 int32_t NetConnServiceProxy::GetAllNets(std::list<int32_t> &netIdList)
729 {
730     MessageParcel data;
731     if (!WriteInterfaceToken(data)) {
732         NETMGR_LOG_E("WriteInterfaceToken failed");
733         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
734     }
735 
736     sptr<IRemoteObject> remote = Remote();
737     if (remote == nullptr) {
738         NETMGR_LOG_E("Remote is null");
739         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
740     }
741 
742     MessageParcel reply;
743     MessageOption option;
744     int32_t error =
745         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_ALL_NETS), data, reply, option);
746     if (error != ERR_NONE) {
747         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
748         return NETMANAGER_ERR_OPERATION_FAILED;
749     }
750 
751     int32_t ret = NETMANAGER_SUCCESS;
752     if (!reply.ReadInt32(ret)) {
753         return NETMANAGER_ERR_READ_REPLY_FAIL;
754     }
755     if (ret == NETMANAGER_SUCCESS) {
756         uint32_t size;
757         if (!reply.ReadUint32(size)) {
758             return NETMANAGER_ERR_READ_REPLY_FAIL;
759         }
760         size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
761         for (uint32_t i = 0; i < size; ++i) {
762             uint32_t value;
763             if (!reply.ReadUint32(value)) {
764                 return NETMANAGER_ERR_READ_REPLY_FAIL;
765             }
766             netIdList.push_back(value);
767         }
768     }
769     return ret;
770 }
771 
GetSpecificUidNet(int32_t uid,int32_t & netId)772 int32_t NetConnServiceProxy::GetSpecificUidNet(int32_t uid, int32_t &netId)
773 {
774     MessageParcel data;
775     if (!WriteInterfaceToken(data)) {
776         NETMGR_LOG_E("WriteInterfaceToken failed");
777         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
778     }
779 
780     if (!data.WriteInt32(uid)) {
781         return NETMANAGER_ERR_WRITE_DATA_FAIL;
782     }
783 
784     sptr<IRemoteObject> remote = Remote();
785     if (remote == nullptr) {
786         NETMGR_LOG_E("Remote is null");
787         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
788     }
789 
790     MessageParcel reply;
791     MessageOption option;
792     int32_t error =
793         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_SPECIFIC_UID_NET), data, reply, option);
794     if (error != ERR_NONE) {
795         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
796         return NETMANAGER_ERR_OPERATION_FAILED;
797     }
798 
799     int32_t ret = NETMANAGER_SUCCESS;
800     if (!reply.ReadInt32(ret)) {
801         return NETMANAGER_ERR_READ_REPLY_FAIL;
802     }
803     if (ret == NETMANAGER_SUCCESS) {
804         if (!reply.ReadInt32(netId)) {
805             return NETMANAGER_ERR_READ_REPLY_FAIL;
806         }
807     }
808     return ret;
809 }
810 
GetConnectionProperties(int32_t netId,NetLinkInfo & info)811 int32_t NetConnServiceProxy::GetConnectionProperties(int32_t netId, NetLinkInfo &info)
812 {
813     MessageParcel data;
814     if (!WriteInterfaceToken(data)) {
815         NETMGR_LOG_E("WriteInterfaceToken failed");
816         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
817     }
818 
819     if (!data.WriteInt32(netId)) {
820         return NETMANAGER_ERR_WRITE_DATA_FAIL;
821     }
822 
823     sptr<IRemoteObject> remote = Remote();
824     if (remote == nullptr) {
825         NETMGR_LOG_E("Remote is null");
826         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
827     }
828 
829     MessageParcel reply;
830     MessageOption option;
831     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_CONNECTION_PROPERTIES),
832                                         data, reply, option);
833     if (error != ERR_NONE) {
834         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
835         return NETMANAGER_ERR_OPERATION_FAILED;
836     }
837 
838     int32_t ret = NETMANAGER_SUCCESS;
839     if (!reply.ReadInt32(ret)) {
840         return NETMANAGER_ERR_READ_REPLY_FAIL;
841     }
842     if (ret == NETMANAGER_SUCCESS) {
843         sptr<NetLinkInfo> netLinkInfo_ptr = NetLinkInfo::Unmarshalling(reply);
844         if (netLinkInfo_ptr != nullptr) {
845             info = *netLinkInfo_ptr;
846         }
847     }
848     return ret;
849 }
850 
GetNetCapabilities(int32_t netId,NetAllCapabilities & netAllCap)851 int32_t NetConnServiceProxy::GetNetCapabilities(int32_t netId, NetAllCapabilities &netAllCap)
852 {
853     MessageParcel data;
854     if (!WriteInterfaceToken(data)) {
855         NETMGR_LOG_E("WriteInterfaceToken failed");
856         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
857     }
858 
859     if (!data.WriteInt32(netId)) {
860         return NETMANAGER_ERR_WRITE_DATA_FAIL;
861     }
862 
863     sptr<IRemoteObject> remote = Remote();
864     if (remote == nullptr) {
865         NETMGR_LOG_E("Remote is null");
866         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
867     }
868 
869     MessageParcel reply;
870     MessageOption option;
871     int32_t error =
872         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_NET_CAPABILITIES), data, reply, option);
873     if (error != ERR_NONE) {
874         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
875         return NETMANAGER_ERR_OPERATION_FAILED;
876     }
877 
878     int32_t ret = NETMANAGER_SUCCESS;
879     if (!reply.ReadInt32(ret)) {
880         return NETMANAGER_ERR_READ_REPLY_FAIL;
881     }
882     return (ret == NETMANAGER_SUCCESS) ? GetNetCapData(reply, netAllCap) : ret;
883 }
884 
GetNetCapData(MessageParcel & reply,NetAllCapabilities & netAllCap)885 int32_t NetConnServiceProxy::GetNetCapData(MessageParcel &reply, NetAllCapabilities &netAllCap)
886 {
887     if (!reply.ReadUint32(netAllCap.linkUpBandwidthKbps_)) {
888         return NETMANAGER_ERR_READ_REPLY_FAIL;
889     }
890     if (!reply.ReadUint32(netAllCap.linkDownBandwidthKbps_)) {
891         return NETMANAGER_ERR_READ_REPLY_FAIL;
892     }
893     uint32_t size = 0;
894     if (!reply.ReadUint32(size)) {
895         return NETMANAGER_ERR_READ_REPLY_FAIL;
896     }
897     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
898     uint32_t value = 0;
899     for (uint32_t i = 0; i < size; ++i) {
900         if (!reply.ReadUint32(value)) {
901             return NETMANAGER_ERR_READ_REPLY_FAIL;
902         }
903         netAllCap.netCaps_.insert(static_cast<NetCap>(value));
904     }
905     if (!reply.ReadUint32(size)) {
906         return NETMANAGER_ERR_READ_REPLY_FAIL;
907     }
908     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
909     for (uint32_t i = 0; i < size; ++i) {
910         if (!reply.ReadUint32(value)) {
911             return NETMANAGER_ERR_READ_REPLY_FAIL;
912         }
913         netAllCap.bearerTypes_.insert(static_cast<NetBearType>(value));
914     }
915     return NETMANAGER_SUCCESS;
916 }
917 
GetAddressesByName(const std::string & host,int32_t netId,std::vector<INetAddr> & addrList)918 int32_t NetConnServiceProxy::GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList)
919 {
920     MessageParcel data;
921     if (!WriteInterfaceToken(data)) {
922         NETMGR_LOG_E("WriteInterfaceToken failed");
923         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
924     }
925     if (!data.WriteString(host)) {
926         return NETMANAGER_ERR_WRITE_DATA_FAIL;
927     }
928     if (!data.WriteInt32(netId)) {
929         return NETMANAGER_ERR_WRITE_DATA_FAIL;
930     }
931     sptr<IRemoteObject> remote = Remote();
932     if (remote == nullptr) {
933         NETMGR_LOG_E("Remote is null");
934         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
935     }
936 
937     MessageParcel reply;
938     MessageOption option;
939     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_ADDRESSES_BY_NAME), data,
940                                         reply, option);
941     if (error != ERR_NONE) {
942         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
943         return NETMANAGER_ERR_OPERATION_FAILED;
944     }
945 
946     int32_t ret = NETMANAGER_SUCCESS;
947     if (!reply.ReadInt32(ret)) {
948         return NETMANAGER_ERR_READ_REPLY_FAIL;
949     }
950 
951     if (ret == NETMANAGER_SUCCESS) {
952         uint32_t size;
953         if (!reply.ReadUint32(size)) {
954             return NETMANAGER_ERR_READ_REPLY_FAIL;
955         }
956         size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
957         for (uint32_t i = 0; i < size; ++i) {
958             sptr<INetAddr> netaddr_ptr = INetAddr::Unmarshalling(reply);
959             if (netaddr_ptr != nullptr) {
960                 addrList.push_back(*netaddr_ptr);
961             }
962         }
963     }
964     return ret;
965 }
966 
GetAddressByName(const std::string & host,int32_t netId,INetAddr & addr)967 int32_t NetConnServiceProxy::GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr)
968 {
969     MessageParcel data;
970     if (!WriteInterfaceToken(data)) {
971         NETMGR_LOG_E("WriteInterfaceToken failed");
972         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
973     }
974 
975     if (!data.WriteString(host)) {
976         return NETMANAGER_ERR_WRITE_DATA_FAIL;
977     }
978     if (!data.WriteInt32(netId)) {
979         return NETMANAGER_ERR_WRITE_DATA_FAIL;
980     }
981     sptr<IRemoteObject> remote = Remote();
982     if (remote == nullptr) {
983         NETMGR_LOG_E("Remote is null");
984         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
985     }
986 
987     MessageParcel reply;
988     MessageOption option;
989     int32_t error =
990         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_ADDRESS_BY_NAME), data, reply, option);
991     if (error != ERR_NONE) {
992         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
993         return NETMANAGER_ERR_OPERATION_FAILED;
994     }
995 
996     int32_t ret = NETMANAGER_SUCCESS;
997     if (!reply.ReadInt32(ret)) {
998         return NETMANAGER_ERR_READ_REPLY_FAIL;
999     }
1000     if (ret == NETMANAGER_SUCCESS) {
1001         sptr<INetAddr> netaddr_ptr = INetAddr::Unmarshalling(reply);
1002         if (netaddr_ptr != nullptr) {
1003             addr = *netaddr_ptr;
1004         }
1005     }
1006     return ret;
1007 }
1008 
BindSocket(int32_t socket_fd,int32_t netId)1009 int32_t NetConnServiceProxy::BindSocket(int32_t socket_fd, int32_t netId)
1010 {
1011     MessageParcel data;
1012     if (!WriteInterfaceToken(data)) {
1013         NETMGR_LOG_E("WriteInterfaceToken failed");
1014         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1015     }
1016 
1017     if (!data.WriteInt32(socket_fd)) {
1018         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1019     }
1020     if (!data.WriteInt32(netId)) {
1021         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1022     }
1023     sptr<IRemoteObject> remote = Remote();
1024     if (remote == nullptr) {
1025         NETMGR_LOG_E("Remote is null");
1026         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1027     }
1028 
1029     MessageParcel reply;
1030     MessageOption option;
1031     int32_t error =
1032         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_BIND_SOCKET), data, reply, option);
1033     if (error != ERR_NONE) {
1034         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1035         return NETMANAGER_ERR_OPERATION_FAILED;
1036     }
1037 
1038     int32_t ret = NETMANAGER_SUCCESS;
1039     if (!reply.ReadInt32(ret)) {
1040         return NETMANAGER_ERR_READ_REPLY_FAIL;
1041     }
1042     return ret;
1043 }
1044 
SetAirplaneMode(bool state)1045 int32_t NetConnServiceProxy::SetAirplaneMode(bool state)
1046 {
1047     MessageParcel data;
1048     if (!WriteInterfaceToken(data)) {
1049         NETMGR_LOG_E("WriteInterfaceToken failed");
1050         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1051     }
1052 
1053     if (!data.WriteBool(state)) {
1054         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1055     }
1056     sptr<IRemoteObject> remote = Remote();
1057     if (remote == nullptr) {
1058         NETMGR_LOG_E("Remote is null");
1059         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1060     }
1061 
1062     MessageParcel reply;
1063     MessageOption option;
1064     int32_t error =
1065         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_AIRPLANE_MODE), data, reply, option);
1066     if (error != ERR_NONE) {
1067         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1068         return NETMANAGER_ERR_OPERATION_FAILED;
1069     }
1070 
1071     int32_t ret = NETMANAGER_SUCCESS;
1072     if (!reply.ReadInt32(ret)) {
1073         return NETMANAGER_ERR_READ_REPLY_FAIL;
1074     }
1075     return ret;
1076 }
1077 
IsDefaultNetMetered(bool & isMetered)1078 int32_t NetConnServiceProxy::IsDefaultNetMetered(bool &isMetered)
1079 {
1080     MessageParcel data;
1081     if (!WriteInterfaceToken(data)) {
1082         NETMGR_LOG_E("WriteInterfaceToken failed");
1083         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1084     }
1085 
1086     sptr<IRemoteObject> remote = Remote();
1087     if (remote == nullptr) {
1088         NETMGR_LOG_E("Remote is null");
1089         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1090     }
1091 
1092     MessageParcel reply;
1093     MessageOption option;
1094     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_IS_DEFAULT_NET_METERED), data,
1095                                         reply, option);
1096     if (error != ERR_NONE) {
1097         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1098         return NETMANAGER_ERR_OPERATION_FAILED;
1099     }
1100 
1101     int32_t ret = NETMANAGER_SUCCESS;
1102     if (!reply.ReadInt32(ret)) {
1103         return NETMANAGER_ERR_READ_REPLY_FAIL;
1104     }
1105     if (ret == NETMANAGER_SUCCESS) {
1106         if (!reply.ReadBool(isMetered)) {
1107             return NETMANAGER_ERR_READ_REPLY_FAIL;
1108         }
1109     }
1110     return ret;
1111 }
1112 
SetGlobalHttpProxy(const HttpProxy & httpProxy)1113 int32_t NetConnServiceProxy::SetGlobalHttpProxy(const HttpProxy &httpProxy)
1114 {
1115     MessageParcel data;
1116     if (!WriteInterfaceToken(data)) {
1117         NETMGR_LOG_E("WriteInterfaceToken failed");
1118         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1119     }
1120 
1121     if (!httpProxy.Marshalling(data)) {
1122         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1123     }
1124 
1125     sptr<IRemoteObject> remote = Remote();
1126     if (remote == nullptr) {
1127         NETMGR_LOG_E("Remote is null");
1128         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1129     }
1130 
1131     MessageParcel reply;
1132     MessageOption option;
1133     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_GLOBAL_HTTP_PROXY), data,
1134                                         reply, option);
1135     if (error != ERR_NONE) {
1136         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1137         return NETMANAGER_ERR_OPERATION_FAILED;
1138     }
1139 
1140     int32_t ret = NETMANAGER_SUCCESS;
1141     if (!reply.ReadInt32(ret)) {
1142         return NETMANAGER_ERR_READ_REPLY_FAIL;
1143     }
1144     return ret;
1145 }
1146 
GetGlobalHttpProxy(HttpProxy & httpProxy)1147 int32_t NetConnServiceProxy::GetGlobalHttpProxy(HttpProxy &httpProxy)
1148 {
1149     MessageParcel data;
1150     if (!WriteInterfaceToken(data)) {
1151         NETMGR_LOG_E("WriteInterfaceToken failed");
1152         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1153     }
1154 
1155     sptr<IRemoteObject> remote = Remote();
1156     if (remote == nullptr) {
1157         NETMGR_LOG_E("Remote is null");
1158         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1159     }
1160 
1161     MessageParcel reply;
1162     MessageOption option;
1163     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_GLOBAL_HTTP_PROXY), data,
1164                                         reply, option);
1165     if (error != ERR_NONE) {
1166         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1167         return NETMANAGER_ERR_OPERATION_FAILED;
1168     }
1169 
1170     int32_t ret = NETMANAGER_SUCCESS;
1171     if (!reply.ReadInt32(ret)) {
1172         return NETMANAGER_ERR_READ_REPLY_FAIL;
1173     }
1174 
1175     if (ret == NETMANAGER_SUCCESS) {
1176         if (!HttpProxy::Unmarshalling(reply, httpProxy)) {
1177             return NETMANAGER_ERR_READ_REPLY_FAIL;
1178         }
1179     }
1180     return ret;
1181 }
1182 
GetDefaultHttpProxy(int32_t bindNetId,HttpProxy & httpProxy)1183 int32_t NetConnServiceProxy::GetDefaultHttpProxy(int32_t bindNetId, HttpProxy &httpProxy)
1184 {
1185     MessageParcel data;
1186     if (!WriteInterfaceToken(data)) {
1187         NETMGR_LOG_E("WriteInterfaceToken failed");
1188         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1189     }
1190 
1191     if (!data.WriteInt32(bindNetId)) {
1192         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1193     }
1194 
1195     sptr<IRemoteObject> remote = Remote();
1196     if (remote == nullptr) {
1197         NETMGR_LOG_E("Remote is null");
1198         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1199     }
1200 
1201     MessageParcel reply;
1202     MessageOption option;
1203     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_DEFAULT_HTTP_PROXY), data,
1204                                         reply, option);
1205     if (error != ERR_NONE) {
1206         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1207         return NETMANAGER_ERR_OPERATION_FAILED;
1208     }
1209 
1210     int32_t ret = NETMANAGER_SUCCESS;
1211     if (!reply.ReadInt32(ret)) {
1212         return NETMANAGER_ERR_READ_REPLY_FAIL;
1213     }
1214 
1215     if (ret == NETMANAGER_SUCCESS) {
1216         if (!HttpProxy::Unmarshalling(reply, httpProxy)) {
1217             return NETMANAGER_ERR_READ_REPLY_FAIL;
1218         }
1219     }
1220     return ret;
1221 }
1222 
GetNetIdByIdentifier(const std::string & ident,std::list<int32_t> & netIdList)1223 int32_t NetConnServiceProxy::GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList)
1224 {
1225     MessageParcel data;
1226     if (!WriteInterfaceToken(data)) {
1227         NETMGR_LOG_E("WriteInterfaceToken failed");
1228         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1229     }
1230 
1231     if (!data.WriteString(ident)) {
1232         NETMGR_LOG_E("Write string data failed");
1233         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1234     }
1235 
1236     sptr<IRemoteObject> remote = Remote();
1237     if (remote == nullptr) {
1238         NETMGR_LOG_E("Remote is null");
1239         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1240     }
1241 
1242     MessageParcel reply;
1243     MessageOption option;
1244     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_NET_ID_BY_IDENTIFIER), data,
1245                                         reply, option);
1246     if (error != ERR_NONE) {
1247         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1248         return NETMANAGER_ERR_OPERATION_FAILED;
1249     }
1250 
1251     int32_t ret = NETMANAGER_SUCCESS;
1252     if (!reply.ReadInt32(ret)) {
1253         NETMGR_LOG_E("Read return code failed");
1254         return NETMANAGER_ERR_READ_REPLY_FAIL;
1255     }
1256 
1257     if (ret == NETMANAGER_SUCCESS) {
1258         uint32_t size = 0;
1259         if (!reply.ReadUint32(size)) {
1260             return NETMANAGER_ERR_READ_REPLY_FAIL;
1261         }
1262         size = size > MAX_IFACE_NUM ? MAX_IFACE_NUM : size;
1263         int32_t value = 0;
1264         for (uint32_t i = 0; i < size; ++i) {
1265             if (!reply.ReadInt32(value)) {
1266                 return NETMANAGER_ERR_READ_REPLY_FAIL;
1267             }
1268             netIdList.push_back(value);
1269         }
1270     }
1271     return ret;
1272 }
1273 
SetAppNet(int32_t netId)1274 int32_t NetConnServiceProxy::SetAppNet(int32_t netId)
1275 {
1276     MessageParcel data;
1277     if (!WriteInterfaceToken(data)) {
1278         NETMGR_LOG_E("WriteInterfaceToken failed");
1279         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1280     }
1281 
1282     if (!data.WriteInt32(netId)) {
1283         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1284     }
1285     sptr<IRemoteObject> remote = Remote();
1286     if (remote == nullptr) {
1287         NETMGR_LOG_E("Remote is null");
1288         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1289     }
1290 
1291     MessageParcel reply;
1292     MessageOption option;
1293     int32_t error =
1294         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_SET_APP_NET), data, reply, option);
1295     if (error != ERR_NONE) {
1296         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1297         return NETMANAGER_ERR_OPERATION_FAILED;
1298     }
1299 
1300     int32_t ret = NETMANAGER_SUCCESS;
1301     if (!reply.ReadInt32(ret)) {
1302         return NETMANAGER_ERR_READ_REPLY_FAIL;
1303     }
1304     return ret;
1305 }
1306 
RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> & callback)1307 int32_t NetConnServiceProxy::RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback)
1308 {
1309     if (callback == nullptr) {
1310         NETMGR_LOG_E("The parameter of callback is nullptr");
1311         return NETMANAGER_ERR_LOCAL_PTR_NULL;
1312     }
1313 
1314     MessageParcel dataParcel;
1315     if (!WriteInterfaceToken(dataParcel)) {
1316         NETMGR_LOG_E("WriteInterfaceToken failed");
1317         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1318     }
1319     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
1320 
1321     sptr<IRemoteObject> remote = Remote();
1322     if (remote == nullptr) {
1323         NETMGR_LOG_E("Remote is null");
1324         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1325     }
1326 
1327     MessageOption option;
1328     MessageParcel replyParcel;
1329     int32_t retCode =
1330         remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_REGISTER_NET_INTERFACE_CALLBACK),
1331                             dataParcel, replyParcel, option);
1332     if (retCode != ERR_NONE) {
1333         return NETMANAGER_ERR_OPERATION_FAILED;
1334     }
1335     return replyParcel.ReadInt32();
1336 }
1337 
GetNetInterfaceConfiguration(const std::string & iface,NetInterfaceConfiguration & config)1338 int32_t NetConnServiceProxy::GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config)
1339 {
1340     MessageParcel data;
1341     if (!WriteInterfaceToken(data)) {
1342         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
1343     }
1344     if (!data.WriteString(iface)) {
1345         return NETMANAGER_ERR_WRITE_DATA_FAIL;
1346     }
1347     sptr<IRemoteObject> remote = Remote();
1348     if (remote == nullptr) {
1349         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
1350     }
1351     MessageParcel reply;
1352     MessageOption option;
1353     int32_t error = remote->SendRequest(static_cast<uint32_t>(ConnInterfaceCode::CMD_NM_GET_INTERFACE_CONFIGURATION),
1354                                         data, reply, option);
1355     if (error != ERR_NONE) {
1356         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
1357         return NETMANAGER_ERR_OPERATION_FAILED;
1358     }
1359     int32_t ret = NETMANAGER_SUCCESS;
1360     if (!reply.ReadInt32(ret)) {
1361         return NETMANAGER_ERR_READ_REPLY_FAIL;
1362     }
1363     if (ret == NETMANAGER_SUCCESS) {
1364         if (!NetInterfaceConfiguration::Unmarshalling(reply, config)) {
1365             return NETMANAGER_ERR_READ_REPLY_FAIL;
1366         }
1367     }
1368     return ret;
1369 }
1370 } // namespace NetManagerStandard
1371 } // namespace OHOS
1372