Skip to main content

can_isotp_queue/
common.rs

1use core::time::Duration;
2
3use can_isotp_interface::SendError;
4
5#[derive(Debug)]
6pub enum QueueIsoTpError<E> {
7    PayloadTooLarge { needed: usize, capacity: usize },
8    Transport(E),
9    ChannelClosed,
10}
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum QueueKind {
14    TxReq,
15    TxResp,
16    RxEvt,
17}
18
19#[derive(Clone, Copy, Debug)]
20pub struct ActorConfig {
21    pub recv_poll_timeout: Duration,
22    pub tx_burst_limit: usize,
23    pub fixed_reply_to: Option<u8>,
24    pub allowed_reply_from: Option<u8>,
25}
26
27impl ActorConfig {
28    pub const fn new(
29        recv_poll_timeout: Duration,
30        tx_burst_limit: usize,
31        fixed_reply_to: Option<u8>,
32    ) -> Self {
33        Self {
34            recv_poll_timeout,
35            tx_burst_limit,
36            fixed_reply_to,
37            allowed_reply_from: None,
38        }
39    }
40
41    pub const fn with_fixed_reply_to(mut self, fixed_reply_to: Option<u8>) -> Self {
42        self.fixed_reply_to = fixed_reply_to;
43        self
44    }
45
46    pub const fn with_allowed_reply_from(mut self, allowed_reply_from: Option<u8>) -> Self {
47        self.allowed_reply_from = allowed_reply_from;
48        self
49    }
50
51    pub const fn normalized_tx_burst_limit(&self) -> usize {
52        if self.tx_burst_limit == 0 {
53            1
54        } else {
55            self.tx_burst_limit
56        }
57    }
58}
59
60impl Default for ActorConfig {
61    fn default() -> Self {
62        Self {
63            recv_poll_timeout: Duration::from_millis(1),
64            tx_burst_limit: 8,
65            fixed_reply_to: None,
66            allowed_reply_from: None,
67        }
68    }
69}
70
71pub trait ActorHooks<E> {
72    fn on_actor_started(&self) {}
73
74    fn on_tx_request(&self, _to: u8, _is_functional: bool, _len: usize, _timeout: Duration) {}
75
76    fn on_tx_result(&self, _result: &Result<(), SendError<QueueIsoTpError<E>>>) {}
77
78    fn on_rx_delivered(&self, _from: u8, _len: usize) {}
79
80    fn on_rx_filtered_source(&self, _expected: u8, _got: u8, _len: usize) {}
81
82    fn on_rx_buffer_too_small(&self, _needed: usize, _got: usize) {}
83
84    fn on_rx_backend_error(&self, _err: &E) {}
85
86    fn on_queue_backpressure(&self, _queue: QueueKind, _blocked_for: Duration) {}
87}
88
89#[derive(Clone, Copy, Debug, Default)]
90pub struct NoopHooks;
91
92impl<E> ActorHooks<E> for NoopHooks {}