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