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