l2c_utils.c 130 KB

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