Arm the telemetry send ack only after preparing the path

A background retry's RESP_CODE_SENT arriving while the path was being
prepared was taken as the telemetry request's ack, so its tag and timeout
replaced ours and a sensor's tagged reply was rejected.
This commit is contained in:
zjs81 committed 2026-10-25 11:26:15 -07:00
1 parent dfc4cd54d8
commit 6213da118e
2 files changed
+83 -4

No files matched your search

+6 -3
View File
@@ -44,7 +44,9 @@ Future<TelemetryLocationResult> requestTelemetryLocation(
Contact contact,
) async {
final completer = Completer<List<Map<String, dynamic>>?>();
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<Map<String, dynamic>>? parsed) {
@@ -53,8 +55,6 @@ Future<TelemetryLocationResult> 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<TelemetryLocationResult> 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)
+77 -1
View File
@@ -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<PathSelection> preparePathForContactSend(Contact contact) async {
handleFrameForTest(_sentFrame(111)); // a background retry's ack
return const PathSelection(pathBytes: [], hopCount: -1, useFlood: true);
}
@override
Future<void> 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(