l2c_utils.c 129 KB

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