l2c_utils.c 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-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 L2CAP utility functions
  21. *
  22. ******************************************************************************/
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "osi/allocator.h"
  26. #include "device/controller.h"
  27. #include "stack/bt_types.h"
  28. #include "stack/hcimsgs.h"
  29. #include "stack/l2cdefs.h"
  30. #include "l2c_int.h"
  31. #include "stack/hcidefs.h"
  32. #include "stack/btu.h"
  33. #include "stack/btm_api.h"
  34. #include "btm_int.h"
  35. #include "stack/hcidefs.h"
  36. #include "osi/allocator.h"
  37. #include "osi/list.h"
  38. /*******************************************************************************
  39. **
  40. ** Function l2cu_allocate_lcb
  41. **
  42. ** Description Look for an unused LCB
  43. **
  44. ** Returns LCB address or NULL if none found
  45. **
  46. *******************************************************************************/
  47. tL2C_LCB *l2cu_allocate_lcb (BD_ADDR p_bd_addr, BOOLEAN is_bonding, tBT_TRANSPORT transport)
  48. {
  49. tL2C_LCB *p_lcb = NULL;
  50. bool list_ret = false;
  51. extern tL2C_LCB *l2cu_find_free_lcb (void);
  52. // temp solution
  53. p_lcb = l2cu_find_free_lcb();
  54. if(p_lcb != NULL) {
  55. list_ret = true;
  56. }
  57. #if (CLASSIC_BT_INCLUDED == TRUE)
  58. /* Check if peer device's and our BD_ADDR is same or not. It
  59. should be different to avoid 'Impersonation in the Pin Pairing
  60. Protocol' (CVE-2020-26555) vulnerability. */
  61. if (memcmp((uint8_t *)p_bd_addr, (uint8_t *)&controller_get_interface()->get_address()->address, sizeof (BD_ADDR)) == 0) {
  62. L2CAP_TRACE_ERROR ("%s connection rejected due to same BD ADDR", __func__);
  63. return (NULL);
  64. }
  65. #endif
  66. if(p_lcb == NULL && list_length(l2cb.p_lcb_pool) < MAX_L2CAP_LINKS) {
  67. p_lcb = (tL2C_LCB *)osi_malloc(sizeof(tL2C_LCB));
  68. if (p_lcb) {
  69. memset (p_lcb, 0, sizeof(tL2C_LCB));
  70. list_ret = list_append(l2cb.p_lcb_pool, p_lcb);
  71. }else {
  72. L2CAP_TRACE_ERROR("Error in allocating L2CAP Link Control Block");
  73. }
  74. }
  75. if (list_ret) {
  76. if (p_lcb) {
  77. btu_free_timer(&p_lcb->timer_entry);
  78. btu_free_timer(&p_lcb->info_timer_entry);
  79. btu_free_timer(&p_lcb->upda_con_timer);
  80. memset (p_lcb, 0, sizeof (tL2C_LCB));
  81. memcpy (p_lcb->remote_bd_addr, p_bd_addr, BD_ADDR_LEN);
  82. p_lcb->in_use = TRUE;
  83. p_lcb->link_state = LST_DISCONNECTED;
  84. p_lcb->handle = HCI_INVALID_HANDLE;
  85. p_lcb->link_flush_tout = 0xFFFF;
  86. p_lcb->timer_entry.param = (TIMER_PARAM_TYPE)p_lcb;
  87. p_lcb->info_timer_entry.param = (TIMER_PARAM_TYPE)p_lcb;
  88. p_lcb->upda_con_timer.param = (TIMER_PARAM_TYPE)p_lcb;
  89. p_lcb->idle_timeout = l2cb.idle_timeout;
  90. p_lcb->id = 1; /* spec does not allow '0' */
  91. p_lcb->is_bonding = is_bonding;
  92. #if (BLE_INCLUDED == TRUE)
  93. p_lcb->transport = transport;
  94. p_lcb->tx_data_len = controller_get_interface()->get_ble_default_data_packet_length();
  95. p_lcb->le_sec_pending_q = fixed_queue_new(QUEUE_SIZE_MAX);
  96. if (transport == BT_TRANSPORT_LE) {
  97. l2cb.num_ble_links_active++;
  98. l2c_ble_link_adjust_allocation();
  99. } else
  100. #endif
  101. {
  102. l2cb.num_links_active++;
  103. l2c_link_adjust_allocation();
  104. }
  105. p_lcb->link_xmit_data_q = list_new(NULL);
  106. return (p_lcb);
  107. }
  108. }
  109. /* If here, no free LCB found */
  110. return (NULL);
  111. }
  112. /*******************************************************************************
  113. **
  114. ** Function l2cu_update_lcb_4_bonding
  115. **
  116. ** Description Mark the lcb for bonding. Used when bonding takes place on
  117. ** an existing ACL connection. (Pre-Lisbon devices)
  118. **
  119. ** Returns Nothing
  120. **
  121. *******************************************************************************/
  122. void l2cu_update_lcb_4_bonding (BD_ADDR p_bd_addr, BOOLEAN is_bonding)
  123. {
  124. tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (p_bd_addr, BT_TRANSPORT_BR_EDR);
  125. if (p_lcb) {
  126. p_lcb->is_bonding = is_bonding;
  127. }
  128. }
  129. /*******************************************************************************
  130. **
  131. ** Function l2cu_release_lcb
  132. **
  133. ** Description Release an LCB. All timers will be stopped, channels
  134. ** dropped, buffers returned etc.
  135. **
  136. ** Returns void
  137. **
  138. *******************************************************************************/
  139. void l2cu_release_lcb (tL2C_LCB *p_lcb)
  140. {
  141. tL2C_CCB *p_ccb;
  142. p_lcb->in_use = FALSE;
  143. p_lcb->is_bonding = FALSE;
  144. #if (BLE_INCLUDED == TRUE)
  145. p_lcb->retry_create_con = 0;
  146. p_lcb->start_time_s = 0;
  147. #endif // #if (BLE_INCLUDED == TRUE)
  148. /* Stop and release timers */
  149. btu_free_timer (&p_lcb->timer_entry);
  150. memset(&p_lcb->timer_entry, 0, sizeof(TIMER_LIST_ENT));
  151. btu_free_timer (&p_lcb->info_timer_entry);
  152. memset(&p_lcb->info_timer_entry, 0, sizeof(TIMER_LIST_ENT));
  153. btu_free_timer(&p_lcb->upda_con_timer);
  154. memset(&p_lcb->upda_con_timer, 0, sizeof(TIMER_LIST_ENT));
  155. /* Release any unfinished L2CAP packet on this link */
  156. if (p_lcb->p_hcit_rcv_acl) {
  157. osi_free(p_lcb->p_hcit_rcv_acl);
  158. p_lcb->p_hcit_rcv_acl = NULL;
  159. }
  160. #if BTM_SCO_INCLUDED == TRUE
  161. #if (BLE_INCLUDED == TRUE)
  162. if (p_lcb->transport == BT_TRANSPORT_BR_EDR)
  163. #endif
  164. {
  165. /* Release all SCO links */
  166. btm_remove_sco_links(p_lcb->remote_bd_addr);
  167. }
  168. #endif
  169. if (p_lcb->sent_not_acked > 0) {
  170. #if (BLE_INCLUDED == TRUE)
  171. if (p_lcb->transport == BT_TRANSPORT_LE) {
  172. l2cb.controller_le_xmit_window += p_lcb->sent_not_acked;
  173. if (l2cb.controller_le_xmit_window > l2cb.num_lm_ble_bufs) {
  174. l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
  175. }
  176. } else
  177. #endif
  178. {
  179. l2cb.controller_xmit_window += p_lcb->sent_not_acked;
  180. if (l2cb.controller_xmit_window > l2cb.num_lm_acl_bufs) {
  181. l2cb.controller_xmit_window = l2cb.num_lm_acl_bufs;
  182. }
  183. }
  184. }
  185. #if (BLE_INCLUDED == TRUE)
  186. // Reset BLE connecting flag only if the address matches
  187. if (!memcmp(l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN)) {
  188. l2cb.is_ble_connecting = FALSE;
  189. }
  190. #endif
  191. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  192. l2cu_process_fixed_disc_cback(p_lcb);
  193. #endif
  194. /* Ensure no CCBs left on this LCB */
  195. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_lcb->ccb_queue.p_first_ccb) {
  196. l2cu_release_ccb (p_ccb);
  197. }
  198. /* Tell BTM Acl management the link was removed */
  199. if ((p_lcb->link_state == LST_CONNECTED) || (p_lcb->link_state == LST_DISCONNECTING)) {
  200. #if (BLE_INCLUDED == TRUE)
  201. btm_acl_removed (p_lcb->remote_bd_addr, p_lcb->transport);
  202. #else
  203. btm_acl_removed (p_lcb->remote_bd_addr, BT_TRANSPORT_BR_EDR);
  204. #endif
  205. }
  206. /* Release any held buffers */
  207. if (p_lcb->link_xmit_data_q) {
  208. while (!list_is_empty(p_lcb->link_xmit_data_q)) {
  209. BT_HDR *p_buf = list_front(p_lcb->link_xmit_data_q);
  210. list_remove(p_lcb->link_xmit_data_q, p_buf);
  211. osi_free(p_buf);
  212. }
  213. list_free(p_lcb->link_xmit_data_q);
  214. p_lcb->link_xmit_data_q = NULL;
  215. }
  216. #if (L2CAP_UCD_INCLUDED == TRUE)
  217. /* clean up any security pending UCD */
  218. l2c_ucd_delete_sec_pending_q(p_lcb);
  219. #endif
  220. #if BLE_INCLUDED == TRUE
  221. /* Re-adjust flow control windows make sure it does not go negative */
  222. if (p_lcb->transport == BT_TRANSPORT_LE) {
  223. if (l2cb.num_ble_links_active >= 1) {
  224. l2cb.num_ble_links_active--;
  225. }
  226. l2c_ble_link_adjust_allocation();
  227. } else
  228. #endif
  229. {
  230. if (l2cb.num_links_active >= 1) {
  231. l2cb.num_links_active--;
  232. }
  233. l2c_link_adjust_allocation();
  234. }
  235. /* Check for ping outstanding */
  236. if (p_lcb->p_echo_rsp_cb) {
  237. tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
  238. /* Zero out the callback in case app immediately calls us again */
  239. p_lcb->p_echo_rsp_cb = NULL;
  240. (*p_cb) (L2CAP_PING_RESULT_NO_LINK);
  241. }
  242. #if (BLE_INCLUDED == TRUE)
  243. /* Check and release all the LE COC connections waiting for security */
  244. if (p_lcb->le_sec_pending_q)
  245. {
  246. while (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
  247. {
  248. tL2CAP_SEC_DATA *p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q, FIXED_QUEUE_MAX_TIMEOUT);
  249. if (p_buf->p_callback) {
  250. p_buf->p_callback(p_lcb->remote_bd_addr, p_lcb->transport, p_buf->p_ref_data, BTM_DEV_RESET);
  251. }
  252. osi_free(p_buf);
  253. }
  254. fixed_queue_free(p_lcb->le_sec_pending_q, NULL);
  255. p_lcb->le_sec_pending_q = NULL;
  256. }
  257. #endif ///BLE_INCLUDED == TRUE
  258. }
  259. /*******************************************************************************
  260. **
  261. ** Function l2cu_find_lcb_by_bd_addr
  262. **
  263. ** Description Look through all active LCBs for a match based on the
  264. ** remote BD address.
  265. **
  266. ** Returns pointer to matched LCB, or NULL if no match
  267. **
  268. *******************************************************************************/
  269. tL2C_LCB *l2cu_find_lcb_by_bd_addr (BD_ADDR p_bd_addr, tBT_TRANSPORT transport)
  270. {
  271. list_node_t *p_node = NULL;
  272. tL2C_LCB *p_lcb = NULL;
  273. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  274. p_lcb = list_node(p_node);
  275. if ((p_lcb->in_use) &&
  276. #if BLE_INCLUDED == TRUE
  277. p_lcb->transport == transport &&
  278. #endif
  279. (!memcmp (p_lcb->remote_bd_addr, p_bd_addr, BD_ADDR_LEN))) {
  280. return (p_lcb);
  281. }
  282. }
  283. /* If here, no match found */
  284. return (NULL);
  285. }
  286. tL2C_LCB *l2cu_find_free_lcb (void)
  287. {
  288. list_node_t *p_node = NULL;
  289. tL2C_LCB *p_lcb = NULL;
  290. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  291. p_lcb = list_node(p_node);
  292. if (!p_lcb->in_use) {
  293. return (p_lcb);
  294. }
  295. }
  296. /* If here, no match found */
  297. return (NULL);
  298. }
  299. uint8_t l2cu_plcb_active_count(void)
  300. {
  301. list_node_t *p_node = NULL;
  302. tL2C_LCB *p_lcb = NULL;
  303. uint8_t active_count = 0;
  304. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  305. p_lcb = list_node(p_node);
  306. if (p_lcb && p_lcb->in_use) {
  307. active_count ++;
  308. }
  309. }
  310. if (active_count >= MAX_L2CAP_CHANNELS) {
  311. L2CAP_TRACE_ERROR("error active count");
  312. active_count = 0;
  313. }
  314. L2CAP_TRACE_DEBUG("plcb active count %d", active_count);
  315. return active_count;
  316. }
  317. /*******************************************************************************
  318. **
  319. ** Function l2cu_get_conn_role
  320. **
  321. ** Description Determine the desired role (master or slave) of a link.
  322. ** If already got a slave link, this one must be a master. If
  323. ** already got at least 1 link where we are the master, make this
  324. ** also a master.
  325. **
  326. ** Returns HCI_ROLE_MASTER or HCI_ROLE_SLAVE
  327. **
  328. *******************************************************************************/
  329. UINT8 l2cu_get_conn_role (tL2C_LCB *p_this_lcb)
  330. {
  331. return l2cb.desire_role;
  332. }
  333. /*******************************************************************************
  334. **
  335. ** Function l2c_is_cmd_rejected
  336. **
  337. ** Description Checks if cmd_code is command or response
  338. ** If a command it will be rejected per spec.
  339. ** This function is used when a illegal packet length is detected
  340. **
  341. ** Returns BOOLEAN - TRUE if cmd_code is a command and it is rejected,
  342. ** FALSE if response code. (command not rejected)
  343. **
  344. *******************************************************************************/
  345. BOOLEAN l2c_is_cmd_rejected (UINT8 cmd_code, UINT8 id, tL2C_LCB *p_lcb)
  346. {
  347. switch (cmd_code) {
  348. case L2CAP_CMD_CONN_REQ:
  349. case L2CAP_CMD_CONFIG_REQ:
  350. case L2CAP_CMD_DISC_REQ:
  351. case L2CAP_CMD_ECHO_REQ:
  352. case L2CAP_CMD_INFO_REQ:
  353. case L2CAP_CMD_AMP_CONN_REQ:
  354. case L2CAP_CMD_AMP_MOVE_REQ:
  355. case L2CAP_CMD_BLE_UPDATE_REQ:
  356. l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_MTU_EXCEEDED, id, L2CAP_DEFAULT_MTU, 0);
  357. L2CAP_TRACE_WARNING ("Dumping first Command (%d)", cmd_code);
  358. return TRUE;
  359. default: /* Otherwise a response */
  360. return FALSE;
  361. }
  362. }
  363. /*******************************************************************************
  364. **
  365. ** Function l2cu_build_header
  366. **
  367. ** Description Builds the L2CAP command packet header
  368. **
  369. ** Returns Pointer to allocated packet or NULL if no resources
  370. **
  371. *******************************************************************************/
  372. BT_HDR *l2cu_build_header (tL2C_LCB *p_lcb, UINT16 len, UINT8 cmd, UINT8 id)
  373. {
  374. BT_HDR *p_buf = (BT_HDR *)osi_malloc(L2CAP_CMD_BUF_SIZE);
  375. UINT8 *p;
  376. if (!p_buf) {
  377. return (NULL);
  378. }
  379. p_buf->offset = L2CAP_SEND_CMD_OFFSET;
  380. p_buf->len = len + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  381. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET;
  382. /* Put in HCI header - handle + pkt boundary */
  383. #if (BLE_INCLUDED == TRUE)
  384. if (p_lcb->transport == BT_TRANSPORT_LE) {
  385. UINT16_TO_STREAM (p, (p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT)));
  386. } else
  387. #endif
  388. {
  389. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  390. UINT16_TO_STREAM (p, p_lcb->handle | l2cb.non_flushable_pbf);
  391. #else
  392. UINT16_TO_STREAM (p, (p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT)));
  393. #endif
  394. }
  395. UINT16_TO_STREAM (p, len + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD);
  396. UINT16_TO_STREAM (p, len + L2CAP_CMD_OVERHEAD);
  397. #if (BLE_INCLUDED == TRUE)
  398. if (p_lcb->transport == BT_TRANSPORT_LE) {
  399. //counter_add("l2cap.ble.tx.bytes", p_buf->len);
  400. //counter_add("l2cap.ble.tx.pkts", 1);
  401. UINT16_TO_STREAM (p, L2CAP_BLE_SIGNALLING_CID);
  402. } else
  403. #endif
  404. {
  405. //counter_add("l2cap.sig.tx.bytes", p_buf->len);
  406. //counter_add("l2cap.sig.tx.pkts", 1);
  407. UINT16_TO_STREAM (p, L2CAP_SIGNALLING_CID);
  408. }
  409. /* Put in L2CAP command header */
  410. UINT8_TO_STREAM (p, cmd);
  411. UINT8_TO_STREAM (p, id);
  412. UINT16_TO_STREAM (p, len);
  413. return (p_buf);
  414. }
  415. /*******************************************************************************
  416. **
  417. ** Function l2cu_adj_id
  418. **
  419. ** Description Checks for valid ID based on specified mask
  420. ** and adjusts the id if invalid.
  421. **
  422. ** Returns void
  423. **
  424. *******************************************************************************/
  425. void l2cu_adj_id (tL2C_LCB *p_lcb, UINT8 adj_mask)
  426. {
  427. if ((adj_mask & L2CAP_ADJ_ZERO_ID) && !p_lcb->id) {
  428. p_lcb->id++;
  429. }
  430. }
  431. /*******************************************************************************
  432. **
  433. ** Function l2cu_send_peer_cmd_reject
  434. **
  435. ** Description Build and send an L2CAP "command reject" message
  436. ** to the peer.
  437. **
  438. ** Returns void
  439. **
  440. *******************************************************************************/
  441. void l2cu_send_peer_cmd_reject (tL2C_LCB *p_lcb, UINT16 reason, UINT8 rem_id,
  442. UINT16 p1, UINT16 p2)
  443. {
  444. UINT16 param_len;
  445. BT_HDR *p_buf;
  446. UINT8 *p;
  447. /* Put in L2CAP packet header */
  448. if (reason == L2CAP_CMD_REJ_MTU_EXCEEDED) {
  449. param_len = 2;
  450. } else if (reason == L2CAP_CMD_REJ_INVALID_CID) {
  451. param_len = 4;
  452. } else {
  453. param_len = 0;
  454. }
  455. if ((p_buf = l2cu_build_header (p_lcb, (UINT16) (L2CAP_CMD_REJECT_LEN + param_len), L2CAP_CMD_REJECT, rem_id)) == NULL ) {
  456. L2CAP_TRACE_WARNING ("L2CAP - no buffer cmd_rej");
  457. return;
  458. }
  459. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  460. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  461. UINT16_TO_STREAM (p, reason);
  462. if (param_len >= 2) {
  463. UINT16_TO_STREAM (p, p1);
  464. }
  465. if (param_len >= 4) {
  466. UINT16_TO_STREAM (p, p2);
  467. }
  468. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  469. }
  470. /*******************************************************************************
  471. **
  472. ** Function l2cu_send_peer_connect_req
  473. **
  474. ** Description Build and send an L2CAP "connection request" message
  475. ** to the peer.
  476. **
  477. ** Returns void
  478. **
  479. *******************************************************************************/
  480. void l2cu_send_peer_connect_req (tL2C_CCB *p_ccb)
  481. {
  482. BT_HDR *p_buf;
  483. UINT8 *p;
  484. /* Create an identifier for this packet */
  485. p_ccb->p_lcb->id++;
  486. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  487. p_ccb->local_id = p_ccb->p_lcb->id;
  488. if ((p_buf = l2cu_build_header (p_ccb->p_lcb, L2CAP_CONN_REQ_LEN, L2CAP_CMD_CONN_REQ,
  489. p_ccb->local_id)) == NULL) {
  490. L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
  491. return;
  492. }
  493. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  494. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  495. UINT16_TO_STREAM (p, p_ccb->p_rcb->real_psm);
  496. UINT16_TO_STREAM (p, p_ccb->local_cid);
  497. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  498. }
  499. /*******************************************************************************
  500. **
  501. ** Function l2cu_send_peer_connect_rsp
  502. **
  503. ** Description Build and send an L2CAP "connection response" message
  504. ** to the peer.
  505. **
  506. ** Returns void
  507. **
  508. *******************************************************************************/
  509. void l2cu_send_peer_connect_rsp (tL2C_CCB *p_ccb, UINT16 result, UINT16 status)
  510. {
  511. BT_HDR *p_buf;
  512. UINT8 *p;
  513. if (result == L2CAP_CONN_PENDING) {
  514. /* if we already sent pending response */
  515. if (p_ccb->flags & CCB_FLAG_SENT_PENDING) {
  516. return;
  517. } else {
  518. p_ccb->flags |= CCB_FLAG_SENT_PENDING;
  519. }
  520. }
  521. if ((p_buf = l2cu_build_header(p_ccb->p_lcb, L2CAP_CONN_RSP_LEN, L2CAP_CMD_CONN_RSP, p_ccb->remote_id)) == NULL) {
  522. L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_rsp");
  523. return;
  524. }
  525. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  526. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  527. UINT16_TO_STREAM (p, p_ccb->local_cid);
  528. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  529. UINT16_TO_STREAM (p, result);
  530. UINT16_TO_STREAM (p, status);
  531. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  532. }
  533. /*******************************************************************************
  534. **
  535. ** Function l2cu_reject_connection
  536. **
  537. ** Description Build and send an L2CAP "connection response neg" message
  538. ** to the peer. This function is called when there is no peer
  539. ** CCB (non-existant PSM or no resources).
  540. **
  541. ** Returns void
  542. **
  543. *******************************************************************************/
  544. void l2cu_reject_connection (tL2C_LCB *p_lcb, UINT16 remote_cid, UINT8 rem_id, UINT16 result)
  545. {
  546. BT_HDR *p_buf;
  547. UINT8 *p;
  548. if ((p_buf = l2cu_build_header(p_lcb, L2CAP_CONN_RSP_LEN, L2CAP_CMD_CONN_RSP, rem_id)) == NULL ) {
  549. L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
  550. return;
  551. }
  552. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  553. UINT16_TO_STREAM (p, 0); /* Local CID of 0 */
  554. UINT16_TO_STREAM (p, remote_cid);
  555. UINT16_TO_STREAM (p, result);
  556. UINT16_TO_STREAM (p, 0); /* Status of 0 */
  557. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  558. }
  559. /*******************************************************************************
  560. **
  561. ** Function l2cu_send_peer_config_req
  562. **
  563. ** Description Build and send an L2CAP "configuration request" message
  564. ** to the peer.
  565. **
  566. ** Returns void
  567. **
  568. *******************************************************************************/
  569. void l2cu_send_peer_config_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  570. {
  571. BT_HDR *p_buf;
  572. UINT16 cfg_len = 0;
  573. UINT8 *p;
  574. /* Create an identifier for this packet */
  575. p_ccb->p_lcb->id++;
  576. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  577. p_ccb->local_id = p_ccb->p_lcb->id;
  578. if (p_cfg->mtu_present) {
  579. cfg_len += L2CAP_CFG_MTU_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  580. }
  581. if (p_cfg->flush_to_present) {
  582. cfg_len += L2CAP_CFG_FLUSH_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  583. }
  584. if (p_cfg->qos_present) {
  585. cfg_len += L2CAP_CFG_QOS_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  586. }
  587. if (p_cfg->fcr_present) {
  588. cfg_len += L2CAP_CFG_FCR_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  589. }
  590. if (p_cfg->fcs_present) {
  591. cfg_len += L2CAP_CFG_FCS_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  592. }
  593. if (p_cfg->ext_flow_spec_present) {
  594. cfg_len += L2CAP_CFG_EXT_FLOW_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  595. }
  596. if ((p_buf = l2cu_build_header (p_ccb->p_lcb, (UINT16) (L2CAP_CONFIG_REQ_LEN + cfg_len),
  597. L2CAP_CMD_CONFIG_REQ, p_ccb->local_id)) == NULL ) {
  598. L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
  599. return;
  600. }
  601. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  602. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  603. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  604. UINT16_TO_STREAM (p, p_cfg->flags); /* Flags (continuation) */
  605. /* Now, put the options */
  606. if (p_cfg->mtu_present) {
  607. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_MTU);
  608. UINT8_TO_STREAM (p, L2CAP_CFG_MTU_OPTION_LEN);
  609. UINT16_TO_STREAM (p, p_cfg->mtu);
  610. }
  611. if (p_cfg->flush_to_present) {
  612. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_FLUSH_TOUT);
  613. UINT8_TO_STREAM (p, L2CAP_CFG_FLUSH_OPTION_LEN);
  614. UINT16_TO_STREAM (p, p_cfg->flush_to);
  615. }
  616. if (p_cfg->qos_present) {
  617. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_QOS);
  618. UINT8_TO_STREAM (p, L2CAP_CFG_QOS_OPTION_LEN);
  619. UINT8_TO_STREAM (p, p_cfg->qos.qos_flags);
  620. UINT8_TO_STREAM (p, p_cfg->qos.service_type);
  621. UINT32_TO_STREAM (p, p_cfg->qos.token_rate);
  622. UINT32_TO_STREAM (p, p_cfg->qos.token_bucket_size);
  623. UINT32_TO_STREAM (p, p_cfg->qos.peak_bandwidth);
  624. UINT32_TO_STREAM (p, p_cfg->qos.latency);
  625. UINT32_TO_STREAM (p, p_cfg->qos.delay_variation);
  626. }
  627. if (p_cfg->fcr_present) {
  628. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_FCR);
  629. UINT8_TO_STREAM (p, L2CAP_CFG_FCR_OPTION_LEN);
  630. UINT8_TO_STREAM (p, p_cfg->fcr.mode);
  631. UINT8_TO_STREAM (p, p_cfg->fcr.tx_win_sz);
  632. UINT8_TO_STREAM (p, p_cfg->fcr.max_transmit);
  633. UINT16_TO_STREAM (p, p_cfg->fcr.rtrans_tout);
  634. UINT16_TO_STREAM (p, p_cfg->fcr.mon_tout);
  635. UINT16_TO_STREAM (p, p_cfg->fcr.mps);
  636. }
  637. if (p_cfg->fcs_present) {
  638. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_FCS);
  639. UINT8_TO_STREAM (p, L2CAP_CFG_FCS_OPTION_LEN);
  640. UINT8_TO_STREAM (p, p_cfg->fcs);
  641. }
  642. if (p_cfg->ext_flow_spec_present) {
  643. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_EXT_FLOW);
  644. UINT8_TO_STREAM (p, L2CAP_CFG_EXT_FLOW_OPTION_LEN);
  645. UINT8_TO_STREAM (p, p_cfg->ext_flow_spec.id);
  646. UINT8_TO_STREAM (p, p_cfg->ext_flow_spec.stype);
  647. UINT16_TO_STREAM (p, p_cfg->ext_flow_spec.max_sdu_size);
  648. UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.sdu_inter_time);
  649. UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.access_latency);
  650. UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.flush_timeout);
  651. }
  652. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  653. }
  654. /*******************************************************************************
  655. **
  656. ** Function l2cu_send_peer_config_rsp
  657. **
  658. ** Description Build and send an L2CAP "configuration response" message
  659. ** to the peer.
  660. **
  661. ** Returns void
  662. **
  663. *******************************************************************************/
  664. void l2cu_send_peer_config_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  665. {
  666. BT_HDR *p_buf;
  667. UINT16 cfg_len = 0;
  668. UINT8 *p;
  669. /* Create an identifier for this packet */
  670. if (p_cfg->mtu_present) {
  671. cfg_len += L2CAP_CFG_MTU_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  672. }
  673. if (p_cfg->flush_to_present) {
  674. cfg_len += L2CAP_CFG_FLUSH_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  675. }
  676. if (p_cfg->qos_present) {
  677. cfg_len += L2CAP_CFG_QOS_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  678. }
  679. if (p_cfg->fcr_present) {
  680. cfg_len += L2CAP_CFG_FCR_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  681. }
  682. if (p_cfg->ext_flow_spec_present) {
  683. cfg_len += L2CAP_CFG_EXT_FLOW_OPTION_LEN + L2CAP_CFG_OPTION_OVERHEAD;
  684. }
  685. if ((p_buf = l2cu_build_header (p_ccb->p_lcb, (UINT16)(L2CAP_CONFIG_RSP_LEN + cfg_len),
  686. L2CAP_CMD_CONFIG_RSP, p_ccb->remote_id)) == NULL ) {
  687. L2CAP_TRACE_WARNING ("L2CAP - no buffer for conn_req");
  688. return;
  689. }
  690. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  691. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  692. UINT16_TO_STREAM (p, p_cfg->flags); /* Flags (continuation) Must match request */
  693. UINT16_TO_STREAM (p, p_cfg->result);
  694. /* Now, put the options */
  695. if (p_cfg->mtu_present) {
  696. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_MTU);
  697. UINT8_TO_STREAM (p, L2CAP_CFG_MTU_OPTION_LEN);
  698. UINT16_TO_STREAM (p, p_cfg->mtu);
  699. }
  700. if (p_cfg->flush_to_present) {
  701. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_FLUSH_TOUT);
  702. UINT8_TO_STREAM (p, L2CAP_CFG_FLUSH_OPTION_LEN);
  703. UINT16_TO_STREAM (p, p_cfg->flush_to);
  704. }
  705. if (p_cfg->qos_present) {
  706. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_QOS);
  707. UINT8_TO_STREAM (p, L2CAP_CFG_QOS_OPTION_LEN);
  708. UINT8_TO_STREAM (p, p_cfg->qos.qos_flags);
  709. UINT8_TO_STREAM (p, p_cfg->qos.service_type);
  710. UINT32_TO_STREAM (p, p_cfg->qos.token_rate);
  711. UINT32_TO_STREAM (p, p_cfg->qos.token_bucket_size);
  712. UINT32_TO_STREAM (p, p_cfg->qos.peak_bandwidth);
  713. UINT32_TO_STREAM (p, p_cfg->qos.latency);
  714. UINT32_TO_STREAM (p, p_cfg->qos.delay_variation);
  715. }
  716. if (p_cfg->fcr_present) {
  717. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_FCR);
  718. UINT8_TO_STREAM (p, L2CAP_CFG_FCR_OPTION_LEN);
  719. UINT8_TO_STREAM (p, p_cfg->fcr.mode);
  720. UINT8_TO_STREAM (p, p_cfg->fcr.tx_win_sz);
  721. UINT8_TO_STREAM (p, p_cfg->fcr.max_transmit);
  722. UINT16_TO_STREAM (p, p_ccb->our_cfg.fcr.rtrans_tout);
  723. UINT16_TO_STREAM (p, p_ccb->our_cfg.fcr.mon_tout);
  724. UINT16_TO_STREAM (p, p_cfg->fcr.mps);
  725. }
  726. if (p_cfg->ext_flow_spec_present) {
  727. UINT8_TO_STREAM (p, L2CAP_CFG_TYPE_EXT_FLOW);
  728. UINT8_TO_STREAM (p, L2CAP_CFG_EXT_FLOW_OPTION_LEN);
  729. UINT8_TO_STREAM (p, p_cfg->ext_flow_spec.id);
  730. UINT8_TO_STREAM (p, p_cfg->ext_flow_spec.stype);
  731. UINT16_TO_STREAM (p, p_cfg->ext_flow_spec.max_sdu_size);
  732. UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.sdu_inter_time);
  733. UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.access_latency);
  734. UINT32_TO_STREAM (p, p_cfg->ext_flow_spec.flush_timeout);
  735. }
  736. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  737. }
  738. /*******************************************************************************
  739. **
  740. ** Function l2cu_send_peer_config_rej
  741. **
  742. ** Description Build and send an L2CAP "configuration reject" message
  743. ** to the peer.
  744. **
  745. ** Returns void
  746. **
  747. *******************************************************************************/
  748. void l2cu_send_peer_config_rej (tL2C_CCB *p_ccb, UINT8 *p_data, UINT16 data_len, UINT16 rej_len)
  749. {
  750. BT_HDR *p_buf;
  751. UINT16 len, cfg_len, buf_space, len1;
  752. UINT8 *p, *p_hci_len, *p_data_end;
  753. UINT8 cfg_code;
  754. L2CAP_TRACE_DEBUG("l2cu_send_peer_config_rej: data_len=%d, rej_len=%d", data_len, rej_len);
  755. len = BT_HDR_SIZE + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD + L2CAP_CONFIG_RSP_LEN;
  756. len1 = 0xFFFF - len;
  757. if (rej_len > len1) {
  758. L2CAP_TRACE_ERROR ("L2CAP - cfg_rej pkt size exceeds buffer design max limit.");
  759. return;
  760. }
  761. p_buf = (BT_HDR *)osi_malloc (len + rej_len);
  762. if (!p_buf) {
  763. L2CAP_TRACE_ERROR ("L2CAP - no buffer for cfg_rej");
  764. return;
  765. }
  766. p_buf->offset = L2CAP_SEND_CMD_OFFSET;
  767. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET;
  768. /* Put in HCI header - handle + pkt boundary */
  769. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  770. if (HCI_NON_FLUSHABLE_PB_SUPPORTED(BTM_ReadLocalFeatures ())) {
  771. UINT16_TO_STREAM (p, (p_ccb->p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT)));
  772. } else
  773. #endif
  774. {
  775. UINT16_TO_STREAM (p, (p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT)));
  776. }
  777. /* Remember the HCI header length position, and save space for it */
  778. p_hci_len = p;
  779. p += 2;
  780. /* Put in L2CAP packet header */
  781. UINT16_TO_STREAM (p, L2CAP_CMD_OVERHEAD + L2CAP_CONFIG_RSP_LEN + rej_len);
  782. UINT16_TO_STREAM (p, L2CAP_SIGNALLING_CID);
  783. /* Put in L2CAP command header */
  784. UINT8_TO_STREAM (p, L2CAP_CMD_CONFIG_RSP);
  785. UINT8_TO_STREAM (p, p_ccb->remote_id);
  786. UINT16_TO_STREAM (p, L2CAP_CONFIG_RSP_LEN + rej_len);
  787. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  788. UINT16_TO_STREAM (p, 0); /* Flags = 0 (no continuation) */
  789. UINT16_TO_STREAM (p, L2CAP_CFG_UNKNOWN_OPTIONS);
  790. buf_space = rej_len;
  791. /* Now, put the rejected options */
  792. p_data_end = p_data + data_len;
  793. while (p_data < p_data_end) {
  794. cfg_code = *p_data;
  795. cfg_len = *(p_data + 1);
  796. switch (cfg_code & 0x7F) {
  797. /* skip known options */
  798. case L2CAP_CFG_TYPE_MTU:
  799. case L2CAP_CFG_TYPE_FLUSH_TOUT:
  800. case L2CAP_CFG_TYPE_QOS:
  801. p_data += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
  802. break;
  803. /* unknown options; copy into rsp if not hints */
  804. default:
  805. /* sanity check option length */
  806. if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= data_len) {
  807. if ((cfg_code & 0x80) == 0) {
  808. if (buf_space >= (cfg_len + L2CAP_CFG_OPTION_OVERHEAD)) {
  809. memcpy(p, p_data, cfg_len + L2CAP_CFG_OPTION_OVERHEAD);
  810. p += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
  811. buf_space -= (cfg_len + L2CAP_CFG_OPTION_OVERHEAD);
  812. } else {
  813. L2CAP_TRACE_WARNING("L2CAP - cfg_rej exceeds allocated buffer");
  814. p_data = p_data_end; /* force loop exit */
  815. break;
  816. }
  817. }
  818. p_data += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
  819. }
  820. /* bad length; force loop exit */
  821. else {
  822. p_data = p_data_end;
  823. }
  824. break;
  825. }
  826. }
  827. len = (UINT16) (p - p_hci_len - 2);
  828. UINT16_TO_STREAM (p_hci_len, len);
  829. p_buf->len = len + 4;
  830. L2CAP_TRACE_DEBUG ("L2CAP - cfg_rej pkt hci_len=%d, l2cap_len=%d",
  831. len, (L2CAP_CMD_OVERHEAD + L2CAP_CONFIG_RSP_LEN + rej_len));
  832. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  833. }
  834. /*******************************************************************************
  835. **
  836. ** Function l2cu_send_peer_disc_req
  837. **
  838. ** Description Build and send an L2CAP "disconnect request" message
  839. ** to the peer.
  840. **
  841. ** Returns void
  842. **
  843. *******************************************************************************/
  844. void l2cu_send_peer_disc_req (tL2C_CCB *p_ccb)
  845. {
  846. BT_HDR *p_buf, *p_buf2;
  847. UINT8 *p;
  848. /* Create an identifier for this packet */
  849. p_ccb->p_lcb->id++;
  850. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  851. p_ccb->local_id = p_ccb->p_lcb->id;
  852. if ((p_buf = l2cu_build_header(p_ccb->p_lcb, L2CAP_DISC_REQ_LEN, L2CAP_CMD_DISC_REQ, p_ccb->local_id)) == NULL) {
  853. L2CAP_TRACE_WARNING ("L2CAP - no buffer for disc_req");
  854. return;
  855. }
  856. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  857. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  858. UINT16_TO_STREAM (p, p_ccb->local_cid);
  859. /* Move all queued data packets to the LCB. In FCR mode, assume the higher
  860. layer checks that all buffers are sent before disconnecting.
  861. */
  862. if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE) {
  863. while ((p_buf2 = (BT_HDR *)fixed_queue_dequeue(p_ccb->xmit_hold_q, 0)) != NULL) {
  864. l2cu_set_acl_hci_header (p_buf2, p_ccb);
  865. l2c_link_check_send_pkts (p_ccb->p_lcb, p_ccb, p_buf2);
  866. }
  867. }
  868. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  869. }
  870. /*******************************************************************************
  871. **
  872. ** Function l2cu_send_peer_disc_rsp
  873. **
  874. ** Description Build and send an L2CAP "disconnect response" message
  875. ** to the peer.
  876. **
  877. ** This function is passed the parameters for the disconnect
  878. ** response instead of the CCB address, as it may be called
  879. ** to send a disconnect response when there is no CCB.
  880. **
  881. ** Returns void
  882. **
  883. *******************************************************************************/
  884. void l2cu_send_peer_disc_rsp (tL2C_LCB *p_lcb, UINT8 remote_id, UINT16 local_cid,
  885. UINT16 remote_cid)
  886. {
  887. BT_HDR *p_buf;
  888. UINT8 *p;
  889. if (!p_lcb) {
  890. L2CAP_TRACE_WARNING("lcb already released\n");
  891. return;
  892. }
  893. if ((p_buf = l2cu_build_header(p_lcb, L2CAP_DISC_RSP_LEN, L2CAP_CMD_DISC_RSP, remote_id)) == NULL) {
  894. L2CAP_TRACE_WARNING ("L2CAP - no buffer for disc_rsp");
  895. return;
  896. }
  897. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  898. UINT16_TO_STREAM (p, local_cid);
  899. UINT16_TO_STREAM (p, remote_cid);
  900. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  901. }
  902. /*******************************************************************************
  903. **
  904. ** Function l2cu_send_peer_echo_req
  905. **
  906. ** Description Build and send an L2CAP "echo request" message
  907. ** to the peer. Note that we do not currently allow
  908. ** data in the echo request.
  909. **
  910. ** Returns void
  911. **
  912. *******************************************************************************/
  913. void l2cu_send_peer_echo_req (tL2C_LCB *p_lcb, UINT8 *p_data, UINT16 data_len)
  914. {
  915. BT_HDR *p_buf;
  916. UINT8 *p;
  917. p_lcb->id++;
  918. l2cu_adj_id(p_lcb, L2CAP_ADJ_ZERO_ID); /* check for wrap to '0' */
  919. if ((p_buf = l2cu_build_header(p_lcb, (UINT16) (L2CAP_ECHO_REQ_LEN + data_len), L2CAP_CMD_ECHO_REQ, p_lcb->id)) == NULL) {
  920. L2CAP_TRACE_WARNING ("L2CAP - no buffer for echo_req");
  921. return;
  922. }
  923. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  924. if (data_len) {
  925. ARRAY_TO_STREAM (p, p_data, data_len);
  926. }
  927. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  928. }
  929. /*******************************************************************************
  930. **
  931. ** Function l2cu_send_peer_echo_rsp
  932. **
  933. ** Description Build and send an L2CAP "echo response" message
  934. ** to the peer.
  935. **
  936. ** Returns void
  937. **
  938. *******************************************************************************/
  939. void l2cu_send_peer_echo_rsp (tL2C_LCB *p_lcb, UINT8 id, UINT8 *p_data, UINT16 data_len)
  940. {
  941. BT_HDR *p_buf;
  942. UINT8 *p;
  943. UINT16 maxlen;
  944. /* Filter out duplicate IDs or if available buffers are low (intruder checking) */
  945. if (!id || id == p_lcb->cur_echo_id) {
  946. /* Dump this request since it is illegal */
  947. L2CAP_TRACE_WARNING ("L2CAP ignoring duplicate echo request (%d)", id);
  948. return;
  949. } else {
  950. p_lcb->cur_echo_id = id;
  951. }
  952. uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
  953. uint16_t acl_packet_size = controller_get_interface()->get_acl_packet_size_classic();
  954. /* Don't return data if it does not fit in ACL and L2CAP MTU */
  955. maxlen = (L2CAP_CMD_BUF_SIZE > acl_packet_size) ?
  956. acl_data_size : (UINT16)L2CAP_CMD_BUF_SIZE;
  957. maxlen -= (UINT16)(BT_HDR_SIZE + HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD +
  958. L2CAP_CMD_OVERHEAD + L2CAP_ECHO_RSP_LEN);
  959. if (data_len > maxlen) {
  960. data_len = 0;
  961. }
  962. if ((p_buf = l2cu_build_header (p_lcb, (UINT16)(L2CAP_ECHO_RSP_LEN + data_len), L2CAP_CMD_ECHO_RSP, id)) == NULL) {
  963. L2CAP_TRACE_WARNING ("L2CAP - no buffer for echo_rsp");
  964. return;
  965. }
  966. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  967. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  968. if (data_len) {
  969. ARRAY_TO_STREAM (p, p_data, data_len);
  970. }
  971. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  972. }
  973. /*******************************************************************************
  974. **
  975. ** Function l2cu_send_peer_info_req
  976. **
  977. ** Description Build and send an L2CAP "info request" message
  978. ** to the peer.
  979. ** Returns void
  980. **
  981. *******************************************************************************/
  982. void l2cu_send_peer_info_req (tL2C_LCB *p_lcb, UINT16 info_type)
  983. {
  984. BT_HDR *p_buf;
  985. UINT8 *p;
  986. /* check for wrap and/or BRCM ID */
  987. p_lcb->id++;
  988. l2cu_adj_id(p_lcb, L2CAP_ADJ_ID);
  989. if ((p_buf = l2cu_build_header(p_lcb, 2, L2CAP_CMD_INFO_REQ, p_lcb->id)) == NULL) {
  990. L2CAP_TRACE_WARNING ("L2CAP - no buffer for info_req");
  991. return;
  992. }
  993. L2CAP_TRACE_EVENT ("l2cu_send_peer_info_req: type 0x%04x", info_type);
  994. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  995. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  996. UINT16_TO_STREAM (p, info_type);
  997. p_lcb->w4_info_rsp = TRUE;
  998. btu_start_timer (&p_lcb->info_timer_entry, BTU_TTYPE_L2CAP_INFO, L2CAP_WAIT_INFO_RSP_TOUT);
  999. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  1000. }
  1001. /*******************************************************************************
  1002. **
  1003. ** Function l2cu_send_peer_info_rsp
  1004. **
  1005. ** Description Build and send an L2CAP "info response" message
  1006. ** to the peer.
  1007. **
  1008. ** Returns void
  1009. **
  1010. *******************************************************************************/
  1011. void l2cu_send_peer_info_rsp (tL2C_LCB *p_lcb, UINT8 remote_id, UINT16 info_type)
  1012. {
  1013. BT_HDR *p_buf;
  1014. UINT8 *p;
  1015. UINT16 len = L2CAP_INFO_RSP_LEN;
  1016. #if (L2CAP_CONFORMANCE_TESTING == TRUE)
  1017. if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
  1018. && (l2cb.test_info_resp & (L2CAP_EXTFEA_ENH_RETRANS | L2CAP_EXTFEA_STREAM_MODE |
  1019. L2CAP_EXTFEA_NO_CRC | L2CAP_EXTFEA_EXT_FLOW_SPEC |
  1020. L2CAP_EXTFEA_FIXED_CHNLS | L2CAP_EXTFEA_EXT_WINDOW |
  1021. L2CAP_EXTFEA_UCD_RECEPTION )) )
  1022. #else
  1023. if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
  1024. && (L2CAP_EXTFEA_SUPPORTED_MASK & (L2CAP_EXTFEA_ENH_RETRANS | L2CAP_EXTFEA_STREAM_MODE |
  1025. L2CAP_EXTFEA_NO_CRC | L2CAP_EXTFEA_FIXED_CHNLS |
  1026. L2CAP_EXTFEA_UCD_RECEPTION )) )
  1027. #endif
  1028. {
  1029. len += L2CAP_EXTENDED_FEATURES_ARRAY_SIZE;
  1030. } else if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE) {
  1031. len += L2CAP_FIXED_CHNL_ARRAY_SIZE;
  1032. } else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE) {
  1033. len += L2CAP_CONNLESS_MTU_INFO_SIZE;
  1034. }
  1035. if ((p_buf = l2cu_build_header(p_lcb, len, L2CAP_CMD_INFO_RSP, remote_id)) == NULL) {
  1036. L2CAP_TRACE_WARNING ("L2CAP - no buffer for info_rsp");
  1037. return;
  1038. }
  1039. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  1040. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  1041. UINT16_TO_STREAM (p, info_type);
  1042. #if (L2CAP_CONFORMANCE_TESTING == TRUE)
  1043. if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
  1044. && (l2cb.test_info_resp & ( L2CAP_EXTFEA_ENH_RETRANS | L2CAP_EXTFEA_STREAM_MODE
  1045. | L2CAP_EXTFEA_UCD_RECEPTION )) )
  1046. #else
  1047. if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
  1048. && (L2CAP_EXTFEA_SUPPORTED_MASK & ( L2CAP_EXTFEA_ENH_RETRANS | L2CAP_EXTFEA_STREAM_MODE
  1049. | L2CAP_EXTFEA_UCD_RECEPTION )) )
  1050. #endif
  1051. {
  1052. UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_SUCCESS);
  1053. #if (BLE_INCLUDED == TRUE)
  1054. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1055. /* optional data are not added for now */
  1056. UINT32_TO_STREAM (p, L2CAP_BLE_EXTFEA_MASK);
  1057. } else
  1058. #endif
  1059. {
  1060. #if L2CAP_CONFORMANCE_TESTING == TRUE
  1061. UINT32_TO_STREAM (p, l2cb.test_info_resp);
  1062. #else
  1063. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  1064. UINT32_TO_STREAM (p, L2CAP_EXTFEA_SUPPORTED_MASK | L2CAP_EXTFEA_FIXED_CHNLS);
  1065. #else
  1066. UINT32_TO_STREAM (p, L2CAP_EXTFEA_SUPPORTED_MASK);
  1067. #endif
  1068. #endif
  1069. }
  1070. } else if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE) {
  1071. UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_SUCCESS);
  1072. memset (p, 0, L2CAP_FIXED_CHNL_ARRAY_SIZE);
  1073. p[0] = L2CAP_FIXED_CHNL_SIG_BIT;
  1074. if ( L2CAP_EXTFEA_SUPPORTED_MASK & L2CAP_EXTFEA_UCD_RECEPTION ) {
  1075. p[0] |= L2CAP_FIXED_CHNL_CNCTLESS_BIT;
  1076. }
  1077. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  1078. {
  1079. int xx;
  1080. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++)
  1081. if (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL) {
  1082. p[(xx + L2CAP_FIRST_FIXED_CHNL) / 8] |= 1 << ((xx + L2CAP_FIRST_FIXED_CHNL) % 8);
  1083. }
  1084. }
  1085. #endif
  1086. } else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE) {
  1087. UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_SUCCESS);
  1088. UINT16_TO_STREAM (p, L2CAP_UCD_MTU);
  1089. } else {
  1090. UINT16_TO_STREAM (p, L2CAP_INFO_RESP_RESULT_NOT_SUPPORTED); /* 'not supported' */
  1091. }
  1092. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  1093. }
  1094. /******************************************************************************
  1095. **
  1096. ** Function l2cu_enqueue_ccb
  1097. **
  1098. ** Description queue CCB by priority. The first CCB is highest priority and
  1099. ** is served at first. The CCB is queued to an LLCB or an LCB.
  1100. **
  1101. ** Returns None
  1102. **
  1103. *******************************************************************************/
  1104. void l2cu_enqueue_ccb (tL2C_CCB *p_ccb)
  1105. {
  1106. tL2C_CCB *p_ccb1;
  1107. tL2C_CCB_Q *p_q = NULL;
  1108. /* Find out which queue the channel is on
  1109. */
  1110. if (p_ccb->p_lcb != NULL) {
  1111. p_q = &p_ccb->p_lcb->ccb_queue;
  1112. }
  1113. if ( (!p_ccb->in_use) || (p_q == NULL) ) {
  1114. L2CAP_TRACE_ERROR ("l2cu_enqueue_ccb CID: 0x%04x ERROR in_use: %u p_lcb: %p",
  1115. p_ccb->local_cid, p_ccb->in_use, p_ccb->p_lcb);
  1116. return;
  1117. }
  1118. L2CAP_TRACE_DEBUG ("l2cu_enqueue_ccb CID: 0x%04x priority: %d",
  1119. p_ccb->local_cid, p_ccb->ccb_priority);
  1120. /* If the queue is empty, we go at the front */
  1121. if (!p_q->p_first_ccb) {
  1122. p_q->p_first_ccb = p_q->p_last_ccb = p_ccb;
  1123. p_ccb->p_next_ccb = p_ccb->p_prev_ccb = NULL;
  1124. } else {
  1125. p_ccb1 = p_q->p_first_ccb;
  1126. while (p_ccb1 != NULL) {
  1127. /* Insert new ccb at the end of the same priority. Lower number, higher priority */
  1128. if (p_ccb->ccb_priority < p_ccb1->ccb_priority) {
  1129. /* Are we at the head of the queue ? */
  1130. if (p_ccb1 == p_q->p_first_ccb) {
  1131. p_q->p_first_ccb = p_ccb;
  1132. } else {
  1133. p_ccb1->p_prev_ccb->p_next_ccb = p_ccb;
  1134. }
  1135. p_ccb->p_next_ccb = p_ccb1;
  1136. p_ccb->p_prev_ccb = p_ccb1->p_prev_ccb;
  1137. p_ccb1->p_prev_ccb = p_ccb;
  1138. break;
  1139. }
  1140. p_ccb1 = p_ccb1->p_next_ccb;
  1141. }
  1142. /* If we are lower then anyone in the list, we go at the end */
  1143. if (!p_ccb1) {
  1144. /* add new ccb at the end of the list */
  1145. p_q->p_last_ccb->p_next_ccb = p_ccb;
  1146. p_ccb->p_next_ccb = NULL;
  1147. p_ccb->p_prev_ccb = p_q->p_last_ccb;
  1148. p_q->p_last_ccb = p_ccb;
  1149. }
  1150. }
  1151. #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
  1152. /* Adding CCB into round robin service table of its LCB */
  1153. if (p_ccb->p_lcb != NULL) {
  1154. /* if this is the first channel in this priority group */
  1155. if (p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb == 0 ) {
  1156. /* Set the first channel to this CCB */
  1157. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = p_ccb;
  1158. /* Set the next serving channel in this group to this CCB */
  1159. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = p_ccb;
  1160. /* Initialize quota of this priority group based on its priority */
  1161. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].quota = L2CAP_GET_PRIORITY_QUOTA(p_ccb->ccb_priority);
  1162. }
  1163. /* increase number of channels in this group */
  1164. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb++;
  1165. }
  1166. #endif
  1167. }
  1168. /******************************************************************************
  1169. **
  1170. ** Function l2cu_dequeue_ccb
  1171. **
  1172. ** Description dequeue CCB from a queue
  1173. **
  1174. ** Returns -
  1175. **
  1176. *******************************************************************************/
  1177. void l2cu_dequeue_ccb (tL2C_CCB *p_ccb)
  1178. {
  1179. tL2C_CCB_Q *p_q = NULL;
  1180. L2CAP_TRACE_DEBUG ("l2cu_dequeue_ccb CID: 0x%04x", p_ccb->local_cid);
  1181. /* Find out which queue the channel is on
  1182. */
  1183. if (p_ccb->p_lcb != NULL) {
  1184. p_q = &p_ccb->p_lcb->ccb_queue;
  1185. }
  1186. if ( (!p_ccb->in_use) || (p_q == NULL) || (p_q->p_first_ccb == NULL) ) {
  1187. L2CAP_TRACE_ERROR ("l2cu_dequeue_ccb CID: 0x%04x ERROR in_use: %u p_lcb: %p p_q: %p p_q->p_first_ccb: %p",
  1188. p_ccb->local_cid, p_ccb->in_use, p_ccb->p_lcb, p_q, p_q ? p_q->p_first_ccb : 0);
  1189. return;
  1190. }
  1191. #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
  1192. /* Removing CCB from round robin service table of its LCB */
  1193. if (p_ccb->p_lcb != NULL) {
  1194. /* decrease number of channels in this priority group */
  1195. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb--;
  1196. /* if it was the last channel in the priority group */
  1197. if (p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb == 0 ) {
  1198. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = NULL;
  1199. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = NULL;
  1200. } else {
  1201. /* if it is the first channel of this group */
  1202. if ( p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb == p_ccb ) {
  1203. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = p_ccb->p_next_ccb;
  1204. }
  1205. /* if it is the next serving channel of this group */
  1206. if ( p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb == p_ccb ) {
  1207. /* simply, start serving from the first channel */
  1208. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb
  1209. = p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb;
  1210. }
  1211. }
  1212. }
  1213. #endif
  1214. if (p_ccb == p_q->p_first_ccb) {
  1215. /* We are removing the first in a queue */
  1216. p_q->p_first_ccb = p_ccb->p_next_ccb;
  1217. if (p_q->p_first_ccb) {
  1218. p_q->p_first_ccb->p_prev_ccb = NULL;
  1219. } else {
  1220. p_q->p_last_ccb = NULL;
  1221. }
  1222. } else if (p_ccb == p_q->p_last_ccb) {
  1223. /* We are removing the last in a queue */
  1224. p_q->p_last_ccb = p_ccb->p_prev_ccb;
  1225. p_q->p_last_ccb->p_next_ccb = NULL;
  1226. } else {
  1227. /* In the middle of a chain. */
  1228. p_ccb->p_prev_ccb->p_next_ccb = p_ccb->p_next_ccb;
  1229. p_ccb->p_next_ccb->p_prev_ccb = p_ccb->p_prev_ccb;
  1230. }
  1231. p_ccb->p_next_ccb = p_ccb->p_prev_ccb = NULL;
  1232. }
  1233. /******************************************************************************
  1234. **
  1235. ** Function l2cu_change_pri_ccb
  1236. **
  1237. ** Description
  1238. **
  1239. ** Returns -
  1240. **
  1241. *******************************************************************************/
  1242. void l2cu_change_pri_ccb (tL2C_CCB *p_ccb, tL2CAP_CHNL_PRIORITY priority)
  1243. {
  1244. if (p_ccb->ccb_priority != priority) {
  1245. /* If CCB is not the only guy on the queue */
  1246. if ( (p_ccb->p_next_ccb != NULL) || (p_ccb->p_prev_ccb != NULL) ) {
  1247. L2CAP_TRACE_DEBUG ("Update CCB list in logical link");
  1248. /* Remove CCB from queue and re-queue it at new priority */
  1249. l2cu_dequeue_ccb (p_ccb);
  1250. p_ccb->ccb_priority = priority;
  1251. l2cu_enqueue_ccb (p_ccb);
  1252. }
  1253. #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
  1254. else {
  1255. /* If CCB is the only guy on the queue, no need to re-enqueue */
  1256. /* update only round robin service data */
  1257. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb = 0;
  1258. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = NULL;
  1259. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = NULL;
  1260. p_ccb->ccb_priority = priority;
  1261. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_first_ccb = p_ccb;
  1262. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].p_serve_ccb = p_ccb;
  1263. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].quota = L2CAP_GET_PRIORITY_QUOTA(p_ccb->ccb_priority);
  1264. p_ccb->p_lcb->rr_serv[p_ccb->ccb_priority].num_ccb = 1;
  1265. }
  1266. #endif
  1267. }
  1268. }
  1269. /*******************************************************************************
  1270. **
  1271. ** Function l2cu_allocate_ccb
  1272. **
  1273. ** Description This function allocates a Channel Control Block and
  1274. ** attaches it to a link control block. The local CID
  1275. ** is also assigned.
  1276. **
  1277. ** Returns pointer to CCB, or NULL if none
  1278. **
  1279. *******************************************************************************/
  1280. bool l2cu_find_ccb_in_list(void *p_ccb_node, void *p_local_cid);
  1281. tL2C_CCB *l2cu_allocate_ccb (tL2C_LCB *p_lcb, UINT16 cid)
  1282. {
  1283. tL2C_CCB *p_ccb = NULL;
  1284. uint16_t tmp_cid = L2CAP_BASE_APPL_CID;
  1285. L2CAP_TRACE_DEBUG ("l2cu_allocate_ccb: cid 0x%04x", cid);
  1286. p_ccb = l2cu_find_free_ccb ();
  1287. if(p_ccb == NULL) {
  1288. if (list_length(l2cb.p_ccb_pool) < MAX_L2CAP_CHANNELS) {
  1289. p_ccb = (tL2C_CCB *)osi_malloc(sizeof(tL2C_CCB));
  1290. if (p_ccb) {
  1291. memset (p_ccb, 0, sizeof(tL2C_CCB));
  1292. list_append(l2cb.p_ccb_pool, p_ccb);
  1293. }
  1294. }
  1295. }
  1296. if (p_ccb == NULL) {
  1297. return (NULL);
  1298. }
  1299. p_ccb->p_next_ccb = p_ccb->p_prev_ccb = NULL;
  1300. p_ccb->in_use = TRUE;
  1301. /* Get a CID for the connection */
  1302. for (tmp_cid = L2CAP_BASE_APPL_CID; tmp_cid < MAX_L2CAP_CHANNELS + L2CAP_BASE_APPL_CID; tmp_cid++) {
  1303. if (list_foreach(l2cb.p_ccb_pool, l2cu_find_ccb_in_list, &tmp_cid) == NULL) {
  1304. break;
  1305. }
  1306. }
  1307. assert(tmp_cid != MAX_L2CAP_CHANNELS + L2CAP_BASE_APPL_CID);
  1308. p_ccb->local_cid = tmp_cid;
  1309. p_ccb->p_lcb = p_lcb;
  1310. p_ccb->p_rcb = NULL;
  1311. p_ccb->should_free_rcb = false;
  1312. /* Set priority then insert ccb into LCB queue (if we have an LCB) */
  1313. p_ccb->ccb_priority = L2CAP_CHNL_PRIORITY_LOW;
  1314. if (p_lcb) {
  1315. l2cu_enqueue_ccb (p_ccb);
  1316. }
  1317. /* clear what peer wants to configure */
  1318. p_ccb->peer_cfg_bits = 0;
  1319. /* Put in default values for configuration */
  1320. memset (&p_ccb->our_cfg, 0, sizeof(tL2CAP_CFG_INFO));
  1321. memset (&p_ccb->peer_cfg, 0, sizeof(tL2CAP_CFG_INFO));
  1322. /* Put in default values for local/peer configurations */
  1323. p_ccb->our_cfg.flush_to = p_ccb->peer_cfg.flush_to = L2CAP_DEFAULT_FLUSH_TO;
  1324. p_ccb->our_cfg.mtu = p_ccb->peer_cfg.mtu = L2CAP_DEFAULT_MTU;
  1325. p_ccb->our_cfg.qos.service_type = p_ccb->peer_cfg.qos.service_type = L2CAP_DEFAULT_SERV_TYPE;
  1326. p_ccb->our_cfg.qos.token_rate = p_ccb->peer_cfg.qos.token_rate = L2CAP_DEFAULT_TOKEN_RATE;
  1327. p_ccb->our_cfg.qos.token_bucket_size = p_ccb->peer_cfg.qos.token_bucket_size = L2CAP_DEFAULT_BUCKET_SIZE;
  1328. p_ccb->our_cfg.qos.peak_bandwidth = p_ccb->peer_cfg.qos.peak_bandwidth = L2CAP_DEFAULT_PEAK_BANDWIDTH;
  1329. p_ccb->our_cfg.qos.latency = p_ccb->peer_cfg.qos.latency = L2CAP_DEFAULT_LATENCY;
  1330. p_ccb->our_cfg.qos.delay_variation = p_ccb->peer_cfg.qos.delay_variation = L2CAP_DEFAULT_DELAY;
  1331. p_ccb->bypass_fcs = 0;
  1332. memset (&p_ccb->ertm_info, 0, sizeof(tL2CAP_ERTM_INFO));
  1333. p_ccb->peer_cfg_already_rejected = FALSE;
  1334. p_ccb->fcr_cfg_tries = L2CAP_MAX_FCR_CFG_TRIES;
  1335. /* stop and release timers */
  1336. btu_free_quick_timer(&p_ccb->fcrb.ack_timer);
  1337. memset(&p_ccb->fcrb.ack_timer, 0, sizeof(TIMER_LIST_ENT));
  1338. p_ccb->fcrb.ack_timer.param = (TIMER_PARAM_TYPE)p_ccb;
  1339. btu_free_quick_timer(&p_ccb->fcrb.mon_retrans_timer);
  1340. memset(&p_ccb->fcrb.mon_retrans_timer, 0, sizeof(TIMER_LIST_ENT));
  1341. p_ccb->fcrb.mon_retrans_timer.param = (TIMER_PARAM_TYPE)p_ccb;
  1342. // btla-specific ++
  1343. /* CSP408639 Fix: When L2CAP send amp move channel request or receive
  1344. * L2CEVT_AMP_MOVE_REQ do following sequence. Send channel move
  1345. * request -> Stop retrans/monitor timer -> Change channel state to CST_AMP_MOVING. */
  1346. // btla-specific --
  1347. #if (CLASSIC_BT_INCLUDED == TRUE)
  1348. l2c_fcr_free_timer (p_ccb);
  1349. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1350. p_ccb->ertm_info.preferred_mode = L2CAP_FCR_BASIC_MODE; /* Default mode for channel is basic mode */
  1351. p_ccb->ertm_info.allowed_modes = L2CAP_FCR_CHAN_OPT_BASIC|L2CAP_FCR_CHAN_OPT_ERTM;
  1352. p_ccb->ertm_info.fcr_rx_buf_size = L2CAP_FCR_RX_BUF_SIZE;
  1353. p_ccb->ertm_info.fcr_tx_buf_size = L2CAP_FCR_TX_BUF_SIZE;
  1354. p_ccb->ertm_info.user_rx_buf_size = L2CAP_USER_RX_BUF_SIZE;
  1355. p_ccb->ertm_info.user_tx_buf_size = L2CAP_USER_TX_BUF_SIZE;
  1356. p_ccb->max_rx_mtu = L2CAP_MTU_SIZE;
  1357. p_ccb->tx_mps = L2CAP_FCR_TX_BUF_SIZE - 32;
  1358. p_ccb->xmit_hold_q = fixed_queue_new(QUEUE_SIZE_MAX);
  1359. #if (CLASSIC_BT_INCLUDED == TRUE)
  1360. p_ccb->fcrb.srej_rcv_hold_q = fixed_queue_new(QUEUE_SIZE_MAX);
  1361. p_ccb->fcrb.retrans_q = fixed_queue_new(QUEUE_SIZE_MAX);
  1362. p_ccb->fcrb.waiting_for_ack_q = fixed_queue_new(QUEUE_SIZE_MAX);
  1363. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1364. p_ccb->cong_sent = FALSE;
  1365. p_ccb->buff_quota = 2; /* This gets set after config */
  1366. /* If CCB was reserved Config_Done can already have some value */
  1367. if (cid == 0) {
  1368. p_ccb->config_done = 0;
  1369. } else {
  1370. L2CAP_TRACE_DEBUG ("l2cu_allocate_ccb: cid 0x%04x config_done:0x%x", cid, p_ccb->config_done);
  1371. }
  1372. p_ccb->chnl_state = CST_CLOSED;
  1373. p_ccb->flags = 0;
  1374. p_ccb->tx_data_rate = L2CAP_CHNL_DATA_RATE_LOW;
  1375. p_ccb->rx_data_rate = L2CAP_CHNL_DATA_RATE_LOW;
  1376. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  1377. p_ccb->is_flushable = FALSE;
  1378. #endif
  1379. btu_free_timer(&p_ccb->timer_entry);
  1380. memset(&p_ccb->timer_entry, 0, sizeof(TIMER_LIST_ENT));
  1381. p_ccb->timer_entry.param = (TIMER_PARAM_TYPE)p_ccb;
  1382. p_ccb->timer_entry.in_use = 0;
  1383. l2c_link_adjust_chnl_allocation ();
  1384. return (p_ccb);
  1385. }
  1386. /*******************************************************************************
  1387. **
  1388. ** Function l2cu_start_post_bond_timer
  1389. **
  1390. ** Description This function starts the ACL Link inactivity timer after
  1391. ** dedicated bonding
  1392. ** This timer can be longer than the normal link inactivity
  1393. ** timer for some platforms.
  1394. **
  1395. ** Returns BOOLEAN - TRUE if idle timer started or disconnect initiated
  1396. ** FALSE if there's one or more pending CCB's exist
  1397. **
  1398. *******************************************************************************/
  1399. BOOLEAN l2cu_start_post_bond_timer (UINT16 handle)
  1400. {
  1401. UINT16 timeout;
  1402. tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
  1403. if (!p_lcb) {
  1404. return (TRUE);
  1405. }
  1406. p_lcb->is_bonding = FALSE;
  1407. /* Only start timer if no control blocks allocated */
  1408. if (p_lcb->ccb_queue.p_first_ccb != NULL) {
  1409. return (FALSE);
  1410. }
  1411. /* If no channels on the connection, start idle timeout */
  1412. if ( (p_lcb->link_state == LST_CONNECTED) || (p_lcb->link_state == LST_CONNECTING) || (p_lcb->link_state == LST_DISCONNECTING) ) {
  1413. if (p_lcb->idle_timeout == 0) {
  1414. if (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER)) {
  1415. p_lcb->link_state = LST_DISCONNECTING;
  1416. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  1417. } else {
  1418. timeout = BT_1SEC_TIMEOUT;
  1419. }
  1420. } else {
  1421. timeout = L2CAP_BONDING_TIMEOUT;
  1422. }
  1423. if (timeout != 0xFFFF) {
  1424. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, timeout);
  1425. }
  1426. return (TRUE);
  1427. }
  1428. return (FALSE);
  1429. }
  1430. /*******************************************************************************
  1431. **
  1432. ** Function l2cu_release_ccb
  1433. **
  1434. ** Description This function releases a Channel Control Block. The timer
  1435. ** is stopped, any attached buffers freed, and the CCB is removed
  1436. ** from the link control block.
  1437. **
  1438. ** Returns void
  1439. **
  1440. *******************************************************************************/
  1441. void l2cu_release_ccb (tL2C_CCB *p_ccb)
  1442. {
  1443. tL2C_LCB *p_lcb = p_ccb->p_lcb;
  1444. tL2C_RCB *p_rcb = p_ccb->p_rcb;
  1445. L2CAP_TRACE_DEBUG ("l2cu_release_ccb: cid 0x%04x in_use: %u", p_ccb->local_cid, p_ccb->in_use);
  1446. /* If already released, could be race condition */
  1447. if (!p_ccb->in_use) {
  1448. return;
  1449. }
  1450. #if BLE_INCLUDED == TRUE
  1451. if (p_lcb->transport == BT_TRANSPORT_LE) {
  1452. /* Take samephore to avoid race condition */
  1453. l2ble_update_att_acl_pkt_num(L2CA_BUFF_FREE, NULL);
  1454. }
  1455. #endif
  1456. #if (SDP_INCLUDED == TRUE)
  1457. if (p_rcb && (p_rcb->psm != p_rcb->real_psm)) {
  1458. btm_sec_clr_service_by_psm(p_rcb->psm);
  1459. }
  1460. #endif ///SMP_INCLUDED == TRUE
  1461. if (p_ccb->should_free_rcb) {
  1462. osi_free(p_rcb);
  1463. p_ccb->p_rcb = NULL;
  1464. p_ccb->should_free_rcb = false;
  1465. }
  1466. if (p_lcb) {
  1467. btm_sec_clr_temp_auth_service (p_lcb->remote_bd_addr);
  1468. }
  1469. /* Stop and free the timer */
  1470. btu_free_timer (&p_ccb->timer_entry);
  1471. fixed_queue_free(p_ccb->xmit_hold_q, osi_free_func);
  1472. p_ccb->xmit_hold_q = NULL;
  1473. #if (CLASSIC_BT_INCLUDED == TRUE)
  1474. fixed_queue_free(p_ccb->fcrb.srej_rcv_hold_q, osi_free_func);
  1475. fixed_queue_free(p_ccb->fcrb.retrans_q, osi_free_func);
  1476. fixed_queue_free(p_ccb->fcrb.waiting_for_ack_q, osi_free_func);
  1477. p_ccb->fcrb.srej_rcv_hold_q = NULL;
  1478. p_ccb->fcrb.retrans_q = NULL;
  1479. p_ccb->fcrb.waiting_for_ack_q = NULL;
  1480. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1481. #if (CLASSIC_BT_INCLUDED == TRUE)
  1482. l2c_fcr_cleanup (p_ccb);
  1483. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1484. /* Channel may not be assigned to any LCB if it was just pre-reserved */
  1485. if ( (p_lcb) &&
  1486. ( (p_ccb->local_cid >= L2CAP_BASE_APPL_CID)
  1487. #if (L2CAP_UCD_INCLUDED == TRUE)
  1488. || (p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID)
  1489. #endif
  1490. )
  1491. ) {
  1492. l2cu_dequeue_ccb (p_ccb);
  1493. /* Delink the CCB from the LCB */
  1494. p_ccb->p_lcb = NULL;
  1495. }
  1496. /* Flag as not in use */
  1497. p_ccb->in_use = FALSE;
  1498. /* If no channels on the connection, start idle timeout */
  1499. if ((p_lcb) && p_lcb->in_use && (p_lcb->link_state == LST_CONNECTED)) {
  1500. if (!p_lcb->ccb_queue.p_first_ccb) {
  1501. l2cu_no_dynamic_ccbs (p_lcb);
  1502. } else {
  1503. /* Link is still active, adjust channel quotas. */
  1504. l2c_link_adjust_chnl_allocation ();
  1505. }
  1506. }
  1507. }
  1508. /*******************************************************************************
  1509. **
  1510. ** Function l2cu_find_ccb_by_remote_cid
  1511. **
  1512. ** Description Look through all active CCBs on a link for a match based
  1513. ** on the remote CID.
  1514. **
  1515. ** Returns pointer to matched CCB, or NULL if no match
  1516. **
  1517. *******************************************************************************/
  1518. tL2C_CCB *l2cu_find_ccb_by_remote_cid (tL2C_LCB *p_lcb, UINT16 remote_cid)
  1519. {
  1520. tL2C_CCB *p_ccb;
  1521. /* If LCB is NULL, look through all active links */
  1522. if (!p_lcb) {
  1523. return NULL;
  1524. } else {
  1525. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
  1526. if ((p_ccb->in_use) && (p_ccb->remote_cid == remote_cid)) {
  1527. return (p_ccb);
  1528. }
  1529. }
  1530. /* If here, no match found */
  1531. return (NULL);
  1532. }
  1533. /*******************************************************************************
  1534. **
  1535. ** Function l2cu_allocate_rcb
  1536. **
  1537. ** Description Look through the Registration Control Blocks for a free
  1538. ** one.
  1539. **
  1540. ** Returns Pointer to the RCB or NULL if not found
  1541. **
  1542. *******************************************************************************/
  1543. tL2C_RCB *l2cu_allocate_rcb (UINT16 psm)
  1544. {
  1545. tL2C_RCB *p_rcb = &l2cb.rcb_pool[0];
  1546. UINT16 xx;
  1547. for (xx = 0; xx < MAX_L2CAP_CLIENTS; xx++, p_rcb++) {
  1548. if (!p_rcb->in_use) {
  1549. p_rcb->in_use = TRUE;
  1550. p_rcb->psm = psm;
  1551. #if (L2CAP_UCD_INCLUDED == TRUE)
  1552. p_rcb->ucd.state = L2C_UCD_STATE_UNUSED;
  1553. #endif
  1554. return (p_rcb);
  1555. }
  1556. }
  1557. /* If here, no free RCB found */
  1558. return (NULL);
  1559. }
  1560. #if (BLE_INCLUDED == TRUE)
  1561. /*******************************************************************************
  1562. **
  1563. ** Function l2cu_allocate_ble_rcb
  1564. **
  1565. ** Description Look through the BLE Registration Control Blocks for a free
  1566. ** one.
  1567. **
  1568. ** Returns Pointer to the BLE RCB or NULL if not found
  1569. **
  1570. *******************************************************************************/
  1571. tL2C_RCB *l2cu_allocate_ble_rcb (UINT16 psm)
  1572. {
  1573. tL2C_RCB *p_rcb = &l2cb.ble_rcb_pool[0];
  1574. UINT16 xx;
  1575. for (xx = 0; xx < BLE_MAX_L2CAP_CLIENTS; xx++, p_rcb++)
  1576. {
  1577. if (!p_rcb->in_use)
  1578. {
  1579. p_rcb->in_use = TRUE;
  1580. p_rcb->psm = psm;
  1581. #if (L2CAP_UCD_INCLUDED == TRUE)
  1582. p_rcb->ucd.state = L2C_UCD_STATE_UNUSED;
  1583. #endif
  1584. return (p_rcb);
  1585. }
  1586. }
  1587. /* If here, no free RCB found */
  1588. return (NULL);
  1589. }
  1590. #endif ///BLE_INCLUDED == TRUE
  1591. /*******************************************************************************
  1592. **
  1593. ** Function l2cu_release_rcb
  1594. **
  1595. ** Description Mark an RCB as no longet in use
  1596. **
  1597. ** Returns void
  1598. **
  1599. *******************************************************************************/
  1600. void l2cu_release_rcb (tL2C_RCB *p_rcb)
  1601. {
  1602. p_rcb->in_use = FALSE;
  1603. p_rcb->psm = 0;
  1604. }
  1605. /*******************************************************************************
  1606. **
  1607. ** Function l2cu_disconnect_chnl
  1608. **
  1609. ** Description Disconnect a channel. Typically, this is due to either
  1610. ** receiving a bad configuration, bad packet or max_retries expiring.
  1611. **
  1612. *******************************************************************************/
  1613. void l2cu_disconnect_chnl (tL2C_CCB *p_ccb)
  1614. {
  1615. UINT16 local_cid = p_ccb->local_cid;
  1616. if (local_cid >= L2CAP_BASE_APPL_CID) {
  1617. tL2CA_DISCONNECT_IND_CB *p_disc_cb = p_ccb->p_rcb->api.pL2CA_DisconnectInd_Cb;
  1618. L2CAP_TRACE_WARNING ("L2CAP - disconnect_chnl CID: 0x%04x", local_cid);
  1619. l2cu_send_peer_disc_req (p_ccb);
  1620. l2cu_release_ccb (p_ccb);
  1621. (*p_disc_cb)(local_cid, FALSE);
  1622. } else {
  1623. /* failure on the AMP channel, probably need to disconnect ACL */
  1624. L2CAP_TRACE_ERROR ("L2CAP - disconnect_chnl CID: 0x%04x Ignored", local_cid);
  1625. }
  1626. }
  1627. /*******************************************************************************
  1628. **
  1629. ** Function l2cu_find_rcb_by_psm
  1630. **
  1631. ** Description Look through the Registration Control Blocks to see if
  1632. ** anyone registered to handle the PSM in question
  1633. **
  1634. ** Returns Pointer to the RCB or NULL if not found
  1635. **
  1636. *******************************************************************************/
  1637. tL2C_RCB *l2cu_find_rcb_by_psm (UINT16 psm)
  1638. {
  1639. tL2C_RCB *p_rcb = &l2cb.rcb_pool[0];
  1640. UINT16 xx;
  1641. for (xx = 0; xx < MAX_L2CAP_CLIENTS; xx++, p_rcb++) {
  1642. if ((p_rcb->in_use) && (p_rcb->psm == psm)) {
  1643. return (p_rcb);
  1644. }
  1645. }
  1646. /* If here, no match found */
  1647. return (NULL);
  1648. }
  1649. #if (BLE_INCLUDED == TRUE)
  1650. /*******************************************************************************
  1651. **
  1652. ** Function l2cu_find_ble_rcb_by_psm
  1653. **
  1654. ** Description Look through the BLE Registration Control Blocks to see if
  1655. ** anyone registered to handle the PSM in question
  1656. **
  1657. ** Returns Pointer to the BLE RCB or NULL if not found
  1658. **
  1659. *******************************************************************************/
  1660. tL2C_RCB *l2cu_find_ble_rcb_by_psm (UINT16 psm)
  1661. {
  1662. tL2C_RCB *p_rcb = &l2cb.ble_rcb_pool[0];
  1663. UINT16 xx;
  1664. for (xx = 0; xx < BLE_MAX_L2CAP_CLIENTS; xx++, p_rcb++)
  1665. {
  1666. if ((p_rcb->in_use) && (p_rcb->psm == psm)) {
  1667. return (p_rcb);
  1668. }
  1669. }
  1670. /* If here, no match found */
  1671. return (NULL);
  1672. }
  1673. #endif ///BLE_INCLUDED == TRUE
  1674. #if (L2CAP_COC_INCLUDED == TRUE)
  1675. /*******************************************************************************
  1676. **
  1677. ** Function l2cu_process_peer_cfg_req
  1678. **
  1679. ** Description This function is called when the peer sends us a "config request"
  1680. ** message. It extracts the configuration of interest and saves
  1681. ** it in the CCB.
  1682. **
  1683. ** Note: Negotiation of the FCR channel type is handled internally,
  1684. ** all others are passed to the upper layer.
  1685. **
  1686. ** Returns UINT8 - L2CAP_PEER_CFG_OK if passed to upper layer,
  1687. ** L2CAP_PEER_CFG_UNACCEPTABLE if automatically responded to
  1688. ** because parameters are unnacceptable from a specification
  1689. ** point of view.
  1690. ** L2CAP_PEER_CFG_DISCONNECT if no compatible channel modes
  1691. ** between the two devices, and shall be closed.
  1692. **
  1693. *******************************************************************************/
  1694. UINT8 l2cu_process_peer_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1695. {
  1696. BOOLEAN mtu_ok = TRUE;
  1697. BOOLEAN qos_type_ok = TRUE;
  1698. BOOLEAN flush_to_ok = TRUE;
  1699. BOOLEAN fcr_ok = TRUE;
  1700. #if (CLASSIC_BT_INCLUDED == TRUE)
  1701. UINT8 fcr_status;
  1702. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1703. /* Ignore FCR parameters for basic mode */
  1704. if (!p_cfg->fcr_present) {
  1705. p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
  1706. }
  1707. /* Save the MTU that our peer can receive */
  1708. if (p_cfg->mtu_present) {
  1709. /* Make sure MTU is at least the minimum */
  1710. if (p_cfg->mtu >= L2CAP_MIN_MTU) {
  1711. /* In basic mode, limit the MTU to our buffer size */
  1712. if ( (p_cfg->fcr_present == FALSE) && (p_cfg->mtu > L2CAP_MTU_SIZE) ) {
  1713. p_cfg->mtu = L2CAP_MTU_SIZE;
  1714. }
  1715. /* Save the accepted value in case of renegotiation */
  1716. p_ccb->peer_cfg.mtu = p_cfg->mtu;
  1717. p_ccb->peer_cfg.mtu_present = TRUE;
  1718. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_MTU;
  1719. } else { /* Illegal MTU value */
  1720. p_cfg->mtu = L2CAP_MIN_MTU;
  1721. mtu_ok = FALSE;
  1722. }
  1723. }
  1724. /* Reload mtu from a previously accepted config request */
  1725. else if (p_ccb->peer_cfg.mtu_present) {
  1726. p_cfg->mtu_present = TRUE;
  1727. p_cfg->mtu = p_ccb->peer_cfg.mtu;
  1728. }
  1729. /* Verify that the flush timeout is a valid value (0 is illegal) */
  1730. if (p_cfg->flush_to_present) {
  1731. if (!p_cfg->flush_to) {
  1732. p_cfg->flush_to = 0xFFFF; /* Infinite retransmissions (spec default) */
  1733. flush_to_ok = FALSE;
  1734. } else { /* Save the accepted value in case of renegotiation */
  1735. p_ccb->peer_cfg.flush_to_present = TRUE;
  1736. p_ccb->peer_cfg.flush_to = p_cfg->flush_to;
  1737. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_FLUSH_TO;
  1738. }
  1739. }
  1740. /* Reload flush_to from a previously accepted config request */
  1741. else if (p_ccb->peer_cfg.flush_to_present) {
  1742. p_cfg->flush_to_present = TRUE;
  1743. p_cfg->flush_to = p_ccb->peer_cfg.flush_to;
  1744. }
  1745. /* Save the QOS settings the the peer is using */
  1746. if (p_cfg->qos_present) {
  1747. /* Make sure service type is not a reserved value; otherwise let upper
  1748. layer decide if acceptable
  1749. */
  1750. if (p_cfg->qos.service_type <= GUARANTEED) {
  1751. p_ccb->peer_cfg.qos = p_cfg->qos;
  1752. p_ccb->peer_cfg.qos_present = TRUE;
  1753. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_QOS;
  1754. } else { /* Illegal service type value */
  1755. p_cfg->qos.service_type = BEST_EFFORT;
  1756. qos_type_ok = FALSE;
  1757. }
  1758. }
  1759. /* Reload QOS from a previously accepted config request */
  1760. else if (p_ccb->peer_cfg.qos_present) {
  1761. p_cfg->qos_present = TRUE;
  1762. p_cfg->qos = p_ccb->peer_cfg.qos;
  1763. }
  1764. #if (CLASSIC_BT_INCLUDED == TRUE)
  1765. if ((fcr_status = l2c_fcr_process_peer_cfg_req (p_ccb, p_cfg)) == L2CAP_PEER_CFG_DISCONNECT) {
  1766. /* Notify caller to disconnect the channel (incompatible modes) */
  1767. p_cfg->result = L2CAP_CFG_FAILED_NO_REASON;
  1768. p_cfg->mtu_present = p_cfg->qos_present = p_cfg->flush_to_present = 0;
  1769. return (L2CAP_PEER_CFG_DISCONNECT);
  1770. }
  1771. fcr_ok = (fcr_status == L2CAP_PEER_CFG_OK);
  1772. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1773. /* Return any unacceptable parameters */
  1774. if (mtu_ok && flush_to_ok && qos_type_ok && fcr_ok) {
  1775. l2cu_adjust_out_mps (p_ccb);
  1776. return (L2CAP_PEER_CFG_OK);
  1777. } else {
  1778. p_cfg->result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
  1779. if (mtu_ok) {
  1780. p_cfg->mtu_present = FALSE;
  1781. }
  1782. if (flush_to_ok) {
  1783. p_cfg->flush_to_present = FALSE;
  1784. }
  1785. if (qos_type_ok) {
  1786. p_cfg->qos_present = FALSE;
  1787. }
  1788. if (fcr_ok) {
  1789. p_cfg->fcr_present = FALSE;
  1790. }
  1791. return (L2CAP_PEER_CFG_UNACCEPTABLE);
  1792. }
  1793. }
  1794. /*******************************************************************************
  1795. **
  1796. ** Function l2cu_process_peer_cfg_rsp
  1797. **
  1798. ** Description This function is called when the peer sends us a "config response"
  1799. ** message. It extracts the configuration of interest and saves
  1800. ** it in the CCB.
  1801. **
  1802. ** Returns void
  1803. **
  1804. *******************************************************************************/
  1805. void l2cu_process_peer_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1806. {
  1807. /* If we wanted QoS and the peer sends us a positive response with QoS, use his values */
  1808. if ( (p_cfg->qos_present) && (p_ccb->our_cfg.qos_present) ) {
  1809. p_ccb->our_cfg.qos = p_cfg->qos;
  1810. }
  1811. if (p_cfg->fcr_present) {
  1812. /* Save the retransmission and monitor timeout values */
  1813. if (p_cfg->fcr.mode == L2CAP_FCR_ERTM_MODE) {
  1814. p_ccb->peer_cfg.fcr.rtrans_tout = p_cfg->fcr.rtrans_tout;
  1815. p_ccb->peer_cfg.fcr.mon_tout = p_cfg->fcr.mon_tout;
  1816. }
  1817. /* Calculate the max number of packets for which we can delay sending an ack */
  1818. if (p_cfg->fcr.tx_win_sz < p_ccb->our_cfg.fcr.tx_win_sz) {
  1819. p_ccb->fcrb.max_held_acks = p_cfg->fcr.tx_win_sz / 3;
  1820. } else {
  1821. p_ccb->fcrb.max_held_acks = p_ccb->our_cfg.fcr.tx_win_sz / 3;
  1822. }
  1823. L2CAP_TRACE_DEBUG ("l2cu_process_peer_cfg_rsp(): peer tx_win_sz: %d, our tx_win_sz: %d, max_held_acks: %d",
  1824. p_cfg->fcr.tx_win_sz, p_ccb->our_cfg.fcr.tx_win_sz, p_ccb->fcrb.max_held_acks);
  1825. }
  1826. }
  1827. /*******************************************************************************
  1828. **
  1829. ** Function l2cu_process_our_cfg_req
  1830. **
  1831. ** Description This function is called when we send a "config request"
  1832. ** message. It extracts the configuration of interest and saves
  1833. ** it in the CCB.
  1834. **
  1835. ** Returns void
  1836. **
  1837. *******************************************************************************/
  1838. void l2cu_process_our_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1839. {
  1840. tL2C_LCB *p_lcb;
  1841. UINT16 hci_flush_to;
  1842. /* Save the QOS settings we are using for transmit */
  1843. if (p_cfg->qos_present) {
  1844. p_ccb->our_cfg.qos_present = TRUE;
  1845. p_ccb->our_cfg.qos = p_cfg->qos;
  1846. }
  1847. if (p_cfg->fcr_present) {
  1848. /* Override FCR options if attempting streaming or basic */
  1849. if (p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE) {
  1850. memset(&p_cfg->fcr, 0, sizeof(tL2CAP_FCR_OPTS));
  1851. } else {
  1852. /* On BR/EDR, timer values are zero in config request */
  1853. /* On class 2 AMP, timer value in config request shall be non-0 processing time */
  1854. /* timer value in config response shall be greater than received processing time */
  1855. p_cfg->fcr.mon_tout = p_cfg->fcr.rtrans_tout = 0;
  1856. if (p_cfg->fcr.mode == L2CAP_FCR_STREAM_MODE) {
  1857. p_cfg->fcr.max_transmit = p_cfg->fcr.tx_win_sz = 0;
  1858. }
  1859. }
  1860. /* Set the threshold to send acks (may be updated in the cfg response) */
  1861. p_ccb->fcrb.max_held_acks = p_cfg->fcr.tx_win_sz / 3;
  1862. /* Include FCS option only if peer can handle it */
  1863. if (p_ccb->p_lcb->peer_ext_fea & L2CAP_EXTFEA_NO_CRC) {
  1864. /* FCS check can be bypassed if peer also desires to bypass */
  1865. if (p_cfg->fcs_present && p_cfg->fcs == L2CAP_CFG_FCS_BYPASS) {
  1866. p_ccb->bypass_fcs |= L2CAP_CFG_FCS_OUR;
  1867. }
  1868. } else {
  1869. p_cfg->fcs_present = FALSE;
  1870. }
  1871. } else {
  1872. p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
  1873. }
  1874. p_ccb->our_cfg.fcr.mode = p_cfg->fcr.mode;
  1875. p_ccb->our_cfg.fcr_present = p_cfg->fcr_present;
  1876. /* Check the flush timeout. If it is lower than the current one used */
  1877. /* then we need to adjust the flush timeout sent to the controller */
  1878. if (p_cfg->flush_to_present) {
  1879. if ((p_cfg->flush_to == 0) || (p_cfg->flush_to == L2CAP_NO_AUTOMATIC_FLUSH)) {
  1880. /* don't send invalid flush timeout */
  1881. /* SPEC: The sender of the Request shall specify its flush timeout value */
  1882. /* if it differs from the default value of 0xFFFF */
  1883. p_cfg->flush_to_present = FALSE;
  1884. } else {
  1885. p_ccb->our_cfg.flush_to = p_cfg->flush_to;
  1886. p_lcb = p_ccb->p_lcb;
  1887. if (p_cfg->flush_to < p_lcb->link_flush_tout) {
  1888. p_lcb->link_flush_tout = p_cfg->flush_to;
  1889. /* If the timeout is within range of HCI, set the flush timeout */
  1890. if (p_cfg->flush_to <= ((HCI_MAX_AUTO_FLUSH_TOUT * 5) / 8)) {
  1891. /* Convert flush timeout to 0.625 ms units, with round */
  1892. hci_flush_to = ((p_cfg->flush_to * 8) + 3) / 5;
  1893. btsnd_hcic_write_auto_flush_tout (p_lcb->handle, hci_flush_to);
  1894. }
  1895. }
  1896. }
  1897. }
  1898. }
  1899. /*******************************************************************************
  1900. **
  1901. ** Function l2cu_process_our_cfg_rsp
  1902. **
  1903. ** Description This function is called when we send the peer a "config response"
  1904. ** message. It extracts the configuration of interest and saves
  1905. ** it in the CCB.
  1906. **
  1907. ** Returns void
  1908. **
  1909. *******************************************************************************/
  1910. void l2cu_process_our_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1911. {
  1912. /* If peer wants QoS, we are allowed to change the values in a positive response */
  1913. if ( (p_cfg->qos_present) && (p_ccb->peer_cfg.qos_present) ) {
  1914. p_ccb->peer_cfg.qos = p_cfg->qos;
  1915. } else {
  1916. p_cfg->qos_present = FALSE;
  1917. }
  1918. l2c_fcr_adj_our_rsp_options (p_ccb, p_cfg);
  1919. }
  1920. #endif // (L2CAP_COC_INCLUDED == TRUE)
  1921. /*******************************************************************************
  1922. **
  1923. ** Function l2cu_device_reset
  1924. **
  1925. ** Description This function is called when reset of the device is
  1926. ** completed. For all active connection simulate HCI_DISC
  1927. **
  1928. ** Returns void
  1929. **
  1930. *******************************************************************************/
  1931. void l2cu_device_reset (void)
  1932. {
  1933. list_node_t *p_node = NULL;
  1934. tL2C_LCB *p_lcb = NULL;
  1935. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  1936. p_lcb = list_node(p_node);
  1937. if ((p_lcb->in_use) && (p_lcb->handle != HCI_INVALID_HANDLE)) {
  1938. l2c_link_hci_disc_comp (p_lcb->handle, (UINT8) - 1);
  1939. }
  1940. }
  1941. #if (BLE_INCLUDED == TRUE)
  1942. l2cb.is_ble_connecting = FALSE;
  1943. #endif
  1944. }
  1945. /*******************************************************************************
  1946. **
  1947. ** Function l2cu_create_conn
  1948. **
  1949. ** Description This function initiates an acl connection via HCI
  1950. **
  1951. ** Returns TRUE if successful, FALSE if gki get buffer fails.
  1952. **
  1953. *******************************************************************************/
  1954. BOOLEAN l2cu_create_conn (tL2C_LCB *p_lcb, tBT_TRANSPORT transport)
  1955. {
  1956. #if BTM_SCO_INCLUDED == TRUE
  1957. BOOLEAN is_sco_active;
  1958. #endif
  1959. list_node_t *p_node = NULL;
  1960. tL2C_LCB *p_lcb_cur = NULL;
  1961. #if (BLE_INCLUDED == TRUE)
  1962. tBT_DEVICE_TYPE dev_type;
  1963. tBLE_ADDR_TYPE addr_type = p_lcb->open_addr_type;
  1964. if(addr_type == BLE_ADDR_UNKNOWN_TYPE) {
  1965. BTM_ReadDevInfo(p_lcb->remote_bd_addr, &dev_type, &addr_type);
  1966. }
  1967. if (transport == BT_TRANSPORT_LE) {
  1968. if (!controller_get_interface()->supports_ble()) {
  1969. return FALSE;
  1970. }
  1971. if(addr_type > BLE_ADDR_TYPE_MAX) {
  1972. addr_type = BLE_ADDR_PUBLIC;
  1973. }
  1974. p_lcb->ble_addr_type = addr_type;
  1975. p_lcb->transport = BT_TRANSPORT_LE;
  1976. return (l2cble_create_conn(p_lcb));
  1977. }
  1978. #endif
  1979. /* If there is a connection where we perform as a slave, try to switch roles
  1980. for this connection */
  1981. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  1982. p_lcb_cur = list_node(p_node);
  1983. if (p_lcb_cur == p_lcb) {
  1984. continue;
  1985. }
  1986. if ((p_lcb_cur->in_use) && (p_lcb_cur->link_role == HCI_ROLE_SLAVE)) {
  1987. #if BTM_SCO_INCLUDED == TRUE
  1988. /* The LMP_switch_req shall be sent only if the ACL logical transport
  1989. is in active mode, when encryption is disabled, and all synchronous
  1990. logical transports on the same physical link are disabled." */
  1991. /* Check if there is any SCO Active on this BD Address */
  1992. is_sco_active = btm_is_sco_active_by_bdaddr(p_lcb_cur->remote_bd_addr);
  1993. L2CAP_TRACE_API ("l2cu_create_conn - btm_is_sco_active_by_bdaddr() is_sco_active = %s", \
  1994. (is_sco_active == TRUE) ? "TRUE" : "FALSE");
  1995. if (is_sco_active == TRUE) {
  1996. continue; /* No Master Slave switch not allowed when SCO Active */
  1997. }
  1998. #endif
  1999. /*4_1_TODO check if btm_cb.devcb.local_features to be used instead */
  2000. if (HCI_SWITCH_SUPPORTED(BTM_ReadLocalFeatures())) {
  2001. /* mark this lcb waiting for switch to be completed and
  2002. start switch on the other one */
  2003. p_lcb->link_state = LST_CONNECTING_WAIT_SWITCH;
  2004. p_lcb->link_role = HCI_ROLE_MASTER;
  2005. if (BTM_SwitchRole (p_lcb_cur->remote_bd_addr, HCI_ROLE_MASTER, NULL) == BTM_CMD_STARTED) {
  2006. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_ROLE_SWITCH_TOUT);
  2007. return (TRUE);
  2008. }
  2009. }
  2010. }
  2011. }
  2012. p_lcb->link_state = LST_CONNECTING;
  2013. return (l2cu_create_conn_after_switch (p_lcb));
  2014. }
  2015. /*******************************************************************************
  2016. **
  2017. ** Function l2cu_get_num_hi_priority
  2018. **
  2019. ** Description Gets the number of high priority channels.
  2020. **
  2021. ** Returns
  2022. **
  2023. *******************************************************************************/
  2024. UINT8 l2cu_get_num_hi_priority (void)
  2025. {
  2026. UINT8 no_hi = 0;
  2027. list_node_t *p_node = NULL;
  2028. tL2C_LCB *p_lcb = NULL;
  2029. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2030. p_lcb = list_node(p_node);
  2031. if ((p_lcb->in_use) && (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)) {
  2032. no_hi++;
  2033. }
  2034. }
  2035. return no_hi;
  2036. }
  2037. /*******************************************************************************
  2038. **
  2039. ** Function l2cu_create_conn_after_switch
  2040. **
  2041. ** Description This function initiates an acl connection via HCI
  2042. ** If switch required to create connection it is already done.
  2043. **
  2044. ** Returns TRUE if successful, FALSE if osi get buffer fails.
  2045. **
  2046. *******************************************************************************/
  2047. BOOLEAN l2cu_create_conn_after_switch (tL2C_LCB *p_lcb)
  2048. {
  2049. UINT8 allow_switch = HCI_CR_CONN_ALLOW_SWITCH;
  2050. tBTM_INQ_INFO *p_inq_info;
  2051. UINT8 page_scan_rep_mode;
  2052. UINT8 page_scan_mode;
  2053. UINT16 clock_offset;
  2054. UINT8 *p_features;
  2055. UINT16 num_acl = BTM_GetNumAclLinks();
  2056. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_lcb->remote_bd_addr);
  2057. UINT8 no_hi_prio_chs = l2cu_get_num_hi_priority();
  2058. p_features = BTM_ReadLocalFeatures();
  2059. L2CAP_TRACE_DEBUG ("l2cu_create_conn_after_switch :%d num_acl:%d no_hi: %d is_bonding:%d",
  2060. l2cb.disallow_switch, num_acl, no_hi_prio_chs, p_lcb->is_bonding);
  2061. /* FW team says that we can participant in 4 piconets
  2062. * typically 3 piconet + 1 for scanning.
  2063. * We can enhance the code to count the number of piconets later. */
  2064. if ( ((!l2cb.disallow_switch && (num_acl < 3)) || (p_lcb->is_bonding && (no_hi_prio_chs == 0)))
  2065. && HCI_SWITCH_SUPPORTED(p_features)) {
  2066. allow_switch = HCI_CR_CONN_ALLOW_SWITCH;
  2067. } else {
  2068. allow_switch = HCI_CR_CONN_NOT_ALLOW_SWITCH;
  2069. }
  2070. p_lcb->link_state = LST_CONNECTING;
  2071. /* Check with the BT manager if details about remote device are known */
  2072. if ((p_inq_info = BTM_InqDbRead(p_lcb->remote_bd_addr)) != NULL) {
  2073. page_scan_rep_mode = p_inq_info->results.page_scan_rep_mode;
  2074. page_scan_mode = p_inq_info->results.page_scan_mode;
  2075. clock_offset = (UINT16)(p_inq_info->results.clock_offset);
  2076. } else {
  2077. /* No info known. Use default settings */
  2078. page_scan_rep_mode = HCI_PAGE_SCAN_REP_MODE_R2;
  2079. page_scan_mode = HCI_MANDATARY_PAGE_SCAN_MODE;
  2080. clock_offset = (p_dev_rec) ? p_dev_rec->clock_offset : 0;
  2081. }
  2082. if (!btsnd_hcic_create_conn (p_lcb->remote_bd_addr,
  2083. ( HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1
  2084. | HCI_PKT_TYPES_MASK_DM3 | HCI_PKT_TYPES_MASK_DH3
  2085. | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5 ),
  2086. page_scan_rep_mode,
  2087. page_scan_mode,
  2088. clock_offset,
  2089. allow_switch))
  2090. {
  2091. L2CAP_TRACE_ERROR ("L2CAP - no buffer for l2cu_create_conn");
  2092. l2cu_release_lcb (p_lcb);
  2093. return (FALSE);
  2094. }
  2095. btm_acl_update_busy_level (BTM_BLI_PAGE_EVT);
  2096. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK,
  2097. L2CAP_LINK_CONNECT_TOUT);
  2098. return (TRUE);
  2099. }
  2100. /*******************************************************************************
  2101. **
  2102. ** Function l2cu_find_lcb_by_state
  2103. **
  2104. ** Description Look through all active LCBs for a match based on the
  2105. ** LCB state.
  2106. **
  2107. ** Returns pointer to first matched LCB, or NULL if no match
  2108. **
  2109. *******************************************************************************/
  2110. tL2C_LCB *l2cu_find_lcb_by_state (tL2C_LINK_STATE state)
  2111. {
  2112. list_node_t *p_node = NULL;
  2113. tL2C_LCB *p_lcb = NULL;
  2114. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2115. p_lcb = list_node(p_node);
  2116. if ((p_lcb->in_use) && (p_lcb->link_state == state)) {
  2117. return (p_lcb);
  2118. }
  2119. }
  2120. /* If here, no match found */
  2121. return (NULL);
  2122. }
  2123. /*******************************************************************************
  2124. **
  2125. ** Function l2cu_lcb_disconnecting
  2126. **
  2127. ** Description On each active lcb, check if the lcb is in disconnecting
  2128. ** state, or if there are no ccb's on the lcb (implying
  2129. idle timeout is running), or if last ccb on the link
  2130. is in disconnecting state.
  2131. **
  2132. ** Returns TRUE if any of above conditions met, FALSE otherwise
  2133. **
  2134. *******************************************************************************/
  2135. BOOLEAN l2cu_lcb_disconnecting (void)
  2136. {
  2137. tL2C_LCB *p_lcb;
  2138. tL2C_CCB *p_ccb;
  2139. BOOLEAN status = FALSE;
  2140. list_node_t *p_node = NULL;
  2141. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2142. p_lcb = list_node(p_node);
  2143. if (p_lcb->in_use) {
  2144. /* no ccbs on lcb, or lcb is in disconnecting state */
  2145. if ((!p_lcb->ccb_queue.p_first_ccb) || (p_lcb->link_state == LST_DISCONNECTING)) {
  2146. status = TRUE;
  2147. break;
  2148. }
  2149. /* only one ccb left on lcb */
  2150. else if (p_lcb->ccb_queue.p_first_ccb == p_lcb->ccb_queue.p_last_ccb) {
  2151. p_ccb = p_lcb->ccb_queue.p_first_ccb;
  2152. if ((p_ccb->in_use) &&
  2153. ((p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP) ||
  2154. (p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP))) {
  2155. status = TRUE;
  2156. break;
  2157. }
  2158. }
  2159. }
  2160. }
  2161. return status;
  2162. }
  2163. /*******************************************************************************
  2164. **
  2165. ** Function l2cu_set_acl_priority
  2166. **
  2167. ** Description Sets the transmission priority for a channel.
  2168. ** (For initial implementation only two values are valid.
  2169. ** L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
  2170. **
  2171. ** Returns TRUE if a valid channel, else FALSE
  2172. **
  2173. *******************************************************************************/
  2174. BOOLEAN l2cu_set_acl_priority (BD_ADDR bd_addr, UINT8 priority, BOOLEAN reset_after_rs)
  2175. {
  2176. tL2C_LCB *p_lcb;
  2177. UINT8 *pp;
  2178. UINT8 command[HCI_BRCM_ACL_PRIORITY_PARAM_SIZE];
  2179. UINT8 vs_param;
  2180. //APPL_TRACE_EVENT("SET ACL PRIORITY %d", priority);
  2181. /* Find the link control block for the acl channel */
  2182. if ((p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  2183. L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_SetAclPriority");
  2184. return (FALSE);
  2185. }
  2186. if (BTM_IS_BRCM_CONTROLLER()) {
  2187. /* Called from above L2CAP through API; send VSC if changed */
  2188. if ((!reset_after_rs && (priority != p_lcb->acl_priority)) ||
  2189. /* Called because of a master/slave role switch; if high resend VSC */
  2190. ( reset_after_rs && p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)) {
  2191. pp = command;
  2192. vs_param = (priority == L2CAP_PRIORITY_HIGH) ? HCI_BRCM_ACL_PRIORITY_HIGH : HCI_BRCM_ACL_PRIORITY_LOW;
  2193. UINT16_TO_STREAM (pp, p_lcb->handle);
  2194. UINT8_TO_STREAM (pp, vs_param);
  2195. BTM_VendorSpecificCommand (HCI_BRCM_SET_ACL_PRIORITY, HCI_BRCM_ACL_PRIORITY_PARAM_SIZE, command, NULL);
  2196. /* Adjust lmp buffer allocation for this channel if priority changed */
  2197. if (p_lcb->acl_priority != priority) {
  2198. p_lcb->acl_priority = priority;
  2199. l2c_link_adjust_allocation();
  2200. }
  2201. }
  2202. }
  2203. return (TRUE);
  2204. }
  2205. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  2206. /******************************************************************************
  2207. **
  2208. ** Function l2cu_set_non_flushable_pbf
  2209. **
  2210. ** Description set L2CAP_PKT_START_NON_FLUSHABLE if controller supoorts
  2211. **
  2212. ** Returns void
  2213. **
  2214. *******************************************************************************/
  2215. void l2cu_set_non_flushable_pbf (BOOLEAN is_supported)
  2216. {
  2217. if (is_supported) {
  2218. l2cb.non_flushable_pbf = (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT);
  2219. } else {
  2220. l2cb.non_flushable_pbf = (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
  2221. }
  2222. }
  2223. #endif
  2224. /*******************************************************************************
  2225. **
  2226. ** Function l2cu_resubmit_pending_sec_req
  2227. **
  2228. ** Description This function is called when required security procedures
  2229. ** are completed and any pending requests can be re-submitted.
  2230. **
  2231. ** Returns void
  2232. **
  2233. *******************************************************************************/
  2234. #if (CLASSIC_BT_INCLUDED == TRUE)
  2235. void l2cu_resubmit_pending_sec_req (BD_ADDR p_bda)
  2236. {
  2237. tL2C_LCB *p_lcb;
  2238. tL2C_CCB *p_ccb;
  2239. tL2C_CCB *p_next_ccb;
  2240. L2CAP_TRACE_DEBUG ("l2cu_resubmit_pending_sec_req p_bda: %p", p_bda);
  2241. list_node_t *p_node = NULL;
  2242. /* If we are called with a BDA, only resubmit for that BDA */
  2243. if (p_bda) {
  2244. p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, BT_TRANSPORT_BR_EDR);
  2245. /* If we don't have one, this is an error */
  2246. if (p_lcb) {
  2247. /* For all channels, send the event through their FSMs */
  2248. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb) {
  2249. p_next_ccb = p_ccb->p_next_ccb;
  2250. l2c_csm_execute (p_ccb, L2CEVT_SEC_RE_SEND_CMD, NULL);
  2251. }
  2252. } else {
  2253. L2CAP_TRACE_WARNING ("l2cu_resubmit_pending_sec_req - unknown BD_ADDR");
  2254. }
  2255. } else {
  2256. /* No BDA pasesed in, so check all links */
  2257. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2258. p_lcb = list_node(p_node);
  2259. if (p_lcb->in_use) {
  2260. /* For all channels, send the event through their FSMs */
  2261. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb) {
  2262. p_next_ccb = p_ccb->p_next_ccb;
  2263. l2c_csm_execute (p_ccb, L2CEVT_SEC_RE_SEND_CMD, NULL);
  2264. }
  2265. }
  2266. }
  2267. }
  2268. }
  2269. #endif ///CLASSIC_BT_INCLUDED == TRUE
  2270. #if L2CAP_CONFORMANCE_TESTING == TRUE
  2271. /*******************************************************************************
  2272. **
  2273. ** Function l2cu_set_info_rsp_mask
  2274. **
  2275. ** Description This function allows the script wrapper to change the
  2276. ** info resp mask for conformance testing.
  2277. **
  2278. ** Returns pointer to CCB, or NULL if none
  2279. **
  2280. *******************************************************************************/
  2281. void l2cu_set_info_rsp_mask (UINT32 mask)
  2282. {
  2283. l2cb.test_info_resp = mask;
  2284. }
  2285. #endif /* L2CAP_CONFORMANCE_TESTING */
  2286. /*******************************************************************************
  2287. **
  2288. ** Function l2cu_adjust_out_mps
  2289. **
  2290. ** Description Sets our MPS based on current controller capabilities
  2291. **
  2292. ** Returns void
  2293. **
  2294. *******************************************************************************/
  2295. void l2cu_adjust_out_mps (tL2C_CCB *p_ccb)
  2296. {
  2297. UINT16 packet_size;
  2298. /* on the tx side MTU is selected based on packet size of the controller */
  2299. packet_size = btm_get_max_packet_size (p_ccb->p_lcb->remote_bd_addr);
  2300. if (packet_size <= (L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + L2CAP_FCS_LEN)) {
  2301. /* something is very wrong */
  2302. L2CAP_TRACE_DEBUG ("l2cu_adjust_out_mps bad packet size: %u will use MPS: %u", packet_size, p_ccb->peer_cfg.fcr.mps);
  2303. p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps;
  2304. } else {
  2305. packet_size -= (L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + L2CAP_FCS_LEN);
  2306. /* We try to negotiate MTU that each packet can be split into whole
  2307. number of max packets. For example if link is 1.2 max packet size is 339 bytes.
  2308. At first calculate how many whole packets it is. MAX L2CAP is 1691 + 4 overhead.
  2309. 1695, that will be 5 Dh5 packets. Now maximum L2CAP packet is
  2310. 5 * 339 = 1695. Minus 4 bytes L2CAP header 1691.
  2311. For EDR 2.0 packet size is 1027. So we better send RFCOMM packet as 1 3DH5 packet
  2312. 1 * 1027 = 1027. Minus 4 bytes L2CAP header 1023. */
  2313. if (p_ccb->peer_cfg.fcr.mps >= packet_size) {
  2314. p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps / packet_size * packet_size;
  2315. } else {
  2316. p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps;
  2317. }
  2318. L2CAP_TRACE_DEBUG ("l2cu_adjust_out_mps use %d Based on peer_cfg.fcr.mps: %u packet_size: %u",
  2319. p_ccb->tx_mps, p_ccb->peer_cfg.fcr.mps, packet_size);
  2320. }
  2321. }
  2322. /*******************************************************************************
  2323. **
  2324. ** Function l2cu_initialize_fixed_ccb
  2325. **
  2326. ** Description Initialize a fixed channel's CCB
  2327. **
  2328. ** Returns TRUE or FALSE
  2329. **
  2330. *******************************************************************************/
  2331. BOOLEAN l2cu_initialize_fixed_ccb (tL2C_LCB *p_lcb, UINT16 fixed_cid, tL2CAP_FCR_OPTS *p_fcr)
  2332. {
  2333. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2334. tL2C_CCB *p_ccb;
  2335. /* If we already have a CCB, then simply return */
  2336. if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] != NULL) {
  2337. return (TRUE);
  2338. }
  2339. if ((p_ccb = l2cu_allocate_ccb (NULL, 0)) == NULL) {
  2340. return (FALSE);
  2341. }
  2342. btu_stop_timer(&p_lcb->timer_entry);
  2343. /* Set CID for the connection */
  2344. p_ccb->local_cid = fixed_cid;
  2345. p_ccb->remote_cid = fixed_cid;
  2346. p_ccb->is_flushable = FALSE;
  2347. p_ccb->timer_entry.param = (TIMER_PARAM_TYPE)p_ccb;
  2348. if (p_fcr) {
  2349. /* Set the FCR parameters. For now, we will use default pools */
  2350. p_ccb->our_cfg.fcr = p_ccb->peer_cfg.fcr = *p_fcr;
  2351. p_ccb->ertm_info.fcr_rx_buf_size = L2CAP_FCR_RX_BUF_SIZE;
  2352. p_ccb->ertm_info.fcr_tx_buf_size = L2CAP_FCR_TX_BUF_SIZE;
  2353. p_ccb->ertm_info.user_rx_buf_size = L2CAP_USER_RX_BUF_SIZE;
  2354. p_ccb->ertm_info.user_tx_buf_size = L2CAP_USER_TX_BUF_SIZE;
  2355. p_ccb->fcrb.max_held_acks = p_fcr->tx_win_sz / 3;
  2356. }
  2357. /* Link ccb to lcb and lcb to ccb */
  2358. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = p_ccb;
  2359. p_ccb->p_lcb = p_lcb;
  2360. /* There is no configuration, so if the link is up, the channel is up */
  2361. if (p_lcb->link_state == LST_CONNECTED) {
  2362. p_ccb->chnl_state = CST_OPEN;
  2363. }
  2364. /* Set the default idle timeout value to use */
  2365. p_ccb->fixed_chnl_idle_tout = l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].default_idle_tout;
  2366. #endif
  2367. return (TRUE);
  2368. }
  2369. /*******************************************************************************
  2370. **
  2371. ** Function l2cu_no_dynamic_ccbs
  2372. **
  2373. ** Description Handles the case when there are no more dynamic CCBs. If there
  2374. ** are any fixed CCBs, start the longest of the fixed CCB timeouts,
  2375. ** otherwise start the default link idle timeout or disconnect.
  2376. **
  2377. ** Returns void
  2378. **
  2379. *******************************************************************************/
  2380. void l2cu_no_dynamic_ccbs (tL2C_LCB *p_lcb)
  2381. {
  2382. #if (SMP_INCLUDED == TRUE)
  2383. tBTM_STATUS rc;
  2384. #endif ///SMP_INCLUDED == TRUE
  2385. UINT16 timeout = p_lcb->idle_timeout;
  2386. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2387. int xx;
  2388. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  2389. if ( (p_lcb->p_fixed_ccbs[xx] != NULL) && (p_lcb->p_fixed_ccbs[xx]->fixed_chnl_idle_tout > timeout) ) {
  2390. timeout = p_lcb->p_fixed_ccbs[xx]->fixed_chnl_idle_tout;
  2391. }
  2392. }
  2393. #endif
  2394. /* If the link is pairing, do not mess with the timeouts */
  2395. if (p_lcb->is_bonding) {
  2396. return;
  2397. }
  2398. if (timeout == 0) {
  2399. L2CAP_TRACE_DEBUG ("l2cu_no_dynamic_ccbs() IDLE timer 0, disconnecting link");
  2400. #if (SMP_INCLUDED == TRUE)
  2401. rc = btm_sec_disconnect (p_lcb->handle, HCI_ERR_PEER_USER);
  2402. if (rc == BTM_CMD_STARTED) {
  2403. l2cu_process_fixed_disc_cback(p_lcb);
  2404. p_lcb->link_state = LST_DISCONNECTING;
  2405. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  2406. } else if (rc == BTM_SUCCESS) {
  2407. l2cu_process_fixed_disc_cback(p_lcb);
  2408. /* BTM SEC will make sure that link is release (probably after pairing is done) */
  2409. p_lcb->link_state = LST_DISCONNECTING;
  2410. timeout = 0xFFFF;
  2411. } else if ( (p_lcb->is_bonding)
  2412. && (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER)) ) {
  2413. l2cu_process_fixed_disc_cback(p_lcb);
  2414. p_lcb->link_state = LST_DISCONNECTING;
  2415. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  2416. } else {
  2417. /* probably no buffer to send disconnect */
  2418. timeout = BT_1SEC_TIMEOUT;
  2419. }
  2420. #else
  2421. if (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER)) {
  2422. l2cu_process_fixed_disc_cback(p_lcb);
  2423. p_lcb->link_state = LST_DISCONNECTING;
  2424. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  2425. } else {
  2426. timeout = BT_1SEC_TIMEOUT;
  2427. }
  2428. #endif ///SMP_INCLUDED == TRUE
  2429. }
  2430. if (timeout != 0xFFFF) {
  2431. L2CAP_TRACE_DEBUG ("l2cu_no_dynamic_ccbs() starting IDLE timeout: %d", timeout);
  2432. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, timeout);
  2433. } else {
  2434. btu_stop_timer(&p_lcb->timer_entry);
  2435. }
  2436. }
  2437. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2438. /*******************************************************************************
  2439. **
  2440. ** Function l2cu_process_fixed_chnl_resp
  2441. **
  2442. ** Description handle a fixed channel response (or lack thereof)
  2443. ** if the link failed, or a fixed channel response was
  2444. ** not received, the bitfield is all zeros.
  2445. **
  2446. *******************************************************************************/
  2447. void l2cu_process_fixed_chnl_resp (tL2C_LCB *p_lcb)
  2448. {
  2449. L2CAP_TRACE_DEBUG("%s",__func__);
  2450. #if (BLE_INCLUDED == TRUE)
  2451. if (p_lcb->transport == BT_TRANSPORT_BR_EDR) {
  2452. /* ignore all not assigned BR/EDR channels */
  2453. p_lcb->peer_chnl_mask[0] &= (L2CAP_FIXED_CHNL_SIG_BIT | \
  2454. L2CAP_FIXED_CHNL_CNCTLESS_BIT | \
  2455. L2CAP_FIXED_CHNL_SMP_BR_BIT);
  2456. } else {
  2457. p_lcb->peer_chnl_mask[0] = l2cb.l2c_ble_fixed_chnls_mask;
  2458. }
  2459. #endif
  2460. /* Tell all registered fixed channels about the connection */
  2461. for (int xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  2462. #if BLE_INCLUDED == TRUE
  2463. /* skip sending LE fix channel callbacks on BR/EDR links */
  2464. if (p_lcb->transport == BT_TRANSPORT_BR_EDR &&
  2465. xx + L2CAP_FIRST_FIXED_CHNL >= L2CAP_ATT_CID &&
  2466. xx + L2CAP_FIRST_FIXED_CHNL <= L2CAP_SMP_CID) {
  2467. continue;
  2468. }
  2469. #endif
  2470. if (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL) {
  2471. if (p_lcb->peer_chnl_mask[(xx + L2CAP_FIRST_FIXED_CHNL) / 8]
  2472. & (1 << ((xx + L2CAP_FIRST_FIXED_CHNL) % 8))) {
  2473. if (p_lcb->p_fixed_ccbs[xx]) {
  2474. p_lcb->p_fixed_ccbs[xx]->chnl_state = CST_OPEN;
  2475. }
  2476. #if BLE_INCLUDED == TRUE
  2477. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2478. p_lcb->remote_bd_addr, TRUE, 0, p_lcb->transport);
  2479. #else
  2480. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2481. p_lcb->remote_bd_addr, TRUE, 0, BT_TRANSPORT_BR_EDR);
  2482. #endif
  2483. } else {
  2484. #if BLE_INCLUDED == TRUE
  2485. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2486. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
  2487. #else
  2488. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2489. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
  2490. #endif
  2491. if (p_lcb->p_fixed_ccbs[xx]) {
  2492. l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
  2493. p_lcb->p_fixed_ccbs[xx] = NULL;
  2494. }
  2495. }
  2496. }
  2497. }
  2498. }
  2499. #endif
  2500. /*******************************************************************************
  2501. **
  2502. ** Function l2cu_process_fixed_disc_cback
  2503. **
  2504. ** Description send l2cap fixed channel disconnection callback to application
  2505. **
  2506. **
  2507. ** Returns void
  2508. **
  2509. *******************************************************************************/
  2510. void l2cu_process_fixed_disc_cback (tL2C_LCB *p_lcb)
  2511. {
  2512. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2513. /* Select peer channels mask to use depending on transport */
  2514. UINT8 peer_channel_mask = p_lcb->peer_chnl_mask[0];
  2515. // For LE, reset the stored peer channel mask
  2516. if (p_lcb->transport == BT_TRANSPORT_LE) {
  2517. p_lcb->peer_chnl_mask[0] = 0;
  2518. }
  2519. for (int xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  2520. if (p_lcb->p_fixed_ccbs[xx]) {
  2521. if (p_lcb->p_fixed_ccbs[xx] != p_lcb->p_pending_ccb) {
  2522. tL2C_CCB *p_l2c_chnl_ctrl_block;
  2523. p_l2c_chnl_ctrl_block = p_lcb->p_fixed_ccbs[xx];
  2524. p_lcb->p_fixed_ccbs[xx] = NULL;
  2525. l2cu_release_ccb(p_l2c_chnl_ctrl_block);
  2526. #if BLE_INCLUDED == TRUE
  2527. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2528. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
  2529. #else
  2530. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2531. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
  2532. #endif
  2533. }
  2534. } else if ( (peer_channel_mask & (1 << (xx + L2CAP_FIRST_FIXED_CHNL)))
  2535. && (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL) ) {
  2536. #if BLE_INCLUDED == TRUE
  2537. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2538. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
  2539. #else
  2540. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2541. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
  2542. #endif
  2543. }
  2544. }
  2545. #endif
  2546. }
  2547. #if (BLE_INCLUDED == TRUE)
  2548. /*******************************************************************************
  2549. **
  2550. ** Function l2cu_send_peer_ble_par_req
  2551. **
  2552. ** Description Build and send a BLE parameter update request message
  2553. ** to the peer.
  2554. **
  2555. ** Returns void
  2556. **
  2557. *******************************************************************************/
  2558. void l2cu_send_peer_ble_par_req (tL2C_LCB *p_lcb, UINT16 min_int, UINT16 max_int,
  2559. UINT16 latency, UINT16 timeout)
  2560. {
  2561. BT_HDR *p_buf;
  2562. UINT8 *p;
  2563. /* Create an identifier for this packet */
  2564. p_lcb->id++;
  2565. l2cu_adj_id (p_lcb, L2CAP_ADJ_ID);
  2566. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_UPD_REQ_LEN,
  2567. L2CAP_CMD_BLE_UPDATE_REQ, p_lcb->id)) == NULL ) {
  2568. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_par_req - no buffer");
  2569. return;
  2570. }
  2571. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2572. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2573. UINT16_TO_STREAM (p, min_int);
  2574. UINT16_TO_STREAM (p, max_int);
  2575. UINT16_TO_STREAM (p, latency);
  2576. UINT16_TO_STREAM (p, timeout);
  2577. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2578. }
  2579. /*******************************************************************************
  2580. **
  2581. ** Function l2cu_send_peer_ble_par_rsp
  2582. **
  2583. ** Description Build and send a BLE parameter update response message
  2584. ** to the peer.
  2585. **
  2586. ** Returns void
  2587. **
  2588. *******************************************************************************/
  2589. void l2cu_send_peer_ble_par_rsp (tL2C_LCB *p_lcb, UINT16 reason, UINT8 rem_id)
  2590. {
  2591. BT_HDR *p_buf;
  2592. UINT8 *p;
  2593. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_UPD_RSP_LEN,
  2594. L2CAP_CMD_BLE_UPDATE_RSP, rem_id)) == NULL ) {
  2595. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_par_rsp - no buffer");
  2596. return;
  2597. }
  2598. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2599. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2600. UINT16_TO_STREAM (p, reason);
  2601. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2602. }
  2603. /*******************************************************************************
  2604. **
  2605. ** Function l2cu_send_peer_ble_credit_based_conn_req
  2606. **
  2607. ** Description Build and send a BLE packet to establish LE connection oriented
  2608. ** L2CAP channel.
  2609. **
  2610. ** Returns void
  2611. **
  2612. *******************************************************************************/
  2613. void l2cu_send_peer_ble_credit_based_conn_req (tL2C_CCB *p_ccb)
  2614. {
  2615. BT_HDR *p_buf;
  2616. UINT8 *p;
  2617. tL2C_LCB *p_lcb = NULL;
  2618. UINT16 mtu;
  2619. UINT16 mps;
  2620. UINT16 initial_credit;
  2621. if (!p_ccb) {
  2622. return;
  2623. }
  2624. p_lcb = p_ccb->p_lcb;
  2625. /* Create an identifier for this packet */
  2626. p_ccb->p_lcb->id++;
  2627. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  2628. p_ccb->local_id = p_ccb->p_lcb->id;
  2629. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ_LEN,
  2630. L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ, p_lcb->id)) == NULL )
  2631. {
  2632. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_conn_req - no buffer");
  2633. return;
  2634. }
  2635. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2636. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2637. mtu = p_ccb->local_conn_cfg.mtu;
  2638. mps = p_ccb->local_conn_cfg.mps;
  2639. initial_credit = p_ccb->local_conn_cfg.credits;
  2640. L2CAP_TRACE_DEBUG ("l2cu_send_peer_ble_credit_based_conn_req PSM:0x%04x local_cid:%d\
  2641. mtu:%d mps:%d initial_credit:%d", p_ccb->p_rcb->real_psm,\
  2642. p_ccb->local_cid, mtu, mps, initial_credit);
  2643. UINT16_TO_STREAM (p, p_ccb->p_rcb->real_psm);
  2644. UINT16_TO_STREAM (p, p_ccb->local_cid);
  2645. UINT16_TO_STREAM (p, mtu);
  2646. UINT16_TO_STREAM (p, mps);
  2647. UINT16_TO_STREAM (p, initial_credit);
  2648. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2649. }
  2650. /*******************************************************************************
  2651. **
  2652. ** Function l2cu_reject_ble_connection
  2653. **
  2654. ** Description Build and send an L2CAP "Credit based connection res" message
  2655. ** to the peer. This function is called for non-success cases.
  2656. **
  2657. ** Returns void
  2658. **
  2659. *******************************************************************************/
  2660. void l2cu_reject_ble_connection (tL2C_LCB *p_lcb, UINT8 rem_id, UINT16 result)
  2661. {
  2662. BT_HDR *p_buf;
  2663. UINT8 *p;
  2664. if ((p_buf = l2cu_build_header(p_lcb, L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES_LEN,
  2665. L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES, rem_id)) == NULL )
  2666. {
  2667. L2CAP_TRACE_WARNING ("l2cu_reject_ble_connection - no buffer");
  2668. return;
  2669. }
  2670. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2671. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2672. UINT16_TO_STREAM (p, 0); /* Local CID of 0 */
  2673. UINT16_TO_STREAM (p, 0); /* MTU */
  2674. UINT16_TO_STREAM (p, 0); /* MPS */
  2675. UINT16_TO_STREAM (p, 0); /* initial credit */
  2676. UINT16_TO_STREAM (p, result);
  2677. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2678. }
  2679. /*******************************************************************************
  2680. **
  2681. ** Function l2cu_send_peer_ble_credit_based_conn_res
  2682. **
  2683. ** Description Build and send an L2CAP "Credit based connection res" message
  2684. ** to the peer. This function is called in case of success.
  2685. **
  2686. ** Returns void
  2687. **
  2688. *******************************************************************************/
  2689. void l2cu_send_peer_ble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
  2690. {
  2691. BT_HDR *p_buf;
  2692. UINT8 *p;
  2693. L2CAP_TRACE_DEBUG ("l2cu_send_peer_ble_credit_based_conn_res");
  2694. if ((p_buf = l2cu_build_header(p_ccb->p_lcb, L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES_LEN,
  2695. L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES, p_ccb->remote_id)) == NULL )
  2696. {
  2697. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_conn_res - no buffer");
  2698. return;
  2699. }
  2700. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2701. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2702. UINT16_TO_STREAM (p, p_ccb->local_cid); /* Local CID */
  2703. UINT16_TO_STREAM (p, p_ccb->local_conn_cfg.mtu); /* MTU */
  2704. UINT16_TO_STREAM (p, p_ccb->local_conn_cfg.mps); /* MPS */
  2705. UINT16_TO_STREAM (p, p_ccb->local_conn_cfg.credits); /* initial credit */
  2706. UINT16_TO_STREAM (p, result);
  2707. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  2708. }
  2709. /*******************************************************************************
  2710. **
  2711. ** Function l2cu_send_peer_ble_flow_control_credit
  2712. **
  2713. ** Description Build and send a BLE packet to give credits to peer device
  2714. ** for LE connection oriented L2CAP channel.
  2715. **
  2716. ** Returns void
  2717. **
  2718. *******************************************************************************/
  2719. void l2cu_send_peer_ble_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
  2720. {
  2721. BT_HDR *p_buf;
  2722. UINT8 *p;
  2723. tL2C_LCB *p_lcb = NULL;
  2724. if (!p_ccb) {
  2725. return;
  2726. }
  2727. p_lcb = p_ccb->p_lcb;
  2728. /* Create an identifier for this packet */
  2729. p_ccb->p_lcb->id++;
  2730. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  2731. p_ccb->local_id = p_ccb->p_lcb->id;
  2732. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_FLOW_CTRL_CREDIT_LEN,
  2733. L2CAP_CMD_BLE_FLOW_CTRL_CREDIT, p_lcb->id)) == NULL )
  2734. {
  2735. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_conn_req - no buffer");
  2736. return;
  2737. }
  2738. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2739. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2740. UINT16_TO_STREAM (p, p_ccb->local_cid);
  2741. UINT16_TO_STREAM (p, credit_value);
  2742. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2743. }
  2744. /*******************************************************************************
  2745. **
  2746. ** Function l2cu_send_peer_ble_credit_based_conn_req
  2747. **
  2748. ** Description Build and send a BLE packet to disconnect LE connection oriented
  2749. ** L2CAP channel.
  2750. **
  2751. ** Returns void
  2752. **
  2753. *******************************************************************************/
  2754. void l2cu_send_peer_ble_credit_based_disconn_req(tL2C_CCB *p_ccb)
  2755. {
  2756. BT_HDR *p_buf;
  2757. UINT8 *p;
  2758. tL2C_LCB *p_lcb = NULL;
  2759. L2CAP_TRACE_DEBUG ("%s",__func__);
  2760. if (!p_ccb) {
  2761. return;
  2762. }
  2763. p_lcb = p_ccb->p_lcb;
  2764. /* Create an identifier for this packet */
  2765. p_ccb->p_lcb->id++;
  2766. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  2767. p_ccb->local_id = p_ccb->p_lcb->id;
  2768. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_DISC_REQ_LEN,
  2769. L2CAP_CMD_DISC_REQ, p_lcb->id)) == NULL )
  2770. {
  2771. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_disconn_req - no buffer");
  2772. return;
  2773. }
  2774. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2775. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2776. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  2777. UINT16_TO_STREAM (p,p_ccb->local_cid);
  2778. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2779. }
  2780. #endif /* BLE_INCLUDED == TRUE */
  2781. /*******************************************************************************
  2782. ** Functions used by both Full and Light Stack
  2783. ********************************************************************************/
  2784. /*******************************************************************************
  2785. **
  2786. ** Function l2cu_find_lcb_by_handle
  2787. **
  2788. ** Description Look through all active LCBs for a match based on the
  2789. ** HCI handle.
  2790. **
  2791. ** Returns pointer to matched LCB, or NULL if no match
  2792. **
  2793. *******************************************************************************/
  2794. tL2C_LCB *l2cu_find_lcb_by_handle (UINT16 handle)
  2795. {
  2796. list_node_t *p_node = NULL;
  2797. tL2C_LCB *p_lcb = NULL;
  2798. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2799. p_lcb = list_node(p_node);
  2800. if ((p_lcb->in_use) && (p_lcb->handle == handle)) {
  2801. return (p_lcb);
  2802. }
  2803. }
  2804. /* If here, no match found */
  2805. return (NULL);
  2806. }
  2807. /*******************************************************************************
  2808. **
  2809. ** Function l2cu_find_ccb_by_cid
  2810. **
  2811. ** Description Look through all active CCBs on a link for a match based
  2812. ** on the local CID. If passed the link pointer is NULL, all
  2813. ** active links are searched.
  2814. **
  2815. ** Returns pointer to matched CCB, or NULL if no match
  2816. **
  2817. *******************************************************************************/
  2818. bool l2cu_find_ccb_in_list(void *p_ccb_node, void *p_local_cid)
  2819. {
  2820. tL2C_CCB *p_ccb = (tL2C_CCB *)p_ccb_node;
  2821. uint8_t local_cid = *((uint8_t *)p_local_cid);
  2822. if (p_ccb->local_cid == local_cid && p_ccb->in_use) {
  2823. return FALSE;
  2824. }
  2825. return TRUE;
  2826. }
  2827. tL2C_CCB *l2cu_find_ccb_by_cid (tL2C_LCB *p_lcb, UINT16 local_cid)
  2828. {
  2829. tL2C_CCB *p_ccb = NULL;
  2830. #if (L2CAP_UCD_INCLUDED == FALSE)
  2831. if (local_cid < L2CAP_BASE_APPL_CID) {
  2832. return NULL;
  2833. }
  2834. #endif //(L2CAP_UCD_INCLUDED == FALSE)
  2835. list_node_t *p_node = NULL;
  2836. p_node = (list_foreach(l2cb.p_ccb_pool, l2cu_find_ccb_in_list, &local_cid));
  2837. if (p_node) {
  2838. p_ccb = (tL2C_CCB *)list_node(p_node);
  2839. if (p_lcb && p_lcb != p_ccb->p_lcb) {
  2840. p_ccb = NULL;
  2841. }
  2842. }
  2843. return (p_ccb);
  2844. }
  2845. tL2C_CCB *l2cu_find_free_ccb (void)
  2846. {
  2847. tL2C_CCB *p_ccb = NULL;
  2848. list_node_t *p_node = NULL;
  2849. for (p_node = list_begin(l2cb.p_ccb_pool); p_node; p_node = list_next(p_node))
  2850. {
  2851. p_ccb = list_node(p_node);
  2852. if(p_ccb && !p_ccb->in_use ) {
  2853. return p_ccb;
  2854. }
  2855. }
  2856. return (NULL);
  2857. }
  2858. #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE && CLASSIC_BT_INCLUDED == TRUE)
  2859. /******************************************************************************
  2860. **
  2861. ** Function l2cu_get_next_channel_in_rr
  2862. **
  2863. ** Description get the next channel to send on a link. It also adjusts the
  2864. ** CCB queue to do a basic priority and round-robin scheduling.
  2865. **
  2866. ** Returns pointer to CCB or NULL
  2867. **
  2868. *******************************************************************************/
  2869. static tL2C_CCB *l2cu_get_next_channel_in_rr(tL2C_LCB *p_lcb)
  2870. {
  2871. tL2C_CCB *p_serve_ccb = NULL;
  2872. tL2C_CCB *p_ccb;
  2873. int i, j;
  2874. /* scan all of priority until finding a channel to serve */
  2875. for ( i = 0; (i < L2CAP_NUM_CHNL_PRIORITY) && (!p_serve_ccb); i++ ) {
  2876. /* scan all channel within serving priority group until finding a channel to serve */
  2877. for ( j = 0; (j < p_lcb->rr_serv[p_lcb->rr_pri].num_ccb) && (!p_serve_ccb); j++) {
  2878. /* scaning from next serving channel */
  2879. p_ccb = p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb;
  2880. if (!p_ccb) {
  2881. L2CAP_TRACE_ERROR("p_serve_ccb is NULL, rr_pri=%d", p_lcb->rr_pri);
  2882. return NULL;
  2883. }
  2884. L2CAP_TRACE_DEBUG("RR scan pri=%d, lcid=0x%04x, q_cout=%d",
  2885. p_ccb->ccb_priority, p_ccb->local_cid,
  2886. fixed_queue_length(p_ccb->xmit_hold_q));
  2887. /* store the next serving channel */
  2888. /* this channel is the last channel of its priority group */
  2889. if (( p_ccb->p_next_ccb == NULL )
  2890. || ( p_ccb->p_next_ccb->ccb_priority != p_ccb->ccb_priority )) {
  2891. /* next serving channel is set to the first channel in the group */
  2892. p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb = p_lcb->rr_serv[p_lcb->rr_pri].p_first_ccb;
  2893. } else {
  2894. /* next serving channel is set to the next channel in the group */
  2895. p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb = p_ccb->p_next_ccb;
  2896. }
  2897. if (p_ccb->chnl_state != CST_OPEN) {
  2898. continue;
  2899. }
  2900. /* eL2CAP option in use */
  2901. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  2902. if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy) {
  2903. continue;
  2904. }
  2905. if (fixed_queue_is_empty(p_ccb->fcrb.retrans_q)) {
  2906. if (fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  2907. continue;
  2908. }
  2909. #if (CLASSIC_BT_INCLUDED == TRUE)
  2910. /* If in eRTM mode, check for window closure */
  2911. if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) ) {
  2912. continue;
  2913. }
  2914. #endif ///CLASSIC_BT_INCLUDED == TRUE
  2915. }
  2916. } else {
  2917. if (fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  2918. continue;
  2919. }
  2920. }
  2921. /* found a channel to serve */
  2922. p_serve_ccb = p_ccb;
  2923. /* decrease quota of its priority group */
  2924. p_lcb->rr_serv[p_lcb->rr_pri].quota--;
  2925. }
  2926. /* if there is no more quota of the priority group or no channel to have data to send */
  2927. if ((p_lcb->rr_serv[p_lcb->rr_pri].quota == 0) || (!p_serve_ccb)) {
  2928. /* serve next priority group */
  2929. p_lcb->rr_pri = (p_lcb->rr_pri + 1) % L2CAP_NUM_CHNL_PRIORITY;
  2930. /* initialize its quota */
  2931. p_lcb->rr_serv[p_lcb->rr_pri].quota = L2CAP_GET_PRIORITY_QUOTA(p_lcb->rr_pri);
  2932. }
  2933. }
  2934. if (p_serve_ccb) {
  2935. L2CAP_TRACE_DEBUG("RR service pri=%d, quota=%d, lcid=0x%04x",
  2936. p_serve_ccb->ccb_priority,
  2937. p_lcb->rr_serv[p_serve_ccb->ccb_priority].quota,
  2938. p_serve_ccb->local_cid );
  2939. }
  2940. return p_serve_ccb;
  2941. }
  2942. #else /* (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) */
  2943. /******************************************************************************
  2944. **
  2945. ** Function l2cu_get_next_channel
  2946. **
  2947. ** Description get the next channel to send on a link bassed on priority
  2948. ** scheduling.
  2949. **
  2950. ** Returns pointer to CCB or NULL
  2951. **
  2952. *******************************************************************************/
  2953. #if (CLASSIC_BT_INCLUDED == TRUE)
  2954. static tL2C_CCB *l2cu_get_next_channel(tL2C_LCB *p_lcb)
  2955. {
  2956. tL2C_CCB *p_ccb;
  2957. /* Get the first CCB with data to send.
  2958. */
  2959. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  2960. if (p_ccb->chnl_state != CST_OPEN) {
  2961. continue;
  2962. }
  2963. if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy) {
  2964. continue;
  2965. }
  2966. if (!fixed_queue_is_empty(p_ccb->fcrb.retrans_q))
  2967. return p_ccb;
  2968. }
  2969. if (fixed_queue_is_empty(p_ccb->xmit_hold_q))
  2970. continue;
  2971. }
  2972. /* If in eRTM mode, check for window closure */
  2973. if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) ) {
  2974. continue;
  2975. }
  2976. /* If here, we found someone */
  2977. return p_ccb;
  2978. }
  2979. return NULL;
  2980. }
  2981. #endif ///CLASSIC_BT_INCLUDED == TRUE
  2982. #endif /* (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) */
  2983. /******************************************************************************
  2984. **
  2985. ** Function l2cu_get_next_buffer_to_send
  2986. **
  2987. ** Description get the next buffer to send on a link. It also adjusts the
  2988. ** CCB queue to do a basic priority and round-robin scheduling.
  2989. **
  2990. ** Returns pointer to buffer or NULL
  2991. **
  2992. *******************************************************************************/
  2993. BT_HDR *l2cu_get_next_buffer_to_send (tL2C_LCB *p_lcb)
  2994. {
  2995. tL2C_CCB *p_ccb;
  2996. BT_HDR *p_buf = NULL;
  2997. /* Highest priority are fixed channels */
  2998. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2999. int xx;
  3000. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  3001. if ((p_ccb = p_lcb->p_fixed_ccbs[xx]) == NULL) {
  3002. continue;
  3003. }
  3004. /* eL2CAP option in use */
  3005. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  3006. #if (CLASSIC_BT_INCLUDED == TRUE)
  3007. if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy) {
  3008. continue;
  3009. }
  3010. /* No more checks needed if sending from the reatransmit queue */
  3011. if (fixed_queue_is_empty(p_ccb->fcrb.retrans_q))
  3012. {
  3013. if (fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  3014. continue;
  3015. }
  3016. /* If in eRTM mode, check for window closure */
  3017. if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) ) {
  3018. continue;
  3019. }
  3020. }
  3021. if ((p_buf = l2c_fcr_get_next_xmit_sdu_seg(p_ccb, 0)) != NULL) {
  3022. l2cu_check_channel_congestion (p_ccb);
  3023. l2cu_set_acl_hci_header (p_buf, p_ccb);
  3024. return (p_buf);
  3025. }
  3026. #else
  3027. continue;
  3028. #endif ///CLASSIC_BT_INCLUDED == TRUE
  3029. } else {
  3030. if (!fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  3031. p_buf = (BT_HDR *)fixed_queue_dequeue(p_ccb->xmit_hold_q, 0);
  3032. if (NULL == p_buf) {
  3033. L2CAP_TRACE_ERROR("l2cu_get_buffer_to_send: No data to be sent");
  3034. return (NULL);
  3035. }
  3036. l2cu_check_channel_congestion (p_ccb);
  3037. l2cu_set_acl_hci_header (p_buf, p_ccb);
  3038. /* send tx complete */
  3039. if (l2cb.fixed_reg[xx].pL2CA_FixedTxComplete_Cb) {
  3040. (*l2cb.fixed_reg[xx].pL2CA_FixedTxComplete_Cb)(p_ccb->local_cid, 1);
  3041. }
  3042. return (p_buf);
  3043. }
  3044. }
  3045. }
  3046. #endif
  3047. #if (CLASSIC_BT_INCLUDED == TRUE)
  3048. #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
  3049. /* get next serving channel in round-robin */
  3050. p_ccb = l2cu_get_next_channel_in_rr( p_lcb );
  3051. #else
  3052. p_ccb = l2cu_get_next_channel( p_lcb );
  3053. #endif
  3054. /* Return if no buffer */
  3055. if (p_ccb == NULL) {
  3056. return (NULL);
  3057. }
  3058. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  3059. if ((p_buf = l2c_fcr_get_next_xmit_sdu_seg(p_ccb, 0)) == NULL) {
  3060. return (NULL);
  3061. }
  3062. } else {
  3063. p_buf = (BT_HDR *)fixed_queue_dequeue(p_ccb->xmit_hold_q, 0);
  3064. if (NULL == p_buf) {
  3065. L2CAP_TRACE_ERROR("l2cu_get_buffer_to_send() #2: No data to be sent");
  3066. return (NULL);
  3067. }
  3068. }
  3069. if ( p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_TxComplete_Cb && (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) ) {
  3070. (*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, 1);
  3071. }
  3072. l2cu_check_channel_congestion (p_ccb);
  3073. l2cu_set_acl_hci_header (p_buf, p_ccb);
  3074. #endif ///CLASSIC_BT_INCLUDED == TRUE
  3075. return (p_buf);
  3076. }
  3077. /******************************************************************************
  3078. **
  3079. ** Function l2cu_set_acl_hci_header
  3080. **
  3081. ** Description Set HCI handle for ACL packet
  3082. **
  3083. ** Returns None
  3084. **
  3085. *******************************************************************************/
  3086. void l2cu_set_acl_hci_header (BT_HDR *p_buf, tL2C_CCB *p_ccb)
  3087. {
  3088. UINT8 *p;
  3089. /* Set the pointer to the beginning of the data minus 4 bytes for the packet header */
  3090. p = (UINT8 *)(p_buf + 1) + p_buf->offset - HCI_DATA_PREAMBLE_SIZE;
  3091. #if (BLE_INCLUDED == TRUE)
  3092. if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE) {
  3093. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT));
  3094. uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_ble();
  3095. /* The HCI transport will segment the buffers. */
  3096. if (p_buf->len > acl_data_size) {
  3097. UINT16_TO_STREAM (p, acl_data_size);
  3098. } else {
  3099. UINT16_TO_STREAM (p, p_buf->len);
  3100. }
  3101. } /* (BLE_INCLUDED == TRUE) */
  3102. else
  3103. #endif
  3104. {
  3105. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  3106. if ( (((p_buf->layer_specific & L2CAP_FLUSHABLE_MASK) == L2CAP_FLUSHABLE_CH_BASED) && (p_ccb->is_flushable))
  3107. || ((p_buf->layer_specific & L2CAP_FLUSHABLE_MASK) == L2CAP_FLUSHABLE_PKT) ) {
  3108. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT));
  3109. } else {
  3110. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | l2cb.non_flushable_pbf);
  3111. }
  3112. #else
  3113. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT));
  3114. #endif
  3115. uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
  3116. /* The HCI transport will segment the buffers. */
  3117. if (p_buf->len > acl_data_size) {
  3118. UINT16_TO_STREAM (p, acl_data_size);
  3119. } else {
  3120. UINT16_TO_STREAM (p, p_buf->len);
  3121. }
  3122. }
  3123. p_buf->offset -= HCI_DATA_PREAMBLE_SIZE;
  3124. p_buf->len += HCI_DATA_PREAMBLE_SIZE;
  3125. }
  3126. /******************************************************************************
  3127. **
  3128. ** Function l2cu_check_channel_congestion
  3129. **
  3130. ** Description check if any change in congestion status
  3131. **
  3132. ** Returns None
  3133. **
  3134. *******************************************************************************/
  3135. void l2cu_check_channel_congestion (tL2C_CCB *p_ccb)
  3136. {
  3137. size_t q_count = fixed_queue_length(p_ccb->xmit_hold_q);
  3138. #if (CLASSIC_BT_INCLUDED == TRUE)
  3139. size_t q_waiting_ack_count = fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q);
  3140. #endif
  3141. #if (L2CAP_UCD_INCLUDED == TRUE)
  3142. if ( p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID ) {
  3143. q_count += fixed_queue_length(p_ccb->p_lcb->ucd_out_sec_pending_q);
  3144. }
  3145. #endif
  3146. /* If the CCB queue limit is subject to a quota, check for congestion */
  3147. /* if this channel has outgoing traffic */
  3148. if (p_ccb->buff_quota != 0) {
  3149. /* If this channel was congested */
  3150. if ( p_ccb->cong_sent ) {
  3151. /* If the channel is not congested now, tell the app */
  3152. if (q_count <= (p_ccb->buff_quota / 2)
  3153. #if (CLASSIC_BT_INCLUDED == TRUE)
  3154. && (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE || q_waiting_ack_count < p_ccb->our_cfg.fcr.tx_win_sz)
  3155. #endif
  3156. ) {
  3157. p_ccb->cong_sent = FALSE;
  3158. if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb) {
  3159. L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (FALSE), CID: 0x%04x xmit_hold_q.count: %u buff_quota: %u",
  3160. p_ccb->local_cid, q_count, p_ccb->buff_quota);
  3161. /* Prevent recursive calling */
  3162. l2cb.is_cong_cback_context = TRUE;
  3163. (*p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)(p_ccb->local_cid, FALSE);
  3164. l2cb.is_cong_cback_context = FALSE;
  3165. }
  3166. #if (L2CAP_UCD_INCLUDED == TRUE)
  3167. else if ( p_ccb->p_rcb && p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID ) {
  3168. if ( p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb ) {
  3169. L2CAP_TRACE_DEBUG ("L2CAP - Calling UCD CongestionStatus_Cb (FALSE), SecPendingQ:%u,XmitQ:%u,Quota:%u",
  3170. fixed_queue_length(p_ccb->p_lcb->ucd_out_sec_pending_q),
  3171. fixed_queue_length(p_ccb->xmit_hold_q),
  3172. p_ccb->buff_quota);
  3173. p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb( p_ccb->p_lcb->remote_bd_addr, FALSE );
  3174. }
  3175. }
  3176. #endif
  3177. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  3178. else {
  3179. UINT8 xx;
  3180. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx ++) {
  3181. if (p_ccb->p_lcb->p_fixed_ccbs[xx] == p_ccb) {
  3182. if (l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb != NULL) {
  3183. (* l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb)(p_ccb->p_lcb->remote_bd_addr, FALSE);
  3184. }
  3185. break;
  3186. }
  3187. }
  3188. }
  3189. #endif
  3190. }
  3191. } else {
  3192. tL2C_LCB *p_lcb = p_ccb->p_lcb;
  3193. /* If this channel was not congested but it is congested now, tell the app */
  3194. if (q_count > p_ccb->buff_quota || (p_lcb && (p_lcb->link_xmit_data_q) && (list_length(p_lcb->link_xmit_data_q) + q_count) > p_ccb->buff_quota)
  3195. #if (CLASSIC_BT_INCLUDED == TRUE)
  3196. || (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE && q_waiting_ack_count >= p_ccb->our_cfg.fcr.tx_win_sz)
  3197. #endif
  3198. ) {
  3199. p_ccb->cong_sent = TRUE;
  3200. if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb) {
  3201. L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (TRUE),CID:0x%04x,XmitQ:%u,Quota:%u",
  3202. p_ccb->local_cid, q_count, p_ccb->buff_quota);
  3203. (*p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)(p_ccb->local_cid, TRUE);
  3204. }
  3205. #if (L2CAP_UCD_INCLUDED == TRUE)
  3206. else if ( p_ccb->p_rcb && p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID ) {
  3207. if ( p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb ) {
  3208. L2CAP_TRACE_DEBUG ("L2CAP - Calling UCD CongestionStatus_Cb (TRUE), SecPendingQ:%u,XmitQ:%u,Quota:%u",
  3209. fixed_queue_length(p_ccb->p_lcb->ucd_out_sec_pending_q),
  3210. fixed_queue_length(p_ccb->xmit_hold_q),
  3211. p_ccb->buff_quota);
  3212. p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb( p_ccb->p_lcb->remote_bd_addr, TRUE );
  3213. }
  3214. }
  3215. #endif
  3216. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  3217. else {
  3218. UINT8 xx;
  3219. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx ++) {
  3220. if (p_ccb->p_lcb->p_fixed_ccbs[xx] == p_ccb) {
  3221. if (l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb != NULL) {
  3222. (* l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb)(p_ccb->p_lcb->remote_bd_addr, TRUE);
  3223. }
  3224. break;
  3225. }
  3226. }
  3227. }
  3228. #endif
  3229. }
  3230. }
  3231. }
  3232. }