-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathiso-tp.h
87 lines (76 loc) · 2.35 KB
/
iso-tp.h
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
#ifndef _ISOTP_H
#define _ISOTP_H
#include <mcp_can.h>
//#define ISO_TP_DEBUG
typedef enum {
ISOTP_IDLE = 0,
ISOTP_SEND,
ISOTP_SEND_FF,
ISOTP_SEND_CF,
ISOTP_WAIT_FIRST_FC,
ISOTP_WAIT_FC,
ISOTP_WAIT_DATA,
ISOTP_FINISHED,
ISOTP_ERROR
} isotp_states_t;
#define CAN_MAX_DLEN 8 //Not extended CAN
/* N_PCI type values in bits 7-4 of N_PCI bytes */
#define N_PCI_SF 0x00 /* single frame */
#define N_PCI_FF 0x10 /* first frame */
#define N_PCI_CF 0x20 /* consecutive frame */
#define N_PCI_FC 0x30 /* flow control */
#define FC_CONTENT_SZ 3 /* flow control content size in byte (FS/BS/STmin) */
/* Flow Status given in FC frame */
#define ISOTP_FC_CTS 0 /* clear to send */
#define ISOTP_FC_WT 1 /* wait */
#define ISOTP_FC_OVFLW 2 /* overflow */
/* Timeout values */
#define TIMEOUT_SESSION 500 /* Timeout between successfull send and receive */
#define TIMEOUT_FC 250 /* Timeout between FF and FC or Block CF and FC */
#define TIMEOUT_CF 250 /* Timeout between CFs */
#define MAX_FCWAIT_FRAME 10
#define MAX_MSGBUF 128 /* Received Message Buffer. Depends on uC ressources!
Should be enough for our needs */
struct Message_t
{
uint16_t len=0;
isotp_states_t tp_state=ISOTP_IDLE;
uint16_t seq_id=1;
uint8_t fc_status=ISOTP_FC_CTS;
uint8_t blocksize=0;
uint8_t min_sep_time=0;
INT32U tx_id=0;
INT32U rx_id=0;
uint8_t *Buffer;
};
class IsoTp
{
public:
IsoTp(MCP_CAN* bus, uint8_t mcp_int);
uint8_t send(Message_t* msg);
uint8_t receive(Message_t* msg);
void print_buffer(INT32U id, uint8_t *buffer, uint16_t len);
private:
MCP_CAN* _bus;
uint8_t _mcp_int;
INT32U rxId;
uint8_t rxLen;
uint8_t rxBuffer[8];
uint16_t rest;
uint8_t fc_wait_frames=0;
INT32U wait_fc=0;
INT32U wait_cf=0;
INT32U wait_session=0;
uint8_t can_send(INT32U id, uint8_t len, uint8_t *data);
uint8_t can_receive(void);
uint8_t send_fc(struct Message_t* msg);
uint8_t send_sf(struct Message_t* msg);
uint8_t send_ff(struct Message_t* msg);
uint8_t send_cf(struct Message_t* msg);
uint8_t rcv_sf(struct Message_t* msg);
uint8_t rcv_ff(struct Message_t* msg);
uint8_t rcv_cf(struct Message_t* msg);
uint8_t rcv_fc(struct Message_t* msg);
void fc_delay(uint8_t sep_time);
};
#endif