feat(transmission): add support for incomplete Protocol 300 messages
Add `try_from_bytes_incomplete` method to handle Protocol 300 transmissions that may contain extra trailing bytes. This improves robustness when parsing messages from unreliable sources or when dealing with partial data. The original `try_from_bytes` method remains strict, while the new method provides a more lenient parsing option for cases where strict validation isn't required. Also added a TODO comment for future validation of message length.
This commit is contained in:
@@ -41,6 +41,27 @@ impl Protocol300Transmission {
|
|||||||
[0x04] => Ok(Self::Eot),
|
[0x04] => Ok(Self::Eot),
|
||||||
[0x15] => Ok(Self::Nack),
|
[0x15] => Ok(Self::Nack),
|
||||||
[0x16, 0x00, 0x00] => Ok(Self::Init),
|
[0x16, 0x00, 0x00] => Ok(Self::Init),
|
||||||
|
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)),//TODO fail if too many bytes
|
||||||
|
b => {
|
||||||
|
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",
|
||||||
|
bytes.len(),
|
||||||
|
bytes.first().unwrap_or(&0x00));
|
||||||
|
Err(FromBytesError::InvalidBytes(b.to_owned()))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to convert bytes to a Protocol 300 Transmission, but ignores any bytes that are too much
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns a `FromBytesError` if the conversion fails in some way
|
||||||
|
pub fn try_from_bytes_incomplete(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||||
|
match bytes {
|
||||||
|
[0x06, ..] => Ok(Self::Ack),
|
||||||
|
[0x05, ..] => Ok(Self::Enq),
|
||||||
|
[0x04, ..] => Ok(Self::Eot),
|
||||||
|
[0x15, ..] => Ok(Self::Nack),
|
||||||
|
[0x16, 0x00, 0x00, ..] => Ok(Self::Init),
|
||||||
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)),
|
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)),
|
||||||
b => {
|
b => {
|
||||||
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",
|
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",
|
||||||
|
|||||||
Reference in New Issue
Block a user