1 /* 2 * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef __ESP_BT_H__ 8 #define __ESP_BT_H__ 9 10 #include <stdint.h> 11 #include <stdbool.h> 12 #include "esp_err.h" 13 #include "sdkconfig.h" 14 #include "esp_task.h" 15 16 #include "nimble/nimble_npl.h" 17 #include "esp_bt_cfg.h" 18 19 #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART 20 #include "driver/uart.h" 21 #endif 22 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** 29 * @brief Bluetooth mode for controller enable/disable 30 */ 31 32 typedef enum { 33 ESP_BT_MODE_IDLE = 0x00, /*!< Bluetooth is not running */ 34 ESP_BT_MODE_BLE = 0x01, /*!< Run BLE mode */ 35 ESP_BT_MODE_CLASSIC_BT = 0x02, /*!< Run Classic BT mode */ 36 ESP_BT_MODE_BTDM = 0x03, /*!< Run dual mode */ 37 } esp_bt_mode_t; 38 39 /** 40 * @brief Bluetooth controller enable/disable/initialised/de-initialised status 41 */ 42 43 typedef enum { 44 ESP_BT_CONTROLLER_STATUS_IDLE = 0, /*!< Controller is in idle state */ 45 ESP_BT_CONTROLLER_STATUS_INITED, /*!< Controller is in initialising state */ 46 ESP_BT_CONTROLLER_STATUS_ENABLED, /*!< Controller is in enabled state */ 47 ESP_BT_CONTROLLER_STATUS_NUM, /*!< Controller is in disabled state */ 48 } esp_bt_controller_status_t; 49 50 /** 51 * @brief BLE tx power type 52 * ESP_BLE_PWR_TYPE_CONN_HDL0-8: for each connection, and only be set after connection completed. 53 * when disconnect, the correspond TX power is not effected. 54 * ESP_BLE_PWR_TYPE_ADV : for advertising/scan response. 55 * ESP_BLE_PWR_TYPE_SCAN : for scan. 56 * ESP_BLE_PWR_TYPE_DEFAULT : if each connection's TX power is not set, it will use this default value. 57 * if neither in scan mode nor in adv mode, it will use this default value. 58 * If none of power type is set, system will use ESP_PWR_LVL_P3 as default for ADV/SCAN/CONN0-9. 59 */ 60 typedef enum { 61 ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, /*!< For connection handle 0 */ 62 ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, /*!< For connection handle 1 */ 63 ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, /*!< For connection handle 2 */ 64 ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, /*!< For connection handle 3 */ 65 ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, /*!< For connection handle 4 */ 66 ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, /*!< For connection handle 5 */ 67 ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, /*!< For connection handle 6 */ 68 ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, /*!< For connection handle 7 */ 69 ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, /*!< For connection handle 8 */ 70 ESP_BLE_PWR_TYPE_ADV = 9, /*!< For advertising */ 71 ESP_BLE_PWR_TYPE_SCAN = 10, /*!< For scan */ 72 ESP_BLE_PWR_TYPE_DEFAULT = 11, /*!< For default, if not set other, it will use default value */ 73 ESP_BLE_PWR_TYPE_NUM = 12, /*!< TYPE numbers */ 74 } esp_ble_power_type_t; 75 76 /** 77 * @brief Bluetooth TX power level(index), it's just a index corresponding to power(dbm). 78 */ 79 typedef enum { 80 ESP_PWR_LVL_N27 = 0, /*!< Corresponding to -27dbm */ 81 ESP_PWR_LVL_N24 = 1, /*!< Corresponding to -24dbm */ 82 ESP_PWR_LVL_N21 = 2, /*!< Corresponding to -21dbm */ 83 ESP_PWR_LVL_N18 = 3, /*!< Corresponding to -18dbm */ 84 ESP_PWR_LVL_N15 = 4, /*!< Corresponding to -15dbm */ 85 ESP_PWR_LVL_N12 = 5, /*!< Corresponding to -12dbm */ 86 ESP_PWR_LVL_N9 = 6, /*!< Corresponding to -9dbm */ 87 ESP_PWR_LVL_N6 = 7, /*!< Corresponding to -6dbm */ 88 ESP_PWR_LVL_N3 = 8, /*!< Corresponding to -3dbm */ 89 ESP_PWR_LVL_N0 = 9, /*!< Corresponding to 0dbm */ 90 ESP_PWR_LVL_P3 = 10, /*!< Corresponding to +3dbm */ 91 ESP_PWR_LVL_P6 = 11, /*!< Corresponding to +6dbm */ 92 ESP_PWR_LVL_P9 = 12, /*!< Corresponding to +9dbm */ 93 ESP_PWR_LVL_P12 = 13, /*!< Corresponding to +12dbm */ 94 ESP_PWR_LVL_P15 = 14, /*!< Corresponding to +15dbm */ 95 ESP_PWR_LVL_P18 = 15, /*!< Corresponding to +18dbm */ 96 ESP_PWR_LVL_INVALID = 0xFF, /*!< Indicates an invalid value */ 97 } esp_power_level_t; 98 99 typedef enum { 100 ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT = 0, 101 ESP_BLE_ENHANCED_PWR_TYPE_ADV, 102 ESP_BLE_ENHANCED_PWR_TYPE_SCAN, 103 ESP_BLE_ENHANCED_PWR_TYPE_INIT, 104 ESP_BLE_ENHANCED_PWR_TYPE_CONN, 105 ESP_BLE_ENHANCED_PWR_TYPE_MAX, 106 } esp_ble_enhanced_power_type_t; 107 108 typedef struct { 109 uint8_t type; 110 uint8_t val[6]; 111 } esp_ble_addr_t; 112 113 /** 114 * @brief Set BLE TX power 115 * Connection Tx power should only be set after connection created. 116 * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc 117 * @param power_level: Power level(index) corresponding to absolute value(dbm) 118 * @return ESP_OK - success, other - failed 119 */ 120 esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level); 121 122 /** 123 * @brief Get BLE TX power 124 * Connection Tx power should only be get after connection created. 125 * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc 126 * @return >= 0 - Power level, < 0 - Invalid 127 */ 128 esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type); 129 130 131 /** 132 * @brief ENHANCED API for Setting BLE TX power 133 * Connection Tx power should only be set after connection created. 134 * @param power_type : The enhanced type of which tx power, could set Advertising/Connection/Default and etc 135 * @param handle : The handle of Advertising or Connection and the value 0 for other enhanced power types. 136 * @param power_level: Power level(index) corresponding to absolute value(dbm) 137 * @return ESP_OK - success, other - failed 138 */ 139 esp_err_t esp_ble_tx_power_set_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle, esp_power_level_t power_level); 140 141 /** 142 * @brief ENHANCED API of Getting BLE TX power 143 * Connection Tx power should only be get after connection created. 144 * @param power_type : The enhanced type of which tx power, could set Advertising/Connection/Default and etc 145 * @param handle : The handle of Advertising or Connection and the value 0 for other enhanced power types. 146 * @return >= 0 - Power level, < 0 - Invalid 147 */ 148 esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle); 149 150 #define CONFIG_VERSION 0x20220824 151 #define CONFIG_MAGIC 0x5A5AA5A5 152 153 /** 154 * @brief Controller config options, depend on config mask. 155 * Config mask indicate which functions enabled, this means 156 * some options or parameters of some functions enabled by config mask. 157 */ 158 159 typedef struct { 160 uint32_t config_version; 161 uint16_t ble_ll_resolv_list_size; 162 uint16_t ble_hci_evt_hi_buf_count; 163 uint16_t ble_hci_evt_lo_buf_count; 164 uint8_t ble_ll_sync_list_cnt; 165 uint8_t ble_ll_sync_cnt; 166 uint16_t ble_ll_rsp_dup_list_count; 167 uint16_t ble_ll_adv_dup_list_count; 168 uint8_t ble_ll_tx_pwr_dbm; 169 uint64_t rtc_freq; 170 uint16_t ble_ll_sca; 171 uint8_t ble_ll_scan_phy_number; 172 uint16_t ble_ll_conn_def_auth_pyld_tmo; 173 uint8_t ble_ll_jitter_usecs; 174 uint16_t ble_ll_sched_max_adv_pdu_usecs; 175 uint16_t ble_ll_sched_direct_adv_max_usecs; 176 uint16_t ble_ll_sched_adv_max_usecs; 177 uint16_t ble_scan_rsp_data_max_len; 178 uint8_t ble_ll_cfg_num_hci_cmd_pkts; 179 uint32_t ble_ll_ctrl_proc_timeout_ms; 180 uint16_t nimble_max_connections; 181 uint8_t ble_whitelist_size; 182 uint16_t ble_acl_buf_size; 183 uint16_t ble_acl_buf_count; 184 uint16_t ble_hci_evt_buf_size; 185 uint16_t ble_multi_adv_instances; 186 uint16_t ble_ext_adv_max_size; 187 uint16_t controller_task_stack_size; 188 uint8_t controller_task_prio; 189 uint8_t controller_run_cpu; 190 uint8_t enable_qa_test; 191 uint8_t enable_bqb_test; 192 uint8_t enable_uart_hci; 193 uint8_t ble_hci_uart_port; 194 uint32_t ble_hci_uart_baud; 195 uint8_t ble_hci_uart_data_bits; 196 uint8_t ble_hci_uart_stop_bits; 197 uint8_t ble_hci_uart_flow_ctrl; 198 uint8_t ble_hci_uart_uart_parity; 199 uint8_t enable_tx_cca; 200 uint8_t cca_rssi_thresh; 201 uint8_t sleep_en; 202 uint8_t coex_phy_coded_tx_rx_time_limit; 203 uint8_t dis_scan_backoff; 204 uint8_t ble_scan_classify_filter_enable; 205 uint8_t cca_drop_mode; 206 int8_t cca_low_tx_pwr; 207 uint8_t main_xtal_freq; 208 uint32_t config_magic; 209 } esp_bt_controller_config_t; 210 211 212 #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() { \ 213 .config_version = CONFIG_VERSION, \ 214 .ble_ll_resolv_list_size = CONFIG_BT_LE_LL_RESOLV_LIST_SIZE, \ 215 .ble_hci_evt_hi_buf_count = DEFAULT_BT_LE_HCI_EVT_HI_BUF_COUNT, \ 216 .ble_hci_evt_lo_buf_count = DEFAULT_BT_LE_HCI_EVT_LO_BUF_COUNT, \ 217 .ble_ll_sync_list_cnt = DEFAULT_BT_LE_MAX_PERIODIC_ADVERTISER_LIST, \ 218 .ble_ll_sync_cnt = DEFAULT_BT_LE_MAX_PERIODIC_SYNCS, \ 219 .ble_ll_rsp_dup_list_count = CONFIG_BT_LE_LL_DUP_SCAN_LIST_COUNT, \ 220 .ble_ll_adv_dup_list_count = CONFIG_BT_LE_LL_DUP_SCAN_LIST_COUNT, \ 221 .ble_ll_tx_pwr_dbm = BLE_LL_TX_PWR_DBM_N, \ 222 .rtc_freq = RTC_FREQ_N, \ 223 .ble_ll_sca = CONFIG_BT_LE_LL_SCA, \ 224 .ble_ll_scan_phy_number = BLE_LL_SCAN_PHY_NUMBER_N, \ 225 .ble_ll_conn_def_auth_pyld_tmo = BLE_LL_CONN_DEF_AUTH_PYLD_TMO_N, \ 226 .ble_ll_jitter_usecs = BLE_LL_JITTER_USECS_N, \ 227 .ble_ll_sched_max_adv_pdu_usecs = BLE_LL_SCHED_MAX_ADV_PDU_USECS_N, \ 228 .ble_ll_sched_direct_adv_max_usecs = BLE_LL_SCHED_DIRECT_ADV_MAX_USECS_N, \ 229 .ble_ll_sched_adv_max_usecs = BLE_LL_SCHED_ADV_MAX_USECS_N, \ 230 .ble_scan_rsp_data_max_len = DEFAULT_BT_LE_SCAN_RSP_DATA_MAX_LEN_N, \ 231 .ble_ll_cfg_num_hci_cmd_pkts = BLE_LL_CFG_NUM_HCI_CMD_PKTS_N, \ 232 .ble_ll_ctrl_proc_timeout_ms = BLE_LL_CTRL_PROC_TIMEOUT_MS_N, \ 233 .nimble_max_connections = DEFAULT_BT_LE_MAX_CONNECTIONS, \ 234 .ble_whitelist_size = DEFAULT_BT_NIMBLE_WHITELIST_SIZE, \ 235 .ble_acl_buf_size = DEFAULT_BT_LE_ACL_BUF_SIZE, \ 236 .ble_acl_buf_count = DEFAULT_BT_LE_ACL_BUF_COUNT, \ 237 .ble_hci_evt_buf_size = DEFAULT_BT_LE_HCI_EVT_BUF_SIZE, \ 238 .ble_multi_adv_instances = DEFAULT_BT_LE_MAX_EXT_ADV_INSTANCES, \ 239 .ble_ext_adv_max_size = DEFAULT_BT_LE_EXT_ADV_MAX_SIZE, \ 240 .controller_task_stack_size = NIMBLE_LL_STACK_SIZE, \ 241 .controller_task_prio = ESP_TASK_BT_CONTROLLER_PRIO, \ 242 .controller_run_cpu = 0, \ 243 .enable_qa_test = RUN_QA_TEST, \ 244 .enable_bqb_test = RUN_BQB_TEST, \ 245 .enable_uart_hci = HCI_UART_EN, \ 246 .ble_hci_uart_port = DEFAULT_BT_LE_HCI_UART_PORT, \ 247 .ble_hci_uart_baud = DEFAULT_BT_LE_HCI_UART_BAUD, \ 248 .ble_hci_uart_data_bits = DEFAULT_BT_LE_HCI_UART_DATA_BITS, \ 249 .ble_hci_uart_stop_bits = DEFAULT_BT_LE_HCI_UART_STOP_BITS, \ 250 .ble_hci_uart_flow_ctrl = DEFAULT_BT_LE_HCI_UART_FLOW_CTRL, \ 251 .ble_hci_uart_uart_parity = DEFAULT_BT_LE_HCI_UART_PARITY, \ 252 .enable_tx_cca = DEFAULT_BT_LE_TX_CCA_ENABLED, \ 253 .cca_rssi_thresh = 256 - DEFAULT_BT_LE_CCA_RSSI_THRESH, \ 254 .cca_drop_mode = 0, \ 255 .cca_low_tx_pwr = 0, \ 256 .sleep_en = NIMBLE_SLEEP_ENABLE, \ 257 .coex_phy_coded_tx_rx_time_limit = DEFAULT_BT_LE_COEX_PHY_CODED_TX_RX_TLIM_EFF, \ 258 .ble_scan_classify_filter_enable = 0, \ 259 .main_xtal_freq = CONFIG_XTAL_FREQ, \ 260 .config_magic = CONFIG_MAGIC, \ 261 } 262 263 esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg); 264 265 /** 266 * @brief Get BT controller is initialised/de-initialised/enabled/disabled 267 * @return status value 268 */ 269 esp_bt_controller_status_t esp_bt_controller_get_status(void); 270 esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type); 271 esp_err_t esp_bt_controller_deinit(void); 272 esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode); 273 esp_err_t esp_bt_controller_disable(void); 274 275 typedef struct esp_vhci_host_callback { 276 void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */ 277 int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/ 278 } esp_vhci_host_callback_t; 279 280 /** @brief esp_vhci_host_check_send_available 281 * used for check actively if the host can send packet to controller or not. 282 * @return true for ready to send, false means cannot send packet 283 */ 284 bool esp_vhci_host_check_send_available(void); 285 286 /** @brief esp_vhci_host_send_packet 287 * host send packet to controller 288 * 289 * Should not call this function from within a critical section 290 * or when the scheduler is suspended. 291 * 292 * @param data the packet point 293 * @param len the packet length 294 */ 295 void esp_vhci_host_send_packet(uint8_t *data, uint16_t len); 296 297 /** @brief esp_vhci_host_register_callback 298 * register the vhci reference callback 299 * struct defined by vhci_host_callback structure. 300 * @param callback esp_vhci_host_callback type variable 301 * @return ESP_OK - success, ESP_FAIL - failed 302 */ 303 esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback); 304 305 /** @brief esp_bt_controller_mem_release 306 * release the controller memory as per the mode 307 * 308 * This function releases the BSS, data and other sections of the controller to heap. The total size is about 70k bytes. 309 * 310 * esp_bt_controller_mem_release(mode) should be called only before esp_bt_controller_init() 311 * or after esp_bt_controller_deinit(). 312 * 313 * Note that once BT controller memory is released, the process cannot be reversed. It means you cannot use the bluetooth 314 * mode which you have released by this function. 315 * 316 * If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled) 317 * then do not call this function. 318 * 319 * If the app calls esp_bt_controller_enable(ESP_BT_MODE_BLE) to use BLE only then it is safe to call 320 * esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT) at initialization time to free unused BT Classic memory. 321 * 322 * If the mode is ESP_BT_MODE_BTDM, then it may be useful to call API esp_bt_mem_release(ESP_BT_MODE_BTDM) instead, 323 * which internally calls esp_bt_controller_mem_release(ESP_BT_MODE_BTDM) and additionally releases the BSS and data 324 * consumed by the BT/BLE host stack to heap. For more details about usage please refer to the documentation of 325 * esp_bt_mem_release() function 326 * 327 * @param mode : the mode want to release memory 328 * @return ESP_OK - success, other - failed 329 */ 330 esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode); 331 332 /** @brief esp_bt_mem_release 333 * release controller memory and BSS and data section of the BT/BLE host stack as per the mode 334 * 335 * This function first releases controller memory by internally calling esp_bt_controller_mem_release(). 336 * Additionally, if the mode is set to ESP_BT_MODE_BTDM, it also releases the BSS and data consumed by the BT/BLE host stack to heap 337 * 338 * Note that once BT memory is released, the process cannot be reversed. It means you cannot use the bluetooth 339 * mode which you have released by this function. 340 * 341 * If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled) 342 * then do not call this function. 343 * 344 * If you never intend to use bluetooth in a current boot-up cycle, you can call esp_bt_mem_release(ESP_BT_MODE_BTDM) 345 * before esp_bt_controller_init or after esp_bt_controller_deinit. 346 * 347 * For example, if a user only uses bluetooth for setting the WiFi configuration, and does not use bluetooth in the rest of the product operation". 348 * In such cases, after receiving the WiFi configuration, you can disable/deinit bluetooth and release its memory. 349 * Below is the sequence of APIs to be called for such scenarios: 350 * 351 * esp_bluedroid_disable(); 352 * esp_bluedroid_deinit(); 353 * esp_bt_controller_disable(); 354 * esp_bt_controller_deinit(); 355 * esp_bt_mem_release(ESP_BT_MODE_BTDM); 356 * 357 * @param mode : the mode whose memory is to be released 358 * @return ESP_OK - success, other - failed 359 */ 360 esp_err_t esp_bt_mem_release(esp_bt_mode_t mode); 361 362 /* Returns random static address or -1 if not present */ 363 extern int esp_ble_hw_get_static_addr(esp_ble_addr_t *addr); 364 365 #ifdef __cplusplus 366 } 367 #endif 368 369 #endif /* __ESP_BT_H__ */ 370