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     NetConnClient::GetInstance().UnregisterNetConnCallback(defaultNetworkCallback_);
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     defaultNetworkCallback_ =
111         new (std::nothrow) NetConnectionCallback(shared_from_this(), CALLBACK_DEFAULT_INTERNET_NETWORK);
112     int32_t result = NetConnClient::GetInstance().RegisterNetConnCallback(defaultNetworkCallback_);
113     if (result == NETMANAGER_SUCCESS) {
114         NETMGR_EXT_LOG_I("Register defaultNetworkCallback_ successful");
115     } else {
116         NETMGR_EXT_LOG_E("Register defaultNetworkCallback_ failed");
117     }
118 }
119 
RegisterUpstreamChangedCallback(const std::shared_ptr<NotifyUpstreamCallback> & callback)120 void NetworkShareUpstreamMonitor::RegisterUpstreamChangedCallback(
121     const std::shared_ptr<NotifyUpstreamCallback> &callback)
122 {
123     notifyUpstreamCallback_ = callback;
124 }
125 
GetCurrentGoodUpstream(std::shared_ptr<UpstreamNetworkInfo> & upstreamNetInfo)126 bool NetworkShareUpstreamMonitor::GetCurrentGoodUpstream(std::shared_ptr<UpstreamNetworkInfo> &upstreamNetInfo)
127 {
128     if (upstreamNetInfo == nullptr || upstreamNetInfo->netHandle_ == nullptr) {
129         NETMGR_EXT_LOG_E("NetConnClient or upstreamNetInfo is null.");
130         return false;
131     }
132     bool hasDefaultNet = true;
133     int32_t result = NetConnClient::GetInstance().HasDefaultNet(hasDefaultNet);
134     if (result != NETMANAGER_SUCCESS || !hasDefaultNet) {
135         NetworkShareHisysEvent::GetInstance().SendFaultEvent(
136             NetworkShareEventOperator::OPERATION_GET_UPSTREAM, NetworkShareEventErrorType::ERROR_GET_UPSTREAM,
137             ERROR_MSG_HAS_NOT_UPSTREAM, NetworkShareEventType::SETUP_EVENT);
138         NETMGR_EXT_LOG_E("NetConn hasDefaultNet error[%{public}d].", result);
139         return false;
140     }
141 
142     NetConnClient::GetInstance().GetDefaultNet(*(upstreamNetInfo->netHandle_));
143     int32_t currentNetId = upstreamNetInfo->netHandle_->GetNetId();
144     NETMGR_EXT_LOG_I("NetConn get defaultNet id[%{public}d].", currentNetId);
145     if (currentNetId <= INVALID_NETID) {
146         NetworkShareHisysEvent::GetInstance().SendFaultEvent(
147             NetworkShareEventOperator::OPERATION_GET_UPSTREAM, NetworkShareEventErrorType::ERROR_GET_UPSTREAM,
148             ERROR_MSG_UPSTREAM_ERROR, NetworkShareEventType::SETUP_EVENT);
149         NETMGR_EXT_LOG_E("NetConn get defaultNet id[%{public}d] is error.", currentNetId);
150         return false;
151     }
152 
153     {
154         std::lock_guard lock(networkMapMutex_);
155         auto iter = networkMaps_.find(currentNetId);
156         if (iter == networkMaps_.end()) {
157             return false;
158         }
159         upstreamNetInfo = iter->second;
160     }
161     defaultNetworkId_ = currentNetId;
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 = new (std::nothrow) NetAllCapabilities();
194         sptr<NetLinkInfo> linkInfo = new (std::nothrow) NetLinkInfo();
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_->ifaceName_ = newNetLinkInfo->ifaceName_;
234             }
235         }
236     }
237 
238     if (currentNetwork != nullptr && defaultNetworkId_ != netHandle->GetNetId()) {
239         if (defaultNetworkId_ == INVALID_NETID) {
240             NETMGR_EXT_LOG_I("Send MainSM ON_LINKPROPERTY event with netHandle[%{public}d].", netHandle->GetNetId());
241             NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_ON_LINKPROPERTIES, currentNetwork);
242         } else {
243             NETMGR_EXT_LOG_I("Send MainSM ON_SWITCH event with netHandle[%{public}d].", netHandle->GetNetId());
244             NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_DEFAULT_SWITCHED, currentNetwork);
245         }
246         defaultNetworkId_ = netHandle->GetNetId();
247     }
248 }
249 
HandleNetLost(sptr<NetHandle> & netHandle)250 void NetworkShareUpstreamMonitor::HandleNetLost(sptr<NetHandle> &netHandle)
251 {
252     if (netHandle == nullptr) {
253         return;
254     }
255     std::shared_ptr<UpstreamNetworkInfo> currentNetInfo = nullptr;
256     {
257         std::lock_guard lock(networkMapMutex_);
258         auto iter = networkMaps_.find(netHandle->GetNetId());
259         if (iter != networkMaps_.end()) {
260             NETMGR_EXT_LOG_I("netHandle[%{public}d] is lost, defaultNetId[%{public}d].", netHandle->GetNetId(),
261                              defaultNetworkId_);
262             currentNetInfo = iter->second;
263         }
264     }
265 
266     if (currentNetInfo != nullptr && defaultNetworkId_ == netHandle->GetNetId()) {
267         NETMGR_EXT_LOG_I("Send MainSM ON_LOST event with netHandle[%{public}d].", defaultNetworkId_);
268         NotifyMainStateMachine(EVENT_UPSTREAM_CALLBACK_ON_LOST, currentNetInfo);
269         defaultNetworkId_ = INVALID_NETID;
270     }
271 }
272 
MonitorEventHandler(const std::shared_ptr<NetworkShareUpstreamMonitor> & networkmonitor,const std::shared_ptr<AppExecFwk::EventRunner> & runner)273 NetworkShareUpstreamMonitor::MonitorEventHandler::MonitorEventHandler(
274     const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor,
275     const std::shared_ptr<AppExecFwk::EventRunner> &runner)
276     : AppExecFwk::EventHandler(runner), networkMonitor_(networkmonitor)
277 {
278 }
279 } // namespace NetManagerStandard
280 } // namespace OHOS
281