Skip to main content

osdp/reply/
ft_stat.rs

1//! `osdp_FTSTAT` (`0x7A`) — file-transfer status.
2//!
3//! # Spec: §7.19
4//!
5//! Body: `status (1) || delay (2 LE) || preferred_size (2 LE) || flags (2 LE)`.
6
7use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11/// `osdp_FTSTAT` body.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct FtStat {
14    /// Status code.
15    pub status: u8,
16    /// Update delay (ms) before next fragment is welcome.
17    pub delay_ms: u16,
18    /// Preferred fragment size in bytes.
19    pub preferred_size: u16,
20    /// Bit-flags (vendor-specific extensions).
21    pub flags: u16,
22}
23
24impl FtStat {
25    /// Encode.
26    pub fn encode(&self) -> Result<Vec<u8>, Error> {
27        let mut out = Vec::with_capacity(7);
28        out.push(self.status);
29        out.extend_from_slice(&self.delay_ms.to_le_bytes());
30        out.extend_from_slice(&self.preferred_size.to_le_bytes());
31        out.extend_from_slice(&self.flags.to_le_bytes());
32        Ok(out)
33    }
34
35    /// Decode.
36    pub fn decode(data: &[u8]) -> Result<Self, Error> {
37        require_exact_len(data, 7, 0x7A)?;
38        Ok(Self {
39            status: data[0],
40            delay_ms: u16::from_le_bytes([data[1], data[2]]),
41            preferred_size: u16::from_le_bytes([data[3], data[4]]),
42            flags: u16::from_le_bytes([data[5], data[6]]),
43        })
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn roundtrip() {
53        let body = FtStat {
54            status: 0x01,
55            delay_ms: 0x0064,
56            preferred_size: 0x0100,
57            flags: 0x0003,
58        };
59        let bytes = body.encode().unwrap();
60        assert_eq!(bytes, [0x01, 0x64, 0x00, 0x00, 0x01, 0x03, 0x00]);
61        assert_eq!(FtStat::decode(&bytes).unwrap(), body);
62    }
63
64    #[test]
65    fn decode_rejects_wrong_length() {
66        assert!(matches!(
67            FtStat::decode(&[0; 6]),
68            Err(Error::PayloadLength { code: 0x7A, .. })
69        ));
70    }
71}