mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
CMF Watch Pro: Reassemble chunks larger than mtu
This commit is contained in:
+19
-1
@@ -59,6 +59,7 @@ public class CmfCharacteristic {
|
||||
private int mtu = 247;
|
||||
|
||||
private final Map<CmfCommand, ChunkBuffer> chunkBuffers = new HashMap<>();
|
||||
private final ByteArrayOutputStream packetBuffer = new ByteArrayOutputStream();
|
||||
|
||||
public CmfCharacteristic(final BluetoothGattCharacteristic bluetoothGattCharacteristic,
|
||||
final Handler handler) {
|
||||
@@ -197,15 +198,30 @@ public class CmfCharacteristic {
|
||||
}
|
||||
|
||||
public void onCharacteristicChanged(final byte[] value) {
|
||||
final ByteBuffer buf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN);
|
||||
final ByteBuffer buf;
|
||||
if (packetBuffer.size() > 0) {
|
||||
// We're already in the middle of a packet
|
||||
packetBuffer.write(value, 0, value.length);
|
||||
buf = ByteBuffer.wrap(packetBuffer.toByteArray()).order(ByteOrder.BIG_ENDIAN);
|
||||
} else {
|
||||
buf = ByteBuffer.wrap(value).order(ByteOrder.BIG_ENDIAN);
|
||||
}
|
||||
|
||||
final byte header = buf.get();
|
||||
if (header != PAYLOAD_HEADER) {
|
||||
LOG.error("Unexpected first byte {}", String.format("0x%02x", header));
|
||||
packetBuffer.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
final int payloadLength = buf.getShort();
|
||||
if (payloadLength > buf.remaining()) {
|
||||
LOG.debug("Got partial payload - waiting for more bytes");
|
||||
// First chunk
|
||||
packetBuffer.write(value, 0, value.length);
|
||||
return;
|
||||
}
|
||||
|
||||
final int cmd1 = buf.getShort() & 0xFFFF;
|
||||
final int chunkCount = buf.getShort();
|
||||
final int chunkIndex = buf.getShort();
|
||||
@@ -247,6 +263,8 @@ public class CmfCharacteristic {
|
||||
chunkBuffers.remove(cmd);
|
||||
}
|
||||
return;
|
||||
} finally {
|
||||
packetBuffer.reset();
|
||||
}
|
||||
} else {
|
||||
payload = new byte[0];
|
||||
|
||||
Reference in New Issue
Block a user