blob: 03cc3a8e95f17280e864fc1c45f3775ab33e8c36 [file] [log] [blame]
/* cs.c - Bluetooth Channel Sounding handling */
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/sys/byteorder.h>
#include <zephyr/bluetooth/cs.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/hci_types.h>
#include "conn_internal.h"
#define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_cs);
#if defined(CONFIG_BT_CHANNEL_SOUNDING)
#if defined(CONFIG_BT_CHANNEL_SOUNDING_TEST)
static struct bt_le_cs_test_cb cs_test_callbacks;
#endif
struct reassembly_buf_meta_data {
uint16_t conn_handle;
};
static void clear_on_disconnect(struct bt_conn *conn, uint8_t reason);
NET_BUF_POOL_FIXED_DEFINE(reassembly_buf_pool, CONFIG_BT_CHANNEL_SOUNDING_REASSEMBLY_BUFFER_CNT,
CONFIG_BT_CHANNEL_SOUNDING_REASSEMBLY_BUFFER_SIZE,
sizeof(struct reassembly_buf_meta_data), NULL);
static sys_slist_t reassembly_bufs = SYS_SLIST_STATIC_INIT(&reassembly_bufs);
struct bt_conn_le_cs_subevent_result reassembled_result;
BT_CONN_CB_DEFINE(cs_conn_callbacks) = {
.disconnected = clear_on_disconnect,
};
/** @brief Allocates new reassembly buffer identified by the connection handle
*
* @param conn_handle Connection handle
* @return struct net_buf* Reassembly buffer, NULL if allocation fails
*/
static struct net_buf *alloc_reassembly_buf(uint16_t conn_handle)
{
struct net_buf *buf = net_buf_alloc(&reassembly_buf_pool, K_NO_WAIT);
if (!buf) {
LOG_ERR("Failed to allocate new reassembly buffer");
return NULL;
}
struct reassembly_buf_meta_data *buf_meta_data =
(struct reassembly_buf_meta_data *)buf->user_data;
buf_meta_data->conn_handle = conn_handle;
net_buf_slist_put(&reassembly_bufs, buf);
LOG_DBG("Allocated new reassembly buffer for conn handle %d", conn_handle);
return buf;
}
/** @brief Frees a reassembly buffer
*
* @note Takes the ownership of the pointer and sets it to NULL
*
* @param buf Double pointer to reassembly buffer
*/
static void free_reassembly_buf(struct net_buf **buf)
{
if (!buf) {
LOG_ERR("NULL double pointer was passed when attempting to free reassembly buffer");
return;
}
if (!(*buf)) {
LOG_WRN("Attempted double free on reassembly buffer");
return;
}
struct reassembly_buf_meta_data *buf_meta_data =
(struct reassembly_buf_meta_data *)((*buf)->user_data);
LOG_DBG("De-allocating reassembly buffer for conn handle %d", buf_meta_data->conn_handle);
if (!sys_slist_find_and_remove(&reassembly_bufs, &(*buf)->node)) {
LOG_WRN("The buffer was not in the list");
}
net_buf_unref(*buf);
*buf = NULL;
}
/** @brief Gets the reassembly buffer identified by the connection handle
*
* @param conn_handle Connection handle
* @param allocate Allocates a new reassembly buffer if it's not allocated already
* @return struct net_buf* Reassembly buffer, NULL if it doesn't exist or failed when allocating new
*/
static struct net_buf *get_reassembly_buf(uint16_t conn_handle, bool allocate)
{
sys_snode_t *node;
SYS_SLIST_FOR_EACH_NODE(&reassembly_bufs, node) {
struct net_buf *buf = CONTAINER_OF(node, struct net_buf, node);
struct reassembly_buf_meta_data *buf_meta_data =
(struct reassembly_buf_meta_data *)(buf->user_data);
if (buf_meta_data->conn_handle == conn_handle) {
return buf;
}
}
return allocate ? alloc_reassembly_buf(conn_handle) : NULL;
}
/** @brief Adds step data to a reassembly buffer
*
* @param reassembly_buf Reassembly buffer
* @param data Step data
* @param data_len Step data length
* @return true if successful, false if there is insufficient space
*/
static bool add_reassembly_data(struct net_buf *reassembly_buf, const uint8_t *data,
uint16_t data_len)
{
if (data_len > net_buf_tailroom(reassembly_buf)) {
LOG_ERR("Not enough reassembly buffer space for subevent result");
return false;
}
net_buf_add_mem(reassembly_buf, data, data_len);
return true;
}
/** @brief Initializes a reassembly buffer from partial step data
*
* @note Upon first call, this function also registers the disconnection callback
* to ensure any dangling reassembly buffer is freed
*
* @param conn_handle Connection handle
* @param steps Step data
* @param step_data_len Step data length
* @return struct net_buf* Pointer to reassembly buffer, NULL if fails to allocate or insert data
*/
static struct net_buf *start_reassembly(uint16_t conn_handle, const uint8_t *steps,
uint16_t step_data_len)
{
struct net_buf *reassembly_buf = get_reassembly_buf(conn_handle, true);
if (!reassembly_buf) {
LOG_ERR("No buffer allocated for the result reassembly");
return NULL;
}
if (reassembly_buf->len) {
LOG_WRN("Over-written incomplete CS subevent results");
}
net_buf_reset(reassembly_buf);
bool success = add_reassembly_data(reassembly_buf, steps, step_data_len);
return success ? reassembly_buf : NULL;
}
/** @brief Adds more step data to reassembly buffer identified by the connection handle
*
* @param conn_handle Connection handle
* @param steps Step data
* @param step_data_len Step data length
* @return struct net_buf* Pointer to reassembly buffer, NULL if fails to insert data
*/
static struct net_buf *continue_reassembly(uint16_t conn_handle, const uint8_t *steps,
uint16_t step_data_len)
{
struct net_buf *reassembly_buf = get_reassembly_buf(conn_handle, false);
if (!reassembly_buf) {
LOG_ERR("No reassembly buffer was allocated for this CS procedure, possibly due to "
"an out-of-order subevent result continue event");
return NULL;
}
if (!reassembly_buf->len) {
LOG_WRN("Discarded out-of-order partial CS subevent results");
return NULL;
}
if (!step_data_len) {
return reassembly_buf;
}
bool success = add_reassembly_data(reassembly_buf, steps, step_data_len);
return success ? reassembly_buf : NULL;
}
/**
* @brief Disconnect callback to clear any dangling reassembly buffer
*
* @param conn Connection
* @param reason Reason
*/
static void clear_on_disconnect(struct bt_conn *conn, uint8_t reason)
{
struct net_buf *buf = get_reassembly_buf(conn->handle, false);
if (buf) {
free_reassembly_buf(&buf);
}
}
/** @brief Invokes user callback for new subevent results
*
* @param conn Connection context, NULL for CS Test subevent results
* @param p_result Pointer to subevent results
*/
static void invoke_subevent_result_callback(struct bt_conn *conn,
struct bt_conn_le_cs_subevent_result *p_result)
{
#if defined(CONFIG_BT_CHANNEL_SOUNDING_TEST)
if (!conn) {
cs_test_callbacks.le_cs_test_subevent_data_available(p_result);
} else
#endif /* CONFIG_BT_CHANNEL_SOUNDING_TEST */
{
notify_cs_subevent_result(conn, p_result);
}
}
/** @brief Resets reassembly results
*
*/
static void reset_reassembly_results(void)
{
memset(&reassembled_result, 0, sizeof(struct bt_conn_le_cs_subevent_result));
}
/** @brief Converts PCT to a pair of int16_t
*
*/
struct bt_le_cs_iq_sample bt_le_cs_parse_pct(const uint8_t pct[3])
{
uint32_t pct_u32 = sys_get_le24(pct);
/* Extract I and Q. */
uint16_t i_u16 = pct_u32 & BT_HCI_LE_CS_PCT_I_MASK;
uint16_t q_u16 = (pct_u32 & BT_HCI_LE_CS_PCT_Q_MASK) >> 12;
/* Convert from 12-bit 2's complement to int16_t */
int16_t i = (i_u16 ^ BIT(11)) - BIT(11);
int16_t q = (q_u16 ^ BIT(11)) - BIT(11);
return (struct bt_le_cs_iq_sample){.i = i, .q = q};
}
void bt_le_cs_set_valid_chmap_bits(uint8_t channel_map[10])
{
memset(channel_map, 0xFF, 10);
/** Channels n = 0, 1, 23, 24, 25, 77, and 78 are not allowed and shall be set to zero.
* Channel 79 is reserved for future use and shall be set to zero.
*/
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 0, 0);
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 1, 0);
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 23, 0);
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 24, 0);
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 25, 0);
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 77, 0);
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 78, 0);
BT_LE_CS_CHANNEL_BIT_SET_VAL(channel_map, 79, 0);
}
int bt_le_cs_read_remote_supported_capabilities(struct bt_conn *conn)
{
struct bt_hci_cp_le_read_remote_supported_capabilities *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_READ_REMOTE_SUPPORTED_CAPABILITIES, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_READ_REMOTE_SUPPORTED_CAPABILITIES, buf, NULL);
}
void bt_hci_le_cs_read_remote_supported_capabilities_complete(struct net_buf *buf)
{
struct bt_conn *conn;
struct bt_conn_le_cs_capabilities remote_cs_capabilities;
struct bt_hci_evt_le_cs_read_remote_supported_capabilities_complete *evt;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
if (evt->status) {
LOG_INF("Read Remote Supported Capabilities failed (status 0x%02X)", evt->status);
return;
}
conn = bt_conn_lookup_handle(sys_le16_to_cpu(evt->conn_handle), BT_CONN_TYPE_LE);
if (!conn) {
LOG_ERR("Could not lookup connection handle when reading remote CS capabilities");
return;
}
remote_cs_capabilities.num_config_supported = evt->num_config_supported;
remote_cs_capabilities.max_consecutive_procedures_supported =
sys_le16_to_cpu(evt->max_consecutive_procedures_supported);
remote_cs_capabilities.num_antennas_supported = evt->num_antennas_supported;
remote_cs_capabilities.max_antenna_paths_supported = evt->max_antenna_paths_supported;
remote_cs_capabilities.initiator_supported =
evt->roles_supported & BT_HCI_LE_CS_INITIATOR_ROLE_MASK;
remote_cs_capabilities.reflector_supported =
evt->roles_supported & BT_HCI_LE_CS_REFLECTOR_ROLE_MASK;
remote_cs_capabilities.mode_3_supported =
evt->modes_supported & BT_HCI_LE_CS_MODES_SUPPORTED_MODE_3_MASK;
remote_cs_capabilities.rtt_aa_only_n = evt->rtt_aa_only_n;
remote_cs_capabilities.rtt_sounding_n = evt->rtt_sounding_n;
remote_cs_capabilities.rtt_random_payload_n = evt->rtt_random_payload_n;
if (evt->rtt_aa_only_n) {
if (evt->rtt_capability & BT_HCI_LE_CS_RTT_AA_ONLY_N_10NS_MASK) {
remote_cs_capabilities.rtt_aa_only_precision =
BT_CONN_LE_CS_RTT_AA_ONLY_10NS;
} else {
remote_cs_capabilities.rtt_aa_only_precision =
BT_CONN_LE_CS_RTT_AA_ONLY_150NS;
}
} else {
remote_cs_capabilities.rtt_aa_only_precision = BT_CONN_LE_CS_RTT_AA_ONLY_NOT_SUPP;
}
if (evt->rtt_sounding_n) {
if (evt->rtt_capability & BT_HCI_LE_CS_RTT_SOUNDING_N_10NS_MASK) {
remote_cs_capabilities.rtt_sounding_precision =
BT_CONN_LE_CS_RTT_SOUNDING_10NS;
} else {
remote_cs_capabilities.rtt_sounding_precision =
BT_CONN_LE_CS_RTT_SOUNDING_150NS;
}
} else {
remote_cs_capabilities.rtt_sounding_precision = BT_CONN_LE_CS_RTT_SOUNDING_NOT_SUPP;
}
if (evt->rtt_random_payload_n) {
if (evt->rtt_capability & BT_HCI_LE_CS_RTT_RANDOM_PAYLOAD_N_10NS_MASK) {
remote_cs_capabilities.rtt_random_payload_precision =
BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_10NS;
} else {
remote_cs_capabilities.rtt_random_payload_precision =
BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_150NS;
}
} else {
remote_cs_capabilities.rtt_random_payload_precision =
BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_NOT_SUPP;
}
remote_cs_capabilities.phase_based_nadm_sounding_supported =
sys_le16_to_cpu(evt->nadm_sounding_capability) &
BT_HCI_LE_CS_NADM_SOUNDING_CAPABILITY_PHASE_BASED_MASK;
remote_cs_capabilities.phase_based_nadm_random_supported =
sys_le16_to_cpu(evt->nadm_random_capability) &
BT_HCI_LE_CS_NADM_RANDOM_CAPABILITY_PHASE_BASED_MASK;
remote_cs_capabilities.cs_sync_2m_phy_supported =
evt->cs_sync_phys_supported & BT_HCI_LE_CS_SYNC_PHYS_2M_MASK;
remote_cs_capabilities.cs_sync_2m_2bt_phy_supported =
evt->cs_sync_phys_supported & BT_HCI_LE_CS_SYNC_PHYS_2M_2BT_MASK;
remote_cs_capabilities.cs_without_fae_supported =
sys_le16_to_cpu(evt->subfeatures_supported) &
BT_HCI_LE_CS_SUBFEATURE_NO_TX_FAE_MASK;
remote_cs_capabilities.chsel_alg_3c_supported =
sys_le16_to_cpu(evt->subfeatures_supported) &
BT_HCI_LE_CS_SUBFEATURE_CHSEL_ALG_3C_MASK;
remote_cs_capabilities.pbr_from_rtt_sounding_seq_supported =
sys_le16_to_cpu(evt->subfeatures_supported) &
BT_HCI_LE_CS_SUBFEATURE_PBR_FROM_RTT_SOUNDING_SEQ_MASK;
remote_cs_capabilities.t_ip1_times_supported = sys_le16_to_cpu(evt->t_ip1_times_supported);
remote_cs_capabilities.t_ip2_times_supported = sys_le16_to_cpu(evt->t_ip2_times_supported);
remote_cs_capabilities.t_fcs_times_supported = sys_le16_to_cpu(evt->t_fcs_times_supported);
remote_cs_capabilities.t_pm_times_supported = sys_le16_to_cpu(evt->t_pm_times_supported);
remote_cs_capabilities.t_sw_time = evt->t_sw_time_supported;
remote_cs_capabilities.tx_snr_capability = evt->tx_snr_capability;
notify_remote_cs_capabilities(conn, remote_cs_capabilities);
bt_conn_unref(conn);
}
int bt_le_cs_set_default_settings(struct bt_conn *conn,
const struct bt_le_cs_set_default_settings_param *params)
{
struct bt_hci_cp_le_cs_set_default_settings *cp;
struct net_buf *buf;
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_REFLECTOR_ROLE_MASK;
}
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_SET_DEFAULT_SETTINGS, buf, NULL);
}
int bt_le_cs_read_remote_fae_table(struct bt_conn *conn)
{
struct bt_hci_cp_le_read_remote_fae_table *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_READ_REMOTE_FAE_TABLE, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_READ_REMOTE_FAE_TABLE, buf, NULL);
}
void bt_hci_le_cs_read_remote_fae_table_complete(struct net_buf *buf)
{
struct bt_conn *conn;
struct bt_conn_le_cs_fae_table fae_table;
struct bt_hci_evt_le_cs_read_remote_fae_table_complete *evt;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
if (evt->status) {
LOG_INF("Read Remote FAE Table failed with status 0x%02X", evt->status);
return;
}
conn = bt_conn_lookup_handle(sys_le16_to_cpu(evt->conn_handle), BT_CONN_TYPE_LE);
if (!conn) {
LOG_ERR("Could not lookup connection handle when reading remote FAE Table");
return;
}
fae_table.remote_fae_table = evt->remote_fae_table;
notify_remote_cs_fae_table(conn, fae_table);
bt_conn_unref(conn);
}
#if defined(CONFIG_BT_CHANNEL_SOUNDING_TEST)
int bt_le_cs_test_cb_register(struct bt_le_cs_test_cb cb)
{
cs_test_callbacks = cb;
return 0;
}
int bt_le_cs_start_test(const struct bt_le_cs_test_param *params)
{
struct bt_hci_op_le_cs_test *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_TEST, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->main_mode_type = params->main_mode;
cp->sub_mode_type = params->sub_mode;
cp->main_mode_repetition = params->main_mode_repetition;
cp->mode_0_steps = params->mode_0_steps;
cp->role = params->role;
cp->rtt_type = params->rtt_type;
cp->cs_sync_phy = params->cs_sync_phy;
cp->cs_sync_antenna_selection = params->cs_sync_antenna_selection;
sys_put_le24(params->subevent_len, cp->subevent_len);
cp->subevent_interval = sys_cpu_to_le16(params->subevent_interval);
cp->max_num_subevents = params->max_num_subevents;
cp->transmit_power_level = params->transmit_power_level;
cp->t_ip1_time = params->t_ip1_time;
cp->t_ip2_time = params->t_ip2_time;
cp->t_fcs_time = params->t_fcs_time;
cp->t_pm_time = params->t_pm_time;
cp->t_sw_time = params->t_sw_time;
cp->tone_antenna_config_selection = params->tone_antenna_config_selection;
cp->reserved = 0;
cp->snr_control_initiator = params->initiator_snr_control;
cp->snr_control_reflector = params->reflector_snr_control;
cp->drbg_nonce = sys_cpu_to_le16(params->drbg_nonce);
cp->channel_map_repetition = params->override_config_0.channel_map_repetition;
cp->override_config = sys_cpu_to_le16(params->override_config);
uint8_t override_parameters_length = 0;
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_0_MASK) {
const uint8_t num_channels = params->override_config_0.set.num_channels;
net_buf_add_u8(buf, num_channels);
override_parameters_length++;
net_buf_add_mem(buf, params->override_config_0.set.channels, num_channels);
override_parameters_length += num_channels;
} else {
net_buf_add_mem(buf, params->override_config_0.not_set.channel_map,
sizeof(params->override_config_0.not_set.channel_map));
net_buf_add_u8(buf, params->override_config_0.not_set.channel_selection_type);
net_buf_add_u8(buf, params->override_config_0.not_set.ch3c_shape);
net_buf_add_u8(buf, params->override_config_0.not_set.ch3c_jump);
override_parameters_length +=
(sizeof(params->override_config_0.not_set.channel_map) +
sizeof(params->override_config_0.not_set.channel_selection_type) +
sizeof(params->override_config_0.not_set.ch3c_shape) +
sizeof(params->override_config_0.not_set.ch3c_jump));
}
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_2_MASK) {
net_buf_add_mem(buf, &params->override_config_2, sizeof(params->override_config_2));
override_parameters_length += sizeof(params->override_config_2);
}
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_3_MASK) {
net_buf_add_mem(buf, &params->override_config_3, sizeof(params->override_config_3));
override_parameters_length += sizeof(params->override_config_3);
}
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_4_MASK) {
net_buf_add_mem(buf, &params->override_config_4, sizeof(params->override_config_4));
override_parameters_length += sizeof(params->override_config_4);
}
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_5_MASK) {
net_buf_add_le32(buf, params->override_config_5.cs_sync_aa_initiator);
net_buf_add_le32(buf, params->override_config_5.cs_sync_aa_reflector);
override_parameters_length += sizeof(params->override_config_5);
}
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_6_MASK) {
net_buf_add_mem(buf, &params->override_config_6, sizeof(params->override_config_6));
override_parameters_length += sizeof(params->override_config_6);
}
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_7_MASK) {
net_buf_add_mem(buf, &params->override_config_7, sizeof(params->override_config_7));
override_parameters_length += sizeof(params->override_config_7);
}
if (params->override_config & BT_HCI_OP_LE_CS_TEST_OVERRIDE_CONFIG_8_MASK) {
net_buf_add_mem(buf, &params->override_config_8, sizeof(params->override_config_8));
override_parameters_length += sizeof(params->override_config_8);
}
cp->override_parameters_length = override_parameters_length;
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_TEST, buf, NULL);
}
#endif /* CONFIG_BT_CHANNEL_SOUNDING_TEST */
void bt_hci_le_cs_subevent_result(struct net_buf *buf)
{
struct bt_conn *conn = NULL;
struct bt_hci_evt_le_cs_subevent_result *evt;
struct bt_conn_le_cs_subevent_result result;
struct bt_conn_le_cs_subevent_result *p_result = &result;
struct net_buf_simple step_data_buf;
struct net_buf *reassembly_buf = NULL;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
uint16_t conn_handle = sys_le16_to_cpu(evt->conn_handle);
#if defined(CONFIG_BT_CHANNEL_SOUNDING_TEST)
if (conn_handle == BT_HCI_LE_CS_TEST_CONN_HANDLE) {
if (!cs_test_callbacks.le_cs_test_subevent_data_available) {
LOG_WRN("No callback registered. Discarded subevent results from CS Test.");
return;
}
} else
#endif /* CONFIG_BT_CHANNEL_SOUNDING_TEST */
{
conn = bt_conn_lookup_handle(conn_handle, BT_CONN_TYPE_LE);
if (!conn) {
LOG_ERR("Unknown connection handle when processing subevent results");
return;
}
}
if (evt->subevent_done_status != BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_PARTIAL) {
p_result->step_data_buf = NULL;
if (evt->num_steps_reported) {
net_buf_simple_init_with_data(&step_data_buf, evt->steps, buf->len);
p_result->step_data_buf = &step_data_buf;
}
} else {
if (evt->procedure_done_status != BT_HCI_LE_CS_PROCEDURE_DONE_STATUS_PARTIAL) {
LOG_WRN("Procedure status is inconsistent with subevent status. Discarding "
"subevent results");
goto abort;
}
if (!evt->num_steps_reported) {
LOG_WRN("Discarding partial results without step data");
goto abort;
}
reassembly_buf = start_reassembly(conn_handle, evt->steps, buf->len);
if (!reassembly_buf) {
goto abort;
}
p_result = &reassembled_result;
p_result->step_data_buf = (struct net_buf_simple *)&reassembly_buf->data;
}
p_result->header.procedure_counter = sys_le16_to_cpu(evt->procedure_counter);
p_result->header.frequency_compensation = sys_le16_to_cpu(evt->frequency_compensation);
p_result->header.procedure_done_status = evt->procedure_done_status;
p_result->header.subevent_done_status = evt->subevent_done_status;
p_result->header.procedure_abort_reason = evt->procedure_abort_reason;
p_result->header.subevent_abort_reason = evt->subevent_abort_reason;
p_result->header.reference_power_level = evt->reference_power_level;
p_result->header.num_antenna_paths = evt->num_antenna_paths;
p_result->header.num_steps_reported = evt->num_steps_reported;
p_result->header.abort_step =
evt->subevent_done_status == BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_ABORTED ? 0 : 255;
p_result->header.config_id = 0;
p_result->header.start_acl_conn_event = 0;
if (conn) {
p_result->header.config_id = evt->config_id;
p_result->header.start_acl_conn_event =
sys_le16_to_cpu(evt->start_acl_conn_event_counter);
}
if (evt->subevent_done_status != BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_PARTIAL) {
invoke_subevent_result_callback(conn, p_result);
}
if (evt->procedure_done_status != BT_CONN_LE_CS_PROCEDURE_INCOMPLETE) {
/* We can now clear the any reassembly buffer allocated for this procedure,
* to avoid code duplication, we're using the abort label to do so
*/
goto abort;
}
if (conn) {
bt_conn_unref(conn);
conn = NULL;
}
return;
abort:
if (conn) {
bt_conn_unref(conn);
conn = NULL;
}
reassembly_buf = get_reassembly_buf(conn_handle, false);
if (reassembly_buf) {
free_reassembly_buf(&reassembly_buf);
}
}
void bt_hci_le_cs_subevent_result_continue(struct net_buf *buf)
{
struct bt_conn *conn = NULL;
struct bt_hci_evt_le_cs_subevent_result_continue *evt;
struct net_buf *reassembly_buf = NULL;
uint16_t conn_handle;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
conn_handle = sys_le16_to_cpu(evt->conn_handle);
#if defined(CONFIG_BT_CHANNEL_SOUNDING_TEST)
if (conn_handle == BT_HCI_LE_CS_TEST_CONN_HANDLE) {
if (!cs_test_callbacks.le_cs_test_subevent_data_available) {
LOG_WRN("No callback registered. Discarded subevent results from CS Test.");
return;
}
} else
#endif /* CONFIG_BT_CHANNEL_SOUNDING_TEST */
{
conn = bt_conn_lookup_handle(conn_handle, BT_CONN_TYPE_LE);
if (!conn) {
LOG_ERR("Unknown connection handle when processing subevent results");
return;
}
}
uint16_t step_data_len = evt->num_steps_reported ? buf->len : 0;
reassembly_buf = continue_reassembly(conn_handle, evt->steps, step_data_len);
if (!reassembly_buf) {
goto abort;
}
reassembled_result.header.procedure_done_status = evt->procedure_done_status;
reassembled_result.header.subevent_done_status = evt->subevent_done_status;
reassembled_result.header.procedure_abort_reason = evt->procedure_abort_reason;
reassembled_result.header.subevent_abort_reason = evt->subevent_abort_reason;
if (evt->num_antenna_paths != reassembled_result.header.num_antenna_paths) {
LOG_WRN("Received inconsistent number of antenna paths from the controller: %d, "
"previous number was: %d",
evt->num_antenna_paths, reassembled_result.header.num_antenna_paths);
}
if (evt->subevent_done_status == BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_ABORTED &&
reassembled_result.header.num_steps_reported < reassembled_result.header.abort_step) {
reassembled_result.header.abort_step = reassembled_result.header.num_steps_reported;
}
reassembled_result.header.num_steps_reported += evt->num_steps_reported;
if (evt->subevent_done_status != BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_PARTIAL) {
invoke_subevent_result_callback(conn, &reassembled_result);
net_buf_reset(reassembly_buf);
reset_reassembly_results();
}
if (evt->procedure_done_status != BT_HCI_LE_CS_PROCEDURE_DONE_STATUS_PARTIAL) {
if (evt->subevent_done_status == BT_HCI_LE_CS_SUBEVENT_DONE_STATUS_PARTIAL) {
LOG_WRN("Procedure status is inconsistent with subevent status. Discarding "
"subevent results");
goto abort;
}
free_reassembly_buf(&reassembly_buf);
}
if (conn) {
bt_conn_unref(conn);
conn = NULL;
}
return;
abort:
if (conn) {
bt_conn_unref(conn);
conn = NULL;
}
if (reassembly_buf) {
free_reassembly_buf(&reassembly_buf);
}
}
void bt_hci_le_cs_config_complete_event(struct net_buf *buf)
{
struct bt_hci_evt_le_cs_config_complete *evt;
struct bt_conn_le_cs_config config;
struct bt_conn *conn;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
if (evt->status) {
LOG_INF("CS Config failed (status 0x%02X)", evt->status);
return;
}
conn = bt_conn_lookup_handle(sys_le16_to_cpu(evt->handle), BT_CONN_TYPE_LE);
if (!conn) {
LOG_ERR("Could not lookup connection handle when reading CS configuration");
return;
}
if (evt->action == BT_HCI_LE_CS_CONFIG_ACTION_REMOVED) {
notify_cs_config_removed(conn, evt->config_id);
bt_conn_unref(conn);
return;
}
config.id = evt->config_id;
config.main_mode_type = evt->main_mode_type;
config.sub_mode_type = evt->sub_mode_type;
config.min_main_mode_steps = evt->min_main_mode_steps;
config.max_main_mode_steps = evt->max_main_mode_steps;
config.main_mode_repetition = evt->main_mode_repetition;
config.mode_0_steps = evt->mode_0_steps;
config.role = evt->role;
config.rtt_type = evt->rtt_type;
config.cs_sync_phy = evt->cs_sync_phy;
config.channel_map_repetition = evt->channel_map_repetition;
config.channel_selection_type = evt->channel_selection_type;
config.ch3c_shape = evt->ch3c_shape;
config.ch3c_jump = evt->ch3c_jump;
config.t_ip1_time_us = evt->t_ip1_time;
config.t_ip2_time_us = evt->t_ip2_time;
config.t_fcs_time_us = evt->t_fcs_time;
config.t_pm_time_us = evt->t_pm_time;
memcpy(config.channel_map, evt->channel_map, ARRAY_SIZE(config.channel_map));
notify_cs_config_created(conn, &config);
bt_conn_unref(conn);
}
int bt_le_cs_create_config(struct bt_conn *conn, struct bt_le_cs_create_config_params *params,
enum bt_le_cs_create_config_context context)
{
struct bt_hci_cp_le_cs_create_config *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_CREATE_CONFIG, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
cp->config_id = params->id;
cp->create_context = context;
cp->main_mode_type = params->main_mode_type;
cp->sub_mode_type = params->sub_mode_type;
cp->min_main_mode_steps = params->min_main_mode_steps;
cp->max_main_mode_steps = params->max_main_mode_steps;
cp->main_mode_repetition = params->main_mode_repetition;
cp->mode_0_steps = params->mode_0_steps;
cp->role = params->role;
cp->rtt_type = params->rtt_type;
cp->cs_sync_phy = params->cs_sync_phy;
cp->channel_map_repetition = params->channel_map_repetition;
cp->channel_selection_type = params->channel_selection_type;
cp->ch3c_shape = params->ch3c_shape;
cp->ch3c_jump = params->ch3c_jump;
cp->reserved = 0;
memcpy(cp->channel_map, params->channel_map, ARRAY_SIZE(cp->channel_map));
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_CREATE_CONFIG, buf, NULL);
}
int bt_le_cs_remove_config(struct bt_conn *conn, uint8_t config_id)
{
struct bt_hci_cp_le_cs_remove_config *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_REMOVE_CONFIG, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
cp->config_id = config_id;
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_REMOVE_CONFIG, buf, NULL);
}
int bt_le_cs_security_enable(struct bt_conn *conn)
{
struct bt_hci_cp_le_security_enable *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_SECURITY_ENABLE, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_SECURITY_ENABLE, buf, NULL);
}
int bt_le_cs_procedure_enable(struct bt_conn *conn,
const struct bt_le_cs_procedure_enable_param *params)
{
struct bt_hci_cp_le_procedure_enable *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_PROCEDURE_ENABLE, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
cp->config_id = params->config_id;
cp->enable = params->enable;
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_PROCEDURE_ENABLE, buf, NULL);
}
int bt_le_cs_set_procedure_parameters(struct bt_conn *conn,
const struct bt_le_cs_set_procedure_parameters_param *params)
{
struct bt_hci_cp_le_set_procedure_parameters *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_SET_PROCEDURE_PARAMETERS, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
cp->config_id = params->config_id;
cp->max_procedure_len = sys_cpu_to_le16(params->max_procedure_len);
cp->min_procedure_interval = sys_cpu_to_le16(params->min_procedure_interval);
cp->max_procedure_interval = sys_cpu_to_le16(params->max_procedure_interval);
cp->max_procedure_count = sys_cpu_to_le16(params->max_procedure_count);
sys_put_le24(params->min_subevent_len, cp->min_subevent_len);
sys_put_le24(params->max_subevent_len, cp->max_subevent_len);
cp->tone_antenna_config_selection = params->tone_antenna_config_selection;
cp->phy = params->phy;
cp->tx_power_delta = params->tx_power_delta;
cp->preferred_peer_antenna = params->preferred_peer_antenna;
cp->snr_control_initiator = params->snr_control_initiator;
cp->snr_control_reflector = params->snr_control_reflector;
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_SET_PROCEDURE_PARAMETERS, buf, NULL);
}
int bt_le_cs_set_channel_classification(uint8_t channel_classification[10])
{
uint8_t *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_SET_CHANNEL_CLASSIFICATION, 10);
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, 10);
memcpy(cp, channel_classification, 10);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_SET_CHANNEL_CLASSIFICATION, buf, NULL);
}
int bt_le_cs_read_local_supported_capabilities(struct bt_conn_le_cs_capabilities *ret)
{
struct bt_hci_rp_le_read_local_supported_capabilities *rp;
struct net_buf *rsp;
int err =
bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_READ_LOCAL_SUPPORTED_CAPABILITIES, NULL, &rsp);
if (err) {
return err;
}
rp = (void *)rsp->data;
uint8_t status = rp->status;
ret->num_config_supported = rp->num_config_supported;
ret->max_consecutive_procedures_supported =
sys_le16_to_cpu(rp->max_consecutive_procedures_supported);
ret->num_antennas_supported = rp->num_antennas_supported;
ret->max_antenna_paths_supported = rp->max_antenna_paths_supported;
ret->initiator_supported = rp->roles_supported & BT_HCI_LE_CS_INITIATOR_ROLE_MASK;
ret->reflector_supported = rp->roles_supported & BT_HCI_LE_CS_REFLECTOR_ROLE_MASK;
ret->mode_3_supported = rp->modes_supported & BT_HCI_LE_CS_MODES_SUPPORTED_MODE_3_MASK;
ret->rtt_aa_only_n = rp->rtt_aa_only_n;
ret->rtt_sounding_n = rp->rtt_sounding_n;
ret->rtt_random_payload_n = rp->rtt_random_payload_n;
if (rp->rtt_aa_only_n) {
if (rp->rtt_capability & BT_HCI_LE_CS_RTT_AA_ONLY_N_10NS_MASK) {
ret->rtt_aa_only_precision = BT_CONN_LE_CS_RTT_AA_ONLY_10NS;
} else {
ret->rtt_aa_only_precision = BT_CONN_LE_CS_RTT_AA_ONLY_150NS;
}
} else {
ret->rtt_aa_only_precision = BT_CONN_LE_CS_RTT_AA_ONLY_NOT_SUPP;
}
if (rp->rtt_sounding_n) {
if (rp->rtt_capability & BT_HCI_LE_CS_RTT_SOUNDING_N_10NS_MASK) {
ret->rtt_sounding_precision = BT_CONN_LE_CS_RTT_SOUNDING_10NS;
} else {
ret->rtt_sounding_precision = BT_CONN_LE_CS_RTT_SOUNDING_150NS;
}
} else {
ret->rtt_sounding_precision = BT_CONN_LE_CS_RTT_SOUNDING_NOT_SUPP;
}
if (rp->rtt_random_payload_n) {
if (rp->rtt_capability & BT_HCI_LE_CS_RTT_RANDOM_PAYLOAD_N_10NS_MASK) {
ret->rtt_random_payload_precision = BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_10NS;
} else {
ret->rtt_random_payload_precision = BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_150NS;
}
} else {
ret->rtt_random_payload_precision = BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_NOT_SUPP;
}
ret->phase_based_nadm_sounding_supported =
sys_le16_to_cpu(rp->nadm_sounding_capability) &
BT_HCI_LE_CS_NADM_SOUNDING_CAPABILITY_PHASE_BASED_MASK;
ret->phase_based_nadm_random_supported =
sys_le16_to_cpu(rp->nadm_random_capability) &
BT_HCI_LE_CS_NADM_RANDOM_CAPABILITY_PHASE_BASED_MASK;
ret->cs_sync_2m_phy_supported = rp->cs_sync_phys_supported & BT_HCI_LE_CS_SYNC_PHYS_2M_MASK;
ret->cs_sync_2m_2bt_phy_supported =
rp->cs_sync_phys_supported & BT_HCI_LE_CS_SYNC_PHYS_2M_2BT_MASK;
ret->cs_without_fae_supported =
sys_le16_to_cpu(rp->subfeatures_supported) & BT_HCI_LE_CS_SUBFEATURE_NO_TX_FAE_MASK;
ret->chsel_alg_3c_supported = sys_le16_to_cpu(rp->subfeatures_supported) &
BT_HCI_LE_CS_SUBFEATURE_CHSEL_ALG_3C_MASK;
ret->pbr_from_rtt_sounding_seq_supported =
sys_le16_to_cpu(rp->subfeatures_supported) &
BT_HCI_LE_CS_SUBFEATURE_PBR_FROM_RTT_SOUNDING_SEQ_MASK;
ret->t_ip1_times_supported = sys_le16_to_cpu(rp->t_ip1_times_supported);
ret->t_ip2_times_supported = sys_le16_to_cpu(rp->t_ip2_times_supported);
ret->t_fcs_times_supported = sys_le16_to_cpu(rp->t_fcs_times_supported);
ret->t_pm_times_supported = sys_le16_to_cpu(rp->t_pm_times_supported);
ret->t_sw_time = rp->t_sw_time_supported;
ret->tx_snr_capability = rp->tx_snr_capability;
net_buf_unref(rsp);
return status;
}
int bt_le_cs_write_cached_remote_supported_capabilities(
struct bt_conn *conn, const struct bt_conn_le_cs_capabilities *params)
{
struct bt_hci_cp_le_write_cached_remote_supported_capabilities *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_WRITE_CACHED_REMOTE_SUPPORTED_CAPABILITIES,
sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
cp->num_config_supported = params->num_config_supported;
cp->max_consecutive_procedures_supported =
sys_cpu_to_le16(params->max_consecutive_procedures_supported);
cp->num_antennas_supported = params->num_antennas_supported;
cp->max_antenna_paths_supported = params->max_antenna_paths_supported;
cp->roles_supported = 0;
if (params->initiator_supported) {
cp->roles_supported |= BT_HCI_LE_CS_INITIATOR_ROLE_MASK;
}
if (params->reflector_supported) {
cp->roles_supported |= BT_HCI_LE_CS_REFLECTOR_ROLE_MASK;
}
cp->modes_supported = 0;
if (params->mode_3_supported) {
cp->modes_supported |= BT_HCI_LE_CS_MODES_SUPPORTED_MODE_3_MASK;
}
cp->rtt_aa_only_n = params->rtt_aa_only_n;
cp->rtt_sounding_n = params->rtt_sounding_n;
cp->rtt_random_payload_n = params->rtt_random_payload_n;
cp->rtt_capability = 0;
if (params->rtt_aa_only_precision == BT_CONN_LE_CS_RTT_AA_ONLY_10NS) {
cp->rtt_capability |= BT_HCI_LE_CS_RTT_AA_ONLY_N_10NS_MASK;
}
if (params->rtt_sounding_precision == BT_CONN_LE_CS_RTT_SOUNDING_10NS) {
cp->rtt_capability |= BT_HCI_LE_CS_RTT_SOUNDING_N_10NS_MASK;
}
if (params->rtt_random_payload_precision == BT_CONN_LE_CS_RTT_RANDOM_PAYLOAD_10NS) {
cp->rtt_capability |= BT_HCI_LE_CS_RTT_RANDOM_PAYLOAD_N_10NS_MASK;
}
cp->nadm_sounding_capability = 0;
if (params->phase_based_nadm_sounding_supported) {
cp->nadm_sounding_capability |=
sys_cpu_to_le16(BT_HCI_LE_CS_NADM_SOUNDING_CAPABILITY_PHASE_BASED_MASK);
}
cp->nadm_random_capability = 0;
if (params->phase_based_nadm_random_supported) {
cp->nadm_random_capability |=
sys_cpu_to_le16(BT_HCI_LE_CS_NADM_RANDOM_CAPABILITY_PHASE_BASED_MASK);
}
cp->cs_sync_phys_supported = 0;
if (params->cs_sync_2m_phy_supported) {
cp->cs_sync_phys_supported |= BT_HCI_LE_CS_SYNC_PHYS_2M_MASK;
}
if (params->cs_sync_2m_2bt_phy_supported) {
cp->cs_sync_phys_supported |= BT_HCI_LE_CS_SYNC_PHYS_2M_2BT_MASK;
}
cp->subfeatures_supported = 0;
if (params->cs_without_fae_supported) {
cp->subfeatures_supported |=
sys_cpu_to_le16(BT_HCI_LE_CS_SUBFEATURE_NO_TX_FAE_MASK);
}
if (params->chsel_alg_3c_supported) {
cp->subfeatures_supported |=
sys_cpu_to_le16(BT_HCI_LE_CS_SUBFEATURE_CHSEL_ALG_3C_MASK);
}
if (params->pbr_from_rtt_sounding_seq_supported) {
cp->subfeatures_supported |=
sys_cpu_to_le16(BT_HCI_LE_CS_SUBFEATURE_PBR_FROM_RTT_SOUNDING_SEQ_MASK);
}
cp->t_ip1_times_supported = sys_cpu_to_le16(params->t_ip1_times_supported);
cp->t_ip2_times_supported = sys_cpu_to_le16(params->t_ip2_times_supported);
cp->t_fcs_times_supported = sys_cpu_to_le16(params->t_fcs_times_supported);
cp->t_pm_times_supported = sys_cpu_to_le16(params->t_pm_times_supported);
cp->t_sw_time_supported = params->t_sw_time;
cp->tx_snr_capability = params->tx_snr_capability;
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_WRITE_CACHED_REMOTE_SUPPORTED_CAPABILITIES, buf,
NULL);
}
int bt_le_cs_write_cached_remote_fae_table(struct bt_conn *conn, int8_t remote_fae_table[72])
{
struct bt_hci_cp_le_write_cached_remote_fae_table *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_WRITE_CACHED_REMOTE_FAE_TABLE, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
memcpy(cp->remote_fae_table, remote_fae_table, sizeof(cp->remote_fae_table));
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_WRITE_CACHED_REMOTE_FAE_TABLE, buf, NULL);
}
void bt_hci_le_cs_security_enable_complete(struct net_buf *buf)
{
struct bt_conn *conn;
struct bt_hci_evt_le_cs_security_enable_complete *evt;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
if (evt->status) {
LOG_INF("Security Enable failed with status 0x%02X", evt->status);
return;
}
conn = bt_conn_lookup_handle(sys_le16_to_cpu(evt->handle), BT_CONN_TYPE_LE);
if (!conn) {
LOG_ERR("Can't lookup conn handle when reading Security Enable Complete event");
return;
}
notify_cs_security_enable_available(conn);
bt_conn_unref(conn);
}
void bt_hci_le_cs_procedure_enable_complete(struct net_buf *buf)
{
struct bt_conn *conn;
struct bt_hci_evt_le_cs_procedure_enable_complete *evt;
struct bt_conn_le_cs_procedure_enable_complete params;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
if (evt->status) {
LOG_INF("Procedure Enable failed with status 0x%02X", evt->status);
return;
}
conn = bt_conn_lookup_handle(sys_le16_to_cpu(evt->handle), BT_CONN_TYPE_LE);
if (!conn) {
LOG_ERR("Can't lookup conn handle when reading Procedure Enable Complete event");
return;
}
if (evt->state == BT_HCI_OP_LE_CS_PROCEDURES_DISABLED) {
struct net_buf *reassembly_buf = get_reassembly_buf(conn->handle, false);
if (reassembly_buf) {
LOG_WRN("De-allocating a dangling reassembly buffer");
free_reassembly_buf(&reassembly_buf);
}
}
params.config_id = evt->config_id;
params.state = evt->state;
params.tone_antenna_config_selection = evt->tone_antenna_config_selection;
params.selected_tx_power = evt->selected_tx_power;
params.subevent_len = sys_get_le24(evt->subevent_len);
params.subevents_per_event = evt->subevents_per_event;
params.subevent_interval = sys_le16_to_cpu(evt->subevent_interval);
params.event_interval = sys_le16_to_cpu(evt->event_interval);
params.procedure_interval = sys_le16_to_cpu(evt->procedure_interval);
params.procedure_count = sys_le16_to_cpu(evt->procedure_count);
params.max_procedure_len = sys_le16_to_cpu(evt->max_procedure_len);
notify_cs_procedure_enable_available(conn, &params);
bt_conn_unref(conn);
}
#if defined(CONFIG_BT_CHANNEL_SOUNDING_TEST)
int bt_le_cs_stop_test(void)
{
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CS_TEST_END, 0);
if (!buf) {
return -ENOBUFS;
}
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CS_TEST_END, buf, NULL);
}
void bt_hci_le_cs_test_end_complete(struct net_buf *buf)
{
struct bt_hci_evt_le_cs_test_end_complete *evt;
if (buf->len < sizeof(*evt)) {
LOG_ERR("Unexpected end of buffer");
return;
}
evt = net_buf_pull_mem(buf, sizeof(*evt));
if (evt->status) {
LOG_INF("CS Test End failed with status 0x%02X", evt->status);
return;
}
struct net_buf *reassembly_buf = get_reassembly_buf(BT_HCI_LE_CS_TEST_CONN_HANDLE, false);
if (reassembly_buf) {
LOG_WRN("De-allocating a dangling reassembly buffer");
free_reassembly_buf(&reassembly_buf);
}
if (cs_test_callbacks.le_cs_test_end_complete) {
cs_test_callbacks.le_cs_test_end_complete();
}
}
#endif /* CONFIG_BT_CHANNEL_SOUNDING_TEST */
void bt_le_cs_step_data_parse(struct net_buf_simple *step_data_buf,
bool (*func)(struct bt_le_cs_subevent_step *step, void *user_data),
void *user_data)
{
if (!step_data_buf) {
LOG_INF("Tried to parse empty step data.");
return;
}
while (step_data_buf->len > 1) {
struct bt_le_cs_subevent_step step;
step.mode = net_buf_simple_pull_u8(step_data_buf);
step.channel = net_buf_simple_pull_u8(step_data_buf);
step.data_len = net_buf_simple_pull_u8(step_data_buf);
if (step.data_len == 0) {
LOG_WRN("Encountered zero-length step data.");
return;
}
step.data = step_data_buf->data;
if (step.data_len > step_data_buf->len) {
LOG_WRN("Step data appears malformed.");
return;
}
if (!func(&step, user_data)) {
return;
}
net_buf_simple_pull(step_data_buf, step.data_len);
}
}
#endif /* CONFIG_BT_CHANNEL_SOUNDING */