esp_a2dp_api.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_A2DP_API_H__
  14. #define __ESP_A2DP_API_H__
  15. #include "esp_err.h"
  16. #include "esp_bt_defs.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /// Media codec types supported by A2DP
  21. #define ESP_A2D_MCT_SBC (0) /*!< SBC */
  22. #define ESP_A2D_MCT_M12 (0x01) /*!< MPEG-1, 2 Audio */
  23. #define ESP_A2D_MCT_M24 (0x02) /*!< MPEG-2, 4 AAC */
  24. #define ESP_A2D_MCT_ATRAC (0x04) /*!< ATRAC family */
  25. #define ESP_A2D_MCT_NON_A2DP (0xff)
  26. typedef uint8_t esp_a2d_mct_t;
  27. /// A2DP media codec capabilities union
  28. typedef struct {
  29. esp_a2d_mct_t type; /*!< A2DP media codec type */
  30. #define ESP_A2D_CIE_LEN_SBC (4)
  31. #define ESP_A2D_CIE_LEN_M12 (4)
  32. #define ESP_A2D_CIE_LEN_M24 (6)
  33. #define ESP_A2D_CIE_LEN_ATRAC (7)
  34. union {
  35. uint8_t sbc[ESP_A2D_CIE_LEN_SBC];
  36. uint8_t m12[ESP_A2D_CIE_LEN_M12];
  37. uint8_t m24[ESP_A2D_CIE_LEN_M24];
  38. uint8_t atrac[ESP_A2D_CIE_LEN_ATRAC];
  39. } cie; /*!< A2DP codec information element */
  40. } __attribute__((packed)) esp_a2d_mcc_t;
  41. /// Bluetooth A2DP connection states
  42. typedef enum {
  43. ESP_A2D_CONNECTION_STATE_DISCONNECTED = 0, /*!< connection released */
  44. ESP_A2D_CONNECTION_STATE_CONNECTING, /*!< connecting remote device */
  45. ESP_A2D_CONNECTION_STATE_CONNECTED, /*!< connection established */
  46. ESP_A2D_CONNECTION_STATE_DISCONNECTING /*!< disconnecting remote device */
  47. } esp_a2d_connection_state_t;
  48. /// Bluetooth A2DP disconnection reason
  49. typedef enum {
  50. ESP_A2D_DISC_RSN_NORMAL = 0, /*!< Finished disconnection that is initiated by local or remote device */
  51. ESP_A2D_DISC_RSN_ABNORMAL /*!< Abnormal disconnection caused by signal loss */
  52. } esp_a2d_disc_rsn_t;
  53. /// Bluetooth A2DP datapath states
  54. typedef enum {
  55. ESP_A2D_AUDIO_STATE_REMOTE_SUSPEND = 0, /*!< audio stream datapath suspended by remote device */
  56. ESP_A2D_AUDIO_STATE_STOPPED, /*!< audio stream datapath stopped */
  57. ESP_A2D_AUDIO_STATE_STARTED, /*!< audio stream datapath started */
  58. } esp_a2d_audio_state_t;
  59. /// A2DP media control command acknowledgement code
  60. typedef enum {
  61. ESP_A2D_MEDIA_CTRL_ACK_SUCCESS = 0, /*!< media control command is acknowledged with success */
  62. ESP_A2D_MEDIA_CTRL_ACK_FAILURE, /*!< media control command is acknowledged with failure */
  63. ESP_A2D_MEDIA_CTRL_ACK_BUSY, /*!< media control command is rejected, as previous command is not yet acknowledged */
  64. } esp_a2d_media_ctrl_ack_t;
  65. /// A2DP media control commands
  66. typedef enum {
  67. ESP_A2D_MEDIA_CTRL_NONE = 0, /*!< dummy command */
  68. ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY, /*!< check whether AVDTP is connected, only used in A2DP source */
  69. ESP_A2D_MEDIA_CTRL_START, /*!< command to set up media transmission channel */
  70. ESP_A2D_MEDIA_CTRL_STOP, /*!< command to stop media transmission */
  71. ESP_A2D_MEDIA_CTRL_SUSPEND, /*!< command to suspend media transmission */
  72. } esp_a2d_media_ctrl_t;
  73. /// A2DP callback events
  74. typedef enum {
  75. ESP_A2D_CONNECTION_STATE_EVT = 0, /*!< connection state changed event */
  76. ESP_A2D_AUDIO_STATE_EVT, /*!< audio stream transmission state changed event */
  77. ESP_A2D_AUDIO_CFG_EVT, /*!< audio codec is configured, only used for A2DP SINK */
  78. ESP_A2D_MEDIA_CTRL_ACK_EVT, /*!< acknowledge event in response to media control commands */
  79. } esp_a2d_cb_event_t;
  80. /// A2DP state callback parameters
  81. typedef union {
  82. /**
  83. * @brief ESP_A2D_CONNECTION_STATE_EVT
  84. */
  85. struct a2d_conn_stat_param {
  86. esp_a2d_connection_state_t state; /*!< one of values from esp_a2d_connection_state_t */
  87. esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
  88. esp_a2d_disc_rsn_t disc_rsn; /*!< reason of disconnection for "DISCONNECTED" */
  89. } conn_stat; /*!< A2DP connection status */
  90. /**
  91. * @brief ESP_A2D_AUDIO_STATE_EVT
  92. */
  93. struct a2d_audio_stat_param {
  94. esp_a2d_audio_state_t state; /*!< one of the values from esp_a2d_audio_state_t */
  95. esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
  96. } audio_stat; /*!< audio stream playing state */
  97. /**
  98. * @brief ESP_A2D_AUDIO_CFG_EVT
  99. */
  100. struct a2d_audio_cfg_param {
  101. esp_bd_addr_t remote_bda; /*!< remote bluetooth device address */
  102. esp_a2d_mcc_t mcc; /*!< A2DP media codec capability information */
  103. } audio_cfg; /*!< media codec configuration infomation */
  104. /**
  105. * @brief ESP_A2D_MEDIA_CTRL_ACK_EVT
  106. */
  107. struct media_ctrl_stat_param {
  108. esp_a2d_media_ctrl_t cmd; /*!< media control commands to acknowledge */
  109. esp_a2d_media_ctrl_ack_t status; /*!< acknowledgement to media control commands */
  110. } media_ctrl_stat; /*!< status in acknowledgement to media control commands */
  111. } esp_a2d_cb_param_t;
  112. /**
  113. * @brief A2DP profile callback function type
  114. *
  115. * @param event : Event type
  116. *
  117. * @param param : Pointer to callback parameter
  118. */
  119. typedef void (* esp_a2d_cb_t)(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
  120. /**
  121. * @brief A2DP profile data callback function
  122. * @param[in] buf : data received from A2DP source device and is PCM format decoder from SBC decoder;
  123. * buf references to a static memory block and can be overwritten by upcoming data
  124. * @param[in] len : size(in bytes) in buf
  125. */
  126. typedef void (* esp_a2d_sink_data_cb_t)(const uint8_t *buf, uint32_t len);
  127. /**
  128. * @brief A2DP source data read callback function
  129. *
  130. * @param[in] buf : buffer to be filled with PCM data stream from higer layer
  131. *
  132. * @param[in] len : size(in bytes) of data block to be copied to buf. -1 is an indication to user
  133. * that data buffer shall be flushed
  134. *
  135. * @return size of bytes read successfully, if the argumetn len is -1, this value is ignored.
  136. *
  137. */
  138. typedef int32_t (* esp_a2d_source_data_cb_t)(uint8_t *buf, int32_t len);
  139. /**
  140. * @brief Register application callback function to A2DP module. This function should be called
  141. * only after esp_bluedroid_enable() completes successfully, used by both A2DP source
  142. * and sink.
  143. *
  144. * @param[in] callback: A2DP event callback function
  145. *
  146. * @return
  147. * - ESP_OK: success
  148. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  149. * - ESP_FAIL: if callback is a NULL function pointer
  150. *
  151. */
  152. esp_err_t esp_a2d_register_callback(esp_a2d_cb_t callback);
  153. /**
  154. * @brief Register A2DP sink data output function; For now the output is PCM data stream decoded
  155. * from SBC format. This function should be called only after esp_bluedroid_enable()
  156. * completes successfully, used only by A2DP sink. The callback is invoked in the context
  157. * of A2DP sink task whose stack size is configurable through menuconfig
  158. *
  159. * @param[in] callback: A2DP sink data callback function
  160. *
  161. * @return
  162. * - ESP_OK: success
  163. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  164. * - ESP_FAIL: if callback is a NULL function pointer
  165. *
  166. */
  167. esp_err_t esp_a2d_sink_register_data_callback(esp_a2d_sink_data_cb_t callback);
  168. /**
  169. *
  170. * @brief Initialize the bluetooth A2DP sink module. This function should be called
  171. * after esp_bluedroid_enable() completes successfully
  172. *
  173. * @return
  174. * - ESP_OK: if the initialization request is sent successfully
  175. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  176. * - ESP_FAIL: others
  177. *
  178. */
  179. esp_err_t esp_a2d_sink_init(void);
  180. /**
  181. *
  182. * @brief De-initialize for A2DP sink module. This function
  183. * should be called only after esp_bluedroid_enable() completes successfully
  184. *
  185. * @return
  186. * - ESP_OK: success
  187. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  188. * - ESP_FAIL: others
  189. *
  190. */
  191. esp_err_t esp_a2d_sink_deinit(void);
  192. /**
  193. *
  194. * @brief Connect to remote bluetooth A2DP source device, must after esp_a2d_sink_init()
  195. *
  196. * @param[in] remote_bda: remote bluetooth device address
  197. *
  198. * @return
  199. * - ESP_OK: connect request is sent to lower layer
  200. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  201. * - ESP_FAIL: others
  202. *
  203. */
  204. esp_err_t esp_a2d_sink_connect(esp_bd_addr_t remote_bda);
  205. /**
  206. *
  207. * @brief Disconnect from the remote A2DP source device
  208. *
  209. * @param[in] remote_bda: remote bluetooth device address
  210. * @return
  211. * - ESP_OK: disconnect request is sent to lower layer
  212. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  213. * - ESP_FAIL: others
  214. *
  215. */
  216. esp_err_t esp_a2d_sink_disconnect(esp_bd_addr_t remote_bda);
  217. /**
  218. *
  219. * @brief media control commands; this API can be used for both A2DP sink and source
  220. *
  221. * @param[in] ctrl: control commands for A2DP data channel
  222. * @return
  223. * - ESP_OK: control command is sent to lower layer
  224. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  225. * - ESP_FAIL: others
  226. *
  227. */
  228. esp_err_t esp_a2d_media_ctrl(esp_a2d_media_ctrl_t ctrl);
  229. /**
  230. *
  231. * @brief Initialize the bluetooth A2DP source module. This function should be called
  232. * after esp_bluedroid_enable() completes successfully
  233. *
  234. * @return
  235. * - ESP_OK: if the initialization request is sent successfully
  236. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  237. * - ESP_FAIL: others
  238. *
  239. */
  240. esp_err_t esp_a2d_source_init(void);
  241. /**
  242. *
  243. * @brief De-initialize for A2DP source module. This function
  244. * should be called only after esp_bluedroid_enable() completes successfully
  245. *
  246. * @return
  247. * - ESP_OK: success
  248. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  249. * - ESP_FAIL: others
  250. *
  251. */
  252. esp_err_t esp_a2d_source_deinit(void);
  253. /**
  254. * @brief Register A2DP source data input function; For now the input is PCM data stream.
  255. * This function should be called only after esp_bluedroid_enable() completes
  256. * successfully. The callback is invoked in the context of A2DP source task whose
  257. * stack size is configurable through menuconfig
  258. *
  259. * @param[in] callback: A2DP source data callback function
  260. *
  261. * @return
  262. * - ESP_OK: success
  263. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  264. * - ESP_FAIL: if callback is a NULL function pointer
  265. *
  266. */
  267. esp_err_t esp_a2d_source_register_data_callback(esp_a2d_source_data_cb_t callback);
  268. /**
  269. *
  270. * @brief Connect to remote A2DP sink device, must after esp_a2d_source_init()
  271. *
  272. * @param[in] remote_bda: remote bluetooth device address
  273. *
  274. * @return
  275. * - ESP_OK: connect request is sent to lower layer
  276. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  277. * - ESP_FAIL: others
  278. *
  279. */
  280. esp_err_t esp_a2d_source_connect(esp_bd_addr_t remote_bda);
  281. /**
  282. *
  283. * @brief Disconnect from the remote A2DP sink device
  284. *
  285. * @param[in] remote_bda: remote bluetooth device address
  286. * @return
  287. * - ESP_OK: disconnect request is sent to lower layer
  288. * - ESP_INVALID_STATE: if bluetooth stack is not yet enabled
  289. * - ESP_FAIL: others
  290. *
  291. */
  292. esp_err_t esp_a2d_source_disconnect(esp_bd_addr_t remote_bda);
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. #endif /* __ESP_A2DP_API_H__ */