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 42 #include <stdarg.h> 43 #include <string.h> 44 45 #include <stdio.h> 46 47 // size of HCI ACL + L2CAP Header for regular data packets 48 #define COMPLETE_L2CAP_HEADER 8 49 50 static void null_event_handler(uint8_t *packet, uint16_t size); 51 static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size); 52 53 static uint8_t * sig_buffer = NULL; 54 static linked_list_t l2cap_channels = NULL; 55 static linked_list_t l2cap_services = NULL; 56 static uint8_t * acl_buffer = NULL; 57 static void (*event_packet_handler) (uint8_t *packet, uint16_t size) = null_event_handler; 58 static void (*data_packet_handler) (uint16_t source_cid, uint8_t *packet, uint16_t size) = null_data_handler; 59 static connection_t * capture_connection = NULL; 60 61 void l2cap_init(){ 62 sig_buffer = malloc( 48 ); 63 acl_buffer = malloc( 255 + 8 ); 64 65 // 66 // register callbacks with HCI 67 // 68 hci_register_event_packet_handler(&l2cap_event_handler); 69 hci_register_acl_packet_handler(&l2cap_acl_handler); 70 } 71 72 73 /** Register L2CAP packet handlers */ 74 static void null_event_handler(uint8_t *packet, uint16_t size){ 75 } 76 static void null_data_handler(uint16_t source_cid, uint8_t *packet, uint16_t size){ 77 } 78 void l2cap_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 79 event_packet_handler = handler; 80 } 81 void l2cap_register_data_packet_handler (void (*handler)(uint16_t source_cid, uint8_t *packet, uint16_t size)){ 82 data_packet_handler = handler; 83 } 84 85 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 86 va_list argptr; 87 va_start(argptr, identifier); 88 uint16_t len = l2cap_create_signaling_internal(sig_buffer, handle, cmd, identifier, argptr); 89 va_end(argptr); 90 return hci_send_acl_packet(sig_buffer, len); 91 } 92 93 l2cap_channel_t * l2cap_get_channel_for_source_cid(uint16_t source_cid){ 94 linked_item_t *it; 95 l2cap_channel_t * channel; 96 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 97 channel = (l2cap_channel_t *) it; 98 if ( channel->source_cid == source_cid) { 99 return channel; 100 } 101 } 102 return NULL; 103 } 104 105 // open outgoing L2CAP channel 106 void l2cap_create_channel_internal(connection_t * connection, bd_addr_t address, uint16_t psm){ 107 108 // alloc structure 109 l2cap_channel_t * chan = malloc(sizeof(l2cap_channel_t)); 110 // TODO: emit error event 111 if (!chan) return; 112 113 // fill in 114 BD_ADDR_COPY(chan->address, address); 115 chan->psm = psm; 116 chan->handle = 0; 117 chan->connection = connection; 118 119 // set initial state 120 chan->state = L2CAP_STATE_CLOSED; 121 chan->sig_id = L2CAP_SIG_ID_INVALID; 122 123 // add to connections list 124 linked_list_add(&l2cap_channels, (linked_item_t *) chan); 125 126 // send connection request 127 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 128 hci_send_cmd(&hci_create_connection, address, 0x18, 0, 0, 0, 0); 129 } 130 131 void l2cap_disconnect_internal(uint16_t source_cid, uint8_t reason){ 132 // find channel for source_cid 133 l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid); 134 if (channel) { 135 channel->sig_id = l2cap_next_sig_id(); 136 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid); 137 channel->state = L2CAP_STATE_WAIT_DISCONNECT; 138 } 139 } 140 141 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 142 linked_item_t *it; 143 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 144 l2cap_channel_t * channel = (l2cap_channel_t *) it; 145 if ( ! BD_ADDR_CMP( channel->address, address) ){ 146 if (channel->state == L2CAP_STATE_CLOSED) { 147 // failure, forward error code 148 l2cap_emit_channel_opened(channel, status); 149 // discard channel 150 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 151 free (channel); 152 } 153 } 154 } 155 } 156 157 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 158 linked_item_t *it; 159 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 160 l2cap_channel_t * channel = (l2cap_channel_t *) it; 161 if ( ! BD_ADDR_CMP( channel->address, address) ){ 162 if (channel->state == L2CAP_STATE_CLOSED) { 163 // success, start l2cap handshake 164 channel->handle = handle; 165 channel->sig_id = l2cap_next_sig_id(); 166 channel->source_cid = l2cap_next_source_cid(); 167 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 168 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->sig_id, channel->psm, channel->source_cid); 169 } 170 } 171 } 172 } 173 174 void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 175 176 bd_addr_t address; 177 hci_con_handle_t handle; 178 179 switch(packet[0]){ 180 181 // handle connection complete events 182 case HCI_EVENT_CONNECTION_COMPLETE: 183 bt_flip_addr(address, &packet[5]); 184 if (packet[2] == 0){ 185 handle = READ_BT_16(packet, 3); 186 l2cap_handle_connection_success_for_addr(address, handle); 187 } else { 188 l2cap_handle_connection_failed_for_addr(address, packet[2]); 189 } 190 break; 191 192 // handle successful create connection cancel command 193 case HCI_EVENT_COMMAND_COMPLETE: 194 if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 195 if (packet[5] == 0){ 196 bt_flip_addr(address, &packet[6]); 197 // CONNECTION TERMINATED BY LOCAL HOST (0X16) 198 l2cap_handle_connection_failed_for_addr(address, 0x16); 199 } 200 } 201 break; 202 203 // handle disconnection complete events 204 case HCI_EVENT_DISCONNECTION_COMPLETE: 205 // send l2cap disconnect events for all channels on this handle 206 handle = READ_BT_16(packet, 3); 207 linked_item_t *it; 208 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 209 l2cap_channel_t * channel = (l2cap_channel_t *) it; 210 if ( channel->handle == handle ){ 211 l2cap_finialize_channel_close(channel); 212 } 213 } 214 break; 215 216 // HCI Connection Timeouts 217 case L2CAP_EVENT_TIMEOUT_CHECK: 218 if (!capture_connection){ 219 hci_con_handle_t handle = READ_BT_16(packet, 2); 220 linked_item_t *it; 221 l2cap_channel_t * channel; 222 int used = 0; 223 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 224 channel = (l2cap_channel_t *) it; 225 if (channel->handle == handle) { 226 used = 1; 227 } 228 } 229 if (!used) { 230 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 231 } 232 } 233 break; 234 235 default: 236 break; 237 } 238 239 // pass on 240 (*event_packet_handler)(packet, size); 241 } 242 243 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 244 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, identifier, channel->dest_cid, channel->source_cid); 245 l2cap_finialize_channel_close(channel); 246 } 247 248 void l2cap_signaling_handler(l2cap_channel_t *channel, uint8_t *packet, uint16_t size){ 249 250 static uint8_t config_options[] = { 1, 2, 150, 0}; // mtu = 48 251 252 uint8_t code = READ_L2CAP_SIGNALING_CODE( packet ); 253 uint8_t identifier = READ_L2CAP_SIGNALING_IDENTIFIER( packet ); 254 uint16_t result = 0; 255 256 switch (channel->state) { 257 258 case L2CAP_STATE_WAIT_CONNECT_RSP: 259 switch (code){ 260 case CONNECTION_RESPONSE: 261 result = READ_BT_16 (packet, L2CAP_SIGNALING_DATA_OFFSET+4); 262 switch (result) { 263 case 0: 264 // successfull connection 265 channel->dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET); 266 channel->sig_id = l2cap_next_sig_id(); 267 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->sig_id, channel->dest_cid, 0, 4, &config_options); 268 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ; 269 break; 270 case 1: 271 // connection pending. get some coffee 272 break; 273 default: 274 // map l2cap connection response result to BTstack status enumeration 275 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 276 break; 277 } 278 break; 279 280 case DISCONNECTION_REQUEST: 281 l2cap_handle_disconnect_request(channel, identifier); 282 break; 283 284 default: 285 //@TODO: implement other signaling packets 286 break; 287 } 288 break; 289 290 case L2CAP_STATE_WAIT_CONFIG_REQ_RSP_OR_CONFIG_REQ: 291 switch (code) { 292 case CONFIGURE_RESPONSE: 293 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ; 294 break; 295 case CONFIGURE_REQUEST: 296 // accept the other's configuration options 297 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]); 298 channel->state = L2CAP_STATE_WAIT_CONFIG_REQ_RSP; 299 break; 300 case DISCONNECTION_REQUEST: 301 l2cap_handle_disconnect_request(channel, identifier); 302 break; 303 default: 304 //@TODO: implement other signaling packets 305 break; 306 } 307 break; 308 309 case L2CAP_STATE_WAIT_CONFIG_REQ: 310 switch (code) { 311 case CONFIGURE_REQUEST: 312 // accept the other's configuration options 313 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, identifier, channel->dest_cid, 0, 0, size - 16, &packet[16]); 314 channel->state = L2CAP_STATE_OPEN; 315 l2cap_emit_channel_opened(channel, 0); // success 316 break; 317 case DISCONNECTION_REQUEST: 318 l2cap_handle_disconnect_request(channel, identifier); 319 break; 320 default: 321 //@TODO: implement other signaling packets 322 break; 323 } 324 break; 325 326 case L2CAP_STATE_WAIT_CONFIG_REQ_RSP: 327 switch (code) { 328 case CONFIGURE_RESPONSE: 329 channel->state = L2CAP_STATE_OPEN; 330 l2cap_emit_channel_opened(channel, 0); // success 331 break; 332 case DISCONNECTION_REQUEST: 333 l2cap_handle_disconnect_request(channel, identifier); 334 break; 335 default: 336 //@TODO: implement other signaling packets 337 break; 338 } 339 break; 340 341 case L2CAP_STATE_WAIT_DISCONNECT: 342 switch (code) { 343 case DISCONNECTION_RESPONSE: 344 l2cap_finialize_channel_close(channel); 345 break; 346 case DISCONNECTION_REQUEST: 347 l2cap_handle_disconnect_request(channel, identifier); 348 break; 349 default: 350 //@TODO: implement other signaling packets 351 break; 352 } 353 break; 354 355 case L2CAP_STATE_CLOSED: 356 // @TODO handle incoming requests 357 break; 358 359 case L2CAP_STATE_OPEN: 360 switch (code) { 361 case DISCONNECTION_REQUEST: 362 l2cap_handle_disconnect_request(channel, identifier); 363 break; 364 default: 365 //@TODO: implement other signaling packets, e.g. re-configure 366 break; 367 } 368 break; 369 } 370 } 371 372 // finalize closed channel 373 void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 374 channel->state = L2CAP_STATE_CLOSED; 375 l2cap_emit_channel_closed(channel); 376 377 // discard channel 378 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 379 free (channel); 380 } 381 382 l2cap_service_t * l2cap_get_service(uint16_t psm){ 383 linked_item_t *it; 384 385 // close open channels 386 for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 387 l2cap_service_t * service = ((l2cap_service_t *) it); 388 if ( service->psm == psm){ 389 return service; 390 }; 391 } 392 return NULL; 393 } 394 395 void l2cap_register_service(connection_t *connection, uint16_t psm, uint16_t mtu){ 396 // check for alread registered psm // TODO: emit error event 397 l2cap_service_t *service = l2cap_get_service(psm); 398 if (service) return; 399 400 // alloc structure // TODO: emit error event 401 service = malloc(sizeof(l2cap_service_t)); 402 if (!service) return; 403 404 // fill in 405 service->psm = psm; 406 service->mtu = mtu; 407 service->connection = connection; 408 409 // add to services list 410 linked_list_add(&l2cap_services, (linked_item_t *) service); 411 } 412 413 void l2cap_unregister_service(connection_t *connection, uint16_t psm){ 414 l2cap_service_t *service = l2cap_get_service(psm); 415 if (service) return; 416 linked_list_remove(&l2cap_services, (linked_item_t *) service); 417 free( service ); 418 } 419 420 // 421 void l2cap_close_connection(connection_t *connection){ 422 linked_item_t *it; 423 424 // close open channels 425 l2cap_channel_t * channel; 426 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 427 channel = (l2cap_channel_t *) it; 428 if ( channel->connection == connection) { 429 channel->sig_id = l2cap_next_sig_id(); 430 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->sig_id, channel->dest_cid, channel->source_cid); 431 channel->state = L2CAP_STATE_WAIT_DISCONNECT; 432 } 433 } 434 435 // TODO: INCOMING unregister services 436 437 } 438 439 // notify client 440 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 441 uint8_t event[17]; 442 event[0] = L2CAP_EVENT_CHANNEL_OPENED; 443 event[1] = sizeof(event) - 2; 444 event[2] = status; 445 bt_flip_addr(&event[3], channel->address); 446 bt_store_16(event, 9, channel->handle); 447 bt_store_16(event, 11, channel->psm); 448 bt_store_16(event, 13, channel->source_cid); 449 bt_store_16(event, 15, channel->dest_cid); 450 socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 451 } 452 453 void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 454 uint8_t event[4]; 455 event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 456 event[1] = sizeof(event) - 2; 457 bt_store_16(event, 2, channel->source_cid); 458 socket_connection_send_packet(channel->connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 459 } 460 461 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 462 463 // Capturing? 464 if (capture_connection) { 465 socket_connection_send_packet(capture_connection, HCI_ACL_DATA_PACKET, 0, packet, size); 466 return; 467 } 468 469 // forward to higher layers - not needed yet 470 // (*data_packet_handler)(channel_id, packet, size); 471 472 // Get Channel ID and command code 473 uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 474 uint8_t code = READ_L2CAP_SIGNALING_CODE( packet ); 475 476 // Get Connection 477 hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 478 479 // Signaling Packet? 480 if (channel_id == 1) { 481 482 if (code < 1 || code == 2 || code >= 8){ 483 // not for a particular channel 484 return; 485 } 486 487 // Get Signaling Identifier and potential destination CID 488 uint8_t sig_id = READ_L2CAP_SIGNALING_IDENTIFIER(packet); 489 uint16_t dest_cid = READ_BT_16(packet, L2CAP_SIGNALING_DATA_OFFSET); 490 491 // Find channel for this sig_id and connection handle 492 linked_item_t *it; 493 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 494 l2cap_channel_t * channel = (l2cap_channel_t *) it; 495 if (channel->handle == handle) { 496 if (code & 1) { 497 // match odd commands by previous signaling identifier 498 if (channel->sig_id == sig_id) { 499 l2cap_signaling_handler( channel, packet, size); 500 } 501 } else { 502 // match even commands by source channel id 503 if (channel->source_cid == dest_cid) { 504 l2cap_signaling_handler( channel, packet, size); 505 } 506 } 507 } 508 } 509 return; 510 } 511 512 // Find channel for this channel_id and connection handle 513 l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(channel_id); 514 if (channel) { 515 socket_connection_send_packet(channel->connection, L2CAP_DATA_PACKET, channel_id, 516 &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 517 } 518 } 519 520 521 void l2cap_send_internal(uint16_t source_cid, uint8_t *data, uint16_t len){ 522 // find channel for source_cid, construct l2cap packet and send 523 l2cap_channel_t * channel = l2cap_get_channel_for_source_cid(source_cid); 524 if (channel) { 525 // 0 - Connection handle : PB=10 : BC=00 526 bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 527 // 2 - ACL length 528 bt_store_16(acl_buffer, 2, len + 4); 529 // 4 - L2CAP packet length 530 bt_store_16(acl_buffer, 4, len + 0); 531 // 6 - L2CAP channel DEST 532 bt_store_16(acl_buffer, 6, channel->dest_cid); 533 // 8 - data 534 memcpy(&acl_buffer[8], data, len); 535 // send 536 hci_send_acl_packet(acl_buffer, len+8); 537 } 538 } 539 540 void l2cap_set_capture_connection(connection_t * connection){ 541 capture_connection = connection; 542 } 543 544