aboutsummaryrefslogtreecommitdiffstats
path: root/CAN-binder/libs
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-05-02 17:52:11 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-05-02 17:52:11 +0200
commit0242c26c2f5dc96387bca7efb118364c800f4ee7 (patch)
treeba0a67a78004a45afd158a3ca3fb01c6fc012e06 /CAN-binder/libs
parent3102ec9ce009d0f28355c5b7df9c5bd5013e6e75 (diff)
parentca20db3dd978871bbb9f01f3c862b510c03d1dc4 (diff)
Add 'CAN-binder/libs/uds-c/' from commit 'ca20db3dd978871bbb9f01f3c862b510c03d1dc4'
git-subtree-dir: CAN-binder/libs/uds-c git-subtree-mainline: 3102ec9ce009d0f28355c5b7df9c5bd5013e6e75 git-subtree-split: ca20db3dd978871bbb9f01f3c862b510c03d1dc4
Diffstat (limited to 'CAN-binder/libs')
-rw-r--r--CAN-binder/libs/uds-c/.gitignore5
-rw-r--r--CAN-binder/libs/uds-c/.gitmodules6
-rw-r--r--CAN-binder/libs/uds-c/.travis.yml8
-rw-r--r--CAN-binder/libs/uds-c/CHANGELOG.mkd9
-rw-r--r--CAN-binder/libs/uds-c/LICENSE24
-rw-r--r--CAN-binder/libs/uds-c/Makefile56
-rw-r--r--CAN-binder/libs/uds-c/README.mkd196
m---------CAN-binder/libs/uds-c/deps/bitfield-c6
m---------CAN-binder/libs/uds-c/deps/isotp-c7
-rw-r--r--CAN-binder/libs/uds-c/runtests.sh17
-rw-r--r--CAN-binder/libs/uds-c/src/uds/extras.c39
-rw-r--r--CAN-binder/libs/uds-c/src/uds/extras.h71
-rw-r--r--CAN-binder/libs/uds-c/src/uds/uds.c400
-rw-r--r--CAN-binder/libs/uds-c/src/uds/uds.h165
-rw-r--r--CAN-binder/libs/uds-c/src/uds/uds_types.h200
-rw-r--r--CAN-binder/libs/uds-c/tests/common.c43
-rw-r--r--CAN-binder/libs/uds-c/tests/test_core.c526
17 files changed, 1778 insertions, 0 deletions
diff --git a/CAN-binder/libs/uds-c/.gitignore b/CAN-binder/libs/uds-c/.gitignore
new file mode 100644
index 0000000..834a305
--- /dev/null
+++ b/CAN-binder/libs/uds-c/.gitignore
@@ -0,0 +1,5 @@
+*.o
+.DS_Store
+*~
+*.bin
+build
diff --git a/CAN-binder/libs/uds-c/.gitmodules b/CAN-binder/libs/uds-c/.gitmodules
new file mode 100644
index 0000000..d9c0194
--- /dev/null
+++ b/CAN-binder/libs/uds-c/.gitmodules
@@ -0,0 +1,6 @@
+[submodule "deps/bitfield-c"]
+ path = deps/bitfield-c
+ url = https://github.com/openxc/bitfield-c
+[submodule "deps/isotp-c"]
+ path = deps/isotp-c
+ url = https://github.com/openxc/isotp-c
diff --git a/CAN-binder/libs/uds-c/.travis.yml b/CAN-binder/libs/uds-c/.travis.yml
new file mode 100644
index 0000000..31bfdeb
--- /dev/null
+++ b/CAN-binder/libs/uds-c/.travis.yml
@@ -0,0 +1,8 @@
+language: c
+compiler:
+ - gcc
+script: make test
+before_install:
+ - git submodule update --init
+ - sudo apt-get update -qq
+ - sudo apt-get install check
diff --git a/CAN-binder/libs/uds-c/CHANGELOG.mkd b/CAN-binder/libs/uds-c/CHANGELOG.mkd
new file mode 100644
index 0000000..96eaa17
--- /dev/null
+++ b/CAN-binder/libs/uds-c/CHANGELOG.mkd
@@ -0,0 +1,9 @@
+# OBD-II Support Library in C
+
+## v0.2
+
+* Add support for multi-frame diagnostic responses.
+
+## v0.1
+
+* Initial release
diff --git a/CAN-binder/libs/uds-c/LICENSE b/CAN-binder/libs/uds-c/LICENSE
new file mode 100644
index 0000000..330d61f
--- /dev/null
+++ b/CAN-binder/libs/uds-c/LICENSE
@@ -0,0 +1,24 @@
+Copyright (c) 2013 Ford Motor Company
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/CAN-binder/libs/uds-c/Makefile b/CAN-binder/libs/uds-c/Makefile
new file mode 100644
index 0000000..4adf3ce
--- /dev/null
+++ b/CAN-binder/libs/uds-c/Makefile
@@ -0,0 +1,56 @@
+CC = gcc
+INCLUDES = -Isrc -Ideps/bitfield-c/src -Ideps/isotp-c/src
+CFLAGS = $(INCLUDES) -c -Wall -Werror -g -ggdb -std=gnu99 -coverage
+LDFLAGS = -coverage -lm
+LDLIBS = -lcheck
+
+TEST_DIR = tests
+TEST_OBJDIR = build
+
+# Guard against \r\n line endings only in Cygwin
+OSTYPE := $(shell uname)
+ifneq ($(OSTYPE),Darwin)
+ OSTYPE := $(shell uname -o)
+ ifeq ($(OSTYPE),Cygwin)
+ TEST_SET_OPTS = igncr
+ endif
+endif
+
+SRC = $(wildcard src/**/*.c)
+SRC += $(wildcard deps/bitfield-c/src/**/*.c)
+SRC += $(wildcard deps/isotp-c/src/**/*.c)
+OBJS = $(patsubst %,$(TEST_OBJDIR)/%,$(SRC:.c=.o))
+TEST_SRC = $(wildcard $(TEST_DIR)/test_*.c)
+TESTS=$(patsubst %.c,$(TEST_OBJDIR)/%.bin,$(TEST_SRC))
+TEST_SUPPORT_SRC = $(TEST_DIR)/common.c
+TEST_SUPPORT_OBJS = $(patsubst %,$(TEST_OBJDIR)/%,$(TEST_SUPPORT_SRC:.c=.o))
+
+all: $(OBJS)
+
+test: $(TESTS)
+ @set -o $(TEST_SET_OPTS) >/dev/null 2>&1
+ @export SHELLOPTS
+ @sh runtests.sh $(TEST_OBJDIR)/$(TEST_DIR)
+
+COVERAGE_INFO_FILENAME = coverage.info
+COVERAGE_INFO_PATH = $(TEST_OBJDIR)/$(COVERAGE_INFO_FILENAME)
+coverage:
+ @lcov --base-directory . --directory src --zerocounters -q
+ @make clean
+ @make test
+ @lcov --base-directory . --directory $(TEST_OBJDIR) -c -o $(TEST_OBJDIR)/coverage.info
+ @lcov --remove $(COVERAGE_INFO_PATH) "deps/*" -o $(COVERAGE_INFO_PATH)
+ @genhtml -o $(TEST_OBJDIR)/coverage -t "isotp-c test coverage" --num-spaces 4 $(COVERAGE_INFO_PATH)
+ @$(BROWSER) $(TEST_OBJDIR)/coverage/index.html
+ @echo "$(GREEN)Coverage information generated in $(TEST_OBJDIR)/coverage/index.html.$(COLOR_RESET)"
+
+$(TEST_OBJDIR)/%.o: %.c
+ @mkdir -p $(dir $@)
+ $(CC) $(CFLAGS) $(CC_SYMBOLS) $(INCLUDES) -o $@ $<
+
+$(TEST_OBJDIR)/%.bin: $(TEST_OBJDIR)/%.o $(OBJS) $(TEST_SUPPORT_OBJS)
+ @mkdir -p $(dir $@)
+ $(CC) $(LDFLAGS) $(CC_SYMBOLS) $(INCLUDES) -o $@ $^ $(LDLIBS)
+
+clean:
+ rm -rf $(TEST_OBJDIR)
diff --git a/CAN-binder/libs/uds-c/README.mkd b/CAN-binder/libs/uds-c/README.mkd
new file mode 100644
index 0000000..aa0d7fd
--- /dev/null
+++ b/CAN-binder/libs/uds-c/README.mkd
@@ -0,0 +1,196 @@
+Unified Diagnostic Services (UDS) Support Library in C
+======================================================
+
+This is a platform agnostic C library that implements the Unified Diagnostics
+Services protocol for automotive electronics. UDS is documented in ISO 14229 and
+is the underpinning for the more well-known On-board Diagnostics (OBD) standard.
+The library currently supports UDS running over CAN (ISO 15765-4), which uses
+the ISO-TP (ISO 15765-2) protocol for message framing.
+
+This library doesn't assume anything about the source of your diagnostic message
+requests or underlying interface to the CAN bus. It uses dependency injection to
+give you complete control.
+
+## Usage
+
+First, create some shim functions to let this library use your lower level
+system:
+
+ // required, this must send a single CAN message with the given arbitration
+ // ID (i.e. the CAN message ID) and data. The size will never be more than 8
+ // bytes.
+ bool send_can(const uint32_t arbitration_id, const uint8_t* data,
+ const uint8_t size) {
+ ...
+ }
+
+ // optional, provide to receive debugging log messages
+ void debug(const char* format, ...) {
+ ...
+ }
+
+
+ // not used in the current version
+ void set_timer(uint16_t time_ms, void (*callback)) {
+ ...
+ }
+
+With your shims in place, create a `DiagnosticShims` object to pass them around:
+
+ DiagnosticShims shims = diagnostic_init_shims(debug, send_can, set_timer);
+
+With your shims in hand, send a simple PID request to the standard broadcast
+address, `0x7df` (we use the constant `OBD2_FUNCTIONAL_BROADCAST_ID` here):
+
+ // Optional: This is your callback that will be called the response to your
+ // diagnostic request is received.
+ void response_received_handler(const DiagnosticResponse* response) {
+ // You received a response! Do something with it.
+ }
+
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&shims,
+ DIAGNOSTIC_STANDARD_PID, // this is a standard PID request, not an extended or enhanced one
+ OBD2_FUNCTIONAL_BROADCAST_ID, // the request is going out to the broadcast arbitration ID
+ 0x2, // we want PID 0x2
+ response_received_handler); // our callback (optional, use NULL if you don't have one)
+
+ if(handle.completed) {
+ if(!handle.success) {
+ // something happened and it already failed - possibly we aren't
+ // able to send CAN messages
+ return;
+ } else {
+ // this should never occur right away - you need to receive a fresh
+ // CAN message first
+ }
+ } else {
+ while(true) {
+ // Continue to read from CAN, passing off each message to the handle.
+ // This will return a 'completed' DiagnosticResponse when the when
+ // the request is completely sent and the response is received
+ // (which may take more than 1 CAN frames)
+ DiagnosticResponse response = diagnostic_receive_can_frame(&shims,
+ &handle, can_message_id, can_data, sizeof(can_data));
+
+ if(response.completed && handle.completed) {
+ if(handle.success) {
+ if(response.success) {
+ // The request was sent successfully, the response was
+ // received successfully, and it was a positive response - we
+ // got back some data!
+ } else {
+ // The request was sent successfully, the response was
+ // received successfully, BUT it was a negative response
+ // from the other node.
+ printf("This is the error code: %d", response.negative_response_code);
+ }
+ } else {
+ // Some other fatal error ocurred - we weren't able to send
+ // the request or receive the response. The CAN connection
+ // may be down.
+ }
+ }
+ }
+ }
+
+### Requests for other modes
+
+If you want to do more besides PID requests on mode 0x1 and 0x22, there's a
+lower level API you can use. Here's how to make a mode 3 request to get DTCs.
+
+ DiagnosticRequest request = {
+ arbitration_id: OBD2_FUNCTIONAL_BROADCAST_ID,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request, NULL);
+
+ if(handle.completed) {
+ if(!handle.success) {
+ // something happened and it already failed - possibly we aren't
+ // able to send CAN messages
+ return;
+ } else {
+ // this should never occur right away - you need to receive a fresh
+ // CAN message first
+ }
+ } else {
+ while(true) {
+ // Continue to read from CAN, passing off each message to the handle.
+ // This will return a 'completed' DiagnosticResponse when the when
+ // the request is completely sent and the response is received
+ // (which may take more than 1 CAN frames)
+ DiagnosticResponse response = diagnostic_receive_can_frame(&shims,
+ &handle, can_message_id, can_data, sizeof(can_data));
+
+ if(response.completed && handle.completed) {
+ if(handle.success) {
+ if(response.success) {
+ // The request was sent successfully, the response was
+ // received successfully, and it was a positive response - we
+ // got back some data!
+ printf("The DTCs are: ");
+ for(int i = 0; i < response.payload_length; i++) {
+ printf("0x%x ", response.payload[i]);
+ }
+ } else {
+ // The request was sent successfully, the response was
+ // received successfully, BUT it was a negative response
+ // from the other node.
+ printf("This is the error code: %d", response.negative_response_code);
+ }
+ } else {
+ // Some other fatal error ocurred - we weren't able to send
+ // the request or receive the response. The CAN connection
+ // may be down.
+ }
+ }
+ }
+ }
+
+## Dependencies
+
+This library requires 2 dependencies:
+
+* [isotp-c](https://github.com/openxc/isotp-c)
+* [bitfield-c](https://github.com/openxc/bitfield-c)
+
+## Testing
+
+The library includes a test suite that uses the `check` C unit test library.
+
+ $ make test
+
+You can also see the test coverage if you have `lcov` installed and the
+`BROWSER` environment variable set to your choice of web browsers:
+
+ $ BROWSER=google-chrome-stable make coverage
+
+## OBD-II Basics
+
+TODO diagram out a request, response and error response
+
+* store the request arb id, mode, pid, and payload locally
+* send a can message
+* get all new can messages passed to it
+* Check the incoming can message to see if it matches one of the standard ECU
+ response IDs, or our arb ID + 0x8
+* if it matches, parse the diagnostic response and call the callback
+
+
+## Future Notes
+
+you're going to request a few PIDs over and over again at some frequency
+you're going to request DTCs once and read the response
+you're going to clear DTCs once
+
+we need another layer on top of that to handle the repeated requests.
+
+## Authors
+
+Chris Peplin cpeplin@ford.com
+
+## License
+
+Copyright (c) 2013 Ford Motor Company
+
+Licensed under the BSD license.
diff --git a/CAN-binder/libs/uds-c/deps/bitfield-c b/CAN-binder/libs/uds-c/deps/bitfield-c
new file mode 160000
+Subproject 6f243098abadbde2e9b2e5b13a94029960ea2e5
diff --git a/CAN-binder/libs/uds-c/deps/isotp-c b/CAN-binder/libs/uds-c/deps/isotp-c
new file mode 160000
+Subproject ee24440b7c123ab1b0317e57be33e837a1cb51f
diff --git a/CAN-binder/libs/uds-c/runtests.sh b/CAN-binder/libs/uds-c/runtests.sh
new file mode 100644
index 0000000..4781636
--- /dev/null
+++ b/CAN-binder/libs/uds-c/runtests.sh
@@ -0,0 +1,17 @@
+echo "Running unit tests:"
+
+for i in $1/*.bin
+do
+ if test -f $i
+ then
+ if ./$i
+ then
+ echo $i PASS
+ else
+ echo "ERROR in test $i:"
+ exit 1
+ fi
+ fi
+done
+
+echo "${txtbld}$(tput setaf 2)All unit tests passed.$(tput sgr0)"
diff --git a/CAN-binder/libs/uds-c/src/uds/extras.c b/CAN-binder/libs/uds-c/src/uds/extras.c
new file mode 100644
index 0000000..2be6bdd
--- /dev/null
+++ b/CAN-binder/libs/uds-c/src/uds/extras.c
@@ -0,0 +1,39 @@
+#include <uds/extras.h>
+#include <uds/uds.h>
+
+// TODO everything below here is for future work...not critical for now.
+
+DiagnosticRequestHandle diagnostic_request_malfunction_indicator_status(
+ DiagnosticShims* shims,
+ DiagnosticMilStatusReceived callback) {
+ // TODO request malfunction indicator light (MIL) status - request mode 1
+ // pid 1, parse first bit
+ DiagnosticRequestHandle handle;
+ return handle;
+}
+
+DiagnosticRequestHandle diagnostic_request_vin(DiagnosticShims* shims,
+ DiagnosticVinReceived callback) {
+ DiagnosticRequestHandle handle;
+ return handle;
+}
+
+DiagnosticRequestHandle diagnostic_request_dtc(DiagnosticShims* shims,
+ DiagnosticTroubleCodeType dtc_type,
+ DiagnosticTroubleCodesReceived callback) {
+ DiagnosticRequestHandle handle;
+ return handle;
+}
+
+bool diagnostic_clear_dtc(DiagnosticShims* shims) {
+ return false;
+}
+
+DiagnosticRequestHandle diagnostic_enumerate_pids(DiagnosticShims* shims,
+ DiagnosticRequest* request, DiagnosticPidEnumerationReceived callback) {
+ // before calling the callback, split up the received bytes into 1 or 2 byte
+ // chunks depending on the mode so the final pid list is actual 1 or 2 byte PIDs
+ // TODO request supported PIDs - request PID 0 and parse 4 bytes in response
+ DiagnosticRequestHandle handle;
+ return handle;
+}
diff --git a/CAN-binder/libs/uds-c/src/uds/extras.h b/CAN-binder/libs/uds-c/src/uds/extras.h
new file mode 100644
index 0000000..126e5d4
--- /dev/null
+++ b/CAN-binder/libs/uds-c/src/uds/extras.h
@@ -0,0 +1,71 @@
+#ifndef __EXTRAS_H__
+#define __EXTRAS_H__
+
+#include <uds/uds_types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// TODO everything in here is unused for the moment!
+
+typedef enum {
+ POWERTRAIN = 0x0,
+ CHASSIS = 0x1,
+ BODY = 0x2,
+ NETWORK = 0x3
+} DiagnosticTroubleCodeGroup;
+
+typedef struct {
+ DiagnosticTroubleCodeGroup group;
+ uint8_t group_num;
+ uint8_t code;
+} DiagnosticTroubleCode;
+
+
+/* Private: TODO unused for now
+ */
+typedef enum {
+ DTC_EMISSIONS,
+ DTC_DRIVE_CYCLE,
+ DTC_PERMANENT
+} DiagnosticTroubleCodeType;
+
+
+// TODO should we enumerate every OBD-II PID? need conversion formulas, too
+typedef struct {
+ uint16_t pid;
+ uint8_t bytes_returned;
+ float min_value;
+ float max_value;
+} DiagnosticParameter;
+
+typedef void (*DiagnosticMilStatusReceived)(bool malfunction_indicator_status);
+typedef void (*DiagnosticVinReceived)(uint8_t vin[]);
+typedef void (*DiagnosticTroubleCodesReceived)(
+ DiagnosticMode mode, DiagnosticTroubleCode* codes);
+typedef void (*DiagnosticPidEnumerationReceived)(
+ const DiagnosticResponse* response, uint16_t* pids);
+
+DiagnosticRequestHandle diagnostic_request_malfunction_indicator_status(
+ DiagnosticShims* shims,
+ DiagnosticMilStatusReceived callback);
+
+DiagnosticRequestHandle diagnostic_request_vin(DiagnosticShims* shims,
+ DiagnosticVinReceived callback);
+
+DiagnosticRequestHandle diagnostic_request_dtc(DiagnosticShims* shims,
+ DiagnosticTroubleCodeType dtc_type,
+ DiagnosticTroubleCodesReceived callback);
+
+bool diagnostic_clear_dtc(DiagnosticShims* shims);
+
+DiagnosticRequestHandle diagnostic_enumerate_pids(DiagnosticShims* shims,
+ DiagnosticRequest* request, DiagnosticPidEnumerationReceived callback);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __EXTRAS_H__
diff --git a/CAN-binder/libs/uds-c/src/uds/uds.c b/CAN-binder/libs/uds-c/src/uds/uds.c
new file mode 100644
index 0000000..0114384
--- /dev/null
+++ b/CAN-binder/libs/uds-c/src/uds/uds.c
@@ -0,0 +1,400 @@
+#include <uds/uds.h>
+#include <bitfield/bitfield.h>
+#include <canutil/read.h>
+#include <string.h>
+#include <limits.h>
+#include <stddef.h>
+#include <sys/param.h>
+#include <inttypes.h>
+
+#define ARBITRATION_ID_OFFSET 0x8
+#define MODE_RESPONSE_OFFSET 0x40
+#define NEGATIVE_RESPONSE_MODE 0x7f
+#define MAX_DIAGNOSTIC_PAYLOAD_SIZE 6
+#define MODE_BYTE_INDEX 0
+#define PID_BYTE_INDEX 1
+#define NEGATIVE_RESPONSE_MODE_INDEX 1
+#define NEGATIVE_RESPONSE_NRC_INDEX 2
+
+#ifndef MAX
+#define MAX(x, y) (((x) > (y)) ? (x) : (y))
+#endif
+
+DiagnosticShims diagnostic_init_shims(LogShim log,
+ SendCanMessageShim send_can_message,
+ SetTimerShim set_timer) {
+ DiagnosticShims shims = {
+ log: log,
+ send_can_message: send_can_message,
+ set_timer: set_timer
+ };
+ return shims;
+}
+
+static void setup_receive_handle(DiagnosticRequestHandle* handle) {
+ if(handle->request.arbitration_id == OBD2_FUNCTIONAL_BROADCAST_ID) {
+ uint32_t response_id;
+ for(response_id = 0;
+ response_id < OBD2_FUNCTIONAL_RESPONSE_COUNT; ++response_id) {
+ handle->isotp_receive_handles[response_id] = isotp_receive(
+ &handle->isotp_shims,
+ OBD2_FUNCTIONAL_RESPONSE_START + response_id,
+ NULL);
+ }
+ handle->isotp_receive_handle_count = OBD2_FUNCTIONAL_RESPONSE_COUNT;
+ } else {
+ handle->isotp_receive_handle_count = 1;
+ handle->isotp_receive_handles[0] = isotp_receive(&handle->isotp_shims,
+ handle->request.arbitration_id + ARBITRATION_ID_OFFSET,
+ NULL);
+ }
+}
+
+static uint16_t autoset_pid_length(uint8_t mode, uint16_t pid,
+ uint8_t pid_length) {
+ if(pid_length == 0) {
+ if(mode <= 0xa || mode == 0x3e ) {
+ pid_length = 1;
+ } else if(pid > 0xffff || ((pid & 0xFF00) > 0x0)) {
+ pid_length = 2;
+ } else {
+ pid_length = 1;
+ }
+ }
+ return pid_length;
+}
+
+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);
+ 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(handle->isotp_send_handle.completed &&
+ !handle->isotp_send_handle.success) {
+ handle->completed = true;
+ handle->success = false;
+ if(shims->log != NULL) {
+ shims->log("%s", "Diagnostic request not sent");
+ }
+ } else 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);
+ }
+}
+
+bool diagnostic_request_sent(DiagnosticRequestHandle* handle) {
+ return handle->isotp_send_handle.completed;
+}
+
+void start_diagnostic_request(DiagnosticShims* shims,
+ DiagnosticRequestHandle* handle) {
+ handle->success = false;
+ handle->completed = false;
+ send_diagnostic_request(shims, handle);
+ if(!handle->completed) {
+ setup_receive_handle(handle);
+ }
+}
+
+DiagnosticRequestHandle generate_diagnostic_request(DiagnosticShims* shims,
+ DiagnosticRequest* request, DiagnosticResponseReceived callback) {
+ DiagnosticRequestHandle handle = {
+ request: *request,
+ callback: callback,
+ success: false,
+ completed: false
+ };
+
+ handle.isotp_shims = isotp_init_shims(shims->log,
+ shims->send_can_message,
+ shims->set_timer);
+ handle.isotp_shims.frame_padding = !request->no_frame_padding;
+
+ return handle;
+ // TODO notes on multi frame:
+ // TODO what are the timers for exactly?
+ //
+ // when sending multi frame, send 1 frame, wait for a response
+ // if it says send all, send all right away
+ // if it says flow control, set the time for the next send
+ // instead of creating a timer with an async callback, add a process_handle
+ // function that's called repeatedly in the main loop - if it's time to
+ // send, we do it. so there's a process_handle_send and receive_can_frame
+ // that are just called continuously from the main loop. it's a waste of a
+ // few cpu cycles but it may be more natural than callbacks.
+ //
+ // what would a timer callback look like...it would need to pass the handle
+ // and that's all. seems like a context void* would be able to capture all
+ // 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;
+}
+
+DiagnosticRequestHandle diagnostic_request_pid(DiagnosticShims* shims,
+ DiagnosticPidRequestType pid_request_type, uint32_t arbitration_id,
+ uint16_t pid, DiagnosticResponseReceived callback) {
+ DiagnosticRequest request = {
+ arbitration_id: arbitration_id,
+ mode: pid_request_type == DIAGNOSTIC_STANDARD_PID ? 0x1 : 0x22,
+ has_pid: true,
+ pid: pid
+ };
+
+ return diagnostic_request(shims, &request, callback);
+}
+
+static bool handle_negative_response(IsoTpMessage* message,
+ DiagnosticResponse* response, DiagnosticShims* shims) {
+ bool response_was_negative = false;
+ if(response->mode == NEGATIVE_RESPONSE_MODE) {
+ response_was_negative = true;
+ if(message->size > NEGATIVE_RESPONSE_MODE_INDEX) {
+ response->mode = message->payload[NEGATIVE_RESPONSE_MODE_INDEX];
+ }
+
+ if(message->size > NEGATIVE_RESPONSE_NRC_INDEX) {
+ response->negative_response_code =
+ message->payload[NEGATIVE_RESPONSE_NRC_INDEX];
+ }
+
+ response->success = false;
+ response->completed = true;
+ }
+ return response_was_negative;
+}
+
+static bool handle_positive_response(DiagnosticRequestHandle* handle,
+ IsoTpMessage* message, DiagnosticResponse* response,
+ DiagnosticShims* shims) {
+ bool response_was_positive = false;
+ if(response->mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
+ response_was_positive = true;
+ // hide the "response" version of the mode from the user
+ // if it matched
+ response->mode = handle->request.mode;
+ response->has_pid = false;
+ 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,
+ PID_BYTE_INDEX * CHAR_BIT, sizeof(uint16_t) * CHAR_BIT);
+ } else {
+ response->pid = message->payload[PID_BYTE_INDEX];
+ }
+
+ }
+
+ if((!handle->request.has_pid && !response->has_pid)
+ || response->pid == handle->request.pid) {
+ response->success = true;
+ response->completed = true;
+
+ uint8_t payload_index = 1 + handle->request.pid_length;
+ response->payload_length = MAX(0, message->size - payload_index);
+ if(response->payload_length > 0) {
+ memcpy(response->payload, &message->payload[payload_index],
+ response->payload_length);
+ }
+ } else {
+ response_was_positive = false;
+ }
+ }
+ return response_was_positive;
+}
+
+DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
+ DiagnosticRequestHandle* handle, const uint32_t arbitration_id,
+ const uint8_t data[], const uint8_t size) {
+
+ DiagnosticResponse response = {
+ arbitration_id: arbitration_id,
+ multi_frame: false,
+ success: false,
+ completed: false
+ };
+
+ if(!handle->isotp_send_handle.completed) {
+ isotp_continue_send(&handle->isotp_shims,
+ &handle->isotp_send_handle, arbitration_id, data, size);
+ } else {
+ uint8_t i;
+ for(i = 0; i < handle->isotp_receive_handle_count; ++i) {
+ IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
+ &handle->isotp_receive_handles[i], arbitration_id, data,
+ size);
+ response.multi_frame = message.multi_frame;
+
+ if(message.completed) {
+ if(message.size > 0) {
+ response.mode = message.payload[0];
+ if(handle_negative_response(&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);
+ }
+
+ handle->success = true;
+ handle->completed = true;
+ }
+ } else {
+ if(shims->log != NULL) {
+ shims->log("Received an empty response on arb ID 0x%x",
+ response.arbitration_id);
+ }
+ }
+
+ if(handle->completed && handle->callback != NULL) {
+ handle->callback(&response);
+ }
+
+ break;
+ }
+ }
+ }
+ return response;
+}
+
+int diagnostic_payload_to_integer(const DiagnosticResponse* response) {
+ return get_bitfield(response->payload, response->payload_length, 0,
+ response->payload_length * CHAR_BIT);
+}
+
+float diagnostic_decode_obd2_pid(const DiagnosticResponse* response) {
+ // handles on the single number values, not the bit encoded ones
+ switch(response->pid) {
+ case 0xa:
+ return response->payload[0] * 3;
+ case 0xc:
+ return (response->payload[0] * 256 + response->payload[1]) / 4.0;
+ case 0xd:
+ case 0x33:
+ case 0xb:
+ return response->payload[0];
+ case 0x10:
+ return (response->payload[0] * 256 + response->payload[1]) / 100.0;
+ case 0x11:
+ case 0x2f:
+ case 0x45:
+ case 0x4c:
+ case 0x52:
+ case 0x5a:
+ case 0x4:
+ return response->payload[0] * 100.0 / 255.0;
+ case 0x46:
+ case 0x5c:
+ case 0xf:
+ case 0x5:
+ return response->payload[0] - 40;
+ case 0x62:
+ return response->payload[0] - 125;
+ default:
+ return diagnostic_payload_to_integer(response);
+ }
+}
+
+void diagnostic_response_to_string(const DiagnosticResponse* response,
+ char* destination, size_t destination_length) {
+ int bytes_used = snprintf(destination, destination_length,
+ "arb_id: 0x%lx, mode: 0x%x, ",
+ (unsigned long) response->arbitration_id,
+ response->mode);
+
+ if(response->has_pid) {
+ bytes_used += snprintf(destination + bytes_used,
+ destination_length - bytes_used,
+ "pid: 0x%x, ",
+ response->pid);
+ }
+
+ if(!response->success) {
+ bytes_used += snprintf(destination + bytes_used,
+ destination_length - bytes_used,
+ "nrc: 0x%x, ",
+ response->negative_response_code);
+ }
+
+ if(response->payload_length > 0) {
+ snprintf(destination + bytes_used, destination_length - bytes_used,
+ "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
+ response->payload[0],
+ response->payload[1],
+ response->payload[2],
+ response->payload[3],
+ response->payload[4],
+ response->payload[5],
+ response->payload[6]);
+ } else {
+ snprintf(destination + bytes_used, destination_length - bytes_used,
+ "no payload");
+ }
+}
+
+void diagnostic_request_to_string(const DiagnosticRequest* request,
+ char* destination, size_t destination_length) {
+ int bytes_used = snprintf(destination, destination_length,
+ "arb_id: 0x%lx, mode: 0x%x, ",
+ (unsigned long) request->arbitration_id,
+ request->mode);
+
+ if(request->has_pid) {
+ bytes_used += snprintf(destination + bytes_used,
+ destination_length - bytes_used,
+ "pid: 0x%x, ",
+ request->pid);
+ }
+
+ int remaining_space = destination_length - bytes_used;
+ if(request->payload_length > 0) {
+ snprintf(destination + bytes_used, remaining_space,
+ "payload: 0x%02x%02x%02x%02x%02x%02x%02x",
+ request->payload[0],
+ request->payload[1],
+ request->payload[2],
+ request->payload[3],
+ request->payload[4],
+ request->payload[5],
+ request->payload[6]);
+ } else {
+ snprintf(destination + bytes_used, remaining_space, "no payload");
+ }
+}
+
+bool diagnostic_request_equals(const DiagnosticRequest* ours,
+ const DiagnosticRequest* theirs) {
+ bool equals = ours->arbitration_id == theirs->arbitration_id &&
+ ours->mode == theirs->mode;
+ equals &= ours->has_pid == theirs->has_pid;
+ equals &= ours->pid == theirs->pid;
+ return equals;
+}
diff --git a/CAN-binder/libs/uds-c/src/uds/uds.h b/CAN-binder/libs/uds-c/src/uds/uds.h
new file mode 100644
index 0000000..4305834
--- /dev/null
+++ b/CAN-binder/libs/uds-c/src/uds/uds.h
@@ -0,0 +1,165 @@
+#ifndef __UDS_H__
+#define __UDS_H__
+
+#include <uds/uds_types.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#define OBD2_FUNCTIONAL_BROADCAST_ID 0x7df
+#define OBD2_FUNCTIONAL_RESPONSE_START 0x7e8
+#define OBD2_FUNCTIONAL_RESPONSE_COUNT 8
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Public: Initialize an DiagnosticShims with the given callback functions.
+ *
+ * If any callbacks are not to be used, set them to NULL. For documentation of
+ * the function type signatures, see higher up in this header file. This struct
+ * is a handy encapsulation used to pass the shims around to the various
+ * diagnostic_* functions.
+ *
+ * Returns a struct with the fields initailized to the callbacks.
+ */
+DiagnosticShims diagnostic_init_shims(LogShim log,
+ SendCanMessageShim send_can_message,
+ SetTimerShim set_timer);
+
+/* 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.
+ * 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 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. 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.
+ *
+ * shims - Low-level shims required to send CAN messages, etc.
+ * pid_request_type - either DIAGNOSTIC_STANDARD_PID (will use mode 0x1 and 1
+ * byte PIDs) or DIAGNOSTIC_ENHANCED_PID (will use mode 0x22 and 2 byte
+ * PIDs)
+ * arbitration_id - The arbitration ID to send the request to.
+ * pid - The PID to request from the other node.
+ * 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 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 diagnostic_request_pid(DiagnosticShims* shims,
+ DiagnosticPidRequestType pid_request_type, uint32_t arbitration_id,
+ uint16_t pid, DiagnosticResponseReceived callback);
+
+/* Public: Continue to send and receive a single diagnostic request, based on a
+ * freshly received CAN message.
+ *
+ * shims - Low-level shims required to send CAN messages, etc.
+ * handle - A DiagnosticRequestHandle previously returned by one of the
+ * diagnostic_request*(..) functions.
+ * arbitration_id - The arbitration_id of the received CAN message.
+ * data - The data of the received CAN message.
+ * size - The size of the data in the received CAN message.
+ *
+ * Returns true if the request was completed and response received, or the
+ * request was otherwise cancelled. Check the 'success' field of the handle to
+ * see if it was successful.
+ */
+DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
+ DiagnosticRequestHandle* handle,
+ const uint32_t arbitration_id, const uint8_t data[],
+ const uint8_t size);
+
+/* Public: Parse the entier payload of the reponse as a single integer.
+ *
+ * response - the received DiagnosticResponse.
+ */
+int diagnostic_payload_to_integer(const DiagnosticResponse* response);
+
+/* Public: Render a DiagnosticResponse as a string into the given buffer.
+ *
+ * response - the response to convert to a string, for debug logging.
+ * destination - the target string buffer.
+ * destination_length - the size of the destination buffer, i.e. the max size
+ * for the rendered string.
+ */
+void diagnostic_response_to_string(const DiagnosticResponse* response,
+ char* destination, size_t destination_length);
+
+/* Public: Render a DiagnosticRequest as a string into the given buffer.
+ *
+ * request - the request to convert to a string, for debug logging.
+ * destination - the target string buffer.
+ * destination_length - the size of the destination buffer, i.e. the max size
+ * for the rendered string.
+ */
+void diagnostic_request_to_string(const DiagnosticRequest* request,
+ char* destination, size_t destination_length);
+
+/* Public: For many OBD-II PIDs with a numerical result, translate a diagnostic
+ * response payload into a meaningful number using the standard formulas.
+ *
+ * Functions pulled from http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01
+ *
+ * Returns the translated value or 0 if the PID is not in the OBD-II standard or
+ * does not use a numerical value (e.g. VIN).
+ */
+float diagnostic_decode_obd2_pid(const DiagnosticResponse* response);
+
+/* Public: Returns true if the "fingerprint" of the two diagnostic messages
+ * matches - the arbitration_id, mode and pid (or lack of pid).
+ */
+bool diagnostic_request_equals(const DiagnosticRequest* ours,
+ const DiagnosticRequest* theirs);
+
+/* Public: Returns true if the request has been completely sent - if false, make
+ * sure you called start_diagnostic_request once to start it, and then pass
+ * incoming CAN messages to it with diagnostic_receive_can_frame(...) so it can
+ * continue the ISO-TP transfer.
+ */
+bool diagnostic_request_sent(DiagnosticRequestHandle* handle);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __UDS_H__
diff --git a/CAN-binder/libs/uds-c/src/uds/uds_types.h b/CAN-binder/libs/uds-c/src/uds/uds_types.h
new file mode 100644
index 0000000..4ebc150
--- /dev/null
+++ b/CAN-binder/libs/uds-c/src/uds/uds_types.h
@@ -0,0 +1,200 @@
+#ifndef __UDS_TYPES_H__
+#define __UDS_TYPES_H__
+
+#include <isotp/isotp.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// TODO This still doesn't have enough space for the largest possible
+// multiframe response. May need to dynamically allocate in the future.
+#define MAX_UDS_RESPONSE_PAYLOAD_LENGTH 127
+#define MAX_UDS_REQUEST_PAYLOAD_LENGTH 7
+#define MAX_RESPONDING_ECU_COUNT 8
+#define VIN_LENGTH 17
+
+/* Private: The four main types of diagnositc requests that determine how the
+ * request should be parsed and what type of callback should be used.
+ *
+ * TODO this may not be used...yet?
+ */
+typedef enum {
+ DIAGNOSTIC_REQUEST_TYPE_PID,
+ DIAGNOSTIC_REQUEST_TYPE_DTC,
+ DIAGNOSTIC_REQUEST_TYPE_MIL_STATUS,
+ DIAGNOSTIC_REQUEST_TYPE_VIN
+} DiagnosticRequestType;
+
+/* Public: A container for a single diagnostic request.
+ *
+ * The only required fields are the arbitration_id and mode.
+ *
+ * arbitration_id - The arbitration ID to send the request.
+ * mode - The OBD-II mode for the request.
+ * 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). 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.
+ * no_frame_padding - false if sent CAN payloads should *not* be padded out to a
+ * full 8 byte CAN frame. Many ECUs require this, but others require the
+ * size of the CAN message to only be the actual data. By default padding
+ * is enabled (so this struct value can default to 0).
+ * type - the type of the request (TODO unused)
+ */
+typedef struct {
+ uint32_t arbitration_id;
+ uint8_t mode;
+ bool has_pid;
+ uint16_t pid;
+ uint8_t pid_length;
+ uint8_t payload[MAX_UDS_REQUEST_PAYLOAD_LENGTH];
+ uint8_t payload_length;
+ bool no_frame_padding;
+ DiagnosticRequestType type;
+} DiagnosticRequest;
+
+/* Public: All possible negative response codes that could be received from a
+ * requested node.
+ *
+ * When a DiagnosticResponse is received and the 'completed' field is true, but
+ * the 'success' field is false, the 'negative_response_code' field will contain
+ * one of these values as reported by the requested node.
+ *
+ * Thanks to canbushack.com for the list of NRCs.
+ */
+typedef enum {
+ NRC_SUCCESS = 0x0,
+ NRC_SERVICE_NOT_SUPPORTED = 0x11,
+ NRC_SUB_FUNCTION_NOT_SUPPORTED = 0x12,
+ NRC_INCORRECT_LENGTH_OR_FORMAT = 0x13,
+ NRC_CONDITIONS_NOT_CORRECT = 0x22,
+ NRC_REQUEST_OUT_OF_RANGE = 0x31,
+ NRC_SECURITY_ACCESS_DENIED = 0x33,
+ NRC_INVALID_KEY = 0x35,
+ NRC_TOO_MANY_ATTEMPS = 0x36,
+ NRC_TIME_DELAY_NOT_EXPIRED = 0x37,
+ NRC_RESPONSE_PENDING = 0x78
+} DiagnosticNegativeResponseCode;
+
+/* Public: A partially or fully completed response to a diagnostic request.
+ *
+ * completed - True if the request is complete - some functions return a
+ * DiagnosticResponse even when it's only partially completed, so be sure
+ * to check this field.
+ * success - True if the request was successful. The value if this
+ * field isn't valid if 'completed' isn't true. If this is 'false', check
+ * the negative_response_code field for the reason.
+ * arbitration_id - The arbitration ID the response was received on.
+ * multi_frame - True if this response (whether completed or not) required
+ * multi-frame CAN support. Can be used for updating time-out functions.
+ * mode - The OBD-II mode for the original request.
+ * has_pid - If this is a response to a PID request, this will be true and the
+ * 'pid' field will be valid.
+ * pid - If the request was for a PID, this is the PID echo. Only valid if
+ * 'has_pid' is true.
+ * negative_response_code - If the request was not successful, 'success' will be
+ * false and this will be set to a DiagnosticNegativeResponseCode returned
+ * by the other node.
+ * payload - An optional payload for the response - NULL if no payload.
+ * payload_length - The length of the payload or 0 if none.
+ */
+typedef struct {
+ bool completed;
+ bool success;
+ bool multi_frame;
+ uint32_t arbitration_id;
+ uint8_t mode;
+ bool has_pid;
+ uint16_t pid;
+ DiagnosticNegativeResponseCode negative_response_code;
+ uint8_t payload[MAX_UDS_RESPONSE_PAYLOAD_LENGTH];
+ uint8_t payload_length;
+} DiagnosticResponse;
+
+/* Public: Friendly names for all OBD-II modes.
+ */
+typedef enum {
+ OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST = 0x1,
+ OBD2_MODE_POWERTRAIN_FREEZE_FRAME_REQUEST = 0x2,
+ OBD2_MODE_EMISSIONS_DTC_REQUEST = 0x3,
+ OBD2_MODE_EMISSIONS_DTC_CLEAR = 0x4,
+ // 0x5 is for non-CAN only
+ // OBD2_MODE_OXYGEN_SENSOR_TEST = 0x5,
+ OBD2_MODE_TEST_RESULTS = 0x6,
+ OBD2_MODE_DRIVE_CYCLE_DTC_REQUEST = 0x7,
+ OBD2_MODE_CONTROL = 0x8,
+ OBD2_MODE_VEHICLE_INFORMATION = 0x9,
+ OBD2_MODE_PERMANENT_DTC_REQUEST = 0xa,
+ // this one isn't technically in uds, but both of the enhanced standards
+ // have their PID requests at 0x22
+ OBD2_MODE_ENHANCED_DIAGNOSTIC_REQUEST = 0x22
+} DiagnosticMode;
+
+/* Public: The signature for an optional function to be called when a diagnostic
+ * request is complete, and a response is received or there is a fatal error.
+ *
+ * response - the completed DiagnosticResponse.
+ */
+typedef void (*DiagnosticResponseReceived)(const DiagnosticResponse* response);
+
+/* Public: A handle for initiating and continuing a single diagnostic request.
+ *
+ * A diagnostic request requires one or more CAN messages to be sent, and one
+ * or more CAN messages to be received before it is completed. This struct
+ * encapsulates the local state required to track the request while it is in
+ * progress.
+ *
+ * request - The original DiagnosticRequest that this handle was created for.
+ * completed - True if the request was completed successfully, or was otherwise
+ * cancelled.
+ * success - True if the request send and receive process was successful. The
+ * value if this field isn't valid if 'completed' isn't true.
+ */
+typedef struct {
+ DiagnosticRequest request;
+ bool success;
+ bool completed;
+
+ // Private
+ IsoTpShims isotp_shims;
+ IsoTpSendHandle isotp_send_handle;
+ IsoTpReceiveHandle isotp_receive_handles[MAX_RESPONDING_ECU_COUNT];
+ uint8_t isotp_receive_handle_count;
+ DiagnosticResponseReceived callback;
+ // DiagnosticMilStatusReceived mil_status_callback;
+ // DiagnosticVinReceived vin_callback;
+} DiagnosticRequestHandle;
+
+/* Public: The two major types of PIDs that determine the OBD-II mode and PID
+ * field length.
+ */
+typedef enum {
+ DIAGNOSTIC_STANDARD_PID,
+ DIAGNOSTIC_ENHANCED_PID
+} DiagnosticPidRequestType;
+
+/* Public: A container for the 3 shim functions used by the library to interact
+ * with the wider system.
+ *
+ * Use the diagnostic_init_shims(...) function to create an instance of this
+ * struct.
+ */
+typedef struct {
+ LogShim log;
+ SendCanMessageShim send_can_message;
+ SetTimerShim set_timer;
+} DiagnosticShims;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __UDS_TYPES_H__
diff --git a/CAN-binder/libs/uds-c/tests/common.c b/CAN-binder/libs/uds-c/tests/common.c
new file mode 100644
index 0000000..fd1e4b2
--- /dev/null
+++ b/CAN-binder/libs/uds-c/tests/common.c
@@ -0,0 +1,43 @@
+#include <uds/uds.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+
+DiagnosticShims SHIMS;
+
+uint32_t last_can_frame_sent_arb_id;
+uint8_t last_can_payload_sent[8];
+uint8_t last_can_payload_size;
+bool can_frame_was_sent;
+
+DiagnosticResponse last_response_received;
+bool last_response_was_received;
+
+void debug(const char* format, ...) {
+ va_list args;
+ va_start(args, format);
+ vprintf(format, args);
+ printf("\r\n");
+ va_end(args);
+}
+
+bool mock_send_can(const uint32_t arbitration_id, const uint8_t* data,
+ const uint8_t size) {
+ can_frame_was_sent = true;
+ last_can_frame_sent_arb_id = arbitration_id;
+ last_can_payload_size = size;
+ if(size > 0) {
+ memcpy(last_can_payload_sent, data, size);
+ }
+ return true;
+}
+
+void setup() {
+ SHIMS = diagnostic_init_shims(debug, mock_send_can, NULL);
+ memset(last_can_payload_sent, 0, sizeof(last_can_payload_sent));
+ can_frame_was_sent = false;
+ last_response_was_received = false;
+}
+
diff --git a/CAN-binder/libs/uds-c/tests/test_core.c b/CAN-binder/libs/uds-c/tests/test_core.c
new file mode 100644
index 0000000..81200cc
--- /dev/null
+++ b/CAN-binder/libs/uds-c/tests/test_core.c
@@ -0,0 +1,526 @@
+#include <uds/uds.h>
+#include <check.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+extern bool can_frame_was_sent;
+extern void setup();
+extern bool last_response_was_received;
+extern DiagnosticResponse last_response_received;
+extern DiagnosticShims SHIMS;
+extern uint16_t last_can_frame_sent_arb_id;
+extern uint8_t last_can_payload_sent[8];
+extern uint8_t last_can_payload_size;
+
+void response_received_handler(const DiagnosticResponse* response) {
+ last_response_was_received = true;
+ last_response_received = *response;
+}
+
+START_TEST (test_receive_wrong_arb_id)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x2, request.mode + 0x40, 0x23};
+ diagnostic_receive_can_frame(&SHIMS, &handle, request.arbitration_id,
+ can_data, sizeof(can_data));
+ fail_if(last_response_was_received);
+}
+END_TEST
+
+START_TEST (test_send_diag_request_with_payload)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST,
+ payload: {0x12, 0x34},
+ payload_length: 2,
+ no_frame_padding: true
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ fail_if(handle.completed);
+ // TODO it'd be better to check the ISO-TP message instead of the CAN frame,
+ // but we don't have a good way to do that
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_sent[1], request.mode);
+ ck_assert_int_eq(last_can_payload_size, 4);
+ ck_assert_int_eq(last_can_payload_sent[2], request.payload[0]);
+ ck_assert_int_eq(last_can_payload_sent[3], request.payload[1]);
+}
+END_TEST
+
+START_TEST (test_send_functional_request)
+{
+ DiagnosticRequest request = {
+ arbitration_id: OBD2_FUNCTIONAL_BROADCAST_ID,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST,
+ no_frame_padding: true
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ fail_if(handle.completed);
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_sent[1], request.mode);
+ ck_assert_int_eq(last_can_payload_size, 2);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x2, request.mode + 0x40, 0x23};
+ for(uint16_t filter = OBD2_FUNCTIONAL_RESPONSE_START; filter <
+ OBD2_FUNCTIONAL_RESPONSE_START + OBD2_FUNCTIONAL_RESPONSE_COUNT;
+ filter++) {
+ DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS,
+ &handle, filter, can_data, sizeof(can_data));
+ fail_unless(response.success);
+ fail_unless(response.completed);
+ fail_unless(handle.completed);
+ ck_assert(last_response_received.success);
+ ck_assert_int_eq(last_response_received.arbitration_id,
+ filter);
+ ck_assert_int_eq(last_response_received.mode, request.mode);
+ fail_if(last_response_received.has_pid);
+ ck_assert_int_eq(last_response_received.payload_length, 1);
+ ck_assert_int_eq(last_response_received.payload[0], can_data[2]);
+ }
+}
+END_TEST
+
+START_TEST (test_sent_message_no_padding)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST,
+ no_frame_padding: true
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ fail_if(handle.completed);
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_size, 2);
+}
+END_TEST
+
+START_TEST (test_sent_message_is_padded_by_default)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ fail_if(handle.completed);
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_size, 8);
+}
+END_TEST
+
+START_TEST (test_sent_message_is_padded)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST,
+ no_frame_padding: false
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ fail_if(handle.completed);
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_size, 8);
+}
+END_TEST
+
+START_TEST (test_send_diag_request)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST,
+ no_frame_padding: true
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ fail_if(handle.completed);
+ fail_unless(can_frame_was_sent);
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_sent[1], request.mode);
+ ck_assert_int_eq(last_can_payload_size, 2);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x2, request.mode + 0x40, 0x23};
+ DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS, &handle,
+ request.arbitration_id + 0x8, can_data, sizeof(can_data));
+ fail_unless(response.success);
+ fail_unless(response.completed);
+ fail_unless(handle.completed);
+ ck_assert(last_response_received.success);
+ ck_assert_int_eq(last_response_received.arbitration_id,
+ request.arbitration_id + 0x8);
+ ck_assert_int_eq(last_response_received.mode, request.mode);
+ fail_if(last_response_received.has_pid);
+ ck_assert_int_eq(last_response_received.payload_length, 1);
+ ck_assert_int_eq(last_response_received.payload[0], can_data[2]);
+}
+END_TEST
+
+START_TEST (test_generate_then_send_request)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST,
+ no_frame_padding: true
+ };
+ DiagnosticRequestHandle handle = generate_diagnostic_request(&SHIMS,
+ &request, response_received_handler);
+
+ fail_if(handle.completed);
+ fail_if(can_frame_was_sent);
+
+ start_diagnostic_request(&SHIMS, &handle);
+ fail_unless(can_frame_was_sent);
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_sent[1], request.mode);
+ ck_assert_int_eq(last_can_payload_size, 2);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x2, request.mode + 0x40, 0x23};
+ DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS, &handle,
+ request.arbitration_id + 0x8, can_data, sizeof(can_data));
+ fail_unless(response.success);
+ fail_unless(response.completed);
+ fail_unless(handle.completed);
+ ck_assert(last_response_received.success);
+ ck_assert_int_eq(last_response_received.arbitration_id,
+ request.arbitration_id + 0x8);
+ ck_assert_int_eq(last_response_received.mode, request.mode);
+ fail_if(last_response_received.has_pid);
+ ck_assert_int_eq(last_response_received.payload_length, 1);
+ ck_assert_int_eq(last_response_received.payload[0], can_data[2]);
+}
+END_TEST
+
+START_TEST (test_generate_diag_request)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_EMISSIONS_DTC_REQUEST,
+ no_frame_padding: true
+ };
+ DiagnosticRequestHandle handle = generate_diagnostic_request(&SHIMS,
+ &request, response_received_handler);
+
+ fail_if(handle.completed);
+ fail_if(can_frame_was_sent);
+}
+END_TEST
+
+START_TEST (test_autoset_pid_length)
+{
+ uint16_t arb_id = OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST;
+ diagnostic_request_pid(&SHIMS, DIAGNOSTIC_STANDARD_PID, arb_id, 0x2,
+ response_received_handler);
+
+ ck_assert_int_eq(last_can_frame_sent_arb_id, arb_id);
+ ck_assert_int_eq(last_can_payload_sent[1], 0x1);
+ ck_assert_int_eq(last_can_payload_sent[2], 0x2);
+ // padding is on for the diagnostic_request_pid helper function - if you
+ // need to turn it off, use the more manual diagnostic_request(...)
+ ck_assert_int_eq(last_can_payload_size, 8);
+
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: 0x22,
+ has_pid: true,
+ pid: 0x1234,
+ no_frame_padding: true
+ };
+ diagnostic_request(&SHIMS, &request, response_received_handler);
+
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_sent[1], request.mode);
+ ck_assert_int_eq(last_can_payload_sent[2], (request.pid & 0xFF00) >> 8);
+ ck_assert_int_eq(last_can_payload_sent[3], request.pid & 0xFF);
+ ck_assert_int_eq(last_can_payload_size, 4);
+
+ request.arbitration_id = 0x101;
+ request.pid = 0x12;
+
+ diagnostic_request(&SHIMS, &request, response_received_handler);
+
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_sent[1], request.mode);
+ ck_assert_int_eq(last_can_payload_sent[2], request.pid);
+ ck_assert_int_eq(last_can_payload_size, 3);
+}
+END_TEST
+
+START_TEST (test_request_pid_standard)
+{
+ uint16_t arb_id = OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST;
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+ DIAGNOSTIC_STANDARD_PID, arb_id, 0x2, response_received_handler);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x3, 0x1 + 0x40, 0x2, 0x45};
+ diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8,
+ can_data, sizeof(can_data));
+ fail_unless(last_response_was_received);
+ ck_assert(last_response_received.success);
+ ck_assert_int_eq(last_response_received.arbitration_id,
+ arb_id + 0x8);
+ ck_assert_int_eq(last_response_received.mode, 0x1);
+ fail_unless(last_response_received.has_pid);
+ ck_assert_int_eq(last_response_received.pid, 0x2);
+ ck_assert_int_eq(last_response_received.payload_length, 1);
+ ck_assert_int_eq(last_response_received.payload[0], can_data[3]);
+}
+END_TEST
+
+START_TEST (test_request_pid_enhanced)
+{
+ uint16_t arb_id = 0x100;
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+ DIAGNOSTIC_ENHANCED_PID, arb_id, 0x1234, response_received_handler);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x4, 0x22 + 0x40, 0x12, 0x34, 0x45};
+ diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8, can_data,
+ sizeof(can_data));
+ fail_unless(last_response_was_received);
+ ck_assert(last_response_received.success);
+ ck_assert_int_eq(last_response_received.arbitration_id,
+ arb_id + 0x8);
+ ck_assert_int_eq(last_response_received.mode, 0x22);
+ fail_unless(last_response_received.has_pid);
+ ck_assert_int_eq(last_response_received.pid, 0x1234);
+ ck_assert_int_eq(last_response_received.payload_length, 1);
+ ck_assert_int_eq(last_response_received.payload[0], can_data[4]);
+}
+END_TEST
+
+START_TEST (test_wrong_mode_response)
+{
+ uint16_t arb_id = 0x100;
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+ DIAGNOSTIC_ENHANCED_PID, arb_id, 0x1234, response_received_handler);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x4, 0x1 + 0x40, 0x12, 0x34, 0x45};
+ diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8, can_data,
+ sizeof(can_data));
+ fail_if(last_response_was_received);
+ fail_if(handle.completed);
+}
+END_TEST
+
+START_TEST (test_missing_pid)
+{
+ uint16_t arb_id = 0x100;
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+ DIAGNOSTIC_ENHANCED_PID, arb_id, 0x1234, response_received_handler);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x1, 0x22 + 0x40};
+ diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8, can_data,
+ sizeof(can_data));
+ fail_if(last_response_was_received);
+ fail_if(handle.completed);
+}
+END_TEST
+
+START_TEST (test_wrong_pid_response)
+{
+ uint16_t arb_id = 0x100;
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+ DIAGNOSTIC_ENHANCED_PID, arb_id, 0x1234, response_received_handler);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x4, 0x22 + 0x40, 0x12, 0x33, 0x45};
+ diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8, can_data,
+ sizeof(can_data));
+ fail_if(last_response_was_received);
+ fail_if(handle.completed);
+}
+END_TEST
+
+START_TEST (test_wrong_pid_then_right_completes)
+{
+ uint16_t arb_id = 0x100;
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+ DIAGNOSTIC_ENHANCED_PID, arb_id, 0x1234, response_received_handler);
+
+ fail_if(last_response_was_received);
+ uint8_t can_data[] = {0x4, 0x22 + 0x40, 0x12, 0x33, 0x45};
+ diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8, can_data,
+ sizeof(can_data));
+ fail_if(last_response_was_received);
+ fail_if(handle.completed);
+
+ can_data[3] = 0x34;
+ diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8, can_data,
+ sizeof(can_data));
+ fail_unless(last_response_was_received);
+ fail_unless(handle.completed);
+ fail_unless(handle.success);
+ fail_unless(last_response_received.success);
+ ck_assert_int_eq(last_response_received.pid, 0x1234);
+}
+END_TEST
+
+START_TEST (test_negative_response)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+ const uint8_t can_data[] = {0x3, 0x7f, request.mode,
+ NRC_SERVICE_NOT_SUPPORTED};
+ DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS, &handle,
+ request.arbitration_id + 0x8, can_data, sizeof(can_data));
+ fail_unless(response.completed);
+ fail_if(response.success);
+ fail_unless(handle.completed);
+
+ fail_if(last_response_received.success);
+ ck_assert_int_eq(last_response_received.arbitration_id,
+ request.arbitration_id + 0x8);
+ ck_assert_int_eq(last_response_received.mode, request.mode);
+ ck_assert_int_eq(last_response_received.pid, 0);
+ ck_assert_int_eq(last_response_received.negative_response_code,
+ NRC_SERVICE_NOT_SUPPORTED);
+ ck_assert_int_eq(last_response_received.payload_length, 0);
+}
+END_TEST
+
+START_TEST (test_payload_to_integer)
+{
+ uint16_t arb_id = OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST;
+ DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+ DIAGNOSTIC_STANDARD_PID, arb_id, 0x2, response_received_handler);
+
+ fail_if(last_response_was_received);
+ const uint8_t can_data[] = {0x4, 0x1 + 0x40, 0x2, 0x45, 0x12};
+ DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS, &handle,
+ arb_id + 0x8, can_data, sizeof(can_data));
+ ck_assert_int_eq(diagnostic_payload_to_integer(&response), 0x4512);
+}
+END_TEST
+
+START_TEST (test_response_multi_frame)
+{
+ DiagnosticRequest request = {
+ arbitration_id: 0x100,
+ mode: OBD2_MODE_VEHICLE_INFORMATION,
+ has_pid: true,
+ pid: 0x2
+ };
+ DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
+ response_received_handler);
+
+ const uint8_t can_data[] = {0x10, 0x14, 0x9 + 0x40, 0x2, 0x1, 0x31, 0x46, 0x4d};
+ DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS, &handle,
+ request.arbitration_id + 0x8, can_data, sizeof(can_data));
+
+ fail_unless(can_frame_was_sent);
+ fail_unless(!response.success);
+ fail_unless(!response.completed);
+ fail_unless(response.multi_frame);
+ ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
+ ck_assert_int_eq(last_can_payload_sent[0], 0x30);
+
+ const uint8_t can_data_1[] = {0x21, 0x43, 0x55, 0x39, 0x4a, 0x39, 0x34, 0x48};
+ response = diagnostic_receive_can_frame(&SHIMS, &handle,
+ request.arbitration_id + 0x8, can_data_1, sizeof(can_data_1));
+ fail_unless(!response.success);
+ fail_unless(!response.completed);
+ fail_unless(response.multi_frame);
+
+ const uint8_t can_data_2[] = {0x22, 0x55, 0x41, 0x30, 0x34, 0x35, 0x32, 0x34};
+ response = diagnostic_receive_can_frame(&SHIMS, &handle,
+ request.arbitration_id + 0x8, can_data_2, sizeof(can_data_2));
+ fail_unless(response.success);
+ fail_unless(response.completed);
+ fail_unless(response.multi_frame);
+ ck_assert_int_eq(response.mode, OBD2_MODE_VEHICLE_INFORMATION);
+ ck_assert_int_eq(response.pid, 0x2);
+ ck_assert_int_eq(response.payload_length, 18);
+ ck_assert_int_eq(response.payload[0], 0x01);
+ ck_assert_int_eq(response.payload[1], 0x31);
+ ck_assert_int_eq(response.payload[2], 0x46);
+ ck_assert_int_eq(response.payload[3], 0x4d);
+ ck_assert_int_eq(response.payload[4], 0x43);
+ ck_assert_int_eq(response.payload[5], 0x55);
+ ck_assert_int_eq(response.payload[6], 0x39);
+ ck_assert_int_eq(response.payload[7], 0x4a);
+ ck_assert_int_eq(response.payload[8], 0x39);
+ ck_assert_int_eq(response.payload[9], 0x34);
+ ck_assert_int_eq(response.payload[10], 0x48);
+ ck_assert_int_eq(response.payload[11], 0x55);
+ ck_assert_int_eq(response.payload[12], 0x41);
+ ck_assert_int_eq(response.payload[13], 0x30);
+ ck_assert_int_eq(response.payload[14], 0x34);
+ ck_assert_int_eq(response.payload[15], 0x35);
+ ck_assert_int_eq(response.payload[16], 0x32);
+ ck_assert_int_eq(response.payload[17], 0x34);
+}
+END_TEST
+
+Suite* testSuite(void) {
+ Suite* s = suite_create("uds");
+ TCase *tc_core = tcase_create("core");
+ tcase_add_checked_fixture(tc_core, setup, NULL);
+ tcase_add_test(tc_core, test_sent_message_no_padding);
+ tcase_add_test(tc_core, test_sent_message_is_padded);
+ tcase_add_test(tc_core, test_sent_message_is_padded_by_default);
+ tcase_add_test(tc_core, test_generate_diag_request);
+ tcase_add_test(tc_core, test_generate_then_send_request);
+ tcase_add_test(tc_core, test_send_diag_request);
+ tcase_add_test(tc_core, test_send_functional_request);
+ tcase_add_test(tc_core, test_send_diag_request_with_payload);
+ tcase_add_test(tc_core, test_receive_wrong_arb_id);
+ tcase_add_test(tc_core, test_autoset_pid_length);
+ tcase_add_test(tc_core, test_request_pid_standard);
+ tcase_add_test(tc_core, test_request_pid_enhanced);
+ tcase_add_test(tc_core, test_wrong_mode_response);
+ tcase_add_test(tc_core, test_wrong_pid_response);
+ tcase_add_test(tc_core, test_missing_pid);
+ tcase_add_test(tc_core, test_wrong_pid_then_right_completes);
+ tcase_add_test(tc_core, test_negative_response);
+ tcase_add_test(tc_core, test_payload_to_integer);
+ tcase_add_test(tc_core, test_response_multi_frame);
+
+ // TODO these are future work:
+ // TODO test request MIL
+ // TODO test request VIN
+ // TODO test request DTC
+ // TODO test clear DTC
+ // TODO test enumerate PIDs
+ suite_add_tcase(s, tc_core);
+
+ return s;
+}
+
+int main(void) {
+ int numberFailed;
+ Suite* s = testSuite();
+ SRunner *sr = srunner_create(s);
+ // Don't fork so we can actually use gdb
+ srunner_set_fork_status(sr, CK_NOFORK);
+ srunner_run_all(sr, CK_NORMAL);
+ numberFailed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (numberFailed == 0) ? 0 : 1;
+}