feat(#297): send a stable per-install client_id in CMD_APP_START
Wadamesh keys its history watermark (last_delivered_seq) by client_id. We sent none, so we shared the empty-string slot with every other MeshCore client on the machine: whichever connected first drained the device history ring and the next app got NO_MORE_MESSAGES for frames it never received. cid_len is 6 by necessity, not preference. Stock reads cmd_frame[1..7] as reserved with the app name at a fixed offset 8; Wadamesh reads the name at 2 + cid_len. Only 6 puts the name at 8 on both, so one frame serves both firmwares with no firmware change. Covered by app_start_frame_test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>pull/419/head
parent
f64c27ef6f
commit
4fd5f76a13
@ -0,0 +1,59 @@
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'prefs_manager.dart';
|
||||
|
||||
/// Per-install client identity sent in `CMD_APP_START` (#297).
|
||||
///
|
||||
/// Wadamesh keeps a per-client history watermark (`last_delivered_seq`) keyed by
|
||||
/// this id. Without one we land in the shared empty-string slot alongside every
|
||||
/// other MeshCore client on the machine, so whichever app connects first drains
|
||||
/// the device's history ring and the next app is told "no more messages" for
|
||||
/// frames it never received.
|
||||
///
|
||||
/// The id is exactly [length] bytes so a single frame satisfies both firmwares:
|
||||
/// stock treats `cmd_frame[1..7]` as reserved and reads the app name at a fixed
|
||||
/// offset 8, while Wadamesh reads it at `2 + cid_len`. With `cid_len == 6` both
|
||||
/// land on 8.
|
||||
class ClientIdStore {
|
||||
static const String _key = 'client_install_id';
|
||||
|
||||
/// Byte length of the id. Do not change without re-checking both firmwares —
|
||||
/// 6 is what keeps the app-name offset agreeing at 8.
|
||||
static const int length = 6;
|
||||
|
||||
/// The install's client id, generated and persisted on first use so it is
|
||||
/// stable across builds (all builds share one SharedPreferences store).
|
||||
static Uint8List load() {
|
||||
final prefs = PrefsManager.instance;
|
||||
final stored = prefs.getString(_key);
|
||||
if (stored != null && stored.length == length * 2) {
|
||||
final bytes = Uint8List(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
final byte = int.tryParse(
|
||||
stored.substring(i * 2, i * 2 + 2),
|
||||
radix: 16,
|
||||
);
|
||||
if (byte == null) {
|
||||
return _generate(prefs);
|
||||
}
|
||||
bytes[i] = byte;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
return _generate(prefs);
|
||||
}
|
||||
|
||||
static Uint8List _generate(SharedPreferences prefs) {
|
||||
final random = Random.secure();
|
||||
final bytes = Uint8List(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
bytes[i] = random.nextInt(256);
|
||||
}
|
||||
final hex = bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
|
||||
prefs.setString(_key, hex);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
|
||||
/// The client-id length in CMD_APP_START is load-bearing (#297).
|
||||
///
|
||||
/// Stock firmware treats `cmd_frame[1..7]` as reserved and reads the app name at
|
||||
/// a FIXED offset 8. Wadamesh reads byte 1 as the client-id length and the app
|
||||
/// name at `2 + cid_len`. Only `cid_len == 6` satisfies both. If this test ever
|
||||
/// fails, one of the two firmwares is now reading the app name out of the
|
||||
/// client-id bytes.
|
||||
void main() {
|
||||
group('buildAppStartFrame', () {
|
||||
test('declares a 6-byte client id so the app name lands at offset 8', () {
|
||||
final clientId = Uint8List.fromList([1, 2, 3, 4, 5, 6]);
|
||||
final frame = buildAppStartFrame(appName: 'Offband', clientId: clientId);
|
||||
|
||||
expect(frame[0], cmdAppStart);
|
||||
expect(frame[1], 6, reason: 'cid_len must be 6 for both firmwares');
|
||||
expect(frame.sublist(2, 8), clientId);
|
||||
expect(
|
||||
String.fromCharCodes(frame.sublist(8, 8 + 'Offband'.length)),
|
||||
'Offband',
|
||||
reason: 'app name must start at offset 8',
|
||||
);
|
||||
});
|
||||
|
||||
test('zero-pads a short client id without moving the app name', () {
|
||||
final frame = buildAppStartFrame(
|
||||
appName: 'X',
|
||||
clientId: Uint8List.fromList([0xAA, 0xBB]),
|
||||
);
|
||||
|
||||
expect(frame[1], 6);
|
||||
expect(frame.sublist(2, 8), [0xAA, 0xBB, 0, 0, 0, 0]);
|
||||
expect(String.fromCharCodes(frame.sublist(8, 9)), 'X');
|
||||
});
|
||||
|
||||
test('truncates an over-long client id without moving the app name', () {
|
||||
final frame = buildAppStartFrame(
|
||||
appName: 'X',
|
||||
clientId: Uint8List.fromList([1, 2, 3, 4, 5, 6, 7, 8]),
|
||||
);
|
||||
|
||||
expect(frame[1], 6);
|
||||
expect(frame.sublist(2, 8), [1, 2, 3, 4, 5, 6]);
|
||||
expect(String.fromCharCodes(frame.sublist(8, 9)), 'X');
|
||||
});
|
||||
|
||||
test('omitted client id still keeps the frame shape', () {
|
||||
final frame = buildAppStartFrame(appName: 'X');
|
||||
|
||||
expect(frame[1], 6);
|
||||
expect(frame.sublist(2, 8), [0, 0, 0, 0, 0, 0]);
|
||||
expect(String.fromCharCodes(frame.sublist(8, 9)), 'X');
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in new issue