isotp-c
Platform-independent ISO 15765-2 transport for embedded C
Loading...
Searching...
No Matches
isotp_defines.h
Go to the documentation of this file.
1#ifndef ISOTPC_USER_DEFINITIONS_H
2#define ISOTPC_USER_DEFINITIONS_H
3
8
9#include <stdint.h>
10
11#include "isotp_config.h"
12
13/**************************************************************
14 * compiler specific defines
15 *************************************************************/
16#ifdef __GNUC__
17 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
18 #define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
19 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
20 #else
21 #error "unsupported byte ordering"
22 #endif
23
24 #define ISOTP_PACKED_STRUCT(content) typedef struct __attribute__((packed)) content
25#endif
26
27/**************************************************************
28 * OS specific defines
29 *************************************************************/
30#ifdef _MSC_VER
31 #define ISOTP_PACKED_STRUCT(content) __pragma(pack(push, 1)) typedef struct content __pragma(pack(pop))
32
33 #define snprintf _snprintf
34
35 #include <windows.h>
36 #define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
37 #define __builtin_bswap8 _byteswap_uint8
38 #define __builtin_bswap16 _byteswap_uint16
39 #define __builtin_bswap32 _byteswap_uint32
40 #define __builtin_bswap64 _byteswap_uint64
41#endif
42
43#define LE32TOH(le) ((uint32_t)(((le) << 24) | (((le) & 0x0000FF00) << 8) | (((le) & 0x00FF0000) >> 8) | ((le) >> 24)))
44
45/**************************************************************
46 * CAN frame length (CAN_DL) defines
47 *************************************************************/
48
52
54#define ISOTP_CAN_DL_CLASSIC 8
55
56#if (ISO_TP_MAX_CAN_FRAME_SIZE != 8) && (ISO_TP_MAX_CAN_FRAME_SIZE != 12) && (ISO_TP_MAX_CAN_FRAME_SIZE != 16) && (ISO_TP_MAX_CAN_FRAME_SIZE != 20) && \
57 (ISO_TP_MAX_CAN_FRAME_SIZE != 24) && (ISO_TP_MAX_CAN_FRAME_SIZE != 32) && (ISO_TP_MAX_CAN_FRAME_SIZE != 48) && (ISO_TP_MAX_CAN_FRAME_SIZE != 64)
58 #error "ISO_TP_MAX_CAN_FRAME_SIZE must be one of 8, 12, 16, 20, 24, 32, 48, 64"
59#endif
60
61#if (ISO_TP_DEFAULT_TX_DL > ISO_TP_MAX_CAN_FRAME_SIZE) || (ISO_TP_DEFAULT_TX_DL < ISOTP_CAN_DL_CLASSIC)
62 #error "ISO_TP_DEFAULT_TX_DL must be at least 8 and must not exceed ISO_TP_MAX_CAN_FRAME_SIZE"
63#endif
64
66#define ISOTP_CAN_FRAME_FLAG_NONE 0x00
68#define ISOTP_CAN_FRAME_FLAG_FD 0x01
70#define ISOTP_CAN_FRAME_FLAG_BRS 0x02
71
78#define ISOTP_SF_MAX_PAYLOAD(can_dl) ((can_dl) > ISOTP_CAN_DL_CLASSIC ? (uint32_t)((can_dl) - 2u) : (uint32_t)((can_dl) - 1u))
79
81
82/**************************************************************
83 * Result codes
84 *************************************************************/
88
90#define ISOTP_RET_OK 0
92#define ISOTP_RET_ERROR -1
94#define ISOTP_RET_INPROGRESS -2
96#define ISOTP_RET_OVERFLOW -3
98#define ISOTP_RET_WRONG_SN -4
100#define ISOTP_RET_NO_DATA -5
102#define ISOTP_RET_TIMEOUT -6
104#define ISOTP_RET_LENGTH -7
106#define ISOTP_RET_NOSPACE -8
107
109
111
112/* return logic true if 'a' is after 'b' */
113#define IsoTpTimeAfter(a, b) ((int32_t)((int32_t)(b) - (int32_t)(a)) < 0)
114
115/* invalid bs */
116#define ISOTP_INVALID_BS 0xFFFF
117
118/* Define the maximum amount of characters allowed in an error message. This fixes code which would otherwise break on Microsoft's dumb platform. */
119#define ISOTP_MAX_ERROR_MSG_SIZE 128
120
122
126
133
140
142
144
145/* can fram defination */
146#if defined(ISOTP_BYTE_ORDER_LITTLE_ENDIAN)
147typedef struct {
148 uint8_t reserve_1 : 4;
149 uint8_t type : 4;
150 uint8_t reserve_2[ISO_TP_MAX_CAN_FRAME_SIZE - 1];
151} IsoTpPciType;
152
153typedef struct {
154 uint8_t SF_DL : 4;
155 uint8_t type : 4;
156 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 1];
157} IsoTpSingleFrame;
158
159typedef struct {
160 uint8_t set_to_zero : 4;
161 uint8_t type : 4;
162 uint8_t SF_DL;
163 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 2];
164} IsoTpSingleFrameEscape;
165
166typedef struct {
167 uint8_t FF_DL_high : 4;
168 uint8_t type : 4;
169 uint8_t FF_DL_low;
170 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 2];
171} IsoTpFirstFrameShort;
172
173ISOTP_PACKED_STRUCT({
174 uint8_t set_to_zero_high : 4;
175 uint8_t type : 4;
176 uint8_t set_to_zero_low;
177 uint32_t FF_DL;
178 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 6];
179} IsoTpFirstFrameLong);
180
181typedef struct {
182 uint8_t SN : 4;
183 uint8_t type : 4;
184 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 1];
185} IsoTpConsecutiveFrame;
186
187typedef struct {
188 uint8_t FS : 4;
189 uint8_t type : 4;
190 uint8_t BS;
191 uint8_t STmin;
192 uint8_t reserve[ISO_TP_MAX_CAN_FRAME_SIZE - 3];
193} IsoTpFlowControl;
194
195#else
196
197typedef struct {
198 uint8_t type : 4;
199 uint8_t reserve_1 : 4;
200 uint8_t reserve_2[ISO_TP_MAX_CAN_FRAME_SIZE - 1];
201} IsoTpPciType;
202
203/*
204 * single frame
205 * +-------------------------+-----+
206 * | byte #0 | ... |
207 * +-------------------------+-----+
208 * | nibble #0 | nibble #1 | ... |
209 * +-------------+-----------+ ... +
210 * | PCIType = 0 | SF_DL | ... |
211 * +-------------+-----------+-----+
212 */
213typedef struct {
214 uint8_t type : 4;
215 uint8_t SF_DL : 4;
216 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 1];
217} IsoTpSingleFrame;
218
219/*
220 * single frame using the SF_DL escape sequence (CAN FD only, CAN_DL > 8)
221 * +-------------------------+-----------------------+-----+
222 * | byte #0 | byte #1 | ... |
223 * +-------------------------+-----------+-----------+-----+
224 * | nibble #0 | nibble #1 | nibble #2 | nibble #3 | ... |
225 * +-------------+-----------+-----------+-----------+-----+
226 * | PCIType = 0 | unused=0 | SF_DL | ... |
227 * +-------------+-----------+-----------------------+-----+
228 */
229typedef struct {
230 uint8_t type : 4;
231 uint8_t set_to_zero : 4;
232 uint8_t SF_DL;
233 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 2];
234} IsoTpSingleFrameEscape;
235
236/*
237 * first frame short
238 * +-------------------------+-----------------------+-----+
239 * | byte #0 | byte #1 | ... |
240 * +-------------------------+-----------+-----------+-----+
241 * | nibble #0 | nibble #1 | nibble #2 | nibble #3 | ... |
242 * +-------------+-----------+-----------+-----------+-----+
243 * | PCIType = 1 | FF_DL | ... |
244 * +-------------+-----------+-----------------------+-----+
245 */
246typedef struct {
247 uint8_t type : 4;
248 uint8_t FF_DL_high : 4;
249 uint8_t FF_DL_low;
250 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 2];
251} IsoTpFirstFrameShort;
252
253/*
254 * first frame long
255 * +-------------------------+-----------------------+---------+---------+---------+---------+
256 * | byte #0 | byte #1 | byte #2 | byte #3 | byte #4 | byte #5 |
257 * +-------------------------+-----------+-----------+---------+---------+---------+---------+
258 * | nibble #0 | nibble #1 | nibble #2 | nibble #3 | ... |
259 * +-------------+-----------+-----------+-----------+---------------------------------------+
260 * | PCIType = 1 | unused=0 | escape sequence = 0 | FF_DL |
261 * +-------------+-----------+-----------------------+---------------------------------------+
262 */
263ISOTP_PACKED_STRUCT({
264 uint8_t type : 4;
265 uint8_t set_to_zero_high : 4;
266 uint8_t set_to_zero_low;
267 uint32_t FF_DL;
268 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 6];
269} IsoTpFirstFrameLong);
270
271/*
272 * consecutive frame
273 * +-------------------------+-----+
274 * | byte #0 | ... |
275 * +-------------------------+-----+
276 * | nibble #0 | nibble #1 | ... |
277 * +-------------+-----------+ ... +
278 * | PCIType = 0 | SN | ... |
279 * +-------------+-----------+-----+
280 */
281typedef struct {
282 uint8_t type : 4;
283 uint8_t SN : 4;
284 uint8_t data[ISO_TP_MAX_CAN_FRAME_SIZE - 1];
285} IsoTpConsecutiveFrame;
286
287/*
288 * flow control frame
289 * +-------------------------+-----------------------+-----------------------+-----+
290 * | byte #0 | byte #1 | byte #2 | ... |
291 * +-------------------------+-----------+-----------+-----------+-----------+-----+
292 * | nibble #0 | nibble #1 | nibble #2 | nibble #3 | nibble #4 | nibble #5 | ... |
293 * +-------------+-----------+-----------+-----------+-----------+-----------+-----+
294 * | PCIType = 1 | FS | BS | STmin | ... |
295 * +-------------+-----------+-----------------------+-----------------------+-----+
296 */
297typedef struct {
298 uint8_t type : 4;
299 uint8_t FS : 4;
300 uint8_t BS;
301 uint8_t STmin;
302 uint8_t reserve[ISO_TP_MAX_CAN_FRAME_SIZE - 3];
303} IsoTpFlowControl;
304
305#endif
306
307typedef struct {
308 uint8_t ptr[ISO_TP_MAX_CAN_FRAME_SIZE];
309} IsoTpDataArray;
310
311typedef struct {
312 union {
313 IsoTpPciType common;
314 IsoTpSingleFrame single_frame;
315 IsoTpSingleFrameEscape single_frame_escape;
316 IsoTpFirstFrameShort first_frame_short;
317 IsoTpFirstFrameLong first_frame_long;
318 IsoTpConsecutiveFrame consecutive_frame;
319 IsoTpFlowControl flow_control;
320 IsoTpDataArray data_array;
321 } as;
322} IsoTpCanMessage;
323
325
326/**************************************************************
327 * Callback types
328 *************************************************************/
329
330#ifdef ISO_TP_TRANSMIT_COMPLETE_CALLBACK
338typedef void (*isotp_tx_done_cb)(void* link, uint32_t tx_size, void* user_arg);
339#endif
340
341#ifdef ISO_TP_RECEIVE_COMPLETE_CALLBACK
350typedef void (*isotp_rx_done_cb)(void* link, const uint8_t* data, uint32_t size, void* user_arg);
351#endif
352
354/* Protocol Control Information (PCI) types. */
355typedef enum {
356 ISOTP_PCI_TYPE_SINGLE = 0x0,
357 ISOTP_PCI_TYPE_FIRST_FRAME = 0x1,
358 TSOTP_PCI_TYPE_CONSECUTIVE_FRAME = 0x2,
359 ISOTP_PCI_TYPE_FLOW_CONTROL_FRAME = 0x3,
360
361 ISOTP_PCI_TYPE_CONSECUTIVE_FRAME = 0x2, // Typo fix; but keep broken value for backwards-compat.
362} IsoTpProtocolControlInformation;
363
364/* Private: Protocol Control Information (PCI) flow control identifiers.
365 */
366typedef enum { PCI_FLOW_STATUS_CONTINUE = 0x0, PCI_FLOW_STATUS_WAIT = 0x1, PCI_FLOW_STATUS_OVERFLOW = 0x2 } IsoTpFlowStatus;
367
369
373#define ISOTP_PROTOCOL_RESULT_OK 0
375#define ISOTP_PROTOCOL_RESULT_TIMEOUT_A -1
377#define ISOTP_PROTOCOL_RESULT_TIMEOUT_BS -2
379#define ISOTP_PROTOCOL_RESULT_TIMEOUT_CR -3
381#define ISOTP_PROTOCOL_RESULT_WRONG_SN -4
383#define ISOTP_PROTOCOL_RESULT_INVALID_FS -5
385#define ISOTP_PROTOCOL_RESULT_UNEXP_PDU -6
387#define ISOTP_PROTOCOL_RESULT_WFT_OVRN -7
389#define ISOTP_PROTOCOL_RESULT_BUFFER_OVFLW -8
391#define ISOTP_PROTOCOL_RESULT_ERROR -9
392
394
395#endif // ISOTPC_USER_DEFINITIONS_H
IsoTpReceiveStatusTypes
IsoTpSendStatusTypes
@ ISOTP_RECEIVE_STATUS_FULL
@ ISOTP_RECEIVE_STATUS_IDLE
@ ISOTP_RECEIVE_STATUS_INPROGRESS
@ ISOTP_SEND_STATUS_ERROR
@ ISOTP_SEND_STATUS_IDLE
@ ISOTP_SEND_STATUS_INPROGRESS
Compile-time transport configuration and defaults.
void(* isotp_rx_done_cb)(void *link, const uint8_t *data, uint32_t size, void *user_arg)
Called after a complete payload has been received successfully.
void(* isotp_tx_done_cb)(void *link, uint32_t tx_size, void *user_arg)
Called after a complete payload has been transmitted successfully.