-
Notifications
You must be signed in to change notification settings - Fork 79
/
isotp_defines.h
225 lines (197 loc) · 6.05 KB
/
isotp_defines.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
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
#ifndef __ISOTP_TYPES__
#define __ISOTP_TYPES__
/**************************************************************
* compiler specific defines
*************************************************************/
#ifdef __GNUC__
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#else
#error "unsupported byte ordering"
#endif
#endif
/**************************************************************
* OS specific defines
*************************************************************/
#ifdef _WIN32
#define snprintf _snprintf
#endif
#ifdef _WIN32
#define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
#define __builtin_bswap8 _byteswap_uint8
#define __builtin_bswap16 _byteswap_uint16
#define __builtin_bswap32 _byteswap_uint32
#define __builtin_bswap64 _byteswap_uint64
#endif
/**************************************************************
* internal used defines
*************************************************************/
#define ISOTP_RET_OK 0
#define ISOTP_RET_ERROR -1
#define ISOTP_RET_INPROGRESS -2
#define ISOTP_RET_OVERFLOW -3
#define ISOTP_RET_WRONG_SN -4
#define ISOTP_RET_NO_DATA -5
#define ISOTP_RET_TIMEOUT -6
#define ISOTP_RET_LENGTH -7
/* return logic true if 'a' is after 'b' */
#define IsoTpTimeAfter(a,b) ((int32_t)((int32_t)(b) - (int32_t)(a)) < 0)
/* invalid bs */
#define ISOTP_INVALID_BS 0xFFFF
/* ISOTP sender status */
typedef enum {
ISOTP_SEND_STATUS_IDLE,
ISOTP_SEND_STATUS_INPROGRESS,
ISOTP_SEND_STATUS_ERROR,
} IsoTpSendStatusTypes;
/* ISOTP receiver status */
typedef enum {
ISOTP_RECEIVE_STATUS_IDLE,
ISOTP_RECEIVE_STATUS_INPROGRESS,
ISOTP_RECEIVE_STATUS_FULL,
} IsoTpReceiveStatusTypes;
/* can fram defination */
#if defined(ISOTP_BYTE_ORDER_LITTLE_ENDIAN)
typedef struct {
uint8_t reserve_1:4;
uint8_t type:4;
uint8_t reserve_2[7];
} IsoTpPciType;
typedef struct {
uint8_t SF_DL:4;
uint8_t type:4;
uint8_t data[7];
} IsoTpSingleFrame;
typedef struct {
uint8_t FF_DL_high:4;
uint8_t type:4;
uint8_t FF_DL_low;
uint8_t data[6];
} IsoTpFirstFrame;
typedef struct {
uint8_t SN:4;
uint8_t type:4;
uint8_t data[7];
} IsoTpConsecutiveFrame;
typedef struct {
uint8_t FS:4;
uint8_t type:4;
uint8_t BS;
uint8_t STmin;
uint8_t reserve[5];
} IsoTpFlowControl;
#else
typedef struct {
uint8_t type:4;
uint8_t reserve_1:4;
uint8_t reserve_2[7];
} IsoTpPciType;
/*
* single frame
* +-------------------------+-----+
* | byte #0 | ... |
* +-------------------------+-----+
* | nibble #0 | nibble #1 | ... |
* +-------------+-----------+ ... +
* | PCIType = 0 | SF_DL | ... |
* +-------------+-----------+-----+
*/
typedef struct {
uint8_t type:4;
uint8_t SF_DL:4;
uint8_t data[7];
} IsoTpSingleFrame;
/*
* first frame
* +-------------------------+-----------------------+-----+
* | byte #0 | byte #1 | ... |
* +-------------------------+-----------+-----------+-----+
* | nibble #0 | nibble #1 | nibble #2 | nibble #3 | ... |
* +-------------+-----------+-----------+-----------+-----+
* | PCIType = 1 | FF_DL | ... |
* +-------------+-----------+-----------------------+-----+
*/
typedef struct {
uint8_t type:4;
uint8_t FF_DL_high:4;
uint8_t FF_DL_low;
uint8_t data[6];
} IsoTpFirstFrame;
/*
* consecutive frame
* +-------------------------+-----+
* | byte #0 | ... |
* +-------------------------+-----+
* | nibble #0 | nibble #1 | ... |
* +-------------+-----------+ ... +
* | PCIType = 0 | SN | ... |
* +-------------+-----------+-----+
*/
typedef struct {
uint8_t type:4;
uint8_t SN:4;
uint8_t data[7];
} IsoTpConsecutiveFrame;
/*
* flow control frame
* +-------------------------+-----------------------+-----------------------+-----+
* | byte #0 | byte #1 | byte #2 | ... |
* +-------------------------+-----------+-----------+-----------+-----------+-----+
* | nibble #0 | nibble #1 | nibble #2 | nibble #3 | nibble #4 | nibble #5 | ... |
* +-------------+-----------+-----------+-----------+-----------+-----------+-----+
* | PCIType = 1 | FS | BS | STmin | ... |
* +-------------+-----------+-----------------------+-----------------------+-----+
*/
typedef struct {
uint8_t type:4;
uint8_t FS:4;
uint8_t BS;
uint8_t STmin;
uint8_t reserve[5];
} IsoTpFlowControl;
#endif
typedef struct {
uint8_t ptr[8];
} IsoTpDataArray;
typedef struct {
union {
IsoTpPciType common;
IsoTpSingleFrame single_frame;
IsoTpFirstFrame first_frame;
IsoTpConsecutiveFrame consecutive_frame;
IsoTpFlowControl flow_control;
IsoTpDataArray data_array;
} as;
} IsoTpCanMessage;
/**************************************************************
* protocol specific defines
*************************************************************/
/* Private: Protocol Control Information (PCI) types, for identifying each frame of an ISO-TP message.
*/
typedef enum {
ISOTP_PCI_TYPE_SINGLE = 0x0,
ISOTP_PCI_TYPE_FIRST_FRAME = 0x1,
TSOTP_PCI_TYPE_CONSECUTIVE_FRAME = 0x2,
ISOTP_PCI_TYPE_FLOW_CONTROL_FRAME = 0x3
} IsoTpProtocolControlInformation;
/* Private: Protocol Control Information (PCI) flow control identifiers.
*/
typedef enum {
PCI_FLOW_STATUS_CONTINUE = 0x0,
PCI_FLOW_STATUS_WAIT = 0x1,
PCI_FLOW_STATUS_OVERFLOW = 0x2
} IsoTpFlowStatus;
/* Private: network layer resault code.
*/
#define ISOTP_PROTOCOL_RESULT_OK 0
#define ISOTP_PROTOCOL_RESULT_TIMEOUT_A -1
#define ISOTP_PROTOCOL_RESULT_TIMEOUT_BS -2
#define ISOTP_PROTOCOL_RESULT_TIMEOUT_CR -3
#define ISOTP_PROTOCOL_RESULT_WRONG_SN -4
#define ISOTP_PROTOCOL_RESULT_INVALID_FS -5
#define ISOTP_PROTOCOL_RESULT_UNEXP_PDU -6
#define ISOTP_PROTOCOL_RESULT_WFT_OVRN -7
#define ISOTP_PROTOCOL_RESULT_BUFFER_OVFLW -8
#define ISOTP_PROTOCOL_RESULT_ERROR -9
#endif