1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <stddef.h>
9 #include <zephyr/ztest.h>
10 
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/hci.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <host/hci_core.h>
15 
16 #include "util/util.h"
17 #include "util/memq.h"
18 #include "util/mem.h"
19 #include "util/dbuf.h"
20 
21 #include "pdu_df.h"
22 #include "lll/pdu_vendor.h"
23 #include "pdu.h"
24 
25 #include "hal/ccm.h"
26 
27 #include "lll.h"
28 #include "lll/lll_adv_types.h"
29 #include "lll_adv.h"
30 #include "lll/lll_adv_pdu.h"
31 #include "lll/lll_adv_internal.h"
32 #include "lll_adv_sync.h"
33 #include "lll/lll_df_types.h"
34 
35 #include "ull_adv_types.h"
36 #include "ull_adv_internal.h"
37 
38 #include "ll.h"
39 #include "common.h"
40 
41 #define TEST_ADV_SET_HANDLE 0
42 #define TEST_PER_ADV_CHAIN_LENGTH 5
43 #define TEST_PER_ADV_SINGLE_PDU 1
44 #define TEST_CTE_COUNT 3
45 #define TEST_CTE_SINGLE 1
46 /* It does not matter for purpose of this tests what is the type or length of CTE used. */
47 #define TEST_CTE_TYPE BT_HCI_LE_AOD_CTE_2US
48 
ZTEST(test_add_cte_to_per_adv_chain,test_add_number_of_cte_to_sigle_pdu_chain)49 ZTEST(test_add_cte_to_per_adv_chain, test_add_number_of_cte_to_sigle_pdu_chain)
50 {
51 	struct ll_adv_set *adv;
52 	uint8_t handle;
53 	int err;
54 
55 	/* Setup for test */
56 	adv = common_create_adv_set(TEST_ADV_SET_HANDLE);
57 	common_prepare_df_cfg(adv, TEST_CTE_COUNT);
58 	common_create_per_adv_chain(adv, TEST_PER_ADV_SINGLE_PDU);
59 	common_validate_per_adv_chain(adv, TEST_PER_ADV_SINGLE_PDU);
60 
61 	handle = ull_adv_handle_get(adv);
62 
63 	err = ll_df_set_cl_cte_tx_enable(handle, true);
64 	zassert_equal(err, 0,
65 		      "Unexpected error while enabling CTE for periodic advertising chain, err: %d",
66 		      err);
67 
68 	/* Validate result */
69 	common_validate_chain_with_cte(adv, TEST_CTE_COUNT, TEST_PER_ADV_SINGLE_PDU);
70 
71 	common_teardown(adv);
72 }
73 
ZTEST(test_add_cte_to_per_adv_chain,test_add_cte_for_each_pdu_in_chain)74 ZTEST(test_add_cte_to_per_adv_chain, test_add_cte_for_each_pdu_in_chain)
75 {
76 	struct ll_adv_set *adv;
77 	uint8_t handle;
78 	int err;
79 
80 	/* Setup for test */
81 	adv = common_create_adv_set(TEST_ADV_SET_HANDLE);
82 	/* Use the same number for PDUs in a chain as for CTE request */
83 	common_prepare_df_cfg(adv, TEST_CTE_COUNT);
84 	common_create_per_adv_chain(adv, TEST_CTE_COUNT);
85 	common_validate_per_adv_chain(adv, TEST_CTE_COUNT);
86 
87 	handle = ull_adv_handle_get(adv);
88 
89 	err = ll_df_set_cl_cte_tx_enable(handle, true);
90 	zassert_equal(err, 0,
91 		      "Unexpected error while enabling CTE for periodic advertising chain, err: %d",
92 		      err);
93 
94 	/* Validate result */
95 	common_validate_chain_with_cte(adv, TEST_CTE_COUNT, TEST_CTE_COUNT);
96 
97 	common_teardown(adv);
98 }
99 
ZTEST(test_add_cte_to_per_adv_chain,test_add_cte_for_not_all_pdu_in_chain)100 ZTEST(test_add_cte_to_per_adv_chain, test_add_cte_for_not_all_pdu_in_chain)
101 {
102 	struct ll_adv_set *adv;
103 	uint8_t handle;
104 	int err;
105 
106 	/* Setup for test */
107 	adv = common_create_adv_set(TEST_ADV_SET_HANDLE);
108 	/* Use the same number for PDUs in a chain as for CTE request */
109 	common_prepare_df_cfg(adv, TEST_CTE_COUNT);
110 	common_create_per_adv_chain(adv, TEST_PER_ADV_CHAIN_LENGTH);
111 	common_validate_per_adv_chain(adv, TEST_PER_ADV_CHAIN_LENGTH);
112 
113 	handle = ull_adv_handle_get(adv);
114 
115 	err = ll_df_set_cl_cte_tx_enable(handle, true);
116 	zassert_equal(err, 0,
117 		      "Unexpected error while enabling CTE for periodic advertising chain, err: %d",
118 		      err);
119 
120 	/* Validate result */
121 	common_validate_chain_with_cte(adv, TEST_CTE_COUNT, TEST_PER_ADV_CHAIN_LENGTH);
122 
123 	common_teardown(adv);
124 }
125 
ZTEST(test_add_cte_to_per_adv_chain,test_add_cte_to_not_all_pdus_in_chain_enqueued_to_lll)126 ZTEST(test_add_cte_to_per_adv_chain, test_add_cte_to_not_all_pdus_in_chain_enqueued_to_lll)
127 {
128 	struct pdu_adv *pdu_prev, *pdu_new;
129 	struct ll_adv_set *adv;
130 	uint8_t handle;
131 	uint8_t upd;
132 	int err;
133 
134 	/* Setup for test */
135 	adv = common_create_adv_set(TEST_ADV_SET_HANDLE);
136 	/* Use the same number for PDUs in a chain as for CTE request */
137 	common_prepare_df_cfg(adv, TEST_CTE_COUNT);
138 	common_create_per_adv_chain(adv, TEST_PER_ADV_CHAIN_LENGTH);
139 	common_validate_per_adv_chain(adv, TEST_PER_ADV_CHAIN_LENGTH);
140 
141 	/* Swap PDU double buffer and get new latest PDU data */
142 	pdu_new = lll_adv_sync_data_latest_get(adv->lll.sync, NULL, &upd);
143 	zassert_not_equal(pdu_new, NULL,
144 			  "Unexpected value of new PDU pointer after PDU double buffer swap");
145 
146 	pdu_prev = lll_adv_sync_data_peek(adv->lll.sync, NULL);
147 	zassert_equal(pdu_prev, pdu_new,
148 		      "Unexpected value of previous PDU pointer after PDU double buffer swap");
149 
150 	handle = ull_adv_handle_get(adv);
151 
152 	err = ll_df_set_cl_cte_tx_enable(handle, true);
153 	zassert_equal(err, 0,
154 		      "Unexpected error while enabling CTE for periodic advertising chain, err: %d",
155 		      err);
156 
157 	/* Validate result */
158 	common_validate_chain_with_cte(adv, TEST_CTE_COUNT, TEST_PER_ADV_CHAIN_LENGTH);
159 
160 	common_teardown(adv);
161 }
162 
ZTEST(test_add_cte_to_per_adv_chain,test_add_cte_for_single_pdu_chain)163 ZTEST(test_add_cte_to_per_adv_chain, test_add_cte_for_single_pdu_chain)
164 {
165 	struct ll_adv_set *adv;
166 	uint8_t handle;
167 	int err;
168 
169 	/* Setup for test */
170 	adv = common_create_adv_set(TEST_ADV_SET_HANDLE);
171 	/* Use the same number for PDUs in a chain as for CTE request */
172 	common_prepare_df_cfg(adv, TEST_CTE_SINGLE);
173 	common_create_per_adv_chain(adv, TEST_PER_ADV_SINGLE_PDU);
174 	common_validate_per_adv_chain(adv, TEST_PER_ADV_SINGLE_PDU);
175 
176 	handle = ull_adv_handle_get(adv);
177 
178 	err = ll_df_set_cl_cte_tx_enable(handle, true);
179 	zassert_equal(err, 0,
180 		      "Unexpected error while enabling CTE for periodic advertising chain, err: %d",
181 		      err);
182 
183 	/* Validate result */
184 	common_validate_chain_with_cte(adv, TEST_CTE_SINGLE, TEST_PER_ADV_SINGLE_PDU);
185 
186 	common_teardown(adv);
187 }
188 
189 ZTEST_SUITE(test_add_cte_to_per_adv_chain, NULL, NULL, NULL, NULL, NULL);
190