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