1 /*
2 * Copyright (c) 2021 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 #include "net_activate.h"
16
17 #include <atomic>
18 #include <functional>
19 #include "net_mgr_log_wrapper.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
23 static std::atomic<uint32_t> g_nextRequestId = MIN_REQUEST_ID;
24 using TimeOutCallback = std::function<void()>;
25
NetActivate(const sptr<NetSpecifier> & specifier,const sptr<INetConnCallback> & callback,TimeOutHandler timeOutHandler,const uint32_t & timeoutMS)26 NetActivate::NetActivate(const sptr<NetSpecifier> &specifier, const sptr<INetConnCallback> &callback,
27 TimeOutHandler timeOutHandler, const uint32_t &timeoutMS)
28 : netSpecifier_(specifier), netConnCallback_(callback), timeoutMS_(timeoutMS), timeOutHandler_(timeOutHandler)
29 {
30 requestId_ = g_nextRequestId++;
31 if (g_nextRequestId > MAX_REQUEST_ID) {
32 g_nextRequestId = MIN_REQUEST_ID;
33 }
34 if (timeoutMS > 0) {
35 lpTimer_ = std::make_unique<Timer>();
36 TimeOutCallback timeOutcallback = std::bind(&NetActivate::TimeOutNetAvailable, this);
37 lpTimer_->StartOnce(timeoutMS, timeOutcallback);
38 }
39 }
40
~NetActivate()41 NetActivate::~NetActivate()
42 {
43 if (lpTimer_) {
44 lpTimer_->Stop();
45 }
46 }
47
TimeOutNetAvailable()48 void NetActivate::TimeOutNetAvailable()
49 {
50 if (netServiceSupplied_) {
51 return;
52 }
53 if (netConnCallback_) {
54 netConnCallback_->NetUnavailable();
55 }
56 if (timeOutHandler_) {
57 timeOutHandler_(requestId_);
58 }
59 }
60
MatchRequestAndNetwork(sptr<NetSupplier> supplier)61 bool NetActivate::MatchRequestAndNetwork(sptr<NetSupplier> supplier)
62 {
63 NETMGR_LOG_E("MatchRequestAndNetwork Enter");
64 if (supplier == nullptr) {
65 NETMGR_LOG_E("supplier is null");
66 return false;
67 }
68 if (!CompareByNetworkIdent(supplier->GetNetSupplierIdent())) {
69 NETMGR_LOG_D("supplier ident is not satisfy");
70 return false;
71 }
72 NetAllCapabilities netAllCaps = supplier->GetNetCapabilities();
73 if (!CompareByNetworkCapabilities(netAllCaps.netCaps_)) {
74 NETMGR_LOG_D("supplier capa is not satisfy");
75 return false;
76 }
77 if (!CompareByNetworkNetType((supplier->GetNetSupplierType()))) {
78 NETMGR_LOG_D("supplier net type not satisfy");
79 return false;
80 }
81 if (!CompareByNetworkBand(netAllCaps.linkUpBandwidthKbps_, netAllCaps.linkDownBandwidthKbps_)) {
82 NETMGR_LOG_D("supplier net band not satisfy");
83 return false;
84 }
85
86 return true;
87 }
88
CompareByNetworkIdent(const std::string & ident)89 bool NetActivate::CompareByNetworkIdent(const std::string &ident)
90 {
91 if (ident.empty() || netSpecifier_->ident_.empty()) {
92 return true;
93 }
94 if (ident == netSpecifier_->ident_) {
95 return true;
96 }
97 return false;
98 }
99
CompareByNetworkCapabilities(const std::set<NetCap> & netCaps)100 bool NetActivate::CompareByNetworkCapabilities(const std::set<NetCap> &netCaps)
101 {
102 if (netSpecifier_ == nullptr) {
103 return false;
104 }
105 std::set<NetCap> &reqCaps = netSpecifier_->netCapabilities_.netCaps_;
106 if (reqCaps.empty()) {
107 return true;
108 }
109 for (auto cap : reqCaps) {
110 if (netCaps.find(cap) == netCaps.end()) {
111 return false;
112 }
113 }
114 return true;
115 }
116
CompareByNetworkNetType(NetBearType bearerType)117 bool NetActivate::CompareByNetworkNetType(NetBearType bearerType)
118 {
119 if (netSpecifier_ == nullptr) {
120 return false;
121 }
122 std::set<NetBearType> &reqTypes = netSpecifier_->netCapabilities_.bearerTypes_;
123 if (reqTypes.empty()) {
124 return true;
125 }
126 if (reqTypes.find(bearerType) == reqTypes.end()) {
127 return false;
128 }
129 return true;
130 }
131
CompareByNetworkBand(uint32_t netLinkUpBand,uint32_t netLinkDownBand)132 bool NetActivate::CompareByNetworkBand(uint32_t netLinkUpBand, uint32_t netLinkDownBand)
133 {
134 uint32_t reqLinkUpBand = netSpecifier_->netCapabilities_.linkUpBandwidthKbps_;
135 uint32_t reqLinkDownBand = netSpecifier_->netCapabilities_.linkDownBandwidthKbps_;
136 if ((netLinkUpBand >= reqLinkUpBand) && (netLinkDownBand >= reqLinkDownBand)) {
137 return true;
138 }
139 return false;
140 }
141
GetNetSpecifier()142 sptr<NetSpecifier> NetActivate::GetNetSpecifier()
143 {
144 return netSpecifier_;
145 }
146
GetRequestId() const147 uint32_t NetActivate::GetRequestId() const
148 {
149 return requestId_;
150 }
151
SetRequestId(uint32_t reqId)152 void NetActivate::SetRequestId(uint32_t reqId)
153 {
154 requestId_ = reqId;
155 }
156
GetServiceSupply() const157 sptr<NetSupplier> NetActivate::GetServiceSupply() const
158 {
159 return netServiceSupplied_;
160 }
161
SetServiceSupply(sptr<NetSupplier> netServiceSupplied)162 void NetActivate::SetServiceSupply(sptr<NetSupplier> netServiceSupplied)
163 {
164 netServiceSupplied_ = netServiceSupplied;
165 }
166
GetNetCallback()167 sptr<INetConnCallback> NetActivate::GetNetCallback()
168 {
169 return netConnCallback_;
170 }
171
HaveCapability(NetCap netCap) const172 bool NetActivate::HaveCapability(NetCap netCap) const
173 {
174 if (netSpecifier_ == nullptr) {
175 return false;
176 }
177 auto &capsRef = netSpecifier_->netCapabilities_.netCaps_;
178 if (capsRef.find(netCap) == capsRef.end()) {
179 return false;
180 }
181 return true;
182 }
183
HaveTypes(const std::set<NetBearType> & bearerTypes) const184 bool NetActivate::HaveTypes(const std::set<NetBearType> &bearerTypes) const
185 {
186 if (netSpecifier_ == nullptr) {
187 return false;
188 }
189 auto &typesRef = netSpecifier_->netCapabilities_.bearerTypes_;
190 bool result = bearerTypes.size() > 0 ? true : false;
191 for (auto type : bearerTypes) {
192 if (typesRef.find(type) == typesRef.end()) {
193 result = false;
194 break;
195 }
196 }
197 return result;
198 }
199 } // namespace NetManagerStandard
200 } // namespace OHOS