Test receiving a negative response.
authorChristopher Peplin <chris.peplin@rhubarbtech.com>
Fri, 3 Jan 2014 20:09:10 +0000 (15:09 -0500)
committerChristopher Peplin <chris.peplin@rhubarbtech.com>
Fri, 3 Jan 2014 20:09:10 +0000 (15:09 -0500)
src/obd2/obd2.c
tests/test_core.c

index 627128c..05be834 100644 (file)
@@ -143,6 +143,7 @@ DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
         IsoTpMessage message = isotp_continue_receive(&handle->isotp_shims,
                 &handle->isotp_receive_handle, arbitration_id, data, size);
 
+        // TODO this could be cleaned and DRY'd up a bit
         if(message.completed) {
             if(message.size > 0) {
                 response.mode = message.payload[0];
@@ -158,6 +159,7 @@ DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
                     if(message.size > NEGATIVE_RESPONSE_NRC_INDEX) {
                         response.negative_response_code = message.payload[NEGATIVE_RESPONSE_NRC_INDEX];
                     }
+
                     response.success = false;
                 } else {
                     if(response.mode == handle->request.mode + MODE_RESPONSE_OFFSET) {
@@ -189,9 +191,9 @@ DiagnosticResponse diagnostic_receive_can_frame(DiagnosticShims* shims,
             }
 
             response.completed = true;
-            // TODO what does it mean for the handle to be successful, vs. the
-            // request to be successful? if we get a NRC, is that a successful
-            // request?
+            // TODO clarify what it means for a handle to be successful (we made
+            // a good request+response) vs a request itself being successfuly
+            // (the other node didn't return a negative response).
             handle->success = true;
             handle->completed = true;
 
index dbe2ead..6e1960a 100644 (file)
@@ -180,6 +180,31 @@ START_TEST (test_handle_completed)
 }
 END_TEST
 
+START_TEST (test_negative_response)
+{
+    DiagnosticRequest request = {
+        arbitration_id: 0x7df,
+        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
+
 Suite* testSuite(void) {
     Suite* s = suite_create("obd2");
     TCase *tc_core = tcase_create("core");
@@ -191,6 +216,7 @@ Suite* testSuite(void) {
     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_negative_response);
 
     // TODO these are future work:
     // TODO test request MIL