hci_transport.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*********************************************************************************************
  2. * Filename : hci_transport.h
  3. * Description :
  4. * Author : Bingquan
  5. * Email : bingquan_cai@zh-jieli.com
  6. * Last modifiled : 2017-01-17 15:14
  7. * Copyright:(c)JIELI 2011-2016 @ , All Rights Reserved.
  8. *********************************************************************************************/
  9. #ifndef __BTCONTROLLER_HCI_TRANSPORT_H
  10. #define __BTCONTROLLER_HCI_TRANSPORT_H
  11. //#include <stdint.h>
  12. #include "generic/typedef.h"
  13. #if defined __cplusplus
  14. extern "C" {
  15. #endif
  16. /* API_START */
  17. typedef struct {
  18. uint32_t baudrate;
  19. int flowcontrol;
  20. const char *device_name;
  21. } btstack_uart_config_t;
  22. typedef enum {
  23. // UART active, sleep off
  24. BTSTACK_UART_SLEEP_OFF = 0,
  25. // used for eHCILL
  26. BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
  27. // used for H5 and for eHCILL without support for wake on CTS pulse
  28. BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE,
  29. } btstack_uart_sleep_mode_t;
  30. typedef enum {
  31. BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE = 1 << BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE,
  32. BTSTACK_UART_SLEEP_MASK_RTS_LOW_WAKE_ON_RX_EDGE = 1 << BTSTACK_UART_SLEEP_RTS_LOW_WAKE_ON_RX_EDGE
  33. } btstack_uart_sleep_mode_mask_t;
  34. typedef struct {
  35. /**
  36. * init transport
  37. * @param uart_config
  38. */
  39. int (*init)(const btstack_uart_config_t *uart_config);
  40. /**
  41. * open transport connection
  42. */
  43. int (*open)(void);
  44. /**
  45. * close transport connection
  46. */
  47. int (*close)(void);
  48. /**
  49. * set callback for block received. NULL disables callback
  50. */
  51. void (*set_block_received)(void (*block_handler)(void));
  52. /**
  53. * set callback for sent. NULL disables callback
  54. */
  55. void (*set_block_sent)(void (*block_handler)(void));
  56. /**
  57. * set baudrate
  58. */
  59. int (*set_baudrate)(uint32_t baudrate);
  60. /**
  61. * set parity
  62. */
  63. int (*set_parity)(int parity);
  64. /**
  65. * set flowcontrol
  66. */
  67. int (*set_flowcontrol)(int flowcontrol);
  68. /**
  69. * receive block
  70. */
  71. void (*receive_block)(uint8_t *buffer, uint16_t len);
  72. /**
  73. * send block
  74. */
  75. void (*send_block)(const uint8_t *buffer, uint16_t length);
  76. // support for sleep modes in TI's H4 eHCILL and H5
  77. /**
  78. * query supported wakeup mechanisms
  79. * @return supported_sleep_modes mask
  80. */
  81. int (*get_supported_sleep_modes)(void);
  82. /**
  83. * set UART sleep mode - allows to turn off UART and it's clocks to save energy
  84. * Supported sleep modes:
  85. * - off: UART active, RTS low if receive_block was called and block not read yet
  86. * - RTS high, wake on CTS: RTS should be high. On CTS pulse, UART gets enabled again and RTS goes to low
  87. * - RTS low, wake on RX: data on RX will trigger UART enable, bytes might get lost
  88. */
  89. void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode);
  90. /**
  91. * set wakeup handler - needed to notify hci transport of wakeup requests by Bluetooth controller
  92. * Called upon CTS pulse or RX data. See sleep modes.
  93. */
  94. void (*set_wakeup_handler)(void (*wakeup_handler)(void));
  95. } btstack_uart_block_t;
  96. // common implementations
  97. const btstack_uart_block_t *btstack_uart_block_posix_instance(void);
  98. const btstack_uart_block_t *btstack_uart_block_windows_instance(void);
  99. const btstack_uart_block_t *btstack_uart_block_embedded_instance(void);
  100. const btstack_uart_block_t *btstack_uart_block_freertos_instance(void);
  101. /* HCI packet types */
  102. typedef struct {
  103. /**
  104. * transport name
  105. */
  106. const char *name;
  107. /**
  108. * init transport
  109. * @param transport_config
  110. */
  111. void (*init)(const void *transport_config);
  112. /**
  113. * open transport connection
  114. */
  115. int (*open)(void);
  116. /**
  117. * close transport connection
  118. */
  119. int (*close)(void);
  120. /**
  121. * register packet handler for HCI packets: ACL, SCO, and Events
  122. */
  123. void (*register_packet_handler)(void (*handler)(int packet_type, const u8 *packet, int size));
  124. /**
  125. * support async transport layers, e.g. IRQ driven without buffers
  126. */
  127. int (*can_send_packet_now)(uint8_t packet_type);
  128. /**
  129. * send packet
  130. */
  131. int (*send_packet)(int packet_type, const u8 *packet, int size);
  132. /**
  133. * extension for UART transport implementations
  134. */
  135. int (*set_baudrate)(uint32_t baudrate);
  136. /**
  137. * extension for UART H5 on CSR: reset BCSP/H5 Link
  138. */
  139. void (*reset_link)(void);
  140. /**
  141. * extension for USB transport implementations: config SCO connections
  142. */
  143. void (*set_sco_config)(uint16_t voice_setting, int num_connections);
  144. } hci_transport_t;
  145. typedef enum {
  146. HCI_TRANSPORT_CONFIG_UART,
  147. HCI_TRANSPORT_CONFIG_USB
  148. } hci_transport_config_type_t;
  149. typedef struct {
  150. hci_transport_config_type_t type;
  151. } hci_transport_config_t;
  152. typedef struct {
  153. hci_transport_config_type_t type; // == HCI_TRANSPORT_CONFIG_UART
  154. uint32_t baudrate_init; // initial baud rate
  155. uint32_t baudrate_main; // = 0: same as initial baudrate
  156. int flowcontrol; //
  157. const char *device_name;
  158. } hci_transport_config_uart_t;
  159. // inline various hci_transport_X.h files
  160. /*
  161. * @brief Setup H4 instance with uart_driver
  162. * @param uart_driver to use
  163. */
  164. const hci_transport_t *hci_transport_h4_instance(const btstack_uart_block_t *uart_driver);
  165. /*
  166. * @brief Setup H5 instance with uart_driver
  167. * @param uart_driver to use
  168. */
  169. const hci_transport_t *hci_transport_h5_instance(const btstack_uart_block_t *uart_driver);
  170. /*
  171. * @brief Enable H5 Low Power Mode: enter sleep mode after x ms of inactivity
  172. * @param inactivity_timeout_ms or 0 for off
  173. */
  174. void hci_transport_h5_set_auto_sleep(uint16_t inactivity_timeout_ms);
  175. /*
  176. * @brief Enable BSCP mode H5, by enabling event parity
  177. */
  178. void hci_transport_h5_enable_bcsp_mode(void);
  179. /*
  180. * @brief
  181. */
  182. const hci_transport_t *hci_transport_usb_instance(void);
  183. const hci_transport_t *hci_transport_uart_instance(void);
  184. const hci_transport_t *hci_transport_h4_controller_instance(void);
  185. const hci_transport_t *hci_transport_h4_host_instance(void);
  186. /**
  187. * @brief Specify USB Bluetooth device via port numbers from root to device
  188. */
  189. void hci_transport_usb_set_path(int len, uint8_t *port_numbers);
  190. /* API_END */
  191. extern const hci_transport_t *hci_transport;
  192. #if defined __cplusplus
  193. }
  194. #endif
  195. #endif // __HCI_TRANSPORT_H