Move a few things about to make compiling with other projects possible.
[apps/low-level-can-service.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 uint16_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 uint16_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 typedef struct {
101     LogShim log;
102     SendCanMessageShim send_can_message;
103     SetTimerShim set_timer;
104 } IsoTpShims;
105
106 /* Private: PCI types, for identifying each frame of an ISO-TP message.
107  */
108 typedef enum {
109     PCI_SINGLE = 0x0,
110     PCI_FIRST_FRAME = 0x1,
111     PCI_CONSECUTIVE_FRAME = 0x2,
112     PCI_FLOW_CONTROL_FRAME = 0x3
113 } IsoTpProtocolControlInformation;
114
115 /* Private: PCI flow control identifiers.
116  */
117 typedef enum {
118     PCI_FLOW_STATUS_CONTINUE = 0x0,
119     PCI_FLOW_STATUS_WAIT = 0x1,
120     PCI_FLOW_STATUS_OVERFLOW = 0x2
121 } IsoTpFlowStatus;
122
123 #ifdef __cplusplus
124 }
125 #endif
126
127 #endif // __ISOTP_TYPES__