From 14c6ce1a3a479376543c1d7bcaa08999bba216e0 Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Sun, 7 Apr 2024 19:50:18 +0200 Subject: [PATCH] Garmin protocol: store max packet size from DeviceInformationMessage also adds messageType to the warnifleftover log message --- .../garmin/messages/DeviceInformationMessage.java | 1 + .../devices/garmin/messages/GFDIMessage.java | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java index 684694ae3..95c74437a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/DeviceInformationMessage.java @@ -35,6 +35,7 @@ public class DeviceInformationMessage extends GFDIMessage { this.deviceName = deviceName; this.deviceModel = deviceModel; + GFDIMessage.setMaxPacketSize(maxPacketSize); this.statusMessage = getStatusMessage(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java index 0ca3ef861..1047c6b8e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/GFDIMessage.java @@ -32,10 +32,19 @@ public abstract class GFDIMessage { public static final int MESSAGE_SYNC_REQUEST = 5037; public static final int MESSAGE_AUTH_NEGOTIATION = 5101; protected static final Logger LOG = LoggerFactory.getLogger(GFDIMessage.class); + private static int maxPacketSize = 375; //safe default? protected final ByteBuffer response = ByteBuffer.allocate(1000); protected GFDIStatusMessage statusMessage; protected GarminMessage garminMessage; + public static int getMaxPacketSize() { + return maxPacketSize; + } + + public static void setMaxPacketSize(int maxPacketSize) { + GFDIMessage.maxPacketSize = maxPacketSize; + } + public static GFDIMessage parseIncoming(byte[] message) { final MessageReader messageReader = new MessageReader(message); @@ -48,7 +57,7 @@ public abstract class GFDIMessage { LOG.error("UNHANDLED GFDI MESSAGE TYPE {}, MESSAGE {}", messageType, message); return new UnhandledMessage(messageType); } finally { - messageReader.warnIfLeftover(); + messageReader.warnIfLeftover(messageType); } } @@ -194,14 +203,14 @@ public abstract class GFDIMessage { } } - public void warnIfLeftover() { + public void warnIfLeftover(int messageType) { if (byteBuffer.hasRemaining() && byteBuffer.position() < (byteBuffer.limit())) { int pos = byteBuffer.position(); int numBytes = (byteBuffer.limit()) - byteBuffer.position(); byte[] leftover = new byte[numBytes]; byteBuffer.get(leftover); byteBuffer.position(pos); - LOG.warn("Leftover bytes when parsing message. Bytes: {}, complete message: {}", GB.hexdump(leftover), GB.hexdump(byteBuffer.array())); + LOG.warn("Leftover bytes when parsing message type {}. Bytes: {}, complete message: {}", messageType, GB.hexdump(leftover), GB.hexdump(byteBuffer.array())); } } }