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_->GetNetLinkInfo().httpProxy_;
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     UpdateNetConnState(NET_CONN_STATE_IDLE);
263 
264     if (netController_ == nullptr) {
265         NETMGR_LOG_E("netController_ is nullptr");
266         return false;
267     }
268     NETMGR_LOG_D("execute RequestNetwork");
269     int32_t errCode = netController_->RequestNetwork(netSupplierIdent_, netCaps, netRequest);
270     NETMGR_LOG_D("RequestNetwork errCode[%{public}d]", errCode);
271     if (errCode != REG_OK) {
272         NETMGR_LOG_E("RequestNetwork fail");
273         return false;
274     }
275     return true;
276 }
277 
SetRestrictBackground(bool restrictBackground)278 void NetSupplier::SetRestrictBackground(bool restrictBackground)
279 {
280     restrictBackground_ = restrictBackground;
281 }
GetRestrictBackground() const282 bool NetSupplier::GetRestrictBackground() const
283 {
284     return restrictBackground_;
285 }
286 
SupplierDisconnection(const std::set<NetCap> &netCaps)287 bool NetSupplier::SupplierDisconnection(const std::set<NetCap> &netCaps)
288 {
289     NETMGR_LOG_D("Supplier[%{public}d, %{public}s] request disconnect, available=%{public}d", supplierId_,
290                  netSupplierIdent_.c_str(), netSupplierInfo_.isAvailable_);
291     if (!netSupplierInfo_.isAvailable_) {
292         NETMGR_LOG_D("The supplier is currently unavailable, there is no need to repeat the request to disconnect.");
293         return true;
294     }
295     if (netController_ == nullptr) {
296         NETMGR_LOG_E("netController_ is nullptr");
297         return false;
298     }
299     NETMGR_LOG_D("execute ReleaseNetwork, supplierId[%{public}d]", supplierId_);
300     int32_t errCode = netController_->ReleaseNetwork(netSupplierIdent_, netCaps);
301     NETMGR_LOG_D("ReleaseNetwork retCode[%{public}d]", errCode);
302     if (errCode != REG_OK) {
303         NETMGR_LOG_E("ReleaseNetwork fail");
304         return false;
305     }
306     return true;
307 }
308 
UpdateNetConnState(NetConnState netConnState)309 void NetSupplier::UpdateNetConnState(NetConnState netConnState)
310 {
311     if (network_) {
312         network_->UpdateNetConnState(netConnState);
313     }
314 }
315 
IsConnecting() const316 bool NetSupplier::IsConnecting() const
317 {
318     if (network_) {
319         return network_->IsConnecting();
320     }
321     return false;
322 }
323 
IsConnected() const324 bool NetSupplier::IsConnected() const
325 {
326     if (network_) {
327         return network_->IsConnected();
328     }
329     return false;
330 }
331 
RequestToConnect(const NetRequest &netrequest)332 bool NetSupplier::RequestToConnect(const NetRequest &netrequest)
333 {
334     if (requestList_.find(netrequest.requestId) == requestList_.end()) {
335         requestList_.insert(netrequest.requestId);
336     }
337     AddRequest(netrequest);
338     return SupplierConnection(netCaps_.ToSet(), netrequest);
339 }
340 
SelectAsBestNetwork(const NetRequest &netrequest)341 int32_t NetSupplier::SelectAsBestNetwork(const NetRequest &netrequest)
342 {
343     NETMGR_LOG_I("Request[%{public}d] select [%{public}d, %{public}s] as best network", netrequest.requestId,
344                  supplierId_, netSupplierIdent_.c_str());
345     if (requestList_.find(netrequest.requestId) == requestList_.end()) {
346         requestList_.insert(netrequest.requestId);
347     }
348     if (bestReqList_.find(netrequest.requestId) == bestReqList_.end()) {
349         bestReqList_.insert(netrequest.requestId);
350     }
351     AddRequest(netrequest);
352     return NETMANAGER_SUCCESS;
353 }
354 
ReceiveBestScore(int32_t bestScore, uint32_t supplierId, const NetRequest &netrequest)355 void NetSupplier::ReceiveBestScore(int32_t bestScore, uint32_t supplierId, const NetRequest &netrequest)
356 {
357     NETMGR_LOG_D("Supplier[%{public}d, %{public}s] receive best score, bestSupplierId[%{public}d]", supplierId_,
358                  netSupplierIdent_.c_str(), supplierId);
359     if (supplierId == supplierId_) {
360         NETMGR_LOG_D("Same net supplier, no need to disconnect.");
361         return;
362     }
363     if (requestList_.empty()) {
364         SupplierDisconnection(netCaps_.ToSet());
365         return;
366     }
367     if (requestList_.find(netrequest.requestId) == requestList_.end()) {
368         NETMGR_LOG_W("Can not find request[%{public}d]", netrequest.requestId);
369         return;
370     }
371     if (netScore_ >= bestScore) {
372         NETMGR_LOG_D("High priority network, no need to disconnect");
373         return;
374     }
375     requestList_.erase(netrequest.requestId);
376     NETMGR_LOG_D("Supplier[%{public}d, %{public}s] remaining request list size[%{public}zd]", supplierId_,
377                  netSupplierIdent_.c_str(), requestList_.size());
378     if (requestList_.empty()) {
379         SupplierDisconnection(netCaps_.ToSet());
380     }
381     RemoveRequest(netrequest);
382 }
383 
CancelRequest(const NetRequest &netrequest)384 int32_t NetSupplier::CancelRequest(const NetRequest &netrequest)
385 {
386     auto iter = requestList_.find(netrequest.requestId);
387     if (iter == requestList_.end()) {
388         return NET_CONN_ERR_SERVICE_NO_REQUEST;
389     }
390     NETMGR_LOG_I("CancelRequest requestId:%{public}u", netrequest.requestId);
391     requestList_.erase(netrequest.requestId);
392     if (requestList_.empty()) {
393         SupplierDisconnection(netCaps_.ToSet());
394     }
395     bestReqList_.erase(netrequest.requestId);
396     RemoveRequest(netrequest);
397     return NETMANAGER_SUCCESS;
398 }
399 
AddRequest(const NetRequest &netRequest)400 void NetSupplier::AddRequest(const NetRequest &netRequest)
401 {
402     if (netController_ == nullptr) {
403         NETMGR_LOG_E("netController_ is nullptr");
404         return;
405     }
406     NetRequest request;
407     request.uid = netRequest.uid;
408     request.ident = netSupplierIdent_;
409     request.netCaps = netCaps_.ToSet();
410     NETMGR_LOG_D("execute AddRequest");
411     int32_t errCode = netController_->AddRequest(request);
412     NETMGR_LOG_D("AddRequest errCode[%{public}d]", errCode);
413     if (errCode != REG_OK) {
414         NETMGR_LOG_E("AddRequest fail");
415         return;
416     }
417     return;
418 }
419 
RemoveRequest(const NetRequest &netrequest)420 void NetSupplier::RemoveRequest(const NetRequest &netrequest)
421 {
422     if (netController_ == nullptr) {
423         NETMGR_LOG_E("netController_ is nullptr");
424         return;
425     }
426     NetRequest request;
427     request.uid = netrequest.uid;
428     request.ident = netSupplierIdent_;
429     request.netCaps = netCaps_.ToSet();
430     NETMGR_LOG_D("execute RemoveRequest");
431     int32_t errCode = netController_->RemoveRequest(request);
432     NETMGR_LOG_D("RemoveRequest errCode[%{public}d]", errCode);
433     if (errCode != REG_OK) {
434         NETMGR_LOG_E("RemoveRequest fail");
435         return;
436     }
437     return;
438 }
439 
RemoveBestRequest(uint32_t reqId)440 void NetSupplier::RemoveBestRequest(uint32_t reqId)
441 {
442     auto iter = bestReqList_.find(reqId);
443     if (iter == bestReqList_.end()) {
444         return;
445     }
446     bestReqList_.erase(reqId);
447     NETMGR_LOG_I("RemoveBestRequest supplierId=[%{public}d], reqId=[%{public}u]", supplierId_, reqId);
448 }
449 
GetBestRequestList()450 std::set<uint32_t> &NetSupplier::GetBestRequestList()
451 {
452     return bestReqList_;
453 }
454 
SetNetValid(NetDetectionStatus netState)455 void NetSupplier::SetNetValid(NetDetectionStatus netState)
456 {
457     NETMGR_LOG_I("Enter SetNetValid. supplier[%{public}d, %{public}s], ifValid[%{public}d]", supplierId_,
458                  netSupplierIdent_.c_str(), netState);
459     if (netState == VERIFICATION_STATE) {
460         if (!HasNetCap(NET_CAPABILITY_VALIDATED)) {
461             netCaps_.InsertNetCap(NET_CAPABILITY_VALIDATED);
462             netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_VALIDATED);
463             NETMGR_LOG_I("NetSupplier inserted cap:NET_CAPABILITY_VALIDATED");
464         }
465         if (HasNetCap(NET_CAPABILITY_PORTAL)) {
466             netCaps_.RemoveNetCap(NET_CAPABILITY_PORTAL);
467             netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_PORTAL);
468             NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_PORTAL");
469         }
470     } else if (netState == CAPTIVE_PORTAL_STATE) {
471         if (!HasNetCap(NET_CAPABILITY_PORTAL)) {
472             netCaps_.InsertNetCap(NET_CAPABILITY_PORTAL);
473             netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_PORTAL);
474             NETMGR_LOG_I("NetSupplier inserted cap:NET_CAPABILITY_PORTAL");
475         }
476         if (HasNetCap(NET_CAPABILITY_VALIDATED)) {
477             netCaps_.RemoveNetCap(NET_CAPABILITY_VALIDATED);
478             netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_VALIDATED);
479             NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_VALIDATED");
480         }
481     } else if (netState == QUALITY_POOR_STATE) {
482         netQuality_ = QUALITY_POOR_STATE;
483     } else if (netState == QUALITY_GOOD_STATE) {
484         netQuality_ = QUALITY_GOOD_STATE;
485     } else if (netState == ACCEPT_UNVALIDATED) {
486         netQuality_ = ACCEPT_UNVALIDATED;
487         isAcceptUnvaliad = true;
488     } else {
489         if (HasNetCap(NET_CAPABILITY_VALIDATED)) {
490             netCaps_.RemoveNetCap(NET_CAPABILITY_VALIDATED);
491             netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_VALIDATED);
492             NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_VALIDATED");
493         }
494         if (HasNetCap(NET_CAPABILITY_PORTAL)) {
495             netCaps_.RemoveNetCap(NET_CAPABILITY_PORTAL);
496             netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_PORTAL);
497             NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_PORTAL");
498         }
499     }
500 }
501 
IsNetValidated() const502 bool NetSupplier::IsNetValidated() const
503 {
504     return HasNetCap(NET_CAPABILITY_VALIDATED);
505 }
506 
507 /**
508  * This method returns the score of the current network supplier.
509  *
510  * It is used to prioritize network suppliers so that higher priority producers can activate when lower
511  * priority networks are available.
512  *
513  * @return the score of the current network supplier.
514  */
GetNetScore() const515 int32_t NetSupplier::GetNetScore() const
516 {
517     return netScore_;
518 }
519 
520 /**
521  * This method returns the real score of current network supplier.
522  *
523  * This method subtracts the score depending on different conditions, or returns netScore_ if the conditions are not
524  * met.
525  * It is used to compare the priorities of different networks.
526  *
527  * @return the real score of current network supplier.
528  */
GetRealScore()529 int32_t NetSupplier::GetRealScore()
530 {
531     // Notice: the order is important here:
532     // 1.If the user chooses to use this network, return MAX_SCORE
533     if (isAcceptUnvaliad) {
534         return static_cast<int32_t>(NetManagerStandard::NetTypeScoreValue::MAX_SCORE);
535     }
536 
537     // 2. If network detection is not complete in the first time, subtract NET_VALID_SCORE.
538     if (IsInFirstTimeDetecting()) {
539         return netScore_ - NET_VALID_SCORE;
540     }
541 
542     // 3. If network is not validated, subtract NET_VALID_SCORE.
543     if (!IsNetValidated()) {
544         return netScore_ - NET_VALID_SCORE;
545     }
546 
547     // 4. Deduct DIFF_SCORE_BETWEEN_GOOD_POOR for poor network quality (reported by the supplier).
548     if (IsNetQualityPoor()) {
549         return netScore_ - DIFF_SCORE_BETWEEN_GOOD_POOR;
550     }
551     return netScore_;
552 }
553 
SetDefault()554 void NetSupplier::SetDefault()
555 {
556     NETMGR_LOG_I("set default supplier[%{public}d].", supplierId_);
557     if (network_) {
558         network_->SetDefaultNetWork();
559     }
560 }
561 
ClearDefault()562 void NetSupplier::ClearDefault()
563 {
564     NETMGR_LOG_I("clear default supplier[%{public}d].", supplierId_);
565     if (network_) {
566         network_->ClearDefaultNetWorkNetId();
567     }
568 }
569 
UpdateGlobalHttpProxy(const HttpProxy &httpProxy)570 void NetSupplier::UpdateGlobalHttpProxy(const HttpProxy &httpProxy)
571 {
572     NETMGR_LOG_I("supplierId[%{public}d] update global httpProxy.", supplierId_);
573     if (network_) {
574         network_->UpdateGlobalHttpProxy(httpProxy);
575     }
576 }
577 
TechToType(NetSlotTech techType)578 std::string NetSupplier::TechToType(NetSlotTech techType)
579 {
580     switch (techType) {
581         case NetSlotTech::SLOT_TYPE_GSM:
582             return "2G";
583         case NetSlotTech::SLOT_TYPE_LTE:
584         case NetSlotTech::SLOT_TYPE_LTE_CA:
585             return "4G";
586         default:
587             return "3G";
588     }
589 }
590 
SetSupplierType(int32_t type)591 void NetSupplier::SetSupplierType(int32_t type)
592 {
593     NETMGR_LOG_I("supplierId[%{public}d] update type[%{public}d].", supplierId_, type);
594     type_ = TechToType(static_cast<NetSlotTech>(type));
595 }
596 
GetSupplierType()597 std::string NetSupplier::GetSupplierType()
598 {
599     return type_;
600 }
601 
ResumeNetworkInfo()602 bool NetSupplier::ResumeNetworkInfo()
603 {
604     if (network_ == nullptr) {
605         NETMGR_LOG_E("network_ is nullptr!");
606         return false;
607     }
608 
609     return network_->ResumeNetworkInfo();
610 }
611 
IsNetQualityPoor()612 bool NetSupplier::IsNetQualityPoor()
613 {
614     return netQuality_ == QUALITY_POOR_STATE;
615 }
616 
SetDetectionDone()617 void NetSupplier::SetDetectionDone()
618 {
619     if (!isFirstTimeDetectionDone) {
620         isFirstTimeDetectionDone = true;
621     }
622     if (HasNetCap(NET_CAPABILITY_CHECKING_CONNECTIVITY)) {
623         netCaps_.RemoveNetCap(NET_CAPABILITY_CHECKING_CONNECTIVITY);
624         netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_CHECKING_CONNECTIVITY);
625         NETMGR_LOG_I("supplier %{public}u detection done, remove NET_CAPABILITY_CHECKING_CONNECTIVITY", supplierId_);
626     }
627 }
628 
IsInFirstTimeDetecting() const629 bool NetSupplier::IsInFirstTimeDetecting() const
630 {
631     return !isFirstTimeDetectionDone;
632 }
633 } // namespace NetManagerStandard
634 } // namespace OHOS
635