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) {
34 for(uint16_t response_id = 0;
35 response_id < OBD2_FUNCTIONAL_RESPONSE_COUNT; ++response_id) {
36 handle->isotp_receive_handles[response_id] = isotp_receive(
37 &handle->isotp_shims, OBD2_FUNCTIONAL_RESPONSE_START + response_id,
40 handle->isotp_receive_handle_count = OBD2_FUNCTIONAL_RESPONSE_COUNT;
42 handle->isotp_receive_handle_count = 1;
43 handle->isotp_receive_handles[0] = isotp_receive(&handle->isotp_shims,
44 handle->request.arbitration_id + ARBITRATION_ID_OFFSET,
50 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
51 DiagnosticRequest* request, DiagnosticResponseReceived callback) {
52 DiagnosticRequestHandle handle = {
59 uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
60 payload[MODE_BYTE_INDEX] = request->mode;
61 if(request->pid_length > 0) {
62 set_bitfield(request->pid, PID_BYTE_INDEX * CHAR_BIT,
63 request->pid_length * CHAR_BIT, payload, sizeof(payload));
65 if(request->payload_length > 0) {
66 memcpy(&payload[PID_BYTE_INDEX + request->pid_length],
67 request->payload, request->payload_length);
70 handle.isotp_shims = isotp_init_shims(shims->log,
71 shims->send_can_message,
74 handle.isotp_send_handle = isotp_send(&handle.isotp_shims,
75 request->arbitration_id, payload,
76 1 + request->payload_length + request->pid_length,
78 if(shims->log != NULL) {
79 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",
80 request->arbitration_id,
90 request->payload_length);
93 setup_receive_handle(&handle);
95 // TODO notes on multi frame:
96 // TODO what are the timers for exactly?
98 // when sending multi frame, send 1 frame, wait for a response
99 // if it says send all, send all right away
100 // if it says flow control, set the time for the next send
101 // instead of creating a timer with an async callback, add a process_handle
102 // function that's called repeatedly in the main loop - if it's time to
103 // send, we do it. so there's a process_handle_send and receive_can_frame
104 // that are just called continuously from the main loop. it's a waste of a
105 // few cpu cycles but it may be more natural than callbacks.
107 // what woudl a timer callback look like...it would need to pass the handle
108 // and that's all. seems like a context void* would be able to capture all
109 // of the information but arg, memory allocation. look at how it's done in
110 // the other library again
115 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
116 DiagnosticPidRequestType pid_request_type, uint16_t arbitration_id,
117 uint16_t pid, DiagnosticResponseReceived callback) {
118 DiagnosticRequest request = {
119 arbitration_id: arbitration_id,
120 mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
122 pid_length: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 1 : 2
125 return diagnostic_request(shims, &request, callback);
128 static bool handle_negative_response(IsoTpMessage* message,
129 DiagnosticResponse* response, DiagnosticShims* shims) {
130 bool response_was_negative = false;
131 if(response->mode == NEGATIVE_RESPONSE_MODE) {
132 response_was_negative = true;
133 if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
134 response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
137 if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
138 response->negative_response_code = message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
141 response->success = false;
142 response->completed = true;
144 return response_was_negative;
147 static bool handle_positive_response(DiagnosticRequestHandle* handle,
148 IsoTpMessage* message, DiagnosticResponse* response,
149 DiagnosticShims* shims) {
150 bool response_was_positive = false;
151 if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
152 response_was_positive = true;
153 // hide the "response" version of the mode from the user
155 response->mode = handle->request.mode;
156 bool has_pid = false;
157 if(handle->request.pid_length > 0 && message->size > 1) {
159 if(handle->request.pid_length == 2) {
160 response->pid = get_bitfield(message->payload, message->size,
161 PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
163 response->pid = message->payload[PID_BYTE_INDEX];
168 uint8_t payload_index = 1 + handle->request.pid_length;
169 response->payload_length = MAX(0, message->size - payload_index);
170 if(response->payload_length > 0) {
171 memcpy(response->payload, &message->payload[payload_index],
172 response->payload_length);
175 if((handle->request.pid_length == 0 && !has_pid)
176 || response->pid == handle->request.pid) {
177 response->success = true;
178 response->completed = true;
180 response_was_positive = false;
183 return response_was_positive;
186 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
187 DiagnosticRequestHandle* handle, const uint16_t arbitration_id,
188 const uint8_t data[], const uint8_t size) {
190 DiagnosticResponse response = {
191 arbitration_id: arbitration_id,
196 if(!handle->isotp_send_handle.completed) {
197 isotp_continue_send(&handle->isotp_shims,
198 &handle->isotp_send_handle, arbitration_id, data, size);
200 for(uint8_t i = 0; i < handle->isotp_receive_handle_count; ++i) {
201 IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
202 &handle->isotp_receive_handles[i], arbitration_id, data, size);
204 // TODO as of now we're completing the handle as soon as one
205 // broadcast response is received....need to hang on for 100ms
206 if(message.completed) {
207 if(message.size > 0) {
208 response.mode = message.payload[0];
209 if(handle_negative_response(&message, &response, shims)) {
210 shims->log("Received a negative response to mode %d on arb ID 0x%x",
211 response.mode, response.arbitration_id);
212 handle->success = true;
213 handle->completed = true;
214 } else if(handle_positive_response(handle, &message, &response,
216 shims->log("Received a positive mode %d response on arb ID 0x%x",
217 response.mode, response.arbitration_id);
218 handle->success = true;
219 handle->completed = true;
221 shims->log("Response was for a mode 0x%x request (pid 0x%x), not our mode 0x%x request (pid 0x%x)",
222 MAX(0, response.mode - MODE_RESPONSE_OFFSET),
223 response.pid, handle->request.mode,
224 handle->request.pid);
225 // TODO just leave handles open until the user decides
226 // to be done with it - keep a count of valid responses
230 shims->log("Received an empty response on arb ID 0x%x",
231 response.arbitration_id);
234 if(handle->completed && handle->callback != NULL) {
235 handle->callback(&response);