From ed0b21beae7d778788f05c46aac000fac537696a Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 2 Nov 2022 19:46:42 +0100 Subject: [PATCH] Increase the BLE packet size In the latest G2 firmware v2.0, the size of the BLE packets increased to 101 bytes (with a one byte header and 100 bytes of actual payload). This caused the download to fail, because the internal buffer was suddenly too small for those larger packets. These larger packets are most likely due to an update in the BLE stack of the dive computer. Originally, the maximum BLE packet size was just 20 bytes (excluding the 4 byte L2CAP header and 3 bytes GATT header), but BLE 4.2 increased the maximum packet size to 244 bytes (or 251 bytes with the headers). The USB HID code path keeps using the same fixed size packets as before. --- src/uwatec_smart.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index d5df3db..e7be4fc 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -275,9 +275,13 @@ uwatec_smart_usbhid_send (uwatec_smart_device_t *device, unsigned char cmd, cons { dc_status_t rc = DC_STATUS_SUCCESS; dc_device_t *abstract = (dc_device_t *) device; - unsigned char buf[PACKETSIZE_USBHID_TX + 1]; + dc_transport_t transport = dc_iostream_get_transport(device->iostream); + unsigned char buf[DATASIZE + 3]; - if (size + 3 > sizeof(buf)) { + size_t packetsize = transport == DC_TRANSPORT_USBHID ? + PACKETSIZE_USBHID_TX + 1 : sizeof(buf); + + if (size > DATASIZE || size + 3 > packetsize) { ERROR (abstract->context, "Command too large (" DC_PRINTF_SIZE ").", size); return DC_STATUS_INVALIDARGS; } @@ -295,7 +299,7 @@ uwatec_smart_usbhid_send (uwatec_smart_device_t *device, unsigned char cmd, cons if (dc_iostream_get_transport(device->iostream) == DC_TRANSPORT_BLE) { rc = dc_iostream_write(device->iostream, buf + 1, size + 2, NULL); } else { - rc = dc_iostream_write(device->iostream, buf, sizeof(buf), NULL); + rc = dc_iostream_write(device->iostream, buf, packetsize, NULL); } if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to send the command."); @@ -311,12 +315,15 @@ uwatec_smart_usbhid_receive (uwatec_smart_device_t *device, dc_event_progress_t dc_status_t rc = DC_STATUS_SUCCESS; dc_device_t *abstract = (dc_device_t *) device; dc_transport_t transport = dc_iostream_get_transport(device->iostream); - unsigned char buf[PACKETSIZE_USBHID_RX]; + unsigned char buf[DATASIZE + 1]; + + size_t packetsize = transport == DC_TRANSPORT_USBHID ? + PACKETSIZE_USBHID_RX : sizeof(buf); size_t nbytes = 0; while (nbytes < size) { size_t transferred = 0; - rc = dc_iostream_read (device->iostream, buf, sizeof(buf), &transferred); + rc = dc_iostream_read (device->iostream, buf, packetsize, &transferred); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to receive the packet."); return rc;