Return completion status when receiving CAN frames.
[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
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 const uint16_t MAX_ISO_TP_MESSAGE_SIZE;
15 const uint16_t MAX_CAN_FRAME_SIZE;
16 const uint8_t ISO_TP_DEFAULT_RESPONSE_TIMEOUT;
17 const bool ISO_TP_DEFAULT_FRAME_PADDING_STATUS;
18
19 typedef struct {
20     const uint16_t arbitration_id;
21     const uint8_t* payload;
22     const uint16_t size;
23 } IsoTpMessage;
24
25 typedef void (*LogShim)(const char* message, ...);
26 typedef bool (*SendCanMessageShim)(const uint16_t arbitration_id,
27         const uint8_t* data, const uint8_t size);
28 typedef bool (*SetTimerShim)(uint16_t time_ms, void (*callback));
29
30 typedef void (*IsoTpMessageReceivedHandler)(const IsoTpMessage* message);
31 typedef void (*IsoTpMessageSentHandler)(const IsoTpMessage* message,
32         const bool success);
33 typedef void (*IsoTpCanFrameSentHandler)(const IsoTpMessage* message);
34
35 typedef struct {
36     LogShim log;
37     SendCanMessageShim send_can_message;
38     SetTimerShim set_timer;
39 } IsoTpShims;
40
41 typedef struct {
42     uint16_t arbitration_id;
43     IsoTpMessageReceivedHandler message_received_callback;
44
45     // Private
46     uint16_t timeout_ms;
47     // timeout_ms: ISO_TP_DEFAULT_RESPONSE_TIMEOUT,
48     bool frame_padding;
49     // frame_padding: ISO_TP_DEFAULT_FRAME_PADDING_STATUS,
50     uint8_t* receive_buffer;
51     uint16_t received_buffer_size;
52     uint16_t incoming_message_size;
53     // TODO timer callback
54 } IsoTpReceiveHandle;
55
56 typedef struct {
57     uint16_t sending_arbitration_id;
58     uint16_t receiving_arbitration_id;
59     IsoTpMessageSentHandler message_sent_callback;
60     IsoTpCanFrameSentHandler can_frame_sent_callback;
61
62     // TODO going to need some state here for multi frame messages
63 } IsoTpSendHandle;
64
65 typedef enum {
66     ISOTP_HANDLE_SENDING,
67     ISOTP_HANDLE_RECEIVING
68 } IsoTpHandleType;
69
70 typedef struct {
71     bool success;
72     bool completed;
73     IsoTpHandleType type;
74     IsoTpReceiveHandle receive_handle;
75     IsoTpSendHandle send_handle;
76 } IsoTpHandle;
77
78
79 typedef enum {
80     PCI_SINGLE = 0x0,
81     PCI_FIRST_FRAME = 0x1,
82     PCI_CONSECUTIVE_FRAME = 0x2,
83     PCI_FLOW_CONTROL_FRAME = 0x3
84 } IsoTpProtocolControlInformation;
85
86 typedef enum {
87     PCI_FLOW_STATUS_CONTINUE = 0x0,
88     PCI_FLOW_STATUS_WAIT = 0x1,
89     PCI_FLOW_STATUS_OVERFLOW = 0x2
90 } IsoTpFlowStatus;
91
92 IsoTpShims isotp_init_shims(LogShim log,
93         SendCanMessageShim send_can_message,
94         SetTimerShim set_timer);
95
96 /* Public:
97  *
98  * Returns true if a complete ISO-TP message was sent or received as of
99  * processing this CAN frame. Check the 'success' and 'completed' flag on the
100  * handle to make sure.
101  */
102 bool isotp_receive_can_frame(IsoTpShims* shims, IsoTpHandle* handle,
103         const uint16_t arbitration_id, const uint8_t data[],
104         const uint8_t size);
105
106 /* Public: Change the timeout for waiting on an ISO-TP response frame.
107  *
108  * If this function is not used, the conventional 100ms is used by default.
109  *
110  * handler - the ISO-TP handler to modify.
111  * timeout - the new timeout in milliseconds.
112  */
113 // void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms);
114
115 // void isotp_destroy(IsoTpHandler* handler);
116
117 void isotp_message_to_string(const IsoTpMessage* message, char* destination,
118         size_t destination_length);
119
120 IsoTpHandle isotp_send(IsoTpShims* shims, const uint16_t arbitration_id,
121         const uint8_t* payload, uint16_t size,
122         IsoTpMessageSentHandler callback);
123
124 IsoTpHandle isotp_receive(IsoTpShims* shims, const uint16_t arbitration_id,
125         IsoTpMessageReceivedHandler callback);
126
127 #ifdef __cplusplus
128 }
129 #endif
130
131 #endif // __ISOTP_H__