pub mod request;
pub mod response;
pub use mbus_core::models::fifo_queue::*;
mod apis;
mod service;
#[cfg(test)]
mod tests {
use heapless::Vec;
use crate::services::fifo_queue::{request::ReqPduCompiler, response::ResponseParser};
use mbus_core::{
data_unit::common::Pdu, errors::MbusError, function_codes::public::FunctionCode,
};
#[test]
fn test_read_fifo_queue_request_valid() {
let pdu = ReqPduCompiler::read_fifo_queue_request(0x0001).unwrap();
assert_eq!(pdu.function_code(), FunctionCode::ReadFifoQueue);
assert_eq!(pdu.data().as_slice(), &[0x00, 0x01]);
assert_eq!(pdu.data_len(), 2);
}
#[test]
fn test_parse_read_fifo_queue_response_valid() {
let response_bytes = [0x18, 0x00, 0x04, 0x00, 0x01, 0x12, 0x34];
let pdu = Pdu::from_bytes(&response_bytes).unwrap();
let (values, count) = ResponseParser::parse_read_fifo_queue_response(&pdu).unwrap();
assert_eq!(&values[..count], &[0x1234]);
}
#[test]
fn test_parse_read_fifo_queue_response_multiple_registers() {
let response_bytes = [0x18, 0x00, 0x06, 0x00, 0x02, 0x12, 0x34, 0x56, 0x78];
let pdu = Pdu::from_bytes(&response_bytes).unwrap();
let (values, count) = ResponseParser::parse_read_fifo_queue_response(&pdu).unwrap();
assert_eq!(&values[..count], &[0x1234, 0x5678]);
}
#[test]
fn test_parse_read_fifo_queue_response_wrong_fc() {
let response_bytes = [0x03, 0x00, 0x04, 0x00, 0x01, 0x12, 0x34]; let pdu = Pdu::from_bytes(&response_bytes).unwrap();
assert_eq!(
ResponseParser::parse_read_fifo_queue_response(&pdu).unwrap_err(),
MbusError::InvalidFunctionCode
);
}
#[test]
fn test_parse_read_fifo_queue_response_data_too_short() {
let response_bytes = [0x18, 0x00, 0x04, 0x00]; let pdu = Pdu::from_bytes(&response_bytes).unwrap();
assert_eq!(
ResponseParser::parse_read_fifo_queue_response(&pdu).unwrap_err(),
MbusError::InvalidPduLength
);
}
#[test]
fn test_parse_read_fifo_queue_response_fifo_byte_count_mismatch() {
let response_bytes = [0x18, 0x00, 0x05, 0x00, 0x01, 0x12, 0x34, 0x00];
let pdu = Pdu::from_bytes(&response_bytes).unwrap();
assert_eq!(
ResponseParser::parse_read_fifo_queue_response(&pdu).unwrap_err(),
MbusError::ParseError
);
}
#[test]
fn test_parse_read_fifo_queue_response_fifo_count_mismatch() {
let response_bytes = [0x18, 0x00, 0x04, 0x00, 0x02, 0x12, 0x34]; let pdu = Pdu::from_bytes(&response_bytes).unwrap();
assert_eq!(
ResponseParser::parse_read_fifo_queue_response(&pdu).unwrap_err(),
MbusError::ParseError
);
}
#[test]
fn test_parse_read_fifo_queue_response_buffer_too_small_for_data() {
let fifo_count = 126;
let fifo_byte_count = 2 + (fifo_count * 2);
let mut response_pdu_bytes: Vec<u8, 512> = Vec::new();
for _ in 0..(1 + 2 + 2 + fifo_count * 2) {
response_pdu_bytes.push(0u8).unwrap();
}
response_pdu_bytes[0] = 0x18; response_pdu_bytes[1..3].copy_from_slice(&(fifo_byte_count as u16).to_be_bytes());
response_pdu_bytes[3..5].copy_from_slice(&(fifo_count as u16).to_be_bytes());
let result = Pdu::from_bytes(&response_pdu_bytes);
assert_eq!(result.unwrap_err(), MbusError::InvalidPduLength);
}
}