1 /* 2 * Copyright (C) 2009 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 21 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * l2cap.c 34 * 35 * Logical Link Control and Adaption Protocl (L2CAP) 36 * 37 * Created by Matthias Ringwald on 5/16/09. 38 */ 39 40 #include "l2cap.h" 41 #include "hci.h" 42 #include "hci_dump.h" 43 #include "debug.h" 44 #include "btstack_memory.h" 45 46 #include <stdarg.h> 47 #include <string.h> 48 49 #include <stdio.h> 50 51 // size of HCI ACL + L2CAP Header for regular data packets (8) 52 #define COMPLETE_L2CAP_HEADER (HCI_ACL_DATA_PKT_HDR + L2CAP_HEADER_SIZE) 53 54 // minimum signaling MTU 55 #define L2CAP_MINIMAL_MTU 48 56 #define L2CAP_DEFAULT_MTU 672 57 58 // nr of buffered acl packets in outgoing queue to get max performance 59 #define NR_BUFFERED_ACL_PACKETS 3 60 61 // used to cache l2cap rejects, echo, and informational requests 62 #define NR_PENDING_SIGNALING_RESPONSES 5 63 64 // offsets for L2CAP SIGNALING COMMANDS 65 #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 66 #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 67 #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 68 #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 69 70 static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 71 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 72 73 // used to cache l2cap rejects, echo, and informational requests 74 static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 75 static int signaling_responses_pending; 76 77 // static buffers 78 static uint8_t sig_buffer[HCI_ACL_DATA_PKT_HDR + L2CAP_HEADER_SIZE + L2CAP_MINIMAL_MTU]; 79 static uint8_t acl_buffer[HCI_ACL_DATA_PKT_HDR + HCI_ACL_BUFFER_SIZE]; 80 81 static linked_list_t l2cap_channels = NULL; 82 static linked_list_t l2cap_services = NULL; 83 static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 84 static int new_credits_blocked = 0; 85 86 // prototypes 87 static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 88 static l2cap_service_t * l2cap_get_service(uint16_t psm); 89 static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 90 static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 91 static void l2cap_emit_connection_request(l2cap_channel_t *channel); 92 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 93 94 95 void l2cap_init(){ 96 97 new_credits_blocked = 0; 98 signaling_responses_pending = 0; 99 100 // 101 // register callback with HCI 102 // 103 hci_register_packet_handler(&l2cap_packet_handler); 104 } 105 106 107 /** Register L2CAP packet handlers */ 108 static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 109 } 110 void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 111 packet_handler = handler; 112 } 113 114 // notify client/protocol handler 115 void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 116 if (channel->packet_handler) { 117 (* (channel->packet_handler))(type, channel->local_cid, data, size); 118 } else { 119 (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 120 } 121 } 122 123 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 124 uint8_t event[21]; 125 event[0] = L2CAP_EVENT_CHANNEL_OPENED; 126 event[1] = sizeof(event) - 2; 127 event[2] = status; 128 bt_flip_addr(&event[3], channel->address); 129 bt_store_16(event, 9, channel->handle); 130 bt_store_16(event, 11, channel->psm); 131 bt_store_16(event, 13, channel->local_cid); 132 bt_store_16(event, 15, channel->remote_cid); 133 bt_store_16(event, 17, channel->local_mtu); 134 bt_store_16(event, 19, channel->remote_mtu); 135 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 136 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 137 } 138 139 void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 140 uint8_t event[4]; 141 event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 142 event[1] = sizeof(event) - 2; 143 bt_store_16(event, 2, channel->local_cid); 144 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 145 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 146 } 147 148 void l2cap_emit_connection_request(l2cap_channel_t *channel) { 149 uint8_t event[16]; 150 event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 151 event[1] = sizeof(event) - 2; 152 bt_flip_addr(&event[2], channel->address); 153 bt_store_16(event, 8, channel->handle); 154 bt_store_16(event, 10, channel->psm); 155 bt_store_16(event, 12, channel->local_cid); 156 bt_store_16(event, 14, channel->remote_cid); 157 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 158 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 159 } 160 161 void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 162 // track credits 163 channel->packets_granted += credits; 164 // log_info("l2cap_emit_credits for cid %u, credits given: %u (+%u)\n", channel->local_cid, channel->packets_granted, credits); 165 166 uint8_t event[5]; 167 event[0] = L2CAP_EVENT_CREDITS; 168 event[1] = sizeof(event) - 2; 169 bt_store_16(event, 2, channel->local_cid); 170 event[4] = credits; 171 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 172 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 173 } 174 175 void l2cap_block_new_credits(uint8_t blocked){ 176 new_credits_blocked = blocked; 177 } 178 179 void l2cap_hand_out_credits(void){ 180 181 if (new_credits_blocked) return; // we're told not to. used by daemon 182 183 linked_item_t *it; 184 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 185 if (!hci_number_free_acl_slots()) return; 186 l2cap_channel_t * channel = (l2cap_channel_t *) it; 187 if (channel->state != L2CAP_STATE_OPEN) continue; 188 if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 189 l2cap_emit_credits(channel, 1); 190 } 191 } 192 } 193 194 l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 195 linked_item_t *it; 196 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 197 l2cap_channel_t * channel = (l2cap_channel_t *) it; 198 if ( channel->local_cid == local_cid) { 199 return channel; 200 } 201 } 202 return NULL; 203 } 204 205 int l2cap_can_send_packet_now(uint16_t local_cid){ 206 l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 207 if (!channel) return 0; 208 if (!channel->packets_granted) return 0; 209 return hci_can_send_packet_now(HCI_ACL_DATA_PACKET); 210 } 211 212 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 213 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 214 if (channel) { 215 return channel->remote_mtu; 216 } 217 return 0; 218 } 219 220 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 221 // log_info("l2cap_send_signaling_packet type %u\n", cmd); 222 va_list argptr; 223 va_start(argptr, identifier); 224 uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 225 va_end(argptr); 226 // log_info("l2cap_send_signaling_packet con %u!\n", handle); 227 return hci_send_acl_packet(sig_buffer, len); 228 } 229 230 int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 231 232 // check for free places on BT module 233 if (!hci_number_free_acl_slots()) { 234 log_info("l2cap_send_internal cid %u, BT module full <-----\n", local_cid); 235 return BTSTACK_ACL_BUFFERS_FULL; 236 } 237 int err = 0; 238 239 // find channel for local_cid, construct l2cap packet and send 240 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 241 if (channel) { 242 if (channel->packets_granted > 0){ 243 --channel->packets_granted; 244 // log_info("l2cap_send_internal cid %u, handle %u, 1 credit used, credits left %u;\n", 245 // local_cid, channel->handle, channel->packets_granted); 246 } else { 247 log_error("l2cap_send_internal cid %u, no credits!\n", local_cid); 248 } 249 250 // 0 - Connection handle : PB=10 : BC=00 251 bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 252 // 2 - ACL length 253 bt_store_16(acl_buffer, 2, len + 4); 254 // 4 - L2CAP packet length 255 bt_store_16(acl_buffer, 4, len + 0); 256 // 6 - L2CAP channel DEST 257 bt_store_16(acl_buffer, 6, channel->remote_cid); 258 // 8 - data 259 memcpy(&acl_buffer[8], data, len); 260 261 // send 262 err = hci_send_acl_packet(acl_buffer, len+8); 263 } 264 265 l2cap_hand_out_credits(); 266 267 return err; 268 } 269 270 // MARK: L2CAP_RUN 271 // process outstanding signaling tasks 272 void l2cap_run(void){ 273 274 // check pending signaling responses 275 while (signaling_responses_pending){ 276 277 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 278 279 hci_con_handle_t handle = signaling_responses[0].handle; 280 uint8_t sig_id = signaling_responses[0].sig_id; 281 uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 282 uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST 283 284 switch (signaling_responses[0].code){ 285 case CONNECTION_REQUEST: 286 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 287 break; 288 case ECHO_REQUEST: 289 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 290 break; 291 case INFORMATION_REQUEST: 292 if (infoType == 2) { 293 uint32_t features = 0; 294 // extended features request supported, however no features present 295 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 296 } else { 297 // all other types are not supported 298 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 299 } 300 break; 301 default: 302 // should not happen 303 break; 304 } 305 306 // remove first item 307 signaling_responses_pending--; 308 int i; 309 for (i=0; i < signaling_responses_pending; i++){ 310 signaling_responses[i] = signaling_responses[i+1]; 311 } 312 } 313 314 uint8_t config_options[4]; 315 linked_item_t *it; 316 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 317 318 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 319 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 320 321 l2cap_channel_t * channel = (l2cap_channel_t *) it; 322 323 // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 324 325 switch (channel->state){ 326 327 case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 328 // send connection request - set state first 329 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 330 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 331 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 332 break; 333 334 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 335 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, channel->reason, 0); 336 // discard channel - l2cap_finialize_channel_close without sending l2cap close event 337 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 338 btstack_memory_l2cap_channel_free(channel); 339 break; 340 341 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 342 channel->state = L2CAP_STATE_CONFIG; 343 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 344 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 345 break; 346 347 case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 348 // success, start l2cap handshake 349 channel->local_sig_id = l2cap_next_sig_id(); 350 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 351 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 352 break; 353 354 case L2CAP_STATE_CONFIG: 355 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 356 channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP; 357 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP; 358 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, NULL); 359 } 360 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 361 channel->state_var &= ~L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 362 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ; 363 channel->local_sig_id = l2cap_next_sig_id(); 364 config_options[0] = 1; // MTU 365 config_options[1] = 2; // len param 366 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 367 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 368 } 369 if (l2cap_channel_ready_for_open(channel)){ 370 channel->state = L2CAP_STATE_OPEN; 371 l2cap_emit_channel_opened(channel, 0); // success 372 l2cap_emit_credits(channel, 1); 373 } 374 break; 375 376 case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 377 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 378 l2cap_finialize_channel_close(channel); 379 break; 380 381 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 382 channel->local_sig_id = l2cap_next_sig_id(); 383 channel->state = L2CAP_STATE_WAIT_DISCONNECT; 384 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 385 break; 386 default: 387 break; 388 } 389 } 390 } 391 392 static uint16_t l2cap_max_l2cap_mtu(void){ 393 return hci_max_acl_data_packet_length()-4; // 4 bytes for L2CAP header 394 } 395 396 // open outgoing L2CAP channel 397 void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, 398 bd_addr_t address, uint16_t psm, uint16_t mtu){ 399 400 // alloc structure 401 l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 402 if (!chan) { 403 // emit error event 404 l2cap_channel_t dummy_channel; 405 BD_ADDR_COPY(dummy_channel.address, address); 406 dummy_channel.psm = psm; 407 l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 408 return; 409 } 410 // limit local mtu to max acl packet length 411 if (mtu > l2cap_max_l2cap_mtu()) { 412 mtu = l2cap_max_l2cap_mtu(); 413 } 414 415 // fill in 416 BD_ADDR_COPY(chan->address, address); 417 chan->psm = psm; 418 chan->handle = 0; 419 chan->connection = connection; 420 chan->packet_handler = packet_handler; 421 chan->remote_mtu = L2CAP_MINIMAL_MTU; 422 chan->local_mtu = mtu; 423 chan->packets_granted = 0; 424 425 // set initial state 426 chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 427 chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 428 chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 429 chan->local_sig_id = L2CAP_SIG_ID_INVALID; 430 431 // add to connections list 432 linked_list_add(&l2cap_channels, (linked_item_t *) chan); 433 434 l2cap_run(); 435 } 436 437 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 438 // find channel for local_cid 439 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 440 if (channel) { 441 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 442 } 443 // process 444 l2cap_run(); 445 } 446 447 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 448 linked_item_t *it = (linked_item_t *) &l2cap_channels; 449 while (it->next){ 450 l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 451 if ( ! BD_ADDR_CMP( channel->address, address) ){ 452 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 453 // failure, forward error code 454 l2cap_emit_channel_opened(channel, status); 455 // discard channel 456 it->next = it->next->next; 457 btstack_memory_l2cap_channel_free(channel); 458 } 459 } else { 460 it = it->next; 461 } 462 } 463 } 464 465 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 466 linked_item_t *it; 467 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 468 l2cap_channel_t * channel = (l2cap_channel_t *) it; 469 if ( ! BD_ADDR_CMP( channel->address, address) ){ 470 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 471 // success, start l2cap handshake 472 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 473 channel->handle = handle; 474 channel->local_cid = l2cap_next_local_cid(); 475 } 476 } 477 } 478 // process 479 l2cap_run(); 480 } 481 482 void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 483 484 bd_addr_t address; 485 hci_con_handle_t handle; 486 l2cap_channel_t * channel; 487 linked_item_t *it; 488 int hci_con_used; 489 490 switch(packet[0]){ 491 492 // handle connection complete events 493 case HCI_EVENT_CONNECTION_COMPLETE: 494 bt_flip_addr(address, &packet[5]); 495 if (packet[2] == 0){ 496 handle = READ_BT_16(packet, 3); 497 l2cap_handle_connection_success_for_addr(address, handle); 498 } else { 499 l2cap_handle_connection_failed_for_addr(address, packet[2]); 500 } 501 break; 502 503 // handle successful create connection cancel command 504 case HCI_EVENT_COMMAND_COMPLETE: 505 if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 506 if (packet[5] == 0){ 507 bt_flip_addr(address, &packet[6]); 508 // CONNECTION TERMINATED BY LOCAL HOST (0X16) 509 l2cap_handle_connection_failed_for_addr(address, 0x16); 510 } 511 } 512 l2cap_run(); // try sending signaling packets first 513 break; 514 515 case HCI_EVENT_COMMAND_STATUS: 516 l2cap_run(); // try sending signaling packets first 517 break; 518 519 // handle disconnection complete events 520 case HCI_EVENT_DISCONNECTION_COMPLETE: 521 // send l2cap disconnect events for all channels on this handle 522 handle = READ_BT_16(packet, 3); 523 it = (linked_item_t *) &l2cap_channels; 524 while (it->next){ 525 l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 526 if ( channel->handle == handle ){ 527 // update prev item before free'ing next element - don't call l2cap_finalize_channel_close 528 it->next = it->next->next; 529 l2cap_emit_channel_closed(channel); 530 btstack_memory_l2cap_channel_free(channel); 531 } else { 532 it = it->next; 533 } 534 } 535 break; 536 537 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 538 l2cap_run(); // try sending signaling packets first 539 l2cap_hand_out_credits(); 540 break; 541 542 // HCI Connection Timeouts 543 case L2CAP_EVENT_TIMEOUT_CHECK: 544 handle = READ_BT_16(packet, 2); 545 if (hci_authentication_active_for_handle(handle)) break; 546 hci_con_used = 0; 547 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 548 channel = (l2cap_channel_t *) it; 549 if (channel->handle == handle) { 550 hci_con_used = 1; 551 } 552 } 553 if (hci_con_used) break; 554 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 555 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 556 break; 557 558 case DAEMON_EVENT_HCI_PACKET_SENT: 559 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 560 channel = (l2cap_channel_t *) it; 561 if (channel->packet_handler) { 562 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 563 } 564 } 565 break; 566 567 default: 568 break; 569 } 570 571 // pass on 572 (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 573 } 574 575 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 576 channel->remote_sig_id = identifier; 577 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 578 l2cap_run(); 579 } 580 581 static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 582 if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 583 signaling_responses[signaling_responses_pending].handle = handle; 584 signaling_responses[signaling_responses_pending].code = code; 585 signaling_responses[signaling_responses_pending].sig_id = sig_id; 586 signaling_responses[signaling_responses_pending].data = data; 587 signaling_responses_pending++; 588 l2cap_run(); 589 } 590 } 591 592 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 593 594 // log_info("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 595 l2cap_service_t *service = l2cap_get_service(psm); 596 if (!service) { 597 // 0x0002 PSM not supported 598 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 599 return; 600 } 601 602 hci_connection_t * hci_connection = connection_for_handle( handle ); 603 if (!hci_connection) { 604 // 605 log_error("no hci_connection for handle %u\n", handle); 606 return; 607 } 608 // alloc structure 609 // log_info("l2cap_handle_connection_request register channel\n"); 610 l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 611 if (!channel){ 612 // 0x0004 No resources available 613 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 614 return; 615 } 616 617 // fill in 618 BD_ADDR_COPY(channel->address, hci_connection->address); 619 channel->psm = psm; 620 channel->handle = handle; 621 channel->connection = service->connection; 622 channel->packet_handler = service->packet_handler; 623 channel->local_cid = l2cap_next_local_cid(); 624 channel->remote_cid = source_cid; 625 channel->local_mtu = service->mtu; 626 channel->remote_mtu = L2CAP_DEFAULT_MTU; 627 channel->packets_granted = 0; 628 channel->remote_sig_id = sig_id; 629 630 // limit local mtu to max acl packet length 631 if (channel->local_mtu > l2cap_max_l2cap_mtu()) { 632 channel->local_mtu = l2cap_max_l2cap_mtu(); 633 } 634 635 // set initial state 636 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 637 channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 638 639 // add to connections list 640 linked_list_add(&l2cap_channels, (linked_item_t *) channel); 641 642 // emit incoming connection request 643 l2cap_emit_connection_request(channel); 644 } 645 646 void l2cap_accept_connection_internal(uint16_t local_cid){ 647 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 648 if (!channel) { 649 log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 650 return; 651 } 652 653 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 654 655 // process 656 l2cap_run(); 657 } 658 659 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 660 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 661 if (!channel) { 662 log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 663 return; 664 } 665 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 666 channel->reason = reason; 667 l2cap_run(); 668 } 669 670 void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 671 672 channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 673 674 // accept the other's configuration options 675 uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 676 uint16_t pos = 8; 677 while (pos < end_pos){ 678 uint8_t type = command[pos++]; 679 uint8_t length = command[pos++]; 680 // MTU { type(8): 1, len(8):2, MTU(16) } 681 if ((type & 0x7f) == 1 && length == 2){ 682 channel->remote_mtu = READ_BT_16(command, pos); 683 // log_info("l2cap cid %u, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 684 } 685 pos += length; 686 } 687 } 688 689 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 690 // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var); 691 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 692 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 693 return 1; 694 } 695 696 697 void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 698 699 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 700 uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 701 uint16_t result = 0; 702 703 log_info("signaling handler code %u, state %u\n", code, channel->state); 704 705 // handle DISCONNECT REQUESTS seperately 706 if (code == DISCONNECTION_REQUEST){ 707 switch (channel->state){ 708 case L2CAP_STATE_CONFIG: 709 case L2CAP_STATE_OPEN: 710 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 711 case L2CAP_STATE_WAIT_DISCONNECT: 712 l2cap_handle_disconnect_request(channel, identifier); 713 break; 714 715 default: 716 // ignore in other states 717 break; 718 } 719 return; 720 } 721 722 switch (channel->state) { 723 724 case L2CAP_STATE_WAIT_CONNECT_RSP: 725 switch (code){ 726 case CONNECTION_RESPONSE: 727 result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 728 switch (result) { 729 case 0: 730 // successful connection 731 channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 732 channel->state = L2CAP_STATE_CONFIG; 733 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ; 734 break; 735 case 1: 736 // connection pending. get some coffee 737 break; 738 default: 739 // channel closed 740 channel->state = L2CAP_STATE_CLOSED; 741 742 // map l2cap connection response result to BTstack status enumeration 743 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 744 745 // drop link key if security block 746 if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 747 hci_drop_link_key_for_bd_addr(&channel->address); 748 } 749 750 // discard channel 751 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 752 btstack_memory_l2cap_channel_free(channel); 753 break; 754 } 755 break; 756 757 default: 758 //@TODO: implement other signaling packets 759 break; 760 } 761 break; 762 763 case L2CAP_STATE_CONFIG: 764 switch (code) { 765 case CONFIGURE_REQUEST: 766 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ; 767 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP; 768 l2cap_signaling_handle_configure_request(channel, command); 769 break; 770 case CONFIGURE_RESPONSE: 771 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP; 772 break; 773 default: 774 break; 775 } 776 if (l2cap_channel_ready_for_open(channel)){ 777 // for open: 778 channel->state = L2CAP_STATE_OPEN; 779 l2cap_emit_channel_opened(channel, 0); 780 l2cap_emit_credits(channel, 1); 781 } 782 break; 783 784 case L2CAP_STATE_WAIT_DISCONNECT: 785 switch (code) { 786 case DISCONNECTION_RESPONSE: 787 l2cap_finialize_channel_close(channel); 788 break; 789 default: 790 //@TODO: implement other signaling packets 791 break; 792 } 793 break; 794 795 case L2CAP_STATE_CLOSED: 796 // @TODO handle incoming requests 797 break; 798 799 case L2CAP_STATE_OPEN: 800 //@TODO: implement other signaling packets, e.g. re-configure 801 break; 802 default: 803 break; 804 } 805 // log_info("new state %u\n", channel->state); 806 } 807 808 809 void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 810 811 // get code, signalind identifier and command len 812 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 813 uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 814 815 // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 816 if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 817 return; 818 } 819 820 // general commands without an assigned channel 821 switch(code) { 822 823 case CONNECTION_REQUEST: { 824 uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 825 uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 826 l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 827 return; 828 } 829 830 case ECHO_REQUEST: 831 l2cap_register_signaling_response(handle, code, sig_id, 0); 832 return; 833 834 case INFORMATION_REQUEST: { 835 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 836 l2cap_register_signaling_response(handle, code, sig_id, infoType); 837 return; 838 } 839 840 default: 841 break; 842 } 843 844 845 // Get potential destination CID 846 uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 847 848 // Find channel for this sig_id and connection handle 849 linked_item_t *it; 850 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 851 l2cap_channel_t * channel = (l2cap_channel_t *) it; 852 if (channel->handle == handle) { 853 if (code & 1) { 854 // match odd commands (responses) by previous signaling identifier 855 if (channel->local_sig_id == sig_id) { 856 l2cap_signaling_handler_channel(channel, command); 857 break; 858 } 859 } else { 860 // match even commands (requests) by local channel id 861 if (channel->local_cid == dest_cid) { 862 l2cap_signaling_handler_channel(channel, command); 863 break; 864 } 865 } 866 } 867 } 868 } 869 870 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 871 872 // Get Channel ID 873 uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 874 875 // Signaling Packet? 876 if (channel_id == 1) { 877 878 // Get Connection 879 hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 880 881 uint16_t command_offset = 8; 882 while (command_offset < size) { 883 884 // handle signaling commands 885 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 886 887 // increment command_offset 888 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 889 } 890 891 l2cap_run(); 892 893 return; 894 } 895 896 // Find channel for this channel_id and connection handle 897 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 898 if (channel) { 899 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 900 } 901 } 902 903 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 904 switch (packet_type) { 905 case HCI_EVENT_PACKET: 906 l2cap_event_handler(packet, size); 907 break; 908 case HCI_ACL_DATA_PACKET: 909 l2cap_acl_handler(packet, size); 910 break; 911 default: 912 break; 913 } 914 } 915 916 // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 917 void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 918 channel->state = L2CAP_STATE_CLOSED; 919 l2cap_emit_channel_closed(channel); 920 // discard channel 921 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 922 btstack_memory_l2cap_channel_free(channel); 923 } 924 925 l2cap_service_t * l2cap_get_service(uint16_t psm){ 926 linked_item_t *it; 927 928 // close open channels 929 for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 930 l2cap_service_t * service = ((l2cap_service_t *) it); 931 if ( service->psm == psm){ 932 return service; 933 }; 934 } 935 return NULL; 936 } 937 938 void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 939 // check for alread registered psm 940 // TODO: emit error event 941 l2cap_service_t *service = l2cap_get_service(psm); 942 if (service) { 943 log_error("l2cap_register_service_internal: PSM %u already registered\n", psm); 944 return; 945 } 946 947 // alloc structure 948 // TODO: emit error event 949 service = btstack_memory_l2cap_service_get(); 950 if (!service) { 951 log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n"); 952 return; 953 } 954 955 // fill in 956 service->psm = psm; 957 service->mtu = mtu; 958 service->connection = connection; 959 service->packet_handler = packet_handler; 960 961 // add to services list 962 linked_list_add(&l2cap_services, (linked_item_t *) service); 963 } 964 965 void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 966 l2cap_service_t *service = l2cap_get_service(psm); 967 if (!service) return; 968 linked_list_remove(&l2cap_services, (linked_item_t *) service); 969 btstack_memory_l2cap_service_free(service); 970 } 971 972 // 973 void l2cap_close_connection(void *connection){ 974 linked_item_t *it; 975 976 // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks 977 l2cap_channel_t * channel; 978 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 979 channel = (l2cap_channel_t *) it; 980 if (channel->connection == connection) { 981 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 982 } 983 } 984 985 // unregister services 986 it = (linked_item_t *) &l2cap_services; 987 while (it->next) { 988 l2cap_service_t * service = (l2cap_service_t *) it->next; 989 if (service->connection == connection){ 990 it->next = it->next->next; 991 btstack_memory_l2cap_service_free(service); 992 } else { 993 it = it->next; 994 } 995 } 996 997 // process 998 l2cap_run(); 999 } 1000