OSDN Git Service

c2cc10a25c73cee2b8ed75c2f9ef2350886a1010
[android-x86/system-bt.git] / stack / btm / btm_sco.cc
1 /******************************************************************************
2  *
3  *  Copyright (C) 2000-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 functions that handle SCO connections. This includes
22  *  operations such as connect, disconnect, change supported packet types.
23  *
24  ******************************************************************************/
25
26 #include <string.h>
27 #include "bt_types.h"
28 #include "bt_target.h"
29 #include "bt_common.h"
30 #include "bt_types.h"
31 #include "hcimsgs.h"
32 #include "btu.h"
33 #include "btm_api.h"
34 #include "btm_int.h"
35 #include "hcidefs.h"
36 #include "bt_utils.h"
37 #include "device/include/controller.h"
38
39
40 #if (BTM_SCO_INCLUDED == TRUE)
41
42 /********************************************************************************/
43 /*                 L O C A L    D A T A    D E F I N I T I O N S                */
44 /********************************************************************************/
45
46 #define SCO_ST_UNUSED           0
47 #define SCO_ST_LISTENING        1
48 #define SCO_ST_W4_CONN_RSP      2
49 #define SCO_ST_CONNECTING       3
50 #define SCO_ST_CONNECTED        4
51 #define SCO_ST_DISCONNECTING    5
52 #define SCO_ST_PEND_UNPARK      6
53 #define SCO_ST_PEND_ROLECHANGE  7
54 #define SCO_ST_PEND_MODECHANGE  8
55
56 /********************************************************************************/
57 /*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
58 /********************************************************************************/
59
60 static const tBTM_ESCO_PARAMS btm_esco_defaults =
61 {
62     BTM_64KBITS_RATE,               /* TX Bandwidth (64 kbits/sec)              */
63     BTM_64KBITS_RATE,               /* RX Bandwidth (64 kbits/sec)              */
64     0x000c,                         /* 12 ms (HS/HF can use EV3, 2-EV3, 3-EV3)  */
65     0x0060,                         /* Inp Linear, Air CVSD, 2s Comp, 16bit     */
66     (BTM_SCO_PKT_TYPES_MASK_HV1 +   /* Packet Types                             */
67      BTM_SCO_PKT_TYPES_MASK_HV2 +
68      BTM_SCO_PKT_TYPES_MASK_HV3 +
69      BTM_SCO_PKT_TYPES_MASK_EV3 +
70      BTM_SCO_PKT_TYPES_MASK_EV4 +
71      BTM_SCO_PKT_TYPES_MASK_EV5),
72    BTM_ESCO_RETRANS_QUALITY         /* Retransmission Effort */
73 };
74
75 /*******************************************************************************
76 **
77 ** Function         btm_sco_flush_sco_data
78 **
79 ** Description      This function is called to flush the SCO data for this channel.
80 **
81 ** Returns          void
82 **
83 *******************************************************************************/
84 void btm_sco_flush_sco_data(uint16_t sco_inx)
85 {
86 #if (BTM_SCO_HCI_INCLUDED == TRUE)
87 #if (BTM_MAX_SCO_LINKS>0)
88     tSCO_CONN   *p ;
89     BT_HDR      *p_buf;
90
91     if (sco_inx < BTM_MAX_SCO_LINKS)
92     {
93         p = &btm_cb.sco_cb.sco_db[sco_inx];
94         while ((p_buf = (BT_HDR *)fixed_queue_try_dequeue(p->xmit_data_q)) != NULL)
95             osi_free(p_buf);
96         }
97     }
98 #else
99     UNUSED(sco_inx);
100 #endif
101 #else
102     UNUSED(sco_inx);
103 #endif
104 }
105 /*******************************************************************************
106 **
107 ** Function         btm_sco_init
108 **
109 ** Description      This function is called at BTM startup to initialize
110 **
111 ** Returns          void
112 **
113 *******************************************************************************/
114 void btm_sco_init (void)
115 {
116 #if (BTM_SCO_HCI_INCLUDED == TRUE)
117     for (int i = 0; i < BTM_MAX_SCO_LINKS; i++)
118         btm_cb.sco_cb.sco_db[i].xmit_data_q = fixed_queue_new(SIZE_MAX);
119 #endif
120
121     /* Initialize nonzero defaults */
122     btm_cb.sco_cb.sco_disc_reason  = BTM_INVALID_SCO_DISC_REASON;
123
124     btm_cb.sco_cb.def_esco_parms = btm_esco_defaults; /* Initialize with defaults */
125     btm_cb.sco_cb.desired_sco_mode = BTM_DEFAULT_SCO_MODE;
126 }
127
128 /*******************************************************************************
129 **
130 ** Function         btm_esco_conn_rsp
131 **
132 ** Description      This function is called upon receipt of an (e)SCO connection
133 **                  request event (BTM_ESCO_CONN_REQ_EVT) to accept or reject
134 **                  the request. Parameters used to negotiate eSCO links.
135 **                  If p_parms is NULL, then default values are used.
136 **                  If the link type of the incoming request is SCO, then only
137 **                  the tx_bw, max_latency, content format, and packet_types are
138 **                  valid.  The hci_status parameter should be
139 **                  ([0x0] to accept, [0x0d..0x0f] to reject)
140 **
141 ** Returns          void
142 **
143 *******************************************************************************/
144 static void btm_esco_conn_rsp (uint16_t sco_inx, uint8_t hci_status, BD_ADDR bda,
145                                tBTM_ESCO_PARAMS *p_parms)
146 {
147 #if (BTM_MAX_SCO_LINKS>0)
148     tSCO_CONN        *p_sco = NULL;
149     tBTM_ESCO_PARAMS *p_setup;
150     uint16_t          temp_pkt_types;
151
152     if (sco_inx < BTM_MAX_SCO_LINKS)
153         p_sco = &btm_cb.sco_cb.sco_db[sco_inx];
154
155     /* Reject the connect request if refused by caller or wrong state */
156     if (hci_status != HCI_SUCCESS || p_sco == NULL)
157     {
158         if (p_sco)
159         {
160             p_sco->state = (p_sco->state == SCO_ST_W4_CONN_RSP) ? SCO_ST_LISTENING
161                                                                 : SCO_ST_UNUSED;
162         }
163
164         if (!btm_cb.sco_cb.esco_supported)
165         {
166             btsnd_hcic_reject_conn(bda, hci_status);
167         }
168         else
169         {
170             btsnd_hcic_reject_esco_conn(bda, hci_status);
171         }
172     }
173     else    /* Connection is being accepted */
174     {
175         p_sco->state = SCO_ST_CONNECTING;
176         p_setup = &p_sco->esco.setup;
177         /* If parameters not specified use the default */
178         if (p_parms)
179             *p_setup = *p_parms;
180         else /* Use the last setup passed thru BTM_SetEscoMode (or defaults) */
181         {
182             *p_setup = btm_cb.sco_cb.def_esco_parms;
183         }
184
185         temp_pkt_types = (p_setup->packet_types &
186                           BTM_SCO_SUPPORTED_PKTS_MASK &
187                           btm_cb.btm_sco_pkt_types_supported);
188
189         /* Make sure at least one eSCO packet type is sent, else might confuse peer */
190         /* Taking this out to confirm with BQB tests
191         ** Real application would like to include this though, as many devices
192         ** do not retry with SCO only if an eSCO connection fails.
193         if (!(temp_pkt_types & BTM_ESCO_LINK_ONLY_MASK))
194         {
195             temp_pkt_types |= BTM_SCO_PKT_TYPES_MASK_EV3;
196         }
197         */
198         /* If SCO request, remove eSCO packet types (conformance) */
199         if (p_sco->esco.data.link_type == BTM_LINK_TYPE_SCO)
200         {
201             temp_pkt_types &= BTM_SCO_LINK_ONLY_MASK;
202             temp_pkt_types |= BTM_SCO_EXCEPTION_PKTS_MASK;
203         }
204         else
205         {
206             /* OR in any exception packet types */
207             temp_pkt_types |= ((p_setup->packet_types & BTM_SCO_EXCEPTION_PKTS_MASK) |
208                 (btm_cb.btm_sco_pkt_types_supported & BTM_SCO_EXCEPTION_PKTS_MASK));
209         }
210
211         btsnd_hcic_accept_esco_conn(bda, p_setup->tx_bw, p_setup->rx_bw,
212                                     p_setup->max_latency, p_setup->voice_contfmt,
213                                     p_setup->retrans_effort, temp_pkt_types);
214         p_setup->packet_types = temp_pkt_types;
215     }
216 #endif
217 }
218
219
220 #if (BTM_SCO_HCI_INCLUDED == TRUE)
221 /*******************************************************************************
222 **
223 ** Function         btm_sco_check_send_pkts
224 **
225 ** Description      This function is called to check if it can send packets
226 **                  to the Host Controller.
227 **
228 ** Returns          void
229 **
230 *******************************************************************************/
231 void btm_sco_check_send_pkts (uint16_t sco_inx)
232 {
233     tSCO_CB  *p_cb = &btm_cb.sco_cb;
234     tSCO_CONN   *p_ccb = &p_cb->sco_db[sco_inx];
235
236     /* If there is data to send, send it now */
237     BT_HDR  *p_buf;
238     while ((p_buf = (BT_HDR *)fixed_queue_try_dequeue(p_ccb->xmit_data_q)) != NULL)
239     {
240 #if (BTM_SCO_HCI_DEBUG == TRUE)
241         BTM_TRACE_DEBUG("btm: [%d] buf in xmit_data_q",
242                         fixed_queue_length(p_ccb->xmit_data_q) + 1);
243 #endif
244
245         HCI_SCO_DATA_TO_LOWER(p_buf);
246     }
247 }
248 #endif /* BTM_SCO_HCI_INCLUDED == TRUE */
249
250 /*******************************************************************************
251 **
252 ** Function         btm_route_sco_data
253 **
254 ** Description      Route received SCO data.
255 **
256 ** Returns          void
257 **
258 *******************************************************************************/
259 void  btm_route_sco_data(BT_HDR *p_msg)
260 {
261 #if (BTM_SCO_HCI_INCLUDED == TRUE)
262     uint16_t    sco_inx, handle;
263     uint8_t     *p = (uint8_t *)(p_msg + 1) + p_msg->offset;
264     uint8_t     pkt_size = 0;
265     uint8_t     pkt_status = 0;
266
267     /* Extract Packet_Status_Flag and handle */
268     STREAM_TO_UINT16 (handle, p);
269     pkt_status = HCID_GET_EVENT(handle);
270     handle   = HCID_GET_HANDLE (handle);
271
272     STREAM_TO_UINT8 (pkt_size, p);
273
274     if ((sco_inx = btm_find_scb_by_handle(handle)) != BTM_MAX_SCO_LINKS )
275     {
276         /* send data callback */
277         if (!btm_cb.sco_cb.p_data_cb )
278             /* if no data callback registered,  just free the buffer  */
279             osi_free(p_msg);
280         else
281         {
282             (*btm_cb.sco_cb.p_data_cb)(sco_inx, p_msg, (tBTM_SCO_DATA_FLAG) pkt_status);
283         }
284     }
285     else /* no mapping handle SCO connection is active, free the buffer */
286     {
287         osi_free(p_msg);
288     }
289 #else
290     osi_free(p_msg);
291 #endif
292 }
293
294 /*******************************************************************************
295 **
296 ** Function         BTM_WriteScoData
297 **
298 ** Description      This function write SCO data to a specified instance. The data
299 **                  to be written p_buf needs to carry an offset of
300 **                  HCI_SCO_PREAMBLE_SIZE bytes, and the data length can not
301 **                  exceed BTM_SCO_DATA_SIZE_MAX bytes, whose default value is set
302 **                  to 60 and is configurable. Data longer than the maximum bytes
303 **                  will be truncated.
304 **
305 ** Returns          BTM_SUCCESS: data write is successful
306 **                  BTM_ILLEGAL_VALUE: SCO data contains illegal offset value.
307 **                  BTM_SCO_BAD_LENGTH: SCO data length exceeds the max SCO packet
308 **                                      size.
309 **                  BTM_NO_RESOURCES: no resources.
310 **                  BTM_UNKNOWN_ADDR: unknown SCO connection handle, or SCO is not
311 **                                    routed via HCI.
312 **
313 **
314 *******************************************************************************/
315 tBTM_STATUS BTM_WriteScoData (uint16_t sco_inx, BT_HDR *p_buf)
316 {
317 #if (BTM_SCO_HCI_INCLUDED == TRUE && BTM_MAX_SCO_LINKS > 0)
318     tSCO_CONN   *p_ccb = &btm_cb.sco_cb.sco_db[sco_inx];
319     uint8_t *p;
320     tBTM_STATUS     status = BTM_SUCCESS;
321
322     if (sco_inx < BTM_MAX_SCO_LINKS && btm_cb.sco_cb.p_data_cb &&
323         p_ccb->state == SCO_ST_CONNECTED)
324     {
325         /* Ensure we have enough space in the buffer for the SCO and HCI headers */
326         if (p_buf->offset < HCI_SCO_PREAMBLE_SIZE)
327         {
328             BTM_TRACE_ERROR ("BTM SCO - cannot send buffer, offset: %d", p_buf->offset);
329             osi_free(p_buf);
330             status = BTM_ILLEGAL_VALUE;
331         }
332         else    /* write HCI header */
333         {
334             /* Step back 3 bytes to add the headers */
335             p_buf->offset -= HCI_SCO_PREAMBLE_SIZE;
336             /* Set the pointer to the beginning of the data */
337             p = (uint8_t *)(p_buf + 1) + p_buf->offset;
338             /* add HCI handle */
339             UINT16_TO_STREAM (p, p_ccb->hci_handle);
340             /* only sent the first BTM_SCO_DATA_SIZE_MAX bytes data if more than max,
341                and set warning status */
342             if (p_buf->len > BTM_SCO_DATA_SIZE_MAX)
343             {
344                 p_buf->len = BTM_SCO_DATA_SIZE_MAX;
345                 status = BTM_SCO_BAD_LENGTH;
346             }
347
348             UINT8_TO_STREAM (p, (uint8_t)p_buf->len);
349             p_buf->len += HCI_SCO_PREAMBLE_SIZE;
350
351             fixed_queue_enqueue(p_ccb->xmit_data_q, p_buf);
352
353             btm_sco_check_send_pkts (sco_inx);
354         }
355     }
356     else
357     {
358         osi_free(p_buf);
359
360         BTM_TRACE_WARNING ("BTM_WriteScoData, invalid sco index: %d at state [%d]",
361             sco_inx, btm_cb.sco_cb.sco_db[sco_inx].state);
362         status = BTM_UNKNOWN_ADDR;
363     }
364
365     return (status);
366
367 #else
368     UNUSED(sco_inx);
369     UNUSED(p_buf);
370     return (BTM_NO_RESOURCES);
371 #endif
372 }
373
374 #if (BTM_MAX_SCO_LINKS>0)
375 /*******************************************************************************
376 **
377 ** Function         btm_send_connect_request
378 **
379 ** Description      This function is called to respond to SCO connect indications
380 **
381 ** Returns          void
382 **
383 *******************************************************************************/
384 static tBTM_STATUS btm_send_connect_request(uint16_t acl_handle,
385                                             tBTM_ESCO_PARAMS *p_setup)
386 {
387     uint16_t temp_pkt_types;
388     uint8_t xx;
389     tACL_CONN *p_acl;
390
391     /* Send connect request depending on version of spec */
392     if (!btm_cb.sco_cb.esco_supported)
393     {
394         btsnd_hcic_add_SCO_conn(acl_handle, BTM_ESCO_2_SCO(p_setup->packet_types));
395     }
396     else
397     {
398         temp_pkt_types = (p_setup->packet_types & BTM_SCO_SUPPORTED_PKTS_MASK &
399                              btm_cb.btm_sco_pkt_types_supported);
400
401         /* OR in any exception packet types */
402         temp_pkt_types |= ((p_setup->packet_types & BTM_SCO_EXCEPTION_PKTS_MASK) |
403             (btm_cb.btm_sco_pkt_types_supported & BTM_SCO_EXCEPTION_PKTS_MASK));
404
405         /* Finally, remove EDR eSCO if the remote device doesn't support it */
406         /* UPF25:  Only SCO was brought up in this case */
407         btm_handle_to_acl_index(acl_handle);
408         if ((xx = btm_handle_to_acl_index(acl_handle)) < MAX_L2CAP_LINKS)
409         {
410             p_acl = &btm_cb.acl_db[xx];
411             if (!HCI_EDR_ESCO_2MPS_SUPPORTED(p_acl->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
412             {
413
414                 BTM_TRACE_WARNING("BTM Remote does not support 2-EDR eSCO");
415                 temp_pkt_types |= (HCI_ESCO_PKT_TYPES_MASK_NO_2_EV3 |
416                                    HCI_ESCO_PKT_TYPES_MASK_NO_2_EV5);
417             }
418             if (!HCI_EDR_ESCO_3MPS_SUPPORTED(p_acl->peer_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
419             {
420
421                 BTM_TRACE_WARNING("BTM Remote does not support 3-EDR eSCO");
422                 temp_pkt_types |= (HCI_ESCO_PKT_TYPES_MASK_NO_3_EV3 |
423                                    HCI_ESCO_PKT_TYPES_MASK_NO_3_EV5);
424             }
425
426              /* Check to see if BR/EDR Secure Connections is being used
427              ** If so, we cannot use SCO-only packet types (HFP 1.7)
428              */
429             if (BTM_BothEndsSupportSecureConnections(p_acl->remote_addr))
430             {
431                 temp_pkt_types &= ~(BTM_SCO_PKT_TYPE_MASK);
432                 BTM_TRACE_DEBUG("%s: SCO Conn: pkt_types after removing SCO (0x%04x)", __func__,
433                                  temp_pkt_types);
434
435                 /* Return error if no packet types left */
436                 if (temp_pkt_types == 0)
437                 {
438                     BTM_TRACE_ERROR("%s: SCO Conn (BR/EDR SC): No packet types available",
439                                     __func__);
440                     return (BTM_WRONG_MODE);
441                 }
442             }
443             else
444             {
445                 BTM_TRACE_DEBUG("%s: SCO Conn(BR/EDR SC):local or peer does not support BR/EDR SC",
446                                 __func__);
447             }
448         }
449
450
451         BTM_TRACE_API("      txbw 0x%x, rxbw 0x%x, lat 0x%x, voice 0x%x, retrans 0x%02x, pkt 0x%04x",
452             p_setup->tx_bw, p_setup->rx_bw,
453             p_setup->max_latency, p_setup->voice_contfmt,
454             p_setup->retrans_effort, temp_pkt_types);
455
456         btsnd_hcic_setup_esco_conn(acl_handle,
457                                    p_setup->tx_bw,
458                                    p_setup->rx_bw,
459                                    p_setup->max_latency,
460                                    p_setup->voice_contfmt,
461                                    p_setup->retrans_effort,
462                                    temp_pkt_types);
463         p_setup->packet_types = temp_pkt_types;
464     }
465
466     return (BTM_CMD_STARTED);
467 }
468 #endif
469
470 /*******************************************************************************
471 **
472 ** Function         btm_set_sco_ind_cback
473 **
474 ** Description      This function is called to register for TCS SCO connect
475 **                  indications.
476 **
477 ** Returns          void
478 **
479 *******************************************************************************/
480 void btm_set_sco_ind_cback( tBTM_SCO_IND_CBACK *sco_ind_cb )
481 {
482     btm_cb.sco_cb.app_sco_ind_cb = sco_ind_cb;
483 }
484
485 /*******************************************************************************
486 **
487 ** Function         btm_accept_sco_link
488 **
489 ** Description      This function is called to respond to TCS SCO connect
490 **                  indications
491 **
492 ** Returns          void
493 **
494 *******************************************************************************/
495 void btm_accept_sco_link(uint16_t sco_inx, tBTM_ESCO_PARAMS *p_setup,
496                          tBTM_SCO_CB *p_conn_cb, tBTM_SCO_CB *p_disc_cb)
497 {
498 #if (BTM_MAX_SCO_LINKS>0)
499     tSCO_CONN        *p_sco;
500
501     if (sco_inx >= BTM_MAX_SCO_LINKS)
502     {
503         BTM_TRACE_ERROR("btm_accept_sco_link: Invalid sco_inx(%d)", sco_inx);
504         return;
505     }
506
507     /* Link role is ignored in for this message */
508     p_sco = &btm_cb.sco_cb.sco_db[sco_inx];
509     p_sco->p_conn_cb = p_conn_cb;
510     p_sco->p_disc_cb = p_disc_cb;
511     p_sco->esco.data.link_type = BTM_LINK_TYPE_ESCO; /* Accept with all supported types */
512
513     BTM_TRACE_DEBUG("TCS accept SCO: Packet Types 0x%04x", p_setup->packet_types);
514
515     btm_esco_conn_rsp(sco_inx, HCI_SUCCESS, p_sco->esco.data.bd_addr, p_setup);
516 #else
517     btm_reject_sco_link(sco_inx);
518 #endif
519 }
520
521 /*******************************************************************************
522 **
523 ** Function         btm_reject_sco_link
524 **
525 ** Description      This function is called to respond to SCO connect indications
526 **
527 ** Returns          void
528 **
529 *******************************************************************************/
530 void btm_reject_sco_link( uint16_t sco_inx )
531 {
532     btm_esco_conn_rsp(sco_inx, HCI_ERR_HOST_REJECT_RESOURCES,
533                       btm_cb.sco_cb.sco_db[sco_inx].esco.data.bd_addr, NULL);
534 }
535
536 /*******************************************************************************
537 **
538 ** Function         BTM_CreateSco
539 **
540 ** Description      This function is called to create an SCO connection. If the
541 **                  "is_orig" flag is true, the connection will be originated,
542 **                  otherwise BTM will wait for the other side to connect.
543 **
544 **                  NOTE:  If BTM_IGNORE_SCO_PKT_TYPE is passed in the pkt_types
545 **                      parameter the default packet types is used.
546 **
547 ** Returns          BTM_UNKNOWN_ADDR if the ACL connection is not up
548 **                  BTM_BUSY         if another SCO being set up to
549 **                                   the same BD address
550 **                  BTM_NO_RESOURCES if the max SCO limit has been reached
551 **                  BTM_CMD_STARTED  if the connection establishment is started.
552 **                                   In this case, "*p_sco_inx" is filled in
553 **                                   with the sco index used for the connection.
554 **
555 *******************************************************************************/
556 tBTM_STATUS BTM_CreateSco (BD_ADDR remote_bda, bool    is_orig, uint16_t pkt_types,
557                            uint16_t *p_sco_inx, tBTM_SCO_CB *p_conn_cb,
558                            tBTM_SCO_CB *p_disc_cb)
559 {
560 #if (BTM_MAX_SCO_LINKS > 0)
561     tBTM_ESCO_PARAMS *p_setup;
562     tSCO_CONN        *p = &btm_cb.sco_cb.sco_db[0];
563     uint16_t          xx;
564     uint16_t          acl_handle = 0;
565     uint16_t          temp_pkt_types;
566     tACL_CONN        *p_acl;
567
568 #if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
569     tBTM_PM_PWR_MD    pm;
570     tBTM_PM_STATE     state;
571 #else
572     uint8_t           mode;
573 #endif  // BTM_SCO_WAKE_PARKED_LINK
574
575     *p_sco_inx = BTM_INVALID_SCO_INDEX;
576
577     /* If originating, ensure that there is an ACL connection to the BD Address */
578     if (is_orig)
579     {
580         if ((!remote_bda) || ((acl_handle = BTM_GetHCIConnHandle (remote_bda, BT_TRANSPORT_BR_EDR)) == 0xFFFF))
581             return (BTM_UNKNOWN_ADDR);
582     }
583
584     if (remote_bda)
585     {
586         /* If any SCO is being established to the remote BD address, refuse this */
587         for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
588         {
589             if (((p->state == SCO_ST_CONNECTING) || (p->state == SCO_ST_LISTENING)
590                 || (p->state == SCO_ST_PEND_UNPARK))
591                 && (!memcmp (p->esco.data.bd_addr, remote_bda, BD_ADDR_LEN)))
592             {
593                 return (BTM_BUSY);
594             }
595         }
596     }
597     else
598     {
599         /* Support only 1 wildcard BD address at a time */
600         for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
601         {
602             if ((p->state == SCO_ST_LISTENING) && (!p->rem_bd_known))
603                 return (BTM_BUSY);
604         }
605     }
606
607     /* Now, try to find an unused control block, and kick off the SCO establishment */
608     for (xx = 0, p = &btm_cb.sco_cb.sco_db[0]; xx < BTM_MAX_SCO_LINKS; xx++, p++)
609     {
610         if (p->state == SCO_ST_UNUSED)
611         {
612             if (remote_bda)
613             {
614                 if (is_orig)
615                 {
616                     /* can not create SCO link if in park mode */
617 #if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
618                     if ((btm_read_power_mode_state(p->esco.data.bd_addr, &state) == BTM_SUCCESS))
619                     {
620                         if (state == BTM_PM_ST_SNIFF || state == BTM_PM_ST_PARK ||
621                             state == BTM_PM_ST_PENDING)
622                         {
623                             BTM_TRACE_DEBUG("%s In sniff, park or pend mode: %d", __func__, state);
624                             memset( (void*)&pm, 0, sizeof(pm));
625                             pm.mode = BTM_PM_MD_ACTIVE;
626                             BTM_SetPowerMode(BTM_PM_SET_ONLY_ID, remote_bda, &pm);
627                             p->state = SCO_ST_PEND_UNPARK;
628                         }
629                     }
630 #else  // BTM_SCO_WAKE_PARKED_LINK
631                     if( (BTM_ReadPowerMode(remote_bda, &mode) == BTM_SUCCESS) && (mode == BTM_PM_MD_PARK) )
632                         return (BTM_WRONG_MODE);
633 #endif  // BTM_SCO_WAKE_PARKED_LINK
634                 }
635                 memcpy (p->esco.data.bd_addr, remote_bda, BD_ADDR_LEN);
636                 p->rem_bd_known = true;
637             }
638             else
639                 p->rem_bd_known = false;
640
641             /* Link role is ignored in for this message */
642             if (pkt_types == BTM_IGNORE_SCO_PKT_TYPE)
643                 pkt_types = btm_cb.sco_cb.def_esco_parms.packet_types;
644
645             p_setup = &p->esco.setup;
646             *p_setup = btm_cb.sco_cb.def_esco_parms;
647             p_setup->packet_types = (btm_cb.sco_cb.desired_sco_mode == BTM_LINK_TYPE_SCO)
648                 ? (pkt_types & BTM_SCO_LINK_ONLY_MASK) : pkt_types;
649
650             temp_pkt_types = (p_setup->packet_types & BTM_SCO_SUPPORTED_PKTS_MASK &
651                              btm_cb.btm_sco_pkt_types_supported);
652
653             /* OR in any exception packet types */
654             if (controller_get_interface()->get_bt_version()->hci_version >= HCI_PROTO_VERSION_2_0)
655             {
656                 temp_pkt_types |= ((p_setup->packet_types & BTM_SCO_EXCEPTION_PKTS_MASK) |
657                     (btm_cb.btm_sco_pkt_types_supported & BTM_SCO_EXCEPTION_PKTS_MASK));
658             }
659             else    /* Only using SCO packet types; turn off EDR also */
660             {
661                 temp_pkt_types |= BTM_SCO_EXCEPTION_PKTS_MASK;
662             }
663
664             p_setup->packet_types = temp_pkt_types;
665             p->p_conn_cb  = p_conn_cb;
666             p->p_disc_cb  = p_disc_cb;
667             p->hci_handle = BTM_INVALID_HCI_HANDLE;
668             p->is_orig = is_orig;
669
670             if( p->state != SCO_ST_PEND_UNPARK )
671             {
672                 if (is_orig)
673                 {
674                     /* If role change is in progress, do not proceed with SCO setup
675                      * Wait till role change is complete */
676                     p_acl = btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
677                     if (p_acl && p_acl->switch_role_state != BTM_ACL_SWKEY_STATE_IDLE)
678                     {
679                         BTM_TRACE_API("Role Change is in progress for ACL handle 0x%04x",acl_handle);
680                         p->state = SCO_ST_PEND_ROLECHANGE;
681
682                     }
683                 }
684             }
685
686             if( p->state != SCO_ST_PEND_UNPARK && p->state != SCO_ST_PEND_ROLECHANGE )
687             {
688                 if (is_orig)
689                 {
690                     BTM_TRACE_API("BTM_CreateSco -> (e)SCO Link for ACL handle 0x%04x, Desired Type %d",
691                                     acl_handle, btm_cb.sco_cb.desired_sco_mode);
692
693                     if ((btm_send_connect_request(acl_handle, p_setup)) != BTM_CMD_STARTED)
694                         return (BTM_NO_RESOURCES);
695
696                     p->state = SCO_ST_CONNECTING;
697                 }
698                 else
699                     p->state = SCO_ST_LISTENING;
700             }
701
702             *p_sco_inx = xx;
703
704             return (BTM_CMD_STARTED);
705         }
706     }
707
708 #endif
709     /* If here, all SCO blocks in use */
710     return (BTM_NO_RESOURCES);
711 }
712
713 #if (BTM_SCO_WAKE_PARKED_LINK == TRUE)
714 /*******************************************************************************
715 **
716 ** Function         btm_sco_chk_pend_unpark
717 **
718 ** Description      This function is called by BTIF when there is a mode change
719 **                  event to see if there are SCO commands waiting for the unpark.
720 **
721 ** Returns          void
722 **
723 *******************************************************************************/
724 void btm_sco_chk_pend_unpark (uint8_t hci_status, uint16_t hci_handle)
725 {
726 #if (BTM_MAX_SCO_LINKS>0)
727     uint16_t    xx;
728     uint16_t    acl_handle;
729     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[0];
730
731     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
732     {
733         if ((p->state == SCO_ST_PEND_UNPARK) &&
734             ((acl_handle = BTM_GetHCIConnHandle (p->esco.data.bd_addr, BT_TRANSPORT_BR_EDR)) == hci_handle))
735
736         {
737             BTM_TRACE_API("btm_sco_chk_pend_unpark -> (e)SCO Link for ACL handle 0x%04x, Desired Type %d, hci_status 0x%02x",
738                                     acl_handle, btm_cb.sco_cb.desired_sco_mode, hci_status);
739
740             if ((btm_send_connect_request(acl_handle, &p->esco.setup)) == BTM_CMD_STARTED)
741                 p->state = SCO_ST_CONNECTING;
742         }
743     }
744 #endif  // BTM_MAX_SCO_LINKS
745 }
746 #endif  // BTM_SCO_WAKE_PARKED_LINK
747
748 /*******************************************************************************
749 **
750 ** Function         btm_sco_chk_pend_rolechange
751 **
752 ** Description      This function is called by BTIF when there is a role change
753 **                  event to see if there are SCO commands waiting for the role change.
754 **
755 ** Returns          void
756 **
757 *******************************************************************************/
758 void btm_sco_chk_pend_rolechange (uint16_t hci_handle)
759 {
760 #if (BTM_MAX_SCO_LINKS>0)
761     uint16_t    xx;
762     uint16_t    acl_handle;
763     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[0];
764
765     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
766     {
767         if ((p->state == SCO_ST_PEND_ROLECHANGE) &&
768             ((acl_handle = BTM_GetHCIConnHandle (p->esco.data.bd_addr, BT_TRANSPORT_BR_EDR)) == hci_handle))
769
770         {
771             BTM_TRACE_API("btm_sco_chk_pend_rolechange -> (e)SCO Link for ACL handle 0x%04x", acl_handle);
772
773             if ((btm_send_connect_request(acl_handle, &p->esco.setup)) == BTM_CMD_STARTED)
774                 p->state = SCO_ST_CONNECTING;
775         }
776     }
777 #endif
778 }
779
780 /*******************************************************************************
781 **
782 ** Function        btm_sco_disc_chk_pend_for_modechange
783 **
784 ** Description     This function is called by btm when there is a mode change
785 **                 event to see if there are SCO  disconnect commands waiting for the mode change.
786 **
787 ** Returns         void
788 **
789 *******************************************************************************/
790 void btm_sco_disc_chk_pend_for_modechange (uint16_t hci_handle)
791 {
792 #if (BTM_MAX_SCO_LINKS>0)
793     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[0];
794
795     BTM_TRACE_DEBUG("%s: hci_handle 0x%04x, p->state 0x%02x", __func__,
796                      hci_handle, p->state);
797
798     for (uint16_t xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
799     {
800         if ((p->state == SCO_ST_PEND_MODECHANGE) &&
801             (BTM_GetHCIConnHandle (p->esco.data.bd_addr, BT_TRANSPORT_BR_EDR)) == hci_handle)
802
803         {
804             BTM_TRACE_DEBUG("%s: SCO Link handle 0x%04x", __func__, p->hci_handle);
805             BTM_RemoveSco(xx);
806         }
807     }
808 #endif
809 }
810
811 /*******************************************************************************
812 **
813 ** Function         btm_sco_conn_req
814 **
815 ** Description      This function is called by BTIF when an SCO connection
816 **                  request is received from a remote.
817 **
818 ** Returns          void
819 **
820 *******************************************************************************/
821 void btm_sco_conn_req (BD_ADDR bda,  DEV_CLASS dev_class, uint8_t link_type)
822 {
823 #if (BTM_MAX_SCO_LINKS>0)
824     tSCO_CB     *p_sco = &btm_cb.sco_cb;
825     tSCO_CONN   *p = &p_sco->sco_db[0];
826     uint16_t    xx;
827     tBTM_ESCO_CONN_REQ_EVT_DATA evt_data;
828
829     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
830     {
831         /*
832          * If the sco state is in the SCO_ST_CONNECTING state, we still need
833          * to return accept sco to avoid race conditon for sco creation
834          */
835         int rem_bd_matches = p->rem_bd_known &&
836           !memcmp (p->esco.data.bd_addr, bda, BD_ADDR_LEN);
837         if (((p->state == SCO_ST_CONNECTING) && rem_bd_matches) ||
838             ((p->state == SCO_ST_LISTENING) && (rem_bd_matches || !p->rem_bd_known)))
839         {
840             /* If this guy was a wildcard, he is not one any more */
841             p->rem_bd_known = true;
842             p->esco.data.link_type = link_type;
843             p->state = SCO_ST_W4_CONN_RSP;
844             memcpy (p->esco.data.bd_addr, bda, BD_ADDR_LEN);
845
846             /* If no callback, auto-accept the connection if packet types match */
847             if (!p->esco.p_esco_cback)
848             {
849                 /* If requesting eSCO reject if default parameters are SCO only */
850                 if ((link_type == BTM_LINK_TYPE_ESCO
851                     && !(p_sco->def_esco_parms.packet_types & BTM_ESCO_LINK_ONLY_MASK)
852                     && ((p_sco->def_esco_parms.packet_types & BTM_SCO_EXCEPTION_PKTS_MASK)
853                        == BTM_SCO_EXCEPTION_PKTS_MASK))
854
855                     /* Reject request if SCO is desired but no SCO packets delected */
856                     || (link_type == BTM_LINK_TYPE_SCO
857                     && !(p_sco->def_esco_parms.packet_types & BTM_SCO_LINK_ONLY_MASK)))
858                 {
859                     btm_esco_conn_rsp(xx, HCI_ERR_HOST_REJECT_RESOURCES, bda, NULL);
860                 }
861                 else    /* Accept the request */
862                 {
863                     btm_esco_conn_rsp(xx, HCI_SUCCESS, bda, NULL);
864                 }
865             }
866             else    /* Notify upper layer of connect indication */
867             {
868                 memcpy(evt_data.bd_addr, bda, BD_ADDR_LEN);
869                 memcpy(evt_data.dev_class, dev_class, DEV_CLASS_LEN);
870                 evt_data.link_type = link_type;
871                 evt_data.sco_inx = xx;
872                 p->esco.p_esco_cback(BTM_ESCO_CONN_REQ_EVT, (tBTM_ESCO_EVT_DATA *)&evt_data);
873             }
874
875             return;
876         }
877     }
878
879     /* TCS usage */
880     if (btm_cb.sco_cb.app_sco_ind_cb)
881     {
882         /* Now, try to find an unused control block */
883         for (xx = 0, p = &btm_cb.sco_cb.sco_db[0]; xx < BTM_MAX_SCO_LINKS; xx++, p++)
884         {
885             if (p->state == SCO_ST_UNUSED)
886             {
887                 p->is_orig = false;
888                 p->state = SCO_ST_LISTENING;
889
890                 p->esco.data.link_type = link_type;
891                 memcpy (p->esco.data.bd_addr, bda, BD_ADDR_LEN);
892                 p->rem_bd_known = true;
893                 break;
894             }
895         }
896         if( xx < BTM_MAX_SCO_LINKS)
897         {
898             btm_cb.sco_cb.app_sco_ind_cb(xx);
899             return;
900         }
901     }
902
903 #endif
904     /* If here, no one wants the SCO connection. Reject it */
905     BTM_TRACE_WARNING("btm_sco_conn_req: No one wants this SCO connection; rejecting it");
906     btm_esco_conn_rsp(BTM_MAX_SCO_LINKS, HCI_ERR_HOST_REJECT_RESOURCES, bda, NULL);
907 }
908
909 /*******************************************************************************
910 **
911 ** Function         btm_sco_connected
912 **
913 ** Description      This function is called by BTIF when an (e)SCO connection
914 **                  is connected.
915 **
916 ** Returns          void
917 **
918 *******************************************************************************/
919 void btm_sco_connected (uint8_t hci_status, BD_ADDR bda, uint16_t hci_handle,
920                         tBTM_ESCO_DATA *p_esco_data)
921 {
922 #if (BTM_MAX_SCO_LINKS>0)
923     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[0];
924     uint16_t    xx;
925     bool        spt = false;
926     tBTM_CHG_ESCO_PARAMS parms;
927 #endif
928
929     btm_cb.sco_cb.sco_disc_reason = hci_status;
930
931 #if (BTM_MAX_SCO_LINKS>0)
932     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
933     {
934         if (((p->state == SCO_ST_CONNECTING) ||
935              (p->state == SCO_ST_LISTENING)  ||
936              (p->state == SCO_ST_W4_CONN_RSP))
937          && (p->rem_bd_known)
938          && (!bda || !memcmp (p->esco.data.bd_addr, bda, BD_ADDR_LEN)))
939         {
940             if (hci_status != HCI_SUCCESS)
941             {
942                 /* Report the error if originator, otherwise remain in Listen mode */
943                 if (p->is_orig)
944                 {
945                     /* If role switch is pending, we need try again after role switch is complete */
946                     if(hci_status == HCI_ERR_ROLE_SWITCH_PENDING)
947                     {
948                         BTM_TRACE_API("Role Change pending for HCI handle 0x%04x",hci_handle);
949                         p->state = SCO_ST_PEND_ROLECHANGE;
950                     }
951                     /* avoid calling disconnect callback because of sco creation race */
952                     else if (hci_status != HCI_ERR_LMP_ERR_TRANS_COLLISION)
953                     {
954                         p->state = SCO_ST_UNUSED;
955                         (*p->p_disc_cb)(xx);
956                     }
957                 }
958                 else
959                 {
960                     /* Notify the upper layer that incoming sco connection has failed. */
961                     if (p->state == SCO_ST_CONNECTING)
962                     {
963                         p->state = SCO_ST_UNUSED;
964                         (*p->p_disc_cb)(xx);
965                     }
966                     else
967                         p->state = SCO_ST_LISTENING;
968                 }
969
970                 return;
971             }
972
973             if (p->state == SCO_ST_LISTENING)
974                 spt = true;
975
976             p->state = SCO_ST_CONNECTED;
977             p->hci_handle = hci_handle;
978
979             if (!btm_cb.sco_cb.esco_supported)
980             {
981                 p->esco.data.link_type = BTM_LINK_TYPE_SCO;
982                 if (spt)
983                 {
984                     parms.packet_types = p->esco.setup.packet_types;
985                     /* Keep the other parameters the same for SCO */
986                     parms.max_latency = p->esco.setup.max_latency;
987                     parms.retrans_effort = p->esco.setup.retrans_effort;
988
989                     BTM_ChangeEScoLinkParms(xx, &parms);
990                 }
991             }
992             else
993             {
994                 if (p_esco_data)
995                     p->esco.data = *p_esco_data;
996             }
997
998             (*p->p_conn_cb)(xx);
999
1000             return;
1001         }
1002     }
1003 #endif
1004 }
1005
1006
1007 /*******************************************************************************
1008 **
1009 ** Function         btm_find_scb_by_handle
1010 **
1011 ** Description      Look through all active SCO connection for a match based on the
1012 **                  HCI handle.
1013 **
1014 ** Returns          index to matched SCO connection CB, or BTM_MAX_SCO_LINKS if
1015 **                  no match.
1016 **
1017 *******************************************************************************/
1018 uint16_t btm_find_scb_by_handle (uint16_t handle)
1019 {
1020     int         xx;
1021     tSCO_CONN    *p = &btm_cb.sco_cb.sco_db[0];
1022
1023     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1024     {
1025         if ((p->state == SCO_ST_CONNECTED) && (p->hci_handle == handle))
1026         {
1027             return (xx);
1028         }
1029     }
1030
1031     /* If here, no match found */
1032     return (xx);
1033 }
1034
1035 /*******************************************************************************
1036 **
1037 ** Function         BTM_RemoveSco
1038 **
1039 ** Description      This function is called to remove a specific SCO connection.
1040 **
1041 ** Returns          status of the operation
1042 **
1043 *******************************************************************************/
1044 tBTM_STATUS BTM_RemoveSco (uint16_t sco_inx)
1045 {
1046 #if (BTM_MAX_SCO_LINKS>0)
1047     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[sco_inx];
1048     uint16_t     tempstate;
1049     tBTM_PM_STATE   state = BTM_PM_ST_INVALID;
1050
1051     BTM_TRACE_DEBUG("%s", __func__);
1052
1053     /* Validity check */
1054     if ((sco_inx >= BTM_MAX_SCO_LINKS) || (p->state == SCO_ST_UNUSED))
1055         return (BTM_UNKNOWN_ADDR);
1056
1057     /* If no HCI handle, simply drop the connection and return */
1058     if (p->hci_handle == BTM_INVALID_HCI_HANDLE || p->state == SCO_ST_PEND_UNPARK)
1059     {
1060         p->hci_handle = BTM_INVALID_HCI_HANDLE;
1061         p->state = SCO_ST_UNUSED;
1062         p->esco.p_esco_cback = NULL;    /* Deregister the eSCO event callback */
1063         return (BTM_SUCCESS);
1064     }
1065
1066     if ((btm_read_power_mode_state(p->esco.data.bd_addr, &state) == BTM_SUCCESS)
1067         && state == BTM_PM_ST_PENDING)
1068     {
1069         BTM_TRACE_DEBUG("%s: BTM_PM_ST_PENDING for ACL mapped with SCO Link 0x%04x",
1070                           __func__, p->hci_handle);
1071         p->state = SCO_ST_PEND_MODECHANGE;
1072         return (BTM_CMD_STARTED);
1073     }
1074
1075     tempstate = p->state;
1076     p->state = SCO_ST_DISCONNECTING;
1077
1078     btsnd_hcic_disconnect(p->hci_handle, HCI_ERR_PEER_USER);
1079
1080     return (BTM_CMD_STARTED);
1081 #else
1082     return (BTM_NO_RESOURCES);
1083 #endif
1084 }
1085
1086 /*******************************************************************************
1087 **
1088 ** Function         btm_remove_sco_links
1089 **
1090 ** Description      This function is called to remove all sco links for an ACL link.
1091 **
1092 ** Returns          void
1093 **
1094 *******************************************************************************/
1095 void btm_remove_sco_links (BD_ADDR bda)
1096 {
1097 #if (BTM_MAX_SCO_LINKS>0)
1098     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[0];
1099     uint16_t     xx;
1100
1101     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1102     {
1103         if (p->rem_bd_known && (!memcmp (p->esco.data.bd_addr, bda, BD_ADDR_LEN)))
1104         {
1105             BTM_RemoveSco(xx);
1106         }
1107     }
1108 #endif
1109 }
1110
1111 /*******************************************************************************
1112 **
1113 ** Function         btm_sco_removed
1114 **
1115 ** Description      This function is called by BTIF when an SCO connection
1116 **                  is removed.
1117 **
1118 ** Returns          void
1119 **
1120 *******************************************************************************/
1121 void btm_sco_removed (uint16_t hci_handle, uint8_t reason)
1122 {
1123 #if (BTM_MAX_SCO_LINKS>0)
1124     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[0];
1125     uint16_t    xx;
1126 #endif
1127
1128     btm_cb.sco_cb.sco_disc_reason = reason;
1129
1130 #if (BTM_MAX_SCO_LINKS>0)
1131     p = &btm_cb.sco_cb.sco_db[0];
1132     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1133     {
1134         if ((p->state != SCO_ST_UNUSED) && (p->state != SCO_ST_LISTENING) && (p->hci_handle == hci_handle))
1135         {
1136             btm_sco_flush_sco_data(xx);
1137
1138             p->state = SCO_ST_UNUSED;
1139             p->hci_handle = BTM_INVALID_HCI_HANDLE;
1140             p->rem_bd_known = false;
1141             p->esco.p_esco_cback = NULL;    /* Deregister eSCO callback */
1142             (*p->p_disc_cb)(xx);
1143
1144             return;
1145         }
1146     }
1147 #endif
1148 }
1149
1150
1151 /*******************************************************************************
1152 **
1153 ** Function         btm_sco_acl_removed
1154 **
1155 ** Description      This function is called when an ACL connection is
1156 **                  removed. If the BD address is NULL, it is assumed that
1157 **                  the local device is down, and all SCO links are removed.
1158 **                  If a specific BD address is passed, only SCO connections
1159 **                  to that BD address are removed.
1160 **
1161 ** Returns          void
1162 **
1163 *******************************************************************************/
1164 void btm_sco_acl_removed (BD_ADDR bda)
1165 {
1166 #if (BTM_MAX_SCO_LINKS>0)
1167     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[0];
1168     uint16_t    xx;
1169
1170     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1171     {
1172         if (p->state != SCO_ST_UNUSED)
1173         {
1174             if ((!bda) || (!memcmp (p->esco.data.bd_addr, bda, BD_ADDR_LEN) && p->rem_bd_known))
1175             {
1176                 btm_sco_flush_sco_data(xx);
1177
1178                 p->state = SCO_ST_UNUSED;
1179                 p->esco.p_esco_cback = NULL;    /* Deregister eSCO callback */
1180                 (*p->p_disc_cb)(xx);
1181             }
1182         }
1183     }
1184 #endif
1185 }
1186
1187
1188 /*******************************************************************************
1189 **
1190 ** Function         BTM_SetScoPacketTypes
1191 **
1192 ** Description      This function is called to set the packet types used for
1193 **                  a specific SCO connection,
1194 **
1195 ** Parameters       pkt_types - One or more of the following
1196 **                  BTM_SCO_PKT_TYPES_MASK_HV1
1197 **                  BTM_SCO_PKT_TYPES_MASK_HV2
1198 **                  BTM_SCO_PKT_TYPES_MASK_HV3
1199 **                  BTM_SCO_PKT_TYPES_MASK_EV3
1200 **                  BTM_SCO_PKT_TYPES_MASK_EV4
1201 **                  BTM_SCO_PKT_TYPES_MASK_EV5
1202 **                  BTM_SCO_PKT_TYPES_MASK_NO_2_EV3
1203 **                  BTM_SCO_PKT_TYPES_MASK_NO_3_EV3
1204 **                  BTM_SCO_PKT_TYPES_MASK_NO_2_EV5
1205 **                  BTM_SCO_PKT_TYPES_MASK_NO_3_EV5
1206 **
1207 **                  BTM_SCO_LINK_ALL_MASK   - enables all supported types
1208 **
1209 ** Returns          status of the operation
1210 **
1211 *******************************************************************************/
1212 tBTM_STATUS BTM_SetScoPacketTypes (uint16_t sco_inx, uint16_t pkt_types)
1213 {
1214 #if (BTM_MAX_SCO_LINKS>0)
1215     tBTM_CHG_ESCO_PARAMS parms;
1216     tSCO_CONN           *p;
1217
1218     /* Validity check */
1219     if (sco_inx >= BTM_MAX_SCO_LINKS)
1220         return (BTM_UNKNOWN_ADDR);
1221
1222     p = &btm_cb.sco_cb.sco_db[sco_inx];
1223     parms.packet_types = pkt_types;
1224
1225     /* Keep the other parameters the same for SCO */
1226     parms.max_latency = p->esco.setup.max_latency;
1227     parms.retrans_effort = p->esco.setup.retrans_effort;
1228
1229     return (BTM_ChangeEScoLinkParms(sco_inx, &parms));
1230 #else
1231     return (BTM_UNKNOWN_ADDR);
1232 #endif
1233 }
1234
1235
1236 /*******************************************************************************
1237 **
1238 ** Function         BTM_ReadScoPacketTypes
1239 **
1240 ** Description      This function is read the packet types used for a specific
1241 **                  SCO connection.
1242 **
1243 ** Returns          Packet types supported for the connection
1244 **                  One or more of the following (bitmask):
1245 **                  BTM_SCO_PKT_TYPES_MASK_HV1
1246 **                  BTM_SCO_PKT_TYPES_MASK_HV2
1247 **                  BTM_SCO_PKT_TYPES_MASK_HV3
1248 **                  BTM_SCO_PKT_TYPES_MASK_EV3
1249 **                  BTM_SCO_PKT_TYPES_MASK_EV4
1250 **                  BTM_SCO_PKT_TYPES_MASK_EV5
1251 **                  BTM_SCO_PKT_TYPES_MASK_NO_2_EV3
1252 **                  BTM_SCO_PKT_TYPES_MASK_NO_3_EV3
1253 **                  BTM_SCO_PKT_TYPES_MASK_NO_2_EV5
1254 **                  BTM_SCO_PKT_TYPES_MASK_NO_3_EV5
1255 **
1256 *******************************************************************************/
1257 uint16_t BTM_ReadScoPacketTypes (uint16_t sco_inx)
1258 {
1259 #if (BTM_MAX_SCO_LINKS>0)
1260     tSCO_CONN *p = &btm_cb.sco_cb.sco_db[sco_inx];
1261
1262     /* Validity check */
1263     if ((sco_inx < BTM_MAX_SCO_LINKS) && (p->state == SCO_ST_CONNECTED))
1264         return (p->esco.setup.packet_types);
1265     else
1266         return (0);
1267 #else
1268     return (0);
1269 #endif
1270 }
1271
1272 /*******************************************************************************
1273 **
1274 ** Function         BTM_ReadScoDiscReason
1275 **
1276 ** Description      This function is returns the reason why an (e)SCO connection
1277 **                  has been removed. It contains the value until read, or until
1278 **                  another (e)SCO connection has disconnected.
1279 **
1280 ** Returns          HCI reason or BTM_INVALID_SCO_DISC_REASON if not set.
1281 **
1282 *******************************************************************************/
1283 uint16_t BTM_ReadScoDiscReason (void)
1284 {
1285     uint16_t res = btm_cb.sco_cb.sco_disc_reason;
1286     btm_cb.sco_cb.sco_disc_reason = BTM_INVALID_SCO_DISC_REASON;
1287     return (res);
1288 }
1289
1290 /*******************************************************************************
1291 **
1292 ** Function         BTM_ReadDeviceScoPacketTypes
1293 **
1294 ** Description      This function is read the SCO packet types that
1295 **                  the device supports.
1296 **
1297 ** Returns          Packet types supported by the device.
1298 **                  One or more of the following (bitmask):
1299 **                  BTM_SCO_PKT_TYPES_MASK_HV1
1300 **                  BTM_SCO_PKT_TYPES_MASK_HV2
1301 **                  BTM_SCO_PKT_TYPES_MASK_HV3
1302 **                  BTM_SCO_PKT_TYPES_MASK_EV3
1303 **                  BTM_SCO_PKT_TYPES_MASK_EV4
1304 **                  BTM_SCO_PKT_TYPES_MASK_EV5
1305 **                  BTM_SCO_PKT_TYPES_MASK_NO_2_EV3
1306 **                  BTM_SCO_PKT_TYPES_MASK_NO_3_EV3
1307 **                  BTM_SCO_PKT_TYPES_MASK_NO_2_EV5
1308 **                  BTM_SCO_PKT_TYPES_MASK_NO_3_EV5
1309 **
1310 *******************************************************************************/
1311 uint16_t BTM_ReadDeviceScoPacketTypes (void)
1312 {
1313     return (btm_cb.btm_sco_pkt_types_supported);
1314 }
1315
1316 /*******************************************************************************
1317 **
1318 ** Function         BTM_ReadScoHandle
1319 **
1320 ** Description      This function is used to read the HCI handle used for a specific
1321 **                  SCO connection,
1322 **
1323 ** Returns          handle for the connection, or 0xFFFF if invalid SCO index.
1324 **
1325 *******************************************************************************/
1326 uint16_t BTM_ReadScoHandle (uint16_t sco_inx)
1327 {
1328 #if (BTM_MAX_SCO_LINKS>0)
1329     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[sco_inx];
1330
1331     /* Validity check */
1332     if ((sco_inx < BTM_MAX_SCO_LINKS) && (p->state == SCO_ST_CONNECTED))
1333         return (p->hci_handle);
1334     else
1335         return (BTM_INVALID_HCI_HANDLE);
1336 #else
1337     return (BTM_INVALID_HCI_HANDLE);
1338 #endif
1339 }
1340
1341 /*******************************************************************************
1342 **
1343 ** Function         BTM_ReadScoBdAddr
1344 **
1345 ** Description      This function is read the remote BD Address for a specific
1346 **                  SCO connection,
1347 **
1348 ** Returns          pointer to BD address or NULL if not known
1349 **
1350 *******************************************************************************/
1351 uint8_t *BTM_ReadScoBdAddr (uint16_t sco_inx)
1352 {
1353 #if (BTM_MAX_SCO_LINKS>0)
1354     tSCO_CONN   *p = &btm_cb.sco_cb.sco_db[sco_inx];
1355
1356     /* Validity check */
1357     if ((sco_inx < BTM_MAX_SCO_LINKS) && (p->rem_bd_known))
1358         return (p->esco.data.bd_addr);
1359     else
1360         return (NULL);
1361 #else
1362     return (NULL);
1363 #endif
1364 }
1365
1366 /*******************************************************************************
1367 **
1368 ** Function         BTM_SetEScoMode
1369 **
1370 ** Description      This function sets up the negotiated parameters for SCO or
1371 **                  eSCO, and sets as the default mode used for outgoing calls to
1372 **                  BTM_CreateSco.  It does not change any currently active (e)SCO links.
1373 **                  Note:  Incoming (e)SCO connections will always use packet types
1374 **                      supported by the controller.  If eSCO is not desired the
1375 **                      feature should be disabled in the controller's feature mask.
1376 **
1377 ** Returns          BTM_SUCCESS if the successful.
1378 **                  BTM_BUSY if there are one or more active (e)SCO links.
1379 **
1380 *******************************************************************************/
1381 tBTM_STATUS BTM_SetEScoMode (tBTM_SCO_TYPE sco_mode, tBTM_ESCO_PARAMS *p_parms)
1382 {
1383     tSCO_CB          *p_esco = &btm_cb.sco_cb;
1384     tBTM_ESCO_PARAMS *p_def = &p_esco->def_esco_parms;
1385
1386     if (p_esco->esco_supported)
1387     {
1388         if (p_parms)
1389         {
1390             if (sco_mode == BTM_LINK_TYPE_ESCO)
1391                 *p_def = *p_parms;  /* Save as the default parameters */
1392             else    /* Load only the SCO packet types */
1393             {
1394                 p_def->packet_types = p_parms->packet_types;
1395                 p_def->tx_bw            = BTM_64KBITS_RATE;
1396                 p_def->rx_bw            = BTM_64KBITS_RATE;
1397                 p_def->max_latency      = 0x000a;
1398                 p_def->voice_contfmt    = 0x0060;
1399                 p_def->retrans_effort   = 0;
1400
1401                 /* OR in any exception packet types */
1402                 p_def->packet_types |= BTM_SCO_EXCEPTION_PKTS_MASK;
1403             }
1404         }
1405         p_esco->desired_sco_mode = sco_mode;
1406         BTM_TRACE_API("BTM_SetEScoMode -> mode %d",  sco_mode);
1407     }
1408     else
1409     {
1410         p_esco->desired_sco_mode = BTM_LINK_TYPE_SCO;
1411         p_def->packet_types &= BTM_SCO_LINK_ONLY_MASK;
1412         p_def->retrans_effort = 0;
1413         BTM_TRACE_API("BTM_SetEScoMode -> mode SCO (eSCO not supported)");
1414     }
1415
1416     BTM_TRACE_DEBUG("    txbw 0x%08x, rxbw 0x%08x, max_lat 0x%04x, voice 0x%04x, pkt 0x%04x, rtx effort 0x%02x",
1417                      p_def->tx_bw, p_def->rx_bw, p_def->max_latency,
1418                      p_def->voice_contfmt, p_def->packet_types,
1419                      p_def->retrans_effort);
1420
1421     return (BTM_SUCCESS);
1422 }
1423
1424
1425
1426 /*******************************************************************************
1427 **
1428 ** Function         BTM_RegForEScoEvts
1429 **
1430 ** Description      This function registers a SCO event callback with the
1431 **                  specified instance.  It should be used to received
1432 **                  connection indication events and change of link parameter
1433 **                  events.
1434 **
1435 ** Returns          BTM_SUCCESS if the successful.
1436 **                  BTM_ILLEGAL_VALUE if there is an illegal sco_inx
1437 **                  BTM_MODE_UNSUPPORTED if controller version is not BT1.2 or
1438 **                          later or does not support eSCO.
1439 **
1440 *******************************************************************************/
1441 tBTM_STATUS BTM_RegForEScoEvts (uint16_t sco_inx, tBTM_ESCO_CBACK *p_esco_cback)
1442 {
1443 #if (BTM_MAX_SCO_LINKS>0)
1444     if (!btm_cb.sco_cb.esco_supported)
1445     {
1446         btm_cb.sco_cb.sco_db[sco_inx].esco.p_esco_cback = NULL;
1447         return (BTM_MODE_UNSUPPORTED);
1448     }
1449
1450     if (sco_inx < BTM_MAX_SCO_LINKS &&
1451         btm_cb.sco_cb.sco_db[sco_inx].state != SCO_ST_UNUSED)
1452     {
1453         btm_cb.sco_cb.sco_db[sco_inx].esco.p_esco_cback = p_esco_cback;
1454         return (BTM_SUCCESS);
1455     }
1456     return (BTM_ILLEGAL_VALUE);
1457 #else
1458     return (BTM_MODE_UNSUPPORTED);
1459 #endif
1460 }
1461
1462 /*******************************************************************************
1463 **
1464 ** Function         BTM_ReadEScoLinkParms
1465 **
1466 ** Description      This function returns the current eSCO link parameters for
1467 **                  the specified handle.  This can be called anytime a connection
1468 **                  is active, but is typically called after receiving the SCO
1469 **                  opened callback.
1470 **
1471 **                  Note: If called over a 1.1 controller, only the packet types
1472 **                        field has meaning.
1473 **
1474 ** Returns          BTM_SUCCESS if returned data is valid connection.
1475 **                  BTM_WRONG_MODE if no connection with a peer device or bad sco_inx.
1476 **
1477 *******************************************************************************/
1478 tBTM_STATUS BTM_ReadEScoLinkParms (uint16_t sco_inx, tBTM_ESCO_DATA *p_parms)
1479 {
1480 #if (BTM_MAX_SCO_LINKS>0)
1481     uint8_t index;
1482
1483     BTM_TRACE_API("BTM_ReadEScoLinkParms -> sco_inx 0x%04x", sco_inx);
1484
1485     if (sco_inx < BTM_MAX_SCO_LINKS &&
1486         btm_cb.sco_cb.sco_db[sco_inx].state >= SCO_ST_CONNECTED)
1487     {
1488         *p_parms = btm_cb.sco_cb.sco_db[sco_inx].esco.data;
1489         return (BTM_SUCCESS);
1490     }
1491
1492     if (sco_inx == BTM_FIRST_ACTIVE_SCO_INDEX)
1493     {
1494         for (index = 0; index < BTM_MAX_SCO_LINKS; index++)
1495         {
1496             if (btm_cb.sco_cb.sco_db[index].state >= SCO_ST_CONNECTED)
1497             {
1498                 BTM_TRACE_API("BTM_ReadEScoLinkParms the first active SCO index is %d",index);
1499                 *p_parms = btm_cb.sco_cb.sco_db[index].esco.data;
1500                 return (BTM_SUCCESS);
1501             }
1502         }
1503     }
1504
1505 #endif
1506
1507     BTM_TRACE_API("BTM_ReadEScoLinkParms cannot find the SCO index!");
1508     memset(p_parms, 0, sizeof(tBTM_ESCO_DATA));
1509     return (BTM_WRONG_MODE);
1510 }
1511
1512 /*******************************************************************************
1513 **
1514 ** Function         BTM_ChangeEScoLinkParms
1515 **
1516 ** Description      This function requests renegotiation of the parameters on
1517 **                  the current eSCO Link.  If any of the changes are accepted
1518 **                  by the controllers, the BTM_ESCO_CHG_EVT event is sent in
1519 **                  the tBTM_ESCO_CBACK function with the current settings of
1520 **                  the link. The callback is registered through the call to
1521 **                  BTM_SetEScoMode.
1522 **
1523 **                  Note: If called over a SCO link (including 1.1 controller),
1524 **                        a change packet type request is sent out instead.
1525 **
1526 ** Returns          BTM_CMD_STARTED if command is successfully initiated.
1527 **                  BTM_NO_RESOURCES - not enough resources to initiate command.
1528 **                  BTM_WRONG_MODE if no connection with a peer device or bad sco_inx.
1529 **
1530 *******************************************************************************/
1531 tBTM_STATUS BTM_ChangeEScoLinkParms (uint16_t sco_inx, tBTM_CHG_ESCO_PARAMS *p_parms)
1532 {
1533 #if (BTM_MAX_SCO_LINKS>0)
1534     tBTM_ESCO_PARAMS *p_setup;
1535     tSCO_CONN        *p_sco;
1536     uint16_t          temp_pkt_types;
1537
1538     /* Make sure sco handle is valid and on an active link */
1539     if (sco_inx >= BTM_MAX_SCO_LINKS ||
1540         btm_cb.sco_cb.sco_db[sco_inx].state != SCO_ST_CONNECTED)
1541         return (BTM_WRONG_MODE);
1542
1543     p_sco = &btm_cb.sco_cb.sco_db[sco_inx];
1544     p_setup = &p_sco->esco.setup;
1545
1546     /* If SCO connection OR eSCO not supported just send change packet types */
1547     if (p_sco->esco.data.link_type == BTM_LINK_TYPE_SCO ||
1548         !btm_cb.sco_cb.esco_supported)
1549     {
1550         p_setup->packet_types = p_parms->packet_types &
1551             (btm_cb.btm_sco_pkt_types_supported & BTM_SCO_LINK_ONLY_MASK);
1552
1553
1554         BTM_TRACE_API("BTM_ChangeEScoLinkParms -> SCO Link for handle 0x%04x, pkt 0x%04x",
1555                          p_sco->hci_handle, p_setup->packet_types);
1556
1557         btsnd_hcic_change_conn_type(p_sco->hci_handle,
1558                                     BTM_ESCO_2_SCO(p_setup->packet_types));
1559     }
1560     else
1561     {
1562         temp_pkt_types = (p_parms->packet_types & BTM_SCO_SUPPORTED_PKTS_MASK &
1563                              btm_cb.btm_sco_pkt_types_supported);
1564
1565         /* OR in any exception packet types */
1566         temp_pkt_types |= ((p_parms->packet_types & BTM_SCO_EXCEPTION_PKTS_MASK) |
1567             (btm_cb.btm_sco_pkt_types_supported & BTM_SCO_EXCEPTION_PKTS_MASK));
1568
1569         BTM_TRACE_API("BTM_ChangeEScoLinkParms -> eSCO Link for handle 0x%04x", p_sco->hci_handle);
1570         BTM_TRACE_API("      txbw 0x%x, rxbw 0x%x, lat 0x%x, voice 0x%x, retrans 0x%02x, pkt 0x%04x",
1571                          p_setup->tx_bw, p_setup->rx_bw, p_parms->max_latency,
1572                          p_setup->voice_contfmt, p_parms->retrans_effort, temp_pkt_types);
1573
1574         /* When changing an existing link, only change latency, retrans, and pkts */
1575         btsnd_hcic_setup_esco_conn(p_sco->hci_handle, p_setup->tx_bw,
1576                                    p_setup->rx_bw, p_parms->max_latency,
1577                                    p_setup->voice_contfmt,
1578                                    p_parms->retrans_effort,
1579                                    temp_pkt_types);
1580         p_parms->packet_types = temp_pkt_types;
1581     }
1582
1583     return (BTM_CMD_STARTED);
1584 #else
1585     return (BTM_WRONG_MODE);
1586 #endif
1587 }
1588
1589 /*******************************************************************************
1590 **
1591 ** Function         BTM_EScoConnRsp
1592 **
1593 ** Description      This function is called upon receipt of an (e)SCO connection
1594 **                  request event (BTM_ESCO_CONN_REQ_EVT) to accept or reject
1595 **                  the request. Parameters used to negotiate eSCO links.
1596 **                  If p_parms is NULL, then values set through BTM_SetEScoMode
1597 **                  are used.
1598 **                  If the link type of the incoming request is SCO, then only
1599 **                  the tx_bw, max_latency, content format, and packet_types are
1600 **                  valid.  The hci_status parameter should be
1601 **                  ([0x0] to accept, [0x0d..0x0f] to reject)
1602 **
1603 **
1604 ** Returns          void
1605 **
1606 *******************************************************************************/
1607 void BTM_EScoConnRsp (uint16_t sco_inx, uint8_t hci_status, tBTM_ESCO_PARAMS *p_parms)
1608 {
1609 #if (BTM_MAX_SCO_LINKS>0)
1610     if (sco_inx < BTM_MAX_SCO_LINKS &&
1611         btm_cb.sco_cb.sco_db[sco_inx].state == SCO_ST_W4_CONN_RSP)
1612     {
1613         btm_esco_conn_rsp(sco_inx, hci_status,
1614                           btm_cb.sco_cb.sco_db[sco_inx].esco.data.bd_addr,
1615                           p_parms);
1616     }
1617 #endif
1618 }
1619
1620 /*******************************************************************************
1621 **
1622 ** Function         btm_read_def_esco_mode
1623 **
1624 ** Description      This function copies the current default esco settings into
1625 **                  the return buffer.
1626 **
1627 ** Returns          tBTM_SCO_TYPE
1628 **
1629 *******************************************************************************/
1630 tBTM_SCO_TYPE btm_read_def_esco_mode (tBTM_ESCO_PARAMS *p_parms)
1631 {
1632 #if (BTM_MAX_SCO_LINKS>0)
1633     *p_parms = btm_cb.sco_cb.def_esco_parms;
1634     return btm_cb.sco_cb.desired_sco_mode;
1635 #else
1636     return BTM_LINK_TYPE_SCO;
1637 #endif
1638 }
1639
1640 /*******************************************************************************
1641 **
1642 ** Function         btm_esco_proc_conn_chg
1643 **
1644 ** Description      This function is called by BTIF when an SCO connection
1645 **                  is changed.
1646 **
1647 ** Returns          void
1648 **
1649 *******************************************************************************/
1650 void btm_esco_proc_conn_chg (uint8_t status, uint16_t handle, uint8_t tx_interval,
1651                              uint8_t retrans_window, uint16_t rx_pkt_len,
1652                              uint16_t tx_pkt_len)
1653 {
1654 #if (BTM_MAX_SCO_LINKS>0)
1655     tSCO_CONN               *p = &btm_cb.sco_cb.sco_db[0];
1656     tBTM_CHG_ESCO_EVT_DATA   data;
1657     uint16_t                 xx;
1658
1659     BTM_TRACE_EVENT("btm_esco_proc_conn_chg -> handle 0x%04x, status 0x%02x",
1660                       handle, status);
1661
1662     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1663     {
1664         if (p->state == SCO_ST_CONNECTED && handle == p->hci_handle)
1665         {
1666             /* If upper layer wants notification */
1667             if (p->esco.p_esco_cback)
1668             {
1669                 memcpy(data.bd_addr, p->esco.data.bd_addr, BD_ADDR_LEN);
1670                 data.hci_status = status;
1671                 data.sco_inx = xx;
1672                 data.rx_pkt_len = p->esco.data.rx_pkt_len = rx_pkt_len;
1673                 data.tx_pkt_len = p->esco.data.tx_pkt_len = tx_pkt_len;
1674                 data.tx_interval = p->esco.data.tx_interval = tx_interval;
1675                 data.retrans_window = p->esco.data.retrans_window = retrans_window;
1676
1677                 (*p->esco.p_esco_cback)(BTM_ESCO_CHG_EVT,
1678                                         (tBTM_ESCO_EVT_DATA *)&data);
1679             }
1680             return;
1681         }
1682     }
1683 #endif
1684 }
1685
1686 /*******************************************************************************
1687 **
1688 ** Function         btm_is_sco_active
1689 **
1690 ** Description      This function is called to see if a SCO handle is already in
1691 **                  use.
1692 **
1693 ** Returns          bool
1694 **
1695 *******************************************************************************/
1696 bool    btm_is_sco_active (uint16_t handle)
1697 {
1698 #if (BTM_MAX_SCO_LINKS>0)
1699     uint16_t   xx;
1700     tSCO_CONN *p = &btm_cb.sco_cb.sco_db[0];
1701
1702     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1703     {
1704         if (handle == p->hci_handle && p->state == SCO_ST_CONNECTED)
1705             return (true);
1706     }
1707 #endif
1708     return (false);
1709 }
1710
1711 /*******************************************************************************
1712 **
1713 ** Function         BTM_GetNumScoLinks
1714 **
1715 ** Description      This function returns the number of active sco links.
1716 **
1717 ** Returns          uint8_t
1718 **
1719 *******************************************************************************/
1720 uint8_t BTM_GetNumScoLinks (void)
1721 {
1722 #if (BTM_MAX_SCO_LINKS>0)
1723     tSCO_CONN *p = &btm_cb.sco_cb.sco_db[0];
1724     uint16_t   xx;
1725     uint8_t    num_scos = 0;
1726
1727     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1728     {
1729         switch (p->state)
1730         {
1731         case SCO_ST_W4_CONN_RSP:
1732         case SCO_ST_CONNECTING:
1733         case SCO_ST_CONNECTED:
1734         case SCO_ST_DISCONNECTING:
1735         case SCO_ST_PEND_UNPARK:
1736             num_scos++;
1737         }
1738     }
1739     return (num_scos);
1740 #else
1741     return (0);
1742 #endif
1743 }
1744
1745
1746 /*******************************************************************************
1747 **
1748 ** Function         btm_is_sco_active_by_bdaddr
1749 **
1750 ** Description      This function is called to see if a SCO active to a bd address.
1751 **
1752 ** Returns          bool
1753 **
1754 *******************************************************************************/
1755 bool    btm_is_sco_active_by_bdaddr (BD_ADDR remote_bda)
1756 {
1757 #if (BTM_MAX_SCO_LINKS>0)
1758     uint8_t xx;
1759     tSCO_CONN *p = &btm_cb.sco_cb.sco_db[0];
1760
1761     /* If any SCO is being established to the remote BD address, refuse this */
1762     for (xx = 0; xx < BTM_MAX_SCO_LINKS; xx++, p++)
1763     {
1764         if ((!memcmp (p->esco.data.bd_addr, remote_bda, BD_ADDR_LEN)) && (p->state == SCO_ST_CONNECTED))
1765         {
1766             return (true);
1767         }
1768     }
1769 #endif
1770     return (false);
1771 }
1772 #else   /* SCO_EXCLUDED == TRUE (Link in stubs) */
1773
1774 tBTM_STATUS BTM_CreateSco (BD_ADDR remote_bda, bool    is_orig,
1775                            uint16_t pkt_types, uint16_t *p_sco_inx,
1776                            tBTM_SCO_CB *p_conn_cb,
1777                            tBTM_SCO_CB *p_disc_cb) {return (BTM_NO_RESOURCES);}
1778 tBTM_STATUS BTM_RemoveSco (uint16_t sco_inx) {return (BTM_NO_RESOURCES);}
1779 tBTM_STATUS BTM_SetScoPacketTypes (uint16_t sco_inx, uint16_t pkt_types) {return (BTM_NO_RESOURCES);}
1780 uint16_t BTM_ReadScoPacketTypes (uint16_t sco_inx) {return (0);}
1781 uint16_t BTM_ReadDeviceScoPacketTypes (void) {return (0);}
1782 uint16_t BTM_ReadScoHandle (uint16_t sco_inx) {return (BTM_INVALID_HCI_HANDLE);}
1783 uint8_t *BTM_ReadScoBdAddr(uint16_t sco_inx) {return((uint8_t *) NULL);}
1784 uint16_t BTM_ReadScoDiscReason (void) {return (BTM_INVALID_SCO_DISC_REASON);}
1785 tBTM_STATUS BTM_SetEScoMode (tBTM_SCO_TYPE sco_mode, tBTM_ESCO_PARAMS *p_parms) {return (BTM_MODE_UNSUPPORTED);}
1786 tBTM_STATUS BTM_RegForEScoEvts (uint16_t sco_inx, tBTM_ESCO_CBACK *p_esco_cback) { return (BTM_ILLEGAL_VALUE);}
1787 tBTM_STATUS BTM_ReadEScoLinkParms (uint16_t sco_inx, tBTM_ESCO_DATA *p_parms) { return (BTM_MODE_UNSUPPORTED);}
1788 tBTM_STATUS BTM_ChangeEScoLinkParms (uint16_t sco_inx, tBTM_CHG_ESCO_PARAMS *p_parms) { return (BTM_MODE_UNSUPPORTED);}
1789 void BTM_EScoConnRsp (uint16_t sco_inx, uint8_t hci_status, tBTM_ESCO_PARAMS *p_parms) {}
1790 uint8_t BTM_GetNumScoLinks (void)  {return (0);}
1791
1792 #endif /* If SCO is being used */