diff options
author | Lubomir I. Ivanov (VMware) <neolit123@gmail.com> | 2018-05-21 17:16:18 +0200 |
---|---|---|
committer | Lubomir I. Ivanov <neolit123@gmail.com> | 2018-05-29 07:57:50 +0000 |
commit | 515c20f5f20dfb4e665e635a30485d7931ff12df (patch) | |
tree | 69d493821900db9e18055daa0302d7e5a403cdab /src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp | |
parent | 4ca1d6a02cbcc5347205b231108b6bc2b351a4c7 (diff) |
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 <denis.shienkov@gmail.com>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Diffstat (limited to 'src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp')
-rw-r--r-- | src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp | 63 |
1 files changed, 42 insertions, 21 deletions
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 <QtCore/qmutex.h> -#include <QtConcurrent/QtConcurrent> +#include <QtCore/QThread> +#include <QtCore/QLoggingCategory> #include <qt_windows.h> #include <setupapi.h> @@ -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<QVariant>(this); - connect(lowenergyScanWatcher, &QFutureWatcher<QVariant>::finished, - this, &QBluetoothDeviceDiscoveryAgentPrivate::completeLeDevicesDiscovery); - } - const QFuture<QVariant> 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<DiscoveryResult>(); + const DiscoveryResult result = res.value<DiscoveryResult>(); 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<QVariant>(this); - connect(classicScanWatcher, &QFutureWatcher<QVariant>::finished, - this, &QBluetoothDeviceDiscoveryAgentPrivate::completeClassicDevicesDiscovery); - } - const QFuture<QVariant> 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<DiscoveryResult>(); + const DiscoveryResult result = res.value<DiscoveryResult>(); 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<Qt::HANDLE>(); + const QVariant res = discoverClassicDevicesStatic(hSearch); + emit discoveryCompleted(res); +} + QT_END_NAMESPACE Q_DECLARE_METATYPE(DiscoveryResult) |