Don't use debug function directly in 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 #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     IsoTpShims* shims;
43     uint16_t arbitration_id;
44     IsoTpMessageReceivedHandler message_received_callback;
45     IsoTpMessageSentHandler message_sent_callback;
46     IsoTpCanFrameSentHandler can_frame_sent_callback;
47
48     // Private
49     uint16_t timeout_ms;
50     bool frame_padding;
51     uint8_t* receive_buffer;
52     uint16_t received_buffer_size;
53     uint16_t incoming_message_size;
54     bool sending;
55     // TODO timer callback
56 } IsoTpHandler;
57
58 typedef enum {
59     PCI_SINGLE = 0x0,
60     PCI_FIRST_FRAME = 0x1,
61     PCI_CONSECUTIVE_FRAME = 0x2,
62     PCI_FLOW_CONTROL_FRAME = 0x3
63 } IsoTpProtocolControlInformation;
64
65 typedef enum {
66     PCI_FLOW_STATUS_CONTINUE = 0x0,
67     PCI_FLOW_STATUS_WAIT = 0x1,
68     PCI_FLOW_STATUS_OVERFLOW = 0x2
69 } IsoTpFlowStatus;
70
71 IsoTpShims isotp_init_shims(LogShim log,
72         SendCanMessageShim send_can_message,
73         SetTimerShim set_timer);
74
75 IsoTpHandler isotp_init(IsoTpShims* shims,
76         uint16_t arbitration_id,
77         IsoTpMessageReceivedHandler message_received_callback,
78         IsoTpMessageSentHandler message_sent_callback,
79         IsoTpCanFrameSentHandler can_frame_sent_callback);
80
81 /* Public: Change the timeout for waiting on an ISO-TP response frame.
82  *
83  * If this function is not used, the conventional 100ms is used by default.
84  *
85  * handler - the ISO-TP handler to modify.
86  * timeout - the new timeout in milliseconds.
87  */
88 void isotp_set_timeout(IsoTpHandler* handler, uint16_t timeout_ms);
89
90 void isotp_destroy(IsoTpHandler* handler);
91
92 void isotp_message_to_string(const IsoTpMessage* message, char* destination,
93         size_t destination_length);
94
95 #ifdef __cplusplus
96 }
97 #endif
98
99 #endif // __ISOTP_H__