Skip to content

Commit 632d084

Browse files
committed
can: FilteredReceiver and associated traits
1 parent 9de2c00 commit 632d084

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

src/can.rs

+113
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,116 @@ pub trait Receiver {
6666
/// Returns a received frame if available.
6767
fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error>;
6868
}
69+
70+
/// Filter mask type.
71+
pub enum MaskType {
72+
/// Each filter of the group has an individual mask.
73+
Individual,
74+
75+
/// All filters of a group share a common filter mask.
76+
Shared,
77+
}
78+
79+
/// Remote frame filter behaviour description.
80+
pub enum RtrFilterBehavior {
81+
/// The RTR bit is part of the filter and the mask.
82+
///
83+
/// Both `Filter::allow_remote()` and `Filter::remote_only()` are implemented.
84+
Configurable,
85+
86+
/// The RTR bit is part of the filter.
87+
///
88+
/// `Filter::remote_only()` is implemented. `Filter::allow_remote()` has no
89+
/// effect.
90+
ConfigurableEitherDataOrRemote,
91+
92+
/// Both data and remote frames with a mathing identifier are accepted.
93+
///
94+
/// `Filter::allow_remote()` nor `Filter::remote_only()` have an effect on the filter configuration.
95+
RemoteAlwaysAllowed,
96+
97+
/// Only data remote frames with a mathing identifier are accepted.
98+
///
99+
/// `Filter::allow_remote()` nor `Filter::remote_only()` have an effect on the filter configuration.
100+
OnlyData,
101+
102+
/// Only data remote frames with a mathing identifier are accepted.
103+
///
104+
/// `Filter::allow_remote()` nor `Filter::remote_only()` have an effect on the filter configuration.
105+
OnlyRemote,
106+
}
107+
108+
/// A filter group with it’s capabilities.
109+
pub trait FilterGroup {
110+
/// Returns the number of consecutive filter with the same capability.
111+
fn num_filters(&self) -> usize;
112+
113+
/// Returs `true` when extended 29bit identifiers are supported (in addition
114+
/// to the standard 11bit identifiers).
115+
fn extended(&self) -> bool;
116+
117+
/// Returns the filter mask type. `None` if no masks is supported.
118+
fn mask(&self) -> Option<MaskType>;
119+
120+
/// Returs the filter behavior in regard to remote frames.
121+
fn rtr(&self) -> RtrFilterBehavior;
122+
}
123+
124+
/// CAN filter interface
125+
pub trait Filter {
126+
/// Creates a filter that accepts all frames.
127+
fn accept_all() -> Self;
128+
129+
/// Creates a filter that accepts frames with the specified standard identifier.
130+
fn new_standard(id: u32) -> Self;
131+
132+
/// Creates a filter that accepts frames with the extended standard identifier.
133+
fn new_extended(id: u32) -> Self;
134+
135+
/// Applies a mask to the filter.
136+
///
137+
/// # Example
138+
///
139+
/// Filter ID: 0b100110111
140+
/// Mask: 0b000001111
141+
///
142+
/// Receive ID: 0b100110011
143+
/// \----> Not accepted (bit 3 did not match)
144+
///
145+
/// Receive ID: 0b000000111 -> accepted
146+
fn with_mask(&mut self, mask: u32) -> &mut Self;
147+
148+
/// Makes the filter acccept both data and remote frames.
149+
///
150+
/// Sets the RTR bit in the filter mask.
151+
/// Only available for filters with `RtrFilterBehavior::Configurable`.
152+
fn allow_remote(&mut self) -> &mut Self;
153+
154+
/// Makes the filter acccept both data and remote frames.
155+
///
156+
/// Sets the RTR bit in the filter and clears it in the mask (if available).
157+
/// Only available for filters with `RtrFilterBehavior::Configurable` or
158+
/// `RtrFilterBehavior::ConfigurableEitherDataOrRemote`.
159+
fn remote_only(&mut self) -> &mut Self;
160+
}
161+
162+
/// A CAN interface that is able to receive frames and specify filters.
163+
pub trait FilteredReceiver: Receiver {
164+
/// Associated filter type.
165+
type Filter: Filter;
166+
167+
/// Associated filter group type.
168+
type FilterGroup: FilterGroup;
169+
170+
/// Associated iterator type for the filter groups.
171+
type FilterGroups: IntoIterator<Item = Self::FilterGroup>;
172+
173+
/// Returns the filter's groups.
174+
fn filter_groups(&self) -> Self::FilterGroups;
175+
176+
/// Adds and enables a filter.
177+
fn add_filter(&mut self, filter: &Self::Filter) -> Result<(), Self::Error>;
178+
179+
/// Clears all filters. No messages can be received anymore.
180+
fn clear_filters(&mut self);
181+
}

0 commit comments

Comments
 (0)