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 alloc::vec::Vec;
10
11/// `osdp_LSTATR` body.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct LStatR {
14    /// Tamper switch state. `0` = normal, `1` = tamper.
15    pub tamper: u8,
16    /// Power state. `0` = normal, `1` = power failure.
17    pub power: u8,
18}
19
20impl LStatR {
21    /// Encode.
22    pub fn encode(&self) -> Result<Vec<u8>, Error> {
23        Ok(alloc::vec![self.tamper, self.power])
24    }
25
26    /// Decode.
27    pub fn decode(data: &[u8]) -> Result<Self, Error> {
28        if data.len() != 2 {
29            return Err(Error::MalformedPayload {
30                code: 0x48,
31                reason: "LSTATR requires 2 bytes",
32            });
33        }
34        Ok(Self {
35            tamper: data[0],
36            power: data[1],
37        })
38    }
39}