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