Split up functions to rx CAN messages for rx and tx of ISO-TP.
[apps/agl-service-can-low-level.git] / src / isotp / isotp.h
1 #ifndef __ISOTP_H__
2 #define __ISOTP_H__
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7
8 #define CAN_MESSAGE_BYTE_SIZE 8
9 #define MAX_ISO_TP_MESSAGE_SIZE 4096
10 // TODO we want to avoid malloc, and we can't be allocated 4K on the stack for
11 // each IsoTpMessage, so for now we're setting an artificial max message size
12 // here - since we only handle single frame messages, 8 bytes is plenty.
13 #define OUR_MAX_ISO_TP_MESSAGE_SIZE 8
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
20 const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS;
21
22 typedef struct {
23     const uint16_t arbitration_id;
24     uint8_t payload[OUR_MAX_ISO_TP_MESSAGE_SIZE];
25     uint16_t size;
26     bool completed;
27 } IsoTpMessage;
28
29 typedef void (*LogShim)(const char* message, ...);
30 typedef bool (*SendCanMessageShim)(const uint16_t arbitration_id,
31         const uint8_t* data, const uint8_t size);
32 typedef bool (*SetTimerShim)(uint16_t time_ms, void (*callback));
33
34 typedef void (*IsoTpMessageReceivedHandler)(const IsoTpMessage* message);
35 typedef void (*IsoTpMessageSentHandler)(const IsoTpMessage* message,
36         const bool success);
37 typedef void (*IsoTpCanFrameSentHandler)(const IsoTpMessage* message);
38
39 typedef struct {
40     LogShim log;
41     SendCanMessageShim send_can_message;
42     SetTimerShim set_timer;
43 } IsoTpShims;
44
45 typedef struct {
46     bool success;
47     bool completed;
48     uint16_t arbitration_id;
49     IsoTpMessageReceivedHandler message_received_callback;
50
51     // Private
52     uint16_t timeout_ms;
53     // timeout_ms: ISO_TP_DEFAULT_RESPONSE_TIMEOUT,
54     bool frame_padding;
55     // frame_padding: ISO_TP_DEFAULT_FRAME_PADDING_STATUS,
56     uint8_t* receive_buffer;
57     uint16_t received_buffer_size;
58     uint16_t incoming_message_size;
59     // TODO timer callback for multi frame
60 } IsoTpReceiveHandle;
61
62 typedef struct {
63     bool success;
64     bool completed;
65     uint16_t sending_arbitration_id;
66     uint16_t receiving_arbitration_id;
67     IsoTpMessageSentHandler message_sent_callback;
68     IsoTpCanFrameSentHandler can_frame_sent_callback;
69     // TODO going to need some state here for multi frame messages
70 } IsoTpSendHandle;
71
72 typedef enum {
73     ISOTP_HANDLE_SENDING,
74     ISOTP_HANDLE_RECEIVING
75 } IsoTpHandleType;
76
77 typedef enum {
78     PCI_SINGLE = 0x0,
79     PCI_FIRST_FRAME = 0x1,
80     PCI_CONSECUTIVE_FRAME = 0x2,
81     PCI_FLOW_CONTROL_FRAME = 0x3
82 } IsoTpProtocolControlInformation;
83
84 typedef enum {
85     PCI_FLOW_STATUS_CONTINUE = 0x0,
86     PCI_FLOW_STATUS_WAIT = 0x1,
87     PCI_FLOW_STATUS_OVERFLOW = 0x2
88 } IsoTpFlowStatus;
89
90 IsoTpShims isotp_init_shims(LogShim log,
91         SendCanMessageShim send_can_message,
92         SetTimerShim set_timer);
93
94 IsoTpMessage isotp_continue_receive(IsoTpShims* shims,
95         IsoTpReceiveHandle* handle, const uint16_t arbitration_id,
96         const uint8_t data[], const uint8_t size);
97
98 bool isotp_continue_send(IsoTpShims* shims, IsoTpSendHandle* handle,
99         const uint16_t arbitration_id, const uint8_t data[],
100         const uint8_t size);
101
102 /* Public: Change the timeout for waiting on an ISO-TP response frame.
103  *
104  * If this function is not used, the conventional 100ms is used by default.
105  *
106  * handler - the ISO-TP handler to modify.
107  * timeout - the new timeout in milliseconds.
108  */
109 // void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms);
110
111 void isotp_message_to_string(const IsoTpMessage* message, char* destination,
112         size_t destination_length);
113
114 IsoTpSendHandle isotp_send(IsoTpShims* shims, const uint16_t arbitration_id,
115         const uint8_t payload[], uint16_t size,
116         IsoTpMessageSentHandler callback);
117
118 IsoTpReceiveHandle isotp_receive(IsoTpShims* shims,
119         const uint16_t arbitration_id, IsoTpMessageReceivedHandler callback);
120
121 #ifdef __cplusplus
122 }
123 #endif
124
125 #endif // __ISOTP_H__