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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! ISO-TP state machine types.
use std::time::Instant;
/// ISO-TP receive state.
#[derive(Debug, Default)]
pub enum RxState {
/// Idle, waiting for SF or FF
#[default]
Idle,
/// Receiving multi-frame message
Receiving {
/// Receive buffer
buffer: Vec<u8>,
/// Expected total length
expected_length: usize,
/// Next expected sequence number (0-15)
next_sequence: u8,
/// Frames received in current block
block_count: u8,
/// Reception start time
start_time: Instant,
/// Last frame receive time
last_frame_time: Instant,
},
}
impl RxState {
/// Check if in idle state.
#[must_use]
pub fn is_idle(&self) -> bool {
matches!(self, RxState::Idle)
}
/// Check if currently receiving.
#[must_use]
pub fn is_receiving(&self) -> bool {
matches!(self, RxState::Receiving { .. })
}
/// Get the current buffer length if receiving.
#[must_use]
pub fn buffer_len(&self) -> Option<usize> {
match self {
RxState::Idle => None,
RxState::Receiving { buffer, .. } => Some(buffer.len()),
}
}
/// Get the expected total length if receiving.
#[must_use]
pub fn expected_length(&self) -> Option<usize> {
match self {
RxState::Idle => None,
RxState::Receiving {
expected_length, ..
} => Some(*expected_length),
}
}
/// Get the progress as a percentage (0-100) if receiving.
#[must_use]
pub fn progress_percent(&self) -> Option<u8> {
match self {
RxState::Idle => None,
RxState::Receiving {
buffer,
expected_length,
..
} => {
if *expected_length == 0 {
Some(0)
} else {
let progress = (buffer.len() * 100 / *expected_length).min(100);
Some(u8::try_from(progress).unwrap_or(100))
}
}
}
}
}
/// ISO-TP transmit state.
#[derive(Debug, Default)]
pub enum TxState {
/// Idle
#[default]
Idle,
/// Waiting for Flow Control after sending FF
WaitingForFc {
/// Data buffer to send
buffer: Vec<u8>,
/// Bytes already sent
offset: usize,
/// Next sequence number
next_sequence: u8,
/// Transmission start time
start_time: Instant,
/// Time when FC wait started
fc_wait_start: Instant,
/// Number of FC(Wait) responses received
wait_count: u8,
},
/// Sending consecutive frames
SendingCf {
/// Data buffer to send
buffer: Vec<u8>,
/// Bytes already sent
offset: usize,
/// Next sequence number
next_sequence: u8,
/// Frames sent in current block
block_count: u8,
/// Block size limit (0 = no limit)
block_size: u8,
/// Minimum separation time
st_min: std::time::Duration,
/// Transmission start time
start_time: Instant,
/// Last frame send time
last_frame_time: Instant,
},
}
impl TxState {
/// Check if in idle state.
#[must_use]
pub fn is_idle(&self) -> bool {
matches!(self, TxState::Idle)
}
/// Check if waiting for flow control.
#[must_use]
pub fn is_waiting_for_fc(&self) -> bool {
matches!(self, TxState::WaitingForFc { .. })
}
/// Check if sending consecutive frames.
#[must_use]
pub fn is_sending_cf(&self) -> bool {
matches!(self, TxState::SendingCf { .. })
}
/// Check if currently sending (either waiting for FC or sending CF).
#[must_use]
pub fn is_sending(&self) -> bool {
!self.is_idle()
}
/// Get the total buffer length if sending.
#[must_use]
pub fn buffer_len(&self) -> Option<usize> {
match self {
TxState::Idle => None,
TxState::WaitingForFc { buffer, .. } | TxState::SendingCf { buffer, .. } => {
Some(buffer.len())
}
}
}
/// Get the bytes sent so far if sending.
#[must_use]
pub fn bytes_sent(&self) -> Option<usize> {
match self {
TxState::Idle => None,
TxState::WaitingForFc { offset, .. } | TxState::SendingCf { offset, .. } => {
Some(*offset)
}
}
}
/// Get the progress as a percentage (0-100) if sending.
#[must_use]
pub fn progress_percent(&self) -> Option<u8> {
match self {
TxState::Idle => None,
TxState::WaitingForFc { buffer, offset, .. }
| TxState::SendingCf { buffer, offset, .. } => {
if buffer.is_empty() {
Some(0)
} else {
let progress = (*offset * 100 / buffer.len()).min(100);
Some(u8::try_from(progress).unwrap_or(100))
}
}
}
}
}
/// Combined ISO-TP channel state.
#[derive(Debug, Default)]
pub struct IsoTpState {
/// Receive state
pub rx: RxState,
/// Transmit state
pub tx: TxState,
}
impl IsoTpState {
/// Create a new idle state.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Check if both RX and TX are idle.
#[must_use]
pub fn is_idle(&self) -> bool {
self.rx.is_idle() && self.tx.is_idle()
}
/// Check if currently receiving.
#[must_use]
pub fn is_receiving(&self) -> bool {
self.rx.is_receiving()
}
/// Check if currently sending.
#[must_use]
pub fn is_sending(&self) -> bool {
self.tx.is_sending()
}
/// Reset both RX and TX to idle.
pub fn reset(&mut self) {
self.rx = RxState::Idle;
self.tx = TxState::Idle;
}
/// Reset only RX to idle.
pub fn reset_rx(&mut self) {
self.rx = RxState::Idle;
}
/// Reset only TX to idle.
pub fn reset_tx(&mut self) {
self.tx = TxState::Idle;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rx_state_idle() {
let state = RxState::Idle;
assert!(state.is_idle());
assert!(!state.is_receiving());
assert_eq!(state.buffer_len(), None);
assert_eq!(state.expected_length(), None);
assert_eq!(state.progress_percent(), None);
}
#[test]
fn test_rx_state_receiving() {
let now = Instant::now();
let state = RxState::Receiving {
buffer: vec![0x01, 0x02, 0x03, 0x04, 0x05],
expected_length: 20,
next_sequence: 2,
block_count: 1,
start_time: now,
last_frame_time: now,
};
assert!(!state.is_idle());
assert!(state.is_receiving());
assert_eq!(state.buffer_len(), Some(5));
assert_eq!(state.expected_length(), Some(20));
assert_eq!(state.progress_percent(), Some(25)); // 5/20 = 25%
}
#[test]
fn test_tx_state_idle() {
let state = TxState::Idle;
assert!(state.is_idle());
assert!(!state.is_waiting_for_fc());
assert!(!state.is_sending_cf());
assert!(!state.is_sending());
assert_eq!(state.buffer_len(), None);
assert_eq!(state.bytes_sent(), None);
assert_eq!(state.progress_percent(), None);
}
#[test]
fn test_tx_state_waiting_for_fc() {
let now = Instant::now();
let state = TxState::WaitingForFc {
buffer: vec![0; 100],
offset: 6,
next_sequence: 1,
start_time: now,
fc_wait_start: now,
wait_count: 0,
};
assert!(!state.is_idle());
assert!(state.is_waiting_for_fc());
assert!(!state.is_sending_cf());
assert!(state.is_sending());
assert_eq!(state.buffer_len(), Some(100));
assert_eq!(state.bytes_sent(), Some(6));
assert_eq!(state.progress_percent(), Some(6)); // 6/100 = 6%
}
#[test]
fn test_tx_state_sending_cf() {
let now = Instant::now();
let state = TxState::SendingCf {
buffer: vec![0; 50],
offset: 25,
next_sequence: 4,
block_count: 3,
block_size: 8,
st_min: std::time::Duration::from_millis(10),
start_time: now,
last_frame_time: now,
};
assert!(!state.is_idle());
assert!(!state.is_waiting_for_fc());
assert!(state.is_sending_cf());
assert!(state.is_sending());
assert_eq!(state.buffer_len(), Some(50));
assert_eq!(state.bytes_sent(), Some(25));
assert_eq!(state.progress_percent(), Some(50)); // 25/50 = 50%
}
#[test]
fn test_isotp_state() {
let mut state = IsoTpState::new();
assert!(state.is_idle());
assert!(!state.is_receiving());
assert!(!state.is_sending());
// Simulate receiving
let now = Instant::now();
state.rx = RxState::Receiving {
buffer: vec![],
expected_length: 100,
next_sequence: 1,
block_count: 0,
start_time: now,
last_frame_time: now,
};
assert!(!state.is_idle());
assert!(state.is_receiving());
assert!(!state.is_sending());
// Reset RX only
state.reset_rx();
assert!(state.is_idle());
// Simulate sending
state.tx = TxState::WaitingForFc {
buffer: vec![0; 50],
offset: 6,
next_sequence: 1,
start_time: now,
fc_wait_start: now,
wait_count: 0,
};
assert!(!state.is_idle());
assert!(!state.is_receiving());
assert!(state.is_sending());
// Full reset
state.reset();
assert!(state.is_idle());
}
#[test]
fn test_progress_edge_cases() {
let now = Instant::now();
// Empty buffer
let state = TxState::SendingCf {
buffer: vec![],
offset: 0,
next_sequence: 0,
block_count: 0,
block_size: 0,
st_min: std::time::Duration::ZERO,
start_time: now,
last_frame_time: now,
};
assert_eq!(state.progress_percent(), Some(0));
// Zero expected length
let state = RxState::Receiving {
buffer: vec![],
expected_length: 0,
next_sequence: 0,
block_count: 0,
start_time: now,
last_frame_time: now,
};
assert_eq!(state.progress_percent(), Some(0));
// 100% complete
let state = RxState::Receiving {
buffer: vec![0; 100],
expected_length: 100,
next_sequence: 0,
block_count: 0,
start_time: now,
last_frame_time: now,
};
assert_eq!(state.progress_percent(), Some(100));
}
}