Skip to main content

mbus_core/models/fifo_queue/
mod.rs

1//! # Modbus FIFO Queue Models
2//!
3//! This module provides the data structures and logic for handling **Read FIFO Queue**
4//! (Function Code 0x18).
5//!
6//! A FIFO (First-In-First-Out) queue in Modbus is a specialized structure where a set of
7//! registers can be read from a single pointer address. This is often used for
8//! data logging or buffering where multiple data points are collected and read in bulk.
9//!
10//! ## Key Features
11//! - **Fixed-Size Storage**: Uses a fixed-size array to store up to 31 registers,
12//!   aligning with the Modbus protocol limits.
13//! - **no_std Compatible**: Designed for embedded systems without heap allocation.
14//! - **Safe Access**: Provides a clean API to retrieve the pointer address and the
15//!   sequence of register values.
16
17mod model;
18pub use model::*;
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23
24    /// Tests the creation of a new `FifoQueue` and verifies its initial state.
25    #[test]
26    fn test_fifo_queue_new() {
27        let ptr_addr = 0x04D2; // 1234
28        let fifo = FifoQueue::new(ptr_addr);
29
30        assert_eq!(fifo.ptr_address(), ptr_addr);
31        assert_eq!(fifo.length(), 0);
32        assert_eq!(fifo.queue().len(), 0);
33    }
34
35    /// Tests loading values into the FIFO queue and verifying the data integrity.
36    #[test]
37    fn test_fifo_queue_with_values() {
38        let mut values = [0u16; MAX_FIFO_QUEUE_COUNT_PER_PDU];
39        values[0] = 0xAAAA;
40        values[1] = 0xBBBB;
41        values[2] = 0xCCCC;
42
43        let fifo = FifoQueue::new(100).with_values(values, 3);
44
45        assert_eq!(fifo.length(), 3);
46        assert_eq!(fifo.queue(), &values[..3]);
47        assert_eq!(fifo.queue()[0], 0xAAAA);
48    }
49
50    /// Tests that the FIFO queue correctly handles and clamps lengths exceeding the PDU limit.
51    #[test]
52    fn test_fifo_queue_overflow_protection() {
53        let values = [1u16; MAX_FIFO_QUEUE_COUNT_PER_PDU];
54
55        // Attempt to set a length of 50, which exceeds the protocol limit of 31
56        let fifo = FifoQueue::new(100).with_values(values, 50);
57
58        // The internal logic should clamp the length to MAX_FIFO_QUEUE_COUNT_PER_PDU
59        assert_eq!(fifo.length(), MAX_FIFO_QUEUE_COUNT_PER_PDU);
60        assert_eq!(fifo.queue().len(), MAX_FIFO_QUEUE_COUNT_PER_PDU);
61    }
62}