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 #ifndef NETWORKSHARE_NETWORK_MONITOR_H 17 #define NETWORKSHARE_NETWORK_MONITOR_H 18 19 #include <any> 20 #include <map> 21 22 #include "event_handler.h" 23 #include "net_conn_callback_stub.h" 24 #include "net_conn_client.h" 25 #include "networkshare_hisysevent.h" 26 #include "networkshare_state_common.h" 27 28 namespace OHOS { 29 namespace NetManagerStandard { 30 static constexpr int32_t INVALID_NETID = -1; 31 32 class NetworkShareUpstreamMonitor : public std::enable_shared_from_this<NetworkShareUpstreamMonitor> { DECLARE_DELAYED_SINGLETON(NetworkShareUpstreamMonitor)33 DECLARE_DELAYED_SINGLETON(NetworkShareUpstreamMonitor) 34 35 class NetConnectionCallback : public NetConnCallbackStub { 36 public: 37 NetConnectionCallback(const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor, int32_t callbackType); 38 ~NetConnectionCallback() = default; 39 40 int32_t NetAvailable(sptr<NetHandle> &netHandle) override; 41 int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap) override; 42 int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info) override; 43 int32_t NetLost(sptr<NetHandle> &netHandle) override; 44 int32_t NetUnavailable() override; 45 int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked) override; 46 47 private: 48 std::shared_ptr<NetworkShareUpstreamMonitor> NetworkMonitor_; 49 }; 50 51 public: 52 class NotifyUpstreamCallback { 53 public: 54 virtual void OnUpstreamStateChanged(int32_t msgName, int32_t param1) = 0; 55 virtual void OnUpstreamStateChanged(int32_t msgName, int32_t param1, int32_t param2, 56 const std::any &messageObj) = 0; 57 }; 58 59 class MonitorEventHandler : public AppExecFwk::EventHandler { 60 public: 61 MonitorEventHandler(const std::shared_ptr<NetworkShareUpstreamMonitor> &networkmonitor, 62 const std::shared_ptr<AppExecFwk::EventRunner> &runner); 63 ~MonitorEventHandler() = default; 64 65 private: 66 std::shared_ptr<NetworkShareUpstreamMonitor> networkMonitor_; 67 }; 68 69 /** 70 * set eventhandler 71 */ 72 void SetOptionData(int what, std::weak_ptr<MonitorEventHandler> &handler); 73 74 /** 75 * set callback to listen default network modify 76 */ 77 void ListenDefaultNetwork(); 78 79 /** 80 * get current upstream networ (default network now) 81 */ 82 bool GetCurrentGoodUpstream(std::shared_ptr<UpstreamNetworkInfo> &upstreamNetInfo); 83 84 /** 85 * register main state machine callback 86 */ 87 void RegisterUpstreamChangedCallback(const std::shared_ptr<NotifyUpstreamCallback> &callback); 88 89 private: 90 void NotifyMainStateMachine(int32_t which, const std::shared_ptr<UpstreamNetworkInfo> &obj); 91 void NotifyMainStateMachine(int32_t which); 92 void HandleNetAvailable(sptr<NetHandle> &netHandle); 93 void HandleNetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &newNetAllCap); 94 void HandleConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &newNetLinkInfo); 95 void HandleNetLost(sptr<NetHandle> &netHandle); 96 97 private: 98 int32_t eventId_ = 0; 99 sptr<NetConnectionCallback> defaultNetworkCallback_ = nullptr; 100 std::map<int32_t, std::shared_ptr<UpstreamNetworkInfo>> networkMaps_; 101 std::mutex networkMapMutex_; 102 int32_t defaultNetworkId_ = INVALID_NETID; 103 std::weak_ptr<MonitorEventHandler> eventHandler_; 104 std::shared_ptr<NotifyUpstreamCallback> notifyUpstreamCallback_ = nullptr; 105 }; 106 } // namespace NetManagerStandard 107 } // namespace OHOS 108 #endif // NETWORKSHARE_NETWORK_MONITOR_H 109