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