use core::{
future::Future,
task::{Context, Poll},
};
use embassy_stm32::can::{
CanRx, CanTx,
enums::BusError,
frame::{FdEnvelope, FdFrame, Frame},
};
use crate::protocol::compat::embassy::FromEmbassyError;
use crate::{
AttemptError, BeginSendError, Driver, IngestResult, Instant, SendPermit, TxAttempt,
TxCompletion, TxOutcome,
};
#[must_use]
pub struct EmbassyTx<'s> {
attempt: Option<TxAttempt<'s>>,
}
impl<'s> EmbassyTx<'s> {
pub fn begin(
driver: &mut Driver<'s>,
permit: SendPermit<'s>,
processed_at: Instant,
) -> Result<Self, BeginSendError> {
Ok(Self {
attempt: Some(driver.begin_send(permit, processed_at)?),
})
}
pub fn poll<F>(
&mut self,
tx: &mut CanTx<'_>,
driver: &mut Driver<'s>,
context: &mut Context<'_>,
mut now: F,
) -> Poll<Result<EmbassyTransmit<'s>, EmbassyTxError>>
where
F: FnMut() -> Instant,
{
let attempt = self
.attempt
.take()
.expect("EmbassyTx polled after completion");
let encoded = match driver.authorize_tx(&attempt, now()) {
Ok(encoded) => encoded,
Err(error) => {
self.attempt = Some(attempt);
return Poll::Ready(Err(EmbassyTxError::Attempt(error)));
}
};
let frame = Frame::from(&encoded);
let write = tx.write(&frame);
let mut write = core::pin::pin!(write);
match write.as_mut().poll(context) {
Poll::Pending => {
self.attempt = Some(attempt);
Poll::Pending
}
Poll::Ready(displaced) => {
let occurred_at = now();
let processed_at = now();
let completion =
driver.finish_tx(attempt, TxOutcome::Submitted { occurred_at }, processed_at);
Poll::Ready(Ok(EmbassyTransmit::Submitted {
displaced,
completion,
}))
}
}
}
pub fn cancel_unsubmitted(
mut self,
driver: &mut Driver<'s>,
occurred_at: Instant,
processed_at: Instant,
) -> Result<(), EmbassyTxError> {
let attempt = self
.attempt
.take()
.expect("EmbassyTx cancelled after completion");
driver
.cancel_unsubmitted(attempt, occurred_at, processed_at)
.map_err(EmbassyTxError::Attempt)
}
pub fn abandon(
mut self,
driver: &mut Driver<'s>,
processed_at: Instant,
) -> Result<(), EmbassyTxError> {
let attempt = self
.attempt
.take()
.expect("EmbassyTx abandoned after completion");
driver
.abandon_attempt(attempt, processed_at)
.map_err(EmbassyTxError::Attempt)
}
}
#[derive(Debug)]
pub enum EmbassyTransmit<'s> {
Submitted {
displaced: Option<Frame>,
completion: Result<TxCompletion<'s>, AttemptError>,
},
}
#[derive(Debug)]
pub enum EmbassyTxError {
Attempt(AttemptError),
}
#[derive(Debug)]
pub enum EmbassyReceive {
Frame {
envelope: FdEnvelope,
classification: IngestResult,
},
Invalid {
envelope: FdEnvelope,
error: FromEmbassyError,
},
}
pub async fn receive(rx: &mut CanRx<'_>) -> Result<FdEnvelope, BusError> {
rx.read_fd().await
}
pub fn ingest_fd_envelope(
driver: &mut Driver<'_>,
envelope: FdEnvelope,
received_at: Instant,
) -> EmbassyReceive {
match crate::protocol::FrameRef::try_from(&envelope.frame) {
Ok(view) => EmbassyReceive::Frame {
classification: driver.ingest(view, received_at),
envelope,
},
Err(error) => EmbassyReceive::Invalid { envelope, error },
}
}
pub type EmbassyCanTx<'d> = CanTx<'d>;
pub type EmbassyCanRx<'d> = CanRx<'d>;
pub fn raw_frame(receive: &EmbassyReceive) -> &FdFrame {
match receive {
EmbassyReceive::Frame { envelope, .. } | EmbassyReceive::Invalid { envelope, .. } => {
&envelope.frame
}
}
}