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