Skip to main content

acex_sim/
io.rs

1// region: Imports
2
3use crate::clock::Instant;
4use acex_core::Vec;
5
6// endregion: Imports
7
8// region: Address
9
10/// A logical node address in the simulation network.
11///
12/// Maps to a CAN ID, DoIP logical address, or any other addressing scheme depending on the
13/// protocol layer in use.
14
15#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16#[cfg_attr(feature = "defmt", derive(defmt::Format))]
17pub struct NodeAddress(pub u32);
18
19// endregion: Address
20
21// region: RawMessage
22
23/// A raw byte message between two nodes.
24///
25/// Used by low-level runtime implementers working directly with frames.
26#[derive(Debug, Clone, PartialEq, Eq)]
27#[cfg_attr(all(feature = "defmt", not(feature = "alloc")), derive(defmt::Format))]
28pub struct RawMessage<const MAX_DATA: usize> {
29    pub src: NodeAddress,
30    pub dst: NodeAddress,
31    pub data: Vec<u8, MAX_DATA>,
32    pub timestamp: Instant,
33}
34
35// endregion: RawMessage
36
37// region: FrameTransport Trait
38
39/// Low-level transport trait for frame-oriented communication.
40///
41/// Implementers of CAN/DoIP runtimes use this trait. The simulation replaces this with an
42/// in-memory channel that can inject faults.
43pub trait FrameTransport<const N: usize> {
44    type Error: core::fmt::Debug;
45
46    /// Sends a raw frame to the given destination.
47    fn send(&mut self, dst: &NodeAddress, data: &[u8]) -> Result<(), Self::Error>;
48
49    /// Receives the next available raw frame, if any. Returns `None` if no frame is available
50    fn recv(&mut self) -> Option<RawMessage<N>>;
51}
52
53// endregion: FrameTransport Trait
54
55// region: MessageTransport Trait
56
57/// High-level transport trait for named message communication.
58///
59/// Application developers building on top of UDS/DoIP use this trait. Messages are typed - the
60/// transport handles serialisation internally.
61pub trait MessageTransport {
62    type Message: core::fmt::Debug;
63    type Error: core::fmt::Debug;
64
65    /// Sends a typed message to the given destination
66    fn send(&mut self, dst: &NodeAddress, message: Self::Message) -> Result<(), Self::Error>;
67
68    /// Receives the next available typed message, if any.
69    fn recv(&mut self) -> Option<(NodeAddress, Self::Message)>;
70}
71
72// endregion: MessageTransport Trait