• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         NETMGR_LOG_I("Use default Supplier for empty cap");
123         return netCaps.HasNetCap(NET_CAPABILITY_INTERNET);
124     }
125     return netCaps.HasNetCaps(reqCaps);
126 }
127 
CompareByNetworkNetType(NetBearType bearerType)128 bool NetActivate::CompareByNetworkNetType(NetBearType bearerType)
129 {
130     if (netSpecifier_ == nullptr) {
131         return false;
132     }
133     std::set<NetBearType> &reqTypes = netSpecifier_->netCapabilities_.bearerTypes_;
134     if (reqTypes.empty()) {
135         return true;
136     }
137     if (reqTypes.find(bearerType) == reqTypes.end()) {
138         return false;
139     }
140     return true;
141 }
142 
CompareByNetworkBand(uint32_t netLinkUpBand,uint32_t netLinkDownBand)143 bool NetActivate::CompareByNetworkBand(uint32_t netLinkUpBand, uint32_t netLinkDownBand)
144 {
145     uint32_t reqLinkUpBand = netSpecifier_->netCapabilities_.linkUpBandwidthKbps_;
146     uint32_t reqLinkDownBand = netSpecifier_->netCapabilities_.linkDownBandwidthKbps_;
147     if ((netLinkUpBand >= reqLinkUpBand) && (netLinkDownBand >= reqLinkDownBand)) {
148         return true;
149     }
150     return false;
151 }
152 
GetNetSpecifier()153 sptr<NetSpecifier> NetActivate::GetNetSpecifier()
154 {
155     return netSpecifier_;
156 }
157 
GetRequestId() const158 uint32_t NetActivate::GetRequestId() const
159 {
160     return requestId_;
161 }
162 
SetRequestId(uint32_t reqId)163 void NetActivate::SetRequestId(uint32_t reqId)
164 {
165     requestId_ = reqId;
166 }
167 
GetServiceSupply() const168 sptr<NetSupplier> NetActivate::GetServiceSupply() const
169 {
170     return netServiceSupplied_;
171 }
172 
SetServiceSupply(sptr<NetSupplier> netServiceSupplied)173 void NetActivate::SetServiceSupply(sptr<NetSupplier> netServiceSupplied)
174 {
175     netServiceSupplied_ = netServiceSupplied;
176 }
177 
GetNetCallback()178 sptr<INetConnCallback> NetActivate::GetNetCallback()
179 {
180     return netConnCallback_;
181 }
182 
HaveCapability(NetCap netCap) const183 bool NetActivate::HaveCapability(NetCap netCap) const
184 {
185     if (netSpecifier_ == nullptr) {
186         return false;
187     }
188     auto &capsRef = netSpecifier_->netCapabilities_.netCaps_;
189     if (capsRef.find(netCap) == capsRef.end()) {
190         return false;
191     }
192     return true;
193 }
194 
HaveTypes(const std::set<NetBearType> & bearerTypes) const195 bool NetActivate::HaveTypes(const std::set<NetBearType> &bearerTypes) const
196 {
197     if (netSpecifier_ == nullptr) {
198         return false;
199     }
200     auto &typesRef = netSpecifier_->netCapabilities_.bearerTypes_;
201     bool result = bearerTypes.size() > 0;
202     for (auto type : bearerTypes) {
203         if (typesRef.find(type) == typesRef.end()) {
204             result = false;
205             break;
206         }
207     }
208     return result;
209 }
210 } // namespace NetManagerStandard
211 } // namespace OHOS
212