Skip to main content

osdp/command/
comset.rs

1//! `osdp_COMSET` (`0x6E`) — set PD address and baud rate.
2//!
3//! # Spec: §6.13
4//!
5//! Body is 5 bytes: `addr (1)` + `baud (4 LE)`.
6
7use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11/// `osdp_COMSET` body.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct ComSet {
14    /// New PD address (`0x00..=0x7E`).
15    pub address: u8,
16    /// New bit rate (e.g. 9600, 115200).
17    pub baud: u32,
18}
19
20impl ComSet {
21    /// Encode.
22    pub fn encode(&self) -> Result<Vec<u8>, Error> {
23        if self.address > crate::MAX_PD_ADDR {
24            return Err(Error::BadAddress(self.address));
25        }
26        let mut out = Vec::with_capacity(5);
27        out.push(self.address);
28        out.extend_from_slice(&self.baud.to_le_bytes());
29        Ok(out)
30    }
31
32    /// Decode.
33    pub fn decode(data: &[u8]) -> Result<Self, Error> {
34        require_exact_len(data, 5, 0x6E)?;
35        Ok(Self {
36            address: data[0],
37            baud: u32::from_le_bytes([data[1], data[2], data[3], data[4]]),
38        })
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    /// Annex E vector: COMSET broadcast → `addr=0x00`, `baud=9600 (0x2580)`.
46    #[test]
47    fn annex_e_vector() {
48        let body = ComSet {
49            address: 0x00,
50            baud: 9600,
51        };
52        let bytes = body.encode().unwrap();
53        assert_eq!(bytes, [0x00, 0x80, 0x25, 0x00, 0x00]);
54    }
55}