From eca78453d6322287e130662db5c84e915033d503 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Mon, 23 Mar 2026 22:26:51 -0700 Subject: [PATCH] Remove debug print statements from MeshCoreConnector, MessageRetryService, and UsbSerialService and fix wrong retry being credited --- lib/connector/meshcore_connector.dart | 2 +- lib/screens/chat_screen.dart | 2 +- lib/services/message_retry_service.dart | 25 ++++++++++++++------- lib/services/usb_serial_service_native.dart | 22 +++++++++--------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 7208454..d489238 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -2839,7 +2839,7 @@ class MeshCoreConnector extends ChangeNotifier { _bleDebugLogService?.logFrame(frame, outgoing: false); final code = frame[0]; - debugPrint('RX frame: code=$code len=${frame.length}'); + // debugPrint('RX frame: code=$code len=${frame.length}'); switch (code) { case respCodeOk: diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 2caddcd..f990412 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -1697,7 +1697,7 @@ class _MessageBubble extends StatelessWidget { child: Text( context.l10n.chat_retryCount( message.retryCount, - 4, + context.read().settings.maxMessageRetries, ), style: TextStyle( fontSize: 10, diff --git a/lib/services/message_retry_service.dart b/lib/services/message_retry_service.dart index c8e89aa..5bc8812 100644 --- a/lib/services/message_retry_service.dart +++ b/lib/services/message_retry_service.dart @@ -21,11 +21,16 @@ class _AckHistoryEntry { }); } -/// (messageId, timestamp, attemptIndex) — stored per ACK hash for O(1) lookup. +/// (messageId, timestamp, attemptIndex, pathSelection) — stored per ACK hash +/// for O(1) lookup. [pathSelection] snapshots the route used for this +/// specific attempt so that a late PUSH_CODE_SEND_CONFIRMED credits the +/// correct path even when the message has since been retried on a different +/// route. typedef AckHashMapping = ({ String messageId, DateTime timestamp, int attemptIndex, + PathSelection? pathSelection, }); class RetryServiceConfig { @@ -382,6 +387,7 @@ class MessageRetryService extends ChangeNotifier { messageId: messageId, timestamp: DateTime.now(), attemptIndex: message.retryCount, + pathSelection: _selectionFromMessage(message), ); // Add this ACK hash to the list of expected ACKs for this message (for history) @@ -395,14 +401,11 @@ class MessageRetryService extends ChangeNotifier { int actualTimeout = timeoutMs; if (config.calculateTimeout != null) { - final calculated = config.calculateTimeout!( + actualTimeout = config.calculateTimeout!( pathLengthValue, message.text.length, contactKey: contact.publicKeyHex, ); - if (timeoutMs <= 0 || calculated < timeoutMs) { - actualTimeout = calculated; - } } final updatedMessage = message.copyWith( @@ -569,6 +572,7 @@ class MessageRetryService extends ChangeNotifier { final config = _config; String? matchedMessageId; int? matchedAttemptIndex; + PathSelection? matchedPathSelection; final ackHashHex = ackHash.toRadixString(16).padLeft(8, '0'); // Clean up old ACK hash mappings (older than 15 minutes) @@ -588,6 +592,7 @@ class MessageRetryService extends ChangeNotifier { if (mapping != null) { matchedMessageId = mapping.messageId; matchedAttemptIndex = mapping.attemptIndex; + matchedPathSelection = mapping.pathSelection; } else { config?.debugLogService?.warn( 'PUSH_CODE_SEND_CONFIRMED: ACK hash $ackHashHex not found in direct mapping, trying fallback', @@ -618,13 +623,13 @@ class MessageRetryService extends ChangeNotifier { } final contact = _pendingContacts[matchedMessageId]; final ackedAttempt = matchedAttemptIndex ?? message.retryCount; - final selection = _selectionFromMessage(message); + final selection = matchedPathSelection ?? _selectionFromMessage(message); final shortText = message.text.length > 20 ? '${message.text.substring(0, 20)}...' : message.text; config?.debugLogService?.info( - 'PUSH_CODE_SEND_CONFIRMED: ACK hash $ackHashHex ✓ "$shortText" delivered to ${contact?.name ?? "unknown"} on retry ${ackedAttempt + 1} in ${tripTimeMs}ms', + 'PUSH_CODE_SEND_CONFIRMED: ACK hash $ackHashHex ✓ "$shortText" delivered to ${contact?.name ?? "unknown"} on attempt $ackedAttempt in ${tripTimeMs}ms', tag: 'AckHash', ); @@ -636,6 +641,8 @@ class MessageRetryService extends ChangeNotifier { tripTimeMs: tripTimeMs, ); + final wasAlreadyResolved = _resolvedMessages.contains(matchedMessageId); + _cleanupMessage(matchedMessageId); config?.updateMessage(deliveredMessage); @@ -658,7 +665,9 @@ class MessageRetryService extends ChangeNotifier { tripTimeMs, ); } - _onMessageResolved(matchedMessageId, contact.publicKeyHex); + if (!wasAlreadyResolved) { + _onMessageResolved(matchedMessageId, contact.publicKeyHex); + } } notifyListeners(); diff --git a/lib/services/usb_serial_service_native.dart b/lib/services/usb_serial_service_native.dart index 40861db..9c8af85 100644 --- a/lib/services/usb_serial_service_native.dart +++ b/lib/services/usb_serial_service_native.dart @@ -273,7 +273,7 @@ class UsbSerialService { throw StateError('USB serial port is not open'); } final packet = wrapUsbSerialTxFrame(data); - _logFrameSummary('USB TX frame', data); + // _logFrameSummary('USB TX frame', data); if (_useAndroidUsbHost) { try { await _androidMethodChannel.invokeMethod('write', { @@ -447,16 +447,16 @@ class UsbSerialService { await _frameController.close(); } - void _logFrameSummary(String prefix, Uint8List bytes) { - if (bytes.isEmpty) { - _debugLogService?.info('$prefix len=0', tag: 'USB Serial'); - return; - } - _debugLogService?.info( - '$prefix code=${bytes[0]} len=${bytes.length}', - tag: 'USB Serial', - ); - } + // void _logFrameSummary(String prefix, Uint8List bytes) { + // if (bytes.isEmpty) { + // _debugLogService?.info('$prefix len=0', tag: 'USB Serial'); + // return; + // } + // _debugLogService?.info( + // '$prefix code=${bytes[0]} len=${bytes.length}', + // tag: 'USB Serial', + // ); + // } /// Returns an ordered list of port paths to try for [portName]. ///