340a32682350ca876ad2c605733b00751b1ca05d
[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(&handle, request.arbitration_id, can_data,
25             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(last_response_was_received);
40     const uint8_t can_data[] = {0x2, request.mode + 0x40, 0x23};
41     diagnostic_receive_can_frame(&handle, request.arbitration_id + 0x8,
42             can_data, sizeof(can_data));
43     fail_unless(last_response_was_received);
44     ck_assert(last_response_received.success);
45     ck_assert_int_eq(last_response_received.arbitration_id,
46             request.arbitration_id + 0x8);
47     // TODO should we set it back to the original mode, or leave as mode + 0x40?
48     ck_assert_int_eq(last_response_received.mode, request.mode);
49     ck_assert_int_eq(last_response_received.pid, 0);
50     ck_assert_int_eq(last_response_received.payload_length, 1);
51     ck_assert_int_eq(last_response_received.payload[0], can_data[2]);
52 }
53 END_TEST
54
55 START_TEST (test_request_pid_standard)
56 {
57     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
58             DIAGNOSTIC_STANDARD_PID, 0x2, response_received_handler);
59
60     fail_if(last_response_was_received);
61     const uint8_t can_data[] = {0x3, 0x1 + 0x40, 0x2, 0x45};
62     // TODO need a constant for the 7df broadcast functional request
63     diagnostic_receive_can_frame(&handle, 0x7df + 0x8,
64             can_data, sizeof(can_data));
65     fail_unless(last_response_was_received);
66     ck_assert(last_response_received.success);
67     ck_assert_int_eq(last_response_received.arbitration_id,
68             0x7df + 0x8);
69     // TODO should we set it back to the original mode, or leave as mode + 0x40?
70     ck_assert_int_eq(last_response_received.mode, 0x1);
71     ck_assert_int_eq(last_response_received.pid, 0x2);
72     ck_assert_int_eq(last_response_received.payload_length, 1);
73     ck_assert_int_eq(last_response_received.payload[0], can_data[3]);
74 }
75 END_TEST
76
77 START_TEST (test_request_pid_enhanced)
78 {
79     DiagnosticRequestHandle handle = diagnostic_request_pid(&SHIMS,
80             DIAGNOSTIC_ENHANCED_PID, 0x2, response_received_handler);
81
82     fail_if(last_response_was_received);
83     const uint8_t can_data[] = {0x4, 0x1 + 0x40, 0x0, 0x2, 0x45};
84     // TODO need a constant for the 7df broadcast functional request
85     diagnostic_receive_can_frame(&handle, 0x7df + 0x8, can_data,
86             sizeof(can_data));
87     fail_unless(last_response_was_received);
88     ck_assert(last_response_received.success);
89     ck_assert_int_eq(last_response_received.arbitration_id,
90             0x7df + 0x8);
91     // TODO should we set it back to the original mode, or leave as mode + 0x40?
92     ck_assert_int_eq(last_response_received.mode, 0x22);
93     ck_assert_int_eq(last_response_received.pid, 0x2);
94     ck_assert_int_eq(last_response_received.payload_length, 1);
95     ck_assert_int_eq(last_response_received.payload[0], can_data[4]);
96 }
97 END_TEST
98
99 Suite* testSuite(void) {
100     Suite* s = suite_create("obd2");
101     TCase *tc_core = tcase_create("core");
102     tcase_add_checked_fixture(tc_core, setup, NULL);
103     tcase_add_test(tc_core, test_send_diag_request);
104     tcase_add_test(tc_core, test_receive_wrong_arb_id);
105     tcase_add_test(tc_core, test_request_pid_standard);
106     tcase_add_test(tc_core, test_request_pid_enhanced);
107
108     // TODO these are future work:
109     // TODO test request MIL
110     // TODO test request VIN
111     // TODO test request DTC
112     // TODO test clear DTC
113     // TODO test enumerate PIDs
114     suite_add_tcase(s, tc_core);
115
116     return s;
117 }
118
119 int main(void) {
120     int numberFailed;
121     Suite* s = testSuite();
122     SRunner *sr = srunner_create(s);
123     // Don't fork so we can actually use gdb
124     srunner_set_fork_status(sr, CK_NOFORK);
125     srunner_run_all(sr, CK_NORMAL);
126     numberFailed = srunner_ntests_failed(sr);
127     srunner_free(sr);
128     return (numberFailed == 0) ? 0 : 1;
129 }