2 #include <bitfield/bitfield.h>
3 #include <canutil/read.h>
9 #define ARBITRATION_ID_OFFSET 0x8
10 #define MODE_RESPONSE_OFFSET 0x40
11 #define NEGATIVE_RESPONSE_MODE 0x7f
12 #define MAX_DIAGNOSTIC_PAYLOAD_SIZE 6
13 #define MODE_BYTE_INDEX 0
14 #define PID_BYTE_INDEX 1
15 #define NEGATIVE_RESPONSE_MODE_INDEX 1
16 #define NEGATIVE_RESPONSE_NRC_INDEX 2
19 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
22 DiagnosticShims diagnostic_init_shims(LogShim log,
23 SendCanMessageShim send_can_message,
24 SetTimerShim set_timer) {
25 DiagnosticShims shims = {
27 send_can_message: send_can_message,
33 static void setup_receive_handle(DiagnosticRequestHandle* handle) {
34 if(handle->request.arbitration_id == OBD2_FUNCTIONAL_BROADCAST_ID) {
37 response_id < OBD2_FUNCTIONAL_RESPONSE_COUNT; ++response_id) {
38 handle->isotp_receive_handles[response_id] = isotp_receive(
40 OBD2_FUNCTIONAL_RESPONSE_START + response_id,
43 handle->isotp_receive_handle_count = OBD2_FUNCTIONAL_RESPONSE_COUNT;
45 handle->isotp_receive_handle_count = 1;
46 handle->isotp_receive_handles[0] = isotp_receive(&handle->isotp_shims,
47 handle->request.arbitration_id + ARBITRATION_ID_OFFSET,
52 static uint16_t autoset_pid_length(uint8_t mode, uint16_t pid,
55 if(pid > 0xffff || mode > 10) {
64 static void send_diagnostic_request(DiagnosticShims* shims,
65 DiagnosticRequestHandle* handle) {
66 uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
67 payload[MODE_BYTE_INDEX] = handle->request.mode;
68 if(handle->request.has_pid) {
69 handle->request.pid_length = autoset_pid_length(handle->request.mode,
70 handle->request.pid, handle->request.pid_length);
71 handle->request.pid_length = handle->request.pid_length;
72 set_bitfield(handle->request.pid, PID_BYTE_INDEX * CHAR_BIT,
73 handle->request.pid_length * CHAR_BIT, payload,
77 if(handle->request.payload_length > 0) {
78 memcpy(&payload[PID_BYTE_INDEX + handle->request.pid_length],
79 handle->request.payload, handle->request.payload_length);
82 handle->isotp_send_handle = isotp_send(&handle->isotp_shims,
83 handle->request.arbitration_id, payload,
84 1 + handle->request.payload_length + handle->request.pid_length,
86 if(shims->log != NULL) {
87 char request_string[128] = {0};
88 diagnostic_request_to_string(&handle->request, request_string,
89 sizeof(request_string));
90 shims->log("Sending diagnostic request: %s", request_string);
94 void start_diagnostic_request(DiagnosticShims* shims,
95 DiagnosticRequestHandle* handle) {
96 handle->success = false;
97 handle->completed = false;
98 send_diagnostic_request(shims, handle);
99 setup_receive_handle(handle);
102 DiagnosticRequestHandle generate_diagnostic_request(DiagnosticShims* shims,
103 DiagnosticRequest* request, DiagnosticResponseReceived callback) {
104 DiagnosticRequestHandle handle = {
111 handle.isotp_shims = isotp_init_shims(shims->log,
112 shims->send_can_message,
114 handle.isotp_shims.frame_padding = !request->no_frame_padding;
117 // TODO notes on multi frame:
118 // TODO what are the timers for exactly?
120 // when sending multi frame, send 1 frame, wait for a response
121 // if it says send all, send all right away
122 // if it says flow control, set the time for the next send
123 // instead of creating a timer with an async callback, add a process_handle
124 // function that's called repeatedly in the main loop - if it's time to
125 // send, we do it. so there's a process_handle_send and receive_can_frame
126 // that are just called continuously from the main loop. it's a waste of a
127 // few cpu cycles but it may be more natural than callbacks.
129 // what would a timer callback look like...it would need to pass the handle
130 // and that's all. seems like a context void* would be able to capture all
131 // of the information but arg, memory allocation. look at how it's done in
132 // the other library again
136 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
137 DiagnosticRequest* request, DiagnosticResponseReceived callback) {
138 DiagnosticRequestHandle handle = generate_diagnostic_request(
139 shims, request, callback);
140 start_diagnostic_request(shims, &handle);
144 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
145 DiagnosticPidRequestType pid_request_type, uint32_t arbitration_id,
146 uint16_t pid, DiagnosticResponseReceived callback) {
147 DiagnosticRequest request = {
148 arbitration_id: arbitration_id,
149 mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
154 return diagnostic_request(shims, &request, callback);
157 static bool handle_negative_response(IsoTpMessage* message,
158 DiagnosticResponse* response, DiagnosticShims* shims) {
159 bool response_was_negative = false;
160 if(response->mode == NEGATIVE_RESPONSE_MODE) {
161 response_was_negative = true;
162 if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
163 response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
166 if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
167 response->negative_response_code =
168 message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
171 response->success = false;
172 response->completed = true;
174 return response_was_negative;
177 static bool handle_positive_response(DiagnosticRequestHandle* handle,
178 IsoTpMessage* message, DiagnosticResponse* response,
179 DiagnosticShims* shims) {
180 bool response_was_positive = false;
181 if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
182 response_was_positive = true;
183 // hide the "response" version of the mode from the user
185 response->mode = handle->request.mode;
186 response->has_pid = false;
187 if(handle->request.has_pid && message->size > 1) {
188 response->has_pid = true;
189 if(handle->request.pid_length == 2) {
190 response->pid = get_bitfield(message->payload, message->size,
191 PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
193 response->pid = message->payload[PID_BYTE_INDEX];
198 if((!handle->request.has_pid && !response->has_pid)
199 || response->pid == handle->request.pid) {
200 response->success = true;
201 response->completed = true;
203 uint8_t payload_index = 1 + handle->request.pid_length;
204 response->payload_length = MAX(0, message->size - payload_index);
205 if(response->payload_length > 0) {
206 memcpy(response->payload, &message->payload[payload_index],
207 response->payload_length);
210 response_was_positive = false;
213 return response_was_positive;
216 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
217 DiagnosticRequestHandle* handle, const uint32_t arbitration_id,
218 const uint8_t data[], const uint8_t size) {
220 DiagnosticResponse response = {
221 arbitration_id: arbitration_id,
226 if(!handle->isotp_send_handle.completed) {
227 isotp_continue_send(&handle->isotp_shims,
228 &handle->isotp_send_handle, arbitration_id, data, size);
231 for(i = 0; i < handle->isotp_receive_handle_count; ++i) {
232 IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
233 &handle->isotp_receive_handles[i], arbitration_id, data,
236 if(message.completed) {
237 if(message.size > 0) {
238 response.mode = message.payload[0];
239 if(handle_negative_response(&message, &response, shims) ||
240 handle_positive_response(handle, &message,
242 if(shims->log != NULL) {
243 char response_string[128] = {0};
244 diagnostic_response_to_string(&response,
245 response_string, sizeof(response_string));
246 shims->log("Diagnostic response received: %s",
250 handle->success = true;
251 handle->completed = true;
254 if(shims->log != NULL) {
255 shims->log("Received an empty response on arb ID 0x%x",
256 response.arbitration_id);
260 if(handle->completed && handle->callback != NULL) {
261 handle->callback(&response);
271 int diagnostic_payload_to_integer(const DiagnosticResponse* response) {
272 return get_bitfield(response->payload, response->payload_length, 0,
273 response->payload_length * CHAR_BIT);
276 float diagnostic_decode_obd2_pid(const DiagnosticResponse* response) {
277 // handles on the single number values, not the bit encoded ones
278 switch(response->pid) {
280 return response->payload[0] * 3;
282 return (response->payload[0] * 256 + response->payload[1]) / 4.0;
286 return response->payload[0];
288 return (response->payload[0] * 256 + response->payload[1]) / 100.0;
296 return response->payload[0] * 100.0 / 255.0;
301 return response->payload[0] - 40;
303 return response->payload[0] - 125;
309 void diagnostic_response_to_string(const DiagnosticResponse* response,
310 char* destination, size_t destination_length) {
311 int bytes_used = snprintf(destination, destination_length,
312 "arb_id: 0x%02x, mode: 0x%x, ",
313 response->arbitration_id,
316 if(response->has_pid) {
317 bytes_used += snprintf(destination + bytes_used,
318 destination_length - bytes_used,
323 if(!response->success) {
324 bytes_used += snprintf(destination + bytes_used,
325 destination_length - bytes_used,
327 response->negative_response_code);
330 if(response->payload_length > 0) {
331 snprintf(destination + bytes_used, destination_length - bytes_used,
332 "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
333 response->payload[0],
334 response->payload[1],
335 response->payload[2],
336 response->payload[3],
337 response->payload[4],
338 response->payload[5],
339 response->payload[6]);
341 snprintf(destination + bytes_used, destination_length - bytes_used,
346 void diagnostic_request_to_string(const DiagnosticRequest* request,
347 char* destination, size_t destination_length) {
348 int bytes_used = snprintf(destination, destination_length,
349 "arb_id: 0x%02x, mode: 0x%x, ",
350 request->arbitration_id,
353 if(request->has_pid) {
354 bytes_used += snprintf(destination + bytes_used,
355 destination_length - bytes_used,
360 int remaining_space = destination_length - bytes_used;
361 if(request->payload_length > 0) {
362 snprintf(destination + bytes_used, remaining_space,
363 "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
370 request->payload[6]);
372 snprintf(destination + bytes_used, remaining_space, "no payload");
376 bool diagnostic_request_equals(const DiagnosticRequest* ours,
377 const DiagnosticRequest* theirs) {
378 bool equals = ours->arbitration_id == theirs->arbitration_id &&
379 ours->mode == theirs->mode;
380 equals &= ours->has_pid == theirs->has_pid;
381 equals &= ours->pid == theirs->pid;