Add an option to pad outgoing CAN frames to 8 bytes (on by default).
authorChristopher Peplin <chris.peplin@rhubarbtech.com>
Fri, 14 Feb 2014 23:06:40 +0000 (18:06 -0500)
committerChristopher Peplin <chris.peplin@rhubarbtech.com>
Fri, 14 Feb 2014 23:07:29 +0000 (18:07 -0500)
Fixed #3.

deps/isotp-c
src/uds/uds.c
src/uds/uds_types.h
tests/test_core.c

index 513d8c8..c01a88b 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 513d8c8d7089960618a1fa00a71442dc39294588
+Subproject commit c01a88ba1e56d455c7187a52a5e244500a0d6b0f
index f38c6aa..9fec9a8 100644 (file)
@@ -87,6 +87,7 @@ DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
     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,
@@ -121,7 +122,7 @@ DiagnosticRequestHandle diagnostic_request(DiagnosticShims* shims,
     // 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 woudl a timer callback look like...it would need to pass the handle
+    // 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
index 88c7ce4..10d15d6 100644 (file)
@@ -42,6 +42,10 @@ typedef enum {
  * 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 {
@@ -52,6 +56,7 @@ typedef struct {
     uint8_t pid_length;
     uint8_t payload[MAX_UDS_PAYLOAD_LENGTH];
     uint8_t payload_length;
+    bool no_frame_padding;
     DiagnosticRequestType type;
 } DiagnosticRequest;
 
index 9f118fc..815954b 100644 (file)
@@ -40,7 +40,8 @@ START_TEST (test_send_diag_request_with_payload)
         arbitration_id: 0x100,
         mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST,
         payload: {0x12, 0x34},
-        payload_length: 2
+        payload_length: 2,
+        no_frame_padding: true
     };
     DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
             response_received_handler);
@@ -60,7 +61,8 @@ START_TEST (test_send_functional_request)
 {
     DiagnosticRequest request = {
         arbitration_id: OBD2_FUNCTIONAL_BROADCAST_ID,
-        mode: OBD2_MODE_EMISSIONS_DTC_REQUEST
+        mode: OBD2_MODE_EMISSIONS_DTC_REQUEST,
+        no_frame_padding: true
     };
     DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
             response_received_handler);
@@ -91,7 +93,23 @@ START_TEST (test_send_functional_request)
 }
 END_TEST
 
-START_TEST (test_send_diag_request)
+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,
@@ -100,6 +118,38 @@ START_TEST (test_send_diag_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);
     ck_assert_int_eq(last_can_frame_sent_arb_id, request.arbitration_id);
     ck_assert_int_eq(last_can_payload_sent[1], request.mode);
@@ -131,19 +181,22 @@ START_TEST (test_autoset_pid_length)
     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);
-    ck_assert_int_eq(last_can_payload_size, 3);
+    // 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: 2
+        pid: 2,
+        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_size, 4);
+    ck_assert_int_eq(last_can_payload_size, 4);
 }
 END_TEST
 
@@ -303,6 +356,9 @@ 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_send_diag_request);
     tcase_add_test(tc_core, test_send_functional_request);
     tcase_add_test(tc_core, test_send_diag_request_with_payload);