forked from rdkcentral/networkmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkManager.h
279 lines (242 loc) · 13.1 KB
/
NetworkManager.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
/**
* 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 "INetworkManager.h"
#include "NetworkManagerLogger.h"
#include <string>
#include <atomic>
#include <mutex>
namespace WPEFramework
{
namespace Plugin
{
/**
* NetworkManager plugin that exposes an API over both COM-RPC and JSON-RPC
*
*/
class NetworkManager : public PluginHost::IPlugin, public PluginHost::JSONRPC, public PluginHost::ISubSystem::IInternet
{
/**
* Our notification handling code
*
* Handle both the Activate/Deactivate notifications and provide a handler
* for notifications raised by the COM-RPC API
*/
class Notification : public RPC::IRemoteConnection::INotification,
public Exchange::INetworkManager::INotification
{
private:
Notification() = delete;
Notification(const Notification &) = delete;
Notification &operator=(const Notification &) = delete;
public:
explicit Notification(NetworkManager *parent)
: _parent(*parent)
{
ASSERT(parent != nullptr);
}
virtual ~Notification() override
{
}
public:
void onInterfaceStateChange(const Exchange::INetworkManager::InterfaceState state, const string interface) override
{
_parent.onInterfaceStateChange(state, interface);
}
void onActiveInterfaceChange(const string prevActiveInterface, const string currentActiveinterface) override
{
_parent.onActiveInterfaceChange(prevActiveInterface, currentActiveinterface);
}
void onIPAddressChange(const string interface, const string ipversion, const string ipaddress, const Exchange::INetworkManager::IPStatus status) override
{
_parent.onIPAddressChange(interface, ipversion, ipaddress, status);
}
void onInternetStatusChange(const Exchange::INetworkManager::InternetStatus prevState, const Exchange::INetworkManager::InternetStatus currState) override
{
_parent.onInternetStatusChange(prevState, currState);
}
void onAvailableSSIDs(const string jsonOfScanResults) override
{
_parent.onAvailableSSIDs(jsonOfScanResults);
}
void onWiFiStateChange(const Exchange::INetworkManager::WiFiState state) override
{
_parent.onWiFiStateChange(state);
}
void onWiFiSignalStrengthChange(const string ssid, const string strength, const Exchange::INetworkManager::WiFiSignalQuality quality) override
{
_parent.onWiFiSignalStrengthChange(ssid, strength, quality);
}
// The activated/deactived methods are part of the RPC::IRemoteConnection::INotification
// interface. These are triggered when Thunder detects a connection/disconnection over the
// COM-RPC link.
void Activated(RPC::IRemoteConnection * /* connection */) override
{
}
void Deactivated(RPC::IRemoteConnection *connection) override
{
// Something's caused the remote connection to be lost - this could be a crash
// on the remote side so deactivate ourselves
_parent.Deactivated(connection);
}
// Build QueryInterface implementation, specifying all possible interfaces we implement
BEGIN_INTERFACE_MAP(Notification)
INTERFACE_ENTRY(Exchange::INetworkManager::INotification)
INTERFACE_ENTRY(RPC::IRemoteConnection::INotification)
END_INTERFACE_MAP
private:
NetworkManager &_parent;
};
public:
NetworkManager();
~NetworkManager() override;
// Implement the basic IPlugin interface that all plugins must implement
const string Initialize(PluginHost::IShell *service) override;
void Deinitialize(PluginHost::IShell *service) override;
string Information() const override;
// Do not allow copy/move constructors
NetworkManager(const NetworkManager &) = delete;
NetworkManager &operator=(const NetworkManager &) = delete;
// Build QueryInterface implementation, specifying all possible interfaces we implement
// This is necessary so that consumers can discover which plugin implements what interface
BEGIN_INTERFACE_MAP(NetworkManager)
// Which interfaces do we implement?
INTERFACE_ENTRY(PluginHost::IPlugin)
INTERFACE_ENTRY(PluginHost::IDispatcher)
INTERFACE_ENTRY(PluginHost::ISubSystem::IInternet)
// We need to tell Thunder that this plugin provides the INetworkManager interface, but
// since it's not actually implemented here we tell Thunder where it can
// find the real implementation
// This allows other components to call QueryInterface<INetworkManager>() and
// receive the actual implementation (which could be in-process or out-of-process)
INTERFACE_AGGREGATE(Exchange::INetworkManager, _networkManager)
END_INTERFACE_MAP
/*
* ------------------------------------------------------------------------------------------------------------
* ISubSystem::IInternet methods
* ------------------------------------------------------------------------------------------------------------
*/
string PublicIPAddress() const override
{
return m_publicIPAddress;
}
network_type NetworkType() const override
{
return (m_publicIPAddress.empty() == true ? PluginHost::ISubSystem::IInternet::UNKNOWN : (m_publicIPAddressType == "IPV6" ? PluginHost::ISubSystem::IInternet::IPV6 : PluginHost::ISubSystem::IInternet::IPV4));
}
void PublishToThunderAboutInternet();
/* Class to store and manage cached data */
template<typename CacheValue>
class Cache {
public:
Cache() : is_set(false) {}
Cache& operator=(const CacheValue& value) {
std::lock_guard<std::mutex> lock(mutex);
this->value = value;
is_set.store(true);
return *this;
}
Cache& operator=(CacheValue&& value) {
std::lock_guard<std::mutex> lock(mutex);
this->value = std::move(value);
is_set.store(true);
return *this;
}
bool isSet() const {
return is_set.load();
}
void reset() {
is_set.store(false);
}
const CacheValue& getValue() const {
std::lock_guard<std::mutex> lock(mutex);
return value;
}
CacheValue& getValue() {
std::lock_guard<std::mutex> lock(mutex);
return value;
}
private:
CacheValue value;
std::atomic<bool> is_set;
mutable std::mutex mutex;
};
// cached varibales
Cache<Exchange::INetworkManager::IPAddress> m_ipv4AddressCache;
Cache<Exchange::INetworkManager::IPAddress> m_ipv6AddressCache;
private:
// Notification/event handlers
// Clean up when we're told to deactivate
void Deactivated(RPC::IRemoteConnection *connection);
// JSON-RPC setup
void RegisterAllMethods();
void UnregisterAllMethods();
// JSON-RPC methods (take JSON in, spit JSON back out)
uint32_t SetLogLevel (const JsonObject& parameters, JsonObject& response);
uint32_t GetLogLevel (const JsonObject& parameters, JsonObject& response);
uint32_t GetAvailableInterfaces (const JsonObject& parameters, JsonObject& response);
uint32_t GetPrimaryInterface (const JsonObject& parameters, JsonObject& response);
uint32_t SetPrimaryInterface (const JsonObject& parameters, JsonObject& response);
uint32_t GetInterfaceState(const JsonObject& parameters, JsonObject& response);
uint32_t SetInterfaceState(const JsonObject& parameters, JsonObject& response);
uint32_t GetIPSettings(const JsonObject& parameters, JsonObject& response);
uint32_t SetIPSettings(const JsonObject& parameters, JsonObject& response);
uint32_t GetStunEndpoint(const JsonObject& parameters, JsonObject& response);
uint32_t SetStunEndpoint(const JsonObject& parameters, JsonObject& response);
uint32_t GetConnectivityTestEndpoints(const JsonObject& parameters, JsonObject& response);
uint32_t SetConnectivityTestEndpoints(const JsonObject& parameters, JsonObject& response);
uint32_t IsConnectedToInternet(const JsonObject& parameters, JsonObject& response);
uint32_t GetCaptivePortalURI(const JsonObject& parameters, JsonObject& response);
uint32_t StartConnectivityMonitoring(const JsonObject& parameters, JsonObject& response);
uint32_t StopConnectivityMonitoring(const JsonObject& parameters, JsonObject& response);
uint32_t GetPublicIP(const JsonObject& parameters, JsonObject& response);
uint32_t Ping(const JsonObject& parameters, JsonObject& response);
uint32_t Trace(const JsonObject& parameters, JsonObject& response);
uint32_t StartWiFiScan(const JsonObject& parameters, JsonObject& response);
uint32_t StopWiFiScan(const JsonObject& parameters, JsonObject& response);
uint32_t GetKnownSSIDs(const JsonObject& parameters, JsonObject& response);
uint32_t AddToKnownSSIDs(const JsonObject& parameters, JsonObject& response);
uint32_t RemoveKnownSSID(const JsonObject& parameters, JsonObject& response);
uint32_t WiFiConnect(const JsonObject& parameters, JsonObject& response);
uint32_t WiFiDisconnect(const JsonObject& parameters, JsonObject& response);
uint32_t GetConnectedSSID(const JsonObject& parameters, JsonObject& response);
uint32_t StartWPS(const JsonObject& parameters, JsonObject& response);
uint32_t StopWPS(const JsonObject& parameters, JsonObject& response);
uint32_t GetWifiState(const JsonObject& parameters, JsonObject& response);
uint32_t GetWiFiSignalStrength(const JsonObject& parameters, JsonObject& response);
uint32_t GetSupportedSecurityModes(const JsonObject& parameters, JsonObject& response);
void onInterfaceStateChange(const Exchange::INetworkManager::InterfaceState state, const string interface);
void onActiveInterfaceChange(const string prevActiveInterface, const string currentActiveinterface);
void onIPAddressChange(const string interface, const string ipversion, const string ipaddress, const Exchange::INetworkManager::IPStatus status);
void onInternetStatusChange(const Exchange::INetworkManager::InternetStatus prevState, const Exchange::INetworkManager::InternetStatus currState);
void onAvailableSSIDs(const string jsonOfScanResults);
void onWiFiStateChange(const Exchange::INetworkManager::WiFiState state);
void onWiFiSignalStrengthChange(const string ssid, const string strength, const Exchange::INetworkManager::WiFiSignalQuality quality);
private:
uint32_t _connectionId;
PluginHost::IShell *_service;
PluginHost::IPlugin* _networkManagerImpl;
Exchange::INetworkManager *_networkManager;
Core::Sink<Notification> _notification;
string m_publicIPAddress;
string m_publicIPAddressType;
};
}
}