1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <zephyr/bluetooth/audio/bap.h>
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/byteorder.h>
13 #include <zephyr/bluetooth/gap.h>
14 #include <zephyr/bluetooth/uuid.h>
15 #include <zephyr/kernel.h>
16 #include <zephyr/sys/printk.h>
17 #include <zephyr/sys/util.h>
18 
19 #include "bstests.h"
20 #include "common.h"
21 
22 extern enum bst_result_t bst_result;
23 
24 /* TODO: Deprecate in favor of broadcast_source_test */
25 
test_main(void)26 static void test_main(void)
27 {
28 	int err;
29 	uint32_t broadcast_id = 1234;
30 	struct bt_le_ext_adv *adv;
31 	struct bt_data ad[2] = {
32 		BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
33 		BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_BROADCAST_AUDIO_VAL),
34 			      BT_BYTES_LIST_LE24(broadcast_id)),
35 	};
36 
37 	err = bt_enable(NULL);
38 	if (err) {
39 		FAIL("Bluetooth init failed (err %d)\n", err);
40 		return;
41 	}
42 
43 	printk("Bluetooth initialized\n");
44 
45 	setup_broadcast_adv(&adv);
46 
47 	/* Set adv data */
48 	err = bt_le_ext_adv_set_data(adv, ad, ARRAY_SIZE(ad), NULL, 0);
49 	if (err) {
50 		FAIL("Failed to set advertising data (err %d)\n", err);
51 		return;
52 	}
53 
54 	/* Enable Periodic Advertising */
55 	err = bt_le_per_adv_start(adv);
56 	if (err) {
57 		FAIL("Failed to enable periodic advertising (err %d)\n", err);
58 		return;
59 	}
60 
61 	/* Start extended advertising */
62 	err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
63 	if (err) {
64 		FAIL("Failed to start extended advertising (err %d)\n", err);
65 		return;
66 	}
67 
68 	printk("Advertising successfully started\n");
69 
70 	k_sleep(K_SECONDS(10));
71 
72 	PASS("BASS broadcaster passed\n");
73 }
74 
75 static const struct bst_test_instance test_bass_broadcaster[] = {
76 	{
77 		.test_id = "bass_broadcaster",
78 		.test_pre_init_f = test_init,
79 		.test_tick_f = test_tick,
80 		.test_main_f = test_main
81 	},
82 	BSTEST_END_MARKER
83 };
84 
test_bass_broadcaster_install(struct bst_test_list * tests)85 struct bst_test_list *test_bass_broadcaster_install(struct bst_test_list *tests)
86 {
87 	return bst_add_tests(tests, test_bass_broadcaster);
88 }
89