-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.rs
248 lines (217 loc) · 6.44 KB
/
device.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
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
//! The device protocol covers communication related to device information and
//! control and firmware updates, usually over TCP.
//! Everything is little endian.
use byteorder::{ByteOrder, LittleEndian};
use core::fmt;
pub const DEFAULT_PORT: u16 = 32101;
pub const SOCKET_BUFFER_LEN: usize = MemoryRegion::MAX_CHUCK_SIZE + 256;
/// Commands are received by the device.
/// The device always responds with a `StatusCode`, possibly
/// followed by a response type.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum Command {
/// Request device information.
/// This command also causes the device to reset its connection after sending a response.
/// It can be used to abort an in-progress update too.
/// Request type: None
/// Response type: json string
Info,
/// Read a region of FLASH memory.
/// Request type: MemoryReadRequest
/// Response type: [u8]
ReadMemory,
/// Write a region of FLASH memory.
/// Request type: MemoryWriteRequest followed by [u8] data
/// Response type: None
WriteMemory,
/// Erase a region of FLASH memory.
/// Address and length must match one of the boot slots (entire region/all sectors).
/// Request type: MemoryEraseRequest
/// Response type: None
EraseMemory,
/// Mark the update as complete and schedule a system reboot.
/// If there was no update in-progress, then this simply reboots the device.
/// Request type: None
/// Response type: None
CompleteAndReboot,
/// Unknown command.
/// The device will always response with StatusCode::UnknownCommand.
/// Request type: None
/// Response type: None
Unknown(u32),
}
impl Command {
pub const WIRE_SIZE: usize = 4;
pub fn from_le_bytes_unchecked(value: &[u8]) -> Self {
Command::from(LittleEndian::read_u32(value))
}
pub fn from_le_bytes(value: &[u8]) -> Option<Self> {
if value.len() >= 4 {
Some(Self::from_le_bytes_unchecked(value))
} else {
None
}
}
}
impl From<u32> for Command {
fn from(value: u32) -> Self {
use Command::*;
match value {
1 => Info,
2 => ReadMemory,
3 => WriteMemory,
4 => EraseMemory,
5 => CompleteAndReboot,
_ => Unknown(value),
}
}
}
impl From<Command> for u32 {
fn from(value: Command) -> Self {
use Command::*;
match value {
Info => 1,
ReadMemory => 2,
WriteMemory => 3,
EraseMemory => 4,
CompleteAndReboot => 5,
Unknown(v) => v,
}
}
}
impl fmt::Display for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct MemoryRegion {
/// Address of the memory region
pub address: u32,
/// Size in bytes of the region
pub length: u32,
}
impl MemoryRegion {
pub const WIRE_SIZE: usize = 8;
pub const MAX_CHUCK_SIZE: usize = 1024;
pub fn new_unchecked(address: u32, length: u32) -> Self {
Self { address, length }
}
pub fn check_length(&self) -> Result<(), StatusCode> {
if self.length % 4 != 0 {
Err(StatusCode::LengthNotMultiple4)
} else if self.length > Self::MAX_CHUCK_SIZE as u32 {
Err(StatusCode::LengthTooLong)
} else if self.length == 0 {
Err(StatusCode::DataLengthIncorrect)
} else {
Ok(())
}
}
pub fn to_le_bytes(self) -> [u8; 8] {
let addr = self.address.to_le_bytes();
let len = self.length.to_le_bytes();
[
addr[0], addr[1], addr[2], addr[3], len[0], len[1], len[2], len[3],
]
}
}
pub type MemoryReadRequest = MemoryRegion;
pub type MemoryWriteRequest = MemoryRegion;
pub type MemoryEraseRequest = MemoryRegion;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum StatusCode {
Success,
UnknownCommand,
InvalidAddress,
LengthNotMultiple4,
LengthTooLong,
DataLengthIncorrect,
EraseError,
WriteError,
FlashError,
NetworkError,
InternalError,
CommandLengthIncorrect,
Unknown(u32),
}
impl StatusCode {
pub fn from_le_bytes_unchecked(value: &[u8]) -> Self {
StatusCode::from(LittleEndian::read_u32(value))
}
pub fn from_le_bytes(value: &[u8]) -> Option<Self> {
if value.len() >= 4 {
Some(Self::from_le_bytes_unchecked(value))
} else {
None
}
}
pub fn is_success(&self) -> bool {
matches!(self, StatusCode::Success)
}
}
impl From<u32> for StatusCode {
fn from(value: u32) -> Self {
use StatusCode::*;
match value {
0 => Success,
1 => UnknownCommand,
2 => InvalidAddress,
3 => LengthNotMultiple4,
4 => LengthTooLong,
5 => DataLengthIncorrect,
6 => EraseError,
7 => WriteError,
8 => FlashError,
9 => NetworkError,
10 => InternalError,
11 => CommandLengthIncorrect,
_ => Unknown(value),
}
}
}
impl From<StatusCode> for u32 {
fn from(value: StatusCode) -> Self {
use StatusCode::*;
match value {
Success => 0,
UnknownCommand => 1,
InvalidAddress => 2,
LengthNotMultiple4 => 3,
LengthTooLong => 4,
DataLengthIncorrect => 5,
EraseError => 6,
WriteError => 7,
FlashError => 8,
NetworkError => 9,
InternalError => 10,
CommandLengthIncorrect => 11,
Unknown(v) => v,
}
}
}
impl fmt::Display for StatusCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip_wire_command() {
for in_c in 0..0xFF_u32 {
let in_c_bytes = in_c.to_le_bytes();
let c = Command::from_le_bytes(&in_c_bytes).unwrap();
assert_eq!(in_c, u32::from(c));
}
}
#[test]
fn round_trip_status_code() {
for in_c in 0..0xFF_u32 {
let in_c_bytes = in_c.to_le_bytes();
let c = StatusCode::from_le_bytes(&in_c_bytes).unwrap();
assert_eq!(in_c, u32::from(c));
}
}
}