l2c_utils.c 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702
  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 (SDP_INCLUDED == TRUE)
  1451. if (p_rcb && (p_rcb->psm != p_rcb->real_psm)) {
  1452. btm_sec_clr_service_by_psm(p_rcb->psm);
  1453. }
  1454. #endif ///SMP_INCLUDED == TRUE
  1455. if (p_ccb->should_free_rcb) {
  1456. osi_free(p_rcb);
  1457. p_ccb->p_rcb = NULL;
  1458. p_ccb->should_free_rcb = false;
  1459. }
  1460. if (p_lcb) {
  1461. btm_sec_clr_temp_auth_service (p_lcb->remote_bd_addr);
  1462. }
  1463. /* Stop and free the timer */
  1464. btu_free_timer (&p_ccb->timer_entry);
  1465. fixed_queue_free(p_ccb->xmit_hold_q, osi_free_func);
  1466. p_ccb->xmit_hold_q = NULL;
  1467. #if (CLASSIC_BT_INCLUDED == TRUE)
  1468. fixed_queue_free(p_ccb->fcrb.srej_rcv_hold_q, osi_free_func);
  1469. fixed_queue_free(p_ccb->fcrb.retrans_q, osi_free_func);
  1470. fixed_queue_free(p_ccb->fcrb.waiting_for_ack_q, osi_free_func);
  1471. p_ccb->fcrb.srej_rcv_hold_q = NULL;
  1472. p_ccb->fcrb.retrans_q = NULL;
  1473. p_ccb->fcrb.waiting_for_ack_q = NULL;
  1474. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1475. #if (CLASSIC_BT_INCLUDED == TRUE)
  1476. l2c_fcr_cleanup (p_ccb);
  1477. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1478. /* Channel may not be assigned to any LCB if it was just pre-reserved */
  1479. if ( (p_lcb) &&
  1480. ( (p_ccb->local_cid >= L2CAP_BASE_APPL_CID)
  1481. #if (L2CAP_UCD_INCLUDED == TRUE)
  1482. || (p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID)
  1483. #endif
  1484. )
  1485. ) {
  1486. l2cu_dequeue_ccb (p_ccb);
  1487. /* Delink the CCB from the LCB */
  1488. p_ccb->p_lcb = NULL;
  1489. }
  1490. /* Flag as not in use */
  1491. p_ccb->in_use = FALSE;
  1492. /* If no channels on the connection, start idle timeout */
  1493. if ((p_lcb) && p_lcb->in_use && (p_lcb->link_state == LST_CONNECTED)) {
  1494. if (!p_lcb->ccb_queue.p_first_ccb) {
  1495. l2cu_no_dynamic_ccbs (p_lcb);
  1496. } else {
  1497. /* Link is still active, adjust channel quotas. */
  1498. l2c_link_adjust_chnl_allocation ();
  1499. }
  1500. }
  1501. }
  1502. /*******************************************************************************
  1503. **
  1504. ** Function l2cu_find_ccb_by_remote_cid
  1505. **
  1506. ** Description Look through all active CCBs on a link for a match based
  1507. ** on the remote CID.
  1508. **
  1509. ** Returns pointer to matched CCB, or NULL if no match
  1510. **
  1511. *******************************************************************************/
  1512. tL2C_CCB *l2cu_find_ccb_by_remote_cid (tL2C_LCB *p_lcb, UINT16 remote_cid)
  1513. {
  1514. tL2C_CCB *p_ccb;
  1515. /* If LCB is NULL, look through all active links */
  1516. if (!p_lcb) {
  1517. return NULL;
  1518. } else {
  1519. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
  1520. if ((p_ccb->in_use) && (p_ccb->remote_cid == remote_cid)) {
  1521. return (p_ccb);
  1522. }
  1523. }
  1524. /* If here, no match found */
  1525. return (NULL);
  1526. }
  1527. /*******************************************************************************
  1528. **
  1529. ** Function l2cu_allocate_rcb
  1530. **
  1531. ** Description Look through the Registration Control Blocks for a free
  1532. ** one.
  1533. **
  1534. ** Returns Pointer to the RCB or NULL if not found
  1535. **
  1536. *******************************************************************************/
  1537. tL2C_RCB *l2cu_allocate_rcb (UINT16 psm)
  1538. {
  1539. tL2C_RCB *p_rcb = &l2cb.rcb_pool[0];
  1540. UINT16 xx;
  1541. for (xx = 0; xx < MAX_L2CAP_CLIENTS; xx++, p_rcb++) {
  1542. if (!p_rcb->in_use) {
  1543. p_rcb->in_use = TRUE;
  1544. p_rcb->psm = psm;
  1545. #if (L2CAP_UCD_INCLUDED == TRUE)
  1546. p_rcb->ucd.state = L2C_UCD_STATE_UNUSED;
  1547. #endif
  1548. return (p_rcb);
  1549. }
  1550. }
  1551. /* If here, no free RCB found */
  1552. return (NULL);
  1553. }
  1554. #if (BLE_INCLUDED == TRUE)
  1555. /*******************************************************************************
  1556. **
  1557. ** Function l2cu_allocate_ble_rcb
  1558. **
  1559. ** Description Look through the BLE Registration Control Blocks for a free
  1560. ** one.
  1561. **
  1562. ** Returns Pointer to the BLE RCB or NULL if not found
  1563. **
  1564. *******************************************************************************/
  1565. tL2C_RCB *l2cu_allocate_ble_rcb (UINT16 psm)
  1566. {
  1567. tL2C_RCB *p_rcb = &l2cb.ble_rcb_pool[0];
  1568. UINT16 xx;
  1569. for (xx = 0; xx < BLE_MAX_L2CAP_CLIENTS; xx++, p_rcb++)
  1570. {
  1571. if (!p_rcb->in_use)
  1572. {
  1573. p_rcb->in_use = TRUE;
  1574. p_rcb->psm = psm;
  1575. #if (L2CAP_UCD_INCLUDED == TRUE)
  1576. p_rcb->ucd.state = L2C_UCD_STATE_UNUSED;
  1577. #endif
  1578. return (p_rcb);
  1579. }
  1580. }
  1581. /* If here, no free RCB found */
  1582. return (NULL);
  1583. }
  1584. #endif ///BLE_INCLUDED == TRUE
  1585. /*******************************************************************************
  1586. **
  1587. ** Function l2cu_release_rcb
  1588. **
  1589. ** Description Mark an RCB as no longet in use
  1590. **
  1591. ** Returns void
  1592. **
  1593. *******************************************************************************/
  1594. void l2cu_release_rcb (tL2C_RCB *p_rcb)
  1595. {
  1596. p_rcb->in_use = FALSE;
  1597. p_rcb->psm = 0;
  1598. }
  1599. /*******************************************************************************
  1600. **
  1601. ** Function l2cu_disconnect_chnl
  1602. **
  1603. ** Description Disconnect a channel. Typically, this is due to either
  1604. ** receiving a bad configuration, bad packet or max_retries expiring.
  1605. **
  1606. *******************************************************************************/
  1607. void l2cu_disconnect_chnl (tL2C_CCB *p_ccb)
  1608. {
  1609. UINT16 local_cid = p_ccb->local_cid;
  1610. if (local_cid >= L2CAP_BASE_APPL_CID) {
  1611. tL2CA_DISCONNECT_IND_CB *p_disc_cb = p_ccb->p_rcb->api.pL2CA_DisconnectInd_Cb;
  1612. L2CAP_TRACE_WARNING ("L2CAP - disconnect_chnl CID: 0x%04x", local_cid);
  1613. l2cu_send_peer_disc_req (p_ccb);
  1614. l2cu_release_ccb (p_ccb);
  1615. (*p_disc_cb)(local_cid, FALSE);
  1616. } else {
  1617. /* failure on the AMP channel, probably need to disconnect ACL */
  1618. L2CAP_TRACE_ERROR ("L2CAP - disconnect_chnl CID: 0x%04x Ignored", local_cid);
  1619. }
  1620. }
  1621. /*******************************************************************************
  1622. **
  1623. ** Function l2cu_find_rcb_by_psm
  1624. **
  1625. ** Description Look through the Registration Control Blocks to see if
  1626. ** anyone registered to handle the PSM in question
  1627. **
  1628. ** Returns Pointer to the RCB or NULL if not found
  1629. **
  1630. *******************************************************************************/
  1631. tL2C_RCB *l2cu_find_rcb_by_psm (UINT16 psm)
  1632. {
  1633. tL2C_RCB *p_rcb = &l2cb.rcb_pool[0];
  1634. UINT16 xx;
  1635. for (xx = 0; xx < MAX_L2CAP_CLIENTS; xx++, p_rcb++) {
  1636. if ((p_rcb->in_use) && (p_rcb->psm == psm)) {
  1637. return (p_rcb);
  1638. }
  1639. }
  1640. /* If here, no match found */
  1641. return (NULL);
  1642. }
  1643. #if (BLE_INCLUDED == TRUE)
  1644. /*******************************************************************************
  1645. **
  1646. ** Function l2cu_find_ble_rcb_by_psm
  1647. **
  1648. ** Description Look through the BLE Registration Control Blocks to see if
  1649. ** anyone registered to handle the PSM in question
  1650. **
  1651. ** Returns Pointer to the BLE RCB or NULL if not found
  1652. **
  1653. *******************************************************************************/
  1654. tL2C_RCB *l2cu_find_ble_rcb_by_psm (UINT16 psm)
  1655. {
  1656. tL2C_RCB *p_rcb = &l2cb.ble_rcb_pool[0];
  1657. UINT16 xx;
  1658. for (xx = 0; xx < BLE_MAX_L2CAP_CLIENTS; xx++, p_rcb++)
  1659. {
  1660. if ((p_rcb->in_use) && (p_rcb->psm == psm)) {
  1661. return (p_rcb);
  1662. }
  1663. }
  1664. /* If here, no match found */
  1665. return (NULL);
  1666. }
  1667. #endif ///BLE_INCLUDED == TRUE
  1668. /*******************************************************************************
  1669. **
  1670. ** Function l2cu_process_peer_cfg_req
  1671. **
  1672. ** Description This function is called when the peer sends us a "config request"
  1673. ** message. It extracts the configuration of interest and saves
  1674. ** it in the CCB.
  1675. **
  1676. ** Note: Negotiation of the FCR channel type is handled internally,
  1677. ** all others are passed to the upper layer.
  1678. **
  1679. ** Returns UINT8 - L2CAP_PEER_CFG_OK if passed to upper layer,
  1680. ** L2CAP_PEER_CFG_UNACCEPTABLE if automatically responded to
  1681. ** because parameters are unnacceptable from a specification
  1682. ** point of view.
  1683. ** L2CAP_PEER_CFG_DISCONNECT if no compatible channel modes
  1684. ** between the two devices, and shall be closed.
  1685. **
  1686. *******************************************************************************/
  1687. UINT8 l2cu_process_peer_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1688. {
  1689. BOOLEAN mtu_ok = TRUE;
  1690. BOOLEAN qos_type_ok = TRUE;
  1691. BOOLEAN flush_to_ok = TRUE;
  1692. BOOLEAN fcr_ok = TRUE;
  1693. #if (CLASSIC_BT_INCLUDED == TRUE)
  1694. UINT8 fcr_status;
  1695. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1696. /* Ignore FCR parameters for basic mode */
  1697. if (!p_cfg->fcr_present) {
  1698. p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
  1699. }
  1700. /* Save the MTU that our peer can receive */
  1701. if (p_cfg->mtu_present) {
  1702. /* Make sure MTU is at least the minimum */
  1703. if (p_cfg->mtu >= L2CAP_MIN_MTU) {
  1704. /* In basic mode, limit the MTU to our buffer size */
  1705. if ( (p_cfg->fcr_present == FALSE) && (p_cfg->mtu > L2CAP_MTU_SIZE) ) {
  1706. p_cfg->mtu = L2CAP_MTU_SIZE;
  1707. }
  1708. /* Save the accepted value in case of renegotiation */
  1709. p_ccb->peer_cfg.mtu = p_cfg->mtu;
  1710. p_ccb->peer_cfg.mtu_present = TRUE;
  1711. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_MTU;
  1712. } else { /* Illegal MTU value */
  1713. p_cfg->mtu = L2CAP_MIN_MTU;
  1714. mtu_ok = FALSE;
  1715. }
  1716. }
  1717. /* Reload mtu from a previously accepted config request */
  1718. else if (p_ccb->peer_cfg.mtu_present) {
  1719. p_cfg->mtu_present = TRUE;
  1720. p_cfg->mtu = p_ccb->peer_cfg.mtu;
  1721. }
  1722. /* Verify that the flush timeout is a valid value (0 is illegal) */
  1723. if (p_cfg->flush_to_present) {
  1724. if (!p_cfg->flush_to) {
  1725. p_cfg->flush_to = 0xFFFF; /* Infinite retransmissions (spec default) */
  1726. flush_to_ok = FALSE;
  1727. } else { /* Save the accepted value in case of renegotiation */
  1728. p_ccb->peer_cfg.flush_to_present = TRUE;
  1729. p_ccb->peer_cfg.flush_to = p_cfg->flush_to;
  1730. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_FLUSH_TO;
  1731. }
  1732. }
  1733. /* Reload flush_to from a previously accepted config request */
  1734. else if (p_ccb->peer_cfg.flush_to_present) {
  1735. p_cfg->flush_to_present = TRUE;
  1736. p_cfg->flush_to = p_ccb->peer_cfg.flush_to;
  1737. }
  1738. /* Save the QOS settings the the peer is using */
  1739. if (p_cfg->qos_present) {
  1740. /* Make sure service type is not a reserved value; otherwise let upper
  1741. layer decide if acceptable
  1742. */
  1743. if (p_cfg->qos.service_type <= GUARANTEED) {
  1744. p_ccb->peer_cfg.qos = p_cfg->qos;
  1745. p_ccb->peer_cfg.qos_present = TRUE;
  1746. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_QOS;
  1747. } else { /* Illegal service type value */
  1748. p_cfg->qos.service_type = BEST_EFFORT;
  1749. qos_type_ok = FALSE;
  1750. }
  1751. }
  1752. /* Reload QOS from a previously accepted config request */
  1753. else if (p_ccb->peer_cfg.qos_present) {
  1754. p_cfg->qos_present = TRUE;
  1755. p_cfg->qos = p_ccb->peer_cfg.qos;
  1756. }
  1757. #if (CLASSIC_BT_INCLUDED == TRUE)
  1758. if ((fcr_status = l2c_fcr_process_peer_cfg_req (p_ccb, p_cfg)) == L2CAP_PEER_CFG_DISCONNECT) {
  1759. /* Notify caller to disconnect the channel (incompatible modes) */
  1760. p_cfg->result = L2CAP_CFG_FAILED_NO_REASON;
  1761. p_cfg->mtu_present = p_cfg->qos_present = p_cfg->flush_to_present = 0;
  1762. return (L2CAP_PEER_CFG_DISCONNECT);
  1763. }
  1764. fcr_ok = (fcr_status == L2CAP_PEER_CFG_OK);
  1765. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1766. /* Return any unacceptable parameters */
  1767. if (mtu_ok && flush_to_ok && qos_type_ok && fcr_ok) {
  1768. l2cu_adjust_out_mps (p_ccb);
  1769. return (L2CAP_PEER_CFG_OK);
  1770. } else {
  1771. p_cfg->result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
  1772. if (mtu_ok) {
  1773. p_cfg->mtu_present = FALSE;
  1774. }
  1775. if (flush_to_ok) {
  1776. p_cfg->flush_to_present = FALSE;
  1777. }
  1778. if (qos_type_ok) {
  1779. p_cfg->qos_present = FALSE;
  1780. }
  1781. if (fcr_ok) {
  1782. p_cfg->fcr_present = FALSE;
  1783. }
  1784. return (L2CAP_PEER_CFG_UNACCEPTABLE);
  1785. }
  1786. }
  1787. /*******************************************************************************
  1788. **
  1789. ** Function l2cu_process_peer_cfg_rsp
  1790. **
  1791. ** Description This function is called when the peer sends us a "config response"
  1792. ** message. It extracts the configuration of interest and saves
  1793. ** it in the CCB.
  1794. **
  1795. ** Returns void
  1796. **
  1797. *******************************************************************************/
  1798. void l2cu_process_peer_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1799. {
  1800. /* If we wanted QoS and the peer sends us a positive response with QoS, use his values */
  1801. if ( (p_cfg->qos_present) && (p_ccb->our_cfg.qos_present) ) {
  1802. p_ccb->our_cfg.qos = p_cfg->qos;
  1803. }
  1804. if (p_cfg->fcr_present) {
  1805. /* Save the retransmission and monitor timeout values */
  1806. if (p_cfg->fcr.mode == L2CAP_FCR_ERTM_MODE) {
  1807. p_ccb->peer_cfg.fcr.rtrans_tout = p_cfg->fcr.rtrans_tout;
  1808. p_ccb->peer_cfg.fcr.mon_tout = p_cfg->fcr.mon_tout;
  1809. }
  1810. /* Calculate the max number of packets for which we can delay sending an ack */
  1811. if (p_cfg->fcr.tx_win_sz < p_ccb->our_cfg.fcr.tx_win_sz) {
  1812. p_ccb->fcrb.max_held_acks = p_cfg->fcr.tx_win_sz / 3;
  1813. } else {
  1814. p_ccb->fcrb.max_held_acks = p_ccb->our_cfg.fcr.tx_win_sz / 3;
  1815. }
  1816. L2CAP_TRACE_DEBUG ("l2cu_process_peer_cfg_rsp(): peer tx_win_sz: %d, our tx_win_sz: %d, max_held_acks: %d",
  1817. p_cfg->fcr.tx_win_sz, p_ccb->our_cfg.fcr.tx_win_sz, p_ccb->fcrb.max_held_acks);
  1818. }
  1819. }
  1820. /*******************************************************************************
  1821. **
  1822. ** Function l2cu_process_our_cfg_req
  1823. **
  1824. ** Description This function is called when we send a "config request"
  1825. ** message. It extracts the configuration of interest and saves
  1826. ** it in the CCB.
  1827. **
  1828. ** Returns void
  1829. **
  1830. *******************************************************************************/
  1831. void l2cu_process_our_cfg_req (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1832. {
  1833. tL2C_LCB *p_lcb;
  1834. UINT16 hci_flush_to;
  1835. /* Save the QOS settings we are using for transmit */
  1836. if (p_cfg->qos_present) {
  1837. p_ccb->our_cfg.qos_present = TRUE;
  1838. p_ccb->our_cfg.qos = p_cfg->qos;
  1839. }
  1840. if (p_cfg->fcr_present) {
  1841. /* Override FCR options if attempting streaming or basic */
  1842. if (p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE) {
  1843. memset(&p_cfg->fcr, 0, sizeof(tL2CAP_FCR_OPTS));
  1844. } else {
  1845. /* On BR/EDR, timer values are zero in config request */
  1846. /* On class 2 AMP, timer value in config request shall be non-0 processing time */
  1847. /* timer value in config response shall be greater than received processing time */
  1848. p_cfg->fcr.mon_tout = p_cfg->fcr.rtrans_tout = 0;
  1849. if (p_cfg->fcr.mode == L2CAP_FCR_STREAM_MODE) {
  1850. p_cfg->fcr.max_transmit = p_cfg->fcr.tx_win_sz = 0;
  1851. }
  1852. }
  1853. /* Set the threshold to send acks (may be updated in the cfg response) */
  1854. p_ccb->fcrb.max_held_acks = p_cfg->fcr.tx_win_sz / 3;
  1855. /* Include FCS option only if peer can handle it */
  1856. if (p_ccb->p_lcb->peer_ext_fea & L2CAP_EXTFEA_NO_CRC) {
  1857. /* FCS check can be bypassed if peer also desires to bypass */
  1858. if (p_cfg->fcs_present && p_cfg->fcs == L2CAP_CFG_FCS_BYPASS) {
  1859. p_ccb->bypass_fcs |= L2CAP_CFG_FCS_OUR;
  1860. }
  1861. } else {
  1862. p_cfg->fcs_present = FALSE;
  1863. }
  1864. } else {
  1865. p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
  1866. }
  1867. p_ccb->our_cfg.fcr.mode = p_cfg->fcr.mode;
  1868. p_ccb->our_cfg.fcr_present = p_cfg->fcr_present;
  1869. /* Check the flush timeout. If it is lower than the current one used */
  1870. /* then we need to adjust the flush timeout sent to the controller */
  1871. if (p_cfg->flush_to_present) {
  1872. if ((p_cfg->flush_to == 0) || (p_cfg->flush_to == L2CAP_NO_AUTOMATIC_FLUSH)) {
  1873. /* don't send invalid flush timeout */
  1874. /* SPEC: The sender of the Request shall specify its flush timeout value */
  1875. /* if it differs from the default value of 0xFFFF */
  1876. p_cfg->flush_to_present = FALSE;
  1877. } else {
  1878. p_ccb->our_cfg.flush_to = p_cfg->flush_to;
  1879. p_lcb = p_ccb->p_lcb;
  1880. if (p_cfg->flush_to < p_lcb->link_flush_tout) {
  1881. p_lcb->link_flush_tout = p_cfg->flush_to;
  1882. /* If the timeout is within range of HCI, set the flush timeout */
  1883. if (p_cfg->flush_to <= ((HCI_MAX_AUTO_FLUSH_TOUT * 5) / 8)) {
  1884. /* Convert flush timeout to 0.625 ms units, with round */
  1885. hci_flush_to = ((p_cfg->flush_to * 8) + 3) / 5;
  1886. btsnd_hcic_write_auto_flush_tout (p_lcb->handle, hci_flush_to);
  1887. }
  1888. }
  1889. }
  1890. }
  1891. }
  1892. /*******************************************************************************
  1893. **
  1894. ** Function l2cu_process_our_cfg_rsp
  1895. **
  1896. ** Description This function is called when we send the peer a "config response"
  1897. ** message. It extracts the configuration of interest and saves
  1898. ** it in the CCB.
  1899. **
  1900. ** Returns void
  1901. **
  1902. *******************************************************************************/
  1903. #if (CLASSIC_BT_INCLUDED == TRUE)
  1904. void l2cu_process_our_cfg_rsp (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1905. {
  1906. /* If peer wants QoS, we are allowed to change the values in a positive response */
  1907. if ( (p_cfg->qos_present) && (p_ccb->peer_cfg.qos_present) ) {
  1908. p_ccb->peer_cfg.qos = p_cfg->qos;
  1909. } else {
  1910. p_cfg->qos_present = FALSE;
  1911. }
  1912. l2c_fcr_adj_our_rsp_options (p_ccb, p_cfg);
  1913. }
  1914. #endif ///CLASSIC_BT_INCLUDED == TRUE
  1915. /*******************************************************************************
  1916. **
  1917. ** Function l2cu_device_reset
  1918. **
  1919. ** Description This function is called when reset of the device is
  1920. ** completed. For all active connection simulate HCI_DISC
  1921. **
  1922. ** Returns void
  1923. **
  1924. *******************************************************************************/
  1925. void l2cu_device_reset (void)
  1926. {
  1927. list_node_t *p_node = NULL;
  1928. tL2C_LCB *p_lcb = NULL;
  1929. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  1930. p_lcb = list_node(p_node);
  1931. if ((p_lcb->in_use) && (p_lcb->handle != HCI_INVALID_HANDLE)) {
  1932. l2c_link_hci_disc_comp (p_lcb->handle, (UINT8) - 1);
  1933. }
  1934. }
  1935. #if (BLE_INCLUDED == TRUE)
  1936. l2cb.is_ble_connecting = FALSE;
  1937. #endif
  1938. }
  1939. /*******************************************************************************
  1940. **
  1941. ** Function l2cu_create_conn
  1942. **
  1943. ** Description This function initiates an acl connection via HCI
  1944. **
  1945. ** Returns TRUE if successful, FALSE if gki get buffer fails.
  1946. **
  1947. *******************************************************************************/
  1948. BOOLEAN l2cu_create_conn (tL2C_LCB *p_lcb, tBT_TRANSPORT transport)
  1949. {
  1950. #if BTM_SCO_INCLUDED == TRUE
  1951. BOOLEAN is_sco_active;
  1952. #endif
  1953. list_node_t *p_node = NULL;
  1954. tL2C_LCB *p_lcb_cur = NULL;
  1955. #if (BLE_INCLUDED == TRUE)
  1956. tBT_DEVICE_TYPE dev_type;
  1957. tBLE_ADDR_TYPE addr_type = p_lcb->open_addr_type;
  1958. if(addr_type == BLE_ADDR_UNKNOWN_TYPE) {
  1959. BTM_ReadDevInfo(p_lcb->remote_bd_addr, &dev_type, &addr_type);
  1960. }
  1961. if (transport == BT_TRANSPORT_LE) {
  1962. if (!controller_get_interface()->supports_ble()) {
  1963. return FALSE;
  1964. }
  1965. if(addr_type > BLE_ADDR_TYPE_MAX) {
  1966. addr_type = BLE_ADDR_PUBLIC;
  1967. }
  1968. p_lcb->ble_addr_type = addr_type;
  1969. p_lcb->transport = BT_TRANSPORT_LE;
  1970. return (l2cble_create_conn(p_lcb));
  1971. }
  1972. #endif
  1973. /* If there is a connection where we perform as a slave, try to switch roles
  1974. for this connection */
  1975. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  1976. p_lcb_cur = list_node(p_node);
  1977. if (p_lcb_cur == p_lcb) {
  1978. continue;
  1979. }
  1980. if ((p_lcb_cur->in_use) && (p_lcb_cur->link_role == HCI_ROLE_SLAVE)) {
  1981. #if BTM_SCO_INCLUDED == TRUE
  1982. /* The LMP_switch_req shall be sent only if the ACL logical transport
  1983. is in active mode, when encryption is disabled, and all synchronous
  1984. logical transports on the same physical link are disabled." */
  1985. /* Check if there is any SCO Active on this BD Address */
  1986. is_sco_active = btm_is_sco_active_by_bdaddr(p_lcb_cur->remote_bd_addr);
  1987. L2CAP_TRACE_API ("l2cu_create_conn - btm_is_sco_active_by_bdaddr() is_sco_active = %s", \
  1988. (is_sco_active == TRUE) ? "TRUE" : "FALSE");
  1989. if (is_sco_active == TRUE) {
  1990. continue; /* No Master Slave switch not allowed when SCO Active */
  1991. }
  1992. #endif
  1993. /*4_1_TODO check if btm_cb.devcb.local_features to be used instead */
  1994. if (HCI_SWITCH_SUPPORTED(BTM_ReadLocalFeatures())) {
  1995. /* mark this lcb waiting for switch to be completed and
  1996. start switch on the other one */
  1997. p_lcb->link_state = LST_CONNECTING_WAIT_SWITCH;
  1998. p_lcb->link_role = HCI_ROLE_MASTER;
  1999. if (BTM_SwitchRole (p_lcb_cur->remote_bd_addr, HCI_ROLE_MASTER, NULL) == BTM_CMD_STARTED) {
  2000. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_ROLE_SWITCH_TOUT);
  2001. return (TRUE);
  2002. }
  2003. }
  2004. }
  2005. }
  2006. p_lcb->link_state = LST_CONNECTING;
  2007. return (l2cu_create_conn_after_switch (p_lcb));
  2008. }
  2009. /*******************************************************************************
  2010. **
  2011. ** Function l2cu_get_num_hi_priority
  2012. **
  2013. ** Description Gets the number of high priority channels.
  2014. **
  2015. ** Returns
  2016. **
  2017. *******************************************************************************/
  2018. UINT8 l2cu_get_num_hi_priority (void)
  2019. {
  2020. UINT8 no_hi = 0;
  2021. list_node_t *p_node = NULL;
  2022. tL2C_LCB *p_lcb = NULL;
  2023. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2024. p_lcb = list_node(p_node);
  2025. if ((p_lcb->in_use) && (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)) {
  2026. no_hi++;
  2027. }
  2028. }
  2029. return no_hi;
  2030. }
  2031. /*******************************************************************************
  2032. **
  2033. ** Function l2cu_create_conn_after_switch
  2034. **
  2035. ** Description This function initiates an acl connection via HCI
  2036. ** If switch required to create connection it is already done.
  2037. **
  2038. ** Returns TRUE if successful, FALSE if osi get buffer fails.
  2039. **
  2040. *******************************************************************************/
  2041. BOOLEAN l2cu_create_conn_after_switch (tL2C_LCB *p_lcb)
  2042. {
  2043. UINT8 allow_switch = HCI_CR_CONN_ALLOW_SWITCH;
  2044. tBTM_INQ_INFO *p_inq_info;
  2045. UINT8 page_scan_rep_mode;
  2046. UINT8 page_scan_mode;
  2047. UINT16 clock_offset;
  2048. UINT8 *p_features;
  2049. UINT16 num_acl = BTM_GetNumAclLinks();
  2050. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (p_lcb->remote_bd_addr);
  2051. UINT8 no_hi_prio_chs = l2cu_get_num_hi_priority();
  2052. p_features = BTM_ReadLocalFeatures();
  2053. L2CAP_TRACE_DEBUG ("l2cu_create_conn_after_switch :%d num_acl:%d no_hi: %d is_bonding:%d",
  2054. l2cb.disallow_switch, num_acl, no_hi_prio_chs, p_lcb->is_bonding);
  2055. /* FW team says that we can participant in 4 piconets
  2056. * typically 3 piconet + 1 for scanning.
  2057. * We can enhance the code to count the number of piconets later. */
  2058. if ( ((!l2cb.disallow_switch && (num_acl < 3)) || (p_lcb->is_bonding && (no_hi_prio_chs == 0)))
  2059. && HCI_SWITCH_SUPPORTED(p_features)) {
  2060. allow_switch = HCI_CR_CONN_ALLOW_SWITCH;
  2061. } else {
  2062. allow_switch = HCI_CR_CONN_NOT_ALLOW_SWITCH;
  2063. }
  2064. p_lcb->link_state = LST_CONNECTING;
  2065. /* Check with the BT manager if details about remote device are known */
  2066. if ((p_inq_info = BTM_InqDbRead(p_lcb->remote_bd_addr)) != NULL) {
  2067. page_scan_rep_mode = p_inq_info->results.page_scan_rep_mode;
  2068. page_scan_mode = p_inq_info->results.page_scan_mode;
  2069. clock_offset = (UINT16)(p_inq_info->results.clock_offset);
  2070. } else {
  2071. /* No info known. Use default settings */
  2072. page_scan_rep_mode = HCI_PAGE_SCAN_REP_MODE_R1;
  2073. page_scan_mode = HCI_MANDATARY_PAGE_SCAN_MODE;
  2074. clock_offset = (p_dev_rec) ? p_dev_rec->clock_offset : 0;
  2075. }
  2076. if (!btsnd_hcic_create_conn (p_lcb->remote_bd_addr,
  2077. ( HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1
  2078. | HCI_PKT_TYPES_MASK_DM3 | HCI_PKT_TYPES_MASK_DH3
  2079. | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5 ),
  2080. page_scan_rep_mode,
  2081. page_scan_mode,
  2082. clock_offset,
  2083. allow_switch))
  2084. {
  2085. L2CAP_TRACE_ERROR ("L2CAP - no buffer for l2cu_create_conn");
  2086. l2cu_release_lcb (p_lcb);
  2087. return (FALSE);
  2088. }
  2089. btm_acl_update_busy_level (BTM_BLI_PAGE_EVT);
  2090. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK,
  2091. L2CAP_LINK_CONNECT_TOUT);
  2092. return (TRUE);
  2093. }
  2094. /*******************************************************************************
  2095. **
  2096. ** Function l2cu_find_lcb_by_state
  2097. **
  2098. ** Description Look through all active LCBs for a match based on the
  2099. ** LCB state.
  2100. **
  2101. ** Returns pointer to first matched LCB, or NULL if no match
  2102. **
  2103. *******************************************************************************/
  2104. tL2C_LCB *l2cu_find_lcb_by_state (tL2C_LINK_STATE state)
  2105. {
  2106. list_node_t *p_node = NULL;
  2107. tL2C_LCB *p_lcb = NULL;
  2108. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2109. p_lcb = list_node(p_node);
  2110. if ((p_lcb->in_use) && (p_lcb->link_state == state)) {
  2111. return (p_lcb);
  2112. }
  2113. }
  2114. /* If here, no match found */
  2115. return (NULL);
  2116. }
  2117. /*******************************************************************************
  2118. **
  2119. ** Function l2cu_lcb_disconnecting
  2120. **
  2121. ** Description On each active lcb, check if the lcb is in disconnecting
  2122. ** state, or if there are no ccb's on the lcb (implying
  2123. idle timeout is running), or if last ccb on the link
  2124. is in disconnecting state.
  2125. **
  2126. ** Returns TRUE if any of above conditions met, FALSE otherwise
  2127. **
  2128. *******************************************************************************/
  2129. BOOLEAN l2cu_lcb_disconnecting (void)
  2130. {
  2131. tL2C_LCB *p_lcb;
  2132. tL2C_CCB *p_ccb;
  2133. BOOLEAN status = FALSE;
  2134. list_node_t *p_node = NULL;
  2135. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2136. p_lcb = list_node(p_node);
  2137. if (p_lcb->in_use) {
  2138. /* no ccbs on lcb, or lcb is in disconnecting state */
  2139. if ((!p_lcb->ccb_queue.p_first_ccb) || (p_lcb->link_state == LST_DISCONNECTING)) {
  2140. status = TRUE;
  2141. break;
  2142. }
  2143. /* only one ccb left on lcb */
  2144. else if (p_lcb->ccb_queue.p_first_ccb == p_lcb->ccb_queue.p_last_ccb) {
  2145. p_ccb = p_lcb->ccb_queue.p_first_ccb;
  2146. if ((p_ccb->in_use) &&
  2147. ((p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP) ||
  2148. (p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP))) {
  2149. status = TRUE;
  2150. break;
  2151. }
  2152. }
  2153. }
  2154. }
  2155. return status;
  2156. }
  2157. /*******************************************************************************
  2158. **
  2159. ** Function l2cu_set_acl_priority
  2160. **
  2161. ** Description Sets the transmission priority for a channel.
  2162. ** (For initial implementation only two values are valid.
  2163. ** L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
  2164. **
  2165. ** Returns TRUE if a valid channel, else FALSE
  2166. **
  2167. *******************************************************************************/
  2168. BOOLEAN l2cu_set_acl_priority (BD_ADDR bd_addr, UINT8 priority, BOOLEAN reset_after_rs)
  2169. {
  2170. tL2C_LCB *p_lcb;
  2171. UINT8 *pp;
  2172. UINT8 command[HCI_BRCM_ACL_PRIORITY_PARAM_SIZE];
  2173. UINT8 vs_param;
  2174. //APPL_TRACE_EVENT("SET ACL PRIORITY %d", priority);
  2175. /* Find the link control block for the acl channel */
  2176. if ((p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_BR_EDR)) == NULL) {
  2177. L2CAP_TRACE_WARNING ("L2CAP - no LCB for L2CA_SetAclPriority");
  2178. return (FALSE);
  2179. }
  2180. if (BTM_IS_BRCM_CONTROLLER()) {
  2181. /* Called from above L2CAP through API; send VSC if changed */
  2182. if ((!reset_after_rs && (priority != p_lcb->acl_priority)) ||
  2183. /* Called because of a master/slave role switch; if high resend VSC */
  2184. ( reset_after_rs && p_lcb->acl_priority == L2CAP_PRIORITY_HIGH)) {
  2185. pp = command;
  2186. vs_param = (priority == L2CAP_PRIORITY_HIGH) ? HCI_BRCM_ACL_PRIORITY_HIGH : HCI_BRCM_ACL_PRIORITY_LOW;
  2187. UINT16_TO_STREAM (pp, p_lcb->handle);
  2188. UINT8_TO_STREAM (pp, vs_param);
  2189. BTM_VendorSpecificCommand (HCI_BRCM_SET_ACL_PRIORITY, HCI_BRCM_ACL_PRIORITY_PARAM_SIZE, command, NULL);
  2190. /* Adjust lmp buffer allocation for this channel if priority changed */
  2191. if (p_lcb->acl_priority != priority) {
  2192. p_lcb->acl_priority = priority;
  2193. l2c_link_adjust_allocation();
  2194. }
  2195. }
  2196. }
  2197. return (TRUE);
  2198. }
  2199. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  2200. /******************************************************************************
  2201. **
  2202. ** Function l2cu_set_non_flushable_pbf
  2203. **
  2204. ** Description set L2CAP_PKT_START_NON_FLUSHABLE if controller supoorts
  2205. **
  2206. ** Returns void
  2207. **
  2208. *******************************************************************************/
  2209. void l2cu_set_non_flushable_pbf (BOOLEAN is_supported)
  2210. {
  2211. if (is_supported) {
  2212. l2cb.non_flushable_pbf = (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT);
  2213. } else {
  2214. l2cb.non_flushable_pbf = (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
  2215. }
  2216. }
  2217. #endif
  2218. /*******************************************************************************
  2219. **
  2220. ** Function l2cu_resubmit_pending_sec_req
  2221. **
  2222. ** Description This function is called when required security procedures
  2223. ** are completed and any pending requests can be re-submitted.
  2224. **
  2225. ** Returns void
  2226. **
  2227. *******************************************************************************/
  2228. #if (CLASSIC_BT_INCLUDED == TRUE)
  2229. void l2cu_resubmit_pending_sec_req (BD_ADDR p_bda)
  2230. {
  2231. tL2C_LCB *p_lcb;
  2232. tL2C_CCB *p_ccb;
  2233. tL2C_CCB *p_next_ccb;
  2234. L2CAP_TRACE_DEBUG ("l2cu_resubmit_pending_sec_req p_bda: %p", p_bda);
  2235. list_node_t *p_node = NULL;
  2236. /* If we are called with a BDA, only resubmit for that BDA */
  2237. if (p_bda) {
  2238. p_lcb = l2cu_find_lcb_by_bd_addr (p_bda, BT_TRANSPORT_BR_EDR);
  2239. /* If we don't have one, this is an error */
  2240. if (p_lcb) {
  2241. /* For all channels, send the event through their FSMs */
  2242. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb) {
  2243. p_next_ccb = p_ccb->p_next_ccb;
  2244. l2c_csm_execute (p_ccb, L2CEVT_SEC_RE_SEND_CMD, NULL);
  2245. }
  2246. } else {
  2247. L2CAP_TRACE_WARNING ("l2cu_resubmit_pending_sec_req - unknown BD_ADDR");
  2248. }
  2249. } else {
  2250. /* No BDA pasesed in, so check all links */
  2251. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2252. p_lcb = list_node(p_node);
  2253. if (p_lcb->in_use) {
  2254. /* For all channels, send the event through their FSMs */
  2255. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_next_ccb) {
  2256. p_next_ccb = p_ccb->p_next_ccb;
  2257. l2c_csm_execute (p_ccb, L2CEVT_SEC_RE_SEND_CMD, NULL);
  2258. }
  2259. }
  2260. }
  2261. }
  2262. }
  2263. #endif ///CLASSIC_BT_INCLUDED == TRUE
  2264. #if L2CAP_CONFORMANCE_TESTING == TRUE
  2265. /*******************************************************************************
  2266. **
  2267. ** Function l2cu_set_info_rsp_mask
  2268. **
  2269. ** Description This function allows the script wrapper to change the
  2270. ** info resp mask for conformance testing.
  2271. **
  2272. ** Returns pointer to CCB, or NULL if none
  2273. **
  2274. *******************************************************************************/
  2275. void l2cu_set_info_rsp_mask (UINT32 mask)
  2276. {
  2277. l2cb.test_info_resp = mask;
  2278. }
  2279. #endif /* L2CAP_CONFORMANCE_TESTING */
  2280. /*******************************************************************************
  2281. **
  2282. ** Function l2cu_adjust_out_mps
  2283. **
  2284. ** Description Sets our MPS based on current controller capabilities
  2285. **
  2286. ** Returns void
  2287. **
  2288. *******************************************************************************/
  2289. void l2cu_adjust_out_mps (tL2C_CCB *p_ccb)
  2290. {
  2291. UINT16 packet_size;
  2292. /* on the tx side MTU is selected based on packet size of the controller */
  2293. packet_size = btm_get_max_packet_size (p_ccb->p_lcb->remote_bd_addr);
  2294. if (packet_size <= (L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + L2CAP_FCS_LEN)) {
  2295. /* something is very wrong */
  2296. L2CAP_TRACE_DEBUG ("l2cu_adjust_out_mps bad packet size: %u will use MPS: %u", packet_size, p_ccb->peer_cfg.fcr.mps);
  2297. p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps;
  2298. } else {
  2299. packet_size -= (L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD + L2CAP_SDU_LEN_OVERHEAD + L2CAP_FCS_LEN);
  2300. /* We try to negotiate MTU that each packet can be split into whole
  2301. number of max packets. For example if link is 1.2 max packet size is 339 bytes.
  2302. At first calculate how many whole packets it is. MAX L2CAP is 1691 + 4 overhead.
  2303. 1695, that will be 5 Dh5 packets. Now maximum L2CAP packet is
  2304. 5 * 339 = 1695. Minus 4 bytes L2CAP header 1691.
  2305. For EDR 2.0 packet size is 1027. So we better send RFCOMM packet as 1 3DH5 packet
  2306. 1 * 1027 = 1027. Minus 4 bytes L2CAP header 1023. */
  2307. if (p_ccb->peer_cfg.fcr.mps >= packet_size) {
  2308. p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps / packet_size * packet_size;
  2309. } else {
  2310. p_ccb->tx_mps = p_ccb->peer_cfg.fcr.mps;
  2311. }
  2312. L2CAP_TRACE_DEBUG ("l2cu_adjust_out_mps use %d Based on peer_cfg.fcr.mps: %u packet_size: %u",
  2313. p_ccb->tx_mps, p_ccb->peer_cfg.fcr.mps, packet_size);
  2314. }
  2315. }
  2316. /*******************************************************************************
  2317. **
  2318. ** Function l2cu_initialize_fixed_ccb
  2319. **
  2320. ** Description Initialize a fixed channel's CCB
  2321. **
  2322. ** Returns TRUE or FALSE
  2323. **
  2324. *******************************************************************************/
  2325. BOOLEAN l2cu_initialize_fixed_ccb (tL2C_LCB *p_lcb, UINT16 fixed_cid, tL2CAP_FCR_OPTS *p_fcr)
  2326. {
  2327. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2328. tL2C_CCB *p_ccb;
  2329. /* If we already have a CCB, then simply return */
  2330. if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] != NULL) {
  2331. return (TRUE);
  2332. }
  2333. if ((p_ccb = l2cu_allocate_ccb (NULL, 0)) == NULL) {
  2334. return (FALSE);
  2335. }
  2336. btu_stop_timer(&p_lcb->timer_entry);
  2337. /* Set CID for the connection */
  2338. p_ccb->local_cid = fixed_cid;
  2339. p_ccb->remote_cid = fixed_cid;
  2340. p_ccb->is_flushable = FALSE;
  2341. p_ccb->timer_entry.param = (TIMER_PARAM_TYPE)p_ccb;
  2342. if (p_fcr) {
  2343. /* Set the FCR parameters. For now, we will use default pools */
  2344. p_ccb->our_cfg.fcr = p_ccb->peer_cfg.fcr = *p_fcr;
  2345. p_ccb->ertm_info.fcr_rx_buf_size = L2CAP_FCR_RX_BUF_SIZE;
  2346. p_ccb->ertm_info.fcr_tx_buf_size = L2CAP_FCR_TX_BUF_SIZE;
  2347. p_ccb->ertm_info.user_rx_buf_size = L2CAP_USER_RX_BUF_SIZE;
  2348. p_ccb->ertm_info.user_tx_buf_size = L2CAP_USER_TX_BUF_SIZE;
  2349. p_ccb->fcrb.max_held_acks = p_fcr->tx_win_sz / 3;
  2350. }
  2351. /* Link ccb to lcb and lcb to ccb */
  2352. p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = p_ccb;
  2353. p_ccb->p_lcb = p_lcb;
  2354. /* There is no configuration, so if the link is up, the channel is up */
  2355. if (p_lcb->link_state == LST_CONNECTED) {
  2356. p_ccb->chnl_state = CST_OPEN;
  2357. }
  2358. /* Set the default idle timeout value to use */
  2359. p_ccb->fixed_chnl_idle_tout = l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].default_idle_tout;
  2360. #endif
  2361. return (TRUE);
  2362. }
  2363. /*******************************************************************************
  2364. **
  2365. ** Function l2cu_no_dynamic_ccbs
  2366. **
  2367. ** Description Handles the case when there are no more dynamic CCBs. If there
  2368. ** are any fixed CCBs, start the longest of the fixed CCB timeouts,
  2369. ** otherwise start the default link idle timeout or disconnect.
  2370. **
  2371. ** Returns void
  2372. **
  2373. *******************************************************************************/
  2374. void l2cu_no_dynamic_ccbs (tL2C_LCB *p_lcb)
  2375. {
  2376. #if (SMP_INCLUDED == TRUE)
  2377. tBTM_STATUS rc;
  2378. #endif ///SMP_INCLUDED == TRUE
  2379. UINT16 timeout = p_lcb->idle_timeout;
  2380. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2381. int xx;
  2382. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  2383. if ( (p_lcb->p_fixed_ccbs[xx] != NULL) && (p_lcb->p_fixed_ccbs[xx]->fixed_chnl_idle_tout > timeout) ) {
  2384. timeout = p_lcb->p_fixed_ccbs[xx]->fixed_chnl_idle_tout;
  2385. }
  2386. }
  2387. #endif
  2388. /* If the link is pairing, do not mess with the timeouts */
  2389. if (p_lcb->is_bonding) {
  2390. return;
  2391. }
  2392. if (timeout == 0) {
  2393. L2CAP_TRACE_DEBUG ("l2cu_no_dynamic_ccbs() IDLE timer 0, disconnecting link");
  2394. #if (SMP_INCLUDED == TRUE)
  2395. rc = btm_sec_disconnect (p_lcb->handle, HCI_ERR_PEER_USER);
  2396. if (rc == BTM_CMD_STARTED) {
  2397. l2cu_process_fixed_disc_cback(p_lcb);
  2398. p_lcb->link_state = LST_DISCONNECTING;
  2399. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  2400. } else if (rc == BTM_SUCCESS) {
  2401. l2cu_process_fixed_disc_cback(p_lcb);
  2402. /* BTM SEC will make sure that link is release (probably after pairing is done) */
  2403. p_lcb->link_state = LST_DISCONNECTING;
  2404. timeout = 0xFFFF;
  2405. } else if ( (p_lcb->is_bonding)
  2406. && (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER)) ) {
  2407. l2cu_process_fixed_disc_cback(p_lcb);
  2408. p_lcb->link_state = LST_DISCONNECTING;
  2409. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  2410. } else {
  2411. /* probably no buffer to send disconnect */
  2412. timeout = BT_1SEC_TIMEOUT;
  2413. }
  2414. #else
  2415. if (btsnd_hcic_disconnect (p_lcb->handle, HCI_ERR_PEER_USER)) {
  2416. l2cu_process_fixed_disc_cback(p_lcb);
  2417. p_lcb->link_state = LST_DISCONNECTING;
  2418. timeout = L2CAP_LINK_DISCONNECT_TOUT;
  2419. } else {
  2420. timeout = BT_1SEC_TIMEOUT;
  2421. }
  2422. #endif ///SMP_INCLUDED == TRUE
  2423. }
  2424. if (timeout != 0xFFFF) {
  2425. L2CAP_TRACE_DEBUG ("l2cu_no_dynamic_ccbs() starting IDLE timeout: %d", timeout);
  2426. btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, timeout);
  2427. } else {
  2428. btu_stop_timer(&p_lcb->timer_entry);
  2429. }
  2430. }
  2431. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2432. /*******************************************************************************
  2433. **
  2434. ** Function l2cu_process_fixed_chnl_resp
  2435. **
  2436. ** Description handle a fixed channel response (or lack thereof)
  2437. ** if the link failed, or a fixed channel response was
  2438. ** not received, the bitfield is all zeros.
  2439. **
  2440. *******************************************************************************/
  2441. void l2cu_process_fixed_chnl_resp (tL2C_LCB *p_lcb)
  2442. {
  2443. L2CAP_TRACE_DEBUG("%s",__func__);
  2444. #if (BLE_INCLUDED == TRUE)
  2445. if (p_lcb->transport == BT_TRANSPORT_BR_EDR) {
  2446. /* ignore all not assigned BR/EDR channels */
  2447. p_lcb->peer_chnl_mask[0] &= (L2CAP_FIXED_CHNL_SIG_BIT | \
  2448. L2CAP_FIXED_CHNL_CNCTLESS_BIT | \
  2449. L2CAP_FIXED_CHNL_SMP_BR_BIT);
  2450. } else {
  2451. p_lcb->peer_chnl_mask[0] = l2cb.l2c_ble_fixed_chnls_mask;
  2452. }
  2453. #endif
  2454. /* Tell all registered fixed channels about the connection */
  2455. for (int xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  2456. #if BLE_INCLUDED == TRUE
  2457. /* skip sending LE fix channel callbacks on BR/EDR links */
  2458. if (p_lcb->transport == BT_TRANSPORT_BR_EDR &&
  2459. xx + L2CAP_FIRST_FIXED_CHNL >= L2CAP_ATT_CID &&
  2460. xx + L2CAP_FIRST_FIXED_CHNL <= L2CAP_SMP_CID) {
  2461. continue;
  2462. }
  2463. #endif
  2464. if (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL) {
  2465. if (p_lcb->peer_chnl_mask[(xx + L2CAP_FIRST_FIXED_CHNL) / 8]
  2466. & (1 << ((xx + L2CAP_FIRST_FIXED_CHNL) % 8))) {
  2467. if (p_lcb->p_fixed_ccbs[xx]) {
  2468. p_lcb->p_fixed_ccbs[xx]->chnl_state = CST_OPEN;
  2469. }
  2470. #if BLE_INCLUDED == TRUE
  2471. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2472. p_lcb->remote_bd_addr, TRUE, 0, p_lcb->transport);
  2473. #else
  2474. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2475. p_lcb->remote_bd_addr, TRUE, 0, BT_TRANSPORT_BR_EDR);
  2476. #endif
  2477. } else {
  2478. #if BLE_INCLUDED == TRUE
  2479. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2480. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
  2481. #else
  2482. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2483. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
  2484. #endif
  2485. if (p_lcb->p_fixed_ccbs[xx]) {
  2486. l2cu_release_ccb (p_lcb->p_fixed_ccbs[xx]);
  2487. p_lcb->p_fixed_ccbs[xx] = NULL;
  2488. }
  2489. }
  2490. }
  2491. }
  2492. }
  2493. #endif
  2494. /*******************************************************************************
  2495. **
  2496. ** Function l2cu_process_fixed_disc_cback
  2497. **
  2498. ** Description send l2cap fixed channel disconnection callback to application
  2499. **
  2500. **
  2501. ** Returns void
  2502. **
  2503. *******************************************************************************/
  2504. void l2cu_process_fixed_disc_cback (tL2C_LCB *p_lcb)
  2505. {
  2506. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2507. /* Select peer channels mask to use depending on transport */
  2508. UINT8 peer_channel_mask = p_lcb->peer_chnl_mask[0];
  2509. // For LE, reset the stored peer channel mask
  2510. if (p_lcb->transport == BT_TRANSPORT_LE) {
  2511. p_lcb->peer_chnl_mask[0] = 0;
  2512. }
  2513. for (int xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  2514. if (p_lcb->p_fixed_ccbs[xx]) {
  2515. if (p_lcb->p_fixed_ccbs[xx] != p_lcb->p_pending_ccb) {
  2516. tL2C_CCB *p_l2c_chnl_ctrl_block;
  2517. p_l2c_chnl_ctrl_block = p_lcb->p_fixed_ccbs[xx];
  2518. p_lcb->p_fixed_ccbs[xx] = NULL;
  2519. l2cu_release_ccb(p_l2c_chnl_ctrl_block);
  2520. #if BLE_INCLUDED == TRUE
  2521. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2522. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
  2523. #else
  2524. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2525. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
  2526. #endif
  2527. }
  2528. } else if ( (peer_channel_mask & (1 << (xx + L2CAP_FIRST_FIXED_CHNL)))
  2529. && (l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb != NULL) ) {
  2530. #if BLE_INCLUDED == TRUE
  2531. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2532. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, p_lcb->transport);
  2533. #else
  2534. (*l2cb.fixed_reg[xx].pL2CA_FixedConn_Cb)(xx + L2CAP_FIRST_FIXED_CHNL,
  2535. p_lcb->remote_bd_addr, FALSE, p_lcb->disc_reason, BT_TRANSPORT_BR_EDR);
  2536. #endif
  2537. }
  2538. }
  2539. #endif
  2540. }
  2541. #if (BLE_INCLUDED == TRUE)
  2542. /*******************************************************************************
  2543. **
  2544. ** Function l2cu_send_peer_ble_par_req
  2545. **
  2546. ** Description Build and send a BLE parameter update request message
  2547. ** to the peer.
  2548. **
  2549. ** Returns void
  2550. **
  2551. *******************************************************************************/
  2552. void l2cu_send_peer_ble_par_req (tL2C_LCB *p_lcb, UINT16 min_int, UINT16 max_int,
  2553. UINT16 latency, UINT16 timeout)
  2554. {
  2555. BT_HDR *p_buf;
  2556. UINT8 *p;
  2557. /* Create an identifier for this packet */
  2558. p_lcb->id++;
  2559. l2cu_adj_id (p_lcb, L2CAP_ADJ_ID);
  2560. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_UPD_REQ_LEN,
  2561. L2CAP_CMD_BLE_UPDATE_REQ, p_lcb->id)) == NULL ) {
  2562. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_par_req - no buffer");
  2563. return;
  2564. }
  2565. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2566. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2567. UINT16_TO_STREAM (p, min_int);
  2568. UINT16_TO_STREAM (p, max_int);
  2569. UINT16_TO_STREAM (p, latency);
  2570. UINT16_TO_STREAM (p, timeout);
  2571. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2572. }
  2573. /*******************************************************************************
  2574. **
  2575. ** Function l2cu_send_peer_ble_par_rsp
  2576. **
  2577. ** Description Build and send a BLE parameter update response message
  2578. ** to the peer.
  2579. **
  2580. ** Returns void
  2581. **
  2582. *******************************************************************************/
  2583. void l2cu_send_peer_ble_par_rsp (tL2C_LCB *p_lcb, UINT16 reason, UINT8 rem_id)
  2584. {
  2585. BT_HDR *p_buf;
  2586. UINT8 *p;
  2587. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_UPD_RSP_LEN,
  2588. L2CAP_CMD_BLE_UPDATE_RSP, rem_id)) == NULL ) {
  2589. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_par_rsp - no buffer");
  2590. return;
  2591. }
  2592. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2593. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2594. UINT16_TO_STREAM (p, reason);
  2595. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2596. }
  2597. /*******************************************************************************
  2598. **
  2599. ** Function l2cu_send_peer_ble_credit_based_conn_req
  2600. **
  2601. ** Description Build and send a BLE packet to establish LE connection oriented
  2602. ** L2CAP channel.
  2603. **
  2604. ** Returns void
  2605. **
  2606. *******************************************************************************/
  2607. void l2cu_send_peer_ble_credit_based_conn_req (tL2C_CCB *p_ccb)
  2608. {
  2609. BT_HDR *p_buf;
  2610. UINT8 *p;
  2611. tL2C_LCB *p_lcb = NULL;
  2612. UINT16 mtu;
  2613. UINT16 mps;
  2614. UINT16 initial_credit;
  2615. if (!p_ccb) {
  2616. return;
  2617. }
  2618. p_lcb = p_ccb->p_lcb;
  2619. /* Create an identifier for this packet */
  2620. p_ccb->p_lcb->id++;
  2621. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  2622. p_ccb->local_id = p_ccb->p_lcb->id;
  2623. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ_LEN,
  2624. L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ, p_lcb->id)) == NULL )
  2625. {
  2626. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_conn_req - no buffer");
  2627. return;
  2628. }
  2629. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2630. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2631. mtu = p_ccb->local_conn_cfg.mtu;
  2632. mps = p_ccb->local_conn_cfg.mps;
  2633. initial_credit = p_ccb->local_conn_cfg.credits;
  2634. L2CAP_TRACE_DEBUG ("l2cu_send_peer_ble_credit_based_conn_req PSM:0x%04x local_cid:%d\
  2635. mtu:%d mps:%d initial_credit:%d", p_ccb->p_rcb->real_psm,\
  2636. p_ccb->local_cid, mtu, mps, initial_credit);
  2637. UINT16_TO_STREAM (p, p_ccb->p_rcb->real_psm);
  2638. UINT16_TO_STREAM (p, p_ccb->local_cid);
  2639. UINT16_TO_STREAM (p, mtu);
  2640. UINT16_TO_STREAM (p, mps);
  2641. UINT16_TO_STREAM (p, initial_credit);
  2642. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2643. }
  2644. /*******************************************************************************
  2645. **
  2646. ** Function l2cu_reject_ble_connection
  2647. **
  2648. ** Description Build and send an L2CAP "Credit based connection res" message
  2649. ** to the peer. This function is called for non-success cases.
  2650. **
  2651. ** Returns void
  2652. **
  2653. *******************************************************************************/
  2654. void l2cu_reject_ble_connection (tL2C_LCB *p_lcb, UINT8 rem_id, UINT16 result)
  2655. {
  2656. BT_HDR *p_buf;
  2657. UINT8 *p;
  2658. if ((p_buf = l2cu_build_header(p_lcb, L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES_LEN,
  2659. L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES, rem_id)) == NULL )
  2660. {
  2661. L2CAP_TRACE_WARNING ("l2cu_reject_ble_connection - no buffer");
  2662. return;
  2663. }
  2664. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2665. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2666. UINT16_TO_STREAM (p, 0); /* Local CID of 0 */
  2667. UINT16_TO_STREAM (p, 0); /* MTU */
  2668. UINT16_TO_STREAM (p, 0); /* MPS */
  2669. UINT16_TO_STREAM (p, 0); /* initial credit */
  2670. UINT16_TO_STREAM (p, result);
  2671. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2672. }
  2673. /*******************************************************************************
  2674. **
  2675. ** Function l2cu_send_peer_ble_credit_based_conn_res
  2676. **
  2677. ** Description Build and send an L2CAP "Credit based connection res" message
  2678. ** to the peer. This function is called in case of success.
  2679. **
  2680. ** Returns void
  2681. **
  2682. *******************************************************************************/
  2683. void l2cu_send_peer_ble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
  2684. {
  2685. BT_HDR *p_buf;
  2686. UINT8 *p;
  2687. L2CAP_TRACE_DEBUG ("l2cu_send_peer_ble_credit_based_conn_res");
  2688. if ((p_buf = l2cu_build_header(p_ccb->p_lcb, L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES_LEN,
  2689. L2CAP_CMD_BLE_CREDIT_BASED_CONN_RES, p_ccb->remote_id)) == NULL )
  2690. {
  2691. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_conn_res - no buffer");
  2692. return;
  2693. }
  2694. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2695. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2696. UINT16_TO_STREAM (p, p_ccb->local_cid); /* Local CID */
  2697. UINT16_TO_STREAM (p, p_ccb->local_conn_cfg.mtu); /* MTU */
  2698. UINT16_TO_STREAM (p, p_ccb->local_conn_cfg.mps); /* MPS */
  2699. UINT16_TO_STREAM (p, p_ccb->local_conn_cfg.credits); /* initial credit */
  2700. UINT16_TO_STREAM (p, result);
  2701. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  2702. }
  2703. /*******************************************************************************
  2704. **
  2705. ** Function l2cu_send_peer_ble_flow_control_credit
  2706. **
  2707. ** Description Build and send a BLE packet to give credits to peer device
  2708. ** for LE connection oriented L2CAP channel.
  2709. **
  2710. ** Returns void
  2711. **
  2712. *******************************************************************************/
  2713. void l2cu_send_peer_ble_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
  2714. {
  2715. BT_HDR *p_buf;
  2716. UINT8 *p;
  2717. tL2C_LCB *p_lcb = NULL;
  2718. if (!p_ccb) {
  2719. return;
  2720. }
  2721. p_lcb = p_ccb->p_lcb;
  2722. /* Create an identifier for this packet */
  2723. p_ccb->p_lcb->id++;
  2724. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  2725. p_ccb->local_id = p_ccb->p_lcb->id;
  2726. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_CMD_BLE_FLOW_CTRL_CREDIT_LEN,
  2727. L2CAP_CMD_BLE_FLOW_CTRL_CREDIT, p_lcb->id)) == NULL )
  2728. {
  2729. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_conn_req - no buffer");
  2730. return;
  2731. }
  2732. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2733. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2734. UINT16_TO_STREAM (p, p_ccb->local_cid);
  2735. UINT16_TO_STREAM (p, credit_value);
  2736. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2737. }
  2738. /*******************************************************************************
  2739. **
  2740. ** Function l2cu_send_peer_ble_credit_based_conn_req
  2741. **
  2742. ** Description Build and send a BLE packet to disconnect LE connection oriented
  2743. ** L2CAP channel.
  2744. **
  2745. ** Returns void
  2746. **
  2747. *******************************************************************************/
  2748. void l2cu_send_peer_ble_credit_based_disconn_req(tL2C_CCB *p_ccb)
  2749. {
  2750. BT_HDR *p_buf;
  2751. UINT8 *p;
  2752. tL2C_LCB *p_lcb = NULL;
  2753. L2CAP_TRACE_DEBUG ("%s",__func__);
  2754. if (!p_ccb) {
  2755. return;
  2756. }
  2757. p_lcb = p_ccb->p_lcb;
  2758. /* Create an identifier for this packet */
  2759. p_ccb->p_lcb->id++;
  2760. l2cu_adj_id(p_ccb->p_lcb, L2CAP_ADJ_ID);
  2761. p_ccb->local_id = p_ccb->p_lcb->id;
  2762. if ((p_buf = l2cu_build_header (p_lcb, L2CAP_DISC_REQ_LEN,
  2763. L2CAP_CMD_DISC_REQ, p_lcb->id)) == NULL )
  2764. {
  2765. L2CAP_TRACE_WARNING ("l2cu_send_peer_ble_credit_based_disconn_req - no buffer");
  2766. return;
  2767. }
  2768. p = (UINT8 *)(p_buf + 1) + L2CAP_SEND_CMD_OFFSET + HCI_DATA_PREAMBLE_SIZE +
  2769. L2CAP_PKT_OVERHEAD + L2CAP_CMD_OVERHEAD;
  2770. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  2771. UINT16_TO_STREAM (p,p_ccb->local_cid);
  2772. l2c_link_check_send_pkts (p_lcb, NULL, p_buf);
  2773. }
  2774. #endif /* BLE_INCLUDED == TRUE */
  2775. /*******************************************************************************
  2776. ** Functions used by both Full and Light Stack
  2777. ********************************************************************************/
  2778. /*******************************************************************************
  2779. **
  2780. ** Function l2cu_find_lcb_by_handle
  2781. **
  2782. ** Description Look through all active LCBs for a match based on the
  2783. ** HCI handle.
  2784. **
  2785. ** Returns pointer to matched LCB, or NULL if no match
  2786. **
  2787. *******************************************************************************/
  2788. tL2C_LCB *l2cu_find_lcb_by_handle (UINT16 handle)
  2789. {
  2790. list_node_t *p_node = NULL;
  2791. tL2C_LCB *p_lcb = NULL;
  2792. for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
  2793. p_lcb = list_node(p_node);
  2794. if ((p_lcb->in_use) && (p_lcb->handle == handle)) {
  2795. return (p_lcb);
  2796. }
  2797. }
  2798. /* If here, no match found */
  2799. return (NULL);
  2800. }
  2801. /*******************************************************************************
  2802. **
  2803. ** Function l2cu_find_ccb_by_cid
  2804. **
  2805. ** Description Look through all active CCBs on a link for a match based
  2806. ** on the local CID. If passed the link pointer is NULL, all
  2807. ** active links are searched.
  2808. **
  2809. ** Returns pointer to matched CCB, or NULL if no match
  2810. **
  2811. *******************************************************************************/
  2812. bool l2cu_find_ccb_in_list(void *p_ccb_node, void *p_local_cid)
  2813. {
  2814. tL2C_CCB *p_ccb = (tL2C_CCB *)p_ccb_node;
  2815. uint8_t local_cid = *((uint8_t *)p_local_cid);
  2816. if (p_ccb->local_cid == local_cid && p_ccb->in_use) {
  2817. return FALSE;
  2818. }
  2819. return TRUE;
  2820. }
  2821. tL2C_CCB *l2cu_find_ccb_by_cid (tL2C_LCB *p_lcb, UINT16 local_cid)
  2822. {
  2823. tL2C_CCB *p_ccb = NULL;
  2824. #if (L2CAP_UCD_INCLUDED == FALSE)
  2825. if (local_cid < L2CAP_BASE_APPL_CID) {
  2826. return NULL;
  2827. }
  2828. #endif //(L2CAP_UCD_INCLUDED == FALSE)
  2829. list_node_t *p_node = NULL;
  2830. p_node = (list_foreach(l2cb.p_ccb_pool, l2cu_find_ccb_in_list, &local_cid));
  2831. if (p_node) {
  2832. p_ccb = (tL2C_CCB *)list_node(p_node);
  2833. if (p_lcb && p_lcb != p_ccb->p_lcb) {
  2834. p_ccb = NULL;
  2835. }
  2836. }
  2837. return (p_ccb);
  2838. }
  2839. tL2C_CCB *l2cu_find_free_ccb (void)
  2840. {
  2841. tL2C_CCB *p_ccb = NULL;
  2842. list_node_t *p_node = NULL;
  2843. for (p_node = list_begin(l2cb.p_ccb_pool); p_node; p_node = list_next(p_node))
  2844. {
  2845. p_ccb = list_node(p_node);
  2846. if(p_ccb && !p_ccb->in_use ) {
  2847. return p_ccb;
  2848. }
  2849. }
  2850. return (NULL);
  2851. }
  2852. #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE && CLASSIC_BT_INCLUDED == TRUE)
  2853. /******************************************************************************
  2854. **
  2855. ** Function l2cu_get_next_channel_in_rr
  2856. **
  2857. ** Description get the next channel to send on a link. It also adjusts the
  2858. ** CCB queue to do a basic priority and round-robin scheduling.
  2859. **
  2860. ** Returns pointer to CCB or NULL
  2861. **
  2862. *******************************************************************************/
  2863. static tL2C_CCB *l2cu_get_next_channel_in_rr(tL2C_LCB *p_lcb)
  2864. {
  2865. tL2C_CCB *p_serve_ccb = NULL;
  2866. tL2C_CCB *p_ccb;
  2867. int i, j;
  2868. /* scan all of priority until finding a channel to serve */
  2869. for ( i = 0; (i < L2CAP_NUM_CHNL_PRIORITY) && (!p_serve_ccb); i++ ) {
  2870. /* scan all channel within serving priority group until finding a channel to serve */
  2871. for ( j = 0; (j < p_lcb->rr_serv[p_lcb->rr_pri].num_ccb) && (!p_serve_ccb); j++) {
  2872. /* scaning from next serving channel */
  2873. p_ccb = p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb;
  2874. if (!p_ccb) {
  2875. L2CAP_TRACE_ERROR("p_serve_ccb is NULL, rr_pri=%d", p_lcb->rr_pri);
  2876. return NULL;
  2877. }
  2878. L2CAP_TRACE_DEBUG("RR scan pri=%d, lcid=0x%04x, q_cout=%d",
  2879. p_ccb->ccb_priority, p_ccb->local_cid,
  2880. fixed_queue_length(p_ccb->xmit_hold_q));
  2881. /* store the next serving channel */
  2882. /* this channel is the last channel of its priority group */
  2883. if (( p_ccb->p_next_ccb == NULL )
  2884. || ( p_ccb->p_next_ccb->ccb_priority != p_ccb->ccb_priority )) {
  2885. /* next serving channel is set to the first channel in the group */
  2886. p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb = p_lcb->rr_serv[p_lcb->rr_pri].p_first_ccb;
  2887. } else {
  2888. /* next serving channel is set to the next channel in the group */
  2889. p_lcb->rr_serv[p_lcb->rr_pri].p_serve_ccb = p_ccb->p_next_ccb;
  2890. }
  2891. if (p_ccb->chnl_state != CST_OPEN) {
  2892. continue;
  2893. }
  2894. /* eL2CAP option in use */
  2895. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  2896. if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy) {
  2897. continue;
  2898. }
  2899. if (fixed_queue_is_empty(p_ccb->fcrb.retrans_q)) {
  2900. if (fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  2901. continue;
  2902. }
  2903. #if (CLASSIC_BT_INCLUDED == TRUE)
  2904. /* If in eRTM mode, check for window closure */
  2905. if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) ) {
  2906. continue;
  2907. }
  2908. #endif ///CLASSIC_BT_INCLUDED == TRUE
  2909. }
  2910. } else {
  2911. if (fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  2912. continue;
  2913. }
  2914. }
  2915. /* found a channel to serve */
  2916. p_serve_ccb = p_ccb;
  2917. /* decrease quota of its priority group */
  2918. p_lcb->rr_serv[p_lcb->rr_pri].quota--;
  2919. }
  2920. /* if there is no more quota of the priority group or no channel to have data to send */
  2921. if ((p_lcb->rr_serv[p_lcb->rr_pri].quota == 0) || (!p_serve_ccb)) {
  2922. /* serve next priority group */
  2923. p_lcb->rr_pri = (p_lcb->rr_pri + 1) % L2CAP_NUM_CHNL_PRIORITY;
  2924. /* initialize its quota */
  2925. p_lcb->rr_serv[p_lcb->rr_pri].quota = L2CAP_GET_PRIORITY_QUOTA(p_lcb->rr_pri);
  2926. }
  2927. }
  2928. if (p_serve_ccb) {
  2929. L2CAP_TRACE_DEBUG("RR service pri=%d, quota=%d, lcid=0x%04x",
  2930. p_serve_ccb->ccb_priority,
  2931. p_lcb->rr_serv[p_serve_ccb->ccb_priority].quota,
  2932. p_serve_ccb->local_cid );
  2933. }
  2934. return p_serve_ccb;
  2935. }
  2936. #else /* (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) */
  2937. /******************************************************************************
  2938. **
  2939. ** Function l2cu_get_next_channel
  2940. **
  2941. ** Description get the next channel to send on a link bassed on priority
  2942. ** scheduling.
  2943. **
  2944. ** Returns pointer to CCB or NULL
  2945. **
  2946. *******************************************************************************/
  2947. #if (CLASSIC_BT_INCLUDED == TRUE)
  2948. static tL2C_CCB *l2cu_get_next_channel(tL2C_LCB *p_lcb)
  2949. {
  2950. tL2C_CCB *p_ccb;
  2951. /* Get the first CCB with data to send.
  2952. */
  2953. for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
  2954. if (p_ccb->chnl_state != CST_OPEN) {
  2955. continue;
  2956. }
  2957. if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy) {
  2958. continue;
  2959. }
  2960. if (!fixed_queue_is_empty(p_ccb->fcrb.retrans_q))
  2961. return p_ccb;
  2962. }
  2963. if (fixed_queue_is_empty(p_ccb->xmit_hold_q))
  2964. continue;
  2965. }
  2966. /* If in eRTM mode, check for window closure */
  2967. if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) ) {
  2968. continue;
  2969. }
  2970. /* If here, we found someone */
  2971. return p_ccb;
  2972. }
  2973. return NULL;
  2974. }
  2975. #endif ///CLASSIC_BT_INCLUDED == TRUE
  2976. #endif /* (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE) */
  2977. /******************************************************************************
  2978. **
  2979. ** Function l2cu_get_next_buffer_to_send
  2980. **
  2981. ** Description get the next buffer to send on a link. It also adjusts the
  2982. ** CCB queue to do a basic priority and round-robin scheduling.
  2983. **
  2984. ** Returns pointer to buffer or NULL
  2985. **
  2986. *******************************************************************************/
  2987. BT_HDR *l2cu_get_next_buffer_to_send (tL2C_LCB *p_lcb)
  2988. {
  2989. tL2C_CCB *p_ccb;
  2990. BT_HDR *p_buf = NULL;
  2991. /* Highest priority are fixed channels */
  2992. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  2993. int xx;
  2994. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx++) {
  2995. if ((p_ccb = p_lcb->p_fixed_ccbs[xx]) == NULL) {
  2996. continue;
  2997. }
  2998. /* eL2CAP option in use */
  2999. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  3000. #if (CLASSIC_BT_INCLUDED == TRUE)
  3001. if (p_ccb->fcrb.wait_ack || p_ccb->fcrb.remote_busy) {
  3002. continue;
  3003. }
  3004. /* No more checks needed if sending from the reatransmit queue */
  3005. if (fixed_queue_is_empty(p_ccb->fcrb.retrans_q))
  3006. {
  3007. if (fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  3008. continue;
  3009. }
  3010. /* If in eRTM mode, check for window closure */
  3011. if ( (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) && (l2c_fcr_is_flow_controlled (p_ccb)) ) {
  3012. continue;
  3013. }
  3014. }
  3015. if ((p_buf = l2c_fcr_get_next_xmit_sdu_seg(p_ccb, 0)) != NULL) {
  3016. l2cu_check_channel_congestion (p_ccb);
  3017. l2cu_set_acl_hci_header (p_buf, p_ccb);
  3018. return (p_buf);
  3019. }
  3020. #else
  3021. continue;
  3022. #endif ///CLASSIC_BT_INCLUDED == TRUE
  3023. } else {
  3024. if (!fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  3025. p_buf = (BT_HDR *)fixed_queue_dequeue(p_ccb->xmit_hold_q, 0);
  3026. if (NULL == p_buf) {
  3027. L2CAP_TRACE_ERROR("l2cu_get_buffer_to_send: No data to be sent");
  3028. return (NULL);
  3029. }
  3030. l2cu_check_channel_congestion (p_ccb);
  3031. l2cu_set_acl_hci_header (p_buf, p_ccb);
  3032. /* send tx complete */
  3033. if (l2cb.fixed_reg[xx].pL2CA_FixedTxComplete_Cb) {
  3034. (*l2cb.fixed_reg[xx].pL2CA_FixedTxComplete_Cb)(p_ccb->local_cid, 1);
  3035. }
  3036. return (p_buf);
  3037. }
  3038. }
  3039. }
  3040. #endif
  3041. #if (CLASSIC_BT_INCLUDED == TRUE)
  3042. #if (L2CAP_ROUND_ROBIN_CHANNEL_SERVICE == TRUE)
  3043. /* get next serving channel in round-robin */
  3044. p_ccb = l2cu_get_next_channel_in_rr( p_lcb );
  3045. #else
  3046. p_ccb = l2cu_get_next_channel( p_lcb );
  3047. #endif
  3048. /* Return if no buffer */
  3049. if (p_ccb == NULL) {
  3050. return (NULL);
  3051. }
  3052. if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
  3053. if ((p_buf = l2c_fcr_get_next_xmit_sdu_seg(p_ccb, 0)) == NULL) {
  3054. return (NULL);
  3055. }
  3056. } else {
  3057. p_buf = (BT_HDR *)fixed_queue_dequeue(p_ccb->xmit_hold_q, 0);
  3058. if (NULL == p_buf) {
  3059. L2CAP_TRACE_ERROR("l2cu_get_buffer_to_send() #2: No data to be sent");
  3060. return (NULL);
  3061. }
  3062. }
  3063. if ( p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_TxComplete_Cb && (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) ) {
  3064. (*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, 1);
  3065. }
  3066. l2cu_check_channel_congestion (p_ccb);
  3067. l2cu_set_acl_hci_header (p_buf, p_ccb);
  3068. #endif ///CLASSIC_BT_INCLUDED == TRUE
  3069. return (p_buf);
  3070. }
  3071. /******************************************************************************
  3072. **
  3073. ** Function l2cu_set_acl_hci_header
  3074. **
  3075. ** Description Set HCI handle for ACL packet
  3076. **
  3077. ** Returns None
  3078. **
  3079. *******************************************************************************/
  3080. void l2cu_set_acl_hci_header (BT_HDR *p_buf, tL2C_CCB *p_ccb)
  3081. {
  3082. UINT8 *p;
  3083. /* Set the pointer to the beginning of the data minus 4 bytes for the packet header */
  3084. p = (UINT8 *)(p_buf + 1) + p_buf->offset - HCI_DATA_PREAMBLE_SIZE;
  3085. #if (BLE_INCLUDED == TRUE)
  3086. if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE) {
  3087. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT));
  3088. uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_ble();
  3089. /* The HCI transport will segment the buffers. */
  3090. if (p_buf->len > acl_data_size) {
  3091. UINT16_TO_STREAM (p, acl_data_size);
  3092. } else {
  3093. UINT16_TO_STREAM (p, p_buf->len);
  3094. }
  3095. } /* (BLE_INCLUDED == TRUE) */
  3096. else
  3097. #endif
  3098. {
  3099. #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
  3100. if ( (((p_buf->layer_specific & L2CAP_FLUSHABLE_MASK) == L2CAP_FLUSHABLE_CH_BASED) && (p_ccb->is_flushable))
  3101. || ((p_buf->layer_specific & L2CAP_FLUSHABLE_MASK) == L2CAP_FLUSHABLE_PKT) ) {
  3102. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT));
  3103. } else {
  3104. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | l2cb.non_flushable_pbf);
  3105. }
  3106. #else
  3107. UINT16_TO_STREAM (p, p_ccb->p_lcb->handle | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT));
  3108. #endif
  3109. uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
  3110. /* The HCI transport will segment the buffers. */
  3111. if (p_buf->len > acl_data_size) {
  3112. UINT16_TO_STREAM (p, acl_data_size);
  3113. } else {
  3114. UINT16_TO_STREAM (p, p_buf->len);
  3115. }
  3116. }
  3117. p_buf->offset -= HCI_DATA_PREAMBLE_SIZE;
  3118. p_buf->len += HCI_DATA_PREAMBLE_SIZE;
  3119. }
  3120. /******************************************************************************
  3121. **
  3122. ** Function l2cu_check_channel_congestion
  3123. **
  3124. ** Description check if any change in congestion status
  3125. **
  3126. ** Returns None
  3127. **
  3128. *******************************************************************************/
  3129. void l2cu_check_channel_congestion (tL2C_CCB *p_ccb)
  3130. {
  3131. size_t q_count = fixed_queue_length(p_ccb->xmit_hold_q);
  3132. #if (L2CAP_UCD_INCLUDED == TRUE)
  3133. if ( p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID ) {
  3134. q_count += fixed_queue_length(p_ccb->p_lcb->ucd_out_sec_pending_q);
  3135. }
  3136. #endif
  3137. /* If the CCB queue limit is subject to a quota, check for congestion */
  3138. /* if this channel has outgoing traffic */
  3139. if (p_ccb->buff_quota != 0) {
  3140. /* If this channel was congested */
  3141. if ( p_ccb->cong_sent ) {
  3142. /* If the channel is not congested now, tell the app */
  3143. if (q_count <= (p_ccb->buff_quota / 2)) {
  3144. p_ccb->cong_sent = FALSE;
  3145. if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb) {
  3146. L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (FALSE), CID: 0x%04x xmit_hold_q.count: %u buff_quota: %u",
  3147. p_ccb->local_cid, q_count, p_ccb->buff_quota);
  3148. /* Prevent recursive calling */
  3149. l2cb.is_cong_cback_context = TRUE;
  3150. (*p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)(p_ccb->local_cid, FALSE);
  3151. l2cb.is_cong_cback_context = FALSE;
  3152. }
  3153. #if (L2CAP_UCD_INCLUDED == TRUE)
  3154. else if ( p_ccb->p_rcb && p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID ) {
  3155. if ( p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb ) {
  3156. L2CAP_TRACE_DEBUG ("L2CAP - Calling UCD CongestionStatus_Cb (FALSE), SecPendingQ:%u,XmitQ:%u,Quota:%u",
  3157. fixed_queue_length(p_ccb->p_lcb->ucd_out_sec_pending_q),
  3158. fixed_queue_length(p_ccb->xmit_hold_q),
  3159. p_ccb->buff_quota);
  3160. p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb( p_ccb->p_lcb->remote_bd_addr, FALSE );
  3161. }
  3162. }
  3163. #endif
  3164. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  3165. else {
  3166. UINT8 xx;
  3167. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx ++) {
  3168. if (p_ccb->p_lcb->p_fixed_ccbs[xx] == p_ccb) {
  3169. if (l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb != NULL) {
  3170. (* l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb)(p_ccb->p_lcb->remote_bd_addr, FALSE);
  3171. }
  3172. break;
  3173. }
  3174. }
  3175. }
  3176. #endif
  3177. }
  3178. } else {
  3179. tL2C_LCB *p_lcb = p_ccb->p_lcb;
  3180. /* If this channel was not congested but it is congested now, tell the app */
  3181. 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)) {
  3182. p_ccb->cong_sent = TRUE;
  3183. if (p_ccb->p_rcb && p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb) {
  3184. L2CAP_TRACE_DEBUG ("L2CAP - Calling CongestionStatus_Cb (TRUE),CID:0x%04x,XmitQ:%u,Quota:%u",
  3185. p_ccb->local_cid, q_count, p_ccb->buff_quota);
  3186. (*p_ccb->p_rcb->api.pL2CA_CongestionStatus_Cb)(p_ccb->local_cid, TRUE);
  3187. }
  3188. #if (L2CAP_UCD_INCLUDED == TRUE)
  3189. else if ( p_ccb->p_rcb && p_ccb->local_cid == L2CAP_CONNECTIONLESS_CID ) {
  3190. if ( p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb ) {
  3191. L2CAP_TRACE_DEBUG ("L2CAP - Calling UCD CongestionStatus_Cb (TRUE), SecPendingQ:%u,XmitQ:%u,Quota:%u",
  3192. fixed_queue_length(p_ccb->p_lcb->ucd_out_sec_pending_q),
  3193. fixed_queue_length(p_ccb->xmit_hold_q),
  3194. p_ccb->buff_quota);
  3195. p_ccb->p_rcb->ucd.cb_info.pL2CA_UCD_Congestion_Status_Cb( p_ccb->p_lcb->remote_bd_addr, TRUE );
  3196. }
  3197. }
  3198. #endif
  3199. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  3200. else {
  3201. UINT8 xx;
  3202. for (xx = 0; xx < L2CAP_NUM_FIXED_CHNLS; xx ++) {
  3203. if (p_ccb->p_lcb->p_fixed_ccbs[xx] == p_ccb) {
  3204. if (l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb != NULL) {
  3205. (* l2cb.fixed_reg[xx].pL2CA_FixedCong_Cb)(p_ccb->p_lcb->remote_bd_addr, TRUE);
  3206. }
  3207. break;
  3208. }
  3209. }
  3210. }
  3211. #endif
  3212. }
  3213. }
  3214. }
  3215. }