Enhance USB serial services with debug logging and reset functionality

- Introduced debug logging in USB serial services for better traceability.
- Added reset method to UsbSerialFrameDecoder to clear buffered data.
- Updated tests to verify the reset functionality of the decoder.
This commit is contained in:
just_stuff_tm
2026-03-02 18:54:12 -05:00
committed by just-stuff-tm
parent c2f544eeba
commit 4c7ee3b3b0
5 changed files with 153 additions and 30 deletions
+19 -4
View File
@@ -5,6 +5,7 @@ import 'dart:js_interop_unsafe';
import 'package:flutter/foundation.dart';
import 'package:web/web.dart' as web;
import 'app_debug_log_service.dart';
import '../utils/usb_port_labels.dart';
import 'usb_serial_frame_codec.dart';
@@ -27,6 +28,7 @@ class UsbSerialService {
String? _connectedPortName;
String? _connectedPortKey;
String _requestPortLabel = 'Choose USB Device';
AppDebugLogService? _debugLogService;
UsbSerialStatus get status => _status;
String? get activePortKey => _connectedPortKey;
@@ -69,6 +71,7 @@ class UsbSerialService {
}
_status = UsbSerialStatus.connecting;
_frameDecoder.reset();
try {
final requestedPortName = normalizeUsbPortName(portName);
@@ -88,7 +91,10 @@ class UsbSerialService {
_status = UsbSerialStatus.connected;
unawaited(_pumpReads());
debugPrint('USB serial opened port=$_connectedPortName via Web Serial');
_debugLogService?.info(
'USB serial opened port=$_connectedPortName via Web Serial',
tag: 'USB Serial',
);
} catch (error) {
await _cleanupFailedConnect();
_status = UsbSerialStatus.disconnected;
@@ -126,6 +132,7 @@ class UsbSerialService {
_port = null;
_connectedPortName = null;
_connectedPortKey = null;
_frameDecoder.reset();
if (reader != null) {
try {
@@ -169,6 +176,10 @@ class UsbSerialService {
_requestPortLabel = trimmed;
}
void setDebugLogService(AppDebugLogService? service) {
_debugLogService = service;
}
void dispose() {
unawaited(disconnect().whenComplete(_closeFrameController));
}
@@ -407,8 +418,9 @@ class UsbSerialService {
void _ingestRawBytes(Uint8List bytes) {
for (final packet in _frameDecoder.ingest(bytes)) {
if (!packet.isRxFrame) {
debugPrint(
_debugLogService?.info(
'USB ignored packet start=0x${packet.frameStart.toRadixString(16).padLeft(2, '0')} len=${packet.payload.length}',
tag: 'USB Serial',
);
continue;
}
@@ -439,10 +451,13 @@ class UsbSerialService {
void _logFrameSummary(String prefix, Uint8List bytes) {
if (bytes.isEmpty) {
debugPrint('$prefix len=0');
_debugLogService?.info('$prefix len=0', tag: 'USB Serial');
return;
}
debugPrint('$prefix code=${bytes[0]} len=${bytes.length}');
_debugLogService?.info(
'$prefix code=${bytes[0]} len=${bytes.length}',
tag: 'USB Serial',
);
}
}