diff --git a/subsys/bluetooth/audio/CMakeLists.txt b/subsys/bluetooth/audio/CMakeLists.txt index 742cb8455b7..fd52d523d5a 100644 --- a/subsys/bluetooth/audio/CMakeLists.txt +++ b/subsys/bluetooth/audio/CMakeLists.txt @@ -50,3 +50,4 @@ zephyr_library_sources_ifdef(CONFIG_BT_AUDIO_BROADCAST_SOURCE broadcast_source.c zephyr_library_sources_ifdef(CONFIG_BT_AUDIO_BROADCAST_SINK broadcast_sink.c) zephyr_library_sources_ifdef(CONFIG_BT_BASS bass.c) zephyr_library_sources_ifdef(CONFIG_BT_BASS_CLIENT bass_client.c) +zephyr_library_sources_ifdef(CONFIG_BT_HAS has.c) diff --git a/subsys/bluetooth/audio/Kconfig b/subsys/bluetooth/audio/Kconfig index 4f27c2609e9..6c486dd6d36 100644 --- a/subsys/bluetooth/audio/Kconfig +++ b/subsys/bluetooth/audio/Kconfig @@ -33,5 +33,6 @@ rsource "Kconfig.mics" rsource "Kconfig.csis" rsource "Kconfig.mcs" rsource "Kconfig.bass" +rsource "Kconfig.has" endif # BT_AUDIO diff --git a/subsys/bluetooth/audio/Kconfig.has b/subsys/bluetooth/audio/Kconfig.has new file mode 100644 index 00000000000..ce9bbb34522 --- /dev/null +++ b/subsys/bluetooth/audio/Kconfig.has @@ -0,0 +1,63 @@ +# Bluetooth Audio - Hearing Access Service options +# +# Copyright (c) 2022 Codecoup +# +# SPDX-License-Identifier: Apache-2.0 + +config BT_HAS + bool "Hearing Access Service support [EXPERIMENTAL]" + select EXPERIMENTAL + help + This option enables support for Hearing Access Service. + +if BT_HAS + +choice BT_HAS_HEARING_AID_TYPE_CHOICE + prompt "Hearing Aid Type selection" + help + Select the Hearing Aid Type to compile. + +config BT_HAS_HEARING_AID_MONAURAL + bool "Monaural Hearing Aid" + +config BT_HAS_HEARING_AID_BINAURAL + depends on BT_CSIS + bool "Binaural Hearing Aid" + +config BT_HAS_HEARING_AID_BANDED + bool "Banded Hearing Aid" + +endchoice # BT_HAS_HEARING_AID_TYPE_CHOICE + +if BT_HAS_HEARING_AID_MONAURAL || BT_HAS_HEARING_AID_BINAURAL + +choice BT_HAS_HEARING_AID_LOCATION + prompt "Hearing Aid Device Location" + help + Select the Hearing Aid Device location. + +config BT_HAS_HEARING_AID_LEFT + bool "Left Ear" + +config BT_HAS_HEARING_AID_RIGHT + bool "Right Ear" + +endchoice # BT_HAS_HEARING_AID_LOCATION + +endif # BT_HAS_HEARING_AID_MONAURAL || BT_HAS_HEARING_AID_BINAURAL + +config BT_HAS_HEARING_AID_TYPE + int # hidden + range 0 2 + default 0 if BT_HAS_HEARING_AID_BINAURAL + default 1 if BT_HAS_HEARING_AID_MONAURAL + default 2 if BT_HAS_HEARING_AID_BANDED + help + The value shall be one of 3 defined by the HAS 1.0 specification table 3.2 + +config BT_DEBUG_HAS + bool "Hearing Access Service debug" + help + This option enables enables Hearing Access Service debug logs. + +endif # BT_HAS diff --git a/subsys/bluetooth/audio/has.c b/subsys/bluetooth/audio/has.c new file mode 100644 index 00000000000..4d3b7f502a9 --- /dev/null +++ b/subsys/bluetooth/audio/has.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include + +#include +#include +#include +#include + +#include "has_internal.h" + +#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HAS) +#define LOG_MODULE_NAME bt_has +#include "common/log.h" + +static struct bt_has has; + +static ssize_t read_features(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, + uint16_t len, uint16_t offset) +{ + BT_DBG("conn %p attr %p offset %d", conn, attr, offset); + + if (offset > sizeof(has.features)) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); + } + + return bt_gatt_attr_read(conn, attr, buf, len, offset, &has.features, + sizeof(has.features)); +} + +/* Hearing Access Service GATT Attributes */ +BT_GATT_SERVICE_DEFINE(has_svc, + BT_GATT_PRIMARY_SERVICE(BT_UUID_HAS), + BT_GATT_CHARACTERISTIC(BT_UUID_HAS_HEARING_AID_FEATURES, BT_GATT_CHRC_READ, + BT_GATT_PERM_READ_ENCRYPT, read_features, NULL, NULL), +); + +static int has_init(const struct device *dev) +{ + ARG_UNUSED(dev); + + /* Initialize the supported features characteristic value */ + has.features = CONFIG_BT_HAS_HEARING_AID_TYPE & BT_HAS_FEAT_HEARING_AID_TYPE_MASK; + + if (IS_ENABLED(CONFIG_BT_HAS_HEARING_AID_BANDED)) { + /* HAP_d1.0r00; 3.7 BAP Unicast Server role requirements + * A Banded Hearing Aid in the HA role shall set the Front Left and the Front + * Right bits to a value of 0b1 in the Sink Audio Locations characteristic value. + */ + bt_audio_capability_set_location(BT_AUDIO_SINK, BT_AUDIO_LOCATION_FRONT_LEFT | + BT_AUDIO_LOCATION_FRONT_RIGHT); + } else if (IS_ENABLED(CONFIG_BT_HAS_HEARING_AID_LEFT)) { + bt_audio_capability_set_location(BT_AUDIO_SINK, BT_AUDIO_LOCATION_FRONT_LEFT); + } else { + bt_audio_capability_set_location(BT_AUDIO_SINK, BT_AUDIO_LOCATION_FRONT_RIGHT); + } + + return 0; +} + +SYS_INIT(has_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/subsys/bluetooth/audio/has_internal.h b/subsys/bluetooth/audio/has_internal.h new file mode 100644 index 00000000000..078fe4b7983 --- /dev/null +++ b/subsys/bluetooth/audio/has_internal.h @@ -0,0 +1,16 @@ +/** @file + * @brief Internal APIs for Bluetooth Hearing Access Profile. + */ + +/* + * Copyright (c) 2022 Codecoup + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define BT_HAS_FEAT_HEARING_AID_TYPE_MASK 0x03 + +struct bt_has { + /** Hearing Aid Features value */ + uint8_t features; +};