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
Strycher 5 months ago
parent f64c27ef6f
commit 4fd5f76a13

@ -47,6 +47,7 @@ import '../storage/channel_message_store.dart';
import '../storage/channel_order_store.dart';
import '../storage/channel_settings_store.dart';
import '../storage/channel_store.dart';
import '../storage/client_id_store.dart';
import '../storage/contact_discovery_store.dart';
import '../storage/contact_settings_store.dart';
import '../storage/contact_store.dart';
@ -3056,7 +3057,7 @@ class MeshCoreConnector extends ChangeNotifier {
_webInitialHandshakeRequestSent = true;
}
await sendFrame(buildDeviceQueryFrame());
await sendFrame(buildAppStartFrame());
await sendFrame(buildAppStartFrame(clientId: ClientIdStore.load()));
await requestBatteryStatus(force: true);
await sendFrame(buildGetCustomVarsFrame());
await sendFrame(buildGetAutoAddFlagsFrame());
@ -3079,7 +3080,7 @@ class MeshCoreConnector extends ChangeNotifier {
_webInitialHandshakeRequestSent = true;
}
await sendFrame(buildDeviceQueryFrame());
await sendFrame(buildAppStartFrame());
await sendFrame(buildAppStartFrame(clientId: ClientIdStore.load()));
await sendFrame(buildGetCustomVarsFrame());
await requestBatteryStatus();
await sendFrame(buildGetAutoAddFlagsFrame());
@ -3122,7 +3123,9 @@ class MeshCoreConnector extends ChangeNotifier {
return;
}
attempts += 1;
unawaited(sendFrame(buildAppStartFrame()));
unawaited(
sendFrame(buildAppStartFrame(clientId: ClientIdStore.load())),
);
if (attempts >= maxAttempts) {
timer.cancel();
}
@ -3155,7 +3158,7 @@ class MeshCoreConnector extends ChangeNotifier {
awaitingSelfInfo: _awaitingSelfInfo,
syncing: syncing,
)) {
unawaited(sendFrame(buildAppStartFrame()));
unawaited(sendFrame(buildAppStartFrame(clientId: ClientIdStore.load())));
if (_appStartRetryAttempt < 5) _appStartRetryAttempt++;
}
_armAppStartRetry();

@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/widgets.dart';
@ -757,16 +758,32 @@ Uint8List buildRemoveContactFrame(Uint8List pubKey) {
return writer.toBytes();
}
/// Byte length of the client id carried in [buildAppStartFrame] (#297).
///
/// 6 is load-bearing, not arbitrary: stock firmware treats `cmd_frame[1..7]` as
/// reserved and reads the app name at a FIXED offset 8, while Wadamesh reads
/// byte 1 as the client-id length and the app name at `2 + cid_len`. Only
/// `cid_len == 6` puts the name at 8 for both, so one frame serves both.
const int clientIdLength = 6;
// Build CMD_APP_START frame
// Format: [cmd][app_ver][reserved x6][app_name...]
// Format: [cmd][cid_len=6][client_id x6][app_name...]
// Stock reads bytes 1..7 as reserved + name at 8; Wadamesh reads the client id
// and lands on the same name offset. See [clientIdLength].
Uint8List buildAppStartFrame({
String appName = 'MeshCoreOpen',
int appVersion = 1,
Uint8List? clientId,
}) {
final id = Uint8List(clientIdLength);
if (clientId != null) {
// Truncate or zero-pad: the length byte must stay 6 or the app-name offset
// desyncs on one of the two firmwares.
id.setRange(0, min(clientId.length, clientIdLength), clientId);
}
final writer = BufferWriter();
writer.writeByte(cmdAppStart);
writer.writeByte(appVersion);
writer.writeBytes(Uint8List(6)); // reserved bytes
writer.writeByte(clientIdLength);
writer.writeBytes(id);
writer.writeString(appName);
writer.writeByte(0);
return writer.toBytes();

@ -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…
Cancel
Save

Powered by TurnKey Linux.