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 <functional>
18
19 #include "net_activate.h"
20 #include "net_caps.h"
21 #include "net_mgr_log_wrapper.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25 static std::atomic<uint32_t> g_nextRequestId = MIN_REQUEST_ID;
26 using TimeOutCallback = std::function<void()>;
27
NetActivate(const sptr<NetSpecifier> & specifier,const sptr<INetConnCallback> & callback,std::weak_ptr<INetActivateCallback> timeoutCallback,const uint32_t & timeoutMS,const std::shared_ptr<AppExecFwk::EventHandler> & netActEventHandler)28 NetActivate::NetActivate(const sptr<NetSpecifier> &specifier, const sptr<INetConnCallback> &callback,
29 std::weak_ptr<INetActivateCallback> timeoutCallback, const uint32_t &timeoutMS,
30 const std::shared_ptr<AppExecFwk::EventHandler> &netActEventHandler)
31 : netSpecifier_(specifier),
32 netConnCallback_(callback),
33 timeoutMS_(timeoutMS),
34 timeoutCallback_(timeoutCallback),
35 netActEventHandler_(netActEventHandler)
36 {
37 requestId_ = g_nextRequestId++;
38 if (g_nextRequestId > MAX_REQUEST_ID) {
39 g_nextRequestId = MIN_REQUEST_ID;
40 }
41 }
42
StartTimeOutNetAvailable()43 void NetActivate::StartTimeOutNetAvailable()
44 {
45 activateName_ = "NetActivate" + std::to_string(requestId_);
46 auto self = shared_from_this();
47 if (netActEventHandler_ != nullptr && timeoutMS_ > 0) {
48 netActEventHandler_->PostTask([self]() { self->TimeOutNetAvailable(); }, activateName_, timeoutMS_);
49 }
50 }
51
~NetActivate()52 NetActivate::~NetActivate()
53 {
54 if (netActEventHandler_ != nullptr) {
55 netActEventHandler_->RemoveTask(activateName_);
56 }
57 }
58
TimeOutNetAvailable()59 void NetActivate::TimeOutNetAvailable()
60 {
61 if (netServiceSupplied_) {
62 return;
63 }
64 if (netConnCallback_) {
65 netConnCallback_->NetUnavailable();
66 }
67
68 auto timeoutCb = timeoutCallback_.lock();
69 if (timeoutCb) {
70 timeoutCb->OnNetActivateTimeOut(requestId_);
71 }
72 }
73
MatchRequestAndNetwork(sptr<NetSupplier> supplier)74 bool NetActivate::MatchRequestAndNetwork(sptr<NetSupplier> supplier)
75 {
76 NETMGR_LOG_I("MatchRequestAndNetwork enter, supplier[%{public}d, %{public}s], request[%{public}d]",
77 (supplier ? supplier->GetSupplierId() : 0),
78 (supplier ? supplier->GetNetSupplierIdent().c_str() : "nullptr"), requestId_);
79 if (supplier == nullptr) {
80 NETMGR_LOG_E("Supplier is null");
81 return false;
82 }
83 if (!CompareByNetworkIdent(supplier->GetNetSupplierIdent())) {
84 NETMGR_LOG_W("Supplier ident is not matched");
85 return false;
86 }
87 if (!CompareByNetworkCapabilities(supplier->GetNetCaps())) {
88 NETMGR_LOG_W("Supplier capability is not matched");
89 return false;
90 }
91 if (!CompareByNetworkNetType((supplier->GetNetSupplierType()))) {
92 NETMGR_LOG_W("Supplier net type not matched");
93 return false;
94 }
95 NetAllCapabilities netAllCaps = supplier->GetNetCapabilities();
96 if (!CompareByNetworkBand(netAllCaps.linkUpBandwidthKbps_, netAllCaps.linkDownBandwidthKbps_)) {
97 NETMGR_LOG_W("Supplier net band not matched");
98 return false;
99 }
100
101 return true;
102 }
103
CompareByNetworkIdent(const std::string & ident)104 bool NetActivate::CompareByNetworkIdent(const std::string &ident)
105 {
106 if (ident.empty() || netSpecifier_->ident_.empty()) {
107 return true;
108 }
109 if (ident == netSpecifier_->ident_) {
110 return true;
111 }
112 return false;
113 }
114
CompareByNetworkCapabilities(const NetCaps & netCaps)115 bool NetActivate::CompareByNetworkCapabilities(const NetCaps &netCaps)
116 {
117 if (netSpecifier_ == nullptr) {
118 return false;
119 }
120 std::set<NetCap> &reqCaps = netSpecifier_->netCapabilities_.netCaps_;
121 if (reqCaps.empty()) {
122 return true;
123 }
124 return netCaps.HasNetCaps(reqCaps);
125 }
126
CompareByNetworkNetType(NetBearType bearerType)127 bool NetActivate::CompareByNetworkNetType(NetBearType bearerType)
128 {
129 if (netSpecifier_ == nullptr) {
130 return false;
131 }
132 std::set<NetBearType> &reqTypes = netSpecifier_->netCapabilities_.bearerTypes_;
133 if (reqTypes.empty()) {
134 return true;
135 }
136 if (reqTypes.find(bearerType) == reqTypes.end()) {
137 return false;
138 }
139 return true;
140 }
141
CompareByNetworkBand(uint32_t netLinkUpBand,uint32_t netLinkDownBand)142 bool NetActivate::CompareByNetworkBand(uint32_t netLinkUpBand, uint32_t netLinkDownBand)
143 {
144 uint32_t reqLinkUpBand = netSpecifier_->netCapabilities_.linkUpBandwidthKbps_;
145 uint32_t reqLinkDownBand = netSpecifier_->netCapabilities_.linkDownBandwidthKbps_;
146 if ((netLinkUpBand >= reqLinkUpBand) && (netLinkDownBand >= reqLinkDownBand)) {
147 return true;
148 }
149 return false;
150 }
151
GetNetSpecifier()152 sptr<NetSpecifier> NetActivate::GetNetSpecifier()
153 {
154 return netSpecifier_;
155 }
156
GetRequestId() const157 uint32_t NetActivate::GetRequestId() const
158 {
159 return requestId_;
160 }
161
SetRequestId(uint32_t reqId)162 void NetActivate::SetRequestId(uint32_t reqId)
163 {
164 requestId_ = reqId;
165 }
166
GetServiceSupply() const167 sptr<NetSupplier> NetActivate::GetServiceSupply() const
168 {
169 return netServiceSupplied_;
170 }
171
SetServiceSupply(sptr<NetSupplier> netServiceSupplied)172 void NetActivate::SetServiceSupply(sptr<NetSupplier> netServiceSupplied)
173 {
174 netServiceSupplied_ = netServiceSupplied;
175 }
176
GetNetCallback()177 sptr<INetConnCallback> NetActivate::GetNetCallback()
178 {
179 return netConnCallback_;
180 }
181
HaveCapability(NetCap netCap) const182 bool NetActivate::HaveCapability(NetCap netCap) const
183 {
184 if (netSpecifier_ == nullptr) {
185 return false;
186 }
187 auto &capsRef = netSpecifier_->netCapabilities_.netCaps_;
188 if (capsRef.find(netCap) == capsRef.end()) {
189 return false;
190 }
191 return true;
192 }
193
HaveTypes(const std::set<NetBearType> & bearerTypes) const194 bool NetActivate::HaveTypes(const std::set<NetBearType> &bearerTypes) const
195 {
196 if (netSpecifier_ == nullptr) {
197 return false;
198 }
199 auto &typesRef = netSpecifier_->netCapabilities_.bearerTypes_;
200 bool result = bearerTypes.size() > 0;
201 for (auto type : bearerTypes) {
202 if (typesRef.find(type) == typesRef.end()) {
203 result = false;
204 break;
205 }
206 }
207 return result;
208 }
209 } // namespace NetManagerStandard
210 } // namespace OHOS