Add a failing test for a simple diag request/response.
authorChristopher Peplin <chris.peplin@rhubarbtech.com>
Tue, 31 Dec 2013 01:40:50 +0000 (20:40 -0500)
committerChristopher Peplin <chris.peplin@rhubarbtech.com>
Tue, 31 Dec 2013 01:40:50 +0000 (20:40 -0500)
src/obd2/obd2.h
tests/common.c
tests/test_core.c

index e6b5bc9..411170d 100644 (file)
@@ -12,11 +12,6 @@ extern "C" {
 #define MAX_OBD2_PAYLOAD_LENGTH 7
 #define VIN_LENGTH 17
 
-typedef void (*LogShim)(const char* message);
-typedef bool (*SendCanMessageShim)(const uint16_t arbitration_id,
-        const uint8_t* data, const uint8_t size);
-typedef bool (*SetTimerShim)(uint16_t time_ms, void (*callback));
-
 typedef struct {
     uint16_t arbitration_id;
     uint8_t mode;
@@ -29,6 +24,7 @@ typedef struct {
 // http://www.canbushack.com/blog/index.php?title=scanning-for-diagnostic-data&more=1&c=1&tb=1&pb=1
 // for the list of NRCs
 typedef enum {
+    NRC_SUCCESS = 0x0,
     NRC_SERVICE_NOT_SUPPORTED = 0x11,
     NRC_SUB_FUNCTION_NOT_SUPPORTED = 0x12,
     NRC_CONDITIONS_NOT_CORRECT = 0x22,
@@ -162,7 +158,7 @@ bool diagnostic_clear_dtc(DiagnosticShims* shims);
 DiagnosticRequestHandle diagnostic_enumerate_pids(DiagnosticShims* shims,
         DiagnosticRequest* request, DiagnosticPidEnumerationReceived callback);
 
-void diagnostic_receive_can_frame(DiagnosticRequestHandle* handler,
+void diagnostic_receive_can_frame(DiagnosticRequestHandle* handle,
         const uint16_t arbitration_id, const uint8_t data[],
         const uint8_t size);
 
index e69de29..e35ef52 100644 (file)
@@ -0,0 +1,50 @@
+#include <obd2/obd2.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+DiagnosticShims SHIMS;
+
+uint16_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);
+}
+
+void mock_send_can(const uint16_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);
+    }
+}
+
+void mock_set_timer(uint16_t time_ms, void (*callback)) {
+}
+
+void response_received_handler(const DiagnosticResponse* response) {
+    last_response_was_received = true;
+    // TODO not sure if we can copy the struct like this
+    last_response_received = *response;
+}
+
+void setup() {
+    SHIMS = diagnostic_init_shims(debug, mock_send_can, mock_set_timer);
+    memset(last_can_payload_sent, 0, sizeof(last_can_payload_sent));
+    can_frame_was_sent = false;
+    last_response_was_received = false;
+}
+
index 65af858..35e948e 100644 (file)
@@ -1,13 +1,56 @@
+#include <obd2/obd2.h>
 #include <check.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdbool.h>
 
-void setup() {
+extern void setup();
+extern bool last_response_was_received;
+extern DiagnosticResponse last_response_received;
+extern DiagnosticShims SHIMS;
+extern DiagnosticResponseReceived response_received_handler;
 
+START_TEST (test_receive_wrong_arb_id)
+{
+    ck_assert(false);
 }
+END_TEST
+
+START_TEST (test_send_diag_request)
+{
+    DiagnosticRequest request = {
+        arbitration_id: 0x7df,
+        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(&handle, request.arbitration_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,
+            request.arbitration_id + 0x8);
+    // TODO should we set it back to the original mode, or leave as mode + 0x40?
+    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]);
+}
+END_TEST
 
-START_TEST (test_fail)
+START_TEST (test_request_pid_standard)
+{
+    fail_unless(false);
+    // TODO test request pid, do the same rigamarole
+    //      kind of leaky, but check that the returned DiagnosticRequest Handle
+    //      has the right PID
+}
+END_TEST
+
+START_TEST (test_request_pid_enhanced)
 {
     fail_unless(false);
 }
@@ -17,7 +60,17 @@ Suite* testSuite(void) {
     Suite* s = suite_create("obd2");
     TCase *tc_core = tcase_create("core");
     tcase_add_checked_fixture(tc_core, setup, NULL);
-    tcase_add_test(tc_core, test_fail);
+    tcase_add_test(tc_core, test_send_diag_request);
+    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);
+
+    // 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;