ISOTP-C
A platform-agnostic ISOTP implementation in C.
isotp_defines.h
Go to the documentation of this file.
1#ifndef __ISOTP_DEFINES_H__
2#define __ISOTP_DEFINES_H__
3
4#include <stdint.h>
5
6/**************************************************************
7 * compiler specific defines
8 *************************************************************/
9#ifdef __GNUC__
10#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
11#define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
12#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
13#else
14#error "unsupported byte ordering"
15#endif
16#endif
17
18/**************************************************************
19 * OS specific defines
20 *************************************************************/
21#ifdef _WIN32
22#define snprintf _snprintf
23#endif
24
25#ifdef _WIN32
26#include <windows.h>
27#define ISOTP_BYTE_ORDER_LITTLE_ENDIAN
28#define __builtin_bswap8 _byteswap_uint8
29#define __builtin_bswap16 _byteswap_uint16
30#define __builtin_bswap32 _byteswap_uint32
31#define __builtin_bswap64 _byteswap_uint64
32#endif
33
34/**************************************************************
35 * internal used defines
36 *************************************************************/
37#define ISOTP_RET_OK 0
38#define ISOTP_RET_ERROR -1
39#define ISOTP_RET_INPROGRESS -2
40#define ISOTP_RET_OVERFLOW -3
41#define ISOTP_RET_WRONG_SN -4
42#define ISOTP_RET_NO_DATA -5
43#define ISOTP_RET_TIMEOUT -6
44#define ISOTP_RET_LENGTH -7
45#define ISOTP_RET_NOSPACE -8
46
47/* return logic true if 'a' is after 'b' */
48#define IsoTpTimeAfter(a,b) ((int32_t)((int32_t)(b) - (int32_t)(a)) < 0)
49
50/* invalid bs */
51#define ISOTP_INVALID_BS 0xFFFF
52
53/* ISOTP sender status */
54typedef enum {
58} IsoTpSendStatusTypes;
59
60/* ISOTP receiver status */
61typedef enum {
65} IsoTpReceiveStatusTypes;
66
67/* can fram defination */
68#if defined(ISOTP_BYTE_ORDER_LITTLE_ENDIAN)
69typedef struct {
70 uint8_t reserve_1:4;
71 uint8_t type:4;
72 uint8_t reserve_2[7];
73} IsoTpPciType;
74
75typedef struct {
76 uint8_t SF_DL:4;
77 uint8_t type:4;
78 uint8_t data[7];
79} IsoTpSingleFrame;
80
81typedef struct {
82 uint8_t FF_DL_high:4;
83 uint8_t type:4;
84 uint8_t FF_DL_low;
85 uint8_t data[6];
86} IsoTpFirstFrame;
87
88typedef struct {
89 uint8_t SN:4;
90 uint8_t type:4;
91 uint8_t data[7];
92} IsoTpConsecutiveFrame;
93
94typedef struct {
95 uint8_t FS:4;
96 uint8_t type:4;
97 uint8_t BS;
98 uint8_t STmin;
99 uint8_t reserve[5];
100} IsoTpFlowControl;
101
102#else
103
104typedef struct {
105 uint8_t type:4;
106 uint8_t reserve_1:4;
107 uint8_t reserve_2[7];
108} IsoTpPciType;
109
110/*
111* single frame
112* +-------------------------+-----+
113* | byte #0 | ... |
114* +-------------------------+-----+
115* | nibble #0 | nibble #1 | ... |
116* +-------------+-----------+ ... +
117* | PCIType = 0 | SF_DL | ... |
118* +-------------+-----------+-----+
119*/
120typedef struct {
121 uint8_t type:4;
122 uint8_t SF_DL:4;
123 uint8_t data[7];
124} IsoTpSingleFrame;
125
126/*
127* first frame
128* +-------------------------+-----------------------+-----+
129* | byte #0 | byte #1 | ... |
130* +-------------------------+-----------+-----------+-----+
131* | nibble #0 | nibble #1 | nibble #2 | nibble #3 | ... |
132* +-------------+-----------+-----------+-----------+-----+
133* | PCIType = 1 | FF_DL | ... |
134* +-------------+-----------+-----------------------+-----+
135*/
136typedef struct {
137 uint8_t type:4;
138 uint8_t FF_DL_high:4;
139 uint8_t FF_DL_low;
140 uint8_t data[6];
141} IsoTpFirstFrame;
142
143/*
144* consecutive frame
145* +-------------------------+-----+
146* | byte #0 | ... |
147* +-------------------------+-----+
148* | nibble #0 | nibble #1 | ... |
149* +-------------+-----------+ ... +
150* | PCIType = 0 | SN | ... |
151* +-------------+-----------+-----+
152*/
153typedef struct {
154 uint8_t type:4;
155 uint8_t SN:4;
156 uint8_t data[7];
157} IsoTpConsecutiveFrame;
158
159/*
160* flow control frame
161* +-------------------------+-----------------------+-----------------------+-----+
162* | byte #0 | byte #1 | byte #2 | ... |
163* +-------------------------+-----------+-----------+-----------+-----------+-----+
164* | nibble #0 | nibble #1 | nibble #2 | nibble #3 | nibble #4 | nibble #5 | ... |
165* +-------------+-----------+-----------+-----------+-----------+-----------+-----+
166* | PCIType = 1 | FS | BS | STmin | ... |
167* +-------------+-----------+-----------------------+-----------------------+-----+
168*/
169typedef struct {
170 uint8_t type:4;
171 uint8_t FS:4;
172 uint8_t BS;
173 uint8_t STmin;
174 uint8_t reserve[5];
175} IsoTpFlowControl;
176
177#endif
178
179typedef struct {
180 uint8_t ptr[8];
181} IsoTpDataArray;
182
183typedef struct {
184 union {
185 IsoTpPciType common;
186 IsoTpSingleFrame single_frame;
187 IsoTpFirstFrame first_frame;
188 IsoTpConsecutiveFrame consecutive_frame;
189 IsoTpFlowControl flow_control;
190 IsoTpDataArray data_array;
191 } as;
192} IsoTpCanMessage;
193
194/**************************************************************
195 * protocol specific defines
196 *************************************************************/
197
198/* Private: Protocol Control Information (PCI) types, for identifying each frame of an ISO-TP message.
199 */
200typedef enum {
205} IsoTpProtocolControlInformation;
206
207/* Private: Protocol Control Information (PCI) flow control identifiers.
208 */
209typedef enum {
213} IsoTpFlowStatus;
214
215/* Private: network layer resault code.
216 */
217#define ISOTP_PROTOCOL_RESULT_OK 0
218#define ISOTP_PROTOCOL_RESULT_TIMEOUT_A -1
219#define ISOTP_PROTOCOL_RESULT_TIMEOUT_BS -2
220#define ISOTP_PROTOCOL_RESULT_TIMEOUT_CR -3
221#define ISOTP_PROTOCOL_RESULT_WRONG_SN -4
222#define ISOTP_PROTOCOL_RESULT_INVALID_FS -5
223#define ISOTP_PROTOCOL_RESULT_UNEXP_PDU -6
224#define ISOTP_PROTOCOL_RESULT_WFT_OVRN -7
225#define ISOTP_PROTOCOL_RESULT_BUFFER_OVFLW -8
226#define ISOTP_PROTOCOL_RESULT_ERROR -9
227
228#endif
void isotp_on_can_message(IsoTpLink *link, uint8_t *data, uint8_t len)
Handles incoming CAN messages. Determines whether an incoming message is a valid ISO-TP frame or not ...
Definition: isotp.c:281
static int isotp_send_single_frame(IsoTpLink *link, uint32_t id)
Definition: isotp.c:53
static int isotp_send_consecutive_frame(IsoTpLink *link)
Definition: isotp.c:103
static int isotp_receive_first_frame(IsoTpLink *link, IsoTpCanMessage *message, uint8_t len)
Definition: isotp.c:154
int isotp_receive(IsoTpLink *link, uint8_t *payload, const uint16_t payload_size, uint16_t *out_size)
Receives and parses the received data and copies the parsed data in to the internal buffer.
Definition: isotp.c:430
static int isotp_receive_consecutive_frame(IsoTpLink *link, IsoTpCanMessage *message, uint8_t len)
Definition: isotp.c:186
static int isotp_receive_flow_control_frame(IsoTpLink *link, IsoTpCanMessage *message, uint8_t len)
Definition: isotp.c:215
static int isotp_send_flow_control(IsoTpLink *link, uint8_t flow_status, uint8_t block_size, uint32_t st_min_us)
Definition: isotp.c:29
void isotp_init_link(IsoTpLink *link, uint32_t sendid, uint8_t *sendbuf, uint16_t sendbufsize, uint8_t *recvbuf, uint16_t recvbufsize)
Initialises the ISO-TP library.
Definition: isotp.c:450
void isotp_poll(IsoTpLink *link)
Polling function; call this function periodically to handle timeouts, send consecutive frames,...
Definition: isotp.c:463
static uint8_t isotp_us_to_st_min(uint32_t us)
STATIC FUNCTIONS ///.
Definition: isotp.c:10
int isotp_send_with_id(IsoTpLink *link, uint32_t id, const uint8_t payload[], uint16_t size)
See isotp_send, with the exception that this function is used only for functional addressing.
Definition: isotp.c:233
static uint32_t isotp_st_min_to_us(uint8_t st_min)
Definition: isotp.c:20
static int isotp_send_first_frame(IsoTpLink *link, uint32_t id)
Definition: isotp.c:79
int isotp_send(IsoTpLink *link, const uint8_t payload[], uint16_t size)
PUBLIC FUNCTIONS ///.
Definition: isotp.c:229
static int isotp_receive_single_frame(IsoTpLink *link, IsoTpCanMessage *message, uint8_t len)
Definition: isotp.c:140
#define ISO_TP_MAX_WFT_NUMBER
Definition: isotp_config.h:17
#define ISO_TP_DEFAULT_ST_MIN_US
Definition: isotp_config.h:12
#define ISO_TP_DEFAULT_RESPONSE_TIMEOUT_US
Definition: isotp_config.h:22
#define ISO_TP_DEFAULT_BLOCK_SIZE
Definition: isotp_config.h:7
@ ISOTP_RECEIVE_STATUS_FULL
Definition: isotp_defines.h:64
@ ISOTP_RECEIVE_STATUS_IDLE
Definition: isotp_defines.h:62
@ ISOTP_RECEIVE_STATUS_INPROGRESS
Definition: isotp_defines.h:63
#define ISOTP_RET_NOSPACE
Definition: isotp_defines.h:45
#define ISOTP_PROTOCOL_RESULT_BUFFER_OVFLW
#define ISOTP_PROTOCOL_RESULT_UNEXP_PDU
#define ISOTP_INVALID_BS
Definition: isotp_defines.h:51
#define ISOTP_RET_NO_DATA
Definition: isotp_defines.h:42
#define ISOTP_PROTOCOL_RESULT_OK
#define ISOTP_RET_OVERFLOW
Definition: isotp_defines.h:40
#define ISOTP_PROTOCOL_RESULT_TIMEOUT_CR
@ ISOTP_SEND_STATUS_ERROR
Definition: isotp_defines.h:57
@ ISOTP_SEND_STATUS_IDLE
Definition: isotp_defines.h:55
@ ISOTP_SEND_STATUS_INPROGRESS
Definition: isotp_defines.h:56
#define ISOTP_PROTOCOL_RESULT_TIMEOUT_BS
#define ISOTP_RET_WRONG_SN
Definition: isotp_defines.h:41
#define ISOTP_PROTOCOL_RESULT_WFT_OVRN
#define ISOTP_RET_LENGTH
Definition: isotp_defines.h:44
#define ISOTP_RET_INPROGRESS
Definition: isotp_defines.h:39
@ PCI_FLOW_STATUS_OVERFLOW
@ PCI_FLOW_STATUS_WAIT
@ PCI_FLOW_STATUS_CONTINUE
#define ISOTP_RET_ERROR
Definition: isotp_defines.h:38
#define ISOTP_RET_OK
Definition: isotp_defines.h:37
@ TSOTP_PCI_TYPE_CONSECUTIVE_FRAME
@ ISOTP_PCI_TYPE_SINGLE
@ ISOTP_PCI_TYPE_FIRST_FRAME
@ ISOTP_PCI_TYPE_FLOW_CONTROL_FRAME
#define IsoTpTimeAfter(a, b)
Definition: isotp_defines.h:48
#define ISOTP_PROTOCOL_RESULT_WRONG_SN
void isotp_user_debug(const char *message,...)
user implemented, print debug message
int isotp_user_send_can(const uint32_t arbitration_id, const uint8_t *data, const uint8_t size)
user implemented, send can message. should return ISOTP_RET_OK when success.
uint32_t isotp_user_get_us(void)
user implemented, gets the amount of time passed since the last call in microseconds
IsoTpDataArray data_array
IsoTpFirstFrame first_frame
IsoTpPciType common
IsoTpConsecutiveFrame consecutive_frame
IsoTpSingleFrame single_frame
IsoTpFlowControl flow_control
uint8_t ptr[8]