1
1
//! Controller Area Network
2
2
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
+
3
24
/// A CAN2.0 Frame
4
25
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 , ( ) > ;
12
29
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 , ( ) > ;
15
33
16
34
/// Returns true if this frame is a extended frame.
17
35
fn is_extended ( & self ) -> bool ;
@@ -30,7 +48,7 @@ pub trait Frame: Sized {
30
48
}
31
49
32
50
/// Returns the frame identifier.
33
- fn id ( & self ) -> u32 ;
51
+ fn id ( & self ) -> Id ;
34
52
35
53
/// Returns the data length code (DLC) which is in the range 0..8.
36
54
///
@@ -120,11 +138,8 @@ pub trait Filter {
120
138
/// Creates a filter that accepts all frames.
121
139
fn accept_all ( ) -> Self ;
122
140
123
- /// Creates a filter that accepts frames with the specified standard identifier.
124
- fn new_standard ( id : u32 ) -> Self ;
125
-
126
- /// Creates a filter that accepts frames with the extended standard identifier.
127
- fn new_extended ( id : u32 ) -> Self ;
141
+ /// Creates a filter that accepts frames with the specified identifier.
142
+ fn new ( id : Id ) -> Self ;
128
143
129
144
/// Applies a mask to the filter.
130
145
///
0 commit comments