• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "isharing_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> &paraSubStateMachine, 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 #ifdef WIFI_MODOULE
118     class WifiHotspotCallback : public IRemoteStub<Wifi::IWifiHotspotCallback> {
119     public:
120         void OnHotspotStateChanged(int state) override;
121         void OnHotspotStaJoin(const Wifi::StationInfo &info) override;
122         void OnHotspotStaLeave(const Wifi::StationInfo &info) override;
123     };
124 #endif
125 
126 public:
127     static NetworkShareTracker &GetInstance();
128     ~NetworkShareTracker() = default;
129 
130     /**
131      * Init
132      */
133     bool Init();
134 
135     /**
136      * Uninit
137      */
138     void Uninit();
139 
140     /**
141      * is surpport share network
142      */
143     int32_t IsNetworkSharingSupported(int32_t &supported);
144 
145     /**
146      * has sharing network
147      */
148     int32_t IsSharing(int32_t &sharingStatus);
149 
150     /**
151      * start share network by type
152      */
153     int32_t StartNetworkSharing(const SharingIfaceType &type);
154 
155     /**
156      * stop share netwaork by type
157      */
158     int32_t StopNetworkSharing(const SharingIfaceType &type);
159 
160     /**
161      * get sharable regexs
162      */
163     int32_t GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs);
164 
165     /**
166      * get sharing type
167      */
168     int32_t GetSharingState(const SharingIfaceType type, SharingIfaceState &state);
169 
170     /**
171      * get sharing ifaces name
172      */
173     int32_t GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces);
174 
175     /**
176      * register callback
177      */
178     int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback);
179 
180     /**
181      * unregister callback
182      */
183     int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback);
184 
185     /**
186      * is need update upstream network
187      */
188     bool UpstreamWanted();
189 
190     /**
191      * modify shared sub state machine list
192      */
193     void ModifySharedSubStateMachineList(bool isAdd, const std::shared_ptr<NetworkShareSubStateMachine> &subSm);
194 
195     /**
196      * get the main state machine
197      */
198     std::shared_ptr<NetworkShareMainStateMachine> &GetMainStateMachine();
199 
200     /**
201      * notify shared sub state machine to update upstream interface when upstream network changed
202      */
203     void SetUpstreamNetHandle(const std::shared_ptr<UpstreamNetworkInfo> &netinfo);
204 
205     /**
206      * get the upstream info
207      */
208     void GetUpstreamInfo(std::shared_ptr<UpstreamNetworkInfo> &upstreamInfo);
209 
210     /**
211      * notify shared sub state machine to update upstream interface
212      */
213     void NotifyDownstreamsHasNewUpstreamIface(const std::shared_ptr<UpstreamNetworkInfo> &netinfo);
214 
215     int32_t GetSharedSubSMTraffic(const TrafficType &type, int32_t &kbByte);
216 
217     void RestartResume();
218 
219     /**
220      * notify power disconnect
221      */
222     void OnPowerDisConnected();
223 
224     /**
225      * notify power connect
226      */
227     void OnPowerConnected();
228 
229 #ifdef WIFI_MODOULE
230     void RegisterWifiApCallback();
231 #endif
232 private:
233     NetworkShareTracker() = default;
234 
235     void HandleSubSmUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> &who, int32_t state,
236                                          int32_t lastError);
237     int32_t EnableNetSharingInternal(const SharingIfaceType &type, bool enable);
238     int32_t SetWifiNetworkSharing(bool enable);
239     int32_t SetUsbNetworkSharing(bool enable);
240     int32_t SetBluetoothNetworkSharing(bool enable);
241     void EnableWifiSubStateMachine();
242     void EnableBluetoothSubStateMachine();
243     int32_t Sharing(const std::string &iface, int32_t reqState);
244     void SendGlobalSharingStateChange();
245     void SendIfaceSharingStateChange(const SharingIfaceType &type, const std::string &iface,
246                                      const SharingIfaceState &state);
247     void SendSharingUpstreamChange(const sptr<NetHandle> &netHandle);
248     int32_t CreateSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType, bool isNcm);
249     void StopSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType);
250     bool IsInterfaceMatchType(const std::string &iface, const SharingIfaceType &type);
251     bool InterfaceNameToType(const std::string &iface, SharingIfaceType &type);
252     bool IsHandleNetlinkEvent(const SharingIfaceType &type, bool up);
253     bool FindSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType,
254                              std::shared_ptr<NetworkShareSubStateMachine> &subSM, std::string &findKey);
255     void InterfaceAdded(const std::string &iface);
256     void InterfaceRemoved(const std::string &iface);
257     void InterfaceStatusChanged(const std::string &iface, bool up);
258     void SetDnsForwarders(const NetHandle &netHandle);
259     void StopDnsProxy();
260     SharingIfaceState SubSmStateToExportState(int32_t state);
261     void OnChangeSharingState(const SharingIfaceType &type, bool state);
262     void RegisterBtPanCallback();
263 #ifdef WIFI_MODOULE
264     void SetWifiState(const Wifi::ApState &state);
265     void HandleHotSpotStarted();
266     void HandleHotSpotClosed();
267     void HandleHotSpotStaJoin();
268     void HandleHotSpotStaLeave();
269     void HandleIdleApStopTimer();
270     void StopIdleApStopTimer();
271     void StartIdleApStopTimer();
272     void GetPowerConnected();
273     int32_t EnableHotspot();
274     int32_t DisableHotspot();
275     int32_t GetStationNum(size_t &num);
276 #endif
277 #ifdef BLUETOOTH_MODOULE
278     void SetBluetoothState(const Bluetooth::BTConnectState &state);
279 #endif
280     void SendMainSMEvent(const std::shared_ptr<NetworkShareSubStateMachine> &subSM, int32_t event, int32_t state);
281     bool CheckValidShareInterface(const std::string &iface);
282     void RecoverSharingType();
283     bool CheckIfUpUsbIface(const std::string &iface);
284 
285 private:
286     ffrt::mutex mutex_;
287     std::shared_ptr<NetworkShareConfiguration> configuration_ = nullptr;
288     sptr<NetsysControllerCallback> netsysCallback_ = nullptr;
289     std::shared_ptr<ffrt::queue> networkShareTrackerFfrtQueue_ = nullptr;
290     std::shared_ptr<NetworkShareMainStateMachine> mainStateMachine_ = nullptr;
291     std::map<std::string, std::shared_ptr<NetSharingSubSmState>> subStateMachineMap_;
292     std::vector<sptr<ISharingEventCallback>> sharingEventCallback_;
293     ffrt::mutex callbackMutex_;
294     bool isNetworkSharing_ = false;
295     std::shared_ptr<UpstreamNetworkInfo> upstreamInfo_ = nullptr;
296     std::vector<SharingIfaceType> clientRequestsVector_;
297     std::vector<std::shared_ptr<NetworkShareSubStateMachine>> sharedSubSM_;
298     bool isStartDnsProxy_ = false;
299 #ifdef WIFI_MODOULE
300     int32_t wifiShareCount_ = 0;
301     sptr<WifiHotspotCallback> wifiHotspotCallback_ = nullptr;
302     Wifi::ApState curWifiState_ = Wifi::ApState::AP_STATE_NONE;
303     uint32_t idleApStopTimerId_ = 0;
304     ffrt::mutex apStopTimerMutex_;
305     bool powerConnected_ = false;
306     bool staConnected_ = false;
307 #endif
308 #ifdef BLUETOOTH_MODOULE
309     std::shared_ptr<SharingPanObserver> panObserver_ = nullptr;
310     int32_t bluetoothShareCount_ = 0;
311     Bluetooth::BTConnectState curBluetoothState_ = Bluetooth::BTConnectState::DISCONNECTED;
312 #endif
313 #ifdef USB_MODOULE
314     int32_t usbShareCount_ = 0;
315     UsbShareState curUsbState_ = UsbShareState::USB_NONE;
316     int32_t previousFuncs_ = 512;
317 #endif
318     std::atomic_bool isInit = false;
319     int32_t netId_;
320     std::string mApIfaceName_;
321 };
322 } // namespace NetManagerStandard
323 } // namespace OHOS
324 #endif // NETWORKSHARE_TRACKER_H
325