4 #include <isotp/isotp.h>
12 #define MAX_OBD2_PAYLOAD_LENGTH 7
16 uint16_t arbitration_id;
19 uint8_t payload[MAX_OBD2_PAYLOAD_LENGTH];
20 uint8_t payload_length;
24 // http://www.canbushack.com/blog/index.php?title=scanning-for-diagnostic-data&more=1&c=1&tb=1&pb=1
25 // for the list of NRCs
28 NRC_SERVICE_NOT_SUPPORTED = 0x11,
29 NRC_SUB_FUNCTION_NOT_SUPPORTED = 0x12,
30 NRC_CONDITIONS_NOT_CORRECT = 0x22,
31 NRC_REQUEST_OUT_OF_RANGE = 0x31,
32 NRC_SECURITY_ACCESS_DENIED = 0x33,
33 NRC_INVALID_KEY = 0x35,
34 NRC_TOO_MANY_ATTEMPS = 0x36,
35 NRC_TIME_DELAY_NOT_EXPIRED = 0x37,
36 NRC_RESPONSE_PENDING = 0x78
37 } DiagnosticNegativeResponseCode;
40 OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST = 0x1,
41 OBD2_MODE_POWERTRAIN_FREEZE_FRAME_REQUEST = 0x2,
42 OBD2_MODE_EMISSIONS_DTC_REQUEST = 0x3,
43 OBD2_MODE_EMISSIONS_DTC_CLEAR = 0x4,
44 // 0x5 is for non-CAN only
45 // OBD2_MODE_OXYGEN_SENSOR_TEST = 0x5,
46 OBD2_MODE_TEST_RESULTS = 0x6,
47 OBD2_MODE_DRIVE_CYCLE_DTC_REQUEST = 0x7,
48 OBD2_MODE_CONTROL = 0x8,
49 OBD2_MODE_VEHICLE_INFORMATION = 0x9,
50 OBD2_MODE_PERMANENT_DTC_REQUEST = 0xa,
51 // this one isn't technically in OBD2, but both of the enhanced standards
52 // have their PID requests at 0x22
53 OBD2_MODE_ENHANCED_DIAGNOSTIC_REQUEST = 0x22
60 } DiagnosticTroubleCodeType;
63 uint16_t arbitration_id;
66 // if mode is one with a PID, read the correct numbers of PID bytes (1 or 2)
67 // into this field, then store the remainder of the payload in the payload
70 DiagnosticNegativeResponseCode negative_response_code;
71 // if response mode is a negative response, read first byte of payload into
72 // NRC and store remainder of payload in payload field
73 uint8_t payload[MAX_OBD2_PAYLOAD_LENGTH];
74 uint8_t payload_length;
82 } DiagnosticTroubleCodeGroup;
85 DiagnosticTroubleCodeGroup group;
88 } DiagnosticTroubleCode;
90 typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
91 typedef void (*DiagnosticMilStatusReceived)(bool malfunction_indicator_status);
92 typedef void (*DiagnosticVinReceived)(uint8_t vin[]);
93 typedef void (*DiagnosticTroubleCodesReceived)(
94 DiagnosticMode mode, DiagnosticTroubleCode* codes);
95 typedef void (*DiagnosticPidEnumerationReceived)(
96 const DiagnosticResponse* response, uint16_t* pids);
98 // TODO should we enumerate every OBD-II PID? need conversion formulas, too
101 uint8_t bytes_returned;
104 } DiagnosticParameter;
107 DIAGNOSTIC_REQUEST_TYPE_PID,
108 DIAGNOSTIC_REQUEST_TYPE_DTC,
109 DIAGNOSTIC_REQUEST_TYPE_MIL_STATUS,
110 DIAGNOSTIC_REQUEST_TYPE_VIN
111 } DiagnosticRequestType;
114 IsoTpHandler isotp_handler;
115 // TODO the Handle may need to keep the original request, otherwise we can't
116 // compare an incoming CAN message to see if it matches the service / PID!
117 // TODO i'm not sure this type/callback in here is too useful - see the
118 // comments in obd2.c:diagnostic_request
119 DiagnosticRequestType type;
120 DiagnosticResponseReceived callback;
121 DiagnosticMilStatusReceived mil_status_callback;
122 DiagnosticVinReceived vin_callback;
124 } DiagnosticRequestHandle;
127 DIAGNOSTIC_STANDARD_PID,
128 DIAGNOSTIC_ENHANCED_PID
129 } DiagnosticPidRequestType;
132 IsoTpShims isotp_shims;
136 DiagnosticShims diagnostic_init_shims(LogShim log,
137 SendCanMessageShim send_can_message,
138 SetTimerShim set_timer);
140 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
141 DiagnosticRequest* request, DiagnosticResponseReceived callback);
143 // decide mode 0x1 / 0x22 based on pid type
144 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
145 DiagnosticPidRequestType pid_request_type, uint16_t pid,
146 DiagnosticResponseReceived callback);
148 DiagnosticRequestHandle diagnostic_request_malfunction_indicator_status(
149 DiagnosticShims* shims,
150 DiagnosticMilStatusReceived callback);
152 DiagnosticRequestHandle diagnostic_request_vin(DiagnosticShims* shims,
153 DiagnosticVinReceived callback);
155 DiagnosticRequestHandle diagnostic_request_dtc(DiagnosticShims* shims,
156 DiagnosticTroubleCodeType dtc_type,
157 DiagnosticTroubleCodesReceived callback);
159 bool diagnostic_clear_dtc(DiagnosticShims* shims);
161 DiagnosticRequestHandle diagnostic_enumerate_pids(DiagnosticShims* shims,
162 DiagnosticRequest* request, DiagnosticPidEnumerationReceived callback);
164 void diagnostic_receive_can_frame(DiagnosticRequestHandle* handle,
165 const uint16_t arbitration_id, const uint8_t data[],