1/*
2 SPDX-FileCopyrightText: 2008, 2011 Will Stephenson <wstephenson@kde.org>
3 SPDX-FileCopyrightText: 2013 Daniel Nicoletti <dantti12@gmail.com>
4 SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7*/
8
9#include "wirelessdevice.h"
10#include "wirelessdevice_p.h"
11
12#undef signals
13#include <libnm/NetworkManager.h>
14#define signals Q_SIGNALS
15
16#include "manager_p.h"
17
18#include "nmdebug.h"
19#include "utils.h"
20
21#include <QDBusMetaType>
22
23NetworkManager::WirelessDevicePrivate::WirelessDevicePrivate(const QString &path, WirelessDevice *q)
24 : DevicePrivate(path, q)
25#ifdef NMQT_STATIC
26 , wirelessIface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::sessionBus())
27#else
28 , wirelessIface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::systemBus())
29#endif
30 , bitRate(0)
31{
32}
33
34NetworkManager::WirelessDevice::WirelessDevice(const QString &path, QObject *parent)
35 : Device(*new WirelessDevicePrivate(path, this), parent)
36{
37 Q_D(WirelessDevice);
38
39 qDBusRegisterMetaType<QList<QDBusObjectPath>>();
40
41#ifdef NMQT_STATIC
42 connect(&d->wirelessIface, &OrgFreedesktopNetworkManagerDeviceWirelessInterface::PropertiesChanged, d, &WirelessDevicePrivate::propertiesChanged);
43#endif
44
45 connect(sender: &d->wirelessIface, signal: &OrgFreedesktopNetworkManagerDeviceWirelessInterface::AccessPointAdded, context: d, slot: &WirelessDevicePrivate::accessPointAdded);
46 connect(sender: &d->wirelessIface, signal: &OrgFreedesktopNetworkManagerDeviceWirelessInterface::AccessPointRemoved, context: d, slot: &WirelessDevicePrivate::accessPointRemoved);
47
48 const QList<QDBusObjectPath> aps = d->wirelessIface.accessPoints();
49 // qCDebug(NMQT) << "AccessPoint list";
50 for (const QDBusObjectPath &op : aps) {
51 // qCDebug(NMQT) << " " << op.path();
52 d->accessPointAdded(op);
53 }
54
55 // Get all WirelessDevices's properties at once
56 QVariantMap initialProperties = NetworkManagerPrivate::retrieveInitialProperties(interfaceName: d->wirelessIface.staticInterfaceName(), path);
57 if (!initialProperties.isEmpty()) {
58 d->propertiesChanged(properties: initialProperties);
59 }
60
61}
62
63NetworkManager::WirelessDevice::~WirelessDevice()
64{
65}
66
67NetworkManager::Device::Type NetworkManager::WirelessDevice::type() const
68{
69 return NetworkManager::Device::Wifi;
70}
71
72QStringList NetworkManager::WirelessDevice::accessPoints() const
73{
74 Q_D(const WirelessDevice);
75 return d->apMap.keys();
76}
77
78QDBusPendingReply<> NetworkManager::WirelessDevice::requestScan(const QVariantMap &options)
79{
80 Q_D(WirelessDevice);
81 d->lastRequestScan = QDateTime::currentDateTime();
82 return d->wirelessIface.RequestScan(options);
83}
84
85NetworkManager::AccessPoint::Ptr NetworkManager::WirelessDevice::activeAccessPoint() const
86{
87 Q_D(const WirelessDevice);
88 return d->activeAccessPoint;
89}
90
91QString NetworkManager::WirelessDevice::hardwareAddress() const
92{
93 Q_D(const WirelessDevice);
94 return d->hardwareAddress;
95}
96
97QString NetworkManager::WirelessDevice::permanentHardwareAddress() const
98{
99 Q_D(const WirelessDevice);
100 return d->permanentHardwareAddress;
101}
102
103NetworkManager::WirelessDevice::OperationMode NetworkManager::WirelessDevice::mode() const
104{
105 Q_D(const WirelessDevice);
106 return d->mode;
107}
108
109int NetworkManager::WirelessDevice::bitRate() const
110{
111 Q_D(const WirelessDevice);
112 return d->bitRate;
113}
114
115QDateTime NetworkManager::WirelessDevice::lastScan() const
116{
117 Q_D(const WirelessDevice);
118 return d->lastScan;
119}
120
121QDateTime NetworkManager::WirelessDevice::lastRequestScan() const
122{
123 Q_D(const WirelessDevice);
124 return d->lastRequestScan;
125}
126
127NetworkManager::WirelessDevice::Capabilities NetworkManager::WirelessDevice::wirelessCapabilities() const
128{
129 Q_D(const WirelessDevice);
130 return d->wirelessCapabilities;
131}
132
133NetworkManager::AccessPoint::Ptr NetworkManager::WirelessDevice::findAccessPoint(const QString &uni)
134{
135 Q_D(WirelessDevice);
136 NetworkManager::AccessPoint::Ptr accessPoint;
137
138 QMap<QString, NetworkManager::AccessPoint::Ptr>::ConstIterator mapIt = d->apMap.constFind(key: uni);
139 if (mapIt != d->apMap.constEnd()) {
140 accessPoint = mapIt.value();
141 } else if (!uni.isEmpty() && uni != QLatin1String("/")) {
142 d->accessPointAdded(QDBusObjectPath(uni));
143 mapIt = d->apMap.constFind(key: uni);
144 if (mapIt != d->apMap.constEnd()) {
145 accessPoint = mapIt.value();
146 }
147 }
148
149 return accessPoint;
150}
151
152NetworkManager::WirelessNetwork::List NetworkManager::WirelessDevice::networks() const
153{
154 Q_D(const WirelessDevice);
155 return d->networks.values();
156}
157
158NetworkManager::WirelessNetwork::Ptr NetworkManager::WirelessDevice::findNetwork(const QString &ssid) const
159{
160 Q_D(const WirelessDevice);
161 NetworkManager::WirelessNetwork::Ptr ret;
162 if (d->networks.contains(key: ssid)) {
163 ret = d->networks.value(key: ssid);
164 }
165 return ret;
166}
167
168void NetworkManager::WirelessDevicePrivate::accessPointAdded(const QDBusObjectPath &accessPoint)
169{
170 // kDebug(1441) << apPath.path();
171 Q_Q(WirelessDevice);
172
173 if (!apMap.contains(key: accessPoint.path())) {
174 NetworkManager::AccessPoint::Ptr accessPointPtr(new NetworkManager::AccessPoint(accessPoint.path()), &QObject::deleteLater);
175 apMap.insert(key: accessPoint.path(), value: accessPointPtr);
176 Q_EMIT q->accessPointAppeared(uni: accessPoint.path());
177
178 const QString ssid = accessPointPtr->ssid();
179 if (!ssid.isEmpty() && !networks.contains(key: ssid)) {
180 NetworkManager::WirelessNetwork::Ptr wifiNetwork(new NetworkManager::WirelessNetwork(accessPointPtr, q), &QObject::deleteLater);
181 networks.insert(key: ssid, value: wifiNetwork);
182 connect(sender: wifiNetwork.data(), signal: &WirelessNetwork::disappeared, context: this, slot: &WirelessDevicePrivate::removeNetwork);
183 Q_EMIT q->networkAppeared(ssid);
184 }
185 }
186}
187
188void NetworkManager::WirelessDevicePrivate::accessPointRemoved(const QDBusObjectPath &accessPoint)
189{
190 // kDebug(1441) << apPath.path();
191 Q_Q(WirelessDevice);
192 if (!apMap.contains(key: accessPoint.path())) {
193 qCDebug(NMQT) << "Access point list lookup failed for " << accessPoint.path();
194 }
195 Q_EMIT q->accessPointDisappeared(uni: accessPoint.path());
196 apMap.remove(key: accessPoint.path());
197}
198
199void NetworkManager::WirelessDevicePrivate::removeNetwork(const QString &network)
200{
201 Q_Q(WirelessDevice);
202
203 if (networks.contains(key: network)) {
204 networks.remove(key: network);
205 Q_EMIT q->networkDisappeared(ssid: network);
206 }
207}
208
209void NetworkManager::WirelessDevicePrivate::propertyChanged(const QString &property, const QVariant &value)
210{
211 Q_Q(WirelessDevice);
212
213 if (property == QLatin1String("ActiveAccessPoint")) {
214 QDBusObjectPath activeAccessPointTmp = qdbus_cast<QDBusObjectPath>(v: value);
215 activeAccessPoint = q->findAccessPoint(uni: activeAccessPointTmp.path());
216 Q_EMIT q->activeAccessPointChanged(activeAccessPointTmp.path());
217 } else if (property == QLatin1String("HwAddress")) {
218 hardwareAddress = value.toString();
219 Q_EMIT q->hardwareAddressChanged(hardwareAddress);
220 } else if (property == QLatin1String("PermHwAddress")) {
221 permanentHardwareAddress = value.toString();
222 Q_EMIT q->permanentHardwareAddressChanged(permanentHardwareAddress);
223 } else if (property == QLatin1String("Bitrate")) {
224 bitRate = value.toUInt();
225 Q_EMIT q->bitRateChanged(bitrate: bitRate);
226 } else if (property == QLatin1String("Mode")) {
227 mode = q->convertOperationMode(value.toUInt());
228 Q_EMIT q->modeChanged(mode);
229 } else if (property == QLatin1String("WirelessCapabilities")) {
230 wirelessCapabilities = q->convertCapabilities(value.toUInt());
231 Q_EMIT q->wirelessCapabilitiesChanged(wirelessCapabilities);
232 } else if (property == QLatin1String("LastScan")) {
233 lastScan = NetworkManager::clockBootTimeToDateTime(clockBootime: value.toLongLong());
234 Q_EMIT q->lastScanChanged(dateTime: lastScan);
235 } else if (property == QLatin1String("AccessPoints")) {
236 // TODO use this instead AccessPointAdded/Removed signals?
237 } else {
238 DevicePrivate::propertyChanged(property, value);
239 }
240}
241
242NetworkManager::WirelessDevice::OperationMode NetworkManager::WirelessDevice::convertOperationMode(uint theirMode)
243{
244 NetworkManager::WirelessDevice::OperationMode ourMode = NetworkManager::WirelessDevice::Unknown;
245 switch (theirMode) {
246 case NM_802_11_MODE_UNKNOWN:
247 ourMode = NetworkManager::WirelessDevice::Unknown;
248 break;
249 case NM_802_11_MODE_ADHOC:
250 ourMode = NetworkManager::WirelessDevice::Adhoc;
251 break;
252 case NM_802_11_MODE_INFRA:
253 ourMode = NetworkManager::WirelessDevice::Infra;
254 break;
255 case NM_802_11_MODE_AP:
256 ourMode = NetworkManager::WirelessDevice::ApMode;
257 break;
258 default:
259 qCDebug(NMQT) << Q_FUNC_INFO << "Unhandled mode" << theirMode;
260 }
261 return ourMode;
262}
263
264NetworkManager::WirelessDevice::Capabilities NetworkManager::WirelessDevice::convertCapabilities(uint caps)
265{
266 return (NetworkManager::WirelessDevice::Capabilities)caps;
267}
268
269#include "moc_wirelessdevice.cpp"
270#include "moc_wirelessdevice_p.cpp"
271

source code of networkmanager-qt/src/wirelessdevice.cpp