ebds 0.4.2

Messages and related types for implementing the EBDS serial communication protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::std;
use std::fmt;

use crate::{
    impl_message_ops, impl_omnibus_nop_reply, len::QUERY_DEVICE_CAPABILITIES_REPLY, MessageOps,
    MessageType,
};

pub mod index {
    pub const CAP0: usize = 3;
    pub const CAP1: usize = 4;
    pub const CAP2: usize = 5;
    pub const CAP3: usize = 6;
    pub const CAP4: usize = 7;
    pub const CAP5: usize = 8;
}

bitfield! {
    /// First set of device capabilties
    #[derive(Clone, Copy, Debug, Default, PartialEq)]
    pub struct Cap0(u8);
    u8;
    /// **OBSOLETE** Extended PUP mode is supported.
    pub extended_pup_mode, _: 0;
    /// Extended orientation handling is supported.
    pub extended_orientation, _: 1;
    /// [QueryApplicationId](crate::QueryApplicationIdCommand) and [QueryVariantId](crate::QueryVariantIdCommand) are supported
    pub application_and_variant_id, _: 2;
    /// QueryBNFStatus is supported.
    pub bnf_status, _: 3;
    /// Test documents are supported.
    pub test_documents, _: 4;
    /// Set Bezel is supported
    pub bezel, _: 5;
    /// Easitrax is supported (with Query Asset Number).
    pub easitrax, _: 6;
}

impl fmt::Display for Cap0 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        write!(f, r#""extended_pup_mode":{},"#, self.extended_pup_mode())?;
        write!(
            f,
            r#""extended_orientation":{},"#,
            self.extended_orientation()
        )?;
        write!(
            f,
            r#""application_and_variant_id":{},"#,
            self.application_and_variant_id()
        )?;
        write!(f, r#""bnf_status":{},"#, self.bnf_status())?;
        write!(f, r#""test_documents":{},"#, self.test_documents())?;
        write!(f, r#""bezel":{},"#, self.bezel())?;
        write!(f, r#""easitrax":{}"#, self.easitrax())?;
        write!(f, "}}")
    }
}

impl From<u8> for Cap0 {
    fn from(b: u8) -> Self {
        Self(b & 0b111_1111)
    }
}

impl From<&Cap0> for u8 {
    fn from(c: &Cap0) -> Self {
        c.0
    }
}

impl From<Cap0> for u8 {
    fn from(c: Cap0) -> Self {
        (&c).into()
    }
}

bitfield! {
    /// Second set of device capabilties
    #[derive(Clone, Copy, Debug, Default, PartialEq)]
    pub struct Cap1(u8);
    u8;
    /// Note Retrieved is supported.
    pub note_retrieved, _: 0;
    /// Advanced Bookmark mode is supported. (see 7.5.9)
    pub advanced_bookmark, _: 1;
    /// Device capable of ABDS download.
    pub abds_download, _: 2;
    /// Device supports Clear Audit Command. (see 7.5.23)
    pub clear_audit, _: 3;
    /// Multi-note escrow is supported.
    pub multi_note_escrow, _: 4;
    /// 32-bit Unix timestamp is supported.
    pub unix_timestamp_32bit, _: 5;
}

impl fmt::Display for Cap1 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        write!(f, r#""note_retrieved":{},"#, self.note_retrieved())?;
        write!(f, r#""advanced_bookmark":{},"#, self.advanced_bookmark())?;
        write!(f, r#""abds_download\":{},"#, self.abds_download())?;
        write!(f, r#""clear_audit\":{},"#, self.clear_audit())?;
        write!(f, r#"multi_note_escrow\":{},"#, self.multi_note_escrow())?;
        write!(
            f,
            r#""unix_timestamp_32bit":{}"#,
            self.unix_timestamp_32bit()
        )?;
        write!(f, "}}")
    }
}

impl From<u8> for Cap1 {
    fn from(b: u8) -> Self {
        Self(b & 0b11_1111)
    }
}

impl From<&Cap1> for u8 {
    fn from(c: &Cap1) -> Self {
        c.0
    }
}

impl From<Cap1> for u8 {
    fn from(c: Cap1) -> Self {
        (&c).into()
    }
}

bitfield! {
    /// Third set of device capabilties
    ///
    /// Note: **SCR Classification** If banknote classification is supported (i.e. Cap Byte 3, bit 1 is set), all denomination
    /// recycling bits will be set to 0.
    #[derive(Clone, Copy, Debug, Default, PartialEq)]
    pub struct Cap2(u8);
    u8;
    /// 1 Denomination recycling is supported.
    pub one_denom_recycling, _: 0;
    /// 2 Denomination recycling is supported.
    pub two_denom_recycling, _: 1;
    /// 3 Denomination recycling is supported.
    pub three_denom_recycling, _: 2;
    /// 4 Denomination recycling is supported.
    pub four_denom_recycling, _: 3;
    /// **Retail Only** Improperly Seated Head Detection is supported.
    pub improperly_seated_head_detection, _: 4;
    /// **SCR** Host Controlled Recycler Inventory (Mixed Denomintion Recycling) is supported.
    pub mixed_denom_recycling, _: 6;
}

impl fmt::Display for Cap2 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        write!(
            f,
            r#""one_denom_recycling":{},"#,
            self.one_denom_recycling()
        )?;
        write!(
            f,
            r#""two_denom_recycling":{},"#,
            self.two_denom_recycling()
        )?;
        write!(
            f,
            r#""three_denom_recycling\":{},"#,
            self.three_denom_recycling()
        )?;
        write!(
            f,
            r#""four_denom_recycling\":{},"#,
            self.four_denom_recycling()
        )?;
        write!(
            f,
            r#""improperly_seated_head_detection":{},"#,
            self.improperly_seated_head_detection()
        )?;
        write!(
            f,
            r#""mixed_denom_recycling":{}"#,
            self.mixed_denom_recycling()
        )?;
        write!(f, "}}")
    }
}

impl From<u8> for Cap2 {
    fn from(b: u8) -> Self {
        Self(b & 0b101_1111)
    }
}

impl From<&Cap2> for u8 {
    fn from(c: &Cap2) -> Self {
        c.0
    }
}

impl From<Cap2> for u8 {
    fn from(c: Cap2) -> Self {
        (&c).into()
    }
}

bitfield! {
    /// Fourth set of device capabilties
    #[derive(Clone, Copy, Debug, Default, PartialEq)]
    pub struct Cap3(u8);
    u8;
    /// Customer Configuration Options Set/Query (Msg 6 Subtypes 0x25 and 0x26) are supported.
    pub customer_config, _: 0;
    /// **SCR Classification** Banknote classification is supported.
    pub banknote_classification, _: 1;
}

impl fmt::Display for Cap3 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            r#"{{"customer_config":{},"banknote_classification":{}}}"#,
            self.customer_config(),
            self.banknote_classification(),
        )
    }
}

impl From<u8> for Cap3 {
    fn from(b: u8) -> Self {
        Self(b & 0x000_0011)
    }
}

impl From<&Cap3> for u8 {
    fn from(c: &Cap3) -> Self {
        c.0
    }
}

impl From<Cap3> for u8 {
    fn from(c: Cap3) -> Self {
        (&c).into()
    }
}

bitfield! {
    /// Fifth set of device capabilties
    #[derive(Clone, Copy, Debug, Default, PartialEq)]
    pub struct Cap4(u8);
    u8;
    /// RFU
    pub reserved, _: 6, 0;
}

impl fmt::Display for Cap4 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, r#"{{"reserved": 0b{:08b}}}"#, self.reserved())
    }
}

impl From<u8> for Cap4 {
    fn from(b: u8) -> Self {
        Self(b & 0b111_1111)
    }
}

impl From<&Cap4> for u8 {
    fn from(c: &Cap4) -> Self {
        c.0
    }
}

impl From<Cap4> for u8 {
    fn from(c: Cap4) -> Self {
        (&c).into()
    }
}

bitfield! {
    /// Sixth set of device capabilties
    #[derive(Clone, Copy, Debug, Default, PartialEq)]
    pub struct Cap5(u8);
    u8;
    /// RFU
    pub reserved, _: 6, 0;
}

impl fmt::Display for Cap5 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, r#"{{"reserved": 0b{:08b}}}"#, self.reserved())
    }
}

impl From<u8> for Cap5 {
    fn from(b: u8) -> Self {
        Self(b & 0b111_1111)
    }
}

impl From<&Cap5> for u8 {
    fn from(c: &Cap5) -> Self {
        c.0
    }
}

impl From<Cap5> for u8 {
    fn from(c: Cap5) -> Self {
        (&c).into()
    }
}

/// Query Device Capabilities - Reply (Subtype 0x0D)
///
/// This the reply for the query of device capabilities:
/// [QueryDeviceCapabilitiesCommand](crate::QueryDeviceCapabilitiesCommand).
///
/// The Query Device Capabilities Reply is formatted as follows:
///
/// | Name  | STX  | LEN  | CTRL | Cap 0 | Cap 1 | Cap 2 | Cap 3 | Cap 4 | Cap 5 | ETX  | CHK |
/// |:------|:----:|:----:|:----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:----:|:---:|
/// | Byte  | 0    | 1    | 2    | 3     | 4     | 5     | 6     | 7     | 8     | 9    | 10  |
/// | Value | 0x02 | 0x0B | 0x6n | nn    | nn    | nn    | nn    | nn    | nn    | 0x03 | zz  |
///
/// The `Cap` fields are bitfields describing device capabilities:
///
/// * [Cap0]
/// * [Cap1]
/// * [Cap2]
/// * [Cap3]
/// * [Cap4] - RFU
/// * [Cap5] - RFU
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct QueryDeviceCapabilitiesReply {
    buf: [u8; QUERY_DEVICE_CAPABILITIES_REPLY],
}

impl QueryDeviceCapabilitiesReply {
    /// Create a new [QueryDeviceCapabilitiesReply] message
    pub fn new() -> Self {
        let mut message = Self {
            buf: [0u8; QUERY_DEVICE_CAPABILITIES_REPLY],
        };

        message.init();
        message.set_message_type(MessageType::AuxCommand);

        message
    }

    /// Get the first set of capabilities
    pub fn cap0(&self) -> Cap0 {
        self.buf[index::CAP0].into()
    }

    /// Get the second set of capabilities
    pub fn cap1(&self) -> Cap1 {
        self.buf[index::CAP1].into()
    }

    /// Get the third set of capabilities
    pub fn cap2(&self) -> Cap2 {
        self.buf[index::CAP2].into()
    }

    /// Get the fourth set of capabilities
    pub fn cap3(&self) -> Cap3 {
        self.buf[index::CAP3].into()
    }

    /// Get the fifth set of capabilities
    pub fn cap4(&self) -> Cap4 {
        self.buf[index::CAP4].into()
    }

    /// Get the sixth set of capabilities
    pub fn cap5(&self) -> Cap5 {
        self.buf[index::CAP5].into()
    }
}

impl_message_ops!(QueryDeviceCapabilitiesReply);
impl_omnibus_nop_reply!(QueryDeviceCapabilitiesReply);

impl fmt::Display for QueryDeviceCapabilitiesReply {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "AckNak: {}, DeviceType: {}, MessageType: {}, Capability set 0: {}, Capability set 1: {}, Capability set 2: {}, Capability set 3: {}, Capability set 4: {}, Capability set 5: {}",
            self.acknak(),
            self.device_type(),
            self.message_type(),
            self.cap0(),
            self.cap1(),
            self.cap2(),
            self.cap3(),
            self.cap4(),
            self.cap5(),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Result;

    #[test]
    #[rustfmt::skip]
    fn test_query_device_capabilities_reply_from_buf() -> Result<()> {
        let msg_bytes = [
            // STX | LEN | Message Type
            0x02, 0x0b, 0x60,
            // Capabilities
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            // ETX | Checksum
            0x03, 0x6b,
        ];

        let mut msg = QueryDeviceCapabilitiesReply::new();
        msg.from_buf(msg_bytes.as_ref())?;

        assert_eq!(msg.message_type(), MessageType::AuxCommand);

        assert_eq!(msg.cap0(), Cap0::from(0));
        assert_eq!(msg.cap1(), Cap1::from(0));
        assert_eq!(msg.cap2(), Cap2::from(0));
        assert_eq!(msg.cap3(), Cap3::from(0));
        assert_eq!(msg.cap4(), Cap4::from(0));
        assert_eq!(msg.cap5(), Cap5::from(0));

        Ok(())
    }
}