1 #ifndef __UDS_TYPES_H__
2 #define __UDS_TYPES_H__
4 #include <isotp/isotp.h>
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
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.
21 * TODO this may not be used...yet?
24 DIAGNOSTIC_REQUEST_TYPE_PID,
25 DIAGNOSTIC_REQUEST_TYPE_DTC,
26 DIAGNOSTIC_REQUEST_TYPE_MIL_STATUS,
27 DIAGNOSTIC_REQUEST_TYPE_VIN
28 } DiagnosticRequestType;
30 /* Public: A container for a single diagnostic request.
32 * The only required fields are the arbitration_id and mode.
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
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
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)
52 uint32_t arbitration_id;
57 uint8_t payload[MAX_UDS_PAYLOAD_LENGTH];
58 uint8_t payload_length;
59 bool no_frame_padding;
60 DiagnosticRequestType type;
63 /* Public: All possible negative response codes that could be received from a
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.
70 * Thanks to canbushack.com for the list of NRCs.
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;
86 /* Public: A partially or fully completed response to a diagnostic request.
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
100 * negative_response_code - If the request was not successful, 'success' will be
101 * false and this will be set to a DiagnosticNegativeResponseCode returned
103 * payload - An optional payload for the response - NULL if no payload.
104 * payload_length - The length of the payload or 0 if none.
109 uint32_t arbitration_id;
113 DiagnosticNegativeResponseCode negative_response_code;
114 uint8_t payload[MAX_UDS_PAYLOAD_LENGTH];
115 uint8_t payload_length;
116 } DiagnosticResponse;
118 /* Public: Friendly names for all OBD-II modes.
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
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.
140 * response - the completed DiagnosticResponse.
142 typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
144 /* Public: A handle for initiating and continuing a single diagnostic request.
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
151 * request - The original DiagnosticRequest that this handle was created for.
152 * completed - True if the request was completed successfully, or was otherwise
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.
158 DiagnosticRequest request;
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;
172 /* Public: The two major types of PIDs that determine the OBD-II mode and PID
176 DIAGNOSTIC_STANDARD_PID,
177 DIAGNOSTIC_ENHANCED_PID
178 } DiagnosticPidRequestType;
180 /* Public: A container for the 3 shim functions used by the library to interact
181 * with the wider system.
183 * Use the diagnostic_init_shims(...) function to create an instance of this
188 SendCanMessageShim send_can_message;
189 SetTimerShim set_timer;
196 #endif // __UDS_TYPES_H__