diff options
Diffstat (limited to 'core/qtserialbluetooth.cpp')
-rw-r--r-- | core/qtserialbluetooth.cpp | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/core/qtserialbluetooth.cpp b/core/qtserialbluetooth.cpp index 6b47524de..f6bbcafdf 100644 --- a/core/qtserialbluetooth.cpp +++ b/core/qtserialbluetooth.cpp @@ -133,42 +133,39 @@ static dc_status_t qt_serial_read(void *io, void* data, size_t size, size_t *act { qt_serial_t *device = (qt_serial_t*) io; - if (device == NULL || device->socket == NULL) + if (device == NULL || device->socket == NULL || !actual) return DC_STATUS_INVALIDARGS; - size_t nbytes = 0; - int rc; + *actual = 0; + for (;;) { + int rc; - while(nbytes < size && device->socket->state() == QBluetoothSocket::ConnectedState) - { - rc = device->socket->read((char *) data + nbytes, size - nbytes); + if (device->socket->state() != QBluetoothSocket::ConnectedState) + return DC_STATUS_IO; + rc = device->socket->read((char *) data, size); if (rc < 0) { if (errno == EINTR || errno == EAGAIN) - continue; // Retry. - - return DC_STATUS_IO; // Something really bad happened :-( - } else if (rc == 0) { - // Wait until the device is available for read operations - QEventLoop loop; - QTimer timer; - timer.setSingleShot(true); - loop.connect(&timer, SIGNAL(timeout()), SLOT(quit())); - loop.connect(device->socket, SIGNAL(readyRead()), SLOT(quit())); - timer.start(device->timeout); - loop.exec(); - - if (!timer.isActive()) - break; + continue; + return DC_STATUS_IO; } - nbytes += rc; - } - - if (actual) - *actual = nbytes; + *actual = rc; + if (rc > 0 || !size) + return DC_STATUS_SUCCESS; + + // Timeout handling + QEventLoop loop; + QTimer timer; + timer.setSingleShot(true); + loop.connect(&timer, SIGNAL(timeout()), SLOT(quit())); + loop.connect(device->socket, SIGNAL(readyRead()), SLOT(quit())); + timer.start(device->timeout); + loop.exec(); - return DC_STATUS_SUCCESS; + if (!timer.isActive()) + return DC_STATUS_TIMEOUT; + } } static dc_status_t qt_serial_write(void *io, const void* data, size_t size, size_t *actual) |