import 'dart:typed_data'; const int usbSerialTxFrameStart = 0x3c; const int usbSerialRxFrameStart = 0x3e; const int usbSerialHeaderLength = 3; // Max companion frame the decoder will accept, matching the firmware's // MAX_FRAME_SIZE (BaseSerialInterface.h = 176, "+4 for transport codes"). This // was 172, which silently rejected full-size frames, exposed by caplog chunks // (176 B), the first feature to use the full frame. Normal traffic stays <=172. const int usbSerialMaxPayloadLength = 176; Uint8List wrapUsbSerialTxFrame(Uint8List payload) { if (payload.length > usbSerialMaxPayloadLength) { throw ArgumentError.value( payload.length, 'payload.length', 'USB serial payload exceeds $usbSerialMaxPayloadLength bytes', ); } final packet = Uint8List(usbSerialHeaderLength + payload.length); packet[0] = usbSerialTxFrameStart; packet[1] = payload.length & 0xff; packet[2] = (payload.length >> 8) & 0xff; packet.setRange(usbSerialHeaderLength, packet.length, payload); return packet; } class UsbSerialDecodedPacket { const UsbSerialDecodedPacket({ required this.frameStart, required this.payload, }); final int frameStart; final Uint8List payload; bool get isRxFrame => frameStart == usbSerialRxFrameStart; } /// Bytes the decoder could not frame and discarded (#711). Lost bytes mean /// lost frames, so callers log this as an error rather than drop it quietly. class UsbSerialSkipReport { const UsbSerialSkipReport({required this.count, required this.sample}); final int count; /// The first discarded bytes, for the log. final List sample; String describe() { final hex = sample .map((b) => b.toRadixString(16).padLeft(2, '0')) .join(' '); return 'discarded $count byte(s) that could not be framed ' '(first: $hex${count > sample.length ? ' ...' : ''})'; } } class UsbSerialFrameDecoder { static const int _skipSampleLength = 16; final List _rxBuffer = []; int _startIndex = 0; int _skipped = 0; final List _skipSample = []; void reset() { _rxBuffer.clear(); _startIndex = 0; _skipped = 0; _skipSample.clear(); } /// Returns what was discarded since the last call, or null if nothing was. UsbSerialSkipReport? takeSkipReport() { if (_skipped == 0) return null; final report = UsbSerialSkipReport( count: _skipped, sample: List.unmodifiable(_skipSample), ); _skipped = 0; _skipSample.clear(); return report; } void _skipByte() { _skipped++; if (_skipSample.length < _skipSampleLength) { _skipSample.add(_rxBuffer[_startIndex]); } _startIndex++; } List ingest(Uint8List bytes) { if (bytes.isEmpty) { return const []; } _rxBuffer.addAll(bytes); final packets = []; while (true) { if (_startIndex >= _rxBuffer.length) { _rxBuffer.clear(); _startIndex = 0; return packets; } if (_rxBuffer[_startIndex] != usbSerialRxFrameStart && _rxBuffer[_startIndex] != usbSerialTxFrameStart) { _skipByte(); _compactBufferIfNeeded(); continue; } final availableLength = _rxBuffer.length - _startIndex; if (availableLength < usbSerialHeaderLength) { _compactBufferIfNeeded(force: true); return packets; } final payloadLength = _rxBuffer[_startIndex + 1] | (_rxBuffer[_startIndex + 2] << 8); if (payloadLength > usbSerialMaxPayloadLength) { _skipByte(); _compactBufferIfNeeded(); continue; } final packetLength = usbSerialHeaderLength + payloadLength; if (availableLength < packetLength) { _compactBufferIfNeeded(force: true); return packets; } final frameStart = _rxBuffer[_startIndex]; final payload = Uint8List.fromList( _rxBuffer.sublist( _startIndex + usbSerialHeaderLength, _startIndex + packetLength, ), ); _startIndex += packetLength; _compactBufferIfNeeded(); packets.add( UsbSerialDecodedPacket(frameStart: frameStart, payload: payload), ); } } void _compactBufferIfNeeded({bool force = false}) { if (_startIndex == 0) { return; } if (!force && _startIndex < 1024 && _startIndex < (_rxBuffer.length ~/ 2)) { return; } _rxBuffer.removeRange(0, _startIndex); _startIndex = 0; } }