import 'dart:async'; import 'package:flutter/foundation.dart'; import '../models/contact.dart'; import '../models/path_selection.dart'; import '../connector/meshcore_connector.dart'; import '../connector/meshcore_protocol.dart'; import '../utils/app_logger.dart'; import 'command_correlation.dart'; /// A repeater reply that arrived with no command waiting to receive it. /// /// [command] carries the original request when the reply could be traced back /// to one that already timed out, and is null when the reply cannot be /// attributed to anything this service sent. class UnmatchedRepeaterResponse { final String repeaterKeyHex; final String response; final String? command; final Duration? sinceTimeout; const UnmatchedRepeaterResponse({ required this.repeaterKeyHex, required this.response, this.command, this.sinceTimeout, }); /// True when this answers a request that timed out rather than arriving /// unsolicited. bool get isLateReply => command != null; } /// Thrown when a CLI command's window closes with no reply. /// /// Carries the window that was actually armed, so callers can report the real /// figure. The previous message printed `(timeoutMs / 1000).ceil()`, so a /// 4074 ms window announced "timeout after 5 seconds": every window in /// (4000, 5000] reported the same 5, and the number shown was never the number /// used (#531). class RepeaterCommandTimeout implements Exception { final String command; final int timeoutMs; const RepeaterCommandTimeout({ required this.command, required this.timeoutMs, }); /// The armed window in seconds to one decimal, e.g. `28.7`. String get secondsText => (timeoutMs / 1000).toStringAsFixed(1); /// Non-localized fallback for logs and for callers without a /// [BuildContext]. UI paths should prefer [secondsText] with their own /// localized string. @override String toString() => 'Command timed out after $secondsText seconds'; } class _ExpiredCommand { final String command; final DateTime expiredAt; const _ExpiredCommand({required this.command, required this.expiredAt}); } class RepeaterCommandService { final MeshCoreConnector _connector; final Map> _pendingCommands = {}; final Map _commandTimeouts = {}; final Map _commandPrefixes = {}; final Map _pendingByPrefix = {}; final Map _expiredCommands = {}; final PrefixAllocator _prefixes = PrefixAllocator(); static const int maxRetries = 5; /// How long a timed-out command stays remembered so a reply arriving after /// its window can still be presented with the request it answers. static const Duration lateReplyRetention = Duration(minutes: 2); /// Invoked when a reply cannot be handed to a waiting command. /// /// Every such reply is already written to the app log by /// [_surfaceUnmatchedResponse], which is the diagnostics channel and is what /// keeps this from being a silent drop (#528). /// /// This sink exists for tests and for any future diagnostics view. **No /// screen wires it.** Late arrivals became ordinary noise once #529 widened /// the window, and the owner asked for them off the UI: log them, do not put /// them in front of users (#589). void Function(UnmatchedRepeaterResponse)? onUnmatchedResponse; /// When set, the late/unmatched log withholds the reply text and the command /// string, logging only lengths. The remote CLI console sets this so a /// slow `get wifi.pwd` reply never writes the password to the app log, the /// same guarantee [LocalCliService] gives (#999 review). Default false keeps /// the repeater path's existing diagnostics. bool redactUnmatchedReplyText = false; RepeaterCommandService(this._connector); /// Send a CLI command to a contact (a repeater/room server OR a companion) /// with automatic retries. Returns a future that completes when a response is /// received or after max retries. The message type is chosen by the target's /// role (#888/#889): a companion gets TXT_TYPE_CLI_COMMAND, a repeater/room /// server the universal CLI_DATA; the reply path is identical for both. Future sendCommand( Contact repeater, String command, { Function(String)? onResponse, Function(int)? onAttempt, int retries = maxRetries, }) async { final attemptCount = retries < 1 ? 1 : retries; final selection = await _connector.preparePathForContactSend(repeater); final attemptPrefixes = []; for (int attempt = 0; attempt < attemptCount; attempt++) { onAttempt?.call(attempt + 1); try { final response = await _sendCommandAttempt( repeater, command, selection, attempt, attemptPrefixes, ); // The caller has its answer, so a straggler from an earlier attempt of // this same command is noise rather than a lost response. for (final prefix in attemptPrefixes) { _expiredCommands.remove(prefix); } onResponse?.call(response); return response; } catch (e) { if (attempt == attemptCount - 1) rethrow; } } throw Exception('Command failed after $attemptCount attempts'); } Future _sendCommandAttempt( Contact repeater, String command, PathSelection selection, int attempt, List attemptPrefixes, ) async { final repeaterKey = repeater.publicKeyHex; final prefix = _nextPrefixToken(); final commandId = '${repeaterKey}_$prefix'; final completer = _registerPending(commandId, prefix); attemptPrefixes.add(prefix); try { final framedCommand = '$prefix$command'; final pathLengthValue = selection.useFlood ? -1 : selection.hopCount; final timestampSeconds = DateTime.now().millisecondsSinceEpoch ~/ 1000; _connector.trackRepeaterAck( contact: repeater, selection: selection, text: framedCommand, timestampSeconds: timestampSeconds, attempt: attempt, ); final frame = buildSendCliCommandFrame( repeater.publicKey, framedCommand, attempt: attempt, timestampSeconds: timestampSeconds, // The target split: companions take the 1.18 command type, repeaters and // room servers the universal CLI_DATA type (#888/#889). txtType: cliTxtTypeForContactType(repeater.type), ); final responseBytes = frame.length > maxFrameSize ? frame.length : maxFrameSize; final timeoutMs = _connector.calculateCliTimeout( pathLength: pathLengthValue, messageBytes: responseBytes, ); await _connector.sendFrame(frame); _commandTimeouts[commandId]?.cancel(); _commandTimeouts[commandId] = Timer( Duration(milliseconds: timeoutMs), () { final completer = _pendingCommands[commandId]; if (completer != null && !completer.isCompleted) { // Prune here as well as on receive. A run of commands that all // time out with no traffic coming back would otherwise accumulate // records until disposal, since handleResponse is the only other // place that prunes and it never runs (Gemini review). _pruneExpiredCommands(); // Remember what this prefix asked so a reply arriving after the // window can still reach the user with its question attached. _expiredCommands[prefix] = _ExpiredCommand( command: command, expiredAt: DateTime.now(), ); completer.completeError( RepeaterCommandTimeout(command: command, timeoutMs: timeoutMs), ); _cleanup(commandId); } }, ); } catch (e) { _cleanup(commandId); throw Exception('Failed to send command: $e'); } try { return await completer.future; } finally { _cleanup(commandId); } } /// Call this when a text message response is received from a repeater void handleResponse(Contact repeater, String responseText) { final repeaterKey = repeater.publicKeyHex; _pruneExpiredCommands(); // Shared `cc|` grammar (#882): same splitter the local frame-66 path uses. final correlated = splitCorrelated(responseText); final prefix = correlated.prefix; final responsePayload = correlated.payload; // A reply that carries a prefix is self-identifying. If that prefix matches // nothing pending, the answer belongs to a command that already completed // or expired, and forcing it onto whatever else is in flight would report // one command's output as another command's result (#532). Only a reply // with no prefix at all, which carries no correlation token, may fall back // to matching by repeater. final matchedId = prefix != null ? _pendingByPrefix[prefix] : null; final commandId = matchedId ?? (prefix != null ? '' : _solePendingFor(repeaterKey)); if (commandId.isNotEmpty) { final completer = _pendingCommands[commandId]; if (completer != null && !completer.isCompleted) { completer.complete(responsePayload); _cleanup(commandId); return; } } // Nothing is waiting for this reply. It is still a real answer from the // repeater, so it gets surfaced and logged rather than dropped (#528). _surfaceUnmatchedResponse(repeaterKey, prefix, responsePayload); } void _surfaceUnmatchedResponse( String repeaterKey, String? prefix, String responsePayload, ) { final expired = prefix != null ? _expiredCommands.remove(prefix) : null; final sinceTimeout = expired == null ? null : DateTime.now().difference(expired.expiredAt); if (expired != null) { final commandText = redactUnmatchedReplyText ? '(withheld)' : '"${expired.command}"'; final payloadText = redactUnmatchedReplyText ? '${responsePayload.length} chars, text withheld' : responsePayload; appLogger.warn( 'Late reply to $commandText from $repeaterKey arrived ' '${sinceTimeout!.inMilliseconds}ms after its window closed: ' '$payloadText', tag: 'RepeaterCommand', ); } else { final payloadText = redactUnmatchedReplyText ? '${responsePayload.length} chars, text withheld' : responsePayload; appLogger.warn( 'Reply from $repeaterKey matched no pending or recently expired ' 'command (prefix: ${prefix ?? 'none'}): $payloadText', tag: 'RepeaterCommand', ); } onUnmatchedResponse?.call( UnmatchedRepeaterResponse( repeaterKeyHex: repeaterKey, response: responsePayload, command: expired?.command, sinceTimeout: sinceTimeout, ), ); } /// The one command awaiting a reply from [repeaterKey], or `''` when that is /// ambiguous. /// /// An unprefixed reply carries no correlation token, so position is all that /// is left to match on. Firmware only echoes the prefix when the command is /// longer than four characters including it (`simple_repeater/MyMesh.cpp` /// `strlen(command) > 4 && command[2] == '|'`), so a very short command can /// legitimately answer without one and the fallback has to stay. /// /// It is only sound with exactly one candidate though. With two or more, /// "first" is map-iteration order, and choosing one would hand a caller /// another command's output. Ambiguity is surfaced instead of guessed. String _solePendingFor(String repeaterKey) { String? only; for (final id in _pendingCommands.keys) { if (!id.startsWith(repeaterKey)) continue; if (only != null) return ''; only = id; } return only ?? ''; } Completer _registerPending(String commandId, String prefix) { final completer = Completer(); _pendingCommands[commandId] = completer; _commandPrefixes[commandId] = prefix; _pendingByPrefix[prefix] = commandId; return completer; } /// Puts a command in flight without performing the send, so a test can set up /// two concurrent commands to one repeater. Deliberately routes through the /// same [_registerPending] the real send path uses, so the two cannot drift. @visibleForTesting Future registerPendingForTest(String repeaterKeyHex, String prefix) => _registerPending('${repeaterKeyHex}_$prefix', prefix).future; /// Records a timed-out command so a reply arriving later can still be /// attributed to it. Exposed because a test cannot drive a real /// send-then-time-out cycle without a connected transport. @visibleForTesting void recordExpiredCommandForTest(String prefix, String command) { _expiredCommands[prefix] = _ExpiredCommand( command: command, expiredAt: DateTime.now(), ); } void _pruneExpiredCommands() { if (_expiredCommands.isEmpty) return; final cutoff = DateTime.now().subtract(lateReplyRetention); _expiredCommands.removeWhere( (_, entry) => entry.expiredAt.isBefore(cutoff), ); } void _cleanup(String commandId) { _commandTimeouts[commandId]?.cancel(); _commandTimeouts.remove(commandId); _pendingCommands.remove(commandId); final prefix = _commandPrefixes.remove(commandId); if (prefix != null) { _pendingByPrefix.remove(prefix); } } void dispose() { for (final timer in _commandTimeouts.values) { timer.cancel(); } _commandTimeouts.clear(); _pendingCommands.clear(); _commandPrefixes.clear(); _pendingByPrefix.clear(); _expiredCommands.clear(); onUnmatchedResponse = null; } String _nextPrefixToken() => _prefixes.next(_pendingByPrefix.keys.toSet()); }