-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathiso-tp.cpp
517 lines (464 loc) · 15 KB
/
iso-tp.cpp
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#include "Arduino.h"
#include "iso-tp.h"
#include <mcp_can.h>
#include <mcp_can_dfs.h>
#include <SPI.h>
IsoTp::IsoTp(MCP_CAN* bus, uint8_t mcp_int)
{
_mcp_int = mcp_int;
_bus = bus;
}
void IsoTp::print_buffer(INT32U id, uint8_t *buffer, uint16_t len)
{
uint16_t i=0;
Serial.print(F("Buffer: "));
Serial.print(id,HEX); Serial.print(F(" ["));
Serial.print(len); Serial.print(F("] "));
for(i=0;i<len;i++)
{
if(buffer[i] < 0x10) Serial.print(F("0"));
Serial.print(buffer[i],HEX);
Serial.print(F(" "));
}
Serial.println();
}
uint8_t IsoTp::can_send(INT32U id, uint8_t len, uint8_t *data)
{
#ifdef ISO_TP_DEBUG
Serial.println(F("Send CAN RAW Data:"));
print_buffer(id, data, len);
#endif
return _bus->sendMsgBuf(id, 0, len, data);
}
uint8_t IsoTp::can_receive(void)
{
bool msgReceived;
if (_mcp_int)
msgReceived = (!digitalRead(_mcp_int)); // IRQ: if pin is low, read receive buffer
else
msgReceived = (_bus->checkReceive() == CAN_MSGAVAIL); // No IRQ: poll receive buffer
if (msgReceived)
{
memset(rxBuffer,0,sizeof(rxBuffer)); // Cleanup Buffer
_bus->readMsgBuf(&rxId, &rxLen, rxBuffer); // Read data: buf = data byte(s)
#ifdef ISO_TP_DEBUG
Serial.println(F("Received CAN RAW Data:"));
print_buffer(rxId, rxBuffer, rxLen);
#endif
return true;
}
else return false;
}
uint8_t IsoTp::send_fc(struct Message_t *msg)
{
uint8_t TxBuf[8]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// FC message high nibble = 0x3 , low nibble = FC Status
TxBuf[0]=(N_PCI_FC | msg->fc_status);
TxBuf[1]=msg->blocksize;
/* fix wrong separation time values according spec */
if ((msg->min_sep_time > 0x7F) && ((msg->min_sep_time < 0xF1)
|| (msg->min_sep_time > 0xF9))) msg->min_sep_time = 0x7F;
TxBuf[2]=msg->min_sep_time;
return can_send(msg->tx_id,8,TxBuf);
}
uint8_t IsoTp::send_sf(struct Message_t *msg) //Send SF Message
{
uint8_t TxBuf[8]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// SF message high nibble = 0x0 , low nibble = Length
TxBuf[0]=(N_PCI_SF | msg->len);
memcpy(TxBuf+1,msg->Buffer,msg->len);
// return can_send(msg->tx_id,msg->len+1,TxBuf);// Add PCI length
return can_send(msg->tx_id,8,TxBuf);// Always send full frame
}
uint8_t IsoTp::send_ff(struct Message_t *msg) // Send FF
{
uint8_t TxBuf[8]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
msg->seq_id=1;
TxBuf[0]=(N_PCI_FF | ((msg->len&0x0F00) >> 8));
TxBuf[1]=(msg->len&0x00FF);
memcpy(TxBuf+2,msg->Buffer,6); // Skip 2 Bytes PCI
return can_send(msg->tx_id,8,TxBuf); // First Frame has full length
}
uint8_t IsoTp::send_cf(struct Message_t *msg) // Send SF Message
{
uint8_t TxBuf[8]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint16_t len=7;
TxBuf[0]=(N_PCI_CF | (msg->seq_id & 0x0F));
if(msg->len>7) len=7; else len=msg->len;
memcpy(TxBuf+1,msg->Buffer,len); // Skip 1 Byte PCI
//return can_send(msg->tx_id,len+1,TxBuf); // Last frame is probably shorter
// than 8 -> Signals last CF Frame
return can_send(msg->tx_id,8,TxBuf); // Last frame is probably shorter
// than 8, pad with 00
}
void IsoTp::fc_delay(uint8_t sep_time)
{
/*
* 0x00 - 0x7F: 0 - 127ms
* 0x80 - 0xF0: reserved
* 0xF1 - 0xF9: 100us - 900us
* 0xFA - 0xFF: reserved
* default 0x7F, 127ms
*/
if(sep_time <= 0x7F)
delay(sep_time);
else if ((sep_time >= 0xF1) && (sep_time <= 0xF9))
delayMicroseconds((sep_time-0xF0)*100);
else
delay(0x7F);
}
uint8_t IsoTp::rcv_sf(struct Message_t* msg)
{
/* get the SF_DL from the N_PCI byte */
msg->len = rxBuffer[0] & 0x0F;
/* copy the received data bytes */
memcpy(msg->Buffer,rxBuffer+1,msg->len); // Skip PCI, SF uses len bytes
msg->tp_state=ISOTP_FINISHED;
return 0;
}
uint8_t IsoTp::rcv_ff(struct Message_t* msg)
{
msg->seq_id=1;
/* get the FF_DL */
msg->len = (rxBuffer[0] & 0x0F) << 8;
msg->len += rxBuffer[1];
rest=msg->len;
/* copy the first received data bytes */
memcpy(msg->Buffer,rxBuffer+2,6); // Skip 2 bytes PCI, FF must have 6 bytes!
rest-=6; // Restlength
msg->tp_state = ISOTP_WAIT_DATA;
#ifdef ISO_TP_DEBUG
Serial.print(F("First frame received with message length: "));
Serial.println(rest);
Serial.println(F("Send flow controll."));
Serial.print(F("ISO-TP state: ")); Serial.println(msg->tp_state);
#endif
/* send our first FC frame with Target Address*/
struct Message_t fc;
fc.tx_id=msg->tx_id;
fc.fc_status=ISOTP_FC_CTS;
fc.blocksize=0;
fc.min_sep_time=0;
return send_fc(&fc);
}
uint8_t IsoTp::rcv_cf(struct Message_t* msg)
{
//Handle Timeout
//If no Frame within 250ms change State to ISOTP_IDLE
INT32U delta=millis()-wait_cf;
if((delta >= TIMEOUT_FC) && msg->seq_id>1)
{
#ifdef ISO_TP_DEBUG
Serial.println(F("CF frame timeout during receive wait_cf="));
Serial.print(wait_cf); Serial.print(F(" delta="));
Serial.println(delta);
#endif
msg->tp_state = ISOTP_IDLE;
return 1;
}
wait_cf=millis();
#ifdef ISO_TP_DEBUG
Serial.print(F("ISO-TP state: ")); Serial.println(msg->tp_state);
Serial.print(F("CF received with message rest length: "));
Serial.println(rest);
#endif
if (msg->tp_state != ISOTP_WAIT_DATA) return 0;
if ((rxBuffer[0] & 0x0F) != (msg->seq_id & 0x0F))
{
#ifdef ISO_TP_DEBUG
Serial.print(F("Got sequence ID: ")); Serial.print(rxBuffer[0] & 0x0F);
Serial.print(F(" Expected: ")); Serial.println(msg->seq_id & 0x0F);
#endif
msg->tp_state = ISOTP_IDLE;
msg->seq_id = 1;
return 1;
}
if(rest<=7) // Last Frame
{
memcpy(msg->Buffer+6+7*(msg->seq_id-1),rxBuffer+1,rest);// 6 Bytes in FF +7
msg->tp_state=ISOTP_FINISHED; // per CF skip PCI
#ifdef ISO_TP_DEBUG
Serial.print(F("Last CF received with seq. ID: "));
Serial.println(msg->seq_id);
#endif
}
else
{
#ifdef ISO_TP_DEBUG
Serial.print(F("CF received with seq. ID: "));
Serial.println(msg->seq_id);
#endif
memcpy(msg->Buffer+6+7*(msg->seq_id-1),rxBuffer+1,7); // 6 Bytes in FF +7
// per CF
rest-=7; // Got another 7 Bytes of Data;
}
msg->seq_id++;
return 0;
}
uint8_t IsoTp::rcv_fc(struct Message_t* msg)
{
uint8_t retval=0;
if (msg->tp_state != ISOTP_WAIT_FC && msg->tp_state != ISOTP_WAIT_FIRST_FC)
return 0;
/* get communication parameters only from the first FC frame */
if (msg->tp_state == ISOTP_WAIT_FIRST_FC)
{
msg->blocksize = rxBuffer[1];
msg->min_sep_time = rxBuffer[2];
/* fix wrong separation time values according spec */
if ((msg->min_sep_time > 0x7F) && ((msg->min_sep_time < 0xF1)
|| (msg->min_sep_time > 0xF9))) msg->min_sep_time = 0x7F;
}
#ifdef ISO_TP_DEBUG
Serial.print(F("FC frame: FS "));
Serial.print(rxBuffer[0]&0x0F);
Serial.print(F(", Blocksize "));
Serial.print(msg->blocksize);
Serial.print(F(", Min. separation Time "));
Serial.println(msg->min_sep_time);
#endif
switch (rxBuffer[0] & 0x0F)
{
case ISOTP_FC_CTS:
msg->tp_state = ISOTP_SEND_CF;
break;
case ISOTP_FC_WT:
fc_wait_frames++;
if(fc_wait_frames >= MAX_FCWAIT_FRAME)
{
#ifdef ISO_TP_DEBUG
Serial.println(F("FC wait frames exceeded."));
#endif
fc_wait_frames=0;
msg->tp_state = ISOTP_IDLE;
retval=1;
}
#ifdef ISO_TP_DEBUG
Serial.println(F("Start waiting for next FC"));
#endif
break;
case ISOTP_FC_OVFLW:
#ifdef ISO_TP_DEBUG
Serial.println(F("Overflow in receiver side"));
#endif
default:
msg->tp_state = ISOTP_IDLE;
retval=1;
}
return retval;
}
uint8_t IsoTp::send(Message_t* msg)
{
uint8_t bs=false;
INT32U delta=0;
uint8_t retval=0;
msg->tp_state=ISOTP_SEND;
while(msg->tp_state!=ISOTP_IDLE && msg->tp_state!=ISOTP_ERROR)
{
bs=false;
#ifdef ISO_TP_DEBUG
Serial.print(F("ISO-TP State: ")); Serial.println(msg->tp_state);
Serial.print(F("Length : ")); Serial.println(msg->len);
#endif
switch(msg->tp_state)
{
case ISOTP_IDLE : break;
case ISOTP_SEND :
if(msg->len<=7)
{
#ifdef ISO_TP_DEBUG
Serial.println(F("Send SF"));
#endif
retval=send_sf(msg);
msg->tp_state=ISOTP_IDLE;
}
else
{
#ifdef ISO_TP_DEBUG
Serial.println(F("Send FF"));
#endif
if(!(retval=send_ff(msg))) // FF complete
{
msg->Buffer+=6;
msg->len-=6;
msg->tp_state=ISOTP_WAIT_FIRST_FC;
fc_wait_frames=0;
wait_fc=millis();
}
}
break;
case ISOTP_WAIT_FIRST_FC:
#ifdef ISO_TP_DEBUG
Serial.println(F("Wait first FC"));
#endif
delta=millis()-wait_fc;
if(delta >= TIMEOUT_FC)
{
#ifdef ISO_TP_DEBUG
Serial.print(F("FC timeout during receive"));
Serial.print(F(" wait_fc="));
Serial.print(wait_fc);
Serial.print(F(" delta="));
Serial.println(delta);
#endif
msg->tp_state = ISOTP_IDLE;
retval=1;
}
break;
case ISOTP_WAIT_FC :
#ifdef ISO_TP_DEBUG
Serial.println(F("Wait FC"));
#endif
break;
case ISOTP_SEND_CF :
#ifdef ISO_TP_DEBUG
Serial.println(F("Send CF"));
#endif
while(msg->len>7 && !bs)
{
fc_delay(msg->min_sep_time);
if(!(retval=send_cf(msg)))
{
#ifdef ISO_TP_DEBUG
Serial.print(F("Send Seq "));
Serial.println(msg->seq_id);
#endif
if(msg->blocksize > 0)
{
#ifdef ISO_TP_DEBUG
Serial.print(F("Blocksize trigger "));
Serial.print(msg->seq_id %
msg->blocksize);
#endif
if(!(msg->seq_id % msg->blocksize))
{
bs=true;
msg->tp_state=ISOTP_WAIT_FC;
#ifdef ISO_TP_DEBUG
Serial.println(F(" yes"));
#endif
}
#ifdef ISO_TP_DEBUG
else Serial.println(F(" no"));
#endif
}
msg->seq_id++;
if (msg->blocksize < 16)
msg->seq_id %= 16;
else
msg->seq_id %= msg->blocksize;
msg->Buffer+=7;
msg->len-=7;
#ifdef ISO_TP_DEBUG
Serial.print(F("Length : "));
Serial.println(msg->len);
#endif
}
}
if(!bs)
{
fc_delay(msg->min_sep_time);
#ifdef ISO_TP_DEBUG
Serial.print(F("Send last Seq "));
Serial.println(msg->seq_id);
#endif
retval=send_cf(msg);
msg->tp_state=ISOTP_IDLE;
}
break;
default : break;
}
if(msg->tp_state==ISOTP_WAIT_FIRST_FC ||
msg->tp_state==ISOTP_WAIT_FC)
{
if(can_receive())
{
#ifdef ISO_TP_DEBUG
Serial.println(F("Send branch:"));
#endif
if(rxId==msg->rx_id)
{
retval=rcv_fc(msg);
memset(rxBuffer,0,sizeof(rxBuffer));
#ifdef ISO_TP_DEBUG
Serial.println(F("rxId OK!"));
#endif
}
}
}
}
return retval;
}
uint8_t IsoTp::receive(Message_t* msg)
{
uint8_t n_pci_type=0;
INT32U delta=0;
wait_session=millis();
#ifdef ISO_TP_DEBUG
Serial.println(F("Start receive..."));
#endif
msg->tp_state=ISOTP_IDLE;
while(msg->tp_state!=ISOTP_FINISHED && msg->tp_state!=ISOTP_ERROR)
{
delta=millis()-wait_session;
if(delta >= TIMEOUT_SESSION)
{
#ifdef ISO_TP_DEBUG
Serial.print(F("ISO-TP Session timeout wait_session="));
Serial.print(wait_session); Serial.print(F(" delta="));
Serial.println(delta);
#endif
return 1;
}
if(can_receive())
{
if(rxId==msg->rx_id)
{
#ifdef ISO_TP_DEBUG
Serial.println(F("rxId OK!"));
#endif
n_pci_type=rxBuffer[0] & 0xF0;
switch (n_pci_type)
{
case N_PCI_FC:
#ifdef ISO_TP_DEBUG
Serial.println(F("FC"));
#endif
/* tx path: fc frame */
rcv_fc(msg);
break;
case N_PCI_SF:
#ifdef ISO_TP_DEBUG
Serial.println(F("SF"));
#endif
/* rx path: single frame */
rcv_sf(msg);
// msg->tp_state=ISOTP_FINISHED;
break;
case N_PCI_FF:
#ifdef ISO_TP_DEBUG
Serial.println(F("FF"));
#endif
/* rx path: first frame */
rcv_ff(msg);
// msg->tp_state=ISOTP_WAIT_DATA;
break;
break;
case N_PCI_CF:
#ifdef ISO_TP_DEBUG
Serial.println(F("CF"));
#endif
/* rx path: consecutive frame */
rcv_cf(msg);
break;
}
memset(rxBuffer,0,sizeof(rxBuffer));
}
}
}
#ifdef ISO_TP_DEBUG
Serial.println(F("ISO-TP message received:"));
print_buffer(msg->rx_id, msg->Buffer, msg->len);
#endif
return 0;
}