From 890d4c3d649dd55817d4bb85d62ad433a61cebe4 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Wed, 26 Sep 2018 11:23:57 -0700 Subject: [PATCH] qt-ble: allow reading of partial packet data The existing BLE dive computers treat BLE as the packetized protocol it is, and read whole packets at a time. However, the Mares BlueLink backend treats it as just a basic "serial over BLE" transport, and for historical reasons reads the reply packets in smaller chunks. This allows that kind of IO behavior, where if the divecomputer backend reads just a part of a packet, we'll split the packet, return the part the user asked for, and push back the leftover packet onto the received packet queue. Signed-off-by: Linus Torvalds --- core/qt-ble.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp index 8d4003d0f..ece3bc6cf 100644 --- a/core/qt-ble.cpp +++ b/core/qt-ble.cpp @@ -191,8 +191,15 @@ dc_status_t BLEObject::read(void *data, size_t size, size_t *actual) QByteArray packet = receivedPackets.takeFirst(); - if ((size_t)packet.size() > size) - return DC_STATUS_NOMEMORY; + // Did we get more than asked for? + // + // Put back the left-over at the beginning of the + // received packet list, and truncate the packet + // we got to just the part asked for. + if ((size_t)packet.size() > size) { + receivedPackets.prepend(packet.mid(size)); + packet.truncate(size); + } memcpy((char *)data, packet.data(), packet.size()); if (actual)