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