1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! Blocking CAN API

/// A blocking CAN interface that is able to transmit and receive frames.
pub trait Can {
    /// Associated frame type.
    type Frame: crate::Frame;

    /// Associated error type.
    type Error;

    /// Puts a frame in the transmit buffer. Blocks until space is available in
    /// the transmit buffer.
    fn try_write(&mut self, frame: &Self::Frame) -> Result<(), Self::Error>;

    /// Blocks until a frame was received or an error occured.
    fn try_read(&mut self) -> Result<Self::Frame, Self::Error>;
}

/// Default implementation of `blocking::can::Can` for implementers of `can::Can`
pub trait Default: crate::Can {}

impl<S> crate::blocking::Can for S
where
    S: Default,
{
    type Frame = S::Frame;
    type Error = S::Error;

    fn try_write(&mut self, frame: &Self::Frame) -> Result<(), Self::Error> {
        let mut replaced_frame;
        let mut frame_to_transmit = frame;
        while let Some(f) = nb::block!(self.try_transmit(&frame_to_transmit))? {
            replaced_frame = f;
            frame_to_transmit = &replaced_frame;
        }
        Ok(())
    }

    fn try_read(&mut self) -> Result<Self::Frame, Self::Error> {
        nb::block!(self.try_receive())
    }
}