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; use thiserror::Error;
#[cfg(feature = "defmt")] #[cfg(feature = "defmt")]
use defmt::{error}; use defmt::error;
#[cfg(feature = "log")] #[cfg(feature = "log")]
use log::{error}; use log::error;
extern crate alloc; extern crate alloc;
use alloc::borrow::ToOwned;
use alloc::vec; use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use alloc::borrow::ToOwned;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Protocol300Transmission { pub enum Protocol300Transmission {
@@ -26,7 +26,7 @@ pub enum Protocol300Transmission {
/// 0x15 - Negative Acknowledgement, indicates an error in the last message /// 0x15 - Negative Acknowledgement, indicates an error in the last message
Nack, Nack,
/// A valid decoded message /// A valid decoded message
Message(Protocol300Message) Message(Protocol300Message),
} }
impl Protocol300Transmission { impl Protocol300Transmission {
@@ -42,14 +42,21 @@ impl Protocol300Transmission {
[0x05] => Ok(Self::Enq), [0x05] => Ok(Self::Enq),
[0x04] => Ok(Self::Eot), [0x04] => Ok(Self::Eot),
[0x15] => Ok(Self::Nack), [0x15] => Ok(Self::Nack),
[0x16] | [0x16, 0x00] => Err(FromBytesError::MessageParsing(
MessageDecodingError::LengthMismatch(),
)),
[0x16, 0x00, 0x00] => Ok(Self::Init), [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 => { 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})",
bytes.len(), bytes.len(),
bytes.first().unwrap_or(&0x00)); bytes.first().unwrap_or(&0x00)
);
Err(FromBytesError::InvalidBytes(b.to_owned())) Err(FromBytesError::InvalidBytes(b.to_owned()))
}, }
} }
} }
@@ -63,14 +70,21 @@ impl Protocol300Transmission {
[0x05, ..] => Ok(Self::Enq), [0x05, ..] => Ok(Self::Enq),
[0x04, ..] => Ok(Self::Eot), [0x04, ..] => Ok(Self::Eot),
[0x15, ..] => Ok(Self::Nack), [0x15, ..] => Ok(Self::Nack),
[0x16] | [0x16, 0x00] => Err(FromBytesError::MessageParsing(
MessageDecodingError::LengthMismatch(),
)),
[0x16, 0x00, 0x00, ..] => Ok(Self::Init), [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 => { 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})",
bytes.len(), bytes.len(),
bytes.first().unwrap_or(&0x00)); bytes.first().unwrap_or(&0x00)
);
Err(FromBytesError::InvalidBytes(b.to_owned())) Err(FromBytesError::InvalidBytes(b.to_owned()))
}, }
} }
} }
@@ -110,53 +124,86 @@ pub enum FromBytesError{
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{FromBytesError, FunctionCode, MessageDecodingError, MessageIdentifier, Protocol300Message, transmission::Protocol300Transmission}; use crate::{
FromBytesError, FunctionCode, MessageDecodingError, MessageIdentifier, Protocol300Message,
transmission::Protocol300Transmission,
};
#[test] #[test]
fn from_bytes_ack() { 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] #[test]
fn from_bytes_nack() { 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] #[test]
fn from_bytes_eot() { 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] #[test]
fn from_bytes_enq() { 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] #[test]
fn from_bytes_init() { 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] #[test]
fn from_bytes_init_incomplete_success() { 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] #[test]
fn from_bytes_init_incomplete_error() { fn from_bytes_init_incomplete_error() {
let res = Protocol300Transmission::try_from_bytes(&[0x16, 0x00, 0x00, 0x05]); 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] #[test]
fn from_bytes_message_incomplete_error() { 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]); let res = Protocol300Transmission::try_from_bytes(&[
assert_eq!(res, Err(FromBytesError::MessageParsing(MessageDecodingError::LengthMismatch()))); 0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9, 0x05,
]);
assert_eq!(
res,
Err(FromBytesError::MessageParsing(
MessageDecodingError::LengthMismatch()
))
);
} }
#[test] #[test]
fn from_bytes_message_incomplete_success() { 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 res = Protocol300Transmission::try_from_bytes_incomplete(&[
0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9, 0x05,
]);
let message = Protocol300Message { let message = Protocol300Message {
data_address: 0x5525, data_address: 0x5525,
telegram_length: 0x09, telegram_length: 0x09,
@@ -164,7 +211,7 @@ mod tests {
message_identifier: MessageIdentifier::Response, message_identifier: MessageIdentifier::Response,
data_length: 0x04, data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11], data: vec![0x07, 0x01, 0x27, 0x11],
checksum: 0xC9 checksum: 0xC9,
}; };
assert_eq!(Protocol300Transmission::Message(message), res.unwrap()); assert_eq!(Protocol300Transmission::Message(message), res.unwrap());
} }
@@ -178,10 +225,15 @@ mod tests {
message_identifier: MessageIdentifier::Response, message_identifier: MessageIdentifier::Response,
data_length: 0x04, data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11], 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]; let bytes = vec![
assert_eq!(Protocol300Transmission::Message(message), Protocol300Transmission::try_from_bytes(&bytes).unwrap()); 0x41, 0x09, 0x01, 0x01, 0x55, 0x25, 0x04, 0x07, 0x01, 0x27, 0x11, 0xC9,
];
assert_eq!(
Protocol300Transmission::Message(message),
Protocol300Transmission::try_from_bytes(&bytes).unwrap()
);
} }
#[test] #[test]
@@ -206,7 +258,10 @@ mod tests {
#[test] #[test]
fn to_bytes_init() { fn to_bytes_init() {
assert_eq!(Protocol300Transmission::Init.to_bytes(), &[0x16, 0x00, 0x00]); assert_eq!(
Protocol300Transmission::Init.to_bytes(),
&[0x16, 0x00, 0x00]
);
} }
#[test] #[test]
@@ -218,9 +273,14 @@ mod tests {
message_identifier: MessageIdentifier::Response, message_identifier: MessageIdentifier::Response,
data_length: 0x04, data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11], 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] #[test]
@@ -257,9 +317,12 @@ mod tests {
message_identifier: MessageIdentifier::Response, message_identifier: MessageIdentifier::Response,
data_length: 0x04, data_length: 0x04,
data: vec![0x07, 0x01, 0x27, 0x11], data: vec![0x07, 0x01, 0x27, 0x11],
checksum: 0xC9 checksum: 0xC9,
}; };
// telegram_length is 0x09, so expected length is 0x09 + 3 = 12 // 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
);
} }
} }