l2c_utils.c 127 KB

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