68d6b6f562fc1c2a904fe4ff77ee68d0599c5078
[apps/agl-service-can-low-level.git] / src / uds / uds_types.h
1 #ifndef __UDS_TYPES_H__
2 #define __UDS_TYPES_H__
3
4 #include <isotp/isotp.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 // TODO This isn't true for multi frame messages - we may need to dynamically
13 // allocate this in the future
14 #define MAX_UDS_PAYLOAD_LENGTH 7
15 #define MAX_RESPONDING_ECU_COUNT 8
16 #define VIN_LENGTH 17
17
18 /* Private: The four main types of diagnositc requests that determine how the
19  * request should be parsed and what type of callback should be used.
20  *
21  * TODO this may not be used...yet?
22  */
23 typedef enum {
24     DIAGNOSTIC_REQUEST_TYPE_PID,
25     DIAGNOSTIC_REQUEST_TYPE_DTC,
26     DIAGNOSTIC_REQUEST_TYPE_MIL_STATUS,
27     DIAGNOSTIC_REQUEST_TYPE_VIN
28 } DiagnosticRequestType;
29
30 /* Public: A container for a single diagnostic request.
31  *
32  * The only required fields are the arbitration_id and mode.
33  *
34  * arbitration_id - The arbitration ID to send the request.
35  * mode - The OBD-II mode for the request.
36  * has_pid - (optional) If the requests uses a PID, this should be true.
37  * pid - (optional) The PID to request, if the mode requires one. has_pid must
38  *      be true.
39  * pid_length - The length of the PID field, either 1 (standard) or 2 bytes
40  *      (extended). If 0, it will be set automatically based on the request
41  *      mode.
42  * payload - (optional) The payload for the request, if the request requires
43  *      one. If payload_length is 0 this field is ignored.
44  * payload_length - The length of the payload, or 0 if no payload is used.
45  * no_frame_padding - false if sent CAN payloads should *not* be padded out to a
46  *      full 8 byte CAN frame. Many ECUs require this, but others require the
47  *      size of the CAN message to only be the actual data. By default padding
48  *      is enabled (so this struct value can default to 0).
49  * type - the type of the request (TODO unused)
50  */
51 typedef struct {
52     uint32_t arbitration_id;
53     uint8_t mode;
54     bool has_pid;
55     uint16_t pid;
56     uint8_t pid_length;
57     uint8_t payload[MAX_UDS_PAYLOAD_LENGTH];
58     uint8_t payload_length;
59     bool no_frame_padding;
60     DiagnosticRequestType type;
61 } DiagnosticRequest;
62
63 /* Public: All possible negative response codes that could be received from a
64  * requested node.
65  *
66  * When a DiagnosticResponse is received and the 'completed' field is true, but
67  * the 'success' field is false, the 'negative_response_code' field will contain
68  * one of these values as reported by the requested node.
69  *
70  * Thanks to canbushack.com for the list of NRCs.
71  */
72 typedef enum {
73     NRC_SUCCESS = 0x0,
74     NRC_SERVICE_NOT_SUPPORTED = 0x11,
75     NRC_SUB_FUNCTION_NOT_SUPPORTED = 0x12,
76     NRC_INCORRECT_LENGTH_OR_FORMAT = 0x13,
77     NRC_CONDITIONS_NOT_CORRECT = 0x22,
78     NRC_REQUEST_OUT_OF_RANGE = 0x31,
79     NRC_SECURITY_ACCESS_DENIED = 0x33,
80     NRC_INVALID_KEY = 0x35,
81     NRC_TOO_MANY_ATTEMPS = 0x36,
82     NRC_TIME_DELAY_NOT_EXPIRED = 0x37,
83     NRC_RESPONSE_PENDING = 0x78
84 } DiagnosticNegativeResponseCode;
85
86 /* Public: A partially or fully completed response to a diagnostic request.
87  *
88  * completed - True if the request is complete - some functions return a
89  *      DiagnosticResponse even when it's only partially completed, so be sure
90  *      to check this field.
91  * success - True if the request was successful. The value if this
92  *      field isn't valid if 'completed' isn't true. If this is 'false', check
93  *      the negative_response_code field for the reason.
94  * arbitration_id - The arbitration ID the response was received on.
95  * mode - The OBD-II mode for the original request.
96  * has_pid - If this is a response to a PID request, this will be true and the
97  *      'pid' field will be valid.
98  * pid - If the request was for a PID, this is the PID echo. Only valid if
99  *      'has_pid' is true.
100  * negative_response_code - If the request was not successful, 'success' will be
101  *      false and this will be set to a DiagnosticNegativeResponseCode returned
102  *      by the other node.
103  * payload - An optional payload for the response - NULL if no payload.
104  * payload_length - The length of the payload or 0 if none.
105  */
106 typedef struct {
107     bool completed;
108     bool success;
109     uint32_t arbitration_id;
110     uint8_t mode;
111     bool has_pid;
112     uint16_t pid;
113     DiagnosticNegativeResponseCode negative_response_code;
114     uint8_t payload[MAX_UDS_PAYLOAD_LENGTH];
115     uint8_t payload_length;
116 } DiagnosticResponse;
117
118 /* Public: Friendly names for all OBD-II modes.
119  */
120 typedef enum {
121     OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST = 0x1,
122     OBD2_MODE_POWERTRAIN_FREEZE_FRAME_REQUEST = 0x2,
123     OBD2_MODE_EMISSIONS_DTC_REQUEST = 0x3,
124     OBD2_MODE_EMISSIONS_DTC_CLEAR = 0x4,
125     // 0x5 is for non-CAN only
126     // OBD2_MODE_OXYGEN_SENSOR_TEST = 0x5,
127     OBD2_MODE_TEST_RESULTS = 0x6,
128     OBD2_MODE_DRIVE_CYCLE_DTC_REQUEST = 0x7,
129     OBD2_MODE_CONTROL = 0x8,
130     OBD2_MODE_VEHICLE_INFORMATION = 0x9,
131     OBD2_MODE_PERMANENT_DTC_REQUEST = 0xa,
132     // this one isn't technically in uds, but both of the enhanced standards
133     // have their PID requests at 0x22
134     OBD2_MODE_ENHANCED_DIAGNOSTIC_REQUEST = 0x22
135 } DiagnosticMode;
136
137 /* Public: The signature for an optional function to be called when a diagnostic
138  * request is complete, and a response is received or there is a fatal error.
139  *
140  * response - the completed DiagnosticResponse.
141  */
142 typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
143
144 /* Public: A handle for initiating and continuing a single diagnostic request.
145  *
146  * A diagnostic request requires one or more CAN messages to be sent, and one
147  * or more CAN messages to be received before it is completed. This struct
148  * encapsulates the local state required to track the request while it is in
149  * progress.
150  *
151  * request - The original DiagnosticRequest that this handle was created for.
152  * completed - True if the request was completed successfully, or was otherwise
153  *      cancelled.
154  * success - True if the request send and receive process was successful. The
155  *      value if this field isn't valid if 'completed' isn't true.
156  */
157 typedef struct {
158     DiagnosticRequest request;
159     bool success;
160     bool completed;
161
162     // Private
163     IsoTpShims isotp_shims;
164     IsoTpSendHandle isotp_send_handle;
165     IsoTpReceiveHandle isotp_receive_handles[MAX_RESPONDING_ECU_COUNT];
166     uint8_t isotp_receive_handle_count;
167     DiagnosticResponseReceived callback;
168     // DiagnosticMilStatusReceived mil_status_callback;
169     // DiagnosticVinReceived vin_callback;
170 } DiagnosticRequestHandle;
171
172 /* Public: The two major types of PIDs that determine the OBD-II mode and PID
173  * field length.
174  */
175 typedef enum {
176     DIAGNOSTIC_STANDARD_PID,
177     DIAGNOSTIC_ENHANCED_PID
178 } DiagnosticPidRequestType;
179
180 /* Public: A container for the 3 shim functions used by the library to interact
181  * with the wider system.
182  *
183  * Use the diagnostic_init_shims(...) function to create an instance of this
184  * struct.
185  */
186 typedef struct {
187     LogShim log;
188     SendCanMessageShim send_can_message;
189     SetTimerShim set_timer;
190 } DiagnosticShims;
191
192 #ifdef __cplusplus
193 }
194 #endif
195
196 #endif // __UDS_TYPES_H__