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