diff --git a/include/zephyr/bluetooth/cs.h b/include/zephyr/bluetooth/cs.h new file mode 100644 index 000000000000000..c777ddce1559bab --- /dev/null +++ b/include/zephyr/bluetooth/cs.h @@ -0,0 +1,86 @@ +/** @file + * @brief Bluetooth Channel Sounding handling + */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef ZEPHYR_INCLUDE_BLUETOOTH_CS_H_ +#define ZEPHYR_INCLUDE_BLUETOOTH_CS_H_ + +/** + * @brief Channel Sounding (CS) + * @defgroup bt_cs Channel Sounding (CS) + * @ingroup bluetooth + * @{ + */ + +#include +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +enum bt_cs_sync_antenna_selection_opt { + /** Use antenna identifier 1 for CS_SYNC packets. */ + BT_CS_ANTENNA_SELECTION_OPT_ONE = BT_HCI_OP_LE_CS_ANTENNA_SEL_ONE, + /** Use antenna identifier 2 for CS_SYNC packets. */ + BT_CS_ANTENNA_SELECTION_OPT_TWO = BT_HCI_OP_LE_CS_ANTENNA_SEL_TWO, + /** Use antenna identifier 3 for CS_SYNC packets. */ + BT_CS_ANTENNA_SELECTION_OPT_THREE = BT_HCI_OP_LE_CS_ANTENNA_SEL_THREE, + /** Use antenna identifier 4 for CS_SYNC packets. */ + BT_CS_ANTENNA_SELECTION_OPT_FOUR = BT_HCI_OP_LE_CS_ANTENNA_SEL_FOUR, + /** Use antennas in repetitive order from 1 to 4 for CS_SYNC packets. */ + BT_CS_ANTENNA_SELECTION_OPT_REPETITIVE = BT_HCI_OP_LE_CS_ANTENNA_SEL_REP, + /** No recommendation for local controller antenna selection. */ + BT_CS_ANTENNA_SELECTION_OPT_NO_RECOMMENDATION = BT_HCI_OP_LE_CS_ANTENNA_SEL_NONE, +}; + +/** Connection subrating parameters for LE connections */ +struct bt_cs_set_default_settings_param { + /** Enable CS initiator role. */ + bool enable_initiator_role; + /** Enable CS reflector role. */ + bool enable_reflector_role; + /** Antenna identifier to be used for CS_SYNC packets by the local controller. + */ + enum bt_cs_sync_antenna_selection_opt cs_sync_antenna_selection; + /** Maximum output power (Effective Isotropic Radiated Power) to be used + * for all CS transmissions. + * + * Value range is @ref BT_HCI_OP_LE_CS_MIN_MAX_TX_POWER to + * @ref BT_HCI_OP_LE_CS_MAX_MAX_TX_POWER. + */ + int8_t max_tx_power; +}; + +/** @brief Set Channel Sounding default settings. + * + * This command is used to set default Channel Sounding settings for this + * connection. + * + * @note To use this API @kconfig{CONFIG_BT_CHANNEL_SOUNDING} must be set. + * + * @param conn Connection Object. + * @param params Channel sounding default settings parameters. + * + * @return Zero on success or (negative) error code on failure. + */ +int bt_cs_set_default_settings(struct bt_conn *conn, + const struct bt_cs_set_default_settings_param *params); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* ZEPHYR_INCLUDE_BLUETOOTH_CS_H_ */ diff --git a/include/zephyr/bluetooth/hci_types.h b/include/zephyr/bluetooth/hci_types.h index 83bd3a55d4c059b..afdb03f0ce9bfa2 100644 --- a/include/zephyr/bluetooth/hci_types.h +++ b/include/zephyr/bluetooth/hci_types.h @@ -2395,6 +2395,28 @@ struct bt_hci_cp_le_tx_test_v4_tx_power { int8_t tx_power; } __packed; +#define BT_HCI_OP_LE_CS_SET_DEFAULT_SETTINGS BT_OP(BT_OGF_LE, 0x008D) /* 0x208D */ + +#define BT_HCI_OP_LE_CS_INITIATOR_ROLE_MASK BIT(0) +#define BT_HCI_OP_LE_CS_REFLECTOR_ROLE_MASK BIT(1) + +#define BT_HCI_OP_LE_CS_MIN_MAX_TX_POWER -127 +#define BT_HCI_OP_LE_CS_MAX_MAX_TX_POWER 20 + +#define BT_HCI_OP_LE_CS_ANTENNA_SEL_ONE 0x01 +#define BT_HCI_OP_LE_CS_ANTENNA_SEL_TWO 0x02 +#define BT_HCI_OP_LE_CS_ANTENNA_SEL_THREE 0x03 +#define BT_HCI_OP_LE_CS_ANTENNA_SEL_FOUR 0x04 +#define BT_HCI_OP_LE_CS_ANTENNA_SEL_REP 0xFE +#define BT_HCI_OP_LE_CS_ANTENNA_SEL_NONE 0xFF + +struct bt_hci_cp_le_cs_set_default_settings { + uint16_t handle; + uint8_t role_enable; + uint8_t cs_sync_antenna_selection; + int8_t max_tx_power; +} __packed; + /* Event definitions */ #define BT_HCI_EVT_UNKNOWN 0x00 diff --git a/subsys/bluetooth/host/CMakeLists.txt b/subsys/bluetooth/host/CMakeLists.txt index 1b1b6a58543698a..83912a1ed7954d0 100644 --- a/subsys/bluetooth/host/CMakeLists.txt +++ b/subsys/bluetooth/host/CMakeLists.txt @@ -48,6 +48,7 @@ if(CONFIG_BT_HCI_HOST) l2cap.c att.c gatt.c + cs.c ) if(CONFIG_BT_SMP) diff --git a/subsys/bluetooth/host/cs.c b/subsys/bluetooth/host/cs.c new file mode 100644 index 000000000000000..b6ad385e9de6938 --- /dev/null +++ b/subsys/bluetooth/host/cs.c @@ -0,0 +1,49 @@ +/* cs.c - Bluetooth Channel Sounding handling */ + +/* + * Copyright (c) 2015-2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#include "conn_internal.h" + +#if defined(CONFIG_BT_CHANNEL_SOUNDING) +int bt_cs_set_default_settings(struct bt_conn *conn, + const struct bt_cs_set_default_settings_param *params) +{ + struct bt_hci_cp_le_cs_set_default_settings *cp; + struct net_buf *buf; + + if (params->max_tx_power > BT_HCI_OP_LE_CS_MAX_MAX_TX_POWER || + params->max_tx_power < BT_HCI_OP_LE_CS_MIN_MAX_TX_POWER) { + return EINVAL; + } + + buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_SET_DEFAULT_SETTINGS, sizeof(*cp)); + if (!buf) { + return -ENOBUFS; + } + + cp = net_buf_add(buf, sizeof(*cp)); + cp->handle = sys_cpu_to_le16(conn->handle); + cp->max_tx_power = params->max_tx_power; + cp->cs_sync_antenna_selection = params->cs_sync_antenna_selection; + cp->role_enable = 0; + + if (params->enable_initiator_role) { + cp->role_enable &= BT_HCI_OP_LE_CS_INITIATOR_ROLE_MASK; + } + + if (params->enable_reflector_role) { + cp->role_enable &= BT_HCI_OP_LE_CS_INITIATOR_ROLE_MASK; + } + + return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_SET_DEFAULT_SETTINGS, buf, NULL); +} +#endif /* CONFIG_BT_CHANNEL_SOUNDING */