|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 5 | + */ |
| 6 | +#include "esp_log.h" |
| 7 | +#include "esp_bt.h" |
| 8 | + |
| 9 | +#include "esp_console.h" |
| 10 | +#include "argtable3/argtable3.h" |
| 11 | +#include "esp_hci_driver.h" |
| 12 | + |
| 13 | +#define PROMPT_STR CONFIG_IDF_TARGET |
| 14 | +static struct { |
| 15 | + struct arg_int *cmd_params; |
| 16 | + struct arg_end *end; |
| 17 | +} dtm_set_tx_power_cmd_args; |
| 18 | + |
| 19 | +static struct { |
| 20 | + struct arg_int *tx_pin; |
| 21 | + struct arg_int *rx_pin; |
| 22 | + struct arg_end *end; |
| 23 | +} dtm_reconfig_uart_cmd_args; |
| 24 | + |
| 25 | +static struct { |
| 26 | + struct arg_int *cmd_params; |
| 27 | + struct arg_end *end; |
| 28 | +} dtm_enable_cmd_args; |
| 29 | + |
| 30 | +static int dtm_set_ble_tx_power_command(int argc, char **argv) |
| 31 | +{ |
| 32 | + esp_err_t ret = ESP_OK; |
| 33 | + int nerrors = arg_parse(argc, argv, (void **) &dtm_set_tx_power_cmd_args); |
| 34 | + if (nerrors != 0) { |
| 35 | + arg_print_errors(stderr, dtm_set_tx_power_cmd_args.end, argv[0]); |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { |
| 40 | + esp_rom_printf("\nPlease enable BLE DTM mode first by using the command enable_ble_dtm -e 1 before sending this command.\n"); |
| 41 | + return 2; |
| 42 | + } |
| 43 | + |
| 44 | + ESP_LOGI(__func__, "Set tx power level '%d'", dtm_set_tx_power_cmd_args.cmd_params->ival[0]); |
| 45 | + if (dtm_set_tx_power_cmd_args.cmd_params->ival[0] > 15) { |
| 46 | + return 3; |
| 47 | + } |
| 48 | + |
| 49 | + ret = esp_ble_tx_power_set_enhanced(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0, dtm_set_tx_power_cmd_args.cmd_params->ival[0]); |
| 50 | + if (ret != ESP_OK) { |
| 51 | + return 4; |
| 52 | + } |
| 53 | + |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +static int dtm_get_ble_tx_power_command(int argc, char **argv) |
| 58 | +{ |
| 59 | + esp_power_level_t power_level = 0xFF; |
| 60 | + if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { |
| 61 | + esp_rom_printf("\nPlease enable BLE DTM mode first by using the command enable_ble_dtm -e 1 before sending this command.\n"); |
| 62 | + return 2; |
| 63 | + } |
| 64 | + |
| 65 | + power_level = esp_ble_tx_power_get_enhanced(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0); |
| 66 | + esp_rom_printf("\nCurrent BLE TX power is %d level\n", power_level); |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +static int dtm_reconfig_uart_pins_command(int argc, char **argv) |
| 71 | +{ |
| 72 | + int nerrors = arg_parse(argc, argv, (void **) &dtm_reconfig_uart_cmd_args); |
| 73 | + if (nerrors != 0) { |
| 74 | + arg_print_errors(stderr, dtm_reconfig_uart_cmd_args.end, argv[0]); |
| 75 | + return 1; |
| 76 | + } |
| 77 | + |
| 78 | + if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { |
| 79 | + esp_rom_printf("\nPlease enable BLE DTM mode first by using the command enable_ble_dtm -e 1 before sending this command.\n"); |
| 80 | + return 2; |
| 81 | + } |
| 82 | + |
| 83 | + ESP_LOGI(__func__, "reconfig tx:'%d', rx: '%d'", |
| 84 | + dtm_reconfig_uart_cmd_args.tx_pin->ival[0], dtm_reconfig_uart_cmd_args.rx_pin->ival[0]); |
| 85 | + hci_uart_reconfig_pin(dtm_reconfig_uart_cmd_args.tx_pin->ival[0], |
| 86 | + dtm_reconfig_uart_cmd_args.rx_pin->ival[0], -1, -1); |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +static int dtm_test_enable_command(int argc, char **argv) |
| 91 | +{ |
| 92 | + int dtm_enable = 0; |
| 93 | + esp_bt_controller_config_t config_opts = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); |
| 94 | + int nerrors = arg_parse(argc, argv, (void **) &dtm_enable_cmd_args); |
| 95 | + if (nerrors != 0) { |
| 96 | + arg_print_errors(stderr, dtm_enable_cmd_args.end, argv[0]); |
| 97 | + return 1; |
| 98 | + } |
| 99 | + |
| 100 | + dtm_enable = dtm_enable_cmd_args.cmd_params->ival[0]; |
| 101 | + ESP_LOGI(__func__, "Enable DTM Test '%d'", dtm_enable); |
| 102 | + if (dtm_enable > 2) { |
| 103 | + return 2; |
| 104 | + } |
| 105 | + |
| 106 | + if (dtm_enable) { |
| 107 | + if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) { |
| 108 | + /* Initialize Bluetooth Controller parameters. */ |
| 109 | + ESP_ERROR_CHECK(esp_bt_controller_init(&config_opts)); |
| 110 | + |
| 111 | + /* Enable the task stack of the Bluetooth Controller. */ |
| 112 | + ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE)); |
| 113 | + } |
| 114 | + } else { |
| 115 | + if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED) { |
| 116 | + ESP_ERROR_CHECK(esp_bt_controller_disable()); |
| 117 | + ESP_ERROR_CHECK(esp_bt_controller_deinit()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +esp_err_t esp_console_register_set_ble_tx_power_command(void) |
| 125 | +{ |
| 126 | + dtm_set_tx_power_cmd_args.cmd_params = arg_int1("i", "index", "<index>","tx power level index"); |
| 127 | + dtm_set_tx_power_cmd_args.end = arg_end(1); |
| 128 | + |
| 129 | + esp_console_cmd_t command = { |
| 130 | + .command = "set_ble_tx_power", |
| 131 | + .help = "Set ble tx power during DTM", |
| 132 | + .func = &dtm_set_ble_tx_power_command, |
| 133 | + .argtable = &dtm_set_tx_power_cmd_args |
| 134 | + }; |
| 135 | + |
| 136 | + return esp_console_cmd_register(&command); |
| 137 | +} |
| 138 | + |
| 139 | +esp_err_t esp_console_register_get_ble_tx_power_command(void) |
| 140 | +{ |
| 141 | + esp_console_cmd_t command = { |
| 142 | + .command = "get_ble_tx_power", |
| 143 | + .help = "Get ble tx power during DTM", |
| 144 | + .func = &dtm_get_ble_tx_power_command, |
| 145 | + }; |
| 146 | + |
| 147 | + return esp_console_cmd_register(&command); |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | +esp_err_t esp_console_register_reconfig_dtm_pins_command(void) |
| 152 | +{ |
| 153 | + dtm_reconfig_uart_cmd_args.tx_pin = arg_int1("t", "tx", "<tx_pin>","tx pin index"); |
| 154 | + dtm_reconfig_uart_cmd_args.rx_pin = arg_int1("r", "rx", "<rx_pin>","rx pin index"); |
| 155 | + dtm_reconfig_uart_cmd_args.end = arg_end(2); |
| 156 | + |
| 157 | + esp_console_cmd_t command = { |
| 158 | + .command = "reconfig_dtm_uart_pin", |
| 159 | + .help = "Reconfig dtm uart pins during DTM", |
| 160 | + .func = &dtm_reconfig_uart_pins_command, |
| 161 | + .argtable = &dtm_reconfig_uart_cmd_args |
| 162 | + }; |
| 163 | + |
| 164 | + return esp_console_cmd_register(&command); |
| 165 | +} |
| 166 | + |
| 167 | +esp_err_t esp_console_register_enable_ble_dtm_command(void) |
| 168 | +{ |
| 169 | + dtm_enable_cmd_args.cmd_params = arg_int1("e", "enable", "<enable>","enable/disable ble dtm test"); |
| 170 | + dtm_enable_cmd_args.end = arg_end(1); |
| 171 | + |
| 172 | + esp_console_cmd_t command = { |
| 173 | + .command = "enable_ble_dtm", |
| 174 | + .help = "Enable BLE DTM Test", |
| 175 | + .func = &dtm_test_enable_command, |
| 176 | + .argtable = &dtm_enable_cmd_args |
| 177 | + }; |
| 178 | + |
| 179 | + return esp_console_cmd_register(&command); |
| 180 | +} |
| 181 | + |
| 182 | +esp_err_t dtm_configuration_command_register(void) |
| 183 | +{ |
| 184 | + esp_console_register_set_ble_tx_power_command(); |
| 185 | + esp_console_register_get_ble_tx_power_command(); |
| 186 | + esp_console_register_reconfig_dtm_pins_command(); |
| 187 | + esp_console_register_enable_ble_dtm_command(); |
| 188 | + return ESP_OK; |
| 189 | +} |
0 commit comments