2 #include <bitfield/bitfield.h>
8 #define ARBITRATION_ID_OFFSET 0x8
9 #define MODE_RESPONSE_OFFSET 0x40
10 #define NEGATIVE_RESPONSE_MODE 0x7f
11 #define MAX_DIAGNOSTIC_PAYLOAD_SIZE 6
12 #define MODE_BYTE_INDEX 0
13 #define PID_BYTE_INDEX 1
14 #define NEGATIVE_RESPONSE_MODE_INDEX 1
15 #define NEGATIVE_RESPONSE_NRC_INDEX 2
18 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
21 DiagnosticShims diagnostic_init_shims(LogShim log,
22 SendCanMessageShim send_can_message,
23 SetTimerShim set_timer) {
24 DiagnosticShims shims = {
26 send_can_message: send_can_message,
32 static void setup_receive_handle(DiagnosticRequestHandle* handle) {
33 if(handle->request.arbitration_id == OBD2_FUNCTIONAL_BROADCAST_ID) {
36 response_id < OBD2_FUNCTIONAL_RESPONSE_COUNT; ++response_id) {
37 handle->isotp_receive_handles[response_id] = isotp_receive(
38 &handle->isotp_shims, OBD2_FUNCTIONAL_RESPONSE_START + response_id,
41 handle->isotp_receive_handle_count = OBD2_FUNCTIONAL_RESPONSE_COUNT;
43 handle->isotp_receive_handle_count = 1;
44 handle->isotp_receive_handles[0] = isotp_receive(&handle->isotp_shims,
45 handle->request.arbitration_id + ARBITRATION_ID_OFFSET,
51 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
52 DiagnosticRequest* request, DiagnosticResponseReceived callback) {
53 DiagnosticRequestHandle handle = {
60 uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
61 payload[MODE_BYTE_INDEX] = request->mode;
62 if(request->pid_length > 0) {
63 set_bitfield(request->pid, PID_BYTE_INDEX * CHAR_BIT,
64 request->pid_length * CHAR_BIT, payload, sizeof(payload));
66 if(request->payload_length > 0) {
67 memcpy(&payload[PID_BYTE_INDEX + request->pid_length],
68 request->payload, request->payload_length);
71 handle.isotp_shims = isotp_init_shims(shims->log,
72 shims->send_can_message,
75 handle.isotp_send_handle = isotp_send(&handle.isotp_shims,
76 request->arbitration_id, payload,
77 1 + request->payload_length + request->pid_length,
79 if(shims->log != NULL) {
80 shims->log("Sending diagnostic request: arb_id: 0x%02x, mode: 0x%x, pid: 0x%x, payload: 0x%02x%02x%02x%02x%02x%02x%02x, size: %d\r\n",
81 request->arbitration_id,
91 request->payload_length);
94 setup_receive_handle(&handle);
96 // TODO notes on multi frame:
97 // TODO what are the timers for exactly?
99 // when sending multi frame, send 1 frame, wait for a response
100 // if it says send all, send all right away
101 // if it says flow control, set the time for the next send
102 // instead of creating a timer with an async callback, add a process_handle
103 // function that's called repeatedly in the main loop - if it's time to
104 // send, we do it. so there's a process_handle_send and receive_can_frame
105 // that are just called continuously from the main loop. it's a waste of a
106 // few cpu cycles but it may be more natural than callbacks.
108 // what woudl a timer callback look like...it would need to pass the handle
109 // and that's all. seems like a context void* would be able to capture all
110 // of the information but arg, memory allocation. look at how it's done in
111 // the other library again
116 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
117 DiagnosticPidRequestType pid_request_type, uint16_t arbitration_id,
118 uint16_t pid, DiagnosticResponseReceived callback) {
119 DiagnosticRequest request = {
120 arbitration_id: arbitration_id,
121 mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
123 pid_length: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 1 : 2
126 return diagnostic_request(shims, &request, callback);
129 static bool handle_negative_response(IsoTpMessage* message,
130 DiagnosticResponse* response, DiagnosticShims* shims) {
131 bool response_was_negative = false;
132 if(response->mode == NEGATIVE_RESPONSE_MODE) {
133 response_was_negative = true;
134 if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
135 response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
138 if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
139 response->negative_response_code = message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
142 response->success = false;
143 response->completed = true;
145 return response_was_negative;
148 static bool handle_positive_response(DiagnosticRequestHandle* handle,
149 IsoTpMessage* message, DiagnosticResponse* response,
150 DiagnosticShims* shims) {
151 bool response_was_positive = false;
152 if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
153 response_was_positive = true;
154 // hide the "response" version of the mode from the user
156 response->mode = handle->request.mode;
157 response->has_pid = false;
158 if(handle->request.pid_length > 0 && message->size > 1) {
159 response->has_pid = true;
160 if(handle->request.pid_length == 2) {
161 response->pid = get_bitfield(message->payload, message->size,
162 PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
164 response->pid = message->payload[PID_BYTE_INDEX];
169 uint8_t payload_index = 1 + handle->request.pid_length;
170 response->payload_length = MAX(0, message->size - payload_index);
171 if(response->payload_length > 0) {
172 memcpy(response->payload, &message->payload[payload_index],
173 response->payload_length);
176 if((handle->request.pid_length == 0 && !response->has_pid)
177 || response->pid == handle->request.pid) {
178 response->success = true;
179 response->completed = true;
181 response_was_positive = false;
184 return response_was_positive;
187 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
188 DiagnosticRequestHandle* handle, const uint16_t arbitration_id,
189 const uint8_t data[], const uint8_t size) {
191 DiagnosticResponse response = {
192 arbitration_id: arbitration_id,
197 if(!handle->isotp_send_handle.completed) {
198 isotp_continue_send(&handle->isotp_shims,
199 &handle->isotp_send_handle, arbitration_id, data, size);
202 for(i = 0; i < handle->isotp_receive_handle_count; ++i) {
203 IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
204 &handle->isotp_receive_handles[i], arbitration_id, data, size);
206 // TODO as of now we're completing the handle as soon as one
207 // broadcast response is received....need to hang on for 100ms
208 if(message.completed) {
209 if(message.size > 0) {
210 response.mode = message.payload[0];
211 if(handle_negative_response(&message, &response, shims)) {
212 shims->log("Received a negative response to mode %d on arb ID 0x%x",
213 response.mode, response.arbitration_id);
214 handle->success = true;
215 handle->completed = true;
216 } else if(handle_positive_response(handle, &message, &response,
218 shims->log("Received a positive mode %d response on arb ID 0x%x",
219 response.mode, response.arbitration_id);
220 handle->success = true;
221 handle->completed = true;
223 shims->log("Response was for a mode 0x%x request (pid 0x%x), not our mode 0x%x request (pid 0x%x)",
224 MAX(0, response.mode - MODE_RESPONSE_OFFSET),
225 response.pid, handle->request.mode,
226 handle->request.pid);
227 // TODO just leave handles open until the user decides
228 // to be done with it - keep a count of valid responses
232 shims->log("Received an empty response on arb ID 0x%x",
233 response.arbitration_id);
236 if(handle->completed && handle->callback != NULL) {
237 handle->callback(&response);