• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "networkshare_upstreammonitor.h"
17 
18 #ifdef SHARE_TRAFFIC_LIMIT_ENABLE
19 #include "cellular_data_client.h"
20 #endif
21 #include "net_manager_constants.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "networkshare_constants.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 namespace {
28 constexpr const char *ERROR_MSG_HAS_NOT_UPSTREAM = "Has not Upstream Network";
29 constexpr const char *ERROR_MSG_UPSTREAM_ERROR = "Get Upstream Network is Error";
30 }
31 
NetConnectionCallback(const std::shared_ptr<NetworkShareUpstreamMonitor> & networkmonitor,int32_t callbackType)32 NetworkShareUpstreamMonitor::NetConnectionCallback::NetConnectionCallback(
33     const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor, int32_t callbackType)
34     : NetworkMonitor_(networkmonitor)
35 {
36 }
37 
NetAvailable(sptr<NetHandle> & netHandle)38 int32_t NetworkShareUpstreamMonitor::NetConnectionCallback::NetAvailable(sptr<NetHandle> &netHandle)
39 {
40     ffrtQueue.submit([weakMonitor = std::weak_ptr(this->NetworkMonitor_), netHandle]() mutable {
41         auto networkMonitor = weakMonitor.lock();
42         if (networkMonitor) {
43             networkMonitor->HandleNetAvailable(netHandle);
44         }
45     });
46     return NETMANAGER_EXT_SUCCESS;
47 }
48 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)49 int32_t NetworkShareUpstreamMonitor::NetConnectionCallback::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
50     const sptr<NetAllCapabilities> &netAllCap)
51 {
52     ffrtQueue.submit([weakMonitor = std::weak_ptr(this->NetworkMonitor_), netHandle, netAllCap]() mutable {
53         auto networkMonitor = weakMonitor.lock();
54         if (networkMonitor) {
55             networkMonitor->HandleNetCapabilitiesChange(netHandle, netAllCap);
56         }
57     });
58     return NETMANAGER_EXT_SUCCESS;
59 }
60 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)61 int32_t NetworkShareUpstreamMonitor::NetConnectionCallback::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
62                                                                                           const sptr<NetLinkInfo> &info)
63 {
64     ffrtQueue.submit([weakMonitor = std::weak_ptr(this->NetworkMonitor_), netHandle, info]() mutable {
65         auto networkMonitor = weakMonitor.lock();
66         if (networkMonitor) {
67             networkMonitor->HandleConnectionPropertiesChange(netHandle, info);
68         }
69     });
70     return NETMANAGER_EXT_SUCCESS;
71 }
72 
NetLost(sptr<NetHandle> & netHandle)73 int32_t NetworkShareUpstreamMonitor::NetConnectionCallback::NetLost(sptr<NetHandle> &netHandle)
74 {
75     ffrtQueue.submit([weakMonitor = std::weak_ptr(this->NetworkMonitor_), netHandle]() mutable {
76         auto networkMonitor = weakMonitor.lock();
77         if (networkMonitor) {
78             networkMonitor->HandleNetLost(netHandle);
79         }
80     });
81     return NETMANAGER_EXT_SUCCESS;
82 }
83 
NetUnavailable()84 int32_t NetworkShareUpstreamMonitor::NetConnectionCallback::NetUnavailable()
85 {
86     return NETMANAGER_EXT_SUCCESS;
87 }
88 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)89 int32_t NetworkShareUpstreamMonitor::NetConnectionCallback::NetBlockStatusChange(sptr<NetHandle> &netHandle,
90                                                                                  bool blocked)
91 {
92     return NETMANAGER_EXT_SUCCESS;
93 }
94 
NetworkShareUpstreamMonitor()95 NetworkShareUpstreamMonitor::NetworkShareUpstreamMonitor() : defaultNetworkId_(INVALID_NETID)
96 {
97     netSpecifier_ = sptr<NetSpecifier>::MakeSptr();
98     netSpecifier_->netCapabilities_.netCaps_ = {NET_CAPABILITY_INTERNET, NET_CAPABILITY_NOT_VPN};
99 }
100 
~NetworkShareUpstreamMonitor()101 NetworkShareUpstreamMonitor::~NetworkShareUpstreamMonitor()
102 {
103     std::lock_guard lock(networkMapMutex_);
104     networkMaps_.clear();
105 }
106 
SetOptionData(int32_t what)107 void NetworkShareUpstreamMonitor::SetOptionData(int32_t what)
108 {
109     eventId_ = what;
110 }
111 
ListenDefaultNetwork()112 void NetworkShareUpstreamMonitor::ListenDefaultNetwork()
113 {
114     std::lock_guard lock(networkCallbackMutex_);
115     if (defaultNetworkCallback_ == nullptr) {
116         defaultNetworkCallback_ =
117             sptr<NetConnectionCallback>::MakeSptr(shared_from_this(), CALLBACK_DEFAULT_INTERNET_NETWORK);
118     }
119 #ifdef SHARE_TRAFFIC_LIMIT_ENABLE
120     bool isSupportDun = false;
121     Telephony::CellularDataClient::GetInstance().GetIfSupportDunApn(isSupportDun);
122     NETMGR_EXT_LOG_I("isSupportDun=%{public}d", isSupportDun);
123     if (isSupportDun) {
124         netSpecifier_->netCapabilities_.netCaps_ = {NET_CAPABILITY_DUN, NET_CAPABILITY_NOT_VPN};
125     } else {
126         netSpecifier_->netCapabilities_.netCaps_ = {NET_CAPABILITY_INTERNET, NET_CAPABILITY_NOT_VPN};
127     }
128 #endif
129     int32_t result = NetConnClient::GetInstance().RegisterNetConnCallback(netSpecifier_, defaultNetworkCallback_, 0);
130     if (result == NETMANAGER_SUCCESS) {
131         NETMGR_EXT_LOG_I("Register defaultNetworkCallback_ successful");
132     } else {
133         NETMGR_EXT_LOG_E("Register defaultNetworkCallback_ failed");
134     }
135 }
136 
UnregisterListenDefaultNetwork()137 void NetworkShareUpstreamMonitor::UnregisterListenDefaultNetwork()
138 {
139     std::lock_guard lock(networkCallbackMutex_);
140     int32_t result = NetConnClient::GetInstance().UnregisterNetConnCallback(defaultNetworkCallback_);
141     if (result == NETMANAGER_SUCCESS) {
142         NETMGR_EXT_LOG_I("UnRegister defaultNetworkCallback_ successful");
143     } else {
144         NETMGR_EXT_LOG_E("UnRegister defaultNetworkCallback_ failed");
145     }
146 }
147 
RegisterUpstreamChangedCallback(const std::shared_ptr<NotifyUpstreamCallback> & callback)148 void NetworkShareUpstreamMonitor::RegisterUpstreamChangedCallback(
149     const std::shared_ptr<NotifyUpstreamCallback> &callback)
150 {
151     notifyUpstreamCallback_ = callback;
152 }
153 
GetCurrentGoodUpstream(std::shared_ptr<UpstreamNetworkInfo> & upstreamNetInfo)154 bool NetworkShareUpstreamMonitor::GetCurrentGoodUpstream(std::shared_ptr<UpstreamNetworkInfo> &upstreamNetInfo)
155 {
156     std::lock_guard lock(networkMapMutex_);
157     auto iter = networkMaps_.find(defaultNetworkId_);
158     if (iter == networkMaps_.end()) {
159         return false;
160     }
161     upstreamNetInfo = iter->second;
162     return true;
163 }
164 
NotifyMainStateMachine(int which,const std::shared_ptr<UpstreamNetworkInfo> & obj)165 void NetworkShareUpstreamMonitor::NotifyMainStateMachine(int which, const std::shared_ptr<UpstreamNetworkInfo> &obj)
166 {
167     if (notifyUpstreamCallback_ == nullptr) {
168         NETMGR_EXT_LOG_E("notifyUpstreamCallback is null.");
169     } else {
170         notifyUpstreamCallback_->OnUpstreamStateChanged(eventId_, which, 0, obj);
171     }
172 }
173 
NotifyMainStateMachine(int which)174 void NetworkShareUpstreamMonitor::NotifyMainStateMachine(int which)
175 {
176     if (notifyUpstreamCallback_ == nullptr) {
177         NETMGR_EXT_LOG_E("notifyUpstreamCallback is null.");
178     } else {
179         notifyUpstreamCallback_->OnUpstreamStateChanged(eventId_, which);
180     }
181 }
182 
HandleNetAvailable(sptr<NetHandle> & netHandle)183 void NetworkShareUpstreamMonitor::HandleNetAvailable(sptr<NetHandle> &netHandle)
184 {
185     if (netHandle == nullptr) {
186         NETMGR_EXT_LOG_E("netHandle is null.");
187         return;
188     }
189     std::lock_guard lock(networkMapMutex_);
190     auto iter = networkMaps_.find(netHandle->GetNetId());
191     if (iter == networkMaps_.end()) {
192         NETMGR_EXT_LOG_I("netHandle[%{public}d] is new.", netHandle->GetNetId());
193         sptr<NetAllCapabilities> netCap = sptr<NetAllCapabilities>::MakeSptr();
194         sptr<NetLinkInfo> linkInfo = sptr<NetLinkInfo>::MakeSptr();
195         std::shared_ptr<UpstreamNetworkInfo> network =
196             std::make_shared<UpstreamNetworkInfo>(netHandle, netCap, linkInfo);
197         networkMaps_.insert(std::make_pair(netHandle->GetNetId(), network));
198     }
199 }
200 
HandleNetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & newNetAllCap)201 void NetworkShareUpstreamMonitor::HandleNetCapabilitiesChange(sptr<NetHandle> &netHandle,
202                                                               const sptr<NetAllCapabilities> &newNetAllCap)
203 {
204     if (netHandle == nullptr || newNetAllCap == nullptr) {
205         NETMGR_EXT_LOG_E("netHandle or netCap is null.");
206         return;
207     }
208     std::lock_guard lock(networkMapMutex_);
209     auto iter = networkMaps_.find(netHandle->GetNetId());
210     if (iter != networkMaps_.end()) {
211         if (iter->second != nullptr && (iter->second)->netAllCap_ != newNetAllCap) {
212             NETMGR_EXT_LOG_I("netHandle[%{public}d] Capabilities Changed.", netHandle->GetNetId());
213             *((iter->second)->netAllCap_) = *(newNetAllCap);
214         }
215     }
216 }
217 
HandleConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & newNetLinkInfo)218 void NetworkShareUpstreamMonitor::HandleConnectionPropertiesChange(sptr<NetHandle> &netHandle,
219                                                                    const sptr<NetLinkInfo> &newNetLinkInfo)
220 {
221     if (netHandle == nullptr || newNetLinkInfo == nullptr) {
222         NETMGR_EXT_LOG_E("netHandle or netLinkInfo is null.");
223         return;
224     }
225     std::shared_ptr<UpstreamNetworkInfo> currentNetwork = nullptr;
226     {
227         std::lock_guard lock(networkMapMutex_);
228         auto iter = networkMaps_.find(netHandle->GetNetId());
229         if (iter != networkMaps_.end()) {
230             if (iter->second != nullptr && (iter->second)->netLinkPro_ != newNetLinkInfo) {
231                 currentNetwork = (iter->second);
232                 NETMGR_EXT_LOG_I("netHandle[%{public}d] ConnectionProperties Changed.", netHandle->GetNetId());
233                 currentNetwork->netLinkPro_ = newNetLinkInfo;
234             }
235         }
236     }
237 
238     if (currentNetwork != nullptr) {
239         if (defaultNetworkId_ == INVALID_NETID || defaultNetworkId_ == netHandle->GetNetId()) {
240             defaultNetworkId_ = netHandle->GetNetId();
241             NETMGR_EXT_LOG_I("Send MainSM ON_LINKPROPERTY event with netHandle[%{public}d].", netHandle->GetNetId());
242             NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_ON_LINKPROPERTIES, currentNetwork);
243         } else {
244             defaultNetworkId_ = netHandle->GetNetId();
245             NETMGR_EXT_LOG_I("Send MainSM ON_SWITCH event with netHandle[%{public}d].", netHandle->GetNetId());
246             NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_DEFAULT_SWITCHED, currentNetwork);
247         }
248     }
249 }
250 
HandleNetLost(sptr<NetHandle> & netHandle)251 void NetworkShareUpstreamMonitor::HandleNetLost(sptr<NetHandle> &netHandle)
252 {
253     if (netHandle == nullptr) {
254         return;
255     }
256     std::shared_ptr<UpstreamNetworkInfo> currentNetInfo = nullptr;
257     {
258         std::lock_guard lock(networkMapMutex_);
259         auto iter = networkMaps_.find(netHandle->GetNetId());
260         if (iter != networkMaps_.end()) {
261             NETMGR_EXT_LOG_I("netHandle[%{public}d] is lost, defaultNetId[%{public}d].", netHandle->GetNetId(),
262                              defaultNetworkId_);
263             currentNetInfo = iter->second;
264             networkMaps_.erase(netHandle->GetNetId());
265         }
266     }
267 
268     if (currentNetInfo != nullptr && defaultNetworkId_ == netHandle->GetNetId()) {
269         NETMGR_EXT_LOG_I("Send MainSM ON_LOST event with netHandle[%{public}d].", defaultNetworkId_);
270         NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_ON_LOST, currentNetInfo);
271         defaultNetworkId_ = INVALID_NETID;
272     }
273 }
274 
OnNetworkConnectChange(int32_t state,int32_t bearerType)275 void NetworkShareUpstreamMonitor::OnNetworkConnectChange(int32_t state, int32_t bearerType)
276 {
277     if (bearerType != BEARER_CELLULAR) {
278         return;
279     }
280     if (state == NET_CONN_STATE_CONNECTED || state == NET_CONN_STATE_DISCONNECTED) {
281         NETMGR_EXT_LOG_I("OnNetworkConnectChange re-Register cell");
282         UnregisterListenDefaultNetwork();
283         ListenDefaultNetwork();
284     }
285 }
286 
MonitorEventHandler(const std::shared_ptr<NetworkShareUpstreamMonitor> & networkmonitor,const std::shared_ptr<AppExecFwk::EventRunner> & runner)287 NetworkShareUpstreamMonitor::MonitorEventHandler::MonitorEventHandler(
288     const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor,
289     const std::shared_ptr<AppExecFwk::EventRunner> &runner)
290     : AppExecFwk::EventHandler(runner), networkMonitor_(networkmonitor)
291 {
292 }
293 } // namespace NetManagerStandard
294 } // namespace OHOS
295