diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 454f00a6..38289a74 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -66,6 +66,7 @@ import 'device_population.dart'; import 'meshcore_protocol.dart'; import '../helpers/region_key.dart'; import '../storage/channel_region_scope_store.dart'; +import 'offband_channel_notify.dart'; import 'offband_device_ui.dart'; import 'offband_screen_settings.dart'; import 'offband_tones.dart'; @@ -419,6 +420,7 @@ class MeshCoreConnector extends ChangeNotifier { Classification? _classification; int? _offbandCaps2; + int? _offbandCaps3; bool? _femLnaEnabled; ButtonMatrix? _buttonMatrix; String? _lastDeviceUiSuppression; @@ -428,6 +430,12 @@ class MeshCoreConnector extends ChangeNotifier { DeviceUiSurface? _pendingDeviceUiWrite; GpsModeState? _gpsModeState; final Map _channelTunes = {}; + + /// Slot index to the radio's own notify mode for that channel (#544). + /// Written only from a device reply, and cleared per radio like every other + /// device-UI surface. Slot-derived state: #1061's identity diff must reset + /// it when a slot's PSK changes, or it describes the wrong channel. + final Map _channelNotifyModes = {}; final Map _eventTones = {}; final Map _tuneSlots = {}; ScreenSettingsState? _screenSettings; @@ -936,6 +944,17 @@ class MeshCoreConnector extends ChangeNotifier { /// an error: every radio predating #508 reports null here. (#480) int? get offbandCaps2 => _offbandCaps2; + /// Third `offband_caps` bitfield (frame offset 87, #544). Null when the frame + /// is too short, which means "no byte-3 capabilities" and is never an error. + /// No bit is allocated from it yet; byte 2 filled up at `0x80`. + int? get offbandCaps3 => _offbandCaps3; + + /// Whether this radio stores a per-channel notify mode for its own buzzer + /// (#544). Says nothing about the app's per-channel notify setting, which is + /// independent and needs no radio. + bool get supportsChannelNotify => + firmwareSupportsChannelNotify(_offbandCaps2); + /// Whether this radio exposes a configurable button-action matrix (#474). bool get supportsButtonMatrix => firmwareSupportsButtonMatrix(_offbandCaps2); @@ -960,6 +979,12 @@ class MeshCoreConnector extends ChangeNotifier { /// The tune the radio reported for [channel], or null if not read yet. ChannelTune? channelTuneFor(int channel) => _channelTunes[channel]; + /// The radio's notify mode for [channel], or null until the radio has + /// reported it. Null is "not yet known", distinct from + /// [RadioChannelNotify.unset], which is the radio saying no choice is set. + RadioChannelNotify? channelNotifyFor(int channel) => + _channelNotifyModes[channel]; + /// The tune the radio reported for [event], or null if not read yet. EventToneState? eventTone(ToneEvent event) => _eventTones[event]; @@ -1147,6 +1172,39 @@ class MeshCoreConnector extends ChangeNotifier { await sendFrame(buildChannelTuneSetFrame(channel, tune)); } + /// Ask the radio which notify mode [channel] has for its own buzzer (#544). + /// + /// Nothing here touches the app's per-channel notify setting: that drives + /// the phone, this drives the radio, and they are independent. + Future requestChannelNotify(int channel) async { + if (!_deviceUiGateOpen('chnotify GET', advertised: supportsChannelNotify)) { + return; + } + _appDebugLogService?.info( + 'chnotify GET -> [0xC5][0x18][$channel]', + tag: 'DeviceUI', + ); + await sendFrame(buildChannelNotifyGetFrame(channel)); + } + + /// Set the radio's notify mode for [channel]. Local state follows the + /// device's re-read reply, never the request, so a refused choice is never + /// shown as saved. + Future setChannelNotify(int channel, RadioChannelNotify mode) async { + if (!_deviceUiGateOpen('chnotify SET', advertised: supportsChannelNotify)) { + return; + } + _deviceUiError = null; + _deviceUiErrorSurface = null; + _pendingDeviceUiWrite = DeviceUiSurface.channelNotify; + _appDebugLogService?.info( + 'chnotify SET -> ch=$channel mode=${mode.name} ' + 'stored=${mode.stored} [0xC5][0x19]', + tag: 'DeviceUI', + ); + await sendFrame(buildChannelNotifySetFrame(channel, mode)); + } + /// Ask the radio how GPS is running (#969). Future requestGpsMode() async { if (!_deviceUiGateOpen('gps GET', advertised: supportsGpsMode)) return; @@ -1487,6 +1545,7 @@ class MeshCoreConnector extends ChangeNotifier { _buttonMatrix = null; _lastDeviceUiSuppression = null; _deviceNotifyScope = null; + _channelNotifyModes.clear(); _deviceUiError = null; _deviceUiErrorSurface = null; _pendingDeviceUiWrite = null; @@ -1532,6 +1591,14 @@ class MeshCoreConnector extends ChangeNotifier { _handleChannelTuneReply(tune); return; } + final chNotify = parseChannelNotifyReply(frame); + if (chNotify != null) { + _handleChannelNotifyReply( + chNotify, + isWrite: frame[1] == offbandUiChannelNotifySet, + ); + return; + } final tunes = parseTuneReply(frame); if (tunes != null) { _handleTuneReply(tunes); @@ -1570,6 +1637,7 @@ class MeshCoreConnector extends ChangeNotifier { DeviceUiSurface.tunePlay => 'Tune preview', DeviceUiSurface.tuneSlot => 'Tune slot', DeviceUiSurface.screenSettings => 'Screen settings', + DeviceUiSurface.channelNotify => 'Radio channel notifications', null => 'Device UI request', }; _appDebugLogService?.warn( @@ -1624,6 +1692,27 @@ class MeshCoreConnector extends ChangeNotifier { notifyListeners(); } + /// The radio reported its own notify mode for one channel (#544). + /// + /// This writes the RADIO's state only. It deliberately does not read or + /// write the app's per-channel notify setting: the two are independent, and + /// a test asserts that a reply here leaves the app store untouched. + void _handleChannelNotifyReply( + RadioChannelNotifyReply reply, { + required bool isWrite, + }) { + if (isWrite && _pendingDeviceUiWrite == DeviceUiSurface.channelNotify) { + _pendingDeviceUiWrite = null; + } + _channelNotifyModes[reply.channel] = reply.mode; + _appDebugLogService?.info( + 'chnotify reply: ch=${reply.channel} mode=${reply.mode.name} ' + '(${isWrite ? 'write confirmed' : 'read'})', + tag: 'DeviceUI', + ); + notifyListeners(); + } + void _handleChannelTuneReply(OffbandUiReply reply) { final t = reply.channelTune; if (t == null) { @@ -4402,6 +4491,7 @@ class MeshCoreConnector extends ChangeNotifier { // until the next device-info lands (Gemini review, task #876). _offbandCaps = null; _offbandCaps2 = null; + _offbandCaps3 = null; _femLnaEnabled = null; _blockListRetryTimer?.cancel(); // Per-connection debounce timers must die with the connection so a timer @@ -7878,6 +7968,22 @@ class MeshCoreConnector extends ChangeNotifier { static int? parseOffbandCaps2(Uint8List frame) => frame.length >= 85 ? frame[84] : null; + /// Third `offband_caps` byte, at frame offset **87** (#544). + /// + /// Byte 2 filled up when the per-channel notify bit took `0x80`, so firmware + /// appends a third. It sits at 87 and NOT at 85, because 85 and 86 are + /// already `ui_led_enabled` and `ui_display_mode`: the same fixed-absolute + /// offset rule that put byte 2 at 84 rather than beside byte 1. Reading 85 + /// as byte 3 would parse the LED state as a capability bitmask. + /// + /// Null on any firmware sending a shorter frame, which means "no byte-3 + /// capabilities" and is never an error. Length-gated, never version-gated + /// (#1065). No bit is allocated from it yet. + static int? parseOffbandCaps3(Uint8List frame) => + frame.length >= offbandCaps3MinFrameLength + ? frame[offbandCaps3Offset] + : null; + void _handleDeviceInfo(Uint8List frame) { if (frame.length < 4) return; if (_shouldGateInitialChannelSync) { @@ -7947,10 +8053,14 @@ class MeshCoreConnector extends ChangeNotifier { _femLnaEnabled = parseFemLnaState(frame); // Second caps byte rides at offset 84, past the FEM state byte (#480). _offbandCaps2 = parseOffbandCaps2(frame); + // Third caps byte at offset 87, past ui_led_enabled (85) and + // ui_display_mode (86). Byte 2 filled up at 0x80 (#544). + _offbandCaps3 = parseOffbandCaps3(frame); } else { _offbandCaps = null; _femLnaEnabled = null; _offbandCaps2 = null; + _offbandCaps3 = null; } // Capability-gated features are invisible when a bit is clear, which looks // identical to a bug. Log the raw inputs so "the toggle didn't appear" can diff --git a/lib/connector/meshcore_protocol.dart b/lib/connector/meshcore_protocol.dart index ceedb35c..dd5d1fba 100644 --- a/lib/connector/meshcore_protocol.dart +++ b/lib/connector/meshcore_protocol.dart @@ -649,6 +649,28 @@ const int offbandCap2GpsMode = 0x20; /// (#987): text sizes, Screen off, time zone, battery full point. const int offbandCap2ScreenSettings = 0x40; +/// Per-channel RADIO notify mode over `0xC5` 0x18 / 0x19 (#544, +/// meshcore-firmware#606). Firmware sets it where `PIN_BUZZER` is defined, so +/// it is a per-unit answer: gate on the BIT, never on model or version. +/// +/// This is the LAST bit in caps byte 2. The change that spends it adds caps +/// **byte 3** at device-info offset 87 ([parseOffbandCaps3Offset]); anything +/// after this takes a bit from there. +const int offbandCap2ChannelNotify = 0x80; + +/// Device-info offset of `offband_caps` BYTE 3, and the minimum frame length +/// that carries it. +/// +/// **87, not 85.** Firmware writes caps byte 2 at 84, then `ui_led_enabled` at +/// 85 and `ui_display_mode` at 86 (`MyMesh.cpp` device-info builder). Reading +/// byte 3 at 85 would parse the LED state as a capability bitmask, which is +/// the exact mistake firmware's own comment warns about for byte 2. +/// +/// Length-gated, never version-gated (#1065): absence means no byte-3 +/// capabilities, and is never an error. +const int offbandCaps3Offset = 87; +const int offbandCaps3MinFrameLength = 88; + /// True iff this radio answers the `0xC5` tune sub-codes. bool firmwareSupportsTunes(int? offbandCaps2) => offbandCaps2 != null && (offbandCaps2 & offbandCap2Tunes) != 0; @@ -661,6 +683,14 @@ bool firmwareSupportsScreenSettings(int? offbandCaps2) => bool firmwareSupportsGpsMode(int? offbandCaps2) => offbandCaps2 != null && (offbandCaps2 & offbandCap2GpsMode) != 0; +/// True iff this radio stores a per-channel notify mode and answers the +/// `0xC5` 0x18 / 0x19 sub-codes (#544). +/// +/// Says nothing about the app's own per-channel notify setting, which needs no +/// radio and is never gated on this. +bool firmwareSupportsChannelNotify(int? offbandCaps2) => + offbandCaps2 != null && (offbandCaps2 & offbandCap2ChannelNotify) != 0; + bool firmwareSupportsOffbandCaplog(int? offbandCaps) => offbandCaps != null && (offbandCaps & offbandCapCaplog) != 0; diff --git a/lib/connector/offband_channel_notify.dart b/lib/connector/offband_channel_notify.dart new file mode 100644 index 00000000..6a7174e6 --- /dev/null +++ b/lib/connector/offband_channel_notify.dart @@ -0,0 +1,98 @@ +import 'dart:typed_data'; + +import 'offband_device_ui.dart'; + +/// Per-channel notification mode stored on the RADIO, over `0xC5` 0x18 / 0x19 +/// (#544, meshcore-firmware#606). +/// +/// This governs the RADIO's buzzer and nothing else. It is deliberately +/// independent of the app's own per-channel `ChannelNotifyMode`, which governs +/// whether the PHONE notifies: two settings, two outputs, no bridging between +/// them (owner, 2026-11-03: "they are separate controls and separate +/// notifications... they are independent settings"). Never resolve one from +/// the other, and never offer an "inherit the app setting" value here. +/// +/// Firmware is the authority and there is no firmware inherit concept: the +/// owner's ruling was "Inherit is on the client side, not on the firmware +/// side. How would firmware inherit from firmware?". [unset] is simply the +/// absence of a per-channel choice, and the radio's own device-wide scope +/// answers for that channel instead. +const int offbandUiChannelNotifyGet = 0x18; +const int offbandUiChannelNotifySet = 0x19; + +/// A channel's radio notify mode, as the radio stores it. +/// +/// The byte is **1-based** and 0 means "no choice made", because the value +/// lives in a field that reads zero on every existing record and every fresh +/// channel. If 0 meant a real mode, every untouched channel in the field would +/// silently adopt it. The shipped `ChannelTune` byte uses the same convention +/// for the same reason, so the two are consistent on the wire. +enum RadioChannelNotify { + /// No per-channel choice: the radio's device-wide scope answers instead. + unset(0x00), + + /// Beep for any message on this channel. + all(0x01), + + /// Beep only when the message names us, by the `@[name]` rule. + mentions(0x02), + + /// Never beep for this channel. + off(0x03); + + const RadioChannelNotify(this.stored); + + /// The wire byte. Never built by hand. + final int stored; + + static RadioChannelNotify? fromStored(int stored) { + for (final m in RadioChannelNotify.values) { + if (m.stored == stored) return m; + } + return null; + } +} + +/// One channel's radio notify mode, as a `0xC5` 0x18 or 0x19 reply reports it. +class RadioChannelNotifyReply { + const RadioChannelNotifyReply({required this.channel, required this.mode}); + final int channel; + final RadioChannelNotify mode; +} + +int _channelByte(int channel) { + if (channel < 0 || channel > 0xFF) { + throw ArgumentError.value(channel, 'channel', 'must be 0 to 255'); + } + return channel; +} + +Uint8List buildChannelNotifyGetFrame(int channel) => Uint8List.fromList([ + cmdOffbandDeviceUi, + offbandUiChannelNotifyGet, + _channelByte(channel), +]); + +Uint8List buildChannelNotifySetFrame(int channel, RadioChannelNotify mode) => + Uint8List.fromList([ + cmdOffbandDeviceUi, + offbandUiChannelNotifySet, + _channelByte(channel), + mode.stored, + ]); + +/// Parse a `0xC5` channel-notify reply (0x18 or 0x19). Null when this is not +/// one, when it is short, or when the mode byte is not a value we know: an +/// unrecognized mode is dropped rather than guessed at, so a firmware that +/// grows a fourth mode cannot have it read as one of ours. +RadioChannelNotifyReply? parseChannelNotifyReply(List frame) { + if (frame.length < 4) return null; + if (frame[0] != respCodeOffbandDeviceUi) return null; + if (frame[1] != offbandUiChannelNotifyGet && + frame[1] != offbandUiChannelNotifySet) { + return null; + } + final mode = RadioChannelNotify.fromStored(frame[3]); + if (mode == null) return null; + return RadioChannelNotifyReply(channel: frame[2], mode: mode); +} diff --git a/lib/connector/offband_device_ui.dart b/lib/connector/offband_device_ui.dart index 8a5ba6bb..2e889d7d 100644 --- a/lib/connector/offband_device_ui.dart +++ b/lib/connector/offband_device_ui.dart @@ -130,6 +130,10 @@ enum DeviceUiSurface { tunePlay, tuneSlot, screenSettings, + + /// The radio's own per-channel notify mode (#544). Distinct from the app's + /// per-channel notify setting, which is not a device-UI surface at all. + channelNotify, } /// The refusal reason for a GPS write. Reason 1 means Power save on a build diff --git a/lib/screens/settings/device_ui_view.dart b/lib/screens/settings/device_ui_view.dart index c0a8d016..9497bc9a 100644 --- a/lib/screens/settings/device_ui_view.dart +++ b/lib/screens/settings/device_ui_view.dart @@ -66,6 +66,12 @@ class _DeviceUiViewState extends State { DeviceUiSurface.tunePlay || DeviceUiSurface.tuneSlot => widget.part == DeviceUiPart.buzzer, DeviceUiSurface.gpsMode || DeviceUiSurface.screenSettings => false, + // The radio's per-channel notify control lives beside the app's own + // per-channel control on the Channels screen, not in this pane (#544 + // Epic C), so a refusal for it is not this screen's to show. Epic C1 + // surfaces it there; until then nothing on this pane can emit it, because + // nothing here writes it. + DeviceUiSurface.channelNotify => false, }; @override diff --git a/test/connector/channel_notify_connector_test.dart b/test/connector/channel_notify_connector_test.dart new file mode 100644 index 00000000..34a7da7d --- /dev/null +++ b/test/connector/channel_notify_connector_test.dart @@ -0,0 +1,272 @@ +// #544 Epic A (#1068): the red baseline for the RADIO's per-channel notify +// mode over 0xC5 0x18 / 0x19. +// +// The feature this pins is that there are TWO independent settings: the app's +// per-channel mode drives the PHONE, the radio's drives the BUZZER, and +// neither moves the other (owner, 2026-11-03). The independence group below is +// the load-bearing part -- it is what stops the two being quietly re-merged by +// a later change. + +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:drift/native.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/connector/meshcore_protocol.dart'; +import 'package:meshcore_open/connector/offband_channel_notify.dart'; +import 'package:meshcore_open/connector/offband_device_ui.dart'; +import 'package:meshcore_open/models/app_settings.dart'; +import 'package:meshcore_open/services/app_settings_service.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'; + +/// RESP_CODE_DEVICE_INFO from an Offband radio (fork length, dual version so +/// the population classifier corroborates it). Caps byte 2 at offset 84; +/// pass [length] 88 with [caps3] to carry a third caps byte at offset 87. +Uint8List deviceInfo({ + required int caps2, + int length = 85, + int caps3 = 0, + int ledByte = 0, + int displayByte = 0, +}) { + final v = ascii.encode('1.5.0-1.17.0'); + final f = Uint8List(length); + f[0] = 13; + f[1] = 22; + f.setRange(60, 60 + v.length, v); + if (length >= 85) f[84] = caps2; + if (length >= 86) f[85] = ledByte; + if (length >= 87) f[86] = displayByte; + if (length >= 88) f[87] = caps3; + return f; +} + +void main() { + late OffbandDatabase db; + late MeshCoreConnector connector; + late List> sent; + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + PrefsManager.reset(); + await PrefsManager.initialize(); + db = OffbandDatabase(NativeDatabase.memory()); + BlobStore.overrideForTest(BlobStore(db)); + sent = []; + connector = MeshCoreConnector(); + connector.sendFrameOverrideForTest = (data) => sent.add(data.toList()); + connector.setConnectedForTest(); + }); + + tearDown(() async { + BlobStore.clearTestOverride(); + await db.close(); + }); + + bool sentChannelNotify() => sent.any( + (f) => + f.length >= 2 && + f[0] == cmdOffbandDeviceUi && + (f[1] == offbandUiChannelNotifyGet || + f[1] == offbandUiChannelNotifySet), + ); + + // ---- A1: the radio's modes ------------------------------------------------ + + group('A1 radio channel notify', () { + test('the wire bytes are the documented ones', () async { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + sent.clear(); + + await connector.requestChannelNotify(2); + await connector.setChannelNotify(2, RadioChannelNotify.mentions); + + expect( + sent, + equals([ + [0xC5, 0x18, 0x02], + [0xC5, 0x19, 0x02, 0x02], + ]), + ); + }); + + test('a reply sets that slot, and only that slot', () { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + connector.handleFrameForTest([0xC5, 0x18, 0x01, 0x02]); + + expect(connector.channelNotifyFor(1), RadioChannelNotify.mentions); + expect(connector.channelNotifyFor(0), isNull); + expect(connector.channelNotifyFor(2), isNull); + }); + + test('mode 0 is unset, and is distinct from Off', () { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + connector.handleFrameForTest([0xC5, 0x18, 0x00, 0x00]); + connector.handleFrameForTest([0xC5, 0x18, 0x01, 0x03]); + + expect(connector.channelNotifyFor(0), RadioChannelNotify.unset); + expect(connector.channelNotifyFor(1), RadioChannelNotify.off); + }); + + test('a write is not shown until the radio confirms it', () async { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + await connector.setChannelNotify(3, RadioChannelNotify.off); + + expect(connector.channelNotifyFor(3), isNull); + + connector.handleFrameForTest([0xC5, 0x19, 0x03, 0x03]); + expect(connector.channelNotifyFor(3), RadioChannelNotify.off); + }); + + test('an unknown mode byte is dropped, never guessed at', () { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + connector.handleFrameForTest([0xC5, 0x18, 0x04, 0x09]); + + expect(connector.channelNotifyFor(4), isNull); + expect(parseChannelNotifyReply([0xC5, 0x18, 0x04, 0x09]), isNull); + }); + + test('state does not survive a change of radio', () { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + connector.handleFrameForTest([0xC5, 0x18, 0x01, 0x01]); + expect(connector.channelNotifyFor(1), RadioChannelNotify.all); + + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + expect(connector.channelNotifyFor(1), isNull); + }); + }); + + // ---- A2: the two settings are independent --------------------------------- + + group('A2 independence of app and radio settings', () { + test('changing the app mode emits no radio frame', () async { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + sent.clear(); + + final settings = AppSettingsService(); + await settings.loadSettings(); + await settings.setChannelNotifyMode( + devicePrefix: 'aabbccddee', + pskHex: '00112233445566778899aabbccddeeff', + channelName: 'skywarn', + mode: ChannelNotifyMode.off, + ); + + expect(sentChannelNotify(), isFalse); + expect(sent, isEmpty); + }); + + test('a radio reply does not touch the app store', () async { + final settings = AppSettingsService(); + await settings.loadSettings(); + const prefix = 'aabbccddee'; + const psk = '00112233445566778899aabbccddeeff'; + await settings.setChannelNotifyMode( + devicePrefix: prefix, + pskHex: psk, + channelName: 'skywarn', + mode: ChannelNotifyMode.all, + ); + + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + connector.handleFrameForTest([0xC5, 0x18, 0x00, 0x03]); + + expect( + settings.channelNotifyMode(devicePrefix: prefix, pskHex: psk), + ChannelNotifyMode.all, + reason: 'the radio saying Off must not mute the phone', + ); + }); + + test('the app setting works with no radio capability at all', () async { + connector.handleFrameForTest(deviceInfo(caps2: 0x00)); + final settings = AppSettingsService(); + await settings.loadSettings(); + const prefix = 'aabbccddee'; + const psk = '00112233445566778899aabbccddeeff'; + + await settings.setChannelNotifyMode( + devicePrefix: prefix, + pskHex: psk, + channelName: 'skywarn', + mode: ChannelNotifyMode.mentionsOnly, + ); + + expect(connector.supportsChannelNotify, isFalse); + expect( + settings.channelNotifyMode(devicePrefix: prefix, pskHex: psk), + ChannelNotifyMode.mentionsOnly, + ); + }); + }); + + // ---- A3: capability and caps byte 3 --------------------------------------- + + group('A3 capability gating and caps byte 3', () { + test('without 0x80 no channel-notify frame is ever emitted', () async { + connector.handleFrameForTest(deviceInfo(caps2: 0x40)); + await connector.requestChannelNotify(0); + await connector.setChannelNotify(0, RadioChannelNotify.off); + + expect(connector.supportsChannelNotify, isFalse); + expect(sentChannelNotify(), isFalse); + }); + + test('0x80 is the bit, and other bits do not open it', () { + connector.handleFrameForTest(deviceInfo(caps2: offbandCap2ChannelNotify)); + expect(connector.supportsChannelNotify, isTrue); + + connector.handleFrameForTest(deviceInfo(caps2: 0x7F)); + expect(connector.supportsChannelNotify, isFalse); + }); + + test('caps byte 3 reads offset 87', () { + connector.handleFrameForTest( + deviceInfo(caps2: 0x80, length: 88, caps3: 0x05), + ); + expect(connector.offbandCaps3, 0x05); + }); + + test('caps byte 3 is null on any shorter frame', () { + for (final n in [85, 86, 87]) { + connector.handleFrameForTest(deviceInfo(caps2: 0x80, length: n)); + expect( + connector.offbandCaps3, + isNull, + reason: 'a $n-byte frame carries no byte 3', + ); + } + }); + + test('85 and 86 are not mistaken for byte 3', () { + // Those offsets are ui_led_enabled and ui_display_mode. Reading either + // as a capability mask is the failure this pins. + connector.handleFrameForTest( + deviceInfo( + caps2: 0x80, + length: 88, + ledByte: 0xAA, + displayByte: 0xBB, + caps3: 0x01, + ), + ); + expect(connector.offbandCaps3, 0x01); + expect(connector.offbandCaps3, isNot(0xAA)); + expect(connector.offbandCaps3, isNot(0xBB)); + }); + + test('a stock-shaped radio exposes nothing, whatever its bytes', () { + final stock = Uint8List(82); + stock[0] = 13; + stock[1] = 14; + connector.handleFrameForTest(stock); + + expect(connector.supportsChannelNotify, isFalse); + expect(connector.offbandCaps3, isNull); + }); + }); +}