summaryrefslogtreecommitdiffstats
path: root/core/qt-ble.cpp
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2018-06-20 15:03:57 +0900
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-06-20 16:38:04 +0900
commit21d6531e457a3c405725a88c42f525425d0831bf (patch)
tree253ea388a759cf18529651bcdbf52ce890dd6583 /core/qt-ble.cpp
parentb75eae95c194452499ba73e606c0bc0b41d3fb32 (diff)
downloadsubsurface-21d6531e457a3c405725a88c42f525425d0831bf.tar.gz
qt-ble: improve responsiveness of waiting for bluetooth data
Our model of waiting for 100ms before re-checking if we got a packet over BLE resulted in potentially horrendously bad latency for received packets. That isn't just a possible performance issue, it actually seems to cause IO errors with my Suunto EON Core. I'm not entirely sure why, but it might simply be some timing interaction, particularly since the IO errors seemed to primarily happen when the dive computer itself was also busy updating the screen (ie if you pressed buttons on the dive computer to switch to compass mode, for example). So replace the silly hardcoded 100ms "waitFor()" function with a WAITFOR() macro that checks the provided expression every time through the loop, which gets us a much lower latency (we basically check every ten milliseconds). The macro is not beautiful, but it WorksForMe(tm). This makes a huge difference to the reliability of the download for me, and might matter for some other dive computers too. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'core/qt-ble.cpp')
-rw-r--r--core/qt-ble.cpp43
1 files changed, 23 insertions, 20 deletions
diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp
index 730d7148e..7556b01f8 100644
--- a/core/qt-ble.cpp
+++ b/core/qt-ble.cpp
@@ -29,17 +29,26 @@ static int debugCounter;
#define MAXIMAL_HW_CREDIT 255
#define MINIMAL_HW_CREDIT 32
-static void waitFor(int ms) {
- Q_ASSERT(QCoreApplication::instance());
- Q_ASSERT(QThread::currentThread());
-
- QElapsedTimer timer;
- timer.start();
-
- do {
- QCoreApplication::processEvents(QEventLoop::AllEvents, ms);
- QThread::msleep(10);
- } while (timer.elapsed() < ms);
+#define WAITFOR(expression, ms) do { \
+ Q_ASSERT(QCoreApplication::instance()); \
+ Q_ASSERT(QThread::currentThread()); \
+ \
+ if (expression) \
+ break; \
+ QElapsedTimer timer; \
+ timer.start(); \
+ \
+ do { \
+ QCoreApplication::processEvents(QEventLoop::AllEvents, ms); \
+ if (expression) \
+ break; \
+ QThread::msleep(10); \
+ } while (timer.elapsed() < (ms)); \
+} while (0)
+
+static void waitFor(int ms)
+{
+ WAITFOR(false, ms);
}
extern "C" {
@@ -176,17 +185,11 @@ dc_status_t BLEObject::read(void *data, size_t size, size_t *actual)
if (list.isEmpty())
return DC_STATUS_IO;
- int msec = BLE_TIMEOUT;
- while (msec > 0 && receivedPackets.isEmpty()) {
- waitFor(100);
- msec -= 100;
- }
+ WAITFOR(!receivedPackets.isEmpty(), BLE_TIMEOUT);
+ if (receivedPackets.isEmpty())
+ return DC_STATUS_IO;
}
- // Still no packet?
- if (receivedPackets.isEmpty())
- return DC_STATUS_IO;
-
QByteArray packet = receivedPackets.takeFirst();
if ((size_t)packet.size() > size)