ebds/note_retrieved/
reply.rs

1use crate::std;
2use std::fmt;
3
4use crate::{
5    bool_enum, impl_extended_ops, impl_message_ops, impl_omnibus_extended_reply,
6    len::{NOTE_RETRIEVED_EVENT, NOTE_RETRIEVED_REPLY},
7    ExtendedCommand, ExtendedCommandOps, MessageOps, MessageType, OmnibusReplyOps,
8};
9
10pub const EVENT: u8 = 0x7f;
11
12bool_enum!(
13    RetrieveAckNak,
14    "Indicates success(0x01) / failure(0x00) of the NoteRetrievedCommand"
15);
16
17pub mod index {
18    pub const ACKNAK: usize = 10;
19    pub const EVENT: usize = 10;
20}
21
22/// Note Retrieved - Reply (Subtype 0x0B)
23///
24/// NoteRetrievedReply represents an immediate reply to the
25/// [NoteRetrievedCommand](crate::NoteRetrievedCommand).
26///
27/// The device will respond to the enable/disable command with an ACK or NAK.
28///
29/// The device ACKs with 0x01 if it can honor the hosts request to either enable or disable. The device will
30/// NAK the command if it is not supported for the current configuration. (ex. BNF is attached).
31///
32/// The Note Retrieved Reply is formatted as follows:
33///
34/// | Name  | STX  | LEN  | CTRL | Subtype | Data 0 | Data 1 | Data 2 | Data 3 | Data 4 | Data 5 | ACK/NAK | ETX  | CHK |
35/// |:------|:----:|:----:|:----:|:-------:|:------:|:------:|:------:|:------:|:------:|:------:|:-------:|:----:|:---:|
36/// | Byte  | 0    | 1    | 2    | 3       | 4      | 5      | 6      | 7      | 8      | 9      | 10      | 11   | 12  |
37/// | Value | 0x02 | 0x0D | 0x7n | 0x0B    | nn     | nn     | nn     | nn     | nn     | nn     | 0x01/00 | 0x03 | zz  |
38#[derive(Clone, Copy, Debug, Default, PartialEq)]
39pub struct NoteRetrievedReply {
40    buf: [u8; NOTE_RETRIEVED_REPLY],
41}
42
43impl NoteRetrievedReply {
44    pub fn new() -> Self {
45        let mut message = Self {
46            buf: [0u8; NOTE_RETRIEVED_REPLY],
47        };
48
49        message.init();
50        message.set_message_type(MessageType::Extended);
51        message.set_extended_command(ExtendedCommand::NoteRetrieved);
52
53        message
54    }
55
56    pub fn retrieved_acknak(&self) -> RetrieveAckNak {
57        self.buf[index::ACKNAK].into()
58    }
59
60    pub fn set_retrieved_acknak(&mut self, acknak: RetrieveAckNak) {
61        self.buf[index::ACKNAK] = acknak.into()
62    }
63}
64
65impl_message_ops!(NoteRetrievedReply);
66impl_omnibus_extended_reply!(NoteRetrievedReply);
67impl_extended_ops!(NoteRetrievedReply);
68
69impl fmt::Display for NoteRetrievedReply {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        write!(
72            f,
73            "AckNak: {}, DeviceType: {}, MessageType: {}, DeviceState: {}, DeviceStatus: {}, ExceptionStatus: {}, MiscDeviceState: {}, ModelNumber: {}, CodeRevision: {}, Retrieved AckNak: {}",
74            self.acknak(),
75            self.device_type(),
76            self.message_type(),
77            self.device_state(),
78            self.device_status(),
79            self.exception_status(),
80            self.misc_device_state(),
81            self.model_number(),
82            self.code_revision(),
83            self.retrieved_acknak(),
84        )
85    }
86}
87
88/// Note Retrieved - Event (Subtype 0x0B)
89///
90/// If the functionality has been enabled, the device will send out a message each time the note is removed
91/// after a return/reject.
92///
93/// The Note Retrieved Event is formatted as follows:
94///
95/// | Name  | STX  | LEN  | CTRL | Subtype | Data 0 | Data 1 | Data 2 | Data 3 | Data 4 | Data 5 | Event | ETX  | CHK |
96/// |:------|:----:|:----:|:----:|:-------:|:------:|:------:|:------:|:------:|:------:|:------:|:-----:|:----:|:---:|
97/// | Byte  | 0    | 1    | 2    | 3       | 4      | 5      | 6      | 7      | 8      | 9      | 10    | 11   | 12  |
98/// | Value | 0x02 | 0x0D | 0x7n | 0x0B    | nn     | nn     | nn     | nn     | nn     | nn     | 0x7F  | 0x03 | zz  |
99///
100/// The `0x7F` for the `Event byte signifies that the note has been removed by the user.
101#[derive(Clone, Copy, Debug, Default, PartialEq)]
102pub struct NoteRetrievedEvent {
103    buf: [u8; NOTE_RETRIEVED_EVENT],
104}
105
106impl NoteRetrievedEvent {
107    pub fn new() -> Self {
108        let mut message = Self {
109            buf: [0u8; NOTE_RETRIEVED_EVENT],
110        };
111
112        message.init();
113        message.set_message_type(MessageType::Extended);
114        message.set_extended_command(ExtendedCommand::NoteRetrieved);
115        message.buf[index::EVENT] = EVENT;
116
117        message
118    }
119
120    pub fn retrieved_event(&self) -> u8 {
121        self.buf[index::EVENT]
122    }
123}
124
125impl_message_ops!(NoteRetrievedEvent);
126impl_omnibus_extended_reply!(NoteRetrievedEvent);
127impl_extended_ops!(NoteRetrievedEvent);
128
129impl fmt::Display for NoteRetrievedEvent {
130    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131        write!(
132            f,
133            "AckNak: {}, DeviceType: {}, MessageType: {}, DeviceState: {}, DeviceStatus: {}, ExceptionStatus: {}, MiscDeviceState: {}, ModelNumber: {}, CodeRevision: {}, Retrieved Event: {}",
134            self.acknak(),
135            self.device_type(),
136            self.message_type(),
137            self.device_state(),
138            self.device_status(),
139            self.exception_status(),
140            self.misc_device_state(),
141            self.model_number(),
142            self.code_revision(),
143            self.retrieved_event(),
144        )
145    }
146}
147
148#[cfg(test)]
149mod tests {
150    use super::*;
151    use crate::Result;
152
153    #[test]
154    #[rustfmt::skip]
155    fn test_note_retrieved_reply_from_buf() -> Result<()> {
156        let msg_bytes = [
157            // STX | LEN | Message Type | Subtype
158            0x02, 0x0d, 0x70, 0x0b,
159            // Data
160            0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161            // ACK/NAK
162            0x01,
163            // ETX | Checksum
164            0x03, 0x77,
165        ];
166
167        let mut msg = NoteRetrievedReply::new();
168        msg.from_buf(msg_bytes.as_ref())?;
169
170        assert_eq!(msg.message_type(), MessageType::Extended);
171        assert_eq!(msg.extended_command(), ExtendedCommand::NoteRetrieved);
172        assert_eq!(msg.retrieved_acknak(), RetrieveAckNak::Set);
173
174        let msg_bytes = [
175            // STX | LEN | Message Type | Subtype
176            0x02, 0x0d, 0x70, 0x0b,
177            // Data
178            0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179            // ACK/NAK
180            0x00,
181            // ETX | Checksum
182            0x03, 0x76,
183        ];
184
185        msg.from_buf(msg_bytes.as_ref())?;
186
187        assert_eq!(msg.message_type(), MessageType::Extended);
188        assert_eq!(msg.extended_command(), ExtendedCommand::NoteRetrieved);
189        assert_eq!(msg.retrieved_acknak(), RetrieveAckNak::Unset);
190
191        Ok(())
192    }
193
194    #[test]
195    #[rustfmt::skip]
196    fn test_note_retrieved_event_from_buf() -> Result<()> {
197        let msg_bytes = [
198            // STX | LEN | Message Type | Subtype
199            0x02, 0x0d, 0x70, 0x0b,
200            // Data
201            0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202            // Event
203            0x7f,
204            // ETX | Checksum
205            0x03, 0x09,
206        ];
207
208        let mut msg = NoteRetrievedEvent::new();
209        msg.from_buf(msg_bytes.as_ref())?;
210
211        assert_eq!(msg.message_type(), MessageType::Extended);
212        assert_eq!(msg.extended_command(), ExtendedCommand::NoteRetrieved);
213        assert_eq!(msg.retrieved_event(), 0x7f);
214
215        let msg_bytes = [
216            // STX | LEN | Message Type | Subtype
217            0x02, 0x0d, 0x70, 0x0b,
218            // Data
219            0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220            // Event (any non-0x7f value is invalid)
221            0x7e,
222            // ETX | Checksum
223            0x03, 0x08,
224        ];
225
226        msg.from_buf(msg_bytes.as_ref())?;
227
228        assert_eq!(msg.retrieved_event(), 0x7e);
229
230        Ok(())
231    }
232}