Maybe too hacky

This commit is contained in:
2026-01-31 16:21:22 +01:00
parent f9a72fe9a3
commit 49ec3abfd9

View File

@@ -1,17 +1,17 @@
use crate::{Protocol300Message, message};
use crate::{MessageDecodingError, Protocol300Message, message};
use thiserror::Error;
#[cfg(feature = "defmt")]
use defmt::{error};
use defmt::error;
#[cfg(feature = "log")]
use log::{error};
use log::error;
extern crate alloc;
use alloc::borrow::ToOwned;
use alloc::vec;
use alloc::vec::Vec;
use alloc::borrow::ToOwned;
#[derive(Debug, PartialEq, Eq)]
pub enum Protocol300Transmission {
@@ -26,12 +26,12 @@ pub enum Protocol300Transmission {
/// 0x15 - Negative Acknowledgement, indicates an error in the last message
Nack,
/// A valid decoded message
Message(Protocol300Message)
Message(Protocol300Message),
}
impl Protocol300Transmission {
/// Try to convert bytes to a Protocol 300 Transmission
///
///
/// # Errors
/// - `FromBytesError` if the conversion fails in some way
/// - `TooManyBytes` if the input slice has more bytes than the decoded transmission
@@ -42,19 +42,26 @@ impl Protocol300Transmission {
[0x05] => Ok(Self::Enq),
[0x04] => Ok(Self::Eot),
[0x15] => Ok(Self::Nack),
[0x16] | [0x16, 0x00] => Err(FromBytesError::MessageParsing(
MessageDecodingError::LengthMismatch(),
)),
[0x16, 0x00, 0x00] => Ok(Self::Init),
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes, false)?)),
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(
bytes, false,
)?)),
b => {
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",
bytes.len(),
bytes.first().unwrap_or(&0x00));
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> {
@@ -63,14 +70,21 @@ impl Protocol300Transmission {
[0x05, ..] => Ok(Self::Enq),
[0x04, ..] => Ok(Self::Eot),
[0x15, ..] => Ok(Self::Nack),
[0x16] | [0x16, 0x00] => Err(FromBytesError::MessageParsing(
MessageDecodingError::LengthMismatch(),
)),
[0x16, 0x00, 0x00, ..] => Ok(Self::Init),
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes, true)?)),
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(
bytes, true,
)?)),
b => {
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",
bytes.len(),
bytes.first().unwrap_or(&0x00));
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()))
},
}
}
}
@@ -99,7 +113,7 @@ impl Protocol300Transmission {
/// Errors that can occur when getting a Protocol 300 Transmission from bytes
#[derive(Error, Debug, PartialEq, Eq)]
pub enum FromBytesError{
pub enum FromBytesError {
#[error("Failed to parse message")]
MessageParsing(#[from] message::MessageDecodingError),
#[error("Invalid bytes in vec")]
@@ -110,78 +124,116 @@ pub enum FromBytesError{
#[cfg(test)]
mod tests {
use crate::{FromBytesError, FunctionCode, MessageDecodingError, MessageIdentifier, Protocol300Message, transmission::Protocol300Transmission};
use crate::{
FromBytesError, FunctionCode, MessageDecodingError, MessageIdentifier, Protocol300Message,
transmission::Protocol300Transmission,
};
#[test]
fn from_bytes_ack() {
assert_eq!(Protocol300Transmission::Ack, Protocol300Transmission::try_from_bytes(&[0x06]).unwrap());
assert_eq!(
Protocol300Transmission::Ack,
Protocol300Transmission::try_from_bytes(&[0x06]).unwrap()
);
}
#[test]
fn from_bytes_nack() {
assert_eq!(Protocol300Transmission::Nack, Protocol300Transmission::try_from_bytes(&[0x15]).unwrap());
assert_eq!(
Protocol300Transmission::Nack,
Protocol300Transmission::try_from_bytes(&[0x15]).unwrap()
);
}
#[test]
fn from_bytes_eot() {
assert_eq!(Protocol300Transmission::Eot, Protocol300Transmission::try_from_bytes(&[0x04]).unwrap());
assert_eq!(
Protocol300Transmission::Eot,
Protocol300Transmission::try_from_bytes(&[0x04]).unwrap()
);
}
#[test]
fn from_bytes_enq() {
assert_eq!(Protocol300Transmission::Enq, Protocol300Transmission::try_from_bytes(&[0x05]).unwrap());
assert_eq!(
Protocol300Transmission::Enq,
Protocol300Transmission::try_from_bytes(&[0x05]).unwrap()
);
}
#[test]
fn from_bytes_init() {
assert_eq!(Protocol300Transmission::Init, Protocol300Transmission::try_from_bytes(&[0x16, 0x00, 0x00]).unwrap());
assert_eq!(
Protocol300Transmission::Init,
Protocol300Transmission::try_from_bytes(&[0x16, 0x00, 0x00]).unwrap()
);
}
#[test]
fn from_bytes_init_incomplete_success() {
assert_eq!(Protocol300Transmission::Init, Protocol300Transmission::try_from_bytes_incomplete(&[0x16, 0x00, 0x00, 0x05]).unwrap());
assert_eq!(
Protocol300Transmission::Init,
Protocol300Transmission::try_from_bytes_incomplete(&[0x16, 0x00, 0x00, 0x05]).unwrap()
);
}
#[test]
fn from_bytes_init_incomplete_error() {
let res = Protocol300Transmission::try_from_bytes(&[0x16, 0x00, 0x00, 0x05]);
assert_eq!(res, Err(FromBytesError::InvalidBytes(vec![0x16, 0x00, 0x00, 0x05])));
assert_eq!(
res,
Err(FromBytesError::InvalidBytes(vec![0x16, 0x00, 0x00, 0x05]))
);
}
#[test]
fn from_bytes_message_incomplete_error() {
let res = Protocol300Transmission::try_from_bytes(&[0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9, 0x05]);
assert_eq!(res, Err(FromBytesError::MessageParsing(MessageDecodingError::LengthMismatch())));
let res = Protocol300Transmission::try_from_bytes(&[
0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9, 0x05,
]);
assert_eq!(
res,
Err(FromBytesError::MessageParsing(
MessageDecodingError::LengthMismatch()
))
);
}
#[test]
fn from_bytes_message_incomplete_success() {
let res = Protocol300Transmission::try_from_bytes_incomplete(&[0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9, 0x05]);
let message = Protocol300Message{
let res = Protocol300Transmission::try_from_bytes_incomplete(&[
0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9, 0x05,
]);
let message = Protocol300Message {
data_address: 0x5525,
telegram_length: 0x09,
function_code: FunctionCode::VirtualREAD,
message_identifier: MessageIdentifier::Response,
data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11],
checksum: 0xC9
checksum: 0xC9,
};
assert_eq!(Protocol300Transmission::Message(message), res.unwrap());
}
#[test]
fn from_bytes_message() {
let message = Protocol300Message{
let message = Protocol300Message {
data_address: 0x5525,
telegram_length: 0x09,
function_code: FunctionCode::VirtualREAD,
message_identifier: MessageIdentifier::Response,
data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11],
checksum: 0xC9
checksum: 0xC9,
};
let bytes = vec![0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9];
assert_eq!(Protocol300Transmission::Message(message), Protocol300Transmission::try_from_bytes(&bytes).unwrap());
let bytes = vec![
0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9,
];
assert_eq!(
Protocol300Transmission::Message(message),
Protocol300Transmission::try_from_bytes(&bytes).unwrap()
);
}
#[test]
@@ -206,21 +258,29 @@ mod tests {
#[test]
fn to_bytes_init() {
assert_eq!(Protocol300Transmission::Init.to_bytes(), &[0x16, 0x00, 0x00]);
assert_eq!(
Protocol300Transmission::Init.to_bytes(),
&[0x16, 0x00, 0x00]
);
}
#[test]
fn to_bytes_message() {
let message = Protocol300Message{
let message = Protocol300Message {
data_address: 0x5525,
telegram_length: 0x09,
function_code: FunctionCode::VirtualREAD,
message_identifier: MessageIdentifier::Response,
data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11],
checksum: 0xC9
checksum: 0xC9,
};
assert_eq!(Protocol300Transmission::Message(message).to_bytes(), &[0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9]);
assert_eq!(
Protocol300Transmission::Message(message).to_bytes(),
&[
0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9
]
);
}
#[test]
@@ -250,16 +310,19 @@ mod tests {
#[test]
fn length_in_bytes_message() {
let message = Protocol300Message{
let message = Protocol300Message {
data_address: 0x5525,
telegram_length: 0x09,
function_code: FunctionCode::VirtualREAD,
message_identifier: MessageIdentifier::Response,
data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11],
checksum: 0xC9
checksum: 0xC9,
};
// telegram_length is 0x09, so expected length is 0x09 + 3 = 12
assert_eq!(Protocol300Transmission::Message(message).length_in_bytes(), 12);
assert_eq!(
Protocol300Transmission::Message(message).length_in_bytes(),
12
);
}
}