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 file contains action functions for the audio gateway.
22 *
23 ******************************************************************************/
24 #include <string.h>
25 #include "bta_ag_int.h"
26 #include "bta/bta_sys.h"
27 #include "bta/bta_ag_api.h"
28 #include "bta/bta_ag_co.h"
29 #include "stack/port_api.h"
30 #include "stack/l2c_api.h"
31 #include "bta_dm_int.h"
32 #include "bta/bta_sdp_api.h"
33 #include "bta/utl.h"
34 #include "common/bt_trace.h"
35 #include "osi/allocator.h"
36
37 #if (BTA_AG_INCLUDED == TRUE)
38 /*****************************************************************************
39 ** Constants
40 *****************************************************************************/
41
42 /* maximum length of data to read from RFCOMM */
43 #define BTA_AG_RFC_READ_MAX 512
44
45 /* maximum AT command length */
46 #define BTA_AG_CMD_MAX 512
47
48 const UINT16 bta_ag_uuid[BTA_AG_NUM_IDX] =
49 {
50 UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,
51 UUID_SERVCLASS_AG_HANDSFREE
52 };
53
54 const UINT8 bta_ag_sec_id[BTA_AG_NUM_IDX] =
55 {
56 BTM_SEC_SERVICE_HEADSET_AG,
57 BTM_SEC_SERVICE_AG_HANDSFREE
58 };
59
60 const tBTA_SERVICE_ID bta_ag_svc_id[BTA_AG_NUM_IDX] =
61 {
62 BTA_HSP_SERVICE_ID,
63 BTA_HFP_SERVICE_ID
64 };
65
66 const tBTA_SERVICE_MASK bta_ag_svc_mask[BTA_AG_NUM_IDX] =
67 {
68 BTA_HSP_SERVICE_MASK,
69 BTA_HFP_SERVICE_MASK
70 };
71
72 typedef void (*tBTA_AG_ATCMD_CBACK)(tBTA_AG_SCB *p_scb, UINT16 cmd, UINT8 arg_type,
73 char *p_arg, INT16 int_arg);
74
75 const tBTA_AG_ATCMD_CBACK bta_ag_at_cback_tbl[BTA_AG_NUM_IDX] =
76 {
77 bta_ag_at_hsp_cback,
78 bta_ag_at_hfp_cback
79 };
80
81 /*******************************************************************************
82 **
83 ** Function bta_ag_cback_open
84 **
85 ** Description Send open callback event to application.
86 **
87 **
88 ** Returns void
89 **
90 *******************************************************************************/
bta_ag_cback_open(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data,tBTA_AG_STATUS status)91 static void bta_ag_cback_open(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data, tBTA_AG_STATUS status)
92 {
93 tBTA_AG_OPEN open;
94 /* call app callback with open event */
95 open.hdr.handle = bta_ag_scb_to_idx(p_scb);
96 open.hdr.app_id = p_scb->app_id;
97 open.status = status;
98 open.service_id = bta_ag_svc_id[p_scb->conn_service];
99 if (p_data) {
100 /* if p_data is provided then we need to pick the bd address from the open api structure */
101 bdcpy(open.bd_addr, p_data->api_open.bd_addr);
102 } else {
103 bdcpy(open.bd_addr, p_scb->peer_addr);
104 }
105 (*bta_ag_cb.p_cback)(BTA_AG_OPEN_EVT, (tBTA_AG *) &open);
106 }
107
108 /*******************************************************************************
109 **
110 ** Function bta_ag_register
111 **
112 ** Description This function initializes values of the AG cb and sets up
113 ** the SDP record for the services.
114 **
115 **
116 ** Returns void
117 **
118 *******************************************************************************/
bta_ag_register(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)119 void bta_ag_register(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
120 {
121 tBTA_AG_REGISTER reg;
122 /* initialize control block */
123 p_scb->reg_services = p_data->api_register.services;
124 p_scb->serv_sec_mask = p_data->api_register.sec_mask;
125 p_scb->features = p_data->api_register.features;
126 p_scb->app_id = p_data->api_register.app_id;
127 /* create SDP records */
128 bta_ag_create_records(p_scb, p_data);
129 /* start RFCOMM servers */
130 bta_ag_start_servers(p_scb, p_scb->reg_services);
131 /* call app callback with register event */
132 reg.hdr.handle = bta_ag_scb_to_idx(p_scb);
133 reg.hdr.app_id = p_scb->app_id;
134 reg.status = BTA_AG_SUCCESS;
135 (*bta_ag_cb.p_cback)(BTA_AG_REGISTER_EVT, (tBTA_AG *) ®);
136 }
137
138 /*******************************************************************************
139 **
140 ** Function bta_ag_deregister
141 **
142 ** Description This function removes the sdp records, closes the RFCOMM
143 ** servers, and deallocates the service control block.
144 **
145 **
146 ** Returns void
147 **
148 *******************************************************************************/
bta_ag_deregister(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)149 void bta_ag_deregister(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
150 {
151 /* set dealloc */
152 p_scb->dealloc = TRUE;
153 /* remove sdp records */
154 bta_ag_del_records(p_scb, p_data);
155 /* remove rfcomm servers */
156 bta_ag_close_servers(p_scb, p_scb->reg_services);
157 /* dealloc */
158 bta_ag_scb_dealloc(p_scb);
159 }
160
161 /*******************************************************************************
162 **
163 ** Function bta_ag_start_dereg
164 **
165 ** Description Start a deregister event.
166 **
167 **
168 ** Returns void
169 **
170 *******************************************************************************/
bta_ag_start_dereg(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)171 void bta_ag_start_dereg(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
172 {
173 /* set dealloc */
174 p_scb->dealloc = TRUE;
175 /* remove sdp records */
176 bta_ag_del_records(p_scb, p_data);
177 }
178
179 /*******************************************************************************
180 **
181 ** Function bta_ag_start_open
182 **
183 ** Description This starts an AG open.
184 **
185 **
186 ** Returns void
187 **
188 *******************************************************************************/
bta_ag_start_open(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)189 void bta_ag_start_open(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
190 {
191 BD_ADDR pending_bd_addr;
192 /* store parameters */
193 if (p_data) {
194 bdcpy(p_scb->peer_addr, p_data->api_open.bd_addr);
195 p_scb->open_services = p_data->api_open.services;
196 p_scb->cli_sec_mask = p_data->api_open.sec_mask;
197 }
198 /* Check if RFCOMM has any incoming connection to avoid collision. */
199 if (PORT_IsOpening (pending_bd_addr)) {
200 /* Let the incoming connection goes through. */
201 /* Issue collision for this scb for now. */
202 /* We will decide what to do when we find incoming connetion later. */
203 bta_ag_collision_cback (0, BTA_ID_AG, 0, p_scb->peer_addr);
204 return;
205 }
206 /* close servers */
207 bta_ag_close_servers(p_scb, p_scb->reg_services);
208 /* set role */
209 p_scb->role = BTA_AG_INT;
210 /* do service search */
211 bta_ag_do_disc(p_scb, p_scb->open_services);
212 }
213
214 /*******************************************************************************
215 **
216 ** Function bta_ag_disc_int_res
217 **
218 ** Description This function handles a discovery result when initiator.
219 **
220 **
221 ** Returns void
222 **
223 *******************************************************************************/
bta_ag_disc_int_res(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)224 void bta_ag_disc_int_res(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
225 {
226 UINT16 event = BTA_AG_DISC_FAIL_EVT;
227 APPL_TRACE_DEBUG ("bta_ag_disc_int_res: Status: %d", p_data->disc_result.status);
228
229 /* if found service */
230 if (p_data->disc_result.status == SDP_SUCCESS ||
231 p_data->disc_result.status == SDP_DB_FULL) {
232 /* get attributes */
233 if (bta_ag_sdp_find_attr(p_scb, p_scb->open_services)) {
234 /* set connected service */
235 p_scb->conn_service = bta_ag_service_to_idx(p_scb->open_services);
236 /* send ourselves sdp ok event */
237 event = BTA_AG_DISC_OK_EVT;
238 }
239 }
240 /* free discovery db */
241 bta_ag_free_db(p_scb, p_data);
242 /* if service not found check if we should search for other service */
243 if ((event == BTA_AG_DISC_FAIL_EVT) &&
244 (p_data->disc_result.status == SDP_SUCCESS ||
245 p_data->disc_result.status == SDP_DB_FULL ||
246 p_data->disc_result.status == SDP_NO_RECS_MATCH)) {
247 if ((p_scb->open_services & BTA_HFP_SERVICE_MASK) &&
248 (p_scb->open_services & BTA_HSP_SERVICE_MASK)) {
249 /* search for HSP */
250 p_scb->open_services &= ~BTA_HFP_SERVICE_MASK;
251 bta_ag_do_disc(p_scb, p_scb->open_services);
252 } else if ((p_scb->open_services & BTA_HSP_SERVICE_MASK) &&
253 (p_scb->hsp_version == HSP_VERSION_1_2)) {
254 /* search for UUID_SERVCLASS_HEADSET for HSP 1.0 device */
255 p_scb->hsp_version = HSP_VERSION_1_0;
256 bta_ag_do_disc(p_scb, p_scb->open_services);
257 } else {
258 /* send ourselves sdp ok/fail event */
259 bta_ag_sm_execute(p_scb, event, p_data);
260 }
261 } else {
262 /* send ourselves sdp ok/fail event */
263 bta_ag_sm_execute(p_scb, event, p_data);
264 }
265 }
266
267 /*******************************************************************************
268 **
269 ** Function bta_ag_disc_acp_res
270 **
271 ** Description This function handles a discovery result when acceptor.
272 **
273 **
274 ** Returns void
275 **
276 *******************************************************************************/
bta_ag_disc_acp_res(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)277 void bta_ag_disc_acp_res(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
278 {
279 /* if found service */
280 if (p_data->disc_result.status == SDP_SUCCESS ||
281 p_data->disc_result.status == SDP_DB_FULL) {
282 /* get attributes */
283 bta_ag_sdp_find_attr(p_scb, bta_ag_svc_mask[p_scb->conn_service]);
284 }
285 /* free discovery db */
286 bta_ag_free_db(p_scb, p_data);
287 }
288
289 /*******************************************************************************
290 **
291 ** Function bta_ag_disc_fail
292 **
293 ** Description This function handles a discovery failure.
294 **
295 **
296 ** Returns void
297 **
298 *******************************************************************************/
bta_ag_disc_fail(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)299 void bta_ag_disc_fail(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
300 {
301 UNUSED(p_data);
302 /* reopen registered servers */
303 bta_ag_start_servers(p_scb, p_scb->reg_services);
304 /* reinitialize stuff */
305 /* call open cback w. failure */
306 bta_ag_cback_open(p_scb, NULL, BTA_AG_FAIL_SDP);
307 }
308
309 /*******************************************************************************
310 **
311 ** Function bta_ag_open_fail
312 **
313 ** Description open connection failed.
314 **
315 **
316 ** Returns void
317 **
318 *******************************************************************************/
bta_ag_open_fail(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)319 void bta_ag_open_fail(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
320 {
321 /* call open cback w. failure */
322 bta_ag_cback_open(p_scb, p_data, BTA_AG_FAIL_RESOURCES);
323 }
324
325 /*******************************************************************************
326 **
327 ** Function bta_ag_rfc_fail
328 **
329 ** Description RFCOMM connection failed.
330 **
331 **
332 ** Returns void
333 **
334 *******************************************************************************/
bta_ag_rfc_fail(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)335 void bta_ag_rfc_fail(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
336 {
337 UNUSED(p_data);
338 /* reinitialize stuff */
339 p_scb->conn_handle = 0;
340 p_scb->conn_service = 0;
341 p_scb->peer_features = 0;
342 #if (BTM_WBS_INCLUDED == TRUE)
343 p_scb->peer_codecs = BTA_AG_CODEC_NONE;
344 p_scb->sco_codec = BTA_AG_CODEC_NONE;
345 #endif
346 p_scb->role = 0;
347 p_scb->svc_conn = FALSE;
348 p_scb->hsp_version = HSP_VERSION_1_2;
349 /*Clear the BD address*/
350 bdcpy(p_scb->peer_addr, bd_addr_null);
351 /* reopen registered servers */
352 bta_ag_start_servers(p_scb, p_scb->reg_services);
353 /* call open cback w. failure */
354 bta_ag_cback_open(p_scb, NULL, BTA_AG_FAIL_RFCOMM);
355 }
356
357 /*******************************************************************************
358 **
359 ** Function bta_ag_rfc_close
360 **
361 ** Description RFCOMM connection closed.
362 **
363 **
364 ** Returns void
365 **
366 *******************************************************************************/
bta_ag_rfc_close(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)367 void bta_ag_rfc_close(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
368 {
369 tBTA_AG_CLOSE close;
370 tBTA_SERVICE_MASK services;
371 int i, num_active_conn = 0;
372 UNUSED(p_data);
373 /* reinitialize stuff */
374 p_scb->conn_service = 0;
375 p_scb->peer_features = 0;
376 #if (BTM_WBS_INCLUDED == TRUE)
377 p_scb->peer_codecs = BTA_AG_CODEC_NONE;
378 p_scb->sco_codec = BTA_AG_CODEC_NONE;
379 /* Clear these flags upon SLC teardown */
380 p_scb->codec_updated = FALSE;
381 p_scb->codec_fallback = FALSE;
382 p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
383 #endif
384 p_scb->role = 0;
385 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
386 p_scb->svc_conn = FALSE;
387 p_scb->hsp_version = HSP_VERSION_1_2;
388 bta_ag_at_reinit(&p_scb->at_cb);
389 /* stop timers */
390 bta_sys_stop_timer(&p_scb->act_timer);
391 #if (BTM_WBS_INCLUDED == TRUE)
392 bta_sys_stop_timer(&p_scb->cn_timer);
393 #endif
394 close.hdr.handle = bta_ag_scb_to_idx(p_scb);
395 close.hdr.app_id = p_scb->app_id;
396 bdcpy(close.bd_addr, p_scb->peer_addr);
397
398 bta_sys_conn_close(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
399
400 /* call close call-out */
401 #if (BTM_SCO_HCI_INCLUDED == TRUE)
402 bta_ag_sco_co_close();
403 #endif
404 /* call close cback */
405 (*bta_ag_cb.p_cback)(BTA_AG_CLOSE_EVT, (tBTA_AG *) &close);
406
407 /* if not deregistering (deallocating) reopen registered servers */
408 if (p_scb->dealloc == FALSE) {
409 /* Clear peer bd_addr so instance can be reused */
410 bdcpy(p_scb->peer_addr, bd_addr_null);
411
412 /* start only unopened server */
413 services = p_scb->reg_services;
414 for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++) {
415 if (p_scb->serv_handle[i]) {
416 services &= ~((tBTA_SERVICE_MASK)1 << (BTA_HSP_SERVICE_ID + i));
417 }
418 }
419 bta_ag_start_servers(p_scb, services);
420 p_scb->conn_handle = 0;
421 /* Make sure SCO state is BTA_AG_SCO_SHUTDOWN_ST */
422 bta_ag_sco_shutdown(p_scb, NULL);
423 /* Check if all the SLCs are down */
424 for (i = 0; i < BTA_AG_NUM_SCB; i++) {
425 if (bta_ag_cb.scb[i].in_use && bta_ag_cb.scb[i].svc_conn) {
426 num_active_conn++;
427 }
428 }
429 if (!num_active_conn) {
430 bta_sys_sco_unuse(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
431 }
432 } else {
433 /* else close port and deallocate scb */
434 RFCOMM_RemoveServer(p_scb->conn_handle);
435 bta_ag_scb_dealloc(p_scb);
436 }
437 }
438
439 /*******************************************************************************
440 **
441 ** Function bta_ag_rfc_open
442 **
443 ** Description Handle RFCOMM channel open.
444 **
445 **
446 ** Returns void
447 **
448 *******************************************************************************/
bta_ag_rfc_open(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)449 void bta_ag_rfc_open(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
450 {
451 /* initialize AT feature variables */
452 p_scb->clip_enabled = FALSE;
453 p_scb->ccwa_enabled = FALSE;
454 p_scb->cmer_enabled = FALSE;
455 p_scb->cmee_enabled = FALSE;
456 p_scb->inband_enabled = ((p_scb->features & BTA_AG_FEAT_INBAND) == BTA_AG_FEAT_INBAND);
457
458 /* set up AT command interpreter */
459 p_scb->at_cb.p_at_tbl = (tBTA_AG_AT_CMD *) bta_ag_at_tbl[p_scb->conn_service];
460 p_scb->at_cb.p_cmd_cback = (tBTA_AG_AT_CMD_CBACK *) bta_ag_at_cback_tbl[p_scb->conn_service];
461 p_scb->at_cb.p_err_cback = (tBTA_AG_AT_ERR_CBACK *) bta_ag_at_err_cback;
462 p_scb->at_cb.p_user = p_scb;
463 p_scb->at_cb.cmd_max_len = BTA_AG_CMD_MAX;
464 bta_ag_at_init(&p_scb->at_cb);
465
466 /* call app open call-out */
467 bta_sys_conn_open(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
468 bta_ag_cback_open(p_scb, NULL, BTA_AG_SUCCESS);
469
470 if (p_scb->conn_service == BTA_AG_HFP) {
471 /* if hfp start timer for service level conn */
472 bta_sys_start_timer(&p_scb->act_timer, BTA_AG_SVC_TOUT_EVT, p_bta_ag_cfg->conn_tout);
473 } else {
474 /* else service level conn is open */
475 bta_ag_svc_conn_open(p_scb, p_data);
476 }
477 }
478
479 /*******************************************************************************
480 **
481 ** Function bta_ag_rfc_acp_open
482 **
483 ** Description Handle RFCOMM channel open when accepting connection.
484 **
485 **
486 ** Returns void
487 **
488 *******************************************************************************/
bta_ag_rfc_acp_open(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)489 void bta_ag_rfc_acp_open(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
490 {
491 UINT16 lcid;
492 int i;
493 tBTA_AG_SCB *ag_scb, *other_scb;
494 BD_ADDR dev_addr = {0};
495 int status;
496 /* set role */
497 p_scb->role = BTA_AG_ACP;
498 APPL_TRACE_DEBUG ("bta_ag_rfc_acp_open: serv_handle0 = %d serv_handle1 = %d",
499 p_scb->serv_handle[0], p_scb->serv_handle[1]);
500 /* get bd addr of peer */
501 if (PORT_SUCCESS != (status = PORT_CheckConnection(p_data->rfc.port_handle, FALSE, dev_addr, &lcid))) {
502 APPL_TRACE_DEBUG ("bta_ag_rfc_acp_open error PORT_CheckConnection returned status %d", status);
503 }
504 /* Collision Handling */
505 for (i = 0, ag_scb = &bta_ag_cb.scb[0]; i < BTA_AG_NUM_SCB; i++, ag_scb++) {
506 if ((ag_scb->in_use) && (ag_scb->colli_tmr_on)) {
507 /* stop collision timer */
508 ag_scb->colli_tmr_on = FALSE;
509 bta_sys_stop_timer (&ag_scb->colli_timer);
510
511 if (bdcmp (dev_addr, ag_scb->peer_addr) == 0) {
512 /* If incoming and outgoing device are same, nothing more to do. */
513 /* Outgoing conn will be aborted because we have successful incoming conn. */
514 } else {
515 /* Resume outgoing connection. */
516 other_scb = bta_ag_get_other_idle_scb (p_scb);
517 if (other_scb) {
518 bdcpy(other_scb->peer_addr, ag_scb->peer_addr);
519 other_scb->open_services = ag_scb->open_services;
520 other_scb->cli_sec_mask = ag_scb->cli_sec_mask;
521 bta_ag_resume_open (other_scb);
522 }
523 }
524 break;
525 }
526 }
527 bdcpy (p_scb->peer_addr, dev_addr);
528 /* determine connected service from port handle */
529 for (i = 0; i < BTA_AG_NUM_IDX; i++) {
530 APPL_TRACE_DEBUG ("bta_ag_rfc_acp_open: i = %d serv_handle = %d port_handle = %d",
531 i, p_scb->serv_handle[i], p_data->rfc.port_handle);
532 if (p_scb->serv_handle[i] == p_data->rfc.port_handle) {
533 p_scb->conn_service = i;
534 p_scb->conn_handle = p_data->rfc.port_handle;
535 break;
536 }
537 }
538 APPL_TRACE_DEBUG ("bta_ag_rfc_acp_open: conn_service = %d conn_handle = %d",
539 p_scb->conn_service, p_scb->conn_handle);
540 /* close any unopened server */
541 bta_ag_close_servers(p_scb, (p_scb->reg_services & ~bta_ag_svc_mask[p_scb->conn_service]));
542 /* do service discovery to get features */
543 bta_ag_do_disc(p_scb, bta_ag_svc_mask[p_scb->conn_service]);
544 /* continue with common open processing */
545 bta_ag_rfc_open(p_scb, p_data);
546 }
547
548 /*******************************************************************************
549 **
550 ** Function bta_ag_rfc_data
551 **
552 ** Description Read and process data from RFCOMM.
553 **
554 **
555 ** Returns void
556 **
557 *******************************************************************************/
bta_ag_rfc_data(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)558 void bta_ag_rfc_data(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
559 {
560 UINT16 len;
561 char buf[BTA_AG_RFC_READ_MAX];
562 UNUSED(p_data);
563 memset(buf, 0, BTA_AG_RFC_READ_MAX);
564
565 APPL_TRACE_DEBUG("bta_ag_rfc_data");
566 /* do the following */
567 for (;;) {
568 /* read data from rfcomm; if bad status, we're done */
569 if (PORT_ReadData(p_scb->conn_handle, buf, BTA_AG_RFC_READ_MAX, &len) != PORT_SUCCESS) {
570 break;
571 }
572 /* if no data, we're done */
573 if (len == 0) {
574 break;
575 }
576 /* run AT command interpreter on data */
577 bta_sys_busy(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
578 bta_ag_at_parse(&p_scb->at_cb, buf, len);
579 bta_sys_idle(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
580 /* no more data to read, we're done */
581 if (len < BTA_AG_RFC_READ_MAX) {
582 break;
583 }
584 }
585 }
586
587 /*******************************************************************************
588 **
589 ** Function bta_ag_start_close
590 **
591 ** Description Start the process of closing SCO and RFCOMM connection.
592 **
593 **
594 ** Returns void
595 **
596 *******************************************************************************/
bta_ag_start_close(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)597 void bta_ag_start_close(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
598 {
599 /* Take the link out of sniff and set L2C idle time to 0 */
600 bta_dm_pm_active(p_scb->peer_addr);
601 L2CA_SetIdleTimeoutByBdAddr(p_scb->peer_addr, 0, BT_TRANSPORT_BR_EDR);
602 /* if SCO is open close SCO and wait on RFCOMM close */
603 if (bta_ag_sco_is_open(p_scb)) {
604 p_scb->post_sco = BTA_AG_POST_SCO_CLOSE_RFC;
605 } else {
606 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
607 bta_ag_rfc_do_close(p_scb, p_data);
608 }
609 /* Always do SCO shutdown to handle all SCO corner cases */
610 bta_ag_sco_shutdown(p_scb, p_data);
611 }
612
613 /*******************************************************************************
614 **
615 ** Function bta_ag_post_sco_open
616 **
617 ** Description Perform post-SCO open action, if any
618 **
619 **
620 ** Returns void
621 **
622 *******************************************************************************/
bta_ag_post_sco_open(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)623 void bta_ag_post_sco_open(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
624 {
625 switch (p_scb->post_sco) {
626 case BTA_AG_POST_SCO_RING:
627 bta_ag_send_ring(p_scb, p_data);
628 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
629 break;
630
631 case BTA_AG_POST_SCO_CALL_CONN:
632 bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_CONN_RES);
633 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
634 break;
635
636 default:
637 break;
638 }
639 }
640
641 /*******************************************************************************
642 **
643 ** Function bta_ag_post_sco_close
644 **
645 ** Description Perform post-SCO close action, if any
646 **
647 **
648 ** Returns void
649 **
650 *******************************************************************************/
bta_ag_post_sco_close(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)651 void bta_ag_post_sco_close(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
652 {
653 switch (p_scb->post_sco) {
654 case BTA_AG_POST_SCO_CLOSE_RFC:
655 bta_ag_rfc_do_close(p_scb, p_data);
656 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
657 break;
658
659 case BTA_AG_POST_SCO_CALL_CONN:
660 bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_CONN_RES);
661 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
662 break;
663
664 case BTA_AG_POST_SCO_CALL_ORIG:
665 bta_ag_send_call_inds(p_scb, BTA_AG_OUT_CALL_ORIG_RES);
666 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
667 break;
668
669 case BTA_AG_POST_SCO_CALL_END:
670 bta_ag_send_call_inds(p_scb, BTA_AG_END_CALL_RES);
671 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
672 break;
673
674 case BTA_AG_POST_SCO_CALL_END_INCALL:
675 {
676 bta_ag_send_call_inds(p_scb, BTA_AG_END_CALL_RES);
677 /* Sending callsetup IND and Ring were defered to after SCO close. */
678 bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_RES);
679
680 if (bta_ag_inband_enabled(p_scb) && !(p_scb->features & BTA_AG_FEAT_NOSCO)) {
681 p_scb->post_sco = BTA_AG_POST_SCO_RING;
682 bta_ag_sco_open(p_scb, p_data);
683 } else {
684 p_scb->post_sco = BTA_AG_POST_SCO_NONE;
685 bta_ag_send_ring(p_scb, p_data);
686 }
687 break;
688 }
689
690 default:
691 break;
692 }
693 }
694
695 /*******************************************************************************
696 **
697 ** Function bta_ag_svc_conn_open
698 **
699 ** Description Service level connection opened
700 **
701 **
702 ** Returns void
703 **
704 *******************************************************************************/
bta_ag_svc_conn_open(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)705 void bta_ag_svc_conn_open(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
706 {
707 tBTA_AG_CONN evt;
708 UNUSED(p_data);
709
710 if (!p_scb->svc_conn) {
711 /* set state variable */
712 p_scb->svc_conn = TRUE;
713 /* Clear AT+BIA mask from previous SLC if any. */
714 p_scb->bia_masked_out = 0;
715 /* stop timer */
716 bta_sys_stop_timer(&p_scb->act_timer);
717 /* call callback */
718 evt.hdr.handle = bta_ag_scb_to_idx(p_scb);
719 evt.hdr.app_id = p_scb->app_id;
720 evt.peer_feat = p_scb->peer_features;
721 bdcpy(evt.bd_addr, p_scb->peer_addr);
722 #if (BTM_WBS_INCLUDED == TRUE)
723 evt.peer_codec = p_scb->peer_codecs;
724 #endif
725 if ((p_scb->call_ind != BTA_AG_CALL_INACTIVE) ||
726 (p_scb->callsetup_ind != BTA_AG_CALLSETUP_NONE)) {
727 bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
728 }
729 (*bta_ag_cb.p_cback)(BTA_AG_CONN_EVT, (tBTA_AG *) &evt);
730 }
731 }
732
733 /*******************************************************************************
734 **
735 ** Function bta_ag_ci_rx_data
736 **
737 ** Description Send result code to RFCOMM
738 **
739 ** Returns void
740 **
741 *******************************************************************************/
bta_ag_ci_rx_data(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)742 void bta_ag_ci_rx_data(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
743 {
744 UINT16 len;
745 tBTA_AG_CI_RX_WRITE *p_rx_write_msg = (tBTA_AG_CI_RX_WRITE *)p_data;
746 char *p_data_area = (char *)(p_rx_write_msg+1); /* Point to data area after header */
747
748 APPL_TRACE_DEBUG("bta_ag_ci_rx_data:");
749 /* send to RFCOMM */
750 bta_sys_busy(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
751 PORT_WriteData(p_scb->conn_handle, p_data_area, strlen(p_data_area), &len);
752 bta_sys_idle(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
753 }
754
755 /*******************************************************************************
756 **
757 ** Function bta_ag_rcvd_slc_ready
758 **
759 ** Description Handles SLC ready call-in in case of pass-through mode.
760 **
761 ** Returns void
762 **
763 *******************************************************************************/
bta_ag_rcvd_slc_ready(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)764 void bta_ag_rcvd_slc_ready(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
765 {
766 UNUSED(p_data);
767 APPL_TRACE_DEBUG("bta_ag_rcvd_slc_ready: handle = %d", bta_ag_scb_to_idx(p_scb));
768
769 if (bta_ag_cb.parse_mode == BTA_AG_PASS_THROUGH) {
770 /* In pass-through mode, BTA knows that SLC is ready only through call-in. */
771 bta_ag_svc_conn_open(p_scb, NULL);
772 }
773 }
774
775 /*******************************************************************************
776 **
777 ** Function bta_ag_setcodec
778 **
779 ** Description Handle API SetCodec
780 **
781 **
782 ** Returns void
783 **
784 *******************************************************************************/
bta_ag_setcodec(tBTA_AG_SCB * p_scb,tBTA_AG_DATA * p_data)785 void bta_ag_setcodec(tBTA_AG_SCB *p_scb, tBTA_AG_DATA *p_data)
786 {
787 #if (BTM_WBS_INCLUDED == TRUE)
788 tBTA_AG_PEER_CODEC codec_type = p_data->api_setcodec.codec;
789 tBTA_AG_VAL val;
790
791 /* Check if the requested codec type is valid */
792 if((codec_type != BTA_AG_CODEC_NONE) &&
793 (codec_type != BTA_AG_CODEC_CVSD) &&
794 (codec_type != BTA_AG_CODEC_MSBC)) {
795 val.num = codec_type;
796 val.hdr.status = BTA_AG_FAIL_RESOURCES;
797 APPL_TRACE_ERROR("%s error: unsupported codec type %d", __func__, codec_type);
798 (*bta_ag_cb.p_cback)(BTA_AG_WBS_EVT, (tBTA_AG *) &val);
799 return;
800 }
801
802 if ((p_scb->peer_codecs & codec_type) ||
803 (codec_type == BTA_AG_CODEC_NONE) ||
804 (codec_type == BTA_AG_CODEC_CVSD)) {
805 p_scb->sco_codec = codec_type;
806 p_scb->codec_updated = TRUE;
807 val.num = codec_type;
808 val.hdr.status = BTA_AG_SUCCESS;
809 APPL_TRACE_DEBUG("%s: Updated codec type %d", __func__, codec_type);
810 } else {
811 val.num = codec_type;
812 val.hdr.status = BTA_AG_FAIL_RESOURCES;
813 APPL_TRACE_ERROR("%s error: unsupported codec type %d",__func__, codec_type);
814 }
815 (*bta_ag_cb.p_cback)(BTA_AG_WBS_EVT, (tBTA_AG *) &val);
816 #endif
817 }
818
819 #endif /* #if (BTA_AG_INCLUDED == TRUE) */
820