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