Skip to main content

osdp/reply/
lstat.rs

1//! `osdp_LSTATR` (`0x48`) — local status report.
2//!
3//! # Spec: §7.6
4//!
5//! Body is exactly 2 bytes: `tamper`, `power`. `0` means normal, `1` means a
6//! fault.
7
8use crate::error::Error;
9use crate::payload_util::require_exact_len;
10use alloc::vec::Vec;
11
12/// `osdp_LSTATR` body.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct LStatR {
15    /// Tamper switch state. `0` = normal, `1` = tamper.
16    pub tamper: u8,
17    /// Power state. `0` = normal, `1` = power failure.
18    pub power: u8,
19}
20
21impl LStatR {
22    /// Encode.
23    pub fn encode(&self) -> Result<Vec<u8>, Error> {
24        Ok(alloc::vec![self.tamper, self.power])
25    }
26
27    /// Decode.
28    pub fn decode(data: &[u8]) -> Result<Self, Error> {
29        require_exact_len(data, 2, 0x48)?;
30        Ok(Self {
31            tamper: data[0],
32            power: data[1],
33        })
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn roundtrip() {
43        let body = LStatR {
44            tamper: 0,
45            power: 1,
46        };
47        let bytes = body.encode().unwrap();
48        assert_eq!(bytes, [0, 1]);
49        assert_eq!(LStatR::decode(&bytes).unwrap(), body);
50    }
51
52    #[test]
53    fn decode_rejects_wrong_length() {
54        assert!(matches!(
55            LStatR::decode(&[0]),
56            Err(Error::PayloadLength { code: 0x48, .. })
57        ));
58    }
59}