Automatically set pid length for outgoing requests if not specified.
[apps/low-level-can-service.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  * type - the type of the request (TODO unused)
46  */
47 typedef struct {
48     uint16_t arbitration_id;
49     uint8_t mode;
50     bool has_pid;
51     uint16_t pid;
52     uint8_t pid_length;
53     uint8_t payload[MAX_UDS_PAYLOAD_LENGTH];
54     uint8_t payload_length;
55     DiagnosticRequestType type;
56 } DiagnosticRequest;
57
58 /* Public: All possible negative response codes that could be received from a
59  * requested node.
60  *
61  * When a DiagnosticResponse is received and the 'completed' field is true, but
62  * the 'success' field is false, the 'negative_response_code' field will contain
63  * one of these values as reported by the requested node.
64  *
65  * Thanks to canbushack.com for the list of NRCs.
66  */
67 typedef enum {
68     NRC_SUCCESS = 0x0,
69     NRC_SERVICE_NOT_SUPPORTED = 0x11,
70     NRC_SUB_FUNCTION_NOT_SUPPORTED = 0x12,
71     NRC_CONDITIONS_NOT_CORRECT = 0x22,
72     NRC_REQUEST_OUT_OF_RANGE = 0x31,
73     NRC_SECURITY_ACCESS_DENIED = 0x33,
74     NRC_INVALID_KEY = 0x35,
75     NRC_TOO_MANY_ATTEMPS = 0x36,
76     NRC_TIME_DELAY_NOT_EXPIRED = 0x37,
77     NRC_RESPONSE_PENDING = 0x78
78 } DiagnosticNegativeResponseCode;
79
80 /* Public: A partially or fully completed response to a diagnostic request.
81  *
82  * completed - True if the request is complete - some functions return a
83  *      DiagnosticResponse even when it's only partially completed, so be sure
84  *      to check this field.
85  * success - True if the request was successful. The value if this
86  *      field isn't valid if 'completed' isn't true. If this is 'false', check
87  *      the negative_response_code field for the reason.
88  * arbitration_id - The arbitration ID the response was received on.
89  * mode - The OBD-II mode for the original request.
90  * has_pid - If this is a response to a PID request, this will be true and the
91  *      'pid' field will be valid.
92  * pid - If the request was for a PID, this is the PID echo. Only valid if
93  *      'has_pid' is true.
94  * negative_response_code - If the request was not successful, 'success' will be
95  *      false and this will be set to a DiagnosticNegativeResponseCode returned
96  *      by the other node.
97  * payload - An optional payload for the response - NULL if no payload.
98  * payload_length - The length of the payload or 0 if none.
99  */
100 typedef struct {
101     bool completed;
102     bool success;
103     uint16_t arbitration_id;
104     uint8_t mode;
105     bool has_pid;
106     uint16_t pid;
107     DiagnosticNegativeResponseCode negative_response_code;
108     uint8_t payload[MAX_UDS_PAYLOAD_LENGTH];
109     uint8_t payload_length;
110 } DiagnosticResponse;
111
112 /* Public: Friendly names for all OBD-II modes.
113  */
114 typedef enum {
115     OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST = 0x1,
116     OBD2_MODE_POWERTRAIN_FREEZE_FRAME_REQUEST = 0x2,
117     OBD2_MODE_EMISSIONS_DTC_REQUEST = 0x3,
118     OBD2_MODE_EMISSIONS_DTC_CLEAR = 0x4,
119     // 0x5 is for non-CAN only
120     // OBD2_MODE_OXYGEN_SENSOR_TEST = 0x5,
121     OBD2_MODE_TEST_RESULTS = 0x6,
122     OBD2_MODE_DRIVE_CYCLE_DTC_REQUEST = 0x7,
123     OBD2_MODE_CONTROL = 0x8,
124     OBD2_MODE_VEHICLE_INFORMATION = 0x9,
125     OBD2_MODE_PERMANENT_DTC_REQUEST = 0xa,
126     // this one isn't technically in uds, but both of the enhanced standards
127     // have their PID requests at 0x22
128     OBD2_MODE_ENHANCED_DIAGNOSTIC_REQUEST = 0x22
129 } DiagnosticMode;
130
131 /* Public the signature for an optional function to be called when a diagnostic
132  * request is complete, and a response is received or there is a fatal error.
133  *
134  * response - the completed DiagnosticResponse.
135  */
136 typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
137
138 typedef float (*DiagnosticResponseDecoder)(const DiagnosticResponse* response);
139
140 /* Public: A handle for initiating and continuing a single diagnostic request.
141  *
142  * A diagnostic request requires one or more CAN messages to be sent, and one
143  * or more CAN messages to be received before it is completed. This struct
144  * encapsulates the local state required to track the request while it is in
145  * progress.
146  *
147  * request - The original DiagnosticRequest that this handle was created for.
148  * completed - True if the request was completed successfully, or was otherwise
149  *      cancelled.
150  * success - True if the request send and receive process was successful. The
151  *      value if this field isn't valid if 'completed' isn't true.
152  */
153 typedef struct {
154     DiagnosticRequest request;
155     bool success;
156     bool completed;
157
158     // Private
159     IsoTpShims isotp_shims;
160     IsoTpSendHandle isotp_send_handle;
161     IsoTpReceiveHandle isotp_receive_handles[MAX_RESPONDING_ECU_COUNT];
162     uint8_t isotp_receive_handle_count;
163     DiagnosticResponseReceived callback;
164     // DiagnosticMilStatusReceived mil_status_callback;
165     // DiagnosticVinReceived vin_callback;
166 } DiagnosticRequestHandle;
167
168 /* Public: The two major types of PIDs that determine the OBD-II mode and PID
169  * field length.
170  */
171 typedef enum {
172     DIAGNOSTIC_STANDARD_PID,
173     DIAGNOSTIC_ENHANCED_PID
174 } DiagnosticPidRequestType;
175
176 /* Public: A container for the 3 shim functions used by the library to interact
177  * with the wider system.
178  *
179  * Use the diagnostic_init_shims(...) function to create an instance of this
180  * struct.
181  */
182 typedef struct {
183     LogShim log;
184     SendCanMessageShim send_can_message;
185     SetTimerShim set_timer;
186 } DiagnosticShims;
187
188 #ifdef __cplusplus
189 }
190 #endif
191
192 #endif // __UDS_TYPES_H__