esp_a2dp_api.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. esp_err_t esp_a2d_register_callback(esp_a2d_cb_t callback)
  22. {
  23. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  24. return ESP_ERR_INVALID_STATE;
  25. }
  26. if (callback == NULL) {
  27. return ESP_FAIL;
  28. }
  29. btc_profile_cb_set(BTC_PID_A2DP, callback);
  30. return ESP_OK;
  31. }
  32. esp_err_t esp_a2d_sink_init(void)
  33. {
  34. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  35. return ESP_ERR_INVALID_STATE;
  36. }
  37. btc_msg_t msg;
  38. msg.sig = BTC_SIG_API_CALL;
  39. msg.pid = BTC_PID_A2DP;
  40. msg.act = BTC_AV_SINK_API_INIT_EVT;
  41. /* Switch to BTC context */
  42. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  43. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  44. }
  45. esp_err_t esp_a2d_sink_deinit(void)
  46. {
  47. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  48. return ESP_ERR_INVALID_STATE;
  49. }
  50. btc_msg_t msg;
  51. msg.sig = BTC_SIG_API_CALL;
  52. msg.pid = BTC_PID_A2DP;
  53. msg.act = BTC_AV_SINK_API_DEINIT_EVT;
  54. /* Switch to BTC context */
  55. bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
  56. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  57. }
  58. esp_err_t esp_a2d_register_data_callback(esp_a2d_data_cb_t callback)
  59. {
  60. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  61. return ESP_ERR_INVALID_STATE;
  62. }
  63. btc_msg_t msg;
  64. msg.sig = BTC_SIG_API_CALL;
  65. msg.pid = BTC_PID_A2DP;
  66. msg.act = BTC_AV_SINK_API_REG_DATA_CB_EVT;
  67. btc_av_args_t arg;
  68. memset(&arg, 0, sizeof(btc_av_args_t));
  69. arg.data_cb = callback;
  70. /* Switch to BTC context */
  71. bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_msg_t), NULL);
  72. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  73. }
  74. esp_err_t esp_a2d_sink_connect(esp_bd_addr_t remote_bda)
  75. {
  76. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  77. return ESP_ERR_INVALID_STATE;
  78. }
  79. bt_status_t stat;
  80. btc_av_args_t arg;
  81. btc_msg_t msg;
  82. msg.sig = BTC_SIG_API_CALL;
  83. msg.pid = BTC_PID_A2DP;
  84. msg.act = BTC_AV_SINK_API_CONNECT_EVT;
  85. memset(&arg, 0, sizeof(btc_av_args_t));
  86. /* Switch to BTC context */
  87. memcpy(&(arg.connect), remote_bda, sizeof(bt_bdaddr_t));
  88. stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
  89. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  90. }
  91. esp_err_t esp_a2d_sink_disconnect(esp_bd_addr_t remote_bda)
  92. {
  93. if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
  94. return ESP_ERR_INVALID_STATE;
  95. }
  96. bt_status_t stat;
  97. btc_msg_t msg;
  98. msg.sig = BTC_SIG_API_CALL;
  99. msg.pid = BTC_PID_A2DP;
  100. msg.act = BTC_AV_SINK_API_DISCONNECT_EVT;
  101. /* Switch to BTC context */
  102. stat = btc_transfer_context(&msg, NULL, 0, NULL);
  103. return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
  104. }
  105. #endif /* #if BTC_AV_INCLUDED */