Add better error logging
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use log::warn;
|
use log::{warn, error};
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
@@ -118,7 +118,13 @@ impl Protocol300Message {
|
|||||||
/// # Errors
|
/// # Errors
|
||||||
/// Returns an error if the data vec is longer than u8. This is also against the spec of Protocol 300, so it's safe to panic
|
/// Returns an error if the data vec is longer than u8. This is also against the spec of Protocol 300, so it's safe to panic
|
||||||
pub fn try_new_write_message(data_address: u16, data: &[u8]) -> Result<Self, MessageCreationError>{
|
pub fn try_new_write_message(data_address: u16, data: &[u8]) -> Result<Self, MessageCreationError>{
|
||||||
let data_length: u8 = data.len().try_into()?;
|
let data_length: u8 = match data.len().try_into() {
|
||||||
|
Ok(len) => len,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to create write message: data length {} exceeds maximum u8 value (255) for address 0x{:04X}", data.len(), data_address);
|
||||||
|
return Err(MessageCreationError::DataTooLong(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
let telegram_length: u8 = 5 + data_length;
|
let telegram_length: u8 = 5 + data_length;
|
||||||
let message_identifier = MessageIdentifier::Request;
|
let message_identifier = MessageIdentifier::Request;
|
||||||
let function_code = FunctionCode::VirtualWRITE;
|
let function_code = FunctionCode::VirtualWRITE;
|
||||||
@@ -150,20 +156,44 @@ impl Protocol300Message {
|
|||||||
/// - Checksum mismatch
|
/// - Checksum mismatch
|
||||||
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, MessageDecodingError> {
|
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, MessageDecodingError> {
|
||||||
if bytes.len() < 8 {
|
if bytes.len() < 8 {
|
||||||
|
error!("Failed to decode message: byte vector too short (length: {}, minimum required: 8)", bytes.len());
|
||||||
return Err(MessageDecodingError::VecTooShort)
|
return Err(MessageDecodingError::VecTooShort)
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes[0] != 0x41 {
|
if bytes[0] != 0x41 {
|
||||||
|
error!("Failed to decode message: invalid telegram start byte 0x{:02X}, expected 0x41", bytes[0]);
|
||||||
return Err(MessageDecodingError::InvalidTelegramStart(bytes[0]));
|
return Err(MessageDecodingError::InvalidTelegramStart(bytes[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
let telegram_length: u8 = bytes[1];
|
let telegram_length: u8 = bytes[1];
|
||||||
if u8::try_from(bytes.len())? != telegram_length + 3 {
|
let actual_length = match u8::try_from(bytes.len()) {
|
||||||
|
Ok(len) => len,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to decode message: byte vector length conversion error");
|
||||||
|
return Err(MessageDecodingError::NoLength(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if actual_length != telegram_length + 3 {
|
||||||
|
error!("Failed to decode message: length mismatch - advertised telegram length: {}, actual length: {}, expected: {}", telegram_length, actual_length, telegram_length + 3);
|
||||||
return Err(MessageDecodingError::LengthMismatch());
|
return Err(MessageDecodingError::LengthMismatch());
|
||||||
}
|
}
|
||||||
|
|
||||||
let message_identifier = MessageIdentifier::try_from(bytes[2])?;
|
let message_identifier = match MessageIdentifier::try_from(bytes[2]) {
|
||||||
let function_code = FunctionCode::try_from(bytes[3])?;
|
Ok(id) => id,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to decode message: invalid message identifier byte 0x{:02X}", bytes[2]);
|
||||||
|
return Err(MessageDecodingError::InvalidMessageIdentifier(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let function_code = match FunctionCode::try_from(bytes[3]) {
|
||||||
|
Ok(fc) => fc,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to decode message: invalid function code byte 0x{:02X}", bytes[3]);
|
||||||
|
return Err(MessageDecodingError::InvalidFunctionCode(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
let data_address = (u16::from(bytes[4]) << 8) | u16::from(bytes[5]);
|
let data_address = (u16::from(bytes[4]) << 8) | u16::from(bytes[5]);
|
||||||
|
|
||||||
let data_length = bytes[6];
|
let data_length = bytes[6];
|
||||||
@@ -193,6 +223,7 @@ impl Protocol300Message {
|
|||||||
|
|
||||||
// Fail if the checksums do not match
|
// Fail if the checksums do not match
|
||||||
if checksum != checksum_calculated {
|
if checksum != checksum_calculated {
|
||||||
|
error!("Failed to decode message: checksum mismatch - received: 0x{checksum:02X}, calculated: 0x{checksum_calculated:02X}, address: 0x{data_address:04X}, message_id: {message_identifier}, function: {function_code}");
|
||||||
return Err(MessageDecodingError::Checksum)
|
return Err(MessageDecodingError::Checksum)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use crate::{Protocol300Message, message};
|
use crate::{Protocol300Message, message};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
use log::error;
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
@@ -34,7 +35,12 @@ impl Protocol300Transmission {
|
|||||||
[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)?)),
|
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)),
|
||||||
_ => Err(FromBytesError::InvalidByte),
|
_ => {
|
||||||
|
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X?})",
|
||||||
|
bytes.len(),
|
||||||
|
bytes.first().unwrap_or(&0x00));
|
||||||
|
Err(FromBytesError::InvalidByte)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user