can_hal/bus.rs
1use crate::error::CanError;
2
3/// The error state of a CAN bus controller.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum BusState {
6 /// Normal operation. Error counters are below 128.
7 ErrorActive,
8 /// Error counters have reached the warning threshold (128–255).
9 /// The controller can still communicate but may be experiencing issues.
10 ErrorPassive,
11 /// The controller has gone off-bus due to excessive errors (counter > 255).
12 /// No frames can be sent or received until recovery.
13 BusOff,
14}
15
16/// Transmit and receive error counters from the CAN controller.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct ErrorCounters {
19 pub transmit: u8,
20 pub receive: u8,
21}
22
23/// Query the health and error state of a CAN bus controller.
24///
25/// Not all backends support this - it is an optional trait.
26pub trait BusStatus {
27 type Error: CanError;
28
29 /// Returns the current bus state of the controller.
30 fn bus_state(&self) -> Result<BusState, Self::Error>;
31
32 /// Returns the current transmit and receive error counters.
33 fn error_counters(&self) -> Result<ErrorCounters, Self::Error>;
34}