1 /*
2  * Copyright 2023 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stddef.h>
7 #include <stdint.h>
8 
9 #include <zephyr/autoconf.h>
10 #include <zephyr/bluetooth/audio/tmap.h>
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/gap.h>
13 #include <zephyr/bluetooth/hci.h>
14 #include <zephyr/bluetooth/conn.h>
15 #include <zephyr/bluetooth/uuid.h>
16 #include <zephyr/bluetooth/gatt.h>
17 #include <zephyr/sys/printk.h>
18 #include <zephyr/sys/util.h>
19 #include <zephyr/types.h>
20 
21 #include "bstests.h"
22 #include "common.h"
23 
24 #ifdef CONFIG_BT_TMAP
25 extern enum bst_result_t bst_result;
26 
27 static uint8_t tmap_addata[] = {
28 	BT_UUID_16_ENCODE(BT_UUID_TMAS_VAL), /* TMAS UUID */
29 	(BT_TMAP_ROLE_UMR | BT_TMAP_ROLE_CT), 0x00, /* TMAP Role  */
30 };
31 
32 static const struct bt_data ad_tmas[] = {
33 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
34 	BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE, 0x09, 0x41), /* Appearance - Earbud  */
35 	BT_DATA(BT_DATA_SVC_DATA16, tmap_addata, ARRAY_SIZE(tmap_addata)),
36 };
37 
test_main(void)38 static void test_main(void)
39 {
40 	int err;
41 	struct bt_le_ext_adv *adv;
42 
43 	err = bt_enable(NULL);
44 	if (err != 0) {
45 		FAIL("Bluetooth init failed (err %d)\n", err);
46 		return;
47 	}
48 
49 	printk("Bluetooth initialized\n");
50 	/* Initialize TMAP */
51 	err = bt_tmap_register(BT_TMAP_ROLE_CT | BT_TMAP_ROLE_UMR);
52 	if (err != 0) {
53 		return;
54 	}
55 	printk("TMAP initialized. Start advertising...\n");
56 	/* Create a connectable extended advertising set */
57 	err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CONN, NULL, &adv);
58 	if (err) {
59 		printk("Failed to create advertising set (err %d)\n", err);
60 		return;
61 	}
62 
63 	err = bt_le_ext_adv_set_data(adv, ad_tmas, ARRAY_SIZE(ad_tmas), NULL, 0);
64 	if (err) {
65 		printk("Failed to set advertising data (err %d)\n", err);
66 		return;
67 	}
68 
69 	err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
70 	if (err) {
71 		printk("Failed to start advertising set (err %d)\n", err);
72 		return;
73 	}
74 
75 	printk("Advertising successfully started\n");
76 	WAIT_FOR_FLAG(flag_connected);
77 	printk("Connected!\n");
78 
79 	PASS("TMAP test passed\n");
80 }
81 
82 static const struct bst_test_instance test_tmas[] = {
83 	{
84 		.test_id = "tmap_server",
85 		.test_pre_init_f = test_init,
86 		.test_tick_f = test_tick,
87 		.test_main_f = test_main,
88 
89 	},
90 	BSTEST_END_MARKER
91 };
92 
test_tmap_server_install(struct bst_test_list * tests)93 struct bst_test_list *test_tmap_server_install(struct bst_test_list *tests)
94 {
95 	return bst_add_tests(tests, test_tmas);
96 }
97 #else
test_tmap_server_install(struct bst_test_list * tests)98 struct bst_test_list *test_tmap_server_install(struct bst_test_list *tests)
99 {
100 	return tests;
101 }
102 #endif /* CONFIG_BT_TMAP */
103