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/logging/log.h>
9 #include <zephyr/net/socketcan.h>
10 #include <zephyr/net/socketcan_utils.h>
11 #include <zephyr/ztest.h>
12 
13 LOG_MODULE_REGISTER(socket_can, LOG_LEVEL_ERR);
14 
15 /**
16  * @brief Test of @a socketcan_to_can_frame()
17  */
ZTEST(socket_can,test_socketcan_frame_to_can_frame)18 ZTEST(socket_can, test_socketcan_frame_to_can_frame)
19 {
20 	struct socketcan_frame sframe = { 0 };
21 	struct can_frame expected = { 0 };
22 	struct can_frame zframe;
23 	const uint8_t data[SOCKETCAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04,
24 						   0x05, 0x06, 0x07, 0x08 };
25 
26 	sframe.can_id = BIT(31) | 1234;
27 	sframe.len = sizeof(data);
28 	memcpy(sframe.data, data, sizeof(sframe.data));
29 
30 	expected.flags = CAN_FRAME_IDE;
31 	expected.id = 1234U;
32 	expected.dlc = can_bytes_to_dlc(sizeof(data));
33 	memcpy(expected.data, data, sizeof(data));
34 
35 	socketcan_to_can_frame(&sframe, &zframe);
36 
37 	LOG_HEXDUMP_DBG((const uint8_t *)&sframe, sizeof(sframe), "sframe");
38 	LOG_HEXDUMP_DBG((const uint8_t *)&zframe, sizeof(zframe), "zframe");
39 	LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
40 
41 	zassert_equal(zframe.flags, expected.flags, "Flags not equal");
42 	zassert_equal(zframe.id, expected.id, "CAN id invalid");
43 	zassert_equal(zframe.dlc, expected.dlc, "Msg length invalid");
44 	zassert_mem_equal(&zframe.data, &expected.data, can_dlc_to_bytes(expected.dlc),
45 			  "CAN data not same");
46 
47 	/* Test RTR flag conversion after comparing data payload */
48 	sframe.can_id |= BIT(30);
49 	expected.flags |= CAN_FRAME_RTR;
50 
51 	socketcan_to_can_frame(&sframe, &zframe);
52 
53 	zassert_equal(zframe.flags, expected.flags, "Flags not equal");
54 	zassert_equal(zframe.id, expected.id, "CAN id invalid");
55 }
56 
57 /**
58  * @brief Test of @a socketcan_from_can_frame()
59  */
ZTEST(socket_can,test_can_frame_to_socketcan_frame)60 ZTEST(socket_can, test_can_frame_to_socketcan_frame)
61 {
62 	struct socketcan_frame sframe = { 0 };
63 	struct socketcan_frame expected = { 0 };
64 	struct can_frame zframe = { 0 };
65 	const uint8_t data[SOCKETCAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04,
66 						   0x05, 0x06, 0x07, 0x08 };
67 
68 	expected.can_id = BIT(31) | 1234;
69 	expected.len = sizeof(data);
70 	memcpy(expected.data, data, sizeof(expected.data));
71 
72 	zframe.flags = CAN_FRAME_IDE;
73 	zframe.id = 1234U;
74 	zframe.dlc = can_bytes_to_dlc(sizeof(data));
75 	memcpy(zframe.data, data, sizeof(data));
76 
77 	socketcan_from_can_frame(&zframe, &sframe);
78 
79 	LOG_HEXDUMP_DBG((const uint8_t *)&sframe, sizeof(sframe), "sframe");
80 	LOG_HEXDUMP_DBG((const uint8_t *)&zframe, sizeof(zframe), "zframe");
81 	LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
82 
83 	zassert_equal(sframe.can_id, expected.can_id, "CAN ID not same");
84 	zassert_equal(sframe.len, expected.len, "CAN msg length not same");
85 	zassert_mem_equal(&sframe.data, &expected.data, sizeof(sframe.data), "CAN data not same");
86 
87 	/* Test RTR flag conversion after comparing data payload */
88 	expected.can_id |= BIT(30);
89 	zframe.flags |= CAN_FRAME_RTR;
90 
91 	socketcan_from_can_frame(&zframe, &sframe);
92 	zassert_equal(sframe.can_id, expected.can_id, "CAN ID not same");
93 }
94 
95 /**
96  * @brief Test of @a socketcan_to_can_filter()
97  */
ZTEST(socket_can,test_socketcan_filter_to_can_filter)98 ZTEST(socket_can, test_socketcan_filter_to_can_filter)
99 {
100 	struct socketcan_filter sfilter = { 0 };
101 	struct can_filter expected = { 0 };
102 	struct can_filter zfilter = { 0 };
103 
104 	sfilter.can_id = BIT(31) | 1234;
105 	sfilter.can_mask = BIT(31) | 1234;
106 
107 	expected.flags = CAN_FILTER_IDE;
108 	expected.id = 1234U;
109 	expected.mask = 1234U;
110 
111 	socketcan_to_can_filter(&sfilter, &zfilter);
112 
113 	LOG_HEXDUMP_DBG((const uint8_t *)&zfilter, sizeof(zfilter), "zfilter");
114 	LOG_HEXDUMP_DBG((const uint8_t *)&sfilter, sizeof(sfilter), "sfilter");
115 	LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
116 
117 	zassert_equal(zfilter.flags, expected.flags, "Flags not equal");
118 	zassert_equal(zfilter.id, expected.id, "CAN id invalid");
119 	zassert_equal(zfilter.mask, expected.mask, "id mask not set");
120 }
121 
122 /**
123  * @brief Test of @a socketcan_from_can_filter()
124  */
ZTEST(socket_can,test_can_filter_to_socketcan_filter)125 ZTEST(socket_can, test_can_filter_to_socketcan_filter)
126 {
127 	struct socketcan_filter sfilter = { 0 };
128 	struct socketcan_filter expected = { 0 };
129 	struct can_filter zfilter = { 0 };
130 
131 	expected.can_id = BIT(31) | 1234;
132 	expected.can_mask = BIT(31) | 1234;
133 #ifndef CONFIG_CAN_ACCEPT_RTR
134 	expected.can_mask |= BIT(30);
135 #endif /* !CONFIG_CAN_ACCEPT_RTR */
136 
137 	zfilter.flags = CAN_FILTER_IDE;
138 	zfilter.id = 1234U;
139 	zfilter.mask = 1234U;
140 
141 	socketcan_from_can_filter(&zfilter, &sfilter);
142 
143 	LOG_HEXDUMP_DBG((const uint8_t *)&zfilter, sizeof(zfilter), "zfilter");
144 	LOG_HEXDUMP_DBG((const uint8_t *)&sfilter, sizeof(sfilter), "sfilter");
145 	LOG_HEXDUMP_DBG((const uint8_t *)&expected, sizeof(expected), "expected");
146 
147 	zassert_equal(sfilter.can_id, expected.can_id, "CAN ID not same");
148 	zassert_equal(sfilter.can_mask, expected.can_mask, "CAN mask not same");
149 }
150 
151 ZTEST_SUITE(socket_can, NULL, NULL, NULL, NULL, NULL);
152