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