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