l2c_utils.cc 115 KB

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