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/coresight/cs_trace_defmt.h>
8 
9 static const uint8_t *exp_data[3];
10 static size_t exp_len[3];
11 static uint8_t exp_id[3];
12 static uint8_t cb_cnt;
13 
callback(uint32_t id,const uint8_t * data,size_t len)14 static void callback(uint32_t id, const uint8_t *data, size_t len)
15 {
16 	zassert_equal(exp_id[cb_cnt], id, NULL);
17 	zassert_equal(len, exp_len[cb_cnt], NULL);
18 	zassert_equal(memcmp(data, exp_data[cb_cnt], len), 0, NULL);
19 	cb_cnt++;
20 }
21 
ZTEST(coresight_trace_deformatter_test,test_err_check)22 ZTEST(coresight_trace_deformatter_test, test_err_check)
23 {
24 	int err;
25 	uint8_t data[16];
26 
27 	err = cs_trace_defmt_init(callback);
28 	zassert_equal(err, 0);
29 
30 	err = cs_trace_defmt_process(data, 15);
31 	zassert_equal(err, -EINVAL);
32 
33 	err = cs_trace_defmt_process(data, 17);
34 	zassert_equal(err, -EINVAL);
35 }
36 
ZTEST(coresight_trace_deformatter_test,test_basic)37 ZTEST(coresight_trace_deformatter_test, test_basic)
38 {
39 	int err;
40 	static const uint8_t id = 0x25;
41 	static const uint8_t data1[] = {/* First byte has ID, byte 2 has LSB bit set */
42 					(id << 1) | 1,
43 					0x6,
44 					0x0 /*LSB bit in aux */,
45 					0xe,
46 					0,
47 					0,
48 					0,
49 					0,
50 					0,
51 					0,
52 					0,
53 					0,
54 					0,
55 					0,
56 					0,
57 					0x2};
58 
59 	static const uint8_t exp_data1[] = {0x6, 0x1, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
60 
61 	cb_cnt = 0;
62 	exp_id[0] = id;
63 	exp_data[0] = exp_data1;
64 	exp_len[0] = 14;
65 
66 	cs_trace_defmt_init(callback);
67 	err = cs_trace_defmt_process(data1, 16);
68 	zassert_equal(err, 0);
69 	zassert_equal(cb_cnt, 1);
70 }
71 
ZTEST(coresight_trace_deformatter_test,test_basic2)72 ZTEST(coresight_trace_deformatter_test, test_basic2)
73 {
74 	int err;
75 	static const uint8_t data1[] = {0x07, 0xAA, 0xA6, 0xA7, 0x2B, 0xA8, 0x54, 0x52,
76 					0x52, 0x54, 0x07, 0xCA, 0xC6, 0xC7, 0xC8, 0x1C};
77 
78 	static const uint8_t exp_data1[] = {0xAA, 0xA6, 0xA7, 0xA8};
79 	static const uint8_t exp_data2[] = {0x55, 0x52, 0x53, 0x54};
80 	static const uint8_t exp_data3[] = {0xCA, 0xC6, 0xC7, 0xC8};
81 
82 	cb_cnt = 0;
83 	exp_id[0] = 0x3;
84 	exp_data[0] = exp_data1;
85 	exp_len[0] = sizeof(exp_data1);
86 	exp_id[1] = 0x15;
87 	exp_data[1] = exp_data2;
88 	exp_len[1] = sizeof(exp_data2);
89 	exp_id[2] = 0x3;
90 	exp_data[2] = exp_data3;
91 	exp_len[2] = sizeof(exp_data3);
92 
93 	cs_trace_defmt_init(callback);
94 	err = cs_trace_defmt_process(data1, 16);
95 	zassert_equal(err, 0);
96 	zassert_equal(cb_cnt, 3);
97 }
98 
99 ZTEST_SUITE(coresight_trace_deformatter_test, NULL, NULL, NULL, NULL, NULL);
100