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