l2c_utils.c 128 KB

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