1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <stddef.h>
10 
11 #include <zephyr/types.h>
12 #include <zephyr/sys/printk.h>
13 #include <zephyr/sys/util.h>
14 
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/hci.h>
17 
18 #include "ll.h"
19 
20 #include "bs_types.h"
21 #include "bs_tracing.h"
22 #include "time_machine.h"
23 #include "bstests.h"
24 
25 #define HANDLE          0x0000
26 #define EVT_PROP_SCAN   BIT(1)
27 #define EVT_PROP_ANON   BIT(5)
28 #define EVT_PROP_TXP    BIT(6)
29 #define ADV_INTERVAL    0x20   /* 20 ms advertising interval */
30 #define ADV_WAIT_MS     10     /* 10 ms wait loop */
31 #define OWN_ADDR_TYPE   BT_ADDR_LE_RANDOM_ID
32 #define PEER_ADDR_TYPE  BT_ADDR_LE_RANDOM_ID
33 #define PEER_ADDR       peer_addr
34 #define ADV_CHAN_MAP    0x07
35 #define FILTER_POLICY   0x00
36 #define ADV_TX_PWR      NULL
37 #define ADV_SEC_SKIP    0
38 #define ADV_PHY_1M      BIT(0)
39 #define ADV_PHY_2M      BIT(1)
40 #define ADV_PHY_CODED   BIT(2)
41 #define ADV_SID         0x0a
42 #define SCAN_REQ_NOT    0
43 
44 #define AD_OP           0x03
45 #define AD_FRAG_PREF    0x00
46 
47 #define ADV_INTERVAL_PERIODIC 0x30
48 
49 #define SCAN_INTERVAL   0x04
50 #define SCAN_WINDOW     0x04
51 
52 #define FAIL(...)					\
53 	do {						\
54 		bst_result = Failed;			\
55 		bs_trace_error_time_line(__VA_ARGS__);	\
56 	} while (0)
57 
58 #define PASS(...)					\
59 	do {						\
60 		bst_result = Passed;			\
61 		bs_trace_info_time(1, __VA_ARGS__);	\
62 	} while (0)
63 
64 extern enum bst_result_t bst_result;
65 
66 static uint8_t const own_addr_reenable[] = {0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5};
67 static uint8_t const own_addr[] = {0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5};
68 static uint8_t const peer_addr[] = {0xc6, 0xc7, 0xc8, 0xc9, 0xc1, 0xcb};
69 
70 static const struct bt_data ad[] = {
71 	BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
72 	};
73 
74 static uint8_t adv_data[] = {
75 		2, BT_DATA_FLAGS, BT_LE_AD_NO_BREDR,
76 	};
77 
78 static uint8_t adv_data1[] = {
79 		2, BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
80 		7, BT_DATA_NAME_COMPLETE, 'Z', 'e', 'p', 'h', 'y', 'r',
81 	};
82 
83 static uint8_t adv_data2[] = {
84 		2, BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
85 	};
86 
87 static uint8_t sr_data[] = {
88 		7, BT_DATA_NAME_COMPLETE, 'Z', 'e', 'p', 'h', 'y', 'r',
89 	};
90 
91 static uint8_t per_adv_data1[] = {
92 		7, BT_DATA_NAME_COMPLETE, 'Z', 'e', 'p', 'h', 'y', 'r',
93 	};
94 
95 static uint8_t per_adv_data2[] = {
96 		8, BT_DATA_NAME_COMPLETE, 'Z', 'e', 'p', 'h', 'y', 'r', '1',
97 	};
98 
99 static uint8_t per_adv_data3[] = {
100 		0xFF, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF,
101 	};
102 
103 static uint8_t chan_map[] = { 0x1F, 0XF1, 0x1F, 0xF1, 0x1F };
104 
105 static bool volatile is_scanned, is_connected, is_disconnected;
106 static bool volatile connection_to_test;
107 static uint8_t adv_data_expected_len;
108 static uint8_t *adv_data_expected;
109 
connected(struct bt_conn * conn,uint8_t conn_err)110 static void connected(struct bt_conn *conn, uint8_t conn_err)
111 {
112 	int err;
113 
114 	printk("Connected.\n");
115 
116 	is_connected = true;
117 
118 	err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
119 	if (err) {
120 		printk("Disconnection failed (err %d).\n", err);
121 	}
122 }
123 
disconnected(struct bt_conn * conn,uint8_t reason)124 static void disconnected(struct bt_conn *conn, uint8_t reason)
125 {
126 	printk("Disconnected.\n");
127 
128 	is_disconnected = true;
129 }
130 
131 BT_CONN_CB_DEFINE(conn_callbacks) = {
132 	.connected = connected,
133 	.disconnected = disconnected,
134 };
135 
136 static bool volatile is_sent;
137 static uint8_t volatile num_sent_actual;
138 
sent_cb(struct bt_le_ext_adv * adv,struct bt_le_ext_adv_sent_info * info)139 void sent_cb(struct bt_le_ext_adv *adv, struct bt_le_ext_adv_sent_info *info)
140 {
141 	printk("%s: num_sent = %u\n", __func__, info->num_sent);
142 
143 	is_sent = true;
144 	num_sent_actual = info->num_sent;
145 }
146 
connected_cb(struct bt_le_ext_adv * adv,struct bt_le_ext_adv_connected_info * info)147 void connected_cb(struct bt_le_ext_adv *adv,
148 		  struct bt_le_ext_adv_connected_info *info)
149 {
150 	printk("%s\n", __func__);
151 }
152 
scanned_cb(struct bt_le_ext_adv * adv,struct bt_le_ext_adv_scanned_info * info)153 void scanned_cb(struct bt_le_ext_adv *adv,
154 		struct bt_le_ext_adv_scanned_info *info)
155 {
156 	printk("%s\n", __func__);
157 }
158 
159 struct bt_le_ext_adv_cb adv_callbacks = {
160 	.sent = sent_cb,
161 	.connected = connected_cb,
162 	.scanned = scanned_cb,
163 };
164 
test_advx_main(void)165 static void test_advx_main(void)
166 {
167 	struct bt_le_ext_adv_start_param ext_adv_param;
168 	struct bt_le_ext_adv *adv;
169 	uint8_t num_sent_expected;
170 	uint16_t evt_prop;
171 	uint8_t adv_type;
172 	uint16_t handle;
173 	uint8_t phy_p;
174 	uint8_t phy_s;
175 	int err;
176 
177 	printk("\n*Extended Advertising test*\n");
178 
179 	printk("Bluetooth initializing...");
180 	err = bt_enable(NULL);
181 	if (err) {
182 		goto exit;
183 	}
184 	printk("success.\n");
185 
186 	printk("Connectable advertising...");
187 	err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
188 	if (err) {
189 		printk("Advertising failed to start (err %d)\n", err);
190 		return;
191 	}
192 	printk("success.\n");
193 
194 	printk("Waiting for connection...");
195 	while (!is_connected) {
196 		k_sleep(K_MSEC(100));
197 	}
198 	printk("success.\n");
199 
200 	printk("Waiting for disconnect...");
201 	while (!is_disconnected) {
202 		k_sleep(K_MSEC(100));
203 	}
204 	printk("success.\n");
205 
206 	printk("Stop advertising...");
207 	err = bt_le_adv_stop();
208 	if (err) {
209 		goto exit;
210 	}
211 	printk("success.\n");
212 
213 	printk("AD Data set...");
214 	handle = 0U;
215 	err = ll_adv_data_set(handle, sizeof(adv_data), adv_data);
216 	if (err) {
217 		goto exit;
218 	}
219 	printk("success.\n");
220 
221 	printk("Directed advertising, parameter set...");
222 	err = ll_adv_params_set(handle, 0, 0, BT_HCI_ADV_DIRECT_IND,
223 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
224 				ADV_CHAN_MAP, FILTER_POLICY,
225 				0, 0, 0, 0, 0, 0);
226 	if (err) {
227 		goto exit;
228 	}
229 	printk("success.\n");
230 
231 	printk("Connectable advertising, parameter set...");
232 	err = ll_adv_params_set(handle, 0, ADV_INTERVAL, BT_HCI_ADV_NONCONN_IND,
233 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
234 				ADV_CHAN_MAP, FILTER_POLICY,
235 				0, 0, 0, 0, 0, 0);
236 	if (err) {
237 		goto exit;
238 	}
239 	printk("success.\n");
240 
241 	printk("Enabling...");
242 	err = ll_adv_enable(handle, 1, 0, 0);
243 	if (err) {
244 		goto exit;
245 	}
246 	printk("success.\n");
247 
248 	k_sleep(K_MSEC(100));
249 
250 	printk("Disabling...");
251 	err = ll_adv_enable(handle, 0, 0, 0);
252 	if (err) {
253 		goto exit;
254 	}
255 	printk("success.\n");
256 
257 	printk("Directed advertising, parameter set...");
258 	err = ll_adv_params_set(handle, 0, 0, BT_HCI_ADV_DIRECT_IND,
259 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
260 				ADV_CHAN_MAP, FILTER_POLICY,
261 				0, 0, 0, 0, 0, 0);
262 	if (err) {
263 		goto exit;
264 	}
265 	printk("success.\n");
266 
267 	printk("AD Data set...");
268 	handle = 0U;
269 	err = ll_adv_data_set(handle, sizeof(adv_data1), adv_data1);
270 	if (err) {
271 		goto exit;
272 	}
273 	printk("success.\n");
274 
275 	printk("Connectable advertising, parameter set...");
276 	err = ll_adv_params_set(handle, 0, ADV_INTERVAL, BT_HCI_ADV_NONCONN_IND,
277 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
278 				ADV_CHAN_MAP, FILTER_POLICY,
279 				0, 0, 0, 0, 0, 0);
280 	if (err) {
281 		goto exit;
282 	}
283 	printk("success.\n");
284 
285 	printk("Enabling...");
286 	err = ll_adv_enable(handle, 1, 0, 0);
287 	if (err) {
288 		goto exit;
289 	}
290 	printk("success.\n");
291 
292 	k_sleep(K_MSEC(100));
293 
294 	printk("Disabling...");
295 	err = ll_adv_enable(handle, 0, 0, 0);
296 	if (err) {
297 		goto exit;
298 	}
299 	printk("success.\n");
300 
301 	printk("Create scannable extended advertising set...");
302 	err = bt_le_ext_adv_create(BT_LE_EXT_ADV_SCAN_NAME, &adv_callbacks,
303 				   &adv);
304 	if (err) {
305 		goto exit;
306 	}
307 	printk("success.\n");
308 
309 	printk("Start scannable advertising...");
310 	ext_adv_param.timeout = 0;
311 	ext_adv_param.num_events = 0;
312 	err = bt_le_ext_adv_start(adv, &ext_adv_param);
313 	if (err) {
314 		goto exit;
315 	}
316 	printk("success.\n");
317 
318 	k_sleep(K_MSEC(500));
319 
320 	printk("Stopping scannable advertising...");
321 	err = bt_le_ext_adv_stop(adv);
322 	if (err) {
323 		goto exit;
324 	}
325 	printk("success.\n");
326 
327 	printk("Removing scannable adv set...");
328 	err = bt_le_ext_adv_delete(adv);
329 	if (err) {
330 		goto exit;
331 	}
332 	printk("success.\n");
333 
334 	printk("Create connectable extended advertising set...");
335 	is_connected = false;
336 	is_disconnected = false;
337 	err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN_NAME, &adv_callbacks, &adv);
338 	if (err) {
339 		goto exit;
340 	}
341 	printk("success.\n");
342 
343 	printk("Start advertising...");
344 	ext_adv_param.timeout = 0;
345 	ext_adv_param.num_events = 0;
346 	err = bt_le_ext_adv_start(adv, &ext_adv_param);
347 	if (err) {
348 		goto exit;
349 	}
350 	printk("success.\n");
351 
352 	printk("Waiting for connection...");
353 	while (!is_connected) {
354 		k_sleep(K_MSEC(100));
355 	}
356 	printk("success.\n");
357 
358 	printk("Waiting for disconnect...");
359 	while (!is_disconnected) {
360 		k_sleep(K_MSEC(100));
361 	}
362 	printk("success.\n");
363 
364 	printk("Removing connectable adv aux set...");
365 	err = bt_le_ext_adv_delete(adv);
366 	if (err) {
367 		goto exit;
368 	}
369 	printk("success.\n");
370 
371 	k_sleep(K_MSEC(1000));
372 
373 	printk("Starting non-connectable advertising...");
374 	err = bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), NULL, 0);
375 	if (err) {
376 		goto exit;
377 	}
378 	printk("success.\n");
379 
380 	k_sleep(K_MSEC(400));
381 
382 	printk("Stopping advertising...");
383 	err = bt_le_adv_stop();
384 	if (err) {
385 		goto exit;
386 	}
387 	printk("success.\n");
388 
389 	k_sleep(K_MSEC(1000));
390 
391 	printk("Create connectable advertising set...");
392 	err = bt_le_ext_adv_create(BT_LE_ADV_CONN_NAME, &adv_callbacks, &adv);
393 	if (err) {
394 		goto exit;
395 	}
396 	printk("success.\n");
397 
398 	printk("Start advertising using extended commands (max_events)...");
399 	is_sent = false;
400 	num_sent_actual = 0;
401 	num_sent_expected = 3;
402 	ext_adv_param.timeout = 0;
403 	ext_adv_param.num_events = 3;
404 	err = bt_le_ext_adv_start(adv, &ext_adv_param);
405 	if (err) {
406 		goto exit;
407 	}
408 	printk("success.\n");
409 
410 	printk("Waiting...");
411 	while (!is_sent) {
412 		k_sleep(K_MSEC(100));
413 	}
414 	printk("done.\n");
415 
416 	if (num_sent_actual != num_sent_expected) {
417 		FAIL("Num sent actual = %u, expected = %u\n", num_sent_actual,
418 		     num_sent_expected);
419 	}
420 
421 	k_sleep(K_MSEC(1000));
422 
423 	printk("Start advertising using extended commands (duration)...");
424 	is_sent = false;
425 	num_sent_actual = 0;
426 	num_sent_expected = 5;
427 	ext_adv_param.timeout = 50;
428 	ext_adv_param.num_events = 0;
429 	err = bt_le_ext_adv_start(adv, &ext_adv_param);
430 	if (err) {
431 		goto exit;
432 	}
433 	printk("success.\n");
434 
435 	printk("Waiting...");
436 	while (!is_sent) {
437 		k_sleep(K_MSEC(100));
438 	}
439 	printk("done.\n");
440 
441 	if (num_sent_actual != num_sent_expected) {
442 		FAIL("Num sent actual = %u, expected = %u\n", num_sent_actual,
443 		     num_sent_expected);
444 	}
445 
446 	k_sleep(K_MSEC(1000));
447 
448 	printk("Re-enable advertising using extended commands (max_events)...");
449 	is_sent = false;
450 	num_sent_actual = 0;
451 	num_sent_expected = 3;
452 	ext_adv_param.timeout = 0;
453 	ext_adv_param.num_events = 3;
454 	err = bt_le_ext_adv_start(adv, &ext_adv_param);
455 	if (err) {
456 		goto exit;
457 	}
458 	printk("success.\n");
459 
460 	k_sleep(K_MSEC(100));
461 
462 	printk("Setting advertising random address before re-enabling...");
463 	handle = 0x0000;
464 	err = ll_adv_aux_random_addr_set(handle, own_addr_reenable);
465 	if (err) {
466 		goto exit;
467 	}
468 	printk("success.\n");
469 
470 	printk("Re-enabling...");
471 	handle = 0x0000;
472 	err = ll_adv_enable(handle, 1,
473 			    ext_adv_param.timeout,
474 			    ext_adv_param.num_events);
475 	if (err) {
476 		goto exit;
477 	}
478 	printk("success.\n");
479 
480 	printk("Waiting...");
481 	while (!is_sent) {
482 		k_sleep(K_MSEC(100));
483 	}
484 	printk("done.\n");
485 
486 	if (num_sent_actual != num_sent_expected) {
487 		FAIL("Num sent actual = %u, expected = %u\n", num_sent_actual,
488 		     num_sent_expected);
489 	}
490 
491 	k_sleep(K_MSEC(1000));
492 
493 	printk("Re-enable advertising using extended commands (duration)...");
494 	is_sent = false;
495 	num_sent_actual = 0;
496 	num_sent_expected = 5;      /* 5 advertising events with a spacing of (100 ms +
497 				     * random_delay of upto 10 ms) transmit in
498 				     * the range of 400 to 440 ms
499 				     */
500 	ext_adv_param.timeout = 50; /* Check there is atmost 5 advertising
501 				     * events in a timeout of 500 ms
502 				     */
503 	ext_adv_param.num_events = 0;
504 	err = bt_le_ext_adv_start(adv, &ext_adv_param);
505 	if (err) {
506 		goto exit;
507 	}
508 	printk("success.\n");
509 
510 	/* Delay 100 ms, and the test should verify that re-enabling still
511 	 * results in correct num of events.
512 	 */
513 	k_sleep(K_MSEC(100));
514 
515 	printk("Re-enabling...");
516 	handle = 0x0000;
517 	err = ll_adv_enable(handle, 1,
518 			    ext_adv_param.timeout,
519 			    ext_adv_param.num_events);
520 	if (err) {
521 		goto exit;
522 	}
523 	printk("success.\n");
524 
525 	printk("Waiting...");
526 	while (!is_sent) {
527 		k_sleep(K_MSEC(100));
528 	}
529 	printk("done.\n");
530 
531 	if (num_sent_actual != num_sent_expected) {
532 		FAIL("Num sent actual = %u, expected = %u\n", num_sent_actual,
533 		     num_sent_expected);
534 	}
535 
536 	k_sleep(K_MSEC(1000));
537 
538 	printk("Start advertising using extended commands (disable)...");
539 	ext_adv_param.timeout = 0;
540 	ext_adv_param.num_events = 5;
541 	err = bt_le_ext_adv_start(adv, &ext_adv_param);
542 	if (err) {
543 		goto exit;
544 	}
545 	printk("success.\n");
546 
547 	k_sleep(K_MSEC(400));
548 
549 	printk("Stopping advertising using extended commands...");
550 	err = bt_le_ext_adv_stop(adv);
551 	if (err) {
552 		goto exit;
553 	}
554 	printk("success.\n");
555 
556 	k_sleep(K_MSEC(1000));
557 
558 	printk("Setting advertising random address...");
559 	handle = 0x0000;
560 	err = ll_adv_aux_random_addr_set(handle, own_addr);
561 	if (err) {
562 		goto exit;
563 	}
564 	printk("success.\n");
565 
566 	printk("Starting non-conn non-scan without aux 1M advertising...");
567 	evt_prop = EVT_PROP_TXP;
568 	adv_type = 0x07; /* Adv. Ext. */
569 	phy_p = ADV_PHY_1M;
570 	phy_s = ADV_PHY_2M;
571 	err = ll_adv_params_set(handle, evt_prop, ADV_INTERVAL, adv_type,
572 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
573 				ADV_CHAN_MAP, FILTER_POLICY, ADV_TX_PWR,
574 				phy_p, ADV_SEC_SKIP, phy_s, ADV_SID,
575 				SCAN_REQ_NOT);
576 	if (err) {
577 		goto exit;
578 	}
579 
580 	printk("enabling...");
581 	err = ll_adv_enable(handle, 1, 0, 0);
582 	if (err) {
583 		goto exit;
584 	}
585 	printk("success.\n");
586 
587 	k_sleep(K_MSEC(400));
588 
589 	printk("Disabling...");
590 	err = ll_adv_enable(handle, 0, 0, 0);
591 	if (err) {
592 		goto exit;
593 	}
594 	printk("success.\n");
595 
596 	k_sleep(K_MSEC(1000));
597 
598 	printk("Enabling non-conn non-scan without aux 1M advertising...");
599 	err = ll_adv_enable(handle, 1, 0, 0);
600 	if (err) {
601 		goto exit;
602 	}
603 	printk("success.\n");
604 
605 	k_sleep(K_MSEC(400));
606 
607 	printk("Adding data, so non-conn non-scan with aux 1M advertising...");
608 	err = ll_adv_aux_ad_data_set(handle, AD_OP, AD_FRAG_PREF,
609 				     sizeof(adv_data), (void *)adv_data);
610 	if (err) {
611 		goto exit;
612 	}
613 	printk("success.\n");
614 
615 	k_sleep(K_MSEC(400));
616 
617 	printk("Update advertising data 1...");
618 	err = ll_adv_aux_ad_data_set(handle, AD_OP, AD_FRAG_PREF,
619 				     sizeof(adv_data1), (void *)adv_data1);
620 	if (err) {
621 		goto exit;
622 	}
623 	printk("success.\n");
624 
625 	k_sleep(K_MSEC(400));
626 
627 	printk("Update advertising data 2...");
628 	err = ll_adv_aux_ad_data_set(handle, AD_OP, AD_FRAG_PREF,
629 				     sizeof(adv_data2), (void *)adv_data2);
630 	if (err) {
631 		goto exit;
632 	}
633 	printk("success.\n");
634 
635 	k_sleep(K_MSEC(400));
636 
637 	printk("Starting directed advertising...");
638 	const bt_addr_le_t direct_addr = {
639 		.type = BT_ADDR_LE_RANDOM,
640 		.a = {
641 			.val = {0x11, 0x22, 0x33, 0x44, 0x55, 0xC6}
642 		}
643 	};
644 	const struct bt_le_adv_param adv_param = {
645 		.options = BT_LE_ADV_OPT_CONNECTABLE,
646 		.peer = &direct_addr,
647 	};
648 	err = bt_le_adv_start(&adv_param, NULL, 0, NULL, 0);
649 	if (err) {
650 		goto exit;
651 	}
652 	printk("success.\n");
653 
654 	k_sleep(K_MSEC(2000));
655 
656 	printk("Disabling...");
657 	err = ll_adv_enable(handle, 0, 0, 0);
658 	if (err) {
659 		goto exit;
660 	}
661 	printk("success.\n");
662 
663 	k_sleep(K_MSEC(1000));
664 
665 	printk("Add to resolving list...");
666 	bt_addr_le_t peer_id_addr = {
667 		.type = BT_ADDR_LE_RANDOM,
668 		.a = {
669 			.val = {0xc6, 0xc7, 0xc8, 0xc9, 0xc1, 0xcb}
670 		}
671 	};
672 	uint8_t pirk[16] = {0x00, };
673 	uint8_t lirk[16] = {0x01, };
674 
675 	err = ll_rl_add(&peer_id_addr, pirk, lirk);
676 	if (err) {
677 		goto exit;
678 	}
679 	printk("success.\n");
680 
681 	printk("Enable resolving list...");
682 	err = ll_rl_enable(BT_HCI_ADDR_RES_ENABLE);
683 	if (err) {
684 		goto exit;
685 	}
686 	printk("success.\n");
687 
688 	printk("Enabling extended...");
689 	err = ll_adv_enable(handle, 1, 0, 0);
690 	if (err) {
691 		goto exit;
692 	}
693 	printk("success.\n");
694 
695 	k_sleep(K_MSEC(400));
696 
697 	printk("Starting periodic 1M advertising...");
698 	err = ll_adv_sync_param_set(handle, ADV_INTERVAL_PERIODIC, 0);
699 	if (err) {
700 		goto exit;
701 	}
702 
703 	printk("enabling periodic...");
704 	err = ll_adv_sync_enable(handle, BT_HCI_LE_SET_PER_ADV_ENABLE_ENABLE);
705 	if (err) {
706 		goto exit;
707 	}
708 	printk("success.\n");
709 
710 	k_sleep(K_MSEC(400));
711 
712 	printk("Update advertising data 1...");
713 	err = ll_adv_aux_ad_data_set(handle, AD_OP, AD_FRAG_PREF,
714 				     sizeof(adv_data1), (void *)adv_data1);
715 	if (err) {
716 		goto exit;
717 	}
718 	printk("success.\n");
719 
720 	k_sleep(K_MSEC(400));
721 
722 	printk("Update advertising data 2...");
723 	err = ll_adv_aux_ad_data_set(handle, AD_OP, AD_FRAG_PREF,
724 				     sizeof(adv_data2), (void *)adv_data2);
725 	if (err) {
726 		goto exit;
727 	}
728 	printk("success.\n");
729 
730 	k_sleep(K_MSEC(400));
731 
732 	printk("Update periodic advertising data 1...");
733 	err = ll_adv_sync_ad_data_set(handle, AD_OP, sizeof(per_adv_data1),
734 				      (void *)per_adv_data1);
735 	if (err) {
736 		goto exit;
737 	}
738 	printk("success.\n");
739 
740 	k_sleep(K_MSEC(400));
741 
742 	printk("Update periodic advertising data 2...");
743 	err = ll_adv_sync_ad_data_set(handle, AD_OP, sizeof(per_adv_data2),
744 				      (void *)per_adv_data2);
745 	if (err) {
746 		goto exit;
747 	}
748 	printk("success.\n");
749 
750 	k_sleep(K_MSEC(400));
751 
752 	printk("Update periodic advertising data 3...");
753 	err = ll_adv_sync_ad_data_set(handle, AD_OP, sizeof(per_adv_data3),
754 				      (void *)per_adv_data3);
755 	if (err) {
756 		goto exit;
757 	}
758 	printk("success.\n");
759 
760 	k_sleep(K_MSEC(400));
761 
762 	printk("Update periodic advertising back to data 2...");
763 	err = ll_adv_sync_ad_data_set(handle, AD_OP, sizeof(per_adv_data2),
764 				      (void *)per_adv_data2);
765 	if (err) {
766 		goto exit;
767 	}
768 	printk("success.\n");
769 
770 	k_sleep(K_MSEC(400));
771 
772 	printk("Disabling...");
773 	err = ll_adv_enable(handle, 0, 0, 0);
774 	if (err) {
775 		goto exit;
776 	}
777 	printk("success.\n");
778 
779 	printk("Periodic Advertising Channel Map Indication...");
780 	err = ll_chm_update(chan_map);
781 	if (err) {
782 		goto exit;
783 	}
784 	printk("success.\n");
785 
786 	k_sleep(K_MSEC(400));
787 
788 	printk("Disabling periodic...");
789 	err = ll_adv_sync_enable(handle, 0);
790 	if (err) {
791 		goto exit;
792 	}
793 	printk("success.\n");
794 
795 	k_sleep(K_MSEC(1000));
796 
797 	printk("enabling periodic...");
798 	err = ll_adv_sync_enable(handle,
799 				 (BT_HCI_LE_SET_PER_ADV_ENABLE_ENABLE |
800 				  BT_HCI_LE_SET_PER_ADV_ENABLE_ADI));
801 	if (err) {
802 		goto exit;
803 	}
804 	printk("success.\n");
805 
806 	printk("Enabling extended...");
807 	err = ll_adv_enable(handle, 1, 0, 0);
808 	if (err) {
809 		goto exit;
810 	}
811 	printk("success.\n");
812 
813 	k_sleep(K_MSEC(400));
814 
815 	printk("Update periodic advertising data (duplicate filter)...");
816 	err = ll_adv_sync_ad_data_set(handle, AD_OP, sizeof(per_adv_data3),
817 				      (void *)per_adv_data3);
818 	if (err) {
819 		goto exit;
820 	}
821 	printk("success.\n");
822 
823 	k_sleep(K_MSEC(1000));
824 
825 	printk("Disabling periodic...");
826 	err = ll_adv_sync_enable(handle, 0);
827 	if (err) {
828 		goto exit;
829 	}
830 	printk("success.\n");
831 
832 	k_sleep(K_MSEC(400));
833 
834 	printk("Disabling...");
835 	err = ll_adv_enable(handle, 0, 0, 0);
836 	if (err) {
837 		goto exit;
838 	}
839 	printk("success.\n");
840 
841 	k_sleep(K_MSEC(1000));
842 
843 	printk("Adding scan response data on non-scannable set...");
844 	err = ll_adv_aux_sr_data_set(handle, AD_OP, AD_FRAG_PREF,
845 				     sizeof(sr_data), (void *)sr_data);
846 	if (err != BT_HCI_ERR_INVALID_PARAM) {
847 		goto exit;
848 	}
849 	printk("success.\n");
850 
851 	printk("Removing adv aux set that's created and disabled ...");
852 	err = ll_adv_aux_set_remove(handle);
853 	if (err) {
854 		goto exit;
855 	}
856 	printk("success.\n");
857 
858 	printk("Creating new adv set (scannable)...");
859 	err = ll_adv_params_set(handle, EVT_PROP_SCAN, ADV_INTERVAL, adv_type,
860 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
861 				ADV_CHAN_MAP, FILTER_POLICY, ADV_TX_PWR,
862 				phy_p, ADV_SEC_SKIP, phy_s, ADV_SID,
863 				SCAN_REQ_NOT);
864 	if (err) {
865 		goto exit;
866 	}
867 	printk("success.\n");
868 
869 	printk("Adding scan response data...");
870 	err = ll_adv_aux_sr_data_set(handle, AD_OP, AD_FRAG_PREF,
871 				     sizeof(sr_data), (void *)sr_data);
872 	if (err) {
873 		goto exit;
874 	}
875 	printk("success.\n");
876 
877 	printk("Enabling non-conn scan with scan response data...");
878 	err = ll_adv_enable(handle, 1, 0, 0);
879 	if (err) {
880 		goto exit;
881 	}
882 	printk("success.\n");
883 
884 	k_sleep(K_MSEC(400));
885 
886 	printk("Disabling...");
887 	err = ll_adv_enable(handle, 0, 0, 0);
888 	if (err) {
889 		goto exit;
890 	}
891 	printk("success.\n");
892 
893 	k_sleep(K_MSEC(1000));
894 
895 	printk("Removing adv aux set that's created and disabled ...");
896 	err = ll_adv_aux_set_remove(handle);
897 	if (err) {
898 		goto exit;
899 	}
900 	printk("success.\n");
901 
902 	printk("Removing adv aux set that's not created ...");
903 	err = ll_adv_aux_set_remove(handle);
904 	if (err != BT_HCI_ERR_UNKNOWN_ADV_IDENTIFIER) {
905 		goto exit;
906 	}
907 	printk("success.\n");
908 
909 	printk("Creating new adv set...");
910 	err = ll_adv_params_set(handle, evt_prop, ADV_INTERVAL, adv_type,
911 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
912 				ADV_CHAN_MAP, FILTER_POLICY, ADV_TX_PWR,
913 				phy_p, ADV_SEC_SKIP, phy_s, ADV_SID,
914 				SCAN_REQ_NOT);
915 	if (err) {
916 		goto exit;
917 	}
918 	printk("success.\n");
919 
920 	printk("Update advertising data 2...");
921 	err = ll_adv_aux_ad_data_set(handle, AD_OP, AD_FRAG_PREF,
922 				     sizeof(adv_data2), (void *)adv_data2);
923 	if (err) {
924 		goto exit;
925 	}
926 	printk("success.\n");
927 
928 	printk("Enabling adv set...");
929 	err = ll_adv_enable(handle, 1, 0, 0);
930 	if (err) {
931 		goto exit;
932 	}
933 	printk("success.\n");
934 
935 	k_sleep(K_MSEC(400));
936 
937 	printk("Removing adv aux set that's created and enabled  ...");
938 	err = ll_adv_aux_set_remove(handle);
939 	if (err != BT_HCI_ERR_CMD_DISALLOWED) {
940 		goto exit;
941 	}
942 	printk("success.\n");
943 
944 	printk("Disabling adv set...");
945 	err = ll_adv_enable(handle, 0, 0, 0);
946 	if (err) {
947 		goto exit;
948 	}
949 	printk("success.\n");
950 
951 	printk("Removing adv aux set that's created and disabled  ...");
952 	err = ll_adv_aux_set_remove(handle);
953 	if (err) {
954 		goto exit;
955 	}
956 	printk("success.\n");
957 
958 	printk("Creating new adv set...");
959 	err = ll_adv_params_set(handle, evt_prop, ADV_INTERVAL, adv_type,
960 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
961 				ADV_CHAN_MAP, FILTER_POLICY, ADV_TX_PWR,
962 				phy_p, ADV_SEC_SKIP, phy_s, ADV_SID,
963 				SCAN_REQ_NOT);
964 	if (err) {
965 		goto exit;
966 	}
967 	printk("success.\n");
968 
969 	printk("Starting periodic 1M advertising...");
970 	err = ll_adv_sync_param_set(handle, ADV_INTERVAL_PERIODIC, 0);
971 	if (err) {
972 		goto exit;
973 	}
974 	printk("success.\n");
975 
976 	printk("enabling periodic...");
977 	err = ll_adv_sync_enable(handle, BT_HCI_LE_SET_PER_ADV_ENABLE_ENABLE);
978 	if (err) {
979 		goto exit;
980 	}
981 	printk("success.\n");
982 
983 	printk("Trying to remove an adv set with sync enabled ...");
984 	err = ll_adv_aux_set_remove(handle);
985 	if (err != BT_HCI_ERR_CMD_DISALLOWED) {
986 		goto exit;
987 	}
988 	printk("success.\n");
989 
990 	printk("Disabling periodic...");
991 	err = ll_adv_sync_enable(handle, 0);
992 	if (err) {
993 		goto exit;
994 	}
995 	printk("success.\n");
996 
997 	printk("Trying to remove an adv set after sync disabled ...");
998 	err = ll_adv_aux_set_remove(handle);
999 	if (err) {
1000 		goto exit;
1001 	}
1002 	printk("success.\n");
1003 
1004 	uint8_t num_adv_sets;
1005 	num_adv_sets = ll_adv_aux_set_count_get();
1006 
1007 	printk("Creating every other adv set ...");
1008 	for (handle = 0; handle < num_adv_sets; handle += 2) {
1009 		err = ll_adv_params_set(handle, evt_prop, ADV_INTERVAL, adv_type,
1010 					OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
1011 					ADV_CHAN_MAP, FILTER_POLICY, ADV_TX_PWR,
1012 					phy_p, ADV_SEC_SKIP, phy_s, ADV_SID,
1013 					SCAN_REQ_NOT);
1014 		if (err) {
1015 			goto exit;
1016 		}
1017 	}
1018 	printk("success.\n");
1019 
1020 	printk("Clearing all adv sets...");
1021 	err = ll_adv_aux_set_clear();
1022 	if (err) {
1023 		goto exit;
1024 	}
1025 	printk("success.\n");
1026 
1027 	printk("Trying to remove adv sets ...");
1028 	for (handle = 0; handle < num_adv_sets; ++handle) {
1029 		err = ll_adv_aux_set_remove(handle);
1030 		if (err != BT_HCI_ERR_UNKNOWN_ADV_IDENTIFIER) {
1031 			goto exit;
1032 		}
1033 	}
1034 	printk("success.\n");
1035 
1036 	printk("Creating one adv set ...");
1037 	handle = 0;
1038 	err = ll_adv_params_set(handle, evt_prop, ADV_INTERVAL, adv_type,
1039 				OWN_ADDR_TYPE, PEER_ADDR_TYPE, PEER_ADDR,
1040 				ADV_CHAN_MAP, FILTER_POLICY, ADV_TX_PWR,
1041 				phy_p, ADV_SEC_SKIP, phy_s, ADV_SID,
1042 				SCAN_REQ_NOT);
1043 	if (err) {
1044 		goto exit;
1045 	}
1046 	printk("success.\n");
1047 
1048 	printk("Enabling adv set...");
1049 	err = ll_adv_enable(handle, 1, 0, 0);
1050 	if (err) {
1051 		goto exit;
1052 	}
1053 	printk("success.\n");
1054 
1055 	printk("Clearing all adv sets...");
1056 	err = ll_adv_aux_set_clear();
1057 	if (err != BT_HCI_ERR_CMD_DISALLOWED) {
1058 		goto exit;
1059 	}
1060 	printk("success.\n");
1061 
1062 	PASS("AdvX tests Passed\n");
1063 	bs_trace_silent_exit(0);
1064 
1065 	return;
1066 
1067 exit:
1068 	printk("failed (%d)\n", err);
1069 
1070 	bst_result = Failed;
1071 	bs_trace_silent_exit(0);
1072 }
1073 
1074 
1075 static bool is_reenable_addr;
1076 
scan_cb(const bt_addr_le_t * addr,int8_t rssi,uint8_t adv_type,struct net_buf_simple * buf)1077 static void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type,
1078 		    struct net_buf_simple *buf)
1079 {
1080 	char le_addr[BT_ADDR_LE_STR_LEN];
1081 
1082 	bt_addr_le_to_str(addr, le_addr, sizeof(le_addr));
1083 	printk("%s: type = 0x%x, addr = %s\n", __func__, adv_type, le_addr);
1084 
1085 	if (!is_reenable_addr &&
1086 	    !memcmp(own_addr_reenable, addr->a.val,
1087 		    sizeof(own_addr_reenable))) {
1088 		is_reenable_addr = true;
1089 	}
1090 
1091 	if (connection_to_test) {
1092 		struct bt_conn *conn;
1093 		int err;
1094 
1095 		connection_to_test = false;
1096 
1097 		err = bt_le_scan_stop();
1098 		if (err) {
1099 			printk("Stop LE scan failed (err %d)\n", err);
1100 			return;
1101 		}
1102 
1103 		err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
1104 					BT_LE_CONN_PARAM_DEFAULT,
1105 					&conn);
1106 		if (err) {
1107 			printk("Create conn failed (err %d)\n", err);
1108 		} else {
1109 			bt_conn_unref(conn);
1110 		}
1111 	} else if (!is_scanned) {
1112 		char addr_str[BT_ADDR_LE_STR_LEN];
1113 
1114 		bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
1115 		printk("Device found: %s, type: %u, AD len: %u, RSSI %d\n",
1116 			addr_str, adv_type, buf->len, rssi);
1117 
1118 		if ((buf->len == adv_data_expected_len) &&
1119 		    !memcmp(buf->data, adv_data_expected,
1120 			    adv_data_expected_len)) {
1121 			is_scanned = true;
1122 		}
1123 	}
1124 }
1125 
phy2str(uint8_t phy)1126 static const char *phy2str(uint8_t phy)
1127 {
1128 	switch (phy) {
1129 	case 0: return "No packets";
1130 	case BT_GAP_LE_PHY_1M: return "LE 1M";
1131 	case BT_GAP_LE_PHY_2M: return "LE 2M";
1132 	case BT_GAP_LE_PHY_CODED: return "LE Coded";
1133 	default: return "Unknown";
1134 	}
1135 }
1136 
1137 #define NAME_LEN 30
1138 
data_cb(struct bt_data * data,void * user_data)1139 static bool data_cb(struct bt_data *data, void *user_data)
1140 {
1141 	char *name = user_data;
1142 
1143 	switch (data->type) {
1144 	case BT_DATA_NAME_SHORTENED:
1145 	case BT_DATA_NAME_COMPLETE:
1146 		memcpy(name, data->data, MIN(data->data_len, NAME_LEN - 1));
1147 		return false;
1148 	default:
1149 		return true;
1150 	}
1151 }
1152 
1153 static bool is_scannable;
1154 static bool is_scan_rsp;
1155 static bool is_periodic;
1156 static uint8_t per_sid;
1157 static bt_addr_le_t per_addr;
1158 static uint8_t per_adv_evt_cnt_actual;
1159 
scan_recv(const struct bt_le_scan_recv_info * info,struct net_buf_simple * buf)1160 static void scan_recv(const struct bt_le_scan_recv_info *info,
1161 		      struct net_buf_simple *buf)
1162 {
1163 	char le_addr[BT_ADDR_LE_STR_LEN];
1164 	char name[NAME_LEN];
1165 
1166 	(void)memset(name, 0, sizeof(name));
1167 
1168 	bt_data_parse(buf, data_cb, name);
1169 
1170 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
1171 	printk("[DEVICE]: %s, AD evt type %u, Tx Pwr: %i, RSSI %i %s "
1172 	       "C:%u S:%u D:%u SR:%u E:%u Prim: %s, Secn: %s, "
1173 	       "Interval: 0x%04x (%u ms), SID: %u\n",
1174 	       le_addr, info->adv_type, info->tx_power, info->rssi, name,
1175 	       (info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) != 0,
1176 	       (info->adv_props & BT_GAP_ADV_PROP_SCANNABLE) != 0,
1177 	       (info->adv_props & BT_GAP_ADV_PROP_DIRECTED) != 0,
1178 	       (info->adv_props & BT_GAP_ADV_PROP_SCAN_RESPONSE) != 0,
1179 	       (info->adv_props & BT_GAP_ADV_PROP_EXT_ADV) != 0,
1180 	       phy2str(info->primary_phy), phy2str(info->secondary_phy),
1181 	       info->interval, info->interval * 5 / 4, info->sid);
1182 
1183 	if (!is_scannable &&
1184 	    ((info->adv_props & BT_GAP_ADV_PROP_SCANNABLE) != 0)) {
1185 		is_scannable = true;
1186 	}
1187 
1188 	if (!is_scan_rsp &&
1189 	    ((info->adv_props & BT_GAP_ADV_PROP_SCANNABLE) != 0) &&
1190 	    ((info->adv_props & BT_GAP_ADV_PROP_SCAN_RESPONSE) != 0) &&
1191 	    (strlen(name) == strlen(CONFIG_BT_DEVICE_NAME)) &&
1192 	    (!strcmp(name, CONFIG_BT_DEVICE_NAME))) {
1193 		is_scan_rsp = true;
1194 	}
1195 
1196 	if (info->interval) {
1197 		if (!is_periodic) {
1198 			is_periodic = true;
1199 			per_sid = info->sid;
1200 			bt_addr_le_copy(&per_addr, info->addr);
1201 		} else {
1202 			if ((per_sid == info->sid) &&
1203 			    bt_addr_le_eq(&per_addr, info->addr)) {
1204 				per_adv_evt_cnt_actual++;
1205 
1206 				printk("per_adv_evt_cnt_actual %u\n",
1207 				       per_adv_evt_cnt_actual);
1208 			}
1209 		}
1210 	}
1211 }
1212 
1213 static bool is_scan_timeout;
1214 
scan_timeout(void)1215 static void scan_timeout(void)
1216 {
1217 	is_scan_timeout = true;
1218 }
1219 
1220 static struct bt_le_scan_cb scan_callbacks = {
1221 	.recv = scan_recv,
1222 	.timeout = scan_timeout,
1223 };
1224 
1225 static bool volatile is_sync;
1226 static bool volatile is_sync_report;
1227 static bool volatile is_sync_lost;
1228 static uint8_t volatile sync_report_len;
1229 static uint8_t sync_report_data[251];
1230 
1231 static void
per_adv_sync_sync_cb(struct bt_le_per_adv_sync * sync,struct bt_le_per_adv_sync_synced_info * info)1232 per_adv_sync_sync_cb(struct bt_le_per_adv_sync *sync,
1233 		     struct bt_le_per_adv_sync_synced_info *info)
1234 {
1235 	char le_addr[BT_ADDR_LE_STR_LEN];
1236 
1237 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
1238 
1239 	printk("PER_ADV_SYNC[%u]: [DEVICE]: %s synced, "
1240 	       "Interval 0x%04x (%u ms), PHY %s\n",
1241 	       bt_le_per_adv_sync_get_index(sync), le_addr,
1242 	       info->interval, info->interval * 5 / 4, phy2str(info->phy));
1243 
1244 	is_sync = true;
1245 }
1246 
1247 static void
per_adv_sync_terminated_cb(struct bt_le_per_adv_sync * sync,const struct bt_le_per_adv_sync_term_info * info)1248 per_adv_sync_terminated_cb(struct bt_le_per_adv_sync *sync,
1249 			   const struct bt_le_per_adv_sync_term_info *info)
1250 {
1251 	char le_addr[BT_ADDR_LE_STR_LEN];
1252 
1253 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
1254 
1255 	printk("PER_ADV_SYNC[%u]: [DEVICE]: %s sync terminated\n",
1256 	       bt_le_per_adv_sync_get_index(sync), le_addr);
1257 
1258 	is_sync_lost = true;
1259 }
1260 
1261 static void
per_adv_sync_recv_cb(struct bt_le_per_adv_sync * sync,const struct bt_le_per_adv_sync_recv_info * info,struct net_buf_simple * buf)1262 per_adv_sync_recv_cb(struct bt_le_per_adv_sync *sync,
1263 		     const struct bt_le_per_adv_sync_recv_info *info,
1264 		     struct net_buf_simple *buf)
1265 {
1266 	char le_addr[BT_ADDR_LE_STR_LEN];
1267 
1268 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
1269 
1270 	printk("PER_ADV_SYNC[%u]: [DEVICE]: %s, tx_power %i, "
1271 	       "RSSI %i, CTE %u, data length %u\n",
1272 	       bt_le_per_adv_sync_get_index(sync), le_addr, info->tx_power,
1273 	       info->rssi, info->cte_type, buf->len);
1274 
1275 	if (!is_sync_report) {
1276 		is_sync_report = true;
1277 		sync_report_len = buf->len;
1278 		memcpy(sync_report_data, buf->data, buf->len);
1279 	}
1280 }
1281 
1282 static struct bt_le_per_adv_sync_cb sync_cb = {
1283 	.synced = per_adv_sync_sync_cb,
1284 	.term = per_adv_sync_terminated_cb,
1285 	.recv = per_adv_sync_recv_cb
1286 };
1287 
test_scanx_main(void)1288 static void test_scanx_main(void)
1289 {
1290 	struct bt_le_scan_param scan_param = {
1291 		.type       = BT_HCI_LE_SCAN_ACTIVE,
1292 		.options    = BT_LE_SCAN_OPT_NONE,
1293 		.interval   = 0x0004,
1294 		.window     = 0x0004,
1295 	};
1296 	struct bt_le_per_adv_sync_param sync_create_param;
1297 	struct bt_le_per_adv_sync *sync = NULL;
1298 	uint8_t per_adv_evt_cnt_expected;
1299 	uint8_t sync_report_len_prev;
1300 	int err;
1301 
1302 	printk("\n*Extended Scanning test*\n");
1303 
1304 	printk("Bluetooth initializing...");
1305 	err = bt_enable(NULL);
1306 	if (err) {
1307 		goto exit;
1308 	}
1309 	printk("success.\n");
1310 
1311 	printk("Scan callbacks register...");
1312 	bt_le_scan_cb_register(&scan_callbacks);
1313 	printk("success.\n");
1314 
1315 	printk("Periodic Advertising callbacks register...");
1316 	bt_le_per_adv_sync_cb_register(&sync_cb);
1317 	printk("Success.\n");
1318 
1319 	connection_to_test = true;
1320 
1321 	printk("Start scanning...");
1322 	is_reenable_addr = false;
1323 	err = bt_le_scan_start(&scan_param, scan_cb);
1324 	if (err) {
1325 		goto exit;
1326 	}
1327 	printk("success.\n");
1328 
1329 	printk("Waiting for connection...");
1330 	while (!is_connected) {
1331 		k_sleep(K_MSEC(100));
1332 	}
1333 	printk("success.\n");
1334 
1335 	printk("Waiting for disconnect...");
1336 	while (!is_disconnected) {
1337 		k_sleep(K_MSEC(100));
1338 	}
1339 	printk("success.\n");
1340 
1341 	is_connected = false;
1342 	is_disconnected = false;
1343 	connection_to_test = false;
1344 
1345 	printk("Start scanning...");
1346 	adv_data_expected = adv_data;
1347 	adv_data_expected_len = sizeof(adv_data);
1348 	err = bt_le_scan_start(&scan_param, scan_cb);
1349 	if (err) {
1350 		goto exit;
1351 	}
1352 	printk("success.\n");
1353 
1354 	printk("Waiting for advertising report, switch back from directed...");
1355 	while (!is_scanned) {
1356 		k_sleep(K_MSEC(100));
1357 	}
1358 	printk("success.\n");
1359 
1360 	printk("Waiting for advertising report, data update while directed...");
1361 	adv_data_expected = adv_data1;
1362 	adv_data_expected_len = sizeof(adv_data1);
1363 	is_scanned = false;
1364 	while (!is_scanned) {
1365 		k_sleep(K_MSEC(100));
1366 	}
1367 	printk("success.\n");
1368 
1369 	printk("Stop scanning...");
1370 	err = bt_le_scan_stop();
1371 	if (err) {
1372 		goto exit;
1373 	}
1374 	printk("success.\n");
1375 
1376 	printk("Start scanning...");
1377 	is_scannable = false;
1378 	is_scan_rsp = false;
1379 	err = bt_le_scan_start(&scan_param, scan_cb);
1380 	if (err) {
1381 		goto exit;
1382 	}
1383 	printk("success.\n");
1384 
1385 	printk("Waiting for scannable advertising report...\n");
1386 	while (!is_scannable) {
1387 		k_sleep(K_MSEC(100));
1388 	}
1389 	printk("success.\n");
1390 
1391 	printk("Waiting for scan response advertising report...\n");
1392 	while (!is_scan_rsp) {
1393 		k_sleep(K_MSEC(100));
1394 	}
1395 	printk("success.\n");
1396 
1397 	/* This wait is to ensure we match with connectable advertising in the
1398 	 * advertiser's timeline.
1399 	 */
1400 	k_sleep(K_MSEC(500));
1401 
1402 	connection_to_test = true;
1403 
1404 	printk("Waiting for connection...");
1405 	while (!is_connected) {
1406 		k_sleep(K_MSEC(100));
1407 	}
1408 	printk("success.\n");
1409 
1410 	printk("Waiting for disconnect...");
1411 	while (!is_disconnected) {
1412 		k_sleep(K_MSEC(100));
1413 	}
1414 	printk("success.\n");
1415 
1416 	printk("Start scanning for a duration...");
1417 	is_scan_timeout = false;
1418 	scan_param.interval = 0x08;
1419 	scan_param.timeout = 100;
1420 	err = bt_le_scan_start(&scan_param, scan_cb);
1421 	if (err) {
1422 		goto exit;
1423 	}
1424 	printk("success.\n");
1425 
1426 	k_sleep(K_MSEC(scan_param.timeout * 10 + 10));
1427 
1428 	printk("Checking for scan timeout...");
1429 	if (!is_scan_timeout) {
1430 		err = -EIO;
1431 		goto exit;
1432 	}
1433 	printk("done.\n");
1434 
1435 	printk("Start continuous scanning for a duration...");
1436 	is_scan_timeout = false;
1437 	scan_param.interval = 0x04;
1438 	err = bt_le_scan_start(&scan_param, scan_cb);
1439 	if (err) {
1440 		goto exit;
1441 	}
1442 	printk("success.\n");
1443 
1444 	k_sleep(K_MSEC(scan_param.timeout * 10 + 10));
1445 
1446 	printk("Checking for scan timeout...");
1447 	if (!is_scan_timeout) {
1448 		err = -EIO;
1449 		goto exit;
1450 	}
1451 	printk("done.\n");
1452 
1453 	scan_param.timeout = 0;
1454 
1455 	k_sleep(K_MSEC(2000));
1456 
1457 	printk("Start scanning for Periodic Advertisements...");
1458 	is_periodic = false;
1459 	is_reenable_addr = false;
1460 	per_adv_evt_cnt_actual = 0;
1461 	per_adv_evt_cnt_expected = 3;
1462 	err = bt_le_scan_start(&scan_param, scan_cb);
1463 	if (err) {
1464 		goto exit;
1465 	}
1466 	printk("success.\n");
1467 
1468 	printk("Verify address update due to re-enable of advertising...");
1469 	while (!is_reenable_addr) {
1470 		k_sleep(K_MSEC(30));
1471 	}
1472 	printk("success.\n");
1473 
1474 	printk("Waiting...");
1475 	while (!is_periodic ||
1476 	       (per_adv_evt_cnt_actual != per_adv_evt_cnt_expected)) {
1477 		k_sleep(K_MSEC(ADV_WAIT_MS));
1478 	}
1479 	printk("done.\n");
1480 
1481 	printk("Stop scanning...");
1482 	err = bt_le_scan_stop();
1483 	if (err) {
1484 		goto exit;
1485 	}
1486 	printk("success.\n");
1487 
1488 	printk("Creating Periodic Advertising Sync 0...");
1489 	bt_addr_le_copy(&sync_create_param.addr, &per_addr);
1490 	sync_create_param.options = 0;
1491 	sync_create_param.sid = 0xf;
1492 	sync_create_param.skip = 0;
1493 	sync_create_param.timeout = 0xa;
1494 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
1495 	if (err) {
1496 		goto exit;
1497 	}
1498 	printk("success.\n");
1499 
1500 	printk("Check duplicate Periodic Advertising Sync create before sync "
1501 	       "established event...");
1502 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
1503 	if (!err) {
1504 		goto exit;
1505 	}
1506 	printk("success.\n");
1507 
1508 	printk("Start scanning...");
1509 	err = bt_le_scan_start(&scan_param, scan_cb);
1510 	if (err) {
1511 		goto exit;
1512 	}
1513 	printk("success.\n");
1514 
1515 	k_sleep(K_MSEC(400));
1516 
1517 	printk("Canceling Periodic Advertising Sync 0 while scanning...");
1518 	err = bt_le_per_adv_sync_delete(sync);
1519 	if (err) {
1520 		goto exit;
1521 	}
1522 	printk("success.\n");
1523 
1524 	printk("Stop scanning...");
1525 	err = bt_le_scan_stop();
1526 	if (err) {
1527 		goto exit;
1528 	}
1529 	printk("success.\n");
1530 
1531 	printk("Creating Periodic Advertising Sync 1...");
1532 	bt_addr_le_copy(&sync_create_param.addr, &per_addr);
1533 	sync_create_param.options = 0;
1534 	sync_create_param.sid = 0xf;
1535 	sync_create_param.skip = 0;
1536 	sync_create_param.timeout = 0xa;
1537 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
1538 	if (err) {
1539 		goto exit;
1540 	}
1541 	printk("success.\n");
1542 
1543 	printk("Canceling Periodic Advertising Sync 1 without scanning...");
1544 	err = bt_le_per_adv_sync_delete(sync);
1545 	if (err) {
1546 		goto exit;
1547 	}
1548 	printk("success.\n");
1549 
1550 	printk("Creating Periodic Advertising Sync 2...");
1551 	is_sync = false;
1552 	bt_addr_le_copy(&sync_create_param.addr, &per_addr);
1553 	sync_create_param.options = 0;
1554 	sync_create_param.sid = per_sid;
1555 	sync_create_param.skip = 0;
1556 	sync_create_param.timeout = 0xa;
1557 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
1558 	if (err) {
1559 		goto exit;
1560 	}
1561 	printk("success.\n");
1562 
1563 	printk("Start scanning...");
1564 	err = bt_le_scan_start(&scan_param, scan_cb);
1565 	if (err) {
1566 		goto exit;
1567 	}
1568 	printk("success.\n");
1569 
1570 	printk("Waiting for sync...");
1571 	while (!is_sync) {
1572 		k_sleep(K_MSEC(100));
1573 	}
1574 	printk("done.\n");
1575 
1576 	printk("Check duplicate Periodic Advertising Sync create after sync "
1577 	       "established event...");
1578 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
1579 	if (!err) {
1580 		goto exit;
1581 	}
1582 	printk("success.\n");
1583 
1584 	printk("Deleting Periodic Advertising Sync 2...");
1585 	err = bt_le_per_adv_sync_delete(sync);
1586 	if (err) {
1587 		goto exit;
1588 	}
1589 	printk("success.\n");
1590 
1591 	printk("Stop scanning...");
1592 	err = bt_le_scan_stop();
1593 	if (err) {
1594 		goto exit;
1595 	}
1596 	printk("success.\n");
1597 
1598 	printk("Creating Periodic Advertising Sync 3, test sync lost...");
1599 	is_sync = false;
1600 	is_sync_report = false;
1601 	sync_report_len = 0;
1602 	is_sync_lost = false;
1603 	bt_addr_le_copy(&sync_create_param.addr, &per_addr);
1604 	sync_create_param.options = 0;
1605 	sync_create_param.sid = per_sid;
1606 	sync_create_param.skip = 0;
1607 	sync_create_param.timeout = 0xa;
1608 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
1609 	if (err) {
1610 		goto exit;
1611 	}
1612 	printk("success.\n");
1613 
1614 	printk("Start scanning...");
1615 	err = bt_le_scan_start(&scan_param, scan_cb);
1616 	if (err) {
1617 		goto exit;
1618 	}
1619 	printk("success.\n");
1620 
1621 	printk("Waiting for sync...");
1622 	while (!is_sync) {
1623 		k_sleep(K_MSEC(100));
1624 	}
1625 	printk("done.\n");
1626 
1627 	printk("Stop scanning...");
1628 	err = bt_le_scan_stop();
1629 	if (err) {
1630 		goto exit;
1631 	}
1632 	printk("success.\n");
1633 
1634 	printk("Waiting for Periodic Advertising Report of 0 bytes...");
1635 	while (!is_sync_report) {
1636 		k_sleep(K_MSEC(100));
1637 	}
1638 	printk("done.\n");
1639 
1640 	if (sync_report_len != 0) {
1641 		FAIL("Incorrect Periodic Advertising Report data.");
1642 	}
1643 
1644 	printk("Waiting for Periodic Advertising Report of %u bytes...",
1645 	       sizeof(per_adv_data1));
1646 	sync_report_len_prev = sync_report_len;
1647 	while (!is_sync_report || (sync_report_len == sync_report_len_prev)) {
1648 		is_sync_report = false;
1649 		k_sleep(K_MSEC(100));
1650 	}
1651 	printk("done.\n");
1652 
1653 	if ((sync_report_len != sizeof(per_adv_data1)) ||
1654 	    memcmp(sync_report_data, per_adv_data1, sizeof(per_adv_data1))) {
1655 		FAIL("Incorrect Periodic Advertising Report data.");
1656 	}
1657 
1658 	printk("Waiting for Periodic Advertising Report of %u bytes...",
1659 	       sizeof(per_adv_data2));
1660 	sync_report_len_prev = sync_report_len;
1661 	while (!is_sync_report || (sync_report_len == sync_report_len_prev)) {
1662 		is_sync_report = false;
1663 		k_sleep(K_MSEC(100));
1664 	}
1665 	printk("done.\n");
1666 
1667 	if ((sync_report_len != sizeof(per_adv_data2)) ||
1668 	    memcmp(sync_report_data, per_adv_data2, sizeof(per_adv_data2))) {
1669 		FAIL("Incorrect Periodic Advertising Report data.");
1670 	}
1671 
1672 	printk("Waiting for Periodic Advertising Report of %u bytes...",
1673 	       sizeof(per_adv_data3));
1674 	sync_report_len_prev = sync_report_len;
1675 	while (!is_sync_report || (sync_report_len == sync_report_len_prev)) {
1676 		is_sync_report = false;
1677 		k_sleep(K_MSEC(100));
1678 	}
1679 	printk("done.\n");
1680 
1681 	if ((sync_report_len != sizeof(per_adv_data3)) ||
1682 	    memcmp(sync_report_data, per_adv_data3, sizeof(per_adv_data3))) {
1683 		FAIL("Incorrect Periodic Advertising Report data.");
1684 	}
1685 
1686 	printk("Waiting for Periodic Advertising Report of %u bytes...",
1687 	       sizeof(per_adv_data2));
1688 	sync_report_len_prev = sync_report_len;
1689 	while (!is_sync_report || (sync_report_len == sync_report_len_prev)) {
1690 		is_sync_report = false;
1691 		k_sleep(K_MSEC(100));
1692 	}
1693 	printk("done.\n");
1694 
1695 	if ((sync_report_len != sizeof(per_adv_data2)) ||
1696 	    memcmp(sync_report_data, per_adv_data2, sizeof(per_adv_data2))) {
1697 		FAIL("Incorrect Periodic Advertising Report data.");
1698 	}
1699 
1700 	printk("Waiting for sync loss...");
1701 	while (!is_sync_lost) {
1702 		k_sleep(K_MSEC(100));
1703 	}
1704 	printk("done.\n");
1705 
1706 	printk("Add to resolving list...");
1707 	bt_addr_le_t peer_id_addr = {
1708 		.type = BT_ADDR_LE_RANDOM,
1709 		.a = {
1710 			.val = {0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5}
1711 		}
1712 	};
1713 	uint8_t pirk[16] = {0x01, };
1714 	uint8_t lirk[16] = {0x00, };
1715 
1716 	err = ll_rl_add(&peer_id_addr, pirk, lirk);
1717 	if (err) {
1718 		goto exit;
1719 	}
1720 	printk("success.\n");
1721 
1722 	printk("Enable resolving list...");
1723 	err = ll_rl_enable(BT_HCI_ADDR_RES_ENABLE);
1724 	if (err) {
1725 		goto exit;
1726 	}
1727 	printk("success.\n");
1728 
1729 	printk("Add device to periodic advertising list...");
1730 	err = bt_le_per_adv_list_add(&peer_id_addr, per_sid);
1731 	if (err) {
1732 		goto exit;
1733 	}
1734 	printk("success.\n");
1735 
1736 	printk("Creating Periodic Advertising Sync 4 after sync lost...");
1737 	is_sync = false;
1738 	is_sync_report = false;
1739 	bt_addr_le_copy(&sync_create_param.addr, &per_addr);
1740 	sync_create_param.options = BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST |
1741 				    BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE;
1742 	sync_create_param.sid = per_sid;
1743 	sync_create_param.skip = 0;
1744 	sync_create_param.timeout = 0xa;
1745 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
1746 	if (err) {
1747 		goto exit;
1748 	}
1749 	printk("success.\n");
1750 
1751 	printk("Start scanning...");
1752 	err = bt_le_scan_start(&scan_param, scan_cb);
1753 	if (err) {
1754 		goto exit;
1755 	}
1756 	printk("success.\n");
1757 
1758 	printk("Waiting for sync...");
1759 	while (!is_sync) {
1760 		k_sleep(K_MSEC(100));
1761 	}
1762 	printk("done.\n");
1763 
1764 	printk("Stop scanning...");
1765 	err = bt_le_scan_stop();
1766 	if (err) {
1767 		goto exit;
1768 	}
1769 	printk("success.\n");
1770 
1771 	printk("Waiting for Periodic Advertising Report of %u bytes...",
1772 	       sizeof(per_adv_data3));
1773 	sync_report_len_prev = sync_report_len;
1774 	while (!is_sync_report || (sync_report_len == sync_report_len_prev)) {
1775 		is_sync_report = false;
1776 		k_sleep(K_MSEC(100));
1777 	}
1778 	printk("done.\n");
1779 
1780 	if ((sync_report_len != sizeof(per_adv_data3)) ||
1781 	    memcmp(sync_report_data, per_adv_data3, sizeof(per_adv_data3))) {
1782 		FAIL("Incorrect Periodic Advertising Report data (%u != %u).",
1783 		     sync_report_len, sizeof(per_adv_data3));
1784 	}
1785 
1786 	printk("Wait for no duplicate Periodic Advertising Report"
1787 	       " is generated...");
1788 	is_sync_report = false;
1789 	k_sleep(K_MSEC(400));
1790 	if (is_sync_report) {
1791 		goto exit;
1792 	}
1793 	printk("success\n");
1794 
1795 	printk("Deleting Periodic Advertising Sync 4...");
1796 	err = bt_le_per_adv_sync_delete(sync);
1797 	if (err) {
1798 		goto exit;
1799 	}
1800 	printk("success.\n");
1801 
1802 	PASS("ScanX tests Passed\n");
1803 
1804 	return;
1805 
1806 exit:
1807 	printk("failed (%d)\n", err);
1808 
1809 	bst_result = Failed;
1810 	bs_trace_silent_exit(0);
1811 }
1812 
test_advx_init(void)1813 static void test_advx_init(void)
1814 {
1815 	bst_ticker_set_next_tick_absolute(30e6);
1816 	bst_result = In_progress;
1817 }
1818 
test_advx_tick(bs_time_t HW_device_time)1819 static void test_advx_tick(bs_time_t HW_device_time)
1820 {
1821 	bst_result = Failed;
1822 	bs_trace_error_line("Test advx/scanx finished.\n");
1823 }
1824 
1825 static const struct bst_test_instance test_def[] = {
1826 	{
1827 		.test_id = "advx",
1828 		.test_descr = "Extended Advertising",
1829 		.test_post_init_f = test_advx_init,
1830 		.test_tick_f = test_advx_tick,
1831 		.test_main_f = test_advx_main
1832 	},
1833 	{
1834 		.test_id = "scanx",
1835 		.test_descr = "Extended scanning",
1836 		.test_post_init_f = test_advx_init,
1837 		.test_tick_f = test_advx_tick,
1838 		.test_main_f = test_scanx_main
1839 	},
1840 	BSTEST_END_MARKER
1841 };
1842 
test_advx_install(struct bst_test_list * tests)1843 struct bst_test_list *test_advx_install(struct bst_test_list *tests)
1844 {
1845 	return bst_add_tests(tests, test_def);
1846 }
1847 
1848 bst_test_install_t test_installers[] = {
1849 	test_advx_install,
1850 	NULL
1851 };
1852 
main(void)1853 int main(void)
1854 {
1855 	bst_main();
1856 	return 0;
1857 }
1858