143625864Smatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 381713bceaSmatthias.ringwald /* 3943625864Smatthias.ringwald * l2cap.c 4043625864Smatthias.ringwald * 4143625864Smatthias.ringwald * Logical Link Control and Adaption Protocl (L2CAP) 4243625864Smatthias.ringwald * 4343625864Smatthias.ringwald * Created by Matthias Ringwald on 5/16/09. 4443625864Smatthias.ringwald */ 4543625864Smatthias.ringwald 4643625864Smatthias.ringwald #include "l2cap.h" 47645658c9Smatthias.ringwald #include "hci.h" 482b3c6c9bSmatthias.ringwald #include "hci_dump.h" 4916ece135SMatthias Ringwald #include "btstack_debug.h" 50d3a9df87Smatthias.ringwald #include "btstack_memory.h" 5143625864Smatthias.ringwald 5243625864Smatthias.ringwald #include <stdarg.h> 5343625864Smatthias.ringwald #include <string.h> 5443625864Smatthias.ringwald 5543625864Smatthias.ringwald #include <stdio.h> 5643625864Smatthias.ringwald 574c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 584c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 594c744e21Smatthias.ringwald 6039bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 61e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 6239bda6d5Smatthias.ringwald 6300d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6800d93d79Smatthias.ringwald 69ffbf8201SMatthias Ringwald static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 7039bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 7139bda6d5Smatthias.ringwald 7239bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 732b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 742b83fb7dSmatthias.ringwald static int signaling_responses_pending; 752b83fb7dSmatthias.ringwald 76fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 77fb37a842SMatthias Ringwald 788f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_channels; 798f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_services; 808f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels; 818f2a52f4SMatthias Ringwald static btstack_linked_list_t l2cap_le_services; 82ffbf8201SMatthias Ringwald static void (*packet_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 831e6aba47Smatthias.ringwald 84c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler; 85c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler; 86462e630dS[email protected] static btstack_packet_handler_t connectionless_channel_packet_handler; 87ac301f95S[email protected] static uint8_t require_security_level2_for_outgoing_sdp; 885652b5ffS[email protected] 8939bda6d5Smatthias.ringwald // prototypes 90fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 919c9876e7SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 92fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 93fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 94fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 95fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 9639bda6d5Smatthias.ringwald 9739bda6d5Smatthias.ringwald 9871de195eSMatthias Ringwald void l2cap_init(void){ 992b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 100808a48abSmatthias.ringwald 101f5454fc6Smatthias.ringwald l2cap_channels = NULL; 102f5454fc6Smatthias.ringwald l2cap_services = NULL; 1037192e786SMatthias Ringwald l2cap_le_services = NULL; 1047192e786SMatthias Ringwald l2cap_le_channels = NULL; 105f5454fc6Smatthias.ringwald 106f5454fc6Smatthias.ringwald packet_handler = null_packet_handler; 107c42e2ff2S[email protected] attribute_protocol_packet_handler = NULL; 108c42e2ff2S[email protected] security_protocol_packet_handler = NULL; 109462e630dS[email protected] connectionless_channel_packet_handler = NULL; 110f5454fc6Smatthias.ringwald 111ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 112ac301f95S[email protected] 113fcadd0caSmatthias.ringwald // 1142718e2e7Smatthias.ringwald // register callback with HCI 115fcadd0caSmatthias.ringwald // 116fb37a842SMatthias Ringwald hci_event_callback_registration.callback = &l2cap_packet_handler; 117fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 118fb37a842SMatthias Ringwald 119fb37a842SMatthias Ringwald hci_register_acl_packet_handler(&l2cap_packet_handler); 120fb37a842SMatthias Ringwald 121c0e866bfSmatthias.ringwald hci_connectable_control(0); // no services yet 122fcadd0caSmatthias.ringwald } 123fcadd0caSmatthias.ringwald 124fcadd0caSmatthias.ringwald 125fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 126ffbf8201SMatthias Ringwald static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 127fcadd0caSmatthias.ringwald } 128ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 129b502e1b0Smatthias.ringwald packet_handler = handler; 1301e6aba47Smatthias.ringwald } 1311e6aba47Smatthias.ringwald 132*17a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 13358de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 13458de5610Smatthias.ringwald } 13558de5610Smatthias.ringwald 13658de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 137c9dc710bS[email protected] 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", 138e0abb8e7S[email protected] status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 139c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 140c9dc710bS[email protected] uint8_t event[23]; 14158de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 14258de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 14358de5610Smatthias.ringwald event[2] = status; 14458de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 145f8fbdce0SMatthias Ringwald little_endian_store_16(event, 9, channel->handle); 146f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 147f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 148f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 149f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 150f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 151f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 15258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 153*17a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 15458de5610Smatthias.ringwald } 15558de5610Smatthias.ringwald 15658de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 157e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 15858de5610Smatthias.ringwald uint8_t event[4]; 15958de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 16058de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 161f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 16258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 163*17a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 16458de5610Smatthias.ringwald } 16558de5610Smatthias.ringwald 16658de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 167e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 168e0abb8e7S[email protected] bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 16958de5610Smatthias.ringwald uint8_t event[16]; 17058de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 17158de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 17258de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 173f8fbdce0SMatthias Ringwald little_endian_store_16(event, 8, channel->handle); 174f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 175f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 176f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 17758de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 178*17a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1790af41d30Smatthias.ringwald } 180808a48abSmatthias.ringwald 1817f02f414SMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){ 182ccf076adS[email protected] uint8_t event[6]; 183ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 184ccf076adS[email protected] event[1] = 4; 185f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, handle); 186f8fbdce0SMatthias Ringwald little_endian_store_16(event, 4, result); 187ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 188ffbf8201SMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 189ccf076adS[email protected] } 190ccf076adS[email protected] 1917f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 192665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 193665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 194665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 195665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 196b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 197f62db1e3Smatthias.ringwald return channel; 198f62db1e3Smatthias.ringwald } 199f62db1e3Smatthias.ringwald } 200f62db1e3Smatthias.ringwald return NULL; 201f62db1e3Smatthias.ringwald } 202f62db1e3Smatthias.ringwald 2036b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2046b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2056b1fde37Smatthias.ringwald if (!channel) return 0; 206a35252c8S[email protected] return hci_can_send_acl_packet_now(channel->handle); 2077856fb31S[email protected] } 2087856fb31S[email protected] 20983e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 21083e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 21183e7cdd9SMatthias Ringwald if (!channel) return 0; 21283e7cdd9SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->handle); 21383e7cdd9SMatthias Ringwald } 21483e7cdd9SMatthias Ringwald 2156cd4da6bS[email protected] int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 2166cd4da6bS[email protected] return hci_can_send_acl_packet_now(handle); 2173cab4fcaS[email protected] } 2183cab4fcaS[email protected] 21996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 22096cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 22196cbd662Smatthias.ringwald if (channel) { 22296cbd662Smatthias.ringwald return channel->remote_mtu; 22396cbd662Smatthias.ringwald } 22496cbd662Smatthias.ringwald return 0; 22596cbd662Smatthias.ringwald } 22696cbd662Smatthias.ringwald 227ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 228665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 229665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 230665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 231665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2325932bd7cS[email protected] if ( &channel->rtx == ts) { 2335932bd7cS[email protected] return channel; 2345932bd7cS[email protected] } 2355932bd7cS[email protected] } 2365932bd7cS[email protected] return NULL; 2375932bd7cS[email protected] } 2385932bd7cS[email protected] 239ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 2405932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2415932bd7cS[email protected] if (!ts) return; 2425932bd7cS[email protected] 2435932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2445932bd7cS[email protected] 2455932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2465932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2475932bd7cS[email protected] // notify client 2485932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2495932bd7cS[email protected] 2505932bd7cS[email protected] // discard channel 2519dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 252665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 2535932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 2545932bd7cS[email protected] } 2555932bd7cS[email protected] 2565932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 2575932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 258528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 2595932bd7cS[email protected] } 2605932bd7cS[email protected] 2615932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 2625932bd7cS[email protected] l2cap_stop_rtx(channel); 263cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 264528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 265528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 266528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 2675932bd7cS[email protected] } 2685932bd7cS[email protected] 2695932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 2705932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 2715932bd7cS[email protected] l2cap_stop_rtx(channel); 272528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 273528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 274528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 2755932bd7cS[email protected] } 2765932bd7cS[email protected] 27771de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 278ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 279ac301f95S[email protected] } 280ac301f95S[email protected] 281df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 282ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 283df3354fcS[email protected] } 2845932bd7cS[email protected] 2857f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 286b1d43497Smatthias.ringwald 287a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 2889da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 289b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 290b1d43497Smatthias.ringwald } 291b1d43497Smatthias.ringwald 2929da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 2932a373862S[email protected] hci_reserve_packet_buffer(); 294facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 29558de5610Smatthias.ringwald va_list argptr; 29658de5610Smatthias.ringwald va_start(argptr, identifier); 29770efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 29858de5610Smatthias.ringwald va_end(argptr); 2999da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 300826f7347S[email protected] return hci_send_acl_packet_buffer(len); 30158de5610Smatthias.ringwald } 30258de5610Smatthias.ringwald 303a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3047f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 30570efece1S[email protected] 306a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3079da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 30870efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 30970efece1S[email protected] } 31070efece1S[email protected] 3119da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3122a373862S[email protected] hci_reserve_packet_buffer(); 313facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 31470efece1S[email protected] va_list argptr; 31570efece1S[email protected] va_start(argptr, identifier); 31670efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 31770efece1S[email protected] va_end(argptr); 3189da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 319826f7347S[email protected] return hci_send_acl_packet_buffer(len); 32070efece1S[email protected] } 32170efece1S[email protected] #endif 32270efece1S[email protected] 323b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 324facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 325b1d43497Smatthias.ringwald } 3266218e6f1Smatthias.ringwald 3276b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3286b4af23dS[email protected] return hci_reserve_packet_buffer(); 3296b4af23dS[email protected] } 3306b4af23dS[email protected] 33168a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 33268a0fcf7S[email protected] hci_release_packet_buffer(); 33368a0fcf7S[email protected] } 33468a0fcf7S[email protected] 33568a0fcf7S[email protected] 336b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 337b1d43497Smatthias.ringwald 338c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 339c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 340c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 341c8b9416aS[email protected] } 342c8b9416aS[email protected] 34358de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 344b1d43497Smatthias.ringwald if (!channel) { 3459da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 346b1d43497Smatthias.ringwald return -1; // TODO: define error 3476218e6f1Smatthias.ringwald } 3486218e6f1Smatthias.ringwald 349a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 3509da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 351a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 352a35252c8S[email protected] } 353a35252c8S[email protected] 3543aff022fSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle); 355b1d43497Smatthias.ringwald 356facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 357b1d43497Smatthias.ringwald 358e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 359e9772277S[email protected] 360e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 361f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 36258de5610Smatthias.ringwald // 2 - ACL length 363f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 36458de5610Smatthias.ringwald // 4 - L2CAP packet length 365f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 36658de5610Smatthias.ringwald // 6 - L2CAP channel DEST 367f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, channel->remote_cid); 36858de5610Smatthias.ringwald // send 369826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 37091b99603Smatthias.ringwald 3716218e6f1Smatthias.ringwald return err; 37258de5610Smatthias.ringwald } 37358de5610Smatthias.ringwald 3742149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 3752149f12eSmatthias.ringwald 376c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 377c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 3782149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 3792149f12eSmatthias.ringwald } 3802149f12eSmatthias.ringwald 381a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(handle)){ 3829da54300S[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 383c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 384c8b9416aS[email protected] } 385c8b9416aS[email protected] 3869da54300S[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 3872149f12eSmatthias.ringwald 388facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 3892149f12eSmatthias.ringwald 390e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 391e9772277S[email protected] 392e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 393f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 3942149f12eSmatthias.ringwald // 2 - ACL length 395f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 3962149f12eSmatthias.ringwald // 4 - L2CAP packet length 397f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 3982149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 399f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, cid); 4002149f12eSmatthias.ringwald // send 401826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4022149f12eSmatthias.ringwald 4032149f12eSmatthias.ringwald return err; 4042149f12eSmatthias.ringwald } 4052149f12eSmatthias.ringwald 406ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 407b1d43497Smatthias.ringwald 408a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 409a35252c8S[email protected] if (!channel) { 410ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 411a35252c8S[email protected] return -1; // TODO: define error 412a35252c8S[email protected] } 413a35252c8S[email protected] 414f0efaa57S[email protected] if (len > channel->remote_mtu){ 415ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 416f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 417f0efaa57S[email protected] } 418f0efaa57S[email protected] 419a35252c8S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)){ 420ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 421b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 422b1d43497Smatthias.ringwald } 423b1d43497Smatthias.ringwald 4242a373862S[email protected] hci_reserve_packet_buffer(); 425facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 426b1d43497Smatthias.ringwald 427b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 428b1d43497Smatthias.ringwald 429b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 430b1d43497Smatthias.ringwald } 431b1d43497Smatthias.ringwald 4322149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 4332149f12eSmatthias.ringwald 434a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 435ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 4362149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4372149f12eSmatthias.ringwald } 4382149f12eSmatthias.ringwald 4392a373862S[email protected] hci_reserve_packet_buffer(); 440facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4412149f12eSmatthias.ringwald 4422149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 4432149f12eSmatthias.ringwald 4442149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 4452149f12eSmatthias.ringwald } 4462149f12eSmatthias.ringwald 4470e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 4480e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 4490e37e417S[email protected] } 4500e37e417S[email protected] 45128ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 45228ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 45328ca2b46S[email protected] } 45428ca2b46S[email protected] 45528ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 45628ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 45728ca2b46S[email protected] } 45828ca2b46S[email protected] 45928ca2b46S[email protected] 460b1d43497Smatthias.ringwald 4618158c421Smatthias.ringwald // MARK: L2CAP_RUN 4622cd0be45Smatthias.ringwald // process outstanding signaling tasks 4637f02f414SMatthias Ringwald static void l2cap_run(void){ 4642b83fb7dSmatthias.ringwald 46522c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 46622c29ab4SMatthias Ringwald 4672b83fb7dSmatthias.ringwald // check pending signaling responses 4682b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 4692b83fb7dSmatthias.ringwald 4702b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 471a35252c8S[email protected] 472a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 473a35252c8S[email protected] 4742b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 4752b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 47663a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 477f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 4782b83fb7dSmatthias.ringwald 479f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 480f53da564S[email protected] signaling_responses_pending--; 481f53da564S[email protected] int i; 482f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 483f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 484f53da564S[email protected] } 485f53da564S[email protected] 486f53da564S[email protected] switch (response_code){ 4872b360848Smatthias.ringwald case CONNECTION_REQUEST: 4882b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 4892bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 4904d816277S[email protected] if (result == 0x0003){ 4912bd8b7e7S[email protected] hci_disconnect_security_block(handle); 4924d816277S[email protected] } 4932b360848Smatthias.ringwald break; 4942b83fb7dSmatthias.ringwald case ECHO_REQUEST: 4952b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 4962b83fb7dSmatthias.ringwald break; 4972b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 4983b0484b3S[email protected] switch (infoType){ 4993b0484b3S[email protected] case 1: { // Connectionless MTU 5003b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5013b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5023b0484b3S[email protected] break; 5033b0484b3S[email protected] } 5043b0484b3S[email protected] case 2: { // Extended Features Supported 505462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 506462e630dS[email protected] uint32_t features = 0x280; 5073b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5083b0484b3S[email protected] break; 5093b0484b3S[email protected] } 5103b0484b3S[email protected] case 3: { // Fixed Channels Supported 5113b0484b3S[email protected] uint8_t map[8]; 5123b0484b3S[email protected] memset(map, 0, 8); 513462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5143b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5153b0484b3S[email protected] break; 5163b0484b3S[email protected] } 5173b0484b3S[email protected] default: 5182b83fb7dSmatthias.ringwald // all other types are not supported 5192b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5203b0484b3S[email protected] break; 5212b83fb7dSmatthias.ringwald } 5222b83fb7dSmatthias.ringwald break; 52363a7246aSmatthias.ringwald case COMMAND_REJECT: 5245ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 525a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 52670efece1S[email protected] case COMMAND_REJECT_LE: 52770efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 52863a7246aSmatthias.ringwald break; 52970efece1S[email protected] #endif 5302b83fb7dSmatthias.ringwald default: 5312b83fb7dSmatthias.ringwald // should not happen 5322b83fb7dSmatthias.ringwald break; 5332b83fb7dSmatthias.ringwald } 5342b83fb7dSmatthias.ringwald } 5352b83fb7dSmatthias.ringwald 536ae280e73Smatthias.ringwald uint8_t config_options[4]; 537665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 538665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 539665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 540baf94f06S[email protected] 541665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 54222c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 5432cd0be45Smatthias.ringwald switch (channel->state){ 5442cd0be45Smatthias.ringwald 545df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 546ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 547baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 548a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 549ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 550a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 551ad671560S[email protected] } 552ad671560S[email protected] break; 553ad671560S[email protected] 55402b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 555baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 55664472d52Smatthias.ringwald // send connection request - set state first 55764472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 55802b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 5598f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 56002b22dc4Smatthias.ringwald break; 56102b22dc4Smatthias.ringwald 562e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 563baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 56422c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 5651eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 566e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 5679dcb2fb2S[email protected] l2cap_stop_rtx(channel); 568665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 569d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 570e7ff783cSmatthias.ringwald break; 571e7ff783cSmatthias.ringwald 572552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 573baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 574fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 57528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 5762a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 577552d92a1Smatthias.ringwald break; 578552d92a1Smatthias.ringwald 5796fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 580baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 5816fdcc387Smatthias.ringwald // success, start l2cap handshake 582b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 5836fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 5842a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 5855932bd7cS[email protected] l2cap_start_rtx(channel); 5866fdcc387Smatthias.ringwald break; 5876fdcc387Smatthias.ringwald 588fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 589baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 59073cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 59163a7246aSmatthias.ringwald uint16_t flags = 0; 59228ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 59363a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 59463a7246aSmatthias.ringwald flags = 1; 59563a7246aSmatthias.ringwald } else { 59628ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 59763a7246aSmatthias.ringwald } 59863a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 59963a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 60063a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 60163a7246aSmatthias.ringwald config_options[0] = 1; // MTU 60263a7246aSmatthias.ringwald config_options[1] = 2; // len param 603f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 60463a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 60563a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 60663a7246aSmatthias.ringwald } else { 60763a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 60863a7246aSmatthias.ringwald } 60963a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 610fa8473a4Smatthias.ringwald } 61173cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 61228ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 61328ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 614b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 615ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 616ae280e73Smatthias.ringwald config_options[1] = 2; // len param 617f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 618b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6195932bd7cS[email protected] l2cap_start_rtx(channel); 620fa8473a4Smatthias.ringwald } 621fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 622552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 623552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 624fa8473a4Smatthias.ringwald } 625552d92a1Smatthias.ringwald break; 626552d92a1Smatthias.ringwald 627e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 628baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 62922c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 630b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6315932bd7cS[email protected] // 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 :) 632756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 633e7ff783cSmatthias.ringwald break; 634e7ff783cSmatthias.ringwald 635e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 636baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 637b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6382cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 6392a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 6402cd0be45Smatthias.ringwald break; 6412cd0be45Smatthias.ringwald default: 6422cd0be45Smatthias.ringwald break; 6432cd0be45Smatthias.ringwald } 6442cd0be45Smatthias.ringwald } 645da886c03S[email protected] 646a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 647da886c03S[email protected] // send l2cap con paramter update if necessary 648da886c03S[email protected] hci_connections_get_iterator(&it); 649665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 650665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 6515d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 652b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 653da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 654b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 655b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 656b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 657b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 658b68d7bc3SMatthias Ringwald break; 659da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 660b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 661b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 662da886c03S[email protected] break; 663da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 664b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 665b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 666da886c03S[email protected] break; 667da886c03S[email protected] default: 668da886c03S[email protected] break; 669da886c03S[email protected] } 670da886c03S[email protected] } 6714d7157c3S[email protected] #endif 672da886c03S[email protected] 67322c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 6742cd0be45Smatthias.ringwald } 6752cd0be45Smatthias.ringwald 6764aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 6774ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 678fa8c92f6Smatthias.ringwald } 679fa8c92f6Smatthias.ringwald 68071de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 6814ff786cfS[email protected] return l2cap_max_mtu(); 682e5e1518dS[email protected] } 683e5e1518dS[email protected] 6842df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 6852df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 6865533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 6872df5dadcS[email protected] // success, start l2cap handshake 6882df5dadcS[email protected] channel->handle = handle; 6892df5dadcS[email protected] // check remote SSP feature first 6902df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 6912df5dadcS[email protected] } 6922df5dadcS[email protected] } 6932df5dadcS[email protected] 6942df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 6952df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 6962df5dadcS[email protected] 6972df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 698ac301f95S[email protected] 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)); 6992df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 7002df5dadcS[email protected] // request security level 2 7012df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 7021429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 7032df5dadcS[email protected] return; 7042df5dadcS[email protected] } 7052df5dadcS[email protected] // fine, go ahead 7062df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7072df5dadcS[email protected] } 7082df5dadcS[email protected] 7099077cb15SMatthias Ringwald /** 7109077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 7119077cb15SMatthias Ringwald * @param packet_handler 7129077cb15SMatthias Ringwald * @param address 7139077cb15SMatthias Ringwald * @param psm 7149077cb15SMatthias Ringwald * @param mtu 7159077cb15SMatthias Ringwald * @param local_cid 7169077cb15SMatthias Ringwald */ 7179077cb15SMatthias Ringwald 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){ 7189077cb15SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 7199077cb15SMatthias Ringwald 7209077cb15SMatthias Ringwald // alloc structure 7219077cb15SMatthias Ringwald l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 7229077cb15SMatthias Ringwald if (!chan) { 7239077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 7249077cb15SMatthias Ringwald } 7259077cb15SMatthias Ringwald 7269077cb15SMatthias Ringwald // Init memory (make valgrind happy) 7279077cb15SMatthias Ringwald memset(chan, 0, sizeof(l2cap_channel_t)); 7289077cb15SMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 7299077cb15SMatthias Ringwald if (mtu > l2cap_max_mtu()) { 7309077cb15SMatthias Ringwald mtu = l2cap_max_mtu(); 7319077cb15SMatthias Ringwald } 7329077cb15SMatthias Ringwald 7339077cb15SMatthias Ringwald // fill in 7349077cb15SMatthias Ringwald BD_ADDR_COPY(chan->address, address); 7359077cb15SMatthias Ringwald chan->psm = psm; 7369077cb15SMatthias Ringwald chan->handle = 0; 7379077cb15SMatthias Ringwald chan->packet_handler = channel_packet_handler; 7389077cb15SMatthias Ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 7399077cb15SMatthias Ringwald chan->local_mtu = mtu; 7409077cb15SMatthias Ringwald chan->local_cid = l2cap_next_local_cid(); 7419077cb15SMatthias Ringwald 7429077cb15SMatthias Ringwald // set initial state 7439077cb15SMatthias Ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 7449077cb15SMatthias Ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 7459077cb15SMatthias Ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 7469077cb15SMatthias Ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 7479077cb15SMatthias Ringwald chan->required_security_level = LEVEL_0; 7489077cb15SMatthias Ringwald 7499077cb15SMatthias Ringwald // add to connections list 750665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan); 7519077cb15SMatthias Ringwald 7529077cb15SMatthias Ringwald // store local_cid 7539077cb15SMatthias Ringwald if (out_local_cid){ 7549077cb15SMatthias Ringwald *out_local_cid = chan->local_cid; 7559077cb15SMatthias Ringwald } 7569077cb15SMatthias Ringwald 7579077cb15SMatthias Ringwald // check if hci connection is already usable 7589077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 7599077cb15SMatthias Ringwald if (conn){ 760add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 7619077cb15SMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, chan); 7629077cb15SMatthias Ringwald // check if remote supported fearures are already received 7639077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 7649077cb15SMatthias Ringwald l2cap_handle_remote_supported_features_received(chan); 7659077cb15SMatthias Ringwald } 7669077cb15SMatthias Ringwald } 7679077cb15SMatthias Ringwald 7689077cb15SMatthias Ringwald l2cap_run(); 7699077cb15SMatthias Ringwald 7709077cb15SMatthias Ringwald return 0; 7719077cb15SMatthias Ringwald } 7729077cb15SMatthias Ringwald 773ce8f182eSMatthias Ringwald void 774ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 775e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 776b35f641cSmatthias.ringwald // find channel for local_cid 777b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 778f62db1e3Smatthias.ringwald if (channel) { 779e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 780f62db1e3Smatthias.ringwald } 7812cd0be45Smatthias.ringwald // process 7822cd0be45Smatthias.ringwald l2cap_run(); 78343625864Smatthias.ringwald } 7841e6aba47Smatthias.ringwald 785afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 786665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 787665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 788665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 789665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 790c22aecc9S[email protected] if ( BD_ADDR_CMP( channel->address, address) != 0) continue; 791c22aecc9S[email protected] // channel for this address found 792c22aecc9S[email protected] switch (channel->state){ 793c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 794c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 795afde0c52Smatthias.ringwald // failure, forward error code 796afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 797afde0c52Smatthias.ringwald // discard channel 7989dcb2fb2S[email protected] l2cap_stop_rtx(channel); 799665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 800d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 801c22aecc9S[email protected] break; 802c22aecc9S[email protected] default: 803c22aecc9S[email protected] break; 804afde0c52Smatthias.ringwald } 805afde0c52Smatthias.ringwald } 806afde0c52Smatthias.ringwald } 807afde0c52Smatthias.ringwald 808afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 809665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 810665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 811665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 812665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 813afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 8142df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 815afde0c52Smatthias.ringwald } 816afde0c52Smatthias.ringwald } 8176fdcc387Smatthias.ringwald // process 8186fdcc387Smatthias.ringwald l2cap_run(); 819afde0c52Smatthias.ringwald } 820b448a0e7Smatthias.ringwald 8217f02f414SMatthias Ringwald static void l2cap_event_handler(uint8_t *packet, uint16_t size){ 822afde0c52Smatthias.ringwald 823afde0c52Smatthias.ringwald bd_addr_t address; 824afde0c52Smatthias.ringwald hci_con_handle_t handle; 825665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8262d00edd4Smatthias.ringwald int hci_con_used; 827afde0c52Smatthias.ringwald 828afde0c52Smatthias.ringwald switch(packet[0]){ 829afde0c52Smatthias.ringwald 830afde0c52Smatthias.ringwald // handle connection complete events 831afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 832afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 833afde0c52Smatthias.ringwald if (packet[2] == 0){ 834f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 835afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 836afde0c52Smatthias.ringwald } else { 837afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 838afde0c52Smatthias.ringwald } 839afde0c52Smatthias.ringwald break; 840afde0c52Smatthias.ringwald 841afde0c52Smatthias.ringwald // handle successful create connection cancel command 842afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 843afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 844afde0c52Smatthias.ringwald if (packet[5] == 0){ 845afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 846afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 847afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 84803cfbabcSmatthias.ringwald } 8491e6aba47Smatthias.ringwald } 85039d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 85139d59809Smatthias.ringwald break; 85239d59809Smatthias.ringwald 85339d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 85439d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 855afde0c52Smatthias.ringwald break; 85627a923d0Smatthias.ringwald 8571e6aba47Smatthias.ringwald // handle disconnection complete events 858afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 859c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 860f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 861665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 862665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 863665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 864c22aecc9S[email protected] if (channel->handle != handle) continue; 86515ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 8669dcb2fb2S[email protected] l2cap_stop_rtx(channel); 867665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 868d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 86927a923d0Smatthias.ringwald } 870afde0c52Smatthias.ringwald break; 871fcadd0caSmatthias.ringwald 8726218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 87302b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 8746218e6f1Smatthias.ringwald break; 8756218e6f1Smatthias.ringwald 876ee091cf1Smatthias.ringwald // HCI Connection Timeouts 877afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 878f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 879bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 88080ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 8812d00edd4Smatthias.ringwald hci_con_used = 0; 882665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 883665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 884665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 885c22aecc9S[email protected] if (channel->handle != handle) continue; 8862d00edd4Smatthias.ringwald hci_con_used = 1; 887c22aecc9S[email protected] break; 888ee091cf1Smatthias.ringwald } 8892d00edd4Smatthias.ringwald if (hci_con_used) break; 890d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 8919edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 892afde0c52Smatthias.ringwald break; 893ee091cf1Smatthias.ringwald 8946e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 895b26c3003SMatthias Ringwald l2cap_run(); // try sending signaling packets first 896b26c3003SMatthias Ringwald 897665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 898665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 899665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 900c22aecc9S[email protected] if (!channel->packet_handler) continue; 9016e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 9026e6710ebSmatthias.ringwald } 9036e6710ebSmatthias.ringwald break; 9046e6710ebSmatthias.ringwald 905df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 906f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 907665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 908665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 909665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 910df3354fcS[email protected] if (channel->handle != handle) continue; 9112df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 912df3354fcS[email protected] break; 913df3354fcS[email protected] } 914c22aecc9S[email protected] break; 915df3354fcS[email protected] 9165611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 917f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 918bd63148eS[email protected] log_info("l2cap - security level update"); 919665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 920665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 921665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 922f85a9399S[email protected] if (channel->handle != handle) continue; 9235533f01eS[email protected] 924bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 925bd63148eS[email protected] 926e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 9275533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 9285533f01eS[email protected] 929df3354fcS[email protected] switch (channel->state){ 930df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 9315533f01eS[email protected] if (actual_level >= required_level){ 932f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 933f85a9399S[email protected] l2cap_emit_connection_request(channel); 9341eb2563eS[email protected] } else { 935775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 9361eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 9371eb2563eS[email protected] } 938df3354fcS[email protected] break; 939df3354fcS[email protected] 940df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 9415533f01eS[email protected] if (actual_level >= required_level){ 942df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 943df3354fcS[email protected] } else { 944df3354fcS[email protected] // disconnnect, authentication not good enough 945df3354fcS[email protected] hci_disconnect_security_block(handle); 946df3354fcS[email protected] } 947df3354fcS[email protected] break; 948df3354fcS[email protected] 949df3354fcS[email protected] default: 950df3354fcS[email protected] break; 951df3354fcS[email protected] } 952f85a9399S[email protected] } 953f85a9399S[email protected] break; 954f85a9399S[email protected] 955afde0c52Smatthias.ringwald default: 956afde0c52Smatthias.ringwald break; 957afde0c52Smatthias.ringwald } 958afde0c52Smatthias.ringwald 959bb4c225dSMatthias Ringwald // pass on: main packet handler 960ffbf8201SMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 961bd63148eS[email protected] 962bd63148eS[email protected] l2cap_run(); 9631e6aba47Smatthias.ringwald } 9641e6aba47Smatthias.ringwald 965afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 966b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 967e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 968e7ff783cSmatthias.ringwald l2cap_run(); 96984836b65Smatthias.ringwald } 97084836b65Smatthias.ringwald 9712b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 9724cf56b4aSmatthias.ringwald // Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused." 9732b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 9742b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 9752b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 9762b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 9772b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 9782b360848Smatthias.ringwald signaling_responses_pending++; 9792b360848Smatthias.ringwald l2cap_run(); 9802b360848Smatthias.ringwald } 9812b360848Smatthias.ringwald } 9822b360848Smatthias.ringwald 983b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 984645658c9Smatthias.ringwald 9859da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 986645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 987645658c9Smatthias.ringwald if (!service) { 988645658c9Smatthias.ringwald // 0x0002 PSM not supported 9892b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 990645658c9Smatthias.ringwald return; 991645658c9Smatthias.ringwald } 992645658c9Smatthias.ringwald 9935061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 994645658c9Smatthias.ringwald if (!hci_connection) { 9952b360848Smatthias.ringwald // 9969da54300S[email protected] log_error("no hci_connection for handle %u", handle); 997645658c9Smatthias.ringwald return; 998645658c9Smatthias.ringwald } 9992bd8b7e7S[email protected] 1000645658c9Smatthias.ringwald // alloc structure 10019da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1002bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 10032b360848Smatthias.ringwald if (!channel){ 10042b360848Smatthias.ringwald // 0x0004 No resources available 10052b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 10062b360848Smatthias.ringwald return; 10072b360848Smatthias.ringwald } 1008c523d53dS[email protected] // Init memory (make valgrind happy) 1009c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1010645658c9Smatthias.ringwald // fill in 1011169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 1012169f8b28Smatthias.ringwald channel->psm = psm; 1013169f8b28Smatthias.ringwald channel->handle = handle; 1014f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1015b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1016b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1017fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 10180a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1019b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1020df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1021645658c9Smatthias.ringwald 1022f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 10232985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 10242985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 10259775e25bSmatthias.ringwald } 10269775e25bSmatthias.ringwald 1027645658c9Smatthias.ringwald // set initial state 1028df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1029ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1030e405ae81Smatthias.ringwald 1031645658c9Smatthias.ringwald // add to connections list 1032665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1033645658c9Smatthias.ringwald 1034f85a9399S[email protected] // assert security requirements 10351eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1036e405ae81Smatthias.ringwald } 1037645658c9Smatthias.ringwald 1038ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1039e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1040b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1041e405ae81Smatthias.ringwald if (!channel) { 1042ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1043e405ae81Smatthias.ringwald return; 1044e405ae81Smatthias.ringwald } 1045e405ae81Smatthias.ringwald 1046552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1047e405ae81Smatthias.ringwald 1048552d92a1Smatthias.ringwald // process 1049552d92a1Smatthias.ringwald l2cap_run(); 1050e405ae81Smatthias.ringwald } 1051645658c9Smatthias.ringwald 1052ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){ 1053e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1054b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1055e405ae81Smatthias.ringwald if (!channel) { 1056ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1057e405ae81Smatthias.ringwald return; 1058e405ae81Smatthias.ringwald } 1059e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1060e7ff783cSmatthias.ringwald channel->reason = reason; 1061e7ff783cSmatthias.ringwald l2cap_run(); 1062645658c9Smatthias.ringwald } 1063645658c9Smatthias.ringwald 10647f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1065b1988dceSmatthias.ringwald 1066b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1067b1988dceSmatthias.ringwald 1068f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 106963a7246aSmatthias.ringwald if (flags & 1) { 107063a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 107163a7246aSmatthias.ringwald } 107263a7246aSmatthias.ringwald 10732784b77dSmatthias.ringwald // accept the other's configuration options 1074f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 10753de7c0caSmatthias.ringwald uint16_t pos = 8; 10763de7c0caSmatthias.ringwald while (pos < end_pos){ 107763a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 107863a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 107963a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 108063a7246aSmatthias.ringwald pos++; 10811dc511deSmatthias.ringwald uint8_t length = command[pos++]; 10821dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 108363a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1084f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 10859da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 108663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 108763a7246aSmatthias.ringwald } 10880fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 10890fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1090f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 10910fe7a9d0S[email protected] } 109263a7246aSmatthias.ringwald // check for unknown options 109363a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1094c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 109563a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 10961dc511deSmatthias.ringwald } 10971dc511deSmatthias.ringwald pos += length; 10981dc511deSmatthias.ringwald } 10992784b77dSmatthias.ringwald } 11002784b77dSmatthias.ringwald 1101fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 11029da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 110373cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 110473cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1105019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1106019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1107fa8473a4Smatthias.ringwald return 1; 1108fa8473a4Smatthias.ringwald } 1109fa8473a4Smatthias.ringwald 1110fa8473a4Smatthias.ringwald 11117f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 11121e6aba47Smatthias.ringwald 111300d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 111400d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 111538e5900eSmatthias.ringwald uint16_t result = 0; 11161e6aba47Smatthias.ringwald 11179da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1118b35f641cSmatthias.ringwald 11199a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 11209a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 11219a011532Smatthias.ringwald switch (channel->state){ 1122fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 11239a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 11242b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 11259a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 11269a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 11279a011532Smatthias.ringwald break; 11289a011532Smatthias.ringwald 11299a011532Smatthias.ringwald default: 11309a011532Smatthias.ringwald // ignore in other states 11319a011532Smatthias.ringwald break; 11329a011532Smatthias.ringwald } 11339a011532Smatthias.ringwald return; 11349a011532Smatthias.ringwald } 11359a011532Smatthias.ringwald 113656081214Smatthias.ringwald // @STATEMACHINE(l2cap) 11371e6aba47Smatthias.ringwald switch (channel->state) { 11381e6aba47Smatthias.ringwald 11391e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 11401e6aba47Smatthias.ringwald switch (code){ 11411e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 11425932bd7cS[email protected] l2cap_stop_rtx(channel); 1143f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 114438e5900eSmatthias.ringwald switch (result) { 114538e5900eSmatthias.ringwald case 0: 1146169f8b28Smatthias.ringwald // successful connection 1147f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1148fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 114928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 115038e5900eSmatthias.ringwald break; 115138e5900eSmatthias.ringwald case 1: 11525932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 11535932bd7cS[email protected] l2cap_start_ertx(channel); 115438e5900eSmatthias.ringwald break; 115538e5900eSmatthias.ringwald default: 1156eb920dbeSmatthias.ringwald // channel closed 1157eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1158f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 115938e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1160eb920dbeSmatthias.ringwald 1161eb920dbeSmatthias.ringwald // drop link key if security block 1162eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 11632e77e513S[email protected] hci_drop_link_key_for_bd_addr(channel->address); 1164eb920dbeSmatthias.ringwald } 1165eb920dbeSmatthias.ringwald 1166eb920dbeSmatthias.ringwald // discard channel 1167665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1168d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 116938e5900eSmatthias.ringwald break; 11701e6aba47Smatthias.ringwald } 11711e6aba47Smatthias.ringwald break; 117238e5900eSmatthias.ringwald 117338e5900eSmatthias.ringwald default: 11741e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 117538e5900eSmatthias.ringwald break; 11761e6aba47Smatthias.ringwald } 11771e6aba47Smatthias.ringwald break; 11781e6aba47Smatthias.ringwald 1179fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1180f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1181ae280e73Smatthias.ringwald switch (code) { 1182ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 118328ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1184ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 118563a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 118663a7246aSmatthias.ringwald // only done if continuation not set 118763a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 118863a7246aSmatthias.ringwald } 1189ae280e73Smatthias.ringwald break; 11901e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 11915932bd7cS[email protected] l2cap_stop_rtx(channel); 11925932bd7cS[email protected] switch (result){ 11935932bd7cS[email protected] case 0: // success 11945932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 11955932bd7cS[email protected] break; 11965932bd7cS[email protected] case 4: // pending 11975932bd7cS[email protected] l2cap_start_ertx(channel); 11985932bd7cS[email protected] break; 11995932bd7cS[email protected] default: 1200fe9d8984S[email protected] // retry on negative result 1201fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1202fe9d8984S[email protected] break; 1203fe9d8984S[email protected] } 12045a67bd4aSmatthias.ringwald break; 12055a67bd4aSmatthias.ringwald default: 12065a67bd4aSmatthias.ringwald break; 12071e6aba47Smatthias.ringwald } 1208fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1209fa8473a4Smatthias.ringwald // for open: 12105a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1211fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1212c8e4258aSmatthias.ringwald } 1213c8e4258aSmatthias.ringwald break; 1214f62db1e3Smatthias.ringwald 1215f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1216f62db1e3Smatthias.ringwald switch (code) { 1217f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 121827a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 121927a923d0Smatthias.ringwald break; 12205a67bd4aSmatthias.ringwald default: 12215a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 12225a67bd4aSmatthias.ringwald break; 122327a923d0Smatthias.ringwald } 122427a923d0Smatthias.ringwald break; 122584836b65Smatthias.ringwald 122684836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 122784836b65Smatthias.ringwald // @TODO handle incoming requests 122884836b65Smatthias.ringwald break; 122984836b65Smatthias.ringwald 123084836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 123184836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 123284836b65Smatthias.ringwald break; 123310642e45Smatthias.ringwald default: 123410642e45Smatthias.ringwald break; 123527a923d0Smatthias.ringwald } 12369da54300S[email protected] // log_info("new state %u", channel->state); 123727a923d0Smatthias.ringwald } 123827a923d0Smatthias.ringwald 123900d93d79Smatthias.ringwald 12407f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 124100d93d79Smatthias.ringwald 124200d93d79Smatthias.ringwald // get code, signalind identifier and command len 124300d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 124400d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 124500d93d79Smatthias.ringwald 124600d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 124700d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 124863a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 124900d93d79Smatthias.ringwald return; 125000d93d79Smatthias.ringwald } 125100d93d79Smatthias.ringwald 125200d93d79Smatthias.ringwald // general commands without an assigned channel 125300d93d79Smatthias.ringwald switch(code) { 125400d93d79Smatthias.ringwald 125500d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1256f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1257f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 125800d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 12592b83fb7dSmatthias.ringwald return; 126000d93d79Smatthias.ringwald } 126100d93d79Smatthias.ringwald 12622b360848Smatthias.ringwald case ECHO_REQUEST: 12632b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 12642b83fb7dSmatthias.ringwald return; 126500d93d79Smatthias.ringwald 126600d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1267f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 12682b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 12692b83fb7dSmatthias.ringwald return; 127000d93d79Smatthias.ringwald } 127100d93d79Smatthias.ringwald 127200d93d79Smatthias.ringwald default: 127300d93d79Smatthias.ringwald break; 127400d93d79Smatthias.ringwald } 127500d93d79Smatthias.ringwald 127600d93d79Smatthias.ringwald 127700d93d79Smatthias.ringwald // Get potential destination CID 1278f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 127900d93d79Smatthias.ringwald 128000d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1281665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1282665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1283665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1284665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1285c22aecc9S[email protected] if (channel->handle != handle) continue; 128600d93d79Smatthias.ringwald if (code & 1) { 1287b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1288b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 128900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 12904e32727eSmatthias.ringwald break; 129100d93d79Smatthias.ringwald } 129200d93d79Smatthias.ringwald } else { 1293b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 129400d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 129500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 12964e32727eSmatthias.ringwald break; 129700d93d79Smatthias.ringwald } 129800d93d79Smatthias.ringwald } 129900d93d79Smatthias.ringwald } 130000d93d79Smatthias.ringwald } 130100d93d79Smatthias.ringwald 13027f02f414SMatthias Ringwald static void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 130300d93d79Smatthias.ringwald 130400d93d79Smatthias.ringwald // Get Channel ID 130500d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 130600d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 130700d93d79Smatthias.ringwald 13085652b5ffS[email protected] switch (channel_id) { 13095652b5ffS[email protected] 13105652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 13115652b5ffS[email protected] 131200d93d79Smatthias.ringwald uint16_t command_offset = 8; 131300d93d79Smatthias.ringwald while (command_offset < size) { 131400d93d79Smatthias.ringwald 131500d93d79Smatthias.ringwald // handle signaling commands 131600d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 131700d93d79Smatthias.ringwald 131800d93d79Smatthias.ringwald // increment command_offset 1319f8fbdce0SMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 132000d93d79Smatthias.ringwald } 13215652b5ffS[email protected] break; 132200d93d79Smatthias.ringwald } 132300d93d79Smatthias.ringwald 13245652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 13255652b5ffS[email protected] if (attribute_protocol_packet_handler) { 13265652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13275652b5ffS[email protected] } 13285652b5ffS[email protected] break; 13295652b5ffS[email protected] 13305652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 13315652b5ffS[email protected] if (security_protocol_packet_handler) { 13325652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13335652b5ffS[email protected] } 13345652b5ffS[email protected] break; 13355652b5ffS[email protected] 1336462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1337462e630dS[email protected] if (connectionless_channel_packet_handler) { 1338462e630dS[email protected] (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1339462e630dS[email protected] } 1340462e630dS[email protected] break; 1341462e630dS[email protected] 13421bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1343ccf076adS[email protected] switch (packet[8]){ 1344ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1345f8fbdce0SMatthias Ringwald uint16_t result = little_endian_read_16(packet, 12); 1346ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1347ccf076adS[email protected] break; 1348ccf076adS[email protected] } 1349ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1350ccf076adS[email protected] uint8_t event[10]; 1351ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1352ccf076adS[email protected] event[1] = 8; 1353ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1354da886c03S[email protected] 1355da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1356da886c03S[email protected] if (connection){ 13576d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 13586d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 13596d91fb6cSMatthias Ringwald uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 13606d91fb6cSMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 13616d91fb6cSMatthias Ringwald break; 13626d91fb6cSMatthias Ringwald } 1363da886c03S[email protected] int update_parameter = 1; 1364a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 1365a4c06b28SMatthias Ringwald gap_le_get_connection_parameter_range(existing_range); 1366f8fbdce0SMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(packet,12); 1367f8fbdce0SMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(packet,14); 1368f8fbdce0SMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(packet,16); 1369f8fbdce0SMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(packet,18); 1370da886c03S[email protected] 1371da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1372da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1373da886c03S[email protected] 1374da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1375da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1376da886c03S[email protected] 1377da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1378da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1379da886c03S[email protected] 1380da886c03S[email protected] if (update_parameter){ 1381da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1382da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1383da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1384da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1385da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1386da886c03S[email protected] } else { 1387da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1388da886c03S[email protected] } 138979f53f1dSMatthias Ringwald connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1390da886c03S[email protected] } 1391da886c03S[email protected] 1392ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1393ffbf8201SMatthias Ringwald (*packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1394da886c03S[email protected] 1395ccf076adS[email protected] break; 1396ccf076adS[email protected] } 1397ccf076adS[email protected] default: { 13981bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 13991bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14001bbc0b23S[email protected] break; 14011bbc0b23S[email protected] } 1402ccf076adS[email protected] } 1403ccf076adS[email protected] break; 1404ccf076adS[email protected] } 14051bbc0b23S[email protected] 14065652b5ffS[email protected] default: { 140700d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 140800d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 140900d93d79Smatthias.ringwald if (channel) { 1410*17a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 141100d93d79Smatthias.ringwald } 14125652b5ffS[email protected] break; 14135652b5ffS[email protected] } 14145652b5ffS[email protected] } 141500d93d79Smatthias.ringwald } 141600d93d79Smatthias.ringwald 14172718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14182718e2e7Smatthias.ringwald switch (packet_type) { 14192718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 14202718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 14212718e2e7Smatthias.ringwald break; 14222718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 14232718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 14242718e2e7Smatthias.ringwald break; 14252718e2e7Smatthias.ringwald default: 14262718e2e7Smatthias.ringwald break; 14272718e2e7Smatthias.ringwald } 14281eb2563eS[email protected] l2cap_run(); 14292718e2e7Smatthias.ringwald } 143000d93d79Smatthias.ringwald 143115ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 143227a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1433f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1434f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1435f62db1e3Smatthias.ringwald // discard channel 14369dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1437665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1438d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1439c8e4258aSmatthias.ringwald } 14401e6aba47Smatthias.ringwald 14418f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1442665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1443665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1444665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1445665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 14469d9bbc01Smatthias.ringwald if ( service->psm == psm){ 14479d9bbc01Smatthias.ringwald return service; 14489d9bbc01Smatthias.ringwald }; 14499d9bbc01Smatthias.ringwald } 14509d9bbc01Smatthias.ringwald return NULL; 14519d9bbc01Smatthias.ringwald } 14529d9bbc01Smatthias.ringwald 14537192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 14547192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 14557192e786SMatthias Ringwald } 14567192e786SMatthias Ringwald 1457e0abb8e7S[email protected] 1458be2053a6SMatthias Ringwald uint8_t l2cap_register_service(btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1459be2053a6SMatthias Ringwald 1460be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1461e0abb8e7S[email protected] 14624bb582b6Smatthias.ringwald // check for alread registered psm 14634bb582b6Smatthias.ringwald // TODO: emit error event 14649d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1465277abc2cSmatthias.ringwald if (service) { 1466be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1467be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1468277abc2cSmatthias.ringwald } 14699d9bbc01Smatthias.ringwald 14704bb582b6Smatthias.ringwald // alloc structure 14714bb582b6Smatthias.ringwald // TODO: emit error event 1472bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1473277abc2cSmatthias.ringwald if (!service) { 1474be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1475be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1476277abc2cSmatthias.ringwald } 14779d9bbc01Smatthias.ringwald 14789d9bbc01Smatthias.ringwald // fill in 14799d9bbc01Smatthias.ringwald service->psm = psm; 14809d9bbc01Smatthias.ringwald service->mtu = mtu; 148105ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1482df3354fcS[email protected] service->required_security_level = security_level; 14839d9bbc01Smatthias.ringwald 14849d9bbc01Smatthias.ringwald // add to services list 1485665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1486c0e866bfSmatthias.ringwald 1487c0e866bfSmatthias.ringwald // enable page scan 1488c0e866bfSmatthias.ringwald hci_connectable_control(1); 14895842b6d9Smatthias.ringwald 1490be2053a6SMatthias Ringwald return 0; 14919d9bbc01Smatthias.ringwald } 14929d9bbc01Smatthias.ringwald 149302f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){ 1494e0abb8e7S[email protected] 1495e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1496e0abb8e7S[email protected] 14979d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1498037d6e48Smatthias.ringwald if (!service) return; 1499665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1500d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1501c0e866bfSmatthias.ringwald 1502c0e866bfSmatthias.ringwald // disable page scan when no services registered 1503665d90f2SMatthias Ringwald if (!btstack_linked_list_empty(&l2cap_services)) return; 1504c0e866bfSmatthias.ringwald hci_connectable_control(0); 15059d9bbc01Smatthias.ringwald } 15069d9bbc01Smatthias.ringwald 15075652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 150805ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 15095652b5ffS[email protected] switch(channel_id){ 15105652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 151105ae8de3SMatthias Ringwald attribute_protocol_packet_handler = the_packet_handler; 15125652b5ffS[email protected] break; 15135652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 151405ae8de3SMatthias Ringwald security_protocol_packet_handler = the_packet_handler; 15155652b5ffS[email protected] break; 1516462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 151705ae8de3SMatthias Ringwald connectionless_channel_packet_handler = the_packet_handler; 1518462e630dS[email protected] break; 15195652b5ffS[email protected] } 15205652b5ffS[email protected] } 15215652b5ffS[email protected] 1522a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 15237192e786SMatthias Ringwald 15247f02f414SMatthias Ringwald 15257f02f414SMatthias Ringwald #if 0 15267192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 15277192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, psm); 15287192e786SMatthias Ringwald } 15297192e786SMatthias Ringwald /** 15307192e786SMatthias Ringwald * @brief Regster L2CAP LE Credit Based Flow Control Mode service 15317192e786SMatthias Ringwald * @param 15327192e786SMatthias Ringwald */ 1533ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, 15347192e786SMatthias Ringwald uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 15357192e786SMatthias Ringwald 15367192e786SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 15377192e786SMatthias Ringwald 15387192e786SMatthias Ringwald // check for alread registered psm 15397192e786SMatthias Ringwald // TODO: emit error event 15407192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 15417192e786SMatthias Ringwald if (service) { 15427192e786SMatthias Ringwald log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 15437192e786SMatthias Ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 15447192e786SMatthias Ringwald return; 15457192e786SMatthias Ringwald } 15467192e786SMatthias Ringwald 15477192e786SMatthias Ringwald // alloc structure 15487192e786SMatthias Ringwald // TODO: emit error event 15497192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 15507192e786SMatthias Ringwald if (!service) { 15517192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 15527192e786SMatthias Ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 15537192e786SMatthias Ringwald return; 15547192e786SMatthias Ringwald } 15557192e786SMatthias Ringwald 15567192e786SMatthias Ringwald // fill in 15577192e786SMatthias Ringwald service->psm = psm; 15587192e786SMatthias Ringwald service->mtu = mtu; 15597192e786SMatthias Ringwald service->mps = mps; 15607192e786SMatthias Ringwald service->packet_handler = packet_handler; 15617192e786SMatthias Ringwald service->required_security_level = security_level; 15627192e786SMatthias Ringwald 15637192e786SMatthias Ringwald // add to services list 1564665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 15657192e786SMatthias Ringwald 15667192e786SMatthias Ringwald // done 15677192e786SMatthias Ringwald l2cap_emit_service_registered(connection, 0, psm); 15687192e786SMatthias Ringwald } 15697192e786SMatthias Ringwald 1570ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) { 15717192e786SMatthias Ringwald 15727192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 15737192e786SMatthias Ringwald 15747192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 15757192e786SMatthias Ringwald if (!service) return; 1576665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 15777192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 15787192e786SMatthias Ringwald } 15797192e786SMatthias Ringwald #endif 15807f02f414SMatthias Ringwald #endif 1581