esp_bt.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __ESP_BT_H__
  7. #define __ESP_BT_H__
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include "esp_err.h"
  11. #include "sdkconfig.h"
  12. #include "esp_task.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #ifdef CONFIG_BT_ENABLED
  17. #define SOC_MEM_BT_DATA_START 0x3ffae6e0
  18. #define SOC_MEM_BT_DATA_END 0x3ffaff10
  19. #define SOC_MEM_BT_EM_START 0x3ffb0000
  20. #define SOC_MEM_BT_EM_END 0x3ffb7cd8
  21. #define SOC_MEM_BT_EM_BTDM0_START 0x3ffb0000
  22. #define SOC_MEM_BT_EM_BTDM0_END 0x3ffb09a8
  23. #define SOC_MEM_BT_EM_BLE_START 0x3ffb09a8
  24. #define SOC_MEM_BT_EM_BLE_END 0x3ffb1ddc
  25. #define SOC_MEM_BT_EM_BTDM1_START 0x3ffb1ddc
  26. #define SOC_MEM_BT_EM_BTDM1_END 0x3ffb2730
  27. #define SOC_MEM_BT_EM_BREDR_START 0x3ffb2730
  28. #define SOC_MEM_BT_EM_BREDR_NO_SYNC_END 0x3ffb6388 //Not calculate with synchronize connection support
  29. #define SOC_MEM_BT_EM_BREDR_END 0x3ffb7cd8 //Calculate with synchronize connection support
  30. #define SOC_MEM_BT_EM_SYNC0_START 0x3ffb6388
  31. #define SOC_MEM_BT_EM_SYNC0_END 0x3ffb6bf8
  32. #define SOC_MEM_BT_EM_SYNC1_START 0x3ffb6bf8
  33. #define SOC_MEM_BT_EM_SYNC1_END 0x3ffb7468
  34. #define SOC_MEM_BT_EM_SYNC2_START 0x3ffb7468
  35. #define SOC_MEM_BT_EM_SYNC2_END 0x3ffb7cd8
  36. #define SOC_MEM_BT_BSS_START 0x3ffb8000
  37. #define SOC_MEM_BT_BSS_END 0x3ffb9a20
  38. #define SOC_MEM_BT_MISC_START 0x3ffbdb28
  39. #define SOC_MEM_BT_MISC_END 0x3ffbdb5c
  40. #define SOC_MEM_BT_EM_PER_SYNC_SIZE 0x870
  41. #define SOC_MEM_BT_EM_BREDR_REAL_END (SOC_MEM_BT_EM_BREDR_NO_SYNC_END + CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF * SOC_MEM_BT_EM_PER_SYNC_SIZE)
  42. #endif //CONFIG_BT_ENABLED
  43. #define ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL 0x20200622
  44. /**
  45. * @brief Bluetooth mode for controller enable/disable
  46. */
  47. typedef enum {
  48. ESP_BT_MODE_IDLE = 0x00, /*!< Bluetooth is not running */
  49. ESP_BT_MODE_BLE = 0x01, /*!< Run BLE mode */
  50. ESP_BT_MODE_CLASSIC_BT = 0x02, /*!< Run Classic BT mode */
  51. ESP_BT_MODE_BTDM = 0x03, /*!< Run dual mode */
  52. } esp_bt_mode_t;
  53. /**
  54. * @brief BLE sleep clock accuracy(SCA), values for ble_sca field in esp_bt_controller_config_t,
  55. * currently only ESP_BLE_SCA_500PPM and ESP_BLE_SCA_250PPM are supported
  56. */
  57. enum {
  58. ESP_BLE_SCA_500PPM = 0, /*!< BLE SCA at 500ppm */
  59. ESP_BLE_SCA_250PPM, /*!< BLE SCA at 250ppm */
  60. ESP_BLE_SCA_150PPM, /*!< BLE SCA at 150ppm */
  61. ESP_BLE_SCA_100PPM, /*!< BLE SCA at 100ppm */
  62. ESP_BLE_SCA_75PPM, /*!< BLE SCA at 75ppm */
  63. ESP_BLE_SCA_50PPM, /*!< BLE SCA at 50ppm */
  64. ESP_BLE_SCA_30PPM, /*!< BLE SCA at 30ppm */
  65. ESP_BLE_SCA_20PPM, /*!< BLE SCA at 20ppm */
  66. };
  67. #ifdef CONFIG_BT_ENABLED
  68. /* While scanning, if the free memory value in controller is less than SCAN_SEND_ADV_RESERVED_SIZE,
  69. the adv packet will be discarded until the memory is restored. */
  70. #define SCAN_SEND_ADV_RESERVED_SIZE 1000
  71. /* enable controller log debug when adv lost */
  72. #define CONTROLLER_ADV_LOST_DEBUG_BIT (0<<0)
  73. #ifdef CONFIG_BTDM_CTRL_HCI_UART_NO
  74. #define BT_HCI_UART_NO_DEFAULT CONFIG_BTDM_CTRL_HCI_UART_NO
  75. #else
  76. #define BT_HCI_UART_NO_DEFAULT 1
  77. #endif /* BT_HCI_UART_NO_DEFAULT */
  78. #ifdef CONFIG_BTDM_CTRL_HCI_UART_BAUDRATE
  79. #define BT_HCI_UART_BAUDRATE_DEFAULT CONFIG_BTDM_CTRL_HCI_UART_BAUDRATE
  80. #else
  81. #define BT_HCI_UART_BAUDRATE_DEFAULT 921600
  82. #endif /* BT_HCI_UART_BAUDRATE_DEFAULT */
  83. #ifdef CONFIG_BTDM_SCAN_DUPL_TYPE
  84. #define SCAN_DUPLICATE_TYPE_VALUE CONFIG_BTDM_SCAN_DUPL_TYPE
  85. #else
  86. #define SCAN_DUPLICATE_TYPE_VALUE 0
  87. #endif
  88. /* normal adv cache size */
  89. #ifdef CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE
  90. #define NORMAL_SCAN_DUPLICATE_CACHE_SIZE CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE
  91. #else
  92. #define NORMAL_SCAN_DUPLICATE_CACHE_SIZE 20
  93. #endif
  94. #ifndef CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN
  95. #define CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN FALSE
  96. #endif
  97. #define SCAN_DUPLICATE_MODE_NORMAL_ADV_ONLY 0
  98. #define SCAN_DUPLICATE_MODE_NORMAL_ADV_MESH_ADV 1
  99. #if CONFIG_BTDM_BLE_MESH_SCAN_DUPL_EN
  100. #define SCAN_DUPLICATE_MODE SCAN_DUPLICATE_MODE_NORMAL_ADV_MESH_ADV
  101. #ifdef CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE
  102. #define MESH_DUPLICATE_SCAN_CACHE_SIZE CONFIG_BTDM_MESH_DUPL_SCAN_CACHE_SIZE
  103. #else
  104. #define MESH_DUPLICATE_SCAN_CACHE_SIZE 50
  105. #endif
  106. #else
  107. #define SCAN_DUPLICATE_MODE SCAN_DUPLICATE_MODE_NORMAL_ADV_ONLY
  108. #define MESH_DUPLICATE_SCAN_CACHE_SIZE 0
  109. #endif
  110. #if defined(CONFIG_BTDM_CTRL_MODE_BLE_ONLY)
  111. #define BTDM_CONTROLLER_MODE_EFF ESP_BT_MODE_BLE
  112. #elif defined(CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY)
  113. #define BTDM_CONTROLLER_MODE_EFF ESP_BT_MODE_CLASSIC_BT
  114. #else
  115. #define BTDM_CONTROLLER_MODE_EFF ESP_BT_MODE_BTDM
  116. #endif
  117. #ifdef CONFIG_BTDM_CTRL_AUTO_LATENCY_EFF
  118. #define BTDM_CTRL_AUTO_LATENCY_EFF CONFIG_BTDM_CTRL_AUTO_LATENCY_EFF
  119. #else
  120. #define BTDM_CTRL_AUTO_LATENCY_EFF false
  121. #endif
  122. #ifdef CONFIG_BTDM_CTRL_HLI
  123. #define BTDM_CTRL_HLI CONFIG_BTDM_CTRL_HLI
  124. #else
  125. #define BTDM_CTRL_HLI false
  126. #endif
  127. #ifdef CONFIG_BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF
  128. #define BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF CONFIG_BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF
  129. #else
  130. #define BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF false
  131. #endif
  132. #define BTDM_CONTROLLER_BLE_MAX_CONN_LIMIT 9 //Maximum BLE connection limitation
  133. #define BTDM_CONTROLLER_BR_EDR_MAX_ACL_CONN_LIMIT 7 //Maximum ACL connection limitation
  134. #define BTDM_CONTROLLER_BR_EDR_MAX_SYNC_CONN_LIMIT 3 //Maximum SCO/eSCO connection limitation
  135. #define BTDM_CONTROLLER_SCO_DATA_PATH_HCI 0 // SCO data is routed to HCI
  136. #define BTDM_CONTROLLER_SCO_DATA_PATH_PCM 1 // SCO data path is PCM
  137. #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() { \
  138. .controller_task_stack_size = ESP_TASK_BT_CONTROLLER_STACK, \
  139. .controller_task_prio = ESP_TASK_BT_CONTROLLER_PRIO, \
  140. .hci_uart_no = BT_HCI_UART_NO_DEFAULT, \
  141. .hci_uart_baudrate = BT_HCI_UART_BAUDRATE_DEFAULT, \
  142. .scan_duplicate_mode = SCAN_DUPLICATE_MODE, \
  143. .scan_duplicate_type = SCAN_DUPLICATE_TYPE_VALUE, \
  144. .normal_adv_size = NORMAL_SCAN_DUPLICATE_CACHE_SIZE, \
  145. .mesh_adv_size = MESH_DUPLICATE_SCAN_CACHE_SIZE, \
  146. .send_adv_reserved_size = SCAN_SEND_ADV_RESERVED_SIZE, \
  147. .controller_debug_flag = CONTROLLER_ADV_LOST_DEBUG_BIT, \
  148. .mode = BTDM_CONTROLLER_MODE_EFF, \
  149. .ble_max_conn = CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF, \
  150. .bt_max_acl_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN_EFF, \
  151. .bt_sco_datapath = CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF, \
  152. .auto_latency = BTDM_CTRL_AUTO_LATENCY_EFF, \
  153. .bt_legacy_auth_vs_evt = BTDM_CTRL_LEGACY_AUTH_VENDOR_EVT_EFF, \
  154. .bt_max_sync_conn = CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF, \
  155. .ble_sca = CONFIG_BTDM_BLE_SLEEP_CLOCK_ACCURACY_INDEX_EFF, \
  156. .pcm_role = CONFIG_BTDM_CTRL_PCM_ROLE_EFF, \
  157. .pcm_polar = CONFIG_BTDM_CTRL_PCM_POLAR_EFF, \
  158. .hli = BTDM_CTRL_HLI, \
  159. .magic = ESP_BT_CONTROLLER_CONFIG_MAGIC_VAL, \
  160. };
  161. #else
  162. #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() {0}; _Static_assert(0, "please enable bluetooth in menuconfig to use bt.h");
  163. #endif
  164. /**
  165. * @brief Controller config options, depend on config mask.
  166. * Config mask indicate which functions enabled, this means
  167. * some options or parameters of some functions enabled by config mask.
  168. */
  169. typedef struct {
  170. /*
  171. * Following parameters can be configured runtime, when call esp_bt_controller_init()
  172. */
  173. uint16_t controller_task_stack_size; /*!< Bluetooth controller task stack size */
  174. uint8_t controller_task_prio; /*!< Bluetooth controller task priority */
  175. uint8_t hci_uart_no; /*!< If use UART1/2 as HCI IO interface, indicate UART number */
  176. uint32_t hci_uart_baudrate; /*!< If use UART1/2 as HCI IO interface, indicate UART baudrate */
  177. uint8_t scan_duplicate_mode; /*!< scan duplicate mode */
  178. uint8_t scan_duplicate_type; /*!< scan duplicate type */
  179. uint16_t normal_adv_size; /*!< Normal adv size for scan duplicate */
  180. uint16_t mesh_adv_size; /*!< Mesh adv size for scan duplicate */
  181. uint16_t send_adv_reserved_size; /*!< Controller minimum memory value */
  182. uint32_t controller_debug_flag; /*!< Controller debug log flag */
  183. uint8_t mode; /*!< Controller mode: BR/EDR, BLE or Dual Mode */
  184. uint8_t ble_max_conn; /*!< BLE maximum connection numbers */
  185. uint8_t bt_max_acl_conn; /*!< BR/EDR maximum ACL connection numbers */
  186. uint8_t bt_sco_datapath; /*!< SCO data path, i.e. HCI or PCM module */
  187. bool auto_latency; /*!< BLE auto latency, used to enhance classic BT performance */
  188. bool bt_legacy_auth_vs_evt; /*!< BR/EDR Legacy auth complete event required to protect from BIAS attack */
  189. /*
  190. * Following parameters can not be configured runtime when call esp_bt_controller_init()
  191. * It will be overwrite with a constant value which in menuconfig or from a macro.
  192. * So, do not modify the value when esp_bt_controller_init()
  193. */
  194. uint8_t bt_max_sync_conn; /*!< BR/EDR maximum ACL connection numbers. Effective in menuconfig */
  195. uint8_t ble_sca; /*!< BLE low power crystal accuracy index */
  196. uint8_t pcm_role; /*!< PCM role (master & slave)*/
  197. uint8_t pcm_polar; /*!< PCM polar trig (falling clk edge & rising clk edge) */
  198. bool hli; /*!< Using high level interrupt or not */
  199. uint32_t magic; /*!< Magic number */
  200. } esp_bt_controller_config_t;
  201. /**
  202. * @brief Bluetooth controller enable/disable/initialised/de-initialised status
  203. */
  204. typedef enum {
  205. ESP_BT_CONTROLLER_STATUS_IDLE = 0,
  206. ESP_BT_CONTROLLER_STATUS_INITED,
  207. ESP_BT_CONTROLLER_STATUS_ENABLED,
  208. ESP_BT_CONTROLLER_STATUS_NUM,
  209. } esp_bt_controller_status_t;
  210. /**
  211. * @brief BLE tx power type
  212. * ESP_BLE_PWR_TYPE_CONN_HDL0-8: for each connection, and only be set after connection completed.
  213. * when disconnect, the correspond TX power is not effected.
  214. * ESP_BLE_PWR_TYPE_ADV : for advertising/scan response.
  215. * ESP_BLE_PWR_TYPE_SCAN : for scan.
  216. * ESP_BLE_PWR_TYPE_DEFAULT : if each connection's TX power is not set, it will use this default value.
  217. * if neither in scan mode nor in adv mode, it will use this default value.
  218. * If none of power type is set, system will use ESP_PWR_LVL_P3 as default for ADV/SCAN/CONN0-9.
  219. */
  220. typedef enum {
  221. ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, /*!< For connection handle 0 */
  222. ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, /*!< For connection handle 1 */
  223. ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, /*!< For connection handle 2 */
  224. ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, /*!< For connection handle 3 */
  225. ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, /*!< For connection handle 4 */
  226. ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, /*!< For connection handle 5 */
  227. ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, /*!< For connection handle 6 */
  228. ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, /*!< For connection handle 7 */
  229. ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, /*!< For connection handle 8 */
  230. ESP_BLE_PWR_TYPE_ADV = 9, /*!< For advertising */
  231. ESP_BLE_PWR_TYPE_SCAN = 10, /*!< For scan */
  232. ESP_BLE_PWR_TYPE_DEFAULT = 11, /*!< For default, if not set other, it will use default value */
  233. ESP_BLE_PWR_TYPE_NUM = 12, /*!< TYPE numbers */
  234. } esp_ble_power_type_t;
  235. /**
  236. * @brief Bluetooth TX power level(index), it's just a index corresponding to power(dbm).
  237. */
  238. typedef enum {
  239. ESP_PWR_LVL_N12 = 0, /*!< Corresponding to -12dbm */
  240. ESP_PWR_LVL_N9 = 1, /*!< Corresponding to -9dbm */
  241. ESP_PWR_LVL_N6 = 2, /*!< Corresponding to -6dbm */
  242. ESP_PWR_LVL_N3 = 3, /*!< Corresponding to -3dbm */
  243. ESP_PWR_LVL_N0 = 4, /*!< Corresponding to 0dbm */
  244. ESP_PWR_LVL_P3 = 5, /*!< Corresponding to +3dbm */
  245. ESP_PWR_LVL_P6 = 6, /*!< Corresponding to +6dbm */
  246. ESP_PWR_LVL_P9 = 7, /*!< Corresponding to +9dbm */
  247. ESP_PWR_LVL_N14 = ESP_PWR_LVL_N12, /*!< Backward compatibility! Setting to -14dbm will actually result to -12dbm */
  248. ESP_PWR_LVL_N11 = ESP_PWR_LVL_N9, /*!< Backward compatibility! Setting to -11dbm will actually result to -9dbm */
  249. ESP_PWR_LVL_N8 = ESP_PWR_LVL_N6, /*!< Backward compatibility! Setting to -8dbm will actually result to -6dbm */
  250. ESP_PWR_LVL_N5 = ESP_PWR_LVL_N3, /*!< Backward compatibility! Setting to -5dbm will actually result to -3dbm */
  251. ESP_PWR_LVL_N2 = ESP_PWR_LVL_N0, /*!< Backward compatibility! Setting to -2dbm will actually result to 0dbm */
  252. ESP_PWR_LVL_P1 = ESP_PWR_LVL_P3, /*!< Backward compatibility! Setting to +1dbm will actually result to +3dbm */
  253. ESP_PWR_LVL_P4 = ESP_PWR_LVL_P6, /*!< Backward compatibility! Setting to +4dbm will actually result to +6dbm */
  254. ESP_PWR_LVL_P7 = ESP_PWR_LVL_P9, /*!< Backward compatibility! Setting to +7dbm will actually result to +9dbm */
  255. } esp_power_level_t;
  256. /**
  257. * @brief Bluetooth audio data transport path
  258. */
  259. typedef enum {
  260. ESP_SCO_DATA_PATH_HCI = 0, /*!< data over HCI transport */
  261. ESP_SCO_DATA_PATH_PCM = 1, /*!< data over PCM interface */
  262. } esp_sco_data_path_t;
  263. /**
  264. * @brief Set BLE TX power
  265. * Connection Tx power should only be set after connection created.
  266. * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
  267. * @param power_level: Power level(index) corresponding to absolute value(dbm)
  268. * @return ESP_OK - success, other - failed
  269. */
  270. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level);
  271. /**
  272. * @brief Get BLE TX power
  273. * Connection Tx power should only be get after connection created.
  274. * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
  275. * @return >= 0 - Power level, < 0 - Invalid
  276. */
  277. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type);
  278. /**
  279. * @brief Set BR/EDR TX power
  280. * BR/EDR power control will use the power in range of minimum value and maximum value.
  281. * The power level will effect the global BR/EDR TX power, such inquire, page, connection and so on.
  282. * Please call the function after esp_bt_controller_enable and before any function which cause RF do TX.
  283. * So you can call the function before doing discovery, profile init and so on.
  284. * For example, if you want BR/EDR use the new TX power to do inquire, you should call
  285. * this function before inquire. Another word, If call this function when BR/EDR is in inquire(ING),
  286. * please do inquire again after call this function.
  287. * Default minimum power level is ESP_PWR_LVL_N0, and maximum power level is ESP_PWR_LVL_P3.
  288. * @param min_power_level: The minimum power level
  289. * @param max_power_level: The maximum power level
  290. * @return ESP_OK - success, other - failed
  291. */
  292. esp_err_t esp_bredr_tx_power_set(esp_power_level_t min_power_level, esp_power_level_t max_power_level);
  293. /**
  294. * @brief Get BR/EDR TX power
  295. * If the argument is not NULL, then store the corresponding value.
  296. * @param min_power_level: The minimum power level
  297. * @param max_power_level: The maximum power level
  298. * @return ESP_OK - success, other - failed
  299. */
  300. esp_err_t esp_bredr_tx_power_get(esp_power_level_t *min_power_level, esp_power_level_t *max_power_level);
  301. /**
  302. * @brief Set default SCO data path
  303. * Should be called after controller is enabled, and before (e)SCO link is established
  304. * @param data_path: SCO data path
  305. * @return ESP_OK - success, other - failed
  306. */
  307. esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path);
  308. /**
  309. * @brief Initialize BT controller to allocate task and other resource.
  310. * This function should be called only once, before any other BT functions are called.
  311. * @param cfg: Initial configuration of BT controller. Different from previous version, there's a mode and some
  312. * connection configuration in "cfg" to configure controller work mode and allocate the resource which is needed.
  313. * @return ESP_OK - success, other - failed
  314. */
  315. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg);
  316. /**
  317. * @brief De-initialize BT controller to free resource and delete task.
  318. *
  319. * This function should be called only once, after any other BT functions are called.
  320. * @return ESP_OK - success, other - failed
  321. */
  322. esp_err_t esp_bt_controller_deinit(void);
  323. /**
  324. * @brief Enable BT controller.
  325. * Due to a known issue, you cannot call esp_bt_controller_enable() a second time
  326. * to change the controller mode dynamically. To change controller mode, call
  327. * esp_bt_controller_disable() and then call esp_bt_controller_enable() with the new mode.
  328. * @param mode : the mode(BLE/BT/BTDM) to enable. For compatible of API, retain this argument. This mode must be
  329. * equal as the mode in "cfg" of esp_bt_controller_init().
  330. * @return ESP_OK - success, other - failed
  331. */
  332. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode);
  333. /**
  334. * @brief Disable BT controller
  335. * @return ESP_OK - success, other - failed
  336. */
  337. esp_err_t esp_bt_controller_disable(void);
  338. /**
  339. * @brief Get BT controller is initialised/de-initialised/enabled/disabled
  340. * @return status value
  341. */
  342. esp_bt_controller_status_t esp_bt_controller_get_status(void);
  343. /** @brief esp_vhci_host_callback
  344. * used for vhci call host function to notify what host need to do
  345. */
  346. typedef struct esp_vhci_host_callback {
  347. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  348. 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*/
  349. } esp_vhci_host_callback_t;
  350. /** @brief esp_vhci_host_check_send_available
  351. * used for check actively if the host can send packet to controller or not.
  352. * @return true for ready to send, false means cannot send packet
  353. */
  354. bool esp_vhci_host_check_send_available(void);
  355. /** @brief esp_vhci_host_send_packet
  356. * host send packet to controller
  357. *
  358. * Should not call this function from within a critical section
  359. * or when the scheduler is suspended.
  360. *
  361. * @param data the packet point
  362. * @param len the packet length
  363. */
  364. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len);
  365. /** @brief esp_vhci_host_register_callback
  366. * register the vhci reference callback
  367. * struct defined by vhci_host_callback structure.
  368. * @param callback esp_vhci_host_callback type variable
  369. * @return ESP_OK - success, ESP_FAIL - failed
  370. */
  371. esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback);
  372. /** @brief esp_bt_controller_mem_release
  373. * release the controller memory as per the mode
  374. *
  375. * This function releases the BSS, data and other sections of the controller to heap. The total size is about 70k bytes.
  376. *
  377. * esp_bt_controller_mem_release(mode) should be called only before esp_bt_controller_init()
  378. * or after esp_bt_controller_deinit().
  379. *
  380. * Note that once BT controller memory is released, the process cannot be reversed. It means you cannot use the bluetooth
  381. * mode which you have released by this function.
  382. *
  383. * If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled)
  384. * then do not call this function.
  385. *
  386. * If the app calls esp_bt_controller_enable(ESP_BT_MODE_BLE) to use BLE only then it is safe to call
  387. * esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT) at initialization time to free unused BT Classic memory.
  388. *
  389. * 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,
  390. * which internally calls esp_bt_controller_mem_release(ESP_BT_MODE_BTDM) and additionally releases the BSS and data
  391. * consumed by the BT/BLE host stack to heap. For more details about usage please refer to the documentation of
  392. * esp_bt_mem_release() function
  393. *
  394. * @param mode : the mode want to release memory
  395. * @return ESP_OK - success, other - failed
  396. */
  397. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode);
  398. /** @brief esp_bt_mem_release
  399. * release controller memory and BSS and data section of the BT/BLE host stack as per the mode
  400. *
  401. * This function first releases controller memory by internally calling esp_bt_controller_mem_release().
  402. * 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
  403. *
  404. * Note that once BT memory is released, the process cannot be reversed. It means you cannot use the bluetooth
  405. * mode which you have released by this function.
  406. *
  407. * If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled)
  408. * then do not call this function.
  409. *
  410. * If you never intend to use bluetooth in a current boot-up cycle, you can call esp_bt_mem_release(ESP_BT_MODE_BTDM)
  411. * before esp_bt_controller_init or after esp_bt_controller_deinit.
  412. *
  413. * 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".
  414. * In such cases, after receiving the WiFi configuration, you can disable/deinit bluetooth and release its memory.
  415. * Below is the sequence of APIs to be called for such scenarios:
  416. *
  417. * esp_bluedroid_disable();
  418. * esp_bluedroid_deinit();
  419. * esp_bt_controller_disable();
  420. * esp_bt_controller_deinit();
  421. * esp_bt_mem_release(ESP_BT_MODE_BTDM);
  422. *
  423. * @note In case of NimBLE host, to release BSS and data memory to heap, the mode needs to be
  424. * set to ESP_BT_MODE_BTDM as controller is dual mode.
  425. * @param mode : the mode whose memory is to be released
  426. * @return ESP_OK - success, other - failed
  427. */
  428. esp_err_t esp_bt_mem_release(esp_bt_mode_t mode);
  429. /**
  430. * @brief enable bluetooth to enter modem sleep
  431. *
  432. * Note that this function shall not be invoked before esp_bt_controller_enable()
  433. *
  434. * There are currently two options for bluetooth modem sleep, one is ORIG mode, and another is EVED Mode. EVED Mode is intended for BLE only.
  435. *
  436. * For ORIG mode:
  437. * Bluetooth modem sleep is enabled in controller start up by default if CONFIG_CTRL_BTDM_MODEM_SLEEP is set and "ORIG mode" is selected. In ORIG modem sleep mode, bluetooth controller will switch off some components and pause to work every now and then, if there is no event to process; and wakeup according to the scheduled interval and resume the work. It can also wakeup earlier upon external request using function "esp_bt_controller_wakeup_request".
  438. *
  439. * @return
  440. * - ESP_OK : success
  441. * - other : failed
  442. */
  443. esp_err_t esp_bt_sleep_enable(void);
  444. /**
  445. * @brief disable bluetooth modem sleep
  446. *
  447. * Note that this function shall not be invoked before esp_bt_controller_enable()
  448. *
  449. * If esp_bt_sleep_disable() is called, bluetooth controller will not be allowed to enter modem sleep;
  450. *
  451. * If ORIG modem sleep mode is in use, if this function is called, bluetooth controller may not immediately wake up if it is dormant then.
  452. * In this case, esp_bt_controller_wakeup_request() can be used to shorten the time for wakeup.
  453. *
  454. * @return
  455. * - ESP_OK : success
  456. * - other : failed
  457. */
  458. esp_err_t esp_bt_sleep_disable(void);
  459. /**
  460. * @brief Manually clear scan duplicate list
  461. *
  462. * Note that scan duplicate list will be automatically cleared when the maximum amount of device in the filter is reached
  463. * the amount of device in the filter can be configured in menuconfig.
  464. *
  465. * @note This function name is incorrectly spelled, it will be fixed in release 5.x version.
  466. *
  467. * @return
  468. * - ESP_OK : success
  469. * - other : failed
  470. */
  471. esp_err_t esp_ble_scan_dupilcate_list_flush(void);
  472. /**
  473. * @brief bt Wi-Fi power domain power on
  474. */
  475. void esp_wifi_bt_power_domain_on(void);
  476. /**
  477. * @brief bt Wi-Fi power domain power off
  478. */
  479. void esp_wifi_bt_power_domain_off(void);
  480. #ifdef __cplusplus
  481. }
  482. #endif
  483. #endif /* __ESP_BT_H__ */