1 /** @file
2  *  @brief Bluetooth Hearing Access Service (HAS) Server role.
3  *
4  *  Copyright (c) 2022 Codecoup
5  *
6  *  SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/printk.h>
11 #include <zephyr/sys/util.h>
12 
13 #include <zephyr/bluetooth/audio/has.h>
14 
15 #define PRESET_INDEX_UNIVERSAL  1
16 #define PRESET_INDEX_OUTDOOR    5
17 #define PRESET_INDEX_NOISY_ENV  8
18 #define PRESET_INDEX_OFFICE     22
19 
select_cb(uint8_t index,bool sync)20 static int select_cb(uint8_t index, bool sync)
21 {
22 	printk("%s %u sync %d", __func__, index, sync);
23 
24 	return 0;
25 }
26 
name_changed_cb(uint8_t index,const char * name)27 static void name_changed_cb(uint8_t index, const char *name)
28 {
29 	printk("%s %u name %s", __func__, index, name);
30 }
31 
32 static const struct bt_has_preset_ops ops = {
33 	.select = select_cb,
34 	.name_changed = name_changed_cb,
35 };
36 
has_server_preset_init(void)37 int has_server_preset_init(void)
38 {
39 	int err;
40 
41 	struct bt_has_preset_register_param param[] = {
42 		{
43 			.index = PRESET_INDEX_UNIVERSAL,
44 			.properties = BT_HAS_PROP_AVAILABLE | BT_HAS_PROP_WRITABLE,
45 			.name = "Universal",
46 			.ops = &ops,
47 		},
48 		{
49 			.index = PRESET_INDEX_OUTDOOR,
50 			.properties = BT_HAS_PROP_AVAILABLE | BT_HAS_PROP_WRITABLE,
51 			.name = "Outdoor",
52 			.ops = &ops,
53 		},
54 		{
55 			.index = PRESET_INDEX_NOISY_ENV,
56 			.properties = BT_HAS_PROP_AVAILABLE | BT_HAS_PROP_WRITABLE,
57 			.name = "Noisy environment",
58 			.ops = &ops,
59 		},
60 		{
61 			.index = PRESET_INDEX_OFFICE,
62 			.properties = BT_HAS_PROP_AVAILABLE | BT_HAS_PROP_WRITABLE,
63 			.name = "Office",
64 			.ops = &ops,
65 		},
66 	};
67 
68 	for (size_t i = 0; i < ARRAY_SIZE(param); i++) {
69 		err = bt_has_preset_register(&param[i]);
70 		if (err != 0) {
71 			return err;
72 		}
73 	}
74 
75 	return 0;
76 }
77 
78 static struct bt_has_features_param features = {
79 	.type = BT_HAS_HEARING_AID_TYPE_MONAURAL,
80 	.preset_sync_support = false,
81 	.independent_presets = false
82 };
83 
has_server_init(void)84 int has_server_init(void)
85 {
86 	int err;
87 
88 	if (IS_ENABLED(CONFIG_HAP_HA_HEARING_AID_BINAURAL)) {
89 		features.type = BT_HAS_HEARING_AID_TYPE_BINAURAL;
90 	} else if (IS_ENABLED(CONFIG_HAP_HA_HEARING_AID_BANDED)) {
91 		features.type = BT_HAS_HEARING_AID_TYPE_BANDED;
92 	}
93 
94 	err = bt_has_register(&features);
95 	if (err) {
96 		return err;
97 	}
98 
99 	if (IS_ENABLED(CONFIG_BT_HAS_PRESET_SUPPORT)) {
100 		return has_server_preset_init();
101 	}
102 
103 	return 0;
104 }
105