1 #ifndef __OBD2_TYPES_H__
2 #define __OBD2_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_OBD2_PAYLOAD_LENGTH 7
17 /* Private: The four main types of diagnositc requests that determine how the
18 * request should be parsed and what type of callback should be used.
20 * TODO this may not be used...yet?
23 DIAGNOSTIC_REQUEST_TYPE_PID,
24 DIAGNOSTIC_REQUEST_TYPE_DTC,
25 DIAGNOSTIC_REQUEST_TYPE_MIL_STATUS,
26 DIAGNOSTIC_REQUEST_TYPE_VIN
27 } DiagnosticRequestType;
29 /* Public: A container for a single diagnostic request.
31 * The only required fields are the arbitration_id and mode.
33 * arbitration_id - The arbitration ID to send the request.
34 * mode - The OBD-II mode for the request.
35 * pid - (optional) The PID to request, if the mode requires one.
36 * pid_length - The length of the PID field, either 1 (standard) or 2 bytes
38 * payload - (optional) The payload for the request, if the request requires
39 * one. If payload_length is 0 this field is ignored.
40 * payload_length - The length of the payload, or 0 if no payload is used.
41 * type - the type of the request (TODO unused)
44 uint16_t arbitration_id;
48 uint8_t payload[MAX_OBD2_PAYLOAD_LENGTH];
49 uint8_t payload_length;
50 DiagnosticRequestType type;
53 /* Public: All possible negative response codes that could be received from a
56 * When a DiagnosticResponse is received and the 'completed' field is true, but
57 * the 'success' field is false, the 'negative_response_code' field will contain
58 * one of these values as reported by the requested node.
60 * Thanks to canbushack.com for the list of NRCs.
64 NRC_SERVICE_NOT_SUPPORTED = 0x11,
65 NRC_SUB_FUNCTION_NOT_SUPPORTED = 0x12,
66 NRC_CONDITIONS_NOT_CORRECT = 0x22,
67 NRC_REQUEST_OUT_OF_RANGE = 0x31,
68 NRC_SECURITY_ACCESS_DENIED = 0x33,
69 NRC_INVALID_KEY = 0x35,
70 NRC_TOO_MANY_ATTEMPS = 0x36,
71 NRC_TIME_DELAY_NOT_EXPIRED = 0x37,
72 NRC_RESPONSE_PENDING = 0x78
73 } DiagnosticNegativeResponseCode;
75 /* Public: A partially or fully completed response to a diagnostic request.
77 * completed - True if the request is complete - some functions return a
78 * DiagnosticResponse even when it's only partially completed, so be sure
79 * to check this field.
80 * success - True if the request was successful. The value if this
81 * field isn't valid if 'completed' isn't true. If this is 'false', check
82 * the negative_response_code field for the reason.
83 * arbitration_id - The arbitration ID the response was received on.
84 * mode - The OBD-II mode for the original request.
85 * pid - If the request was for a PID, this is the PID echo.
86 * negative_response_code - If the request was not successful, 'success' will be
87 * false and this will be set to a DiagnosticNegativeResponseCode returned
89 * payload - An optional payload for the response - NULL if no payload.
90 * payload_length - The length of the payload or 0 if none.
95 uint16_t arbitration_id;
98 DiagnosticNegativeResponseCode negative_response_code;
99 uint8_t payload[MAX_OBD2_PAYLOAD_LENGTH];
100 uint8_t payload_length;
101 } DiagnosticResponse;
103 /* Public: Friendly names for all OBD-II modes.
106 OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST = 0x1,
107 OBD2_MODE_POWERTRAIN_FREEZE_FRAME_REQUEST = 0x2,
108 OBD2_MODE_EMISSIONS_DTC_REQUEST = 0x3,
109 OBD2_MODE_EMISSIONS_DTC_CLEAR = 0x4,
110 // 0x5 is for non-CAN only
111 // OBD2_MODE_OXYGEN_SENSOR_TEST = 0x5,
112 OBD2_MODE_TEST_RESULTS = 0x6,
113 OBD2_MODE_DRIVE_CYCLE_DTC_REQUEST = 0x7,
114 OBD2_MODE_CONTROL = 0x8,
115 OBD2_MODE_VEHICLE_INFORMATION = 0x9,
116 OBD2_MODE_PERMANENT_DTC_REQUEST = 0xa,
117 // this one isn't technically in OBD2, but both of the enhanced standards
118 // have their PID requests at 0x22
119 OBD2_MODE_ENHANCED_DIAGNOSTIC_REQUEST = 0x22
122 /* Public the signature for an optional function to be called when a diagnostic
123 * request is complete, and a response is received or there is a fatal error.
125 * response - the completed DiagnosticResponse.
127 typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
129 typedef float (*DiagnosticResponseDecoder)(const DiagnosticResponse* response);
131 /* Public: A handle for initiating and continuing a single diagnostic request.
133 * A diagnostic request requires one or more CAN messages to be sent, and one
134 * or more CAN messages to be received before it is completed. This struct
135 * encapsulates the local state required to track the request while it is in
138 * request - The original DiagnosticRequest that this handle was created for.
139 * completed - True if the request was completed successfully, or was otherwise
141 * success - True if the request send and receive process was successful. The
142 * value if this field isn't valid if 'completed' isn't true.
145 DiagnosticRequest request;
150 IsoTpShims isotp_shims;
151 IsoTpSendHandle isotp_send_handle;
152 IsoTpReceiveHandle isotp_receive_handle;
153 DiagnosticResponseReceived callback;
154 // DiagnosticMilStatusReceived mil_status_callback;
155 // DiagnosticVinReceived vin_callback;
156 } DiagnosticRequestHandle;
158 /* Public: The two major types of PIDs that determine the OBD-II mode and PID
162 DIAGNOSTIC_STANDARD_PID,
163 DIAGNOSTIC_ENHANCED_PID
164 } DiagnosticPidRequestType;
166 /* Public: A container for the 3 shim functions used by the library to interact
167 * with the wider system.
169 * Use the diagnostic_init_shims(...) function to create an instance of this
174 SendCanMessageShim send_can_message;
175 SetTimerShim set_timer;
182 #endif // __OBD2_TYPES_H__