summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLubomir I. Ivanov (VMware) <neolit123@gmail.com>2018-05-21 17:16:18 +0200
committerLubomir I. Ivanov <neolit123@gmail.com>2018-05-29 07:57:50 +0000
commit515c20f5f20dfb4e665e635a30485d7931ff12df (patch)
tree69d493821900db9e18055daa0302d7e5a403cdab /src
parent4ca1d6a02cbcc5347205b231108b6bc2b351a4c7 (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')
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent_p.h33
-rw-r--r--src/bluetooth/qbluetoothdevicediscoveryagent_win.cpp63
-rw-r--r--src/bluetooth/qlowenergycontroller_win.cpp2
3 files changed, 72 insertions, 26 deletions
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 <QtConcurrent>
+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 <QtCore/QPointer>
#include <QtCore/QTimer>
@@ -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<QVariant> *classicScanWatcher;
- QFutureWatcher<QVariant> *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 <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)
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 <QtCore/QEvent>
#include <QtCore/QMutex>
#include <QtCore/QThread>
+#include <QtCore/QDataStream>
+#include <QtCore/QCoreApplication>
#include <algorithm> // for std::max