diff options
author | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2014-01-24 21:21:26 -0500 |
---|---|---|
committer | Christopher Peplin <chris.peplin@rhubarbtech.com> | 2014-01-24 21:21:26 -0500 |
commit | 295f3267b2c41bf28d054f2f43b528be38daac1c (patch) | |
tree | 727fe03f21ddddf548d8e7b9cfa162d9ef5aa87e /src | |
parent | c706e3a3896dbd9fb0477a715b1aab6ba28ed7ac (diff) |
Automatically set pid length for outgoing requests if not specified.
Diffstat (limited to 'src')
-rw-r--r-- | src/uds/uds.c | 25 | ||||
-rw-r--r-- | src/uds/uds_types.h | 8 |
2 files changed, 26 insertions, 7 deletions
diff --git a/src/uds/uds.c b/src/uds/uds.c index 3295aaaf..6a4ce451 100644 --- a/src/uds/uds.c +++ b/src/uds/uds.c @@ -49,6 +49,18 @@ static void setup_receive_handle(DiagnosticRequestHandle* handle) { } } +static uint16_t autoset_pid_length(uint8_t mode, uint16_t pid, + uint8_t pid_length) { + if(pid_length == 0) { + if(pid > 0xffff || mode > 10) { + pid_length = 2; + } else { + pid_length = 1; + } + } + return pid_length; +} + DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims, DiagnosticRequest* request, DiagnosticResponseReceived callback) { DiagnosticRequestHandle handle = { @@ -60,7 +72,10 @@ DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims, uint8_t payload[MAX_DIAGNOSTIC_PAYLOAD_SIZE] = {0}; payload[MODE_BYTE_INDEX] = request->mode; - if(request->pid_length > 0) { + 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)); } @@ -120,8 +135,8 @@ DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims, DiagnosticRequest request = { arbitration_id: arbitration_id, mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22, - pid: pid, - pid_length: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 1 : 2 + has_pid: true, + pid: pid }; return diagnostic_request(shims, &request, callback); @@ -157,7 +172,7 @@ static bool handle_positive_response(DiagnosticRequestHandle* handle, // if it matched response->mode = handle->request.mode; response->has_pid = false; - if(handle->request.pid_length > 0 && message->size > 1) { + if(handle->request.has_pid && message->size > 1) { response->has_pid = true; if(handle->request.pid_length == 2) { response->pid = get_bitfield(message->payload, message->size, @@ -175,7 +190,7 @@ static bool handle_positive_response(DiagnosticRequestHandle* handle, response->payload_length); } - if((handle->request.pid_length == 0 && !response->has_pid) + if((!handle->request.has_pid && !response->has_pid) || response->pid == handle->request.pid) { response->success = true; response->completed = true; diff --git a/src/uds/uds_types.h b/src/uds/uds_types.h index f8c7ef08..88c7ce44 100644 --- a/src/uds/uds_types.h +++ b/src/uds/uds_types.h @@ -33,9 +33,12 @@ typedef enum { * * arbitration_id - The arbitration ID to send the request. * mode - The OBD-II mode for the request. - * pid - (optional) The PID to request, if the mode requires one. + * has_pid - (optional) If the requests uses a PID, this should be true. + * pid - (optional) The PID to request, if the mode requires one. has_pid must + * be true. * pid_length - The length of the PID field, either 1 (standard) or 2 bytes - * (extended). + * (extended). If 0, it will be set automatically based on the request + * mode. * payload - (optional) The payload for the request, if the request requires * one. If payload_length is 0 this field is ignored. * payload_length - The length of the payload, or 0 if no payload is used. @@ -44,6 +47,7 @@ typedef enum { typedef struct { uint16_t arbitration_id; uint8_t mode; + bool has_pid; uint16_t pid; uint8_t pid_length; uint8_t payload[MAX_UDS_PAYLOAD_LENGTH]; |