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