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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::constants::{DEFAULT_BLOCK_SIZE, DEFAULT_ST_MIN};
use crate::core::{FlowControlContext, FlowControlState};
use crate::error::Error;
/// ISO 15765-2 frame type define.
#[repr(u8)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum FrameType {
/// | - data length -| - N_PCI bytes - | - note - |
///
/// | - le 8 - | - bit0(3~0) = length - | - std2004 - |
///
/// | - gt 8 - | - bit0(3~0) = 0; bit1(7~0) = length - | - std2016 - |
Single = 0x00,
/// | - data length -| - N_PCI bytes - | - note - |
///
/// | - le 4095 - | - bit0(3~0) + bit1(7~0) = length - | - std2004 - |
///
/// | - gt 4095 - | - bit0(3~0) + bit1(7~0) = 0; byte2~5(7~0) = length - | - std2016 - |
First = 0x10,
Consecutive = 0x20,
FlowControl = 0x30,
}
impl From<FrameType> for u8 {
#[inline]
fn from(val: FrameType) -> Self {
val as u8
}
}
impl TryFrom<u8> for FrameType {
type Error = Error;
#[inline]
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value & 0xF0 {
0x00 => Ok(Self::Single),
0x10 => Ok(Self::First),
0x20 => Ok(Self::Consecutive),
0x30 => Ok(Self::FlowControl),
v => Err(Error::InvalidParam(format!("`frame type`({})", v))),
}
}
}
/// ISO-TP frame define.
#[derive(Debug, Clone)]
pub enum Frame {
/// The ISO-TP single frame.
SingleFrame { data: Vec<u8> },
/// The ISO-TP first frame.
FirstFrame { length: u32, data: Vec<u8> },
/// The ISO-TP consecutive frame.
ConsecutiveFrame { sequence: u8, data: Vec<u8> },
/// The ISO-TP flow control frame.
FlowControlFrame(FlowControlContext),
}
unsafe impl Send for Frame {}
impl From<&Frame> for FrameType {
fn from(val: &Frame) -> Self {
match val {
Frame::SingleFrame { .. } => FrameType::Single,
Frame::FirstFrame { .. } => FrameType::First,
Frame::ConsecutiveFrame { .. } => FrameType::Consecutive,
Frame::FlowControlFrame(..) => FrameType::FlowControl,
}
}
}
impl Frame {
/// Decode frame from origin data like `02 10 01`.
///
/// # Parameters
///
/// * `data` - the source data.
///
/// # Return
///
/// A struct that implements [`IsoTpFrame`] if parameters are valid.
pub fn decode<T: AsRef<[u8]>>(data: T) -> Result<Self, Error> {
let data = data.as_ref();
let length = data.len();
match length {
0 => Err(Error::EmptyPdu),
1..=2 => Err(Error::InvalidPdu(data.to_vec())),
3.. => {
let byte0 = data[0];
match FrameType::try_from(byte0)? {
FrameType::Single => {
// Single frame
#[cfg(feature = "can")]
crate::can::standard::decode_single(data, byte0, length)
}
FrameType::First => {
// First frame
#[cfg(feature = "can")]
crate::can::standard::decode_first(data, byte0, length)
}
FrameType::Consecutive => {
let sequence = byte0 & 0x0F;
Ok(Self::ConsecutiveFrame {
sequence,
data: Vec::from(&data[1..]),
})
}
FrameType::FlowControl => {
// let suppress_positive = (data1 & 0x80) == 0x80;
let state = FlowControlState::try_from(byte0 & 0x0F)?;
let fc = FlowControlContext::new(state, data[1], data[2])?;
Ok(Self::FlowControlFrame(fc))
}
}
} // v => Err(IsoTpError::LengthOutOfRange(v)),
}
}
/// Encode frame to data.
///
/// # Parameters
///
/// * `padding` - the padding value when the length of return value is insufficient.
///
/// # Returns
///
/// The encoded data.
pub fn encode(self, padding: Option<u8>) -> Vec<u8> {
match self {
Self::SingleFrame { data } =>
{
#[cfg(feature = "can")]
crate::can::standard::encode_single(data, padding)
}
Self::FirstFrame { length, data } =>
{
#[cfg(feature = "can")]
crate::can::standard::encode_first(length, data)
}
Self::ConsecutiveFrame { sequence, mut data } => {
let mut result = vec![FrameType::Consecutive as u8 | sequence];
result.append(&mut data);
#[cfg(feature = "can")]
result.resize(
rs_can::MAX_FRAME_SIZE,
padding.unwrap_or(rs_can::DEFAULT_PADDING),
);
result
}
Self::FlowControlFrame(context) => {
let byte0_h: u8 = FrameType::FlowControl.into();
let byte0_l: u8 = context.state().into();
let mut result = vec![byte0_h | byte0_l, context.block_size(), context.st_min()];
result.resize(
rs_can::MAX_FRAME_SIZE,
padding.unwrap_or(rs_can::DEFAULT_PADDING),
);
result
}
}
}
/// Encoding full multi-frame from original data.
///
/// # Parameters
///
/// * `data` - original data
///
/// * `flow_ctrl` - the flow control context(added one default)
///
/// # Returns
///
/// The frames contain either a `SingleFrame` or a multi-frame sequence starting
///
/// with a `FirstFrame` and followed by at least one `FlowControlFrame`.
#[inline]
pub fn from_data<T: AsRef<[u8]>>(data: T) -> Result<Vec<Self>, Error> {
#[cfg(feature = "can")]
crate::can::standard::from_data(data.as_ref())
}
/// New single frame from data.
///
/// * `data` - the single frame data
///
/// # Returns
///
/// A new `SingleFrame` if parameters are valid.
#[inline]
pub fn single_frame<T: AsRef<[u8]>>(data: T) -> Result<Self, Error> {
#[cfg(feature = "can")]
crate::can::standard::new_single(data)
}
/// New flow control frame from data.
///
/// # Parameters
///
/// * `state` - [`FlowControlState`]
/// * `block_size` - the block size
/// * `st_min` - separation time minimum
///
/// # Returns
///
/// A new `FlowControlFrame` if parameters are valid.
#[inline]
pub fn flow_ctrl_frame(
state: FlowControlState,
block_size: u8,
st_min: u8,
) -> Result<Self, Error> {
Ok(Self::FlowControlFrame(FlowControlContext::new(
state, block_size, st_min,
)?))
}
#[inline]
pub fn default_flow_ctrl_frame() -> Self {
Self::flow_ctrl_frame(
FlowControlState::Continues,
DEFAULT_BLOCK_SIZE,
DEFAULT_ST_MIN,
)
.unwrap()
}
}