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 (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;