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