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