1 /*
2 * Copyright (c) 2022 Vestas Wind Systems A/S
3 * Copyright (c) 2019 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/drivers/can.h>
9 #include <zephyr/ztest.h>
10
11 #include "common.h"
12
13 /**
14 * @addtogroup t_driver_can
15 * @{
16 * @defgroup t_can_utilities test_can_utilities
17 * @}
18 */
19
20 /**
21 * @brief Test of @a can_dlc_to_bytes()
22 */
ZTEST(can_utilities,test_can_dlc_to_bytes)23 ZTEST(can_utilities, test_can_dlc_to_bytes)
24 {
25 uint8_t dlc;
26
27 /* CAN 2.0B/CAN FD DLC, 0 to 8 data bytes */
28 for (dlc = 0; dlc <= 8; dlc++) {
29 zassert_equal(can_dlc_to_bytes(dlc), dlc, "wrong number of bytes for DLC %u", dlc);
30 }
31
32 /* CAN FD DLC, 12 to 64 data bytes in steps */
33 zassert_equal(can_dlc_to_bytes(9), 12, "wrong number of bytes for DLC 9");
34 zassert_equal(can_dlc_to_bytes(10), 16, "wrong number of bytes for DLC 10");
35 zassert_equal(can_dlc_to_bytes(11), 20, "wrong number of bytes for DLC 11");
36 zassert_equal(can_dlc_to_bytes(12), 24, "wrong number of bytes for DLC 12");
37 zassert_equal(can_dlc_to_bytes(13), 32, "wrong number of bytes for DLC 13");
38 zassert_equal(can_dlc_to_bytes(14), 48, "wrong number of bytes for DLC 14");
39 zassert_equal(can_dlc_to_bytes(15), 64, "wrong number of bytes for DLC 15");
40 }
41
42 /**
43 * @brief Test of @a can_bytes_to_dlc()
44 */
ZTEST(can_utilities,test_can_bytes_to_dlc)45 ZTEST(can_utilities, test_can_bytes_to_dlc)
46 {
47 uint8_t bytes;
48
49 /* CAN 2.0B DLC, 0 to 8 data bytes */
50 for (bytes = 0; bytes <= 8; bytes++) {
51 zassert_equal(can_bytes_to_dlc(bytes), bytes, "wrong DLC for %u byte(s)", bytes);
52 }
53
54 /* CAN FD DLC, 12 to 64 data bytes in steps */
55 zassert_equal(can_bytes_to_dlc(12), 9, "wrong DLC for 12 bytes");
56 zassert_equal(can_bytes_to_dlc(16), 10, "wrong DLC for 16 bytes");
57 zassert_equal(can_bytes_to_dlc(20), 11, "wrong DLC for 20 bytes");
58 zassert_equal(can_bytes_to_dlc(24), 12, "wrong DLC for 24 bytes");
59 zassert_equal(can_bytes_to_dlc(32), 13, "wrong DLC for 32 bytes");
60 zassert_equal(can_bytes_to_dlc(48), 14, "wrong DLC for 48 bytes");
61 zassert_equal(can_bytes_to_dlc(64), 15, "wrong DLC for 64 bytes");
62 }
63
64 /**
65 * @brief Test of @a can_frame_matches_filter()
66 */
ZTEST(can_utilities,test_can_frame_matches_filter)67 ZTEST(can_utilities, test_can_frame_matches_filter)
68 {
69 const struct can_filter test_ext_filter_std_id_1 = {
70 .flags = CAN_FILTER_IDE,
71 .id = TEST_CAN_STD_ID_1,
72 .mask = CAN_EXT_ID_MASK
73 };
74
75 /* Standard (11-bit) frames and filters */
76 zassert_true(can_frame_matches_filter(&test_std_frame_1, &test_std_filter_1));
77 zassert_true(can_frame_matches_filter(&test_std_frame_2, &test_std_filter_2));
78 zassert_true(can_frame_matches_filter(&test_std_frame_1, &test_std_masked_filter_1));
79 zassert_true(can_frame_matches_filter(&test_std_frame_2, &test_std_masked_filter_2));
80 zassert_false(can_frame_matches_filter(&test_std_frame_1, &test_std_filter_2));
81 zassert_false(can_frame_matches_filter(&test_std_frame_2, &test_std_filter_1));
82 zassert_false(can_frame_matches_filter(&test_std_frame_1, &test_std_masked_filter_2));
83 zassert_false(can_frame_matches_filter(&test_std_frame_2, &test_std_masked_filter_1));
84
85 /* Extended (29-bit) frames and filters */
86 zassert_true(can_frame_matches_filter(&test_ext_frame_1, &test_ext_filter_1));
87 zassert_true(can_frame_matches_filter(&test_ext_frame_2, &test_ext_filter_2));
88 zassert_true(can_frame_matches_filter(&test_ext_frame_1, &test_ext_masked_filter_1));
89 zassert_true(can_frame_matches_filter(&test_ext_frame_2, &test_ext_masked_filter_2));
90 zassert_false(can_frame_matches_filter(&test_ext_frame_1, &test_ext_filter_2));
91 zassert_false(can_frame_matches_filter(&test_ext_frame_2, &test_ext_filter_1));
92 zassert_false(can_frame_matches_filter(&test_ext_frame_1, &test_ext_masked_filter_2));
93 zassert_false(can_frame_matches_filter(&test_ext_frame_2, &test_ext_masked_filter_1));
94
95 /* Standard (11-bit) frames and extended (29-bit) filters */
96 zassert_false(can_frame_matches_filter(&test_std_frame_1, &test_ext_filter_1));
97 zassert_false(can_frame_matches_filter(&test_std_frame_2, &test_ext_filter_2));
98 zassert_false(can_frame_matches_filter(&test_std_frame_1, &test_ext_masked_filter_1));
99 zassert_false(can_frame_matches_filter(&test_std_frame_2, &test_ext_masked_filter_2));
100 zassert_false(can_frame_matches_filter(&test_std_frame_1, &test_ext_filter_std_id_1));
101
102 /* Extended (29-bit) frames and standard (11-bit) filters */
103 zassert_false(can_frame_matches_filter(&test_ext_frame_1, &test_std_filter_1));
104 zassert_false(can_frame_matches_filter(&test_ext_frame_2, &test_std_filter_2));
105 zassert_false(can_frame_matches_filter(&test_ext_frame_1, &test_std_masked_filter_1));
106 zassert_false(can_frame_matches_filter(&test_ext_frame_2, &test_std_masked_filter_2));
107
108 /* Remote transmission request (RTR) frames */
109 zassert_true(can_frame_matches_filter(&test_std_rtr_frame_1, &test_std_filter_1));
110 zassert_true(can_frame_matches_filter(&test_ext_rtr_frame_1, &test_ext_filter_1));
111
112 #ifdef CONFIG_CAN_FD_MODE
113 /* CAN FD format frames and filters */
114 zassert_true(can_frame_matches_filter(&test_std_fdf_frame_1, &test_std_filter_1));
115 zassert_true(can_frame_matches_filter(&test_std_fdf_frame_2, &test_std_filter_2));
116 #endif /* CONFIG_CAN_FD_MODE */
117 }
118
119 ZTEST_SUITE(can_utilities, NULL, NULL, NULL, NULL, NULL);
120