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 handle->isotp_receive_handle = isotp_receive(&handle->isotp_shims,
34 handle->request.arbitration_id + ARBITRATION_ID_OFFSET,
39 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
40 DiagnosticRequest* request, DiagnosticResponseReceived callback) {
41 DiagnosticRequestHandle handle = {
48 uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
49 payload[MODE_BYTE_INDEX] = request->mode;
50 if(request->pid_length > 0) {
51 set_bitfield(request->pid, PID_BYTE_INDEX * CHAR_BIT,
52 request->pid_length * CHAR_BIT, payload, sizeof(payload));
54 if(request->payload_length > 0) {
55 memcpy(&payload[PID_BYTE_INDEX + request->pid_length],
56 request->payload, request->payload_length);
59 handle.isotp_shims = isotp_init_shims(shims->log,
60 shims->send_can_message,
63 handle.isotp_send_handle = isotp_send(&handle.isotp_shims,
64 request->arbitration_id, payload,
65 1 + request->payload_length + request->pid_length,
67 if(shims->log != NULL) {
68 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",
69 request->arbitration_id,
79 request->payload_length);
82 setup_receive_handle(&handle);
84 // TODO notes on multi frame:
85 // TODO what are the timers for exactly?
87 // when sending multi frame, send 1 frame, wait for a response
88 // if it says send all, send all right away
89 // if it says flow control, set the time for the next send
90 // instead of creating a timer with an async callback, add a process_handle
91 // function that's called repeatedly in the main loop - if it's time to
92 // send, we do it. so there's a process_handle_send and receive_can_frame
93 // that are just called continuously from the main loop. it's a waste of a
94 // few cpu cycles but it may be more natural than callbacks.
96 // what woudl a timer callback look like...it would need to pass the handle
97 // and that's all. seems like a context void* would be able to capture all
98 // of the information but arg, memory allocation. look at how it's done in
99 // the other library again
104 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
105 DiagnosticPidRequestType pid_request_type, uint16_t arbitration_id,
106 uint16_t pid, DiagnosticResponseReceived callback) {
107 DiagnosticRequest request = {
108 arbitration_id: arbitration_id,
109 mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
111 pid_length: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 1 : 2
114 return diagnostic_request(shims, &request, callback);
117 static bool handle_negative_response(IsoTpMessage* message,
118 DiagnosticResponse* response, DiagnosticShims* shims) {
119 bool response_was_negative = false;
120 if(response->mode == NEGATIVE_RESPONSE_MODE) {
121 response_was_negative = true;
122 if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
123 response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
126 if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
127 response->negative_response_code = message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
130 response->success = false;
131 response->completed = true;
133 return response_was_negative;
136 static bool handle_positive_response(DiagnosticRequestHandle* handle,
137 IsoTpMessage* message, DiagnosticResponse* response,
138 DiagnosticShims* shims) {
139 bool response_was_positive = false;
140 if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
141 response_was_positive = true;
142 // hide the "response" version of the mode from the user
144 response->mode = handle->request.mode;
145 bool has_pid = false;
146 if(handle->request.pid_length > 0 && message->size > 1) {
148 if(handle->request.pid_length == 2) {
149 response->pid = get_bitfield(message->payload, message->size,
150 PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
152 response->pid = message->payload[PID_BYTE_INDEX];
157 uint8_t payload_index = 1 + handle->request.pid_length;
158 response->payload_length = MAX(0, message->size - payload_index);
159 if(response->payload_length > 0) {
160 memcpy(response->payload, &message->payload[payload_index],
161 response->payload_length);
164 if((handle->request.pid_length == 0 && !has_pid)
165 || response->pid == handle->request.pid) {
166 response->success = true;
167 response->completed = true;
169 response_was_positive = false;
172 return response_was_positive;
175 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
176 DiagnosticRequestHandle* handle, const uint16_t arbitration_id,
177 const uint8_t data[], const uint8_t size) {
179 DiagnosticResponse response = {
180 arbitration_id: arbitration_id,
185 if(!handle->isotp_send_handle.completed) {
186 isotp_continue_send(&handle->isotp_shims,
187 &handle->isotp_send_handle, arbitration_id, data, size);
188 } else if(!handle->isotp_receive_handle.completed) {
189 IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
190 &handle->isotp_receive_handle, arbitration_id, data, size);
192 if(message.completed) {
193 if(message.size > 0) {
194 response.mode = message.payload[0];
195 if(handle_negative_response(&message, &response, shims)) {
196 shims->log("Received a negative response to mode %d on arb ID 0x%x",
197 response.mode, response.arbitration_id);
198 handle->success = true;
199 handle->completed = true;
200 } else if(handle_positive_response(handle, &message, &response,
202 shims->log("Received a positive mode %d response on arb ID 0x%x",
203 response.mode, response.arbitration_id);
204 handle->success = true;
205 handle->completed = true;
207 shims->log("Response was for a mode 0x%x request (pid 0x%x), not our mode 0x%x request (pid 0x%x)",
208 MAX(0, response.mode - MODE_RESPONSE_OFFSET),
209 response.pid, handle->request.mode,
210 handle->request.pid);
211 setup_receive_handle(handle);
214 shims->log("Received an empty response on arb ID 0x%x",
215 response.arbitration_id);
218 if(handle->completed && handle->callback != NULL) {
219 handle->callback(&response);
223 shims->log("Mode %d request to arb ID 0x%x is already completed",
224 handle->request.mode, handle->request.arbitration_id);