1a52db1e293e0821ce4b07d96f95b01ecdafcf78
[apps/agl-service-can-low-level.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 DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
53         DiagnosticRequest* request, DiagnosticResponseReceived callback) {
54     DiagnosticRequestHandle handle = {
55         request: *request,
56         callback: callback,
57         success: false,
58         completed: false
59     };
60
61     uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
62     payload[MODE_BYTE_INDEX] = request->mode;
63     if(request->pid_length > 0) {
64         set_bitfield(request->pid, PID_BYTE_INDEX * CHAR_BIT,
65                 request->pid_length * CHAR_BIT, payload, sizeof(payload));
66     }
67     if(request->payload_length > 0) {
68         memcpy(&payload[PID_BYTE_INDEX + request->pid_length],
69                 request->payload, request->payload_length);
70     }
71
72     handle.isotp_shims = isotp_init_shims(shims->log,
73             shims->send_can_message,
74             shims->set_timer);
75
76     handle.isotp_send_handle = isotp_send(&handle.isotp_shims,
77             request->arbitration_id, payload,
78             1 + request->payload_length + request->pid_length,
79             NULL);
80     if(shims->log != NULL) {
81         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",
82                 request->arbitration_id,
83                 request->mode,
84                 request->pid,
85                 request->payload[0],
86                 request->payload[1],
87                 request->payload[2],
88                 request->payload[3],
89                 request->payload[4],
90                 request->payload[5],
91                 request->payload[6],
92                 request->payload_length);
93     }
94
95     setup_receive_handle(&handle);
96
97     // TODO notes on multi frame:
98     // TODO what are the timers for exactly?
99     //
100     // when sending multi frame, send 1 frame, wait for a response
101     // if it says send all, send all right away
102     // if it says flow control, set the time for the next send
103     // instead of creating a timer with an async callback, add a process_handle
104     // function that's called repeatedly in the main loop - if it's time to
105     // send, we do it. so there's a process_handle_send and receive_can_frame
106     // that are just called continuously from the main loop. it's a waste of a
107     // few cpu cycles but it may be more  natural than callbacks.
108     //
109     // what woudl a timer callback look like...it would need to pass the handle
110     // and that's all. seems like a context void* would be able to capture all
111     // of the information but arg, memory allocation. look at how it's done in
112     // the other library again
113     //
114     return handle;
115 }
116
117 DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
118         DiagnosticPidRequestType pid_request_type, uint16_t arbitration_id,
119         uint16_t pid, DiagnosticResponseReceived callback) {
120     DiagnosticRequest request = {
121         arbitration_id: arbitration_id,
122         mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
123         pid: pid,
124         pid_length: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 1 : 2
125     };
126
127     return diagnostic_request(shims, &request, callback);
128 }
129
130 static bool handle_negative_response(IsoTpMessage* message,
131         DiagnosticResponse* response, DiagnosticShims* shims) {
132     bool response_was_negative = false;
133     if(response->mode == NEGATIVE_RESPONSE_MODE) {
134         response_was_negative = true;
135         if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
136             response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
137         }
138
139         if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
140             response->negative_response_code =
141                     message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
142         }
143
144         response->success = false;
145         response->completed = true;
146     }
147     return response_was_negative;
148 }
149
150 static bool handle_positive_response(DiagnosticRequestHandle* handle,
151         IsoTpMessage* message, DiagnosticResponse* response,
152         DiagnosticShims* shims) {
153     bool response_was_positive = false;
154     if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
155         response_was_positive = true;
156         // hide the "response" version of the mode from the user
157         // if it matched
158         response->mode = handle->request.mode;
159         response->has_pid = false;
160         if(handle->request.pid_length > 0 && message->size > 1) {
161             response->has_pid = true;
162             if(handle->request.pid_length == 2) {
163                 response->pid = get_bitfield(message->payload, message->size,
164                         PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
165             } else {
166                 response->pid = message->payload[PID_BYTE_INDEX];
167             }
168
169         }
170
171         uint8_t payload_index = 1 + handle->request.pid_length;
172         response->payload_length = MAX(0, message->size - payload_index);
173         if(response->payload_length > 0) {
174             memcpy(response->payload, &message->payload[payload_index],
175                     response->payload_length);
176         }
177
178         if((handle->request.pid_length == 0 && !response->has_pid)
179                 || response->pid == handle->request.pid) {
180             response->success = true;
181             response->completed = true;
182         } else {
183             response_was_positive = false;
184         }
185     }
186     return response_was_positive;
187 }
188
189 DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
190         DiagnosticRequestHandle* handle, const uint16_t arbitration_id,
191         const uint8_t data[], const uint8_t size) {
192
193     DiagnosticResponse response = {
194         arbitration_id: arbitration_id,
195         success: false,
196         completed: false
197     };
198
199     if(!handle->isotp_send_handle.completed) {
200         isotp_continue_send(&handle->isotp_shims,
201                 &handle->isotp_send_handle, arbitration_id, data, size);
202     } else {
203         uint8_t i;
204         for(i = 0; i < handle->isotp_receive_handle_count; ++i) {
205             IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
206                     &handle->isotp_receive_handles[i], arbitration_id, data,
207                     size);
208
209             if(message.completed) {
210                 if(message.size > 0) {
211                     response.mode = message.payload[0];
212                     if(handle_negative_response(&message, &response, shims)) {
213                         shims->log("Received a negative response to mode %d on arb ID 0x%x",
214                                 response.mode, response.arbitration_id);
215                         handle->success = true;
216                         handle->completed = true;
217                     } else if(handle_positive_response(handle, &message,
218                                 &response, shims)) {
219                         shims->log("Received a positive mode %d response on arb ID 0x%x",
220                                 response.mode, response.arbitration_id);
221                         handle->success = true;
222                         handle->completed = true;
223                     } else {
224                         shims->log("Response was for a mode 0x%x request (pid 0x%x), not our mode 0x%x request (pid 0x%x)",
225                                 MAX(0, response.mode - MODE_RESPONSE_OFFSET),
226                                 response.pid, handle->request.mode,
227                                 handle->request.pid);
228                         // TODO just leave handles open until the user decides
229                         // to be done with it - keep a count of valid responses
230                         // received.
231                     }
232                 } else {
233                     shims->log("Received an empty response on arb ID 0x%x",
234                             response.arbitration_id);
235                 }
236
237                 if(handle->completed && handle->callback != NULL) {
238                     handle->callback(&response);
239                 }
240
241                 // TODO as of now we're completing the handle as soon as one
242                 // broadcast response is received....need to hang on for 100ms
243                 break;
244             }
245         }
246     }
247     return response;
248 }
249
250 float diagnostic_payload_to_float(const DiagnosticResponse* response) {
251     return bitfield_parse_float(response->payload,
252             response->payload_length, 0,
253             response->payload_length * CHAR_BIT, 1.0, 0);
254 }