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 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "common/bt_target.h"
16 #include <string.h>
17 #include "esp_err.h"
18 #include "esp_a2dp_api.h"
19 #include "esp_bt_main.h"
20 #include "btc/btc_manage.h"
21 #include "btc_av.h"
22 
23 #if BTC_AV_INCLUDED
24 
25 #if BTC_AV_SINK_INCLUDED
esp_a2d_sink_init(void)26 esp_err_t esp_a2d_sink_init(void)
27 {
28     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
29         return ESP_ERR_INVALID_STATE;
30     }
31 
32     btc_msg_t msg;
33 
34     msg.sig = BTC_SIG_API_CALL;
35     msg.pid = BTC_PID_A2DP;
36     msg.act = BTC_AV_SINK_API_INIT_EVT;
37 
38     /* Switch to BTC context */
39     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
40     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
41 }
42 
esp_a2d_sink_deinit(void)43 esp_err_t esp_a2d_sink_deinit(void)
44 {
45     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
46         return ESP_ERR_INVALID_STATE;
47     }
48 
49     btc_msg_t msg;
50 
51     msg.sig = BTC_SIG_API_CALL;
52     msg.pid = BTC_PID_A2DP;
53     msg.act = BTC_AV_SINK_API_DEINIT_EVT;
54 
55     /* Switch to BTC context */
56     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
57     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
58 }
59 
esp_a2d_sink_register_data_callback(esp_a2d_sink_data_cb_t callback)60 esp_err_t esp_a2d_sink_register_data_callback(esp_a2d_sink_data_cb_t callback)
61 {
62     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
63         return ESP_ERR_INVALID_STATE;
64     }
65 
66     btc_msg_t msg;
67     msg.sig = BTC_SIG_API_CALL;
68     msg.pid = BTC_PID_A2DP;
69     msg.act = BTC_AV_SINK_API_REG_DATA_CB_EVT;
70 
71     btc_av_args_t arg;
72     memset(&arg, 0, sizeof(btc_av_args_t));
73     arg.data_cb = callback;
74 
75     /* Switch to BTC context */
76     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
77     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
78 }
79 
esp_a2d_sink_connect(esp_bd_addr_t remote_bda)80 esp_err_t esp_a2d_sink_connect(esp_bd_addr_t remote_bda)
81 {
82     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
83         return ESP_ERR_INVALID_STATE;
84     }
85 
86     bt_status_t stat;
87     btc_av_args_t arg;
88     btc_msg_t msg;
89 
90     msg.sig = BTC_SIG_API_CALL;
91     msg.pid = BTC_PID_A2DP;
92     msg.act = BTC_AV_SINK_API_CONNECT_EVT;
93 
94     memset(&arg, 0, sizeof(btc_av_args_t));
95 
96     /* Switch to BTC context */
97     memcpy(&(arg.connect), remote_bda, sizeof(bt_bdaddr_t));
98     stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
99     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
100 }
101 
esp_a2d_sink_disconnect(esp_bd_addr_t remote_bda)102 esp_err_t esp_a2d_sink_disconnect(esp_bd_addr_t remote_bda)
103 {
104     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
105         return ESP_ERR_INVALID_STATE;
106     }
107 
108     bt_status_t stat;
109     btc_av_args_t arg;
110     btc_msg_t msg;
111 
112     msg.sig = BTC_SIG_API_CALL;
113     msg.pid = BTC_PID_A2DP;
114     msg.act = BTC_AV_SINK_API_DISCONNECT_EVT;
115 
116     /* Switch to BTC context */
117     memcpy(&(arg.disconn), remote_bda, sizeof(bt_bdaddr_t));
118     stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
119     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
120 }
121 
122 #endif /* BTC_AV_SINK_INCLUDED */
123 
esp_a2d_register_callback(esp_a2d_cb_t callback)124 esp_err_t esp_a2d_register_callback(esp_a2d_cb_t callback)
125 {
126     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
127         return ESP_ERR_INVALID_STATE;
128     }
129 
130     if (callback == NULL) {
131         return ESP_FAIL;
132     }
133 
134     btc_profile_cb_set(BTC_PID_A2DP, callback);
135     return ESP_OK;
136 }
137 
esp_a2d_media_ctrl(esp_a2d_media_ctrl_t ctrl)138 esp_err_t esp_a2d_media_ctrl(esp_a2d_media_ctrl_t ctrl)
139 {
140     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
141         return ESP_ERR_INVALID_STATE;
142     }
143 
144     bt_status_t stat;
145     btc_av_args_t arg;
146     btc_msg_t msg;
147 
148     msg.sig = BTC_SIG_API_CALL;
149     msg.pid = BTC_PID_A2DP;
150     msg.act = BTC_AV_API_MEDIA_CTRL_EVT;
151 
152     memset(&arg, 0, sizeof(btc_av_args_t));
153 
154     /* Switch to BTC context */
155     arg.ctrl = ctrl;
156     stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
157     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
158 }
159 
160 #if BTC_AV_SRC_INCLUDED
esp_a2d_source_init(void)161 esp_err_t esp_a2d_source_init(void)
162 {
163     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
164         return ESP_ERR_INVALID_STATE;
165     }
166 
167     btc_msg_t msg;
168 
169     msg.sig = BTC_SIG_API_CALL;
170     msg.pid = BTC_PID_A2DP;
171     msg.act = BTC_AV_SRC_API_INIT_EVT;
172 
173     /* Switch to BTC context */
174     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
175     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
176 }
177 
esp_a2d_source_deinit(void)178 esp_err_t esp_a2d_source_deinit(void)
179 {
180     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
181         return ESP_ERR_INVALID_STATE;
182     }
183 
184     btc_msg_t msg;
185 
186     msg.sig = BTC_SIG_API_CALL;
187     msg.pid = BTC_PID_A2DP;
188     msg.act = BTC_AV_SRC_API_DEINIT_EVT;
189 
190     /* Switch to BTC context */
191     bt_status_t stat = btc_transfer_context(&msg, NULL, 0, NULL);
192     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
193 }
194 
esp_a2d_source_connect(esp_bd_addr_t remote_bda)195 esp_err_t esp_a2d_source_connect(esp_bd_addr_t remote_bda)
196 {
197     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
198         return ESP_ERR_INVALID_STATE;
199     }
200 
201     bt_status_t stat;
202     btc_av_args_t arg;
203     btc_msg_t msg;
204 
205     msg.sig = BTC_SIG_API_CALL;
206     msg.pid = BTC_PID_A2DP;
207     msg.act = BTC_AV_SRC_API_CONNECT_EVT;
208 
209     memset(&arg, 0, sizeof(btc_av_args_t));
210 
211     /* Switch to BTC context */
212     memcpy(&(arg.src_connect), remote_bda, sizeof(bt_bdaddr_t));
213     stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
214     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
215 }
216 
esp_a2d_source_disconnect(esp_bd_addr_t remote_bda)217 esp_err_t esp_a2d_source_disconnect(esp_bd_addr_t remote_bda)
218 {
219     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
220         return ESP_ERR_INVALID_STATE;
221     }
222 
223     bt_status_t stat;
224     btc_av_args_t arg;
225     btc_msg_t msg;
226 
227     msg.sig = BTC_SIG_API_CALL;
228     msg.pid = BTC_PID_A2DP;
229     msg.act = BTC_AV_SRC_API_DISCONNECT_EVT;
230 
231     memset(&arg, 0, sizeof(btc_av_args_t));
232 
233     /* Switch to BTC context */
234     memcpy(&(arg.src_disconn), remote_bda, sizeof(bt_bdaddr_t));
235     stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
236     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
237 }
238 
esp_a2d_source_register_data_callback(esp_a2d_source_data_cb_t callback)239 esp_err_t esp_a2d_source_register_data_callback(esp_a2d_source_data_cb_t callback)
240 {
241     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
242         return ESP_ERR_INVALID_STATE;
243     }
244 
245     btc_msg_t msg;
246     msg.sig = BTC_SIG_API_CALL;
247     msg.pid = BTC_PID_A2DP;
248     msg.act = BTC_AV_SRC_API_REG_DATA_CB_EVT;
249 
250     btc_av_args_t arg;
251     memset(&arg, 0, sizeof(btc_av_args_t));
252     arg.src_data_cb = callback;
253 
254     /* Switch to BTC context */
255     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
256     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
257 }
258 
259 #endif /* BTC_AV_SRC_INCLUDED */
260 
261 #endif /* #if BTC_AV_INCLUDED */
262