btm_sco.c 66 KB

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