serial_can/
frame.rs

1use embedded_can::Id;
2
3/// Serial CAN frame.
4#[derive(Debug, PartialEq, Eq, Copy, Clone)]
5pub struct Frame {
6    id: Id,
7    remote: bool,
8    dlc: u8,
9    data: [u8; 8],
10}
11
12impl embedded_can::Frame for Frame {
13    fn new(id: impl Into<Id>, data: &[u8]) -> Option<Self> {
14        if data.len() > 8 {
15            return None;
16        }
17
18        let mut data_all = [0; 8];
19        data_all[0..data.len()].copy_from_slice(&data);
20
21        Some(Self {
22            id: id.into(),
23            remote: false,
24            dlc: data.len() as u8,
25            data: data_all,
26        })
27    }
28
29    fn new_remote(id: impl Into<Id>, dlc: usize) -> Option<Self> {
30        if dlc > 8 {
31            return None;
32        }
33
34        Some(Self {
35            id: id.into(),
36            remote: true,
37            dlc: dlc as u8,
38            data: [0; 8],
39        })
40    }
41
42    fn id(&self) -> Id {
43        self.id
44    }
45
46    fn dlc(&self) -> usize {
47        self.dlc as usize
48    }
49
50    fn data(&self) -> &[u8] {
51        &self.data[0..self.dlc()]
52    }
53
54    fn is_extended(&self) -> bool {
55        match self.id {
56            Id::Extended(_) => true,
57            Id::Standard(_) => false,
58        }
59    }
60
61    fn is_remote_frame(&self) -> bool {
62        self.remote
63    }
64}