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 44 #include <stdarg.h> 45 #include <string.h> 46 47 #include <stdio.h> 48 49 // size of HCI ACL + L2CAP Header for regular data packets 50 #define COMPLETE_L2CAP_HEADER 8 51 52 // offsets for L2CAP SIGNALING COMMANDS 53 #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 54 #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 55 #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 56 #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 57 58 static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size); 59 static void null_data_handler(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size); 60 61 static uint8_t * sig_buffer = NULL; 62 static linked_list_t l2cap_channels = NULL; 63 static linked_list_t l2cap_services = NULL; 64 static uint8_t * acl_buffer = NULL; 65 static void (*event_packet_handler) (connection_t * connection, uint8_t *packet, uint16_t size) = null_event_handler; 66 static void (*data_packet_handler) (connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size) = null_data_handler; 67 static connection_t * capture_connection = NULL; 68 69 static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 70 71 void l2cap_init(){ 72 sig_buffer = malloc( 48 ); 73 acl_buffer = malloc( 255 + 8 ); 74 75 // 76 // register callbacks with HCI 77 // 78 hci_register_event_packet_handler(&l2cap_event_handler); 79 hci_register_acl_packet_handler(&l2cap_acl_handler); 80 } 81 82 83 /** Register L2CAP packet handlers */ 84 static void null_event_handler(connection_t * connection, uint8_t *packet, uint16_t size){ 85 } 86 static void null_data_handler(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size){ 87 } 88 void l2cap_register_event_packet_handler(void (*handler)(connection_t * connection, uint8_t *packet, uint16_t size)){ 89 event_packet_handler = handler; 90 } 91 void l2cap_register_data_packet_handler (void (*handler)(connection_t * connection, uint16_t local_cid, uint8_t *packet, uint16_t size)){ 92 data_packet_handler = handler; 93 } 94 95 // notify client/protocol handler 96 void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 97 if (channel->packet_handler) { 98 (* (channel->packet_handler))(type, channel->local_cid, data, size); 99 } else { 100 switch (type){ 101 case HCI_EVENT_PACKET: 102 (*event_packet_handler)(channel->connection, data, size); 103 break; 104 case L2CAP_DATA_PACKET: 105 (*data_packet_handler)(channel->connection, channel->local_cid, data, size); 106 break; 107 default: 108 // ?? 109 break; 110 } 111 } 112 } 113 114 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 115 uint8_t event[17]; 116 event[0] = L2CAP_EVENT_CHANNEL_OPENED; 117 event[1] = sizeof(event) - 2; 118 event[2] = status; 119 bt_flip_addr(&event[3], channel->address); 120 bt_store_16(event, 9, channel->handle); 121 bt_store_16(event, 11, channel->psm); 122 bt_store_16(event, 13, channel->local_cid); 123 bt_store_16(event, 15, channel->remote_cid); 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 l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 151 linked_item_t *it; 152 l2cap_channel_t * channel; 153 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 154 channel = (l2cap_channel_t *) it; 155 if ( channel->local_cid == local_cid) { 156 return channel; 157 } 158 } 159 return NULL; 160 } 161 162 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 163 // printf("l2cap_send_signaling_packet type %u\n", cmd); 164 va_list argptr; 165 va_start(argptr, identifier); 166 uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 167 va_end(argptr); 168 return hci_send_acl_packet(sig_buffer, len); 169 } 170 171 void l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 172 // find channel for local_cid, construct l2cap packet and send 173 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 174 if (channel) { 175 // 0 - Connection handle : PB=10 : BC=00 176 bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 177 // 2 - ACL length 178 bt_store_16(acl_buffer, 2, len + 4); 179 // 4 - L2CAP packet length 180 bt_store_16(acl_buffer, 4, len + 0); 181 // 6 - L2CAP channel DEST 182 bt_store_16(acl_buffer, 6, channel->remote_cid); 183 // 8 - data 184 memcpy(&acl_buffer[8], data, len); 185 // send 186 hci_send_acl_packet(acl_buffer, len+8); 187 } 188 } 189 190 // open outgoing L2CAP channel 191 void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){ 192 193 // alloc structure 194 l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 195 // TODO: emit error event 196 if (!chan) return; 197 198 // fill in 199 BD_ADDR_COPY(chan->address, address); 200 chan->psm = psm; 201 chan->handle = 0; 202 chan->connection = connection; 203 chan->packet_handler = NULL; 204 205 // set initial state 206 chan->state = L2CAP_STATE_CLOSED; 207 chan->sig_id = L2CAP_SIG_ID_INVALID; 208 209 // add to connections list 210 linked_list_add(&l2cap_channels, (linked_item_t *) chan); 211 212 // send connection request 213 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 214 hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 215 } 216 217 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 218 // find channel for local_cid 219 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 220 if (channel) { 221 channel->sig_id = l2cap_next_sig_id(); 222 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 223 channel->state = L2CAP_STATE_WAIT_DISCONNECT; 224 } 225 } 226 227 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 228 linked_item_t *it; 229 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 230 l2cap_channel_t * channel = (l2cap_channel_t *) it; 231 if ( ! BD_ADDR_CMP( channel->address, address) ){ 232 if (channel->state == L2CAP_STATE_CLOSED) { 233 // failure, forward error code 234 l2cap_emit_channel_opened(channel, status); 235 // discard channel 236 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 237 free (channel); 238 } 239 } 240 } 241 } 242 243 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 244 linked_item_t *it; 245 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 246 l2cap_channel_t * channel = (l2cap_channel_t *) it; 247 if ( ! BD_ADDR_CMP( channel->address, address) ){ 248 if (channel->state == L2CAP_STATE_CLOSED) { 249 // success, start l2cap handshake 250 channel->handle = handle; 251 channel->sig_id = l2cap_next_sig_id(); 252 channel->local_cid = l2cap_next_local_cid(); 253 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 254 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->local_cid); 255 } 256 } 257 } 258 } 259 260 void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 261 262 bd_addr_t address; 263 hci_con_handle_t handle; 264 265 switch(packet[0]){ 266 267 // handle connection complete events 268 case HCI_EVENT_CONNECTION_COMPLETE: 269 bt_flip_addr(address, &packet[5]); 270 if (packet[2] == 0){ 271 handle = READ_BT_16(packet, 3); 272 l2cap_handle_connection_success_for_addr(address, handle); 273 } else { 274 l2cap_handle_connection_failed_for_addr(address, packet[2]); 275 } 276 break; 277 278 // handle successful create connection cancel command 279 case HCI_EVENT_COMMAND_COMPLETE: 280 if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 281 if (packet[5] == 0){ 282 bt_flip_addr(address, &packet[6]); 283 // CONNECTION TERMINATED BY LOCAL HOST (0X16) 284 l2cap_handle_connection_failed_for_addr(address, 0x16); 285 } 286 } 287 break; 288 289 // handle disconnection complete events 290 case HCI_EVENT_DISCONNECTION_COMPLETE: 291 // send l2cap disconnect events for all channels on this handle 292 handle = READ_BT_16(packet, 3); 293 linked_item_t *it; 294 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 295 l2cap_channel_t * channel = (l2cap_channel_t *) it; 296 if ( channel->handle == handle ){ 297 l2cap_finialize_channel_close(channel); 298 } 299 } 300 break; 301 302 // HCI Connection Timeouts 303 case L2CAP_EVENT_TIMEOUT_CHECK: 304 if (!capture_connection){ 305 hci_con_handle_t handle = READ_BT_16(packet, 2); 306 linked_item_t *it; 307 l2cap_channel_t * channel; 308 int used = 0; 309 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 310 channel = (l2cap_channel_t *) it; 311 if (channel->handle == handle) { 312 used = 1; 313 } 314 } 315 if (!used) { 316 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 317 } 318 } 319 break; 320 321 default: 322 break; 323 } 324 325 // pass on 326 (*event_packet_handler)(NULL, packet, size); 327 } 328 329 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 330 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->remote_cid, channel->local_cid); 331 l2cap_finialize_channel_close(channel); 332 } 333 334 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 335 336 // printf("l2cap_handle_connection_request for handle %u, psm %u cid %u\n", handle, psm, source_cid); 337 l2cap_service_t *service = l2cap_get_service(psm); 338 if (!service) { 339 // 0x0002 PSM not supported 340 // printf("l2cap_handle_connection_request no PSM for psm %u/n", psm); 341 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, 0x0002, 0); 342 return; 343 } 344 345 hci_connection_t * hci_connection = connection_for_handle( handle ); 346 if (!hci_connection) { 347 fprintf(stderr, "no hci_connection for handle %u\n", handle); 348 // TODO: emit error 349 return; 350 } 351 // alloc structure 352 // printf("l2cap_handle_connection_request register channel\n"); 353 l2cap_channel_t * channel = malloc(sizeof(l2cap_channel_t)); 354 // TODO: emit error event 355 if (!channel) return; 356 357 // fill in 358 BD_ADDR_COPY(channel->address, hci_connection->address); 359 channel->psm = psm; 360 channel->handle = handle; 361 channel->connection = service->connection; 362 channel->packet_handler = service->packet_handler; 363 channel->local_cid = l2cap_next_local_cid(); 364 channel->remote_cid = source_cid; 365 366 // set initial state 367 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 368 369 // temp. store req sig id 370 channel->sig_id = sig_id; 371 372 // add to connections list 373 linked_list_add(&l2cap_channels, (linked_item_t *) channel); 374 375 // emit incoming connection request 376 l2cap_emit_connection_request(channel); 377 } 378 379 void l2cap_accept_connection_internal(uint16_t local_cid){ 380 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 381 if (!channel) { 382 fprintf(stderr, "l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 383 return; 384 } 385 386 // accept connection 387 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, channel->local_cid, channel->remote_cid, 0, 0); 388 389 // set real sig and state and start config 390 channel->sig_id = l2cap_next_sig_id(); 391 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 392 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 393 394 // printf("new state %u\n", channel->state); 395 } 396 397 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 398 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 399 if (!channel) { 400 fprintf(stderr, "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid,reason); 401 return; 402 } 403 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->sig_id, 0, 0, reason, 0); 404 405 // discard channel 406 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 407 free (channel); 408 } 409 410 void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 411 412 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 413 uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 414 uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 415 uint16_t result = 0; 416 417 // printf("signaling handler code %u\n", code); 418 419 switch (channel->state) { 420 421 case L2CAP_STATE_WAIT_CONNECT_RSP: 422 switch (code){ 423 case CONNECTION_RESPONSE: 424 result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 425 switch (result) { 426 case 0: 427 // successful connection 428 channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 429 channel->sig_id = l2cap_next_sig_id(); 430 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->remote_cid, 0, 4, &config_options); 431 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 432 break; 433 case 1: 434 // connection pending. get some coffee 435 break; 436 default: 437 // map l2cap connection response result to BTstack status enumeration 438 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 439 break; 440 } 441 break; 442 443 case DISCONNECTION_REQUEST: 444 l2cap_handle_disconnect_request(channel, identifier); 445 break; 446 447 default: 448 //@TODO: implement other signaling packets 449 break; 450 } 451 break; 452 453 case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 454 switch (code) { 455 case CONFIGURE_RESPONSE: 456 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 457 break; 458 case CONFIGURE_REQUEST: 459 // accept the other's configuration options 460 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 461 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 462 break; 463 case DISCONNECTION_REQUEST: 464 l2cap_handle_disconnect_request(channel, identifier); 465 break; 466 default: 467 //@TODO: implement other signaling packets 468 break; 469 } 470 break; 471 472 case L2CAP_STATE_WAIT_CONFIG_REQ: 473 switch (code) { 474 case CONFIGURE_REQUEST: 475 // accept the other's configuration options 476 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->remote_cid, 0, 0, len-4, &command[8]); 477 channel->state = L2CAP_STATE_OPEN; 478 l2cap_emit_channel_opened(channel, 0); // success 479 break; 480 case DISCONNECTION_REQUEST: 481 l2cap_handle_disconnect_request(channel, identifier); 482 break; 483 default: 484 //@TODO: implement other signaling packets 485 break; 486 } 487 break; 488 489 case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 490 switch (code) { 491 case CONFIGURE_RESPONSE: 492 channel->state = L2CAP_STATE_OPEN; 493 l2cap_emit_channel_opened(channel, 0); // success 494 break; 495 case DISCONNECTION_REQUEST: 496 l2cap_handle_disconnect_request(channel, identifier); 497 break; 498 default: 499 //@TODO: implement other signaling packets 500 break; 501 } 502 break; 503 504 case L2CAP_STATE_WAIT_DISCONNECT: 505 switch (code) { 506 case DISCONNECTION_RESPONSE: 507 l2cap_finialize_channel_close(channel); 508 break; 509 case DISCONNECTION_REQUEST: 510 l2cap_handle_disconnect_request(channel, identifier); 511 break; 512 default: 513 //@TODO: implement other signaling packets 514 break; 515 } 516 break; 517 518 case L2CAP_STATE_CLOSED: 519 // @TODO handle incoming requests 520 break; 521 522 case L2CAP_STATE_OPEN: 523 switch (code) { 524 case DISCONNECTION_REQUEST: 525 l2cap_handle_disconnect_request(channel, identifier); 526 break; 527 default: 528 //@TODO: implement other signaling packets, e.g. re-configure 529 break; 530 } 531 break; 532 } 533 // printf("new state %u\n", channel->state); 534 } 535 536 537 void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 538 539 // get code, signalind identifier and command len 540 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 541 uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 542 uint16_t len = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 543 544 // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 545 if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 546 return; 547 } 548 549 // general commands without an assigned channel 550 switch(code) { 551 552 case CONNECTION_REQUEST: { 553 uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 554 uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 555 l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 556 break; 557 } 558 559 case ECHO_REQUEST: { 560 // send back packet 561 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, len, &command[L2CAP_SIGNALING_COMMAND_DATA_OFFSET]); 562 break; 563 } 564 565 case INFORMATION_REQUEST: { 566 // we neither support connectionless L2CAP data nor support any flow control modes yet 567 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 568 if (infoType == 2) { 569 uint32_t features = 0; 570 // extended features request supported, however no features present 571 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, 4, &features); 572 } else { 573 // all other types are not supported 574 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 575 } 576 break;; 577 } 578 579 default: 580 break; 581 } 582 583 584 // Get potential destination CID 585 uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 586 587 // Find channel for this sig_id and connection handle 588 linked_item_t *it; 589 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 590 l2cap_channel_t * channel = (l2cap_channel_t *) it; 591 if (channel->handle == handle) { 592 if (code & 1) { 593 // match odd commands by previous signaling identifier 594 if (channel->sig_id == sig_id) { 595 l2cap_signaling_handler_channel(channel, command); 596 } 597 } else { 598 // match even commands by local channel id 599 if (channel->local_cid == dest_cid) { 600 l2cap_signaling_handler_channel(channel, command); 601 } 602 } 603 } 604 } 605 } 606 607 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 608 609 // Capturing? 610 if (capture_connection) { 611 (*data_packet_handler)(capture_connection, 0, packet, size); 612 return; 613 } 614 615 // Get Channel ID 616 uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 617 618 // Signaling Packet? 619 if (channel_id == 1) { 620 621 // Get Connection 622 hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 623 624 uint16_t command_offset = 8; 625 while (command_offset < size) { 626 627 // handle signaling commands 628 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 629 630 // increment command_offset 631 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 632 } 633 return; 634 } 635 636 // Find channel for this channel_id and connection handle 637 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 638 if (channel) { 639 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 640 } 641 } 642 643 644 // finalize closed channel 645 void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 646 channel->state = L2CAP_STATE_CLOSED; 647 l2cap_emit_channel_closed(channel); 648 649 // discard channel 650 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 651 free (channel); 652 } 653 654 l2cap_service_t * l2cap_get_service(uint16_t psm){ 655 linked_item_t *it; 656 657 // close open channels 658 for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 659 l2cap_service_t * service = ((l2cap_service_t *) it); 660 if ( service->psm == psm){ 661 return service; 662 }; 663 } 664 return NULL; 665 } 666 667 void l2cap_register_service_internal(connection_t *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu){ 668 // check for alread registered psm // TODO: emit error event 669 l2cap_service_t *service = l2cap_get_service(psm); 670 if (service) return; 671 672 // alloc structure // TODO: emit error event 673 service = malloc(sizeof(l2cap_service_t)); 674 if (!service) return; 675 676 // fill in 677 service->psm = psm; 678 service->mtu = mtu; 679 service->connection = connection; 680 service->packet_handler = packet_handler; 681 682 // add to services list 683 linked_list_add(&l2cap_services, (linked_item_t *) service); 684 } 685 686 void l2cap_unregister_service_internal(connection_t *connection, uint16_t psm){ 687 l2cap_service_t *service = l2cap_get_service(psm); 688 if (!service) return; 689 linked_list_remove(&l2cap_services, (linked_item_t *) service); 690 free(service); 691 } 692 693 // 694 void l2cap_close_connection(connection_t *connection){ 695 linked_item_t *it; 696 697 // close open channels 698 l2cap_channel_t * channel; 699 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 700 channel = (l2cap_channel_t *) it; 701 if (channel->connection == connection) { 702 channel->sig_id = l2cap_next_sig_id(); 703 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->remote_cid, channel->local_cid); 704 channel->state = L2CAP_STATE_WAIT_DISCONNECT; 705 } 706 } 707 708 // unregister services 709 l2cap_service_t *service; 710 for (it = (linked_item_t *) l2cap_services; it ; ){ 711 service = (l2cap_service_t *) it->next; 712 if (service->connection == connection){ 713 it->next = service->item.next; 714 free(service); 715 } else { 716 it = it->next; 717 } 718 } 719 } 720 721 722 void l2cap_set_capture_connection(connection_t * connection){ 723 capture_connection = connection; 724 } 725 726