1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::base_packet::{MCTPMessageBody, MCTPTransportHeader};
use crate::control_packet::RoutingInformationUpdateEntryType;
use crate::mctp_traits::MCTPHeader;
use smbus_pec::pec;
pub const HDR_VERSION: u8 = 0b001;
pub(crate) const MCTP_SMBUS_COMMAND_CODE: u8 = 0x0F;
bitfield! {
pub struct MCTPSMBusHeader([u8]);
u8;
pub dest_read_write, set_dest_read_write: 0, 0;
pub dest_slave_addr, set_dest_slave_addr : 7, 1;
pub command_code, set_command_code: 15, 8;
pub byte_count, set_byte_count: 23, 16;
pub source_read_write, set_source_read_write: 24, 24;
pub source_slave_addr, set_source_slave_addr: 30, 25;
}
impl MCTPSMBusHeader<[u8; 4]> {
pub fn new() -> Self {
let buf = [0; 4];
MCTPSMBusHeader(buf)
}
pub fn new_from_buf(buf: [u8; 4]) -> Self {
MCTPSMBusHeader(buf)
}
}
impl Default for MCTPSMBusHeader<[u8; 4]> {
fn default() -> Self {
Self::new()
}
}
pub(crate) struct MCTPSMBusPacket<'a, 'b> {
smbus_header: &'a mut MCTPSMBusHeader<[u8; 4]>,
base_header: &'a MCTPTransportHeader<[u8; 4]>,
data_bytes: &'a MCTPMessageBody<'a, 'b>,
}
impl<'a, 'b> MCTPSMBusPacket<'a, 'b> {
pub fn new(
smbus_header: &'a mut MCTPSMBusHeader<[u8; 4]>,
base_header: &'a MCTPTransportHeader<[u8; 4]>,
data_bytes: &'b MCTPMessageBody,
) -> Self {
let mut packet = Self {
smbus_header,
base_header,
data_bytes,
};
packet.finalise();
packet
}
fn finalise(&mut self) {
self.smbus_header.set_byte_count(self.len() as u8 - 4);
}
}
impl<'a, 'b> MCTPHeader for MCTPSMBusPacket<'a, 'b> {
fn len(&self) -> usize {
let mut size = 0;
size += 4;
size += 4;
size += self.data_bytes.len();
size += 1;
size
}
fn to_raw_bytes(&self, buf: &mut [u8]) -> usize {
let mut size = 0;
buf[0..4].copy_from_slice(&self.smbus_header.0);
size += 4;
buf[4..8].copy_from_slice(&self.base_header.0);
size += 4;
size += self.data_bytes.to_raw_bytes(&mut buf[size..]);
buf[size] = pec(&buf[0..size]);
size += 1;
size
}
}
bitfield! {
pub struct SMBusRoutingInformationUpdateEntry([u8]);
u8;
pub entry_type, set_entry_type: 3, 0;
_rsvd, _: 7, 4;
pub eid_range_size, set_eid_range_size: 15, 8;
pub first_eid, set_first_eid: 23, 16;
pub physical_address, set_physical_address: 31, 24;
}
impl SMBusRoutingInformationUpdateEntry<[u8; 4]> {
pub fn new(
entry_type: RoutingInformationUpdateEntryType,
range_size: u8,
first_eid: u8,
physical_address: u8,
) -> Self {
let buf = [0; 4];
let mut format = SMBusRoutingInformationUpdateEntry(buf);
format.set_entry_type(entry_type as u8);
format.set_eid_range_size(range_size);
format.set_first_eid(first_eid);
format.set_physical_address(physical_address);
format
}
pub fn new_from_buf(buf: [u8; 4]) -> Self {
SMBusRoutingInformationUpdateEntry(buf)
}
}