• 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 
~NetworkShareUpstreamMonitor()97 NetworkShareUpstreamMonitor::~NetworkShareUpstreamMonitor()
98 {
99     std::lock_guard lock(networkMapMutex_);
100     networkMaps_.clear();
101 }
102 
SetOptionData(int32_t what)103 void NetworkShareUpstreamMonitor::SetOptionData(int32_t what)
104 {
105     eventId_ = what;
106 }
107 
ListenDefaultNetwork()108 void NetworkShareUpstreamMonitor::ListenDefaultNetwork()
109 {
110     std::lock_guard lock(networkCallbackMutex_);
111     if (defaultNetworkCallback_ == nullptr) {
112         defaultNetworkCallback_ =
113             new (std::nothrow) NetConnectionCallback(shared_from_this(), CALLBACK_DEFAULT_INTERNET_NETWORK);
114     }
115 #ifdef SHARE_TRAFFIC_LIMIT_ENABLE
116     netSpecifier_ = (std::make_unique<NetSpecifier>()).release();
117     bool isSupportDun = false;
118     Telephony::CellularDataClient::GetInstance().GetIfSupportDunApn(isSupportDun);
119     NETMGR_EXT_LOG_I("isSupportDun=%{public}d", isSupportDun);
120     isSupportDun = false;
121     if (isSupportDun) {
122         netSpecifier_->netCapabilities_.netCaps_ = {NET_CAPABILITY_DUN, NET_CAPABILITY_NOT_VPN};
123     } else {
124         netSpecifier_->netCapabilities_.netCaps_ = {NET_CAPABILITY_INTERNET, NET_CAPABILITY_NOT_VPN};
125     }
126     int32_t result = NetConnClient::GetInstance().RegisterNetConnCallback(netSpecifier_, defaultNetworkCallback_, 0);
127 #else
128     int32_t result = NetConnClient::GetInstance().RegisterNetConnCallback(defaultNetworkCallback_);
129 #endif
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     if (upstreamNetInfo == nullptr || upstreamNetInfo->netHandle_ == nullptr) {
157         NETMGR_EXT_LOG_E("NetConnClient or upstreamNetInfo is null.");
158         return false;
159     }
160     bool hasDefaultNet = true;
161     int32_t result = NetConnClient::GetInstance().HasDefaultNet(hasDefaultNet);
162     if (result != NETMANAGER_SUCCESS || !hasDefaultNet) {
163         NetworkShareHisysEvent::GetInstance().SendFaultEvent(
164             NetworkShareEventOperator::OPERATION_GET_UPSTREAM, NetworkShareEventErrorType::ERROR_GET_UPSTREAM,
165             ERROR_MSG_HAS_NOT_UPSTREAM, NetworkShareEventType::SETUP_EVENT);
166         NETMGR_EXT_LOG_E("NetConn hasDefaultNet error[%{public}d].", result);
167         return false;
168     }
169 
170     NetConnClient::GetInstance().GetDefaultNet(*(upstreamNetInfo->netHandle_));
171     int32_t currentNetId = upstreamNetInfo->netHandle_->GetNetId();
172     NETMGR_EXT_LOG_I("NetConn get defaultNet id[%{public}d].", currentNetId);
173     if (currentNetId <= INVALID_NETID) {
174         NetworkShareHisysEvent::GetInstance().SendFaultEvent(
175             NetworkShareEventOperator::OPERATION_GET_UPSTREAM, NetworkShareEventErrorType::ERROR_GET_UPSTREAM,
176             ERROR_MSG_UPSTREAM_ERROR, NetworkShareEventType::SETUP_EVENT);
177         NETMGR_EXT_LOG_E("NetConn get defaultNet id[%{public}d] is error.", currentNetId);
178         return false;
179     }
180 
181     {
182         std::lock_guard lock(networkMapMutex_);
183         auto iter = networkMaps_.find(currentNetId);
184         if (iter == networkMaps_.end()) {
185             return false;
186         }
187         upstreamNetInfo = iter->second;
188     }
189     defaultNetworkId_ = currentNetId;
190     return true;
191 }
192 
NotifyMainStateMachine(int which,const std::shared_ptr<UpstreamNetworkInfo> & obj)193 void NetworkShareUpstreamMonitor::NotifyMainStateMachine(int which, const std::shared_ptr<UpstreamNetworkInfo> &obj)
194 {
195     if (notifyUpstreamCallback_ == nullptr) {
196         NETMGR_EXT_LOG_E("notifyUpstreamCallback is null.");
197     } else {
198         notifyUpstreamCallback_->OnUpstreamStateChanged(eventId_, which, 0, obj);
199     }
200 }
201 
NotifyMainStateMachine(int which)202 void NetworkShareUpstreamMonitor::NotifyMainStateMachine(int which)
203 {
204     if (notifyUpstreamCallback_ == nullptr) {
205         NETMGR_EXT_LOG_E("notifyUpstreamCallback is null.");
206     } else {
207         notifyUpstreamCallback_->OnUpstreamStateChanged(eventId_, which);
208     }
209 }
210 
HandleNetAvailable(sptr<NetHandle> & netHandle)211 void NetworkShareUpstreamMonitor::HandleNetAvailable(sptr<NetHandle> &netHandle)
212 {
213     if (netHandle == nullptr) {
214         NETMGR_EXT_LOG_E("netHandle is null.");
215         return;
216     }
217     std::lock_guard lock(networkMapMutex_);
218     auto iter = networkMaps_.find(netHandle->GetNetId());
219     if (iter == networkMaps_.end()) {
220         NETMGR_EXT_LOG_I("netHandle[%{public}d] is new.", netHandle->GetNetId());
221         sptr<NetAllCapabilities> netCap = new (std::nothrow) NetAllCapabilities();
222         sptr<NetLinkInfo> linkInfo = new (std::nothrow) NetLinkInfo();
223         std::shared_ptr<UpstreamNetworkInfo> network =
224             std::make_shared<UpstreamNetworkInfo>(netHandle, netCap, linkInfo);
225         networkMaps_.insert(std::make_pair(netHandle->GetNetId(), network));
226     }
227 }
228 
HandleNetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & newNetAllCap)229 void NetworkShareUpstreamMonitor::HandleNetCapabilitiesChange(sptr<NetHandle> &netHandle,
230                                                               const sptr<NetAllCapabilities> &newNetAllCap)
231 {
232     if (netHandle == nullptr || newNetAllCap == nullptr) {
233         NETMGR_EXT_LOG_E("netHandle or netCap is null.");
234         return;
235     }
236     std::lock_guard lock(networkMapMutex_);
237     auto iter = networkMaps_.find(netHandle->GetNetId());
238     if (iter != networkMaps_.end()) {
239         if (iter->second != nullptr && (iter->second)->netAllCap_ != newNetAllCap) {
240             NETMGR_EXT_LOG_I("netHandle[%{public}d] Capabilities Changed.", netHandle->GetNetId());
241             *((iter->second)->netAllCap_) = *(newNetAllCap);
242         }
243     }
244 }
245 
HandleConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & newNetLinkInfo)246 void NetworkShareUpstreamMonitor::HandleConnectionPropertiesChange(sptr<NetHandle> &netHandle,
247                                                                    const sptr<NetLinkInfo> &newNetLinkInfo)
248 {
249     if (netHandle == nullptr || newNetLinkInfo == nullptr) {
250         NETMGR_EXT_LOG_E("netHandle or netLinkInfo is null.");
251         return;
252     }
253     std::shared_ptr<UpstreamNetworkInfo> currentNetwork = nullptr;
254     {
255         std::lock_guard lock(networkMapMutex_);
256         auto iter = networkMaps_.find(netHandle->GetNetId());
257         if (iter != networkMaps_.end()) {
258             if (iter->second != nullptr && (iter->second)->netLinkPro_ != newNetLinkInfo) {
259                 currentNetwork = (iter->second);
260                 NETMGR_EXT_LOG_I("netHandle[%{public}d] ConnectionProperties Changed.", netHandle->GetNetId());
261                 currentNetwork->netLinkPro_ = newNetLinkInfo;
262             }
263         }
264     }
265 
266     if (currentNetwork != nullptr) {
267         if (defaultNetworkId_ == INVALID_NETID || defaultNetworkId_ == netHandle->GetNetId()) {
268             NETMGR_EXT_LOG_I("Send MainSM ON_LINKPROPERTY event with netHandle[%{public}d].", netHandle->GetNetId());
269             NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_ON_LINKPROPERTIES, currentNetwork);
270         } else {
271             NETMGR_EXT_LOG_I("Send MainSM ON_SWITCH event with netHandle[%{public}d].", netHandle->GetNetId());
272             NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_DEFAULT_SWITCHED, currentNetwork);
273         }
274         defaultNetworkId_ = netHandle->GetNetId();
275     }
276 }
277 
HandleNetLost(sptr<NetHandle> & netHandle)278 void NetworkShareUpstreamMonitor::HandleNetLost(sptr<NetHandle> &netHandle)
279 {
280     if (netHandle == nullptr) {
281         return;
282     }
283     std::shared_ptr<UpstreamNetworkInfo> currentNetInfo = nullptr;
284     {
285         std::lock_guard lock(networkMapMutex_);
286         auto iter = networkMaps_.find(netHandle->GetNetId());
287         if (iter != networkMaps_.end()) {
288             NETMGR_EXT_LOG_I("netHandle[%{public}d] is lost, defaultNetId[%{public}d].", netHandle->GetNetId(),
289                              defaultNetworkId_);
290             currentNetInfo = iter->second;
291         }
292     }
293 
294     if (currentNetInfo != nullptr && defaultNetworkId_ == netHandle->GetNetId()) {
295         NETMGR_EXT_LOG_I("Send MainSM ON_LOST event with netHandle[%{public}d].", defaultNetworkId_);
296         NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_ON_LOST, currentNetInfo);
297         defaultNetworkId_ = INVALID_NETID;
298     }
299 }
300 
MonitorEventHandler(const std::shared_ptr<NetworkShareUpstreamMonitor> & networkmonitor,const std::shared_ptr<AppExecFwk::EventRunner> & runner)301 NetworkShareUpstreamMonitor::MonitorEventHandler::MonitorEventHandler(
302     const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor,
303     const std::shared_ptr<AppExecFwk::EventRunner> &runner)
304     : AppExecFwk::EventHandler(runner), networkMonitor_(networkmonitor)
305 {
306 }
307 } // namespace NetManagerStandard
308 } // namespace OHOS
309