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