forked from rdkcentral/networkmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkManagerImplementation.h
287 lines (240 loc) · 13.5 KB
/
NetworkManagerImplementation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* If not stated otherwise in this file or this component's LICENSE
* file the following copyright and licenses apply:
*
* Copyright 2022 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#pragma once
#include "Module.h"
#include <iostream>
#include <net/if.h>
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include <atomic>
using namespace std;
#include "INetworkManager.h"
#include "NetworkManagerLogger.h"
#include "WiFiSignalStrengthMonitor.h"
#include "NetworkManagerConnectivity.h"
#include "NetworkManagerStunClient.h"
namespace WPEFramework
{
namespace Plugin
{
class NetworkManagerImplementation : public Exchange::INetworkManager
{
enum NetworkEvents
{
NETMGR_PING,
NETMGR_TRACE,
};
class ConnectivityConf : public Core::JSON::Container {
public:
ConnectivityConf& operator=(const ConnectivityConf&) = delete;
ConnectivityConf()
: Core::JSON::Container()
, endpoint_1(_T("http://clients3.google.com/generate_204"))
, endpoint_2(_T(""))
, endpoint_3(_T(""))
, endpoint_4(_T(""))
, endpoint_5(_T(""))
, ConnectivityCheckInterval(6)
{
Add(_T("endpoint_1"), &endpoint_1);
Add(_T("endpoint_2"), &endpoint_2);
Add(_T("endpoint_3"), &endpoint_3);
Add(_T("endpoint_4"), &endpoint_4);
Add(_T("endpoint_5"), &endpoint_5);
Add(_T("interval"), &ConnectivityCheckInterval);
}
~ConnectivityConf() override = default;
public:
/* connectivity configuration */
Core::JSON::String endpoint_1;
Core::JSON::String endpoint_2;
Core::JSON::String endpoint_3;
Core::JSON::String endpoint_4;
Core::JSON::String endpoint_5;
Core::JSON::DecUInt32 ConnectivityCheckInterval;
};
class Stun : public Core::JSON::Container {
public:
Stun& operator=(const Stun&) = delete;
Stun()
: Core::JSON::Container()
, stunEndpoint(_T("stun.l.google.com"))
, port(19302)
, interval(30)
{
Add(_T("endpoint"), &stunEndpoint);
Add(_T("port"), &port);
Add(_T("interval"), &interval);
}
~Stun() override = default;
public:
/* stun configuration */
Core::JSON::String stunEndpoint;
Core::JSON::DecUInt32 port;
Core::JSON::DecUInt32 interval;
};
class WiFiConfig : public Core::JSON::Container
{
public:
Core::JSON::String ssid;
Core::JSON::String password;
Core::JSON::DecUInt8 security;
WiFiConfig() : Core::JSON::Container()
{
Add(_T("ssid"), &ssid);
Add(_T("password"), &password);
Add(_T("security"), &security);
}
~WiFiConfig() override = default;
WiFiConfig& operator=(const WiFiConfig&) = delete;
};
class Configuration : public Core::JSON::Container {
private:
Configuration(const Configuration&);
Configuration& operator=(const Configuration&);
public:
Configuration()
: Core::JSON::Container()
{
Add(_T("connectivity"), &connectivityConf);
Add(_T("stun"), &stun);
Add(_T("loglevel"), &loglevel);
}
~Configuration() override = default;
public:
ConnectivityConf connectivityConf;
Stun stun;
Core::JSON::DecUInt32 loglevel;
};
class Job : public Core::IDispatch {
public:
Job(function<void()> work)
: _work(work)
{
}
void Dispatch() override
{
_work();
}
private:
function<void()> _work;
};
public:
NetworkManagerImplementation();
~NetworkManagerImplementation() override;
// Do not allow copy/move constructors
NetworkManagerImplementation(const NetworkManagerImplementation &) = delete;
NetworkManagerImplementation &operator=(const NetworkManagerImplementation &) = delete;
BEGIN_INTERFACE_MAP(NetworkManagerImplementation)
INTERFACE_ENTRY(Exchange::INetworkManager)
END_INTERFACE_MAP
// Handle Notification registration/removal
uint32_t Register(INetworkManager::INotification *notification) override;
uint32_t Unregister(INetworkManager::INotification *notification) override;
public:
// Below Control APIs will work with RDK or GNome NW.
/* @brief Get all the Available Interfaces */
uint32_t GetAvailableInterfaces (IInterfaceDetailsIterator*& interfaces/* @out */) override;
/* @brief Get the active Interface used for external world communication */
uint32_t GetPrimaryInterface (string& interface /* @out */) override;
/* @brief Set the active Interface used for external world communication */
uint32_t SetPrimaryInterface (const string& interface/* @in */) override;
/* @brief Enable/Disable the given interface */
uint32_t SetInterfaceState(const string& interface/* @in */, const bool isEnabled/* @in */) override;
/* @brief Get the state of given interface */
uint32_t GetInterfaceState(const string& interface/* @in */, bool& isEnabled/* @out */) override;
/* @brief Get IP Address Of the Interface */
uint32_t GetIPSettings(string& interface /* @inout */, const string& ipversion /* @in */, IPAddress& address /* @out */) override;
/* @brief Set IP Address Of the Interface */
uint32_t SetIPSettings(const string& interface /* @in */, const IPAddress& address /* @in */) override;
// WiFi Specific Methods
/* @brief Initiate a WIFI Scan; This is Async method and returns the scan results as Event */
uint32_t StartWiFiScan(const string& frequency /* @in */, IStringIterator* const ssids/* @in */) override;
uint32_t StopWiFiScan(void) override;
uint32_t GetKnownSSIDs(IStringIterator*& ssids /* @out */) override;
uint32_t AddToKnownSSIDs(const WiFiConnectTo& ssid /* @in */) override;
uint32_t RemoveKnownSSID(const string& ssid /* @in */) override;
uint32_t WiFiConnect(const WiFiConnectTo& ssid /* @in */) override;
uint32_t WiFiDisconnect(void) override;
uint32_t GetConnectedSSID(WiFiSSIDInfo& ssidInfo /* @out */) override;
uint32_t StartWPS(const WiFiWPS& method /* @in */, const string& wps_pin /* @in */) override;
uint32_t StopWPS(void) override;
uint32_t GetWifiState(WiFiState &state) override;
uint32_t GetWiFiSignalStrength(string& ssid /* @out */, string& signalStrength /* @out */, WiFiSignalQuality& quality /* @out */) override;
uint32_t SetStunEndpoint (string const endpoint /* @in */, const uint32_t port /* @in */, const uint32_t bindTimeout /* @in */, const uint32_t cacheTimeout /* @in */) override;
uint32_t GetStunEndpoint (string &endpoint /* @out */, uint32_t& port /* @out */, uint32_t& bindTimeout /* @out */, uint32_t& cacheTimeout /* @out */) const override;
/* @brief Get ConnectivityTest Endpoints */
uint32_t GetConnectivityTestEndpoints(IStringIterator*& endpoints/* @out */) const override;
/* @brief Set ConnectivityTest Endpoints */
uint32_t SetConnectivityTestEndpoints(IStringIterator* const endpoints /* @in */) override;
/* @brief Get Internet Connectivty Status */
uint32_t IsConnectedToInternet(string &ipversion /* @inout */, InternetStatus &result /* @out */) override;
/* @brief Get Authentication URL if the device is behind Captive Portal */
uint32_t GetCaptivePortalURI(string &endpoints/* @out */) const override;
/* @brief Start The Internet Connectivity Monitoring */
uint32_t StartConnectivityMonitoring(const uint32_t interval/* @in */) override;
/* @brief Stop The Internet Connectivity Monitoring */
uint32_t StopConnectivityMonitoring(void) const override;
/* @brief Get the Public IP used for external world communication */
uint32_t GetPublicIP (string &ipversion /* @inout */, string& ipaddress /* @out */) override;
/* @brief Request for ping and get the response in as event. The GUID used in the request will be returned in the event. */
uint32_t Ping (const string ipversion /* @in */, const string endpoint /* @in */, const uint32_t noOfRequest /* @in */, const uint16_t timeOutInSeconds /* @in */, const string guid /* @in */, string& response /* @out */) override;
/* @brief Request for trace get the response in as event. The GUID used in the request will be returned in the event. */
uint32_t Trace (const string ipversion /* @in */, const string endpoint /* @in */, const uint32_t noOfRequest /* @in */, const string guid /* @in */, string& response /* @out */) override;
uint32_t GetSupportedSecurityModes(ISecurityModeIterator*& securityModes /* @out */) const override;
/* @brief Set the network manager plugin log level */
uint32_t SetLogLevel(const Logging& level /* @in */) override;
uint32_t GetLogLevel(Logging& level /* @out */) override;
/* @brief configure network manager plugin */
uint32_t Configure(PluginHost::IShell* service) override;
/* Events */
void ReportInterfaceStateChange(const Exchange::INetworkManager::InterfaceState state, const string interface);
void ReportActiveInterfaceChange(const string prevActiveInterface, const string currentActiveinterface);
void ReportIPAddressChange(const string interface, const string ipversion, const string ipaddress, const Exchange::INetworkManager::IPStatus status);
void ReportInternetStatusChange(const Exchange::INetworkManager::InternetStatus prevState, const Exchange::INetworkManager::InternetStatus currState);
void ReportAvailableSSIDs(const JsonArray &arrayofWiFiScanResults);
void ReportWiFiStateChange(const Exchange::INetworkManager::WiFiState state);
void ReportWiFiSignalStrengthChange(const string ssid, const string strength, const Exchange::INetworkManager::WiFiSignalQuality quality);
private:
void platform_init(void);
void getInitialConnectionState(void);
void executeExternally(NetworkEvents event, const string commandToExecute, string& response);
void threadEventRegistration(void);
void filterScanResults(JsonArray &ssids);
private:
std::list<Exchange::INetworkManager::INotification *> _notificationCallbacks;
Core::CriticalSection _notificationLock;
string m_defaultInterface;
string m_publicIP;
stun::client stunClient;
string m_stunEndpoint;
uint16_t m_stunPort;
uint16_t m_stunBindTimeout;
uint16_t m_stunCacheTimeout;
std::thread m_registrationThread;
string m_filterfrequency;
std::vector<std::string> m_filterSsidslist;
public:
std::atomic<bool> m_ethConnected;
std::atomic<bool> m_wlanConnected;
WiFiSignalStrengthMonitor m_wifiSignalMonitor;
mutable ConnectivityMonitor connectivityMonitor;
};
}
}