a466e8e0c2fcb50f2dd1b530f0ee398e41b34cb5
[apps/agl-service-can-low-level.git] / tests / test_core.c
1 #include <obd2/obd2.h>
2 #include <check.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdbool.h>
6
7 extern void setup();
8 extern bool last_response_was_received;
9 extern DiagnosticResponse last_response_received;
10 extern DiagnosticShims SHIMS;
11 extern DiagnosticResponseReceived response_received_handler;
12
13 START_TEST (test_receive_wrong_arb_id)
14 {
15     DiagnosticRequest request = {
16         arbitration_id: 0x7df,
17         mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST
18     };
19     DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
20             response_received_handler);
21
22     fail_if(last_response_was_received);
23     const uint8_t can_data[] = {0x2, request.mode + 0x40, 0x23};
24     diagnostic_receive_can_frame(&SHIMS, &handle, request.arbitration_id,
25             can_data, sizeof(can_data));
26     fail_if(last_response_was_received);
27 }
28 END_TEST
29
30 START_TEST (test_send_diag_request)
31 {
32     DiagnosticRequest request = {
33         arbitration_id: 0x7df,
34         mode: OBD2_MODE_POWERTRAIN_DIAGNOSTIC_REQUEST
35     };
36     DiagnosticRequestHandle handle = diagnostic_request(&SHIMS, &request,
37             response_received_handler);
38
39     fail_if(handle.completed);
40
41     fail_if(last_response_was_received);
42     const uint8_t can_data[] = {0x2, request.mode + 0x40, 0x23};
43     DiagnosticResponse response = diagnostic_receive_can_frame(&SHIMS, &handle,
44             request.arbitration_id + 0x8, can_data, sizeof(can_data));
45     fail_unless(response.success);
46     fail_unless(response.completed);
47     fail_unless(handle.completed);
48     fail_unless(last_response_was_received);
49     ck_assert(last_response_received.success);
50     ck_assert_int_eq(last_response_received.arbitration_id,
51             request.arbitration_id + 0x8);
52     // TODO should we set it back to the original mode, or leave as mode + 0x40?
53     ck_assert_int_eq(last_response_received.mode, request.mode);
54     ck_assert_int_eq(last_response_received.pid, 0);
55     ck_assert_int_eq(last_response_received.payload_length, 1);
56     ck_assert_int_eq(last_response_received.payload[0], can_data[2]);
57 }
58 END_TEST
59
60 START_TEST (test_request_pid_standard)
61 {
62     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
63             DIAGNOSTIC_STANDARD_PID, 0x2, response_received_handler);
64
65     fail_if(last_response_was_received);
66     const uint8_t can_data[] = {0x3, 0x1 + 0x40, 0x2, 0x45};
67     // TODO need a constant for the 7df broadcast functional request
68     diagnostic_receive_can_frame(&SHIMS, &handle, 0x7df + 0x8,
69             can_data, sizeof(can_data));
70     fail_unless(last_response_was_received);
71     ck_assert(last_response_received.success);
72     ck_assert_int_eq(last_response_received.arbitration_id,
73             0x7df + 0x8);
74     // TODO should we set it back to the original mode, or leave as mode + 0x40?
75     ck_assert_int_eq(last_response_received.mode, 0x1);
76     ck_assert_int_eq(last_response_received.pid, 0x2);
77     ck_assert_int_eq(last_response_received.payload_length, 1);
78     ck_assert_int_eq(last_response_received.payload[0], can_data[3]);
79 }
80 END_TEST
81
82 START_TEST (test_request_pid_enhanced)
83 {
84     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
85             DIAGNOSTIC_ENHANCED_PID, 0x2, response_received_handler);
86
87     fail_if(last_response_was_received);
88     const uint8_t can_data[] = {0x4, 0x1 + 0x40, 0x0, 0x2, 0x45};
89     // TODO need a constant for the 7df broadcast functional request
90     diagnostic_receive_can_frame(&SHIMS, &handle, 0x7df + 0x8, can_data,
91             sizeof(can_data));
92     fail_unless(last_response_was_received);
93     ck_assert(last_response_received.success);
94     ck_assert_int_eq(last_response_received.arbitration_id,
95             0x7df + 0x8);
96     // TODO should we set it back to the original mode, or leave as mode + 0x40?
97     ck_assert_int_eq(last_response_received.mode, 0x22);
98     ck_assert_int_eq(last_response_received.pid, 0x2);
99     ck_assert_int_eq(last_response_received.payload_length, 1);
100     ck_assert_int_eq(last_response_received.payload[0], can_data[4]);
101 }
102 END_TEST
103
104 Suite* testSuite(void) {
105     Suite* s = suite_create("obd2");
106     TCase *tc_core = tcase_create("core");
107     tcase_add_checked_fixture(tc_core, setup, NULL);
108     tcase_add_test(tc_core, test_send_diag_request);
109     tcase_add_test(tc_core, test_receive_wrong_arb_id);
110     tcase_add_test(tc_core, test_request_pid_standard);
111     tcase_add_test(tc_core, test_request_pid_enhanced);
112
113     // TODO these are future work:
114     // TODO test request MIL
115     // TODO test request VIN
116     // TODO test request DTC
117     // TODO test clear DTC
118     // TODO test enumerate PIDs
119     suite_add_tcase(s, tc_core);
120
121     return s;
122 }
123
124 int main(void) {
125     int numberFailed;
126     Suite* s = testSuite();
127     SRunner *sr = srunner_create(s);
128     // Don't fork so we can actually use gdb
129     srunner_set_fork_status(sr, CK_NOFORK);
130     srunner_run_all(sr, CK_NORMAL);
131     numberFailed = srunner_ntests_failed(sr);
132     srunner_free(sr);
133     return (numberFailed == 0) ? 0 : 1;
134 }