ISOTP-C
A platform-agnostic ISOTP implementation in C.
isotp.h
Go to the documentation of this file.
1#ifndef __ISOTP_H__
2#define __ISOTP_H__
3
4#include <stdio.h>
5#include <string.h>
6
7#ifdef __cplusplus
8#include <stdint.h>
9
10extern "C" {
11#endif
12
13#include "isotp_defines.h"
14#include "isotp_config.h"
15#include "isotp_user.h"
16
17/**
18 * @brief Struct containing the data for linking an application to a CAN instance.
19 * The data stored in this struct is used internally and may be used by software programs
20 * using this library.
21 */
22typedef struct IsoTpLink {
23 /* sender paramters */
24 uint32_t send_arbitration_id; /* used to reply consecutive frame */
25 /* message buffer */
26 uint8_t* send_buffer;
27 uint16_t send_buf_size;
28 uint16_t send_size;
29 uint16_t send_offset;
30 /* multi-frame flags */
31 uint8_t send_sn;
32 uint16_t send_bs_remain; /* Remaining block size */
33 uint32_t send_st_min_us; /* Separation Time between consecutive frames */
34 uint8_t send_wtf_count; /* Maximum number of FC.Wait frame transmissions */
35 uint32_t send_timer_st; /* Last time send consecutive frame */
36 uint32_t send_timer_bs; /* Time until reception of the next FlowControl N_PDU
37 start at sending FF, CF, receive FC
38 end at receive FC */
40 uint8_t send_status;
41 /* receiver paramters */
43 /* message buffer */
46 uint16_t receive_size;
48 /* multi-frame control */
49 uint8_t receive_sn;
50 uint8_t receive_bs_count; /* Maximum number of FC.Wait frame transmissions */
51 uint32_t receive_timer_cr; /* Time until transmission of the next ConsecutiveFrame N_PDU
52 start at sending FC, receive CF
53 end at receive FC */
56} IsoTpLink;
57
58/**
59 * @brief Initialises the ISO-TP library.
60 *
61 * @param link The @code IsoTpLink @endcode instance used for transceiving data.
62 * @param sendid The ID used to send data to other CAN nodes.
63 * @param sendbuf A pointer to an area in memory which can be used as a buffer for data to be sent.
64 * @param sendbufsize The size of the buffer area.
65 * @param recvbuf A pointer to an area in memory which can be used as a buffer for data to be received.
66 * @param recvbufsize The size of the buffer area.
67 */
68void isotp_init_link(IsoTpLink *link, uint32_t sendid,
69 uint8_t *sendbuf, uint16_t sendbufsize,
70 uint8_t *recvbuf, uint16_t recvbufsize);
71
72/**
73 * @brief Polling function; call this function periodically to handle timeouts, send consecutive frames, etc.
74 *
75 * @param link The @code IsoTpLink @endcode instance used.
76 */
77void isotp_poll(IsoTpLink *link);
78
79/**
80 * @brief Handles incoming CAN messages.
81 * Determines whether an incoming message is a valid ISO-TP frame or not and handles it accordingly.
82 *
83 * @param link The @code IsoTpLink @endcode instance used for transceiving data.
84 * @param data The data received via CAN.
85 * @param len The length of the data received.
86 */
87void isotp_on_can_message(IsoTpLink *link, uint8_t *data, uint8_t len);
88
89/**
90 * @brief Sends ISO-TP frames via CAN, using the ID set in the initialising function.
91 *
92 * Single-frame messages will be sent immediately when calling this function.
93 * Multi-frame messages will be sent consecutively when calling isotp_poll.
94 *
95 * @param link The @code IsoTpLink @endcode instance used for transceiving data.
96 * @param payload The payload to be sent. (Up to 4095 bytes).
97 * @param size The size of the payload to be sent.
98 *
99 * @return Possible return values:
100 * - @code ISOTP_RET_OVERFLOW @endcode
101 * - @code ISOTP_RET_INPROGRESS @endcode
102 * - @code ISOTP_RET_OK @endcode
103 * - The return value of the user shim function isotp_user_send_can().
104 */
105int isotp_send(IsoTpLink *link, const uint8_t payload[], uint16_t size);
106
107/**
108 * @brief See @link isotp_send @endlink, with the exception that this function is used only for functional addressing.
109 */
110int isotp_send_with_id(IsoTpLink *link, uint32_t id, const uint8_t payload[], uint16_t size);
111
112/**
113 * @brief Receives and parses the received data and copies the parsed data in to the internal buffer.
114 * @param link The @link IsoTpLink @endlink instance used to transceive data.
115 * @param payload A pointer to an area in memory where the raw data is copied from.
116 * @param payload_size The size of the received (raw) CAN data.
117 * @param out_size A reference to a variable which will contain the size of the actual (parsed) data.
118 *
119 * @return Possible return values:
120 * - @link ISOTP_RET_OK @endlink
121 * - @link ISOTP_RET_NO_DATA @endlink
122 */
123int isotp_receive(IsoTpLink *link, uint8_t *payload, const uint16_t payload_size, uint16_t *out_size);
124
125#ifdef __cplusplus
126}
127#endif
128
129#endif // __ISOTP_H__
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]