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