Files
TuneBossPC/TuneBoss.Core/BLE/PacketParser.cs
T
2026-09-28 16:55:31 +08:00

169 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace TuneBoss.Core.BLE;
/// <summary>
/// Stateful byte-stream parser for TuneBoss BLE packets.
/// Extracted from Form1_BLEhelper.cs ProcessByte() / OnFullPacketReceived().
/// Thread-safe only when all calls are serialized (BLE processing loop does this).
/// </summary>
public sealed class PacketParser
{
public sealed record ParsedPacket(
byte DlCommand,
byte SeqNum,
byte DwCount,
byte TlResponse,
byte TlCommand,
byte TlLength,
byte[] Payload);
private readonly Action<ParsedPacket> _onPacketReceived;
// ── Parser state ─────────────────────────────────────────────────────────
private int _byteCycle;
private int _startSeqCycle;
private int _packetHeaderCycle;
private int _dWordByteTrack;
private int _dWordCheckSumTrack;
private readonly byte[] _startSeq = new byte[4];
private readonly byte[] _packetHeader = new byte[4];
private readonly List<byte> _dataBuffer = new();
private readonly List<byte> _csBuffer = new();
private byte _currDlCommand;
private byte _currDlSeqCount;
private byte _currDlDWordCount;
private byte _currTlResp;
private byte _currTlCommand;
private byte _currTlLength;
public PacketParser(Action<ParsedPacket> onPacketReceived)
{
_onPacketReceived = onPacketReceived;
}
public void Reset()
{
_byteCycle = 0;
_startSeqCycle = 0;
_packetHeaderCycle = 0;
_dWordByteTrack = 0;
_dWordCheckSumTrack = 0;
_dataBuffer.Clear();
_csBuffer.Clear();
_currDlCommand = 0;
_currDlSeqCount = 0;
_currDlDWordCount = 0;
_currTlResp = 0;
_currTlCommand = 0;
_currTlLength = 0;
}
public void ProcessByte(byte b)
{
// ── Zone 1: bytes 0–3 — preamble FF 00 FF 00 ─────────────────────
if (_byteCycle <= 3)
{
_startSeq[_startSeqCycle] = b;
if (_startSeqCycle == 3)
{
bool valid = _startSeq[0] == 0xFF && _startSeq[1] == 0x00
&& _startSeq[2] == 0xFF && _startSeq[3] == 0x00;
if (!valid) { Reset(); return; }
}
_startSeqCycle++;
_byteCycle++;
return;
}
// ── Zone 2: bytes 4–7 — packet header ────────────────────────────
if (_byteCycle <= 7)
{
_packetHeader[_packetHeaderCycle] = b;
if (_packetHeaderCycle == 3)
{
if (!HeaderIntegrityOk(_packetHeader)) { Reset(); return; }
_currDlCommand = _packetHeader[0];
_currDlSeqCount = _packetHeader[1];
_currDlDWordCount = _packetHeader[2];
}
_packetHeaderCycle++;
_byteCycle++;
return;
}
// ── Zone 3: no payload ────────────────────────────────────────────
if (_currDlDWordCount == 0) { Reset(); return; }
// ── Zone 4: bytes 8–11 — TL header (first DWord of payload) ──────
if (_byteCycle <= 11)
{
switch (_byteCycle)
{
case 8: _currTlResp = b; break;
case 9: _currTlCommand = b; break;
case 10: _currTlLength = b; break;
}
_dataBuffer.Add(b);
_byteCycle++;
return;
}
// ── Zone 5: bytes 12+ — payload bytes + checksum ─────────────────
_byteCycle++;
int payloadBytes = 4 * (_currDlDWordCount - 1); // DWords after the TL header
if (_dWordByteTrack < payloadBytes)
{
_dataBuffer.Add(b);
_dWordByteTrack++;
}
else if (_dWordCheckSumTrack < 4)
{
_csBuffer.Add(b);
_dWordCheckSumTrack++;
if (_dWordCheckSumTrack == 4)
OnFullPacketReceived();
}
}
private void OnFullPacketReceived()
{
byte[] data = _dataBuffer.ToArray();
byte[] cs = _csBuffer.ToArray();
// Verify checksum
var expected = Protocol.ChecksumHelper.GenChecksumDW(data);
var receivedCs = (uint)(cs[0] | (cs[1] << 8) | (cs[2] << 16) | (cs[3] << 24));
if (expected != receivedCs) { Reset(); return; }
var packet = new ParsedPacket(
DlCommand: _currDlCommand,
SeqNum: _currDlSeqCount,
DwCount: _currDlDWordCount,
TlResponse: _currTlResp,
TlCommand: _currTlCommand,
TlLength: _currTlLength,
Payload: data);
Reset();
_onPacketReceived(packet);
}
private static bool HeaderIntegrityOk(byte[] h)
{
byte sum = unchecked((byte)(h[0] + h[1] + h[2]));
byte invSum = unchecked((byte)~sum);
return invSum == h[3];
}
}