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