From 515c20f5f20dfb4e665e635a30485d7931ff12df Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov (VMware)" Date: Mon, 21 May 2018 17:16:18 +0200 Subject: win32: remove usage of QFuture in qbluetoothdevicediscoveryagent Introduce a couple of QThread instances and workers for the Bluetooth Classic and BLE device discovery. Replaces the usage of QFuture for this file. Remove includes of QtConcurrent. This introduced some errors which are solved by including: - QLoggingCategory in qbluetoothdevicediscoveryagent_win.cpp - QDataStream and QCoreApplication in qlowenergycontroller_win.cpp Change-Id: Iba2cbc147c714ae87515294d50cb4e502edd00a7 Reviewed-by: Denis Shienkov Reviewed-by: Oliver Wolff --- src/bluetooth/qbluetoothdevicediscoveryagent_p.h | 33 ++++++++++-- .../qbluetoothdevicediscoveryagent_win.cpp | 63 ++++++++++++++-------- src/bluetooth/qlowenergycontroller_win.cpp | 2 + 3 files changed, 72 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h index ee3531cb..489ed807 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h @@ -80,7 +80,28 @@ QT_END_NAMESPACE #endif #ifdef QT_WIN_BLUETOOTH -#include +QT_BEGIN_NAMESPACE +class QThread; + +class ThreadWorkerLE : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE void discover(); +signals: + void discoveryCompleted(const QVariant res); +}; + +class ThreadWorkerClassic : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE void discover(QVariant hSearch); +signals: + void discoveryCompleted(const QVariant res); +}; +QT_END_NAMESPACE + #elif defined(QT_WINRT_BLUETOOTH) #include #include @@ -177,9 +198,9 @@ private: void finishDiscovery(QBluetoothDeviceDiscoveryAgent::Error errorCode, const QString &errorText); void startLeDevicesDiscovery(); - void completeLeDevicesDiscovery(); + void completeLeDevicesDiscovery(const QVariant res); void startClassicDevicesDiscovery(Qt::HANDLE hSearch = nullptr); - void completeClassicDevicesDiscovery(); + void completeClassicDevicesDiscovery(const QVariant res); void processDiscoveredDevice(const QBluetoothDeviceInfo &foundDevice); @@ -188,8 +209,10 @@ private: bool pendingStart; bool active; - QFutureWatcher *classicScanWatcher; - QFutureWatcher *lowenergyScanWatcher; + QThread *threadLE = nullptr; + QThread *threadClassic = nullptr; + ThreadWorkerLE *threadWorkerLE = nullptr; + ThreadWorkerClassic *threadWorkerClassic = nullptr; #endif #ifdef QT_WINRT_BLUETOOTH diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp index a7755696..3d1b0c4b 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp @@ -44,7 +44,8 @@ #include "qbluetoothlocaldevice_p.h" #include -#include +#include +#include #include #include @@ -332,17 +333,34 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( , pendingCancel(false) , pendingStart(false) , active(false) - , classicScanWatcher(nullptr) - , lowenergyScanWatcher(nullptr) , lowEnergySearchTimeout(-1) // remains -1 -> timeout not supported , q_ptr(parent) { + threadLE = new QThread; + threadWorkerLE = new ThreadWorkerLE; + threadWorkerLE->moveToThread(threadLE); + connect(threadWorkerLE, &ThreadWorkerLE::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery); + connect(threadLE, &QThread::finished, threadWorkerLE, &ThreadWorkerLE::deleteLater); + connect(threadLE, &QThread::finished, threadLE, &QThread::deleteLater); + threadLE->start(); + + threadClassic = new QThread; + threadWorkerClassic = new ThreadWorkerClassic; + threadWorkerClassic->moveToThread(threadClassic); + connect(threadWorkerClassic, &ThreadWorkerClassic::discoveryCompleted, this, &QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery); + connect(threadClassic, &QThread::finished, threadWorkerClassic, &ThreadWorkerClassic::deleteLater); + connect(threadClassic, &QThread::finished, threadClassic, &QThread::deleteLater); + threadClassic->start(); } QBluetoothDeviceDiscoveryAgentPrivate::~QBluetoothDeviceDiscoveryAgentPrivate() { if (active) stop(); + if (threadLE) + threadLE->quit(); + if (threadClassic) + threadClassic->quit(); } bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const @@ -447,23 +465,17 @@ void QBluetoothDeviceDiscoveryAgentPrivate::finishDiscovery(QBluetoothDeviceDisc void QBluetoothDeviceDiscoveryAgentPrivate::startLeDevicesDiscovery() { - if (!lowenergyScanWatcher) { - lowenergyScanWatcher = new QFutureWatcher(this); - connect(lowenergyScanWatcher, &QFutureWatcher::finished, - this, &QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery); - } - const QFuture future = QtConcurrent::run(discoverLeDevicesStatic); - lowenergyScanWatcher->setFuture(future); + QMetaObject::invokeMethod(threadWorkerLE, "discover", Qt::QueuedConnection); } -void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery() +void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery(const QVariant res) { if (pendingCancel && !pendingStart) { cancelDiscovery(); } else if (pendingStart) { restartDiscovery(); } else { - const DiscoveryResult result = lowenergyScanWatcher->result().value(); + const DiscoveryResult result = res.value(); if (result.systemErrorCode == NO_ERROR || result.systemErrorCode == ERROR_NO_MORE_ITEMS) { for (const QBluetoothDeviceInfo &device : result.devices) processDiscoveredDevice(device); @@ -484,18 +496,13 @@ void QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery() void QBluetoothDeviceDiscoveryAgentPrivate::startClassicDevicesDiscovery(Qt::HANDLE hSearch) { - if (!classicScanWatcher) { - classicScanWatcher = new QFutureWatcher(this); - connect(classicScanWatcher, &QFutureWatcher::finished, - this, &QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery); - } - const QFuture future = QtConcurrent::run(discoverClassicDevicesStatic, hSearch); - classicScanWatcher->setFuture(future); + QMetaObject::invokeMethod(threadWorkerClassic, "discover", Qt::QueuedConnection, + Q_ARG(QVariant, QVariant::fromValue(hSearch))); } -void QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery() +void QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery(const QVariant res) { - const DiscoveryResult result = classicScanWatcher->result().value(); + const DiscoveryResult result = res.value(); if (pendingCancel && !pendingStart) { closeClassicSearch(result.hSearch); cancelDiscovery(); @@ -560,6 +567,20 @@ void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevice( } } + +void ThreadWorkerLE::discover() +{ + const QVariant res = discoverLeDevicesStatic(); + emit discoveryCompleted(res); +} + +void ThreadWorkerClassic::discover(QVariant search) +{ + Qt::HANDLE hSearch = search.value(); + const QVariant res = discoverClassicDevicesStatic(hSearch); + emit discoveryCompleted(res); +} + QT_END_NAMESPACE Q_DECLARE_METATYPE(DiscoveryResult) diff --git a/src/bluetooth/qlowenergycontroller_win.cpp b/src/bluetooth/qlowenergycontroller_win.cpp index 9b2ab6a2..c6ea5c78 100644 --- a/src/bluetooth/qlowenergycontroller_win.cpp +++ b/src/bluetooth/qlowenergycontroller_win.cpp @@ -46,6 +46,8 @@ #include #include #include +#include +#include #include // for std::max -- cgit v1.2.3