1 /*
2 * Copyright (c) 2021-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 <atomic>
17 #include <cinttypes>
18
19 #include "broadcast_manager.h"
20 #include "net_manager_constants.h"
21 #include "net_mgr_log_wrapper.h"
22 #include "net_supplier.h"
23 #include "netsys_controller.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 constexpr int32_t REG_OK = 0;
29 constexpr const char *SIMID_IDENT_PREFIX = "simId";
30 }
31 static std::atomic<uint32_t> g_nextNetSupplierId = 0x03EB;
32
NetSupplier(NetBearType bearerType,const std::string & netSupplierIdent,const std::set<NetCap> & netCaps)33 NetSupplier::NetSupplier(NetBearType bearerType, const std::string &netSupplierIdent, const std::set<NetCap> &netCaps)
34 : netSupplierType_(bearerType),
35 netSupplierIdent_(netSupplierIdent),
36 netCaps_(netCaps),
37 supplierId_(g_nextNetSupplierId++)
38 {
39 netAllCapabilities_.netCaps_ = netCaps;
40 netAllCapabilities_.bearerTypes_.insert(bearerType);
41 ResetNetSupplier();
42 InitNetScore();
43 }
44
GetSupplierCallback()45 sptr<INetSupplierCallback> NetSupplier::GetSupplierCallback()
46 {
47 return netController_;
48 }
49
RegisterSupplierCallback(const sptr<INetSupplierCallback> & callback)50 void NetSupplier::RegisterSupplierCallback(const sptr<INetSupplierCallback> &callback)
51 {
52 netController_ = callback;
53 }
54
InitNetScore()55 void NetSupplier::InitNetScore()
56 {
57 int32_t netScore = 0;
58 auto iter = netTypeScore_.find(netSupplierType_);
59 if (iter == netTypeScore_.end()) {
60 NETMGR_LOG_E("Can not find net bearer type[%{public}d] for this net service", netSupplierType_);
61 return;
62 }
63 NETMGR_LOG_D("Net type[%{public}d],default score[%{public}d]",
64 static_cast<int32_t>(iter->first), static_cast<int32_t>(iter->second));
65 netScore = static_cast<int32_t>(iter->second);
66 netScore_ = netScore;
67 NETMGR_LOG_D("netScore_ = %{public}d", netScore_);
68 }
69
70 /**
71 * Reset all attributes that may change in the supplier, such as detection progress and network quality.
72 */
ResetNetSupplier()73 void NetSupplier::ResetNetSupplier()
74 {
75 // Reset network quality.
76 netQuality_ = QUALITY_NORMAL_STATE;
77 // Reset network detection progress.
78 isFirstTimeDetectionDone = false;
79 //Reset User Selection
80 isAcceptUnvaliad = false;
81 // Reset network capabilities for checking connectivity finished flag.
82 netCaps_.InsertNetCap(NET_CAPABILITY_CHECKING_CONNECTIVITY);
83 // Reset network verification status to validated.
84 SetNetValid(VERIFICATION_STATE);
85 // Reset checking connectivity flag.
86 netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_CHECKING_CONNECTIVITY);
87 // Reset network extAttribute.
88 netExtAttribute_ = "";
89 NETMGR_LOG_I("Reset net supplier %{public}u", supplierId_);
90 }
91
operator ==(const NetSupplier & netSupplier) const92 bool NetSupplier::operator==(const NetSupplier &netSupplier) const
93 {
94 return supplierId_ == netSupplier.supplierId_ && netSupplierType_ == netSupplier.netSupplierType_ &&
95 netSupplierIdent_ == netSupplier.netSupplierIdent_ && netCaps_ == netSupplier.netCaps_;
96 }
97
UpdateNetSupplierInfo(const NetSupplierInfo & netSupplierInfo)98 void NetSupplier::UpdateNetSupplierInfo(const NetSupplierInfo &netSupplierInfo)
99 {
100 NETMGR_LOG_D("Update net supplier[%{public}d, %{public}s], netSupplierInfo[%{public}s]", supplierId_,
101 netSupplierIdent_.c_str(), netSupplierInfo_.ToString("").c_str());
102 bool oldAvailable = netSupplierInfo_.isAvailable_;
103 netSupplierInfo_ = netSupplierInfo;
104 netAllCapabilities_.linkUpBandwidthKbps_ = netSupplierInfo_.linkUpBandwidthKbps_;
105 netAllCapabilities_.linkDownBandwidthKbps_ = netSupplierInfo_.linkDownBandwidthKbps_;
106 if (!netSupplierInfo_.ident_.empty()) {
107 netSupplierIdent_ = netSupplierInfo_.ident_;
108 }
109 if (netSupplierInfo_.score_ != 0) {
110 netScore_ = netSupplierInfo_.score_;
111 }
112 if (oldAvailable == netSupplierInfo_.isAvailable_) {
113 NETMGR_LOG_W("Same supplier available status:[%{public}d]", oldAvailable);
114 return;
115 }
116 if (network_ == nullptr) {
117 NETMGR_LOG_E("network_ is nullptr!");
118 return;
119 }
120 network_->UpdateBasicNetwork(netSupplierInfo_.isAvailable_);
121 if (!netSupplierInfo_.isAvailable_) {
122 UpdateNetConnState(NET_CONN_STATE_DISCONNECTED);
123 }
124 }
125
UpdateNetLinkInfo(NetLinkInfo & netLinkInfo)126 int32_t NetSupplier::UpdateNetLinkInfo(NetLinkInfo &netLinkInfo)
127 {
128 if (network_ == nullptr) {
129 NETMGR_LOG_E("network_ is nullptr!");
130 return NET_CONN_ERR_INVALID_NETWORK;
131 }
132
133 if (GetNetSupplierIdent().substr(0, strlen(SIMID_IDENT_PREFIX)) == SIMID_IDENT_PREFIX) {
134 netLinkInfo.ident_ = GetNetSupplierIdent().substr(strlen(SIMID_IDENT_PREFIX));
135 }
136 NETMGR_LOG_D("Update netlink info: netLinkInfo[%{public}s]", netLinkInfo.ToString(" ").c_str());
137 if (!network_->UpdateNetLinkInfo(netLinkInfo)) {
138 return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
139 }
140 UpdateNetConnState(NET_CONN_STATE_CONNECTED);
141 return NETMANAGER_SUCCESS;
142 }
143
GetNetSupplierType() const144 NetBearType NetSupplier::GetNetSupplierType() const
145 {
146 return netSupplierType_;
147 }
148
GetNetSupplierIdent() const149 std::string NetSupplier::GetNetSupplierIdent() const
150 {
151 return netSupplierIdent_;
152 }
153
CompareNetCaps(const std::set<NetCap> caps) const154 bool NetSupplier::CompareNetCaps(const std::set<NetCap> caps) const
155 {
156 if (caps.empty()) {
157 return true;
158 }
159 return netCaps_.HasNetCaps(caps);
160 }
161
HasNetCap(NetCap cap) const162 bool NetSupplier::HasNetCap(NetCap cap) const
163 {
164 return netCaps_.HasNetCap(cap);
165 }
166
HasNetCaps(const std::set<NetCap> & caps) const167 bool NetSupplier::HasNetCaps(const std::set<NetCap> &caps) const
168 {
169 return netCaps_.HasNetCaps(caps);
170 }
171
GetNetCaps() const172 const NetCaps &NetSupplier::GetNetCaps() const
173 {
174 return netCaps_;
175 }
176
GetNetCapabilities() const177 NetAllCapabilities NetSupplier::GetNetCapabilities() const
178 {
179 return netAllCapabilities_;
180 }
181
SetNetwork(const std::shared_ptr<Network> & network)182 void NetSupplier::SetNetwork(const std::shared_ptr<Network> &network)
183 {
184 network_ = network;
185 if (network_ != nullptr) {
186 netHandle_ = std::make_unique<NetHandle>(network_->GetNetId()).release();
187 }
188 }
189
GetNetwork() const190 std::shared_ptr<Network> NetSupplier::GetNetwork() const
191 {
192 return network_;
193 }
194
GetNetId() const195 int32_t NetSupplier::GetNetId() const
196 {
197 if (network_ == nullptr) {
198 return INVALID_NET_ID;
199 }
200 return network_->GetNetId();
201 }
202
GetNetHandle() const203 sptr<NetHandle> NetSupplier::GetNetHandle() const
204 {
205 return netHandle_;
206 }
207
GetHttpProxy(HttpProxy & httpProxy)208 void NetSupplier::GetHttpProxy(HttpProxy &httpProxy)
209 {
210 if (network_ == nullptr) {
211 NETMGR_LOG_E("network_ is nullptr.");
212 return;
213 }
214 httpProxy = network_->GetHttpProxy();
215 }
216
GetSupplierId() const217 uint32_t NetSupplier::GetSupplierId() const
218 {
219 return supplierId_;
220 }
221
GetRoaming() const222 bool NetSupplier::GetRoaming() const
223 {
224 return netSupplierInfo_.isRoaming_;
225 }
226
GetStrength() const227 int8_t NetSupplier::GetStrength() const
228 {
229 return netSupplierInfo_.strength_;
230 }
231
GetFrequency() const232 uint16_t NetSupplier::GetFrequency() const
233 {
234 return netSupplierInfo_.frequency_;
235 }
236
GetSupplierUid() const237 int32_t NetSupplier::GetSupplierUid() const
238 {
239 return netSupplierInfo_.uid_;
240 }
241
GetUid() const242 int32_t NetSupplier::GetUid() const
243 {
244 return uid_;
245 }
246
SetUid(int32_t uid)247 void NetSupplier::SetUid(int32_t uid)
248 {
249 uid_ = uid;
250 }
251
IsAvailable() const252 bool NetSupplier::IsAvailable() const
253 {
254 return netSupplierInfo_.isAvailable_;
255 }
256
SupplierConnection(const std::set<NetCap> & netCaps,const NetRequest & netRequest)257 bool NetSupplier::SupplierConnection(const std::set<NetCap> &netCaps, const NetRequest &netRequest)
258 {
259 NETMGR_LOG_D("Supplier[%{public}d, %{public}s] request connect, available=%{public}d", supplierId_,
260 netSupplierIdent_.c_str(), netSupplierInfo_.isAvailable_);
261 if (netSupplierInfo_.isAvailable_ && netRequest.ident.empty()) {
262 NETMGR_LOG_D("The supplier is currently available, there is no need to repeat the request for connection.");
263 return true;
264 }
265 if (!(netSupplierType_ == NetBearType::BEARER_WIFI && !netRequest.ident.empty())) {
266 UpdateNetConnState(NET_CONN_STATE_IDLE);
267 }
268
269 if (netController_ == nullptr) {
270 NETMGR_LOG_E("netController_ is nullptr");
271 return false;
272 }
273 NETMGR_LOG_D("execute RequestNetwork");
274 int32_t errCode = netController_->RequestNetwork(netSupplierIdent_, netCaps, netRequest);
275 NETMGR_LOG_D("RequestNetwork errCode[%{public}d]", errCode);
276 if (errCode != REG_OK) {
277 NETMGR_LOG_E("RequestNetwork fail");
278 return false;
279 }
280 return true;
281 }
282
SetRestrictBackground(bool restrictBackground)283 void NetSupplier::SetRestrictBackground(bool restrictBackground)
284 {
285 restrictBackground_ = restrictBackground;
286 }
GetRestrictBackground() const287 bool NetSupplier::GetRestrictBackground() const
288 {
289 return restrictBackground_;
290 }
291
SupplierDisconnection(const std::set<NetCap> & netCaps,uint32_t uid)292 bool NetSupplier::SupplierDisconnection(const std::set<NetCap> &netCaps, uint32_t uid)
293 {
294 NETMGR_LOG_D("Supplier[%{public}d, %{public}s] request disconnect, available=%{public}d", supplierId_,
295 netSupplierIdent_.c_str(), netSupplierInfo_.isAvailable_);
296 bool isInternal = HasNetCap(NET_CAPABILITY_INTERNAL_DEFAULT);
297 bool isXcap = HasNetCap(NET_CAPABILITY_XCAP);
298 bool isMms = HasNetCap(NET_CAPABILITY_MMS);
299 if (!netSupplierInfo_.isAvailable_ && !isInternal && !isXcap && !isMms) {
300 NETMGR_LOG_D("The supplier is currently unavailable, there is no need to repeat the request to disconnect.");
301 return true;
302 }
303 if (netController_ == nullptr) {
304 NETMGR_LOG_E("netController_ is nullptr");
305 return false;
306 }
307 NETMGR_LOG_D("execute ReleaseNetwork, supplierId[%{public}d]", supplierId_);
308 NetRequest request;
309 request.uid = uid;
310 request.ident = netSupplierIdent_;
311 request.netCaps = netCaps;
312 int32_t errCode = netController_->ReleaseNetwork(request);
313 NETMGR_LOG_D("ReleaseNetwork retCode[%{public}d]", errCode);
314 if (errCode != REG_OK) {
315 NETMGR_LOG_E("ReleaseNetwork fail");
316 return false;
317 }
318 return true;
319 }
320
UpdateNetConnState(NetConnState netConnState)321 void NetSupplier::UpdateNetConnState(NetConnState netConnState)
322 {
323 if (network_) {
324 network_->UpdateNetConnState(netConnState);
325 }
326 }
327
IsConnecting() const328 bool NetSupplier::IsConnecting() const
329 {
330 if (network_) {
331 return network_->IsConnecting();
332 }
333 return false;
334 }
335
IsConnected() const336 bool NetSupplier::IsConnected() const
337 {
338 if (network_) {
339 return network_->IsConnected();
340 }
341 return false;
342 }
343
RequestToConnect(const NetRequest & netrequest)344 bool NetSupplier::RequestToConnect(const NetRequest &netrequest)
345 {
346 if (requestList_.find(netrequest.requestId) == requestList_.end()) {
347 requestList_.insert(netrequest.requestId);
348 }
349 AddRequest(netrequest);
350 return SupplierConnection(netCaps_.ToSet(), netrequest);
351 }
352
SelectAsBestNetwork(const NetRequest & netrequest)353 int32_t NetSupplier::SelectAsBestNetwork(const NetRequest &netrequest)
354 {
355 NETMGR_LOG_I("Request[%{public}d] select [%{public}d, %{public}s] as best network", netrequest.requestId,
356 supplierId_, netSupplierIdent_.c_str());
357 if (requestList_.find(netrequest.requestId) == requestList_.end()) {
358 requestList_.insert(netrequest.requestId);
359 }
360 if (bestReqList_.find(netrequest.requestId) == bestReqList_.end()) {
361 bestReqList_.insert(netrequest.requestId);
362 }
363 AddRequest(netrequest);
364 return NETMANAGER_SUCCESS;
365 }
366
ReceiveBestScore(int32_t bestScore,uint32_t supplierId,const NetRequest & netrequest)367 void NetSupplier::ReceiveBestScore(int32_t bestScore, uint32_t supplierId, const NetRequest &netrequest)
368 {
369 NETMGR_LOG_D("Supplier[%{public}d, %{public}s] receive best score, bestSupplierId[%{public}d]", supplierId_,
370 netSupplierIdent_.c_str(), supplierId);
371 if (supplierId == supplierId_) {
372 NETMGR_LOG_D("Same net supplier, no need to disconnect.");
373 return;
374 }
375 if (requestList_.empty() && HasNetCap(NET_CAPABILITY_INTERNET)) {
376 SupplierDisconnection(netCaps_.ToSet(), netrequest.uid);
377 return;
378 }
379 if (requestList_.find(netrequest.requestId) == requestList_.end()) {
380 NETMGR_LOG_D("Can not find request[%{public}d]", netrequest.requestId);
381 return;
382 }
383 if (netScore_ >= bestScore) {
384 NETMGR_LOG_D("High priority network, no need to disconnect");
385 return;
386 }
387 requestList_.erase(netrequest.requestId);
388 NETMGR_LOG_D("Supplier[%{public}d, %{public}s] remaining request list size[%{public}zd]", supplierId_,
389 netSupplierIdent_.c_str(), requestList_.size());
390 SupplierDisconnection(netCaps_.ToSet(), netrequest.uid);
391 }
392
CancelRequest(const NetRequest & netrequest)393 int32_t NetSupplier::CancelRequest(const NetRequest &netrequest)
394 {
395 auto iter = requestList_.find(netrequest.requestId);
396 if (iter == requestList_.end()) {
397 return NET_CONN_ERR_SERVICE_NO_REQUEST;
398 }
399 NETMGR_LOG_I("CancelRequest requestId:%{public}u", netrequest.requestId);
400 requestList_.erase(netrequest.requestId);
401 bestReqList_.erase(netrequest.requestId);
402 SupplierDisconnection(netCaps_.ToSet(), netrequest.uid);
403 return NETMANAGER_SUCCESS;
404 }
405
AddRequest(const NetRequest & netRequest)406 void NetSupplier::AddRequest(const NetRequest &netRequest)
407 {
408 if (netController_ == nullptr) {
409 NETMGR_LOG_E("netController_ is nullptr");
410 return;
411 }
412 NetRequest request;
413 request.uid = netRequest.uid;
414 request.ident = netSupplierIdent_;
415 request.netCaps = netCaps_.ToSet();
416 NETMGR_LOG_D("execute AddRequest");
417 int32_t errCode = netController_->AddRequest(request);
418 NETMGR_LOG_D("AddRequest errCode[%{public}d]", errCode);
419 if (errCode != REG_OK) {
420 NETMGR_LOG_E("AddRequest fail");
421 return;
422 }
423 return;
424 }
425
RemoveBestRequest(uint32_t reqId)426 void NetSupplier::RemoveBestRequest(uint32_t reqId)
427 {
428 auto iter = bestReqList_.find(reqId);
429 if (iter == bestReqList_.end()) {
430 return;
431 }
432 bestReqList_.erase(reqId);
433 NETMGR_LOG_I("RemoveBestRequest supplierId=[%{public}d], reqId=[%{public}u]", supplierId_, reqId);
434 }
435
GetBestRequestList()436 std::set<uint32_t> &NetSupplier::GetBestRequestList()
437 {
438 return bestReqList_;
439 }
440
SetNetValid(NetDetectionStatus netState)441 void NetSupplier::SetNetValid(NetDetectionStatus netState)
442 {
443 NETMGR_LOG_I("Enter SetNetValid. supplier[%{public}d, %{public}s], ifValid[%{public}d]", supplierId_,
444 netSupplierIdent_.c_str(), netState);
445 if (netState == VERIFICATION_STATE) {
446 if (!HasNetCap(NET_CAPABILITY_VALIDATED)) {
447 netCaps_.InsertNetCap(NET_CAPABILITY_VALIDATED);
448 netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_VALIDATED);
449 NETMGR_LOG_I("NetSupplier inserted cap:NET_CAPABILITY_VALIDATED");
450 }
451 if (HasNetCap(NET_CAPABILITY_PORTAL)) {
452 netCaps_.RemoveNetCap(NET_CAPABILITY_PORTAL);
453 netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_PORTAL);
454 NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_PORTAL, need to clear DNS cache");
455 int32_t ret = NetsysController::GetInstance().FlushDnsCache(network_->GetNetId());
456 if (ret != NETMANAGER_SUCCESS) {
457 NETMGR_LOG_E("FlushDnsCache failed, ret = %{public}d", ret);
458 }
459 }
460 } else if (netState == CAPTIVE_PORTAL_STATE) {
461 if (!HasNetCap(NET_CAPABILITY_PORTAL)) {
462 netCaps_.InsertNetCap(NET_CAPABILITY_PORTAL);
463 netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_PORTAL);
464 NETMGR_LOG_I("NetSupplier inserted cap:NET_CAPABILITY_PORTAL");
465 }
466 if (HasNetCap(NET_CAPABILITY_VALIDATED)) {
467 netCaps_.RemoveNetCap(NET_CAPABILITY_VALIDATED);
468 netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_VALIDATED);
469 NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_VALIDATED");
470 }
471 } else if (netState == QUALITY_POOR_STATE) {
472 netQuality_ = QUALITY_POOR_STATE;
473 } else if (netState == QUALITY_GOOD_STATE) {
474 netQuality_ = QUALITY_GOOD_STATE;
475 } else if (netState == ACCEPT_UNVALIDATED) {
476 netQuality_ = ACCEPT_UNVALIDATED;
477 isAcceptUnvaliad = true;
478 } else {
479 if (HasNetCap(NET_CAPABILITY_VALIDATED)) {
480 netCaps_.RemoveNetCap(NET_CAPABILITY_VALIDATED);
481 netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_VALIDATED);
482 NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_VALIDATED");
483 }
484 if (HasNetCap(NET_CAPABILITY_PORTAL)) {
485 netCaps_.RemoveNetCap(NET_CAPABILITY_PORTAL);
486 netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_PORTAL);
487 NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_PORTAL");
488 }
489 }
490 }
491
IsNetValidated() const492 bool NetSupplier::IsNetValidated() const
493 {
494 return HasNetCap(NET_CAPABILITY_VALIDATED) && !HasNetCap(NET_CAPABILITY_CHECKING_CONNECTIVITY);
495 }
496
497 /**
498 * This method returns the score of the current network supplier.
499 *
500 * It is used to prioritize network suppliers so that higher priority producers can activate when lower
501 * priority networks are available.
502 *
503 * @return the score of the current network supplier.
504 */
GetNetScore() const505 int32_t NetSupplier::GetNetScore() const
506 {
507 return netScore_;
508 }
509
510 /**
511 * This method returns the real score of current network supplier.
512 *
513 * This method subtracts the score depending on different conditions, or returns netScore_ if the conditions are not
514 * met.
515 * It is used to compare the priorities of different networks.
516 *
517 * @return the real score of current network supplier.
518 */
GetRealScore()519 int32_t NetSupplier::GetRealScore()
520 {
521 // Notice: the order is important here:
522 // 1.If the user chooses to use this network, return MAX_SCORE
523 if (isAcceptUnvaliad) {
524 return static_cast<int32_t>(NetManagerStandard::NetTypeScoreValue::MAX_SCORE);
525 }
526
527 // 2. If network detection is not complete in the first time, subtract NET_VALID_SCORE.
528 if (IsInFirstTimeDetecting()) {
529 return netScore_ - NET_VALID_SCORE;
530 }
531
532 // 3. If network is not validated, subtract NET_VALID_SCORE.
533 if (!IsNetValidated()) {
534 return netScore_ - NET_VALID_SCORE;
535 }
536
537 // 4. Deduct DIFF_SCORE_BETWEEN_GOOD_POOR for poor network quality (reported by the supplier).
538 if (IsNetQualityPoor()) {
539 return netScore_ - DIFF_SCORE_BETWEEN_GOOD_POOR;
540 }
541 return netScore_;
542 }
543
SetDefault()544 void NetSupplier::SetDefault()
545 {
546 NETMGR_LOG_I("set default supplier[%{public}d].", supplierId_);
547 if (network_) {
548 network_->SetDefaultNetWork();
549 }
550 }
551
ClearDefault()552 void NetSupplier::ClearDefault()
553 {
554 NETMGR_LOG_I("clear default supplier[%{public}d].", supplierId_);
555 if (network_) {
556 network_->ClearDefaultNetWorkNetId();
557 }
558 }
559
UpdateGlobalHttpProxy(const HttpProxy & httpProxy)560 void NetSupplier::UpdateGlobalHttpProxy(const HttpProxy &httpProxy)
561 {
562 NETMGR_LOG_I("supplierId[%{public}d] update global httpProxy.", supplierId_);
563 if (network_) {
564 network_->UpdateGlobalHttpProxy(httpProxy);
565 }
566 }
567
TechToType(NetSlotTech techType)568 std::string NetSupplier::TechToType(NetSlotTech techType)
569 {
570 switch (techType) {
571 case NetSlotTech::SLOT_TYPE_GSM:
572 return "2G";
573 case NetSlotTech::SLOT_TYPE_LTE:
574 case NetSlotTech::SLOT_TYPE_LTE_CA:
575 return "4G";
576 default:
577 return "3G";
578 }
579 }
580
SetSupplierType(int32_t type)581 void NetSupplier::SetSupplierType(int32_t type)
582 {
583 NETMGR_LOG_I("supplierId[%{public}d] update type[%{public}d].", supplierId_, type);
584 type_ = TechToType(static_cast<NetSlotTech>(type));
585 }
586
GetSupplierType()587 std::string NetSupplier::GetSupplierType()
588 {
589 return type_;
590 }
591
ResumeNetworkInfo()592 bool NetSupplier::ResumeNetworkInfo()
593 {
594 if (network_ == nullptr) {
595 NETMGR_LOG_E("network_ is nullptr!");
596 return false;
597 }
598
599 return network_->ResumeNetworkInfo();
600 }
601
IsNetQualityPoor()602 bool NetSupplier::IsNetQualityPoor()
603 {
604 return netQuality_ == QUALITY_POOR_STATE;
605 }
606
IsNetAcceptUnavalidate()607 bool NetSupplier::IsNetAcceptUnavalidate()
608 {
609 return netQuality_ == ACCEPT_UNVALIDATED;
610 }
611
SetDetectionDone()612 void NetSupplier::SetDetectionDone()
613 {
614 if (!isFirstTimeDetectionDone) {
615 isFirstTimeDetectionDone = true;
616 }
617 if (HasNetCap(NET_CAPABILITY_CHECKING_CONNECTIVITY)) {
618 netCaps_.RemoveNetCap(NET_CAPABILITY_CHECKING_CONNECTIVITY);
619 netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_CHECKING_CONNECTIVITY);
620 NETMGR_LOG_I("supplier %{public}u detection done, remove NET_CAPABILITY_CHECKING_CONNECTIVITY", supplierId_);
621 }
622 }
623
IsInFirstTimeDetecting() const624 bool NetSupplier::IsInFirstTimeDetecting() const
625 {
626 return !isFirstTimeDetectionDone;
627 }
628
SetReuseCap(NetCap reuseCap,bool add)629 void NetSupplier::SetReuseCap(NetCap reuseCap, bool add)
630 {
631 if (add) {
632 netCaps_.InsertNetCap(reuseCap);
633 netAllCapabilities_.netCaps_.insert(reuseCap);
634 } else {
635 netCaps_.RemoveNetCap(reuseCap);
636 netAllCapabilities_.netCaps_.erase(reuseCap);
637 }
638 }
639
GetNetExtAttribute()640 std::string NetSupplier::GetNetExtAttribute()
641 {
642 if (netExtAttribute_.empty() && netHandle_ != nullptr) {
643 NETMGR_LOG_E("supplier %{public}u, netId: %{public}d, get netExtAttribute is empty",
644 supplierId_, netHandle_->GetNetId());
645 }
646 return netExtAttribute_;
647 }
648
SetNetExtAttribute(const std::string & netExtAttribute)649 void NetSupplier::SetNetExtAttribute(const std::string &netExtAttribute)
650 {
651 if (netHandle_ != nullptr) {
652 NETMGR_LOG_I("supplier %{public}u, netId: %{public}d, set netExtAtt: [length: %{public}d, value: %{private}s]",
653 supplierId_, netHandle_->GetNetId(), (int)netExtAttribute.size(), netExtAttribute.c_str());
654 }
655 netExtAttribute_ = netExtAttribute;
656 }
657 } // namespace NetManagerStandard
658 } // namespace OHOS
659