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