host_can/
frame.rs

1use crate::id::Id;
2pub use embedded_can::Frame;
3
4type CanPdu = [u8; 8];
5
6/// A standard CAN 2.0 frame
7/// (NOTE: this will change to an enum w/standard, fd, error, etc.)
8#[derive(Copy, Clone, Debug)]
9pub struct CanFrame {
10    id: Id,
11    dlc: u8,
12    data: CanPdu,
13}
14
15impl Frame for CanFrame {
16    fn new(id: impl Into<Id>, data: &[u8]) -> Option<Self> {
17        let len = data.len();
18        if len > std::mem::size_of::<CanPdu>() {
19            return None;
20        }
21        let mut bytes = [0u8; 8];
22        bytes[..len].copy_from_slice(data);
23        Some(CanFrame {
24            id: id.into(),
25            dlc: len as u8,
26            data: bytes,
27        })
28    }
29
30    fn new_remote(_: impl Into<Id>, _: usize) -> Option<Self> {
31        None
32    }
33
34    fn is_extended(&self) -> bool {
35        match self.id {
36            embedded_can::Id::Extended(_) => true,
37            embedded_can::Id::Standard(_) => false,
38        }
39    }
40
41    fn is_remote_frame(&self) -> bool {
42        false
43    }
44
45    fn id(&self) -> embedded_can::Id {
46        self.id
47    }
48
49    fn dlc(&self) -> usize {
50        self.dlc as usize
51    }
52
53    fn data(&self) -> &[u8] {
54        &self.data[..self.dlc as usize]
55    }
56}
57
58// TODO: CanFdFrame