-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconcept2response.rs
311 lines (295 loc) · 10.7 KB
/
concept2response.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
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
/// Library for parsing vectors of bytes from the Concept2 machine into
/// Concept2Response structs.
use crate::consts;
use std::convert::TryInto;
/// All implemented (so far) responses that can be parsed from the machine.
/// Note `ProprietaryCommand`, which contains a variety of Concept2-specific
/// commands that are not part of the CSAFE specification.
#[derive(Debug, PartialEq, Eq)]
pub enum Concept2Response {
GetStatus,
GetVersion,
GetUserID(String),
GetSerialNumber(String),
GetOdometer(u32, u8),
ProprietaryCommand(Vec<Concept2ResponseProprietary>),
}
/// Proprietary commands that are not part of the CSAFE specification. These
/// commands are wrapped inside a special format byte. The Concept2 spec refers
/// to this idea as a "long command" - that is, commands containing more than one
/// byte.
#[derive(Debug, PartialEq, Eq)]
pub enum Concept2ResponseProprietary {
GetWorkTime(u32, u8),
GetWorkDistance(u32, u8),
GetWorkoutType(u8),
}
/// A struct that contains all of the parts of a *single* ResponseFrame.
/// When the bytes come back, each command is parsed as an identifier,
/// the number of bytes of data that are incoming, and the data. This
/// struct wraps those things to be parsed by the `parse` method.
pub struct ResponseFrame {
identifier: u8,
bytes: u8,
data: Vec<u8>,
}
impl ResponseFrame {
pub fn parse(self) -> Option<Concept2Response> {
match self.identifier {
consts::csafe_commands::GET_USER_ID => {
assert!(self.bytes == 5);
Some(Concept2Response::GetUserID(
String::from_utf8(self.data).expect("parse error"),
))
}
consts::csafe_commands::GET_SERIAL_NUMBER => {
assert!(self.bytes == 9);
Some(Concept2Response::GetSerialNumber(
String::from_utf8(self.data).expect("parse error"),
))
}
consts::csafe_commands::GET_ODOMETER => {
assert!(self.bytes == 5);
let distance: u32 = u32::from_le_bytes(
self.data
.iter()
.cloned()
.take(4)
.collect::<Vec<u8>>()
.as_slice()
.try_into()
.expect("incorrect slice length"),
);
let units: u8 = *self.data.last().unwrap();
Some(Concept2Response::GetOdometer(distance, units))
}
consts::csafe_commands::PROPRIETARY_COMMAND => parse_proprietary(self.data),
_ => None,
}
}
}
/// Because these proprietary responses are nested inside a regular response,
/// we need an additional function to parse them.
fn parse_proprietary(vec: Vec<u8>) -> Option<Concept2Response> {
let mut proprietary_vec: Vec<Concept2ResponseProprietary> = Vec::new();
let mut vec_iter = vec.into_iter();
while let Some(identifier) = vec_iter.next() {
match identifier {
consts::csafe_commands::GET_WORK_TIME => {
if vec_iter.next() != Some(5) {
return None;
}
proprietary_vec.push(Concept2ResponseProprietary::GetWorkTime(
u32::from_le_bytes(
vec_iter
.by_ref()
.take(4)
.collect::<Vec<u8>>()
.as_slice()
.try_into()
.expect("incorrect slice length"),
),
vec_iter.next().unwrap(),
));
}
consts::csafe_commands::GET_WORK_DISTANCE => {
if vec_iter.next() != Some(5) {
return None;
}
proprietary_vec.push(Concept2ResponseProprietary::GetWorkDistance(
u32::from_le_bytes(
vec_iter
.by_ref()
.take(4)
.collect::<Vec<u8>>()
.as_slice()
.try_into()
.expect("incorrect slice length"),
),
vec_iter.next().unwrap(),
));
}
consts::csafe_commands::GET_WORKOUT_TYPE => {
if vec_iter.next() != Some(1) {
return None;
}
proprietary_vec.push(Concept2ResponseProprietary::GetWorkoutType(
vec_iter.next().unwrap(),
))
}
_ => {
return None;
}
}
}
Some(Concept2Response::ProprietaryCommand(proprietary_vec))
}
/// Takes an iterator of bytes, takes a single chunk of bytes according to the
/// `bytes` field of the frame, and constructs a `ResponseFrame` struct to pass
/// to the `parse` method.
fn parse_c2r<'a, T>(iter: &mut T) -> Option<Concept2Response>
where
T: Iterator<Item = &'a u8>,
{
let identifier = iter.next();
let bytes = iter.next();
match (identifier, bytes) {
(Some(i), Some(b)) => {
let data: Vec<u8> = iter.take(usize::from(*b)).copied().collect();
if data.len() == usize::from(*b) {
ResponseFrame {
identifier: *i,
bytes: *b,
data,
}
.parse()
} else {
None
}
}
_ => None,
}
}
/// Applies `parse_c2r` on an iterator of bytes until no more `Concept2Response` structs can
/// be constructed from the bytes.
fn parse_helper<'a>(iter: &mut impl Iterator<Item = &'a u8>) -> Option<Vec<Concept2Response>> {
let mut result = vec![];
while let Some(c2r) = parse_c2r(iter) {
result.push(c2r);
}
Some(result)
}
fn checksum_iter<'a>(iter: impl Iterator<Item = &'a u8>) -> u8 {
iter.fold(0, |acc, &x| x ^ acc)
}
/// As mentioned in `concept2command.rs`, `0xf0` through `0xf3` are special control
/// bytes. As a result, the data fields cannot contain these bytes. So, the Concept2
/// machine replaces data bytes with these values with `0xf3`, followed by a number.
/// This function replaces these "stuffed" pairs of bytes with the actual data value.
fn unpack_bytes(v: &[u8]) -> Vec<u8> {
let mut vec_iter = v.iter();
// Skipping the report number and the start flag.
let mut result: Vec<u8> = vec_iter.by_ref().take(2).cloned().collect();
while let Some(x) = vec_iter.next() {
match x {
0xf2 => {
result.push(0xf2);
return result;
}
0xf3 => match vec_iter.next() {
Some(&0x00) => result.push(0xf0),
Some(&0x01) => result.push(0xf1),
Some(&0x02) => result.push(0xf2),
Some(&0x03) => result.push(0xf3),
_ => {
result.push(0xf3);
return result;
}
},
x => result.push(*x),
}
}
result
}
/// Single public method for taking a vector of bytes and
/// returning a vector of `Concept2Response` frames.
pub fn parse_vec(v: &[u8]) -> Option<Vec<Concept2Response>> {
let unpacked_vec: Vec<u8> = unpack_bytes(v);
let start_flag = unpacked_vec.get(1);
let end_flag = unpacked_vec.iter().rev().next();
let checksum = unpacked_vec.iter().rev().nth(1);
let length = unpacked_vec.len();
let actual_checksum = checksum_iter(unpacked_vec.iter().skip(2).take(length - 4));
match (
start_flag,
end_flag,
checksum.map(|x| *x == actual_checksum),
) {
(Some(&consts::CSAFE_START_FLAG), Some(&consts::CSAFE_STOP_FLAG), Some(true)) => {
parse_helper(&mut v.iter().skip(3).take(length - 5))
}
_ => None,
}
}
mod tests {
#[test]
fn test_parse_get_user_id() {
let v: Vec<u8> = vec![
0x1, 0xf1, 0x81, 0x92, 0x5, 0x30, 0x30, 0x30, 0x30, 0x30, 0x26, 0xf2,
];
assert_eq!(
Some(vec![super::Concept2Response::GetUserID(String::from(
"00000"
))]),
super::parse_vec(&v)
);
}
#[test]
fn test_parse_get_serial_number() {
let v: Vec<u8> = vec![
0x1, 0xf1, 0x81, 0x94, 0x9, 0x34, 0x33, 0x30, 0x32, 0x32, 0x38, 0x35, 0x32, 0x35, 0x21,
0xf2,
];
assert_eq!(
Some(vec![super::Concept2Response::GetSerialNumber(
String::from("430228525")
)]),
super::parse_vec(&v)
);
}
#[test]
fn test_parse_get_odometer() {
let v: Vec<u8> = vec![
0x1, 0xf1, 0x81, 0x9b, 0x5, 0xf4, 0x24, 0x21, 0x0, 0x24, 0xca, 0xf2,
];
assert_eq!(
Some(vec![super::Concept2Response::GetOdometer(2172148, 0x24)]),
super::parse_vec(&v)
);
}
#[test]
fn test_parse_get_work_distance() {
let v: Vec<u8> = vec![
0x1, 0xf1, 0x1, 0x1a, 0x7, 0xa3, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0xba, 0xf2,
];
assert_eq!(
Some(vec![super::Concept2Response::ProprietaryCommand(vec![
super::Concept2ResponseProprietary::GetWorkDistance(0, 0)
])]),
super::parse_vec(&v)
);
}
#[test]
fn test_parse_get_work_distance_and_workout_type() {
let v: Vec<u8> = vec![
0x1, 0xf1, 0x81, 0x1a, 0xa, 0xa3, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x89, 0x1, 0x8, 0xb7,
0xf2,
];
assert_eq!(
Some(vec![super::Concept2Response::ProprietaryCommand(vec![
super::Concept2ResponseProprietary::GetWorkDistance(0, 0),
super::Concept2ResponseProprietary::GetWorkoutType(8)
])]),
super::parse_vec(&v)
);
}
#[test]
fn test_compound_message() {
let v: Vec<u8> = vec![
0x1, 0xf1, 0x1, 0x1a, 0x11, 0xa0, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa3, 0x5, 0x0, 0x0,
0x0, 0x0, 0x0, 0x89, 0x1, 0x8, 0x94, 0x9, 0x34, 0x33, 0x30, 0x32, 0x32, 0x38, 0x35,
0x32, 0x35, 0x29, 0xf2,
];
assert_eq!(
Some(vec![
super::Concept2Response::ProprietaryCommand(vec![
super::Concept2ResponseProprietary::GetWorkTime(0, 0),
super::Concept2ResponseProprietary::GetWorkDistance(0, 0),
super::Concept2ResponseProprietary::GetWorkoutType(8)
]),
super::Concept2Response::GetSerialNumber(String::from("430228525"))
]),
super::parse_vec(&v)
);
}
}