Rename from simply obd2 to more general UDS (unified diagnostics).
[apps/low-level-can-service.git] / tests / test_core.c
index 51dff33..489e174 100644 (file)
@@ -1,4 +1,4 @@
-#include <obd2/obd2.h>
+#include <uds/uds.h>
 #include <check.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -20,7 +20,7 @@ void response_received_handler(const DiagnosticResponse* response) {
 START_TEST (test_receive_wrong_arb_id)
 {
     DiagnosticRequest request = {
-        arbitration_id: 0x7df,
+        arbitration_id: 0x100,
         mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST
     };
     DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
@@ -37,7 +37,7 @@ END_TEST
 START_TEST (test_send_diag_request_with_payload)
 {
     DiagnosticRequest request = {
-        arbitration_id: 0x7df,
+        arbitration_id: 0x100,
         mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST,
         payload: {0x12, 0x34},
         payload_length: 2
@@ -56,10 +56,45 @@ START_TEST (test_send_diag_request_with_payload)
 }
 END_TEST
 
+START_TEST (test_send_functional_request)
+{
+    DiagnosticRequest request = {
+        arbitration_id: OBD2_FUNCTIONAL_BROADCAST_ID,
+        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_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_send_diag_request)
 {
     DiagnosticRequest request = {
-        arbitration_id: 0x7df,
+        arbitration_id: 0x100,
         mode: OBD2_MODE_EMISSIONS_DTC_REQUEST
     };
     DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
@@ -81,7 +116,7 @@ START_TEST (test_send_diag_request)
     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);
+        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]);
 }
@@ -89,8 +124,7 @@ END_TEST
 
 START_TEST (test_request_pid_standard)
 {
-    // TODO need a constant for the 7df broadcast functional request
-    uint16_t arb_id = 0x7df;
+    uint16_t arb_id = OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST;
     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
             DIAGNOSTIC_STANDARD_PID, arb_id, 0x2, response_received_handler);
 
@@ -103,6 +137,7 @@ START_TEST (test_request_pid_standard)
     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]);
@@ -111,7 +146,7 @@ END_TEST
 
 START_TEST (test_request_pid_enhanced)
 {
-    uint16_t arb_id = 0x7df;
+    uint16_t arb_id = 0x100;
     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
             DIAGNOSTIC_ENHANCED_PID, arb_id, 0x2, response_received_handler);
 
@@ -124,6 +159,7 @@ START_TEST (test_request_pid_enhanced)
     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, 0x2);
     ck_assert_int_eq(last_response_received.payload_length, 1);
     ck_assert_int_eq(last_response_received.payload[0], can_data[4]);
@@ -132,7 +168,7 @@ END_TEST
 
 START_TEST (test_wrong_mode_response)
 {
-    uint16_t arb_id = 0x7df;
+    uint16_t arb_id = 0x100;
     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
             DIAGNOSTIC_ENHANCED_PID, arb_id, 0x2, response_received_handler);
 
@@ -140,52 +176,69 @@ START_TEST (test_wrong_mode_response)
     const uint8_t can_data[] = {0x4, 0x1 + 0x40, 0x0, 0x2, 0x45};
     diagnostic_receive_can_frame(&SHIMS, &handle, arb_id + 0x8, can_data,
             sizeof(can_data));
-    // TODO change this if we even re-request a message receipt on a mode or PID
-    // mismatch
-    fail_unless(last_response_was_received);
-    fail_unless(handle.completed);
-    fail_if(last_response_received.success);
+    fail_if(last_response_was_received);
+    fail_if(handle.completed);
 }
 END_TEST
 
-START_TEST (test_handle_completed)
+START_TEST (test_missing_pid)
 {
-    DiagnosticRequest request = {
-        arbitration_id: 0x7df,
-        mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST
-    };
-    DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
-            response_received_handler);
+    uint16_t arb_id = 0x100;
+    DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+            DIAGNOSTIC_ENHANCED_PID, arb_id, 0x2, 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
 
-    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);
+START_TEST (test_wrong_pid_response)
+{
+    uint16_t arb_id = 0x100;
+    DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+            DIAGNOSTIC_ENHANCED_PID, arb_id, 0x2, response_received_handler);
 
-    response = diagnostic_receive_can_frame(&SHIMS, &handle,
-            request.arbitration_id + 0x8, can_data, sizeof(can_data));
-    fail_if(response.success);
-    fail_if(response.completed);
-    fail_unless(handle.completed);
+    fail_if(last_response_was_received);
+    const uint8_t can_data[] = {0x4, 0x22 + 0x40, 0x0, 0x3, 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
 
-    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);
-    ck_assert_int_eq(last_response_received.pid, 0);
-    ck_assert_int_eq(last_response_received.payload_length, 1);
-    ck_assert_int_eq(last_response_received.payload[0], can_data[2]);
+START_TEST (test_wrong_pid_then_right_completes)
+{
+    uint16_t arb_id = 0x100;
+    DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
+            DIAGNOSTIC_ENHANCED_PID, arb_id, 0x2, response_received_handler);
+
+    fail_if(last_response_was_received);
+    uint8_t can_data[] = {0x4, 0x22 + 0x40, 0x0, 0x3, 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] = 0x2;
+    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, 0x2);
 }
 END_TEST
 
 START_TEST (test_negative_response)
 {
     DiagnosticRequest request = {
-        arbitration_id: 0x7df,
+        arbitration_id: 0x100,
         mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST
     };
     DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
@@ -208,16 +261,19 @@ START_TEST (test_negative_response)
 END_TEST
 
 Suite* testSuite(void) {
-    Suite* s = suite_create("obd2");
+    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_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_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_handle_completed);
+    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);
 
     // TODO these are future work: