9d05980aeaab8c28508ffde744751f39ccaaaf1f
[apps/agl-service-can-low-level.git] / src / isotp / isotp_types.h
1 #ifndef __ISOTP_TYPES__
2 #define __ISOTP_TYPES__
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7
8 #define CAN_MESSAGE_BYTE_SIZE 8
9 #define MAX_ISO_TP_MESSAGE_SIZE 4096
10 // TODO we want to avoid malloc, and we can't be allocated 4K on the stack for
11 // each IsoTpMessage, so for now we're setting an artificial max message size
12 // here - since we only handle single frame messages, 8 bytes is plenty.
13 #define OUR_MAX_ISO_TP_MESSAGE_SIZE 8
14
15 /* Private: The default timeout to use when waiting for a response during a
16  * multi-frame send or receive.
17  */
18 #define ISO_TP_DEFAULT_RESPONSE_TIMEOUT 100
19
20 /* Private: Determines if by default, padding is added to ISO-TP message frames.
21  */
22 #define ISO_TP_DEFAULT_FRAME_PADDING_STATUS true
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 /* Public: A container for a sent or received ISO-TP message.
29  *
30  * completed - An IsoTpMessage is the return value from a few functions - this
31  *      attribute will be true if the message is actually completely received.
32  *      If the function returns but is only partially through receiving the
33  *      message, this will be false and you should not consider the other data
34  *      to be valid.
35  * arbitration_id - The arbitration ID of the message.
36  * payload - The optional payload of the message - don't forget to check the
37  *      size!
38  * size -  The size of the payload. The size will be 0 if there is no payload.
39  */
40 typedef struct {
41     const uint32_t arbitration_id;
42     uint8_t payload[OUR_MAX_ISO_TP_MESSAGE_SIZE];
43     uint16_t size;
44     bool completed;
45 } IsoTpMessage;
46
47 /* Public: The type signature for an optional logging function, if the user
48  * wishes to provide one. It should print, store or otherwise display the
49  * message.
50  *
51  * message - A format string to log using the given parameters.
52  * ... (vargs) - the parameters for the format string.
53  */
54 typedef void (*LogShim)(const char* message, ...);
55 /* Public: The type signature for a function to send a single CAN message.
56  *
57  * arbitration_id - The arbitration ID of the message.
58  * data - The data payload for the message. NULL is valid if size is also 0.
59  * size - The size of the data payload, in bytes.
60  *
61  * Returns true if the CAN message was sent successfully.
62  */
63 typedef bool (*SendCanMessageShim)(const uint32_t arbitration_id,
64         const uint8_t* data, const uint8_t size);
65
66 /* Public: The type signature for a... TODO, not used yet.
67  */
68 typedef bool (*SetTimerShim)(uint16_t time_ms, void (*callback));
69
70 /* Public: The signature for a function to be called when an ISO-TP message has
71  * been completely received.
72  *
73  * message - The received message.
74  */
75 typedef void (*IsoTpMessageReceivedHandler)(const IsoTpMessage* message);
76
77 /* Public: the signature for a function to be called when an ISO-TP message has
78  * been completely sent, or had a fatal error during sending.
79  *
80  * message - The sent message.
81  * success - True if the message was sent successfully.
82  */
83 typedef void (*IsoTpMessageSentHandler)(const IsoTpMessage* message,
84         const bool success);
85
86 /* Public: The signature for a function to be called when a CAN frame has been
87  * sent as as part of sending or receive an ISO-TP message.
88  *
89  * This is really only useful for debugging the library itself.
90  *
91  * message - The ISO-TP message that generated this CAN frame.
92  */
93 typedef void (*IsoTpCanFrameSentHandler)(const IsoTpMessage* message);
94
95 /* Public: A container for the 3 shim functions used by the library to interact
96  * with the wider system.
97  *
98  * Use the isotp_init_shims(...) function to create an instance of this struct.
99  *
100  * By default, all CAN frames sent from this device in the process of an ISO-TP
101  * message are padded out to a complete 8 byte frame. This is often required by
102  * ECUs. To disable this feature, change the 'frame_padding' field to false on
103  * the IsoTpShims object returned from isotp_init_shims(...).
104  *
105  * frame_padding - true if outgoing CAN frames should be padded to a full 8
106  *      bytes.
107  */
108 typedef struct {
109     LogShim log;
110     SendCanMessageShim send_can_message;
111     SetTimerShim set_timer;
112     bool frame_padding;
113 } IsoTpShims;
114
115 /* Private: PCI types, for identifying each frame of an ISO-TP message.
116  */
117 typedef enum {
118     PCI_SINGLE = 0x0,
119     PCI_FIRST_FRAME = 0x1,
120     PCI_CONSECUTIVE_FRAME = 0x2,
121     PCI_FLOW_CONTROL_FRAME = 0x3
122 } IsoTpProtocolControlInformation;
123
124 /* Private: PCI flow control identifiers.
125  */
126 typedef enum {
127     PCI_FLOW_STATUS_CONTINUE = 0x0,
128     PCI_FLOW_STATUS_WAIT = 0x1,
129     PCI_FLOW_STATUS_OVERFLOW = 0x2
130 } IsoTpFlowStatus;
131
132 #ifdef __cplusplus
133 }
134 #endif
135
136 #endif // __ISOTP_TYPES__