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 <torvalds@linux-foundation.org>
This commit is contained in:
Linus Torvalds 2018-09-23 11:58:25 -07:00 committed by Dirk Hohndel
parent 35b8a4f404
commit 594f13eeaf

View file

@ -47,11 +47,6 @@ static int debugCounter;
} while (timer.elapsed() < (ms)); \ } while (timer.elapsed() < (ms)); \
} while (0) } while (0)
static void waitFor(int ms)
{
WAITFOR(false, ms);
}
extern "C" { extern "C" {
void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState) void BLEObject::serviceStateChanged(QLowEnergyService::ServiceState)
@ -226,11 +221,8 @@ dc_status_t BLEObject::setHwCredit(unsigned int c)
QLowEnergyService::WriteWithResponse); QLowEnergyService::WriteWithResponse);
/* And wait for the answer*/ /* And wait for the answer*/
int msec = BLE_TIMEOUT; WAITFOR(isCharacteristicWritten, BLE_TIMEOUT);
while (msec > 0 && !isCharacteristicWritten) {
waitFor(100);
msec -= 100;
}
if (!isCharacteristicWritten) if (!isCharacteristicWritten)
return DC_STATUS_TIMEOUT; return DC_STATUS_TIMEOUT;
return DC_STATUS_SUCCESS; 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(); controller->connectToDevice();
// Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step // 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; WAITFOR(controller->state() != QLowEnergyController::ConnectingState, BLE_TIMEOUT);
while (msec > 0 && controller->state() == QLowEnergyController::ConnectingState) {
waitFor(100);
msec -= 100;
}
switch (controller->state()) { switch (controller->state()) {
case QLowEnergyController::ConnectedState: 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(); controller->discoverServices();
msec = BLE_TIMEOUT; WAITFOR(controller->state() != QLowEnergyController::DiscoveringState, BLE_TIMEOUT);
while (msec > 0 && controller->state() == QLowEnergyController::DiscoveringState) {
waitFor(100);
msec -= 100;
}
qDebug() << " .. done discovering services"; qDebug() << " .. done discovering services";
if (ble->preferredService() == nullptr) { 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"; qDebug() << " .. discovering details";
msec = BLE_TIMEOUT; WAITFOR(ble->preferredService()->state() != QLowEnergyService::DiscoveringServices, BLE_TIMEOUT);
while (msec > 0 && ble->preferredService()->state() == QLowEnergyService::DiscoveringServices) {
waitFor(100);
msec -= 100;
}
if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) { if (ble->preferredService()->state() != QLowEnergyService::ServiceDiscovered) {
qDebug() << "failed to find suitable service on" << devaddr; qDebug() << "failed to find suitable service on" << devaddr;