From 472febfe6963442463a1e3db9c260aee9217a115 Mon Sep 17 00:00:00 2001 From: Piotr Pryga Date: Sun, 10 Jan 2021 22:55:38 -0800 Subject: [PATCH] Bluetooth: host: direction: Add public API to set CTE TX enable for adv Add public function to set Constant Tone Extension transmission enabled or disabled for periodic advertising. Signed-off-by: Piotr Pryga --- include/bluetooth/direction.h | 22 ++++++++++ subsys/bluetooth/host/direction.c | 69 +++++++++++++++++++++++++++++++ subsys/bluetooth/host/hci_core.h | 8 ++++ 3 files changed, 99 insertions(+) diff --git a/include/bluetooth/direction.h b/include/bluetooth/direction.h index cf1e0cbaf0f..87075e5d55a 100644 --- a/include/bluetooth/direction.h +++ b/include/bluetooth/direction.h @@ -37,4 +37,26 @@ struct bt_df_adv_cte_tx_param { int bt_df_set_adv_cte_tx_param(struct bt_le_ext_adv *adv, const struct bt_df_adv_cte_tx_param *params); +/** @brief Enable transmission of Constant Tone Extension for the given + * advertising set. + * + * Transmission of Constant Tone Extension may be enabled only after setting + * periodic advertising parameters (@ref bt_le_per_adv_set_param) and Constant + * Tone Extension parameters (@ref bt_df_set_adv_cte_tx_param). + * + * @param[in] adv Advertising set object. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_df_adv_cte_tx_enable(struct bt_le_ext_adv *adv); + +/** @brief Disable transmission of Constant Tone Extension for the given + * advertising set. + * + * @param[in] adv Advertising set object. + * + * @return Zero on success or (negative) error code otherwise. + */ +int bt_df_adv_cte_tx_disable(struct bt_le_ext_adv *adv); + #endif /* ZEPHYR_INCLUDE_BLUETOOTH_DF_H_ */ diff --git a/subsys/bluetooth/host/direction.c b/subsys/bluetooth/host/direction.c index de3048ba3e1..418811661e4 100644 --- a/subsys/bluetooth/host/direction.c +++ b/subsys/bluetooth/host/direction.c @@ -156,6 +156,40 @@ static int hci_df_read_ant_info(uint8_t *switch_sample_rates, return 0; } +/* @brief Function handles send of HCI commnad to enable or disables CTE + * transmission for given advertising set. + * + * @param[in] adv Pointer to advertising set + * @param[in] enable Enable or disable CTE TX + * + * @return Zero in case of success, other value in case of failure. + */ +static int hci_df_set_adv_cte_tx_enable(struct bt_le_ext_adv *adv, + bool enable) +{ + struct bt_hci_cp_le_set_cl_cte_tx_enable *cp; + struct bt_hci_cmd_state_set state; + struct net_buf *buf; + + buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_CL_CTE_TX_ENABLE, sizeof(*cp)); + if (!buf) { + return -ENOBUFS; + } + + cp = net_buf_add(buf, sizeof(*cp)); + (void)memset(cp, 0, sizeof(*cp)); + + cp->handle = adv->handle; + cp->cte_enable = enable ? 1 : 0; + + bt_hci_cmd_state_set_init(&state, adv->flags, BT_PER_ADV_CTE_ENABLED, + enable); + bt_hci_cmd_data_state_set(buf, &state); + + return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_CL_CTE_TX_ENABLE, + buf, NULL); +} + /* @brief Function sets CTE parameters for connection object * * @param[in] cte_types Allowed response CTE types @@ -271,10 +305,45 @@ int bt_df_set_adv_cte_tx_param(struct bt_le_ext_adv *adv, return -EINVAL; } + if (atomic_test_bit(adv->flags, BT_PER_ADV_CTE_ENABLED)) { + return -EINVAL; + } + err = hci_df_set_cl_cte_tx_params(adv, params); if (err) { return err; } + atomic_set_bit(adv->flags, BT_PER_ADV_CTE_PARAMS_SET); + return 0; } + +static int bt_df_set_adv_cte_tx_enabled(struct bt_le_ext_adv *adv, bool enable) +{ + if (!atomic_test_bit(adv->flags, BT_PER_ADV_PARAMS_SET)) { + return -EINVAL; + } + + if (!atomic_test_bit(adv->flags, BT_PER_ADV_CTE_PARAMS_SET)) { + return -EINVAL; + } + + if (enable == atomic_test_bit(adv->flags, BT_PER_ADV_CTE_ENABLED)) { + return -EALREADY; + } + + return hci_df_set_adv_cte_tx_enable(adv, enable); +} + +int bt_df_adv_cte_tx_enable(struct bt_le_ext_adv *adv) +{ + __ASSERT_NO_MSG(adv); + return bt_df_set_adv_cte_tx_enabled(adv, true); +} + +int bt_df_adv_cte_tx_disable(struct bt_le_ext_adv *adv) +{ + __ASSERT_NO_MSG(adv); + return bt_df_set_adv_cte_tx_enabled(adv, false); +} diff --git a/subsys/bluetooth/host/hci_core.h b/subsys/bluetooth/host/hci_core.h index 7cfe92ad8d2..a7c581395cd 100644 --- a/subsys/bluetooth/host/hci_core.h +++ b/subsys/bluetooth/host/hci_core.h @@ -105,6 +105,14 @@ enum { BT_PER_ADV_ENABLED, /* Periodic Advertising parameters has been set in the controller. */ BT_PER_ADV_PARAMS_SET, + /* Constant Tone Extension parameters for Periodic Advertising + * has been set in the controller. + */ + BT_PER_ADV_CTE_PARAMS_SET, + /* Constant Tone Extension for Periodic Advertising has been enabled + * in the controller. + */ + BT_PER_ADV_CTE_ENABLED, BT_ADV_NUM_FLAGS, };