Skip to main content

ebds/
soft_reset.rs

1use crate::{
2    impl_aux_ops, impl_message_ops, len::SOFT_RESET, std::fmt, AuxCommand, AuxCommandOps,
3    MessageOps, MessageType,
4};
5
6pub mod index {
7    pub const DATA: usize = 3;
8    pub const DATA_END: usize = DATA + 2;
9}
10
11/// Acceptor Soft Reset: (Subtype 0x7F)
12///
13/// This command is used to reset the device. There is not necessarily a reply to this command, but some
14/// data may be sent by the device. The host system should ignore all data sent by the device for at least
15/// one second. Further, the device may take as much as fifteen seconds to return to normal operation after
16/// being reset and the host should poll, once per second, for at least fifteen seconds until the device
17/// replies.
18///
19/// The acceptor soft reset command takes the form:
20///
21/// | Name  | STX  | LEN  | CTRL | Data A | Data B | Command | ETX  | CHK |
22/// |:------|:----:|:----:|:----:|:------:|:------:|:-------:|:----:|:---:|
23/// | Byte  | 0    | 1    | 2    | 3      | 4      | 5       | 6    | 7   |
24/// | Value | 0x02 | 0x08 | 0x6n | 0x7F   | 0x7F   | 0x7F    | 0x03 | zz  |
25#[repr(C)]
26#[derive(Clone, Copy, Debug, Default, PartialEq)]
27pub struct SoftReset {
28    buf: [u8; SOFT_RESET],
29}
30
31impl SoftReset {
32    /// Creates a new [SoftReset] message.
33    pub fn new() -> Self {
34        let mut message = Self {
35            buf: [0u8; SOFT_RESET],
36        };
37
38        message.init();
39        message.set_message_type(MessageType::AuxCommand);
40        message.set_aux_command(AuxCommand::SoftReset);
41        message.set_reset_data();
42
43        message
44    }
45
46    fn set_reset_data(&mut self) {
47        self.buf[index::DATA..index::DATA_END].copy_from_slice([0x7f, 0x7f].as_ref());
48    }
49}
50
51impl_message_ops!(SoftReset);
52impl_aux_ops!(SoftReset);
53
54impl fmt::Display for SoftReset {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        write!(f, "{{")?;
57        write!(f, r#""acknak": {}, "#, self.acknak())?;
58        write!(f, r#""device_type": {}, "#, self.device_type())?;
59        write!(f, r#""message_type": {}, "#, self.message_type())?;
60        write!(f, r#""aux_command": {}"#, self.aux_command())?;
61        write!(f, "}}")
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::Result;
69
70    #[test]
71    #[rustfmt::skip]
72    fn test_soft_reset_from_bytes() -> Result<()> {
73        let msg_bytes = [
74            // STX | LEN | Message type | Subtype
75            0x02, 0x08, 0x60,
76            // Data
77            0x7f, 0x7f,
78            // Command
79            0x7f,
80            // ETX | Checksum
81            0x03, 0x17,
82        ];
83
84        let mut msg = SoftReset::new();
85        msg.from_buf(msg_bytes.as_ref())?;
86
87        assert_eq!(msg.message_type(), MessageType::AuxCommand);
88        assert_eq!(msg.aux_command(), AuxCommand::SoftReset);
89
90        Ok(())
91    }
92}