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