@@ -21,13 +21,31 @@ const MCTP_I2C_HEADER: usize = 4;
2121// bytecount is limited to u8, includes MCTP payload + 1 byte i2c source
2222pub const MCTP_I2C_MAXMTU : usize = u8:: MAX as usize - 1 ;
2323
24+ /// MCTP I2C encapsulation header
2425pub struct MctpI2cHeader {
26+ /// Destination address
27+ ///
28+ /// The 7-bit destination address of the encapsulated packet.
29+ /// (Not including the SMBus R/W# bit)
2530 pub dest : u8 ,
31+ /// Source address
32+ ///
33+ /// The 7-bit source address of the encapsulated packet.
34+ /// (Not including fixed bit `[0]`)
2635 pub source : u8 ,
36+ /// Byte count
37+ ///
38+ /// The count of bytes that follow the _Byte Count_ field up to,
39+ /// but not including, the PEC byte.
2740 pub byte_count : usize ,
2841}
2942
3043impl MctpI2cHeader {
44+ /// Encode this header
45+ ///
46+ /// Creates a 4-byte header with destination, command code, byte count, and source.
47+ /// Returns a [BadArgument](Error::BadArgument) error when the addresses are not 7-bit,
48+ /// or when the byte count is larger than [u8::MAX].
3149 fn encode ( & self ) -> Result < [ u8 ; 4 ] > {
3250 if self . dest > 0x7f || self . source > 0x7f {
3351 return Err ( Error :: BadArgument ) ;
@@ -85,6 +103,17 @@ impl MctpI2cEncap {
85103 self . own_addr
86104 }
87105
106+ /// Decode a I2C encapsulated packet
107+ ///
108+ /// Decodes and verifies the I2C transport header.
109+ /// Optionally an included _PEC_ will be verified.
110+ ///
111+ /// The inner MCTP packet and the decoded header are returned on success.
112+ ///
113+ /// ### Errors when
114+ /// - The _PEC_ is incorrect
115+ /// - Header decoding fails
116+ /// - The decoded byte count field does not match the packet size
88117 pub fn decode < ' f > (
89118 & self ,
90119 mut packet : & ' f [ u8 ] ,
@@ -112,6 +141,9 @@ impl MctpI2cEncap {
112141 trace ! ( "Packet byte count mismatch" ) ;
113142 return Err ( Error :: InvalidInput ) ;
114143 }
144+ if header. dest != self . own_addr {
145+ trace ! ( "I2C destination address mismatch" ) ;
146+ }
115147
116148 Ok ( ( & packet[ MCTP_I2C_HEADER ..] , header) )
117149 }
0 commit comments