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