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