Increase arb IDs to uint32_t to fit extended IDs.
[apps/low-level-can-service.git] / src / uds / uds.c
1 #include <uds/uds.h>
2 #include <bitfield/bitfield.h>
3 #include <canutil/read.h>
4 #include <string.h>
5 #include <limits.h>
6 #include <stddef.h>
7 #include <sys/param.h>
8
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
17
18 #ifndef MAX
19 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
20 #endif
21
22 DiagnosticShims diagnostic_init_shims(LogShim log,
23         SendCanMessageShim send_can_message,
24         SetTimerShim set_timer) {
25     DiagnosticShims shims = {
26         log: log,
27         send_can_message: send_can_message,
28         set_timer: set_timer
29     };
30     return shims;
31 }
32
33 static void setup_receive_handle(DiagnosticRequestHandle* handle) {
34     if(handle->request.arbitration_id == OBD2_FUNCTIONAL_BROADCAST_ID) {
35         uint32_t response_id;
36         for(response_id = 0;
37                 response_id < OBD2_FUNCTIONAL_RESPONSE_COUNT; ++response_id) {
38             handle->isotp_receive_handles[response_id] = isotp_receive(
39                     &handle->isotp_shims,
40                     OBD2_FUNCTIONAL_RESPONSE_START + response_id,
41                     NULL);
42         }
43         handle->isotp_receive_handle_count = OBD2_FUNCTIONAL_RESPONSE_COUNT;
44     } else {
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,
48                 NULL);
49     }
50 }
51
52 static uint16_t autoset_pid_length(uint8_t mode, uint16_t pid,
53         uint8_t pid_length) {
54     if(pid_length == 0) {
55         if(pid > 0xffff || mode > 10) {
56             pid_length = 2;
57         } else {
58             pid_length = 1;
59         }
60     }
61     return pid_length;
62 }
63
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,
74                 sizeof(payload));
75     }
76
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);
80     }
81
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,
85             NULL);
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);
91     }
92 }
93
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);
100 }
101
102 DiagnosticRequestHandle generate_diagnostic_request(DiagnosticShims* shims,
103         DiagnosticRequest* request, DiagnosticResponseReceived callback) {
104     DiagnosticRequestHandle handle = {
105         request: *request,
106         callback: callback,
107         success: false,
108         completed: false
109     };
110
111     handle.isotp_shims = isotp_init_shims(shims->log,
112             shims->send_can_message,
113             shims->set_timer);
114     handle.isotp_shims.frame_padding = !request->no_frame_padding;
115
116     return handle;
117     // TODO notes on multi frame:
118     // TODO what are the timers for exactly?
119     //
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.
128     //
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
133     //
134 }
135
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);
141     return handle;
142 }
143
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,
150         has_pid: true,
151         pid: pid
152     };
153
154     return diagnostic_request(shims, &request, callback);
155 }
156
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];
164         }
165
166         if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
167             response->negative_response_code =
168                     message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
169         }
170
171         response->success = false;
172         response->completed = true;
173     }
174     return response_was_negative;
175 }
176
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
184         // if it matched
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);
192             } else {
193                 response->pid = message->payload[PID_BYTE_INDEX];
194             }
195
196         }
197
198         if((!handle->request.has_pid && !response->has_pid)
199                 || response->pid == handle->request.pid) {
200             response->success = true;
201             response->completed = true;
202
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);
208             }
209         } else {
210             response_was_positive = false;
211         }
212     }
213     return response_was_positive;
214 }
215
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) {
219
220     DiagnosticResponse response = {
221         arbitration_id: arbitration_id,
222         success: false,
223         completed: false
224     };
225
226     if(!handle->isotp_send_handle.completed) {
227         isotp_continue_send(&handle->isotp_shims,
228                 &handle->isotp_send_handle, arbitration_id, data, size);
229     } else {
230         uint8_t i;
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,
234                     size);
235
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,
241                                 &response, shims)) {
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",
247                                     response_string);
248                         }
249
250                         handle->success = true;
251                         handle->completed = true;
252                     }
253                 } else {
254                     if(shims->log != NULL) {
255                         shims->log("Received an empty response on arb ID 0x%x",
256                                 response.arbitration_id);
257                     }
258                 }
259
260                 if(handle->completed && handle->callback != NULL) {
261                     handle->callback(&response);
262                 }
263
264                 break;
265             }
266         }
267     }
268     return response;
269 }
270
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);
274 }
275
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) {
279         case 0xa:
280             return response->payload[0] * 3;
281         case 0xc:
282             return (response->payload[0] * 256 + response->payload[1]) / 4.0;
283         case 0xd:
284         case 0x33:
285         case 0xb:
286             return response->payload[0];
287         case 0x10:
288             return (response->payload[0] * 256 + response->payload[1]) / 100.0;
289         case 0x11:
290         case 0x2f:
291         case 0x45:
292         case 0x4c:
293         case 0x52:
294         case 0x5a:
295         case 0x4:
296             return response->payload[0] * 100.0 / 255.0;
297         case 0x46:
298         case 0x5c:
299         case 0xf:
300         case 0x5:
301             return response->payload[0] - 40;
302         case 0x62:
303             return response->payload[0] - 125;
304         default:
305             return 0;
306     }
307 }
308
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,
314             response->mode);
315
316     if(response->has_pid) {
317         bytes_used += snprintf(destination + bytes_used,
318                 destination_length - bytes_used,
319                 "pid: 0x%x, ",
320                 response->pid);
321     }
322
323     if(!response->success) {
324         bytes_used += snprintf(destination + bytes_used,
325                 destination_length - bytes_used,
326                 "nrc: 0x%x, ",
327                 response->negative_response_code);
328     }
329
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]);
340     } else {
341         snprintf(destination + bytes_used, destination_length - bytes_used,
342                 "no payload");
343     }
344 }
345
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,
351             request->mode);
352
353     if(request->has_pid) {
354         bytes_used += snprintf(destination + bytes_used,
355                 destination_length - bytes_used,
356                 "pid: 0x%x, ",
357                 request->pid);
358     }
359
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",
364                 request->payload[0],
365                 request->payload[1],
366                 request->payload[2],
367                 request->payload[3],
368                 request->payload[4],
369                 request->payload[5],
370                 request->payload[6]);
371     } else {
372         snprintf(destination + bytes_used, remaining_space, "no payload");
373     }
374 }
375
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;
382     return equals;
383 }