Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbluetoothdevicediscoveryagent_bluez.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtCore/QLoggingCategory>
5
6#include <QtCore/qcoreapplication.h>
7
11#include "qbluetoothuuid.h"
12
13#include "bluez/bluez5_helper_p.h"
14#include "bluez/objectmanager_p.h"
15#include "bluez/adapter1_bluez5_p.h"
16#include "bluez/device1_bluez5_p.h"
17#include "bluez/properties_p.h"
18#include "bluez/bluetoothmanagement_p.h"
19
21
22Q_DECLARE_LOGGING_CATEGORY(QT_BT_BLUEZ)
23
24using namespace QtBluetoothPrivate; // for D-Bus wrappers
25
26QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate(
27 const QBluetoothAddress &deviceAdapter, QBluetoothDeviceDiscoveryAgent *parent) :
28 adapterAddress(deviceAdapter),
29 q_ptr(parent)
30{
31 initializeBluez5();
32 manager = new OrgFreedesktopDBusObjectManagerInterface(
33 QStringLiteral("org.bluez"),
34 QStringLiteral("/"),
35 QDBusConnection::systemBus(), parent);
36 QObject::connect(manager,
37 &OrgFreedesktopDBusObjectManagerInterface::InterfacesAdded,
38 q_ptr,
39 [this](const QDBusObjectPath &objectPath, InterfaceList interfacesAndProperties) {
40 this->_q_InterfacesAdded(objectPath, interfacesAndProperties);
41 });
42
43 // start private address monitoring
44 BluetoothManagement::instance();
45}
46
47QBluetoothDeviceDiscoveryAgentPrivate::~QBluetoothDeviceDiscoveryAgentPrivate()
48{
49 delete adapter;
50}
51
52//TODO: Qt6 remove the pendingCancel/pendingStart logic as it is cumbersome.
53// It is a behavior change across all platforms and was initially done
54// for Bluez. The behavior should be similar to QBluetoothServiceDiscoveryAgent
55// PendingCancel creates issues whereby the agent is still shutting down
56// but isActive() below already returns false. This means the isActive() is
57// out of sync with the finished() and cancel() signal.
58
59bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const
60{
61 if (pendingStart)
62 return true;
63 if (pendingCancel)
64 return false; //TODO Qt6: remove pending[Cancel|Start] logic (see comment above)
65
66 return adapter;
67}
68
69QBluetoothDeviceDiscoveryAgent::DiscoveryMethods QBluetoothDeviceDiscoveryAgent::supportedDiscoveryMethods()
70{
71 return (ClassicMethod | LowEnergyMethod);
72}
73
74void QBluetoothDeviceDiscoveryAgentPrivate::start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods)
75{
76 if (pendingCancel == true) {
77 pendingStart = true;
78 return;
79 }
80
81 lastError = QBluetoothDeviceDiscoveryAgent::NoError;
82 errorString.clear();
83 discoveredDevices.clear();
84 devicesProperties.clear();
85
86 Q_Q(QBluetoothDeviceDiscoveryAgent);
87
88 bool ok = false;
89 const QString adapterPath = findAdapterForAddress(adapterAddress, &ok);
90 if (!ok || adapterPath.isEmpty()) {
91 qCWarning(QT_BT_BLUEZ) << "Cannot find Bluez 5 adapter for device search" << ok;
92 lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError;
93 errorString = QBluetoothDeviceDiscoveryAgent::tr("Cannot find valid Bluetooth adapter.");
94 q->errorOccurred(lastError);
95 return;
96 }
97
98 adapter = new OrgBluezAdapter1Interface(QStringLiteral("org.bluez"), adapterPath,
99 QDBusConnection::systemBus());
100
101 if (!adapter->powered()) {
102 qCDebug(QT_BT_BLUEZ) << "Aborting device discovery due to offline Bluetooth Adapter";
103 lastError = QBluetoothDeviceDiscoveryAgent::PoweredOffError;
104 errorString = QBluetoothDeviceDiscoveryAgent::tr("Device is powered off");
105 delete adapter;
106 adapter = nullptr;
107 emit q->errorOccurred(lastError);
108 return;
109 }
110
111 QVariantMap map;
112 if (methods == (QBluetoothDeviceDiscoveryAgent::LowEnergyMethod|QBluetoothDeviceDiscoveryAgent::ClassicMethod))
113 map.insert(QStringLiteral("Transport"), QStringLiteral("auto"));
114 else if (methods & QBluetoothDeviceDiscoveryAgent::LowEnergyMethod)
115 map.insert(QStringLiteral("Transport"), QStringLiteral("le"));
116 else
117 map.insert(QStringLiteral("Transport"), QStringLiteral("bredr"));
118
119 // older BlueZ 5.x versions don't have this function
120 // filterReply returns UnknownMethod which we ignore
121 QDBusPendingReply<> filterReply = adapter->SetDiscoveryFilter(map);
122 filterReply.waitForFinished();
123 if (filterReply.isError()) {
124 if (filterReply.error().type() == QDBusError::Other
125 && filterReply.error().name() == QStringLiteral("org.bluez.Error.Failed")) {
126 qCDebug(QT_BT_BLUEZ) << "Discovery method" << methods << "not supported";
127 lastError = QBluetoothDeviceDiscoveryAgent::UnsupportedDiscoveryMethod;
128 errorString = QBluetoothDeviceDiscoveryAgent::tr("One or more device discovery methods "
129 "are not supported on this platform");
130 delete adapter;
131 adapter = nullptr;
132 emit q->errorOccurred(lastError);
133 return;
134 } else if (filterReply.error().type() != QDBusError::UnknownMethod) {
135 qCDebug(QT_BT_BLUEZ) << "SetDiscoveryFilter failed:" << filterReply.error();
136 }
137 }
138
139 QtBluezDiscoveryManager::instance()->registerDiscoveryInterest(adapter->path());
140 QObject::connect(QtBluezDiscoveryManager::instance(), &QtBluezDiscoveryManager::discoveryInterrupted,
141 q, [this](const QString &path){
142 this->_q_discoveryInterrupted(path);
143 });
144 OrgFreedesktopDBusPropertiesInterface *prop = new OrgFreedesktopDBusPropertiesInterface(
145 QStringLiteral("org.bluez"), QStringLiteral(""), QDBusConnection::systemBus());
146 QObject::connect(prop, &OrgFreedesktopDBusPropertiesInterface::PropertiesChanged,
147 q, [this](const QString &interface, const QVariantMap &changedProperties,
148 const QStringList &invalidatedProperties,
149 const QDBusMessage &signal) {
150 this->_q_PropertiesChanged(interface, signal.path(), changedProperties, invalidatedProperties);
151 });
152
153 // remember what we have to cleanup
154 propertyMonitors.append(prop);
155
156 // collect initial set of information
157 QDBusPendingReply<ManagedObjectList> reply = manager->GetManagedObjects();
158 reply.waitForFinished();
159 if (!reply.isError()) {
160 ManagedObjectList managedObjectList = reply.value();
161 for (ManagedObjectList::const_iterator it = managedObjectList.constBegin(); it != managedObjectList.constEnd(); ++it) {
162 const QDBusObjectPath &path = it.key();
163 const InterfaceList &ifaceList = it.value();
164
165 for (InterfaceList::const_iterator jt = ifaceList.constBegin(); jt != ifaceList.constEnd(); ++jt) {
166 const QString &iface = jt.key();
167
168 if (iface == QStringLiteral("org.bluez.Device1")) {
169
170 if (path.path().indexOf(adapter->path()) != 0)
171 continue; //devices whose path doesn't start with same path we skip
172
173 deviceFound(path.path(), jt.value());
174 if (!isActive()) // Can happen if stop() was called from a slot in user code.
175 return;
176 }
177 }
178 }
179 }
180
181 // wait interval and sum up what was found
182 if (!discoveryTimer) {
183 discoveryTimer = new QTimer(q);
184 discoveryTimer->setSingleShot(true);
185 QObject::connect(discoveryTimer, &QTimer::timeout,
186 q, [this]() {
187 this->_q_discoveryFinished();
188 });
189 }
190
191 if (lowEnergySearchTimeout > 0) { // otherwise no timeout and stop() required
192 discoveryTimer->setInterval(lowEnergySearchTimeout);
193 discoveryTimer->start();
194 }
195}
196
197void QBluetoothDeviceDiscoveryAgentPrivate::stop()
198{
199 if (!adapter)
200 return;
201
202 qCDebug(QT_BT_BLUEZ) << Q_FUNC_INFO;
203 pendingCancel = true;
204 pendingStart = false;
205 _q_discoveryFinished();
206}
207
208// Returns invalid QBluetoothDeviceInfo in case of error
209static QBluetoothDeviceInfo createDeviceInfoFromBluez5Device(const QVariantMap& properties)
210{
211 const QBluetoothAddress btAddress(properties[QStringLiteral("Address")].toString());
212 if (btAddress.isNull())
213 return QBluetoothDeviceInfo();
214
215 const QString btName = properties[QStringLiteral("Alias")].toString();
216 quint32 btClass = properties[QStringLiteral("Class")].toUInt();
217
218 QBluetoothDeviceInfo deviceInfo(btAddress, btName, btClass);
219 deviceInfo.setRssi(qvariant_cast<short>(properties[QStringLiteral("RSSI")]));
220
221 QList<QBluetoothUuid> uuids;
222 bool foundLikelyLowEnergyUuid = false;
223 const QStringList foundUuids = qvariant_cast<QStringList>(properties[QStringLiteral("UUIDs")]);
224 for (const auto &u: foundUuids) {
225 const QBluetoothUuid id(u);
226 if (id.isNull())
227 continue;
228
229 if (!foundLikelyLowEnergyUuid) {
230 //once we found one BTLE service we are done
231 bool ok = false;
232 quint16 shortId = id.toUInt16(&ok);
233 quint16 genericAccessInt = static_cast<quint16>(QBluetoothUuid::ServiceClassUuid::GenericAccess);
234 if (ok && ((shortId & genericAccessInt) == genericAccessInt))
235 foundLikelyLowEnergyUuid = true;
236 }
237 uuids.append(id);
238 }
239 deviceInfo.setServiceUuids(uuids);
240
241 if (!btClass) {
242 deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::LowEnergyCoreConfiguration);
243 } else {
244 deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::BaseRateCoreConfiguration);
245 if (foundLikelyLowEnergyUuid)
246 deviceInfo.setCoreConfigurations(QBluetoothDeviceInfo::BaseRateAndLowEnergyCoreConfiguration);
247 }
248
249 const ManufacturerDataList deviceManufacturerData = qdbus_cast<ManufacturerDataList>(properties[QStringLiteral("ManufacturerData")]);
250 const QList<quint16> keysManufacturer = deviceManufacturerData.keys();
251 for (quint16 key : keysManufacturer)
252 deviceInfo.setManufacturerData(
253 key, deviceManufacturerData.value(key).variant().toByteArray());
254
255 const ServiceDataList deviceServiceData =
256 qdbus_cast<ServiceDataList>(properties[QStringLiteral("ServiceData")]);
257 const QList<QString> keysService = deviceServiceData.keys();
258 for (QString key : keysService)
259 deviceInfo.setServiceData(QBluetoothUuid(key),
260 deviceServiceData.value(key).variant().toByteArray());
261
262 return deviceInfo;
263}
264
265void QBluetoothDeviceDiscoveryAgentPrivate::deviceFound(const QString &devicePath,
266 const QVariantMap &properties)
267{
268 Q_Q(QBluetoothDeviceDiscoveryAgent);
269
270 if (!q->isActive())
271 return;
272
273 auto deviceAdapter = qvariant_cast<QDBusObjectPath>(properties[QStringLiteral("Adapter")]);
274 if (deviceAdapter.path() != adapter->path())
275 return;
276
277 // read information
278 QBluetoothDeviceInfo deviceInfo = createDeviceInfoFromBluez5Device(properties);
279 if (!deviceInfo.isValid()) // no point reporting an empty address
280 return;
281
282 qCDebug(QT_BT_BLUEZ) << "Discovered: " << deviceInfo.name() << deviceInfo.address()
283 << "Num UUIDs" << deviceInfo.serviceUuids().size()
284 << "total device" << discoveredDevices.size() << "cached"
285 << "RSSI" << deviceInfo.rssi()
286 << "Num ManufacturerData" << deviceInfo.manufacturerData().size()
287 << "Num ServiceData" << deviceInfo.serviceData().size();
288
289 // Cache the properties so we do not have to access dbus every time to get a value
290 devicesProperties[devicePath] = properties;
291
292 for (qsizetype i = 0; i < discoveredDevices.size(); ++i) {
293 if (discoveredDevices[i].address() == deviceInfo.address()) {
294 if (lowEnergySearchTimeout > 0 && discoveredDevices[i] == deviceInfo) {
295 qCDebug(QT_BT_BLUEZ) << "Duplicate: " << deviceInfo.address();
296 return;
297 }
298 discoveredDevices.replace(i, deviceInfo);
299
300 emit q->deviceDiscovered(deviceInfo);
301 return; // this works if the list doesn't contain duplicates. Don't let it.
302 }
303 }
304
305 discoveredDevices.append(deviceInfo);
306 emit q->deviceDiscovered(deviceInfo);
307}
308
309void QBluetoothDeviceDiscoveryAgentPrivate::_q_InterfacesAdded(const QDBusObjectPath &object_path,
310 InterfaceList interfaces_and_properties)
311{
312 Q_Q(QBluetoothDeviceDiscoveryAgent);
313
314 if (!q->isActive())
315 return;
316
317 if (interfaces_and_properties.contains(QStringLiteral("org.bluez.Device1"))) {
318 // device interfaces belonging to different adapter
319 // will be filtered out by deviceFound();
320 deviceFound(object_path.path(),
321 interfaces_and_properties[QStringLiteral("org.bluez.Device1")]);
322 }
323}
324
325void QBluetoothDeviceDiscoveryAgentPrivate::_q_discoveryFinished()
326{
327 Q_Q(QBluetoothDeviceDiscoveryAgent);
328
329 if (discoveryTimer)
330 discoveryTimer->stop();
331
332 QtBluezDiscoveryManager::instance()->disconnect(q);
333 QtBluezDiscoveryManager::instance()->unregisterDiscoveryInterest(adapter->path());
334
335 qDeleteAll(propertyMonitors);
336 propertyMonitors.clear();
337
338 delete adapter;
339 adapter = nullptr;
340
341 if (pendingCancel && !pendingStart) {
342 pendingCancel = false;
343 emit q->canceled();
344 } else if (pendingStart) {
345 pendingStart = false;
346 pendingCancel = false;
347 start(QBluetoothDeviceDiscoveryAgent::ClassicMethod
348 | QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
349 } else {
350 emit q->finished();
351 }
352}
353
354void QBluetoothDeviceDiscoveryAgentPrivate::_q_discoveryInterrupted(const QString &path)
355{
356 Q_Q(QBluetoothDeviceDiscoveryAgent);
357
358 if (!q->isActive())
359 return;
360
361 if (path == adapter->path()) {
362 qCWarning(QT_BT_BLUEZ) << "Device discovery aborted due to unexpected adapter changes from another process.";
363
364 if (discoveryTimer)
365 discoveryTimer->stop();
366
367 QtBluezDiscoveryManager::instance()->disconnect(q);
368 // no need to call unregisterDiscoveryInterest since QtBluezDiscoveryManager
369 // does this automatically when emitting discoveryInterrupted(QString) signal
370
371 delete adapter;
372 adapter = nullptr;
373
374 errorString = QBluetoothDeviceDiscoveryAgent::tr("Bluetooth adapter error");
375 lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError;
376 emit q->errorOccurred(lastError);
377 }
378}
379
380void QBluetoothDeviceDiscoveryAgentPrivate::_q_PropertiesChanged(const QString &interface,
381 const QString &path,
382 const QVariantMap &changed_properties,
383 const QStringList &invalidated_properties)
384{
385 Q_Q(QBluetoothDeviceDiscoveryAgent);
386 if (interface != QStringLiteral("org.bluez.Device1"))
387 return;
388
389 if (!devicesProperties.contains(path))
390 return;
391
392 // Update the cached properties before checking changed_properties for RSSI and ManufacturerData
393 // so the cached properties are always up to date.
394 QVariantMap & properties = devicesProperties[path];
395 for (QVariantMap::const_iterator it = changed_properties.constBegin();
396 it != changed_properties.constEnd(); ++it) {
397 properties[it.key()] = it.value();
398 }
399
400 for (const QString & property : invalidated_properties)
401 properties.remove(property);
402
403 const auto info = createDeviceInfoFromBluez5Device(properties);
404 if (!info.isValid())
405 return;
406
407 if (changed_properties.contains(QStringLiteral("RSSI"))
408 || changed_properties.contains(QStringLiteral("ManufacturerData"))) {
409
410 for (qsizetype i = 0; i < discoveredDevices.size(); ++i) {
411 if (discoveredDevices[i].address() == info.address()) {
412 QBluetoothDeviceInfo::Fields updatedFields = QBluetoothDeviceInfo::Field::None;
413 if (changed_properties.contains(QStringLiteral("RSSI"))) {
414 qCDebug(QT_BT_BLUEZ) << "Updating RSSI for" << info.address()
415 << changed_properties.value(QStringLiteral("RSSI"));
416 discoveredDevices[i].setRssi(
417 changed_properties.value(QStringLiteral("RSSI")).toInt());
418 updatedFields.setFlag(QBluetoothDeviceInfo::Field::RSSI);
419 }
420 if (changed_properties.contains(QStringLiteral("ManufacturerData"))) {
421 qCDebug(QT_BT_BLUEZ) << "Updating ManufacturerData for" << info.address();
422 ManufacturerDataList changedManufacturerData =
423 qdbus_cast< ManufacturerDataList >(changed_properties.value(QStringLiteral("ManufacturerData")));
424
425 const QList<quint16> keys = changedManufacturerData.keys();
426 bool wasNewValue = false;
427 for (quint16 key : keys) {
428 bool added = discoveredDevices[i].setManufacturerData(key, changedManufacturerData.value(key).variant().toByteArray());
429 wasNewValue = (wasNewValue || added);
430 }
431
432 if (wasNewValue)
433 updatedFields.setFlag(QBluetoothDeviceInfo::Field::ManufacturerData);
434 }
435
436 if (lowEnergySearchTimeout > 0) {
437 if (discoveredDevices[i] != info) { // field other than manufacturer or rssi changed
438 if (discoveredDevices.at(i).name() == info.name()) {
439 qCDebug(QT_BT_BLUEZ) << "Almost Duplicate " << info.address()
440 << info.name() << "- replacing in place";
441 discoveredDevices.replace(i, info);
442 emit q->deviceDiscovered(info);
443 }
444 } else {
445 if (!updatedFields.testFlag(QBluetoothDeviceInfo::Field::None))
446 emit q->deviceUpdated(discoveredDevices[i], updatedFields);
447 }
448
449 return;
450 }
451
452 discoveredDevices.replace(i, info);
453 emit q_ptr->deviceDiscovered(discoveredDevices[i]);
454
455 if (!updatedFields.testFlag(QBluetoothDeviceInfo::Field::None))
456 emit q->deviceUpdated(discoveredDevices[i], updatedFields);
457 return;
458 }
459 }
460 }
461}
462QT_END_NAMESPACE
QMap< QString, QDBusVariant > ServiceDataList
QMap< quint16, QDBusVariant > ManufacturerDataList
static QBluetoothDeviceInfo createDeviceInfoFromBluez5Device(const QVariantMap &properties)