1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <map>
17 #include <sstream>
18
19 #include "net_conn_client.h"
20 #include "net_connection_adapter.h"
21 #include "net_manager_constants.h"
22 #include "net_mgr_log_wrapper.h"
23 #include "securec.h"
24
25 namespace OHOS::NetManagerStandard {
26
27 using BearTypeMap = std::map<NetBearType, NetConn_NetBearerType>;
28 using ReverseBearTypeMap = std::map<NetConn_NetBearerType, NetBearType>;
29 using NetCapMap = std::map<NetCap, NetConn_NetCap>;
30 using ReverseNetCapMap = std::map<NetConn_NetCap, NetCap>;
31
32 static BearTypeMap bearTypeMap = {{BEARER_CELLULAR, NETCONN_BEARER_CELLULAR},
33 {BEARER_WIFI, NETCONN_BEARER_WIFI},
34 {BEARER_BLUETOOTH, NETCONN_BEARER_BLUETOOTH},
35 {BEARER_ETHERNET, NETCONN_BEARER_ETHERNET},
36 {BEARER_VPN, NETCONN_BEARER_VPN}};
37
38 static ReverseBearTypeMap reverseBearTypeMap = {{NETCONN_BEARER_CELLULAR, BEARER_CELLULAR},
39 {NETCONN_BEARER_WIFI, BEARER_WIFI},
40 {NETCONN_BEARER_BLUETOOTH, BEARER_BLUETOOTH},
41 {NETCONN_BEARER_ETHERNET, BEARER_ETHERNET},
42 {NETCONN_BEARER_VPN, BEARER_VPN}};
43
44 static NetCapMap netCapMap = {{NET_CAPABILITY_MMS, NETCONN_NET_CAPABILITY_MMS},
45 {NET_CAPABILITY_SUPL, NETCONN_NET_CAPABILITY_SUPL},
46 {NET_CAPABILITY_DUN, NETCONN_NET_CAPABILITY_DUN},
47 {NET_CAPABILITY_IA, NETCONN_NET_CAPABILITY_IA},
48 {NET_CAPABILITY_XCAP, NETCONN_NET_CAPABILITY_XCAP},
49 {NET_CAPABILITY_NOT_METERED, NETCONN_NET_CAPABILITY_NOT_METERED},
50 {NET_CAPABILITY_INTERNET, NETCONN_NET_CAPABILITY_INTERNET},
51 {NET_CAPABILITY_NOT_VPN, NETCONN_NET_CAPABILITY_NOT_VPN},
52 {NET_CAPABILITY_VALIDATED, NETCONN_NET_CAPABILITY_VALIDATED},
53 {NET_CAPABILITY_PORTAL, NETCONN_NET_CAPABILITY_PORTAL},
54 {NET_CAPABILITY_CHECKING_CONNECTIVITY, NETCONN_NET_CAPABILITY_CHECKING_CONNECTIVITY}};
55
56 static ReverseNetCapMap reverseNetCapMap = {
57 {NETCONN_NET_CAPABILITY_MMS, NET_CAPABILITY_MMS},
58 {NETCONN_NET_CAPABILITY_SUPL, NET_CAPABILITY_SUPL},
59 {NETCONN_NET_CAPABILITY_DUN, NET_CAPABILITY_DUN},
60 {NETCONN_NET_CAPABILITY_IA, NET_CAPABILITY_IA},
61 {NETCONN_NET_CAPABILITY_XCAP, NET_CAPABILITY_XCAP},
62 {NETCONN_NET_CAPABILITY_NOT_METERED, NET_CAPABILITY_NOT_METERED},
63 {NETCONN_NET_CAPABILITY_INTERNET, NET_CAPABILITY_INTERNET},
64 {NETCONN_NET_CAPABILITY_NOT_VPN, NET_CAPABILITY_NOT_VPN},
65 {NETCONN_NET_CAPABILITY_VALIDATED, NET_CAPABILITY_VALIDATED},
66 {NETCONN_NET_CAPABILITY_PORTAL, NET_CAPABILITY_PORTAL},
67 {NETCONN_NET_CAPABILITY_CHECKING_CONNECTIVITY, NET_CAPABILITY_CHECKING_CONNECTIVITY}};
68
Conv2Ch(const std::string s,char * ch)69 static int32_t Conv2Ch(const std::string s, char *ch)
70 {
71 if (s.length() > NETCONN_MAX_STR_LEN - 1) {
72 NETMGR_LOG_E("string out of memory");
73 return NETMANAGER_ERR_INTERNAL;
74 }
75 if (strcpy_s(ch, NETCONN_MAX_STR_LEN, s.c_str()) != EOK) {
76 NETMGR_LOG_E("string copy failed");
77 return NETMANAGER_ERR_INTERNAL;
78 }
79 return NETMANAGER_SUCCESS;
80 }
81
Conv2INetAddr(const INetAddr & netAddrObj,NetConn_NetAddr * netAddr)82 static int32_t Conv2INetAddr(const INetAddr &netAddrObj, NetConn_NetAddr *netAddr)
83 {
84 netAddr->family = netAddrObj.family_;
85 netAddr->prefixlen = netAddrObj.prefixlen_;
86 netAddr->port = netAddrObj.port_;
87
88 int32_t ret = Conv2Ch(netAddrObj.address_, netAddr->address);
89 if (ret != NETMANAGER_SUCCESS) {
90 return ret;
91 }
92 return NETMANAGER_SUCCESS;
93 }
94
Conv2NetHandleList(const std::list<sptr<NetHandle>> & netHandleObjList,NetConn_NetHandleList * netHandleList)95 int32_t Conv2NetHandleList(const std::list<sptr<NetHandle>> &netHandleObjList, NetConn_NetHandleList *netHandleList)
96 {
97 int32_t i = 0;
98 for (const auto &netHandleObj : netHandleObjList) {
99 if (i > NETCONN_MAX_NET_SIZE - 1) {
100 NETMGR_LOG_E("netHandleList out of memory");
101 return NETMANAGER_ERR_INTERNAL;
102 }
103 netHandleList->netHandles[i++].netId = (*netHandleObj).GetNetId();
104 }
105 netHandleList->netHandleListSize = netHandleObjList.size();
106 return NETMANAGER_SUCCESS;
107 }
108
Conv2NetHandle(NetHandle & netHandleObj,NetConn_NetHandle * netHandle)109 int32_t Conv2NetHandle(NetHandle &netHandleObj, NetConn_NetHandle *netHandle)
110 {
111 netHandle->netId = netHandleObj.GetNetId();
112 return NETMANAGER_SUCCESS;
113 }
114
Conv2NetHandleObj(NetConn_NetHandle * netHandle,NetHandle & netHandleObj)115 int32_t Conv2NetHandleObj(NetConn_NetHandle *netHandle, NetHandle &netHandleObj)
116 {
117 netHandleObj.SetNetId(netHandle->netId);
118 return NETMANAGER_SUCCESS;
119 }
120
Conv2HttpProxy(const HttpProxy & httpProxyObj,NetConn_HttpProxy * httpProxy)121 int32_t Conv2HttpProxy(const HttpProxy &httpProxyObj, NetConn_HttpProxy *httpProxy)
122 {
123 int32_t ret = Conv2Ch(httpProxyObj.GetHost(), httpProxy->host);
124 if (ret != NETMANAGER_SUCCESS) {
125 return ret;
126 }
127 httpProxy->port = httpProxyObj.GetPort();
128
129 int32_t i = 0;
130 for (const auto &exclusion : httpProxyObj.GetExclusionList()) {
131 if (i > NETCONN_MAX_EXCLUSION_SIZE - 1) {
132 NETMGR_LOG_E("exclusionList out of memory");
133 return NETMANAGER_ERR_INTERNAL;
134 }
135 ret = Conv2Ch(exclusion, httpProxy->exclusionList[i++]);
136 if (ret != NETMANAGER_SUCCESS) {
137 return ret;
138 }
139 }
140
141 httpProxy->exclusionListSize = static_cast<int32_t>(httpProxyObj.GetExclusionList().size());
142
143 return NETMANAGER_SUCCESS;
144 }
145
ConvertNetConn2HttpProxy(const NetConn_HttpProxy & netConn,HttpProxy & httpProxyObj)146 void ConvertNetConn2HttpProxy(const NetConn_HttpProxy &netConn, HttpProxy &httpProxyObj)
147 {
148 httpProxyObj.SetHost(std::string(netConn.host));
149 httpProxyObj.SetPort(netConn.port);
150 std::list<std::string> exclusionList;
151 for (int32_t i = 0; i < netConn.exclusionListSize; i++) {
152 exclusionList.emplace_back(netConn.exclusionList[i]);
153 }
154 httpProxyObj.SetExclusionList(exclusionList);
155 }
156
Conv2NetLinkInfo(NetLinkInfo & infoObj,NetConn_ConnectionProperties * prop)157 int32_t Conv2NetLinkInfo(NetLinkInfo &infoObj, NetConn_ConnectionProperties *prop)
158 {
159 int32_t ret = Conv2Ch(infoObj.ifaceName_, prop->ifaceName);
160 if (ret != NETMANAGER_SUCCESS) {
161 return ret;
162 }
163 ret = Conv2Ch(infoObj.domain_, prop->domain);
164 if (ret != NETMANAGER_SUCCESS) {
165 return ret;
166 }
167 ret = Conv2Ch(infoObj.tcpBufferSizes_, prop->tcpBufferSizes);
168 if (ret != NETMANAGER_SUCCESS) {
169 return ret;
170 }
171
172 int32_t i = 0;
173 for (const auto &netAddr : infoObj.netAddrList_) {
174 if (i > NETCONN_MAX_ADDR_SIZE - 1) {
175 NETMGR_LOG_E("netAddrList out of memory");
176 return NETMANAGER_ERR_INTERNAL;
177 }
178 ret = Conv2INetAddr(netAddr, &(prop->netAddrList[i++]));
179 if (ret != NETMANAGER_SUCCESS) {
180 return ret;
181 }
182 }
183 prop->netAddrListSize = static_cast<int32_t>(infoObj.netAddrList_.size());
184
185 i = 0;
186 for (const auto &dns : infoObj.dnsList_) {
187 if (i > NETCONN_MAX_ADDR_SIZE - 1) {
188 NETMGR_LOG_E("dnsList out of memory");
189 return NETMANAGER_ERR_INTERNAL;
190 }
191 ret = Conv2INetAddr(dns, &(prop->dnsList[i++]));
192 if (ret != NETMANAGER_SUCCESS) {
193 return ret;
194 }
195 }
196 prop->dnsListSize = static_cast<int32_t>(infoObj.dnsList_.size());
197
198 ret = Conv2HttpProxy(infoObj.httpProxy_, &(prop->httpProxy));
199 if (ret != NETMANAGER_SUCCESS) {
200 return ret;
201 }
202
203 return NETMANAGER_SUCCESS;
204 }
205
Conv2NetAllCapabilities(NetAllCapabilities & netAllCapsObj,NetConn_NetCapabilities * netAllCaps)206 int32_t Conv2NetAllCapabilities(NetAllCapabilities &netAllCapsObj, NetConn_NetCapabilities *netAllCaps)
207 {
208 netAllCaps->linkUpBandwidthKbps = netAllCapsObj.linkUpBandwidthKbps_;
209 netAllCaps->linkDownBandwidthKbps = netAllCapsObj.linkDownBandwidthKbps_;
210
211 int32_t i = 0;
212 for (const auto &netCap : netAllCapsObj.netCaps_) {
213 if (i > NETCONN_MAX_CAP_SIZE - 1) {
214 NETMGR_LOG_E("netCapsList out of memory");
215 return NETMANAGER_ERR_INTERNAL;
216 }
217
218 NetCapMap::iterator iterMap = netCapMap.find(netCap);
219 if (iterMap == netCapMap.end()) {
220 NETMGR_LOG_E("unknown netCapMap key");
221 return NETMANAGER_ERR_INTERNAL;
222 }
223 netAllCaps->netCaps[i++] = iterMap->second;
224 }
225 netAllCaps->netCapsSize = static_cast<int32_t>(netAllCapsObj.netCaps_.size());
226
227 i = 0;
228 for (const auto &bearType : netAllCapsObj.bearerTypes_) {
229 if (i > NETCONN_MAX_BEARER_TYPE_SIZE - 1) {
230 NETMGR_LOG_E("bearerTypes out of memory");
231 return NETMANAGER_ERR_INTERNAL;
232 }
233
234 BearTypeMap::iterator iterMap = bearTypeMap.find(bearType);
235 if (iterMap == bearTypeMap.end()) {
236 NETMGR_LOG_E("unknown bearTypeMap key");
237 return NETMANAGER_ERR_INTERNAL;
238 }
239 netAllCaps->bearerTypes[i++] = iterMap->second;
240 }
241 netAllCaps->bearerTypesSize = static_cast<int32_t>(netAllCapsObj.bearerTypes_.size());
242
243 return NETMANAGER_SUCCESS;
244 }
245
ConvFromNetAllCapabilities(NetAllCapabilities & netAllCapsObj,NetConn_NetCapabilities * netAllCaps)246 int32_t ConvFromNetAllCapabilities(NetAllCapabilities &netAllCapsObj, NetConn_NetCapabilities *netAllCaps)
247 {
248 netAllCapsObj.linkUpBandwidthKbps_ = netAllCaps->linkUpBandwidthKbps;
249 netAllCapsObj.linkDownBandwidthKbps_ = netAllCaps->linkDownBandwidthKbps;
250
251 if (netAllCaps->netCapsSize > NETCONN_MAX_CAP_SIZE) {
252 NETMGR_LOG_E("netCapsList out of memory");
253 return NETMANAGER_ERR_PARAMETER_ERROR;
254 }
255
256 for (int32_t i = 0; i < netAllCaps->netCapsSize; ++i) {
257 auto netCap = netAllCaps->netCaps[i];
258 auto iterMap = reverseNetCapMap.find(netCap);
259 if (iterMap == reverseNetCapMap.end()) {
260 NETMGR_LOG_E("unknown netCapMap key");
261 return NETMANAGER_ERR_PARAMETER_ERROR;
262 }
263 netAllCapsObj.netCaps_.insert(iterMap->second);
264 }
265
266 if (netAllCaps->bearerTypesSize > NETCONN_MAX_BEARER_TYPE_SIZE) {
267 NETMGR_LOG_E("bearerTypes out of memory");
268 return NETMANAGER_ERR_PARAMETER_ERROR;
269 }
270
271 for (int32_t i = 0; i < netAllCaps->bearerTypesSize; ++i) {
272 auto bearType = netAllCaps->bearerTypes[i];
273 auto iterMap = reverseBearTypeMap.find(bearType);
274 if (iterMap == reverseBearTypeMap.end()) {
275 NETMGR_LOG_E("unknown bearTypeMap key");
276 return NETMANAGER_ERR_PARAMETER_ERROR;
277 }
278 netAllCapsObj.bearerTypes_.insert(iterMap->second);
279 }
280
281 return NETMANAGER_SUCCESS;
282 }
283
splitStr(const std::string & str,const char delimiter)284 std::vector<std::string> splitStr(const std::string &str, const char delimiter)
285 {
286 std::vector<std::string> tokens;
287 std::string token;
288 std::istringstream tokenStream(str);
289 while (std::getline(tokenStream, token, delimiter)) {
290 tokens.push_back(token);
291 }
292 return tokens;
293 }
294
Conv2TraceRouteInfoRtt(const std::string & rttStr,uint32_t (* rtt)[NETCONN_MAX_RTT_NUM])295 int32_t Conv2TraceRouteInfoRtt(const std::string &rttStr, uint32_t (*rtt)[NETCONN_MAX_RTT_NUM])
296 {
297 if (rtt == nullptr) {
298 return NETMANAGER_ERR_INTERNAL;
299 }
300 std::vector<std::string> tokens = splitStr(rttStr, ';');
301 uint32_t tokensSize = tokens.size();
302 for (uint32_t i = 0; i < tokensSize; ++i) {
303 if (i >= NETCONN_MAX_RTT_NUM) {
304 return NETMANAGER_SUCCESS;
305 }
306 double num;
307 std::istringstream iss(tokens[i]);
308 if (iss >> num) {
309 (*rtt)[i] = static_cast<uint32_t>(num);
310 }
311 }
312 return NETMANAGER_SUCCESS;
313 }
314
Conv2TraceRouteInfo(const std::string & traceRouteInfoStr,NetConn_TraceRouteInfo * traceRouteInfo,uint32_t maxJumpNumber)315 int32_t Conv2TraceRouteInfo(const std::string &traceRouteInfoStr, NetConn_TraceRouteInfo *traceRouteInfo,
316 uint32_t maxJumpNumber)
317 {
318 if (traceRouteInfo == nullptr) {
319 return NETMANAGER_ERR_INTERNAL;
320 }
321
322 // traceRouteInfo is "1 *.*.*.*;2;3;4 ..." pos is space position
323 const uint32_t pos2 = 2;
324 const uint32_t pos3 = 3;
325 std::vector<std::string> tokens = splitStr(traceRouteInfoStr, ' ');
326 uint32_t tokensSize = static_cast<uint32_t>(tokens.size());
327 for (uint32_t i = 0; i * pos3 < tokensSize; i++) {
328 if (i >= maxJumpNumber) {
329 return NETMANAGER_SUCCESS;
330 }
331 uint32_t num = 0;
332 std::istringstream iss(tokens[i * pos3]);
333 if (iss >> num) {
334 traceRouteInfo[i].jumpNo = static_cast<uint8_t>(num);
335 }
336 if (strcpy_s(traceRouteInfo[i].address, NETCONN_MAX_STR_LEN, tokens[i * pos3 + 1].c_str()) != 0) {
337 NETMGR_LOG_E("Conv2TraceRouteInfo string copy failed");
338 return NETMANAGER_ERR_INTERNAL;
339 }
340 if (Conv2TraceRouteInfoRtt(tokens[i * pos3 + pos2], &traceRouteInfo[i].rtt) != NETMANAGER_SUCCESS) {
341 return NETMANAGER_ERR_INTERNAL;
342 }
343 }
344 return NETMANAGER_SUCCESS;
345 }
346
NetConnCallbackStubAdapter(NetConn_NetConnCallback * callback)347 NetConnCallbackStubAdapter::NetConnCallbackStubAdapter(NetConn_NetConnCallback *callback)
348 {
349 this->callback_.onNetworkAvailable = callback->onNetworkAvailable;
350 this->callback_.onNetCapabilitiesChange = callback->onNetCapabilitiesChange;
351 this->callback_.onConnetionProperties = callback->onConnetionProperties;
352 this->callback_.onNetLost = callback->onNetLost;
353 this->callback_.onNetUnavailable = callback->onNetUnavailable;
354 this->callback_.onNetBlockStatusChange = callback->onNetBlockStatusChange;
355 }
356
NetAvailable(sptr<NetHandle> & netHandle)357 int32_t NetConnCallbackStubAdapter::NetAvailable(sptr<NetHandle> &netHandle)
358 {
359 if (this->callback_.onNetworkAvailable == nullptr || netHandle == nullptr) {
360 return NETMANAGER_SUCCESS;
361 }
362 NetConn_NetHandle netHandleInner;
363 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
364 if (ret != NETMANAGER_SUCCESS) {
365 return ret;
366 }
367
368 this->callback_.onNetworkAvailable(&netHandleInner);
369 return NETMANAGER_SUCCESS;
370 }
371
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)372 int32_t NetConnCallbackStubAdapter::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
373 const sptr<NetAllCapabilities> &netAllCap)
374 {
375 if (this->callback_.onNetCapabilitiesChange == nullptr || netHandle == nullptr || netAllCap == nullptr) {
376 return NETMANAGER_SUCCESS;
377 }
378 NetConn_NetHandle netHandleInner;
379 NetConn_NetCapabilities netAllCapsInner;
380 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
381 if (ret != NETMANAGER_SUCCESS) {
382 return ret;
383 }
384 ret = Conv2NetAllCapabilities(*netAllCap, &netAllCapsInner);
385 if (ret != NETMANAGER_SUCCESS) {
386 return ret;
387 }
388
389 this->callback_.onNetCapabilitiesChange(&netHandleInner, &netAllCapsInner);
390 return NETMANAGER_SUCCESS;
391 }
392
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)393 int32_t NetConnCallbackStubAdapter::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
394 const sptr<NetLinkInfo> &info)
395 {
396 if (this->callback_.onConnetionProperties == nullptr || netHandle == nullptr || info == nullptr) {
397 return NETMANAGER_SUCCESS;
398 }
399 NetConn_NetHandle netHandleInner;
400 NetConn_ConnectionProperties netInfoInner;
401 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
402 if (ret != NETMANAGER_SUCCESS) {
403 return ret;
404 }
405 ret = Conv2NetLinkInfo(*info, &netInfoInner);
406 if (ret != NETMANAGER_SUCCESS) {
407 return ret;
408 }
409
410 this->callback_.onConnetionProperties(&netHandleInner, &netInfoInner);
411 return NETMANAGER_SUCCESS;
412 }
413
NetLost(sptr<NetHandle> & netHandle)414 int32_t NetConnCallbackStubAdapter::NetLost(sptr<NetHandle> &netHandle)
415 {
416 if (this->callback_.onNetLost == nullptr || netHandle == nullptr) {
417 return NETMANAGER_SUCCESS;
418 }
419 NetConn_NetHandle netHandleInner;
420 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
421 if (ret != NETMANAGER_SUCCESS) {
422 return ret;
423 }
424
425 this->callback_.onNetLost(&netHandleInner);
426 return NETMANAGER_SUCCESS;
427 }
428
NetUnavailable()429 int32_t NetConnCallbackStubAdapter::NetUnavailable()
430 {
431 if (this->callback_.onNetUnavailable == nullptr) {
432 return NETMANAGER_SUCCESS;
433 }
434 this->callback_.onNetUnavailable();
435 return NETMANAGER_SUCCESS;
436 }
437
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)438 int32_t NetConnCallbackStubAdapter::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
439 {
440 if (this->callback_.onNetBlockStatusChange == nullptr || netHandle == nullptr) {
441 return NETMANAGER_SUCCESS;
442 }
443 NetConn_NetHandle netHandleInner;
444 int32_t ret = Conv2NetHandle(*netHandle, &netHandleInner);
445 if (ret != NETMANAGER_SUCCESS) {
446 return ret;
447 }
448 this->callback_.onNetBlockStatusChange(&netHandleInner, blocked);
449 return NETMANAGER_SUCCESS;
450 }
451
GetInstance()452 NetConnCallbackManager &NetConnCallbackManager::GetInstance()
453 {
454 static NetConnCallbackManager instance;
455 return instance;
456 }
457
RegisterNetConnCallback(NetConn_NetSpecifier * specifier,NetConn_NetConnCallback * netConnCallback,const uint32_t & timeout,uint32_t * callbackId)458 int32_t NetConnCallbackManager::RegisterNetConnCallback(NetConn_NetSpecifier *specifier,
459 NetConn_NetConnCallback *netConnCallback,
460 const uint32_t &timeout, uint32_t *callbackId)
461 {
462 sptr<NetConnCallbackStubAdapter> callback = sptr<NetConnCallbackStubAdapter>::MakeSptr(netConnCallback);
463 sptr<NetSpecifier> specifierInner = new NetSpecifier;
464
465 if (specifier != nullptr) {
466 int32_t ret = ConvFromNetAllCapabilities(specifierInner->netCapabilities_, &specifier->caps);
467 if (ret != NETMANAGER_SUCCESS) {
468 NETMGR_LOG_E("ConvFromNetAllCapabilities failed");
469 return ret;
470 }
471 if (specifier->bearerPrivateIdentifier != nullptr) {
472 specifierInner->ident_ = std::string(specifier->bearerPrivateIdentifier);
473 }
474 ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifierInner, callback, timeout);
475 if (ret != NETMANAGER_SUCCESS) {
476 NETMGR_LOG_E("RegisterNetConnCallback failed");
477 return ret;
478 }
479 } else {
480 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
481 if (ret != NETMANAGER_SUCCESS) {
482 NETMGR_LOG_E("RegisterNetConnCallback failed");
483 return ret;
484 }
485 }
486
487 std::lock_guard<std::mutex> lock(this->callbackMapMutex_);
488 *callbackId = this->index_++;
489 this->callbackMap_[*callbackId] = callback;
490 return NETMANAGER_SUCCESS;
491 }
492
UnregisterNetConnCallback(uint32_t callbackId)493 int32_t NetConnCallbackManager::UnregisterNetConnCallback(uint32_t callbackId)
494 {
495 std::lock_guard<std::mutex> lock(this->callbackMapMutex_);
496 auto it = this->callbackMap_.find(callbackId);
497 if (it != this->callbackMap_.end()) {
498 int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(it->second);
499 this->callbackMap_.erase(it);
500 return ret;
501 } else {
502 return NET_CONN_ERR_CALLBACK_NOT_FOUND;
503 }
504 }
505
506 } // namespace OHOS::NetManagerStandard
507