l2c_utils.c 129 KB

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