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/bluetooth.h>
11 #include <zephyr/bluetooth/byteorder.h>
12 #include <zephyr/bluetooth/gap.h>
13 #include <zephyr/bluetooth/uuid.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/sys/printk.h>
16 #include <zephyr/sys/util.h>
17 
18 #include "bstests.h"
19 #include "common.h"
20 
21 extern enum bst_result_t bst_result;
22 
23 /* TODO: Deprecate in favor of broadcast_source_test */
24 
test_main(void)25 static void test_main(void)
26 {
27 	int err;
28 	uint32_t broadcast_id = 1234;
29 	struct bt_le_ext_adv *adv;
30 	struct bt_data ad[2] = {
31 		BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
32 		BT_DATA_BYTES(BT_DATA_SVC_DATA16,
33 			      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 	/* Create a non-connectable advertising set */
46 	err = bt_le_ext_adv_create(BT_LE_EXT_ADV_NCONN, NULL, &adv);
47 	if (err) {
48 		FAIL("Failed to create advertising set (err %d)\n", err);
49 		return;
50 	}
51 
52 	/* Set periodic advertising parameters */
53 	err = bt_le_per_adv_set_param(adv, BT_LE_PER_ADV_DEFAULT);
54 	if (err) {
55 		FAIL("Failed to set periodic advertising parameters (err %d)\n", err);
56 		return;
57 	}
58 
59 	/* Set adv data */
60 	err = bt_le_ext_adv_set_data(adv, ad, ARRAY_SIZE(ad), NULL, 0);
61 	if (err) {
62 		FAIL("Failed to set advertising data (err %d)\n", err);
63 		return;
64 	}
65 
66 	/* Enable Periodic Advertising */
67 	err = bt_le_per_adv_start(adv);
68 	if (err) {
69 		FAIL("Failed to enable periodic advertising (err %d)\n", err);
70 		return;
71 	}
72 
73 	/* Start extended advertising */
74 	err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
75 	if (err) {
76 		FAIL("Failed to start extended advertising (err %d)\n", err);
77 		return;
78 	}
79 
80 	printk("Advertising successfully started\n");
81 
82 	k_sleep(K_SECONDS(10));
83 
84 	PASS("BASS broadcaster passed\n");
85 }
86 
87 static const struct bst_test_instance test_bass_broadcaster[] = {
88 	{
89 		.test_id = "bass_broadcaster",
90 		.test_pre_init_f = test_init,
91 		.test_tick_f = test_tick,
92 		.test_main_f = test_main
93 	},
94 	BSTEST_END_MARKER
95 };
96 
test_bass_broadcaster_install(struct bst_test_list * tests)97 struct bst_test_list *test_bass_broadcaster_install(struct bst_test_list *tests)
98 {
99 	return bst_add_tests(tests, test_bass_broadcaster);
100 }
101