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