1 /*
2  * Copyright (c) 2024 Demant A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stddef.h>
8 
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/types.h>
13 #include <stddef.h>
14 #include <errno.h>
15 #include <zephyr/kernel.h>
16 
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/hci.h>
19 #include <zephyr/bluetooth/iso.h>
20 #include <zephyr/bluetooth/hci.h>
21 #include <zephyr/bluetooth/conn.h>
22 #include <zephyr/bluetooth/uuid.h>
23 #include <zephyr/bluetooth/gatt.h>
24 #include <zephyr/sys/byteorder.h>
25 
26 #include "subsys/bluetooth/host/hci_core.h"
27 #include "subsys/bluetooth/controller/include/ll.h"
28 #include "subsys/bluetooth/controller/util/memq.h"
29 #include "subsys/bluetooth/controller/ll_sw/lll.h"
30 
31 #include "bs_types.h"
32 #include "bs_tracing.h"
33 #include "time_machine.h"
34 #include "bstests.h"
35 
36 static struct bt_conn *default_conn;
37 
38 extern enum bst_result_t bst_result;
39 
40 #define FAIL(...)					\
41 	do {						\
42 		bst_result = Failed;			\
43 		bs_trace_error_time_line(__VA_ARGS__);	\
44 	} while (0)
45 
46 #define PASS(...)					\
47 	do {						\
48 		bst_result = Passed;			\
49 		bs_trace_info_time(1, __VA_ARGS__);	\
50 	} while (0)
51 
52 extern enum bst_result_t bst_result;
53 
54 static K_SEM_DEFINE(sem_is_sync, 0, 1);
55 static K_SEM_DEFINE(sem_is_conn, 0, 1);
56 
57 /* Set timeout to 20s */
58 #define K_SEM_TIMEOUT    K_MSEC(20000)
59 
60 struct bt_le_per_adv_sync *default_sync;
61 
connected(struct bt_conn * conn,uint8_t conn_err)62 static void connected(struct bt_conn *conn, uint8_t conn_err)
63 {
64 	char addr[BT_ADDR_LE_STR_LEN];
65 
66 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
67 
68 	if (conn_err) {
69 		FAIL("Failed to connect to %s (%u)\n", addr, conn_err);
70 		return;
71 	}
72 	printk("Connected: %s\n", addr);
73 
74 	k_sem_give(&sem_is_conn);
75 }
76 
eir_found(struct bt_data * data,void * user_data)77 static bool eir_found(struct bt_data *data, void *user_data)
78 {
79 	bt_addr_le_t *addr = user_data;
80 	int i;
81 
82 	printk("[AD]: %u data_len %u\n", data->type, data->data_len);
83 
84 	switch (data->type) {
85 	case BT_DATA_UUID16_SOME:
86 	case BT_DATA_UUID16_ALL:
87 		if (data->data_len % sizeof(uint16_t) != 0U) {
88 			FAIL("AD malformed\n");
89 			return true;
90 		}
91 
92 		for (i = 0; i < data->data_len; i += sizeof(uint16_t)) {
93 			const struct bt_uuid *uuid;
94 			struct bt_le_conn_param *param;
95 			uint16_t u16;
96 			int err;
97 
98 			memcpy(&u16, &data->data[i], sizeof(u16));
99 			uuid = BT_UUID_DECLARE_16(sys_le16_to_cpu(u16));
100 			if (bt_uuid_cmp(uuid, BT_UUID_HRS)) {
101 				continue;
102 			}
103 
104 			err = bt_le_scan_stop();
105 			if (err) {
106 				FAIL("Stop LE scan failed (err %d)\n", err);
107 				continue;
108 			}
109 
110 			param = BT_LE_CONN_PARAM_DEFAULT;
111 			err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
112 						param, &default_conn);
113 			if (err) {
114 				printk("Create conn failed (err %d)\n", err);
115 			}
116 
117 			return false;
118 		}
119 	}
120 
121 	return true;
122 }
123 
device_found(const bt_addr_le_t * addr,int8_t rssi,uint8_t type,struct net_buf_simple * ad)124 static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
125 		struct net_buf_simple *ad)
126 {
127 	char dev[BT_ADDR_LE_STR_LEN];
128 
129 	bt_addr_le_to_str(addr, dev, sizeof(dev));
130 	printk("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i\n",
131 			dev, type, ad->len, rssi);
132 
133 	/* We're only interested in connectable events */
134 	if (type == BT_GAP_ADV_TYPE_ADV_IND ||
135 		type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
136 		bt_data_parse(ad, eir_found, (void *)addr);
137 	}
138 }
139 
disconnected(struct bt_conn * conn,uint8_t reason)140 static void disconnected(struct bt_conn *conn, uint8_t reason)
141 {
142 	char addr[BT_ADDR_LE_STR_LEN];
143 
144 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
145 
146 	printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
147 
148 	if (default_conn != conn) {
149 		return;
150 	}
151 
152 	bt_conn_unref(default_conn);
153 	default_conn = NULL;
154 }
155 
156 static struct bt_conn_cb conn_callbacks = {
157 	.connected = connected,
158 	.disconnected = disconnected,
159 };
160 
161 
setup_ext_adv(struct bt_le_ext_adv ** adv)162 static void setup_ext_adv(struct bt_le_ext_adv **adv)
163 {
164 	int err;
165 
166 	printk("Create advertising set...");
167 	err = bt_le_ext_adv_create(BT_LE_EXT_ADV_NCONN, NULL, adv);
168 	if (err) {
169 		FAIL("Failed to create advertising set (err %d)\n", err);
170 		return;
171 	}
172 	printk("success.\n");
173 
174 	printk("Setting Periodic Advertising parameters...");
175 	err = bt_le_per_adv_set_param(*adv, BT_LE_PER_ADV_DEFAULT);
176 	if (err) {
177 		FAIL("Failed to set periodic advertising parameters (err %d)\n",
178 			 err);
179 		return;
180 	}
181 	printk("success.\n");
182 
183 	printk("Enable Periodic Advertising...");
184 	err = bt_le_per_adv_start(*adv);
185 	if (err) {
186 		FAIL("Failed to enable periodic advertising (err %d)\n", err);
187 		return;
188 	}
189 	printk("success.\n");
190 
191 	printk("Start extended advertising...");
192 	err = bt_le_ext_adv_start(*adv, BT_LE_EXT_ADV_START_DEFAULT);
193 	if (err) {
194 		printk("Failed to start extended advertising (err %d)\n", err);
195 		return;
196 	}
197 	printk("success.\n");
198 }
199 
teardown_ext_adv(struct bt_le_ext_adv * adv)200 static void teardown_ext_adv(struct bt_le_ext_adv *adv)
201 {
202 	int err;
203 
204 	printk("Stop Periodic Advertising...");
205 	err = bt_le_per_adv_stop(adv);
206 	if (err) {
207 		FAIL("Failed to stop periodic advertising (err %d)\n", err);
208 		return;
209 	}
210 	printk("success.\n");
211 
212 	printk("Stop Extended Advertising...");
213 	err = bt_le_ext_adv_stop(adv);
214 	if (err) {
215 		FAIL("Failed to stop extended advertising (err %d)\n", err);
216 		return;
217 	}
218 	printk("success.\n");
219 
220 	printk("Deleting Extended Advertising...");
221 	err = bt_le_ext_adv_delete(adv);
222 	if (err) {
223 		FAIL("Failed to delete extended advertising (err %d)\n", err);
224 		return;
225 	}
226 	printk("success.\n");
227 }
228 
phy2str(uint8_t phy)229 static const char *phy2str(uint8_t phy)
230 {
231 	switch (phy) {
232 	case 0: return "No packets";
233 	case BT_GAP_LE_PHY_1M: return "LE 1M";
234 	case BT_GAP_LE_PHY_2M: return "LE 2M";
235 	case BT_GAP_LE_PHY_CODED: return "LE Coded";
236 	default: return "Unknown";
237 	}
238 }
239 
pa_sync_cb(struct bt_le_per_adv_sync * sync,struct bt_le_per_adv_sync_synced_info * info)240 static void pa_sync_cb(struct bt_le_per_adv_sync *sync,
241 			 struct bt_le_per_adv_sync_synced_info *info)
242 {
243 	char le_addr[BT_ADDR_LE_STR_LEN];
244 
245 	default_sync = sync;
246 
247 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
248 
249 	printk("PER_ADV_SYNC[%u]: [DEVICE]: %s synced, "
250 		   "Interval 0x%04x (%u ms), PHY %s\n",
251 		   bt_le_per_adv_sync_get_index(sync), le_addr,
252 		   info->interval, info->interval * 5 / 4, phy2str(info->phy));
253 
254 	k_sem_give(&sem_is_sync);
255 
256 	printk("Stop scanning\n");
257 	bt_le_scan_stop();
258 	printk("success.\n");
259 }
260 
pa_terminated_cb(struct bt_le_per_adv_sync * sync,const struct bt_le_per_adv_sync_term_info * info)261 static void pa_terminated_cb(struct bt_le_per_adv_sync *sync,
262 				 const struct bt_le_per_adv_sync_term_info *info)
263 {
264 	char le_addr[BT_ADDR_LE_STR_LEN];
265 
266 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
267 
268 	printk("PER_ADV_SYNC[%u]: [DEVICE]: %s sync terminated\n",
269 		   bt_le_per_adv_sync_get_index(sync), le_addr);
270 }
271 
272 static void
pa_state_changed_cb(struct bt_le_per_adv_sync * sync,const struct bt_le_per_adv_sync_state_info * info)273 pa_state_changed_cb(struct bt_le_per_adv_sync *sync,
274 			const struct bt_le_per_adv_sync_state_info *info)
275 {
276 	printk("PER_ADV_SYNC[%u]: state changed, receive %s.\n",
277 		   bt_le_per_adv_sync_get_index(sync),
278 		   info->recv_enabled ? "enabled" : "disabled");
279 }
280 
281 
282 static struct bt_le_per_adv_sync_cb sync_cb = {
283 	.synced = pa_sync_cb,
284 	.term = pa_terminated_cb,
285 	.state_changed = pa_state_changed_cb,
286 };
287 
288 static const struct bt_data ad[] = {
289 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
290 	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
291 			  BT_UUID_16_ENCODE(BT_UUID_HRS_VAL),
292 			  BT_UUID_16_ENCODE(BT_UUID_BAS_VAL),
293 			  BT_UUID_16_ENCODE(BT_UUID_CTS_VAL)),
294 };
295 
bt_ready(void)296 static void bt_ready(void)
297 {
298 	int err;
299 
300 	printk("Peripheral Bluetooth initialized\n");
301 
302 	err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
303 	if (err) {
304 		FAIL("Advertising failed to start (err %d)\n", err);
305 		return;
306 	}
307 
308 	printk("Advertising successfully started\n");
309 }
310 
311 #define NAME_LEN 30
312 
data_cb(struct bt_data * data,void * user_data)313 static bool data_cb(struct bt_data *data, void *user_data)
314 {
315 	char *name = user_data;
316 
317 	switch (data->type) {
318 	case BT_DATA_NAME_SHORTENED:
319 	case BT_DATA_NAME_COMPLETE:
320 		memcpy(name, data->data, MIN(data->data_len, NAME_LEN - 1));
321 		return false;
322 	default:
323 		return true;
324 	}
325 }
326 
327 static bool volatile is_periodic;
328 static bt_addr_le_t per_addr;
329 static uint8_t per_sid;
330 
scan_recv(const struct bt_le_scan_recv_info * info,struct net_buf_simple * buf)331 static void scan_recv(const struct bt_le_scan_recv_info *info,
332 			  struct net_buf_simple *buf)
333 {
334 	char le_addr[BT_ADDR_LE_STR_LEN];
335 	char name[NAME_LEN];
336 
337 	(void)memset(name, 0, sizeof(name));
338 
339 	bt_data_parse(buf, data_cb, name);
340 
341 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
342 	printk("[DEVICE]: %s, AD evt type %u, Tx Pwr: %i, RSSI %i %s "
343 		   "C:%u S:%u D:%u SR:%u E:%u Prim: %s, Secn: %s, "
344 		   "Interval: 0x%04x (%u ms), SID: %u\n",
345 		   le_addr, info->adv_type, info->tx_power, info->rssi, name,
346 		   (info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) != 0,
347 		   (info->adv_props & BT_GAP_ADV_PROP_SCANNABLE) != 0,
348 		   (info->adv_props & BT_GAP_ADV_PROP_DIRECTED) != 0,
349 		   (info->adv_props & BT_GAP_ADV_PROP_SCAN_RESPONSE) != 0,
350 		   (info->adv_props & BT_GAP_ADV_PROP_EXT_ADV) != 0,
351 		   phy2str(info->primary_phy), phy2str(info->secondary_phy),
352 		   info->interval, info->interval * 5 / 4, info->sid);
353 
354 	if (info->interval) {
355 		if (!is_periodic) {
356 			is_periodic = true;
357 			per_sid = info->sid;
358 			bt_addr_le_copy(&per_addr, info->addr);
359 		}
360 	}
361 }
362 
363 static struct bt_le_scan_cb scan_callbacks = {
364 	.recv = scan_recv,
365 };
366 
test_broadcast_main(void)367 static void test_broadcast_main(void)
368 {
369 	struct bt_le_ext_adv *adv;
370 	int err;
371 
372 	printk("\n*PA Broadcaster*\n");
373 
374 	printk("Bluetooth initializing...");
375 	err = bt_enable(NULL);
376 	if (err) {
377 		FAIL("Could not init BT: %d\n", err);
378 		return;
379 	}
380 	printk("success.\n");
381 
382 	setup_ext_adv(&adv);
383 
384 	k_sleep(K_MSEC(40000));
385 
386 	teardown_ext_adv(adv);
387 	adv = NULL;
388 
389 	PASS("Broadcast PA Passed\n");
390 }
391 
test_broadcast_past_sender_main(void)392 static void test_broadcast_past_sender_main(void)
393 {
394 	struct bt_le_ext_adv *adv;
395 	int err;
396 
397 	printk("\n*Broadcaster*\n");
398 
399 	printk("Connection callbacks register...\n");
400 	bt_conn_cb_register(&conn_callbacks);
401 	printk("Success.\n");
402 
403 	printk("Bluetooth initializing...");
404 	err = bt_enable(NULL);
405 	if (err) {
406 		FAIL("Could not init BT: %d\n", err);
407 		return;
408 	}
409 	printk("success.\n");
410 
411 	printk("Scanning for peripheral\n");
412 	err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, device_found);
413 	if (err) {
414 		FAIL("Scanning failed to start (err %d)\n", err);
415 		return;
416 	}
417 	printk("success.\n");
418 
419 	printk("Waiting for connection...\n");
420 	err = k_sem_take(&sem_is_conn, K_SEM_TIMEOUT);
421 	if (err) {
422 		FAIL("Failed to connect (err %d)\n", err);
423 		return;
424 	}
425 	printk("success.\n");
426 
427 	setup_ext_adv(&adv);
428 
429 	k_sleep(K_MSEC(500));
430 
431 	printk("Connection established and broadcasting - sending PAST\n");
432 	err = bt_le_per_adv_set_info_transfer(adv, default_conn, 0);
433 	if (err != 0) {
434 		FAIL("Could not transfer periodic adv sync: %d", err);
435 		return;
436 	}
437 	printk("success.\n");
438 
439 	printk("Wait 20s for PAST to be send\n");
440 	k_sleep(K_SEM_TIMEOUT);
441 
442 	printk("Disconnect before actually passing\n");
443 	err = bt_conn_disconnect(default_conn,
444 					BT_HCI_ERR_REMOTE_USER_TERM_CONN);
445 	if (err) {
446 		FAIL("Disconnection failed (err %d)\n", err);
447 		return;
448 	}
449 	printk("success.\n");
450 
451 	teardown_ext_adv(adv);
452 	adv = NULL;
453 
454 	PASS("Broadcast PA Passed\n");
455 }
456 
test_past_send_main(void)457 static void test_past_send_main(void)
458 {
459 	struct bt_le_scan_param scan_param = {
460 		.type	   = BT_LE_SCAN_TYPE_ACTIVE,
461 		.options	= BT_LE_SCAN_OPT_NONE,
462 		.interval   = 0x0004,
463 		.window	 = 0x0004,
464 	};
465 	struct bt_le_per_adv_sync_param sync_create_param;
466 	struct bt_le_per_adv_sync *sync = NULL;
467 	uint16_t service_data = 0;
468 	int err;
469 
470 	printk("\n*Send PAST test*\n");
471 
472 	printk("Connection callbacks register...\n");
473 	bt_conn_cb_register(&conn_callbacks);
474 	printk("Success.\n");
475 
476 	printk("Bluetooth initializing...\n");
477 	err = bt_enable(NULL);
478 	if (err) {
479 		FAIL("Could not init BT: %d\n", err);
480 		return;
481 	}
482 	printk("success.\n");
483 
484 	printk("Scan callbacks register...\n");
485 	bt_le_scan_cb_register(&scan_callbacks);
486 	printk("success.\n");
487 
488 	printk("Periodic Advertising callbacks register...\n");
489 	bt_le_per_adv_sync_cb_register(&sync_cb);
490 	printk("Success.\n");
491 
492 	printk("Start scanning...\n");
493 	is_periodic = false;
494 	err = bt_le_scan_start(&scan_param, NULL);
495 	if (err) {
496 		FAIL("Could not start scan: %d\n", err);
497 		return;
498 	}
499 	printk("success.\n");
500 
501 	while (!is_periodic) {
502 		k_sleep(K_MSEC(100));
503 	}
504 	printk("Periodic Advertising found (SID: %u)\n", per_sid);
505 
506 	printk("Creating Periodic Advertising Sync...\n");
507 	bt_addr_le_copy(&sync_create_param.addr, &per_addr);
508 	sync_create_param.options =
509 		BT_LE_PER_ADV_SYNC_OPT_REPORTING_INITIALLY_DISABLED;
510 	sync_create_param.sid = per_sid;
511 	sync_create_param.skip = 0;
512 	sync_create_param.timeout = 0xa;
513 
514 	err = bt_le_per_adv_sync_create(&sync_create_param, &sync);
515 	if (err) {
516 		FAIL("Could not create sync: %d\n", err);
517 		return;
518 	}
519 	printk("success.\n");
520 
521 	printk("Waiting for sync...\n");
522 	err = k_sem_take(&sem_is_sync, K_SEM_TIMEOUT);
523 	if (err) {
524 		FAIL("failed (err %d)\n", err);
525 		return;
526 	}
527 	printk("success.\n");
528 
529 
530 	printk("Scanning for peripheral\n");
531 	err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, device_found);
532 	if (err) {
533 		FAIL("Scanning failed to start (err %d)\n", err);
534 		return;
535 	}
536 	printk("success.\n");
537 
538 	printk("Waiting for connection...\n");
539 	err = k_sem_take(&sem_is_conn, K_SEM_TIMEOUT);
540 	if (err) {
541 		FAIL("Failed to connect (err %d)\n", err);
542 		return;
543 	}
544 	printk("success.\n");
545 
546 	k_sleep(K_MSEC(1000));
547 
548 	printk("Connection established - sending PAST\n");
549 	err = bt_le_per_adv_sync_transfer(sync, default_conn, service_data);
550 	if (err != 0) {
551 		FAIL("Could not transfer periodic adv sync: %d", err);
552 		return;
553 	}
554 	printk("success.\n");
555 
556 	printk("Wait 20s for PAST to be send\n");
557 	k_sleep(K_SEM_TIMEOUT);
558 
559 	printk("Disconnect before actually passing\n");
560 	err = bt_conn_disconnect(default_conn,
561 					BT_HCI_ERR_REMOTE_USER_TERM_CONN);
562 	if (err) {
563 		FAIL("Disconnection failed (err %d)\n", err);
564 		return;
565 	}
566 	printk("success.\n");
567 
568 	printk("Deleting Periodic Advertising Sync...\n");
569 	err = bt_le_per_adv_sync_delete(sync);
570 	if (err) {
571 		FAIL("Failed to delete periodic advertising sync (err %d)\n",
572 			 err);
573 		return;
574 	}
575 	printk("success.\n");
576 
577 	PASS("PAST send test Passed\n");
578 }
579 
test_past_recv_main(void)580 static void test_past_recv_main(void)
581 {
582 	struct bt_le_per_adv_sync_transfer_param past_param;
583 	int err;
584 
585 	printk("\n*Receive PAST Test*\n");
586 
587 	printk("Connection callbacks register...\n");
588 	bt_conn_cb_register(&conn_callbacks);
589 	printk("Success.\n");
590 
591 	printk("Bluetooth initializing...\n");
592 	err = bt_enable(NULL);
593 	if (err) {
594 		FAIL("Could not init BT: %d\n", err);
595 		return;
596 	}
597 	printk("success.\n");
598 
599 	printk("Scan callbacks register...\n");
600 	bt_le_scan_cb_register(&scan_callbacks);
601 	printk("success.\n");
602 
603 	printk("Periodic Advertising callbacks register...\n");
604 	bt_le_per_adv_sync_cb_register(&sync_cb);
605 	printk("Success.\n");
606 
607 	printk("Set default PAST Params.\n");
608 	past_param.skip = 1;
609 	past_param.timeout = 1000; /* 10 seconds */
610 	past_param.options = BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES;
611 
612 	err = bt_le_per_adv_sync_transfer_subscribe(NULL, &past_param);
613 	if (err) {
614 		FAIL("Failed to set default past parameters(err %d)\n",
615 			 err);
616 		return;
617 	}
618 	printk("success.\n");
619 
620 	bt_ready();
621 
622 	printk("Waiting for connection...\n");
623 	err = k_sem_take(&sem_is_conn, K_SEM_TIMEOUT);
624 	if (err) {
625 		FAIL("Failed to connect (err %d)\n", err);
626 		return;
627 	}
628 	printk("success.\n");
629 
630 	printk("Set PAST parameters for connection...\n");
631 	err = bt_le_per_adv_sync_transfer_subscribe(default_conn, &past_param);
632 	if (err) {
633 		FAIL("Failed to set default past parameters(err %d)\n",
634 			 err);
635 		return;
636 	}
637 	printk("success.\n");
638 
639 	printk("Wait 20s for Periodic advertisement sync to be established\n");
640 	err = k_sem_take(&sem_is_sync, K_SEM_TIMEOUT);
641 	if (err) {
642 		FAIL("failed (err %d)\n", err);
643 		return;
644 	}
645 	printk("success.\n");
646 
647 	printk("Deleting Periodic Advertising Sync...");
648 	err = bt_le_per_adv_sync_delete(default_sync);
649 	if (err) {
650 		FAIL("Failed to delete periodic advertising sync (err %d)\n",
651 			 err);
652 		return;
653 	}
654 	printk("success.\n");
655 
656 	PASS("PAST recv test Passed\n");
657 }
658 
test_past_recv_main_default_param(void)659 static void test_past_recv_main_default_param(void)
660 {
661 	struct bt_le_per_adv_sync_transfer_param past_param;
662 	int err;
663 
664 	printk("\n*Receive PAST Test*\n");
665 
666 	printk("Connection callbacks register...\n");
667 	bt_conn_cb_register(&conn_callbacks);
668 	printk("Success.\n");
669 
670 	printk("Bluetooth initializing...\n");
671 	err = bt_enable(NULL);
672 	if (err) {
673 		FAIL("Could not init BT: %d\n", err);
674 		return;
675 	}
676 	printk("success.\n");
677 
678 	printk("Scan callbacks register...\n");
679 	bt_le_scan_cb_register(&scan_callbacks);
680 	printk("success.\n");
681 
682 	printk("Periodic Advertising callbacks register...\n");
683 	bt_le_per_adv_sync_cb_register(&sync_cb);
684 	printk("Success.\n");
685 
686 	printk("Set default PAST Params.\n");
687 	past_param.skip = 1;
688 	past_param.timeout = 1000; /* 10 seconds */
689 	past_param.options = BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES;
690 
691 	err = bt_le_per_adv_sync_transfer_subscribe(NULL, &past_param);
692 	if (err) {
693 		FAIL("Failed to set default past parameters(err %d)\n",
694 			 err);
695 		return;
696 	}
697 	printk("success.\n");
698 
699 	bt_ready();
700 
701 	printk("Waiting for connection...\n");
702 	err = k_sem_take(&sem_is_conn, K_SEM_TIMEOUT);
703 	if (err) {
704 		FAIL("Failed to connect (err %d)\n", err);
705 		return;
706 	}
707 	printk("success.\n");
708 
709 	printk("Wait 20s for Periodic advertisement sync to be established\n");
710 	err = k_sem_take(&sem_is_sync, K_SEM_TIMEOUT);
711 	if (err) {
712 		FAIL("failed (err %d)\n", err);
713 		return;
714 	}
715 	printk("success.\n");
716 
717 	printk("Deleting Periodic Advertising Sync...");
718 	err = bt_le_per_adv_sync_delete(default_sync);
719 	if (err) {
720 		FAIL("Failed to delete periodic advertising sync (err %d)\n",
721 			 err);
722 		return;
723 	}
724 	printk("success.\n");
725 
726 	PASS("PAST recv test Passed\n");
727 }
728 
test_past_init(void)729 static void test_past_init(void)
730 {
731 	bst_ticker_set_next_tick_absolute(60e6);
732 	bst_result = In_progress;
733 }
734 
test_past_tick(bs_time_t HW_device_time)735 static void test_past_tick(bs_time_t HW_device_time)
736 {
737 	if (bst_result != Passed) {
738 		FAIL("test failed (not passed after seconds)\n");
739 	}
740 }
741 
742 static const struct bst_test_instance test_def[] = {
743 	{
744 		.test_id = "broadcast_pa",
745 		.test_descr = "Periodic Advertisement broadcaster",
746 		.test_pre_init_f = test_past_init,
747 		.test_tick_f = test_past_tick,
748 		.test_main_f = test_broadcast_main
749 	},
750 	{
751 		.test_id = "receive_past",
752 		.test_descr = "Peripheral device, waiting for connection "
753 		"and then waits for receiving PAST, then syncs to PA",
754 		.test_pre_init_f = test_past_init,
755 		.test_tick_f = test_past_tick,
756 		.test_main_f = test_past_recv_main
757 	},
758 	{
759 		.test_id = "receive_past_default_param",
760 		.test_descr = "Peripheral device, waiting for connection "
761 		"and then waits for receiving PAST with the default PAST parameter set,"
762 		" then syncs to PA",
763 		.test_pre_init_f = test_past_init,
764 		.test_tick_f = test_past_tick,
765 		.test_main_f = test_past_recv_main_default_param
766 	},
767 	{
768 		.test_id = "send_past",
769 		.test_descr = "Central that syncs to PA from broadcaster,"
770 		"connects to peripheral and sends PAST",
771 		.test_pre_init_f = test_past_init,
772 		.test_tick_f = test_past_tick,
773 		.test_main_f = test_past_send_main
774 	},
775 	{
776 		.test_id = "broadcast_past_sender",
777 		.test_descr = "PA broadcaster, connects and sends PAST to peripheral",
778 		.test_pre_init_f = test_past_init,
779 		.test_tick_f = test_past_tick,
780 		.test_main_f = test_broadcast_past_sender_main
781 	},
782 	BSTEST_END_MARKER
783 };
784 
test_past_install(struct bst_test_list * tests)785 struct bst_test_list *test_past_install(struct bst_test_list *tests)
786 {
787 	return bst_add_tests(tests, test_def);
788 }
789