diff --git a/lib/connector/device_population.dart b/lib/connector/device_population.dart index b3802972..7713d748 100644 --- a/lib/connector/device_population.dart +++ b/lib/connector/device_population.dart @@ -14,6 +14,8 @@ import 'dart:typed_data'; +import 'offband_capability_advert.dart'; + /// Which firmware family the connected radio belongs to, as far as wire /// behavior is concerned. enum DevicePopulation { @@ -56,12 +58,41 @@ class Classification { /// cap-bit gates. bool get allowsOffbandFrames => population == DevicePopulation.offband; - /// CMD_RUN_CLI_COMMAND (66) may be emitted. Deliberately false for - /// [DevicePopulation.offband] on the current fork base; the absorb flips - /// this at the D1 seam together with the durable marker. + /// The STOCK door to CMD_RUN_CLI_COMMAND (66): a stock 1.18+ radio has it by + /// definition. False for [DevicePopulation.offband], whose CLI is advertised + /// per radio by tags `0x06`/`0x07` instead of a device-info marker (#1155). + /// Gate on [resolveCliSupport], never on this alone. bool get allowsCliFrame => population == DevicePopulation.stockModern; } +/// Which CLI surfaces this radio supports (#1155, firmware #2186). +/// +/// - Stock 1.18+: both, from the classifier alone. A stock radio is never sent +/// the advert request, so it has no tags to read. +/// - Offband: each surface only when its advert tag is PRESENT. No advert yet, +/// a radio that predates the advert, or an advert without the tag all mean +/// hidden, which is what every Offband radio had before the tags existed. +/// - Anything else, including no classification yet: neither. +/// +/// Tags are read only for an Offband radio, so nothing opens the CLI on a radio +/// the classifier could not place. +({bool local, bool remote}) resolveCliSupport({ + required Classification? classification, + required OffbandCapabilityAdvert? advert, +}) { + final population = classification?.population; + if (population == DevicePopulation.stockModern) { + return (local: true, remote: true); + } + if (population != DevicePopulation.offband || advert == null) { + return (local: false, remote: false); + } + return ( + local: advert.has(offbandInfoTagCliLocal), + remote: advert.has(offbandInfoTagCliRemote), + ); +} + /// Stock device-info replies are exactly this long; the first fork byte /// (offband_caps) sits at this offset. [verified: CMD_DEVICE_QUERY reply /// construction at companion-v1.18.0 -82 bytes, unchanged from 1.17.x] diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index d4abb7a5..5caf8ec3 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -1195,6 +1195,20 @@ class MeshCoreConnector extends ChangeNotifier { /// has anything to show (#523, #1132). bool get supportsIndicators => supportsDisplayControl || supportsLedControl; + /// Whether this radio runs CLI lines from its own client (frames 66/29): + /// stock 1.18+, or Offband advertising `0x06` (#1155). + bool get supportsLocalCli => resolveCliSupport( + classification: _classification, + advert: _capabilityAdvert, + ).local; + + /// Whether this radio sends remote CLI (TXT type 3) and honors the contact + /// remote-CLI grant: stock 1.18+, or Offband advertising `0x07` (#1155). + bool get supportsRemoteCli => resolveCliSupport( + classification: _classification, + advert: _capabilityAdvert, + ).remote; + /// The LED and display state the radio last reported, from device-info or a /// reply. Null when it has not said. bool? get ledEnabled => _ledEnabled; @@ -1500,6 +1514,8 @@ class MeshCoreConnector extends ChangeNotifier { ' rotation=$supportsDisplayRotation' ' display=$supportsDisplayControl' ' led=$supportsLedControl' + ' cliLocal=$supportsLocalCli' + ' cliRemote=$supportsRemoteCli' ' ackCapacity=${ackCapacity ?? 'absent'}', tag: 'DeviceUI', ); diff --git a/lib/connector/offband_capability_advert.dart b/lib/connector/offband_capability_advert.dart index 955c79ae..9a2dae3a 100644 --- a/lib/connector/offband_capability_advert.dart +++ b/lib/connector/offband_capability_advert.dart @@ -40,6 +40,15 @@ const int offbandInfoTagAckCapacity = 0x03; /// bit. Values are read over `0x05`/`0x06` and `0x07`/`0x08`. const int offbandInfoTagDisplayControl = 0x04; const int offbandInfoTagLedControl = 0x05; + +/// The 1.18 companion CLI (firmware #2211, merged `8d94ef78`; emitted by +/// #2213). `0x06` CLI_LOCAL: this radio's own client can run CLI lines +/// (`CMD_RUN_CLI_COMMAND` 66, `RESP_CODE_CLI_REPLY` 29). `0x07` CLI_REMOTE: it +/// sends `TXT_TYPE_CLI_COMMAND` (3) for the client, and runs one from a contact +/// granted remote CLI (contact flag `0x10`). Gated by `resolveCliSupport` in +/// `device_population.dart`. +const int offbandInfoTagCliLocal = 0x06; +const int offbandInfoTagCliRemote = 0x07; const int offbandInfoTagContinue = 0xFF; /// `0x00` is not a valid tag. diff --git a/test/connector/cli_advert_gate_test.dart b/test/connector/cli_advert_gate_test.dart new file mode 100644 index 00000000..94d29e06 --- /dev/null +++ b/test/connector/cli_advert_gate_test.dart @@ -0,0 +1,244 @@ +// #1156 (Epic #1155, firmware #2186 / #2213): an Offband radio offers the CLI +// only when its 0xC5 0x1C advert carries the tag. 0x06 CLI_LOCAL opens the +// Device CLI console (frames 66/29); 0x07 CLI_REMOTE opens remote CLI (TXT type +// 3) and the per-contact permission switch. A present tag is authoritative; an +// advert without it, a radio that predates the advert, and an advert still in +// flight all leave the CLI hidden, which is what every Offband radio had before. +// Stock 1.18 is unchanged: the classifier alone opens it. + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:drift/native.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/device_population.dart'; +import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/connector/offband_capability_advert.dart'; +import 'package:meshcore_open/connector/offband_device_ui.dart'; +import 'package:meshcore_open/storage/drift/blob_store.dart'; +import 'package:meshcore_open/storage/drift/offband_database.dart'; +import 'package:meshcore_open/storage/prefs_manager.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +/// An Offband device-info reply: fork length, dual version string. +Uint8List offbandDeviceInfo() { + final v = ascii.encode('1.8.0-1.18.0'); + final f = Uint8List(87); + f[0] = 13; + f[1] = 22; + f.setRange(60, 60 + v.length, v); + return f; +} + +/// A stock 1.18 device-info reply: exactly 82 bytes, ver 14. +Uint8List stockDeviceInfo({int verCode = 14}) { + final v = ascii.encode('v1.18.0'); + final f = Uint8List(stockDeviceInfoLength); + f[0] = 13; + f[1] = verCode; + f.setRange(60, 60 + v.length, v); + return f; +} + +/// A `0xC5` `0x1C` advert reply carrying [tags] as zero-length records. +Uint8List advert(List tags) => Uint8List.fromList([ + cmdOffbandDeviceUi, + offbandUiInfoGet, + for (final t in tags) ...[t, 0x00], +]); + +/// `[0xC5][0x7F][0x05]`: this radio predates the advert. +Uint8List advertUnsupported() => Uint8List.fromList([ + cmdOffbandDeviceUi, + offbandUiErr, + offbandUiErrReasonMalformed, +]); + +const offband = Classification( + population: DevicePopulation.offband, + conflict: false, + evidence: 'test', +); +const stockModern = Classification( + population: DevicePopulation.stockModern, + conflict: false, + evidence: 'test', +); +const stockLegacy = Classification( + population: DevicePopulation.stockLegacy, + conflict: false, + evidence: 'test', +); + +OffbandCapabilityAdvert parsed(List tags) => + parseCapabilityAdvertReply(advert(tags))!; + +void main() { + test('the tag numbers are the ones claimed on firmware-base (8d94ef78)', () { + expect(offbandInfoTagCliLocal, 0x06); + expect(offbandInfoTagCliRemote, 0x07); + }); + + group('resolveCliSupport', () { + test('stock 1.18: both, with no advert at all', () { + expect(resolveCliSupport(classification: stockModern, advert: null), ( + local: true, + remote: true, + )); + }); + + test('Offband with both tags: both', () { + expect( + resolveCliSupport( + classification: offband, + advert: parsed([offbandInfoTagCliLocal, offbandInfoTagCliRemote]), + ), + (local: true, remote: true), + ); + }); + + test('Offband with 0x06 only: local, not remote', () { + expect( + resolveCliSupport( + classification: offband, + advert: parsed([offbandInfoTagCliLocal]), + ), + (local: true, remote: false), + ); + }); + + test('Offband with 0x07 only: remote, not local', () { + expect( + resolveCliSupport( + classification: offband, + advert: parsed([offbandInfoTagCliRemote]), + ), + (local: false, remote: true), + ); + }); + + test('Offband advert without either tag: neither (today\'s behavior)', () { + expect( + resolveCliSupport( + classification: offband, + advert: parsed([offbandInfoTagChanNotify, offbandInfoTagLedControl]), + ), + (local: false, remote: false), + ); + }); + + test('Offband with no advert (in flight, or predates it): neither', () { + expect(resolveCliSupport(classification: offband, advert: null), ( + local: false, + remote: false, + )); + }); + + test('stock legacy: neither, even if an advert carried the tags', () { + // Tags are read only for an Offband radio. Fail closed everywhere else. + expect( + resolveCliSupport( + classification: stockLegacy, + advert: parsed([offbandInfoTagCliLocal, offbandInfoTagCliRemote]), + ), + (local: false, remote: false), + ); + }); + + test('not classified yet: neither', () { + expect(resolveCliSupport(classification: null, advert: null), ( + local: false, + remote: false, + )); + }); + }); + + group('connector', () { + late OffbandDatabase db; + late MeshCoreConnector connector; + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + db = OffbandDatabase(NativeDatabase.memory()); + BlobStore.overrideForTest(BlobStore(db)); + connector = MeshCoreConnector(); + connector.sendFrameOverrideForTest = (_) {}; + connector.setConnectedForTest(); + }); + + tearDown(() async { + BlobStore.clearTestOverride(); + await db.close(); + }); + + test('Offband radio advertising both tags: both consoles', () { + connector.handleFrameForTest(offbandDeviceInfo()); + connector.handleFrameForTest( + advert([offbandInfoTagCliLocal, offbandInfoTagCliRemote]), + ); + + expect(connector.supportsLocalCli, isTrue); + expect(connector.supportsRemoteCli, isTrue); + }); + + test('Offband radio, advert without the tags: neither', () { + connector.handleFrameForTest(offbandDeviceInfo()); + connector.handleFrameForTest(advert([offbandInfoTagDisplayRotation])); + + expect(connector.supportsLocalCli, isFalse); + expect(connector.supportsRemoteCli, isFalse); + }); + + test('Offband radio, advert not answered yet: neither', () { + connector.handleFrameForTest(offbandDeviceInfo()); + + expect(connector.capabilityAdvertState, CapabilityAdvertState.unknown); + expect(connector.supportsLocalCli, isFalse); + expect(connector.supportsRemoteCli, isFalse); + }); + + test('Offband radio that predates the advert: neither', () { + connector.handleFrameForTest(offbandDeviceInfo()); + connector.handleFrameForTest(advertUnsupported()); + + expect( + connector.capabilityAdvertState, + CapabilityAdvertState.unsupported, + ); + expect(connector.supportsLocalCli, isFalse); + expect(connector.supportsRemoteCli, isFalse); + }); + + test('stock 1.18 radio: both, unchanged', () { + connector.handleFrameForTest(stockDeviceInfo()); + + expect(connector.supportsLocalCli, isTrue); + expect(connector.supportsRemoteCli, isTrue); + }); + + test('stock radio below 1.18: neither, unchanged', () { + connector.handleFrameForTest(stockDeviceInfo(verCode: 13)); + + expect(connector.supportsLocalCli, isFalse); + expect(connector.supportsRemoteCli, isFalse); + }); + + test('the next radio does not inherit the previous radio\'s tags', () { + connector.handleFrameForTest(offbandDeviceInfo()); + connector.handleFrameForTest( + advert([offbandInfoTagCliLocal, offbandInfoTagCliRemote]), + ); + expect(connector.supportsLocalCli, isTrue); + + // A new device-info drops the old advert, so this radio is gated on its + // own answer, and it predates the advert. + connector.handleFrameForTest(offbandDeviceInfo()); + connector.handleFrameForTest(advertUnsupported()); + + expect(connector.supportsLocalCli, isFalse); + expect(connector.supportsRemoteCli, isFalse); + }); + }); +}