Skip to content

Commit 285efaa

Browse files
committed
can: Use enum for the identifier
1 parent 449b801 commit 285efaa

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/can.rs

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
//! Controller Area Network
22
3+
/// CAN Identifier
4+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
5+
pub enum Id {
6+
/// Standard 11bit Identifier (0..=0x7FF)
7+
Standard(u32),
8+
9+
/// Extended 29bit Identifier (0..=0x1FFF_FFFF)
10+
Extended(u32),
11+
}
12+
13+
impl Id {
14+
/// Returs true when the identifier is valid, false otherwise.
15+
pub fn valid(self) -> bool {
16+
match self {
17+
Id::Standard(id) if id <= 0x7FF => true,
18+
Id::Extended(id) if id <= 0x1FFF_FFFF => true,
19+
_ => false,
20+
}
21+
}
22+
}
23+
324
/// A CAN2.0 Frame
425
pub trait Frame: Sized {
5-
/// Creates a new frame with a standard identifier (0..=0x7FF).
6-
/// Returns an error when the the identifier is not valid.
7-
fn new_standard(id: u32, data: &[u8]) -> Result<Self, ()>;
8-
9-
/// Creates a new frame with an extended identifier (0..=0x1FFF_FFFF).
10-
/// Returns an error when the the identifier is not valid.
11-
fn new_extended(id: u32, data: &[u8]) -> Result<Self, ()>;
26+
/// Creates a new frame.
27+
/// Returns an error when the the identifier is not valid or the data slice is too long.
28+
fn new(id: Id, data: &[u8]) -> Result<Self, ()>;
1229

13-
/// Marks this frame as a remote frame (by setting the RTR bit).
14-
fn with_rtr(&mut self, dlc: usize) -> &mut Self;
30+
/// Creates a new remote frame (RTR bit set).
31+
/// Returns an error when the the identifier is or the data length code (DLC) not valid.
32+
fn new_remote(id: Id, dlc: usize) -> Result<Self, ()>;
1533

1634
/// Returns true if this frame is a extended frame.
1735
fn is_extended(&self) -> bool;
@@ -30,7 +48,7 @@ pub trait Frame: Sized {
3048
}
3149

3250
/// Returns the frame identifier.
33-
fn id(&self) -> u32;
51+
fn id(&self) -> Id;
3452

3553
/// Returns the data length code (DLC) which is in the range 0..8.
3654
///

0 commit comments

Comments
 (0)