diff --git a/lib/services/telemetry_location_request.dart b/lib/services/telemetry_location_request.dart index dec338e2..53b03923 100644 --- a/lib/services/telemetry_location_request.dart +++ b/lib/services/telemetry_location_request.dart @@ -44,7 +44,9 @@ Future requestTelemetryLocation( Contact contact, ) async { final completer = Completer>?>(); - var awaitingSent = true; + // Armed just before our send: a background retry's RESP_CODE_SENT that + // lands while the path is being prepared must not be taken for ours. + var awaitingSent = false; int? tag; Timer? timeout; void finish(List>? parsed) { @@ -53,8 +55,6 @@ Future requestTelemetryLocation( completer.complete(parsed); } - // Guards the wait for the radio's send ack; the ack's trip time replaces it. - timeout = Timer(const Duration(seconds: 10), () => finish(null)); final isChat = contact.type == advTypeChat; final subscription = connector.receivedFrames.listen((frame) { if (frame.isEmpty || completer.isCompleted) return; @@ -92,6 +92,9 @@ Future requestTelemetryLocation( PathSelection? selection; try { selection = await connector.preparePathForContactSend(contact); + awaitingSent = true; + // Guards the wait for the radio's send ack; its trip time replaces this. + timeout = Timer(const Duration(seconds: 10), () => finish(null)); await connector.sendFrame( isChat ? buildSendTelemetryReq(contact.publicKey) diff --git a/test/connector/telemetry_location_test.dart b/test/connector/telemetry_location_test.dart index 2db93a0d..098ba731 100644 --- a/test/connector/telemetry_location_test.dart +++ b/test/connector/telemetry_location_test.dart @@ -3,6 +3,9 @@ import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; import 'package:meshcore_open/connector/meshcore_connector.dart'; import 'package:meshcore_open/connector/meshcore_protocol.dart'; +import 'package:meshcore_open/helpers/cayenne_lpp.dart'; +import 'package:meshcore_open/models/contact.dart'; +import 'package:meshcore_open/models/path_selection.dart'; import 'package:meshcore_open/services/telemetry_location_request.dart'; import 'package:meshcore_open/storage/prefs_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -12,12 +15,13 @@ Uint8List _contactFrame( required int advertTimestamp, required double lat, required double lon, + int type = advTypeChat, }) { final data = ByteData(1 + 32 + 3 + 64 + 32 + 4 + 12); final bytes = data.buffer.asUint8List(); bytes[0] = respCodeContact; bytes.setRange(1, 33, key); - bytes[33] = advTypeChat; + bytes[33] = type; bytes[35] = 0xFF; // path unknown bytes.setRange(100, 103, 'Bob'.codeUnits); data.setUint32(132, advertTimestamp, Endian.little); @@ -26,6 +30,58 @@ Uint8List _contactFrame( return bytes; } +Uint8List _sentFrame(int tag) { + final data = ByteData(10); + data.setUint8(0, respCodeSent); + data.setUint32(2, tag, Endian.little); + data.setUint32(6, 5000, Endian.little); // est. timeout ms + return data.buffer.asUint8List(); +} + +/// A sensor's binary telemetry reply carrying a Cayenne LPP GPS fix. +Uint8List _gpsResponse(int tag, double lat, double lon) { + int24(double v) { + final n = v.round() & 0xFFFFFF; + return [(n >> 16) & 0xFF, (n >> 8) & 0xFF, n & 0xFF]; + } + + final tagBytes = ByteData(4)..setUint32(0, tag, Endian.little); + return Uint8List.fromList([ + pushCodeBinaryResponse, + 0, + ...tagBytes.buffer.asUint8List(), + 1, + CayenneLpp.lppGps, + ...int24(lat * 10000), + ...int24(lon * 10000), + ...int24(0), + ]); +} + +/// Replays radio traffic around a telemetry request: an unrelated send ack +/// arrives while the path is prepared, then the request's own ack and reply. +class _RadioConnector extends MeshCoreConnector { + @override + bool get isConnected => true; + + @override + Future preparePathForContactSend(Contact contact) async { + handleFrameForTest(_sentFrame(111)); // a background retry's ack + return const PathSelection(pathBytes: [], hopCount: -1, useFlood: true); + } + + @override + Future sendFrame( + Uint8List data, { + String? channelSendQueueId, + bool expectsGenericAck = false, + bool waitForGenericAck = false, + }) async { + handleFrameForTest(_sentFrame(222)); + handleFrameForTest(_gpsResponse(222, 45.5, -122.6)); + } +} + void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -108,6 +164,26 @@ void main() { expect(contact.telemetryFixAt, isNull); }); + test('a send ack during path preparation is not taken as ours', () async { + final connector = _RadioConnector(); + connector.handleFrameForTest( + _contactFrame( + key, + advertTimestamp: 1700000000, + lat: 10, + lon: 20, + type: advTypeSensor, + ), + ); + final sensor = connector.contacts.single; + + final result = await requestTelemetryLocation(connector, sensor); + + expect(result, TelemetryLocationResult.located); + expect(connector.contacts.single.latitude, closeTo(45.5, 1e-4)); + expect(connector.contacts.single.longitude, closeTo(-122.6, 1e-4)); + }); + test('telemetryGpsFix skips missing and null-island fixes', () { expect(telemetryGpsFix([]), isNull); expect(