esp_a2dp_api.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "common/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/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_av_args_t arg;
  88. btc_msg_t msg;
  89. msg.sig = BTC_SIG_API_CALL;
  90. msg.pid = BTC_PID_A2DP;
  91. msg.act = BTC_AV_SINK_API_DISCONNECT_EVT;
  92. /* Switch to BTC context */
  93. memcpy(&(arg.disconn), remote_bda, sizeof(bt_bdaddr_t));
  94. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  95. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  96. }
  97. #endif /* BTC_AV_SINK_INCLUDED */
  98. esp_err_t esp_a2d_register_callback(esp_a2d_cb_t callback)
  99. {
  100. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  101. return ESP_ERR_INVALID_STATE;
  102. }
  103. if (callback == NULL) {
  104. return ESP_FAIL;
  105. }
  106. btc_profile_cb_set(BTC_PID_A2DP, callback);
  107. return ESP_OK;
  108. }
  109. esp_err_t esp_a2d_media_ctrl(esp_a2d_media_ctrl_t ctrl)
  110. {
  111. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  112. return ESP_ERR_INVALID_STATE;
  113. }
  114. bt_status_t stat;
  115. btc_av_args_t arg;
  116. btc_msg_t msg;
  117. msg.sig = BTC_SIG_API_CALL;
  118. msg.pid = BTC_PID_A2DP;
  119. msg.act = BTC_AV_API_MEDIA_CTRL_EVT;
  120. memset(&arg, 0, sizeof(btc_av_args_t));
  121. /* Switch to BTC context */
  122. arg.ctrl = ctrl;
  123. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  124. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  125. }
  126. #if BTC_AV_SRC_INCLUDED
  127. esp_err_t esp_a2d_source_init(void)
  128. {
  129. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  130. return ESP_ERR_INVALID_STATE;
  131. }
  132. btc_msg_t msg;
  133. msg.sig = BTC_SIG_API_CALL;
  134. msg.pid = BTC_PID_A2DP;
  135. msg.act = BTC_AV_SRC_API_INIT_EVT;
  136. /* Switch to BTC context */
  137. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  138. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  139. }
  140. esp_err_t esp_a2d_source_deinit(void)
  141. {
  142. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  143. return ESP_ERR_INVALID_STATE;
  144. }
  145. btc_msg_t msg;
  146. msg.sig = BTC_SIG_API_CALL;
  147. msg.pid = BTC_PID_A2DP;
  148. msg.act = BTC_AV_SRC_API_DEINIT_EVT;
  149. /* Switch to BTC context */
  150. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  151. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  152. }
  153. esp_err_t esp_a2d_source_connect(esp_bd_addr_t remote_bda)
  154. {
  155. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  156. return ESP_ERR_INVALID_STATE;
  157. }
  158. bt_status_t stat;
  159. btc_av_args_t arg;
  160. btc_msg_t msg;
  161. msg.sig = BTC_SIG_API_CALL;
  162. msg.pid = BTC_PID_A2DP;
  163. msg.act = BTC_AV_SRC_API_CONNECT_EVT;
  164. memset(&arg, 0, sizeof(btc_av_args_t));
  165. /* Switch to BTC context */
  166. memcpy(&(arg.src_connect), remote_bda, sizeof(bt_bdaddr_t));
  167. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  168. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  169. }
  170. esp_err_t esp_a2d_source_disconnect(esp_bd_addr_t remote_bda)
  171. {
  172. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  173. return ESP_ERR_INVALID_STATE;
  174. }
  175. bt_status_t stat;
  176. btc_av_args_t arg;
  177. btc_msg_t msg;
  178. msg.sig = BTC_SIG_API_CALL;
  179. msg.pid = BTC_PID_A2DP;
  180. msg.act = BTC_AV_SRC_API_DISCONNECT_EVT;
  181. memset(&arg, 0, sizeof(btc_av_args_t));
  182. /* Switch to BTC context */
  183. memcpy(&(arg.src_disconn), remote_bda, sizeof(bt_bdaddr_t));
  184. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  185. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  186. }
  187. esp_err_t esp_a2d_source_register_data_callback(esp_a2d_source_data_cb_t callback)
  188. {
  189. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  190. return ESP_ERR_INVALID_STATE;
  191. }
  192. btc_msg_t msg;
  193. msg.sig = BTC_SIG_API_CALL;
  194. msg.pid = BTC_PID_A2DP;
  195. msg.act = BTC_AV_SRC_API_REG_DATA_CB_EVT;
  196. btc_av_args_t arg;
  197. memset(&arg, 0, sizeof(btc_av_args_t));
  198. arg.src_data_cb = callback;
  199. /* Switch to BTC context */
  200. bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  201. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  202. }
  203. #endif /* BTC_AV_SRC_INCLUDED */
  204. #endif /* #if BTC_AV_INCLUDED */