summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorGravatar Alex Blasche <alexander.blasche@qt.io>2017-06-27 15:58:36 +0200
committerGravatar Linus Torvalds <torvalds@linux-foundation.org>2017-06-27 11:03:19 -0700
commite79bede0aa5b3bd169a29dbb907002d6ac717c6e (patch)
tree90d9c1dde12b1994c615d3687284e0b724108ef1 /core
parent57753321b069c93050a40eaddef9a49f1a2c9e19 (diff)
downloadsubsurface-e79bede0aa5b3bd169a29dbb907002d6ac717c6e.tar.gz
Use QLowEnergyController without QEventLoop
We rather use wait in combination with spinning the event loop. Signed-off-by: Alex Blasche <alexander.blasche@qt.io> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'core')
-rw-r--r--core/qt-ble.cpp62
1 files changed, 42 insertions, 20 deletions
diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp
index 3b165aa94..85dd0b1d1 100644
--- a/core/qt-ble.cpp
+++ b/core/qt-ble.cpp
@@ -3,7 +3,10 @@
#include <QtBluetooth/QBluetoothAddress>
#include <QLowEnergyController>
+#include <QCoreApplication>
+#include <QElapsedTimer>
#include <QEventLoop>
+#include <QThread>
#include <QTimer>
#include <QDebug>
@@ -18,6 +21,20 @@
extern "C" {
+void waitFor(int ms) {
+ Q_ASSERT(QCoreApplication::instance());
+ Q_ASSERT(QThread::currentThread());
+
+ QElapsedTimer timer;
+ timer.start();
+
+ do {
+ QCoreApplication::processEvents(QEventLoop::AllEvents, ms);
+ QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
+ QThread::msleep(10);
+ } while (timer.elapsed() < ms);
+}
+
void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState s)
{
QList<QLowEnergyCharacteristic> list;
@@ -144,21 +161,15 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
qDebug() << "qt_ble_open(" << devaddr << ")";
- // Wait until the connection succeeds or until an error occurs
- QEventLoop loop;
- loop.connect(controller, SIGNAL(connected()), SLOT(quit()));
- loop.connect(controller, SIGNAL(error(QLowEnergyController::Error)), SLOT(quit()));
+ // Try to connect to the device
+ controller->connectToDevice();
// Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step
- QTimer timer;
int msec = 5000;
- timer.setSingleShot(true);
- loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
-
- // Try to connect to the device
- controller->connectToDevice();
- timer.start(msec);
- loop.exec();
+ while (msec > 0 && controller->state() == QLowEnergyController::ConnectingState) {
+ waitFor(100);
+ msec -= 100;
+ };
switch (controller->state()) {
case QLowEnergyController::ConnectedState:
@@ -174,24 +185,35 @@ dc_status_t qt_ble_open(dc_custom_io_t *io, dc_context_t *context, const char *d
/* We need to discover services etc here! */
BLEObject *ble = new BLEObject(controller);
- loop.connect(controller, SIGNAL(discoveryFinished()), SLOT(quit()));
ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid)));
qDebug() << " .. discovering services";
controller->discoverServices();
- timer.start(msec);
- loop.exec();
+
+ msec = 5000;
+ while (msec > 0 && controller->state() == QLowEnergyController::DiscoveringState) {
+ waitFor(100);
+ msec -= 100;
+ };
qDebug() << " .. done discovering services";
+ if (ble->preferredService() == nullptr) {
+ qDebug() << "failed to find suitable service on" << devaddr;
+ report_error("Failed to find suitable service on '%s'", devaddr);
+ controller->disconnectFromDevice();
+ delete controller;
+ return DC_STATUS_IO;
+ }
qDebug() << " .. discovering details";
+ msec = 5000;
+ while (msec > 0 && ble->preferredService()->state() == QLowEnergyService::DiscoveringServices) {
+ waitFor(100);
+ msec -= 100;
+ };
- timer.start(msec);
- loop.exec();
-
- qDebug() << " .. done waiting";
- if (ble->preferredService() == nullptr) {
+ if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) {
qDebug() << "failed to find suitable service on" << devaddr;
report_error("Failed to find suitable service on '%s'", devaddr);
controller->disconnectFromDevice();