Skip to content

Commit c96ad8f

Browse files
committed
First take on filters
1 parent b35d830 commit c96ad8f

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

src/can.rs

+67-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,55 @@ pub trait Id {
2020
fn extended_id(&self) -> Option<Self::ExtendedId>;
2121
}
2222

23+
/// A type that will either accept or filter a `Frame`.
24+
/// The filtering is done solely on the `ID` of the `Frame`.
25+
#[cfg(feature = "unproven")]
26+
pub trait Filter {
27+
/// The Id type this filter works on
28+
type Id: Id;
29+
30+
/// Constructs a filter that only accepts `Frame`s with the provided identifier.
31+
fn from_id(id: Self::Id) -> Self;
32+
33+
/// Constructs a filter that will accept any `Frame`.
34+
fn accept_all() -> Self;
35+
36+
/// Constructs a filter that will accept any `Frame` with extended `Id`.
37+
fn accept_extended_id() -> Self;
38+
39+
/// Constructs a filter that will accept any `Frame` with base`Id`.
40+
fn accept_base_id() -> Self;
41+
42+
/// Create a `Filter` from a filter/mask combination.
43+
///
44+
/// *Note: When filtering base id any rule put on `bit_pos >= 11` will (for implementers: must) be ignored*
45+
///
46+
/// ### Panic
47+
/// (for implementers: must) panic if mask have bits equal to `1` for bit_position `>= 29`.
48+
fn from_mask(mask: u32, filter: u32, accept_base_id: bool, accept_extended_id: bool) -> Self;
49+
50+
/// Apply a filter rule on a specific bit.
51+
///
52+
/// *Note: When filtering base id any rule put on `bit_pos >= 11` will (for implementers: must) be ignored*
53+
///
54+
/// ### Panic
55+
/// (for implementers: must) panic if `bit_pos >= 29`.
56+
fn set_filter_bit(&mut self, bit_pos: u8, bit_state: bool);
57+
58+
/// Apply a filter rule on a specific bit.
59+
///
60+
/// *Note: When filtering base id any rule put on `bit_pos >= 11` will (for implementers: must) be ignored*
61+
///
62+
/// ### Panic
63+
/// (for implementers: must) panic if `bit_pos >= 29`.
64+
fn clear_filter_bit(&mut self, bit_pos: u8);
65+
66+
/// Returns `true` if the `Frame` would have been accepted by this filter.
67+
/// Returns `false` if the `Frame` would have been filtered by this filter.
68+
fn accept<T: Frame<Id=Self::Id>>(&self, frame: T) -> bool;
69+
70+
}
71+
2372

2473
/// A Can Frame
2574
#[cfg(feature = "unproven")]
@@ -91,8 +140,14 @@ pub trait FdFrame {
91140
/// A CAN interface
92141
#[cfg(feature = "unproven")]
93142
pub trait Interface {
143+
/// The Id type that works with this `Interface`
144+
type Id: Id;
145+
94146
/// The Can Frame this Interface operates on
95-
type Frame: Frame;
147+
type Frame: Frame<Id = Self::Id>;
148+
149+
/// The Filter type used in this `Interface`
150+
type Filter: Filter<Id = Self::Id>;
96151

97152
/// The Interface Error type
98153
type Error;
@@ -112,6 +167,17 @@ pub trait Interface {
112167
/// `transmit_fd(fd_frame)` would return a `Frame` or `WouldBlock`.
113168
fn transmit_buffer_full(&self) -> bool;
114169

170+
/// Set the can controller in a mode where it only accept frames matching the given filter.
171+
///
172+
/// If there exists several receive buffers, this filter will be applied for all of them.
173+
///
174+
/// *Note: Even after this method has been called, there may still be `Frame`s in the receive buffer with
175+
/// identifiers that would not been received with this `Filter`.*
176+
fn set_filter(&mut self, filter: Self::Filter);
177+
178+
/// Set the can controller in a mode where it will accept all frames.
179+
fn clear_filter(&mut self);
180+
115181
}
116182

117183
/// A CAN interface also supporting Can-FD

0 commit comments

Comments
 (0)