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