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_I("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_I("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 auto iter = requestList_.find(reqId);
316 if (iter == requestList_.end()) {
317 return NET_CONN_ERR_SERVICE_NO_REQUEST;
318 }
319 requestList_.erase(reqId);
320 if (requestList_.empty()) {
321 SupplierDisconnection(netCaps_.ToSet());
322 }
323 bestReqList_.erase(reqId);
324 return NETMANAGER_SUCCESS;
325 }
326
RemoveBestRequest(uint32_t reqId)327 void NetSupplier::RemoveBestRequest(uint32_t reqId)
328 {
329 NETMGR_LOG_D("Enter RemoveBestRequest");
330 auto iter = bestReqList_.find(reqId);
331 if (iter == bestReqList_.end()) {
332 return;
333 }
334 bestReqList_.erase(reqId);
335 }
336
GetBestRequestList()337 std::set<uint32_t> &NetSupplier::GetBestRequestList()
338 {
339 return bestReqList_;
340 }
341
SetNetValid(bool ifValid)342 void NetSupplier::SetNetValid(bool ifValid)
343 {
344 NETMGR_LOG_I("Enter SetNetValid. supplier[%{public}d, %{public}s], ifValid[%{public}d]", supplierId_,
345 netSupplierIdent_.c_str(), ifValid);
346 if (ifValid) {
347 if (!HasNetCap(NET_CAPABILITY_VALIDATED)) {
348 netCaps_.InsertNetCap(NET_CAPABILITY_VALIDATED);
349 netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_VALIDATED);
350 NETMGR_LOG_I("NetSupplier inserted cap:NET_CAPABILITY_VALIDATED");
351 }
352 } else {
353 if (HasNetCap(NET_CAPABILITY_VALIDATED)) {
354 netCaps_.RemoveNetCap(NET_CAPABILITY_VALIDATED);
355 netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_VALIDATED);
356 NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_VALIDATED");
357 }
358 }
359 }
360
IsNetValidated()361 bool NetSupplier::IsNetValidated()
362 {
363 return HasNetCap(NET_CAPABILITY_VALIDATED);
364 }
365
SetNetScore(int32_t score)366 void NetSupplier::SetNetScore(int32_t score)
367 {
368 netScore_ = score;
369 NETMGR_LOG_D("netScore_ = %{public}d", netScore_);
370 }
371
GetNetScore() const372 int32_t NetSupplier::GetNetScore() const
373 {
374 return netScore_;
375 }
376
SetRealScore(int32_t score)377 void NetSupplier::SetRealScore(int32_t score)
378 {
379 netRealScore_ = score;
380 NETMGR_LOG_D("netRealScore_ = %{public}d", netRealScore_);
381 }
382
GetRealScore()383 int32_t NetSupplier::GetRealScore()
384 {
385 return netRealScore_;
386 }
387
SetDefault()388 void NetSupplier::SetDefault()
389 {
390 if (network_) {
391 network_->SetDefaultNetWork();
392 }
393 }
394
ClearDefault()395 void NetSupplier::ClearDefault()
396 {
397 if (network_) {
398 network_->ClearDefaultNetWorkNetId();
399 }
400 }
401 } // namespace NetManagerStandard
402 } // namespace OHOS
403