diff --git a/include/bluetooth/direction.h b/include/bluetooth/direction.h index 59a3a906faf..a10d2b82506 100644 --- a/include/bluetooth/direction.h +++ b/include/bluetooth/direction.h @@ -142,6 +142,7 @@ struct bt_df_conn_iq_samples_report { /** Pinter to IQ samples data. */ struct bt_hci_le_iq_sample const *sample; }; + /** * @brief Set or update the Constant Tone Extension parameters for periodic advertising set. * @@ -217,4 +218,26 @@ int bt_df_conn_cte_rx_enable(struct bt_conn *conn, const struct bt_df_conn_cte_r */ int bt_df_conn_cte_rx_disable(struct bt_conn *conn); +/** + * @brief Enable Constant Tone Extension response procedure for a connection. + * + * The function is available if @kconfig{CONFIG_BT_DF_CONNECTION_CTE_RSP} is enabled. + * + * @param conn Connection object. + * + * @return Zero in case of success, other value in case of failure. + */ +int bt_df_conn_cte_rsp_enable(struct bt_conn *conn); + +/** + * @brief Disable Constant Tone Extension response procedure for a connection. + * + * The function is available if @kconfig{CONFIG_BT_DF_CONNECTION_CTE_RSP} is enabled. + * + * @param conn Connection object. + * + * @return Zero in case of success, other value in case of failure. + */ +int bt_df_conn_cte_rsp_disable(struct bt_conn *conn); + #endif /* ZEPHYR_INCLUDE_BLUETOOTH_DF_H_ */ diff --git a/subsys/bluetooth/host/Kconfig b/subsys/bluetooth/host/Kconfig index 8d5fd892d20..70f510de9f4 100644 --- a/subsys/bluetooth/host/Kconfig +++ b/subsys/bluetooth/host/Kconfig @@ -546,26 +546,39 @@ config BT_DF It will allow to: get information about antennae, configure Constant Tone Extension, transmit CTE and sample incoming CTE. +if BT_DF + config BT_DF_CONNECTIONLESS_CTE_RX bool "Enable support for receive of CTE in connectionless mode" - depends on BT_DF help Enable support for reception and sampling of Constant Tone Extension in connectionless mode. config BT_DF_CONNECTION_CTE_RX bool "Enable support for receive of CTE in connection mode" - depends on BT_DF help Enable support for reception and sampling of Constant Tone Extension in connection mode. +config BT_DF_CONNECTION_CTE_TX + bool "Enable support for transmission of CTE in connection mode" + help + Enable support for transmission of Constant Tone Extension in + connection mode. + +config BT_DF_CONNECTION_CTE_RSP + bool "Enable support for CTE request procedure in connection mode" + depends on BT_DF_CONNECTION_CTE_TX + help + Enable support for request of Constant Tone Extension in connection + mode. + config BT_DEBUG_DF bool "Bluetooth Direction Finding debug" - depends on BT_DF help This option enables debug support for Bluetooth Direction Finding +endif # BT_DF endif # BT_HCI_HOST config BT_ECC diff --git a/subsys/bluetooth/host/conn_internal.h b/subsys/bluetooth/host/conn_internal.h index 1a0271ed958..5e590ada62d 100644 --- a/subsys/bluetooth/host/conn_internal.h +++ b/subsys/bluetooth/host/conn_internal.h @@ -44,6 +44,8 @@ enum { BT_CONN_AUTO_DATA_LEN_COMPLETE, BT_CONN_CTE_RX_ENABLED, /* CTE receive and sampling is enabled */ + BT_CONN_CTE_TX_PARAMS_SET, /* CTE transmission parameters are set */ + BT_CONN_CTE_RSP_ENABLED, /* CTE response procedure is enabled */ /* Total number of flags - must be at the end of the enum */ BT_CONN_NUM_FLAGS, diff --git a/subsys/bluetooth/host/direction.c b/subsys/bluetooth/host/direction.c index acfcae57f57..39d930c930d 100644 --- a/subsys/bluetooth/host/direction.c +++ b/subsys/bluetooth/host/direction.c @@ -593,6 +593,52 @@ int hci_df_prepare_connection_iq_report(struct net_buf *buf, } #endif /* CONFIG_BT_DF_CONNECTION_CTE_RX */ +#if defined(CONFIG_BT_DF_CONNECTION_CTE_RSP) +static void prepare_conn_cte_rsp_enable_cmd_params(struct net_buf *buf, const struct bt_conn *conn, + bool enable) +{ + struct bt_hci_cp_le_conn_cte_rsp_enable *cp; + + cp = net_buf_add(buf, sizeof(*cp)); + (void)memset(cp, 0, sizeof(*cp)); + + cp->handle = sys_cpu_to_le16(conn->handle); + cp->enable = enable ? 1U : 0U; +} + +static int hci_df_set_conn_cte_rsp_enable(struct bt_conn *conn, bool enable) +{ + struct bt_hci_rp_le_conn_cte_rsp_enable *rp; + struct bt_hci_cmd_state_set state; + struct net_buf *buf, *rsp; + int err; + + buf = bt_hci_cmd_create(BT_HCI_OP_LE_CONN_CTE_RSP_ENABLE, + sizeof(struct bt_hci_cp_le_conn_cte_rsp_enable)); + if (!buf) { + return -ENOBUFS; + } + + prepare_conn_cte_rsp_enable_cmd_params(buf, conn, enable); + + bt_hci_cmd_state_set_init(buf, &state, conn->flags, BT_CONN_CTE_RSP_ENABLED, enable); + + err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CONN_CTE_RSP_ENABLE, buf, &rsp); + if (err) { + return err; + } + + rp = (void *)rsp->data; + if (conn->handle != sys_le16_to_cpu(rp->handle)) { + err = -EIO; + } + + net_buf_unref(rsp); + + return err; +} +#endif /* CONFIG_BT_DF_CONNECTION_CTE_RSP */ + /* @brief Function initializes Direction Finding in Host * * @return Zero in case of success, other value in case of failure. @@ -767,3 +813,39 @@ int bt_df_conn_cte_rx_disable(struct bt_conn *conn) return bt_df_set_conn_cte_rx_enable(conn, false, NULL); } #endif /* CONFIG_BT_DF_CONNECTION_CTE_RX */ + +#if defined(CONFIG_BT_DF_CONNECTION_CTE_RSP) +static int bt_df_set_conn_cte_rsp_enable(struct bt_conn *conn, bool enable) +{ + CHECKIF(!conn) { + return -EINVAL; + } + + if (!BT_FEAT_LE_CONNECTION_CTE_RESP(bt_dev.le.features)) { + BT_WARN("CTE response procedure is not supported"); + return -ENOTSUP; + } + + if (conn->state != BT_CONN_CONNECTED) { + BT_ERR("not connected"); + return -ENOTCONN; + } + + if (!atomic_test_bit(conn->flags, BT_CONN_CTE_TX_PARAMS_SET)) { + BT_ERR("Can't start CTE response procedure before CTE TX params setup"); + return -EINVAL; + } + + return hci_df_set_conn_cte_rsp_enable(conn, enable); +} + +int bt_df_conn_cte_rsp_enable(struct bt_conn *conn) +{ + return bt_df_set_conn_cte_rsp_enable(conn, true); +} + +int bt_df_conn_cte_rsp_disable(struct bt_conn *conn) +{ + return bt_df_set_conn_cte_rsp_enable(conn, false); +} +#endif /* CONFIG_BT_DF_CONNECTION_CTE_RSP */