esp_bt.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_BT_H__
  14. #define __ESP_BT_H__
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "esp_err.h"
  18. #include "sdkconfig.h"
  19. #include "esp_task.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /**
  24. * @brief Controller config options, depend on config mask.
  25. * Config mask indicate which functions enabled, this means
  26. * some options or parameters of some functions enabled by config mask.
  27. */
  28. typedef struct {
  29. uint16_t controller_task_stack_size; /*!< Bluetooth controller task stack size */
  30. uint8_t controller_task_prio; /*!< Bluetooth controller task priority */
  31. uint8_t hci_uart_no; /*!< If use UART1/2 as HCI IO interface, indicate UART number */
  32. uint32_t hci_uart_baudrate; /*!< If use UART1/2 as HCI IO interface, indicate UART baudrate */
  33. uint8_t scan_duplicate_mode; /*!< If use UART1/2 as HCI IO interface, indicate UART baudrate */
  34. uint16_t normal_adv_size; /*!< Normal adv size for scan duplicate */
  35. uint16_t mesh_adv_size; /*!< Mesh adv size for scan duplicate */
  36. uint16_t send_adv_reserved_size; /*!< Controller minimum memory value */
  37. uint32_t controller_debug_flag; /*!< Controller debug log flag */
  38. } esp_bt_controller_config_t;
  39. #ifdef CONFIG_BT_ENABLED
  40. /* While scanning, if the free memory value in controller is less than SCAN_SEND_ADV_RESERVED_SIZE,
  41. the adv packet will be discarded until the memory is restored. */
  42. #define SCAN_SEND_ADV_RESERVED_SIZE 1000
  43. /* enable controller log debug when adv lost */
  44. #define CONTROLLER_ADV_LOST_DEBUG_BIT (0<<0)
  45. #ifdef CONFIG_BT_HCI_UART_NO
  46. #define BT_HCI_UART_NO_DEFAULT CONFIG_BT_HCI_UART_NO
  47. #else
  48. #define BT_HCI_UART_NO_DEFAULT 1
  49. #endif /* BT_HCI_UART_NO_DEFAULT */
  50. #ifdef CONFIG_BT_HCI_UART_BAUDRATE
  51. #define BT_HCI_UART_BAUDRATE_DEFAULT CONFIG_BT_HCI_UART_BAUDRATE
  52. #else
  53. #define BT_HCI_UART_BAUDRATE_DEFAULT 921600
  54. #endif /* BT_HCI_UART_BAUDRATE_DEFAULT */
  55. /* normal adv cache size */
  56. #ifdef CONFIG_DUPLICATE_SCAN_CACHE_SIZE
  57. #define NORMAL_SCAN_DUPLICATE_CACHE_SIZE CONFIG_DUPLICATE_SCAN_CACHE_SIZE
  58. #else
  59. #define NORMAL_SCAN_DUPLICATE_CACHE_SIZE 20
  60. #endif
  61. #ifndef CONFIG_BLE_MESH_SCAN_DUPLICATE_EN
  62. #define CONFIG_BLE_MESH_SCAN_DUPLICATE_EN FALSE
  63. #endif
  64. #define SCAN_DUPLICATE_MODE_NORMAL_ADV_ONLY 0
  65. #define SCAN_DUPLICATE_MODE_NORMAL_ADV_MESH_ADV 1
  66. #if CONFIG_BLE_MESH_SCAN_DUPLICATE_EN
  67. #define SCAN_DUPLICATE_MODE SCAN_DUPLICATE_MODE_NORMAL_ADV_MESH_ADV
  68. #ifdef CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE
  69. #define MESH_DUPLICATE_SCAN_CACHE_SIZE CONFIG_MESH_DUPLICATE_SCAN_CACHE_SIZE
  70. #else
  71. #define MESH_DUPLICATE_SCAN_CACHE_SIZE 50
  72. #endif
  73. #else
  74. #define SCAN_DUPLICATE_MODE SCAN_DUPLICATE_MODE_NORMAL_ADV_ONLY
  75. #define MESH_DUPLICATE_SCAN_CACHE_SIZE 0
  76. #endif
  77. #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() { \
  78. .controller_task_stack_size = ESP_TASK_BT_CONTROLLER_STACK, \
  79. .controller_task_prio = ESP_TASK_BT_CONTROLLER_PRIO, \
  80. .hci_uart_no = BT_HCI_UART_NO_DEFAULT, \
  81. .hci_uart_baudrate = BT_HCI_UART_BAUDRATE_DEFAULT, \
  82. .scan_duplicate_mode = SCAN_DUPLICATE_MODE, \
  83. .normal_adv_size = NORMAL_SCAN_DUPLICATE_CACHE_SIZE, \
  84. .mesh_adv_size = MESH_DUPLICATE_SCAN_CACHE_SIZE, \
  85. .send_adv_reserved_size = SCAN_SEND_ADV_RESERVED_SIZE, \
  86. .controller_debug_flag = CONTROLLER_ADV_LOST_DEBUG_BIT, \
  87. };
  88. #else
  89. #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() {0}; _Static_assert(0, "please enable bluetooth in menuconfig to use bt.h");
  90. #endif
  91. /**
  92. * @brief Bluetooth mode for controller enable/disable
  93. */
  94. typedef enum {
  95. ESP_BT_MODE_IDLE = 0x00, /*!< Bluetooth is not running */
  96. ESP_BT_MODE_BLE = 0x01, /*!< Run BLE mode */
  97. ESP_BT_MODE_CLASSIC_BT = 0x02, /*!< Run Classic BT mode */
  98. ESP_BT_MODE_BTDM = 0x03, /*!< Run dual mode */
  99. } esp_bt_mode_t;
  100. /**
  101. * @brief Bluetooth controller enable/disable/initialised/de-initialised status
  102. */
  103. typedef enum {
  104. ESP_BT_CONTROLLER_STATUS_IDLE = 0,
  105. ESP_BT_CONTROLLER_STATUS_INITED,
  106. ESP_BT_CONTROLLER_STATUS_ENABLED,
  107. ESP_BT_CONTROLLER_STATUS_NUM,
  108. } esp_bt_controller_status_t;
  109. /**
  110. * @brief BLE tx power type
  111. * ESP_BLE_PWR_TYPE_CONN_HDL0-8: for each connection, and only be set after connection completed.
  112. * when disconnect, the correspond TX power is not effected.
  113. * ESP_BLE_PWR_TYPE_ADV : for advertising/scan response.
  114. * ESP_BLE_PWR_TYPE_SCAN : for scan.
  115. * ESP_BLE_PWR_TYPE_DEFAULT : if each connection's TX power is not set, it will use this default value.
  116. * if neither in scan mode nor in adv mode, it will use this default value.
  117. * If none of power type is set, system will use ESP_PWR_LVL_P1 as default for ADV/SCAN/CONN0-9.
  118. */
  119. typedef enum {
  120. ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, /*!< For connection handle 0 */
  121. ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, /*!< For connection handle 1 */
  122. ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, /*!< For connection handle 2 */
  123. ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, /*!< For connection handle 3 */
  124. ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, /*!< For connection handle 4 */
  125. ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, /*!< For connection handle 5 */
  126. ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, /*!< For connection handle 6 */
  127. ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, /*!< For connection handle 7 */
  128. ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, /*!< For connection handle 8 */
  129. ESP_BLE_PWR_TYPE_ADV = 9, /*!< For advertising */
  130. ESP_BLE_PWR_TYPE_SCAN = 10, /*!< For scan */
  131. ESP_BLE_PWR_TYPE_DEFAULT = 11, /*!< For default, if not set other, it will use default value */
  132. ESP_BLE_PWR_TYPE_NUM = 12, /*!< TYPE numbers */
  133. } esp_ble_power_type_t;
  134. /**
  135. * @brief Bluetooth TX power level(index), it's just a index corresponding to power(dbm).
  136. */
  137. typedef enum {
  138. ESP_PWR_LVL_N14 = 0, /*!< Corresponding to -14dbm */
  139. ESP_PWR_LVL_N11 = 1, /*!< Corresponding to -11dbm */
  140. ESP_PWR_LVL_N8 = 2, /*!< Corresponding to -8dbm */
  141. ESP_PWR_LVL_N5 = 3, /*!< Corresponding to -5dbm */
  142. ESP_PWR_LVL_N2 = 4, /*!< Corresponding to -2dbm */
  143. ESP_PWR_LVL_P1 = 5, /*!< Corresponding to 1dbm */
  144. ESP_PWR_LVL_P4 = 6, /*!< Corresponding to 4dbm */
  145. ESP_PWR_LVL_P7 = 7, /*!< Corresponding to 7dbm */
  146. } esp_power_level_t;
  147. /**
  148. * @brief Bluetooth audio data transport path
  149. */
  150. typedef enum {
  151. ESP_SCO_DATA_PATH_HCI = 0, /*!< data over HCI transport */
  152. ESP_SCO_DATA_PATH_PCM = 1, /*!< data over PCM interface */
  153. } esp_sco_data_path_t;
  154. /**
  155. * @brief Set BLE TX power
  156. * Connection Tx power should only be set after connection created.
  157. * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
  158. * @param power_level: Power level(index) corresponding to absolute value(dbm)
  159. * @return ESP_OK - success, other - failed
  160. */
  161. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level);
  162. /**
  163. * @brief Get BLE TX power
  164. * Connection Tx power should only be get after connection created.
  165. * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
  166. * @return >= 0 - Power level, < 0 - Invalid
  167. */
  168. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type);
  169. /**
  170. * @brief Set BR/EDR TX power
  171. * BR/EDR power control will use the power in range of minimum value and maximum value.
  172. * The power level will effect the global BR/EDR TX power, such inquire, page, connection and so on.
  173. * Please call the function after esp_bt_controller_enable and before any function which cause RF do TX.
  174. * So you can call the function can before do discover, beofre profile init and so on.
  175. * For example, if you want BR/EDR use the new TX power to do inquire, you should call
  176. * this function before inquire. Another word, If call this function when BR/EDR is in inquire(ING),
  177. * please do inquire again after call this function.
  178. * Default minimum power level is ESP_PWR_LVL_N2, and maximum power level is ESP_PWR_LVL_P1.
  179. * @param min_power_level: The minimum power level
  180. * @param max_power_level: The maximum power level
  181. * @return ESP_OK - success, other - failed
  182. */
  183. esp_err_t esp_bredr_tx_power_set(esp_power_level_t min_power_level, esp_power_level_t max_power_level);
  184. /**
  185. * @brief Get BR/EDR TX power
  186. * If the argument is not NULL, then store the corresponding value.
  187. * @param min_power_level: The minimum power level
  188. * @param max_power_level: The maximum power level
  189. * @return ESP_OK - success, other - failed
  190. */
  191. esp_err_t esp_bredr_tx_power_get(esp_power_level_t *min_power_level, esp_power_level_t *max_power_level);
  192. /**
  193. * @brief set default SCO data path
  194. * Should be called after controller is enabled, and before (e)SCO link is established
  195. * @param data_path: SCO data path
  196. * @return ESP_OK - success, other - failed
  197. */
  198. esp_err_t esp_bredr_sco_datapath_set(esp_sco_data_path_t data_path);
  199. /**
  200. * @brief Initialize BT controller to allocate task and other resource.
  201. * @param cfg: Initial configuration of BT controller.
  202. * This function should be called only once, before any other BT functions are called.
  203. * @return ESP_OK - success, other - failed
  204. */
  205. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg);
  206. /**
  207. * @brief De-initialize BT controller to free resource and delete task.
  208. *
  209. * This function should be called only once, after any other BT functions are called.
  210. * This function is not whole completed, esp_bt_controller_init cannot called after this function.
  211. * @return ESP_OK - success, other - failed
  212. */
  213. esp_err_t esp_bt_controller_deinit(void);
  214. /**
  215. * @brief Enable BT controller.
  216. * Due to a known issue, you cannot call esp_bt_controller_enable() a second time
  217. * to change the controller mode dynamically. To change controller mode, call
  218. * esp_bt_controller_disable() and then call esp_bt_controller_enable() with the new mode.
  219. * @param mode : the mode(BLE/BT/BTDM) to enable.
  220. * @return ESP_OK - success, other - failed
  221. */
  222. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode);
  223. /**
  224. * @brief Disable BT controller
  225. * @return ESP_OK - success, other - failed
  226. */
  227. esp_err_t esp_bt_controller_disable(void);
  228. /**
  229. * @brief Get BT controller is initialised/de-initialised/enabled/disabled
  230. * @return status value
  231. */
  232. esp_bt_controller_status_t esp_bt_controller_get_status(void);
  233. /** @brief esp_vhci_host_callback
  234. * used for vhci call host function to notify what host need to do
  235. */
  236. typedef struct esp_vhci_host_callback {
  237. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  238. 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*/
  239. } esp_vhci_host_callback_t;
  240. /** @brief esp_vhci_host_check_send_available
  241. * used for check actively if the host can send packet to controller or not.
  242. * @return true for ready to send, false means cannot send packet
  243. */
  244. bool esp_vhci_host_check_send_available(void);
  245. /** @brief esp_vhci_host_send_packet
  246. * host send packet to controller
  247. * @param data the packet point
  248. *,@param len the packet length
  249. */
  250. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len);
  251. /** @brief esp_vhci_host_register_callback
  252. * register the vhci referece callback, the call back
  253. * struct defined by vhci_host_callback structure.
  254. * @param callback esp_vhci_host_callback type variable
  255. */
  256. void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback);
  257. /** @brief esp_bt_controller_mem_release
  258. * release the memory by mode, if never use the bluetooth mode
  259. * it can release the .bbs, .data and other section to heap.
  260. * The total size is about 70k bytes.
  261. *
  262. * esp_bt_controller_mem_release(mode) should be called only before esp_bt_controller_init()
  263. * or after esp_bt_controller_deinit().
  264. *
  265. * Note that once BT controller memory is released, the process cannot be reversed. It means you can not use the bluetooth
  266. * mode which you have released by this function.
  267. *
  268. * If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled)
  269. * then do not call this function.
  270. *
  271. * If the app calls esp_bt_controller_enable(ESP_BT_MODE_BLE) to use BLE only then it is safe to call
  272. * esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT) at initialisation time to free unused BT Classic memory.
  273. *
  274. * If user never use bluetooth controller, could call esp_bt_controller_mem_release(ESP_BT_MODE_BTDM)
  275. * before esp_bt_controller_init or after esp_bt_controller_deinit.
  276. *
  277. * For example, user only use bluetooth to config SSID and PASSWORD of WIFI, after config, will never use bluetooth.
  278. * Then, could call esp_bt_controller_mem_release(ESP_BT_MODE_BTDM) after esp_bt_controller_deinit.
  279. *
  280. * @param mode : the mode want to release memory
  281. * @return ESP_OK - success, other - failed
  282. */
  283. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode);
  284. /**
  285. * @brief enable bluetooth to enter modem sleep
  286. *
  287. * Note that this function shall not be invoked before esp_bt_controller_enable()
  288. *
  289. * 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.
  290. *
  291. * For ORIG mode:
  292. * Bluetooth modem sleep is enabled in controller start up by default if CONFIG_BTDM_CONTROLLER_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".
  293. * Note that currently there is problem in the combination use of bluetooth modem sleep and Dynamic Frequency Scaling(DFS). So do not enable DFS if bluetooth modem sleep is in use.
  294. *
  295. * @return
  296. * - ESP_OK : success
  297. * - other : failed
  298. */
  299. esp_err_t esp_bt_sleep_enable(void);
  300. /**
  301. * @brief disable bluetooth modem sleep
  302. *
  303. * Note that this function shall not be invoked before esp_bt_controller_enable()
  304. *
  305. * If esp_bt_sleep_disable() is called, bluetooth controller will not be allowed to enter modem sleep;
  306. *
  307. * 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.
  308. * In this case, esp_bt_controller_wakeup_request() can be used to shorten the time for wakeup.
  309. *
  310. * @return
  311. * - ESP_OK : success
  312. * - other : failed
  313. */
  314. esp_err_t esp_bt_sleep_disable(void);
  315. /**
  316. * @brief to check whether bluetooth controller is sleeping at the instant, if modem sleep is enabled
  317. *
  318. * Note that this function shall not be invoked before esp_bt_controller_enable()
  319. * This function is supposed to be used ORIG mode of modem sleep
  320. *
  321. * @return true if in modem sleep state, false otherwise
  322. */
  323. bool esp_bt_controller_is_sleeping(void);
  324. /**
  325. * @brief request controller to wakeup from sleeping state during sleep mode
  326. *
  327. * Note that this function shall not be invoked before esp_bt_controller_enable()
  328. * Note that this function is supposed to be used ORIG mode of modem sleep
  329. * Note that after this request, bluetooth controller may again enter sleep as long as the modem sleep is enabled
  330. *
  331. * Profiling shows that it takes several milliseconds to wakeup from modem sleep after this request.
  332. * Generally it takes longer if 32kHz XTAL is used than the main XTAL, due to the lower frequncy of the former as the bluetooth low power clock source.
  333. */
  334. void esp_bt_controller_wakeup_request(void);
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif /* __ESP_BT_H__ */