diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 52006501..8b17ce24 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -1164,9 +1164,23 @@ class MeshCoreConnector extends ChangeNotifier { bool get supportsScreenSettings => firmwareSupportsScreenSettings(_offbandCaps2); - /// Whether this radio has a settable display or LED (#523, caps byte 2 - /// `0x04`). Which of the two it has, it says only by refusing the other. - bool get supportsIndicators => firmwareSupportsIndicators(_offbandCaps2); + /// Whether this radio's display mode can be set (#1132, firmware #2069). + /// See [resolveIndicatorSupport] for the precedence between the advert tags + /// and caps2 `0x04`. + bool get supportsDisplayControl => resolveIndicatorSupport( + offbandCaps2: _offbandCaps2, + advert: _capabilityAdvert, + ).display; + + /// Whether this radio's status LED can be set (#1132, firmware #2069). + bool get supportsLedControl => resolveIndicatorSupport( + offbandCaps2: _offbandCaps2, + advert: _capabilityAdvert, + ).led; + + /// Whether this radio has a settable display or LED, so the Indicators page + /// has anything to show (#523, #1132). + bool get supportsIndicators => supportsDisplayControl || supportsLedControl; /// The LED and display state the radio last reported, from device-info or a /// reply. Null when it has not said. @@ -1471,6 +1485,8 @@ class MeshCoreConnector extends ChangeNotifier { '${advert ?? 'UNSUPPORTED, falling back to device-info offsets'}' ' chanNotify=$supportsChannelNotify' ' rotation=$supportsDisplayRotation' + ' display=$supportsDisplayControl' + ' led=$supportsLedControl' ' ackCapacity=${ackCapacity ?? 'absent'}', tag: 'DeviceUI', ); @@ -1890,9 +1906,10 @@ class MeshCoreConnector extends ChangeNotifier { Future _sendIndicatorSet( String what, DeviceUiSurface surface, - Uint8List frame, - ) async { - if (!_deviceUiGateOpen(what, advertised: supportsIndicators)) return; + Uint8List frame, { + required bool advertised, + }) async { + if (!_deviceUiGateOpen(what, advertised: advertised)) return; _deviceUiError = null; _deviceUiErrorSurface = null; _pendingDeviceUiWrite = surface; @@ -1904,6 +1921,7 @@ class MeshCoreConnector extends ChangeNotifier { 'display SET', DeviceUiSurface.display, buildDisplaySetFrame(mode), + advertised: supportsDisplayControl, ); /// Refused locally until the radio has answered the rotation GET, so @@ -1920,6 +1938,7 @@ class MeshCoreConnector extends ChangeNotifier { 'rotation SET', DeviceUiSurface.rotation, buildRotationSetFrame(degrees), + advertised: supportsDisplayRotation, ); } @@ -1945,6 +1964,7 @@ class MeshCoreConnector extends ChangeNotifier { 'led SET', DeviceUiSurface.led, buildLedSetFrame(on: on), + advertised: supportsLedControl, ); /// Re-read whatever headless-UI state this radio supports. Called after every @@ -9220,7 +9240,7 @@ class MeshCoreConnector extends ChangeNotifier { /// LED (byte 85) and display mode (byte 86), appended unconditionally on /// v21+ (firmware #542). Each is null when the frame stops short of it. Like /// the FEM byte, presence says nothing about capability: - /// [supportsIndicators] does. + /// [supportsDisplayControl] and [supportsLedControl] do. /// /// Bytes 82 to 86 keep working and are explicitly untouched by the advert /// contract (§7). Nothing here reads 87, and nothing may: rotation is read diff --git a/lib/connector/offband_capability_advert.dart b/lib/connector/offband_capability_advert.dart index 48c8a04d..955c79ae 100644 --- a/lib/connector/offband_capability_advert.dart +++ b/lib/connector/offband_capability_advert.dart @@ -33,6 +33,13 @@ const int offbandUiInfoGet = 0x1C; const int offbandInfoTagChanNotify = 0x01; const int offbandInfoTagDisplayRotation = 0x02; const int offbandInfoTagAckCapacity = 0x03; + +/// Display control and LED control are separate capabilities (firmware #2067, +/// PR OffbandMesh/meshcore-firmware#2080, merged `c835aeca`): a board can have +/// one without the other, and caps2 `0x04` INDICATORS answers both with one +/// bit. Values are read over `0x05`/`0x06` and `0x07`/`0x08`. +const int offbandInfoTagDisplayControl = 0x04; +const int offbandInfoTagLedControl = 0x05; const int offbandInfoTagContinue = 0xFF; /// `0x00` is not a valid tag. diff --git a/lib/connector/offband_indicators.dart b/lib/connector/offband_indicators.dart index 8b7ed3ad..5ea4d747 100644 --- a/lib/connector/offband_indicators.dart +++ b/lib/connector/offband_indicators.dart @@ -1,5 +1,6 @@ import 'dart:typed_data'; +import 'offband_capability_advert.dart'; import 'offband_device_ui.dart'; /// The radio's display and LED over `0xC5` (#523, #944): display mode, display @@ -32,7 +33,9 @@ import 'offband_device_ui.dart'; /// advert, NOT by caps2 `0x04` INDICATORS and NOT by probing `0x1A` and /// watching for a refusal. Contract §3 forbids that probing outright: /// "what is forbidden is discovering support by trying each feature's sub-code -/// and watching for errors." Display mode and the LED keep the INDICATORS bit. +/// and watching for errors." Display mode and the LED each have their own +/// advert tag, with the INDICATORS bit as the fallback: see +/// [resolveIndicatorSupport]. /// /// A board without a display, or without a controllable LED, refuses that /// half with reason 6; a display that cannot turn refuses rotation with 10. @@ -52,6 +55,42 @@ const List displayRotations = [0, 180]; bool firmwareSupportsIndicators(int? offbandCaps2) => offbandCaps2 != null && (offbandCaps2 & offbandCap2Indicators) != 0; +/// Which indicator controls this radio has, each on its own (#1132, firmware +/// #2069). +/// +/// caps2 `0x04` answers display AND LED with one bit, so a board with a real +/// display and no controllable LED showed an LED switch the radio then refused +/// with `[0xC5][0x7F][0x06]` (firmware #2065: a T096, a T1000-E, a QCC badge). +/// Precedence, from the firmware header on `firmware-base`, the same shape as +/// channel notify: +/// +/// - the advert carries `TAG_DISPLAY_CONTROL` and/or `TAG_LED_CONTROL`: the +/// tags decide, each control on its own, and the bit is ignored; +/// - the advert answered with neither tag: firmware from before the tags, so +/// the bit answers both, as it always has; +/// - no advert (the radio predates `0x1C`, or has not answered yet): the bit +/// answers both. +/// +/// The bit is not retired: radios on firmware 1.7.0 and earlier answer the +/// advert without these tags, and dropping the bit would hide working display +/// control on them. Support is never found by sending a GET and reading the +/// refusal (contract §3). +({bool display, bool led}) resolveIndicatorSupport({ + required int? offbandCaps2, + required OffbandCapabilityAdvert? advert, +}) { + if (advert != null && + (advert.has(offbandInfoTagDisplayControl) || + advert.has(offbandInfoTagLedControl))) { + return ( + display: advert.has(offbandInfoTagDisplayControl), + led: advert.has(offbandInfoTagLedControl), + ); + } + final bit = firmwareSupportsIndicators(offbandCaps2); + return (display: bit, led: bit); +} + /// When the radio's screen is lit. Values are the wire encoding. enum DisplayMode { auto(0, 'Auto', 'On while in use, then off after the screen timeout'), diff --git a/lib/screens/settings/indicators_view.dart b/lib/screens/settings/indicators_view.dart index e6d6a607..b079b570 100644 --- a/lib/screens/settings/indicators_view.dart +++ b/lib/screens/settings/indicators_view.dart @@ -7,12 +7,13 @@ import '../../connector/offband_indicators.dart'; import '../../widgets/device_ui_error_banner.dart'; /// The radio's display and LED (#523, #944): display mode, rotation and the -/// status LED. Gated on caps byte 2 `0x04`; the Node Settings row that leads -/// here is gated the same way. +/// status LED. Each block shows only when the radio has that control +/// ([MeshCoreConnector.supportsDisplayControl], +/// [MeshCoreConnector.supportsLedControl], #1132); the Node Settings row that +/// leads here shows when it has either. /// -/// Every control draws from what the radio reported, never from the tap. A -/// board without a display or without a controllable LED refuses that write -/// and says so in the banner. English-only, matching [DeviceUiView]. +/// Every control draws from what the radio reported, never from the tap. +/// English-only, matching [DeviceUiView]. class IndicatorsView extends StatelessWidget { const IndicatorsView({super.key}); @@ -33,28 +34,31 @@ class IndicatorsView extends StatelessWidget { message: error, onDismiss: connector.clearDeviceUiError, ), - const _Header('Display'), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 16), - child: SegmentedButton( - segments: [ - for (final m in DisplayMode.values) - ButtonSegment(value: m, label: Text(m.label)), - ], - selected: {?mode}, - emptySelectionAllowed: true, - onSelectionChanged: (v) { - if (v.isNotEmpty) connector.setDisplayMode(v.first); - }, + if (connector.supportsDisplayControl) ...[ + const _Header('Display'), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16), + child: SegmentedButton( + segments: [ + for (final m in DisplayMode.values) + ButtonSegment(value: m, label: Text(m.label)), + ], + selected: {?mode}, + emptySelectionAllowed: true, + onSelectionChanged: (v) { + if (v.isNotEmpty) connector.setDisplayMode(v.first); + }, + ), ), - ), - Padding( - padding: const EdgeInsets.fromLTRB(16, 8, 16, 0), - child: Text( - mode?.description ?? 'The radio has not reported its display mode', - style: Theme.of(context).textTheme.bodySmall, + Padding( + padding: const EdgeInsets.fromLTRB(16, 8, 16, 0), + child: Text( + mode?.description ?? + 'The radio has not reported its display mode', + style: Theme.of(context).textTheme.bodySmall, + ), ), - ), + ], if (connector.supportsDisplayRotation) ...[ const _Header('Rotation'), Padding( @@ -76,17 +80,19 @@ class IndicatorsView extends StatelessWidget { ), ), ], - const _Header('LED'), - SwitchListTile( - title: const Text('Status LED'), - subtitle: Text( - led == null - ? 'The radio has not reported its LED' - : 'Turn off to save battery or to stay unseen', + if (connector.supportsLedControl) ...[ + const _Header('LED'), + SwitchListTile( + title: const Text('Status LED'), + subtitle: Text( + led == null + ? 'The radio has not reported its LED' + : 'Turn off to save battery or to stay unseen', + ), + value: led ?? false, + onChanged: connector.setLedEnabled, ), - value: led ?? false, - onChanged: connector.setLedEnabled, - ), + ], ], ); } diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 5ad90988..beefeb89 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -497,8 +497,8 @@ class _SettingsScreenState extends State { ), ), ], - // Display mode, rotation and LED (#523). Strictly on the 0x04 - // bit: no bit, no row, no command. + // Display mode, rotation and LED (#523). Shown when the radio + // has display control or LED control (#1132). if (connector.supportsIndicators) ...[ const Divider(height: 1), ListTile( diff --git a/test/connector/indicators_connector_test.dart b/test/connector/indicators_connector_test.dart index 2b1109c3..37f9adab 100644 --- a/test/connector/indicators_connector_test.dart +++ b/test/connector/indicators_connector_test.dart @@ -174,6 +174,151 @@ void main() { expect(sentSub(offbandUiRotationGet), isFalse); }); + // #1132 (firmware #2069): display control and LED control are separate tags + // in the advert. Precedence from the firmware header: the tags decide when + // the advert carries either; otherwise caps2 0x04 answers both. + group('display and LED gate on their own tags', () { + void connectWith({required int caps2, List? tags}) { + connector.handleFrameForTest(deviceInfo(caps2: caps2)); + if (tags != null) { + connector.handleFrameForTest(Uint8List.fromList(advert(tags))); + } + } + + test( + 'display tag only: display shown, LED hidden and never sent', + () async { + connectWith( + caps2: offbandCap2Indicators, + tags: [offbandInfoTagDisplayControl], + ); + sent.clear(); + + expect(connector.supportsDisplayControl, isTrue); + expect(connector.supportsLedControl, isFalse); + expect(connector.supportsIndicators, isTrue); + + await connector.setLedEnabled(false); + await connector.setDisplayMode(DisplayMode.alwaysOn); + expect(sentSub(offbandUiLedSet), isFalse); + expect(sentSub(offbandUiDisplaySet), isTrue); + }, + ); + + test('LED tag only: LED shown, display hidden and never sent', () async { + connectWith( + caps2: offbandCap2Indicators, + tags: [offbandInfoTagLedControl], + ); + sent.clear(); + + expect(connector.supportsDisplayControl, isFalse); + expect(connector.supportsLedControl, isTrue); + + await connector.setDisplayMode(DisplayMode.alwaysOn); + await connector.setLedEnabled(false); + expect(sentSub(offbandUiDisplaySet), isFalse); + expect(sentSub(offbandUiLedSet), isTrue); + }); + + test('both tags: both shown', () { + connectWith( + caps2: offbandCap2Indicators, + tags: [offbandInfoTagDisplayControl, offbandInfoTagLedControl], + ); + + expect(connector.supportsDisplayControl, isTrue); + expect(connector.supportsLedControl, isTrue); + }); + + test('a tag decides even with caps2 0x04 clear', () { + connectWith(caps2: 0x01, tags: [offbandInfoTagLedControl]); + + expect(connector.supportsDisplayControl, isFalse); + expect(connector.supportsLedControl, isTrue); + expect(connector.supportsIndicators, isTrue); + }); + + test('advert without either tag: caps2 0x04 answers both, as today', () { + connectWith( + caps2: offbandCap2Indicators, + tags: [offbandInfoTagDisplayRotation], + ); + + expect(connector.supportsDisplayControl, isTrue); + expect(connector.supportsLedControl, isTrue); + }); + + test('advert without either tag and caps2 0x04 clear: no Indicators', () { + connectWith(caps2: 0x01, tags: [offbandInfoTagChanNotify]); + + expect(connector.supportsDisplayControl, isFalse); + expect(connector.supportsLedControl, isFalse); + expect(connector.supportsIndicators, isFalse); + }); + + test('a radio that predates the advert: caps2 0x04 answers both', () { + connector.handleFrameForTest(deviceInfo()); + connector.handleFrameForTest(Uint8List.fromList(advertUnsupported())); + + expect( + connector.capabilityAdvertState, + CapabilityAdvertState.unsupported, + ); + expect(connector.supportsDisplayControl, isTrue); + expect(connector.supportsLedControl, isTrue); + }); + + test('a radio that predates the advert, caps2 0x04 clear: nothing', () { + connector.handleFrameForTest(deviceInfo(caps2: 0x01)); + connector.handleFrameForTest(Uint8List.fromList(advertUnsupported())); + + expect(connector.supportsIndicators, isFalse); + }); + + test('before the advert answers, caps2 0x04 answers both', () { + connectWith(caps2: offbandCap2Indicators); + + expect(connector.capabilityAdvertState, CapabilityAdvertState.unknown); + expect(connector.supportsDisplayControl, isTrue); + expect(connector.supportsLedControl, isTrue); + }); + + test('support is never probed: no display or LED GET is sent', () { + connectWith( + caps2: offbandCap2Indicators, + tags: [offbandInfoTagDisplayControl], + ); + + expect(sentSub(offbandUiDisplayGet), isFalse); + expect(sentSub(offbandUiLedGet), isFalse); + }); + + test('an unknown tag is skipped by its length and kept', () { + final reply = parseCapabilityAdvertReply([ + cmdOffbandDeviceUi, + offbandUiInfoGet, + offbandInfoTagLedControl, + 0x00, + 0x06, + 0x02, + 0xAA, + 0xBB, + 0x07, + 0x00, + ])!; + + expect(reply.malformed, isFalse); + expect(reply.has(offbandInfoTagLedControl), isTrue); + expect(reply.tags[0x06], [0xAA, 0xBB]); + expect(reply.has(0x07), isTrue); + expect(resolveIndicatorSupport(offbandCaps2: 0, advert: reply), ( + display: false, + led: true, + )); + }); + }); + group('rotation', () { test('the INDICATORS bit alone does NOT open rotation', () { // The gate moved to the advert tag. 0x04 means indicators, which is a diff --git a/test/widgets/indicators_view_test.dart b/test/widgets/indicators_view_test.dart index 1f8d8815..edbb2416 100644 --- a/test/widgets/indicators_view_test.dart +++ b/test/widgets/indicators_view_test.dart @@ -12,6 +12,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:provider/provider.dart'; import 'package:meshcore_open/connector/meshcore_connector.dart'; +import 'package:meshcore_open/connector/offband_indicators.dart'; import 'package:meshcore_open/screens/settings/indicators_view.dart'; /// Rotation is advertised, but the radio has not answered the GET yet. This is @@ -82,6 +83,30 @@ void main() { expect(button.selected, {180}); }); + // #1132: display and LED show independently. + testWidgets('display control only: no Status LED switch', (tester) async { + await _pump(tester, _Controls(display: true, led: false)); + + expect(find.text('Display'), findsOneWidget); + expect(find.text('Status LED'), findsNothing); + expect(find.byType(SwitchListTile), findsNothing); + }); + + testWidgets('LED control only: no Display block', (tester) async { + await _pump(tester, _Controls(display: false, led: true)); + + expect(find.text('Display'), findsNothing); + expect(find.byType(SegmentedButton), findsNothing); + expect(find.text('Status LED'), findsOneWidget); + }); + + testWidgets('both controls: both shown', (tester) async { + await _pump(tester, _Controls(display: true, led: true)); + + expect(find.text('Display'), findsOneWidget); + expect(find.text('Status LED'), findsOneWidget); + }); + testWidgets('a radio that does not advertise rotation gets no control', ( tester, ) async { @@ -93,3 +118,13 @@ void main() { } class _DummyConnector extends MeshCoreConnector {} + +class _Controls extends MeshCoreConnector { + _Controls({required this.display, required this.led}); + final bool display; + final bool led; + @override + bool get supportsDisplayControl => display; + @override + bool get supportsLedControl => led; +}