From 39977787f8285c08d8315ccbb5f1a6d79165139d Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Sat, 3 Oct 2026 23:14:42 -0400 Subject: [PATCH] feat(#769): stock and Offband fake radio profiles from protocol manifests tool/fake_radio/gen_protocol_manifest.py reads companion firmware source at a git ref and emits the manifest format proposed in OffbandMesh/meshcore-firmware#1318. Pinned copies, verified identical to the generator's output: - offband-v1.5.0-beta7 (53951759): code 22, MeshCore base v1.17.0 - companion-v1.17.1 (stock, d9296435): code 13 FakeRadioProfile.fromManifest takes codes, bits and the DEVICE_INFO version string (offbandClientVersion() derivation) from the manifest; naming a bit the manifest lacks throws instead of reading 0. Offband answers GPS status, the block list (BlockStore dedup/swap-remove semantics) and the packet-hash query; stock rejects every Offband command as unsupported. Found writing it: the A2 default had OFFBAND_CAP_CAPLOG as 0x08 (it is 0x20) and the version string as 1.5.0-1.17.1 (beta7 is on v1.17.0). Both now come from the manifest. 27 tests. Co-Authored-By: Claude Opus 5.5 --- test/fake_radio/fake_radio_core_test.dart | 7 +- test/fake_radio/fake_radio_profile_test.dart | 163 ++++++++++++++++++ test/support/fake_radio/fake_radio.dart | 98 ++++++++++- test/support/fake_radio/fake_radio_codes.dart | 22 +++ .../fake_radio/fake_radio_manifest.dart | 92 ++++++++++ .../fake_radio/fake_radio_profile.dart | 71 +++++--- .../manifests/offband-v1.5.0-beta7.json | 159 +++++++++++++++++ .../manifests/stock-companion-v1.17.1.json | 133 ++++++++++++++ tool/fake_radio/gen_protocol_manifest.py | 99 +++++++++++ 9 files changed, 807 insertions(+), 37 deletions(-) create mode 100644 test/fake_radio/fake_radio_profile_test.dart create mode 100644 test/support/fake_radio/fake_radio_manifest.dart create mode 100644 test/support/fake_radio/manifests/offband-v1.5.0-beta7.json create mode 100644 test/support/fake_radio/manifests/stock-companion-v1.17.1.json create mode 100644 tool/fake_radio/gen_protocol_manifest.py diff --git a/test/fake_radio/fake_radio_core_test.dart b/test/fake_radio/fake_radio_core_test.dart index 83ff32e8..8f95fc65 100644 --- a/test/fake_radio/fake_radio_core_test.dart +++ b/test/fake_radio/fake_radio_core_test.dart @@ -33,7 +33,8 @@ void main() { expect(f[3], 8); expect(_cstr(f, 8, 12), '1 Oct 2026'); expect(_cstr(f, 20, 40), 'Fake Radio Board'); - expect(_cstr(f, 60, 20), '1.5.0-1.17.1'); + // offband-v1.5.0-beta7 is built on MeshCore v1.17.0. + expect(_cstr(f, 60, 20), '1.5.0-1.17.0'); expect(f.length, 87); expect(f[82], 0x22); // BLOCK | CAPLOG expect(f[84], 0x08); // PKT_HASH @@ -41,7 +42,7 @@ void main() { test('stock: ends after path hash mode (82 bytes), no Offband tail', () { final f = FakeRadio( - profile: FakeRadioProfile.stockDefault, + profile: FakeRadioProfile.stock(), ).handle(_cmd([fwCmdDeviceQuery, 4])).single; expect(f[1], 13); expect(_cstr(f, 60, 20), 'v1.17.1'); @@ -127,7 +128,7 @@ void main() { }); test('stock refuses a time earlier than its clock', () { - final radio = FakeRadio(profile: FakeRadioProfile.stockDefault); + final radio = FakeRadio(profile: FakeRadioProfile.stock()); expect(radio.handle(_cmd([fwCmdSetDeviceTime, ..._le32(1000)])).single, [ fwRespErr, fwErrIllegalArg, diff --git a/test/fake_radio/fake_radio_profile_test.dart b/test/fake_radio/fake_radio_profile_test.dart new file mode 100644 index 00000000..9da29918 --- /dev/null +++ b/test/fake_radio/fake_radio_profile_test.dart @@ -0,0 +1,163 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; + +import '../support/fake_radio/fake_radio.dart'; +import '../support/fake_radio/fake_radio_codes.dart'; +import '../support/fake_radio/fake_radio_manifest.dart'; +import '../support/fake_radio/fake_radio_profile.dart'; + +Uint8List _cmd(List b) => Uint8List.fromList(b); + +void main() { + group('pinned manifests (#769)', () { + test('Offband: offband-v1.5.0-beta7 on MeshCore v1.17.0, code 22', () { + final m = FakeRadioManifest.pinnedOffband(); + expect(m.isOffband, isTrue); + expect(m.ref, 'offband-v1.5.0-beta7'); + expect(m.firmwareVerCode, 22); + expect(m.firmwareVersion, 'v1.17.0'); + expect(m.deviceInfoVersion, '1.5.0-1.17.0'); + expect(m.offbandCaps['OFFBAND_CAP_CAPLOG'], 0x20); + expect(m.offbandCommands['CMD_OFFBAND_PKT_HASH'], 0xC6); + }); + + test('stock: companion-v1.17.1, code 13, no Offband anything', () { + final m = FakeRadioManifest.pinnedStock(); + expect(m.isOffband, isFalse); + expect(m.firmwareVerCode, 13); + expect(m.deviceInfoVersion, 'v1.17.1'); + expect(m.offbandCaps, isEmpty); + expect(m.offbandCommands, isEmpty); + }); + + test('a wrong format or a newer format_version is refused', () { + expect( + () => FakeRadioManifest.parse(jsonEncode({'format': 'x'})), + throwsFormatException, + ); + expect( + () => FakeRadioManifest.parse( + jsonEncode({ + 'format': 'offband-protocol-manifest', + 'format_version': 2, + }), + ), + throwsFormatException, + ); + }); + + test('a capability the manifest lacks fails loudly, never reads as 0', () { + final m = FakeRadioManifest.pinnedOffband(); + expect(() => m.capsOf(['OFFBAND_CAP_NOPE']), throwsStateError); + expect( + () => FakeRadioProfile.fromManifest(m, caps: ['OFFBAND_CAP_NOPE']), + throwsStateError, + ); + }); + }); + + group('profiles', () { + test('Offband defaults to the bits every companion sets', () { + final p = FakeRadioProfile.offband(); + expect(p.offbandCaps, 0x02 | 0x20); // BLOCK | CAPLOG + expect(p.offbandCaps2, 0x08); // PKT_HASH + }); + + test('board bits can be named', () { + final p = FakeRadioProfile.fromManifest( + FakeRadioManifest.pinnedOffband(), + caps: ['OFFBAND_CAP_BLOCK', 'OFFBAND_CAP_FEM_LNA'], + caps2: ['OFFBAND_CAP2_NOTIFY_SCOPE'], + ); + expect(p.offbandCaps, 0x06); + expect(p.offbandCaps2, 0x01); + }); + + test('stock rejects every Offband command as unsupported', () { + final radio = FakeRadio(profile: FakeRadioProfile.stock()); + for (final code in [ + fwOffbandConfig, + fwOffbandGps, + fwOffbandBlock, + fwOffbandPktHash, + ]) { + expect(radio.handle(_cmd([code, fwBlockList])).single, [ + fwRespErr, + fwErrUnsupportedCmd, + ]); + } + }); + }); + + group('Offband commands', () { + final key = List.filled(fwPubKeySize, 0x42); + final other = List.filled(fwPubKeySize, 0x43); + + test('block: add dedups, remove swaps, list streams start/keys/end', () { + final radio = FakeRadio(); + expect(radio.handle(_cmd([fwOffbandBlock, fwBlockAdd, ...key])).single, [ + fwOffbandBlock, + fwBlockAdd, + 1, + ]); + // A repeat is still OK (BlockStore dedup). + expect( + radio.handle(_cmd([fwOffbandBlock, fwBlockAdd, ...key])).single[2], + 1, + ); + radio.handle(_cmd([fwOffbandBlock, fwBlockAdd, ...other])); + final list = radio.handle(_cmd([fwOffbandBlock, fwBlockList])); + expect(list.first, [fwOffbandBlock, fwBlockList, 0xFF, 2]); + expect(list[1].sublist(3), key); + expect(list.last, [fwOffbandBlock, fwBlockList, 0xFE]); + + expect( + radio.handle(_cmd([fwOffbandBlock, fwBlockRemove, ...key])).single[2], + 1, + ); + expect(radio.blockedKeys.single, other); + expect( + radio.handle(_cmd([fwOffbandBlock, fwBlockRemove, ...key])).single[2], + 0, + ); + }); + + test('GPS status reply is "enabled=N " plus board text, NUL-ended', () { + final f = FakeRadio().handle(_cmd([fwOffbandGps])).single; + expect(f[0], fwOffbandGps); + expect(f.last, 0); + expect(utf8.decode(f.sublist(1, f.length - 1)), 'enabled=0 detected=0'); + }); + + test('packet hash: unknown key, or malformed when not 7 bytes', () { + final radio = FakeRadio(); + expect( + radio.handle(_cmd([fwOffbandPktHash, fwPktHashGet, 1, 2, 3, 4, 0])), + [ + [fwOffbandPktHash, fwPktHashErr, fwPktHashErrUnknownKey], + ], + ); + expect(radio.handle(_cmd([fwOffbandPktHash, fwPktHashGet, 1])).single, [ + fwOffbandPktHash, + fwPktHashErr, + fwPktHashErrMalformed, + ]); + }); + + test('commands needing unadvertised bits are unsupported', () { + final radio = FakeRadio(); + for (final code in [ + fwOffbandConfig, + fwOffbandFemLna, + fwOffbandDeviceUi, + ]) { + expect(radio.handle(_cmd([code, 0])).single, [ + fwRespErr, + fwErrUnsupportedCmd, + ]); + } + }); + }); +} diff --git a/test/support/fake_radio/fake_radio.dart b/test/support/fake_radio/fake_radio.dart index a38c2856..ee4976cf 100644 --- a/test/support/fake_radio/fake_radio.dart +++ b/test/support/fake_radio/fake_radio.dart @@ -15,12 +15,10 @@ import 'fake_radio_seed.dart'; /// the named firmware function builds it, in /// OffbandMesh/meshcore-firmware `examples/companion_radio/MyMesh.cpp`. class FakeRadio { - FakeRadio({ - FakeRadioSeed? seed, - this.profile = FakeRadioProfile.offbandDefault, - FakeClock? clock, - }) : seed = seed ?? FakeRadioSeed(), - clock = clock ?? FakeClock() { + FakeRadio({FakeRadioSeed? seed, FakeRadioProfile? profile, FakeClock? clock}) + : seed = seed ?? FakeRadioSeed(), + profile = profile ?? FakeRadioProfile.offband(), + clock = clock ?? FakeClock() { final s = this.seed; name = s.name; txPowerDbm = s.txPowerDbm; @@ -71,6 +69,12 @@ class FakeRadio { final List contacts = []; final Map channels = {}; + /// The Offband user-block list (0xC2), as public keys. + final List blockedKeys = []; + + /// `MAX_BLOCKED_KEYS` (src/helpers/BlockStore.h:20). + static const int maxBlockedKeys = 32; + /// Frames waiting for CMD_SYNC_NEXT_MESSAGE (the firmware's offline queue). final List offlineQueue = []; @@ -92,6 +96,9 @@ class FakeRadio { received.add(Uint8List.fromList(cmd)); if (cmd.isEmpty) return []; final len = cmd.length; + if (profile.offband && cmd[0] >= fwOffbandConfig && cmd[0] <= 0xCF) { + return _offband(cmd); + } switch (cmd[0]) { case fwCmdDeviceQuery when len >= 2: return [deviceInfoFrame()]; @@ -159,6 +166,85 @@ class FakeRadio { } } + /// Offband commands. The fake answers the ones a default companion's client + /// sends: GPS status, the block list and the packet-hash query. Config, + /// FEM LNA, caplog and device-UI need capability bits the default profile + /// doesn't advertise, so a client honoring the bits never sends them; the + /// fake rejects them as unsupported rather than invent a reply. + List _offband(Uint8List cmd) { + final len = cmd.length; + switch (cmd[0]) { + case fwOffbandGps: + // MyMesh.cpp:1975-1983: "enabled=N " + board status, NUL-terminated. + final enabled = customVars['gps'] == '1' ? 1 : 0; + return [ + Uint8List.fromList([ + fwOffbandGps, + ...utf8.encode('enabled=$enabled ${profile.gpsStatusText}'), + 0, + ]), + ]; + case fwOffbandBlock when len >= 2: + return _block(cmd); + case fwOffbandPktHash when len >= 2 && cmd[1] == fwPktHashGet: + // MyMesh.cpp:2114-2133: exactly 7 bytes; the fake keeps no send ring, + // so every well-formed query is an unknown key. + return [ + Uint8List.fromList([ + fwOffbandPktHash, + fwPktHashErr, + len != 7 ? fwPktHashErrMalformed : fwPktHashErrUnknownKey, + ]), + ]; + default: + return [errFrame(fwErrUnsupportedCmd)]; + } + } + + /// 0xC2 (MyMesh.cpp:2074-2100) and its list drain (`blockListDrain`). + List _block(Uint8List cmd) { + final sub = cmd[1]; + Uint8List ack(bool ok) => + Uint8List.fromList([fwOffbandBlock, sub, ok ? 1 : 0]); + bool same(Uint8List a, List b) { + for (var i = 0; i < fwPubKeySize; i++) { + if (a[i] != b[i]) return false; + } + return true; + } + + if ((sub == fwBlockAdd || sub == fwBlockRemove) && + cmd.length >= 2 + fwPubKeySize) { + // BlockStore (src/helpers/BlockStore.h:48-70): add dedups (a repeat is + // true), fails only when full; remove swaps the last entry into the gap. + final key = cmd.sublist(2, 2 + fwPubKeySize); + final at = blockedKeys.indexWhere((k) => same(k, key)); + if (sub == fwBlockAdd) { + if (at >= 0) return [ack(true)]; + if (blockedKeys.length >= maxBlockedKeys) return [ack(false)]; + blockedKeys.add(Uint8List.fromList(key)); + return [ack(true)]; + } + if (at < 0) return [ack(false)]; + final last = blockedKeys.removeLast(); + if (at < blockedKeys.length) blockedKeys[at] = last; + return [ack(true)]; + } + if (sub == fwBlockClear) { + blockedKeys.clear(); + return [ack(true)]; + } + if (sub == fwBlockList) { + return [ + Uint8List.fromList([fwOffbandBlock, sub, 0xFF, blockedKeys.length]), + for (var i = 0; i < blockedKeys.length; i++) + Uint8List.fromList([fwOffbandBlock, sub, i, ...blockedKeys[i]]), + Uint8List.fromList([fwOffbandBlock, sub, 0xFE]), + ]; + } + return [errFrame(fwErrIllegalArg)]; + } + /// `writeOKFrame` (MyMesh.cpp:258). static Uint8List okFrame() => Uint8List.fromList([fwRespOk]); diff --git a/test/support/fake_radio/fake_radio_codes.dart b/test/support/fake_radio/fake_radio_codes.dart index 2fb21c18..da521b0b 100644 --- a/test/support/fake_radio/fake_radio_codes.dart +++ b/test/support/fake_radio/fake_radio_codes.dart @@ -53,6 +53,28 @@ const int fwErrTableFull = 3; const int fwErrBadState = 4; const int fwErrIllegalArg = 6; +// Offband commands (MyMesh.cpp:110,117; OffbandConfigProtocol.h). Request and +// response share the code. +const int fwOffbandConfig = 0xC0; +const int fwOffbandGps = 0xC1; +const int fwOffbandBlock = 0xC2; +const int fwOffbandFemLna = 0xC3; +const int fwOffbandCaplog = 0xC4; +const int fwOffbandDeviceUi = 0xC5; +const int fwOffbandPktHash = 0xC6; + +/// 0xC2 sub-commands (OffbandConfigProtocol.h:312-315). +const int fwBlockAdd = 0x01; +const int fwBlockRemove = 0x02; +const int fwBlockList = 0x03; +const int fwBlockClear = 0x04; + +/// 0xC6 sub-commands and errors (OffbandConfigProtocol.h:158-161). +const int fwPktHashGet = 0x01; +const int fwPktHashErr = 0x7F; +const int fwPktHashErrUnknownKey = 1; +const int fwPktHashErrMalformed = 2; + /// `ADV_TYPE_*` (src/helpers/AdvertDataHelpers.h). const int fwAdvTypeChat = 1; const int fwAdvTypeRepeater = 2; diff --git a/test/support/fake_radio/fake_radio_manifest.dart b/test/support/fake_radio/fake_radio_manifest.dart new file mode 100644 index 00000000..a99b952c --- /dev/null +++ b/test/support/fake_radio/fake_radio_manifest.dart @@ -0,0 +1,92 @@ +import 'dart:convert'; +import 'dart:io'; + +/// A firmware protocol manifest (#769): the codes, capability bits and version +/// a firmware build defines, generated from its source. Today's copies are +/// pinned under `manifests/` by `tool/fake_radio/gen_protocol_manifest.py`; +/// OffbandMesh/meshcore-firmware#1318 will publish them from firmware CI. +class FakeRadioManifest { + FakeRadioManifest._(this.json); + + factory FakeRadioManifest.parse(String text) { + final json = jsonDecode(text) as Map; + if (json['format'] != 'offband-protocol-manifest') { + throw FormatException('not a protocol manifest: ${json['format']}'); + } + if (json['format_version'] != 1) { + throw FormatException( + 'unsupported manifest format_version ${json['format_version']}', + ); + } + return FakeRadioManifest._(json); + } + + factory FakeRadioManifest.load(String path) => + FakeRadioManifest.parse(File(path).readAsStringSync()); + + static const String _dir = 'test/support/fake_radio/manifests'; + + /// The Offband release the fake's Offband profile is pinned to. + static FakeRadioManifest pinnedOffband() => + FakeRadioManifest.load('$_dir/offband-v1.5.0-beta7.json'); + + /// The upstream MeshCore release the fake's stock profile is pinned to. + static FakeRadioManifest pinnedStock() => + FakeRadioManifest.load('$_dir/stock-companion-v1.17.1.json'); + + final Map json; + + Map get _firmware => + json['firmware'] as Map; + + String get flavor => _firmware['flavor'] as String; + bool get isOffband => flavor == 'offband'; + String get ref => _firmware['ref'] as String; + String get commit => _firmware['commit'] as String; + + /// `FIRMWARE_VERSION`, the MeshCore base, e.g. `v1.17.0`. + String get firmwareVersion => _firmware['firmware_version'] as String; + int get firmwareVerCode => _firmware['firmware_ver_code'] as int; + + Map section(String name) => + (json[name] as Map).map((k, v) => MapEntry(k, v as int)); + + Map get commands => section('commands'); + Map get responses => section('responses'); + Map get pushes => section('pushes'); + Map get errors => section('errors'); + Map get offbandCommands => section('offband_commands'); + Map get offbandResponses => section('offband_responses'); + Map get offbandCaps => section('offband_caps'); + Map get offbandCaps2 => section('offband_caps2'); + + /// OR of the named capability bits; throws if the manifest lacks one, so a + /// renamed or removed bit fails loudly instead of reading as 0. + int capsOf(Iterable names, {bool second = false}) { + final table = second ? offbandCaps2 : offbandCaps; + var bits = 0; + for (final n in names) { + final v = table[n]; + if (v == null) throw StateError('$ref defines no $n'); + bits |= v; + } + return bits; + } + + /// The 20-byte DEVICE_INFO version field this build reports. + /// + /// Offband: `offbandClientVersion()` (MyMesh.cpp:1761), the Offband core, + /// a '-', then the MeshCore core (e.g. `1.5.0-1.17.0`); each core is cut at + /// its first '-', with the 'v' stripped from the MeshCore one. Stock: + /// `FIRMWARE_VERSION` as is. + String get deviceInfoVersion { + if (!isOffband) return firmwareVersion; + var mc = firmwareVersion; + if (mc.startsWith('v') || mc.startsWith('V')) mc = mc.substring(1); + mc = mc.split('-').first; + final at = ref.indexOf('offband-v'); + if (at < 0) return mc; + final ob = ref.substring(at + 'offband-v'.length).split('-').first; + return ob.isEmpty ? mc : '$ob-$mc'; + } +} diff --git a/test/support/fake_radio/fake_radio_profile.dart b/test/support/fake_radio/fake_radio_profile.dart index 7418819e..58c22443 100644 --- a/test/support/fake_radio/fake_radio_profile.dart +++ b/test/support/fake_radio/fake_radio_profile.dart @@ -1,25 +1,55 @@ -/// Which firmware the fake radio plays. The difference is what DEVICE_INFO -/// reports and whether Offband (0xC0-0xCF) commands are answered. -/// -/// A4 (#769) builds these from the firmware's protocol manifests; until then -/// [offband] and [stock] carry the values read from firmware source: -/// - Offband: `examples/companion_radio/MyMesh.h:22` (FIRMWARE_VER_CODE 22) and -/// the DEVICE_INFO tail at MyMesh.cpp:2437-2504. -/// - Stock: upstream meshcore-dev/MeshCore `a366955c` (v1.17.1, -/// FIRMWARE_VER_CODE 13), whose DEVICE_INFO ends after path_hash_mode. +import 'fake_radio_manifest.dart'; + +/// Which firmware the fake radio plays (#769). The difference is what +/// DEVICE_INFO reports and whether Offband commands are answered. Built from a +/// firmware protocol manifest, so codes, bits and versions follow the firmware. class FakeRadioProfile { const FakeRadioProfile({ required this.name, required this.firmwareVerCode, required this.versionString, required this.offband, + this.manifest, this.offbandCaps = 0, this.femLnaEnabled = false, this.offbandCaps2 = 0, this.ledEnabled = 0, this.displayMode = 0, + this.gpsStatusText = 'detected=0', }); + /// A companion build of [manifest]'s firmware. + /// + /// Offband capability bits default to the ones every companion sets + /// unconditionally: OFFBAND_CAP_BLOCK and OFFBAND_CAP_CAPLOG + /// (MyMesh.cpp:2448,2453) and OFFBAND_CAP2_PKT_HASH (MyMesh.cpp:2499). + /// Board-dependent bits (observer, FEM LNA, buzzer, button, indicators) are + /// off unless named. + factory FakeRadioProfile.fromManifest( + FakeRadioManifest manifest, { + Iterable caps = const ['OFFBAND_CAP_BLOCK', 'OFFBAND_CAP_CAPLOG'], + Iterable caps2 = const ['OFFBAND_CAP2_PKT_HASH'], + }) { + final offband = manifest.isOffband; + return FakeRadioProfile( + name: manifest.flavor, + firmwareVerCode: manifest.firmwareVerCode, + versionString: manifest.deviceInfoVersion, + offband: offband, + manifest: manifest, + offbandCaps: offband ? manifest.capsOf(caps) : 0, + offbandCaps2: offband ? manifest.capsOf(caps2, second: true) : 0, + ); + } + + /// The pinned Offband release (`offband-v1.5.0-beta7`). + factory FakeRadioProfile.offband() => + FakeRadioProfile.fromManifest(FakeRadioManifest.pinnedOffband()); + + /// The pinned upstream MeshCore release (`companion-v1.17.1`). + factory FakeRadioProfile.stock() => + FakeRadioProfile.fromManifest(FakeRadioManifest.pinnedStock()); + final String name; final int firmwareVerCode; @@ -29,29 +59,14 @@ class FakeRadioProfile { /// True for Offband firmware: the extended DEVICE_INFO tail is sent and /// Offband commands are answered. Stock rejects them as unknown commands. final bool offband; + final FakeRadioManifest? manifest; final int offbandCaps; final bool femLnaEnabled; final int offbandCaps2; final int ledEnabled; final int displayMode; - static const FakeRadioProfile offbandDefault = FakeRadioProfile( - name: 'offband', - firmwareVerCode: 22, - versionString: '1.5.0-1.17.1', - offband: true, - // OFFBAND_CAP_BLOCK (0x02) | OFFBAND_CAP_CAPLOG (0x20), always set on the - // companion (MyMesh.cpp:2448,2453; OffbandConfigProtocol.h:301,305); no - // observer, no FEM LNA. - offbandCaps: 0x02 | 0x20, - // OFFBAND_CAP2_PKT_HASH, advertised unconditionally (MyMesh.cpp:2499). - offbandCaps2: 0x08, - ); - - static const FakeRadioProfile stockDefault = FakeRadioProfile( - name: 'stock', - firmwareVerCode: 13, - versionString: 'v1.17.1', - offband: false, - ); + /// What `sensors.getGpsStatusText` adds after `enabled=N ` in the 0xC1 + /// reply. Board-specific in firmware; a seed value here. + final String gpsStatusText; } diff --git a/test/support/fake_radio/manifests/offband-v1.5.0-beta7.json b/test/support/fake_radio/manifests/offband-v1.5.0-beta7.json new file mode 100644 index 00000000..6cbf76ac --- /dev/null +++ b/test/support/fake_radio/manifests/offband-v1.5.0-beta7.json @@ -0,0 +1,159 @@ +{ + "commands": { + "CMD_ADD_UPDATE_CONTACT": 9, + "CMD_APP_START": 1, + "CMD_DEVICE_QUERY": 22, + "CMD_EXPORT_CONTACT": 17, + "CMD_EXPORT_PRIVATE_KEY": 23, + "CMD_FACTORY_RESET": 51, + "CMD_GET_ADVERT_PATH": 42, + "CMD_GET_ALLOWED_REPEAT_FREQ": 60, + "CMD_GET_AUTOADD_CONFIG": 59, + "CMD_GET_BATT_AND_STORAGE": 20, + "CMD_GET_CHANNEL": 31, + "CMD_GET_CONTACTS": 4, + "CMD_GET_CONTACT_BY_KEY": 30, + "CMD_GET_CUSTOM_VARS": 40, + "CMD_GET_DEFAULT_FLOOD_SCOPE": 64, + "CMD_GET_DEVICE_TIME": 5, + "CMD_GET_STATS": 56, + "CMD_GET_TUNING_PARAMS": 43, + "CMD_HAS_CONNECTION": 28, + "CMD_IMPORT_CONTACT": 18, + "CMD_IMPORT_PRIVATE_KEY": 24, + "CMD_LOGOUT": 29, + "CMD_REBOOT": 19, + "CMD_REMOVE_CONTACT": 15, + "CMD_RESET_PATH": 13, + "CMD_SEND_ANON_REQ": 57, + "CMD_SEND_BINARY_REQ": 50, + "CMD_SEND_CHANNEL_DATA": 62, + "CMD_SEND_CHANNEL_TXT_MSG": 3, + "CMD_SEND_CONTROL_DATA": 55, + "CMD_SEND_LOGIN": 26, + "CMD_SEND_PATH_DISCOVERY_REQ": 52, + "CMD_SEND_RAW_DATA": 25, + "CMD_SEND_RAW_PACKET": 65, + "CMD_SEND_SELF_ADVERT": 7, + "CMD_SEND_STATUS_REQ": 27, + "CMD_SEND_TELEMETRY_REQ": 39, + "CMD_SEND_TRACE_PATH": 36, + "CMD_SEND_TXT_MSG": 2, + "CMD_SET_ADVERT_LATLON": 14, + "CMD_SET_ADVERT_NAME": 8, + "CMD_SET_AUTOADD_CONFIG": 58, + "CMD_SET_CHANNEL": 32, + "CMD_SET_CUSTOM_VAR": 41, + "CMD_SET_DEFAULT_FLOOD_SCOPE": 63, + "CMD_SET_DEVICE_PIN": 37, + "CMD_SET_DEVICE_TIME": 6, + "CMD_SET_FLOOD_SCOPE_KEY": 54, + "CMD_SET_OTHER_PARAMS": 38, + "CMD_SET_PATH_HASH_MODE": 61, + "CMD_SET_RADIO_PARAMS": 11, + "CMD_SET_RADIO_TX_POWER": 12, + "CMD_SET_TUNING_PARAMS": 21, + "CMD_SHARE_CONTACT": 16, + "CMD_SIGN_DATA": 34, + "CMD_SIGN_FINISH": 35, + "CMD_SIGN_START": 33, + "CMD_SYNC_NEXT_MESSAGE": 10 + }, + "errors": { + "ERR_CODE_BAD_STATE": 4, + "ERR_CODE_FILE_IO_ERROR": 5, + "ERR_CODE_ILLEGAL_ARG": 6, + "ERR_CODE_NOT_FOUND": 2, + "ERR_CODE_TABLE_FULL": 3, + "ERR_CODE_UNSUPPORTED_CMD": 1 + }, + "firmware": { + "commit": "53951759bceb4a6b4a90aac55d9ae9829712abf5", + "firmware_ver_code": 22, + "firmware_version": "v1.17.0", + "flavor": "offband", + "ref": "offband-v1.5.0-beta7" + }, + "format": "offband-protocol-manifest", + "format_version": 1, + "generated_by": "meshcore-client tool/fake_radio/gen_protocol_manifest.py", + "offband_caps": { + "OFFBAND_CAP_BLOCK": 2, + "OFFBAND_CAP_CAPLOG": 32, + "OFFBAND_CAP_FEM_LNA": 4, + "OFFBAND_CAP_WIFI_OBSERVER": 1 + }, + "offband_caps2": { + "OFFBAND_CAP2_BUTTON_MATRIX": 2, + "OFFBAND_CAP2_INDICATORS": 4, + "OFFBAND_CAP2_NOTIFY_SCOPE": 1, + "OFFBAND_CAP2_PKT_HASH": 8 + }, + "offband_commands": { + "CMD_OFFBAND_BLOCK": 194, + "CMD_OFFBAND_CAPLOG": 196, + "CMD_OFFBAND_CONFIG": 192, + "CMD_OFFBAND_DEVICE_UI": 197, + "CMD_OFFBAND_FEM_LNA": 195, + "CMD_OFFBAND_GPS": 193, + "CMD_OFFBAND_PKT_HASH": 198 + }, + "offband_responses": { + "RESP_CODE_OFFBAND_BLOCK": 194, + "RESP_CODE_OFFBAND_CAPLOG": 196, + "RESP_CODE_OFFBAND_CONFIG": 192, + "RESP_CODE_OFFBAND_DEVICE_UI": 197, + "RESP_CODE_OFFBAND_FEM_LNA": 195, + "RESP_CODE_OFFBAND_GPS": 193, + "RESP_CODE_OFFBAND_PKT_HASH": 198 + }, + "pushes": { + "PUSH_CODE_ADVERT": 128, + "PUSH_CODE_BINARY_RESPONSE": 140, + "PUSH_CODE_CONTACTS_FULL": 144, + "PUSH_CODE_CONTACT_DELETED": 143, + "PUSH_CODE_CONTROL_DATA": 142, + "PUSH_CODE_LOGIN_FAIL": 134, + "PUSH_CODE_LOGIN_SUCCESS": 133, + "PUSH_CODE_LOG_RX_DATA": 136, + "PUSH_CODE_MSG_WAITING": 131, + "PUSH_CODE_NEW_ADVERT": 138, + "PUSH_CODE_PATH_DISCOVERY_RESPONSE": 141, + "PUSH_CODE_PATH_UPDATED": 129, + "PUSH_CODE_RAW_DATA": 132, + "PUSH_CODE_SEND_CONFIRMED": 130, + "PUSH_CODE_STATUS_RESPONSE": 135, + "PUSH_CODE_TELEMETRY_RESPONSE": 139, + "PUSH_CODE_TRACE_DATA": 137 + }, + "responses": { + "RESP_CODE_ADVERT_PATH": 22, + "RESP_CODE_AUTOADD_CONFIG": 25, + "RESP_CODE_BATT_AND_STORAGE": 12, + "RESP_CODE_CHANNEL_DATA_RECV": 27, + "RESP_CODE_CHANNEL_INFO": 18, + "RESP_CODE_CHANNEL_MSG_RECV": 8, + "RESP_CODE_CHANNEL_MSG_RECV_V3": 17, + "RESP_CODE_CONTACT": 3, + "RESP_CODE_CONTACTS_START": 2, + "RESP_CODE_CONTACT_MSG_RECV": 7, + "RESP_CODE_CONTACT_MSG_RECV_V3": 16, + "RESP_CODE_CURR_TIME": 9, + "RESP_CODE_CUSTOM_VARS": 21, + "RESP_CODE_DEFAULT_FLOOD_SCOPE": 28, + "RESP_CODE_DEVICE_INFO": 13, + "RESP_CODE_DISABLED": 15, + "RESP_CODE_END_OF_CONTACTS": 4, + "RESP_CODE_ERR": 1, + "RESP_CODE_EXPORT_CONTACT": 11, + "RESP_CODE_NO_MORE_MESSAGES": 10, + "RESP_CODE_OK": 0, + "RESP_CODE_PRIVATE_KEY": 14, + "RESP_CODE_SELF_INFO": 5, + "RESP_CODE_SENT": 6, + "RESP_CODE_SIGNATURE": 20, + "RESP_CODE_SIGN_START": 19, + "RESP_CODE_STATS": 24, + "RESP_CODE_TUNING_PARAMS": 23 + } +} diff --git a/test/support/fake_radio/manifests/stock-companion-v1.17.1.json b/test/support/fake_radio/manifests/stock-companion-v1.17.1.json new file mode 100644 index 00000000..a00322e0 --- /dev/null +++ b/test/support/fake_radio/manifests/stock-companion-v1.17.1.json @@ -0,0 +1,133 @@ +{ + "commands": { + "CMD_ADD_UPDATE_CONTACT": 9, + "CMD_APP_START": 1, + "CMD_DEVICE_QUERY": 22, + "CMD_EXPORT_CONTACT": 17, + "CMD_EXPORT_PRIVATE_KEY": 23, + "CMD_FACTORY_RESET": 51, + "CMD_GET_ADVERT_PATH": 42, + "CMD_GET_ALLOWED_REPEAT_FREQ": 60, + "CMD_GET_AUTOADD_CONFIG": 59, + "CMD_GET_BATT_AND_STORAGE": 20, + "CMD_GET_CHANNEL": 31, + "CMD_GET_CONTACTS": 4, + "CMD_GET_CONTACT_BY_KEY": 30, + "CMD_GET_CUSTOM_VARS": 40, + "CMD_GET_DEFAULT_FLOOD_SCOPE": 64, + "CMD_GET_DEVICE_TIME": 5, + "CMD_GET_STATS": 56, + "CMD_GET_TUNING_PARAMS": 43, + "CMD_HAS_CONNECTION": 28, + "CMD_IMPORT_CONTACT": 18, + "CMD_IMPORT_PRIVATE_KEY": 24, + "CMD_LOGOUT": 29, + "CMD_REBOOT": 19, + "CMD_REMOVE_CONTACT": 15, + "CMD_RESET_PATH": 13, + "CMD_SEND_ANON_REQ": 57, + "CMD_SEND_BINARY_REQ": 50, + "CMD_SEND_CHANNEL_DATA": 62, + "CMD_SEND_CHANNEL_TXT_MSG": 3, + "CMD_SEND_CONTROL_DATA": 55, + "CMD_SEND_LOGIN": 26, + "CMD_SEND_PATH_DISCOVERY_REQ": 52, + "CMD_SEND_RAW_DATA": 25, + "CMD_SEND_RAW_PACKET": 65, + "CMD_SEND_SELF_ADVERT": 7, + "CMD_SEND_STATUS_REQ": 27, + "CMD_SEND_TELEMETRY_REQ": 39, + "CMD_SEND_TRACE_PATH": 36, + "CMD_SEND_TXT_MSG": 2, + "CMD_SET_ADVERT_LATLON": 14, + "CMD_SET_ADVERT_NAME": 8, + "CMD_SET_AUTOADD_CONFIG": 58, + "CMD_SET_CHANNEL": 32, + "CMD_SET_CUSTOM_VAR": 41, + "CMD_SET_DEFAULT_FLOOD_SCOPE": 63, + "CMD_SET_DEVICE_PIN": 37, + "CMD_SET_DEVICE_TIME": 6, + "CMD_SET_FLOOD_SCOPE_KEY": 54, + "CMD_SET_OTHER_PARAMS": 38, + "CMD_SET_PATH_HASH_MODE": 61, + "CMD_SET_RADIO_PARAMS": 11, + "CMD_SET_RADIO_TX_POWER": 12, + "CMD_SET_TUNING_PARAMS": 21, + "CMD_SHARE_CONTACT": 16, + "CMD_SIGN_DATA": 34, + "CMD_SIGN_FINISH": 35, + "CMD_SIGN_START": 33, + "CMD_SYNC_NEXT_MESSAGE": 10 + }, + "errors": { + "ERR_CODE_BAD_STATE": 4, + "ERR_CODE_FILE_IO_ERROR": 5, + "ERR_CODE_ILLEGAL_ARG": 6, + "ERR_CODE_NOT_FOUND": 2, + "ERR_CODE_TABLE_FULL": 3, + "ERR_CODE_UNSUPPORTED_CMD": 1 + }, + "firmware": { + "commit": "d92964352441e53b93e8667b802e04f6e072b39e", + "firmware_ver_code": 13, + "firmware_version": "v1.17.1", + "flavor": "stock", + "ref": "companion-v1.17.1" + }, + "format": "offband-protocol-manifest", + "format_version": 1, + "generated_by": "meshcore-client tool/fake_radio/gen_protocol_manifest.py", + "offband_caps": {}, + "offband_caps2": {}, + "offband_commands": {}, + "offband_responses": {}, + "pushes": { + "PUSH_CODE_ADVERT": 128, + "PUSH_CODE_BINARY_RESPONSE": 140, + "PUSH_CODE_CONTACTS_FULL": 144, + "PUSH_CODE_CONTACT_DELETED": 143, + "PUSH_CODE_CONTROL_DATA": 142, + "PUSH_CODE_LOGIN_FAIL": 134, + "PUSH_CODE_LOGIN_SUCCESS": 133, + "PUSH_CODE_LOG_RX_DATA": 136, + "PUSH_CODE_MSG_WAITING": 131, + "PUSH_CODE_NEW_ADVERT": 138, + "PUSH_CODE_PATH_DISCOVERY_RESPONSE": 141, + "PUSH_CODE_PATH_UPDATED": 129, + "PUSH_CODE_RAW_DATA": 132, + "PUSH_CODE_SEND_CONFIRMED": 130, + "PUSH_CODE_STATUS_RESPONSE": 135, + "PUSH_CODE_TELEMETRY_RESPONSE": 139, + "PUSH_CODE_TRACE_DATA": 137 + }, + "responses": { + "RESP_CODE_ADVERT_PATH": 22, + "RESP_CODE_AUTOADD_CONFIG": 25, + "RESP_CODE_BATT_AND_STORAGE": 12, + "RESP_CODE_CHANNEL_DATA_RECV": 27, + "RESP_CODE_CHANNEL_INFO": 18, + "RESP_CODE_CHANNEL_MSG_RECV": 8, + "RESP_CODE_CHANNEL_MSG_RECV_V3": 17, + "RESP_CODE_CONTACT": 3, + "RESP_CODE_CONTACTS_START": 2, + "RESP_CODE_CONTACT_MSG_RECV": 7, + "RESP_CODE_CONTACT_MSG_RECV_V3": 16, + "RESP_CODE_CURR_TIME": 9, + "RESP_CODE_CUSTOM_VARS": 21, + "RESP_CODE_DEFAULT_FLOOD_SCOPE": 28, + "RESP_CODE_DEVICE_INFO": 13, + "RESP_CODE_DISABLED": 15, + "RESP_CODE_END_OF_CONTACTS": 4, + "RESP_CODE_ERR": 1, + "RESP_CODE_EXPORT_CONTACT": 11, + "RESP_CODE_NO_MORE_MESSAGES": 10, + "RESP_CODE_OK": 0, + "RESP_CODE_PRIVATE_KEY": 14, + "RESP_CODE_SELF_INFO": 5, + "RESP_CODE_SENT": 6, + "RESP_CODE_SIGNATURE": 20, + "RESP_CODE_SIGN_START": 19, + "RESP_CODE_STATS": 24, + "RESP_CODE_TUNING_PARAMS": 23 + } +} diff --git a/tool/fake_radio/gen_protocol_manifest.py b/tool/fake_radio/gen_protocol_manifest.py new file mode 100644 index 00000000..5449b343 --- /dev/null +++ b/tool/fake_radio/gen_protocol_manifest.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""Generate a protocol manifest from MeshCore companion firmware source. + +Bootstrap for the fake radio (#769, Feature #755) until +OffbandMesh/meshcore-firmware#1318 publishes manifests from firmware CI. +Reads the source at a git ref (no checkout) and prints JSON to stdout. + + python tool/fake_radio/gen_protocol_manifest.py \ + --repo C:/Dev/meshcore-firmware --ref offband-v1.5.0-beta7 \ + --flavor offband > test/support/fake_radio/manifests/offband-v1.5.0-beta7.json + +Stock: --ref companion-v1.17.1 --flavor stock (upstream meshcore-dev/MeshCore +tag, fetched into the clone). +""" +import argparse +import json +import re +import subprocess +import sys + +COMPANION = "examples/companion_radio" +DEFINE = re.compile(r"^#define\s+([A-Z0-9_]+)\s+(0x[0-9A-Fa-f]+|\d+)\b", re.M) +CONSTEXPR = re.compile( + r"^constexpr\s+uint8_t\s+([A-Z0-9_]+)\s*=\s*(0x[0-9A-Fa-f]+|\d+)\s*;", re.M +) +VER_CODE = re.compile(r"^#define\s+FIRMWARE_VER_CODE\s+(\d+)", re.M) +VERSION = re.compile(r'^#define\s+FIRMWARE_VERSION\s+"([^"]+)"', re.M) + + +def show(repo, ref, path): + out = subprocess.run( + ["git", "-C", repo, "show", f"{ref}:{path}"], + capture_output=True, + text=True, + encoding="utf-8", + ) + if out.returncode != 0: + return None + return out.stdout + + +def pick(pairs, prefix): + return {name: int(value, 0) for name, value in pairs if name.startswith(prefix)} + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--repo", required=True) + ap.add_argument("--ref", required=True) + ap.add_argument("--flavor", choices=["offband", "stock"], required=True) + args = ap.parse_args() + + header = show(args.repo, args.ref, f"{COMPANION}/MyMesh.h") + source = show(args.repo, args.ref, f"{COMPANION}/MyMesh.cpp") + if header is None or source is None: + sys.exit(f"can't read {COMPANION} at {args.ref}") + commit = subprocess.run( + ["git", "-C", args.repo, "rev-parse", f"{args.ref}^{{commit}}"], + capture_output=True, + text=True, + ).stdout.strip() + + defines = DEFINE.findall(source) + pairs = list(defines) + offband = show(args.repo, args.ref, f"{COMPANION}/OffbandConfigProtocol.h") + if args.flavor == "offband": + if offband is None: + sys.exit("offband flavor but OffbandConfigProtocol.h is missing") + pairs += CONSTEXPR.findall(offband) + DEFINE.findall(offband) + + commands = pick(pairs, "CMD_") + responses = pick(pairs, "RESP_CODE_") + manifest = { + "format": "offband-protocol-manifest", + "format_version": 1, + "generated_by": "meshcore-client tool/fake_radio/gen_protocol_manifest.py", + "firmware": { + "flavor": args.flavor, + "ref": args.ref, + "commit": commit, + "firmware_version": VERSION.search(header).group(1), + "firmware_ver_code": int(VER_CODE.search(header).group(1)), + }, + "commands": {k: v for k, v in commands.items() if "OFFBAND" not in k}, + "responses": {k: v for k, v in responses.items() if "OFFBAND" not in k}, + "pushes": pick(pairs, "PUSH_CODE_"), + "errors": pick(pairs, "ERR_CODE_"), + "offband_commands": {k: v for k, v in commands.items() if "OFFBAND" in k}, + "offband_responses": {k: v for k, v in responses.items() if "OFFBAND" in k}, + "offband_caps": { + k: v for k, v in pick(pairs, "OFFBAND_CAP_").items() + }, + "offband_caps2": pick(pairs, "OFFBAND_CAP2_"), + } + print(json.dumps(manifest, indent=2, sort_keys=True)) + + +if __name__ == "__main__": + main()