2 #include <bitfield/bitfield.h>
3 #include <canutil/read.h>
10 #define ARBITRATION_ID_OFFSET 0x8
11 #define MODE_RESPONSE_OFFSET 0x40
12 #define NEGATIVE_RESPONSE_MODE 0x7f
13 #define MAX_DIAGNOSTIC_PAYLOAD_SIZE 6
14 #define MODE_BYTE_INDEX 0
15 #define PID_BYTE_INDEX 1
16 #define NEGATIVE_RESPONSE_MODE_INDEX 1
17 #define NEGATIVE_RESPONSE_NRC_INDEX 2
20 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
23 DiagnosticShims diagnostic_init_shims(LogShim log,
24 SendCanMessageShim send_can_message,
25 SetTimerShim set_timer) {
26 DiagnosticShims shims = {
28 send_can_message: send_can_message,
34 static void setup_receive_handle(DiagnosticRequestHandle* handle) {
35 if(handle->request.arbitration_id == OBD2_FUNCTIONAL_BROADCAST_ID) {
38 response_id < OBD2_FUNCTIONAL_RESPONSE_COUNT; ++response_id) {
39 handle->isotp_receive_handles[response_id] = isotp_receive(
41 OBD2_FUNCTIONAL_RESPONSE_START + response_id,
44 handle->isotp_receive_handle_count = OBD2_FUNCTIONAL_RESPONSE_COUNT;
46 handle->isotp_receive_handle_count = 1;
47 handle->isotp_receive_handles[0] = isotp_receive(&handle->isotp_shims,
48 handle->request.arbitration_id + ARBITRATION_ID_OFFSET,
53 static uint16_t autoset_pid_length(uint8_t mode, uint16_t pid,
56 if(mode <= 0xa || mode == 0x3e ) {
58 } else if(pid > 0xffff || ((pid & 0xFF00) > 0x0)) {
67 static void send_diagnostic_request(DiagnosticShims* shims,
68 DiagnosticRequestHandle* handle) {
69 uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
70 payload[MODE_BYTE_INDEX] = handle->request.mode;
71 if(handle->request.has_pid) {
72 handle->request.pid_length = autoset_pid_length(handle->request.mode,
73 handle->request.pid, handle->request.pid_length);
74 set_bitfield(handle->request.pid, PID_BYTE_INDEX * CHAR_BIT,
75 handle->request.pid_length * CHAR_BIT, payload,
79 if(handle->request.payload_length > 0) {
80 memcpy(&payload[PID_BYTE_INDEX + handle->request.pid_length],
81 handle->request.payload, handle->request.payload_length);
84 handle->isotp_send_handle = isotp_send(&handle->isotp_shims,
85 handle->request.arbitration_id, payload,
86 1 + handle->request.payload_length + handle->request.pid_length,
88 if(handle->isotp_send_handle.completed &&
89 !handle->isotp_send_handle.success) {
90 handle->completed = true;
91 handle->success = false;
92 if(shims->log != NULL) {
93 shims->log("%s", "Diagnostic request not sent");
95 } else if(shims->log != NULL) {
96 char request_string[128] = {0};
97 diagnostic_request_to_string(&handle->request, request_string,
98 sizeof(request_string));
99 shims->log("Sending diagnostic request: %s", request_string);
103 bool diagnostic_request_sent(DiagnosticRequestHandle* handle) {
104 return handle->isotp_send_handle.completed;
107 void start_diagnostic_request(DiagnosticShims* shims,
108 DiagnosticRequestHandle* handle) {
109 handle->success = false;
110 handle->completed = false;
111 send_diagnostic_request(shims, handle);
112 if(!handle->completed) {
113 setup_receive_handle(handle);
117 DiagnosticRequestHandle generate_diagnostic_request(DiagnosticShims* shims,
118 DiagnosticRequest* request, DiagnosticResponseReceived callback) {
119 DiagnosticRequestHandle handle = {
126 handle.isotp_shims = isotp_init_shims(shims->log,
127 shims->send_can_message,
129 handle.isotp_shims.frame_padding = !request->no_frame_padding;
132 // TODO notes on multi frame:
133 // TODO what are the timers for exactly?
135 // when sending multi frame, send 1 frame, wait for a response
136 // if it says send all, send all right away
137 // if it says flow control, set the time for the next send
138 // instead of creating a timer with an async callback, add a process_handle
139 // function that's called repeatedly in the main loop - if it's time to
140 // send, we do it. so there's a process_handle_send and receive_can_frame
141 // that are just called continuously from the main loop. it's a waste of a
142 // few cpu cycles but it may be more natural than callbacks.
144 // what would a timer callback look like...it would need to pass the handle
145 // and that's all. seems like a context void* would be able to capture all
146 // of the information but arg, memory allocation. look at how it's done in
147 // the other library again
151 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
152 DiagnosticRequest* request, DiagnosticResponseReceived callback) {
153 DiagnosticRequestHandle handle = generate_diagnostic_request(
154 shims, request, callback);
155 start_diagnostic_request(shims, &handle);
159 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
160 DiagnosticPidRequestType pid_request_type, uint32_t arbitration_id,
161 uint16_t pid, DiagnosticResponseReceived callback) {
162 DiagnosticRequest request = {
163 arbitration_id: arbitration_id,
164 mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
169 return diagnostic_request(shims, &request, callback);
172 static bool handle_negative_response(IsoTpMessage* message,
173 DiagnosticResponse* response, DiagnosticShims* shims) {
174 bool response_was_negative = false;
175 if(response->mode == NEGATIVE_RESPONSE_MODE) {
176 response_was_negative = true;
177 if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
178 response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
181 if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
182 response->negative_response_code =
183 message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
186 response->success = false;
187 response->completed = true;
189 return response_was_negative;
192 static bool handle_positive_response(DiagnosticRequestHandle* handle,
193 IsoTpMessage* message, DiagnosticResponse* response,
194 DiagnosticShims* shims) {
195 bool response_was_positive = false;
196 if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
197 response_was_positive = true;
198 // hide the "response" version of the mode from the user
200 response->mode = handle->request.mode;
201 response->has_pid = false;
202 if(handle->request.has_pid && message->size > 1) {
203 response->has_pid = true;
204 if(handle->request.pid_length == 2) {
205 response->pid = get_bitfield(message->payload, message->size,
206 PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
208 response->pid = message->payload[PID_BYTE_INDEX];
213 if((!handle->request.has_pid && !response->has_pid)
214 || response->pid == handle->request.pid) {
215 response->success = true;
216 response->completed = true;
218 uint8_t payload_index = 1 + handle->request.pid_length;
219 response->payload_length = MAX(0, message->size - payload_index);
220 if(response->payload_length > 0) {
221 memcpy(response->payload, &message->payload[payload_index],
222 response->payload_length);
225 response_was_positive = false;
228 return response_was_positive;
231 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
232 DiagnosticRequestHandle* handle, const uint32_t arbitration_id,
233 const uint8_t data[], const uint8_t size) {
235 DiagnosticResponse response = {
236 arbitration_id: arbitration_id,
242 if(!handle->isotp_send_handle.completed) {
243 isotp_continue_send(&handle->isotp_shims,
244 &handle->isotp_send_handle, arbitration_id, data, size);
247 for(i = 0; i < handle->isotp_receive_handle_count; ++i) {
248 IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
249 &handle->isotp_receive_handles[i], arbitration_id, data,
251 response.multi_frame = message.multi_frame;
253 if(message.completed) {
254 if(message.size > 0) {
255 response.mode = message.payload[0];
256 if(handle_negative_response(&message, &response, shims) ||
257 handle_positive_response(handle, &message,
259 if(shims->log != NULL) {
260 char response_string[128] = {0};
261 diagnostic_response_to_string(&response,
262 response_string, sizeof(response_string));
263 shims->log("Diagnostic response received: %s",
267 handle->success = true;
268 handle->completed = true;
271 if(shims->log != NULL) {
272 shims->log("Received an empty response on arb ID 0x%x",
273 response.arbitration_id);
277 if(handle->completed && handle->callback != NULL) {
278 handle->callback(&response);
288 int diagnostic_payload_to_integer(const DiagnosticResponse* response) {
289 return get_bitfield(response->payload, response->payload_length, 0,
290 response->payload_length * CHAR_BIT);
293 float diagnostic_decode_obd2_pid(const DiagnosticResponse* response) {
294 // handles on the single number values, not the bit encoded ones
295 switch(response->pid) {
297 return response->payload[0] * 3;
299 return (response->payload[0] * 256 + response->payload[1]) / 4.0;
303 return response->payload[0];
305 return (response->payload[0] * 256 + response->payload[1]) / 100.0;
313 return response->payload[0] * 100.0 / 255.0;
318 return response->payload[0] - 40;
320 return response->payload[0] - 125;
322 return diagnostic_payload_to_integer(response);
326 void diagnostic_response_to_string(const DiagnosticResponse* response,
327 char* destination, size_t destination_length) {
328 int bytes_used = snprintf(destination, destination_length,
329 "arb_id: 0x%lx, mode: 0x%x, ",
330 (unsigned long) response->arbitration_id,
333 if(response->has_pid) {
334 bytes_used += snprintf(destination + bytes_used,
335 destination_length - bytes_used,
340 if(!response->success) {
341 bytes_used += snprintf(destination + bytes_used,
342 destination_length - bytes_used,
344 response->negative_response_code);
347 if(response->payload_length > 0) {
348 snprintf(destination + bytes_used, destination_length - bytes_used,
349 "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
350 response->payload[0],
351 response->payload[1],
352 response->payload[2],
353 response->payload[3],
354 response->payload[4],
355 response->payload[5],
356 response->payload[6]);
358 snprintf(destination + bytes_used, destination_length - bytes_used,
363 void diagnostic_request_to_string(const DiagnosticRequest* request,
364 char* destination, size_t destination_length) {
365 int bytes_used = snprintf(destination, destination_length,
366 "arb_id: 0x%lx, mode: 0x%x, ",
367 (unsigned long) request->arbitration_id,
370 if(request->has_pid) {
371 bytes_used += snprintf(destination + bytes_used,
372 destination_length - bytes_used,
377 int remaining_space = destination_length - bytes_used;
378 if(request->payload_length > 0) {
379 snprintf(destination + bytes_used, remaining_space,
380 "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
387 request->payload[6]);
389 snprintf(destination + bytes_used, remaining_space, "no payload");
393 bool diagnostic_request_equals(const DiagnosticRequest* ours,
394 const DiagnosticRequest* theirs) {
395 bool equals = ours->arbitration_id == theirs->arbitration_id &&
396 ours->mode == theirs->mode;
397 equals &= ours->has_pid == theirs->has_pid;
398 equals &= ours->pid == theirs->pid;