1use bit_field::BitField;
3
4use crate::can::enums::FrameCreateError;
5
6#[cfg(feature = "time")]
8pub type Timestamp = embassy_time::Instant;
9
10#[cfg(not(feature = "time"))]
12pub type Timestamp = u16;
13
14#[derive(Debug, Copy, Clone)]
16pub struct Header {
17 id: embedded_can::Id,
18 len: u8,
19 flags: u8,
20}
21
22#[cfg(feature = "defmt")]
23impl defmt::Format for Header {
24 fn format(&self, fmt: defmt::Formatter<'_>) {
25 match self.id() {
26 embedded_can::Id::Standard(id) => {
27 defmt::write!(fmt, "Can Standard ID={:x} len={}", id.as_raw(), self.len,)
28 }
29 embedded_can::Id::Extended(id) => {
30 defmt::write!(fmt, "Can Extended ID={:x} len={}", id.as_raw(), self.len,)
31 }
32 }
33 }
34}
35
36impl Header {
37 const FLAG_RTR: usize = 0; const FLAG_FDCAN: usize = 1; const FLAG_BRS: usize = 2; pub fn new(id: embedded_can::Id, len: u8, rtr: bool) -> Header {
43 let mut flags = 0u8;
44 flags.set_bit(Self::FLAG_RTR, rtr);
45 Header { id, len, flags }
46 }
47
48 pub fn new_fd(id: embedded_can::Id, len: u8, rtr: bool, brs: bool) -> Header {
50 let mut flags = 0u8;
51 flags.set_bit(Self::FLAG_RTR, rtr);
52 flags.set_bit(Self::FLAG_FDCAN, true);
53 flags.set_bit(Self::FLAG_BRS, brs);
54 Header { id, len, flags }
55 }
56
57 pub fn id(&self) -> &embedded_can::Id {
59 &self.id
60 }
61
62 pub fn len(&self) -> u8 {
64 self.len
65 }
66
67 pub fn rtr(&self) -> bool {
69 self.flags.get_bit(Self::FLAG_RTR)
70 }
71
72 pub fn fdcan(&self) -> bool {
74 self.flags.get_bit(Self::FLAG_FDCAN)
75 }
76
77 pub fn bit_rate_switching(&self) -> bool {
79 self.flags.get_bit(Self::FLAG_BRS)
80 }
81
82 pub(crate) fn priority(&self) -> u32 {
84 match self.id() {
85 embedded_can::Id::Standard(id) => (id.as_raw() as u32) << 18,
86 embedded_can::Id::Extended(id) => id.as_raw(),
87 }
88 }
89}
90
91pub trait CanHeader: Sized {
94 fn from_header(header: Header, data: &[u8]) -> Result<Self, FrameCreateError>;
96
97 fn header(&self) -> &Header;
99}
100
101#[derive(Debug, Copy, Clone)]
105#[cfg_attr(feature = "defmt", derive(defmt::Format))]
106pub struct ClassicData {
107 pub(crate) bytes: [u8; Self::MAX_DATA_LEN],
108}
109
110impl ClassicData {
111 pub(crate) const MAX_DATA_LEN: usize = 8;
112 pub fn new(data: &[u8]) -> Result<Self, FrameCreateError> {
117 if data.len() > 8 {
118 return Err(FrameCreateError::InvalidDataLength);
119 }
120
121 let mut bytes = [0; 8];
122 bytes[..data.len()].copy_from_slice(data);
123
124 Ok(Self { bytes })
125 }
126
127 pub fn raw(&self) -> &[u8] {
129 &self.bytes
130 }
131
132 pub const fn is_valid_len(len: usize) -> bool {
134 match len {
135 0..=8 => true,
136 _ => false,
137 }
138 }
139
140 #[inline]
142 pub const fn empty() -> Self {
143 Self { bytes: [0; 8] }
144 }
145}
146
147#[derive(Debug, Copy, Clone)]
150#[cfg_attr(feature = "defmt", derive(defmt::Format))]
151pub struct Frame {
152 can_header: Header,
153 data: ClassicData,
154}
155
156impl Frame {
157 pub fn new(can_header: Header, raw_data: &[u8]) -> Result<Self, FrameCreateError> {
159 let data = ClassicData::new(raw_data)?;
160 Ok(Frame { can_header, data: data })
161 }
162
163 pub fn new_data(id: impl Into<embedded_can::Id>, data: &[u8]) -> Result<Self, FrameCreateError> {
165 let eid: embedded_can::Id = id.into();
166 let header = Header::new(eid, data.len() as u8, false);
167 Self::new(header, data)
168 }
169
170 pub fn new_extended(raw_id: u32, raw_data: &[u8]) -> Result<Self, FrameCreateError> {
172 if let Some(id) = embedded_can::ExtendedId::new(raw_id) {
173 Self::new(Header::new(id.into(), raw_data.len() as u8, false), raw_data)
174 } else {
175 Err(FrameCreateError::InvalidCanId)
176 }
177 }
178
179 pub fn new_standard(raw_id: u16, raw_data: &[u8]) -> Result<Self, FrameCreateError> {
181 if let Some(id) = embedded_can::StandardId::new(raw_id) {
182 Self::new(Header::new(id.into(), raw_data.len() as u8, false), raw_data)
183 } else {
184 Err(FrameCreateError::InvalidCanId)
185 }
186 }
187
188 pub fn new_remote(id: impl Into<embedded_can::Id>, len: usize) -> Result<Self, FrameCreateError> {
190 if len <= 8usize {
191 Self::new(Header::new(id.into(), len as u8, true), &[0; 8])
192 } else {
193 Err(FrameCreateError::InvalidDataLength)
194 }
195 }
196
197 pub fn header(&self) -> &Header {
199 &self.can_header
200 }
201
202 pub fn id(&self) -> &embedded_can::Id {
204 &self.can_header.id
205 }
206
207 pub fn data(&self) -> &[u8] {
209 &self.data.raw()
210 }
211
212 pub fn priority(&self) -> u32 {
214 self.header().priority()
215 }
216}
217
218impl embedded_can::Frame for Frame {
219 fn new(id: impl Into<embedded_can::Id>, raw_data: &[u8]) -> Option<Self> {
220 let frameopt = Frame::new(Header::new(id.into(), raw_data.len() as u8, false), raw_data);
221 match frameopt {
222 Ok(frame) => Some(frame),
223 Err(_) => None,
224 }
225 }
226 fn new_remote(id: impl Into<embedded_can::Id>, len: usize) -> Option<Self> {
227 if len <= 8 {
228 let frameopt = Frame::new(Header::new(id.into(), len as u8, true), &[0; 8]);
229 match frameopt {
230 Ok(frame) => Some(frame),
231 Err(_) => None,
232 }
233 } else {
234 None
235 }
236 }
237 fn is_extended(&self) -> bool {
238 match self.can_header.id {
239 embedded_can::Id::Extended(_) => true,
240 embedded_can::Id::Standard(_) => false,
241 }
242 }
243 fn is_remote_frame(&self) -> bool {
244 self.can_header.rtr()
245 }
246 fn id(&self) -> embedded_can::Id {
247 self.can_header.id
248 }
249 fn dlc(&self) -> usize {
250 self.can_header.len as usize
251 }
252 fn data(&self) -> &[u8] {
253 &self.data.raw()
254 }
255}
256
257impl CanHeader for Frame {
258 fn from_header(header: Header, data: &[u8]) -> Result<Self, FrameCreateError> {
259 Self::new(header, data)
260 }
261
262 fn header(&self) -> &Header {
263 self.header()
264 }
265}
266
267#[derive(Debug, Clone)]
272#[cfg_attr(feature = "defmt", derive(defmt::Format))]
273pub struct Envelope {
274 pub ts: Timestamp,
276 pub frame: Frame,
278}
279
280impl Envelope {
281 pub fn parts(self) -> (Frame, Timestamp) {
283 (self.frame, self.ts)
284 }
285}
286
287#[derive(Debug, Copy, Clone)]
291#[cfg_attr(feature = "defmt", derive(defmt::Format))]
292pub struct FdData {
293 pub(crate) bytes: [u8; 64],
294}
295
296impl FdData {
297 pub fn new(data: &[u8]) -> Result<Self, FrameCreateError> {
302 if !FdData::is_valid_len(data.len()) {
303 return Err(FrameCreateError::InvalidDataLength);
304 }
305
306 let mut bytes = [0; 64];
307 bytes[..data.len()].copy_from_slice(data);
308
309 Ok(Self { bytes })
310 }
311
312 pub fn raw(&self) -> &[u8] {
314 &self.bytes
315 }
316
317 pub const fn is_valid_len(len: usize) -> bool {
319 match len {
320 0..=8 => true,
321 12 => true,
322 16 => true,
323 20 => true,
324 24 => true,
325 32 => true,
326 48 => true,
327 64 => true,
328 _ => false,
329 }
330 }
331
332 #[inline]
334 pub const fn empty() -> Self {
335 Self { bytes: [0; 64] }
336 }
337}
338
339#[derive(Debug, Copy, Clone)]
341#[cfg_attr(feature = "defmt", derive(defmt::Format))]
342pub struct FdFrame {
343 can_header: Header,
344 data: FdData,
345}
346
347impl FdFrame {
348 pub fn new(can_header: Header, raw_data: &[u8]) -> Result<Self, FrameCreateError> {
350 let data = FdData::new(raw_data)?;
351 Ok(FdFrame { can_header, data })
352 }
353
354 pub fn new_extended(raw_id: u32, raw_data: &[u8]) -> Result<Self, FrameCreateError> {
356 if let Some(id) = embedded_can::ExtendedId::new(raw_id) {
357 Self::new(Header::new(id.into(), raw_data.len() as u8, false), raw_data)
358 } else {
359 Err(FrameCreateError::InvalidCanId)
360 }
361 }
362
363 pub fn new_standard(raw_id: u16, raw_data: &[u8]) -> Result<Self, FrameCreateError> {
365 if let Some(id) = embedded_can::StandardId::new(raw_id) {
366 Self::new(Header::new(id.into(), raw_data.len() as u8, false), raw_data)
367 } else {
368 Err(FrameCreateError::InvalidCanId)
369 }
370 }
371
372 pub fn new_remote(id: impl Into<embedded_can::Id>, len: usize) -> Result<Self, FrameCreateError> {
374 if len <= 8 {
375 Self::new(Header::new(id.into(), len as u8, true), &[0; 8])
376 } else {
377 Err(FrameCreateError::InvalidDataLength)
378 }
379 }
380
381 pub fn header(&self) -> &Header {
383 &self.can_header
384 }
385
386 pub fn id(&self) -> &embedded_can::Id {
388 &self.can_header.id
389 }
390
391 pub fn data(&self) -> &[u8] {
393 &self.data.raw()
394 }
395}
396
397impl embedded_can::Frame for FdFrame {
398 fn new(id: impl Into<embedded_can::Id>, raw_data: &[u8]) -> Option<Self> {
399 match FdFrame::new(Header::new_fd(id.into(), raw_data.len() as u8, false, true), raw_data) {
400 Ok(frame) => Some(frame),
401 Err(_) => None,
402 }
403 }
404 fn new_remote(id: impl Into<embedded_can::Id>, len: usize) -> Option<Self> {
405 if len <= 8 {
406 match FdFrame::new(Header::new_fd(id.into(), len as u8, true, true), &[0; 64]) {
407 Ok(frame) => Some(frame),
408 Err(_) => None,
409 }
410 } else {
411 None
412 }
413 }
414 fn is_extended(&self) -> bool {
415 match self.can_header.id {
416 embedded_can::Id::Extended(_) => true,
417 embedded_can::Id::Standard(_) => false,
418 }
419 }
420 fn is_remote_frame(&self) -> bool {
421 self.can_header.rtr()
422 }
423 fn id(&self) -> embedded_can::Id {
424 self.can_header.id
425 }
426 fn dlc(&self) -> usize {
428 self.can_header.len as usize
429 }
430 fn data(&self) -> &[u8] {
431 &self.data.raw()
432 }
433}
434
435impl CanHeader for FdFrame {
436 fn from_header(header: Header, data: &[u8]) -> Result<Self, FrameCreateError> {
437 Self::new(header, data)
438 }
439
440 fn header(&self) -> &Header {
441 self.header()
442 }
443}
444
445#[derive(Debug, Clone)]
449#[cfg_attr(feature = "defmt", derive(defmt::Format))]
450pub struct FdEnvelope {
451 pub ts: Timestamp,
453
454 pub frame: FdFrame,
456}
457
458impl FdEnvelope {
459 pub fn parts(self) -> (FdFrame, Timestamp) {
461 (self.frame, self.ts)
462 }
463}