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