mctp_rs/medium/mod.rs
1use crate::{
2 buffer_encoding::{BufferEncoding, EncodingDecoder, EncodingEncoder},
3 error::MctpPacketResult,
4};
5
6pub mod smbus_espi;
7mod util;
8
9#[cfg(feature = "serial")]
10pub mod serial;
11
12pub trait MctpMedium: Sized {
13 /// the medium specific header and trailer for the packet
14 type Frame: MctpMediumFrame<Self>;
15
16 /// the error type for deserialization of the medium specific header
17 type Error: core::fmt::Debug + Copy + Clone + PartialEq + Eq;
18
19 // the type used for the data needed to send a reply to a request
20 type ReplyContext: core::fmt::Debug + Copy + Clone + PartialEq + Eq;
21
22 /// the byte-stuffing transform used by this medium when (de)serializing
23 /// wire bytes. Stateless — see [`crate::buffer_encoding`]. Most media
24 /// use [`PassthroughEncoding`](crate::buffer_encoding::PassthroughEncoding)
25 /// (no transform); media that need byte-stuffing (e.g., DSP0253 serial)
26 /// supply their own impl.
27 type Encoding: BufferEncoding;
28
29 /// the maximum transmission unit for the medium
30 fn max_message_body_size(&self) -> usize;
31
32 /// Deserialize a packet into the medium-specific header (frame) and an
33 /// [`EncodingDecoder`] that wraps the inner stuffed-region bytes.
34 /// Higher layers (e.g., `parse_transport_header`, the payload copy
35 /// loop in `MctpPacketContext`) read decoded bytes through the
36 /// returned decoder and physically cannot bypass the medium's
37 /// encoding by slicing the underlying buffer directly.
38 fn deserialize<'buf>(
39 &self,
40 packet: &'buf [u8],
41 ) -> MctpPacketResult<(Self::Frame, EncodingDecoder<'buf, Self::Encoding>), Self>;
42
43 /// Serialize a packet by allowing the caller's `message_writer`
44 /// closure to write decoded bytes into the medium's stuffed region
45 /// through an [`EncodingEncoder`]. The medium owns its outer framing
46 /// (e.g., SMBus header + PEC, DSP0253 start/end flags + FCS) and
47 /// inspects the encoder's
48 /// [`wire_position`](EncodingEncoder::wire_position) after the
49 /// closure returns to size headers/trailers and compute checksums.
50 fn serialize<'buf, F>(
51 &self,
52 reply_context: Self::ReplyContext,
53 buffer: &'buf mut [u8],
54 message_writer: F,
55 ) -> MctpPacketResult<&'buf [u8], Self>
56 where
57 F: for<'a> FnOnce(&mut EncodingEncoder<'a, Self::Encoding>) -> MctpPacketResult<(), Self>;
58}
59
60pub trait MctpMediumFrame<M: MctpMedium>: Clone + Copy {
61 fn packet_size(&self) -> usize;
62 fn reply_context(&self) -> M::ReplyContext;
63}