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