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_TRACKER_H 17 #define NETWORKSHARE_TRACKER_H 18 19 #include <any> 20 #include <atomic> 21 #include <map> 22 23 #ifdef BLUETOOTH_MODOULE 24 #include "bluetooth_pan.h" 25 #include "bluetooth_remote_device.h" 26 #endif 27 #include "event_handler.h" 28 #include "i_netshare_result_callback.h" 29 #include "i_sharing_event_callback.h" 30 #include "i_wifi_hotspot_callback.h" 31 #include "net_manager_ext_constants.h" 32 #include "netsys_controller_callback.h" 33 #include "networkshare_configuration.h" 34 #include "networkshare_hisysevent.h" 35 #include "networkshare_main_statemachine.h" 36 #include "networkshare_sub_statemachine.h" 37 #include "networkshare_upstreammonitor.h" 38 #include "wifi_ap_msg.h" 39 #include "wifi_hotspot.h" 40 41 namespace OHOS { 42 namespace NetManagerStandard { 43 enum class EHandlerEventType { EVENT_HANDLER_MSG_FIR = 1, EVENT_HANDLER_MSG_SEC = 2 }; 44 45 class NetworkShareMainStateMachine; 46 class NetworkShareTracker { 47 class NetsysCallback : public NetsysControllerCallback { 48 public: 49 NetsysCallback() = default; 50 ~NetsysCallback() = default; 51 52 int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int, int) override; 53 int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int, int) override; 54 int32_t OnInterfaceAdded(const std::string &iface) override; 55 int32_t OnInterfaceRemoved(const std::string &iface) override; 56 int32_t OnInterfaceChanged(const std::string &, bool) override; 57 int32_t OnInterfaceLinkStateChanged(const std::string &, bool) override; 58 int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &) override; 59 int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override; 60 int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override; 61 }; 62 63 class ManagerEventHandler : public AppExecFwk::EventHandler { 64 public: 65 explicit ManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner); 66 ~ManagerEventHandler() = default; 67 68 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 69 }; 70 71 class MainSmUpstreamCallback : public NetworkShareUpstreamMonitor::NotifyUpstreamCallback { 72 public: 73 MainSmUpstreamCallback() = default; 74 virtual ~MainSmUpstreamCallback() = default; 75 76 void OnUpstreamStateChanged(int32_t msgName, int32_t param1) override; 77 void OnUpstreamStateChanged(int32_t msgName, int32_t param1, int32_t param2, 78 const std::any &messageObj) override; 79 }; 80 81 class SubSmUpstreamCallback : public NetworkShareSubStateMachine::SubStateMachineCallback { 82 public: 83 SubSmUpstreamCallback() = default; 84 virtual ~SubSmUpstreamCallback() = default; 85 86 void OnUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> ¶SubStateMachine, int state, 87 int lastError) override; 88 }; 89 90 class WifiShareHotspotEventCallback : public Wifi::IWifiHotspotCallback { 91 public: 92 WifiShareHotspotEventCallback() = default; 93 virtual ~WifiShareHotspotEventCallback() = default; 94 95 void OnHotspotStateChanged(int state) override; 96 void OnHotspotStaJoin(const Wifi::StationInfo &info) override; 97 void OnHotspotStaLeave(const Wifi::StationInfo &info) override; 98 OHOS::sptr<OHOS::IRemoteObject> AsObject() override; 99 }; 100 101 #ifdef BLUETOOTH_MODOULE 102 class SharingPanObserver : public Bluetooth::PanObserver { 103 public: 104 SharingPanObserver() = default; 105 virtual ~SharingPanObserver() = default; 106 107 void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int state) override; 108 }; 109 #endif 110 class NetSharingSubSmState { 111 public: 112 NetSharingSubSmState(const std::shared_ptr<NetworkShareSubStateMachine> &subStateMachine, bool isNcm); 113 ~NetSharingSubSmState() = default; 114 115 public: 116 std::shared_ptr<NetworkShareSubStateMachine> subStateMachine_; 117 int32_t lastState_; 118 int32_t lastError_; 119 bool isNcm_; 120 }; 121 122 public: 123 static NetworkShareTracker &GetInstance(); 124 ~NetworkShareTracker() = default; 125 126 /** 127 * Init 128 */ 129 bool Init(); 130 131 /** 132 * Uninit 133 */ 134 void Uninit(); 135 136 /** 137 * is surpport share network 138 */ 139 int32_t IsNetworkSharingSupported(int32_t &supported); 140 141 /** 142 * has sharing network 143 */ 144 int32_t IsSharing(int32_t &sharingStatus); 145 146 /** 147 * start share network by type 148 */ 149 int32_t StartNetworkSharing(const SharingIfaceType &type); 150 151 /** 152 * stop share netwaork by type 153 */ 154 int32_t StopNetworkSharing(const SharingIfaceType &type); 155 156 /** 157 * get sharable regexs 158 */ 159 int32_t GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs); 160 161 /** 162 * get sharing type 163 */ 164 int32_t GetSharingState(const SharingIfaceType type, SharingIfaceState &state); 165 166 /** 167 * get sharing ifaces name 168 */ 169 int32_t GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces); 170 171 /** 172 * register callback 173 */ 174 int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback); 175 176 /** 177 * unregister callback 178 */ 179 int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback); 180 181 /** 182 * is need update upstream network 183 */ 184 bool UpstreamWanted(); 185 186 /** 187 * modify shared sub state machine list 188 */ 189 void ModifySharedSubStateMachineList(bool isAdd, const std::shared_ptr<NetworkShareSubStateMachine> &subSm); 190 191 /** 192 * get the main state machine 193 */ 194 std::shared_ptr<NetworkShareMainStateMachine> &GetMainStateMachine(); 195 196 /** 197 * notify shared sub state machine to update upstream interface when upstream network changed 198 */ 199 void SetUpstreamNetHandle(const std::shared_ptr<UpstreamNetworkInfo> &netinfo); 200 201 /** 202 * get the upstream info 203 */ 204 void GetUpstreamInfo(std::shared_ptr<UpstreamNetworkInfo> &upstreamInfo); 205 206 /** 207 * notify shared sub state machine to update upstream interface 208 */ 209 void NotifyDownstreamsHasNewUpstreamIface(const std::shared_ptr<UpstreamNetworkInfo> &netinfo); 210 211 int32_t GetSharedSubSMTraffic(const TrafficType &type, int32_t &kbByte); 212 213 private: 214 NetworkShareTracker() = default; 215 216 void HandleSubSmUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> &who, int32_t state, 217 int32_t lastError); 218 int32_t EnableNetSharingInternal(const SharingIfaceType &type, bool enable); 219 int32_t SetWifiNetworkSharing(bool enable); 220 int32_t SetUsbNetworkSharing(bool enable); 221 int32_t SetBluetoothNetworkSharing(bool enable); 222 void EnableWifiSubStateMachine(); 223 void EnableBluetoothSubStateMachine(); 224 int32_t Sharing(const std::string &iface, int32_t reqState); 225 void SendGlobalSharingStateChange(); 226 void SendIfaceSharingStateChange(const SharingIfaceType &type, const std::string &iface, 227 const SharingIfaceState &state); 228 void SendSharingUpstreamChange(const sptr<NetHandle> &netHandle); 229 int32_t CreateSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType, bool isNcm); 230 void StopSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType); 231 bool IsInterfaceMatchType(const std::string &iface, const SharingIfaceType &type); 232 bool InterfaceNameToType(const std::string &iface, SharingIfaceType &type); 233 bool IsHandleNetlinkEvent(const SharingIfaceType &type, bool up); 234 bool FindSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType, 235 std::shared_ptr<NetworkShareSubStateMachine> &subSM, std::string &findKey); 236 void InterfaceAdded(const std::string &iface); 237 void InterfaceRemoved(const std::string &iface); 238 void InterfaceStatusChanged(const std::string &iface, bool up); 239 void SetDnsForwarders(const NetHandle &netHandle); 240 void StopDnsProxy(); 241 SharingIfaceState SubSmStateToExportState(int32_t state); 242 void RegisterWifiApCallback(); 243 void RegisterBtPanCallback(); 244 void SetWifiState(const Wifi::ApState &state); 245 #ifdef BLUETOOTH_MODOULE 246 void SetBluetoothState(const Bluetooth::BTConnectState &state); 247 #endif 248 void SendMainSMEvent(const std::shared_ptr<NetworkShareSubStateMachine> &subSM, int32_t event, int32_t state); 249 250 private: 251 std::mutex mutex_; 252 std::shared_ptr<NetworkShareConfiguration> configuration_ = nullptr; 253 sptr<NetsysControllerCallback> netsysCallback_ = nullptr; 254 std::shared_ptr<NetworkShareTracker::ManagerEventHandler> eventHandler_ = nullptr; 255 std::weak_ptr<NetworkShareUpstreamMonitor::MonitorEventHandler> monitorHandler_; 256 std::shared_ptr<NetworkShareMainStateMachine> mainStateMachine_ = nullptr; 257 std::map<std::string, std::shared_ptr<NetSharingSubSmState>> subStateMachineMap_; 258 std::vector<sptr<ISharingEventCallback>> sharingEventCallback_; 259 std::mutex callbackMutex_; 260 bool isNetworkSharing_ = false; 261 std::shared_ptr<UpstreamNetworkInfo> upstreamInfo_ = nullptr; 262 std::vector<SharingIfaceType> clientRequestsVector_; 263 std::vector<std::shared_ptr<NetworkShareSubStateMachine>> sharedSubSM_; 264 bool isStartDnsProxy_ = false; 265 int32_t wifiShareCount_ = 0; 266 int32_t usbShareCount_ = 0; 267 Wifi::ApState curWifiState_ = Wifi::ApState::AP_STATE_NONE; 268 #ifdef BLUETOOTH_MODOULE 269 int32_t bluetoothShareCount_ = 0; 270 std::shared_ptr<SharingPanObserver> panObserver_ = nullptr; 271 Bluetooth::BTConnectState curBluetoothState_ = Bluetooth::BTConnectState::DISCONNECTED; 272 #endif 273 UsbShareState curUsbState_ = UsbShareState::USB_NONE; 274 std::atomic_bool isInit = false; 275 }; 276 } // namespace NetManagerStandard 277 } // namespace OHOS 278 #endif // NETWORKSHARE_TRACKER_H 279