1 /******************************************************************************
2 *
3 * Copyright (C) 2003-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This is the implementation of the API for the audio gateway (AG)
22 * subsystem of BTA, Broadcom's Bluetooth application layer for mobile
23 * phones.
24 *
25 ******************************************************************************/
26
27 #include <string.h>
28 #include "bta/bta_api.h"
29 #include "bta/bta_sys.h"
30 #include "bta/bta_ag_api.h"
31 #include "bta_ag_int.h"
32 #include "osi/allocator.h"
33
34 #if (BTA_AG_INCLUDED == TRUE)
35 /*****************************************************************************
36 ** Constants
37 *****************************************************************************/
38
39 static const tBTA_SYS_REG bta_ag_reg =
40 {
41 bta_ag_hdl_event,
42 BTA_AgDisable
43 };
44
45 /*******************************************************************************
46 **
47 ** Function BTA_AgEnable
48 **
49 ** Description Enable the audio gateway service. When the enable
50 ** operation is complete the callback function will be
51 ** called with a BTA_AG_ENABLE_EVT. This function must
52 ** be called before other function in the AG API are
53 ** called.
54 **
55 ** Returns BTA_SUCCESS if OK, BTA_FAILURE otherwise.
56 **
57 *******************************************************************************/
BTA_AgEnable(tBTA_AG_PARSE_MODE parse_mode,tBTA_AG_CBACK * p_cback)58 tBTA_STATUS BTA_AgEnable(tBTA_AG_PARSE_MODE parse_mode, tBTA_AG_CBACK *p_cback)
59 {
60 tBTA_AG_API_ENABLE *p_buf;
61 UINT8 idx;
62
63 /* Error if AG is already enabled, or AG is in the middle of disabling. */
64 for (idx = 0; idx < BTA_AG_NUM_SCB; idx++) {
65 if (bta_ag_cb.scb[idx].in_use) {
66 APPL_TRACE_ERROR ("BTA_AgEnable: FAILED, AG already enabled.");
67 return BTA_FAILURE;
68 }
69 }
70 /* register with BTA system manager */
71 bta_sys_register(BTA_ID_AG, &bta_ag_reg);
72
73 if ((p_buf = (tBTA_AG_API_ENABLE *) osi_malloc(sizeof(tBTA_AG_API_ENABLE))) != NULL) {
74 p_buf->hdr.event = BTA_AG_API_ENABLE_EVT;
75 p_buf->parse_mode = parse_mode;
76 p_buf->p_cback = p_cback;
77 bta_sys_sendmsg(p_buf);
78 }
79 return BTA_SUCCESS;
80 }
81
82 /*******************************************************************************
83 **
84 ** Function BTA_AgDisable
85 **
86 ** Description Disable the audio gateway service
87 **
88 **
89 ** Returns void
90 **
91 *******************************************************************************/
BTA_AgDisable(void)92 void BTA_AgDisable(void)
93 {
94 BT_HDR *p_buf;
95 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
96 p_buf->event = BTA_AG_API_DISABLE_EVT;
97 bta_sys_sendmsg(p_buf);
98 }
99 }
100
101 /*******************************************************************************
102 **
103 ** Function BTA_AgRegister
104 **
105 ** Description Register an Audio Gateway service.
106 **
107 **
108 ** Returns void
109 **
110 *******************************************************************************/
BTA_AgRegister(tBTA_SERVICE_MASK services,tBTA_SEC sec_mask,tBTA_AG_FEAT features,char * p_service_names[],UINT8 app_id)111 void BTA_AgRegister(tBTA_SERVICE_MASK services, tBTA_SEC sec_mask,tBTA_AG_FEAT features,
112 char * p_service_names[], UINT8 app_id)
113 {
114 tBTA_AG_API_REGISTER *p_buf;
115 int i;
116
117 if ((p_buf = (tBTA_AG_API_REGISTER *) osi_malloc(sizeof(tBTA_AG_API_REGISTER))) != NULL) {
118 p_buf->hdr.event = BTA_AG_API_REGISTER_EVT;
119 p_buf->features = features;
120 p_buf->sec_mask = sec_mask;
121 p_buf->services = services;
122 p_buf->app_id = app_id;
123 for (i = 0; i < BTA_AG_NUM_IDX; i++) {
124 if(p_service_names[i]) {
125 BCM_STRNCPY_S(p_buf->p_name[i], p_service_names[i], BTA_SERVICE_NAME_LEN);
126 p_buf->p_name[i][BTA_SERVICE_NAME_LEN] = 0;
127 } else {
128 p_buf->p_name[i][0] = 0;
129 }
130 }
131 bta_sys_sendmsg(p_buf);
132 }
133 }
134
135 /*******************************************************************************
136 **
137 ** Function BTA_AgDeregister
138 **
139 ** Description Deregister an audio gateway service.
140 **
141 **
142 ** Returns void
143 **
144 *******************************************************************************/
BTA_AgDeregister(UINT16 handle)145 void BTA_AgDeregister(UINT16 handle)
146 {
147 BT_HDR *p_buf;
148 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
149 p_buf->event = BTA_AG_API_DEREGISTER_EVT;
150 p_buf->layer_specific = handle;
151 bta_sys_sendmsg(p_buf);
152 }
153 }
154
155 /*******************************************************************************
156 **
157 ** Function BTA_AgOpen
158 **
159 ** Description Opens a connection to a headset or hands-free device.
160 ** When connection is open callback function is called
161 ** with a BTA_AG_OPEN_EVT. Only the data connection is
162 ** opened. The audio connection is not opened.
163 **
164 **
165 ** Returns void
166 **
167 *******************************************************************************/
BTA_AgOpen(UINT16 handle,BD_ADDR bd_addr,tBTA_SEC sec_mask,tBTA_SERVICE_MASK services)168 void BTA_AgOpen(UINT16 handle, BD_ADDR bd_addr, tBTA_SEC sec_mask, tBTA_SERVICE_MASK services)
169 {
170 tBTA_AG_API_OPEN *p_buf;
171
172 if ((p_buf = (tBTA_AG_API_OPEN *) osi_malloc(sizeof(tBTA_AG_API_OPEN))) != NULL) {
173 p_buf->hdr.event = BTA_AG_API_OPEN_EVT;
174 p_buf->hdr.layer_specific = handle;
175 bdcpy(p_buf->bd_addr, bd_addr);
176 p_buf->services = services;
177 p_buf->sec_mask = sec_mask;
178 bta_sys_sendmsg(p_buf);
179 }
180 }
181
182 /*******************************************************************************
183 **
184 ** Function BTA_AgClose
185 **
186 ** Description Close the current connection to a headset or a handsfree
187 ** Any current audio connection will also be closed.
188 **
189 **
190 ** Returns void
191 **
192 *******************************************************************************/
BTA_AgClose(UINT16 handle)193 void BTA_AgClose(UINT16 handle)
194 {
195 BT_HDR *p_buf;
196
197 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
198 p_buf->event = BTA_AG_API_CLOSE_EVT;
199 p_buf->layer_specific = handle;
200 bta_sys_sendmsg(p_buf);
201 }
202 }
203
204 /*******************************************************************************
205 **
206 ** Function BTA_AgAudioOpen
207 **
208 ** Description Opens an audio connection to the currently connected
209 ** headset or handsfree.
210 **
211 **
212 ** Returns void
213 **
214 *******************************************************************************/
BTA_AgAudioOpen(UINT16 handle)215 void BTA_AgAudioOpen(UINT16 handle)
216 {
217 BT_HDR *p_buf;
218
219 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
220 p_buf->event = BTA_AG_API_AUDIO_OPEN_EVT;
221 p_buf->layer_specific = handle;
222 bta_sys_sendmsg(p_buf);
223 }
224 }
225
226 /*******************************************************************************
227 **
228 ** Function BTA_AgAudioClose
229 **
230 ** Description Close the currently active audio connection to a headset
231 ** or handsfree. The data connection remains open
232 **
233 **
234 ** Returns void
235 **
236 *******************************************************************************/
BTA_AgAudioClose(UINT16 handle)237 void BTA_AgAudioClose(UINT16 handle)
238 {
239 BT_HDR *p_buf;
240
241 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
242 p_buf->event = BTA_AG_API_AUDIO_CLOSE_EVT;
243 p_buf->layer_specific = handle;
244 bta_sys_sendmsg(p_buf);
245 }
246 }
247
248
249 /*******************************************************************************
250 **
251 ** Function BTA_AgResult
252 **
253 ** Description Send an AT result code to a headset or hands-free device.
254 ** This function is only used when the AG parse mode is set
255 ** to BTA_AG_PARSE.
256 **
257 **
258 ** Returns void
259 **
260 *******************************************************************************/
BTA_AgResult(UINT16 handle,tBTA_AG_RES result,tBTA_AG_RES_DATA * p_data)261 void BTA_AgResult(UINT16 handle, tBTA_AG_RES result, tBTA_AG_RES_DATA *p_data)
262 {
263 tBTA_AG_API_RESULT *p_buf;
264
265 if ((p_buf = (tBTA_AG_API_RESULT *) osi_malloc(sizeof(tBTA_AG_API_RESULT))) != NULL) {
266 p_buf->hdr.event = BTA_AG_API_RESULT_EVT;
267 p_buf->hdr.layer_specific = handle;
268 p_buf->result = result;
269 if(p_data) {
270 memcpy(&p_buf->data, p_data, sizeof(p_buf->data));
271 }
272 bta_sys_sendmsg(p_buf);
273 }
274 }
275
276 /*******************************************************************************
277 **
278 ** Function BTA_AgSetCodec
279 **
280 ** Description Specify the codec type to be used for the subsequent
281 ** audio connection.
282 **
283 **
284 **
285 ** Returns void
286 **
287 *******************************************************************************/
BTA_AgSetCodec(UINT16 handle,tBTA_AG_PEER_CODEC codec)288 void BTA_AgSetCodec(UINT16 handle, tBTA_AG_PEER_CODEC codec)
289 {
290 tBTA_AG_API_SETCODEC *p_buf;
291
292 if ((p_buf = (tBTA_AG_API_SETCODEC *) osi_malloc(sizeof(tBTA_AG_API_SETCODEC))) != NULL) {
293 p_buf->hdr.event = BTA_AG_API_SETCODEC_EVT;
294 p_buf->hdr.layer_specific = handle;
295 p_buf->codec = codec;
296 bta_sys_sendmsg(p_buf);
297 }
298 }
299
300 #if (BTM_SCO_HCI_INCLUDED == TRUE )
301 /************************************************************************************************
302 * Function BTA_AgCiData
303 *
304 * Description Trigger the lower-layer to fetch and send audio data. This function is only
305 * only used in the case that Voice Over HCI is enabled. Precondition is that
306 * the HFP audio connection is connected. After this function is called, lower
307 * layer will invoke esp_hf_client_outgoing_data_cb_t to fetch data
308 *
309 ***********************************************************************************************/
BTA_AgCiData(UINT16 handle)310 void BTA_AgCiData(UINT16 handle)
311 {
312 BT_HDR *p_buf;
313 tBTA_AG_SCB *p_scb;
314 if ((p_scb = bta_ag_scb_by_idx(handle)) != NULL && (p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR))) != NULL) {
315 p_buf->event = BTA_AG_CI_SCO_DATA_EVT;
316 p_buf->layer_specific = handle;
317 bta_sys_sendmsg(p_buf);
318 }
319 }
320 #endif /* #if (BTM_SCO_HCI_INCLUDED == TRUE ) */
321
322 #endif /* #if (BTA_AG_INCLUDED == TRUE)*/
323