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