From 709c1df2af4b871c5f38c2eb432ea00a8c449922 Mon Sep 17 00:00:00 2001 From: Jan Mulder Date: Tue, 4 Jul 2017 09:03:30 +0200 Subject: BLE: read until no more data in coming in The current BLE read reads just one 20 bype packet. That packet size is set in ble_serial_ops, so, without being able to test on anything other than a OSTC3, I assume that this holds for other BLE DCs too. So, I think is is weird that those interfaces work with the current read() of just one packet at the time. As we need a blocking read (at least for the OSTC parser), just read all data that is available on the input. And when we think we are done, give the QtEventloop control to see if there is more, and process that incoming data as well. All this basically implements a blocking read. CAVEAT 1: This might break the reading from the currently working BLE devices. CAVEAT 2: With this, I still cannot read the OSTC3 completely. For developers familiar with the HW transfer protocol: it just stops while reading the first full dive (header + profile) command 0x66, despite correctly reading about 5Kb of data before. For some reason, I do not believe that this is related to this commit. CAVEAT 3: All above tested on Linux Desktop with bluez stack, and confirmed NOT to work on Android 7.1.2, build with Qt 5.9.0, And yes, I know 5.9.1 recommended. Signed-off-by: Jan Mulder --- core/qt-ble.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/core/qt-ble.cpp b/core/qt-ble.cpp index 5d4e35d54..3f53eeca5 100644 --- a/core/qt-ble.cpp +++ b/core/qt-ble.cpp @@ -146,6 +146,10 @@ dc_status_t BLEObject::write(const void *data, size_t size, size_t *actual) { Q_UNUSED(actual) // that seems like it might cause problems + if (!receivedPackets.isEmpty()) { + qDebug() << ".. write HIT with still incoming packets in queue"; + } + QList list = preferredService()->characteristics(); QByteArray bytes((const char *)data, (int) size); @@ -169,6 +173,7 @@ dc_status_t BLEObject::write(const void *data, size_t size, size_t *actual) dc_status_t BLEObject::read(void *data, size_t size, size_t *actual) { + *actual = 0; if (receivedPackets.isEmpty()) { QList list = preferredService()->characteristics(); if (list.isEmpty()) @@ -185,15 +190,27 @@ dc_status_t BLEObject::read(void *data, size_t size, size_t *actual) if (receivedPackets.isEmpty()) return DC_STATUS_IO; - QByteArray packet = receivedPackets.takeFirst(); + int offset = 0; + while (!receivedPackets.isEmpty()) { + while (!receivedPackets.isEmpty()) { + QByteArray packet = receivedPackets.takeFirst(); - if (device_is_shearwater(device)) - packet.remove(0,2); + if (device_is_shearwater(device)) + packet.remove(0,2); - if (size > (size_t)packet.size()) - size = packet.size(); - memcpy(data, packet.data(), size); - *actual = size; + //qDebug() << ".. read (packet.length, contents, size)" << packet.size() << packet.toHex() << size; + + if ((offset + packet.size()) > size) { + qDebug() << "BLE read trouble, receive buffer too small"; + return DC_STATUS_NOMEMORY; + } + + memcpy((char *)data + offset, packet.data(), packet.size()); + offset += packet.size(); + *actual += packet.size(); + } + waitFor(50); // and process some Qt events to see if there is more data coming in. + } return DC_STATUS_SUCCESS; } -- cgit v1.2.3-70-g09d2