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