|
| 1 | +//! Controller Area Network |
| 2 | +
|
| 3 | +/// A CAN2.0 Frame |
| 4 | +pub trait Frame { |
| 5 | + /// Creates a new frame with a standard identifier. |
| 6 | + fn new_standard(id: u32, data: &[u8]) -> Self; |
| 7 | + |
| 8 | + /// Creates a new frame with an extended identifier. |
| 9 | + fn new_extended(id: u32, data: &[u8]) -> Self; |
| 10 | + |
| 11 | + /// Marks this frame as a remote frame (by setting the RTR bit). |
| 12 | + fn with_rtr(&mut self, dlc: usize) -> &mut Self; |
| 13 | + |
| 14 | + /// Returns true if this frame is a extended frame. |
| 15 | + fn is_extended(&self) -> bool; |
| 16 | + |
| 17 | + /// Returns true if this frame is a standard frame. |
| 18 | + fn is_standard(&self) -> bool { |
| 19 | + !self.is_extended() |
| 20 | + } |
| 21 | + |
| 22 | + /// Returns true if this frame is a remote frame. |
| 23 | + fn is_remote_frame(&self) -> bool; |
| 24 | + |
| 25 | + /// Returns true if this frame is a data frame. |
| 26 | + fn is_data_frame(&self) -> bool { |
| 27 | + !self.is_remote_frame() |
| 28 | + } |
| 29 | + |
| 30 | + /// Returns the frame identifier. |
| 31 | + fn id(&self) -> u32; |
| 32 | + |
| 33 | + /// Returns the data length code (DLC) which is in the range 0..8. |
| 34 | + /// |
| 35 | + /// For data frames the DLC value always matches the lenght of the data. |
| 36 | + /// Remote frames no not carry any data, yet the DLC can be greater than 0. |
| 37 | + fn dlc(&self) -> usize; |
| 38 | + |
| 39 | + /// Returns the frame data (0..8 bytes in length). |
| 40 | + fn data(&self) -> &[u8]; |
| 41 | +} |
| 42 | + |
| 43 | +/// A CAN interface that is able to transmit frames. |
| 44 | +pub trait Transmitter { |
| 45 | + /// Associated frame type. |
| 46 | + type Frame: Frame; |
| 47 | + |
| 48 | + /// Associated error type. |
| 49 | + type Error; |
| 50 | + |
| 51 | + /// Puts a frame in the transmit buffer. |
| 52 | + /// |
| 53 | + /// If the buffer is full, this function will try to replace a lower priority frame |
| 54 | + /// and return it. This is to avoid the priority inversion problem. |
| 55 | + fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error>; |
| 56 | +} |
| 57 | + |
| 58 | +/// A CAN interface that is able to receive frames. |
| 59 | +pub trait Receiver { |
| 60 | + /// Associated frame type. |
| 61 | + type Frame: Frame; |
| 62 | + |
| 63 | + /// Associated error type. |
| 64 | + type Error; |
| 65 | + |
| 66 | + /// Returns a received frame if available. |
| 67 | + fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error>; |
| 68 | +} |
0 commit comments