From 594f13eeafe52eb070c10daad8322ce2074dd4e5 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sun, 23 Sep 2018 11:58:25 -0700 Subject: [PATCH] qt-ble: use the WAITFOR() macro rather than open-coding wait loops This is not only much clearer (and smaller code), but it also lowers the latency for the waiting, since we don't always wait for the full 100ms. Get rid of the now unused "waitfor()" function that just unconditionally waited for 100ms. Signed-off-by: Linus Torvalds --- core/qt-ble.cpp | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp index b4678aa5c..d84c223c2 100644 --- a/core/qt-ble.cpp +++ b/core/qt-ble.cpp @@ -47,11 +47,6 @@ static int debugCounter; } while (timer.elapsed() < (ms)); \ } while (0) -static void waitFor(int ms) -{ - WAITFOR(false, ms); -} - extern "C" { void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState) @@ -226,11 +221,8 @@ dc_status_t BLEObject::setHwCredit(unsigned int c) QLowEnergyService::WriteWithResponse); /* And wait for the answer*/ - int msec = BLE_TIMEOUT; - while (msec > 0 && !isCharacteristicWritten) { - waitFor(100); - msec -= 100; - } + WAITFOR(isCharacteristicWritten, BLE_TIMEOUT); + if (!isCharacteristicWritten) return DC_STATUS_TIMEOUT; return DC_STATUS_SUCCESS; @@ -318,11 +310,7 @@ dc_status_t qt_ble_open(void **io, dc_context_t *, const char *devaddr, dc_user_ controller->connectToDevice(); // Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step - int msec = BLE_TIMEOUT; - while (msec > 0 && controller->state() == QLowEnergyController::ConnectingState) { - waitFor(100); - msec -= 100; - } + WAITFOR(controller->state() != QLowEnergyController::ConnectingState, BLE_TIMEOUT); switch (controller->state()) { case QLowEnergyController::ConnectedState: @@ -350,11 +338,7 @@ dc_status_t qt_ble_open(void **io, dc_context_t *, const char *devaddr, dc_user_ controller->discoverServices(); - msec = BLE_TIMEOUT; - while (msec > 0 && controller->state() == QLowEnergyController::DiscoveringState) { - waitFor(100); - msec -= 100; - } + WAITFOR(controller->state() != QLowEnergyController::DiscoveringState, BLE_TIMEOUT); qDebug() << " .. done discovering services"; if (ble->preferredService() == nullptr) { @@ -365,11 +349,7 @@ dc_status_t qt_ble_open(void **io, dc_context_t *, const char *devaddr, dc_user_ } qDebug() << " .. discovering details"; - msec = BLE_TIMEOUT; - while (msec > 0 && ble->preferredService()->state() == QLowEnergyService::DiscoveringServices) { - waitFor(100); - msec -= 100; - } + WAITFOR(ble->preferredService()->state() != QLowEnergyService::DiscoveringServices, BLE_TIMEOUT); if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) { qDebug() << "failed to find suitable service on" << devaddr;