l2c_utils.c 128 KB

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