Draft progress implementing single frame rx.
[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
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 const uint16_t MAX_ISO_TP_MESSAGE_SIZE;
12 const uint16_t MAX_CAN_FRAME_SIZE;
13 const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
14 const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS;
15
16 typedef void (*LogShim)(const char* message);
17 typedef bool (*SendCanMessageShim)(const uint16_t arbitration_id, const uint8_t* data,
18             const uint8_t size);
19 typedef bool (*SetTimerShim)(uint16_t time_ms, void (*callback));
20
21 typedef void (*IsoTpMessageReceivedHandler)(const uint16_t arbitration_id,
22         const uint8_t* payload, const uint16_t size);
23 typedef void (*IsoTpMessageSentHandler)(const bool success,
24         const uint16_t arbitration_id, const uint8_t* payload,
25         const uint16_t size);
26 typedef void (*IsoTpCanFrameSentHandler)(const uint16_t arbitration_id,
27         const uint8_t* payload, const uint8_t size);
28
29 // TODO this is really necessary and it would leak this library into the user's
30 // code
31 // typedef struct {
32     // const uint16_t arbitration_id;
33     // const uint8_t* payload;
34     // const uint16_t size;
35 // } IsoTpMessage;
36
37 typedef struct {
38     LogShim log;
39     SendCanMessageShim send_can_message;
40     SetTimerShim set_timer;
41 } IsoTpShims;
42
43 typedef struct {
44     IsoTpShims* shims;
45     uint16_t arbitration_id;
46     IsoTpMessageReceivedHandler message_received_callback;
47     IsoTpMessageSentHandler message_sent_callback;
48     IsoTpCanFrameSentHandler can_frame_sent_callback;
49
50     // Private
51     uint16_t timeout_ms;
52     bool frame_padding;
53     uint8_t* receive_buffer;
54     uint16_t received_buffer_size;
55     uint16_t incoming_message_size;
56     bool sending;
57     // TODO timer callback
58 } IsoTpHandler;
59
60 typedef enum {
61     PCI_SINGLE = 0x0,
62     PCI_FIRST_FRAME = 0x1,
63     PCI_CONSECUTIVE_FRAME = 0x2,
64     PCI_FLOW_CONTROL_FRAME = 0x3
65 } IsoTpProtocolControlInformation;
66
67 typedef enum {
68     PCI_FLOW_STATUS_CONTINUE = 0x0,
69     PCI_FLOW_STATUS_WAIT = 0x1,
70     PCI_FLOW_STATUS_OVERFLOW = 0x2
71 } IsoTpFlowStatus;
72
73 IsoTpShims isotp_init_shims(LogShim log,
74         SendCanMessageShim send_can_message,
75         SetTimerShim set_timer);
76
77 IsoTpHandler isotp_init(IsoTpShims* shims,
78         uint16_t arbitration_id,
79         IsoTpMessageReceivedHandler message_received_callback,
80         IsoTpMessageSentHandler message_sent_callback,
81         IsoTpCanFrameSentHandler can_frame_sent_callback);
82
83 /* Public: Change the timeout for waiting on an ISO-TP response frame.
84  *
85  * If this function is not used, the conventional 100ms is used by default.
86  *
87  * handler - the ISO-TP handler to modify.
88  * timeout - the new timeout in milliseconds.
89  */
90 void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms);
91
92 // TODO we have to make sure to copy the payload internall if it's more than 1
93 // frame, the soure could go out of scope
94 bool isotp_send(const uint8_t* payload, uint16_t payload_size);
95
96 void isotp_receive_can_frame(IsoTpHandler* handler,
97         const uint16_t arbitration_id, const uint8_t* data,
98         const uint8_t length);
99
100 void isotp_destroy(IsoTpHandler* handler);
101
102 void log_isotp_message(const uint16_t arbitration_id, const uint8_t* payload,
103         const uint16_t size);
104
105
106 #ifdef __cplusplus
107 }
108 #endif
109
110 #endif // __ISOTP_H__