1use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct ComSet {
14 pub address: u8,
16 pub baud: u32,
18}
19
20impl ComSet {
21 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 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 #[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}