-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreply.rs
125 lines (105 loc) · 4.12 KB
/
reply.rs
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
use crate::std;
use std::fmt;
use crate::{
impl_extended_reply_ops, impl_message_ops, impl_omnibus_extended_reply,
len::EXTENDED_NOTE_INHIBITS_REPLY_ALT, ExtendedCommand, ExtendedReplyOps, MessageOps,
MessageType, OmnibusReply,
};
pub mod index {
pub const DATA: usize = 4;
}
/// Set Extended Note Inhibits - Reply (Subtype 0x03)
/// Represents a reply for a [SetExtendedNoteInhibits](crate::SetExtendedNoteInhibits) command.
///
/// The device will reply with a standard Omnibus reply detailed in section 7.1.2 with no extended data.
pub type ExtendedNoteInhibitsReply = OmnibusReply;
/// Represents an alternate reply for a [SetExtendedNoteInhibits](crate::SetExtendedNoteInhibits) command.
///
/// In some firmware, an alternate reply is given. This reply also contains no extended data.
///
/// The Extended Note Inhibits Alternate Reply is formatted as follows:
///
/// | Name | STX | LEN | CTRL | Subtype | Data 0 | Data 1 | Data 2 | Data 3 | Data 4 | Data 5 | ETX | CHK |
/// |:------|:----:|:----:|:----:|:-------:|:------:|:------:|:------:|:------:|:------:|:------:|:----:|:---:|
/// | Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
/// | Value | 0x02 | 0x0C | 0x7n | 0x03 | nn | nn | nn | nn | nn | nn | 0x03 | zz |
///
/// **WARNING** In order to avoid possible confusion processing the extended note data, this command should
/// only be sent when the device is in the idle state.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ExtendedNoteInhibitsReplyAlt {
buf: [u8; EXTENDED_NOTE_INHIBITS_REPLY_ALT],
}
impl ExtendedNoteInhibitsReplyAlt {
/// Creates a new [ExtendedNoteInhibitsReplyAlt] message.
pub fn new() -> Self {
let mut message = Self {
buf: [0u8; EXTENDED_NOTE_INHIBITS_REPLY_ALT],
};
message.init();
message.set_message_type(MessageType::Extended);
message.set_extended_command(ExtendedCommand::SetExtendedNoteInhibits);
message
}
}
impl_message_ops!(ExtendedNoteInhibitsReplyAlt);
impl_omnibus_extended_reply!(ExtendedNoteInhibitsReplyAlt);
impl_extended_reply_ops!(ExtendedNoteInhibitsReplyAlt);
impl From<ExtendedNoteInhibitsReply> for ExtendedNoteInhibitsReplyAlt {
fn from(msg: ExtendedNoteInhibitsReply) -> Self {
use crate::index as omnibus_index;
let msg_buf = msg.buf();
let msg_etx_index = msg.etx_index();
let mut res = Self::new();
let res_etx_index = res.etx_index();
let res_buf = res.buf_mut();
res_buf[index::DATA..res_etx_index]
.copy_from_slice(msg_buf[omnibus_index::DATA..msg_etx_index].as_ref());
res.calculate_checksum();
res
}
}
impl fmt::Display for ExtendedNoteInhibitsReplyAlt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"AckNak: {}, DeviceType: {}, MessageType: {}, ExtendedCommand: {}, DeviceState: {}, DeviceStatus: {}, ExceptionStatus: {}, MiscDeviceState: {}, ModelNumber: {}, CodeRevision: {}",
self.acknak(),
self.device_type(),
self.message_type(),
self.extended_command(),
self.device_state(),
self.device_status(),
self.exception_status(),
self.misc_device_state(),
self.model_number(),
self.code_revision(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Result;
#[test]
#[rustfmt::skip]
fn test_extended_note_inhibits_reply_alt_from_bytes() -> Result<()> {
let msg_bytes = [
// STX | LEN | Message type | Subtype
0x02, 0x0c, 0x70, 0x03,
// Data
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// ETX | Checksum
0x03, 0x7f,
];
let mut msg = ExtendedNoteInhibitsReplyAlt::new();
msg.from_buf(msg_bytes.as_ref())?;
assert_eq!(msg.message_type(), MessageType::Extended);
assert_eq!(
msg.extended_command(),
ExtendedCommand::SetExtendedNoteInhibits
);
Ok(())
}
}