Skip to main content

osdp/command/
keep_active.rs

1//! `osdp_KEEPACTIVE` (`0xA7`) — instruct the PD to keep its reader active.
2//!
3//! # Spec: §6.27
4//!
5//! Body is a 16-bit little-endian millisecond duration.
6
7use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11/// `osdp_KEEPACTIVE` body.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct KeepActive {
14    /// Hold duration in milliseconds.
15    pub duration_ms: u16,
16}
17
18impl KeepActive {
19    /// Encode.
20    pub fn encode(&self) -> Result<Vec<u8>, Error> {
21        Ok(self.duration_ms.to_le_bytes().to_vec())
22    }
23
24    /// Decode.
25    pub fn decode(data: &[u8]) -> Result<Self, Error> {
26        require_exact_len(data, 2, 0xA7)?;
27        Ok(Self {
28            duration_ms: u16::from_le_bytes([data[0], data[1]]),
29        })
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn roundtrip() {
39        let body = KeepActive {
40            duration_ms: 0x1234,
41        };
42        let bytes = body.encode().unwrap();
43        assert_eq!(bytes, [0x34, 0x12]);
44        assert_eq!(KeepActive::decode(&bytes).unwrap(), body);
45    }
46
47    #[test]
48    fn decode_rejects_wrong_length() {
49        assert!(matches!(
50            KeepActive::decode(&[0x10]),
51            Err(Error::PayloadLength { code: 0xA7, .. })
52        ));
53    }
54}