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