• 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 #include "bluetooth_pan.h"
24 #include "bluetooth_remote_device.h"
25 #include "event_handler.h"
26 #include "i_netshare_result_callback.h"
27 #include "i_sharing_event_callback.h"
28 #include "i_wifi_hotspot_callback.h"
29 #include "net_manager_ext_constants.h"
30 #include "netsys_controller_callback.h"
31 #include "networkshare_configuration.h"
32 #include "networkshare_hisysevent.h"
33 #include "networkshare_main_statemachine.h"
34 #include "networkshare_sub_statemachine.h"
35 #include "networkshare_upstreammonitor.h"
36 #include "wifi_ap_msg.h"
37 #include "wifi_hotspot.h"
38 
39 namespace OHOS {
40 namespace NetManagerStandard {
41 enum class EHandlerEventType { EVENT_HANDLER_MSG_FIR = 1, EVENT_HANDLER_MSG_SEC = 2 };
42 
43 class NetworkShareMainStateMachine;
44 class NetworkShareTracker {
45     class NetsysCallback : public NetsysControllerCallback {
46     public:
47         NetsysCallback() = default;
48         ~NetsysCallback() = default;
49 
50         int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int, int) override;
51         int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int, int) override;
52         int32_t OnInterfaceAdded(const std::string &iface) override;
53         int32_t OnInterfaceRemoved(const std::string &iface) override;
54         int32_t OnInterfaceChanged(const std::string &, bool) override;
55         int32_t OnInterfaceLinkStateChanged(const std::string &, bool) override;
56         int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &) override;
57         int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override;
58         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
59     };
60 
61     class ManagerEventHandler : public AppExecFwk::EventHandler {
62     public:
63         explicit ManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner);
64         ~ManagerEventHandler() = default;
65 
66         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
67     };
68 
69     class MainSmUpstreamCallback : public NetworkShareUpstreamMonitor::NotifyUpstreamCallback {
70     public:
71         MainSmUpstreamCallback() = default;
72         virtual ~MainSmUpstreamCallback() = default;
73 
74         void OnUpstreamStateChanged(int32_t msgName, int32_t param1) override;
75         void OnUpstreamStateChanged(int32_t msgName, int32_t param1, int32_t param2,
76                                     const std::any &messageObj) override;
77     };
78 
79     class SubSmUpstreamCallback : public NetworkShareSubStateMachine::SubStateMachineCallback {
80     public:
81         SubSmUpstreamCallback() = default;
82         virtual ~SubSmUpstreamCallback() = default;
83 
84         void OnUpdateInterfaceState(const std::shared_ptr<NetworkShareSubStateMachine> &paraSubStateMachine, int state,
85                                     int lastError) override;
86     };
87 
88     class WifiShareHotspotEventCallback : public Wifi::IWifiHotspotCallback {
89     public:
90         WifiShareHotspotEventCallback() = default;
91         virtual ~WifiShareHotspotEventCallback() = default;
92 
93         void OnHotspotStateChanged(int state) override;
94         void OnHotspotStaJoin(const Wifi::StationInfo &info) override;
95         void OnHotspotStaLeave(const Wifi::StationInfo &info) override;
96         OHOS::sptr<OHOS::IRemoteObject> AsObject() override;
97     };
98 
99     class SharingPanObserver : public Bluetooth::PanObserver {
100     public:
101         SharingPanObserver() = default;
102         virtual ~SharingPanObserver() = default;
103 
104         void OnConnectionStateChanged(const Bluetooth::BluetoothRemoteDevice &device, int state) override;
105     };
106 
107     class NetSharingSubSmState {
108     public:
109         NetSharingSubSmState(const std::shared_ptr<NetworkShareSubStateMachine> &subStateMachine, bool isNcm);
110         ~NetSharingSubSmState() = default;
111 
112     public:
113         std::shared_ptr<NetworkShareSubStateMachine> subStateMachine_;
114         int32_t lastState_;
115         int32_t lastError_;
116         bool isNcm_;
117     };
118 
119 public:
120     static NetworkShareTracker &GetInstance();
121     ~NetworkShareTracker() = default;
122 
123     /**
124      * Init
125      */
126     bool Init();
127 
128     /**
129      * Uninit
130      */
131     void Uninit();
132 
133     /**
134      * is surpport share network
135      */
136     int32_t IsNetworkSharingSupported(int32_t &supported);
137 
138     /**
139      * has sharing network
140      */
141     int32_t IsSharing(int32_t &sharingStatus);
142 
143     /**
144      * start share network by type
145      */
146     int32_t StartNetworkSharing(const SharingIfaceType &type);
147 
148     /**
149      * stop share netwaork by type
150      */
151     int32_t StopNetworkSharing(const SharingIfaceType &type);
152 
153     /**
154      * get sharable regexs
155      */
156     int32_t GetSharableRegexs(SharingIfaceType type, std::vector<std::string> &ifaceRegexs);
157 
158     /**
159      * get sharing type
160      */
161     int32_t GetSharingState(const SharingIfaceType type, SharingIfaceState &state);
162 
163     /**
164      * get sharing ifaces name
165      */
166     int32_t GetNetSharingIfaces(const SharingIfaceState &state, std::vector<std::string> &ifaces);
167 
168     /**
169      * register callback
170      */
171     int32_t RegisterSharingEvent(sptr<ISharingEventCallback> callback);
172 
173     /**
174      * unregister callback
175      */
176     int32_t UnregisterSharingEvent(sptr<ISharingEventCallback> callback);
177 
178     /**
179      * is need update upstream network
180      */
181     bool UpstreamWanted();
182 
183     /**
184      * modify shared sub state machine list
185      */
186     void ModifySharedSubStateMachineList(bool isAdd, const std::shared_ptr<NetworkShareSubStateMachine> &subSm);
187 
188     /**
189      * get the main state machine
190      */
191     std::shared_ptr<NetworkShareMainStateMachine> &GetMainStateMachine();
192 
193     /**
194      * notify shared sub state machine to update upstream interface when upstream network changed
195      */
196     void SetUpstreamNetHandle(const std::shared_ptr<UpstreamNetworkInfo> &netinfo);
197 
198     /**
199      * get the upstream info
200      */
201     void GetUpstreamInfo(std::shared_ptr<UpstreamNetworkInfo> &upstreamInfo);
202 
203     /**
204      * notify shared sub state machine to update upstream interface
205      */
206     void NotifyDownstreamsHasNewUpstreamIface(const std::shared_ptr<UpstreamNetworkInfo> &netinfo);
207 
208     int32_t GetSharedSubSMTraffic(const TrafficType &type, int32_t &kbByte);
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 RegisterWifiApCallback();
240     void RegisterBtPanCallback();
241     void SetWifiState(const Wifi::ApState &state);
242     void SetBluetoothState(const Bluetooth::BTConnectState &state);
243     void SendMainSMEvent(const std::shared_ptr<NetworkShareSubStateMachine> &subSM, int32_t event, int32_t state);
244 
245 private:
246     std::mutex mutex_;
247     std::shared_ptr<NetworkShareConfiguration> configuration_ = nullptr;
248     sptr<NetsysControllerCallback> netsysCallback_ = nullptr;
249     std::shared_ptr<NetworkShareTracker::ManagerEventHandler> eventHandler_ = nullptr;
250     std::weak_ptr<NetworkShareUpstreamMonitor::MonitorEventHandler> monitorHandler_;
251     std::shared_ptr<NetworkShareMainStateMachine> mainStateMachine_ = nullptr;
252     std::map<std::string, std::shared_ptr<NetSharingSubSmState>> subStateMachineMap_;
253     std::vector<sptr<ISharingEventCallback>> sharingEventCallback_;
254     std::mutex callbackMutex_;
255     std::shared_ptr<SharingPanObserver> panObserver_ = nullptr;
256     bool isNetworkSharing_ = false;
257     std::shared_ptr<UpstreamNetworkInfo> upstreamInfo_ = nullptr;
258     std::vector<SharingIfaceType> clientRequestsVector_;
259     std::vector<std::shared_ptr<NetworkShareSubStateMachine>> sharedSubSM_;
260     bool isStartDnsProxy_ = false;
261     int32_t wifiShareCount_ = 0;
262     int32_t bluetoothShareCount_ = 0;
263     int32_t usbShareCount_ = 0;
264     Wifi::ApState curWifiState_ = Wifi::ApState::AP_STATE_NONE;
265     Bluetooth::BTConnectState curBluetoothState_ = Bluetooth::BTConnectState::DISCONNECTED;
266     UsbShareState curUsbState_ = UsbShareState::USB_NONE;
267     std::atomic_bool isInit = false;
268 };
269 } // namespace NetManagerStandard
270 } // namespace OHOS
271 #endif // NETWORKSHARE_TRACKER_H
272