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