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
use alloc::vec;
use alloc::vec::Vec;
use cobs::{decode, encode, max_encoding_length};
use bxcan::{Data, ExtendedId, Frame as BxFrame, Id};
#[derive(Debug, PartialEq)]
pub enum FrameId {
LastFrameId(u16),
CurrentFrameId(u16),
}
#[derive(Debug, PartialEq)]
pub enum FrameError {
FrameIsStandard,
FrameIsRemote,
FrameIdMissing,
WrongSize,
CobsError,
}
#[derive(Debug, PartialEq)]
pub struct Frame {
pub not_error_flag: bool,
pub start_frame_flag: bool,
pub multi_frame_flag: bool,
pub frame_id: FrameId,
pub device_address: u16,
pub data_len: u8,
pub data: [u8; 8],
}
impl Frame {
pub fn from_bxcan_frame(frame: BxFrame) -> Result<Self, FrameError> {
if let Id::Extended(id) = frame.id() {
let id = id.as_raw();
let not_error_flag = ((id >> 28) & 0x0001) != 0;
let start_frame_flag = ((id >> 27) & 0x0001) != 0;
let multi_frame_flag = ((id >> 26) & 0x0001) != 0;
let frame_id_nibble = ((id >> 16) & 0x000f) as u16;
let device_address = ((id >> 0) & 0xffff) as u16;
if let Some(frame_data) = frame.data() {
let data_len = frame.dlc();
let mut data = [0u8; 8];
for i in 0..(data_len as usize) {
data[i] = frame_data[i];
}
if multi_frame_flag {
if data_len == 0 {
return Err(FrameError::FrameIdMissing);
}
let frame_id = if start_frame_flag {
FrameId::LastFrameId((frame_id_nibble << 8) | data[0] as u16)
} else {
FrameId::CurrentFrameId((frame_id_nibble << 8) | data[0] as u16)
};
Ok(Frame {
not_error_flag,
start_frame_flag,
multi_frame_flag,
frame_id,
device_address,
data_len,
data,
})
} else {
let start_frame_flag = true;
let frame_id = FrameId::LastFrameId(0x00);
Ok(Frame {
not_error_flag,
start_frame_flag,
multi_frame_flag,
frame_id,
device_address,
data_len,
data,
})
}
} else {
Err(FrameError::FrameIsRemote)
}
} else {
Err(FrameError::FrameIsStandard)
}
}
pub fn to_bxcan_frame(&self) -> BxFrame {
let mut id = 0x00;
id |= (self.not_error_flag as u32) << 28;
id |= (self.start_frame_flag as u32) << 27;
id |= (self.multi_frame_flag as u32) << 26;
match self.frame_id {
FrameId::LastFrameId(frame_id) => id |= ((frame_id & 0x0f00) as u32 >> 8) << 16,
FrameId::CurrentFrameId(frame_id) => id |= ((frame_id & 0x0f00) as u32 >> 8) << 16,
}
id |= (self.device_address & 0xffff) as u32;
BxFrame::new_data(
ExtendedId::new(id).unwrap(),
Data::new(&self.data[0..self.data_len as usize]).unwrap(),
)
}
pub fn from_usart_frame(encoded: Vec<u8>) -> Result<Self, FrameError> {
let mut frame = vec![0; encoded.len()];
match decode(&encoded[..], &mut frame[..]) {
Ok(n) => frame.truncate(n),
Err(_) => return Err(FrameError::CobsError),
}
if frame.len() < 5 || frame.len() != frame[4] as usize + 5 {
return Err(FrameError::WrongSize);
}
let not_error_flag = ((frame[0] >> 7) & 0x01) != 0;
let start_frame_flag = ((frame[0] >> 6) & 0x01) != 0;
let multi_frame_flag = ((frame[0] >> 5) & 0x01) != 0;
let frame_id = if start_frame_flag {
FrameId::LastFrameId((((frame[0] & 0x0f) as u16) << 8) | frame[1] as u16)
} else {
FrameId::CurrentFrameId((((frame[0] & 0x0f) as u16) << 8) | frame[1] as u16)
};
let device_address = ((frame[2] as u16) << 8) | frame[3] as u16;
let data_len = frame[4];
let mut data = [0u8; 8];
for i in 0..data_len as usize {
data[i] = frame[i + 5];
}
Ok(Frame {
not_error_flag,
start_frame_flag,
multi_frame_flag,
frame_id,
device_address,
data_len,
data,
})
}
pub fn to_usart_frame(&self) -> Vec<u8> {
let mut frame = vec![0x00u8; self.data_len as usize + 5];
frame[0] |= (self.not_error_flag as u8) << 7;
frame[0] |= (self.start_frame_flag as u8) << 6;
frame[0] |= (self.multi_frame_flag as u8) << 5;
match self.frame_id {
FrameId::LastFrameId(frame_id) => frame[0] |= ((frame_id & 0x0f00) >> 8) as u8,
FrameId::CurrentFrameId(frame_id) => frame[0] |= ((frame_id & 0x0f00) >> 8) as u8,
}
match self.frame_id {
FrameId::LastFrameId(frame_id) => frame[1] |= (frame_id & 0x00ff) as u8,
FrameId::CurrentFrameId(frame_id) => frame[1] |= (frame_id & 0x00ff) as u8,
}
frame[2] = ((self.device_address & 0xff00) >> 8) as u8;
frame[3] = (self.device_address & 0x00ff) as u8;
frame[4] = self.data_len;
for i in 0..self.data_len as usize {
frame[i + 5] = self.data[i];
}
let mut encoded = vec![0; max_encoding_length(frame.len())];
let encoded_len = encode(&frame[..], &mut encoded[..]);
encoded.truncate(encoded_len);
return encoded;
}
}
#[cfg(test)]
mod tests {
use super::*;
const FRAME_ID: u32 = 0x1405_5555;
const FRAME_DATA: [u8; 8] = [0x55; 8];
const FRAME: Frame = Frame {
not_error_flag: true,
start_frame_flag: false,
multi_frame_flag: true,
frame_id: FrameId::CurrentFrameId(0x0555),
device_address: 0x5555,
data_len: 8,
data: FRAME_DATA,
};
#[test]
fn from_bxcan_frame_test() {
let bxcan_frame = BxFrame::new_data(ExtendedId::new(FRAME_ID).unwrap(), FRAME_DATA);
let ross_frame = Frame::from_bxcan_frame(bxcan_frame).unwrap();
assert_eq!(ross_frame, FRAME);
}
#[test]
fn to_bxcan_frame_test() {
let bxcan_frame = FRAME.to_bxcan_frame();
let bxcan_frame_expected =
BxFrame::new_data(ExtendedId::new(FRAME_ID).unwrap(), FRAME_DATA);
assert_eq!(bxcan_frame, bxcan_frame_expected);
}
#[test]
fn from_usart_frame_test() {
let usart_frame = vec![
0x0e,
0xa5,
0x55,
0x55,
0x55,
0x08,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
];
let ross_frame = Frame::from_usart_frame(usart_frame).unwrap();
assert_eq!(ross_frame, FRAME);
}
#[test]
fn to_usart_frame_test() {
let usart_frame = FRAME.to_usart_frame();
let usart_frame_expected = vec![
0x0e,
0xa5,
0x55,
0x55,
0x55,
0x08,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
0x55,
];
assert_eq!(usart_frame, usart_frame_expected);
}
}