1 /******************************************************************************
2  *
3  *  Copyright (C) 2004-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 file contains the audio gateway functions controlling the RFCOMM
22  *  connections.
23  *
24  ******************************************************************************/
25 
26 #include <string.h>
27 #include "bta_api.h"
28 #include "bta_sys.h"
29 #include "bta_ag_api.h"
30 #include "bta_ag_int.h"
31 #include "bta_ag_co.h"
32 #include "btm_api.h"
33 #include "port_api.h"
34 #include "rfcdefs.h"
35 #include "bt_common.h"
36 #include "utl.h"
37 
38 /* Event mask for RfCOMM port callback */
39 #define BTA_AG_PORT_EV_MASK         PORT_EV_RXCHAR
40 
41 /* each scb has its own rfcomm callbacks */
42 void bta_ag_port_cback_1(UINT32 code, UINT16 port_handle);
43 void bta_ag_port_cback_2(UINT32 code, UINT16 port_handle);
44 void bta_ag_port_cback_3(UINT32 code, UINT16 port_handle);
45 
46 void bta_ag_mgmt_cback_1(UINT32 code, UINT16 port_handle);
47 void bta_ag_mgmt_cback_2(UINT32 code, UINT16 port_handle);
48 void bta_ag_mgmt_cback_3(UINT32 code, UINT16 port_handle);
49 
50 int bta_ag_data_cback_1(UINT16 port_handle, void *p_data, UINT16 len);
51 int bta_ag_data_cback_2(UINT16 port_handle, void *p_data, UINT16 len);
52 int bta_ag_data_cback_3(UINT16 port_handle, void *p_data, UINT16 len);
53 
54 /* rfcomm callback function tables */
55 typedef tPORT_CALLBACK *tBTA_AG_PORT_CBACK;
56 const tBTA_AG_PORT_CBACK bta_ag_port_cback_tbl[] =
57 {
58     bta_ag_port_cback_1,
59     bta_ag_port_cback_2,
60     bta_ag_port_cback_3
61 };
62 
63 const tBTA_AG_PORT_CBACK bta_ag_mgmt_cback_tbl[] =
64 {
65     bta_ag_mgmt_cback_1,
66     bta_ag_mgmt_cback_2,
67     bta_ag_mgmt_cback_3
68 };
69 
70 typedef tPORT_DATA_CALLBACK *tBTA_AG_DATA_CBACK;
71 const tBTA_AG_DATA_CBACK bta_ag_data_cback_tbl[] =
72 {
73     bta_ag_data_cback_1,
74     bta_ag_data_cback_2,
75     bta_ag_data_cback_3
76 };
77 
78 /*******************************************************************************
79 **
80 ** Function         bta_ag_port_cback
81 **
82 ** Description      RFCOMM Port callback
83 **
84 **
85 ** Returns          void
86 **
87 *******************************************************************************/
bta_ag_port_cback(UINT32 code,UINT16 port_handle,UINT16 handle)88 static void bta_ag_port_cback(UINT32 code, UINT16 port_handle, UINT16 handle)
89 {
90     tBTA_AG_SCB *p_scb;
91     UNUSED(code);
92 
93     if ((p_scb = bta_ag_scb_by_idx(handle)) != NULL)
94     {
95         /* ignore port events for port handles other than connected handle */
96         if (port_handle != p_scb->conn_handle)
97         {
98             APPL_TRACE_DEBUG("ag_port_cback ignoring handle:%d conn_handle = %d other handle = %d",
99                               port_handle, p_scb->conn_handle, handle);
100             return;
101         }
102 
103         BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
104         p_buf->event = BTA_AG_RFC_DATA_EVT;
105         p_buf->layer_specific = handle;
106         bta_sys_sendmsg(p_buf);
107     }
108 }
109 
110 /*******************************************************************************
111 **
112 ** Function         bta_ag_mgmt_cback
113 **
114 ** Description      RFCOMM management callback
115 **
116 **
117 ** Returns          void
118 **
119 *******************************************************************************/
bta_ag_mgmt_cback(UINT32 code,UINT16 port_handle,UINT16 handle)120 static void bta_ag_mgmt_cback(UINT32 code, UINT16 port_handle, UINT16 handle)
121 {
122     tBTA_AG_SCB     *p_scb;
123     UINT16          event;
124     UINT8           i;
125     BOOLEAN         found_handle = FALSE;
126 
127     APPL_TRACE_DEBUG("ag_mgmt_cback : code = %d, port_handle = %d, handle = %d",
128                         code, port_handle, handle);
129 
130     if ((p_scb = bta_ag_scb_by_idx(handle)) != NULL)
131     {
132         /* ignore close event for port handles other than connected handle */
133         if ((code != PORT_SUCCESS) && (port_handle != p_scb->conn_handle))
134         {
135             APPL_TRACE_DEBUG("ag_mgmt_cback ignoring handle:%d", port_handle);
136             return;
137         }
138 
139         if (code == PORT_SUCCESS)
140         {
141             if (p_scb->conn_handle)     /* Outgoing connection */
142             {
143                 if (port_handle == p_scb->conn_handle)
144                     found_handle = TRUE;
145             }
146             else                        /* Incoming connection */
147             {
148                 for (i = 0; i < BTA_AG_NUM_IDX; i++)
149                 {
150                     if (port_handle == p_scb->serv_handle[i])
151                         found_handle = TRUE;
152                 }
153             }
154 
155             if (!found_handle)
156             {
157                 APPL_TRACE_ERROR ("bta_ag_mgmt_cback: PORT_SUCCESS, ignoring handle = %d", port_handle);
158                 return;
159             }
160 
161             event = BTA_AG_RFC_OPEN_EVT;
162         }
163         /* distinguish server close events */
164         else if (port_handle == p_scb->conn_handle)
165         {
166             event = BTA_AG_RFC_CLOSE_EVT;
167         }
168         else
169         {
170             event = BTA_AG_RFC_SRV_CLOSE_EVT;
171         }
172 
173         tBTA_AG_RFC *p_buf = (tBTA_AG_RFC *)osi_malloc(sizeof(tBTA_AG_RFC));
174         p_buf->hdr.event = event;
175         p_buf->hdr.layer_specific = handle;
176         p_buf->port_handle = port_handle;
177         bta_sys_sendmsg(p_buf);
178     }
179 }
180 
181 /*******************************************************************************
182 **
183 ** Function         bta_ag_data_cback
184 **
185 ** Description      RFCOMM data callback
186 **
187 **
188 ** Returns          void
189 **
190 *******************************************************************************/
bta_ag_data_cback(UINT16 port_handle,void * p_data,UINT16 len,UINT16 handle)191 static int bta_ag_data_cback(UINT16 port_handle, void *p_data, UINT16 len, UINT16 handle)
192 {
193     UNUSED(port_handle);
194 
195     /* call data call-out directly */
196     bta_ag_co_tx_write(handle, (UINT8 *) p_data, len);
197     return 0;
198 }
199 
200 /*******************************************************************************
201 **
202 ** Function         bta_ag_port_cback_1 to 3
203 **                  bta_ag_mgmt_cback_1 to 3
204 **
205 ** Description      RFCOMM callback functions.  This is an easy way to
206 **                  distinguish scb from the callback.
207 **
208 **
209 ** Returns          void
210 **
211 *******************************************************************************/
bta_ag_mgmt_cback_1(UINT32 code,UINT16 handle)212 void bta_ag_mgmt_cback_1(UINT32 code, UINT16 handle) {bta_ag_mgmt_cback(code, handle, 1);}
bta_ag_mgmt_cback_2(UINT32 code,UINT16 handle)213 void bta_ag_mgmt_cback_2(UINT32 code, UINT16 handle) {bta_ag_mgmt_cback(code, handle, 2);}
bta_ag_mgmt_cback_3(UINT32 code,UINT16 handle)214 void bta_ag_mgmt_cback_3(UINT32 code, UINT16 handle) {bta_ag_mgmt_cback(code, handle, 3);}
bta_ag_port_cback_1(UINT32 code,UINT16 handle)215 void bta_ag_port_cback_1(UINT32 code, UINT16 handle) {bta_ag_port_cback(code, handle, 1);}
bta_ag_port_cback_2(UINT32 code,UINT16 handle)216 void bta_ag_port_cback_2(UINT32 code, UINT16 handle) {bta_ag_port_cback(code, handle, 2);}
bta_ag_port_cback_3(UINT32 code,UINT16 handle)217 void bta_ag_port_cback_3(UINT32 code, UINT16 handle) {bta_ag_port_cback(code, handle, 3);}
218 
219 /*******************************************************************************
220 **
221 ** Function         bta_ag_data_cback_1 to 3
222 **
223 ** Description      RFCOMM data callback functions.  This is an easy way to
224 **                  distinguish scb from the callback.
225 **
226 **
227 ** Returns          void
228 **
229 *******************************************************************************/
bta_ag_data_cback_1(UINT16 port_handle,void * p_data,UINT16 len)230 int bta_ag_data_cback_1(UINT16 port_handle, void *p_data, UINT16 len)
231 {
232     return bta_ag_data_cback(port_handle, p_data, len, 1);
233 }
bta_ag_data_cback_2(UINT16 port_handle,void * p_data,UINT16 len)234 int bta_ag_data_cback_2(UINT16 port_handle, void *p_data, UINT16 len)
235 {
236     return bta_ag_data_cback(port_handle, p_data, len, 2);
237 }
bta_ag_data_cback_3(UINT16 port_handle,void * p_data,UINT16 len)238 int bta_ag_data_cback_3(UINT16 port_handle, void *p_data, UINT16 len)
239 {
240     return bta_ag_data_cback(port_handle, p_data, len, 3);
241 }
242 
243 /*******************************************************************************
244 **
245 ** Function         bta_ag_setup_port
246 **
247 ** Description      Setup RFCOMM port for use by AG.
248 **
249 **
250 ** Returns          void
251 **
252 *******************************************************************************/
bta_ag_setup_port(tBTA_AG_SCB * p_scb,UINT16 handle)253 void bta_ag_setup_port(tBTA_AG_SCB *p_scb, UINT16 handle)
254 {
255     UINT16 i = bta_ag_scb_to_idx(p_scb) - 1;
256 
257     /* set up data callback if using pass through mode */
258     if (bta_ag_cb.parse_mode == BTA_AG_PASS_THROUGH)
259     {
260         PORT_SetDataCallback(handle, bta_ag_data_cback_tbl[i]);
261     }
262 
263     PORT_SetEventMask(handle, BTA_AG_PORT_EV_MASK);
264     PORT_SetEventCallback(handle, bta_ag_port_cback_tbl[i]);
265 }
266 
267 /*******************************************************************************
268 **
269 ** Function         bta_ag_start_servers
270 **
271 ** Description      Setup RFCOMM servers for use by AG.
272 **
273 **
274 ** Returns          void
275 **
276 *******************************************************************************/
bta_ag_start_servers(tBTA_AG_SCB * p_scb,tBTA_SERVICE_MASK services)277 void bta_ag_start_servers(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK services)
278 {
279     int i;
280     int bta_ag_port_status;
281 
282     services >>= BTA_HSP_SERVICE_ID;
283     for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1)
284     {
285         /* if service is set in mask */
286         if (services & 1)
287         {
288             BTM_SetSecurityLevel(FALSE, "", bta_ag_sec_id[i], p_scb->serv_sec_mask,
289                 BT_PSM_RFCOMM, BTM_SEC_PROTO_RFCOMM, bta_ag_cb.profile[i].scn);
290 
291             bta_ag_port_status =  RFCOMM_CreateConnection(bta_ag_uuid[i], bta_ag_cb.profile[i].scn,
292                 TRUE, BTA_AG_MTU, (UINT8 *) bd_addr_any, &(p_scb->serv_handle[i]),
293                 bta_ag_mgmt_cback_tbl[bta_ag_scb_to_idx(p_scb) - 1]);
294 
295             if( bta_ag_port_status  == PORT_SUCCESS )
296             {
297                 bta_ag_setup_port(p_scb, p_scb->serv_handle[i]);
298             }
299             else
300             {
301                 /* TODO: CR#137125 to handle to error properly */
302                 APPL_TRACE_DEBUG("bta_ag_start_servers: RFCOMM_CreateConnection returned error:%d", bta_ag_port_status);
303             }
304         }
305     }
306 }
307 
308 /*******************************************************************************
309 **
310 ** Function         bta_ag_close_servers
311 **
312 ** Description      Close RFCOMM servers port for use by AG.
313 **
314 **
315 ** Returns          void
316 **
317 *******************************************************************************/
bta_ag_close_servers(tBTA_AG_SCB * p_scb,tBTA_SERVICE_MASK services)318 void bta_ag_close_servers(tBTA_AG_SCB *p_scb, tBTA_SERVICE_MASK services)
319 {
320     int i;
321 
322     services >>= BTA_HSP_SERVICE_ID;
323     for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++, services >>= 1)
324     {
325         /* if service is set in mask */
326         if (services & 1)
327         {
328             RFCOMM_RemoveServer(p_scb->serv_handle[i]);
329             p_scb->serv_handle[i] = 0;
330         }
331     }
332 }
333 
334 /*******************************************************************************
335 **
336 ** Function         bta_ag_is_server_closed
337 **
338 ** Description      Returns TRUE if all servers are closed.
339 **
340 **
341 ** Returns          TRUE if all servers are closed, FALSE otherwise
342 **
343 *******************************************************************************/
bta_ag_is_server_closed(tBTA_AG_SCB * p_scb)344 BOOLEAN bta_ag_is_server_closed (tBTA_AG_SCB *p_scb)
345 {
346     UINT8 xx;
347     BOOLEAN is_closed = TRUE;
348 
349     for (xx = 0; xx < BTA_AG_NUM_IDX; xx++)
350     {
351         if (p_scb->serv_handle[xx] != 0)
352             is_closed = FALSE;
353     }
354 
355     return is_closed;
356 }
357 
358 /*******************************************************************************
359 **
360 ** Function         bta_ag_rfc_do_open
361 **
362 ** Description      Open an RFCOMM connection to the peer device.
363 **
364 **
365 ** Returns          void
366 **
367 *******************************************************************************/
bta_ag_rfc_do_open(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)368 void bta_ag_rfc_do_open(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
369 {
370     BTM_SetSecurityLevel(TRUE, "", bta_ag_sec_id[p_scb->conn_service],
371         p_scb->cli_sec_mask, BT_PSM_RFCOMM, BTM_SEC_PROTO_RFCOMM, p_scb->peer_scn);
372 
373     if (RFCOMM_CreateConnection(bta_ag_uuid[p_scb->conn_service], p_scb->peer_scn,
374             FALSE, BTA_AG_MTU, p_scb->peer_addr, &(p_scb->conn_handle),
375             bta_ag_mgmt_cback_tbl[bta_ag_scb_to_idx(p_scb) - 1]) == PORT_SUCCESS)
376     {
377         bta_ag_setup_port(p_scb, p_scb->conn_handle);
378         APPL_TRACE_DEBUG("bta_ag_rfc_do_open : conn_handle = %d", p_scb->conn_handle);
379     }
380     /* RFCOMM create connection failed; send ourselves RFCOMM close event */
381     else
382     {
383         bta_ag_sm_execute(p_scb, BTA_AG_RFC_CLOSE_EVT, p_data);
384     }
385 }
386 
387 /*******************************************************************************
388 **
389 ** Function         bta_ag_rfc_do_close
390 **
391 ** Description      Close RFCOMM connection.
392 **
393 **
394 ** Returns          void
395 **
396 *******************************************************************************/
bta_ag_rfc_do_close(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)397 void bta_ag_rfc_do_close(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
398 {
399     UNUSED(p_data);
400 
401     if (p_scb->conn_handle) {
402         RFCOMM_RemoveConnection(p_scb->conn_handle);
403     } else {
404         /* Close API was called while AG is in Opening state.               */
405         /* Need to trigger the state machine to send callback to the app    */
406         /* and move back to INIT state.                                     */
407         tBTA_AG_RFC *p_buf = (tBTA_AG_RFC *)osi_malloc(sizeof(tBTA_AG_RFC));
408         p_buf->hdr.event = BTA_AG_RFC_CLOSE_EVT;
409         p_buf->hdr.layer_specific = bta_ag_scb_to_idx(p_scb);
410         bta_sys_sendmsg(p_buf);
411 
412         /* Cancel SDP if it had been started. */
413         /*
414         if(p_scb->p_disc_db)
415         {
416             (void)SDP_CancelServiceSearch (p_scb->p_disc_db);
417         }
418         */
419     }
420 }
421 
422