Changeset e79bede in subsurface


Ignore:
Timestamp:
Jun 27, 2017, 11:03:19 AM (3 weeks ago)
Author:
Linus Torvalds <torvalds@…>
Branches:
master
Children:
d01b7bf
Parents:
57753321
git-author:
Alex Blasche <alexander.blasche@…> (06/27/17 06:58:36)
git-committer:
Linus Torvalds <torvalds@…> (06/27/17 11:03:19)
Message:

Use QLowEnergyController without QEventLoop

We rather use wait in combination with spinning the event loop.

Signed-off-by: Alex Blasche <alexander.blasche@…>
Signed-off-by: Linus Torvalds <torvalds@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/qt-ble.cpp

    r57753321 re79bede  
    44#include <QtBluetooth/QBluetoothAddress>
    55#include <QLowEnergyController>
     6#include <QCoreApplication>
     7#include <QElapsedTimer>
    68#include <QEventLoop>
     9#include <QThread>
    710#include <QTimer>
    811#include <QDebug>
     
    1821
    1922extern "C" {
     23
     24void waitFor(int ms) {
     25        Q_ASSERT(QCoreApplication::instance());
     26        Q_ASSERT(QThread::currentThread());
     27
     28        QElapsedTimer timer;
     29        timer.start();
     30
     31        do {
     32                QCoreApplication::processEvents(QEventLoop::AllEvents, ms);
     33                QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
     34                QThread::msleep(10);
     35        } while (timer.elapsed() < ms);
     36}
    2037
    2138void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState s)
     
    145162        qDebug() << "qt_ble_open(" << devaddr << ")";
    146163
    147         // Wait until the connection succeeds or until an error occurs
    148         QEventLoop loop;
    149         loop.connect(controller, SIGNAL(connected()), SLOT(quit()));
    150         loop.connect(controller, SIGNAL(error(QLowEnergyController::Error)), SLOT(quit()));
    151 
    152         // Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step
    153         QTimer timer;
    154         int msec = 5000;
    155         timer.setSingleShot(true);
    156         loop.connect(&timer, SIGNAL(timeout()), SLOT(quit()));
    157 
    158164        // Try to connect to the device
    159165        controller->connectToDevice();
    160         timer.start(msec);
    161         loop.exec();
     166
     167        // Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step
     168        int msec = 5000;
     169        while (msec > 0 && controller->state() == QLowEnergyController::ConnectingState) {
     170                waitFor(100);
     171                msec -= 100;
     172        };
    162173
    163174        switch (controller->state()) {
     
    175186        /* We need to discover services etc here! */
    176187        BLEObject *ble = new BLEObject(controller);
    177         loop.connect(controller, SIGNAL(discoveryFinished()), SLOT(quit()));
    178188        ble->connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), SLOT(addService(QBluetoothUuid)));
    179189
     
    181191
    182192        controller->discoverServices();
    183         timer.start(msec);
    184         loop.exec();
     193
     194        msec = 5000;
     195        while (msec > 0 && controller->state() == QLowEnergyController::DiscoveringState) {
     196                waitFor(100);
     197                msec -= 100;
     198        };
    185199
    186200        qDebug() << " .. done discovering services";
    187 
    188         qDebug() << " .. discovering details";
    189 
    190         timer.start(msec);
    191         loop.exec();
    192 
    193         qDebug() << " .. done waiting";
    194201        if (ble->preferredService() == nullptr) {
    195202                qDebug() << "failed to find suitable service on" << devaddr;
     
    200207        }
    201208
     209        qDebug() << " .. discovering details";
     210        msec = 5000;
     211        while (msec > 0 && ble->preferredService()->state() == QLowEnergyService::DiscoveringServices) {
     212                waitFor(100);
     213                msec -= 100;
     214        };
     215
     216        if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) {
     217                qDebug() << "failed to find suitable service on" << devaddr;
     218                report_error("Failed to find suitable service on '%s'", devaddr);
     219                controller->disconnectFromDevice();
     220                delete controller;
     221                return DC_STATUS_IO;
     222        }
     223
    202224
    203225        qDebug() << " .. enabling notifications";
Note: See TracChangeset for help on using the changeset viewer.