l2c_fcr.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2004-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 the L2CAP 1.2 Flow Control and retransmissions
  21. * functions
  22. *
  23. ******************************************************************************/
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "common/bt_trace.h"
  28. #include "stack/bt_types.h"
  29. #include "stack/hcimsgs.h"
  30. #include "stack/l2c_api.h"
  31. #include "l2c_int.h"
  32. #include "stack/l2cdefs.h"
  33. #include "stack/btm_api.h"
  34. #include "btm_int.h"
  35. #include "stack/btu.h"
  36. #include "osi/allocator.h"
  37. #if (L2CAP_COC_INCLUDED == TRUE)
  38. /* Flag passed to retransmit_i_frames() when all packets should be retransmitted */
  39. #define L2C_FCR_RETX_ALL_PKTS 0xFF
  40. #if BT_TRACE_VERBOSE == TRUE
  41. static char *SAR_types[] = { "Unsegmented", "Start", "End", "Continuation" };
  42. static char *SUP_types[] = { "RR", "REJ", "RNR", "SREJ" };
  43. #endif
  44. /* Look-up table for the CRC calculation */
  45. static const unsigned short crctab[256] = {
  46. 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
  47. 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
  48. 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
  49. 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
  50. 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  51. 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  52. 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  53. 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  54. 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  55. 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  56. 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  57. 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  58. 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  59. 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  60. 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  61. 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  62. 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  63. 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  64. 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  65. 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  66. 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  67. 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  68. 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  69. 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  70. 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  71. 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  72. 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  73. 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  74. 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  75. 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  76. 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  77. 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
  78. };
  79. /*******************************************************************************
  80. ** Static local functions
  81. */
  82. static BOOLEAN process_reqseq (tL2C_CCB *p_ccb, UINT16 ctrl_word);
  83. static void process_s_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word);
  84. static void process_i_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word, BOOLEAN delay_ack);
  85. static BOOLEAN retransmit_i_frames (tL2C_CCB *p_ccb, UINT8 tx_seq);
  86. static void prepare_I_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, BOOLEAN is_retransmission);
  87. static void process_stream_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf);
  88. static BOOLEAN do_sar_reassembly (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word);
  89. #if (L2CAP_ERTM_STATS == TRUE)
  90. static void l2c_fcr_collect_ack_delay (tL2C_CCB *p_ccb, UINT8 num_bufs_acked);
  91. #endif
  92. /*******************************************************************************
  93. **
  94. ** Function l2c_fcr_updcrc
  95. **
  96. ** Description This function computes the CRC using the look-up table.
  97. **
  98. ** Returns CRC
  99. **
  100. *******************************************************************************/
  101. static unsigned short l2c_fcr_updcrc(unsigned short icrc, unsigned char *icp, int icnt)
  102. {
  103. register unsigned short crc = icrc;
  104. register unsigned char *cp = icp;
  105. register int cnt = icnt;
  106. while (cnt--) {
  107. crc = ((crc >> 8) & 0xff) ^ crctab[(crc & 0xff) ^ *cp++];
  108. }
  109. return (crc);
  110. }
  111. /*******************************************************************************
  112. **
  113. ** Function l2c_fcr_tx_get_fcs
  114. **
  115. ** Description This function computes the CRC for a frame to be TXed.
  116. **
  117. ** Returns CRC
  118. **
  119. *******************************************************************************/
  120. static UINT16 l2c_fcr_tx_get_fcs (BT_HDR *p_buf)
  121. {
  122. UINT8 *p = ((UINT8 *) (p_buf + 1)) + p_buf->offset;
  123. return (l2c_fcr_updcrc (L2CAP_FCR_INIT_CRC, p, p_buf->len));
  124. }
  125. /*******************************************************************************
  126. **
  127. ** Function l2c_fcr_rx_get_fcs
  128. **
  129. ** Description This function computes the CRC for a received frame.
  130. **
  131. ** Returns CRC
  132. **
  133. *******************************************************************************/
  134. static UINT16 l2c_fcr_rx_get_fcs (BT_HDR *p_buf)
  135. {
  136. UINT8 *p = ((UINT8 *) (p_buf + 1)) + p_buf->offset;
  137. /* offset points past the L2CAP header, but the CRC check includes it */
  138. p -= L2CAP_PKT_OVERHEAD;
  139. return (l2c_fcr_updcrc (L2CAP_FCR_INIT_CRC, p, p_buf->len + L2CAP_PKT_OVERHEAD));
  140. }
  141. /*******************************************************************************
  142. **
  143. ** Function l2c_fcr_start_timer
  144. **
  145. ** Description This function starts the (monitor or retransmission) timer.
  146. **
  147. ** Returns -
  148. **
  149. *******************************************************************************/
  150. void l2c_fcr_start_timer (tL2C_CCB *p_ccb)
  151. {
  152. assert(p_ccb != NULL);
  153. UINT32 tout;
  154. /* The timers which are in milliseconds */
  155. if (p_ccb->fcrb.wait_ack) {
  156. tout = (UINT32)p_ccb->our_cfg.fcr.mon_tout;
  157. } else {
  158. tout = (UINT32)p_ccb->our_cfg.fcr.rtrans_tout;
  159. }
  160. /* Only start a timer that was not started */
  161. if (p_ccb->fcrb.mon_retrans_timer.in_use == 0) {
  162. btu_start_quick_timer (&p_ccb->fcrb.mon_retrans_timer, BTU_TTYPE_L2CAP_CHNL, tout * QUICK_TIMER_TICKS_PER_SEC / 1000);
  163. }
  164. }
  165. /*******************************************************************************
  166. **
  167. ** Function l2c_fcr_stop_timer
  168. **
  169. ** Description This function stops the (monitor or transmission) timer.
  170. **
  171. ** Returns -
  172. **
  173. *******************************************************************************/
  174. void l2c_fcr_stop_timer (tL2C_CCB *p_ccb)
  175. {
  176. assert(p_ccb != NULL);
  177. if (p_ccb->fcrb.mon_retrans_timer.in_use) {
  178. btu_stop_quick_timer (&p_ccb->fcrb.mon_retrans_timer);
  179. }
  180. }
  181. /*******************************************************************************
  182. **
  183. ** Function l2c_fcr_free_timer
  184. **
  185. ** Description This function releases the (monitor or transmission) timer.
  186. **
  187. ** Returns -
  188. **
  189. *******************************************************************************/
  190. void l2c_fcr_free_timer (tL2C_CCB *p_ccb)
  191. {
  192. assert(p_ccb != NULL);
  193. btu_free_quick_timer (&p_ccb->fcrb.mon_retrans_timer);
  194. }
  195. /*******************************************************************************
  196. **
  197. ** Function l2c_fcr_cleanup
  198. **
  199. ** Description This function cleans up the variable used for flow-control/retrans.
  200. **
  201. ** Returns -
  202. **
  203. *******************************************************************************/
  204. void l2c_fcr_cleanup (tL2C_CCB *p_ccb)
  205. {
  206. assert(p_ccb != NULL);
  207. tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
  208. l2c_fcr_stop_timer (p_ccb);
  209. if (p_fcrb->p_rx_sdu) {
  210. osi_free(p_fcrb->p_rx_sdu);
  211. p_fcrb->p_rx_sdu = NULL;
  212. }
  213. fixed_queue_free(p_fcrb->waiting_for_ack_q, osi_free_func);
  214. p_fcrb->waiting_for_ack_q = NULL;
  215. fixed_queue_free(p_fcrb->srej_rcv_hold_q, osi_free_func);
  216. p_fcrb->srej_rcv_hold_q = NULL;
  217. fixed_queue_free(p_fcrb->retrans_q, osi_free_func);
  218. p_fcrb->retrans_q = NULL;
  219. btu_free_quick_timer (&p_fcrb->ack_timer);
  220. memset(&p_fcrb->ack_timer, 0, sizeof(TIMER_LIST_ENT));
  221. btu_free_quick_timer (&p_ccb->fcrb.mon_retrans_timer);
  222. memset(&p_fcrb->mon_retrans_timer, 0, sizeof(TIMER_LIST_ENT));
  223. #if (L2CAP_ERTM_STATS == TRUE)
  224. if ( (p_ccb->local_cid >= L2CAP_BASE_APPL_CID) && (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) ) {
  225. UINT32 dur = osi_time_get_os_boottime_ms() - p_ccb->fcrb.connect_tick_count;
  226. char *p_str = (char *)osi_malloc(120);
  227. UINT16 i;
  228. UINT32 throughput_avg, ack_delay_avg, ack_q_count_avg;
  229. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
  230. "--- L2CAP ERTM Stats for CID: 0x%04x Duration: %08ums", p_ccb->local_cid, dur);
  231. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
  232. "Retransmissions:%08u Times Flow Controlled:%08u Retrans Touts:%08u Ack Touts:%08u",
  233. p_ccb->fcrb.pkts_retransmitted, p_ccb->fcrb.xmit_window_closed, p_ccb->fcrb.retrans_touts, p_ccb->fcrb.xmit_ack_touts);
  234. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
  235. "Times there is less than 2 packets in controller when flow controlled:%08u", p_ccb->fcrb.controller_idle);
  236. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
  237. "max_held_acks:%08u, in_cfg.fcr.tx_win_sz:%08u", p_ccb->fcrb.max_held_acks, p_ccb->peer_cfg.fcr.tx_win_sz );
  238. if (p_str) {
  239. sprintf(p_str, "Sent Pkts:%08u Bytes:%10u(%06u/sec) RR:%08u REJ:%08u RNR:%08u SREJ:%08u",
  240. p_ccb->fcrb.ertm_pkt_counts[0], p_ccb->fcrb.ertm_byte_counts[0],
  241. (dur >= 10 ? (p_ccb->fcrb.ertm_byte_counts[0] * 100) / (dur / 10) : 0),
  242. p_ccb->fcrb.s_frames_sent[0], p_ccb->fcrb.s_frames_sent[1], p_ccb->fcrb.s_frames_sent[2], p_ccb->fcrb.s_frames_sent[3]);
  243. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC, "%s", p_str);
  244. sprintf(p_str, "Rcvd Pkts:%08u Bytes:%10u(%06u/sec) RR:%08u REJ:%08u RNR:%08u SREJ:%08u",
  245. p_ccb->fcrb.ertm_pkt_counts[1], p_ccb->fcrb.ertm_byte_counts[1],
  246. (dur >= 10 ? (p_ccb->fcrb.ertm_byte_counts[1] * 100) / (dur / 10) : 0),
  247. p_ccb->fcrb.s_frames_rcvd[0], p_ccb->fcrb.s_frames_rcvd[1], p_ccb->fcrb.s_frames_rcvd[2], p_ccb->fcrb.s_frames_rcvd[3]);
  248. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC, "%s", p_str);
  249. throughput_avg = 0;
  250. ack_delay_avg = 0;
  251. ack_q_count_avg = 0;
  252. for (i = 0; i < L2CAP_ERTM_STATS_NUM_AVG; i++ ) {
  253. if (i == p_ccb->fcrb.ack_delay_avg_index ) {
  254. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
  255. "[%02u] collecting data ...", i );
  256. continue;
  257. }
  258. sprintf(p_str, "[%02u] throughput: %5u, ack_delay avg:%3u, min:%3u, max:%3u, ack_q_count avg:%3u, min:%3u, max:%3u",
  259. i, p_ccb->fcrb.throughput[i],
  260. p_ccb->fcrb.ack_delay_avg[i], p_ccb->fcrb.ack_delay_min[i], p_ccb->fcrb.ack_delay_max[i],
  261. p_ccb->fcrb.ack_q_count_avg[i], p_ccb->fcrb.ack_q_count_min[i], p_ccb->fcrb.ack_q_count_max[i] );
  262. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC, "%s", p_str);
  263. throughput_avg += p_ccb->fcrb.throughput[i];
  264. ack_delay_avg += p_ccb->fcrb.ack_delay_avg[i];
  265. ack_q_count_avg += p_ccb->fcrb.ack_q_count_avg[i];
  266. }
  267. throughput_avg /= (L2CAP_ERTM_STATS_NUM_AVG - 1);
  268. ack_delay_avg /= (L2CAP_ERTM_STATS_NUM_AVG - 1);
  269. ack_q_count_avg /= (L2CAP_ERTM_STATS_NUM_AVG - 1);
  270. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
  271. "throughput_avg: %8u (kbytes/sec), ack_delay_avg: %8u ms, ack_q_count_avg: %8u",
  272. throughput_avg, ack_delay_avg, ack_q_count_avg );
  273. osi_free(p_str);
  274. }
  275. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
  276. "---");
  277. }
  278. #endif
  279. memset (p_fcrb, 0, sizeof (tL2C_FCRB));
  280. }
  281. /*******************************************************************************
  282. **
  283. ** Function l2c_fcr_clone_buf
  284. **
  285. ** Description This function allocates and copies requested part of a buffer
  286. ** at a new-offset.
  287. **
  288. ** Returns pointer to new buffer
  289. **
  290. *******************************************************************************/
  291. BT_HDR *l2c_fcr_clone_buf (BT_HDR *p_buf, UINT16 new_offset, UINT16 no_of_bytes)
  292. {
  293. assert(p_buf != NULL);
  294. /*
  295. * NOTE: We allocate extra L2CAP_FCS_LEN octets, in case we need to put
  296. * the FCS (Frame Check Sequence) at the end of the buffer.
  297. */
  298. uint16_t buf_size = no_of_bytes + sizeof(BT_HDR) + new_offset + L2CAP_FCS_LEN;
  299. #if (L2CAP_ERTM_STATS == TRUE)
  300. /*
  301. * NOTE: If L2CAP_ERTM_STATS is enabled, we need 4 extra octets at the
  302. * end for a timestamp at the end of an I-frame.
  303. */
  304. buf_size += sizeof(uint32_t);
  305. #endif
  306. BT_HDR *p_buf2 = (BT_HDR *)osi_malloc(buf_size);
  307. p_buf2->offset = new_offset;
  308. p_buf2->len = no_of_bytes;
  309. memcpy(((UINT8 *)(p_buf2 + 1)) + p_buf2->offset,
  310. ((UINT8 *)(p_buf + 1)) + p_buf->offset,
  311. no_of_bytes);
  312. return (p_buf2);
  313. }
  314. /*******************************************************************************
  315. **
  316. ** Function l2c_fcr_is_flow_controlled
  317. **
  318. ** Description This function checks if the CCB is flow controlled by peer.
  319. **
  320. ** Returns The control word
  321. **
  322. *******************************************************************************/
  323. BOOLEAN l2c_fcr_is_flow_controlled (tL2C_CCB *p_ccb)
  324. {
  325. assert(p_ccb != NULL);
  326. if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
  327. /* Check if remote side flowed us off or the transmit window is full */
  328. if ( (p_ccb->fcrb.remote_busy == TRUE)
  329. || (fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q) >= p_ccb->peer_cfg.fcr.tx_win_sz) ) {
  330. #if (L2CAP_ERTM_STATS == TRUE)
  331. if (!fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  332. p_ccb->fcrb.xmit_window_closed++;
  333. if ((p_ccb->p_lcb->sent_not_acked < 2) && (l2cb.controller_xmit_window > 0)) {
  334. p_ccb->fcrb.controller_idle++;
  335. }
  336. }
  337. #endif
  338. return (TRUE);
  339. }
  340. }
  341. return (FALSE);
  342. }
  343. /*******************************************************************************
  344. **
  345. ** Function prepare_I_frame
  346. **
  347. ** Description This function sets the FCR variables in an I-frame that is
  348. ** about to be sent to HCI for transmission. This may be the
  349. ** first time the I-frame is sent, or a retransmission
  350. **
  351. ** Returns -
  352. **
  353. *******************************************************************************/
  354. static void prepare_I_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, BOOLEAN is_retransmission)
  355. {
  356. assert(p_ccb != NULL);
  357. assert(p_buf != NULL);
  358. tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
  359. UINT8 *p;
  360. UINT16 fcs;
  361. UINT16 ctrl_word;
  362. BOOLEAN set_f_bit = p_fcrb->send_f_rsp;
  363. p_fcrb->send_f_rsp = FALSE;
  364. if (is_retransmission) {
  365. /* Get the old control word and clear out the old req_seq and F bits */
  366. p = ((UINT8 *) (p_buf + 1)) + p_buf->offset + L2CAP_PKT_OVERHEAD;
  367. STREAM_TO_UINT16 (ctrl_word, p);
  368. ctrl_word &= ~(L2CAP_FCR_REQ_SEQ_BITS + L2CAP_FCR_F_BIT);
  369. } else {
  370. ctrl_word = p_buf->layer_specific & L2CAP_FCR_SEG_BITS; /* SAR bits */
  371. ctrl_word |= (p_fcrb->next_tx_seq << L2CAP_FCR_TX_SEQ_BITS_SHIFT); /* Tx Seq */
  372. p_fcrb->next_tx_seq = (p_fcrb->next_tx_seq + 1) & L2CAP_FCR_SEQ_MODULO;
  373. }
  374. /* Set the F-bit and reqseq only if using re-transmission mode */
  375. if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
  376. if (set_f_bit) {
  377. ctrl_word |= L2CAP_FCR_F_BIT;
  378. }
  379. ctrl_word |= (p_fcrb->next_seq_expected) << L2CAP_FCR_REQ_SEQ_BITS_SHIFT;
  380. p_fcrb->last_ack_sent = p_ccb->fcrb.next_seq_expected;
  381. if (p_ccb->fcrb.ack_timer.in_use) {
  382. btu_stop_quick_timer (&p_ccb->fcrb.ack_timer);
  383. }
  384. }
  385. /* Set the control word */
  386. p = ((UINT8 *) (p_buf + 1)) + p_buf->offset + L2CAP_PKT_OVERHEAD;
  387. UINT16_TO_STREAM (p, ctrl_word);
  388. /* Compute the FCS and add to the end of the buffer if not bypassed */
  389. if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
  390. /* length field in l2cap header has to include FCS length */
  391. p = ((UINT8 *) (p_buf + 1)) + p_buf->offset;
  392. UINT16_TO_STREAM (p, p_buf->len + L2CAP_FCS_LEN - L2CAP_PKT_OVERHEAD);
  393. /* Calculate the FCS */
  394. fcs = l2c_fcr_tx_get_fcs(p_buf);
  395. /* Point to the end of the buffer and put the FCS there */
  396. p = ((UINT8 *) (p_buf + 1)) + p_buf->offset + p_buf->len;
  397. UINT16_TO_STREAM (p, fcs);
  398. p_buf->len += L2CAP_FCS_LEN;
  399. }
  400. #if BT_TRACE_VERBOSE == TRUE
  401. if (is_retransmission) {
  402. L2CAP_TRACE_EVENT ("L2CAP eRTM ReTx I-frame CID: 0x%04x Len: %u SAR: %s TxSeq: %u ReqSeq: %u F: %u",
  403. p_ccb->local_cid, p_buf->len,
  404. SAR_types[(ctrl_word & L2CAP_FCR_SAR_BITS) >> L2CAP_FCR_SAR_BITS_SHIFT],
  405. (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
  406. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  407. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  408. } else {
  409. L2CAP_TRACE_EVENT ("L2CAP eRTM Tx I-frame CID: 0x%04x Len: %u SAR: %-12s TxSeq: %u ReqSeq: %u F: %u",
  410. p_ccb->local_cid, p_buf->len,
  411. SAR_types[(ctrl_word & L2CAP_FCR_SAR_BITS) >> L2CAP_FCR_SAR_BITS_SHIFT],
  412. (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
  413. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  414. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  415. }
  416. #endif
  417. /* Start the retransmission timer if not already running */
  418. if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
  419. l2c_fcr_start_timer (p_ccb);
  420. }
  421. }
  422. /*******************************************************************************
  423. **
  424. ** Function l2c_fcr_send_S_frame
  425. **
  426. ** Description This function formats and sends an S-frame for transmission.
  427. **
  428. ** Returns -
  429. **
  430. *******************************************************************************/
  431. void l2c_fcr_send_S_frame (tL2C_CCB *p_ccb, UINT16 function_code, UINT16 pf_bit)
  432. {
  433. assert(p_ccb != NULL);
  434. BT_HDR *p_buf;
  435. UINT8 *p;
  436. UINT16 ctrl_word;
  437. UINT16 fcs;
  438. if ((!p_ccb->in_use) || (p_ccb->chnl_state != CST_OPEN)) {
  439. return;
  440. }
  441. #if (L2CAP_ERTM_STATS == TRUE)
  442. p_ccb->fcrb.s_frames_sent[function_code]++;
  443. #endif
  444. if (pf_bit == L2CAP_FCR_P_BIT) {
  445. p_ccb->fcrb.wait_ack = TRUE;
  446. l2c_fcr_stop_timer (p_ccb); /* Restart the monitor timer */
  447. l2c_fcr_start_timer (p_ccb);
  448. }
  449. /* Create the control word to use */
  450. ctrl_word = (function_code << L2CAP_FCR_SUP_SHIFT) | L2CAP_FCR_S_FRAME_BIT;
  451. ctrl_word |= (p_ccb->fcrb.next_seq_expected << L2CAP_FCR_REQ_SEQ_BITS_SHIFT);
  452. ctrl_word |= pf_bit;
  453. if ((p_buf = (BT_HDR *)osi_malloc(L2CAP_CMD_BUF_SIZE)) != NULL) {
  454. p_buf->offset = HCI_DATA_PREAMBLE_SIZE;
  455. p_buf->len = L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD;
  456. /* Set the pointer to the beginning of the data */
  457. p = (UINT8 *)(p_buf + 1) + p_buf->offset;
  458. /* Put in the L2CAP header */
  459. UINT16_TO_STREAM (p, L2CAP_FCR_OVERHEAD + L2CAP_FCS_LEN);
  460. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  461. UINT16_TO_STREAM (p, ctrl_word);
  462. /* Compute the FCS and add to the end of the buffer if not bypassed */
  463. if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
  464. fcs = l2c_fcr_tx_get_fcs (p_buf);
  465. UINT16_TO_STREAM (p, fcs);
  466. p_buf->len += L2CAP_FCS_LEN;
  467. } else { /* rewrite the length without FCS length */
  468. p -= 6;
  469. UINT16_TO_STREAM (p, L2CAP_FCR_OVERHEAD);
  470. }
  471. /* Now, the HCI transport header */
  472. p_buf->layer_specific = L2CAP_NON_FLUSHABLE_PKT;
  473. l2cu_set_acl_hci_header (p_buf, p_ccb);
  474. #if BT_TRACE_VERBOSE == TRUE
  475. if ((((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 1)
  476. || (((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 3)) {
  477. L2CAP_TRACE_WARNING ("L2CAP eRTM Tx S-frame CID: 0x%04x ctrlword: 0x%04x Type: %s ReqSeq: %u P: %u F: %u",
  478. p_ccb->local_cid, ctrl_word,
  479. SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
  480. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  481. (ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
  482. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  483. L2CAP_TRACE_WARNING (" Buf Len: %u", p_buf->len);
  484. } else {
  485. L2CAP_TRACE_EVENT ("L2CAP eRTM Tx S-frame CID: 0x%04x ctrlword: 0x%04x Type: %s ReqSeq: %u P: %u F: %u",
  486. p_ccb->local_cid, ctrl_word,
  487. SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
  488. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  489. (ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
  490. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  491. L2CAP_TRACE_EVENT (" Buf Len: %u", p_buf->len);
  492. }
  493. #endif /* BT_TRACE_VERBOSE */
  494. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
  495. p_ccb->fcrb.last_ack_sent = p_ccb->fcrb.next_seq_expected;
  496. if (p_ccb->fcrb.ack_timer.in_use) {
  497. btu_stop_quick_timer (&p_ccb->fcrb.ack_timer);
  498. }
  499. } else {
  500. L2CAP_TRACE_ERROR ("l2c_fcr_send_S_frame(No Resources) cid 0x%04x, Type: 0x%4x",
  501. p_ccb->local_cid, function_code);
  502. }
  503. }
  504. /*******************************************************************************
  505. **
  506. ** Function l2c_fcr_proc_pdu
  507. **
  508. ** Description This function is the entry point for processing of a
  509. ** received PDU when in flow control and/or retransmission modes.
  510. **
  511. ** Returns -
  512. **
  513. *******************************************************************************/
  514. void l2c_fcr_proc_pdu (tL2C_CCB *p_ccb, BT_HDR *p_buf)
  515. {
  516. assert(p_ccb != NULL);
  517. assert(p_buf != NULL);
  518. UINT8 *p;
  519. UINT16 fcs;
  520. UINT16 min_pdu_len;
  521. UINT16 ctrl_word;
  522. /* Check the length */
  523. min_pdu_len = (p_ccb->bypass_fcs == L2CAP_BYPASS_FCS) ?
  524. (UINT16)L2CAP_FCR_OVERHEAD : (UINT16)(L2CAP_FCS_LEN + L2CAP_FCR_OVERHEAD);
  525. if (p_buf->len < min_pdu_len) {
  526. L2CAP_TRACE_WARNING ("Rx L2CAP PDU: CID: 0x%04x Len too short: %u", p_ccb->local_cid, p_buf->len);
  527. osi_free (p_buf);
  528. return;
  529. }
  530. if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_STREAM_MODE) {
  531. process_stream_frame (p_ccb, p_buf);
  532. return;
  533. }
  534. #if BT_TRACE_VERBOSE == TRUE
  535. /* Get the control word */
  536. p = ((UINT8 *)(p_buf + 1)) + p_buf->offset;
  537. STREAM_TO_UINT16 (ctrl_word, p);
  538. if (ctrl_word & L2CAP_FCR_S_FRAME_BIT) {
  539. if ((((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 1)
  540. || (((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 3)) {
  541. /* REJ or SREJ */
  542. L2CAP_TRACE_WARNING ("L2CAP eRTM Rx S-frame: cid: 0x%04x Len: %u Type: %s ReqSeq: %u P: %u F: %u",
  543. p_ccb->local_cid, p_buf->len,
  544. SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
  545. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  546. (ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
  547. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  548. } else {
  549. L2CAP_TRACE_EVENT ("L2CAP eRTM Rx S-frame: cid: 0x%04x Len: %u Type: %s ReqSeq: %u P: %u F: %u",
  550. p_ccb->local_cid, p_buf->len,
  551. SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
  552. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  553. (ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
  554. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  555. }
  556. } else {
  557. L2CAP_TRACE_EVENT ("L2CAP eRTM Rx I-frame: cid: 0x%04x Len: %u SAR: %-12s TxSeq: %u ReqSeq: %u F: %u",
  558. p_ccb->local_cid, p_buf->len,
  559. SAR_types[(ctrl_word & L2CAP_FCR_SAR_BITS) >> L2CAP_FCR_SAR_BITS_SHIFT],
  560. (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
  561. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  562. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  563. }
  564. L2CAP_TRACE_EVENT (" eRTM Rx Nxt_tx_seq %u, Lst_rx_ack %u, Nxt_seq_exp %u, Lst_ack_snt %u, wt_q.cnt %u, tries %u",
  565. p_ccb->fcrb.next_tx_seq, p_ccb->fcrb.last_rx_ack,
  566. p_ccb->fcrb.next_seq_expected,
  567. p_ccb->fcrb.last_ack_sent,
  568. fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q),
  569. p_ccb->fcrb.num_tries);
  570. #endif /* BT_TRACE_VERBOSE */
  571. /* Verify FCS if using */
  572. if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
  573. p = ((UINT8 *)(p_buf + 1)) + p_buf->offset + p_buf->len - L2CAP_FCS_LEN;
  574. /* Extract and drop the FCS from the packet */
  575. STREAM_TO_UINT16 (fcs, p);
  576. p_buf->len -= L2CAP_FCS_LEN;
  577. if (l2c_fcr_rx_get_fcs(p_buf) != fcs) {
  578. L2CAP_TRACE_WARNING ("Rx L2CAP PDU: CID: 0x%04x BAD FCS", p_ccb->local_cid);
  579. osi_free(p_buf);
  580. return;
  581. }
  582. }
  583. /* Get the control word */
  584. p = ((UINT8 *)(p_buf + 1)) + p_buf->offset;
  585. STREAM_TO_UINT16 (ctrl_word, p);
  586. p_buf->len -= L2CAP_FCR_OVERHEAD;
  587. p_buf->offset += L2CAP_FCR_OVERHEAD;
  588. /* If we had a poll bit outstanding, check if we got a final response */
  589. if (p_ccb->fcrb.wait_ack) {
  590. /* If final bit not set, ignore the frame unless it is a polled S-frame */
  591. if ( !(ctrl_word & L2CAP_FCR_F_BIT) ) {
  592. if ( (ctrl_word & L2CAP_FCR_P_BIT) && (ctrl_word & L2CAP_FCR_S_FRAME_BIT) ) {
  593. if (p_ccb->fcrb.srej_sent) {
  594. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_SREJ, L2CAP_FCR_F_BIT);
  595. } else if (p_ccb->fcrb.local_busy) {
  596. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, L2CAP_FCR_F_BIT);
  597. } else {
  598. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_F_BIT);
  599. }
  600. /* Got a poll while in wait_ack state, so re-start our timer with 1-second */
  601. /* This is a small optimization... the monitor timer is 12 secs, but we saw */
  602. /* that if the other side sends us a poll when we are waiting for a final, */
  603. /* then it speeds up recovery significantly if we poll him back soon after his poll. */
  604. btu_start_quick_timer (&p_ccb->fcrb.mon_retrans_timer, BTU_TTYPE_L2CAP_CHNL, QUICK_TIMER_TICKS_PER_SEC);
  605. }
  606. osi_free (p_buf);
  607. return;
  608. }
  609. p_ccb->fcrb.wait_ack = FALSE;
  610. /* P and F are mutually exclusive */
  611. if (ctrl_word & L2CAP_FCR_S_FRAME_BIT) {
  612. ctrl_word &= ~L2CAP_FCR_P_BIT;
  613. }
  614. if (fixed_queue_is_empty(p_ccb->fcrb.waiting_for_ack_q)) {
  615. p_ccb->fcrb.num_tries = 0;
  616. }
  617. l2c_fcr_stop_timer (p_ccb);
  618. } else {
  619. /* Otherwise, ensure the final bit is ignored */
  620. ctrl_word &= ~L2CAP_FCR_F_BIT;
  621. }
  622. /* Process receive sequence number */
  623. if (!process_reqseq (p_ccb, ctrl_word)) {
  624. osi_free (p_buf);
  625. return;
  626. }
  627. /* Process based on whether it is an S-frame or an I-frame */
  628. if (ctrl_word & L2CAP_FCR_S_FRAME_BIT) {
  629. process_s_frame (p_ccb, p_buf, ctrl_word);
  630. } else {
  631. process_i_frame (p_ccb, p_buf, ctrl_word, FALSE);
  632. }
  633. /* Return if the channel got disconnected by a bad packet or max retransmissions */
  634. if ( (!p_ccb->in_use) || (p_ccb->chnl_state != CST_OPEN) ) {
  635. return;
  636. }
  637. /* If we have some buffers held while doing SREJ, and SREJ has cleared, process them now */
  638. if ( (!p_ccb->fcrb.local_busy) && (!p_ccb->fcrb.srej_sent) &&
  639. (!fixed_queue_is_empty(p_ccb->fcrb.srej_rcv_hold_q))) {
  640. fixed_queue_t *temp_q = p_ccb->fcrb.srej_rcv_hold_q;
  641. p_ccb->fcrb.srej_rcv_hold_q = fixed_queue_new(QUEUE_SIZE_MAX);
  642. while ((p_buf = (BT_HDR *)fixed_queue_dequeue(temp_q, 0)) != NULL) {
  643. if (p_ccb->in_use && (p_ccb->chnl_state == CST_OPEN)) {
  644. /* Get the control word */
  645. p = ((UINT8 *)(p_buf + 1)) + p_buf->offset - L2CAP_FCR_OVERHEAD;
  646. STREAM_TO_UINT16 (ctrl_word, p);
  647. L2CAP_TRACE_DEBUG ("l2c_fcr_proc_pdu() CID: 0x%04x Process Buffer from SREJ_Hold_Q TxSeq: %u Expected_Seq: %u",
  648. p_ccb->local_cid, (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
  649. p_ccb->fcrb.next_seq_expected);
  650. /* Process the SREJ held I-frame, but do not send an RR for each individual frame */
  651. process_i_frame (p_ccb, p_buf, ctrl_word, TRUE);
  652. } else {
  653. osi_free (p_buf);
  654. }
  655. /* If more frames were lost during SREJ, send a REJ */
  656. if (p_ccb->fcrb.rej_after_srej) {
  657. p_ccb->fcrb.rej_after_srej = FALSE;
  658. p_ccb->fcrb.rej_sent = TRUE;
  659. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_REJ, 0);
  660. }
  661. }
  662. fixed_queue_free(temp_q, NULL);
  663. /* Now, if needed, send one RR for the whole held queue */
  664. if ( (!p_ccb->fcrb.local_busy) && (!p_ccb->fcrb.rej_sent) && (!p_ccb->fcrb.srej_sent)
  665. && (p_ccb->fcrb.next_seq_expected != p_ccb->fcrb.last_ack_sent) ) {
  666. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, 0);
  667. } else {
  668. L2CAP_TRACE_DEBUG ("l2c_fcr_proc_pdu() not sending RR CID: 0x%04x local_busy:%d rej_sent:%d srej_sent:%d Expected_Seq:%u Last_Ack:%u",
  669. p_ccb->local_cid, p_ccb->fcrb.local_busy, p_ccb->fcrb.rej_sent, p_ccb->fcrb.srej_sent, p_ccb->fcrb.next_seq_expected,
  670. p_ccb->fcrb.last_ack_sent);
  671. }
  672. }
  673. /* If a window has opened, check if we can send any more packets */
  674. if ( (!fixed_queue_is_empty(p_ccb->fcrb.retrans_q) ||
  675. !fixed_queue_is_empty(p_ccb->xmit_hold_q))
  676. && (p_ccb->fcrb.wait_ack == FALSE)
  677. && (l2c_fcr_is_flow_controlled (p_ccb) == FALSE) ) {
  678. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, NULL);
  679. }
  680. }
  681. /*******************************************************************************
  682. **
  683. ** Function l2c_fcr_proc_tout
  684. **
  685. ** Description Handle a timeout. We should be in error recovery state.
  686. **
  687. ** Returns -
  688. **
  689. *******************************************************************************/
  690. void l2c_fcr_proc_tout (tL2C_CCB *p_ccb)
  691. {
  692. assert(p_ccb != NULL);
  693. L2CAP_TRACE_DEBUG ("l2c_fcr_proc_tout: CID: 0x%04x num_tries: %u (max: %u) wait_ack: %u ack_q_count: %u",
  694. p_ccb->local_cid, p_ccb->fcrb.num_tries,
  695. p_ccb->peer_cfg.fcr.max_transmit,
  696. p_ccb->fcrb.wait_ack,
  697. fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q));
  698. #if (L2CAP_ERTM_STATS == TRUE)
  699. p_ccb->fcrb.retrans_touts++;
  700. #endif
  701. if ( (p_ccb->peer_cfg.fcr.max_transmit != 0) && (++p_ccb->fcrb.num_tries > p_ccb->peer_cfg.fcr.max_transmit) ) {
  702. l2cu_disconnect_chnl (p_ccb);
  703. } else {
  704. if (!p_ccb->fcrb.srej_sent && !p_ccb->fcrb.rej_sent) {
  705. if (p_ccb->fcrb.local_busy) {
  706. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, L2CAP_FCR_P_BIT);
  707. } else {
  708. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_P_BIT);
  709. }
  710. }
  711. }
  712. }
  713. /*******************************************************************************
  714. **
  715. ** Function l2c_fcr_proc_ack_tout
  716. **
  717. ** Description Send RR/RNR if we have not acked I frame
  718. **
  719. ** Returns -
  720. **
  721. *******************************************************************************/
  722. void l2c_fcr_proc_ack_tout (tL2C_CCB *p_ccb)
  723. {
  724. assert(p_ccb != NULL);
  725. L2CAP_TRACE_DEBUG ("l2c_fcr_proc_ack_tout: CID: 0x%04x State: %u Wack:%u Rq:%d Acked:%d", p_ccb->local_cid,
  726. p_ccb->chnl_state, p_ccb->fcrb.wait_ack, p_ccb->fcrb.next_seq_expected, p_ccb->fcrb.last_ack_sent);
  727. if ( (p_ccb->chnl_state == CST_OPEN) && (!p_ccb->fcrb.wait_ack)
  728. && (p_ccb->fcrb.last_ack_sent != p_ccb->fcrb.next_seq_expected) ) {
  729. #if (L2CAP_ERTM_STATS == TRUE)
  730. p_ccb->fcrb.xmit_ack_touts++;
  731. #endif
  732. if (p_ccb->fcrb.local_busy) {
  733. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, 0);
  734. } else {
  735. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, 0);
  736. }
  737. }
  738. }
  739. /*******************************************************************************
  740. **
  741. ** Function process_reqseq
  742. **
  743. ** Description Handle receive sequence number
  744. **
  745. ** Returns -
  746. **
  747. *******************************************************************************/
  748. static BOOLEAN process_reqseq (tL2C_CCB *p_ccb, UINT16 ctrl_word)
  749. {
  750. assert(p_ccb != NULL);
  751. tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
  752. UINT8 req_seq, num_bufs_acked, xx;
  753. UINT16 ls;
  754. UINT16 full_sdus_xmitted;
  755. /* Receive sequence number does not ack anything for SREJ with P-bit set to zero */
  756. if ( (ctrl_word & L2CAP_FCR_S_FRAME_BIT)
  757. && ((ctrl_word & L2CAP_FCR_SUP_BITS) == (L2CAP_FCR_SUP_SREJ << L2CAP_FCR_SUP_SHIFT))
  758. && ((ctrl_word & L2CAP_FCR_P_BIT) == 0) ) {
  759. /* If anything still waiting for ack, restart the timer if it was stopped */
  760. if (!fixed_queue_is_empty(p_fcrb->waiting_for_ack_q)) {
  761. l2c_fcr_start_timer(p_ccb);
  762. }
  763. return (TRUE);
  764. }
  765. /* Extract the receive sequence number from the control word */
  766. req_seq = (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT;
  767. num_bufs_acked = (req_seq - p_fcrb->last_rx_ack) & L2CAP_FCR_SEQ_MODULO;
  768. /* Verify the request sequence is in range before proceeding */
  769. if (num_bufs_acked > fixed_queue_length(p_fcrb->waiting_for_ack_q)) {
  770. /* The channel is closed if ReqSeq is not in range */
  771. L2CAP_TRACE_WARNING ("L2CAP eRTM Frame BAD Req_Seq - ctrl_word: 0x%04x req_seq 0x%02x last_rx_ack: 0x%02x QCount: %u",
  772. ctrl_word, req_seq, p_fcrb->last_rx_ack,
  773. fixed_queue_length(p_fcrb->waiting_for_ack_q));
  774. l2cu_disconnect_chnl (p_ccb);
  775. return (FALSE);
  776. }
  777. p_fcrb->last_rx_ack = req_seq;
  778. /* Now we can release all acknowledged frames, and restart the retransmission timer if needed */
  779. if (num_bufs_acked != 0) {
  780. p_fcrb->num_tries = 0;
  781. full_sdus_xmitted = 0;
  782. #if (L2CAP_ERTM_STATS == TRUE)
  783. l2c_fcr_collect_ack_delay (p_ccb, num_bufs_acked);
  784. #endif
  785. for (xx = 0; xx < num_bufs_acked; xx++) {
  786. BT_HDR *p_tmp = (BT_HDR *)fixed_queue_dequeue(p_fcrb->waiting_for_ack_q, 0);
  787. ls = p_tmp->layer_specific & L2CAP_FCR_SAR_BITS;
  788. if ( (ls == L2CAP_FCR_UNSEG_SDU) || (ls == L2CAP_FCR_END_SDU) ) {
  789. full_sdus_xmitted++;
  790. }
  791. osi_free(p_tmp);
  792. if (p_ccb->cong_sent) {
  793. l2cu_check_channel_congestion(p_ccb);
  794. }
  795. }
  796. /* If we are still in a wait_ack state, do not mess with the timer */
  797. if (!p_ccb->fcrb.wait_ack) {
  798. l2c_fcr_stop_timer (p_ccb);
  799. }
  800. /* Check if we need to call the "packet_sent" callback */
  801. if ( (p_ccb->p_rcb) && (p_ccb->p_rcb->api.pL2CA_TxComplete_Cb) && (full_sdus_xmitted) ) {
  802. /* Special case for eRTM, if all packets sent, send 0xFFFF */
  803. if (fixed_queue_is_empty(p_fcrb->waiting_for_ack_q) &&
  804. fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
  805. full_sdus_xmitted = 0xFFFF;
  806. }
  807. (*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, full_sdus_xmitted);
  808. }
  809. }
  810. /* If anything still waiting for ack, restart the timer if it was stopped */
  811. if (!fixed_queue_is_empty(p_fcrb->waiting_for_ack_q)) {
  812. l2c_fcr_start_timer (p_ccb);
  813. }
  814. return (TRUE);
  815. }
  816. /*******************************************************************************
  817. **
  818. ** Function process_s_frame
  819. **
  820. ** Description Process an S frame
  821. **
  822. ** Returns -
  823. **
  824. *******************************************************************************/
  825. static void process_s_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word)
  826. {
  827. assert(p_ccb != NULL);
  828. assert(p_buf != NULL);
  829. tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
  830. UINT16 s_frame_type = (ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT;
  831. BOOLEAN remote_was_busy;
  832. BOOLEAN all_ok = TRUE;
  833. if (p_buf->len != 0) {
  834. L2CAP_TRACE_WARNING ("Incorrect S-frame Length (%d)", p_buf->len);
  835. }
  836. L2CAP_TRACE_DEBUG ("process_s_frame ctrl_word 0x%04x fcrb_remote_busy:%d", ctrl_word, p_fcrb->remote_busy);
  837. #if (L2CAP_ERTM_STATS == TRUE)
  838. p_ccb->fcrb.s_frames_rcvd[s_frame_type]++;
  839. #endif
  840. if (ctrl_word & L2CAP_FCR_P_BIT) {
  841. p_fcrb->rej_sent = FALSE; /* After checkpoint, we can send anoher REJ */
  842. p_fcrb->send_f_rsp = TRUE; /* Set a flag in case an I-frame is pending */
  843. }
  844. switch (s_frame_type) {
  845. case L2CAP_FCR_SUP_RR:
  846. remote_was_busy = p_fcrb->remote_busy;
  847. p_fcrb->remote_busy = FALSE;
  848. if ( (ctrl_word & L2CAP_FCR_F_BIT) || (remote_was_busy) ) {
  849. all_ok = retransmit_i_frames (p_ccb, L2C_FCR_RETX_ALL_PKTS);
  850. }
  851. break;
  852. case L2CAP_FCR_SUP_REJ:
  853. p_fcrb->remote_busy = FALSE;
  854. all_ok = retransmit_i_frames (p_ccb, L2C_FCR_RETX_ALL_PKTS);
  855. break;
  856. case L2CAP_FCR_SUP_RNR:
  857. p_fcrb->remote_busy = TRUE;
  858. l2c_fcr_stop_timer (p_ccb);
  859. break;
  860. case L2CAP_FCR_SUP_SREJ:
  861. p_fcrb->remote_busy = FALSE;
  862. all_ok = retransmit_i_frames (p_ccb, (UINT8)((ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT));
  863. break;
  864. }
  865. if (all_ok) {
  866. /* If polled, we need to respond with F-bit. Note, we may have sent a I-frame with the F-bit */
  867. if (p_fcrb->send_f_rsp) {
  868. if (p_fcrb->srej_sent) {
  869. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_SREJ, L2CAP_FCR_F_BIT);
  870. } else if (p_fcrb->local_busy) {
  871. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, L2CAP_FCR_F_BIT);
  872. } else {
  873. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_F_BIT);
  874. }
  875. p_fcrb->send_f_rsp = FALSE;
  876. }
  877. } else {
  878. L2CAP_TRACE_DEBUG ("process_s_frame hit_max_retries");
  879. }
  880. osi_free (p_buf);
  881. }
  882. /*******************************************************************************
  883. **
  884. ** Function process_i_frame
  885. **
  886. ** Description Process an I frame
  887. **
  888. ** Returns -
  889. **
  890. *******************************************************************************/
  891. static void process_i_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word, BOOLEAN delay_ack)
  892. {
  893. assert(p_ccb != NULL);
  894. assert(p_buf != NULL);
  895. tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
  896. UINT8 tx_seq, num_lost, num_to_ack, next_srej;
  897. /* If we were doing checkpoint recovery, first retransmit all unacked I-frames */
  898. if (ctrl_word & L2CAP_FCR_F_BIT) {
  899. if (!retransmit_i_frames (p_ccb, L2C_FCR_RETX_ALL_PKTS)) {
  900. osi_free(p_buf);
  901. return;
  902. }
  903. }
  904. #if (L2CAP_ERTM_STATS == TRUE)
  905. p_ccb->fcrb.ertm_pkt_counts[1]++;
  906. p_ccb->fcrb.ertm_byte_counts[1] += p_buf->len;
  907. #endif
  908. /* Extract the sequence number */
  909. tx_seq = (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT;
  910. /* If we have flow controlled the peer, ignore any bad I-frames from him */
  911. if ( (tx_seq != p_fcrb->next_seq_expected) && (p_fcrb->local_busy) ) {
  912. L2CAP_TRACE_WARNING ("Dropping bad I-Frame since we flowed off, tx_seq:%u", tx_seq);
  913. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, 0);
  914. osi_free(p_buf);
  915. return;
  916. }
  917. /* Check if tx-sequence is the expected one */
  918. if (tx_seq != p_fcrb->next_seq_expected) {
  919. num_lost = (tx_seq - p_fcrb->next_seq_expected) & L2CAP_FCR_SEQ_MODULO;
  920. /* Is the frame a duplicate ? If so, just drop it */
  921. if (num_lost >= p_ccb->our_cfg.fcr.tx_win_sz) {
  922. /* Duplicate - simply drop it */
  923. L2CAP_TRACE_WARNING ("process_i_frame() Dropping Duplicate Frame tx_seq:%u ExpectedTxSeq %u", tx_seq, p_fcrb->next_seq_expected);
  924. osi_free(p_buf);
  925. } else {
  926. L2CAP_TRACE_WARNING ("process_i_frame() CID: 0x%04x Lost: %u tx_seq:%u ExpTxSeq %u Rej: %u SRej: %u",
  927. p_ccb->local_cid, num_lost, tx_seq, p_fcrb->next_seq_expected, p_fcrb->rej_sent, p_fcrb->srej_sent);
  928. if (p_fcrb->srej_sent) {
  929. /* If SREJ sent, save the frame for later processing as long as it is in sequence */
  930. next_srej = (((BT_HDR *)fixed_queue_try_peek_last(p_fcrb->srej_rcv_hold_q))->layer_specific + 1) & L2CAP_FCR_SEQ_MODULO;
  931. if ( (tx_seq == next_srej) && (fixed_queue_length(p_fcrb->srej_rcv_hold_q) < p_ccb->our_cfg.fcr.tx_win_sz) ) {
  932. /* If user gave us a pool for held rx buffers, use that */
  933. /* TODO: Could that happen? Get rid of this code. */
  934. if (p_ccb->ertm_info.fcr_rx_buf_size != L2CAP_FCR_RX_BUF_SIZE) {
  935. BT_HDR *p_buf2;
  936. /* Adjust offset and len so that control word is copied */
  937. p_buf->offset -= L2CAP_FCR_OVERHEAD;
  938. p_buf->len += L2CAP_FCR_OVERHEAD;
  939. p_buf2 = l2c_fcr_clone_buf (p_buf, p_buf->offset, p_buf->len);
  940. if (p_buf2) {
  941. osi_free (p_buf);
  942. p_buf = p_buf2;
  943. }
  944. p_buf->offset += L2CAP_FCR_OVERHEAD;
  945. p_buf->len -= L2CAP_FCR_OVERHEAD;
  946. }
  947. L2CAP_TRACE_DEBUG ("process_i_frame() Lost: %u tx_seq:%u ExpTxSeq %u Rej: %u SRej1",
  948. num_lost, tx_seq, p_fcrb->next_seq_expected, p_fcrb->rej_sent);
  949. p_buf->layer_specific = tx_seq;
  950. fixed_queue_enqueue(p_fcrb->srej_rcv_hold_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  951. } else {
  952. L2CAP_TRACE_WARNING ("process_i_frame() CID: 0x%04x frame dropped in Srej Sent next_srej:%u hold_q.count:%u win_sz:%u",
  953. p_ccb->local_cid, next_srej, fixed_queue_length(p_fcrb->srej_rcv_hold_q), p_ccb->our_cfg.fcr.tx_win_sz);
  954. p_fcrb->rej_after_srej = TRUE;
  955. osi_free (p_buf);
  956. }
  957. } else if (p_fcrb->rej_sent) {
  958. L2CAP_TRACE_WARNING ("process_i_frame() CID: 0x%04x Lost: %u tx_seq:%u ExpTxSeq %u Rej: 1 SRej: %u",
  959. p_ccb->local_cid, num_lost, tx_seq, p_fcrb->next_seq_expected, p_fcrb->srej_sent);
  960. /* If REJ sent, just drop the frame */
  961. osi_free (p_buf);
  962. } else {
  963. L2CAP_TRACE_DEBUG ("process_i_frame() CID: 0x%04x tx_seq:%u ExpTxSeq %u Rej: %u",
  964. p_ccb->local_cid, tx_seq, p_fcrb->next_seq_expected, p_fcrb->rej_sent);
  965. /* If only one lost, we will send SREJ, otherwise we will send REJ */
  966. if (num_lost > 1) {
  967. osi_free (p_buf);
  968. p_fcrb->rej_sent = TRUE;
  969. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_REJ, 0);
  970. } else {
  971. if (!fixed_queue_is_empty(p_fcrb->srej_rcv_hold_q)) {
  972. L2CAP_TRACE_ERROR ("process_i_frame() CID: 0x%04x sending SREJ tx_seq:%d hold_q.count:%u",
  973. p_ccb->local_cid, tx_seq, fixed_queue_length(p_fcrb->srej_rcv_hold_q));
  974. }
  975. p_buf->layer_specific = tx_seq;
  976. fixed_queue_enqueue(p_fcrb->srej_rcv_hold_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  977. p_fcrb->srej_sent = TRUE;
  978. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_SREJ, 0);
  979. }
  980. btu_stop_quick_timer (&p_ccb->fcrb.ack_timer);
  981. }
  982. }
  983. return;
  984. }
  985. /* Seq number is the next expected. Clear possible reject exception in case it occured */
  986. p_fcrb->rej_sent = p_fcrb->srej_sent = FALSE;
  987. /* Adjust the next_seq, so that if the upper layer sends more data in the callback
  988. context, the received frame is acked by an I-frame. */
  989. p_fcrb->next_seq_expected = (tx_seq + 1) & L2CAP_FCR_SEQ_MODULO;
  990. /* If any SAR problem in eRTM mode, spec says disconnect. */
  991. if (!do_sar_reassembly (p_ccb, p_buf, ctrl_word)) {
  992. L2CAP_TRACE_WARNING ("process_i_frame() CID: 0x%04x reassembly failed", p_ccb->local_cid);
  993. l2cu_disconnect_chnl (p_ccb);
  994. return;
  995. }
  996. /* RR optimization - if peer can still send us more, then start an ACK timer */
  997. num_to_ack = (p_fcrb->next_seq_expected - p_fcrb->last_ack_sent) & L2CAP_FCR_SEQ_MODULO;
  998. if ( (num_to_ack < p_ccb->fcrb.max_held_acks) && (!p_fcrb->local_busy) ) {
  999. delay_ack = TRUE;
  1000. }
  1001. /* We should neve never ack frame if we are not in OPEN state */
  1002. if ((num_to_ack != 0) && p_ccb->in_use && (p_ccb->chnl_state == CST_OPEN)) {
  1003. /* If no frames are awaiting transmission or are held, send an RR or RNR S-frame for ack */
  1004. if (delay_ack) {
  1005. /* If it is the first I frame we did not ack, start ack timer */
  1006. if (!p_ccb->fcrb.ack_timer.in_use) {
  1007. btu_start_quick_timer (&p_ccb->fcrb.ack_timer, BTU_TTYPE_L2CAP_FCR_ACK,
  1008. (L2CAP_FCR_ACK_TOUT * QUICK_TIMER_TICKS_PER_SEC) / 1000);
  1009. }
  1010. } else if ((fixed_queue_is_empty(p_ccb->xmit_hold_q) ||
  1011. l2c_fcr_is_flow_controlled(p_ccb))
  1012. && fixed_queue_is_empty(p_ccb->fcrb.srej_rcv_hold_q)) {
  1013. if (p_fcrb->local_busy) {
  1014. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, 0);
  1015. } else {
  1016. l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, 0);
  1017. }
  1018. }
  1019. }
  1020. }
  1021. /*******************************************************************************
  1022. **
  1023. ** Function process_stream_frame
  1024. **
  1025. ** Description This function processes frames in streaming mode
  1026. **
  1027. ** Returns -
  1028. **
  1029. *******************************************************************************/
  1030. static void process_stream_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf)
  1031. {
  1032. assert(p_ccb != NULL);
  1033. assert(p_buf != NULL);
  1034. UINT16 ctrl_word;
  1035. UINT16 fcs;
  1036. UINT8 *p;
  1037. UINT8 tx_seq;
  1038. /* Verify FCS if using */
  1039. if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
  1040. p = ((UINT8 *)(p_buf + 1)) + p_buf->offset + p_buf->len - L2CAP_FCS_LEN;
  1041. /* Extract and drop the FCS from the packet */
  1042. STREAM_TO_UINT16 (fcs, p);
  1043. p_buf->len -= L2CAP_FCS_LEN;
  1044. if (l2c_fcr_rx_get_fcs(p_buf) != fcs) {
  1045. L2CAP_TRACE_WARNING ("Rx L2CAP PDU: CID: 0x%04x BAD FCS", p_ccb->local_cid);
  1046. osi_free(p_buf);
  1047. return;
  1048. }
  1049. }
  1050. /* Get the control word */
  1051. p = ((UINT8 *)(p_buf + 1)) + p_buf->offset;
  1052. STREAM_TO_UINT16 (ctrl_word, p);
  1053. p_buf->len -= L2CAP_FCR_OVERHEAD;
  1054. p_buf->offset += L2CAP_FCR_OVERHEAD;
  1055. /* Make sure it is an I-frame */
  1056. if (ctrl_word & L2CAP_FCR_S_FRAME_BIT) {
  1057. L2CAP_TRACE_WARNING ("Rx L2CAP PDU: CID: 0x%04x BAD S-frame in streaming mode ctrl_word: 0x%04x", p_ccb->local_cid, ctrl_word);
  1058. osi_free (p_buf);
  1059. return;
  1060. }
  1061. #if BT_TRACE_VERBOSE == TRUE
  1062. L2CAP_TRACE_EVENT ("L2CAP eRTM Rx I-frame: cid: 0x%04x Len: %u SAR: %-12s TxSeq: %u ReqSeq: %u F: %u",
  1063. p_ccb->local_cid, p_buf->len,
  1064. SAR_types[(ctrl_word & L2CAP_FCR_SAR_BITS) >> L2CAP_FCR_SAR_BITS_SHIFT],
  1065. (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
  1066. (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
  1067. (ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
  1068. #endif
  1069. /* Extract the sequence number */
  1070. tx_seq = (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT;
  1071. /* Check if tx-sequence is the expected one */
  1072. if (tx_seq != p_ccb->fcrb.next_seq_expected) {
  1073. L2CAP_TRACE_WARNING ("Rx L2CAP PDU: CID: 0x%04x Lost frames Exp: %u Got: %u p_rx_sdu: %p",
  1074. p_ccb->local_cid, p_ccb->fcrb.next_seq_expected, tx_seq, p_ccb->fcrb.p_rx_sdu);
  1075. /* Lost one or more packets, so flush the SAR queue */
  1076. if (p_ccb->fcrb.p_rx_sdu != NULL) {
  1077. osi_free(p_ccb->fcrb.p_rx_sdu);
  1078. p_ccb->fcrb.p_rx_sdu = NULL;
  1079. }
  1080. }
  1081. p_ccb->fcrb.next_seq_expected = (tx_seq + 1) & L2CAP_FCR_SEQ_MODULO;
  1082. if (!do_sar_reassembly (p_ccb, p_buf, ctrl_word)) {
  1083. /* Some sort of SAR error, so flush the SAR queue */
  1084. if (p_ccb->fcrb.p_rx_sdu != NULL) {
  1085. osi_free(p_ccb->fcrb.p_rx_sdu);
  1086. p_ccb->fcrb.p_rx_sdu = NULL;
  1087. }
  1088. }
  1089. }
  1090. /*******************************************************************************
  1091. **
  1092. ** Function do_sar_reassembly
  1093. **
  1094. ** Description Process SAR bits and re-assemble frame
  1095. **
  1096. ** Returns TRUE if all OK, else FALSE
  1097. **
  1098. *******************************************************************************/
  1099. static BOOLEAN do_sar_reassembly (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word)
  1100. {
  1101. assert(p_ccb != NULL);
  1102. assert(p_buf != NULL);
  1103. tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
  1104. UINT16 sar_type = ctrl_word & L2CAP_FCR_SEG_BITS;
  1105. BOOLEAN packet_ok = TRUE;
  1106. UINT8 *p;
  1107. /* Check if the SAR state is correct */
  1108. if ((sar_type == L2CAP_FCR_UNSEG_SDU) || (sar_type == L2CAP_FCR_START_SDU)) {
  1109. if (p_fcrb->p_rx_sdu != NULL) {
  1110. L2CAP_TRACE_WARNING ("SAR - got unexpected unsegmented or start SDU Expected len: %u Got so far: %u",
  1111. p_fcrb->rx_sdu_len, p_fcrb->p_rx_sdu->len);
  1112. packet_ok = FALSE;
  1113. }
  1114. /* Check the length of the packet */
  1115. if ( (sar_type == L2CAP_FCR_START_SDU) && (p_buf->len < L2CAP_SDU_LEN_OVERHEAD) ) {
  1116. L2CAP_TRACE_WARNING ("SAR start packet too short: %u", p_buf->len);
  1117. packet_ok = FALSE;
  1118. }
  1119. } else {
  1120. if (p_fcrb->p_rx_sdu == NULL) {
  1121. L2CAP_TRACE_WARNING ("SAR - got unexpected cont or end SDU");
  1122. packet_ok = FALSE;
  1123. }
  1124. }
  1125. if ( (packet_ok) && (sar_type != L2CAP_FCR_UNSEG_SDU) ) {
  1126. p = ((UINT8 *)(p_buf + 1)) + p_buf->offset;
  1127. /* For start SDU packet, extract the SDU length */
  1128. if (sar_type == L2CAP_FCR_START_SDU) {
  1129. /* Get the SDU length */
  1130. STREAM_TO_UINT16 (p_fcrb->rx_sdu_len, p);
  1131. p_buf->offset += 2;
  1132. p_buf->len -= 2;
  1133. if (p_fcrb->rx_sdu_len > p_ccb->max_rx_mtu) {
  1134. L2CAP_TRACE_WARNING ("SAR - SDU len: %u larger than MTU: %u", p_fcrb->rx_sdu_len, p_fcrb->rx_sdu_len);
  1135. packet_ok = FALSE;
  1136. } else if ((p_fcrb->p_rx_sdu = (BT_HDR *)osi_malloc(L2CAP_MAX_BUF_SIZE)) == NULL) {
  1137. L2CAP_TRACE_ERROR ("SAR - no buffer for SDU start user_rx_buf_size:%d", p_ccb->ertm_info.user_rx_buf_size);
  1138. packet_ok = FALSE;
  1139. } else {
  1140. p_fcrb->p_rx_sdu->offset = 4; /* this is the minimal offset required by OBX to process incoming packets */
  1141. p_fcrb->p_rx_sdu->len = 0;
  1142. }
  1143. }
  1144. if (packet_ok) {
  1145. if ((p_fcrb->p_rx_sdu->len + p_buf->len) > p_fcrb->rx_sdu_len) {
  1146. L2CAP_TRACE_ERROR ("SAR - SDU len exceeded Type: %u Lengths: %u %u %u",
  1147. sar_type, p_fcrb->p_rx_sdu->len, p_buf->len, p_fcrb->rx_sdu_len);
  1148. packet_ok = FALSE;
  1149. } else if ( (sar_type == L2CAP_FCR_END_SDU) && ((p_fcrb->p_rx_sdu->len + p_buf->len) != p_fcrb->rx_sdu_len) ) {
  1150. L2CAP_TRACE_WARNING ("SAR - SDU end rcvd but SDU incomplete: %u %u %u",
  1151. p_fcrb->p_rx_sdu->len, p_buf->len, p_fcrb->rx_sdu_len);
  1152. packet_ok = FALSE;
  1153. } else {
  1154. memcpy (((UINT8 *) (p_fcrb->p_rx_sdu + 1)) + p_fcrb->p_rx_sdu->offset + p_fcrb->p_rx_sdu->len, p, p_buf->len);
  1155. p_fcrb->p_rx_sdu->len += p_buf->len;
  1156. osi_free (p_buf);
  1157. p_buf = NULL;
  1158. if (sar_type == L2CAP_FCR_END_SDU) {
  1159. p_buf = p_fcrb->p_rx_sdu;
  1160. p_fcrb->p_rx_sdu = NULL;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. if (packet_ok == FALSE) {
  1166. osi_free (p_buf);
  1167. } else if (p_buf != NULL) {
  1168. #if (L2CAP_NUM_FIXED_CHNLS > 0)
  1169. if (p_ccb->local_cid < L2CAP_BASE_APPL_CID &&
  1170. (p_ccb->local_cid >= L2CAP_FIRST_FIXED_CHNL && p_ccb->local_cid <= L2CAP_LAST_FIXED_CHNL)) {
  1171. if (l2cb.fixed_reg[p_ccb->local_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)
  1172. (*l2cb.fixed_reg[p_ccb->local_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)
  1173. (p_ccb->local_cid, p_ccb->p_lcb->remote_bd_addr, p_buf);
  1174. } else
  1175. #endif
  1176. {
  1177. l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_buf);
  1178. }
  1179. }
  1180. return (packet_ok);
  1181. }
  1182. /*******************************************************************************
  1183. **
  1184. ** Function retransmit_i_frames
  1185. **
  1186. ** Description This function retransmits i-frames awaiting acks.
  1187. **
  1188. ** Returns BOOLEAN - TRUE if retransmitted
  1189. **
  1190. *******************************************************************************/
  1191. static BOOLEAN retransmit_i_frames (tL2C_CCB *p_ccb, UINT8 tx_seq)
  1192. {
  1193. assert(p_ccb != NULL);
  1194. BT_HDR *p_buf = NULL;
  1195. UINT8 *p;
  1196. UINT8 buf_seq;
  1197. UINT16 ctrl_word;
  1198. if ( (!fixed_queue_is_empty(p_ccb->fcrb.waiting_for_ack_q))
  1199. && (p_ccb->peer_cfg.fcr.max_transmit != 0)
  1200. && (p_ccb->fcrb.num_tries >= p_ccb->peer_cfg.fcr.max_transmit) ) {
  1201. L2CAP_TRACE_EVENT ("Max Tries Exceeded: (last_acq: %d CID: 0x%04x num_tries: %u (max: %u) ack_q_count: %u",
  1202. p_ccb->fcrb.last_rx_ack, p_ccb->local_cid, p_ccb->fcrb.num_tries, p_ccb->peer_cfg.fcr.max_transmit,
  1203. fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q));
  1204. l2cu_disconnect_chnl (p_ccb);
  1205. return (FALSE);
  1206. }
  1207. /* tx_seq indicates whether to retransmit a specific sequence or all (if == L2C_FCR_RETX_ALL_PKTS) */
  1208. list_t *list_ack = NULL;
  1209. const list_node_t *node_ack = NULL;
  1210. if (! fixed_queue_is_empty(p_ccb->fcrb.waiting_for_ack_q)) {
  1211. list_ack = fixed_queue_get_list(p_ccb->fcrb.waiting_for_ack_q);
  1212. node_ack = list_begin(list_ack);
  1213. }
  1214. if (tx_seq != L2C_FCR_RETX_ALL_PKTS) {
  1215. /* If sending only one, the sequence number tells us which one. Look for it.
  1216. */
  1217. if (list_ack != NULL) {
  1218. for ( ; node_ack != list_end(list_ack); node_ack = list_next(node_ack)) {
  1219. p_buf = (BT_HDR *)list_node(node_ack);
  1220. /* Get the old control word */
  1221. p = ((UINT8 *) (p_buf+1)) + p_buf->offset + L2CAP_PKT_OVERHEAD;
  1222. STREAM_TO_UINT16 (ctrl_word, p);
  1223. buf_seq = (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT;
  1224. L2CAP_TRACE_DEBUG ("retransmit_i_frames() cur seq: %u looking for: %u", buf_seq, tx_seq);
  1225. if (tx_seq == buf_seq) {
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. if (!p_buf) {
  1231. L2CAP_TRACE_ERROR ("retransmit_i_frames() UNKNOWN seq: %u q_count: %u",
  1232. tx_seq,
  1233. fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q));
  1234. return (TRUE);
  1235. }
  1236. } else {
  1237. // Iterate though list and flush the amount requested from
  1238. // the transmit data queue that satisfy the layer and event conditions.
  1239. for (const list_node_t *node = list_begin(p_ccb->p_lcb->link_xmit_data_q);
  1240. node != list_end(p_ccb->p_lcb->link_xmit_data_q);) {
  1241. BT_HDR *p_buf = (BT_HDR *)list_node(node);
  1242. node = list_next(node);
  1243. /* Do not flush other CIDs or partial segments */
  1244. if ((p_buf->layer_specific == 0) && (p_buf->event == p_ccb->local_cid)) {
  1245. list_remove(p_ccb->p_lcb->link_xmit_data_q, p_buf);
  1246. osi_free(p_buf);
  1247. }
  1248. }
  1249. /* Also flush our retransmission queue */
  1250. while (!fixed_queue_is_empty(p_ccb->fcrb.retrans_q)) {
  1251. osi_free(fixed_queue_dequeue(p_ccb->fcrb.retrans_q, 0));
  1252. }
  1253. if (list_ack != NULL) {
  1254. node_ack = list_begin(list_ack);
  1255. }
  1256. }
  1257. if (list_ack != NULL) {
  1258. while (node_ack != list_end(list_ack))
  1259. {
  1260. p_buf = (BT_HDR *)list_node(node_ack);
  1261. node_ack = list_next(node_ack);
  1262. BT_HDR *p_buf2 = l2c_fcr_clone_buf(p_buf, p_buf->offset, p_buf->len);
  1263. if (p_buf2)
  1264. {
  1265. p_buf2->layer_specific = p_buf->layer_specific;
  1266. fixed_queue_enqueue(p_ccb->fcrb.retrans_q, p_buf2, FIXED_QUEUE_MAX_TIMEOUT);
  1267. }
  1268. if ( (tx_seq != L2C_FCR_RETX_ALL_PKTS) || (p_buf2 == NULL) ) {
  1269. break;
  1270. }
  1271. }
  1272. }
  1273. l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, NULL);
  1274. if (fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q))
  1275. {
  1276. p_ccb->fcrb.num_tries++;
  1277. l2c_fcr_start_timer (p_ccb);
  1278. }
  1279. return (TRUE);
  1280. }
  1281. /*******************************************************************************
  1282. **
  1283. ** Function l2c_fcr_get_next_xmit_sdu_seg
  1284. **
  1285. ** Description Get the next SDU segment to transmit.
  1286. **
  1287. ** Returns pointer to buffer with segment or NULL
  1288. **
  1289. *******************************************************************************/
  1290. BT_HDR *l2c_fcr_get_next_xmit_sdu_seg (tL2C_CCB *p_ccb, UINT16 max_packet_length)
  1291. {
  1292. assert(p_ccb != NULL);
  1293. BOOLEAN first_seg = FALSE, /* The segment is the first part of data */
  1294. mid_seg = FALSE, /* The segment is the middle part of data */
  1295. last_seg = FALSE; /* The segment is the last part of data */
  1296. UINT16 sdu_len = 0;
  1297. BT_HDR *p_buf, *p_xmit;
  1298. UINT8 *p;
  1299. UINT16 max_pdu = p_ccb->tx_mps /* Needed? - L2CAP_MAX_HEADER_FCS*/;
  1300. /* If there is anything in the retransmit queue, that goes first
  1301. */
  1302. p_buf = (BT_HDR *)fixed_queue_dequeue(p_ccb->fcrb.retrans_q, 0);
  1303. if (p_buf != NULL) {
  1304. /* Update Rx Seq and FCS if we acked some packets while this one was queued */
  1305. prepare_I_frame (p_ccb, p_buf, TRUE);
  1306. p_buf->event = p_ccb->local_cid;
  1307. #if (L2CAP_ERTM_STATS == TRUE)
  1308. p_ccb->fcrb.pkts_retransmitted++;
  1309. p_ccb->fcrb.ertm_pkt_counts[0]++;
  1310. p_ccb->fcrb.ertm_byte_counts[0] += (p_buf->len - 8);
  1311. #endif
  1312. return (p_buf);
  1313. }
  1314. /* For BD/EDR controller, max_packet_length is set to 0 */
  1315. /* For AMP controller, max_packet_length is set by available blocks */
  1316. if ( (max_packet_length > L2CAP_MAX_HEADER_FCS)
  1317. && (max_pdu + L2CAP_MAX_HEADER_FCS > max_packet_length) ) {
  1318. max_pdu = max_packet_length - L2CAP_MAX_HEADER_FCS;
  1319. }
  1320. p_buf = (BT_HDR *)fixed_queue_try_peek_first(p_ccb->xmit_hold_q);
  1321. /* If there is more data than the MPS, it requires segmentation */
  1322. if (p_buf->len > max_pdu) {
  1323. /* We are using the "event" field to tell is if we already started segmentation */
  1324. if (p_buf->event == 0) {
  1325. first_seg = TRUE;
  1326. sdu_len = p_buf->len;
  1327. } else {
  1328. mid_seg = TRUE;
  1329. }
  1330. /* Get a new buffer and copy the data that can be sent in a PDU */
  1331. p_xmit = l2c_fcr_clone_buf (p_buf, L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET,
  1332. max_pdu);
  1333. if (p_xmit != NULL) {
  1334. p_buf->event = p_ccb->local_cid;
  1335. p_xmit->event = p_ccb->local_cid;
  1336. p_buf->len -= max_pdu;
  1337. p_buf->offset += max_pdu;
  1338. /* copy PBF setting */
  1339. p_xmit->layer_specific = p_buf->layer_specific;
  1340. } else { /* Should never happen if the application has configured buffers correctly */
  1341. L2CAP_TRACE_ERROR ("L2CAP - cannot get buffer for segmentation, max_pdu: %u", max_pdu);
  1342. return (NULL);
  1343. }
  1344. } else { /* Use the original buffer if no segmentation, or the last segment */
  1345. p_xmit = (BT_HDR *)fixed_queue_dequeue(p_ccb->xmit_hold_q, 0);
  1346. if (p_xmit->event != 0) {
  1347. last_seg = TRUE;
  1348. }
  1349. p_xmit->event = p_ccb->local_cid;
  1350. }
  1351. /* Step back to add the L2CAP headers */
  1352. p_xmit->offset -= (L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD);
  1353. p_xmit->len += L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD;
  1354. if (first_seg) {
  1355. p_xmit->offset -= L2CAP_SDU_LEN_OVERHEAD;
  1356. p_xmit->len += L2CAP_SDU_LEN_OVERHEAD;
  1357. }
  1358. /* Set the pointer to the beginning of the data */
  1359. p = (UINT8 *)(p_xmit + 1) + p_xmit->offset;
  1360. /* Now the L2CAP header */
  1361. /* Note: if FCS has to be included then the length is recalculated later */
  1362. UINT16_TO_STREAM (p, p_xmit->len - L2CAP_PKT_OVERHEAD);
  1363. UINT16_TO_STREAM (p, p_ccb->remote_cid);
  1364. if (first_seg) {
  1365. /* Skip control word and add SDU length */
  1366. p += 2;
  1367. UINT16_TO_STREAM (p, sdu_len);
  1368. /* We will store the SAR type in layer-specific */
  1369. /* layer_specific is shared with flushable flag(bits 0-1), don't clear it */
  1370. p_xmit->layer_specific |= L2CAP_FCR_START_SDU;
  1371. first_seg = FALSE;
  1372. } else if (mid_seg) {
  1373. p_xmit->layer_specific |= L2CAP_FCR_CONT_SDU;
  1374. } else if (last_seg) {
  1375. p_xmit->layer_specific |= L2CAP_FCR_END_SDU;
  1376. } else {
  1377. p_xmit->layer_specific |= L2CAP_FCR_UNSEG_SDU;
  1378. }
  1379. prepare_I_frame (p_ccb, p_xmit, FALSE);
  1380. if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
  1381. BT_HDR *p_wack = l2c_fcr_clone_buf (p_xmit, HCI_DATA_PREAMBLE_SIZE, p_xmit->len);
  1382. if (!p_wack) {
  1383. L2CAP_TRACE_ERROR("L2CAP - no buffer for xmit cloning, CID: 0x%04x Length: %u",
  1384. p_ccb->local_cid, p_xmit->len);
  1385. /* We will not save the FCS in case we reconfigure and change options */
  1386. if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
  1387. p_xmit->len -= L2CAP_FCS_LEN;
  1388. }
  1389. /* Pretend we sent it and it got lost */
  1390. fixed_queue_enqueue(p_ccb->fcrb.waiting_for_ack_q, p_xmit, FIXED_QUEUE_MAX_TIMEOUT);
  1391. return (NULL);
  1392. } else {
  1393. #if (L2CAP_ERTM_STATS == TRUE)
  1394. /* set timestamp at the end of tx I-frame to get acking delay */
  1395. p = ((UINT8 *) (p_wack + 1)) + p_wack->offset + p_wack->len;
  1396. UINT32_TO_STREAM (p, osi_time_get_os_boottime_ms());
  1397. #endif
  1398. /* We will not save the FCS in case we reconfigure and change options */
  1399. if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
  1400. p_wack->len -= L2CAP_FCS_LEN;
  1401. }
  1402. p_wack->layer_specific = p_xmit->layer_specific;
  1403. fixed_queue_enqueue(p_ccb->fcrb.waiting_for_ack_q, p_wack, FIXED_QUEUE_MAX_TIMEOUT);
  1404. }
  1405. #if (L2CAP_ERTM_STATS == TRUE)
  1406. p_ccb->fcrb.ertm_pkt_counts[0]++;
  1407. p_ccb->fcrb.ertm_byte_counts[0] += (p_xmit->len - 8);
  1408. #endif
  1409. }
  1410. return (p_xmit);
  1411. }
  1412. /*******************************************************************************
  1413. ** Configuration negotiation functions
  1414. **
  1415. ** The following functions are used in negotiating channel modes during
  1416. ** configuration
  1417. ********************************************************************************/
  1418. /*******************************************************************************
  1419. **
  1420. ** Function l2c_fcr_chk_chan_modes
  1421. **
  1422. ** Description Validates and adjusts if necessary, the FCR options
  1423. ** based on remote EXT features.
  1424. **
  1425. ** Note: This assumes peer EXT Features have been received.
  1426. ** Basic mode is used if FCR Options have not been received
  1427. **
  1428. ** Returns UINT8 - nonzero if can continue, '0' if no compatible channels
  1429. **
  1430. *******************************************************************************/
  1431. UINT8 l2c_fcr_chk_chan_modes (tL2C_CCB *p_ccb)
  1432. {
  1433. assert(p_ccb != NULL);
  1434. /* Remove nonbasic options that the peer does not support */
  1435. if (!(p_ccb->p_lcb->peer_ext_fea & L2CAP_EXTFEA_ENH_RETRANS)) {
  1436. p_ccb->ertm_info.allowed_modes &= ~L2CAP_FCR_CHAN_OPT_ERTM;
  1437. }
  1438. if (!(p_ccb->p_lcb->peer_ext_fea & L2CAP_EXTFEA_STREAM_MODE)) {
  1439. p_ccb->ertm_info.allowed_modes &= ~L2CAP_FCR_CHAN_OPT_STREAM;
  1440. }
  1441. /* At least one type needs to be set (Basic, ERTM, STM) to continue */
  1442. if (!p_ccb->ertm_info.allowed_modes) {
  1443. L2CAP_TRACE_WARNING ("L2CAP - Peer does not support our desired channel types");
  1444. }
  1445. return (p_ccb->ertm_info.allowed_modes);
  1446. }
  1447. /*******************************************************************************
  1448. **
  1449. ** Function l2c_fcr_adj_our_req_options
  1450. **
  1451. ** Description Validates and sets up the FCR options passed in from
  1452. ** L2CA_ConfigReq based on remote device's features.
  1453. **
  1454. ** Returns TRUE if no errors, Otherwise FALSE
  1455. **
  1456. *******************************************************************************/
  1457. BOOLEAN l2c_fcr_adj_our_req_options (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1458. {
  1459. assert(p_ccb != NULL);
  1460. assert(p_cfg != NULL);
  1461. tL2CAP_FCR_OPTS *p_fcr = &p_cfg->fcr;
  1462. if (p_fcr->mode != p_ccb->ertm_info.preferred_mode) {
  1463. L2CAP_TRACE_WARNING ("l2c_fcr_adj_our_req_options - preferred_mode (%d), does not match mode (%d)",
  1464. p_ccb->ertm_info.preferred_mode, p_fcr->mode);
  1465. /* The preferred mode is passed in through tL2CAP_ERTM_INFO, so override this one */
  1466. p_fcr->mode = p_ccb->ertm_info.preferred_mode;
  1467. }
  1468. /* If upper layer did not request eRTM mode, BASIC must be used */
  1469. if (p_ccb->ertm_info.allowed_modes == L2CAP_FCR_CHAN_OPT_BASIC) {
  1470. if (p_cfg->fcr_present && p_fcr->mode != L2CAP_FCR_BASIC_MODE) {
  1471. L2CAP_TRACE_WARNING ("l2c_fcr_adj_our_req_options (mode %d): ERROR: No FCR options set using BASIC mode", p_fcr->mode);
  1472. }
  1473. p_fcr->mode = L2CAP_FCR_BASIC_MODE;
  1474. }
  1475. /* Process the FCR options if initial channel bring-up (not a reconfig request)
  1476. ** Determine initial channel mode to try based on our options and remote's features
  1477. */
  1478. if (p_cfg->fcr_present && !(p_ccb->config_done & RECONFIG_FLAG)) {
  1479. /* We need to have at least one mode type common with the peer */
  1480. if (!l2c_fcr_chk_chan_modes(p_ccb)) {
  1481. /* Two channels have incompatible supported types */
  1482. l2cu_disconnect_chnl (p_ccb);
  1483. return (FALSE);
  1484. }
  1485. /* Basic is the only common channel mode between the two devices */
  1486. else if (p_ccb->ertm_info.allowed_modes == L2CAP_FCR_CHAN_OPT_BASIC) {
  1487. /* We only want to try Basic, so bypass sending the FCR options entirely */
  1488. p_cfg->fcr_present = FALSE;
  1489. p_cfg->fcs_present = FALSE; /* Illegal to use FCS option in basic mode */
  1490. p_cfg->ext_flow_spec_present = FALSE; /* Illegal to use extended flow spec in basic mode */
  1491. }
  1492. /* We have at least one non-basic mode available
  1493. * Override mode from available mode options based on preference, if needed
  1494. */
  1495. else {
  1496. /* If peer does not support STREAMING, try ERTM */
  1497. if (p_fcr->mode == L2CAP_FCR_STREAM_MODE && !(p_ccb->ertm_info.allowed_modes & L2CAP_FCR_CHAN_OPT_STREAM)) {
  1498. L2CAP_TRACE_DEBUG ("L2C CFG: mode is STREAM, but peer does not support; Try ERTM");
  1499. p_fcr->mode = L2CAP_FCR_ERTM_MODE;
  1500. }
  1501. /* If peer does not support ERTM, try BASIC (will support this if made it here in the code) */
  1502. if (p_fcr->mode == L2CAP_FCR_ERTM_MODE && !(p_ccb->ertm_info.allowed_modes & L2CAP_FCR_CHAN_OPT_ERTM)) {
  1503. L2CAP_TRACE_DEBUG ("L2C CFG: mode is ERTM, but peer does not support; Try BASIC");
  1504. p_fcr->mode = L2CAP_FCR_BASIC_MODE;
  1505. }
  1506. }
  1507. if (p_fcr->mode != L2CAP_FCR_BASIC_MODE) {
  1508. /* MTU must be smaller than buffer size */
  1509. if ( (p_cfg->mtu_present) && (p_cfg->mtu > p_ccb->max_rx_mtu) ) {
  1510. L2CAP_TRACE_WARNING ("L2CAP - MTU: %u larger than buf size: %u", p_cfg->mtu, p_ccb->max_rx_mtu);
  1511. return (FALSE);
  1512. }
  1513. /* application want to use the default MPS */
  1514. if (p_fcr->mps == L2CAP_DEFAULT_ERM_MPS) {
  1515. p_fcr->mps = L2CAP_MPS_OVER_BR_EDR;
  1516. }
  1517. /* MPS must be less than MTU */
  1518. else if (p_fcr->mps > p_ccb->max_rx_mtu) {
  1519. L2CAP_TRACE_WARNING ("L2CAP - MPS %u invalid MTU: %u", p_fcr->mps, p_ccb->max_rx_mtu);
  1520. return (FALSE);
  1521. }
  1522. /* We always initially read into the HCI buffer pool, so make sure it fits */
  1523. if (p_fcr->mps > (L2CAP_MTU_SIZE - L2CAP_MAX_HEADER_FCS)) {
  1524. p_fcr->mps = L2CAP_MTU_SIZE - L2CAP_MAX_HEADER_FCS;
  1525. }
  1526. } else {
  1527. p_cfg->fcs_present = FALSE; /* Illegal to use FCS option in basic mode */
  1528. p_cfg->ext_flow_spec_present = FALSE; /* Illegal to use extended flow spec in basic mode */
  1529. }
  1530. p_ccb->our_cfg.fcr = *p_fcr;
  1531. } else { /* Not sure how to send a reconfiguration(??) should fcr be included? */
  1532. p_ccb->our_cfg.fcr_present = FALSE;
  1533. }
  1534. return (TRUE);
  1535. }
  1536. /*******************************************************************************
  1537. **
  1538. ** Function l2c_fcr_adj_monitor_retran_timeout
  1539. **
  1540. ** Description Overrides monitor/retrans timer value based on controller
  1541. **
  1542. ** Returns None
  1543. **
  1544. *******************************************************************************/
  1545. void l2c_fcr_adj_monitor_retran_timeout (tL2C_CCB *p_ccb)
  1546. {
  1547. assert(p_ccb != NULL);
  1548. /* adjust our monitor/retran timeout */
  1549. if (p_ccb->out_cfg_fcr_present) {
  1550. /*
  1551. ** if we requestd ERTM or accepted ERTM
  1552. ** We may accept ERTM even if we didn't request ERTM, in case of requesting STREAM
  1553. */
  1554. if ((p_ccb->our_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE)
  1555. || (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE)) {
  1556. /* upper layer setting is ignored */
  1557. p_ccb->our_cfg.fcr.mon_tout = L2CAP_MIN_MONITOR_TOUT;
  1558. p_ccb->our_cfg.fcr.rtrans_tout = L2CAP_MIN_RETRANS_TOUT;
  1559. } else {
  1560. p_ccb->our_cfg.fcr.mon_tout = 0;
  1561. p_ccb->our_cfg.fcr.rtrans_tout = 0;
  1562. }
  1563. L2CAP_TRACE_DEBUG ("l2c_fcr_adj_monitor_retran_timeout: mon_tout:%d, rtrans_tout:%d",
  1564. p_ccb->our_cfg.fcr.mon_tout, p_ccb->our_cfg.fcr.rtrans_tout);
  1565. }
  1566. }
  1567. /*******************************************************************************
  1568. **
  1569. ** Function l2c_fcr_adj_our_rsp_options
  1570. **
  1571. ** Description Overrides any neccesary FCR options passed in from
  1572. ** L2CA_ConfigRsp based on our FCR options.
  1573. ** Only makes adjustments if channel is in ERTM mode.
  1574. **
  1575. ** Returns None
  1576. **
  1577. *******************************************************************************/
  1578. void l2c_fcr_adj_our_rsp_options (tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1579. {
  1580. assert(p_ccb != NULL);
  1581. assert(p_cfg != NULL);
  1582. /* adjust our monitor/retran timeout */
  1583. l2c_fcr_adj_monitor_retran_timeout(p_ccb);
  1584. p_cfg->fcr_present = p_ccb->out_cfg_fcr_present;
  1585. if (p_cfg->fcr_present) {
  1586. // btla-specific ++
  1587. /* Temporary - until a better algorithm is implemented */
  1588. /* If peer's tx_wnd_sz requires too many buffers for us to support, then adjust it. For now, respond with our own tx_wnd_sz. */
  1589. /* Note: peer is not guaranteed to obey our adjustment */
  1590. if (p_ccb->peer_cfg.fcr.tx_win_sz > p_ccb->our_cfg.fcr.tx_win_sz) {
  1591. L2CAP_TRACE_DEBUG ("%s: adjusting requested tx_win_sz from %i to %i", __FUNCTION__, p_ccb->peer_cfg.fcr.tx_win_sz, p_ccb->our_cfg.fcr.tx_win_sz);
  1592. p_ccb->peer_cfg.fcr.tx_win_sz = p_ccb->our_cfg.fcr.tx_win_sz;
  1593. }
  1594. // btla-specific --
  1595. p_cfg->fcr.mode = p_ccb->peer_cfg.fcr.mode;
  1596. p_cfg->fcr.tx_win_sz = p_ccb->peer_cfg.fcr.tx_win_sz;
  1597. p_cfg->fcr.max_transmit = p_ccb->peer_cfg.fcr.max_transmit;
  1598. p_cfg->fcr.mps = p_ccb->peer_cfg.fcr.mps;
  1599. p_cfg->fcr.rtrans_tout = p_ccb->our_cfg.fcr.rtrans_tout;
  1600. p_cfg->fcr.mon_tout = p_ccb->our_cfg.fcr.mon_tout;
  1601. }
  1602. }
  1603. /*******************************************************************************
  1604. **
  1605. ** Function l2c_fcr_renegotiate_chan
  1606. **
  1607. ** Description Called upon unsuccessful peer response to config request.
  1608. ** If the error is because of the channel mode, it will try
  1609. ** to resend using another supported optional channel.
  1610. **
  1611. ** Returns TRUE if resent configuration, False if channel matches or
  1612. ** cannot match.
  1613. **
  1614. *******************************************************************************/
  1615. BOOLEAN l2c_fcr_renegotiate_chan(tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1616. {
  1617. assert(p_ccb != NULL);
  1618. assert(p_cfg != NULL);
  1619. UINT8 peer_mode = p_ccb->our_cfg.fcr.mode;
  1620. BOOLEAN can_renegotiate;
  1621. /* Skip if this is a reconfiguration from OPEN STATE or if FCR is not returned */
  1622. if (!p_cfg->fcr_present || (p_ccb->config_done & RECONFIG_FLAG)) {
  1623. return (FALSE);
  1624. }
  1625. /* Only retry if there are more channel options to try */
  1626. if (p_cfg->result == L2CAP_CFG_UNACCEPTABLE_PARAMS) {
  1627. peer_mode = (p_cfg->fcr_present) ? p_cfg->fcr.mode : L2CAP_FCR_BASIC_MODE;
  1628. if (p_ccb->our_cfg.fcr.mode != peer_mode) {
  1629. if ((--p_ccb->fcr_cfg_tries) == 0) {
  1630. p_cfg->result = L2CAP_CFG_FAILED_NO_REASON;
  1631. L2CAP_TRACE_WARNING ("l2c_fcr_renegotiate_chan (Max retries exceeded)");
  1632. }
  1633. can_renegotiate = FALSE;
  1634. /* Try another supported mode if available based on our last attempted channel */
  1635. switch (p_ccb->our_cfg.fcr.mode) {
  1636. /* Our Streaming mode request was unnacceptable; try ERTM or Basic */
  1637. case L2CAP_FCR_STREAM_MODE:
  1638. /* Peer wants ERTM and we support it */
  1639. if ( (peer_mode == L2CAP_FCR_ERTM_MODE) && (p_ccb->ertm_info.allowed_modes & L2CAP_FCR_CHAN_OPT_ERTM) ) {
  1640. L2CAP_TRACE_DEBUG ("l2c_fcr_renegotiate_chan(Trying ERTM)");
  1641. p_ccb->our_cfg.fcr.mode = L2CAP_FCR_ERTM_MODE;
  1642. can_renegotiate = TRUE;
  1643. } else /* Falls through */
  1644. case L2CAP_FCR_ERTM_MODE: {
  1645. /* We can try basic for any other peer mode if we support it */
  1646. if (p_ccb->ertm_info.allowed_modes & L2CAP_FCR_CHAN_OPT_BASIC) {
  1647. L2CAP_TRACE_DEBUG ("l2c_fcr_renegotiate_chan(Trying Basic)");
  1648. can_renegotiate = TRUE;
  1649. p_ccb->our_cfg.fcr.mode = L2CAP_FCR_BASIC_MODE;
  1650. }
  1651. }
  1652. break;
  1653. default:
  1654. /* All other scenarios cannot be renegotiated */
  1655. break;
  1656. }
  1657. if (can_renegotiate) {
  1658. p_ccb->our_cfg.fcr_present = TRUE;
  1659. if (p_ccb->our_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE) {
  1660. p_ccb->our_cfg.fcs_present = FALSE;
  1661. p_ccb->our_cfg.ext_flow_spec_present = FALSE;
  1662. /* Basic Mode uses ACL Data Pool, make sure the MTU fits */
  1663. if ( (p_cfg->mtu_present) && (p_cfg->mtu > L2CAP_MTU_SIZE) ) {
  1664. L2CAP_TRACE_WARNING ("L2CAP - adjust MTU: %u too large", p_cfg->mtu);
  1665. p_cfg->mtu = L2CAP_MTU_SIZE;
  1666. }
  1667. }
  1668. l2cu_process_our_cfg_req (p_ccb, &p_ccb->our_cfg);
  1669. l2cu_send_peer_config_req (p_ccb, &p_ccb->our_cfg);
  1670. btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_L2CAP_CHNL, L2CAP_CHNL_CFG_TIMEOUT);
  1671. return (TRUE);
  1672. }
  1673. }
  1674. }
  1675. /* Disconnect if the channels do not match */
  1676. if (p_ccb->our_cfg.fcr.mode != peer_mode) {
  1677. L2CAP_TRACE_WARNING ("L2C CFG: Channels incompatible (local %d, peer %d)",
  1678. p_ccb->our_cfg.fcr.mode, peer_mode);
  1679. l2cu_disconnect_chnl (p_ccb);
  1680. }
  1681. return (FALSE);
  1682. }
  1683. /*******************************************************************************
  1684. **
  1685. ** Function l2c_fcr_process_peer_cfg_req
  1686. **
  1687. ** Description This function is called to process the FCR options passed
  1688. ** in the peer's configuration request.
  1689. **
  1690. ** Returns UINT8 - L2CAP_PEER_CFG_OK, L2CAP_PEER_CFG_UNACCEPTABLE,
  1691. ** or L2CAP_PEER_CFG_DISCONNECT.
  1692. **
  1693. *******************************************************************************/
  1694. UINT8 l2c_fcr_process_peer_cfg_req(tL2C_CCB *p_ccb, tL2CAP_CFG_INFO *p_cfg)
  1695. {
  1696. assert(p_ccb != NULL);
  1697. assert(p_cfg != NULL);
  1698. UINT16 max_retrans_size;
  1699. UINT8 fcr_ok = L2CAP_PEER_CFG_OK;
  1700. p_ccb->p_lcb->w4_info_rsp = FALSE; /* Handles T61x SonyEricsson Bug in Info Request */
  1701. L2CAP_TRACE_EVENT ("l2c_fcr_process_peer_cfg_req() CFG fcr_present:%d fcr.mode:%d CCB FCR mode:%d preferred: %u allowed:%u",
  1702. p_cfg->fcr_present, p_cfg->fcr.mode, p_ccb->our_cfg.fcr.mode, p_ccb->ertm_info.preferred_mode,
  1703. p_ccb->ertm_info.allowed_modes);
  1704. /* If Peer wants basic, we are done (accept it or disconnect) */
  1705. if (p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE) {
  1706. /* If we do not allow basic, disconnect */
  1707. if ( !(p_ccb->ertm_info.allowed_modes & L2CAP_FCR_CHAN_OPT_BASIC) ) {
  1708. fcr_ok = L2CAP_PEER_CFG_DISCONNECT;
  1709. }
  1710. }
  1711. /* Need to negotiate if our modes are not the same */
  1712. else if (p_cfg->fcr.mode != p_ccb->ertm_info.preferred_mode) {
  1713. /* If peer wants a mode that we don't support then retry our mode (ex. rtx/flc), OR
  1714. ** If we want ERTM and they wanted streaming retry our mode.
  1715. ** Note: If we have already determined they support our mode previously
  1716. ** from their EXF mask.
  1717. */
  1718. if ( (((1 << p_cfg->fcr.mode) & L2CAP_FCR_CHAN_OPT_ALL_MASK) == 0)
  1719. || (p_ccb->ertm_info.preferred_mode == L2CAP_FCR_ERTM_MODE) ) {
  1720. p_cfg->fcr.mode = p_ccb->our_cfg.fcr.mode;
  1721. p_cfg->fcr.tx_win_sz = p_ccb->our_cfg.fcr.tx_win_sz;
  1722. p_cfg->fcr.max_transmit = p_ccb->our_cfg.fcr.max_transmit;
  1723. fcr_ok = L2CAP_PEER_CFG_UNACCEPTABLE;
  1724. }
  1725. /* If we wanted basic, then try to renegotiate it */
  1726. else if (p_ccb->ertm_info.preferred_mode == L2CAP_FCR_BASIC_MODE) {
  1727. p_cfg->fcr.mode = L2CAP_FCR_BASIC_MODE;
  1728. p_cfg->fcr.max_transmit = p_cfg->fcr.tx_win_sz = 0;
  1729. p_cfg->fcr.rtrans_tout = p_cfg->fcr.mon_tout = p_cfg->fcr.mps = 0;
  1730. p_ccb->our_cfg.fcr.rtrans_tout = p_ccb->our_cfg.fcr.mon_tout = p_ccb->our_cfg.fcr.mps = 0;
  1731. fcr_ok = L2CAP_PEER_CFG_UNACCEPTABLE;
  1732. }
  1733. /* Only other valid case is if they want ERTM and we wanted STM which should be
  1734. accepted if we support it; otherwise the channel should be disconnected */
  1735. else if ( (p_cfg->fcr.mode != L2CAP_FCR_ERTM_MODE)
  1736. || !(p_ccb->ertm_info.allowed_modes & L2CAP_FCR_CHAN_OPT_ERTM) ) {
  1737. fcr_ok = L2CAP_PEER_CFG_DISCONNECT;
  1738. }
  1739. }
  1740. /* Configuration for FCR channels so make any adjustments and fwd to upper layer */
  1741. if (fcr_ok == L2CAP_PEER_CFG_OK) {
  1742. /* by default don't need to send params in the response */
  1743. p_ccb->out_cfg_fcr_present = FALSE;
  1744. /* Make any needed adjustments for the response to the peer */
  1745. if (p_cfg->fcr_present && p_cfg->fcr.mode != L2CAP_FCR_BASIC_MODE) {
  1746. /* Peer desires to bypass FCS check, and streaming or ERTM mode */
  1747. if (p_cfg->fcs_present) {
  1748. p_ccb->peer_cfg.fcs = p_cfg->fcs;
  1749. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_FCS;
  1750. if ( p_cfg->fcs == L2CAP_CFG_FCS_BYPASS) {
  1751. p_ccb->bypass_fcs |= L2CAP_CFG_FCS_PEER;
  1752. }
  1753. }
  1754. max_retrans_size = p_ccb->ertm_info.fcr_tx_buf_size - sizeof(BT_HDR)
  1755. - L2CAP_MIN_OFFSET - L2CAP_SDU_LEN_OFFSET - L2CAP_FCS_LEN;
  1756. /* Ensure the MPS is not bigger than the MTU */
  1757. if ( (p_cfg->fcr.mps == 0) || (p_cfg->fcr.mps > p_ccb->peer_cfg.mtu) ) {
  1758. p_cfg->fcr.mps = p_ccb->peer_cfg.mtu;
  1759. p_ccb->out_cfg_fcr_present = TRUE;
  1760. }
  1761. /* Ensure the MPS is not bigger than our retransmission buffer */
  1762. if (p_cfg->fcr.mps > max_retrans_size) {
  1763. L2CAP_TRACE_DEBUG("CFG: Overriding MPS to %d (orig %d)", max_retrans_size, p_cfg->fcr.mps);
  1764. p_cfg->fcr.mps = max_retrans_size;
  1765. p_ccb->out_cfg_fcr_present = TRUE;
  1766. }
  1767. if (p_cfg->fcr.mode == L2CAP_FCR_ERTM_MODE || p_cfg->fcr.mode == L2CAP_FCR_STREAM_MODE) {
  1768. /* Always respond with FCR ERTM parameters */
  1769. p_ccb->out_cfg_fcr_present = TRUE;
  1770. }
  1771. }
  1772. /* Everything ok, so save the peer's adjusted fcr options */
  1773. p_ccb->peer_cfg.fcr = p_cfg->fcr;
  1774. if (p_cfg->fcr_present) {
  1775. p_ccb->peer_cfg_bits |= L2CAP_CH_CFG_MASK_FCR;
  1776. }
  1777. } else if (fcr_ok == L2CAP_PEER_CFG_UNACCEPTABLE) {
  1778. /* Allow peer only one retry for mode */
  1779. if (p_ccb->peer_cfg_already_rejected) {
  1780. fcr_ok = L2CAP_PEER_CFG_DISCONNECT;
  1781. } else {
  1782. p_ccb->peer_cfg_already_rejected = TRUE;
  1783. }
  1784. }
  1785. return (fcr_ok);
  1786. }
  1787. #if (L2CAP_ERTM_STATS == TRUE)
  1788. /*******************************************************************************
  1789. **
  1790. ** Function l2c_fcr_collect_ack_delay
  1791. **
  1792. ** Description collect throughput, delay, queue size of waiting ack
  1793. **
  1794. ** Parameters
  1795. ** tL2C_CCB
  1796. **
  1797. ** Returns void
  1798. **
  1799. *******************************************************************************/
  1800. static void l2c_fcr_collect_ack_delay (tL2C_CCB *p_ccb, UINT8 num_bufs_acked)
  1801. {
  1802. UINT32 index;
  1803. BT_HDR *p_buf;
  1804. UINT8 *p;
  1805. UINT32 timestamp, delay;
  1806. UINT8 xx;
  1807. UINT8 str[120];
  1808. index = p_ccb->fcrb.ack_delay_avg_index;
  1809. /* update sum, max and min of waiting for ack queue size */
  1810. p_ccb->fcrb.ack_q_count_avg[index] +=
  1811. fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q);
  1812. if (fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q) > p_ccb->fcrb.ack_q_count_max[index]) {
  1813. p_ccb->fcrb.ack_q_count_max[index] = fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q);
  1814. }
  1815. if (fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q) < p_ccb->fcrb.ack_q_count_min[index]) {
  1816. p_ccb->fcrb.ack_q_count_min[index] = fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q);
  1817. }
  1818. /* update sum, max and min of round trip delay of acking */
  1819. list_t *list = NULL;
  1820. if (! fixed_queue_is_empty(p_ccb->fcrb.waiting_for_ack_q)) {
  1821. list = fixed_queue_get_list(p_ccb->fcrb.waiting_for_ack_q);
  1822. }
  1823. if (list != NULL) {
  1824. for (const list_node_t *node = list_begin(list), xx = 0;
  1825. (node != list_end(list)) && (xx < num_bufs_acked);
  1826. node = list_next(node), xx++) {
  1827. p_buf = list_node(node);
  1828. /* adding up length of acked I-frames to get throughput */
  1829. p_ccb->fcrb.throughput[index] += p_buf->len - 8;
  1830. if ( xx == num_bufs_acked - 1 ) {
  1831. /* get timestamp from tx I-frame that receiver is acking */
  1832. p = ((UINT8 *) (p_buf+1)) + p_buf->offset + p_buf->len;
  1833. if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
  1834. p += L2CAP_FCS_LEN;
  1835. }
  1836. STREAM_TO_UINT32(timestamp, p);
  1837. delay = osi_time_get_os_boottime_ms() - timestamp;
  1838. p_ccb->fcrb.ack_delay_avg[index] += delay;
  1839. if ( delay > p_ccb->fcrb.ack_delay_max[index] ) {
  1840. p_ccb->fcrb.ack_delay_max[index] = delay;
  1841. }
  1842. if ( delay < p_ccb->fcrb.ack_delay_min[index] ) {
  1843. p_ccb->fcrb.ack_delay_min[index] = delay;
  1844. }
  1845. }
  1846. }
  1847. }
  1848. p_ccb->fcrb.ack_delay_avg_count++;
  1849. /* calculate average and initialize next avg, min and max */
  1850. if (p_ccb->fcrb.ack_delay_avg_count > L2CAP_ERTM_STATS_AVG_NUM_SAMPLES) {
  1851. p_ccb->fcrb.ack_delay_avg_count = 0;
  1852. p_ccb->fcrb.ack_q_count_avg[index] /= L2CAP_ERTM_STATS_AVG_NUM_SAMPLES;
  1853. p_ccb->fcrb.ack_delay_avg[index] /= L2CAP_ERTM_STATS_AVG_NUM_SAMPLES;
  1854. /* calculate throughput */
  1855. timestamp = osi_time_get_os_boottime_ms();
  1856. if (timestamp - p_ccb->fcrb.throughput_start > 0 ) {
  1857. p_ccb->fcrb.throughput[index] /= (timestamp - p_ccb->fcrb.throughput_start);
  1858. }
  1859. p_ccb->fcrb.throughput_start = timestamp;
  1860. sprintf(str, "[%02u] throughput: %5u, ack_delay avg:%3u, min:%3u, max:%3u, ack_q_count avg:%3u, min:%3u, max:%3u",
  1861. index, p_ccb->fcrb.throughput[index],
  1862. p_ccb->fcrb.ack_delay_avg[index], p_ccb->fcrb.ack_delay_min[index], p_ccb->fcrb.ack_delay_max[index],
  1863. p_ccb->fcrb.ack_q_count_avg[index], p_ccb->fcrb.ack_q_count_min[index], p_ccb->fcrb.ack_q_count_max[index] );
  1864. BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC, "%s", str);
  1865. index = (index + 1) % L2CAP_ERTM_STATS_NUM_AVG;
  1866. p_ccb->fcrb.ack_delay_avg_index = index;
  1867. p_ccb->fcrb.ack_q_count_max[index] = 0;
  1868. p_ccb->fcrb.ack_q_count_min[index] = 0xFFFFFFFF;
  1869. p_ccb->fcrb.ack_q_count_avg[index] = 0;
  1870. p_ccb->fcrb.ack_delay_max[index] = 0;
  1871. p_ccb->fcrb.ack_delay_min[index] = 0xFFFFFFFF;
  1872. p_ccb->fcrb.ack_delay_avg[index] = 0;
  1873. p_ccb->fcrb.throughput[index] = 0;
  1874. }
  1875. }
  1876. #endif
  1877. #endif /// (L2CAP_COC_INCLUDED == TRUE)