• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 }
29 static std::atomic<uint32_t> g_nextNetSupplierId = 0x03EB;
30 
NetSupplier(NetBearType bearerType,const std::string & netSupplierIdent,const std::set<NetCap> & netCaps)31 NetSupplier::NetSupplier(NetBearType bearerType, const std::string &netSupplierIdent, const std::set<NetCap> &netCaps)
32     : netSupplierType_(bearerType),
33       netSupplierIdent_(netSupplierIdent),
34       netCaps_(netCaps),
35       supplierId_(g_nextNetSupplierId++)
36 {
37     netAllCapabilities_.netCaps_ = netCaps;
38     netAllCapabilities_.bearerTypes_.insert(bearerType);
39 }
40 
RegisterSupplierCallback(const sptr<INetSupplierCallback> & callback)41 void NetSupplier::RegisterSupplierCallback(const sptr<INetSupplierCallback> &callback)
42 {
43     netController_ = callback;
44 }
45 
operator ==(const NetSupplier & netSupplier) const46 bool NetSupplier::operator==(const NetSupplier &netSupplier) const
47 {
48     return supplierId_ == netSupplier.supplierId_ && netSupplierType_ == netSupplier.netSupplierType_ &&
49            netSupplierIdent_ == netSupplier.netSupplierIdent_ && netCaps_ == netSupplier.netCaps_;
50 }
51 
UpdateNetSupplierInfo(const NetSupplierInfo & netSupplierInfo)52 void NetSupplier::UpdateNetSupplierInfo(const NetSupplierInfo &netSupplierInfo)
53 {
54     NETMGR_LOG_D("Update net supplier[%{public}d, %{public}s], netSupplierInfo[%{public}s]", supplierId_,
55                  netSupplierIdent_.c_str(), netSupplierInfo_.ToString("").c_str());
56     bool oldAvailable = netSupplierInfo_.isAvailable_;
57     netSupplierInfo_ = netSupplierInfo;
58     netAllCapabilities_.linkUpBandwidthKbps_ = netSupplierInfo_.linkUpBandwidthKbps_;
59     netAllCapabilities_.linkDownBandwidthKbps_ = netSupplierInfo_.linkDownBandwidthKbps_;
60     if (oldAvailable == netSupplierInfo_.isAvailable_) {
61         NETMGR_LOG_W("Same supplier available status:[%{public}d]", oldAvailable);
62         return;
63     }
64     if (network_ == nullptr) {
65         NETMGR_LOG_E("network_ is nullptr!");
66         return;
67     }
68     network_->UpdateBasicNetwork(netSupplierInfo_.isAvailable_);
69     if (!netSupplierInfo_.isAvailable_) {
70         UpdateNetConnState(NET_CONN_STATE_DISCONNECTED);
71     }
72 }
73 
UpdateNetLinkInfo(const NetLinkInfo & netLinkInfo)74 int32_t NetSupplier::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo)
75 {
76     NETMGR_LOG_D("Update netlink info: netLinkInfo[%{public}s]", netLinkInfo.ToString(" ").c_str());
77     if (network_ == nullptr) {
78         NETMGR_LOG_E("network_ is nullptr!");
79         return NET_CONN_ERR_INVALID_NETWORK;
80     }
81 
82     if (!network_->UpdateNetLinkInfo(netLinkInfo)) {
83         return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
84     }
85     UpdateNetConnState(NET_CONN_STATE_CONNECTED);
86     return NETMANAGER_SUCCESS;
87 }
88 
GetNetSupplierType() const89 NetBearType NetSupplier::GetNetSupplierType() const
90 {
91     return netSupplierType_;
92 }
93 
GetNetSupplierIdent() const94 std::string NetSupplier::GetNetSupplierIdent() const
95 {
96     return netSupplierIdent_;
97 }
98 
CompareNetCaps(const std::set<NetCap> caps) const99 bool NetSupplier::CompareNetCaps(const std::set<NetCap> caps) const
100 {
101     if (caps.empty()) {
102         return true;
103     }
104     return netCaps_.HasNetCaps(caps);
105 }
106 
HasNetCap(NetCap cap) const107 bool NetSupplier::HasNetCap(NetCap cap) const
108 {
109     return netCaps_.HasNetCap(cap);
110 }
111 
HasNetCaps(const std::set<NetCap> & caps) const112 bool NetSupplier::HasNetCaps(const std::set<NetCap> &caps) const
113 {
114     return netCaps_.HasNetCaps(caps);
115 }
116 
GetNetCaps() const117 const NetCaps &NetSupplier::GetNetCaps() const
118 {
119     return netCaps_;
120 }
121 
GetNetCapabilities() const122 NetAllCapabilities NetSupplier::GetNetCapabilities() const
123 {
124     return netAllCapabilities_;
125 }
126 
SetNetwork(const std::shared_ptr<Network> & network)127 void NetSupplier::SetNetwork(const std::shared_ptr<Network> &network)
128 {
129     network_ = network;
130     if (network_ != nullptr) {
131         netHandle_ = std::make_unique<NetHandle>(network_->GetNetId()).release();
132     }
133 }
134 
GetNetwork() const135 std::shared_ptr<Network> NetSupplier::GetNetwork() const
136 {
137     return network_;
138 }
139 
GetNetId() const140 int32_t NetSupplier::GetNetId() const
141 {
142     if (network_ == nullptr) {
143         return INVALID_NET_ID;
144     }
145     return network_->GetNetId();
146 }
147 
GetNetHandle() const148 sptr<NetHandle> NetSupplier::GetNetHandle() const
149 {
150     return netHandle_;
151 }
152 
GetHttpProxy(HttpProxy & httpProxy)153 void NetSupplier::GetHttpProxy(HttpProxy &httpProxy)
154 {
155     if (network_ == nullptr) {
156         NETMGR_LOG_E("network_ is nullptr.");
157         return;
158     }
159     httpProxy = network_->GetNetLinkInfo().httpProxy_;
160 }
161 
GetSupplierId() const162 uint32_t NetSupplier::GetSupplierId() const
163 {
164     return supplierId_;
165 }
166 
GetRoaming() const167 bool NetSupplier::GetRoaming() const
168 {
169     return netSupplierInfo_.isRoaming_;
170 }
171 
GetStrength() const172 int8_t NetSupplier::GetStrength() const
173 {
174     return netSupplierInfo_.strength_;
175 }
176 
GetFrequency() const177 uint16_t NetSupplier::GetFrequency() const
178 {
179     return netSupplierInfo_.frequency_;
180 }
181 
GetSupplierUid() const182 int32_t NetSupplier::GetSupplierUid() const
183 {
184     return netSupplierInfo_.uid_;
185 }
186 
SupplierConnection(const std::set<NetCap> & netCaps)187 bool NetSupplier::SupplierConnection(const std::set<NetCap> &netCaps)
188 {
189     NETMGR_LOG_I("Supplier[%{public}d, %{public}s] request connect", supplierId_, netSupplierIdent_.c_str());
190     if (netSupplierInfo_.isAvailable_) {
191         NETMGR_LOG_W("The supplier is currently available, there is no need to repeat the request for connection.");
192         return true;
193     }
194     UpdateNetConnState(NET_CONN_STATE_IDLE);
195 
196     if (netController_ == nullptr) {
197         NETMGR_LOG_E("netController_ is nullptr");
198         return false;
199     }
200     NETMGR_LOG_D("execute RequestNetwork");
201     int32_t errCode = netController_->RequestNetwork(netSupplierIdent_, netCaps);
202     NETMGR_LOG_D("RequestNetwork errCode[%{public}d]", errCode);
203     if (errCode != REG_OK) {
204         NETMGR_LOG_E("RequestNetwork fail");
205         return false;
206     }
207     return true;
208 }
209 
SetRestrictBackground(bool restrictBackground)210 void NetSupplier::SetRestrictBackground(bool restrictBackground)
211 {
212     restrictBackground_ = restrictBackground;
213 }
GetRestrictBackground() const214 bool NetSupplier::GetRestrictBackground() const
215 {
216     return restrictBackground_;
217 }
218 
SupplierDisconnection(const std::set<NetCap> & netCaps)219 bool NetSupplier::SupplierDisconnection(const std::set<NetCap> &netCaps)
220 {
221     NETMGR_LOG_I("Supplier[%{public}d, %{public}s] request disconnect", supplierId_, netSupplierIdent_.c_str());
222     if (!netSupplierInfo_.isAvailable_) {
223         NETMGR_LOG_W("The supplier is currently unavailable, there is no need to repeat the request to disconnect.");
224         return true;
225     }
226     if (netController_ == nullptr) {
227         NETMGR_LOG_E("netController_ is nullptr");
228         return false;
229     }
230     NETMGR_LOG_D("execute ReleaseNetwork, supplierId[%{public}d]", supplierId_);
231     int32_t errCode = netController_->ReleaseNetwork(netSupplierIdent_, netCaps);
232     NETMGR_LOG_D("ReleaseNetwork retCode[%{public}d]", errCode);
233     if (errCode != REG_OK) {
234         NETMGR_LOG_E("ReleaseNetwork fail");
235         return false;
236     }
237     return true;
238 }
239 
UpdateNetConnState(NetConnState netConnState)240 void NetSupplier::UpdateNetConnState(NetConnState netConnState)
241 {
242     if (network_) {
243         network_->UpdateNetConnState(netConnState);
244     }
245 }
246 
IsConnecting() const247 bool NetSupplier::IsConnecting() const
248 {
249     if (network_) {
250         return network_->IsConnecting();
251     }
252     return false;
253 }
254 
IsConnected() const255 bool NetSupplier::IsConnected() const
256 {
257     if (network_) {
258         return network_->IsConnected();
259     }
260     return false;
261 }
262 
RequestToConnect(uint32_t reqId)263 bool NetSupplier::RequestToConnect(uint32_t reqId)
264 {
265     if (requestList_.find(reqId) == requestList_.end()) {
266         requestList_.insert(reqId);
267     }
268     return SupplierConnection(netCaps_.ToSet());
269 }
270 
SelectAsBestNetwork(uint32_t reqId)271 int32_t NetSupplier::SelectAsBestNetwork(uint32_t reqId)
272 {
273     NETMGR_LOG_I("Request[%{public}d] select supplier[%{public}d, %{public}s] as best network", reqId, supplierId_,
274                  netSupplierIdent_.c_str());
275     if (requestList_.find(reqId) == requestList_.end()) {
276         requestList_.insert(reqId);
277     }
278     if (bestReqList_.find(reqId) == bestReqList_.end()) {
279         bestReqList_.insert(reqId);
280     }
281     return NETMANAGER_SUCCESS;
282 }
283 
ReceiveBestScore(uint32_t reqId,int32_t bestScore,uint32_t supplierId)284 void NetSupplier::ReceiveBestScore(uint32_t reqId, int32_t bestScore, uint32_t supplierId)
285 {
286     NETMGR_LOG_D("Supplier[%{public}d, %{public}s] receive best score, bestSupplierId[%{public}d]", supplierId_,
287                  netSupplierIdent_.c_str(), supplierId);
288     if (supplierId == supplierId_) {
289         NETMGR_LOG_W("Same net supplier, no need to disconnect.");
290         return;
291     }
292     if (requestList_.empty()) {
293         SupplierDisconnection(netCaps_.ToSet());
294         return;
295     }
296     if (requestList_.find(reqId) == requestList_.end()) {
297         NETMGR_LOG_W("Can not find request[%{public}d]", reqId);
298         return;
299     }
300     if (netScore_ >= bestScore) {
301         NETMGR_LOG_W("High priority network, no need to disconnect");
302         return;
303     }
304     requestList_.erase(reqId);
305     NETMGR_LOG_D("Supplier[%{public}d, %{public}s] remaining request list size[%{public}zd]", supplierId_,
306                  netSupplierIdent_.c_str(), requestList_.size());
307     if (requestList_.empty()) {
308         SupplierDisconnection(netCaps_.ToSet());
309     }
310     bestReqList_.erase(reqId);
311 }
312 
CancelRequest(uint32_t reqId)313 int32_t NetSupplier::CancelRequest(uint32_t reqId)
314 {
315     NETMGR_LOG_I("CancelRequest netId = %{public}u", reqId);
316     auto iter = requestList_.find(reqId);
317     if (iter == requestList_.end()) {
318         return NET_CONN_ERR_SERVICE_NO_REQUEST;
319     }
320     requestList_.erase(reqId);
321     if (requestList_.empty()) {
322         SupplierDisconnection(netCaps_.ToSet());
323     }
324     bestReqList_.erase(reqId);
325     return NETMANAGER_SUCCESS;
326 }
327 
RemoveBestRequest(uint32_t reqId)328 void NetSupplier::RemoveBestRequest(uint32_t reqId)
329 {
330     NETMGR_LOG_I("Enter RemoveBestRequest supplierId=[%{public}d], reqId=[%{public}d]", supplierId_, reqId);
331     auto iter = bestReqList_.find(reqId);
332     if (iter == bestReqList_.end()) {
333         return;
334     }
335     bestReqList_.erase(reqId);
336 }
337 
GetBestRequestList()338 std::set<uint32_t> &NetSupplier::GetBestRequestList()
339 {
340     return bestReqList_;
341 }
342 
SetNetValid(bool ifValid)343 void NetSupplier::SetNetValid(bool ifValid)
344 {
345     NETMGR_LOG_I("Enter SetNetValid. supplier[%{public}d, %{public}s], ifValid[%{public}d]", supplierId_,
346                  netSupplierIdent_.c_str(), ifValid);
347     if (ifValid) {
348         if (!HasNetCap(NET_CAPABILITY_VALIDATED)) {
349             netCaps_.InsertNetCap(NET_CAPABILITY_VALIDATED);
350             netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_VALIDATED);
351             NETMGR_LOG_I("NetSupplier inserted cap:NET_CAPABILITY_VALIDATED");
352         }
353     } else {
354         if (HasNetCap(NET_CAPABILITY_VALIDATED)) {
355             netCaps_.RemoveNetCap(NET_CAPABILITY_VALIDATED);
356             netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_VALIDATED);
357             NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_VALIDATED");
358         }
359     }
360 }
361 
IsNetValidated()362 bool NetSupplier::IsNetValidated()
363 {
364     return HasNetCap(NET_CAPABILITY_VALIDATED);
365 }
366 
SetNetScore(int32_t score)367 void NetSupplier::SetNetScore(int32_t score)
368 {
369     netScore_ = score;
370     NETMGR_LOG_D("netScore_ = %{public}d", netScore_);
371 }
372 
GetNetScore() const373 int32_t NetSupplier::GetNetScore() const
374 {
375     return netScore_;
376 }
377 
SetRealScore(int32_t score)378 void NetSupplier::SetRealScore(int32_t score)
379 {
380     netRealScore_ = score;
381     NETMGR_LOG_D("netRealScore_ = %{public}d", netRealScore_);
382 }
383 
GetRealScore()384 int32_t NetSupplier::GetRealScore()
385 {
386     return netRealScore_;
387 }
388 
SetDefault()389 void NetSupplier::SetDefault()
390 {
391     NETMGR_LOG_I("set default supplier[%{public}d].", supplierId_);
392     if (network_) {
393         network_->SetDefaultNetWork();
394     }
395 }
396 
ClearDefault()397 void NetSupplier::ClearDefault()
398 {
399     NETMGR_LOG_I("clear default supplier[%{public}d].", supplierId_);
400     if (network_) {
401         network_->ClearDefaultNetWorkNetId();
402     }
403 }
404 
UpdateGlobalHttpProxy(const HttpProxy & httpProxy)405 void NetSupplier::UpdateGlobalHttpProxy(const HttpProxy &httpProxy)
406 {
407     NETMGR_LOG_I("supplierId[%{public}d] update global httpProxy.", supplierId_);
408     if (network_) {
409         network_->UpdateGlobalHttpProxy(httpProxy);
410     }
411 }
412 
TechToType(NetSlotTech techType)413 std::string NetSupplier::TechToType(NetSlotTech techType)
414 {
415     switch (techType) {
416         case NetSlotTech::SLOT_TYPE_GSM:
417             return "2G";
418         case NetSlotTech::SLOT_TYPE_LTE:
419         case NetSlotTech::SLOT_TYPE_LTE_CA:
420             return "4G";
421         default:
422             return "3G";
423     }
424 }
425 
SetSupplierType(int32_t type)426 void NetSupplier::SetSupplierType(int32_t type)
427 {
428     NETMGR_LOG_I("supplierId[%{public}d] update type[%{public}d].", supplierId_, type);
429     type_ = TechToType(static_cast<NetSlotTech>(type));
430 }
431 
GetSupplierType()432 std::string NetSupplier::GetSupplierType()
433 {
434     return type_;
435 }
436 
ResumeNetworkInfo()437 bool NetSupplier::ResumeNetworkInfo()
438 {
439     if (network_ == nullptr) {
440         NETMGR_LOG_E("network_ is nullptr!");
441         return false;
442     }
443 
444     return network_->ResumeNetworkInfo();
445 }
446 } // namespace NetManagerStandard
447 } // namespace OHOS
448