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