esp_bt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. } esp_bt_controller_config_t;
  34. #ifdef CONFIG_BT_ENABLED
  35. #ifdef CONFIG_BT_HCI_UART_NO
  36. #define BT_HCI_UART_NO_DEFAULT CONFIG_BT_HCI_UART_NO
  37. #else
  38. #define BT_HCI_UART_NO_DEFAULT 1
  39. #endif /* BT_HCI_UART_NO_DEFAULT */
  40. #ifdef CONFIG_BT_HCI_UART_BAUDRATE
  41. #define BT_HCI_UART_BAUDRATE_DEFAULT CONFIG_BT_HCI_UART_BAUDRATE
  42. #else
  43. #define BT_HCI_UART_BAUDRATE_DEFAULT 921600
  44. #endif /* BT_HCI_UART_BAUDRATE_DEFAULT */
  45. #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() { \
  46. .controller_task_stack_size = ESP_TASK_BT_CONTROLLER_STACK, \
  47. .controller_task_prio = ESP_TASK_BT_CONTROLLER_PRIO, \
  48. .hci_uart_no = BT_HCI_UART_NO_DEFAULT, \
  49. .hci_uart_baudrate = BT_HCI_UART_BAUDRATE_DEFAULT, \
  50. };
  51. #else
  52. #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() {0}; _Static_assert(0, "please enable bluetooth in menuconfig to use bt.h");
  53. #endif
  54. /**
  55. * @brief Bluetooth mode for controller enable/disable
  56. */
  57. typedef enum {
  58. ESP_BT_MODE_IDLE = 0x00, /*!< Bluetooth is not running */
  59. ESP_BT_MODE_BLE = 0x01, /*!< Run BLE mode */
  60. ESP_BT_MODE_CLASSIC_BT = 0x02, /*!< Run Classic BT mode */
  61. ESP_BT_MODE_BTDM = 0x03, /*!< Run dual mode */
  62. } esp_bt_mode_t;
  63. /**
  64. * @brief Bluetooth controller enable/disable/initialised/de-initialised status
  65. */
  66. typedef enum {
  67. ESP_BT_CONTROLLER_STATUS_IDLE = 0,
  68. ESP_BT_CONTROLLER_STATUS_INITED,
  69. ESP_BT_CONTROLLER_STATUS_ENABLED,
  70. ESP_BT_CONTROLLER_STATUS_NUM,
  71. } esp_bt_controller_status_t;
  72. /**
  73. * @brief BLE tx power type
  74. * ESP_BLE_PWR_TYPE_CONN_HDL0-8: for each connection, and only be set after connection completed.
  75. * when disconnect, the correspond TX power is not effected.
  76. * ESP_BLE_PWR_TYPE_ADV : for advertising/scan response.
  77. * ESP_BLE_PWR_TYPE_SCAN : for scan.
  78. * ESP_BLE_PWR_TYPE_DEFAULT : if each connection's TX power is not set, it will use this default value.
  79. * if neither in scan mode nor in adv mode, it will use this default value.
  80. * If none of power type is set, system will use ESP_PWR_LVL_P1 as default for ADV/SCAN/CONN0-9.
  81. */
  82. typedef enum {
  83. ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, /*!< For connection handle 0 */
  84. ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, /*!< For connection handle 1 */
  85. ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, /*!< For connection handle 2 */
  86. ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, /*!< For connection handle 3 */
  87. ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, /*!< For connection handle 4 */
  88. ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, /*!< For connection handle 5 */
  89. ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, /*!< For connection handle 6 */
  90. ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, /*!< For connection handle 7 */
  91. ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, /*!< For connection handle 8 */
  92. ESP_BLE_PWR_TYPE_ADV = 9, /*!< For advertising */
  93. ESP_BLE_PWR_TYPE_SCAN = 10, /*!< For scan */
  94. ESP_BLE_PWR_TYPE_DEFAULT = 11, /*!< For default, if not set other, it will use default value */
  95. ESP_BLE_PWR_TYPE_NUM = 12, /*!< TYPE numbers */
  96. } esp_ble_power_type_t;
  97. /**
  98. * @brief Bluetooth TX power level(index), it's just a index corresponding to power(dbm).
  99. */
  100. typedef enum {
  101. ESP_PWR_LVL_N14 = 0, /*!< Corresponding to -14dbm */
  102. ESP_PWR_LVL_N11 = 1, /*!< Corresponding to -11dbm */
  103. ESP_PWR_LVL_N8 = 2, /*!< Corresponding to -8dbm */
  104. ESP_PWR_LVL_N5 = 3, /*!< Corresponding to -5dbm */
  105. ESP_PWR_LVL_N2 = 4, /*!< Corresponding to -2dbm */
  106. ESP_PWR_LVL_P1 = 5, /*!< Corresponding to 1dbm */
  107. ESP_PWR_LVL_P4 = 6, /*!< Corresponding to 4dbm */
  108. ESP_PWR_LVL_P7 = 7, /*!< Corresponding to 7dbm */
  109. } esp_power_level_t;
  110. /**
  111. * @brief Set BLE TX power
  112. * Connection Tx power should only be set after connection created.
  113. * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
  114. * @param power_level: Power level(index) corresponding to absolute value(dbm)
  115. * @return ESP_OK - success, other - failed
  116. */
  117. esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level);
  118. /**
  119. * @brief Get BLE TX power
  120. * Connection Tx power should only be get after connection created.
  121. * @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
  122. * @return >= 0 - Power level, < 0 - Invalid
  123. */
  124. esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type);
  125. /**
  126. * @brief Initialize BT controller to allocate task and other resource.
  127. * @param cfg: Initial configuration of BT controller.
  128. * This function should be called only once, before any other BT functions are called.
  129. * @return ESP_OK - success, other - failed
  130. */
  131. esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg);
  132. /**
  133. * @brief De-initialize BT controller to free resource and delete task.
  134. *
  135. * This function should be called only once, after any other BT functions are called.
  136. * This function is not whole completed, esp_bt_controller_init cannot called after this function.
  137. * @return ESP_OK - success, other - failed
  138. */
  139. esp_err_t esp_bt_controller_deinit(void);
  140. /**
  141. * @brief Enable BT controller.
  142. * Due to a known issue, you cannot call esp_bt_controller_enable() a second time
  143. * to change the controller mode dynamically. To change controller mode, call
  144. * esp_bt_controller_disable() and then call esp_bt_controller_enable() with the new mode.
  145. * @param mode : the mode(BLE/BT/BTDM) to enable.
  146. * @return ESP_OK - success, other - failed
  147. */
  148. esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode);
  149. /**
  150. * @brief Disable BT controller
  151. * @return ESP_OK - success, other - failed
  152. */
  153. esp_err_t esp_bt_controller_disable(void);
  154. /**
  155. * @brief Get BT controller is initialised/de-initialised/enabled/disabled
  156. * @return status value
  157. */
  158. esp_bt_controller_status_t esp_bt_controller_get_status(void);
  159. /** @brief esp_vhci_host_callback
  160. * used for vhci call host function to notify what host need to do
  161. */
  162. typedef struct esp_vhci_host_callback {
  163. void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
  164. 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*/
  165. } esp_vhci_host_callback_t;
  166. /** @brief esp_vhci_host_check_send_available
  167. * used for check actively if the host can send packet to controller or not.
  168. * @return true for ready to send, false means cannot send packet
  169. */
  170. bool esp_vhci_host_check_send_available(void);
  171. /** @brief esp_vhci_host_send_packet
  172. * host send packet to controller
  173. * @param data the packet point
  174. *,@param len the packet length
  175. */
  176. void esp_vhci_host_send_packet(uint8_t *data, uint16_t len);
  177. /** @brief esp_vhci_host_register_callback
  178. * register the vhci referece callback, the call back
  179. * struct defined by vhci_host_callback structure.
  180. * @param callback esp_vhci_host_callback type variable
  181. */
  182. void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback);
  183. /** @brief esp_bt_controller_mem_release
  184. * release the memory by mode, if never use the bluetooth mode
  185. * it can release the .bbs, .data and other section to heap.
  186. * The total size is about 70k bytes.
  187. *
  188. * esp_bt_controller_mem_release(mode) should be called only before esp_bt_controller_init()
  189. * or after esp_bt_controller_deinit().
  190. *
  191. * Note that once BT controller memory is released, the process cannot be reversed. It means you can not use the bluetooth
  192. * mode which you have released by this function.
  193. *
  194. * If your firmware will later upgrade the Bluetooth controller mode (BLE -> BT Classic or disabled -> enabled)
  195. * then do not call this function.
  196. *
  197. * If the app calls esp_bt_controller_enable(ESP_BT_MODE_BLE) to use BLE only then it is safe to call
  198. * esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT) at initialisation time to free unused BT Classic memory.
  199. *
  200. * If user never use bluetooth controller, could call esp_bt_controller_mem_release(ESP_BT_MODE_BTDM)
  201. * before esp_bt_controller_init or after esp_bt_controller_deinit.
  202. *
  203. * For example, user only use bluetooth to config SSID and PASSWORD of WIFI, after config, will never use bluetooth.
  204. * Then, could call esp_bt_controller_mem_release(ESP_BT_MODE_BTDM) after esp_bt_controller_deinit.
  205. *
  206. * @param mode : the mode want to release memory
  207. * @return ESP_OK - success, other - failed
  208. */
  209. esp_err_t esp_bt_controller_mem_release(esp_bt_mode_t mode);
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif /* __ESP_BT_H__ */