1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 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 BLUEKITCHEN GMBH 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 34 * [email protected] 35 * 36 */ 37 38 /* 39 * l2cap.c 40 * 41 * Logical Link Control and Adaption Protocl (L2CAP) 42 * 43 * Created by Matthias Ringwald on 5/16/09. 44 */ 45 46 #include "l2cap.h" 47 #include "hci.h" 48 #include "hci_dump.h" 49 #include "btstack_debug.h" 50 #include "btstack_memory.h" 51 52 #include <stdarg.h> 53 #include <string.h> 54 55 #include <stdio.h> 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 3 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 static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 70 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 71 72 // used to cache l2cap rejects, echo, and informational requests 73 static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 74 static int signaling_responses_pending; 75 76 static btstack_linked_list_t l2cap_channels; 77 static btstack_linked_list_t l2cap_services; 78 static btstack_linked_list_t l2cap_le_channels; 79 static btstack_linked_list_t l2cap_le_services; 80 static void (*packet_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 81 82 static btstack_packet_handler_t attribute_protocol_packet_handler; 83 static btstack_packet_handler_t security_protocol_packet_handler; 84 static btstack_packet_handler_t connectionless_channel_packet_handler; 85 static uint8_t require_security_level2_for_outgoing_sdp; 86 87 // prototypes 88 static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 89 static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 90 static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 91 static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 92 static void l2cap_emit_connection_request(l2cap_channel_t *channel); 93 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 94 95 96 void l2cap_init(void){ 97 signaling_responses_pending = 0; 98 99 l2cap_channels = NULL; 100 l2cap_services = NULL; 101 l2cap_le_services = NULL; 102 l2cap_le_channels = NULL; 103 104 packet_handler = null_packet_handler; 105 attribute_protocol_packet_handler = NULL; 106 security_protocol_packet_handler = NULL; 107 connectionless_channel_packet_handler = NULL; 108 109 require_security_level2_for_outgoing_sdp = 0; 110 111 // 112 // register callback with HCI 113 // 114 hci_register_packet_handler(&l2cap_packet_handler); 115 hci_connectable_control(0); // no services yet 116 } 117 118 119 /** Register L2CAP packet handlers */ 120 static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 121 } 122 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 123 packet_handler = handler; 124 } 125 126 // notify client/protocol handler 127 static void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 128 if (channel->packet_handler) { 129 (* (channel->packet_handler))(type, channel->local_cid, data, size); 130 } else { 131 (*packet_handler)(type, channel->local_cid, data, size); 132 } 133 } 134 135 void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 136 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", 137 status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 138 channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 139 uint8_t event[23]; 140 event[0] = L2CAP_EVENT_CHANNEL_OPENED; 141 event[1] = sizeof(event) - 2; 142 event[2] = status; 143 bt_flip_addr(&event[3], channel->address); 144 bt_store_16(event, 9, channel->handle); 145 bt_store_16(event, 11, channel->psm); 146 bt_store_16(event, 13, channel->local_cid); 147 bt_store_16(event, 15, channel->remote_cid); 148 bt_store_16(event, 17, channel->local_mtu); 149 bt_store_16(event, 19, channel->remote_mtu); 150 bt_store_16(event, 21, channel->flush_timeout); 151 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 152 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 153 } 154 155 void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 156 log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 157 uint8_t event[4]; 158 event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 159 event[1] = sizeof(event) - 2; 160 bt_store_16(event, 2, channel->local_cid); 161 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 162 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 163 } 164 165 void l2cap_emit_connection_request(l2cap_channel_t *channel) { 166 log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 167 bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 168 uint8_t event[16]; 169 event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 170 event[1] = sizeof(event) - 2; 171 bt_flip_addr(&event[2], channel->address); 172 bt_store_16(event, 8, channel->handle); 173 bt_store_16(event, 10, channel->psm); 174 bt_store_16(event, 12, channel->local_cid); 175 bt_store_16(event, 14, channel->remote_cid); 176 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 177 l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 178 } 179 180 static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){ 181 uint8_t event[6]; 182 event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 183 event[1] = 4; 184 bt_store_16(event, 2, handle); 185 bt_store_16(event, 4, result); 186 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 187 (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 188 } 189 190 static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 191 btstack_linked_list_iterator_t it; 192 btstack_linked_list_iterator_init(&it, &l2cap_channels); 193 while (btstack_linked_list_iterator_has_next(&it)){ 194 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 195 if ( channel->local_cid == local_cid) { 196 return channel; 197 } 198 } 199 return NULL; 200 } 201 202 int l2cap_can_send_packet_now(uint16_t local_cid){ 203 l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 204 if (!channel) return 0; 205 return hci_can_send_acl_packet_now(channel->handle); 206 } 207 208 // @deprecated 209 int l2cap_can_send_connectionless_packet_now(void){ 210 // TODO provide real handle 211 return l2cap_can_send_fixed_channel_packet_now(0x1234); 212 } 213 214 int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 215 return hci_can_send_acl_packet_now(handle); 216 } 217 218 uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 219 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 220 if (channel) { 221 return channel->remote_mtu; 222 } 223 return 0; 224 } 225 226 static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 227 btstack_linked_list_iterator_t it; 228 btstack_linked_list_iterator_init(&it, &l2cap_channels); 229 while (btstack_linked_list_iterator_has_next(&it)){ 230 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 231 if ( &channel->rtx == ts) { 232 return channel; 233 } 234 } 235 return NULL; 236 } 237 238 static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 239 l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 240 if (!ts) return; 241 242 log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 243 244 // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 245 // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 246 // notify client 247 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 248 249 // discard channel 250 // no need to stop timer here, it is removed from list during timer callback 251 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 252 btstack_memory_l2cap_channel_free(channel); 253 } 254 255 static void l2cap_stop_rtx(l2cap_channel_t * channel){ 256 log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 257 btstack_run_loop_remove_timer(&channel->rtx); 258 } 259 260 static void l2cap_start_rtx(l2cap_channel_t * channel){ 261 l2cap_stop_rtx(channel); 262 log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 263 btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 264 btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 265 btstack_run_loop_add_timer(&channel->rtx); 266 } 267 268 static void l2cap_start_ertx(l2cap_channel_t * channel){ 269 log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 270 l2cap_stop_rtx(channel); 271 btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 272 btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 273 btstack_run_loop_add_timer(&channel->rtx); 274 } 275 276 void l2cap_require_security_level_2_for_outgoing_sdp(void){ 277 require_security_level2_for_outgoing_sdp = 1; 278 } 279 280 static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 281 return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 282 } 283 284 static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 285 286 if (!hci_can_send_acl_packet_now(handle)){ 287 log_info("l2cap_send_signaling_packet, cannot send"); 288 return BTSTACK_ACL_BUFFERS_FULL; 289 } 290 291 // log_info("l2cap_send_signaling_packet type %u", cmd); 292 hci_reserve_packet_buffer(); 293 uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 294 va_list argptr; 295 va_start(argptr, identifier); 296 uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 297 va_end(argptr); 298 // log_info("l2cap_send_signaling_packet con %u!", handle); 299 return hci_send_acl_packet_buffer(len); 300 } 301 302 #ifdef HAVE_BLE 303 static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 304 305 if (!hci_can_send_acl_packet_now(handle)){ 306 log_info("l2cap_send_signaling_packet, cannot send"); 307 return BTSTACK_ACL_BUFFERS_FULL; 308 } 309 310 // log_info("l2cap_send_signaling_packet type %u", cmd); 311 hci_reserve_packet_buffer(); 312 uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 313 va_list argptr; 314 va_start(argptr, identifier); 315 uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 316 va_end(argptr); 317 // log_info("l2cap_send_signaling_packet con %u!", handle); 318 return hci_send_acl_packet_buffer(len); 319 } 320 #endif 321 322 uint8_t *l2cap_get_outgoing_buffer(void){ 323 return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 324 } 325 326 int l2cap_reserve_packet_buffer(void){ 327 return hci_reserve_packet_buffer(); 328 } 329 330 void l2cap_release_packet_buffer(void){ 331 hci_release_packet_buffer(); 332 } 333 334 335 int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 336 337 if (!hci_is_packet_buffer_reserved()){ 338 log_error("l2cap_send_prepared called without reserving packet first"); 339 return BTSTACK_ACL_BUFFERS_FULL; 340 } 341 342 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 343 if (!channel) { 344 log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 345 return -1; // TODO: define error 346 } 347 348 if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 349 log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 350 return BTSTACK_ACL_BUFFERS_FULL; 351 } 352 353 log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle); 354 355 uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 356 357 int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 358 359 // 0 - Connection handle : PB=pb : BC=00 360 bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 361 // 2 - ACL length 362 bt_store_16(acl_buffer, 2, len + 4); 363 // 4 - L2CAP packet length 364 bt_store_16(acl_buffer, 4, len + 0); 365 // 6 - L2CAP channel DEST 366 bt_store_16(acl_buffer, 6, channel->remote_cid); 367 // send 368 int err = hci_send_acl_packet_buffer(len+8); 369 370 return err; 371 } 372 373 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 374 375 if (!hci_is_packet_buffer_reserved()){ 376 log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 377 return BTSTACK_ACL_BUFFERS_FULL; 378 } 379 380 if (!hci_can_send_prepared_acl_packet_now(handle)){ 381 log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 382 return BTSTACK_ACL_BUFFERS_FULL; 383 } 384 385 log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 386 387 uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 388 389 int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 390 391 // 0 - Connection handle : PB=pb : BC=00 392 bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 393 // 2 - ACL length 394 bt_store_16(acl_buffer, 2, len + 4); 395 // 4 - L2CAP packet length 396 bt_store_16(acl_buffer, 4, len + 0); 397 // 6 - L2CAP channel DEST 398 bt_store_16(acl_buffer, 6, cid); 399 // send 400 int err = hci_send_acl_packet_buffer(len+8); 401 402 return err; 403 } 404 405 int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 406 407 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 408 if (!channel) { 409 log_error("l2cap_send_internal no channel for cid 0x%02x", local_cid); 410 return -1; // TODO: define error 411 } 412 413 if (len > channel->remote_mtu){ 414 log_error("l2cap_send_internal cid 0x%02x, data length exceeds remote MTU.", local_cid); 415 return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 416 } 417 418 if (!hci_can_send_acl_packet_now(channel->handle)){ 419 log_info("l2cap_send_internal cid 0x%02x, cannot send", local_cid); 420 return BTSTACK_ACL_BUFFERS_FULL; 421 } 422 423 hci_reserve_packet_buffer(); 424 uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 425 426 memcpy(&acl_buffer[8], data, len); 427 428 return l2cap_send_prepared(local_cid, len); 429 } 430 431 int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 432 433 if (!hci_can_send_acl_packet_now(handle)){ 434 log_info("l2cap_send_internal cid 0x%02x, cannot send", cid); 435 return BTSTACK_ACL_BUFFERS_FULL; 436 } 437 438 hci_reserve_packet_buffer(); 439 uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 440 441 memcpy(&acl_buffer[8], data, len); 442 443 return l2cap_send_prepared_connectionless(handle, cid, len); 444 } 445 446 int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 447 return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 448 } 449 450 static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 451 channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 452 } 453 454 static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 455 channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 456 } 457 458 459 460 // MARK: L2CAP_RUN 461 // process outstanding signaling tasks 462 static void l2cap_run(void){ 463 464 // log_info("l2cap_run: entered"); 465 466 // check pending signaling responses 467 while (signaling_responses_pending){ 468 469 hci_con_handle_t handle = signaling_responses[0].handle; 470 471 if (!hci_can_send_acl_packet_now(handle)) break; 472 473 uint8_t sig_id = signaling_responses[0].sig_id; 474 uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 475 uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 476 uint8_t response_code = signaling_responses[0].code; 477 478 // remove first item before sending (to avoid sending response mutliple times) 479 signaling_responses_pending--; 480 int i; 481 for (i=0; i < signaling_responses_pending; i++){ 482 memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 483 } 484 485 switch (response_code){ 486 case CONNECTION_REQUEST: 487 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 488 // also disconnect if result is 0x0003 - security blocked 489 if (result == 0x0003){ 490 hci_disconnect_security_block(handle); 491 } 492 break; 493 case ECHO_REQUEST: 494 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 495 break; 496 case INFORMATION_REQUEST: 497 switch (infoType){ 498 case 1: { // Connectionless MTU 499 uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 500 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 501 break; 502 } 503 case 2: { // Extended Features Supported 504 // extended features request supported, features: fixed channels, unicast connectionless data reception 505 uint32_t features = 0x280; 506 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 507 break; 508 } 509 case 3: { // Fixed Channels Supported 510 uint8_t map[8]; 511 memset(map, 0, 8); 512 map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 513 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 514 break; 515 } 516 default: 517 // all other types are not supported 518 l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 519 break; 520 } 521 break; 522 case COMMAND_REJECT: 523 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 524 #ifdef HAVE_BLE 525 case COMMAND_REJECT_LE: 526 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 527 break; 528 #endif 529 default: 530 // should not happen 531 break; 532 } 533 } 534 535 uint8_t config_options[4]; 536 btstack_linked_list_iterator_t it; 537 btstack_linked_list_iterator_init(&it, &l2cap_channels); 538 while (btstack_linked_list_iterator_has_next(&it)){ 539 540 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 541 // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 542 switch (channel->state){ 543 544 case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 545 case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 546 if (!hci_can_send_acl_packet_now(channel->handle)) break; 547 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 548 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 549 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 550 } 551 break; 552 553 case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 554 if (!hci_can_send_command_packet_now()) break; 555 // send connection request - set state first 556 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 557 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 558 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 559 break; 560 561 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 562 if (!hci_can_send_acl_packet_now(channel->handle)) break; 563 channel->state = L2CAP_STATE_INVALID; 564 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 565 // discard channel - l2cap_finialize_channel_close without sending l2cap close event 566 l2cap_stop_rtx(channel); 567 btstack_linked_list_iterator_remove(&it); 568 btstack_memory_l2cap_channel_free(channel); 569 break; 570 571 case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 572 if (!hci_can_send_acl_packet_now(channel->handle)) break; 573 channel->state = L2CAP_STATE_CONFIG; 574 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 575 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 576 break; 577 578 case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 579 if (!hci_can_send_acl_packet_now(channel->handle)) break; 580 // success, start l2cap handshake 581 channel->local_sig_id = l2cap_next_sig_id(); 582 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 583 l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 584 l2cap_start_rtx(channel); 585 break; 586 587 case L2CAP_STATE_CONFIG: 588 if (!hci_can_send_acl_packet_now(channel->handle)) break; 589 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 590 uint16_t flags = 0; 591 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 592 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 593 flags = 1; 594 } else { 595 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 596 } 597 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 598 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 599 } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 600 config_options[0] = 1; // MTU 601 config_options[1] = 2; // len param 602 bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 603 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 604 channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 605 } else { 606 l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 607 } 608 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 609 } 610 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 611 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 612 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 613 channel->local_sig_id = l2cap_next_sig_id(); 614 config_options[0] = 1; // MTU 615 config_options[1] = 2; // len param 616 bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 617 l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 618 l2cap_start_rtx(channel); 619 } 620 if (l2cap_channel_ready_for_open(channel)){ 621 channel->state = L2CAP_STATE_OPEN; 622 l2cap_emit_channel_opened(channel, 0); // success 623 } 624 break; 625 626 case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 627 if (!hci_can_send_acl_packet_now(channel->handle)) break; 628 channel->state = L2CAP_STATE_INVALID; 629 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 630 // 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 :) 631 l2cap_finialize_channel_close(channel); // -- remove from list 632 break; 633 634 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 635 if (!hci_can_send_acl_packet_now(channel->handle)) break; 636 channel->local_sig_id = l2cap_next_sig_id(); 637 channel->state = L2CAP_STATE_WAIT_DISCONNECT; 638 l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 639 break; 640 default: 641 break; 642 } 643 } 644 645 #ifdef HAVE_BLE 646 // send l2cap con paramter update if necessary 647 hci_connections_get_iterator(&it); 648 while(btstack_linked_list_iterator_has_next(&it)){ 649 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 650 if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 651 if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 652 switch (connection->le_con_parameter_update_state){ 653 case CON_PARAMETER_UPDATE_SEND_REQUEST: 654 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 655 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 656 connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 657 break; 658 case CON_PARAMETER_UPDATE_SEND_RESPONSE: 659 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 660 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 661 break; 662 case CON_PARAMETER_UPDATE_DENY: 663 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 664 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 665 break; 666 default: 667 break; 668 } 669 } 670 #endif 671 672 // log_info("l2cap_run: exit"); 673 } 674 675 uint16_t l2cap_max_mtu(void){ 676 return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 677 } 678 679 uint16_t l2cap_max_le_mtu(void){ 680 return l2cap_max_mtu(); 681 } 682 683 static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 684 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 685 log_info("l2cap_handle_connection_complete expected state"); 686 // success, start l2cap handshake 687 channel->handle = handle; 688 // check remote SSP feature first 689 channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 690 } 691 } 692 693 static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 694 if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 695 696 // we have been waiting for remote supported features, if both support SSP, 697 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)); 698 if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 699 // request security level 2 700 channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 701 gap_request_security_level(channel->handle, LEVEL_2); 702 return; 703 } 704 // fine, go ahead 705 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 706 } 707 708 /** 709 * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 710 * @param packet_handler 711 * @param address 712 * @param psm 713 * @param mtu 714 * @param local_cid 715 */ 716 uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid){ 717 log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 718 719 // alloc structure 720 l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 721 if (!chan) { 722 return BTSTACK_MEMORY_ALLOC_FAILED; 723 } 724 725 // Init memory (make valgrind happy) 726 memset(chan, 0, sizeof(l2cap_channel_t)); 727 // limit local mtu to max acl packet length - l2cap header 728 if (mtu > l2cap_max_mtu()) { 729 mtu = l2cap_max_mtu(); 730 } 731 732 // fill in 733 BD_ADDR_COPY(chan->address, address); 734 chan->psm = psm; 735 chan->handle = 0; 736 chan->packet_handler = channel_packet_handler; 737 chan->remote_mtu = L2CAP_MINIMAL_MTU; 738 chan->local_mtu = mtu; 739 chan->local_cid = l2cap_next_local_cid(); 740 741 // set initial state 742 chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 743 chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 744 chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 745 chan->local_sig_id = L2CAP_SIG_ID_INVALID; 746 chan->required_security_level = LEVEL_0; 747 748 // add to connections list 749 btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan); 750 751 // store local_cid 752 if (out_local_cid){ 753 *out_local_cid = chan->local_cid; 754 } 755 756 // check if hci connection is already usable 757 hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 758 if (conn){ 759 log_info("l2cap_create_channel_internal, hci connection already exists"); 760 l2cap_handle_connection_complete(conn->con_handle, chan); 761 // check if remote supported fearures are already received 762 if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 763 l2cap_handle_remote_supported_features_received(chan); 764 } 765 } 766 767 l2cap_run(); 768 769 return 0; 770 } 771 772 void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 773 log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 774 // find channel for local_cid 775 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 776 if (channel) { 777 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 778 } 779 // process 780 l2cap_run(); 781 } 782 783 static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 784 btstack_linked_list_iterator_t it; 785 btstack_linked_list_iterator_init(&it, &l2cap_channels); 786 while (btstack_linked_list_iterator_has_next(&it)){ 787 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 788 if ( BD_ADDR_CMP( channel->address, address) != 0) continue; 789 // channel for this address found 790 switch (channel->state){ 791 case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 792 case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 793 // failure, forward error code 794 l2cap_emit_channel_opened(channel, status); 795 // discard channel 796 l2cap_stop_rtx(channel); 797 btstack_linked_list_iterator_remove(&it); 798 btstack_memory_l2cap_channel_free(channel); 799 break; 800 default: 801 break; 802 } 803 } 804 } 805 806 static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 807 btstack_linked_list_iterator_t it; 808 btstack_linked_list_iterator_init(&it, &l2cap_channels); 809 while (btstack_linked_list_iterator_has_next(&it)){ 810 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 811 if ( ! BD_ADDR_CMP( channel->address, address) ){ 812 l2cap_handle_connection_complete(handle, channel); 813 } 814 } 815 // process 816 l2cap_run(); 817 } 818 819 static void l2cap_event_handler(uint8_t *packet, uint16_t size){ 820 821 bd_addr_t address; 822 hci_con_handle_t handle; 823 btstack_linked_list_iterator_t it; 824 int hci_con_used; 825 826 switch(packet[0]){ 827 828 // handle connection complete events 829 case HCI_EVENT_CONNECTION_COMPLETE: 830 bt_flip_addr(address, &packet[5]); 831 if (packet[2] == 0){ 832 handle = READ_BT_16(packet, 3); 833 l2cap_handle_connection_success_for_addr(address, handle); 834 } else { 835 l2cap_handle_connection_failed_for_addr(address, packet[2]); 836 } 837 break; 838 839 // handle successful create connection cancel command 840 case HCI_EVENT_COMMAND_COMPLETE: 841 if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 842 if (packet[5] == 0){ 843 bt_flip_addr(address, &packet[6]); 844 // CONNECTION TERMINATED BY LOCAL HOST (0X16) 845 l2cap_handle_connection_failed_for_addr(address, 0x16); 846 } 847 } 848 l2cap_run(); // try sending signaling packets first 849 break; 850 851 case HCI_EVENT_COMMAND_STATUS: 852 l2cap_run(); // try sending signaling packets first 853 break; 854 855 // handle disconnection complete events 856 case HCI_EVENT_DISCONNECTION_COMPLETE: 857 // send l2cap disconnect events for all channels on this handle and free them 858 handle = READ_BT_16(packet, 3); 859 btstack_linked_list_iterator_init(&it, &l2cap_channels); 860 while (btstack_linked_list_iterator_has_next(&it)){ 861 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 862 if (channel->handle != handle) continue; 863 l2cap_emit_channel_closed(channel); 864 l2cap_stop_rtx(channel); 865 btstack_linked_list_iterator_remove(&it); 866 btstack_memory_l2cap_channel_free(channel); 867 } 868 break; 869 870 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 871 l2cap_run(); // try sending signaling packets first 872 break; 873 874 // HCI Connection Timeouts 875 case L2CAP_EVENT_TIMEOUT_CHECK: 876 handle = READ_BT_16(packet, 2); 877 if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 878 if (hci_authentication_active_for_handle(handle)) break; 879 hci_con_used = 0; 880 btstack_linked_list_iterator_init(&it, &l2cap_channels); 881 while (btstack_linked_list_iterator_has_next(&it)){ 882 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 883 if (channel->handle != handle) continue; 884 hci_con_used = 1; 885 break; 886 } 887 if (hci_con_used) break; 888 if (!hci_can_send_command_packet_now()) break; 889 hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 890 break; 891 892 case DAEMON_EVENT_HCI_PACKET_SENT: 893 l2cap_run(); // try sending signaling packets first 894 895 btstack_linked_list_iterator_init(&it, &l2cap_channels); 896 while (btstack_linked_list_iterator_has_next(&it)){ 897 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 898 if (!channel->packet_handler) continue; 899 (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 900 } 901 if (attribute_protocol_packet_handler) { 902 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 903 } 904 if (security_protocol_packet_handler) { 905 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 906 } 907 if (connectionless_channel_packet_handler) { 908 (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 909 } 910 break; 911 912 case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 913 handle = READ_BT_16(packet, 3); 914 btstack_linked_list_iterator_init(&it, &l2cap_channels); 915 while (btstack_linked_list_iterator_has_next(&it)){ 916 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 917 if (channel->handle != handle) continue; 918 l2cap_handle_remote_supported_features_received(channel); 919 break; 920 } 921 break; 922 923 case GAP_SECURITY_LEVEL: 924 handle = READ_BT_16(packet, 2); 925 log_info("l2cap - security level update"); 926 btstack_linked_list_iterator_init(&it, &l2cap_channels); 927 while (btstack_linked_list_iterator_has_next(&it)){ 928 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 929 if (channel->handle != handle) continue; 930 931 log_info("l2cap - state %u", channel->state); 932 933 gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 934 gap_security_level_t required_level = channel->required_security_level; 935 936 switch (channel->state){ 937 case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 938 if (actual_level >= required_level){ 939 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 940 l2cap_emit_connection_request(channel); 941 } else { 942 channel->reason = 0x0003; // security block 943 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 944 } 945 break; 946 947 case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 948 if (actual_level >= required_level){ 949 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 950 } else { 951 // disconnnect, authentication not good enough 952 hci_disconnect_security_block(handle); 953 } 954 break; 955 956 default: 957 break; 958 } 959 } 960 break; 961 962 default: 963 break; 964 } 965 966 // pass on: main packet handler, att and sm packet handlers 967 (*packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 968 if (attribute_protocol_packet_handler){ 969 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 970 } 971 if (security_protocol_packet_handler) { 972 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 973 } 974 if (connectionless_channel_packet_handler) { 975 (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 976 } 977 978 l2cap_run(); 979 } 980 981 static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 982 channel->remote_sig_id = identifier; 983 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 984 l2cap_run(); 985 } 986 987 static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 988 // Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused." 989 if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 990 signaling_responses[signaling_responses_pending].handle = handle; 991 signaling_responses[signaling_responses_pending].code = code; 992 signaling_responses[signaling_responses_pending].sig_id = sig_id; 993 signaling_responses[signaling_responses_pending].data = data; 994 signaling_responses_pending++; 995 l2cap_run(); 996 } 997 } 998 999 static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1000 1001 // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1002 l2cap_service_t *service = l2cap_get_service(psm); 1003 if (!service) { 1004 // 0x0002 PSM not supported 1005 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1006 return; 1007 } 1008 1009 hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1010 if (!hci_connection) { 1011 // 1012 log_error("no hci_connection for handle %u", handle); 1013 return; 1014 } 1015 1016 // alloc structure 1017 // log_info("l2cap_handle_connection_request register channel"); 1018 l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 1019 if (!channel){ 1020 // 0x0004 No resources available 1021 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 1022 return; 1023 } 1024 // Init memory (make valgrind happy) 1025 memset(channel, 0, sizeof(l2cap_channel_t)); 1026 // fill in 1027 BD_ADDR_COPY(channel->address, hci_connection->address); 1028 channel->psm = psm; 1029 channel->handle = handle; 1030 channel->packet_handler = service->packet_handler; 1031 channel->local_cid = l2cap_next_local_cid(); 1032 channel->remote_cid = source_cid; 1033 channel->local_mtu = service->mtu; 1034 channel->remote_mtu = L2CAP_DEFAULT_MTU; 1035 channel->remote_sig_id = sig_id; 1036 channel->required_security_level = service->required_security_level; 1037 1038 // limit local mtu to max acl packet length - l2cap header 1039 if (channel->local_mtu > l2cap_max_mtu()) { 1040 channel->local_mtu = l2cap_max_mtu(); 1041 } 1042 1043 // set initial state 1044 channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1045 channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1046 1047 // add to connections list 1048 btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1049 1050 // assert security requirements 1051 gap_request_security_level(handle, channel->required_security_level); 1052 } 1053 1054 void l2cap_accept_connection_internal(uint16_t local_cid){ 1055 log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1056 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1057 if (!channel) { 1058 log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 1059 return; 1060 } 1061 1062 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1063 1064 // process 1065 l2cap_run(); 1066 } 1067 1068 void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 1069 log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1070 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1071 if (!channel) { 1072 log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 1073 return; 1074 } 1075 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1076 channel->reason = reason; 1077 l2cap_run(); 1078 } 1079 1080 static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1081 1082 channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1083 1084 uint16_t flags = READ_BT_16(command, 6); 1085 if (flags & 1) { 1086 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 1087 } 1088 1089 // accept the other's configuration options 1090 uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1091 uint16_t pos = 8; 1092 while (pos < end_pos){ 1093 uint8_t option_hint = command[pos] >> 7; 1094 uint8_t option_type = command[pos] & 0x7f; 1095 log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 1096 pos++; 1097 uint8_t length = command[pos++]; 1098 // MTU { type(8): 1, len(8):2, MTU(16) } 1099 if (option_type == 1 && length == 2){ 1100 channel->remote_mtu = READ_BT_16(command, pos); 1101 // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 1102 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 1103 } 1104 // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 1105 if (option_type == 2 && length == 2){ 1106 channel->flush_timeout = READ_BT_16(command, pos); 1107 } 1108 // check for unknown options 1109 if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1110 log_info("l2cap cid %u, unknown options", channel->local_cid); 1111 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 1112 } 1113 pos += length; 1114 } 1115 } 1116 1117 static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 1118 // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 1119 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 1120 if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1121 // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1122 if (channel->state == L2CAP_STATE_OPEN) return 0; 1123 return 1; 1124 } 1125 1126 1127 static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 1128 1129 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 1130 uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1131 uint16_t result = 0; 1132 1133 log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1134 1135 // handle DISCONNECT REQUESTS seperately 1136 if (code == DISCONNECTION_REQUEST){ 1137 switch (channel->state){ 1138 case L2CAP_STATE_CONFIG: 1139 case L2CAP_STATE_OPEN: 1140 case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 1141 case L2CAP_STATE_WAIT_DISCONNECT: 1142 l2cap_handle_disconnect_request(channel, identifier); 1143 break; 1144 1145 default: 1146 // ignore in other states 1147 break; 1148 } 1149 return; 1150 } 1151 1152 // @STATEMACHINE(l2cap) 1153 switch (channel->state) { 1154 1155 case L2CAP_STATE_WAIT_CONNECT_RSP: 1156 switch (code){ 1157 case CONNECTION_RESPONSE: 1158 l2cap_stop_rtx(channel); 1159 result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1160 switch (result) { 1161 case 0: 1162 // successful connection 1163 channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1164 channel->state = L2CAP_STATE_CONFIG; 1165 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1166 break; 1167 case 1: 1168 // connection pending. get some coffee, but start the ERTX 1169 l2cap_start_ertx(channel); 1170 break; 1171 default: 1172 // channel closed 1173 channel->state = L2CAP_STATE_CLOSED; 1174 // map l2cap connection response result to BTstack status enumeration 1175 l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1176 1177 // drop link key if security block 1178 if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 1179 hci_drop_link_key_for_bd_addr(channel->address); 1180 } 1181 1182 // discard channel 1183 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1184 btstack_memory_l2cap_channel_free(channel); 1185 break; 1186 } 1187 break; 1188 1189 default: 1190 //@TODO: implement other signaling packets 1191 break; 1192 } 1193 break; 1194 1195 case L2CAP_STATE_CONFIG: 1196 result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1197 switch (code) { 1198 case CONFIGURE_REQUEST: 1199 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1200 l2cap_signaling_handle_configure_request(channel, command); 1201 if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 1202 // only done if continuation not set 1203 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 1204 } 1205 break; 1206 case CONFIGURE_RESPONSE: 1207 l2cap_stop_rtx(channel); 1208 switch (result){ 1209 case 0: // success 1210 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 1211 break; 1212 case 4: // pending 1213 l2cap_start_ertx(channel); 1214 break; 1215 default: 1216 // retry on negative result 1217 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1218 break; 1219 } 1220 break; 1221 default: 1222 break; 1223 } 1224 if (l2cap_channel_ready_for_open(channel)){ 1225 // for open: 1226 channel->state = L2CAP_STATE_OPEN; 1227 l2cap_emit_channel_opened(channel, 0); 1228 } 1229 break; 1230 1231 case L2CAP_STATE_WAIT_DISCONNECT: 1232 switch (code) { 1233 case DISCONNECTION_RESPONSE: 1234 l2cap_finialize_channel_close(channel); 1235 break; 1236 default: 1237 //@TODO: implement other signaling packets 1238 break; 1239 } 1240 break; 1241 1242 case L2CAP_STATE_CLOSED: 1243 // @TODO handle incoming requests 1244 break; 1245 1246 case L2CAP_STATE_OPEN: 1247 //@TODO: implement other signaling packets, e.g. re-configure 1248 break; 1249 default: 1250 break; 1251 } 1252 // log_info("new state %u", channel->state); 1253 } 1254 1255 1256 static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 1257 1258 // get code, signalind identifier and command len 1259 uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 1260 uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1261 1262 // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 1263 if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 1264 l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1265 return; 1266 } 1267 1268 // general commands without an assigned channel 1269 switch(code) { 1270 1271 case CONNECTION_REQUEST: { 1272 uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1273 uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 1274 l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 1275 return; 1276 } 1277 1278 case ECHO_REQUEST: 1279 l2cap_register_signaling_response(handle, code, sig_id, 0); 1280 return; 1281 1282 case INFORMATION_REQUEST: { 1283 uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1284 l2cap_register_signaling_response(handle, code, sig_id, infoType); 1285 return; 1286 } 1287 1288 default: 1289 break; 1290 } 1291 1292 1293 // Get potential destination CID 1294 uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1295 1296 // Find channel for this sig_id and connection handle 1297 btstack_linked_list_iterator_t it; 1298 btstack_linked_list_iterator_init(&it, &l2cap_channels); 1299 while (btstack_linked_list_iterator_has_next(&it)){ 1300 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1301 if (channel->handle != handle) continue; 1302 if (code & 1) { 1303 // match odd commands (responses) by previous signaling identifier 1304 if (channel->local_sig_id == sig_id) { 1305 l2cap_signaling_handler_channel(channel, command); 1306 break; 1307 } 1308 } else { 1309 // match even commands (requests) by local channel id 1310 if (channel->local_cid == dest_cid) { 1311 l2cap_signaling_handler_channel(channel, command); 1312 break; 1313 } 1314 } 1315 } 1316 } 1317 1318 static void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 1319 1320 // Get Channel ID 1321 uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 1322 hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 1323 1324 switch (channel_id) { 1325 1326 case L2CAP_CID_SIGNALING: { 1327 1328 uint16_t command_offset = 8; 1329 while (command_offset < size) { 1330 1331 // handle signaling commands 1332 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 1333 1334 // increment command_offset 1335 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1336 } 1337 break; 1338 } 1339 1340 case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1341 if (attribute_protocol_packet_handler) { 1342 (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1343 } 1344 break; 1345 1346 case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1347 if (security_protocol_packet_handler) { 1348 (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1349 } 1350 break; 1351 1352 case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1353 if (connectionless_channel_packet_handler) { 1354 (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1355 } 1356 break; 1357 1358 case L2CAP_CID_SIGNALING_LE: { 1359 switch (packet[8]){ 1360 case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1361 uint16_t result = READ_BT_16(packet, 12); 1362 l2cap_emit_connection_parameter_update_response(handle, result); 1363 break; 1364 } 1365 case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1366 uint8_t event[10]; 1367 event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1368 event[1] = 8; 1369 memcpy(&event[2], &packet[12], 8); 1370 1371 hci_connection_t * connection = hci_connection_for_handle(handle); 1372 if (connection){ 1373 if (connection->role != HCI_ROLE_MASTER){ 1374 // reject command without notifying upper layer when not in master role 1375 uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 1376 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1377 break; 1378 } 1379 int update_parameter = 1; 1380 le_connection_parameter_range_t existing_range; 1381 gap_le_get_connection_parameter_range(existing_range); 1382 uint16_t le_conn_interval_min = READ_BT_16(packet,12); 1383 uint16_t le_conn_interval_max = READ_BT_16(packet,14); 1384 uint16_t le_conn_latency = READ_BT_16(packet,16); 1385 uint16_t le_supervision_timeout = READ_BT_16(packet,18); 1386 1387 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1388 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1389 1390 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1391 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1392 1393 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1394 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1395 1396 if (update_parameter){ 1397 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1398 connection->le_conn_interval_min = le_conn_interval_min; 1399 connection->le_conn_interval_max = le_conn_interval_max; 1400 connection->le_conn_latency = le_conn_latency; 1401 connection->le_supervision_timeout = le_supervision_timeout; 1402 } else { 1403 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1404 } 1405 connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1406 } 1407 1408 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1409 (*packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1410 1411 break; 1412 } 1413 default: { 1414 uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 1415 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1416 break; 1417 } 1418 } 1419 break; 1420 } 1421 1422 default: { 1423 // Find channel for this channel_id and connection handle 1424 l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 1425 if (channel) { 1426 l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1427 } 1428 break; 1429 } 1430 } 1431 } 1432 1433 static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 1434 switch (packet_type) { 1435 case HCI_EVENT_PACKET: 1436 l2cap_event_handler(packet, size); 1437 break; 1438 case HCI_ACL_DATA_PACKET: 1439 l2cap_acl_handler(packet, size); 1440 break; 1441 default: 1442 break; 1443 } 1444 l2cap_run(); 1445 } 1446 1447 // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 1448 void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1449 channel->state = L2CAP_STATE_CLOSED; 1450 l2cap_emit_channel_closed(channel); 1451 // discard channel 1452 l2cap_stop_rtx(channel); 1453 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1454 btstack_memory_l2cap_channel_free(channel); 1455 } 1456 1457 static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1458 btstack_linked_list_iterator_t it; 1459 btstack_linked_list_iterator_init(&it, services); 1460 while (btstack_linked_list_iterator_has_next(&it)){ 1461 l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 1462 if ( service->psm == psm){ 1463 return service; 1464 }; 1465 } 1466 return NULL; 1467 } 1468 1469 static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 1470 return l2cap_get_service_internal(&l2cap_services, psm); 1471 } 1472 1473 1474 uint8_t l2cap_register_service(btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1475 1476 log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1477 1478 // check for alread registered psm 1479 // TODO: emit error event 1480 l2cap_service_t *service = l2cap_get_service(psm); 1481 if (service) { 1482 log_error("l2cap_register_service: PSM %u already registered", psm); 1483 return L2CAP_SERVICE_ALREADY_REGISTERED; 1484 } 1485 1486 // alloc structure 1487 // TODO: emit error event 1488 service = btstack_memory_l2cap_service_get(); 1489 if (!service) { 1490 log_error("l2cap_register_service: no memory for l2cap_service_t"); 1491 return BTSTACK_MEMORY_ALLOC_FAILED; 1492 } 1493 1494 // fill in 1495 service->psm = psm; 1496 service->mtu = mtu; 1497 service->packet_handler = service_packet_handler; 1498 service->required_security_level = security_level; 1499 1500 // add to services list 1501 btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1502 1503 // enable page scan 1504 hci_connectable_control(1); 1505 1506 return 0; 1507 } 1508 1509 void l2cap_unregister_service(uint16_t psm){ 1510 1511 log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1512 1513 l2cap_service_t *service = l2cap_get_service(psm); 1514 if (!service) return; 1515 btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1516 btstack_memory_l2cap_service_free(service); 1517 1518 // disable page scan when no services registered 1519 if (!btstack_linked_list_empty(&l2cap_services)) return; 1520 hci_connectable_control(0); 1521 } 1522 1523 // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 1524 void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 1525 switch(channel_id){ 1526 case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1527 attribute_protocol_packet_handler = the_packet_handler; 1528 break; 1529 case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1530 security_protocol_packet_handler = the_packet_handler; 1531 break; 1532 case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1533 connectionless_channel_packet_handler = the_packet_handler; 1534 break; 1535 } 1536 } 1537 1538 #ifdef HAVE_BLE 1539 1540 1541 #if 0 1542 static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 1543 return l2cap_get_service_internal(&l2cap_le_services, psm); 1544 } 1545 /** 1546 * @brief Regster L2CAP LE Credit Based Flow Control Mode service 1547 * @param 1548 */ 1549 void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, 1550 uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 1551 1552 log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 1553 1554 // check for alread registered psm 1555 // TODO: emit error event 1556 l2cap_service_t *service = l2cap_le_get_service(psm); 1557 if (service) { 1558 log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 1559 l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 1560 return; 1561 } 1562 1563 // alloc structure 1564 // TODO: emit error event 1565 service = btstack_memory_l2cap_service_get(); 1566 if (!service) { 1567 log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 1568 l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 1569 return; 1570 } 1571 1572 // fill in 1573 service->psm = psm; 1574 service->mtu = mtu; 1575 service->mps = mps; 1576 service->packet_handler = packet_handler; 1577 service->required_security_level = security_level; 1578 1579 // add to services list 1580 btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 1581 1582 // done 1583 l2cap_emit_service_registered(connection, 0, psm); 1584 } 1585 1586 void l2cap_le_unregister_service(uint16_t psm) { 1587 1588 log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 1589 1590 l2cap_service_t *service = l2cap_le_get_service(psm); 1591 if (!service) return; 1592 btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 1593 btstack_memory_l2cap_service_free(service); 1594 } 1595 #endif 1596 #endif 1597