summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristopher Peplin <chris.peplin@rhubarbtech.com>2014-02-26 16:34:35 -0500
committerChristopher Peplin <chris.peplin@rhubarbtech.com>2014-02-26 16:34:35 -0500
commit9a943761ae920653dd8fe6e7fedfc58bd50c7938 (patch)
tree9c0576b271aeb8610581dded98bb7fb65a7419a9 /src
parentce9aaed64f1c180c5d39356abb08cd86ae06b3c4 (diff)
Optionally split up preparing a diag request and actually sending it.
Diffstat (limited to 'src')
-rw-r--r--src/uds/uds.c83
-rw-r--r--src/uds/uds.h32
2 files changed, 82 insertions, 33 deletions
diff --git a/src/uds/uds.c b/src/uds/uds.c
index 4e237446..7303677a 100644
--- a/src/uds/uds.c
+++ b/src/uds/uds.c
@@ -61,7 +61,45 @@ static uint16_t autoset_pid_length(uint8_t mode, uint16_t pid,
return pid_length;
}
-DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
+static void send_diagnostic_request(DiagnosticShims* shims,
+ DiagnosticRequestHandle* handle) {
+ uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
+ payload[MODE_BYTE_INDEX] = handle->request.mode;
+ if(handle->request.has_pid) {
+ handle->request.pid_length = autoset_pid_length(handle->request.mode,
+ handle->request.pid, handle->request.pid_length);
+ handle->request.pid_length = handle->request.pid_length;
+ set_bitfield(handle->request.pid, PID_BYTE_INDEX * CHAR_BIT,
+ handle->request.pid_length * CHAR_BIT, payload,
+ sizeof(payload));
+ }
+
+ if(handle->request.payload_length > 0) {
+ memcpy(&payload[PID_BYTE_INDEX + handle->request.pid_length],
+ handle->request.payload, handle->request.payload_length);
+ }
+
+ handle->isotp_send_handle = isotp_send(&handle->isotp_shims,
+ handle->request.arbitration_id, payload,
+ 1 + handle->request.payload_length + handle->request.pid_length,
+ NULL);
+ if(shims->log != NULL) {
+ char request_string[128] = {0};
+ diagnostic_request_to_string(&handle->request, request_string,
+ sizeof(request_string));
+ shims->log("Sending diagnostic request: %s", request_string);
+ }
+}
+
+void start_diagnostic_request(DiagnosticShims* shims,
+ DiagnosticRequestHandle* handle) {
+ handle->success = false;
+ handle->completed = false;
+ send_diagnostic_request(shims, handle);
+ setup_receive_handle(handle);
+}
+
+DiagnosticRequestHandle generate_diagnostic_request(DiagnosticShims* shims,
DiagnosticRequest* request, DiagnosticResponseReceived callback) {
DiagnosticRequestHandle handle = {
request: *request,
@@ -70,37 +108,12 @@ DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
completed: false
};
- uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0};
- payload[MODE_BYTE_INDEX] = request->mode;
- if(request->has_pid) {
- request->pid_length = autoset_pid_length(request->mode,
- request->pid, request->pid_length);
- handle.request.pid_length = request->pid_length;
- set_bitfield(request->pid, PID_BYTE_INDEX * CHAR_BIT,
- request->pid_length * CHAR_BIT, payload, sizeof(payload));
- }
- if(request->payload_length > 0) {
- memcpy(&payload[PID_BYTE_INDEX + request->pid_length],
- request->payload, request->payload_length);
- }
-
handle.isotp_shims = isotp_init_shims(shims->log,
shims->send_can_message,
shims->set_timer);
handle.isotp_shims.frame_padding = !request->no_frame_padding;
- handle.isotp_send_handle = isotp_send(&handle.isotp_shims,
- request->arbitration_id, payload,
- 1 + request->payload_length + request->pid_length,
- NULL);
- if(shims->log != NULL) {
- char request_string[128] = {0};
- diagnostic_request_to_string(request, request_string, sizeof(request_string));
- shims->log("Sending diagnostic request: %s", request_string);
- }
-
- setup_receive_handle(&handle);
-
+ return handle;
// TODO notes on multi frame:
// TODO what are the timers for exactly?
//
@@ -118,6 +131,13 @@ DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
// of the information but arg, memory allocation. look at how it's done in
// the other library again
//
+}
+
+DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
+ DiagnosticRequest* request, DiagnosticResponseReceived callback) {
+ DiagnosticRequestHandle handle = generate_diagnostic_request(
+ shims, request, callback);
+ start_diagnostic_request(shims, &handle);
return handle;
}
@@ -217,11 +237,14 @@ DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
if(message.size > 0) {
response.mode = message.payload[0];
if(handle_negative_response(&message, &response, shims) ||
- handle_positive_response(handle, &message, &response, shims)) {
+ handle_positive_response(handle, &message,
+ &response, shims)) {
if(shims->log != NULL) {
char response_string[128] = {0};
- diagnostic_response_to_string(&response, response_string, sizeof(response_string));
- shims->log("Diagnostic response received: %s", response_string);
+ diagnostic_response_to_string(&response,
+ response_string, sizeof(response_string));
+ shims->log("Diagnostic response received: %s",
+ response_string);
}
handle->success = true;
diff --git a/src/uds/uds.h b/src/uds/uds.h
index 0d40561a..22ff36af 100644
--- a/src/uds/uds.h
+++ b/src/uds/uds.h
@@ -26,8 +26,8 @@ DiagnosticShims diagnostic_init_shims(LogShim log,
SendCanMessageShim send_can_message,
SetTimerShim set_timer);
-/* Public: Initiate a diagnostic request and return a handle, ready to completly
- * send the request and process the response via
+/* Public: Generate a new diagnostic request, send the first CAN message frame
+ * and set up the handle required to process the response via
* diagnostic_receive_can_frame(...).
*
* shims - Low-level shims required to send CAN messages, etc.
@@ -38,11 +38,37 @@ DiagnosticShims diagnostic_init_shims(LogShim log,
* Returns a handle to be used with diagnostic_receive_can_frame to complete
* sending the request and receive the response. The 'completed' field in the
* returned DiagnosticRequestHandle will be true when the message is completely
- * sent.
+ * sent. The first frame of the message will already be sent.
*/
DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
DiagnosticRequest* request, DiagnosticResponseReceived callback);
+/* Public: Generate the handle for a new diagnostic request, but do not send any
+ * data to CAN yet - you must call start_diagnostic_request(...) on the handle
+ * returned from this function actually kick off the request.
+ *
+ * shims - Low-level shims required to send CAN messages, etc.
+ * request -
+ * callback - an optional function to be called when the response is receved
+ * (use NULL if no callback is required).
+ *
+ * Returns a handle to be used with start_diagnostic_request and then
+ * diagnostic_receive_can_frame to complete sending the request and receive the
+ * response. The 'completed' field in the returned DiagnosticRequestHandle will
+ * be true when the message is completely sent.
+ */
+DiagnosticRequestHandle generate_diagnostic_request(DiagnosticShims* shims,
+ DiagnosticRequest* request, DiagnosticResponseReceived callback);
+
+/* Public: Send the first frame of the request to CAN for the handle, generated
+ * by generate_diagnostic_request.
+ *
+ * You can also call this method to re-do the request for a handle that has
+ * already completed.
+ */
+void start_diagnostic_request(DiagnosticShims* shims,
+ DiagnosticRequestHandle* handle);
+
/* Public: Request a PID from the given arbitration ID, determining the mode
* automatically based on the PID type.
*