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