• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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> &paraSubStateMachine, int state,
87                                     int lastError) override;
88     };
89 
90 #ifdef BLUETOOTH_MODOULE
91     class SharingPanObserver : public Bluetooth::PanObserver {
92     public:
93         SharingPanObserver() = default;
94         virtual ~SharingPanObserver() = default;
95 
96         void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int state) override;
97     };
98 #endif
99     class NetSharingSubSmState {
100     public:
101         NetSharingSubSmState(const std::shared_ptr<NetworkShareSubStateMachine> &subStateMachine, bool isNcm);
102         ~NetSharingSubSmState() = default;
103 
104     public:
105         std::shared_ptr<NetworkShareSubStateMachine> subStateMachine_;
106         int32_t lastState_;
107         int32_t lastError_;
108         bool isNcm_;
109     };
110 
111 public:
112     static NetworkShareTracker &GetInstance();
113     ~NetworkShareTracker() = default;
114 
115     /**
116      * Init
117      */
118     bool Init();
119 
120     /**
121      * Uninit
122      */
123     void Uninit();
124 
125     /**
126      * is surpport share network
127      */
128     int32_t IsNetworkSharingSupported(int32_t &supported);
129 
130     /**
131      * has sharing network
132      */
133     int32_t IsSharing(int32_t &sharingStatus);
134 
135     /**
136      * start share network by type
137      */
138     int32_t StartNetworkSharing(const SharingIfaceType &type);
139 
140     /**
141      * stop share netwaork by type
142      */
143     int32_t StopNetworkSharing(const SharingIfaceType &type);
144 
145     /**
146      * get sharable regexs
147      */
148     int32_t GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs);
149 
150     /**
151      * get sharing type
152      */
153     int32_t GetSharingState(const SharingIfaceType type, SharingIfaceState &state);
154 
155     /**
156      * get sharing ifaces name
157      */
158     int32_t GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces);
159 
160     /**
161      * register callback
162      */
163     int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback);
164 
165     /**
166      * unregister callback
167      */
168     int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback);
169 
170     /**
171      * is need update upstream network
172      */
173     bool UpstreamWanted();
174 
175     /**
176      * modify shared sub state machine list
177      */
178     void ModifySharedSubStateMachineList(bool isAdd, const std::shared_ptr<NetworkShareSubStateMachine> &subSm);
179 
180     /**
181      * get the main state machine
182      */
183     std::shared_ptr<NetworkShareMainStateMachine> &GetMainStateMachine();
184 
185     /**
186      * notify shared sub state machine to update upstream interface when upstream network changed
187      */
188     void SetUpstreamNetHandle(const std::shared_ptr<UpstreamNetworkInfo> &netinfo);
189 
190     /**
191      * get the upstream info
192      */
193     void GetUpstreamInfo(std::shared_ptr<UpstreamNetworkInfo> &upstreamInfo);
194 
195     /**
196      * notify shared sub state machine to update upstream interface
197      */
198     void NotifyDownstreamsHasNewUpstreamIface(const std::shared_ptr<UpstreamNetworkInfo> &netinfo);
199 
200     int32_t GetSharedSubSMTraffic(const TrafficType &type, int32_t &kbByte);
201 
202 private:
203     NetworkShareTracker() = default;
204 
205     void HandleSubSmUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> &who, int32_t state,
206                                          int32_t lastError);
207     int32_t EnableNetSharingInternal(const SharingIfaceType &type, bool enable);
208     int32_t SetWifiNetworkSharing(bool enable);
209     int32_t SetUsbNetworkSharing(bool enable);
210     int32_t SetBluetoothNetworkSharing(bool enable);
211     void EnableWifiSubStateMachine();
212     void EnableBluetoothSubStateMachine();
213     int32_t Sharing(const std::string &iface, int32_t reqState);
214     void SendGlobalSharingStateChange();
215     void SendIfaceSharingStateChange(const SharingIfaceType &type, const std::string &iface,
216                                      const SharingIfaceState &state);
217     void SendSharingUpstreamChange(const sptr<NetHandle> &netHandle);
218     int32_t CreateSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType, bool isNcm);
219     void StopSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType);
220     bool IsInterfaceMatchType(const std::string &iface, const SharingIfaceType &type);
221     bool InterfaceNameToType(const std::string &iface, SharingIfaceType &type);
222     bool IsHandleNetlinkEvent(const SharingIfaceType &type, bool up);
223     bool FindSubStateMachine(const std::string &iface, const SharingIfaceType &interfaceType,
224                              std::shared_ptr<NetworkShareSubStateMachine> &subSM, std::string &findKey);
225     void InterfaceAdded(const std::string &iface);
226     void InterfaceRemoved(const std::string &iface);
227     void InterfaceStatusChanged(const std::string &iface, bool up);
228     void SetDnsForwarders(const NetHandle &netHandle);
229     void StopDnsProxy();
230     SharingIfaceState SubSmStateToExportState(int32_t state);
231     static void OnWifiHotspotStateChanged(int state);
232     void RegisterWifiApCallback();
233     void RegisterBtPanCallback();
234     void SetWifiState(const Wifi::ApState &state);
235 #ifdef BLUETOOTH_MODOULE
236     void SetBluetoothState(const Bluetooth::BTConnectState &state);
237 #endif
238     void SendMainSMEvent(const std::shared_ptr<NetworkShareSubStateMachine> &subSM, int32_t event, int32_t state);
239 
240 private:
241     std::mutex mutex_;
242     std::shared_ptr<NetworkShareConfiguration> configuration_ = nullptr;
243     sptr<NetsysControllerCallback> netsysCallback_ = nullptr;
244     std::shared_ptr<NetworkShareTracker::ManagerEventHandler> eventHandler_ = nullptr;
245     std::weak_ptr<NetworkShareUpstreamMonitor::MonitorEventHandler> monitorHandler_;
246     std::shared_ptr<NetworkShareMainStateMachine> mainStateMachine_ = nullptr;
247     std::map<std::string, std::shared_ptr<NetSharingSubSmState>> subStateMachineMap_;
248     std::vector<sptr<ISharingEventCallback>> sharingEventCallback_;
249     std::mutex callbackMutex_;
250     bool isNetworkSharing_ = false;
251     std::shared_ptr<UpstreamNetworkInfo> upstreamInfo_ = nullptr;
252     std::vector<SharingIfaceType> clientRequestsVector_;
253     std::vector<std::shared_ptr<NetworkShareSubStateMachine>> sharedSubSM_;
254     bool isStartDnsProxy_ = false;
255     int32_t wifiShareCount_ = 0;
256     int32_t usbShareCount_ = 0;
257     Wifi::ApState curWifiState_ = Wifi::ApState::AP_STATE_NONE;
258 #ifdef BLUETOOTH_MODOULE
259     std::shared_ptr<SharingPanObserver> panObserver_ = nullptr;
260     int32_t bluetoothShareCount_ = 0;
261     Bluetooth::BTConnectState curBluetoothState_ = Bluetooth::BTConnectState::DISCONNECTED;
262 #endif
263     UsbShareState curUsbState_ = UsbShareState::USB_NONE;
264     std::atomic_bool isInit = false;
265     WifiEvent g_wifiEvent = {0};
266 };
267 } // namespace NetManagerStandard
268 } // namespace OHOS
269 #endif // NETWORKSHARE_TRACKER_H
270