1 /*
2 * Copyright (c) 2021-2022 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 static std::atomic<uint32_t> g_nextNetSupplierId = 0x03EB;
27 static constexpr int32_t REG_OK = 0;
28
NetSupplier(NetBearType bearerType,const std::string & netSupplierIdent,const std::set<NetCap> & netCaps)29 NetSupplier::NetSupplier(NetBearType bearerType, const std::string &netSupplierIdent, const std::set<NetCap> &netCaps)
30 : netSupplierType_(bearerType),
31 netSupplierIdent_(netSupplierIdent),
32 netCaps_(netCaps),
33 supplierId_(g_nextNetSupplierId++)
34 {
35 netAllCapabilities_.netCaps_ = netCaps;
36 netAllCapabilities_.bearerTypes_.insert(bearerType);
37 }
38
RegisterSupplierCallback(const sptr<INetSupplierCallback> & callback)39 void NetSupplier::RegisterSupplierCallback(const sptr<INetSupplierCallback> &callback)
40 {
41 netController_ = callback;
42 }
43
operator ==(const NetSupplier & netSupplier) const44 bool NetSupplier::operator==(const NetSupplier &netSupplier) const
45 {
46 return supplierId_ == netSupplier.supplierId_ && netSupplierType_ == netSupplier.netSupplierType_ &&
47 netSupplierIdent_ == netSupplier.netSupplierIdent_ && netCaps_ == netSupplier.netCaps_;
48 }
49
UpdateNetSupplierInfo(const NetSupplierInfo & netSupplierInfo)50 void NetSupplier::UpdateNetSupplierInfo(const NetSupplierInfo &netSupplierInfo)
51 {
52 NETMGR_LOG_D("Update net supplier[%{public}d, %{public}s], netSupplierInfo[%{public}s]", supplierId_,
53 netSupplierIdent_.c_str(), netSupplierInfo_.ToString("").c_str());
54 bool oldAvailable = netSupplierInfo_.isAvailable_;
55 netSupplierInfo_ = netSupplierInfo;
56 netAllCapabilities_.linkUpBandwidthKbps_ = netSupplierInfo_.linkUpBandwidthKbps_;
57 netAllCapabilities_.linkDownBandwidthKbps_ = netSupplierInfo_.linkDownBandwidthKbps_;
58 if (oldAvailable == netSupplierInfo_.isAvailable_) {
59 return;
60 }
61 if (network_ == nullptr) {
62 NETMGR_LOG_E("network_ is nullptr!");
63 return;
64 }
65 network_->UpdateBasicNetwork(netSupplierInfo_.isAvailable_);
66 if (!netSupplierInfo_.isAvailable_) {
67 UpdateNetConnState(NET_CONN_STATE_DISCONNECTED);
68 }
69 }
70
UpdateNetLinkInfo(const NetLinkInfo & netLinkInfo)71 int32_t NetSupplier::UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo)
72 {
73 NETMGR_LOG_D("Update netlink info: netLinkInfo[%{public}s]", netLinkInfo.ToString(" ").c_str());
74 if (network_ == nullptr) {
75 NETMGR_LOG_E("network_ is nullptr!");
76 return NET_CONN_ERR_INVALID_NETWORK;
77 }
78
79 if (!network_->UpdateNetLinkInfo(netLinkInfo)) {
80 return NET_CONN_ERR_SERVICE_UPDATE_NET_LINK_INFO_FAIL;
81 }
82 UpdateNetConnState(NET_CONN_STATE_CONNECTED);
83 return NETMANAGER_SUCCESS;
84 }
85
GetNetSupplierType() const86 NetBearType NetSupplier::GetNetSupplierType() const
87 {
88 return netSupplierType_;
89 }
90
GetNetSupplierIdent() const91 std::string NetSupplier::GetNetSupplierIdent() const
92 {
93 return netSupplierIdent_;
94 }
95
CompareNetCaps(const std::set<NetCap> caps) const96 bool NetSupplier::CompareNetCaps(const std::set<NetCap> caps) const
97 {
98 const bool ret = (caps == netCaps_.ToSet());
99 NETMGR_LOG_D("CompareNetCaps ret:[%{public}d]", ret);
100 return ret;
101 }
102
HasNetCap(NetCap cap) const103 bool NetSupplier::HasNetCap(NetCap cap) const
104 {
105 return netCaps_.HasNetCap(cap);
106 }
107
HasNetCaps(const std::set<NetCap> & caps) const108 bool NetSupplier::HasNetCaps(const std::set<NetCap> &caps) const
109 {
110 return netCaps_.HasNetCaps(caps);
111 }
112
GetNetCaps() const113 const NetCaps &NetSupplier::GetNetCaps() const
114 {
115 return netCaps_;
116 }
117
GetNetCapabilities() const118 NetAllCapabilities NetSupplier::GetNetCapabilities() const
119 {
120 return netAllCapabilities_;
121 }
122
SetNetwork(const std::shared_ptr<Network> & network)123 void NetSupplier::SetNetwork(const std::shared_ptr<Network> &network)
124 {
125 network_ = network;
126 if (network_ != nullptr) {
127 netHandle_ = std::make_unique<NetHandle>(network_->GetNetId()).release();
128 }
129 }
130
GetNetwork() const131 std::shared_ptr<Network> NetSupplier::GetNetwork() const
132 {
133 return network_;
134 }
135
GetNetId() const136 int32_t NetSupplier::GetNetId() const
137 {
138 if (network_ == nullptr) {
139 return INVALID_NET_ID;
140 }
141 return network_->GetNetId();
142 }
143
GetNetHandle() const144 sptr<NetHandle> NetSupplier::GetNetHandle() const
145 {
146 return netHandle_;
147 }
148
GetSupplierId() const149 uint32_t NetSupplier::GetSupplierId() const
150 {
151 return supplierId_;
152 }
153
GetRoaming() const154 bool NetSupplier::GetRoaming() const
155 {
156 return netSupplierInfo_.isRoaming_;
157 }
158
GetStrength() const159 int8_t NetSupplier::GetStrength() const
160 {
161 return netSupplierInfo_.strength_;
162 }
163
GetFrequency() const164 uint16_t NetSupplier::GetFrequency() const
165 {
166 return netSupplierInfo_.frequency_;
167 }
168
GetSupplierUid() const169 int32_t NetSupplier::GetSupplierUid() const
170 {
171 return netSupplierInfo_.uid_;
172 }
173
SupplierConnection(const std::set<NetCap> & netCaps)174 bool NetSupplier::SupplierConnection(const std::set<NetCap> &netCaps)
175 {
176 NETMGR_LOG_D("param ident[%{public}s]", netSupplierIdent_.c_str());
177 if (IsConnecting()) {
178 NETMGR_LOG_D("this service is connecting");
179 return true;
180 }
181 if (IsConnected()) {
182 NETMGR_LOG_D("this service is already connected");
183 return true;
184 }
185 UpdateNetConnState(NET_CONN_STATE_IDLE);
186
187 if (netController_ == nullptr) {
188 NETMGR_LOG_E("netController_ is nullptr");
189 return false;
190 }
191 NETMGR_LOG_D("execute RequestNetwork");
192 int32_t errCode = netController_->RequestNetwork(netSupplierIdent_, netCaps);
193 NETMGR_LOG_D("RequestNetwork errCode[%{public}d]", errCode);
194 if (errCode != REG_OK) {
195 NETMGR_LOG_E("RequestNetwork fail");
196 return false;
197 }
198 UpdateNetConnState(NET_CONN_STATE_CONNECTING);
199 return true;
200 }
201
SetRestrictBackground(bool restrictBackground)202 void NetSupplier::SetRestrictBackground(bool restrictBackground)
203 {
204 restrictBackground_ = restrictBackground;
205 }
GetRestrictBackground() const206 bool NetSupplier::GetRestrictBackground() const
207 {
208 return restrictBackground_;
209 }
210
SupplierDisconnection(const std::set<NetCap> & netCaps)211 bool NetSupplier::SupplierDisconnection(const std::set<NetCap> &netCaps)
212 {
213 NETMGR_LOG_D("supplier[%{public}d, %{public}s]", supplierId_, netSupplierIdent_.c_str());
214 if ((!IsConnecting()) && (!IsConnected())) {
215 NETMGR_LOG_D("no need to disconnect");
216 return true;
217 }
218 if (netController_ == nullptr) {
219 NETMGR_LOG_E("netController_ is nullptr");
220 return false;
221 }
222 NETMGR_LOG_D("execute ReleaseNetwork, supplierId[%{public}d]", supplierId_);
223 int32_t errCode = netController_->ReleaseNetwork(netSupplierIdent_, netCaps);
224 NETMGR_LOG_D("ReleaseNetwork retCode[%{public}d]", errCode);
225 if (errCode != REG_OK) {
226 NETMGR_LOG_E("ReleaseNetwork fail");
227 return false;
228 }
229 return true;
230 }
231
UpdateNetConnState(NetConnState netConnState)232 void NetSupplier::UpdateNetConnState(NetConnState netConnState)
233 {
234 if (network_) {
235 network_->UpdateNetConnState(netConnState);
236 }
237 }
238
IsConnecting() const239 bool NetSupplier::IsConnecting() const
240 {
241 if (network_) {
242 return network_->IsConnecting();
243 }
244 return false;
245 }
246
IsConnected() const247 bool NetSupplier::IsConnected() const
248 {
249 if (network_) {
250 return network_->IsConnected();
251 }
252 return false;
253 }
254
AddRequestIdToList(uint32_t requestId)255 void NetSupplier::AddRequestIdToList(uint32_t requestId)
256 {
257 NETMGR_LOG_D("AddRequestIdToList reqId = [%{public}u]", requestId);
258 requestList_.insert(requestId);
259 }
260
RequestToConnect(uint32_t reqId)261 bool NetSupplier::RequestToConnect(uint32_t reqId)
262 {
263 requestList_.insert(reqId);
264 return SupplierConnection(netCaps_.ToSet());
265 }
266
SelectAsBestNetwork(uint32_t reqId)267 int32_t NetSupplier::SelectAsBestNetwork(uint32_t reqId)
268 {
269 NETMGR_LOG_D("NetSupplier::SelectAsBestNetwork");
270 requestList_.insert(reqId);
271 bestReqList_.insert(reqId);
272 return NETMANAGER_SUCCESS;
273 }
274
ReceiveBestScore(uint32_t reqId,int32_t bestScore,uint32_t supplierId)275 void NetSupplier::ReceiveBestScore(uint32_t reqId, int32_t bestScore, uint32_t supplierId)
276 {
277 NETMGR_LOG_D("NetSupplier::ReceiveBestScore, supplierId[%{public}d, %{public}s], bestSupplierId[%{public}d]",
278 supplierId_, netSupplierIdent_.c_str(), supplierId);
279 if (requestList_.empty()) {
280 SupplierDisconnection(netCaps_.ToSet());
281 return;
282 }
283 auto iter = requestList_.find(reqId);
284 if (iter == requestList_.end()) {
285 NETMGR_LOG_D("NetSupplier::ReceiveBestScore, supplierId[%{public}d], can not find request[%{public}d]",
286 supplierId_, reqId);
287 return;
288 }
289 if (supplierId != supplierId_ && netScore_ < bestScore) {
290 requestList_.erase(reqId);
291 if (requestList_.empty()) {
292 SupplierDisconnection(netCaps_.ToSet());
293 }
294 bestReqList_.erase(reqId);
295 }
296 }
297
CancelRequest(uint32_t reqId)298 int32_t NetSupplier::CancelRequest(uint32_t reqId)
299 {
300 auto iter = requestList_.find(reqId);
301 if (iter == requestList_.end()) {
302 return NET_CONN_ERR_SERVICE_NO_REQUEST;
303 }
304 requestList_.erase(reqId);
305 if (requestList_.empty()) {
306 SupplierDisconnection(netCaps_.ToSet());
307 }
308 bestReqList_.erase(reqId);
309 return NETMANAGER_SUCCESS;
310 }
311
RemoveBestRequest(uint32_t reqId)312 void NetSupplier::RemoveBestRequest(uint32_t reqId)
313 {
314 NETMGR_LOG_D("Enter RemoveBestRequest");
315 auto iter = bestReqList_.find(reqId);
316 if (iter == bestReqList_.end()) {
317 return;
318 }
319 bestReqList_.erase(reqId);
320 }
321
GetBestRequestList()322 std::set<uint32_t> &NetSupplier::GetBestRequestList()
323 {
324 return bestReqList_;
325 }
326
SetNetValid(bool ifValid)327 void NetSupplier::SetNetValid(bool ifValid)
328 {
329 NETMGR_LOG_I("Enter SetNetValid. supplier[%{public}d, %{public}s], ifValid[%{public}d]", supplierId_,
330 netSupplierIdent_.c_str(), ifValid);
331 if (ifValid) {
332 if (!HasNetCap(NET_CAPABILITY_VALIDATED)) {
333 netCaps_.InsertNetCap(NET_CAPABILITY_VALIDATED);
334 netAllCapabilities_.netCaps_.insert(NET_CAPABILITY_VALIDATED);
335 NETMGR_LOG_I("NetSupplier inserted cap:NET_CAPABILITY_VALIDATED");
336 }
337 } else {
338 if (HasNetCap(NET_CAPABILITY_VALIDATED)) {
339 netCaps_.RemoveNetCap(NET_CAPABILITY_VALIDATED);
340 netAllCapabilities_.netCaps_.erase(NET_CAPABILITY_VALIDATED);
341 NETMGR_LOG_I("NetSupplier remove cap:NET_CAPABILITY_VALIDATED");
342 }
343 }
344 }
345
IsNetValidated()346 bool NetSupplier::IsNetValidated()
347 {
348 return HasNetCap(NET_CAPABILITY_VALIDATED);
349 }
350
SetNetScore(int32_t score)351 void NetSupplier::SetNetScore(int32_t score)
352 {
353 netScore_ = score;
354 NETMGR_LOG_D("netScore_ = %{public}d", netScore_);
355 }
356
GetNetScore() const357 int32_t NetSupplier::GetNetScore() const
358 {
359 return netScore_;
360 }
361
SetRealScore(int32_t score)362 void NetSupplier::SetRealScore(int32_t score)
363 {
364 netRealScore_ = score;
365 NETMGR_LOG_D("netRealScore_ = %{public}d", netRealScore_);
366 }
367
GetRealScore()368 int32_t NetSupplier::GetRealScore()
369 {
370 return netRealScore_;
371 }
372
SetDefault()373 void NetSupplier::SetDefault()
374 {
375 if (network_) {
376 network_->SetDefaultNetWork();
377 }
378 }
379
ClearDefault()380 void NetSupplier::ClearDefault()
381 {
382 if (network_) {
383 network_->ClearDefaultNetWorkNetId();
384 }
385 }
386 } // namespace NetManagerStandard
387 } // namespace OHOS
388