Skip to main content

ace_sim/
io.rs

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