From 2b34bc4162b279288e4a15990890df5b642bd831 Mon Sep 17 00:00:00 2001 From: Janez T Date: Thu, 5 Mar 2026 20:12:09 +0100 Subject: [PATCH] fix: stop lpp zero tail ref: --- lib/services/cayenne_lpp_parser.dart | 16 ++++++++++++++++ test/services/cayenne_lpp_parser_test.dart | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/services/cayenne_lpp_parser.dart b/lib/services/cayenne_lpp_parser.dart index 60379d2..0d69fc9 100644 --- a/lib/services/cayenne_lpp_parser.dart +++ b/lib/services/cayenne_lpp_parser.dart @@ -25,6 +25,14 @@ class CayenneLppParser { int fieldCount = 0; while (reader.hasRemaining) { + if (fieldCount > 0 && _isZeroPaddedTail(data, reader.remainingBytesCount)) { + debugPrint( + ' Detected zero-padded telemetry tail, stopping parse at position ' + '${data.length - reader.remainingBytesCount}', + ); + break; + } + try { fieldCount++; debugPrint( @@ -250,6 +258,14 @@ class CayenneLppParser { return ((voltage - 3.0) / 1.2) * 100.0; } + static bool _isZeroPaddedTail(Uint8List data, int remainingBytes) { + final start = data.length - remainingBytes; + for (int i = start; i < data.length; i++) { + if (data[i] != 0) return false; + } + return remainingBytes > 0; + } + /// Create Cayenne LPP data for GPS location /// Standard Cayenne LPP GPS format (type 0x88): /// - Latitude: 3 bytes, signed 24-bit, big-endian, × 10000 diff --git a/test/services/cayenne_lpp_parser_test.dart b/test/services/cayenne_lpp_parser_test.dart index 2ee41bc..1a45ae2 100644 --- a/test/services/cayenne_lpp_parser_test.dart +++ b/test/services/cayenne_lpp_parser_test.dart @@ -410,6 +410,24 @@ void main() { expect(decoded.batteryMilliVolts, closeTo(3850, 1)); }); + test('stops parsing at zero-padded telemetry tail', () { + final payload = Uint8List.fromList([ + 0x01, 0x74, 0x01, 0x5F, // voltage 3.51V + 0x01, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, + ]); + + final decoded = CayenneLppParser.parse(payload); + + expect(decoded.batteryMilliVolts, closeTo(3510, 1)); + expect(decoded.batteryPercentage, closeTo(42.5, 0.1)); + expect(decoded.gpsLocation, isNotNull); + expect(decoded.gpsLocation!.latitude, 0.0); + expect(decoded.gpsLocation!.longitude, 0.0); + expect(decoded.extraSensorData?['digital_input_0'], isNull); + }); + test('empty data returns empty telemetry', () { final empty = Uint8List(0); final decoded = CayenneLppParser.parse(empty);