• 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_conn_service.h"
22 #include "net_mgr_log_wrapper.h"
23 #include "app_state_aware.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 static std::atomic<uint32_t> g_nextRequestId = MIN_REQUEST_ID;
28 static std::string IDENT_WIFI = "wifi";
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,const std::shared_ptr<AppExecFwk::EventHandler> & netActEventHandler,uint32_t uid,const int32_t registerType)31 NetActivate::NetActivate(const sptr<NetSpecifier> &specifier, const sptr<INetConnCallback> &callback,
32                          std::weak_ptr<INetActivateCallback> timeoutCallback, const uint32_t &timeoutMS,
33                          const std::shared_ptr<AppExecFwk::EventHandler> &netActEventHandler,
34                          uint32_t uid, const int32_t registerType)
35     : netSpecifier_(specifier),
36       netConnCallback_(callback),
37       timeoutMS_(timeoutMS),
38       timeoutCallback_(timeoutCallback),
39       netActEventHandler_(netActEventHandler),
40       uid_(uid),
41       registerType_(registerType)
42 {
43     requestId_ = g_nextRequestId++;
44     if (g_nextRequestId > MAX_REQUEST_ID) {
45         g_nextRequestId = MIN_REQUEST_ID;
46     }
47 }
48 
StartTimeOutNetAvailable()49 void NetActivate::StartTimeOutNetAvailable()
50 {
51     activateName_ = "NetActivate" + std::to_string(requestId_);
52     auto self = shared_from_this();
53     if (netActEventHandler_ != nullptr && timeoutMS_ > 0) {
54         netActEventHandler_->PostTask([self]() { self->TimeOutNetAvailable(); }, activateName_, timeoutMS_);
55     }
56 }
57 
~NetActivate()58 NetActivate::~NetActivate()
59 {
60     if (netActEventHandler_ != nullptr) {
61         netActEventHandler_->RemoveTask(activateName_);
62     }
63 }
64 
TimeOutNetAvailable()65 void NetActivate::TimeOutNetAvailable()
66 {
67     if (netServiceSupplied_) {
68         return;
69     }
70     if (netConnCallback_) {
71         netConnCallback_->NetUnavailable();
72     }
73 
74     auto timeoutCb = timeoutCallback_.lock();
75     if (timeoutCb) {
76         timeoutCb->OnNetActivateTimeOut(requestId_);
77     }
78 }
79 
MatchRequestAndNetwork(sptr<NetSupplier> supplier,bool skipCheckIdent)80 bool NetActivate::MatchRequestAndNetwork(sptr<NetSupplier> supplier, bool skipCheckIdent)
81 {
82     NETMGR_LOG_D("supplier[%{public}d, %{public}s], request[%{public}d]",
83                  (supplier ? supplier->GetSupplierId() : 0),
84                  (supplier ? supplier->GetNetSupplierIdent().c_str() : "nullptr"), requestId_);
85     if (supplier == nullptr) {
86         NETMGR_LOG_E("Supplier is null");
87         return false;
88     }
89     if (!CompareByNetworkCapabilities(supplier->GetNetCaps())) {
90         NETMGR_LOG_D("Supplier[%{public}d], request[%{public}d], capability is not matched", supplier->GetSupplierId(),
91                      requestId_);
92         return false;
93     }
94     if (!CompareByNetworkNetType((supplier->GetNetSupplierType()))) {
95         NETMGR_LOG_W("Supplier[%{public}d], request[%{public}d], Supplier net type not matched",
96                      supplier->GetSupplierId(), requestId_);
97         return false;
98     }
99     if (!CompareByNetworkIdent(supplier->GetNetSupplierIdent(), supplier->GetNetSupplierType(),
100         skipCheckIdent)) {
101         NETMGR_LOG_W("Supplier[%{public}d], request[%{public}d], Supplier ident is not matched",
102                      supplier->GetSupplierId(), requestId_);
103         return false;
104     }
105     NetAllCapabilities netAllCaps = supplier->GetNetCapabilities();
106     if (!CompareByNetworkBand(netAllCaps.linkUpBandwidthKbps_, netAllCaps.linkDownBandwidthKbps_)) {
107         NETMGR_LOG_W("Supplier[%{public}d], request[%{public}d], supplier net band not matched",
108                      supplier->GetSupplierId(), requestId_);
109         return false;
110     }
111 
112     return true;
113 }
114 
CompareByNetworkIdent(const std::string & ident,NetBearType bearerType,bool skipCheckIdent)115 bool NetActivate::CompareByNetworkIdent(const std::string &ident, NetBearType bearerType, bool skipCheckIdent)
116 {
117     if (ident.empty() || netSpecifier_->ident_.empty()) {
118         return true;
119     }
120     if (IDENT_WIFI == netSpecifier_->ident_) {
121         return true;
122     }
123     if (ident == netSpecifier_->ident_) {
124         return true;
125     }
126     if (skipCheckIdent && BEARER_WIFI == bearerType) {
127         return true;
128     }
129     return false;
130 }
131 
CompareByNetworkCapabilities(const NetCaps & netCaps)132 bool NetActivate::CompareByNetworkCapabilities(const NetCaps &netCaps)
133 {
134     if (netSpecifier_ == nullptr) {
135         return false;
136     }
137     std::set<NetCap> &reqCaps = netSpecifier_->netCapabilities_.netCaps_;
138     if (reqCaps.empty()) {
139         NETMGR_LOG_D("Use default Supplier for empty cap");
140         return netCaps.HasNetCap(NET_CAPABILITY_INTERNET);
141     }
142     return netCaps.HasNetCaps(reqCaps);
143 }
144 
CompareByNetworkNetType(NetBearType bearerType)145 bool NetActivate::CompareByNetworkNetType(NetBearType bearerType)
146 {
147     if (netSpecifier_ == nullptr) {
148         return false;
149     }
150     std::set<NetBearType> &reqTypes = netSpecifier_->netCapabilities_.bearerTypes_;
151     if (reqTypes.empty()) {
152         return true;
153     }
154     if (reqTypes.find(bearerType) == reqTypes.end()) {
155         return false;
156     }
157     return true;
158 }
159 
CompareByNetworkBand(uint32_t netLinkUpBand,uint32_t netLinkDownBand)160 bool NetActivate::CompareByNetworkBand(uint32_t netLinkUpBand, uint32_t netLinkDownBand)
161 {
162     uint32_t reqLinkUpBand = netSpecifier_->netCapabilities_.linkUpBandwidthKbps_;
163     uint32_t reqLinkDownBand = netSpecifier_->netCapabilities_.linkDownBandwidthKbps_;
164     if ((netLinkUpBand >= reqLinkUpBand) && (netLinkDownBand >= reqLinkDownBand)) {
165         return true;
166     }
167     return false;
168 }
169 
GetNetSpecifier()170 sptr<NetSpecifier> NetActivate::GetNetSpecifier()
171 {
172     return netSpecifier_;
173 }
174 
GetRequestId() const175 uint32_t NetActivate::GetRequestId() const
176 {
177     return requestId_;
178 }
179 
GetBearType() const180 std::set<NetBearType> NetActivate::GetBearType() const
181 {
182     return netSpecifier_->netCapabilities_.bearerTypes_;
183 }
184 
GetRegisterType() const185 int32_t NetActivate::GetRegisterType() const
186 {
187     return registerType_;
188 }
189 
SetRequestId(uint32_t reqId)190 void NetActivate::SetRequestId(uint32_t reqId)
191 {
192     requestId_ = reqId;
193 }
194 
GetServiceSupply() const195 sptr<NetSupplier> NetActivate::GetServiceSupply() const
196 {
197     return netServiceSupplied_;
198 }
199 
SetServiceSupply(sptr<NetSupplier> netServiceSupplied)200 void NetActivate::SetServiceSupply(sptr<NetSupplier> netServiceSupplied)
201 {
202     netServiceSupplied_ = netServiceSupplied;
203 }
204 
GetNetCallback()205 sptr<INetConnCallback> NetActivate::GetNetCallback()
206 {
207     return netConnCallback_;
208 }
209 
HaveCapability(NetCap netCap) const210 bool NetActivate::HaveCapability(NetCap netCap) const
211 {
212     if (netSpecifier_ == nullptr) {
213         return false;
214     }
215     auto &capsRef = netSpecifier_->netCapabilities_.netCaps_;
216     if (capsRef.find(netCap) == capsRef.end()) {
217         return false;
218     }
219     return true;
220 }
221 
HaveTypes(const std::set<NetBearType> & bearerTypes) const222 bool NetActivate::HaveTypes(const std::set<NetBearType> &bearerTypes) const
223 {
224     if (netSpecifier_ == nullptr) {
225         return false;
226     }
227     auto &typesRef = netSpecifier_->netCapabilities_.bearerTypes_;
228     bool result = bearerTypes.size() > 0;
229     for (auto type : bearerTypes) {
230         if (typesRef.find(type) == typesRef.end()) {
231             result = false;
232             break;
233         }
234     }
235     return result;
236 }
237 
GetUid() const238 uint32_t NetActivate::GetUid() const
239 {
240     return uid_;
241 }
242 
IsAppFrozened() const243 bool NetActivate::IsAppFrozened() const
244 {
245     bool isAppFrozened = isAppFrozened_.load();
246     return isAppFrozened;
247 }
248 
SetIsAppFrozened(bool isFrozened)249 void NetActivate::SetIsAppFrozened(bool isFrozened)
250 {
251     isAppFrozened_ = isFrozened;
252 }
253 
GetLastCallbackType() const254 CallbackType NetActivate::GetLastCallbackType() const
255 {
256     int32_t lastCallbackType = lastCallbackType_;
257     return static_cast<CallbackType>(lastCallbackType);
258 }
259 
SetLastCallbackType(CallbackType callbackType)260 void NetActivate::SetLastCallbackType(CallbackType callbackType)
261 {
262     if ((callbackType == CALL_TYPE_UPDATE_CAP || callbackType == CALL_TYPE_UPDATE_LINK)
263         && (lastCallbackType_.load() == CALL_TYPE_AVAILABLE)) {
264         return;
265     }
266     lastCallbackType_ = callbackType;
267 }
268 
269 
GetLastServiceSupply()270 sptr<NetSupplier> NetActivate::GetLastServiceSupply()
271 {
272     std::lock_guard<std::mutex> lock(lastNetServiceSuppliedMutex_);
273     return lastNetServiceSupplied_;
274 }
275 
SetLastServiceSupply(sptr<NetSupplier> lastNetServiceSupplied)276 void NetActivate::SetLastServiceSupply(sptr<NetSupplier> lastNetServiceSupplied)
277 {
278     std::lock_guard<std::mutex> lock(lastNetServiceSuppliedMutex_);
279     lastNetServiceSupplied_ = lastNetServiceSupplied;
280 }
281 
IsAllowCallback(CallbackType callbackType)282 bool NetActivate::IsAllowCallback(CallbackType callbackType)
283 {
284     if (!NetConnService::GetInstance()->IsAppFrozenedCallbackLimitation()) {
285         return true;
286     }
287     bool isAppFrozened = isAppFrozened_.load();
288     bool isForegroundApp = AppStateAwareManager::GetInstance().IsForegroundApp(uid_);
289     if (isAppFrozened && !isForegroundApp) {
290         if (lastCallbackType_ != CALL_TYPE_LOST && callbackType == CALL_TYPE_LOST
291             && GetLastServiceSupply() == nullptr) {
292             SetLastServiceSupply(netServiceSupplied_);
293         }
294         SetLastCallbackType(callbackType);
295 
296         NETMGR_LOG_I("UID[%{public}d] is AppFrozened, not Allow send callbackType[%{public}d]",
297             uid_, callbackType);
298         return false;
299     }
300     return true;
301 }
302 
303 } // namespace NetManagerStandard
304 } // namespace OHOS
305