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