NetworkManagerQt

fakenetwork.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Jan Grulich <jgrulich@redhat.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "fakenetwork.h"
8#include "connection.h"
9#include "wireddevice.h"
10#include "wirelessdevice.h"
11
12#include <QDBusConnection>
13#include <QDBusMetaType>
14#include <QTimer>
15
16#include "../manager.h"
17#include "connectionsettings.h"
18
19FakeNetwork::FakeNetwork(QObject *parent)
20 : QObject(parent)
21 , m_activatingConnection(QDBusObjectPath("/"))
23 , m_networkingEnabled(true)
24 , m_primaryConnection(QDBusObjectPath("/"))
25 , m_state(20)
26 , m_version(QLatin1String("0.9.10.0"))
27 , m_wimaxEnabled(true)
28 , m_wimaxHardwareEnabled(true)
29 , m_wirelessEnabled(true)
30 , m_wirelessHardwareEnabled(true)
31 , m_wwanEnabled(true)
32 , m_wwanHardwareEnabled(true)
33 , m_activeConnectionsCounter(0)
34 , m_deviceCounter(0)
35 , m_settings(new Settings(this))
36{
37 qDBusRegisterMetaType<QDBusObjectPath>();
38 registerService();
39 connect(m_settings, &Settings::connectionAdded, this, &FakeNetwork::onConnectionAdded);
40 connect(m_settings, &Settings::connectionRemoved, this, &FakeNetwork::onConnectionRemoved);
41}
42
43FakeNetwork::~FakeNetwork()
44{
45 unregisterService();
46 qDeleteAll(m_devices);
47 delete m_settings;
48}
49
50QDBusObjectPath FakeNetwork::activatingConnection() const
51{
52 return m_activatingConnection;
53}
54
55QList<QDBusObjectPath> FakeNetwork::activeConnections() const
56{
57 return m_activeConnections.keys();
58}
59
60uint FakeNetwork::connectivity() const
61{
62 return m_connectivity;
63}
64
65QList<QDBusObjectPath> FakeNetwork::devices() const
66{
67 return m_devices.keys();
68}
69
70bool FakeNetwork::networkingEnabled() const
71{
72 return m_networkingEnabled;
73}
74
75QDBusObjectPath FakeNetwork::primaryConnection() const
76{
77 return m_primaryConnection;
78}
79
80uint FakeNetwork::state() const
81{
82 return m_state;
83}
84
85QString FakeNetwork::version() const
86{
87 return m_version;
88}
89
90bool FakeNetwork::wimaxEnabled() const
91{
92 return m_wimaxEnabled;
93}
94
95void FakeNetwork::setWimaxEnabled(bool enabled)
96{
97 m_wimaxEnabled = enabled;
98
99 QVariantMap map;
100 map.insert(QLatin1String("WimaxEnabled"), m_wimaxEnabled);
101 Q_EMIT PropertiesChanged(map);
102}
103
104bool FakeNetwork::wimaxHardwareEnabled() const
105{
106 return m_wimaxHardwareEnabled;
107}
108
109void FakeNetwork::setWimaxHardwareEnabled(bool enabled)
110{
111 m_wimaxHardwareEnabled = enabled;
112
113 QVariantMap map;
114 map.insert(QLatin1String("WimaxHardwareEnabled"), m_wimaxHardwareEnabled);
115 Q_EMIT PropertiesChanged(map);
116}
117
118bool FakeNetwork::wirelessEnabled() const
119{
120 return m_wirelessEnabled;
121}
122
123void FakeNetwork::setWirelessEnabled(bool enabled)
124{
125 m_wirelessEnabled = enabled;
126
127 QVariantMap map;
128 map.insert(QLatin1String("WirelessEnabled"), m_wirelessEnabled);
129 Q_EMIT PropertiesChanged(map);
130}
131
132bool FakeNetwork::wirelessHardwareEnabled() const
133{
134 return m_wirelessHardwareEnabled;
135}
136
137void FakeNetwork::setWirelessHardwareEnabled(bool enabled)
138{
139 m_wirelessHardwareEnabled = enabled;
140
141 QVariantMap map;
142 map.insert(QLatin1String("WirelessHardwareEnabled"), m_wirelessHardwareEnabled);
143 Q_EMIT PropertiesChanged(map);
144}
145
146bool FakeNetwork::wwanEnabled() const
147{
148 return m_wwanEnabled;
149}
150
151void FakeNetwork::setWwanEnabled(bool enabled)
152{
153 m_wwanEnabled = enabled;
154
155 QVariantMap map;
156 map.insert(QLatin1String("WwanEnabled"), m_wwanEnabled);
157 Q_EMIT PropertiesChanged(map);
158}
159
160bool FakeNetwork::wwanHardwareEnabled() const
161{
162 return m_wwanHardwareEnabled;
163}
164
165void FakeNetwork::addDevice(Device *device)
166{
167 QString newDevicePath = QString("/org/kde/fakenetwork/Devices/") + QString::number(m_deviceCounter++);
168 device->setDevicePath(newDevicePath);
169 m_devices.insert(QDBusObjectPath(newDevicePath), device);
171 Q_EMIT DeviceAdded(QDBusObjectPath(newDevicePath));
172}
173
174void FakeNetwork::removeDevice(Device *device)
175{
176 m_devices.remove(QDBusObjectPath(device->devicePath()));
177 QDBusConnection::sessionBus().unregisterObject(device->devicePath());
178 Q_EMIT DeviceRemoved(QDBusObjectPath(device->devicePath()));
179}
180
181void FakeNetwork::registerService()
182{
186
187 for (auto it = m_devices.cbegin(); it != m_devices.cend(); ++it) {
188 const QDBusObjectPath &devicePath = it.key();
190 Q_EMIT DeviceAdded(devicePath);
191 }
192}
193
194void FakeNetwork::unregisterService()
195{
196 for (auto it = m_devices.cbegin(); it != m_devices.cend(); ++it) {
197 const QDBusObjectPath &devicePath = it.key();
199 Q_EMIT DeviceRemoved(devicePath);
200 }
201
202 QDBusConnection::sessionBus().unregisterObject(QLatin1String("/org/kde/fakenetwork/Settings"));
205}
206
207QDBusObjectPath FakeNetwork::ActivateConnection(const QDBusObjectPath &connection, const QDBusObjectPath &device, const QDBusObjectPath &specific_object)
208{
209 QString newActiveConnectionPath = QString("/org/kde/fakenetwork/ActiveConnection/") + QString::number(m_activeConnectionsCounter++);
210 ActiveConnection *newActiveConnection = new ActiveConnection(this);
211 newActiveConnection->addDevice(device);
212 newActiveConnection->setActiveConnectionPath(newActiveConnectionPath);
213 newActiveConnection->setConnection(connection);
214 newActiveConnection->setSpecificObject(specific_object);
215 newActiveConnection->setState(NetworkManager::ActiveConnection::Activating);
216
217 m_activeConnections.insert(QDBusObjectPath(newActiveConnectionPath), newActiveConnection);
218 QDBusConnection::sessionBus().registerObject(newActiveConnectionPath, newActiveConnection, QDBusConnection::ExportScriptableContents);
219
220 m_activatingConnection = QDBusObjectPath(newActiveConnectionPath);
221
222 QVariantMap map;
223 map.insert(QLatin1String("ActiveConnections"), QVariant::fromValue<QList<QDBusObjectPath>>(m_activeConnections.keys()));
224 map.insert(QLatin1String("ActivatingConnection"), QVariant::fromValue(QDBusObjectPath(newActiveConnectionPath)));
225 Q_EMIT PropertiesChanged(map);
226
227 Device *usedDevice = static_cast<Device *>(QDBusConnection::sessionBus().objectRegisteredAt(device.path()));
228 if (usedDevice) {
229 m_activatedDevice = usedDevice->devicePath();
230 // Start simulation of activation
231 usedDevice->setActiveConnection(newActiveConnectionPath);
232 usedDevice->setState(NetworkManager::Device::Preparing);
233 QTimer::singleShot(100, this, SLOT(updateConnectingState()));
234 }
235
236 return QDBusObjectPath(newActiveConnectionPath);
237}
238
239void FakeNetwork::updateConnectingState()
240{
241 QVariantMap deviceMap;
242 Device *device = m_devices.value(QDBusObjectPath(m_activatedDevice));
243 if (device->state() == NetworkManager::Device::Preparing) {
245 } else if (device->state() == NetworkManager::Device::ConfiguringHardware) {
246 device->setState(NetworkManager::Device::NeedAuth);
247 } else if (device->state() == NetworkManager::Device::NeedAuth) {
249 } else if (device->state() == NetworkManager::Device::ConfiguringIp) {
250 device->setState(NetworkManager::Device::CheckingIp);
251 } else if (device->state() == NetworkManager::Device::CheckingIp) {
252 device->setState(NetworkManager::Device::Activated);
253
254 ActiveConnection *activeConnection =
255 static_cast<ActiveConnection *>(QDBusConnection::sessionBus().objectRegisteredAt(device->activeConnection().path()));
256 if (activeConnection) {
257 QVariantMap activeConnectionMap;
258 activeConnectionMap.insert(QLatin1String("State"), NetworkManager::ActiveConnection::Activated);
259
260 activeConnection->setState(NetworkManager::ActiveConnection::Activated);
261 QDBusMessage message = QDBusMessage::createSignal(activeConnection->activeConnectionPath(),
262 QLatin1String("org.kde.fakenetwork.Connection.Active"),
263 QLatin1String("PropertiesChanged"));
264 message << activeConnectionMap;
266
267 QDBusMessage message2 = QDBusMessage::createSignal(activeConnection->activeConnectionPath(),
268 QLatin1String("org.kde.fakenetwork.Connection.Active"),
269 QLatin1String("StateChanged"));
270 message2 << (uint)2 << (uint)1; // NM_ACTIVE_CONNECTION_STATE_ACTIVATED << NM_ACTIVE_CONNECTION_STATE_REASON_NONE
272 }
273 // TODO: set dhcp4Config, dhcp6Config, ip4Config, ip6Config
274 // IP Interface is usually same as interface
275 device->setIpInterface(device->interface());
276 // Set some IP address
277 device->setIpv4Address(1763189258);
278
279 deviceMap.insert(QLatin1String("IpInterface"), device->ipInterface());
280 deviceMap.insert(QLatin1String("Ip4Address"), device->ip4Address());
281 deviceMap.insert(QLatin1String("ActiveConnection"), m_activatingConnection.path());
282
283 // Update FakeNetwork state, connectivity, primary connection
284 m_connectivity = NetworkManager::Connectivity::Full;
285 m_primaryConnection = m_activatingConnection;
286 m_activatingConnection = QDBusObjectPath("/");
287 m_state = 70;
288 QVariantMap networkMap;
289 networkMap.insert(QLatin1String("ActivatingConnection"), QVariant::fromValue(m_activatingConnection));
290 networkMap.insert(QLatin1String("Connectivity"), NetworkManager::Connectivity::Full);
291 networkMap.insert(QLatin1String("PrimaryConnection"), QVariant::fromValue(m_primaryConnection));
292 networkMap.insert(QLatin1String("State"), m_state);
293
294 QDBusMessage message =
295 QDBusMessage::createSignal(QLatin1String("/org/kde/fakenetwork"), QLatin1String("org.kde.fakenetwork"), QLatin1String("PropertiesChanged"));
296 message << networkMap;
298
299 Q_EMIT StateChanged(m_state);
300 }
301
302 deviceMap.insert(QLatin1String("State"), device->state());
303 QDBusMessage message = QDBusMessage::createSignal(device->devicePath(), device->deviceInterface(), QLatin1String("PropertiesChanged"));
304 message << deviceMap;
306
307 if (device->state() != NetworkManager::Device::Activated) {
308 QTimer::singleShot(100, this, SLOT(updateConnectingState()));
309 }
310}
311
312uint FakeNetwork::CheckConnectivity() const
313{
314 return m_connectivity;
315}
316
317void FakeNetwork::DeactivateConnection(const QDBusObjectPath &active_connection)
318{
319 ActiveConnection *activeConnection = m_activeConnections.value(active_connection);
320 if (activeConnection) {
321 activeConnection->setState(NetworkManager::ActiveConnection::Deactivating);
322
323 QVariantMap activeConnectionMap;
324 activeConnectionMap.insert(QLatin1String("State"), NetworkManager::ActiveConnection::Deactivating);
325
326 activeConnection->setState(NetworkManager::ActiveConnection::Activated);
327 QDBusMessage message = QDBusMessage::createSignal(activeConnection->activeConnectionPath(),
328 QLatin1String("org.kde.fakenetwork.Connection.Active"),
329 QLatin1String("PropertiesChanged"));
330 message << activeConnectionMap;
332
333 QDBusMessage message2 = QDBusMessage::createSignal(activeConnection->activeConnectionPath(),
334 QLatin1String("org.kde.fakenetwork.Connection.Active"),
335 QLatin1String("StateChanged"));
336 message2 << (uint)4 << (uint)2; // NM_ACTIVE_CONNECTION_STATE_DEACTIVATED << NM_ACTIVE_CONNECTION_STATE_REASON_USER_DISCONNECTED
338
339 Device *device = m_devices.value(activeConnection->devices().first());
340 if (device) {
341 m_deactivatedDevice = device->devicePath();
342 device->setState(NetworkManager::Device::Deactivating);
343 QTimer::singleShot(100, this, SLOT(updateDeactivatingState()));
344 }
345
346 // Update FakeNetwork state, connectivity, primary connection
348 m_primaryConnection = QDBusObjectPath("/");
349 m_state = 20;
350 QVariantMap networkMap;
351 networkMap.insert(QLatin1String("Connectivity"), m_connectivity);
352 networkMap.insert(QLatin1String("PrimaryConnection"), QVariant::fromValue(m_primaryConnection));
353 networkMap.insert(QLatin1String("State"), m_state);
354
355 message = QDBusMessage::createSignal(QLatin1String("/org/kde/fakenetwork"), QLatin1String("org.kde.fakenetwork"), QLatin1String("PropertiesChanged"));
356 message << networkMap;
358
359 Q_EMIT StateChanged(m_state);
360 }
361}
362
363void FakeNetwork::updateDeactivatingState()
364{
365 QVariantMap deviceMap;
366 Device *device = m_devices.value(QDBusObjectPath(m_deactivatedDevice));
367 ActiveConnection *activeConnection = static_cast<ActiveConnection *>(QDBusConnection::sessionBus().objectRegisteredAt(device->activeConnection().path()));
368 if (activeConnection) {
369 QVariantMap activeConnectionMap;
370 activeConnectionMap.insert(QLatin1String("State"), NetworkManager::ActiveConnection::Deactivated);
371
372 activeConnection->setState(NetworkManager::ActiveConnection::Activated);
373 QDBusMessage message = QDBusMessage::createSignal(activeConnection->activeConnectionPath(),
374 QLatin1String("org.kde.fakenetwork.Connection.Active"),
375 QLatin1String("PropertiesChanged"));
376 message << activeConnectionMap;
378
379 QDBusMessage message2 = QDBusMessage::createSignal(activeConnection->activeConnectionPath(),
380 QLatin1String("org.kde.fakenetwork.Connection.Active"),
381 QLatin1String("StateChanged"));
382 message2 << (uint)3 << (uint)2; // NM_ACTIVE_CONNECTION_STATE_DEACTIVATING << NM_ACTIVE_CONNECTION_STATE_REASON_USER_DISCONNECTED
384
385 removeActiveConnection(QDBusObjectPath(activeConnection->activeConnectionPath()));
386 }
387
388 device->setActiveConnection(QLatin1String("/"));
389 device->setState(NetworkManager::Device::Disconnected);
390 // TODO: set dhcp4Config, dhcp6Config, ip4Config, ip6Config
391 // IP Interface is usually same as interface
392 device->setIpInterface("");
393 // Set some IP address
394 device->setIpv4Address(0);
395
396 deviceMap.insert(QLatin1String("ActiveConnection"), device->activeConnection().path());
397 deviceMap.insert(QLatin1String("IpInterface"), device->ipInterface());
398 deviceMap.insert(QLatin1String("Ip4Address"), device->ip4Address());
399 deviceMap.insert(QLatin1String("State"), device->state());
400
401 QDBusMessage message = QDBusMessage::createSignal(device->devicePath(), device->deviceInterface(), QLatin1String("PropertiesChanged"));
402 message << deviceMap;
404}
405
406QDBusObjectPath FakeNetwork::GetDeviceByIpIface(const QString &iface)
407{
408 Q_UNUSED(iface)
409 // TODO
410 return QDBusObjectPath();
411}
412
413QList<QDBusObjectPath> FakeNetwork::GetDevices() const
414{
415 return m_devices.keys();
416}
417
418void FakeNetwork::onConnectionAdded(const QDBusObjectPath &connection)
419{
420 Connection *newConnection = static_cast<Connection *>(QDBusConnection::sessionBus().objectRegisteredAt(connection.path()));
421 if (newConnection) {
422 NMVariantMapMap settings = newConnection->GetSettings();
423 NetworkManager::ConnectionSettings::ConnectionType type =
424 NetworkManager::ConnectionSettings::typeFromString(settings.value(QLatin1String("connection")).value(QLatin1String("type")).toString());
425 if (!m_devices.isEmpty()) {
426 Device *selectedDevice = nullptr;
427 for (Device *device : std::as_const(m_devices)) {
428 if (type == NetworkManager::ConnectionSettings::Wired && device->deviceType() == NetworkManager::Device::Ethernet) {
429 selectedDevice = device;
430 device->addAvailableConnection(connection);
431 break;
432 } else if (type == NetworkManager::ConnectionSettings::Wireless && device->deviceType() == NetworkManager::Device::Wifi) {
433 selectedDevice = device;
434 // TODO
435 break;
436 }
437 }
438
439 if (selectedDevice) {
440 QVariantMap map;
441 map.insert(QLatin1String("AvailableConnections"), QVariant::fromValue<QList<QDBusObjectPath>>(selectedDevice->availableConnections()));
442
443 QDBusMessage message =
444 QDBusMessage::createSignal(selectedDevice->devicePath(), selectedDevice->deviceInterface(), QLatin1String("PropertiesChanged"));
445 message << map;
447 }
448 }
449 }
450}
451
452void FakeNetwork::onConnectionRemoved(const QDBusObjectPath &connection)
453{
454 for (Device *device : std::as_const(m_devices)) {
455 if (device && device->availableConnections().contains(connection)) {
456 device->removeAvailableConnection(connection);
457
458 QVariantMap map;
459 map.insert(QLatin1String("AvailableConnections"), QVariant::fromValue<QList<QDBusObjectPath>>(device->availableConnections()));
460
461 QDBusMessage message = QDBusMessage::createSignal(device->devicePath(), device->deviceInterface(), QLatin1String("PropertiesChanged"));
462 message << map;
464 }
465 }
466}
467
468void FakeNetwork::removeActiveConnection(const QDBusObjectPath &activeConnection)
469{
470 delete m_activeConnections.value(activeConnection);
471 m_activeConnections.remove(activeConnection);
473
474 QVariantMap map;
475 map.insert(QLatin1String("ActiveConnections"), QVariant::fromValue<QList<QDBusObjectPath>>(m_activeConnections.keys()));
476 Q_EMIT PropertiesChanged(map);
477}
478
479#include "moc_fakenetwork.cpp"
@ Deactivating
The connection is being torn down and cleaned up.
@ Activating
The connection is activating.
@ Deactivated
The connection is no longer active.
@ Activated
The connection is activated.
@ ConfiguringIp
The IP settings of the device are being requested and configured.
Definition device.h:74
@ ConfiguringHardware
The device is being configured.
Definition device.h:72
@ Disconnected
The device is not connected.
Definition device.h:70
@ Activated
The device is active.
Definition device.h:77
@ NeedAuth
The device is awaiting secrets necessary to continue connection.
Definition device.h:73
@ Preparing
The device is preparing to connect.
Definition device.h:71
@ Deactivating
The device's network connection is being torn down.
Definition device.h:78
@ CheckingIp
The device's IP connectivity ability is being determined.
Definition device.h:75
@ Wifi
the Ieee80211 family of wireless networks
Definition device.h:194
@ Ethernet
Ieee8023 wired ethernet.
Definition device.h:193
char * toString(const EngineQuery &query)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
This class allows querying the underlying system to discover the available network interfaces and rea...
Definition accesspoint.h:21
Connectivity
Describes the network connectivity state.
Definition manager.h:100
@ Full
The host is connected to a network, and appears to be able to reach the full Internet.
Definition manager.h:105
@ NoConnectivity
The host is not connected to any network.
Definition manager.h:102
QObject * objectRegisteredAt(const QString &path) const const
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
bool registerService(const QString &serviceName)
bool send(const QDBusMessage &message) const const
QDBusConnection sessionBus()
void unregisterObject(const QString &path, UnregisterMode mode)
bool unregisterService(const QString &serviceName)
QDBusMessage createSignal(const QString &path, const QString &interface, const QString &name)
QString path() const const
bool contains(const AT &value) const const
T & first()
const_iterator cbegin() const const
const_iterator cend() const const
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
QList< Key > keys() const const
size_type remove(const Key &key)
T value(const Key &key, const T &defaultValue) const const
Q_EMITQ_EMIT
QString number(double n, char format, int precision)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:46:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.