636bbfca1aeb774f5e236e89307f9bc784750cd5
[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 #include <inttypes.h>
9
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
18
19 #ifndef MAX
20 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
21 #endif
22
23 DiagnosticShims diagnostic_init_shims(LogShim log,
24         SendCanMessageShim send_can_message,
25         SetTimerShim set_timer) {
26     DiagnosticShims shims = {
27         log: log,
28         send_can_message: send_can_message,
29         set_timer: set_timer
30     };
31     return shims;
32 }
33
34 static void setup_receive_handle(DiagnosticRequestHandle* handle) {
35     if(handle->request.arbitration_id == OBD2_FUNCTIONAL_BROADCAST_ID) {
36         uint32_t response_id;
37         for(response_id = 0;
38                 response_id < OBD2_FUNCTIONAL_RESPONSE_COUNT; ++response_id) {
39             handle->isotp_receive_handles[response_id] = isotp_receive(
40                     &handle->isotp_shims,
41                     OBD2_FUNCTIONAL_RESPONSE_START + response_id,
42                     NULL);
43         }
44         handle->isotp_receive_handle_count = OBD2_FUNCTIONAL_RESPONSE_COUNT;
45     } else {
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,
49                 NULL);
50     }
51 }
52
53 static uint16_t autoset_pid_length(uint8_t mode, uint16_t pid,
54         uint8_t pid_length) {
55     if(pid_length == 0) {
56         if(pid > 0xffff || (mode != 0x3e && mode > 0xa)) {
57             pid_length = 2;
58         } else {
59             pid_length = 1;
60         }
61     }
62     return pid_length;
63 }
64
65 static void send_diagnostic_request(DiagnosticShims* shims,
66         DiagnosticRequestHandle* handle) {
67     uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
68     payload[MODE_BYTE_INDEX] = handle->request.mode;
69     if(handle->request.has_pid) {
70         handle->request.pid_length = autoset_pid_length(handle->request.mode,
71                 handle->request.pid, handle->request.pid_length);
72         handle->request.pid_length = handle->request.pid_length;
73         set_bitfield(handle->request.pid, PID_BYTE_INDEX * CHAR_BIT,
74                 handle->request.pid_length * CHAR_BIT, payload,
75                 sizeof(payload));
76     }
77
78     if(handle->request.payload_length > 0) {
79         memcpy(&payload[PID_BYTE_INDEX + handle->request.pid_length],
80                 handle->request.payload, handle->request.payload_length);
81     }
82
83     handle->isotp_send_handle = isotp_send(&handle->isotp_shims,
84             handle->request.arbitration_id, payload,
85             1 + handle->request.payload_length + handle->request.pid_length,
86             NULL);
87     if(handle->isotp_send_handle.completed &&
88             !handle->isotp_send_handle.success) {
89         handle->completed = true;
90         handle->success = false;
91         if(shims->log != NULL) {
92             shims->log("%s", "Diagnostic request not sent");
93         }
94     } else if(shims->log != NULL) {
95         char request_string[128] = {0};
96         diagnostic_request_to_string(&handle->request, request_string,
97                 sizeof(request_string));
98         shims->log("Sending diagnostic request: %s", request_string);
99     }
100 }
101
102 bool diagnostic_request_sent(DiagnosticRequestHandle* handle) {
103     return handle->isotp_send_handle.completed;
104 }
105
106 void start_diagnostic_request(DiagnosticShims* shims,
107         DiagnosticRequestHandle* handle) {
108     handle->success = false;
109     handle->completed = false;
110     send_diagnostic_request(shims, handle);
111     if(!handle->completed) {
112         setup_receive_handle(handle);
113     }
114 }
115
116 DiagnosticRequestHandle generate_diagnostic_request(DiagnosticShims* shims,
117         DiagnosticRequest* request, DiagnosticResponseReceived callback) {
118     DiagnosticRequestHandle handle = {
119         request: *request,
120         callback: callback,
121         success: false,
122         completed: false
123     };
124
125     handle.isotp_shims = isotp_init_shims(shims->log,
126             shims->send_can_message,
127             shims->set_timer);
128     handle.isotp_shims.frame_padding = !request->no_frame_padding;
129
130     return handle;
131     // TODO notes on multi frame:
132     // TODO what are the timers for exactly?
133     //
134     // when sending multi frame, send 1 frame, wait for a response
135     // if it says send all, send all right away
136     // if it says flow control, set the time for the next send
137     // instead of creating a timer with an async callback, add a process_handle
138     // function that's called repeatedly in the main loop - if it's time to
139     // send, we do it. so there's a process_handle_send and receive_can_frame
140     // that are just called continuously from the main loop. it's a waste of a
141     // few cpu cycles but it may be more  natural than callbacks.
142     //
143     // what would a timer callback look like...it would need to pass the handle
144     // and that's all. seems like a context void* would be able to capture all
145     // of the information but arg, memory allocation. look at how it's done in
146     // the other library again
147     //
148 }
149
150 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
151         DiagnosticRequest* request, DiagnosticResponseReceived callback) {
152     DiagnosticRequestHandle handle = generate_diagnostic_request(
153             shims, request, callback);
154     start_diagnostic_request(shims, &handle);
155     return handle;
156 }
157
158 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
159         DiagnosticPidRequestType pid_request_type, uint32_t arbitration_id,
160         uint16_t pid, DiagnosticResponseReceived callback) {
161     DiagnosticRequest request = {
162         arbitration_id: arbitration_id,
163         mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
164         has_pid: true,
165         pid: pid
166     };
167
168     return diagnostic_request(shims, &request, callback);
169 }
170
171 static bool handle_negative_response(IsoTpMessage* message,
172         DiagnosticResponse* response, DiagnosticShims* shims) {
173     bool response_was_negative = false;
174     if(response->mode == NEGATIVE_RESPONSE_MODE) {
175         response_was_negative = true;
176         if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
177             response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
178         }
179
180         if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
181             response->negative_response_code =
182                     message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
183         }
184
185         response->success = false;
186         response->completed = true;
187     }
188     return response_was_negative;
189 }
190
191 static bool handle_positive_response(DiagnosticRequestHandle* handle,
192         IsoTpMessage* message, DiagnosticResponse* response,
193         DiagnosticShims* shims) {
194     bool response_was_positive = false;
195     if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
196         response_was_positive = true;
197         // hide the "response" version of the mode from the user
198         // if it matched
199         response->mode = handle->request.mode;
200         response->has_pid = false;
201         if(handle->request.has_pid && message->size > 1) {
202             response->has_pid = true;
203             if(handle->request.pid_length == 2) {
204                 response->pid = get_bitfield(message->payload, message->size,
205                         PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
206             } else {
207                 response->pid = message->payload[PID_BYTE_INDEX];
208             }
209
210         }
211
212         if((!handle->request.has_pid && !response->has_pid)
213                 || response->pid == handle->request.pid) {
214             response->success = true;
215             response->completed = true;
216
217             uint8_t payload_index = 1 + handle->request.pid_length;
218             response->payload_length = MAX(0, message->size - payload_index);
219             if(response->payload_length > 0) {
220                 memcpy(response->payload, &message->payload[payload_index],
221                         response->payload_length);
222             }
223         } else {
224             response_was_positive = false;
225         }
226     }
227     return response_was_positive;
228 }
229
230 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
231         DiagnosticRequestHandle* handle, const uint32_t arbitration_id,
232         const uint8_t data[], const uint8_t size) {
233
234     DiagnosticResponse response = {
235         arbitration_id: arbitration_id,
236         success: false,
237         completed: false
238     };
239
240     if(!handle->isotp_send_handle.completed) {
241         isotp_continue_send(&handle->isotp_shims,
242                 &handle->isotp_send_handle, arbitration_id, data, size);
243     } else {
244         uint8_t i;
245         for(i = 0; i < handle->isotp_receive_handle_count; ++i) {
246             IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
247                     &handle->isotp_receive_handles[i], arbitration_id, data,
248                     size);
249
250             if(message.completed) {
251                 if(message.size > 0) {
252                     response.mode = message.payload[0];
253                     if(handle_negative_response(&message, &response, shims) ||
254                             handle_positive_response(handle, &message,
255                                 &response, shims)) {
256                         if(shims->log != NULL) {
257                             char response_string[128] = {0};
258                             diagnostic_response_to_string(&response,
259                                     response_string, sizeof(response_string));
260                             shims->log("Diagnostic response received: %s",
261                                     response_string);
262                         }
263
264                         handle->success = true;
265                         handle->completed = true;
266                     }
267                 } else {
268                     if(shims->log != NULL) {
269                         shims->log("Received an empty response on arb ID 0x%x",
270                                 response.arbitration_id);
271                     }
272                 }
273
274                 if(handle->completed && handle->callback != NULL) {
275                     handle->callback(&response);
276                 }
277
278                 break;
279             }
280         }
281     }
282     return response;
283 }
284
285 int diagnostic_payload_to_integer(const DiagnosticResponse* response) {
286     return get_bitfield(response->payload, response->payload_length, 0,
287             response->payload_length * CHAR_BIT);
288 }
289
290 float diagnostic_decode_obd2_pid(const DiagnosticResponse* response) {
291     // handles on the single number values, not the bit encoded ones
292     switch(response->pid) {
293         case 0xa:
294             return response->payload[0] * 3;
295         case 0xc:
296             return (response->payload[0] * 256 + response->payload[1]) / 4.0;
297         case 0xd:
298         case 0x33:
299         case 0xb:
300             return response->payload[0];
301         case 0x10:
302             return (response->payload[0] * 256 + response->payload[1]) / 100.0;
303         case 0x11:
304         case 0x2f:
305         case 0x45:
306         case 0x4c:
307         case 0x52:
308         case 0x5a:
309         case 0x4:
310             return response->payload[0] * 100.0 / 255.0;
311         case 0x46:
312         case 0x5c:
313         case 0xf:
314         case 0x5:
315             return response->payload[0] - 40;
316         case 0x62:
317             return response->payload[0] - 125;
318         default:
319             return diagnostic_payload_to_integer(response);
320     }
321 }
322
323 void diagnostic_response_to_string(const DiagnosticResponse* response,
324         char* destination, size_t destination_length) {
325     int bytes_used = snprintf(destination, destination_length,
326             "arb_id: 0x%lx, mode: 0x%x, ",
327             (unsigned long) response->arbitration_id,
328             response->mode);
329
330     if(response->has_pid) {
331         bytes_used += snprintf(destination + bytes_used,
332                 destination_length - bytes_used,
333                 "pid: 0x%x, ",
334                 response->pid);
335     }
336
337     if(!response->success) {
338         bytes_used += snprintf(destination + bytes_used,
339                 destination_length - bytes_used,
340                 "nrc: 0x%x, ",
341                 response->negative_response_code);
342     }
343
344     if(response->payload_length > 0) {
345         snprintf(destination + bytes_used, destination_length - bytes_used,
346                 "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
347                 response->payload[0],
348                 response->payload[1],
349                 response->payload[2],
350                 response->payload[3],
351                 response->payload[4],
352                 response->payload[5],
353                 response->payload[6]);
354     } else {
355         snprintf(destination + bytes_used, destination_length - bytes_used,
356                 "no payload");
357     }
358 }
359
360 void diagnostic_request_to_string(const DiagnosticRequest* request,
361         char* destination, size_t destination_length) {
362     int bytes_used = snprintf(destination, destination_length,
363             "arb_id: 0x%lx, mode: 0x%x, ",
364             (unsigned long) request->arbitration_id,
365             request->mode);
366
367     if(request->has_pid) {
368         bytes_used += snprintf(destination + bytes_used,
369                 destination_length - bytes_used,
370                 "pid: 0x%x, ",
371                 request->pid);
372     }
373
374     int remaining_space = destination_length - bytes_used;
375     if(request->payload_length > 0) {
376         snprintf(destination + bytes_used, remaining_space,
377                 "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
378                 request->payload[0],
379                 request->payload[1],
380                 request->payload[2],
381                 request->payload[3],
382                 request->payload[4],
383                 request->payload[5],
384                 request->payload[6]);
385     } else {
386         snprintf(destination + bytes_used, remaining_space, "no payload");
387     }
388 }
389
390 bool diagnostic_request_equals(const DiagnosticRequest* ours,
391         const DiagnosticRequest* theirs) {
392     bool equals = ours->arbitration_id == theirs->arbitration_id &&
393         ours->mode == theirs->mode;
394     equals &= ours->has_pid == theirs->has_pid;
395     equals &= ours->pid == theirs->pid;
396     return equals;
397 }