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