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 "bta_api.h"
28#include "bd.h"
29#include "bta_sys.h"
30#include "bta_ag_api.h"
31#include "bta_ag_int.h"
32#include "gki.h"
33#include <string.h>
34
35/*****************************************************************************
36**  Constants
37*****************************************************************************/
38
39static 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*******************************************************************************/
58tBTA_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    {
66        if (bta_ag_cb.scb[idx].in_use)
67        {
68            APPL_TRACE_ERROR0 ("BTA_AgEnable: FAILED, AG already enabled.");
69            return BTA_FAILURE;
70        }
71    }
72
73    /* register with BTA system manager */
74    GKI_sched_lock();
75    bta_sys_register(BTA_ID_AG, &bta_ag_reg);
76    GKI_sched_unlock();
77
78    if ((p_buf = (tBTA_AG_API_ENABLE *) GKI_getbuf(sizeof(tBTA_AG_API_ENABLE))) != NULL)
79    {
80        p_buf->hdr.event = BTA_AG_API_ENABLE_EVT;
81        p_buf->parse_mode = parse_mode;
82        p_buf->p_cback = p_cback;
83        bta_sys_sendmsg(p_buf);
84    }
85
86    return BTA_SUCCESS;
87
88}
89
90/*******************************************************************************
91**
92** Function         BTA_AgDisable
93**
94** Description      Disable the audio gateway service
95**
96**
97** Returns          void
98**
99*******************************************************************************/
100void BTA_AgDisable(void)
101{
102    BT_HDR  *p_buf;
103
104    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
105    {
106        p_buf->event = BTA_AG_API_DISABLE_EVT;
107        bta_sys_sendmsg(p_buf);
108    }
109}
110
111/*******************************************************************************
112**
113** Function         BTA_AgRegister
114**
115** Description      Register an Audio Gateway service.
116**
117**
118** Returns          void
119**
120*******************************************************************************/
121void BTA_AgRegister(tBTA_SERVICE_MASK services, tBTA_SEC sec_mask,tBTA_AG_FEAT features,
122                  char * p_service_names[], UINT8 app_id)
123{
124    tBTA_AG_API_REGISTER    *p_buf;
125    int                     i;
126
127    if ((p_buf = (tBTA_AG_API_REGISTER *) GKI_getbuf(sizeof(tBTA_AG_API_REGISTER))) != NULL)
128    {
129        p_buf->hdr.event = BTA_AG_API_REGISTER_EVT;
130        p_buf->features = features;
131        p_buf->sec_mask = sec_mask;
132        p_buf->services = services;
133        p_buf->app_id = app_id;
134        for (i = 0; i < BTA_AG_NUM_IDX; i++)
135        {
136            if(p_service_names[i])
137            {
138                BCM_STRNCPY_S(p_buf->p_name[i], BTA_SERVICE_NAME_LEN+1, p_service_names[i], BTA_SERVICE_NAME_LEN);
139                p_buf->p_name[i][BTA_SERVICE_NAME_LEN] = 0;
140            }
141            else
142            {
143                p_buf->p_name[i][0] = 0;
144            }
145        }
146        bta_sys_sendmsg(p_buf);
147    }
148}
149
150/*******************************************************************************
151**
152** Function         BTA_AgDeregister
153**
154** Description      Deregister an audio gateway service.
155**
156**
157** Returns          void
158**
159*******************************************************************************/
160void BTA_AgDeregister(UINT16 handle)
161{
162    BT_HDR  *p_buf;
163
164    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
165    {
166        p_buf->event = BTA_AG_API_DEREGISTER_EVT;
167        p_buf->layer_specific = handle;
168        bta_sys_sendmsg(p_buf);
169    }
170}
171
172/*******************************************************************************
173**
174** Function         BTA_AgOpen
175**
176** Description      Opens a connection to a headset or hands-free device.
177**                  When connection is open callback function is called
178**                  with a BTA_AG_OPEN_EVT. Only the data connection is
179**                  opened. The audio connection is not opened.
180**
181**
182** Returns          void
183**
184*******************************************************************************/
185void BTA_AgOpen(UINT16 handle, BD_ADDR bd_addr, tBTA_SEC sec_mask, tBTA_SERVICE_MASK services)
186{
187    tBTA_AG_API_OPEN  *p_buf;
188
189    if ((p_buf = (tBTA_AG_API_OPEN *) GKI_getbuf(sizeof(tBTA_AG_API_OPEN))) != NULL)
190    {
191        p_buf->hdr.event = BTA_AG_API_OPEN_EVT;
192        p_buf->hdr.layer_specific = handle;
193        bdcpy(p_buf->bd_addr, bd_addr);
194        p_buf->services = services;
195        p_buf->sec_mask = sec_mask;
196        bta_sys_sendmsg(p_buf);
197    }
198}
199
200/*******************************************************************************
201**
202** Function         BTA_AgClose
203**
204** Description      Close the current connection to a headset or a handsfree
205**                  Any current audio connection will also be closed.
206**
207**
208** Returns          void
209**
210*******************************************************************************/
211void BTA_AgClose(UINT16 handle)
212{
213    BT_HDR  *p_buf;
214
215    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
216    {
217        p_buf->event = BTA_AG_API_CLOSE_EVT;
218        p_buf->layer_specific = handle;
219        bta_sys_sendmsg(p_buf);
220    }
221}
222
223/*******************************************************************************
224**
225** Function         BTA_AgAudioOpen
226**
227** Description      Opens an audio connection to the currently connected
228**                  headset or hnadsfree.
229**
230**
231** Returns          void
232**
233*******************************************************************************/
234void BTA_AgAudioOpen(UINT16 handle)
235{
236    BT_HDR  *p_buf;
237
238    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
239    {
240        p_buf->event = BTA_AG_API_AUDIO_OPEN_EVT;
241        p_buf->layer_specific = handle;
242        bta_sys_sendmsg(p_buf);
243    }
244}
245
246/*******************************************************************************
247**
248** Function         BTA_AgAudioClose
249**
250** Description      Close the currently active audio connection to a headset
251**                  or hnadsfree. The data connection remains open
252**
253**
254** Returns          void
255**
256*******************************************************************************/
257void BTA_AgAudioClose(UINT16 handle)
258{
259    BT_HDR  *p_buf;
260
261    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
262    {
263        p_buf->event = BTA_AG_API_AUDIO_CLOSE_EVT;
264        p_buf->layer_specific = handle;
265        bta_sys_sendmsg(p_buf);
266    }
267}
268
269
270/*******************************************************************************
271**
272** Function         BTA_AgResult
273**
274** Description      Send an AT result code to a headset or hands-free device.
275**                  This function is only used when the AG parse mode is set
276**                  to BTA_AG_PARSE.
277**
278**
279** Returns          void
280**
281*******************************************************************************/
282void BTA_AgResult(UINT16 handle, tBTA_AG_RES result, tBTA_AG_RES_DATA *p_data)
283{
284    tBTA_AG_API_RESULT  *p_buf;
285
286    if ((p_buf = (tBTA_AG_API_RESULT *) GKI_getbuf(sizeof(tBTA_AG_API_RESULT))) != NULL)
287    {
288        p_buf->hdr.event = BTA_AG_API_RESULT_EVT;
289        p_buf->hdr.layer_specific = handle;
290        p_buf->result = result;
291        if(p_data)
292        {
293            memcpy(&p_buf->data, p_data, sizeof(p_buf->data));
294        }
295        bta_sys_sendmsg(p_buf);
296    }
297}
298
299/*******************************************************************************
300**
301** Function         BTA_AgSetCodec
302**
303** Description      Specify the codec type to be used for the subsequent
304**                  audio connection.
305**
306**
307**
308** Returns          void
309**
310*******************************************************************************/
311void BTA_AgSetCodec(UINT16 handle, tBTA_AG_PEER_CODEC codec)
312{
313    tBTA_AG_API_SETCODEC    *p_buf;
314
315    if ((p_buf = (tBTA_AG_API_SETCODEC *) GKI_getbuf(sizeof(tBTA_AG_API_SETCODEC))) != NULL)
316    {
317        p_buf->hdr.event = BTA_AG_API_SETCODEC_EVT;
318        p_buf->hdr.layer_specific = handle;
319        p_buf->codec = codec;
320        bta_sys_sendmsg(p_buf);
321    }
322}
323
324