feat(#821): region protocol frames + version gate (Epic #813 T2)

Adds the stock flood-scope companion frames (CMD 54/63/64, RESP 28), the regions-discovery anon request (CMD 57 type 0x01) and its 0x8C CSV reply parser, and firmwareSupportsRegionScope (FIRMWARE_VER_CODE >= 13). Wire layouts verified in firmware source; frame build/parse round-trip tested incl. empty and non-matching frames.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
epic/816-integration
diffwireauto 2 months ago committed by Strycher
parent beb6d6434e
commit 4b295f74fc

@ -667,6 +667,166 @@ OffbandPktHash? parseOffbandPktHashReply(Uint8List frame) {
return OffbandPktHash(timestamp: ts, channelIdx: chan, hashHex: hashHex);
}
// --- Region flood-scoping + regions discovery (Feature #812, Epic #813).
//
// Stock MeshCore companion frames, verified against firmware source:
// - CMD_SET_FLOOD_SCOPE_KEY 54 (companion MyMesh.cpp:3512): sub-0 sets a 16-byte
// send-scope override ([54,0,key16]) or clears it to the node default ([54,0]);
// sub-1 ([54,1]) forces unscoped sends (FIRMWARE_VER_CODE 12+).
// - CMD_SET_DEFAULT_FLOOD_SCOPE 63 (MyMesh.cpp:3523): [63,name:31,key:16] sets
// the node default, bare [63] clears it.
// - CMD_GET_DEFAULT_FLOOD_SCOPE 64 (MyMesh.cpp:3540) -> RESP_CODE_DEFAULT_FLOOD_SCOPE
// 28: bare [28] = none, or [28,name:31,key:16].
// - Discovery is an ANON request: CMD_SEND_ANON_REQ 57 carrying
// [timestamp:4][ANON_REQ_TYPE_REGIONS 0x01][reply_path_len] (companion
// MyMesh.cpp:3114; repeater simple_repeater MyMesh.cpp:586-607,150-163). The
// repeater replies region names as a CSV; the companion forwards it to the app
// as PUSH_CODE_BINARY_RESPONSE 0x8C: [0x8C,0,tag:4,clock:4,CSV]. The tag is the
// request timestamp the firmware echoes in RESP_CODE_SENT.
const int cmdSetFloodScopeKey = 54;
const int cmdSetDefaultFloodScope = 63;
const int cmdGetDefaultFloodScope = 64;
const int respCodeDefaultFloodScope = 28;
const int anonReqTypeRegions = 0x01;
const int floodScopeSubSetKey = 0x00;
const int floodScopeSubUnscoped = 0x01;
/// Byte length of the on-wire region transport key and the default-scope name
/// field (firmware `TransportKey::key` and `default_scope_name`).
const int regionKeyLength = 16;
const int defaultScopeNameField = 31;
/// True iff the connected firmware supports region flood-scoping and regions
/// discovery. Floor is `FIRMWARE_VER_CODE` 13: `CMD_SEND_ANON_REQ` accepts a
/// non-contact repeater recipient at 13+ (companion `MyMesh.cpp:3118`), the path
/// regions discovery relies on; `CMD_SET_FLOOD_SCOPE_KEY` sub-0 is older (8+) and
/// sub-1 unscoped is 12+, both covered by 13. Fail-closed: the client must never
/// emit a region frame when this is false. (#813)
bool firmwareSupportsRegionScope(int? firmwareVerCode) =>
(firmwareVerCode ?? 0) >= 13;
/// Regions-discovery ANON request to [repeaterPubKey] (32 bytes). Wire:
/// `[57][pubkey32][timestamp:4 LE][0x01][reply_path_len=0]`. The firmware echoes
/// [timestamp] as the `RESP_CODE_SENT` tag, so the caller correlates the `0x8C`
/// reply by that tag. A zero reply-path means zero-hop direct, which the repeater
/// requires (`isRouteDirect`) to answer a regions request.
Uint8List buildAnonRegionsRequestFrame(
Uint8List repeaterPubKey, {
int? timestamp,
}) {
final ts = timestamp ?? DateTime.now().millisecondsSinceEpoch ~/ 1000;
final writer = BufferWriter();
writer.writeByte(cmdSendAnonReq);
writer.writeBytes(repeaterPubKey);
writer.writeUInt32LE(ts);
writer.writeByte(anonReqTypeRegions);
writer.writeByte(0); // reply_path_len = 0 (zero-hop direct)
return writer.toBytes();
}
/// Set the companion's flood send-scope override to [key], applied to subsequent
/// sends until cleared. Wire: `[54][0][key16]`. (FIRMWARE_VER_CODE 8+)
Uint8List buildSetFloodScopeKeyFrame(Uint8List key) {
final padded = Uint8List(regionKeyLength);
padded.setRange(0, min(key.length, regionKeyLength), key);
final writer = BufferWriter();
writer.writeByte(cmdSetFloodScopeKey);
writer.writeByte(floodScopeSubSetKey);
writer.writeBytes(padded);
return writer.toBytes();
}
/// Clear the flood send-scope override, reverting to the node default scope.
/// Wire: `[54][0]`. (FIRMWARE_VER_CODE 8+)
Uint8List buildClearFloodScopeFrame() =>
Uint8List.fromList([cmdSetFloodScopeKey, floodScopeSubSetKey]);
/// Force subsequent sends unscoped (no flood scope). Wire: `[54][1]`.
/// (FIRMWARE_VER_CODE 12+)
Uint8List buildFloodScopeUnscopedFrame() =>
Uint8List.fromList([cmdSetFloodScopeKey, floodScopeSubUnscoped]);
/// Set the node default flood scope to ([name],[key]). Wire:
/// `[63][name:31 NUL-padded][key:16]`. Firmware requires a name of 1..30 bytes.
Uint8List buildSetDefaultFloodScopeFrame(String name, Uint8List key) {
final padded = Uint8List(regionKeyLength);
padded.setRange(0, min(key.length, regionKeyLength), key);
final writer = BufferWriter();
writer.writeByte(cmdSetDefaultFloodScope);
writer.writeCString(name, defaultScopeNameField);
writer.writeBytes(padded);
return writer.toBytes();
}
/// Clear the node default flood scope. Wire: `[63]`.
Uint8List buildClearDefaultFloodScopeFrame() =>
Uint8List.fromList([cmdSetDefaultFloodScope]);
/// Query the node default flood scope. Wire: `[64]` -> RESP 28.
Uint8List buildGetDefaultFloodScopeFrame() =>
Uint8List.fromList([cmdGetDefaultFloodScope]);
/// A node's default flood scope, from a `RESP_CODE_DEFAULT_FLOOD_SCOPE` (28)
/// reply. [name] is empty when the node has no default scope set.
class DefaultFloodScope {
const DefaultFloodScope({required this.name, required this.key});
final String name;
final Uint8List key;
bool get isSet => name.isNotEmpty;
}
/// Parse a `RESP_CODE_DEFAULT_FLOOD_SCOPE` (28) reply. Returns null when [frame]
/// is not a RESP 28; otherwise a [DefaultFloodScope] whose [name] is empty when
/// the node reports no default (bare `[28]`).
DefaultFloodScope? parseDefaultFloodScopeReply(Uint8List frame) {
if (frame.isEmpty || frame[0] != respCodeDefaultFloodScope) return null;
if (frame.length < 1 + defaultScopeNameField + regionKeyLength) {
return DefaultFloodScope(name: '', key: Uint8List(0));
}
final reader = BufferReader(frame);
reader.readByte(); // resp code
final name = reader.readCStringGreedy(defaultScopeNameField);
final key = reader.readBytes(regionKeyLength);
return DefaultFloodScope(name: name, key: key);
}
/// A regions-discovery reply parsed from a `PUSH_CODE_BINARY_RESPONSE` (0x8C)
/// envelope: `[0x8C][0][tag:4][clock:4][CSV names]`. [tag] matches the
/// `RESP_CODE_SENT` tag from [buildAnonRegionsRequestFrame]; names are
/// `#`-stripped and comma-separated, with `*` marking a wildcard.
class RegionsReply {
const RegionsReply({
required this.tag,
required this.clock,
required this.regionNames,
});
final int tag;
final int clock;
final List<String> regionNames;
}
/// Parse a `0x8C` frame as a regions reply. Returns null for a short or non-0x8C
/// frame. An empty name list is a valid reply (a repeater flooding no regions),
/// never a parse error. Callers match [RegionsReply.tag] to a request they sent;
/// `0x8C` is a generic binary-response envelope, so only interpret it as regions
/// for a tag that was a regions request.
RegionsReply? parseRegionsReply(Uint8List frame) {
if (frame.length < 10 || frame[0] != pushCodeBinaryResponse) return null;
final reader = BufferReader(frame);
reader.readByte(); // 0x8C
reader.readByte(); // reserved
final tag = reader.readUInt32LE();
final clock = reader.readUInt32LE();
final csv = reader.readCString();
final names = csv
.split(',')
.map((s) => s.trim())
.where((s) => s.isNotEmpty)
.toList();
return RegionsReply(tag: tag, clock: clock, regionNames: names);
}
const int statsTypeCore = 0;
const int statsTypeRadio = 1;
const int statsTypePackets = 2;

@ -0,0 +1,156 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
// Wire contract verified against firmware source:
// - companion MyMesh.cpp:3114-3144 (CMD_SEND_ANON_REQ, non-contact 13+),
// 3512-3548 (CMD_SET_FLOOD_SCOPE_KEY 54, CMD_SET/GET_DEFAULT_FLOOD_SCOPE
// 63/64, RESP_CODE_DEFAULT_FLOOD_SCOPE 28), 1201-1211 (0x8C envelope).
// - simple_repeater MyMesh.cpp:586-607, 150-163 (anon regions req/reply).
Uint8List _key16(int seed) =>
Uint8List.fromList(List<int>.generate(16, (i) => (seed + i) & 0xFF));
void main() {
group('buildAnonRegionsRequestFrame', () {
test('[57][pubkey32][ts4 LE][0x01][reply_path_len=0]', () {
final pub = Uint8List.fromList(List<int>.generate(32, (i) => i + 1));
final frame = buildAnonRegionsRequestFrame(pub, timestamp: 0x11223344);
expect(frame.length, 1 + 32 + 4 + 1 + 1);
expect(frame[0], cmdSendAnonReq);
expect(frame.sublist(1, 33), pub);
expect(frame.sublist(33, 37), [0x44, 0x33, 0x22, 0x11]); // LE timestamp
expect(frame[37], anonReqTypeRegions);
expect(frame[38], 0); // zero reply-path = zero-hop direct
});
});
group('flood-scope key frames', () {
test('set: [54][0][key16]', () {
final frame = buildSetFloodScopeKeyFrame(_key16(0xA0));
expect(frame.length, 2 + 16);
expect(frame[0], cmdSetFloodScopeKey);
expect(frame[1], 0);
expect(frame.sublist(2), _key16(0xA0));
});
test('clear override to node default: [54][0]', () {
expect(buildClearFloodScopeFrame(), [cmdSetFloodScopeKey, 0]);
});
test('force unscoped (ver 12+): [54][1]', () {
expect(buildFloodScopeUnscopedFrame(), [cmdSetFloodScopeKey, 1]);
});
});
group('default flood scope', () {
test('set: [63][name:31 NUL-padded][key:16]', () {
final frame = buildSetDefaultFloodScopeFrame('oki', _key16(0x10));
expect(frame.length, 1 + 31 + 16);
expect(frame[0], cmdSetDefaultFloodScope);
expect(frame.sublist(1, 4), 'oki'.codeUnits);
expect(frame[4], 0); // NUL terminator
expect(frame.sublist(32, 48), _key16(0x10));
});
test('clear: [63]', () {
expect(buildClearDefaultFloodScopeFrame(), [cmdSetDefaultFloodScope]);
});
test('get: [64]', () {
expect(buildGetDefaultFloodScopeFrame(), [cmdGetDefaultFloodScope]);
});
test('parse RESP 28 with a set scope', () {
final f = BytesBuilder()
..addByte(respCodeDefaultFloodScope)
..add(_padName('oki', 31))
..add(_key16(0x10));
final scope = parseDefaultFloodScopeReply(f.toBytes());
expect(scope, isNotNull);
expect(scope!.name, 'oki');
expect(scope.key, _key16(0x10));
expect(scope.isSet, isTrue);
});
test('parse bare RESP 28 as no-default', () {
final scope = parseDefaultFloodScopeReply(
Uint8List.fromList([respCodeDefaultFloodScope]),
);
expect(scope, isNotNull);
expect(scope!.isSet, isFalse);
});
test('parse returns null for a non-28 frame', () {
expect(
parseDefaultFloodScopeReply(Uint8List.fromList([respCodeOk])),
isNull,
);
});
});
group('parseRegionsReply (0x8C envelope)', () {
Uint8List reply(int tag, int clock, String csv) {
final b = BytesBuilder()
..addByte(pushCodeBinaryResponse)
..addByte(0) // reserved
..add(_u32le(tag))
..add(_u32le(clock))
..add(Uint8List.fromList([...csv.codeUnits, 0]));
return b.toBytes();
}
test('extracts tag, clock, and comma-split names', () {
final r = parseRegionsReply(reply(0xDEADBEEF, 0x01020304, 'oki,test,*'));
expect(r, isNotNull);
expect(r!.tag, 0xDEADBEEF);
expect(r.clock, 0x01020304);
expect(r.regionNames, ['oki', 'test', '*']);
});
test('empty CSV yields no names (not a parse error)', () {
final r = parseRegionsReply(reply(1, 2, ''));
expect(r, isNotNull);
expect(r!.regionNames, isEmpty);
});
test('null for a short or non-0x8C frame', () {
expect(
parseRegionsReply(Uint8List.fromList([pushCodeBinaryResponse, 0])),
isNull,
);
expect(
parseRegionsReply(
Uint8List.fromList([respCodeOk, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
),
isNull,
);
});
});
group('firmwareSupportsRegionScope', () {
test('floor is FIRMWARE_VER_CODE 13', () {
expect(firmwareSupportsRegionScope(null), isFalse);
expect(firmwareSupportsRegionScope(7), isFalse);
expect(firmwareSupportsRegionScope(12), isFalse);
expect(firmwareSupportsRegionScope(13), isTrue);
expect(firmwareSupportsRegionScope(22), isTrue);
});
});
}
Uint8List _u32le(int v) {
final b = Uint8List(4);
ByteData.sublistView(b).setUint32(0, v, Endian.little);
return b;
}
Uint8List _padName(String name, int len) {
final b = Uint8List(len);
final bytes = name.codeUnits;
for (var i = 0; i < bytes.length && i < len - 1; i++) {
b[i] = bytes[i];
}
return b;
}
Loading…
Cancel
Save

Powered by TurnKey Linux.