From edd1a660dac902338c12942ce3030eee1adc5f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sun, 15 Sep 2024 18:35:31 +0100 Subject: [PATCH] Xiaomi: Fix gps track parsing The crc at the end was not being taken into account to check the file size. --- .../devices/xiaomi/activity/impl/DailyDetailsParser.java | 3 ++- .../devices/xiaomi/activity/impl/ManualSamplesParser.java | 3 ++- .../devices/xiaomi/activity/impl/WorkoutGpsParser.java | 5 ++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/DailyDetailsParser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/DailyDetailsParser.java index 0f7e55bab..537b4b527 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/DailyDetailsParser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/DailyDetailsParser.java @@ -63,6 +63,7 @@ public class DailyDetailsParser extends XiaomiActivityParser { } final ByteBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); + buf.limit(buf.limit() - 4); // discard crc at the end buf.get(new byte[7]); // skip fileId bytes final byte fileIdPadding = buf.get(); if (fileIdPadding != 0) { @@ -80,7 +81,7 @@ public class DailyDetailsParser extends XiaomiActivityParser { timestamp.setTime(fileId.getTimestamp()); final List samples = new ArrayList<>(); - while (buf.position() < buf.limit() - 4 /* crc at the end */) { + while (buf.position() < buf.limit()) { complexParser.reset(); final XiaomiActivitySample sample = new XiaomiActivitySample(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/ManualSamplesParser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/ManualSamplesParser.java index 56bcd125b..2a34f9c46 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/ManualSamplesParser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/ManualSamplesParser.java @@ -51,6 +51,7 @@ public class ManualSamplesParser extends XiaomiActivityParser { } final ByteBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); + buf.limit(buf.limit() - 4); // discard crc at the end buf.get(new byte[7]); // skip fileId bytes final byte fileIdPadding = buf.get(); if (fileIdPadding != 0) { @@ -67,7 +68,7 @@ public class ManualSamplesParser extends XiaomiActivityParser { final List samples = new ArrayList<>(); - while (buf.position() < buf.limit() - 4 /* crc at the end */) { + while (buf.position() < buf.limit()) { final int timestamp = buf.getInt(); final int type = buf.get() & 0xff; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/WorkoutGpsParser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/WorkoutGpsParser.java index 5e0efdce9..08162b2e9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/WorkoutGpsParser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/xiaomi/activity/impl/WorkoutGpsParser.java @@ -68,12 +68,12 @@ public class WorkoutGpsParser extends XiaomiActivityParser { } final ByteBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); + buf.limit(buf.limit() - 4); // discard crc at the end buf.get(new byte[7]); // skip fileId bytes final byte fileIdPadding = buf.get(); if (fileIdPadding != 0) { LOG.warn("Expected 0 padding after fileId, got {} - parsing might fail", fileIdPadding); } - final byte[] header = new byte[headerSize]; buf.get(header); @@ -81,12 +81,11 @@ public class WorkoutGpsParser extends XiaomiActivityParser { if ((buf.limit() - buf.position()) % sampleSize != 0) { LOG.warn("Remaining data in the buffer is not a multiple of {}", sampleSize); - return false; } final ActivityTrack activityTrack = new ActivityTrack(); - while (buf.position() < buf.limit() - 4 /* crc at the end */) { + while (buf.position() < buf.limit()) { final int ts = buf.getInt(); final float longitude = buf.getFloat(); final float latitude = buf.getFloat();