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