From 2af5b8a5923c45afeade6e49033dc14f3fd9d3ad Mon Sep 17 00:00:00 2001 From: Sean Madigan Date: Tue, 10 Sep 2024 11:02:29 +0100 Subject: [PATCH] bluetooth: shell: Add CS to BT shell with set default settings command Use a new file and command for this where all CS commands can live. Added support for bt_cs_set_default_settings command. Added test case where shell is built with CS to ensure code is built in CI. Signed-off-by: Sean Madigan --- subsys/bluetooth/shell/CMakeLists.txt | 4 + subsys/bluetooth/shell/cs.c | 139 ++++++++++++++++++++++++++ tests/bluetooth/shell/testcase.yaml | 7 ++ 3 files changed, 150 insertions(+) create mode 100644 subsys/bluetooth/shell/cs.c diff --git a/subsys/bluetooth/shell/CMakeLists.txt b/subsys/bluetooth/shell/CMakeLists.txt index 58d3ba5b2d951ed..f6cc40e6d4a0634 100644 --- a/subsys/bluetooth/shell/CMakeLists.txt +++ b/subsys/bluetooth/shell/CMakeLists.txt @@ -29,6 +29,10 @@ zephyr_library_sources_ifdef( CONFIG_BT_ISO iso.c ) +zephyr_library_sources_ifdef( + CONFIG_BT_CHANNEL_SOUNDING + cs.c + ) zephyr_library_sources_ifdef( CONFIG_BT_IAS ias.c diff --git a/subsys/bluetooth/shell/cs.c b/subsys/bluetooth/shell/cs.c new file mode 100644 index 000000000000000..2a90de770c8613a --- /dev/null +++ b/subsys/bluetooth/shell/cs.c @@ -0,0 +1,139 @@ +/** @file + * @brief Bluetooth Channel Sounding (CS) shell + * + */ + +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "bt.h" + +static int check_cs_sync_antenna_selection_input(uint16_t input) +{ + if (input != BT_CS_ANTENNA_SELECTION_OPT_ONE && input != BT_CS_ANTENNA_SELECTION_OPT_TWO && + input != BT_CS_ANTENNA_SELECTION_OPT_THREE && + input != BT_CS_ANTENNA_SELECTION_OPT_FOUR && + input != BT_CS_ANTENNA_SELECTION_OPT_REPETITIVE && + input != BT_CS_ANTENNA_SELECTION_OPT_NO_RECOMMENDATION) { + return -EINVAL; + } + + return 0; +} + +static int check_max_tx_power_input(int16_t input) +{ + if (input < BT_HCI_OP_LE_CS_MIN_MAX_TX_POWER || input > BT_HCI_OP_LE_CS_MAX_MAX_TX_POWER) { + return -EINVAL; + } + + return 0; +} + +static int cmd_set_default_settings(const struct shell *sh, size_t argc, char *argv[]) +{ + int err = 0; + struct bt_cs_set_default_settings_param params; + uint16_t antenna_input; + int16_t tx_power_input; + + if (default_conn == NULL) { + shell_error(sh, "Conn handle error, at least one connection is required."); + return -ENOEXEC; + } + + params.enable_initiator_role = shell_strtobool(argv[1], 10, &err); + if (err) { + shell_help(sh); + shell_error(sh, "Could not parse input 1, Enable initiator role"); + return SHELL_CMD_HELP_PRINTED; + } + + params.enable_reflector_role = shell_strtobool(argv[2], 10, &err); + if (err) { + shell_help(sh); + shell_error(sh, "Could not parse input 2, Enable reflector role"); + return SHELL_CMD_HELP_PRINTED; + } + + antenna_input = shell_strtoul(argv[3], 16, &err); + if (err) { + shell_help(sh); + shell_error(sh, "Could not parse input 3, CS_SYNC antenna selection"); + return SHELL_CMD_HELP_PRINTED; + } + + err = check_cs_sync_antenna_selection_input(antenna_input); + if (err) { + shell_help(sh); + shell_error(sh, "CS_SYNC antenna selection input invalid"); + return SHELL_CMD_HELP_PRINTED; + } + + tx_power_input = shell_strtol(argv[4], 10, &err); + if (err) { + shell_help(sh); + shell_error(sh, "Could not parse input 4, Max TX power"); + return SHELL_CMD_HELP_PRINTED; + } + + err = check_max_tx_power_input(tx_power_input); + if (err) { + shell_help(sh); + shell_error(sh, "Max TX power input invalid"); + return SHELL_CMD_HELP_PRINTED; + } + + params.cs_sync_antenna_selection = antenna_input; + params.max_tx_power = tx_power_input; + + err = bt_cs_set_default_settings(default_conn, ¶ms); + if (err) { + shell_error(sh, "bt_cs_set_default_settings returned error %d", err); + return -ENOEXEC; + } + + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE( + cs_cmds, + SHELL_CMD_ARG( + set_default_settings, NULL, + " " + " ", + cmd_set_default_settings, 1, 4), + SHELL_SUBCMD_SET_END); + +static int cmd_cs(const struct shell *sh, size_t argc, char **argv) +{ + if (argc == 1) { + shell_help(sh); + + return SHELL_CMD_HELP_PRINTED; + } + + shell_error(sh, "%s unknown parameter: %s", argv[0], argv[1]); + + return -EINVAL; +} + +SHELL_CMD_ARG_REGISTER(cs, &cs_cmds, "Bluetooth CS shell commands", cmd_cs, 1, 1); diff --git a/tests/bluetooth/shell/testcase.yaml b/tests/bluetooth/shell/testcase.yaml index 4a3bca95fb11bb8..db08fef9acda31a 100644 --- a/tests/bluetooth/shell/testcase.yaml +++ b/tests/bluetooth/shell/testcase.yaml @@ -43,6 +43,13 @@ tests: platform_allow: - native_sim build_only: true + bluetooth.shell.channel_sounding: + extra_configs: + - CONFIG_BT_CHANNEL_SOUNDING=y + - CONFIG_BT_CTLR=n + platform_allow: + - native_posix + build_only: true bluetooth.shell.cdc_acm: extra_args: - OVERLAY_CONFIG=cdc_acm.conf