esp_a2dp_api.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include "bt_target.h"
  14. #include <string.h>
  15. #include "esp_err.h"
  16. #include "esp_a2dp_api.h"
  17. #include "esp_bt_main.h"
  18. #include "btc_manage.h"
  19. #include "btc_av.h"
  20. #if BTC_AV_INCLUDED
  21. #if BTC_AV_SINK_INCLUDED
  22. esp_err_t esp_a2d_sink_init(void)
  23. {
  24. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  25. return ESP_ERR_INVALID_STATE;
  26. }
  27. btc_msg_t msg;
  28. msg.sig = BTC_SIG_API_CALL;
  29. msg.pid = BTC_PID_A2DP;
  30. msg.act = BTC_AV_SINK_API_INIT_EVT;
  31. /* Switch to BTC context */
  32. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  33. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  34. }
  35. esp_err_t esp_a2d_sink_deinit(void)
  36. {
  37. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  38. return ESP_ERR_INVALID_STATE;
  39. }
  40. btc_msg_t msg;
  41. msg.sig = BTC_SIG_API_CALL;
  42. msg.pid = BTC_PID_A2DP;
  43. msg.act = BTC_AV_SINK_API_DEINIT_EVT;
  44. /* Switch to BTC context */
  45. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  46. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  47. }
  48. esp_err_t esp_a2d_sink_register_data_callback(esp_a2d_sink_data_cb_t callback)
  49. {
  50. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  51. return ESP_ERR_INVALID_STATE;
  52. }
  53. btc_msg_t msg;
  54. msg.sig = BTC_SIG_API_CALL;
  55. msg.pid = BTC_PID_A2DP;
  56. msg.act = BTC_AV_SINK_API_REG_DATA_CB_EVT;
  57. btc_av_args_t arg;
  58. memset(&arg, 0, sizeof(btc_av_args_t));
  59. arg.data_cb = callback;
  60. /* Switch to BTC context */
  61. bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  62. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  63. }
  64. esp_err_t esp_a2d_sink_connect(esp_bd_addr_t remote_bda)
  65. {
  66. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  67. return ESP_ERR_INVALID_STATE;
  68. }
  69. bt_status_t stat;
  70. btc_av_args_t arg;
  71. btc_msg_t msg;
  72. msg.sig = BTC_SIG_API_CALL;
  73. msg.pid = BTC_PID_A2DP;
  74. msg.act = BTC_AV_SINK_API_CONNECT_EVT;
  75. memset(&arg, 0, sizeof(btc_av_args_t));
  76. /* Switch to BTC context */
  77. memcpy(&(arg.connect), remote_bda, sizeof(bt_bdaddr_t));
  78. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  79. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  80. }
  81. esp_err_t esp_a2d_sink_disconnect(esp_bd_addr_t remote_bda)
  82. {
  83. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  84. return ESP_ERR_INVALID_STATE;
  85. }
  86. bt_status_t stat;
  87. btc_msg_t msg;
  88. msg.sig = BTC_SIG_API_CALL;
  89. msg.pid = BTC_PID_A2DP;
  90. msg.act = BTC_AV_SINK_API_DISCONNECT_EVT;
  91. /* Switch to BTC context */
  92. stat = btc_transfer_context(&msg, NULL, 0, NULL);
  93. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  94. }
  95. #endif /* BTC_AV_SINK_INCLUDED */
  96. esp_err_t esp_a2d_register_callback(esp_a2d_cb_t callback)
  97. {
  98. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  99. return ESP_ERR_INVALID_STATE;
  100. }
  101. if (callback == NULL) {
  102. return ESP_FAIL;
  103. }
  104. btc_profile_cb_set(BTC_PID_A2DP, callback);
  105. return ESP_OK;
  106. }
  107. esp_err_t esp_a2d_media_ctrl(esp_a2d_media_ctrl_t ctrl)
  108. {
  109. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  110. return ESP_ERR_INVALID_STATE;
  111. }
  112. bt_status_t stat;
  113. btc_av_args_t arg;
  114. btc_msg_t msg;
  115. msg.sig = BTC_SIG_API_CALL;
  116. msg.pid = BTC_PID_A2DP;
  117. msg.act = BTC_AV_API_MEDIA_CTRL_EVT;
  118. memset(&arg, 0, sizeof(btc_av_args_t));
  119. /* Switch to BTC context */
  120. arg.ctrl = ctrl;
  121. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  122. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  123. }
  124. #if BTC_AV_SRC_INCLUDED
  125. esp_err_t esp_a2d_source_init(void)
  126. {
  127. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  128. return ESP_ERR_INVALID_STATE;
  129. }
  130. btc_msg_t msg;
  131. msg.sig = BTC_SIG_API_CALL;
  132. msg.pid = BTC_PID_A2DP;
  133. msg.act = BTC_AV_SRC_API_INIT_EVT;
  134. /* Switch to BTC context */
  135. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  136. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  137. }
  138. esp_err_t esp_a2d_source_deinit(void)
  139. {
  140. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  141. return ESP_ERR_INVALID_STATE;
  142. }
  143. btc_msg_t msg;
  144. msg.sig = BTC_SIG_API_CALL;
  145. msg.pid = BTC_PID_A2DP;
  146. msg.act = BTC_AV_SRC_API_DEINIT_EVT;
  147. /* Switch to BTC context */
  148. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  149. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  150. }
  151. esp_err_t esp_a2d_source_connect(esp_bd_addr_t remote_bda)
  152. {
  153. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  154. return ESP_ERR_INVALID_STATE;
  155. }
  156. bt_status_t stat;
  157. btc_av_args_t arg;
  158. btc_msg_t msg;
  159. msg.sig = BTC_SIG_API_CALL;
  160. msg.pid = BTC_PID_A2DP;
  161. msg.act = BTC_AV_SRC_API_CONNECT_EVT;
  162. memset(&arg, 0, sizeof(btc_av_args_t));
  163. /* Switch to BTC context */
  164. memcpy(&(arg.src_connect), remote_bda, sizeof(bt_bdaddr_t));
  165. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  166. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  167. }
  168. esp_err_t esp_a2d_source_disconnect(esp_bd_addr_t remote_bda)
  169. {
  170. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  171. return ESP_ERR_INVALID_STATE;
  172. }
  173. bt_status_t stat;
  174. btc_msg_t msg;
  175. msg.sig = BTC_SIG_API_CALL;
  176. msg.pid = BTC_PID_A2DP;
  177. msg.act = BTC_AV_SRC_API_DISCONNECT_EVT;
  178. /* Switch to BTC context */
  179. stat = btc_transfer_context(&msg, NULL, 0, NULL);
  180. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  181. }
  182. esp_err_t esp_a2d_source_register_data_callback(esp_a2d_source_data_cb_t callback)
  183. {
  184. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  185. return ESP_ERR_INVALID_STATE;
  186. }
  187. btc_msg_t msg;
  188. msg.sig = BTC_SIG_API_CALL;
  189. msg.pid = BTC_PID_A2DP;
  190. msg.act = BTC_AV_SRC_API_REG_DATA_CB_EVT;
  191. btc_av_args_t arg;
  192. memset(&arg, 0, sizeof(btc_av_args_t));
  193. arg.src_data_cb = callback;
  194. /* Switch to BTC context */
  195. bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  196. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  197. }
  198. #endif /* BTC_AV_SRC_INCLUDED */
  199. #endif /* #if BTC_AV_INCLUDED */