Skip to main content

osdp/reply/
com.rs

1//! `osdp_COM` (`0x54`) — communications configuration report.
2//!
3//! # Spec: §7.13
4//!
5//! Body is 5 bytes: `address (1) + baud (4 LE)` — same layout as
6//! `osdp_COMSET`.
7
8use crate::error::Error;
9use alloc::vec::Vec;
10
11/// `osdp_COM` body.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Com {
14    /// PD's address.
15    pub address: u8,
16    /// PD's baud rate.
17    pub baud: u32,
18}
19
20impl Com {
21    /// Encode.
22    pub fn encode(&self) -> Result<Vec<u8>, Error> {
23        let mut out = Vec::with_capacity(5);
24        out.push(self.address);
25        out.extend_from_slice(&self.baud.to_le_bytes());
26        Ok(out)
27    }
28
29    /// Decode.
30    pub fn decode(data: &[u8]) -> Result<Self, Error> {
31        if data.len() != 5 {
32            return Err(Error::MalformedPayload {
33                code: 0x54,
34                reason: "COM requires 5 bytes",
35            });
36        }
37        Ok(Self {
38            address: data[0],
39            baud: u32::from_le_bytes([data[1], data[2], data[3], data[4]]),
40        })
41    }
42}