diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 623c9438..d039c6c7 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -223,6 +223,15 @@ } } }, + "deviceCli_remoteUnsupported": "{command}: not sent. Your radio's firmware doesn't support remote CLI.", + "@deviceCli_remoteUnsupported": { + "description": "Console row when the connected radio refuses to send a remote-CLI command because its firmware predates TXT_TYPE_CLI_COMMAND (#999). Nothing was transmitted.", + "placeholders": { + "command": { + "type": "String" + } + } + }, "deviceCli_sendFailed": "Send failed: {error}", "@deviceCli_sendFailed": { "placeholders": { diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index 1c602ca9..b7012c2e 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -772,6 +772,12 @@ abstract class AppLocalizations { /// **'{command}: no reply in {seconds} s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'** String deviceCli_remoteNoReply(String command, String seconds); + /// Console row when the connected radio refuses to send a remote-CLI command because its firmware predates TXT_TYPE_CLI_COMMAND (#999). Nothing was transmitted. + /// + /// In en, this message translates to: + /// **'{command}: not sent. Your radio\'s firmware doesn\'t support remote CLI.'** + String deviceCli_remoteUnsupported(String command); + /// No description provided for @deviceCli_sendFailed. /// /// In en, this message translates to: diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index a1d4fb28..d35b52de 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -373,6 +373,11 @@ class AppLocalizationsBg extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index beb031ee..35cd8db8 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -376,6 +376,11 @@ class AppLocalizationsDe extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index 76c43ef4..54bf3d39 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -370,6 +370,11 @@ class AppLocalizationsEn extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index 066c36e7..4e46d975 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -374,6 +374,11 @@ class AppLocalizationsEs extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index 9457d01a..219de29e 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -376,6 +376,11 @@ class AppLocalizationsFr extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index 332e73dd..c08c1cb2 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -373,6 +373,11 @@ class AppLocalizationsHu extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index cf8a15ba..91389a7e 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -376,6 +376,11 @@ class AppLocalizationsIt extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index 03bb77e6..d3010fdd 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -363,6 +363,11 @@ class AppLocalizationsJa extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index bbb09faf..39187c25 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -362,6 +362,11 @@ class AppLocalizationsKo extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index cbad6ba8..b6236c72 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -372,6 +372,11 @@ class AppLocalizationsNl extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index 2220cabc..fe5b39ba 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -377,6 +377,11 @@ class AppLocalizationsPl extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index e3bab813..7ce0252f 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -375,6 +375,11 @@ class AppLocalizationsPt extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index e1bf4494..37a816a1 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -375,6 +375,11 @@ class AppLocalizationsRu extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index 0f284362..25689cce 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -374,6 +374,11 @@ class AppLocalizationsSk extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index 8f6ac161..3c70d749 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -372,6 +372,11 @@ class AppLocalizationsSl extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index 549ba49f..637ec25d 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -371,6 +371,11 @@ class AppLocalizationsSv extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index a8d8a988..08d295ab 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -374,6 +374,11 @@ class AppLocalizationsUk extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index f5907c33..e7790698 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -360,6 +360,11 @@ class AppLocalizationsZh extends AppLocalizations { return '$command: no reply in $seconds s. This radio may not allow remote CLI from you, or is unreachable. Not retried.'; } + @override + String deviceCli_remoteUnsupported(String command) { + return '$command: not sent. Your radio\'s firmware doesn\'t support remote CLI.'; + } + @override String deviceCli_sendFailed(String error) { return 'Send failed: $error'; diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index e7aee762..f7678864 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -412,9 +412,12 @@ class _ChatScreenState extends State { ], ), ), - // Remote CLI is a companion feature (#999): repeaters and - // room servers have their own CLI via the repeater hub. - if (widget.contact.isCompanion) + // Remote CLI is a companion feature (#999), and the + // connected radio must speak the 1.18 CLI to send it. + if (DeviceCliScreen.offersRemoteConsole( + contact: widget.contact, + classification: connector.classification, + )) PopupMenuItem( value: 'cliConsole', child: Row( diff --git a/lib/screens/device_cli_screen.dart b/lib/screens/device_cli_screen.dart index 5e42ea7a..cf438cec 100644 --- a/lib/screens/device_cli_screen.dart +++ b/lib/screens/device_cli_screen.dart @@ -18,6 +18,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; +import '../connector/device_population.dart'; import '../connector/meshcore_connector.dart'; import '../helpers/device_name_validator.dart'; import '../helpers/snack_bar_builder.dart'; @@ -92,6 +93,20 @@ class DeviceCliScreen extends StatefulWidget { @visibleForTesting final CliConsoleTransport? transportOverride; + /// Whether to offer the remote console for [contact]. Two conditions, both + /// required: the target is a companion (repeaters and room servers have + /// their own CLI via the repeater hub), and the CONNECTED radio speaks the + /// 1.18 CLI. A pre-1.18 radio refuses to send a type-3 command + /// (companion-v1.17.1 accepts only plain and CLI_DATA in CMD_SEND_TXT_MSG; + /// 1.18.0 adds CLI_COMMAND), so offering the console there would only + /// produce failures. Type-3 send and the local CLI frame shipped together + /// in 1.18, so this reuses the local console's gate. A null classification + /// (disconnected, or not yet classified) offers nothing. + static bool offersRemoteConsole({ + required Contact contact, + required Classification? classification, + }) => contact.isCompanion && (classification?.allowsCliFrame ?? false); + @override State createState() => _DeviceCliScreenState(); } @@ -218,6 +233,14 @@ class _DeviceCliScreenState extends State { ? context.l10n.deviceCli_remoteNoReply(command, e.secondsText) : context.l10n.deviceCli_noReply(command, e.secondsText), ); + } on CliConsoleUnsupported { + if (!mounted) return; + // The connected radio refused the send itself (pre-1.18 firmware), so + // nothing went out: say so now instead of waiting out a timeout that + // would blame the target. + entry = _ConsoleEntry.sendFailed( + context.l10n.deviceCli_remoteUnsupported(command), + ); } catch (e) { if (!mounted) return; entry = _ConsoleEntry.sendFailed( diff --git a/lib/services/cli_console_transport.dart b/lib/services/cli_console_transport.dart index 16676fd4..0580f2b6 100644 --- a/lib/services/cli_console_transport.dart +++ b/lib/services/cli_console_transport.dart @@ -37,6 +37,19 @@ class CliConsoleTimeout implements Exception { String toString() => 'CLI command timed out after $secondsText s'; } +/// The connected radio refused to send a remote CLI command at all: its +/// firmware does not accept TXT_TYPE_CLI_COMMAND (pre-1.18 answers +/// ERR_CODE_UNSUPPORTED_CMD). Distinct from [CliConsoleTimeout], which means +/// the command went out and the target never answered. +class CliConsoleUnsupported implements Exception { + const CliConsoleUnsupported({required this.command}); + + final String command; + + @override + String toString() => 'The connected radio does not support remote CLI'; +} + /// One CLI transport for the console: the connected device, or a remote contact. abstract class CliConsoleTransport { /// Runs [command] and completes with the correlated reply, or throws @@ -92,8 +105,59 @@ class RemoteCliTransport implements CliConsoleTransport { StreamSubscription? _sub; bool _disposed = false; + /// Rejection channel for the command in flight: completed with + /// [CliConsoleUnsupported] when the radio refuses the send outright. + /// + /// It only listens while [_armed]: from the moment the text frame is written + /// (the `onAttempt` hook fires after the path update and runs straight into + /// the frame write, with no yield in between) until the radio answers that + /// write. The firmware answers each CMD_SEND_TXT_MSG at once, in order: a + /// pre-1.18 radio with ERR_CODE_UNSUPPORTED_CMD for type 3; a 1.18 radio with + /// RESP_CODE_SENT carrying an expected-ack of 0, since a CLI command expects + /// no ack (companion-v1.18.0 CMD_SEND_TXT_MSG). A plain DM's SENT carries a + /// real ack hash, so it never closes the window. + /// + /// ERR_CODE_UNSUPPORTED_CMD is also the firmware's answer to any unknown + /// command. Arming at the write, rather than at [run], keeps an unrelated + /// refusal from failing a valid command. The residual exposure is one + /// landing inside our own send's round trip, on a radio where the connector + /// already withholds fork frames. + /// + /// One command at a time: a new [run] replaces the channel, and the replaced + /// command falls back to its timeout rather than risk misattribution. + Completer? _sendWindow; + String? _sendWindowCommand; + bool _armed = false; + void _onFrame(Uint8List frame) { if (_disposed || frame.isEmpty) return; + if (frame[0] == respCodeSent) { + // [RESP_CODE_SENT][is_flood][expected_ack x4][est_timeout x4]. Only a + // command send's SENT (ack 0) answers ours. + if (_armed && + frame.length >= 6 && + frame[2] == 0 && + frame[3] == 0 && + frame[4] == 0 && + frame[5] == 0) { + _armed = false; + } + return; + } + if (frame[0] == respCodeErr) { + final code = frame.length > 1 ? frame[1] : -1; + final window = _sendWindow; + if (_armed && + code == errCodeUnsupportedCmd && + window != null && + !window.isCompleted) { + _armed = false; + window.completeError( + CliConsoleUnsupported(command: _sendWindowCommand ?? ''), + ); + } + return; + } if (frame[0] != respCodeContactMsgRecv && frame[0] != respCodeContactMsgRecvV3) { return; @@ -134,12 +198,35 @@ class RemoteCliTransport implements CliConsoleTransport { @override Future run(String command, {Duration? timeout}) async { + final window = Completer(); + _sendWindow = window; + _sendWindowCommand = command; + _armed = false; + try { + // Whichever lands first: the reply (or its timeout), or the radio + // refusing the send. Future.any keeps handlers on the loser, so its later + // timeout is absorbed rather than surfacing as an unhandled error. + return await Future.any([_send(command, window), window.future]); + } finally { + if (identical(_sendWindow, window)) { + _sendWindow = null; + _armed = false; + } + } + } + + Future _send(String command, Completer window) async { // retries: 1 - one send, no auto-retry. See the file header. try { final text = await _service.sendCommand( _resolveTarget(), command, retries: 1, + // Fires after the path update, immediately before the text frame is + // written: arm the rejection window here, not earlier. + onAttempt: (_) { + if (identical(_sendWindow, window)) _armed = true; + }, ); // payloadLength drives CliReply.atReplyBound (the local 165-byte device // bound). A remote reply is a reassembled mesh message, so that bound is @@ -160,6 +247,13 @@ class RemoteCliTransport implements CliConsoleTransport { _disposed = true; _sub?.cancel(); _sub = null; + // Release a command still waiting, so its run() cannot hang. + final window = _sendWindow; + _sendWindow = null; + _armed = false; + if (window != null && !window.isCompleted) { + window.completeError(StateError('RemoteCliTransport disposed')); + } _service.dispose(); } } diff --git a/test/screens/device_cli_screen_test.dart b/test/screens/device_cli_screen_test.dart index a8ae2bf5..a138c9b8 100644 --- a/test/screens/device_cli_screen_test.dart +++ b/test/screens/device_cli_screen_test.dart @@ -38,6 +38,17 @@ class _TimeoutTransport implements CliConsoleTransport { void dispose() {} } +/// The connected radio refuses the send outright (pre-1.18 firmware). +class _UnsupportedTransport implements CliConsoleTransport { + @override + Future run(String command, {Duration? timeout}) async { + throw CliConsoleUnsupported(command: command); + } + + @override + void dispose() {} +} + void main() { late OffbandDatabase db; late MeshCoreConnector connector; @@ -354,5 +365,37 @@ void main() { findsOneWidget, ); }); + + testWidgets('a refused send says nothing went out, not a timeout (#999)', ( + tester, + ) async { + await tester.pumpWidget( + ChangeNotifierProvider.value( + value: connector, + child: MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: DeviceCliScreen( + remoteContact: companion(), + transportOverride: _UnsupportedTransport(), + ), + ), + ), + ); + await tester.pumpAndSettle(); + + await send(tester, 'get radio'); + await tester.pumpAndSettle(); + + expect( + find.text( + "get radio: not sent. Your radio's firmware doesn't support " + 'remote CLI.', + ), + findsOneWidget, + ); + // Not the #891 row, which would blame the target. + expect(find.textContaining('may not allow remote CLI'), findsNothing); + }); }); } diff --git a/test/screens/remote_console_gate_test.dart b/test/screens/remote_console_gate_test.dart new file mode 100644 index 00000000..5c6ba539 --- /dev/null +++ b/test/screens/remote_console_gate_test.dart @@ -0,0 +1,75 @@ +// #999 reopen: the companion "CLI console" entry is offered only when BOTH the +// target is a companion AND the connected radio speaks the 1.18 CLI. A +// pre-1.18 radio (stock 1.17, or Offband before the absorb) refuses a type-3 +// send, so offering the console there only produces failures. + +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:meshcore_open/connector/device_population.dart'; +import 'package:meshcore_open/connector/meshcore_protocol.dart'; +import 'package:meshcore_open/models/contact.dart'; +import 'package:meshcore_open/screens/device_cli_screen.dart'; + +Contact _contact(int type) => Contact( + publicKey: Uint8List.fromList(List.generate(32, (i) => i)), + receivedName: 'X', + type: type, + pathLength: 0, + path: Uint8List(0), + lastSeen: DateTime.fromMillisecondsSinceEpoch(0), +); + +Classification _radio(DevicePopulation population) => + Classification(population: population, conflict: false, evidence: 'test'); + +void main() { + test('a companion on a 1.18 radio is offered the console', () { + expect( + DeviceCliScreen.offersRemoteConsole( + contact: _contact(advTypeChat), + classification: _radio(DevicePopulation.stockModern), + ), + isTrue, + ); + }); + + test('no console when the connected radio is pre-1.18 or Offband', () { + for (final population in [ + DevicePopulation.stockLegacy, + DevicePopulation.offband, + ]) { + expect( + DeviceCliScreen.offersRemoteConsole( + contact: _contact(advTypeChat), + classification: _radio(population), + ), + isFalse, + reason: '$population cannot send a type-3 command', + ); + } + }); + + test('no console when the radio is not classified (disconnected)', () { + expect( + DeviceCliScreen.offersRemoteConsole( + contact: _contact(advTypeChat), + classification: null, + ), + isFalse, + ); + }); + + test('no console for a repeater or room server, even on a 1.18 radio', () { + for (final type in [advTypeRepeater, advTypeRoom, advTypeSensor]) { + expect( + DeviceCliScreen.offersRemoteConsole( + contact: _contact(type), + classification: _radio(DevicePopulation.stockModern), + ), + isFalse, + reason: 'type $type has its own CLI via the repeater hub', + ); + } + }); +} diff --git a/test/services/cli_console_transport_test.dart b/test/services/cli_console_transport_test.dart index 2c180a9a..c7836d83 100644 --- a/test/services/cli_console_transport_test.dart +++ b/test/services/cli_console_transport_test.dart @@ -239,6 +239,146 @@ void main() { }); }); + group('fail fast when the connected radio refuses the send (#999)', () { + // A pre-1.18 companion answers CMD_SEND_TXT_MSG carrying type 3 with + // ERR_CODE_UNSUPPORTED_CMD (companion-v1.17.1 MyMesh.cpp). The command + // never went out, so waiting out the mesh timeout would end in a row that + // blames the target. + final errUnsupported = Uint8List.fromList([ + respCodeErr, + errCodeUnsupportedCmd, + ]); + // [RESP_CODE_SENT][is_flood][expected_ack x4][est timeout x4]. 1.18 sends a + // CLI command with no ack expected, so OUR send's SENT carries ack 0; a + // plain DM's SENT carries a real ack hash. + Uint8List sent(List ack) => + Uint8List.fromList([respCodeSent, 0, ...ack, 0x10, 0x27, 0, 0]); + final commandSent = sent([0, 0, 0, 0]); + final dmSent = sent([0xDE, 0xAD, 0xBE, 0xEF]); + + Future< + ({ + MeshCoreConnector connector, + Contact companion, + Future run, + String prefix, + RemoteCliTransport transport, + }) + > + startRun() async { + final connector = MeshCoreConnector(); + final companion = _companion(); + final cliSent = Completer(); + connector.sendFrameOverrideForTest = (f) { + if (!cliSent.isCompleted && f.isNotEmpty && f[0] == cmdSendTxtMsg) { + cliSent.complete(f); + } + }; + connector.setConnectedForTest(); + connector.contactsForTest.add(companion); + final transport = RemoteCliTransport(connector, companion); + final run = transport.run('get radio'); + final frame = await cliSent.future; + return ( + connector: connector, + companion: companion, + run: run, + prefix: ascii.decode(frame.sublist(13, 16)), + transport: transport, + ); + } + + test('an unsupported-command error fails the command at once', () async { + final s = await startRun(); + s.connector.handleFrameForTest(errUnsupported); + await expectLater( + s.run.timeout(const Duration(seconds: 1)), + throwsA(isA()), + ); + s.transport.dispose(); + }); + + test('a different error code does not fail the command', () async { + final s = await startRun(); + s.connector.handleFrameForTest( + Uint8List.fromList([respCodeErr, errCodeNotFound]), + ); + s.connector.handleFrameForTest( + _replyFrame( + senderPrefix: s.companion.publicKey, + text: '${s.prefix}radio ok', + ), + ); + expect((await s.run).text, 'radio ok'); + s.transport.dispose(); + }); + + test('an error after the radio accepted the send is not ours', () async { + final s = await startRun(); + s.connector.handleFrameForTest(commandSent); + s.connector.handleFrameForTest(errUnsupported); + s.connector.handleFrameForTest( + _replyFrame( + senderPrefix: s.companion.publicKey, + text: '${s.prefix}radio ok', + ), + ); + expect((await s.run).text, 'radio ok'); + s.transport.dispose(); + }); + + test('a DM\'s SENT does not close the window early', () async { + // An earlier in-flight DM's acknowledgement (real ack hash) must not be + // mistaken for ours, or a refusal after it would be missed. + final s = await startRun(); + s.connector.handleFrameForTest(dmSent); + s.connector.handleFrameForTest(errUnsupported); + await expectLater( + s.run.timeout(const Duration(seconds: 1)), + throwsA(isA()), + ); + s.transport.dispose(); + }); + + test('an error before the text frame is written is not ours', () async { + // The window arms only at the write. An unrelated refusal that lands + // while the path update is still in progress must not fail the command. + final connector = MeshCoreConnector(); + final companion = _companion(); + final cliSent = Completer(); + connector.sendFrameOverrideForTest = (f) { + if (!cliSent.isCompleted && f.isNotEmpty && f[0] == cmdSendTxtMsg) { + cliSent.complete(f); + } + }; + connector.setConnectedForTest(); + connector.contactsForTest.add(companion); + final transport = RemoteCliTransport(connector, companion); + final run = transport.run('get radio'); + // run() has yielded inside the path update; nothing is armed yet. + connector.handleFrameForTest(errUnsupported); + final frame = await cliSent.future; + final prefix = ascii.decode(frame.sublist(13, 16)); + connector.handleFrameForTest( + _replyFrame( + senderPrefix: companion.publicKey, + text: '${prefix}radio ok', + ), + ); + expect((await run).text, 'radio ok'); + transport.dispose(); + }); + + test('dispose with a command in flight does not hang its run()', () async { + final s = await startRun(); + s.transport.dispose(); + await expectLater( + s.run.timeout(const Duration(seconds: 1)), + throwsA(isA()), + ); + }); + }); + group('LocalCliTransport', () { test('a local timeout surfaces as CliConsoleTimeout', () async { final connector = MeshCoreConnector(); diff --git a/untranslated.json b/untranslated.json index 6b2eb55f..15068b60 100644 --- a/untranslated.json +++ b/untranslated.json @@ -17,6 +17,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -352,6 +353,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -687,6 +689,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -1022,6 +1025,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -1357,6 +1361,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -1692,6 +1697,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -2027,6 +2033,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -2362,6 +2369,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -2697,6 +2705,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -3032,6 +3041,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -3367,6 +3377,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -3702,6 +3713,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -4037,6 +4049,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -4372,6 +4385,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -4707,6 +4721,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -5042,6 +5057,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy", @@ -5377,6 +5393,7 @@ "deviceCli_emptyHint", "deviceCli_noReply", "deviceCli_remoteNoReply", + "deviceCli_remoteUnsupported", "deviceCli_sendFailed", "deviceCli_truncatedByDevice", "deviceCli_secretLongPressToCopy",