1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/ztest.h>
7 #include <zephyr/debug/mipi_stp_decoder.h>
8 
9 static int cnt;
10 static enum mipi_stp_decoder_ctrl_type exp_type[10];
11 static union mipi_stp_decoder_data exp_data[10];
12 static size_t exp_data_len[10];
13 static uint64_t exp_ts[10];
14 static bool exp_marked[10];
15 static int d_cnt;
16 
cb(enum mipi_stp_decoder_ctrl_type type,union mipi_stp_decoder_data data,uint64_t * ts,bool marked)17 static void cb(enum mipi_stp_decoder_ctrl_type type, union mipi_stp_decoder_data data, uint64_t *ts,
18 	       bool marked)
19 {
20 	zassert_equal(exp_type[d_cnt], type, "Expected: %d got:%d", exp_type[d_cnt], type);
21 
22 	if (exp_ts[d_cnt] == UINT64_MAX) {
23 		zassert_equal(ts, NULL, NULL);
24 	} else {
25 		zassert_true(ts != NULL, NULL);
26 		zassert_equal(exp_ts[d_cnt], *ts, "exp:%llx got:%llx", exp_ts[d_cnt], *ts);
27 	}
28 
29 	zassert_equal(exp_marked[d_cnt], marked, NULL);
30 	zassert_equal(
31 		memcmp((uint8_t *)&exp_data[d_cnt], (uint8_t *)&data.data, exp_data_len[d_cnt]), 0,
32 		NULL);
33 	d_cnt++;
34 }
35 
36 static const struct mipi_stp_decoder_config config = {
37 	.cb = cb,
38 };
39 
40 #define ADD_ITEM(_cnt, _type, _ts, _marked, _data)                                                 \
41 	do {                                                                                       \
42 		exp_type[_cnt] = _type;                                                            \
43 		exp_ts[_cnt] = (uint64_t)_ts;                                                      \
44 		exp_marked[_cnt] = _marked;                                                        \
45 		exp_data[_cnt].data = _data;                                                       \
46 		exp_data_len[_cnt++] = sizeof(_data);                                              \
47 	} while (0)
48 
ZTEST(mipi_stp_decoder_test,test_chunk_null)49 ZTEST(mipi_stp_decoder_test, test_chunk_null)
50 {
51 	uint8_t data[] = {0x00, 0x00};
52 
53 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
54 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
55 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
56 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
57 
58 	mipi_stp_decoder_decode(data, sizeof(data));
59 	zassert_equal(cnt, d_cnt, NULL);
60 }
61 
ZTEST(mipi_stp_decoder_test,test_chunk_master)62 ZTEST(mipi_stp_decoder_test, test_chunk_master)
63 {
64 	/* 0x1(m8) 0xab 0x0 (null) 0xf1(m16) 0x3412 */
65 	uint8_t data[] = {0xa1, 0x0b, 0x1f, 0x34, 0x12};
66 
67 	ADD_ITEM(cnt, STP_DECODER_MASTER, UINT64_MAX, false, (uint8_t)0xab);
68 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
69 	ADD_ITEM(cnt, STP_DECODER_MASTER, UINT64_MAX, false, (uint16_t)0x4321);
70 
71 	mipi_stp_decoder_decode(data, sizeof(data));
72 	zassert_equal(cnt, d_cnt, NULL);
73 }
74 
ZTEST(mipi_stp_decoder_test,test_chunk_channel)75 ZTEST(mipi_stp_decoder_test, test_chunk_channel)
76 {
77 	/* 0(null) 1(m8) ab 3(c8) ab f3(c16) 4664 3(c8) bb 1(m8) 0b 3(c8) aa*/
78 	uint8_t data[] = {0x10, 0xba, 0xa3, 0xfb, 0x63, 0x44, 0x36, 0xbb, 0x01, 0x3b, 0xaa};
79 
80 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
81 	ADD_ITEM(cnt, STP_DECODER_MASTER, UINT64_MAX, false, (uint8_t)0xab);
82 	ADD_ITEM(cnt, STP_DECODER_CHANNEL, UINT64_MAX, false, (uint8_t)0xab);
83 	ADD_ITEM(cnt, STP_DECODER_CHANNEL, UINT64_MAX, false, (uint16_t)0x6446);
84 	/* MSB byte is taken from previous C16 */
85 	ADD_ITEM(cnt, STP_DECODER_CHANNEL, UINT64_MAX, false, (uint16_t)0x64bb);
86 	ADD_ITEM(cnt, STP_DECODER_MASTER, UINT64_MAX, false, (uint8_t)0x0b);
87 	/* M8 resets current channel */
88 	ADD_ITEM(cnt, STP_DECODER_CHANNEL, UINT64_MAX, false, (uint8_t)0xaa);
89 
90 	mipi_stp_decoder_decode(data, sizeof(data));
91 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
92 }
93 
ZTEST(mipi_stp_decoder_test,test_chunk_data)94 ZTEST(mipi_stp_decoder_test, test_chunk_data)
95 {
96 	/* 4(d8) ab 5(d16) 0x3456 6(d32) 0x11223344 7(d64) 0x1020304050607080 */
97 	/* f8(dm8) ab f9(dm16) 0x3456 fa(dm32) 0x11223344 fb(dm64) 0x1020304050607080 */
98 	uint8_t data[] = {0xa4, 0x5b, 0x43, 0x65, 0x16, 0x21, 0x32, 0x43, 0x74, 0x01,
99 			  0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
100 
101 			  0x8f, 0xba, 0x9f, 0x43, 0x65, 0xaf, 0x11, 0x22, 0x33, 0x44,
102 			  0xbf, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
103 
104 	ADD_ITEM(cnt, STP_DATA8, UINT64_MAX, false, (uint8_t)0xab);
105 	ADD_ITEM(cnt, STP_DATA16, UINT64_MAX, false, (uint16_t)0x3456);
106 	ADD_ITEM(cnt, STP_DATA32, UINT64_MAX, false, (uint32_t)0x11223344);
107 	ADD_ITEM(cnt, STP_DATA64, UINT64_MAX, false, (uint32_t)0x1020304050607080);
108 	ADD_ITEM(cnt, STP_DATA8, UINT64_MAX, true, (uint8_t)0xab);
109 	ADD_ITEM(cnt, STP_DATA16, UINT64_MAX, true, (uint16_t)0x3456);
110 	ADD_ITEM(cnt, STP_DATA32, UINT64_MAX, true, (uint32_t)0x11223344);
111 	ADD_ITEM(cnt, STP_DATA64, UINT64_MAX, true, (uint32_t)0x1020304050607080);
112 
113 	mipi_stp_decoder_decode(data, sizeof(data));
114 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
115 }
116 
ZTEST(mipi_stp_decoder_test,test_chunk_data_ts)117 ZTEST(mipi_stp_decoder_test, test_chunk_data_ts)
118 {
119 	uint8_t data[] = {
120 		/*d8ts + 13b TS */ 0x4f,
121 		0xba,
122 		0x1d,
123 		0x21,
124 		0x32,
125 		0x43,
126 		0x54,
127 		0x65,
128 		0x76,
129 		0x07,
130 		/*d16ts + 3b TS */ 0x5f,
131 		0xba,
132 		0xdc,
133 		0x13,
134 		0x22,
135 		/*d32ts + 3b TS */ 0x6f,
136 		0x11,
137 		0x22,
138 		0xba,
139 		0xdc,
140 		0x13,
141 		0x22,
142 		/*d64ts + 3b TS */ 0x7f,
143 		0x11,
144 		0x22,
145 		0xba,
146 		0xdc,
147 		0x11,
148 		0x22,
149 		0x33,
150 		0x44,
151 		0x13,
152 		0x22,
153 		/*d8mts + 14b TS */ 0xa8,
154 		0xeb,
155 		0x11,
156 		0x22,
157 		0x33,
158 		0x44,
159 		0x55,
160 		0x66,
161 		0x77,
162 		0x88,
163 		/*d16mts + 2b TS */ 0xa9,
164 		0xcb,
165 		0x2d,
166 		0x31,
167 		/*d32mts + 2b TS */ 0xaa,
168 		0xcb,
169 		0x1d,
170 		0x21,
171 		0x22,
172 		0x31,
173 		/*d64mts + 2b TS */ 0xab,
174 		0xcb,
175 		0x1d,
176 		0x21,
177 		0x12,
178 		0x11,
179 		0x11,
180 		0x11,
181 		0x21,
182 		0x31,
183 	};
184 
185 	ADD_ITEM(cnt, STP_DATA8, 0x11223344556677, false, (uint8_t)0xab);
186 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
187 	ADD_ITEM(cnt, STP_DATA16, 0x11223344556122, false, (uint16_t)0xabcd);
188 	ADD_ITEM(cnt, STP_DATA32, 0x11223344556122, false, (uint32_t)0x1122abcd);
189 	ADD_ITEM(cnt, STP_DATA64, 0x11223344556122, false, (uint64_t)0x1122abcd11223344);
190 	ADD_ITEM(cnt, STP_DATA8, 0x1122334455667788, true, (uint8_t)0xab);
191 	ADD_ITEM(cnt, STP_DATA16, 0x1122334455667713, true, (uint16_t)0xabcd);
192 	ADD_ITEM(cnt, STP_DATA32, 0x1122334455667713, true, (uint32_t)0xabcd1122);
193 	ADD_ITEM(cnt, STP_DATA64, 0x1122334455667713, true, (uint64_t)0xabcd112211111111);
194 
195 	mipi_stp_decoder_decode(data, sizeof(data));
196 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
197 }
198 
ZTEST(mipi_stp_decoder_test,test_multi_chunk_data_ts)199 ZTEST(mipi_stp_decoder_test, test_multi_chunk_data_ts)
200 {
201 	/*d8ts + 13b TS */
202 	uint8_t data[] = {0x4f, 0xba, 0x1d, 0x21, 0x32};
203 	uint8_t data2[] = {
204 		0x43, 0x54, 0x65, 0x76, 0x07,
205 	};
206 
207 	ADD_ITEM(cnt, STP_DATA8, 0x11223344556677, false, (uint8_t)0xab);
208 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
209 
210 	/* First part without any packet decoded. */
211 	mipi_stp_decoder_decode(data, sizeof(data));
212 	zassert_equal(d_cnt, 0, "got:%d exp:%d", d_cnt, 0);
213 
214 	mipi_stp_decoder_decode(data2, sizeof(data2));
215 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
216 }
217 
ZTEST(mipi_stp_decoder_test,test_chunk_errors)218 ZTEST(mipi_stp_decoder_test, test_chunk_errors)
219 {
220 	uint8_t data[] = {/*merr 0x12 gerr 0x12 null */ 0x12, 0xf2, 0x12, 0x02};
221 
222 	ADD_ITEM(cnt, STP_DECODER_MERROR, UINT64_MAX, false, (uint8_t)0x12);
223 	ADD_ITEM(cnt, STP_DECODER_GERROR, UINT64_MAX, false, (uint8_t)0x12);
224 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
225 
226 	mipi_stp_decoder_decode(data, sizeof(data));
227 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
228 }
229 
ZTEST(mipi_stp_decoder_test,test_chunk_freq)230 ZTEST(mipi_stp_decoder_test, test_chunk_freq)
231 {
232 	uint8_t data[] = {/* freq 0x11223344 null */ 0x0f,
233 			  0x18,
234 			  0x21,
235 			  0x32,
236 			  0x43,
237 			  0x04,
238 			  /* freq_ts 0x11223344 + 2b TS */ 0x0f,
239 			  0x19,
240 			  0x21,
241 			  0x32,
242 			  0x43,
243 			  0x24,
244 			  0x12};
245 
246 	ADD_ITEM(cnt, STP_DECODER_FREQ, UINT64_MAX, false, (uint32_t)0x11223344);
247 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
248 	ADD_ITEM(cnt, STP_DECODER_FREQ, 0x21ULL, false, (uint32_t)0x11223344);
249 
250 	mipi_stp_decoder_decode(data, sizeof(data));
251 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
252 }
253 
ZTEST(mipi_stp_decoder_test,test_chunk_async)254 ZTEST(mipi_stp_decoder_test, test_chunk_async)
255 {
256 	uint8_t data[] = {/* null async null*/
257 			  0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
258 
259 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
260 	ADD_ITEM(cnt, STP_DECODER_ASYNC, UINT64_MAX, false, (uint8_t)0);
261 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
262 
263 	mipi_stp_decoder_decode(data, sizeof(data));
264 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
265 }
266 
ZTEST(mipi_stp_decoder_test,test_multi_chunk_async)267 ZTEST(mipi_stp_decoder_test, test_multi_chunk_async)
268 {
269 	/* null async null split into 2 buffers */
270 	uint8_t data[] = {
271 		0xf0,
272 		0xff,
273 		0xff,
274 	};
275 	uint8_t data2[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
276 
277 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
278 	ADD_ITEM(cnt, STP_DECODER_ASYNC, UINT64_MAX, false, (uint8_t)0);
279 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
280 
281 	/* First part only null packet is decoded */
282 	mipi_stp_decoder_decode(data, sizeof(data));
283 	zassert_equal(d_cnt, 1, "got:%d exp:%d", d_cnt, 1);
284 
285 	mipi_stp_decoder_decode(data2, sizeof(data2));
286 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
287 }
288 
ZTEST(mipi_stp_decoder_test,test_chunk_freq2)289 ZTEST(mipi_stp_decoder_test, test_chunk_freq2)
290 {
291 	/* null async null split into 2 buffers */
292 	uint8_t data[] = {0xf0, 0x80, 0x00, 0xc4, 0xb4, 0x04};
293 
294 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
295 	ADD_ITEM(cnt, STP_DECODER_FREQ, UINT64_MAX, false, (uint64_t)5000000);
296 
297 	mipi_stp_decoder_decode(data, sizeof(data));
298 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
299 }
300 
ZTEST(mipi_stp_decoder_test,test_sync_loss)301 ZTEST(mipi_stp_decoder_test, test_sync_loss)
302 {
303 	/* null async null split into 2 buffers */
304 	uint8_t data[] = {0xf0, 0x80, 0x00, 0xc4, 0xff, 0xff, 0xff, 0xff, 0xf5, 0xff, 0xff, 0xff,
305 			  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x60, 0x11, 0x11, 0x11, 0x11};
306 
307 	ADD_ITEM(cnt, STP_DECODER_NULL, UINT64_MAX, false, (uint8_t)0);
308 	ADD_ITEM(cnt, STP_DECODER_ASYNC, UINT64_MAX, false, (uint8_t)0);
309 	ADD_ITEM(cnt, STP_DATA32, UINT64_MAX, false, (uint32_t)0x11111111);
310 
311 	mipi_stp_decoder_decode(data, 4);
312 	mipi_stp_decoder_sync_loss();
313 	mipi_stp_decoder_decode(&data[4], sizeof(data) - 4);
314 
315 	zassert_equal(d_cnt, cnt, "got:%d exp:%d", d_cnt, cnt);
316 }
317 
before(void * data)318 static void before(void *data)
319 {
320 	cnt = 0;
321 	d_cnt = 0;
322 	mipi_stp_decoder_init(&config);
323 }
324 
325 ZTEST_SUITE(mipi_stp_decoder_test, NULL, NULL, before, NULL, NULL);
326