use crate::protocol::compat::embedded_can::InvalidFrameLength;
use embedded_can::{Frame, blocking, nb};
use crate::{AttemptError, BeginSendError, Driver, IngestResult, OperationId};
#[derive(Debug, PartialEq, Eq)]
pub enum NbTransmit<F> {
Submitted {
displaced: Option<F>,
},
WouldBlock,
Uncertain {
displaced: Option<F>,
error: AttemptError,
},
}
#[derive(Debug)]
pub enum NbTransmitError<E> {
Begin(BeginSendError),
FrameRejected,
Attempt(AttemptError),
Driver(E),
}
#[derive(Debug)]
pub enum NbReceive<F> {
Empty,
Frame {
frame: F,
classification: IngestResult,
},
}
#[derive(Debug)]
pub enum NbReceiveError<F, E> {
Driver(E),
InvalidFrame {
frame: F,
error: InvalidFrameLength,
},
}
pub type NbReceiveResult<C> = Result<
NbReceive<<C as nb::Can>::Frame>,
NbReceiveError<<C as nb::Can>::Frame, <C as nb::Can>::Error>,
>;
#[derive(Debug)]
pub enum BlockingTransmitError<E> {
Begin(BeginSendError),
FrameRejected,
Attempt(AttemptError),
Driver(E),
}
#[derive(Debug)]
pub enum BlockingReceiveError<F, E> {
Driver(E),
InvalidFrame {
frame: F,
error: InvalidFrameLength,
},
}
pub type BlockingReceiveResult<C> = Result<
NbReceive<<C as blocking::Can>::Frame>,
BlockingReceiveError<<C as blocking::Can>::Frame, <C as blocking::Can>::Error>,
>;
pub fn transmit_nb<C>(
driver: &mut Driver,
id: OperationId,
can: &mut C,
mut now_ms: impl FnMut() -> u64,
) -> Result<NbTransmit<C::Frame>, NbTransmitError<C::Error>>
where
C: nb::Can,
C::Frame: Frame,
{
let attempt = driver
.begin_send(id, now_ms())
.map_err(NbTransmitError::Begin)?;
let Some(frame) = attempt.frame().to_embedded_can::<C::Frame>() else {
attempt
.not_sent(now_ms())
.map_err(NbTransmitError::Attempt)?;
return Err(NbTransmitError::FrameRejected);
};
match can.transmit(&frame) {
Ok(displaced) => match attempt.submitted(now_ms()) {
Ok(()) => Ok(NbTransmit::Submitted { displaced }),
Err(error) => Ok(NbTransmit::Uncertain { displaced, error }),
},
Err(::nb::Error::WouldBlock) => attempt
.would_block(now_ms())
.map(|()| NbTransmit::WouldBlock)
.map_err(NbTransmitError::Attempt),
Err(::nb::Error::Other(error)) => Err(NbTransmitError::Driver(error)),
}
}
pub fn receive_nb<C>(
driver: &mut Driver,
can: &mut C,
timestamp_ms: impl FnOnce() -> u64,
) -> NbReceiveResult<C>
where
C: nb::Can,
C::Frame: Frame,
{
match can.receive() {
Ok(frame) => ingest_classic_frame(driver, frame, timestamp_ms())
.map_err(|(frame, error)| NbReceiveError::InvalidFrame { frame, error }),
Err(::nb::Error::WouldBlock) => Ok(NbReceive::Empty),
Err(::nb::Error::Other(error)) => Err(NbReceiveError::Driver(error)),
}
}
pub fn ingest_classic_frame<F>(
driver: &mut Driver,
frame: F,
received_at_ms: u64,
) -> Result<NbReceive<F>, (F, InvalidFrameLength)>
where
F: Frame,
{
let view = match crate::protocol::FrameRef::from_classic_embedded_can(&frame) {
Ok(view) => view,
Err(error) => return Err((frame, error)),
};
let classification = driver.ingest(view, received_at_ms);
Ok(NbReceive::Frame {
frame,
classification,
})
}
pub fn transmit_blocking<C>(
driver: &mut Driver,
id: OperationId,
can: &mut C,
mut now_ms: impl FnMut() -> u64,
) -> Result<(), BlockingTransmitError<C::Error>>
where
C: blocking::Can,
C::Frame: Frame,
{
let attempt = driver
.begin_send(id, now_ms())
.map_err(BlockingTransmitError::Begin)?;
let Some(frame) = attempt.frame().to_embedded_can::<C::Frame>() else {
attempt
.not_sent(now_ms())
.map_err(BlockingTransmitError::Attempt)?;
return Err(BlockingTransmitError::FrameRejected);
};
can.transmit(&frame)
.map_err(BlockingTransmitError::Driver)?;
attempt
.submitted(now_ms())
.map_err(BlockingTransmitError::Attempt)
}
pub fn receive_blocking<C>(
driver: &mut Driver,
can: &mut C,
timestamp_ms: impl FnOnce() -> u64,
) -> BlockingReceiveResult<C>
where
C: blocking::Can,
C::Frame: Frame,
{
let frame = can.receive().map_err(BlockingReceiveError::Driver)?;
ingest_classic_frame(driver, frame, timestamp_ms())
.map_err(|(frame, error)| BlockingReceiveError::InvalidFrame { frame, error })
}