1 /* 2 * Copyright (C) 2009-2012 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 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at [email protected] 34 * 35 */ 36 37 /* 38 * l2cap.c 39 * 40 * Logical Link Control and Adaption Protocl (L2CAP) 41 * 42 * Created by Matthias Ringwald on 5/16/09. 43 */ 44 45 #include "l2cap.h" 46 #include "hci.h" 47 #include "hci_dump.h" 48 #include "debug.h" 49 #include "btstack_memory.h" 50 51 #include <stdarg.h> 52 #include <string.h> 53 54 #include <stdio.h> 55 56 // nr of buffered acl packets in outgoing queue to get max performance 57 #define NR_BUFFERED_ACL_PACKETS 3 58 59 // used to cache l2cap rejects, echo, and informational requests 60 #define NR_PENDING_SIGNALING_RESPONSES 3 61 62 // offsets for L2CAP SIGNALING COMMANDS 63 #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 64 #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 65 #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 66 #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 67 68 static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 69 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 70 71 // used to cache l2cap rejects, echo, and informational requests 72 static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 73 static int signaling_responses_pending; 74 75 static linked_list_t l2cap_channels; 76 static linked_list_t l2cap_services; 77 static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 78 static int new_credits_blocked = 0; 79 80 static btstack_packet_handler_t attribute_protocol_packet_handler; 81 static btstack_packet_handler_t security_protocol_packet_handler; 82 static uint8_t require_security_level2_for_outgoing_sdp; 83 84 // prototypes 85 static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 86 static l2cap_service_t * l2cap_get_service(uint16_t psm); 87 static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 88 static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 89 static void l2cap_emit_connection_request(l2cap_channel_t *channel); 90 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 91 92 93 void l2cap_init(){ 94 new_credits_blocked = 0; 95 signaling_responses_pending = 0; 96 97 l2cap_channels = NULL; 98 l2cap_services = NULL; 99 100 packet_handler = null_packet_handler; 101 attribute_protocol_packet_handler = NULL; 102 security_protocol_packet_handler = NULL; 103 104 require_security_level2_for_outgoing_sdp = 0; 105 106 // 107 // register callback with HCI 108 // 109 hci_register_packet_handler(&l2cap_packet_handler); 110 hci_connectable_control(0); // no services yet 111 } 112 113 114 /** Register L2CAP packet handlers */ 115 static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 116 } 117 void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 118 packet_handler = handler; 119 } 120 121 // notify client/protocol handler 122 void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 123 if (channel->packet_handler) { 124 (* (channel->packet_handler))(type, channel->local_cid, data, size); 125 } else { 126 (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 127 } 128 } 129 130 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 131 log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u", 132 status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 133 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 134 uint8_t event[23]; 135 event[0] = L2CAP_EVENT_CHANNEL_OPENED; 136 event[1] = sizeof(event) - 2; 137 event[2] = status; 138 bt_flip_addr(&event[3], channel->address); 139 bt_store_16(event, 9, channel->handle); 140 bt_store_16(event, 11, channel->psm); 141 bt_store_16(event, 13, channel->local_cid); 142 bt_store_16(event, 15, channel->remote_cid); 143 bt_store_16(event, 17, channel->local_mtu); 144 bt_store_16(event, 19, channel->remote_mtu); 145 bt_store_16(event, 19, channel->flush_timeout); 146 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 147 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 148 } 149 150 void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 151 log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 152 uint8_t event[4]; 153 event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 154 event[1] = sizeof(event) - 2; 155 bt_store_16(event, 2, channel->local_cid); 156 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 157 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 158 } 159 160 void l2cap_emit_connection_request(l2cap_channel_t *channel) { 161 log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 162 bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 163 uint8_t event[16]; 164 event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 165 event[1] = sizeof(event) - 2; 166 bt_flip_addr(&event[2], channel->address); 167 bt_store_16(event, 8, channel->handle); 168 bt_store_16(event, 10, channel->psm); 169 bt_store_16(event, 12, channel->local_cid); 170 bt_store_16(event, 14, channel->remote_cid); 171 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 172 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 173 } 174 175 static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ 176 log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); 177 uint8_t event[5]; 178 event[0] = L2CAP_EVENT_SERVICE_REGISTERED; 179 event[1] = sizeof(event) - 2; 180 event[2] = status; 181 bt_store_16(event, 3, psm); 182 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 183 (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 184 } 185 186 void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 187 188 log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits); 189 // track credits 190 channel->packets_granted += credits; 191 192 uint8_t event[5]; 193 event[0] = L2CAP_EVENT_CREDITS; 194 event[1] = sizeof(event) - 2; 195 bt_store_16(event, 2, channel->local_cid); 196 event[4] = credits; 197 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 198 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 199 } 200 201 void l2cap_block_new_credits(uint8_t blocked){ 202 new_credits_blocked = blocked; 203 } 204 205 void l2cap_hand_out_credits(void){ 206 207 if (new_credits_blocked) return; // we're told not to. used by daemon 208 209 linked_item_t *it; 210 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 211 if (!hci_number_free_acl_slots()) return; 212 l2cap_channel_t * channel = (l2cap_channel_t *) it; 213 if (channel->state != L2CAP_STATE_OPEN) continue; 214 if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 215 l2cap_emit_credits(channel, 1); 216 } 217 } 218 } 219 220 l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 221 linked_item_t *it; 222 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 223 l2cap_channel_t * channel = (l2cap_channel_t *) it; 224 if ( channel->local_cid == local_cid) { 225 return channel; 226 } 227 } 228 return NULL; 229 } 230 231 int l2cap_can_send_packet_now(uint16_t local_cid){ 232 l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 233 if (!channel) return 0; 234 if (!channel->packets_granted) return 0; 235 return hci_can_send_packet_now(HCI_ACL_DATA_PACKET); 236 } 237 238 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 239 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 240 if (channel) { 241 return channel->remote_mtu; 242 } 243 return 0; 244 } 245 246 static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 247 linked_item_t *it; 248 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 249 l2cap_channel_t * channel = (l2cap_channel_t *) it; 250 if ( &channel->rtx == ts) { 251 return channel; 252 } 253 } 254 return NULL; 255 } 256 257 static void l2cap_rtx_timeout(timer_source_t * ts){ 258 l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 259 if (!ts) return; 260 261 log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 262 263 // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 264 // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 265 // notify client 266 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 267 268 // discard channel 269 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 270 btstack_memory_l2cap_channel_free(channel); 271 } 272 273 static void l2cap_stop_rtx(l2cap_channel_t * channel){ 274 log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 275 run_loop_remove_timer(&channel->rtx); 276 } 277 278 static void l2cap_start_rtx(l2cap_channel_t * channel){ 279 log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 280 l2cap_stop_rtx(channel); 281 run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 282 run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 283 run_loop_add_timer(&channel->rtx); 284 } 285 286 static void l2cap_start_ertx(l2cap_channel_t * channel){ 287 log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 288 l2cap_stop_rtx(channel); 289 run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 290 run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 291 run_loop_add_timer(&channel->rtx); 292 } 293 294 void l2cap_require_security_level_2_for_outgoing_sdp(){ 295 require_security_level2_for_outgoing_sdp = 1; 296 } 297 298 static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 299 return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 300 } 301 302 int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 303 304 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 305 log_info("l2cap_send_signaling_packet, cannot send\n"); 306 return BTSTACK_ACL_BUFFERS_FULL; 307 } 308 309 // log_info("l2cap_send_signaling_packet type %u\n", cmd); 310 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 311 va_list argptr; 312 va_start(argptr, identifier); 313 uint16_t len = l2cap_create_signaling_internal(acl_buffer, handle, cmd, identifier, argptr); 314 va_end(argptr); 315 // log_info("l2cap_send_signaling_packet con %u!\n", handle); 316 return hci_send_acl_packet(acl_buffer, len); 317 } 318 319 uint8_t *l2cap_get_outgoing_buffer(void){ 320 return hci_get_outgoing_acl_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 321 } 322 323 int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 324 325 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 326 log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid); 327 return BTSTACK_ACL_BUFFERS_FULL; 328 } 329 330 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 331 if (!channel) { 332 log_error("l2cap_send_internal no channel for cid 0x%02x\n", local_cid); 333 return -1; // TODO: define error 334 } 335 336 if (channel->packets_granted == 0){ 337 log_error("l2cap_send_internal cid 0x%02x, no credits!\n", local_cid); 338 return -1; // TODO: define error 339 } 340 341 --channel->packets_granted; 342 343 log_debug("l2cap_send_internal cid 0x%02x, handle %u, 1 credit used, credits left %u;\n", 344 local_cid, channel->handle, channel->packets_granted); 345 346 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 347 348 // 0 - Connection handle : PB=10 : BC=00 349 bt_store_16(acl_buffer, 0, channel->handle | (2 << 12) | (0 << 14)); 350 // 2 - ACL length 351 bt_store_16(acl_buffer, 2, len + 4); 352 // 4 - L2CAP packet length 353 bt_store_16(acl_buffer, 4, len + 0); 354 // 6 - L2CAP channel DEST 355 bt_store_16(acl_buffer, 6, channel->remote_cid); 356 // send 357 int err = hci_send_acl_packet(acl_buffer, len+8); 358 359 l2cap_hand_out_credits(); 360 361 return err; 362 } 363 364 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 365 366 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 367 log_info("l2cap_send_prepared_to_handle cid 0x%02x, cannot send\n", cid); 368 return BTSTACK_ACL_BUFFERS_FULL; 369 } 370 371 log_debug("l2cap_send_prepared_to_handle cid 0x%02x, handle %u\n", cid, handle); 372 373 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 374 375 // 0 - Connection handle : PB=10 : BC=00 376 bt_store_16(acl_buffer, 0, handle | (2 << 12) | (0 << 14)); 377 // 2 - ACL length 378 bt_store_16(acl_buffer, 2, len + 4); 379 // 4 - L2CAP packet length 380 bt_store_16(acl_buffer, 4, len + 0); 381 // 6 - L2CAP channel DEST 382 bt_store_16(acl_buffer, 6, cid); 383 // send 384 int err = hci_send_acl_packet(acl_buffer, len+8); 385 386 l2cap_hand_out_credits(); 387 388 return err; 389 } 390 391 int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 392 393 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 394 log_info("l2cap_send_internal cid 0x%02x, cannot send\n", local_cid); 395 return BTSTACK_ACL_BUFFERS_FULL; 396 } 397 398 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 399 400 memcpy(&acl_buffer[8], data, len); 401 402 return l2cap_send_prepared(local_cid, len); 403 } 404 405 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 406 407 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 408 log_info("l2cap_send_internal cid 0x%02x, cannot send\n", cid); 409 return BTSTACK_ACL_BUFFERS_FULL; 410 } 411 412 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 413 414 memcpy(&acl_buffer[8], data, len); 415 416 return l2cap_send_prepared_connectionless(handle, cid, len); 417 } 418 419 int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 420 return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 421 } 422 423 static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 424 channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 425 } 426 427 static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 428 channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 429 } 430 431 432 433 // MARK: L2CAP_RUN 434 // process outstanding signaling tasks 435 void l2cap_run(void){ 436 437 // check pending signaling responses 438 while (signaling_responses_pending){ 439 440 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 441 442 hci_con_handle_t handle = signaling_responses[0].handle; 443 uint8_t sig_id = signaling_responses[0].sig_id; 444 uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 445 uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 446 447 switch (signaling_responses[0].code){ 448 case CONNECTION_REQUEST: 449 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 450 // also disconnect if result is 0x0003 - security blocked 451 hci_disconnect_security_block(handle); 452 break; 453 case ECHO_REQUEST: 454 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 455 break; 456 case INFORMATION_REQUEST: 457 switch (infoType){ 458 case 1: { // Connectionless MTU 459 uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 460 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 461 break; 462 } 463 case 2: { // Extended Features Supported 464 // extended features request supported, only supporing fixed channel map 465 uint32_t features = 0x80; 466 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 467 break; 468 } 469 case 3: { // Fixed Channels Supported 470 uint8_t map[8]; 471 memset(map, 0, 8); 472 map[0] = 0x01; // L2CAP Signaling Channel 473 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 474 break; 475 } 476 default: 477 // all other types are not supported 478 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 479 break; 480 } 481 break; 482 case COMMAND_REJECT: 483 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 484 break; 485 default: 486 // should not happen 487 break; 488 } 489 490 // remove first item 491 signaling_responses_pending--; 492 int i; 493 for (i=0; i < signaling_responses_pending; i++){ 494 memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 495 } 496 } 497 498 uint8_t config_options[4]; 499 linked_item_t *it; 500 linked_item_t *next; 501 for (it = (linked_item_t *) l2cap_channels; it ; it = next){ 502 next = it->next; // cache next item as current item might get freed 503 504 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 505 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)) break; 506 507 l2cap_channel_t * channel = (l2cap_channel_t *) it; 508 509 // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 510 511 512 switch (channel->state){ 513 514 case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 515 case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 516 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 517 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 518 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 519 } 520 break; 521 522 case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 523 // send connection request - set state first 524 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 525 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 526 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 527 break; 528 529 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 530 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 531 // discard channel - l2cap_finialize_channel_close without sending l2cap close event 532 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); // -- remove from list 533 btstack_memory_l2cap_channel_free(channel); 534 break; 535 536 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 537 channel->state = L2CAP_STATE_CONFIG; 538 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 539 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 540 break; 541 542 case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 543 // success, start l2cap handshake 544 channel->local_sig_id = l2cap_next_sig_id(); 545 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 546 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 547 l2cap_start_rtx(channel); 548 break; 549 550 case L2CAP_STATE_CONFIG: 551 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 552 uint16_t flags = 0; 553 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 554 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 555 flags = 1; 556 } else { 557 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 558 } 559 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 560 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 561 } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 562 config_options[0] = 1; // MTU 563 config_options[1] = 2; // len param 564 bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 565 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 566 channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 567 } else { 568 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 569 } 570 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 571 } 572 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 573 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 574 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 575 channel->local_sig_id = l2cap_next_sig_id(); 576 config_options[0] = 1; // MTU 577 config_options[1] = 2; // len param 578 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 579 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 580 l2cap_start_rtx(channel); 581 } 582 if (l2cap_channel_ready_for_open(channel)){ 583 channel->state = L2CAP_STATE_OPEN; 584 l2cap_emit_channel_opened(channel, 0); // success 585 l2cap_emit_credits(channel, 1); 586 } 587 break; 588 589 case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 590 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 591 // we don't start an RTX timer for a disconnect - there's no point in closing the channel if the other side doesn't respond :) 592 l2cap_finialize_channel_close(channel); // -- remove from list 593 break; 594 595 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 596 channel->local_sig_id = l2cap_next_sig_id(); 597 channel->state = L2CAP_STATE_WAIT_DISCONNECT; 598 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 599 break; 600 default: 601 break; 602 } 603 } 604 } 605 606 uint16_t l2cap_max_mtu(void){ 607 return hci_max_acl_data_packet_length() - L2CAP_HEADER_SIZE; 608 } 609 610 static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 611 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 612 log_info("l2cap_handle_connection_complete expected state"); 613 // success, start l2cap handshake 614 channel->handle = handle; 615 channel->local_cid = l2cap_next_local_cid(); 616 // check remote SSP feature first 617 channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 618 } 619 } 620 621 static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 622 if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 623 624 // we have been waiting for remote supported features, if both support SSP, 625 log_info("l2cap received remote supported features, sec_level_0_allowed for psm %u = %u", channel->psm, l2cap_security_level_0_allowed_for_PSM(channel->psm)); 626 if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 627 // request security level 2 628 channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 629 gap_request_security_level(channel->handle, LEVEL_2); 630 return; 631 } 632 // fine, go ahead 633 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 634 } 635 636 // open outgoing L2CAP channel 637 void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, 638 bd_addr_t address, uint16_t psm, uint16_t mtu){ 639 640 log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 641 642 // alloc structure 643 l2cap_channel_t * chan = (l2cap_channel_t*) btstack_memory_l2cap_channel_get(); 644 if (!chan) { 645 // emit error event 646 l2cap_channel_t dummy_channel; 647 BD_ADDR_COPY(dummy_channel.address, address); 648 dummy_channel.psm = psm; 649 l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 650 return; 651 } 652 // limit local mtu to max acl packet length 653 if (mtu > l2cap_max_mtu()) { 654 mtu = l2cap_max_mtu(); 655 } 656 657 // fill in 658 BD_ADDR_COPY(chan->address, address); 659 chan->psm = psm; 660 chan->handle = 0; 661 chan->connection = connection; 662 chan->packet_handler = packet_handler; 663 chan->remote_mtu = L2CAP_MINIMAL_MTU; 664 chan->local_mtu = mtu; 665 chan->packets_granted = 0; 666 667 // set initial state 668 chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 669 chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 670 chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 671 chan->local_sig_id = L2CAP_SIG_ID_INVALID; 672 chan->required_security_level = LEVEL_0; 673 674 // add to connections list 675 linked_list_add(&l2cap_channels, (linked_item_t *) chan); 676 677 // check if hci connection is already usable 678 hci_connection_t * conn = hci_connection_for_bd_addr((bd_addr_t*)address); 679 if (conn){ 680 log_info("l2cap_create_channel_internal, hci connection already exists"); 681 l2cap_handle_connection_complete(conn->con_handle, chan); 682 // check ir remote supported fearures are already received 683 if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 684 l2cap_handle_remote_supported_features_received(chan); 685 } 686 } 687 688 l2cap_run(); 689 } 690 691 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 692 log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 693 // find channel for local_cid 694 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 695 if (channel) { 696 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 697 } 698 // process 699 l2cap_run(); 700 } 701 702 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 703 linked_item_t *it = (linked_item_t *) &l2cap_channels; 704 while (it->next){ 705 l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 706 if ( ! BD_ADDR_CMP( channel->address, address) ){ 707 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 708 // failure, forward error code 709 l2cap_emit_channel_opened(channel, status); 710 // discard channel 711 it->next = it->next->next; 712 btstack_memory_l2cap_channel_free(channel); 713 } 714 } else { 715 it = it->next; 716 } 717 } 718 } 719 720 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 721 linked_item_t *it; 722 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 723 l2cap_channel_t * channel = (l2cap_channel_t *) it; 724 if ( ! BD_ADDR_CMP( channel->address, address) ){ 725 l2cap_handle_connection_complete(handle, channel); 726 } 727 } 728 // process 729 l2cap_run(); 730 } 731 732 void l2cap_event_handler( uint8_t *packet, uint16_t size ){ 733 734 bd_addr_t address; 735 hci_con_handle_t handle; 736 l2cap_channel_t * channel; 737 linked_item_t *it; 738 int hci_con_used; 739 740 switch(packet[0]){ 741 742 // handle connection complete events 743 case HCI_EVENT_CONNECTION_COMPLETE: 744 bt_flip_addr(address, &packet[5]); 745 if (packet[2] == 0){ 746 handle = READ_BT_16(packet, 3); 747 l2cap_handle_connection_success_for_addr(address, handle); 748 } else { 749 l2cap_handle_connection_failed_for_addr(address, packet[2]); 750 } 751 break; 752 753 // handle successful create connection cancel command 754 case HCI_EVENT_COMMAND_COMPLETE: 755 if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 756 if (packet[5] == 0){ 757 bt_flip_addr(address, &packet[6]); 758 // CONNECTION TERMINATED BY LOCAL HOST (0X16) 759 l2cap_handle_connection_failed_for_addr(address, 0x16); 760 } 761 } 762 l2cap_run(); // try sending signaling packets first 763 break; 764 765 case HCI_EVENT_COMMAND_STATUS: 766 l2cap_run(); // try sending signaling packets first 767 break; 768 769 // handle disconnection complete events 770 case HCI_EVENT_DISCONNECTION_COMPLETE: 771 // send l2cap disconnect events for all channels on this handle 772 handle = READ_BT_16(packet, 3); 773 it = (linked_item_t *) &l2cap_channels; 774 while (it->next){ 775 l2cap_channel_t * channel = (l2cap_channel_t *) it->next; 776 if ( channel->handle == handle ){ 777 // update prev item before free'ing next element - don't call l2cap_finalize_channel_close 778 it->next = it->next->next; 779 l2cap_emit_channel_closed(channel); 780 btstack_memory_l2cap_channel_free(channel); 781 } else { 782 it = it->next; 783 } 784 } 785 break; 786 787 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 788 l2cap_run(); // try sending signaling packets first 789 l2cap_hand_out_credits(); 790 break; 791 792 // HCI Connection Timeouts 793 case L2CAP_EVENT_TIMEOUT_CHECK: 794 handle = READ_BT_16(packet, 2); 795 if (hci_authentication_active_for_handle(handle)) break; 796 hci_con_used = 0; 797 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 798 channel = (l2cap_channel_t *) it; 799 if (channel->handle == handle) { 800 hci_con_used = 1; 801 } 802 } 803 if (hci_con_used) break; 804 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) break; 805 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 806 break; 807 808 case DAEMON_EVENT_HCI_PACKET_SENT: 809 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 810 channel = (l2cap_channel_t *) it; 811 if (channel->packet_handler) { 812 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 813 } 814 } 815 if (attribute_protocol_packet_handler) { 816 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 817 } 818 if (security_protocol_packet_handler) { 819 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 820 } 821 break; 822 823 case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 824 handle = READ_BT_16(packet, 3); 825 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 826 channel = (l2cap_channel_t *) it; 827 if (channel->handle != handle) continue; 828 l2cap_handle_remote_supported_features_received(channel); 829 break; 830 } 831 832 case GAP_SECURITY_LEVEL: 833 handle = READ_BT_16(packet, 2); 834 log_info("l2cap - security level update"); 835 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 836 channel = (l2cap_channel_t *) it; 837 if (channel->handle != handle) continue; 838 839 log_info("l2cap - state %u", channel->state); 840 841 gap_security_level_t actual_level = packet[4]; 842 gap_security_level_t required_level = channel->required_security_level; 843 844 switch (channel->state){ 845 case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 846 if (actual_level >= required_level){ 847 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 848 l2cap_emit_connection_request(channel); 849 } else { 850 channel->reason = 0x03; // security block 851 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 852 } 853 break; 854 855 case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 856 if (actual_level >= required_level){ 857 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 858 } else { 859 // disconnnect, authentication not good enough 860 hci_disconnect_security_block(handle); 861 } 862 break; 863 864 default: 865 break; 866 } 867 } 868 break; 869 870 default: 871 break; 872 } 873 874 // pass on 875 (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 876 877 l2cap_run(); 878 } 879 880 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 881 channel->remote_sig_id = identifier; 882 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 883 l2cap_run(); 884 } 885 886 static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 887 // Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused." 888 if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 889 signaling_responses[signaling_responses_pending].handle = handle; 890 signaling_responses[signaling_responses_pending].code = code; 891 signaling_responses[signaling_responses_pending].sig_id = sig_id; 892 signaling_responses[signaling_responses_pending].data = data; 893 signaling_responses_pending++; 894 l2cap_run(); 895 } 896 } 897 898 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 899 900 // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x\n", handle, psm, source_cid); 901 l2cap_service_t *service = l2cap_get_service(psm); 902 if (!service) { 903 // 0x0002 PSM not supported 904 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 905 return; 906 } 907 908 hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 909 if (!hci_connection) { 910 // 911 log_error("no hci_connection for handle %u\n", handle); 912 return; 913 } 914 915 // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP 916 if ( hci_ssp_supported_on_both_sides(handle) 917 && gap_security_level(handle) == LEVEL_0 918 && !l2cap_security_level_0_allowed_for_PSM(psm)){ 919 920 // 0x0003 Security Block 921 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003); 922 return; 923 } 924 925 926 // alloc structure 927 // log_info("l2cap_handle_connection_request register channel\n"); 928 l2cap_channel_t * channel = (l2cap_channel_t*) btstack_memory_l2cap_channel_get(); 929 if (!channel){ 930 // 0x0004 No resources available 931 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 932 return; 933 } 934 935 // fill in 936 BD_ADDR_COPY(channel->address, hci_connection->address); 937 channel->psm = psm; 938 channel->handle = handle; 939 channel->connection = service->connection; 940 channel->packet_handler = service->packet_handler; 941 channel->local_cid = l2cap_next_local_cid(); 942 channel->remote_cid = source_cid; 943 channel->local_mtu = service->mtu; 944 channel->remote_mtu = L2CAP_DEFAULT_MTU; 945 channel->packets_granted = 0; 946 channel->remote_sig_id = sig_id; 947 channel->required_security_level = service->required_security_level; 948 949 // limit local mtu to max acl packet length 950 if (channel->local_mtu > l2cap_max_mtu()) { 951 channel->local_mtu = l2cap_max_mtu(); 952 } 953 954 // set initial state 955 channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 956 channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 957 958 // add to connections list 959 linked_list_add(&l2cap_channels, (linked_item_t *) channel); 960 961 // assert security requirements 962 gap_request_security_level(handle, channel->required_security_level); 963 } 964 965 void l2cap_accept_connection_internal(uint16_t local_cid){ 966 log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 967 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 968 if (!channel) { 969 log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 970 return; 971 } 972 973 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 974 975 // process 976 l2cap_run(); 977 } 978 979 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 980 log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 981 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 982 if (!channel) { 983 log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 984 return; 985 } 986 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 987 channel->reason = reason; 988 l2cap_run(); 989 } 990 991 void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 992 993 channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 994 995 uint16_t flags = READ_BT_16(command, 6); 996 if (flags & 1) { 997 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 998 } 999 1000 // accept the other's configuration options 1001 uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1002 uint16_t pos = 8; 1003 while (pos < end_pos){ 1004 uint8_t option_hint = command[pos] >> 7; 1005 uint8_t option_type = command[pos] & 0x7f; 1006 log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 1007 pos++; 1008 uint8_t length = command[pos++]; 1009 // MTU { type(8): 1, len(8):2, MTU(16) } 1010 if (option_type == 1 && length == 2){ 1011 channel->remote_mtu = READ_BT_16(command, pos); 1012 // log_info("l2cap cid 0x%02x, remote mtu %u\n", channel->local_cid, channel->remote_mtu); 1013 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 1014 } 1015 // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 1016 if (option_type == 2 && length == 2){ 1017 channel->flush_timeout = READ_BT_16(command, pos); 1018 } 1019 // check for unknown options 1020 if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1021 log_info("l2cap cid %u, unknown options", channel->local_cid); 1022 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 1023 } 1024 pos += length; 1025 } 1026 } 1027 1028 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 1029 // log_info("l2cap_channel_ready_for_open 0x%02x\n", channel->state_var); 1030 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 1031 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1032 return 1; 1033 } 1034 1035 1036 void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 1037 1038 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 1039 uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1040 uint16_t result = 0; 1041 1042 log_info("L2CAP signaling handler code %u, state %u\n", code, channel->state); 1043 1044 // handle DISCONNECT REQUESTS seperately 1045 if (code == DISCONNECTION_REQUEST){ 1046 switch (channel->state){ 1047 case L2CAP_STATE_CONFIG: 1048 case L2CAP_STATE_OPEN: 1049 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 1050 case L2CAP_STATE_WAIT_DISCONNECT: 1051 l2cap_handle_disconnect_request(channel, identifier); 1052 break; 1053 1054 default: 1055 // ignore in other states 1056 break; 1057 } 1058 return; 1059 } 1060 1061 // @STATEMACHINE(l2cap) 1062 switch (channel->state) { 1063 1064 case L2CAP_STATE_WAIT_CONNECT_RSP: 1065 switch (code){ 1066 case CONNECTION_RESPONSE: 1067 l2cap_stop_rtx(channel); 1068 result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1069 switch (result) { 1070 case 0: 1071 // successful connection 1072 channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1073 channel->state = L2CAP_STATE_CONFIG; 1074 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1075 break; 1076 case 1: 1077 // connection pending. get some coffee, but start the ERTX 1078 l2cap_start_ertx(channel); 1079 break; 1080 default: 1081 // channel closed 1082 channel->state = L2CAP_STATE_CLOSED; 1083 // map l2cap connection response result to BTstack status enumeration 1084 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1085 1086 // drop link key if security block 1087 if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 1088 hci_drop_link_key_for_bd_addr(&channel->address); 1089 } 1090 1091 // discard channel 1092 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1093 btstack_memory_l2cap_channel_free(channel); 1094 break; 1095 } 1096 break; 1097 1098 default: 1099 //@TODO: implement other signaling packets 1100 break; 1101 } 1102 break; 1103 1104 case L2CAP_STATE_CONFIG: 1105 result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1106 switch (code) { 1107 case CONFIGURE_REQUEST: 1108 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1109 l2cap_signaling_handle_configure_request(channel, command); 1110 if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 1111 // only done if continuation not set 1112 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 1113 } 1114 break; 1115 case CONFIGURE_RESPONSE: 1116 l2cap_stop_rtx(channel); 1117 switch (result){ 1118 case 0: // success 1119 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 1120 break; 1121 case 4: // pending 1122 l2cap_start_ertx(channel); 1123 break; 1124 default: 1125 // retry on negative result 1126 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1127 break; 1128 } 1129 break; 1130 default: 1131 break; 1132 } 1133 if (l2cap_channel_ready_for_open(channel)){ 1134 // for open: 1135 channel->state = L2CAP_STATE_OPEN; 1136 l2cap_emit_channel_opened(channel, 0); 1137 l2cap_emit_credits(channel, 1); 1138 } 1139 break; 1140 1141 case L2CAP_STATE_WAIT_DISCONNECT: 1142 switch (code) { 1143 case DISCONNECTION_RESPONSE: 1144 l2cap_finialize_channel_close(channel); 1145 break; 1146 default: 1147 //@TODO: implement other signaling packets 1148 break; 1149 } 1150 break; 1151 1152 case L2CAP_STATE_CLOSED: 1153 // @TODO handle incoming requests 1154 break; 1155 1156 case L2CAP_STATE_OPEN: 1157 //@TODO: implement other signaling packets, e.g. re-configure 1158 break; 1159 default: 1160 break; 1161 } 1162 // log_info("new state %u\n", channel->state); 1163 } 1164 1165 1166 void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 1167 1168 // get code, signalind identifier and command len 1169 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 1170 uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1171 1172 // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 1173 if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 1174 l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1175 return; 1176 } 1177 1178 // general commands without an assigned channel 1179 switch(code) { 1180 1181 case CONNECTION_REQUEST: { 1182 uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1183 uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 1184 l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 1185 return; 1186 } 1187 1188 case ECHO_REQUEST: 1189 l2cap_register_signaling_response(handle, code, sig_id, 0); 1190 return; 1191 1192 case INFORMATION_REQUEST: { 1193 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1194 l2cap_register_signaling_response(handle, code, sig_id, infoType); 1195 return; 1196 } 1197 1198 default: 1199 break; 1200 } 1201 1202 1203 // Get potential destination CID 1204 uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1205 1206 // Find channel for this sig_id and connection handle 1207 linked_item_t *it; 1208 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1209 l2cap_channel_t * channel = (l2cap_channel_t *) it; 1210 if (channel->handle == handle) { 1211 if (code & 1) { 1212 // match odd commands (responses) by previous signaling identifier 1213 if (channel->local_sig_id == sig_id) { 1214 l2cap_signaling_handler_channel(channel, command); 1215 break; 1216 } 1217 } else { 1218 // match even commands (requests) by local channel id 1219 if (channel->local_cid == dest_cid) { 1220 l2cap_signaling_handler_channel(channel, command); 1221 break; 1222 } 1223 } 1224 } 1225 } 1226 } 1227 1228 void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 1229 1230 // Get Channel ID 1231 uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 1232 hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 1233 1234 switch (channel_id) { 1235 1236 case L2CAP_CID_SIGNALING: { 1237 1238 uint16_t command_offset = 8; 1239 while (command_offset < size) { 1240 1241 // handle signaling commands 1242 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 1243 1244 // increment command_offset 1245 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1246 } 1247 break; 1248 } 1249 1250 case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1251 if (attribute_protocol_packet_handler) { 1252 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1253 } 1254 break; 1255 1256 case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1257 if (security_protocol_packet_handler) { 1258 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1259 } 1260 break; 1261 1262 default: { 1263 // Find channel for this channel_id and connection handle 1264 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 1265 if (channel) { 1266 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1267 } 1268 break; 1269 } 1270 } 1271 } 1272 1273 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 1274 switch (packet_type) { 1275 case HCI_EVENT_PACKET: 1276 l2cap_event_handler(packet, size); 1277 break; 1278 case HCI_ACL_DATA_PACKET: 1279 l2cap_acl_handler(packet, size); 1280 break; 1281 default: 1282 break; 1283 } 1284 l2cap_run(); 1285 } 1286 1287 // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 1288 void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1289 channel->state = L2CAP_STATE_CLOSED; 1290 l2cap_emit_channel_closed(channel); 1291 // discard channel 1292 linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1293 btstack_memory_l2cap_channel_free(channel); 1294 } 1295 1296 l2cap_service_t * l2cap_get_service(uint16_t psm){ 1297 linked_item_t *it; 1298 1299 // close open channels 1300 for (it = (linked_item_t *) l2cap_services; it ; it = it->next){ 1301 l2cap_service_t * service = ((l2cap_service_t *) it); 1302 if ( service->psm == psm){ 1303 return service; 1304 }; 1305 } 1306 return NULL; 1307 } 1308 1309 void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1310 1311 log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1312 1313 // check for alread registered psm 1314 // TODO: emit error event 1315 l2cap_service_t *service = l2cap_get_service(psm); 1316 if (service) { 1317 log_error("l2cap_register_service_internal: PSM %u already registered\n", psm); 1318 l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 1319 return; 1320 } 1321 1322 // alloc structure 1323 // TODO: emit error event 1324 service = (l2cap_service_t *) btstack_memory_l2cap_service_get(); 1325 if (!service) { 1326 log_error("l2cap_register_service_internal: no memory for l2cap_service_t\n"); 1327 l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 1328 return; 1329 } 1330 1331 // fill in 1332 service->psm = psm; 1333 service->mtu = mtu; 1334 service->connection = connection; 1335 service->packet_handler = packet_handler; 1336 service->required_security_level = security_level; 1337 1338 // add to services list 1339 linked_list_add(&l2cap_services, (linked_item_t *) service); 1340 1341 // enable page scan 1342 hci_connectable_control(1); 1343 1344 // done 1345 l2cap_emit_service_registered(connection, 0, psm); 1346 } 1347 1348 void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 1349 1350 log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1351 1352 l2cap_service_t *service = l2cap_get_service(psm); 1353 if (!service) return; 1354 linked_list_remove(&l2cap_services, (linked_item_t *) service); 1355 btstack_memory_l2cap_service_free(service); 1356 1357 // disable page scan when no services registered 1358 if (!linked_list_empty(&l2cap_services)) return; 1359 hci_connectable_control(0); 1360 } 1361 1362 // 1363 void l2cap_close_connection(void *connection){ 1364 linked_item_t *it; 1365 1366 // close open channels - note to myself: no channel is freed, so no new for fancy iterator tricks 1367 l2cap_channel_t * channel; 1368 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 1369 channel = (l2cap_channel_t *) it; 1370 if (channel->connection == connection) { 1371 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1372 } 1373 } 1374 1375 // unregister services 1376 it = (linked_item_t *) &l2cap_services; 1377 while (it->next) { 1378 l2cap_service_t * service = (l2cap_service_t *) it->next; 1379 if (service->connection == connection){ 1380 it->next = it->next->next; 1381 btstack_memory_l2cap_service_free(service); 1382 } else { 1383 it = it->next; 1384 } 1385 } 1386 1387 // process 1388 l2cap_run(); 1389 } 1390 1391 // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 1392 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 1393 switch(channel_id){ 1394 case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1395 attribute_protocol_packet_handler = packet_handler; 1396 break; 1397 case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1398 security_protocol_packet_handler = packet_handler; 1399 break; 1400 } 1401 } 1402 1403 #ifdef HAVE_BLE 1404 // Request LE connection parameter update 1405 int l2cap_le_request_connection_parameter_update(uint16_t handle, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier){ 1406 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 1407 log_info("l2cap_send_signaling_packet, cannot send\n"); 1408 return BTSTACK_ACL_BUFFERS_FULL; 1409 } 1410 // log_info("l2cap_send_signaling_packet type %u\n", cmd); 1411 uint8_t *acl_buffer = hci_get_outgoing_acl_packet_buffer(); 1412 uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier); 1413 return hci_send_acl_packet(acl_buffer, len); 1414 } 1415 #endif 1416 1417