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" 496218e6f1Smatthias.ringwald #include "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 6939bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, 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 76c42e2ff2S[email protected] static linked_list_t l2cap_channels; 77c42e2ff2S[email protected] static linked_list_t l2cap_services; 787192e786SMatthias Ringwald static linked_list_t l2cap_le_channels; 797192e786SMatthias Ringwald static linked_list_t l2cap_le_services; 8036944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 81808a48abSmatthias.ringwald static int new_credits_blocked = 0; 821e6aba47Smatthias.ringwald 83c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler; 84c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler; 85462e630dS[email protected] static btstack_packet_handler_t connectionless_channel_packet_handler; 86ac301f95S[email protected] static uint8_t require_security_level2_for_outgoing_sdp; 875652b5ffS[email protected] 8839bda6d5Smatthias.ringwald // prototypes 89fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 909c9876e7SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 91fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 92fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 93fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 94fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 9539bda6d5Smatthias.ringwald 9639bda6d5Smatthias.ringwald 9771de195eSMatthias Ringwald void l2cap_init(void){ 98808a48abSmatthias.ringwald new_credits_blocked = 0; 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 // 1162718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 117c0e866bfSmatthias.ringwald hci_connectable_control(0); // no services yet 118fcadd0caSmatthias.ringwald } 119fcadd0caSmatthias.ringwald 120fcadd0caSmatthias.ringwald 121fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 12236944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 123fcadd0caSmatthias.ringwald } 12436944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 125b502e1b0Smatthias.ringwald packet_handler = handler; 1261e6aba47Smatthias.ringwald } 1271e6aba47Smatthias.ringwald 12858de5610Smatthias.ringwald // notify client/protocol handler 1297f02f414SMatthias Ringwald static void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 13058de5610Smatthias.ringwald if (channel->packet_handler) { 13158de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 13258de5610Smatthias.ringwald } else { 13336944dffSmatthias.ringwald (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 13458de5610Smatthias.ringwald } 13558de5610Smatthias.ringwald } 13658de5610Smatthias.ringwald 13758de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 138c9dc710bS[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", 139e0abb8e7S[email protected] status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 140c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 141c9dc710bS[email protected] uint8_t event[23]; 14258de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 14358de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 14458de5610Smatthias.ringwald event[2] = status; 14558de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 14658de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 14758de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 14858de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 14958de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 1504c98aa43Smatthias.ringwald bt_store_16(event, 17, channel->local_mtu); 1514c98aa43Smatthias.ringwald bt_store_16(event, 19, channel->remote_mtu); 15254ed2ecaS[email protected] bt_store_16(event, 21, channel->flush_timeout); 15358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 15458de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 15558de5610Smatthias.ringwald } 15658de5610Smatthias.ringwald 15758de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 158e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 15958de5610Smatthias.ringwald uint8_t event[4]; 16058de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 16158de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 16258de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 16358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 16458de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 16558de5610Smatthias.ringwald } 16658de5610Smatthias.ringwald 16758de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 168e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 169e0abb8e7S[email protected] bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 17058de5610Smatthias.ringwald uint8_t event[16]; 17158de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 17258de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 17358de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 17458de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 17558de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 17658de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 17758de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 17858de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17958de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1800af41d30Smatthias.ringwald } 181808a48abSmatthias.ringwald 1827f02f414SMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){ 183ccf076adS[email protected] uint8_t event[6]; 184ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 185ccf076adS[email protected] event[1] = 4; 186ccf076adS[email protected] bt_store_16(event, 2, handle); 187ccf076adS[email protected] bt_store_16(event, 4, result); 188ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 189ccf076adS[email protected] (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); 190ccf076adS[email protected] } 191ccf076adS[email protected] 1927f02f414SMatthias Ringwald static void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 193e0abb8e7S[email protected] 194e0abb8e7S[email protected] log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits); 1956218e6f1Smatthias.ringwald // track credits 1966218e6f1Smatthias.ringwald channel->packets_granted += credits; 1976218e6f1Smatthias.ringwald 1986218e6f1Smatthias.ringwald uint8_t event[5]; 1996218e6f1Smatthias.ringwald event[0] = L2CAP_EVENT_CREDITS; 2006218e6f1Smatthias.ringwald event[1] = sizeof(event) - 2; 2016218e6f1Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 2026218e6f1Smatthias.ringwald event[4] = credits; 2036218e6f1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 2046218e6f1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 2056218e6f1Smatthias.ringwald } 2066218e6f1Smatthias.ringwald 207808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){ 208808a48abSmatthias.ringwald new_credits_blocked = blocked; 209808a48abSmatthias.ringwald } 210808a48abSmatthias.ringwald 2117f02f414SMatthias Ringwald static void l2cap_hand_out_credits(void){ 212808a48abSmatthias.ringwald 213808a48abSmatthias.ringwald if (new_credits_blocked) return; // we're told not to. used by daemon 214808a48abSmatthias.ringwald 215c22aecc9S[email protected] linked_list_iterator_t it; 216c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 217c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 218c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 2190e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 22077aee70fSMatthias Ringwald if (!hci_number_free_acl_slots_for_handle(channel->handle)) return; 2214c744e21Smatthias.ringwald if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 2228d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 2238d371091Smatthias.ringwald } 2248d371091Smatthias.ringwald } 2258d371091Smatthias.ringwald } 2268d371091Smatthias.ringwald 2277f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 228c22aecc9S[email protected] linked_list_iterator_t it; 229c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 230c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 231c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 232b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 233f62db1e3Smatthias.ringwald return channel; 234f62db1e3Smatthias.ringwald } 235f62db1e3Smatthias.ringwald } 236f62db1e3Smatthias.ringwald return NULL; 237f62db1e3Smatthias.ringwald } 238f62db1e3Smatthias.ringwald 2396b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2406b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2416b1fde37Smatthias.ringwald if (!channel) return 0; 2426b1fde37Smatthias.ringwald if (!channel->packets_granted) return 0; 243a35252c8S[email protected] return hci_can_send_acl_packet_now(channel->handle); 2447856fb31S[email protected] } 2457856fb31S[email protected] 2466cd4da6bS[email protected] // @deprecated 2473cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){ 248a35252c8S[email protected] // TODO provide real handle 2496cd4da6bS[email protected] return l2cap_can_send_fixed_channel_packet_now(0x1234); 2506cd4da6bS[email protected] } 2516cd4da6bS[email protected] 2526cd4da6bS[email protected] int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 2536cd4da6bS[email protected] return hci_can_send_acl_packet_now(handle); 2543cab4fcaS[email protected] } 2553cab4fcaS[email protected] 25696cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 25796cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 25896cbd662Smatthias.ringwald if (channel) { 25996cbd662Smatthias.ringwald return channel->remote_mtu; 26096cbd662Smatthias.ringwald } 26196cbd662Smatthias.ringwald return 0; 26296cbd662Smatthias.ringwald } 26396cbd662Smatthias.ringwald 2645932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 265c22aecc9S[email protected] linked_list_iterator_t it; 266c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 267c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 268c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 2695932bd7cS[email protected] if ( &channel->rtx == ts) { 2705932bd7cS[email protected] return channel; 2715932bd7cS[email protected] } 2725932bd7cS[email protected] } 2735932bd7cS[email protected] return NULL; 2745932bd7cS[email protected] } 2755932bd7cS[email protected] 2765932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){ 2775932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2785932bd7cS[email protected] if (!ts) return; 2795932bd7cS[email protected] 2805932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2815932bd7cS[email protected] 2825932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2835932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2845932bd7cS[email protected] // notify client 2855932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2865932bd7cS[email protected] 2875932bd7cS[email protected] // discard channel 2889dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 2895932bd7cS[email protected] linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 2905932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 2915932bd7cS[email protected] } 2925932bd7cS[email protected] 2935932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 2945932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 2955932bd7cS[email protected] run_loop_remove_timer(&channel->rtx); 2965932bd7cS[email protected] } 2975932bd7cS[email protected] 2985932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 2995932bd7cS[email protected] l2cap_stop_rtx(channel); 300cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 3015932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3025932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 3035932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3045932bd7cS[email protected] } 3055932bd7cS[email protected] 3065932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3075932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3085932bd7cS[email protected] l2cap_stop_rtx(channel); 3095932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3105932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 3115932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3125932bd7cS[email protected] } 3135932bd7cS[email protected] 31471de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 315ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 316ac301f95S[email protected] } 317ac301f95S[email protected] 318df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 319ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 320df3354fcS[email protected] } 3215932bd7cS[email protected] 3227f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 323b1d43497Smatthias.ringwald 324a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3259da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 326b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 327b1d43497Smatthias.ringwald } 328b1d43497Smatthias.ringwald 3299da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3302a373862S[email protected] hci_reserve_packet_buffer(); 331facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 33258de5610Smatthias.ringwald va_list argptr; 33358de5610Smatthias.ringwald va_start(argptr, identifier); 33470efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 33558de5610Smatthias.ringwald va_end(argptr); 3369da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 337826f7347S[email protected] return hci_send_acl_packet_buffer(len); 33858de5610Smatthias.ringwald } 33958de5610Smatthias.ringwald 34070efece1S[email protected] #ifdef HAVE_BLE 3417f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 34270efece1S[email protected] 343a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3449da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 34570efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 34670efece1S[email protected] } 34770efece1S[email protected] 3489da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3492a373862S[email protected] hci_reserve_packet_buffer(); 350facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 35170efece1S[email protected] va_list argptr; 35270efece1S[email protected] va_start(argptr, identifier); 35370efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 35470efece1S[email protected] va_end(argptr); 3559da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 356826f7347S[email protected] return hci_send_acl_packet_buffer(len); 35770efece1S[email protected] } 35870efece1S[email protected] #endif 35970efece1S[email protected] 360b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 361facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 362b1d43497Smatthias.ringwald } 3636218e6f1Smatthias.ringwald 3646b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3656b4af23dS[email protected] return hci_reserve_packet_buffer(); 3666b4af23dS[email protected] } 3676b4af23dS[email protected] 36868a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 36968a0fcf7S[email protected] hci_release_packet_buffer(); 37068a0fcf7S[email protected] } 37168a0fcf7S[email protected] 37268a0fcf7S[email protected] 373b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 374b1d43497Smatthias.ringwald 375c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 376c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 377c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 378c8b9416aS[email protected] } 379c8b9416aS[email protected] 38058de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 381b1d43497Smatthias.ringwald if (!channel) { 3829da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 383b1d43497Smatthias.ringwald return -1; // TODO: define error 3846218e6f1Smatthias.ringwald } 3856218e6f1Smatthias.ringwald 386b1d43497Smatthias.ringwald if (channel->packets_granted == 0){ 3879da54300S[email protected] log_error("l2cap_send_prepared cid 0x%02x, no credits!", local_cid); 388b1d43497Smatthias.ringwald return -1; // TODO: define error 389b1d43497Smatthias.ringwald } 390b1d43497Smatthias.ringwald 391a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 3929da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 393a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 394a35252c8S[email protected] } 395a35252c8S[email protected] 396b1d43497Smatthias.ringwald --channel->packets_granted; 397b1d43497Smatthias.ringwald 3989da54300S[email protected] log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used, credits left %u;", 399b1d43497Smatthias.ringwald local_cid, channel->handle, channel->packets_granted); 400b1d43497Smatthias.ringwald 401facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 402b1d43497Smatthias.ringwald 403e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 404e9772277S[email protected] 405e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 406e9772277S[email protected] bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 40758de5610Smatthias.ringwald // 2 - ACL length 40858de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 40958de5610Smatthias.ringwald // 4 - L2CAP packet length 41058de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 41158de5610Smatthias.ringwald // 6 - L2CAP channel DEST 41258de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 41358de5610Smatthias.ringwald // send 414826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 41591b99603Smatthias.ringwald 41691b99603Smatthias.ringwald l2cap_hand_out_credits(); 41791b99603Smatthias.ringwald 4186218e6f1Smatthias.ringwald return err; 41958de5610Smatthias.ringwald } 42058de5610Smatthias.ringwald 4212149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 4222149f12eSmatthias.ringwald 423c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 424c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4252149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4262149f12eSmatthias.ringwald } 4272149f12eSmatthias.ringwald 428a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(handle)){ 4299da54300S[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 430c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 431c8b9416aS[email protected] } 432c8b9416aS[email protected] 4339da54300S[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 4342149f12eSmatthias.ringwald 435facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4362149f12eSmatthias.ringwald 437e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 438e9772277S[email protected] 439e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 440e9772277S[email protected] bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 4412149f12eSmatthias.ringwald // 2 - ACL length 4422149f12eSmatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 4432149f12eSmatthias.ringwald // 4 - L2CAP packet length 4442149f12eSmatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 4452149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 4462149f12eSmatthias.ringwald bt_store_16(acl_buffer, 6, cid); 4472149f12eSmatthias.ringwald // send 448826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4492149f12eSmatthias.ringwald 4502149f12eSmatthias.ringwald l2cap_hand_out_credits(); 4512149f12eSmatthias.ringwald 4522149f12eSmatthias.ringwald return err; 4532149f12eSmatthias.ringwald } 4542149f12eSmatthias.ringwald 455b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 456b1d43497Smatthias.ringwald 457a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 458a35252c8S[email protected] if (!channel) { 4599da54300S[email protected] log_error("l2cap_send_internal no channel for cid 0x%02x", local_cid); 460a35252c8S[email protected] return -1; // TODO: define error 461a35252c8S[email protected] } 462a35252c8S[email protected] 463f0efaa57S[email protected] if (len > channel->remote_mtu){ 464f0efaa57S[email protected] log_error("l2cap_send_internal cid 0x%02x, data length exceeds remote MTU.", local_cid); 465f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 466f0efaa57S[email protected] } 467f0efaa57S[email protected] 468a35252c8S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)){ 4699da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", local_cid); 470b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 471b1d43497Smatthias.ringwald } 472b1d43497Smatthias.ringwald 4732a373862S[email protected] hci_reserve_packet_buffer(); 474facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 475b1d43497Smatthias.ringwald 476b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 477b1d43497Smatthias.ringwald 478b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 479b1d43497Smatthias.ringwald } 480b1d43497Smatthias.ringwald 4812149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 4822149f12eSmatthias.ringwald 483a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4849da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", cid); 4852149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4862149f12eSmatthias.ringwald } 4872149f12eSmatthias.ringwald 4882a373862S[email protected] hci_reserve_packet_buffer(); 489facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4902149f12eSmatthias.ringwald 4912149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 4922149f12eSmatthias.ringwald 4932149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 4942149f12eSmatthias.ringwald } 4952149f12eSmatthias.ringwald 4960e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 4970e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 4980e37e417S[email protected] } 4990e37e417S[email protected] 50028ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 50128ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 50228ca2b46S[email protected] } 50328ca2b46S[email protected] 50428ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 50528ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 50628ca2b46S[email protected] } 50728ca2b46S[email protected] 50828ca2b46S[email protected] 509b1d43497Smatthias.ringwald 5108158c421Smatthias.ringwald // MARK: L2CAP_RUN 5112cd0be45Smatthias.ringwald // process outstanding signaling tasks 5127f02f414SMatthias Ringwald static void l2cap_run(void){ 5132b83fb7dSmatthias.ringwald 51422c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 51522c29ab4SMatthias Ringwald 5162b83fb7dSmatthias.ringwald // check pending signaling responses 5172b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5182b83fb7dSmatthias.ringwald 5192b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 520a35252c8S[email protected] 521a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 522a35252c8S[email protected] 5232b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5242b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 52563a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 526f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5272b83fb7dSmatthias.ringwald 528f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 529f53da564S[email protected] signaling_responses_pending--; 530f53da564S[email protected] int i; 531f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 532f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 533f53da564S[email protected] } 534f53da564S[email protected] 535f53da564S[email protected] switch (response_code){ 5362b360848Smatthias.ringwald case CONNECTION_REQUEST: 5372b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5382bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5394d816277S[email protected] if (result == 0x0003){ 5402bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5414d816277S[email protected] } 5422b360848Smatthias.ringwald break; 5432b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5442b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5452b83fb7dSmatthias.ringwald break; 5462b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5473b0484b3S[email protected] switch (infoType){ 5483b0484b3S[email protected] case 1: { // Connectionless MTU 5493b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5503b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5513b0484b3S[email protected] break; 5523b0484b3S[email protected] } 5533b0484b3S[email protected] case 2: { // Extended Features Supported 554462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 555462e630dS[email protected] uint32_t features = 0x280; 5563b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5573b0484b3S[email protected] break; 5583b0484b3S[email protected] } 5593b0484b3S[email protected] case 3: { // Fixed Channels Supported 5603b0484b3S[email protected] uint8_t map[8]; 5613b0484b3S[email protected] memset(map, 0, 8); 562462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5633b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5643b0484b3S[email protected] break; 5653b0484b3S[email protected] } 5663b0484b3S[email protected] default: 5672b83fb7dSmatthias.ringwald // all other types are not supported 5682b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5693b0484b3S[email protected] break; 5702b83fb7dSmatthias.ringwald } 5712b83fb7dSmatthias.ringwald break; 57263a7246aSmatthias.ringwald case COMMAND_REJECT: 5735ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 57470efece1S[email protected] #ifdef HAVE_BLE 57570efece1S[email protected] case COMMAND_REJECT_LE: 57670efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 57763a7246aSmatthias.ringwald break; 57870efece1S[email protected] #endif 5792b83fb7dSmatthias.ringwald default: 5802b83fb7dSmatthias.ringwald // should not happen 5812b83fb7dSmatthias.ringwald break; 5822b83fb7dSmatthias.ringwald } 5832b83fb7dSmatthias.ringwald } 5842b83fb7dSmatthias.ringwald 585ae280e73Smatthias.ringwald uint8_t config_options[4]; 586c22aecc9S[email protected] linked_list_iterator_t it; 587c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 588c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 589baf94f06S[email protected] 590c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 59122c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 5922cd0be45Smatthias.ringwald switch (channel->state){ 5932cd0be45Smatthias.ringwald 594df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 595ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 596baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 597a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 598ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 599a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 600ad671560S[email protected] } 601ad671560S[email protected] break; 602ad671560S[email protected] 60302b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 604baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 60564472d52Smatthias.ringwald // send connection request - set state first 60664472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 60702b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6088f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 60902b22dc4Smatthias.ringwald break; 61002b22dc4Smatthias.ringwald 611e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 612baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 61322c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 6141eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 615e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6169dcb2fb2S[email protected] l2cap_stop_rtx(channel); 617c22aecc9S[email protected] linked_list_iterator_remove(&it); 618d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 619e7ff783cSmatthias.ringwald break; 620e7ff783cSmatthias.ringwald 621552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 622baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 623fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 62428ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 6252a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 626552d92a1Smatthias.ringwald break; 627552d92a1Smatthias.ringwald 6286fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 629baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 6306fdcc387Smatthias.ringwald // success, start l2cap handshake 631b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6326fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 6332a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6345932bd7cS[email protected] l2cap_start_rtx(channel); 6356fdcc387Smatthias.ringwald break; 6366fdcc387Smatthias.ringwald 637fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 638baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 63973cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 64063a7246aSmatthias.ringwald uint16_t flags = 0; 64128ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 64263a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 64363a7246aSmatthias.ringwald flags = 1; 64463a7246aSmatthias.ringwald } else { 64528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 64663a7246aSmatthias.ringwald } 64763a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 64863a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 64963a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 65063a7246aSmatthias.ringwald config_options[0] = 1; // MTU 65163a7246aSmatthias.ringwald config_options[1] = 2; // len param 65263a7246aSmatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 65363a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 65463a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 65563a7246aSmatthias.ringwald } else { 65663a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 65763a7246aSmatthias.ringwald } 65863a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 659fa8473a4Smatthias.ringwald } 66073cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 66128ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 66228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 663b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 664ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 665ae280e73Smatthias.ringwald config_options[1] = 2; // len param 666ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 667b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6685932bd7cS[email protected] l2cap_start_rtx(channel); 669fa8473a4Smatthias.ringwald } 670fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 671552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 672552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 673552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 674fa8473a4Smatthias.ringwald } 675552d92a1Smatthias.ringwald break; 676552d92a1Smatthias.ringwald 677e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 678baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 67922c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 680b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6815932bd7cS[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 :) 682756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 683e7ff783cSmatthias.ringwald break; 684e7ff783cSmatthias.ringwald 685e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 686baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 687b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6882cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 6892a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 6902cd0be45Smatthias.ringwald break; 6912cd0be45Smatthias.ringwald default: 6922cd0be45Smatthias.ringwald break; 6932cd0be45Smatthias.ringwald } 6942cd0be45Smatthias.ringwald } 695da886c03S[email protected] 6964d7157c3S[email protected] #ifdef HAVE_BLE 697da886c03S[email protected] // send l2cap con paramter update if necessary 698da886c03S[email protected] hci_connections_get_iterator(&it); 699da886c03S[email protected] while(linked_list_iterator_has_next(&it)){ 700da886c03S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it); 7015d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 702b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 703da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 704b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 705b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 706b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 707b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 708b68d7bc3SMatthias Ringwald break; 709da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 710b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 711b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 712da886c03S[email protected] break; 713da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 714b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 715b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 716da886c03S[email protected] break; 717da886c03S[email protected] default: 718da886c03S[email protected] break; 719da886c03S[email protected] } 720da886c03S[email protected] } 7214d7157c3S[email protected] #endif 722da886c03S[email protected] 72322c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 7242cd0be45Smatthias.ringwald } 7252cd0be45Smatthias.ringwald 7264aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 7274ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 728fa8c92f6Smatthias.ringwald } 729fa8c92f6Smatthias.ringwald 73071de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 7314ff786cfS[email protected] return l2cap_max_mtu(); 732e5e1518dS[email protected] } 733e5e1518dS[email protected] 7342df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 7352df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 7365533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 7372df5dadcS[email protected] // success, start l2cap handshake 7382df5dadcS[email protected] channel->handle = handle; 7392df5dadcS[email protected] // check remote SSP feature first 7402df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 7412df5dadcS[email protected] } 7422df5dadcS[email protected] } 7432df5dadcS[email protected] 7442df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 7452df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 7462df5dadcS[email protected] 7472df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 748ac301f95S[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)); 7492df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 7502df5dadcS[email protected] // request security level 2 7512df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 7521429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 7532df5dadcS[email protected] return; 7542df5dadcS[email protected] } 7552df5dadcS[email protected] // fine, go ahead 7562df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7572df5dadcS[email protected] } 7582df5dadcS[email protected] 7599077cb15SMatthias Ringwald /** 7609077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 7619077cb15SMatthias Ringwald * @param packet_handler 7629077cb15SMatthias Ringwald * @param address 7639077cb15SMatthias Ringwald * @param psm 7649077cb15SMatthias Ringwald * @param mtu 7659077cb15SMatthias Ringwald * @param local_cid 7669077cb15SMatthias Ringwald */ 7679077cb15SMatthias 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){ 7689077cb15SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 7699077cb15SMatthias Ringwald 7709077cb15SMatthias Ringwald // alloc structure 7719077cb15SMatthias Ringwald l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 7729077cb15SMatthias Ringwald if (!chan) { 7739077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 7749077cb15SMatthias Ringwald } 7759077cb15SMatthias Ringwald 7769077cb15SMatthias Ringwald // Init memory (make valgrind happy) 7779077cb15SMatthias Ringwald memset(chan, 0, sizeof(l2cap_channel_t)); 7789077cb15SMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 7799077cb15SMatthias Ringwald if (mtu > l2cap_max_mtu()) { 7809077cb15SMatthias Ringwald mtu = l2cap_max_mtu(); 7819077cb15SMatthias Ringwald } 7829077cb15SMatthias Ringwald 7839077cb15SMatthias Ringwald // fill in 7849077cb15SMatthias Ringwald BD_ADDR_COPY(chan->address, address); 7859077cb15SMatthias Ringwald chan->psm = psm; 7869077cb15SMatthias Ringwald chan->handle = 0; 7879077cb15SMatthias Ringwald chan->packet_handler = channel_packet_handler; 7889077cb15SMatthias Ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 7899077cb15SMatthias Ringwald chan->local_mtu = mtu; 7909077cb15SMatthias Ringwald chan->packets_granted = 0; 7919077cb15SMatthias Ringwald chan->local_cid = l2cap_next_local_cid(); 7929077cb15SMatthias Ringwald 7939077cb15SMatthias Ringwald // set initial state 7949077cb15SMatthias Ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 7959077cb15SMatthias Ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 7969077cb15SMatthias Ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 7979077cb15SMatthias Ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 7989077cb15SMatthias Ringwald chan->required_security_level = LEVEL_0; 7999077cb15SMatthias Ringwald 8009077cb15SMatthias Ringwald // add to connections list 8019077cb15SMatthias Ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 8029077cb15SMatthias Ringwald 8039077cb15SMatthias Ringwald // store local_cid 8049077cb15SMatthias Ringwald if (out_local_cid){ 8059077cb15SMatthias Ringwald *out_local_cid = chan->local_cid; 8069077cb15SMatthias Ringwald } 8079077cb15SMatthias Ringwald 8089077cb15SMatthias Ringwald // check if hci connection is already usable 8099077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 8109077cb15SMatthias Ringwald if (conn){ 8119077cb15SMatthias Ringwald log_info("l2cap_create_channel_internal, hci connection already exists"); 8129077cb15SMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, chan); 8139077cb15SMatthias Ringwald // check if remote supported fearures are already received 8149077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 8159077cb15SMatthias Ringwald l2cap_handle_remote_supported_features_received(chan); 8169077cb15SMatthias Ringwald } 8179077cb15SMatthias Ringwald } 8189077cb15SMatthias Ringwald 8199077cb15SMatthias Ringwald l2cap_run(); 8209077cb15SMatthias Ringwald 8219077cb15SMatthias Ringwald return 0; 8229077cb15SMatthias Ringwald } 8239077cb15SMatthias Ringwald 824b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 825e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 826b35f641cSmatthias.ringwald // find channel for local_cid 827b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 828f62db1e3Smatthias.ringwald if (channel) { 829e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 830f62db1e3Smatthias.ringwald } 8312cd0be45Smatthias.ringwald // process 8322cd0be45Smatthias.ringwald l2cap_run(); 83343625864Smatthias.ringwald } 8341e6aba47Smatthias.ringwald 835afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 836c22aecc9S[email protected] linked_list_iterator_t it; 837c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 838c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 839c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 840c22aecc9S[email protected] if ( BD_ADDR_CMP( channel->address, address) != 0) continue; 841c22aecc9S[email protected] // channel for this address found 842c22aecc9S[email protected] switch (channel->state){ 843c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 844c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 845afde0c52Smatthias.ringwald // failure, forward error code 846afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 847afde0c52Smatthias.ringwald // discard channel 8489dcb2fb2S[email protected] l2cap_stop_rtx(channel); 849c22aecc9S[email protected] linked_list_iterator_remove(&it); 850d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 851c22aecc9S[email protected] break; 852c22aecc9S[email protected] default: 853c22aecc9S[email protected] break; 854afde0c52Smatthias.ringwald } 855afde0c52Smatthias.ringwald } 856afde0c52Smatthias.ringwald } 857afde0c52Smatthias.ringwald 858afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 859c22aecc9S[email protected] linked_list_iterator_t it; 860c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 861c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 862c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 863afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 8642df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 865afde0c52Smatthias.ringwald } 866afde0c52Smatthias.ringwald } 8676fdcc387Smatthias.ringwald // process 8686fdcc387Smatthias.ringwald l2cap_run(); 869afde0c52Smatthias.ringwald } 870b448a0e7Smatthias.ringwald 8717f02f414SMatthias Ringwald static void l2cap_event_handler(uint8_t *packet, uint16_t size){ 872afde0c52Smatthias.ringwald 873afde0c52Smatthias.ringwald bd_addr_t address; 874afde0c52Smatthias.ringwald hci_con_handle_t handle; 875c22aecc9S[email protected] linked_list_iterator_t it; 8762d00edd4Smatthias.ringwald int hci_con_used; 877afde0c52Smatthias.ringwald 878afde0c52Smatthias.ringwald switch(packet[0]){ 879afde0c52Smatthias.ringwald 880afde0c52Smatthias.ringwald // handle connection complete events 881afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 882afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 883afde0c52Smatthias.ringwald if (packet[2] == 0){ 884afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 885afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 886afde0c52Smatthias.ringwald } else { 887afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 888afde0c52Smatthias.ringwald } 889afde0c52Smatthias.ringwald break; 890afde0c52Smatthias.ringwald 891afde0c52Smatthias.ringwald // handle successful create connection cancel command 892afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 893afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 894afde0c52Smatthias.ringwald if (packet[5] == 0){ 895afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 896afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 897afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 89803cfbabcSmatthias.ringwald } 8991e6aba47Smatthias.ringwald } 90039d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 90139d59809Smatthias.ringwald break; 90239d59809Smatthias.ringwald 90339d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 90439d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 905afde0c52Smatthias.ringwald break; 90627a923d0Smatthias.ringwald 9071e6aba47Smatthias.ringwald // handle disconnection complete events 908afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 909c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 910afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 911c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 912c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 913c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 914c22aecc9S[email protected] if (channel->handle != handle) continue; 91515ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 9169dcb2fb2S[email protected] l2cap_stop_rtx(channel); 917c22aecc9S[email protected] linked_list_iterator_remove(&it); 918d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 91927a923d0Smatthias.ringwald } 920afde0c52Smatthias.ringwald break; 921fcadd0caSmatthias.ringwald 9226218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 92302b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 9248d371091Smatthias.ringwald l2cap_hand_out_credits(); 9256218e6f1Smatthias.ringwald break; 9266218e6f1Smatthias.ringwald 927ee091cf1Smatthias.ringwald // HCI Connection Timeouts 928afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 92936944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 930bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 93180ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 9322d00edd4Smatthias.ringwald hci_con_used = 0; 933c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 934c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 935c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 936c22aecc9S[email protected] if (channel->handle != handle) continue; 9372d00edd4Smatthias.ringwald hci_con_used = 1; 938c22aecc9S[email protected] break; 939ee091cf1Smatthias.ringwald } 9402d00edd4Smatthias.ringwald if (hci_con_used) break; 941d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 9429edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 943afde0c52Smatthias.ringwald break; 944ee091cf1Smatthias.ringwald 9456e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 946c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 947c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 948c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 949c22aecc9S[email protected] if (!channel->packet_handler) continue; 9506e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 9516e6710ebSmatthias.ringwald } 9525652b5ffS[email protected] if (attribute_protocol_packet_handler) { 9535652b5ffS[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9545652b5ffS[email protected] } 9555652b5ffS[email protected] if (security_protocol_packet_handler) { 956133efcfdSmatthias.ringwald (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9575652b5ffS[email protected] } 958462e630dS[email protected] if (connectionless_channel_packet_handler) { 959462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 960462e630dS[email protected] } 9616e6710ebSmatthias.ringwald break; 9626e6710ebSmatthias.ringwald 963df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 964df3354fcS[email protected] handle = READ_BT_16(packet, 3); 965c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 966c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 967c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 968df3354fcS[email protected] if (channel->handle != handle) continue; 9692df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 970df3354fcS[email protected] break; 971df3354fcS[email protected] } 972c22aecc9S[email protected] break; 973df3354fcS[email protected] 974a00031e2S[email protected] case GAP_SECURITY_LEVEL: 975a00031e2S[email protected] handle = READ_BT_16(packet, 2); 976bd63148eS[email protected] log_info("l2cap - security level update"); 977c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 978c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 979c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 980f85a9399S[email protected] if (channel->handle != handle) continue; 9815533f01eS[email protected] 982bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 983bd63148eS[email protected] 984e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 9855533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 9865533f01eS[email protected] 987df3354fcS[email protected] switch (channel->state){ 988df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 9895533f01eS[email protected] if (actual_level >= required_level){ 990f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 991f85a9399S[email protected] l2cap_emit_connection_request(channel); 9921eb2563eS[email protected] } else { 993775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 9941eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 9951eb2563eS[email protected] } 996df3354fcS[email protected] break; 997df3354fcS[email protected] 998df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 9995533f01eS[email protected] if (actual_level >= required_level){ 1000df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1001df3354fcS[email protected] } else { 1002df3354fcS[email protected] // disconnnect, authentication not good enough 1003df3354fcS[email protected] hci_disconnect_security_block(handle); 1004df3354fcS[email protected] } 1005df3354fcS[email protected] break; 1006df3354fcS[email protected] 1007df3354fcS[email protected] default: 1008df3354fcS[email protected] break; 1009df3354fcS[email protected] } 1010f85a9399S[email protected] } 1011f85a9399S[email protected] break; 1012f85a9399S[email protected] 1013afde0c52Smatthias.ringwald default: 1014afde0c52Smatthias.ringwald break; 1015afde0c52Smatthias.ringwald } 1016afde0c52Smatthias.ringwald 1017e0707417S[email protected] // pass on: main packet handler, att and sm packet handlers 1018b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 1019e0707417S[email protected] if (attribute_protocol_packet_handler){ 1020e0707417S[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1021e0707417S[email protected] } 1022e0707417S[email protected] if (security_protocol_packet_handler) { 1023e0707417S[email protected] (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1024e0707417S[email protected] } 1025462e630dS[email protected] if (connectionless_channel_packet_handler) { 1026462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1027462e630dS[email protected] } 1028bd63148eS[email protected] 1029bd63148eS[email protected] l2cap_run(); 10301e6aba47Smatthias.ringwald } 10311e6aba47Smatthias.ringwald 1032afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1033b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1034e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1035e7ff783cSmatthias.ringwald l2cap_run(); 103684836b65Smatthias.ringwald } 103784836b65Smatthias.ringwald 10382b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 10394cf56b4aSmatthias.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." 10402b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 10412b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 10422b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 10432b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 10442b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 10452b360848Smatthias.ringwald signaling_responses_pending++; 10462b360848Smatthias.ringwald l2cap_run(); 10472b360848Smatthias.ringwald } 10482b360848Smatthias.ringwald } 10492b360848Smatthias.ringwald 1050b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1051645658c9Smatthias.ringwald 10529da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1053645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1054645658c9Smatthias.ringwald if (!service) { 1055645658c9Smatthias.ringwald // 0x0002 PSM not supported 10562b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1057645658c9Smatthias.ringwald return; 1058645658c9Smatthias.ringwald } 1059645658c9Smatthias.ringwald 10605061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1061645658c9Smatthias.ringwald if (!hci_connection) { 10622b360848Smatthias.ringwald // 10639da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1064645658c9Smatthias.ringwald return; 1065645658c9Smatthias.ringwald } 10662bd8b7e7S[email protected] 1067645658c9Smatthias.ringwald // alloc structure 10689da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1069bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 10702b360848Smatthias.ringwald if (!channel){ 10712b360848Smatthias.ringwald // 0x0004 No resources available 10722b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 10732b360848Smatthias.ringwald return; 10742b360848Smatthias.ringwald } 1075c523d53dS[email protected] // Init memory (make valgrind happy) 1076c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1077645658c9Smatthias.ringwald // fill in 1078169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 1079169f8b28Smatthias.ringwald channel->psm = psm; 1080169f8b28Smatthias.ringwald channel->handle = handle; 1081169f8b28Smatthias.ringwald channel->connection = service->connection; 1082f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1083b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1084b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1085fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 10860a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1087761b0451Smatthias.ringwald channel->packets_granted = 0; 1088b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1089df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1090645658c9Smatthias.ringwald 1091f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 10922985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 10932985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 10949775e25bSmatthias.ringwald } 10959775e25bSmatthias.ringwald 1096645658c9Smatthias.ringwald // set initial state 1097df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1098ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1099e405ae81Smatthias.ringwald 1100645658c9Smatthias.ringwald // add to connections list 1101169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 1102645658c9Smatthias.ringwald 1103f85a9399S[email protected] // assert security requirements 11041eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1105e405ae81Smatthias.ringwald } 1106645658c9Smatthias.ringwald 1107b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 1108e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1109b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1110e405ae81Smatthias.ringwald if (!channel) { 11117d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 1112e405ae81Smatthias.ringwald return; 1113e405ae81Smatthias.ringwald } 1114e405ae81Smatthias.ringwald 1115552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1116e405ae81Smatthias.ringwald 1117552d92a1Smatthias.ringwald // process 1118552d92a1Smatthias.ringwald l2cap_run(); 1119e405ae81Smatthias.ringwald } 1120645658c9Smatthias.ringwald 1121b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 1122e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1123b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1124e405ae81Smatthias.ringwald if (!channel) { 11257d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 1126e405ae81Smatthias.ringwald return; 1127e405ae81Smatthias.ringwald } 1128e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1129e7ff783cSmatthias.ringwald channel->reason = reason; 1130e7ff783cSmatthias.ringwald l2cap_run(); 1131645658c9Smatthias.ringwald } 1132645658c9Smatthias.ringwald 11337f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1134b1988dceSmatthias.ringwald 1135b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1136b1988dceSmatthias.ringwald 113763a7246aSmatthias.ringwald uint16_t flags = READ_BT_16(command, 6); 113863a7246aSmatthias.ringwald if (flags & 1) { 113963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 114063a7246aSmatthias.ringwald } 114163a7246aSmatthias.ringwald 11422784b77dSmatthias.ringwald // accept the other's configuration options 11433de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 11443de7c0caSmatthias.ringwald uint16_t pos = 8; 11453de7c0caSmatthias.ringwald while (pos < end_pos){ 114663a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 114763a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 114863a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 114963a7246aSmatthias.ringwald pos++; 11501dc511deSmatthias.ringwald uint8_t length = command[pos++]; 11511dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 115263a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 11531dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 11549da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 115563a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 115663a7246aSmatthias.ringwald } 11570fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 11580fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 11590fe7a9d0S[email protected] channel->flush_timeout = READ_BT_16(command, pos); 11600fe7a9d0S[email protected] } 116163a7246aSmatthias.ringwald // check for unknown options 116263a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1163c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 116463a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 11651dc511deSmatthias.ringwald } 11661dc511deSmatthias.ringwald pos += length; 11671dc511deSmatthias.ringwald } 11682784b77dSmatthias.ringwald } 11692784b77dSmatthias.ringwald 1170fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 11719da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 117273cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 117373cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1174019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1175019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1176fa8473a4Smatthias.ringwald return 1; 1177fa8473a4Smatthias.ringwald } 1178fa8473a4Smatthias.ringwald 1179fa8473a4Smatthias.ringwald 11807f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 11811e6aba47Smatthias.ringwald 118200d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 118300d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 118438e5900eSmatthias.ringwald uint16_t result = 0; 11851e6aba47Smatthias.ringwald 11869da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1187b35f641cSmatthias.ringwald 11889a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 11899a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 11909a011532Smatthias.ringwald switch (channel->state){ 1191fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 11929a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 11932b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 11949a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 11959a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 11969a011532Smatthias.ringwald break; 11979a011532Smatthias.ringwald 11989a011532Smatthias.ringwald default: 11999a011532Smatthias.ringwald // ignore in other states 12009a011532Smatthias.ringwald break; 12019a011532Smatthias.ringwald } 12029a011532Smatthias.ringwald return; 12039a011532Smatthias.ringwald } 12049a011532Smatthias.ringwald 120556081214Smatthias.ringwald // @STATEMACHINE(l2cap) 12061e6aba47Smatthias.ringwald switch (channel->state) { 12071e6aba47Smatthias.ringwald 12081e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 12091e6aba47Smatthias.ringwald switch (code){ 12101e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 12115932bd7cS[email protected] l2cap_stop_rtx(channel); 121200d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 121338e5900eSmatthias.ringwald switch (result) { 121438e5900eSmatthias.ringwald case 0: 1215169f8b28Smatthias.ringwald // successful connection 121600d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1217fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 121828ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 121938e5900eSmatthias.ringwald break; 122038e5900eSmatthias.ringwald case 1: 12215932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 12225932bd7cS[email protected] l2cap_start_ertx(channel); 122338e5900eSmatthias.ringwald break; 122438e5900eSmatthias.ringwald default: 1225eb920dbeSmatthias.ringwald // channel closed 1226eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1227f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 122838e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1229eb920dbeSmatthias.ringwald 1230eb920dbeSmatthias.ringwald // drop link key if security block 1231eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 12322e77e513S[email protected] hci_drop_link_key_for_bd_addr(channel->address); 1233eb920dbeSmatthias.ringwald } 1234eb920dbeSmatthias.ringwald 1235eb920dbeSmatthias.ringwald // discard channel 1236eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1237d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 123838e5900eSmatthias.ringwald break; 12391e6aba47Smatthias.ringwald } 12401e6aba47Smatthias.ringwald break; 124138e5900eSmatthias.ringwald 124238e5900eSmatthias.ringwald default: 12431e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 124438e5900eSmatthias.ringwald break; 12451e6aba47Smatthias.ringwald } 12461e6aba47Smatthias.ringwald break; 12471e6aba47Smatthias.ringwald 1248fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1249fe9d8984S[email protected] result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1250ae280e73Smatthias.ringwald switch (code) { 1251ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 125228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1253ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 125463a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 125563a7246aSmatthias.ringwald // only done if continuation not set 125663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 125763a7246aSmatthias.ringwald } 1258ae280e73Smatthias.ringwald break; 12591e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 12605932bd7cS[email protected] l2cap_stop_rtx(channel); 12615932bd7cS[email protected] switch (result){ 12625932bd7cS[email protected] case 0: // success 12635932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 12645932bd7cS[email protected] break; 12655932bd7cS[email protected] case 4: // pending 12665932bd7cS[email protected] l2cap_start_ertx(channel); 12675932bd7cS[email protected] break; 12685932bd7cS[email protected] default: 1269fe9d8984S[email protected] // retry on negative result 1270fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1271fe9d8984S[email protected] break; 1272fe9d8984S[email protected] } 12735a67bd4aSmatthias.ringwald break; 12745a67bd4aSmatthias.ringwald default: 12755a67bd4aSmatthias.ringwald break; 12761e6aba47Smatthias.ringwald } 1277fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1278fa8473a4Smatthias.ringwald // for open: 12795a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1280fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 12816218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 1282c8e4258aSmatthias.ringwald } 1283c8e4258aSmatthias.ringwald break; 1284f62db1e3Smatthias.ringwald 1285f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1286f62db1e3Smatthias.ringwald switch (code) { 1287f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 128827a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 128927a923d0Smatthias.ringwald break; 12905a67bd4aSmatthias.ringwald default: 12915a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 12925a67bd4aSmatthias.ringwald break; 129327a923d0Smatthias.ringwald } 129427a923d0Smatthias.ringwald break; 129584836b65Smatthias.ringwald 129684836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 129784836b65Smatthias.ringwald // @TODO handle incoming requests 129884836b65Smatthias.ringwald break; 129984836b65Smatthias.ringwald 130084836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 130184836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 130284836b65Smatthias.ringwald break; 130310642e45Smatthias.ringwald default: 130410642e45Smatthias.ringwald break; 130527a923d0Smatthias.ringwald } 13069da54300S[email protected] // log_info("new state %u", channel->state); 130727a923d0Smatthias.ringwald } 130827a923d0Smatthias.ringwald 130900d93d79Smatthias.ringwald 13107f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 131100d93d79Smatthias.ringwald 131200d93d79Smatthias.ringwald // get code, signalind identifier and command len 131300d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 131400d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 131500d93d79Smatthias.ringwald 131600d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 131700d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 131863a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 131900d93d79Smatthias.ringwald return; 132000d93d79Smatthias.ringwald } 132100d93d79Smatthias.ringwald 132200d93d79Smatthias.ringwald // general commands without an assigned channel 132300d93d79Smatthias.ringwald switch(code) { 132400d93d79Smatthias.ringwald 132500d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 132600d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 132700d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 132800d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 13292b83fb7dSmatthias.ringwald return; 133000d93d79Smatthias.ringwald } 133100d93d79Smatthias.ringwald 13322b360848Smatthias.ringwald case ECHO_REQUEST: 13332b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 13342b83fb7dSmatthias.ringwald return; 133500d93d79Smatthias.ringwald 133600d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 133700d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 13382b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 13392b83fb7dSmatthias.ringwald return; 134000d93d79Smatthias.ringwald } 134100d93d79Smatthias.ringwald 134200d93d79Smatthias.ringwald default: 134300d93d79Smatthias.ringwald break; 134400d93d79Smatthias.ringwald } 134500d93d79Smatthias.ringwald 134600d93d79Smatthias.ringwald 134700d93d79Smatthias.ringwald // Get potential destination CID 134800d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 134900d93d79Smatthias.ringwald 135000d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1351c22aecc9S[email protected] linked_list_iterator_t it; 1352c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1353c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1354c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1355c22aecc9S[email protected] if (channel->handle != handle) continue; 135600d93d79Smatthias.ringwald if (code & 1) { 1357b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1358b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 135900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13604e32727eSmatthias.ringwald break; 136100d93d79Smatthias.ringwald } 136200d93d79Smatthias.ringwald } else { 1363b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 136400d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 136500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13664e32727eSmatthias.ringwald break; 136700d93d79Smatthias.ringwald } 136800d93d79Smatthias.ringwald } 136900d93d79Smatthias.ringwald } 137000d93d79Smatthias.ringwald } 137100d93d79Smatthias.ringwald 13727f02f414SMatthias Ringwald static void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 137300d93d79Smatthias.ringwald 137400d93d79Smatthias.ringwald // Get Channel ID 137500d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 137600d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 137700d93d79Smatthias.ringwald 13785652b5ffS[email protected] switch (channel_id) { 13795652b5ffS[email protected] 13805652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 13815652b5ffS[email protected] 138200d93d79Smatthias.ringwald uint16_t command_offset = 8; 138300d93d79Smatthias.ringwald while (command_offset < size) { 138400d93d79Smatthias.ringwald 138500d93d79Smatthias.ringwald // handle signaling commands 138600d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 138700d93d79Smatthias.ringwald 138800d93d79Smatthias.ringwald // increment command_offset 138900d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 139000d93d79Smatthias.ringwald } 13915652b5ffS[email protected] break; 139200d93d79Smatthias.ringwald } 139300d93d79Smatthias.ringwald 13945652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 13955652b5ffS[email protected] if (attribute_protocol_packet_handler) { 13965652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13975652b5ffS[email protected] } 13985652b5ffS[email protected] break; 13995652b5ffS[email protected] 14005652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 14015652b5ffS[email protected] if (security_protocol_packet_handler) { 14025652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14035652b5ffS[email protected] } 14045652b5ffS[email protected] break; 14055652b5ffS[email protected] 1406462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1407462e630dS[email protected] if (connectionless_channel_packet_handler) { 1408462e630dS[email protected] (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1409462e630dS[email protected] } 1410462e630dS[email protected] break; 1411462e630dS[email protected] 14121bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1413ccf076adS[email protected] switch (packet[8]){ 1414ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1415ccf076adS[email protected] uint16_t result = READ_BT_16(packet, 12); 1416ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1417ccf076adS[email protected] break; 1418ccf076adS[email protected] } 1419ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1420ccf076adS[email protected] uint8_t event[10]; 1421ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1422ccf076adS[email protected] event[1] = 8; 1423ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1424da886c03S[email protected] 1425da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1426da886c03S[email protected] if (connection){ 14276d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 14286d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 14296d91fb6cSMatthias Ringwald uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14306d91fb6cSMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14316d91fb6cSMatthias Ringwald break; 14326d91fb6cSMatthias Ringwald } 1433da886c03S[email protected] int update_parameter = 1; 1434a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 1435a4c06b28SMatthias Ringwald gap_le_get_connection_parameter_range(existing_range); 1436da886c03S[email protected] uint16_t le_conn_interval_min = READ_BT_16(packet,12); 1437da886c03S[email protected] uint16_t le_conn_interval_max = READ_BT_16(packet,14); 1438da886c03S[email protected] uint16_t le_conn_latency = READ_BT_16(packet,16); 1439da886c03S[email protected] uint16_t le_supervision_timeout = READ_BT_16(packet,18); 1440da886c03S[email protected] 1441da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1442da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1443da886c03S[email protected] 1444da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1445da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1446da886c03S[email protected] 1447da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1448da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1449da886c03S[email protected] 1450da886c03S[email protected] if (update_parameter){ 1451da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1452da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1453da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1454da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1455da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1456da886c03S[email protected] } else { 1457da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1458da886c03S[email protected] } 145979f53f1dSMatthias Ringwald connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1460da886c03S[email protected] } 1461da886c03S[email protected] 1462ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1463ccf076adS[email protected] (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1464da886c03S[email protected] 1465ccf076adS[email protected] break; 1466ccf076adS[email protected] } 1467ccf076adS[email protected] default: { 14681bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14691bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14701bbc0b23S[email protected] break; 14711bbc0b23S[email protected] } 1472ccf076adS[email protected] } 1473ccf076adS[email protected] break; 1474ccf076adS[email protected] } 14751bbc0b23S[email protected] 14765652b5ffS[email protected] default: { 147700d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 147800d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 147900d93d79Smatthias.ringwald if (channel) { 148058de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 148100d93d79Smatthias.ringwald } 14825652b5ffS[email protected] break; 14835652b5ffS[email protected] } 14845652b5ffS[email protected] } 148500d93d79Smatthias.ringwald } 148600d93d79Smatthias.ringwald 14872718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14882718e2e7Smatthias.ringwald switch (packet_type) { 14892718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 14902718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 14912718e2e7Smatthias.ringwald break; 14922718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 14932718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 14942718e2e7Smatthias.ringwald break; 14952718e2e7Smatthias.ringwald default: 14962718e2e7Smatthias.ringwald break; 14972718e2e7Smatthias.ringwald } 14981eb2563eS[email protected] l2cap_run(); 14992718e2e7Smatthias.ringwald } 150000d93d79Smatthias.ringwald 150115ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 150227a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1503f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1504f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1505f62db1e3Smatthias.ringwald // discard channel 15069dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1507f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1508d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1509c8e4258aSmatthias.ringwald } 15101e6aba47Smatthias.ringwald 15117192e786SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(linked_list_t * services, uint16_t psm){ 1512c22aecc9S[email protected] linked_list_iterator_t it; 15137192e786SMatthias Ringwald linked_list_iterator_init(&it, services); 1514c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1515c22aecc9S[email protected] l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it); 15169d9bbc01Smatthias.ringwald if ( service->psm == psm){ 15179d9bbc01Smatthias.ringwald return service; 15189d9bbc01Smatthias.ringwald }; 15199d9bbc01Smatthias.ringwald } 15209d9bbc01Smatthias.ringwald return NULL; 15219d9bbc01Smatthias.ringwald } 15229d9bbc01Smatthias.ringwald 15237192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 15247192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 15257192e786SMatthias Ringwald } 15267192e786SMatthias Ringwald 1527e0abb8e7S[email protected] 1528*be2053a6SMatthias 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){ 1529*be2053a6SMatthias Ringwald 1530*be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1531e0abb8e7S[email protected] 15324bb582b6Smatthias.ringwald // check for alread registered psm 15334bb582b6Smatthias.ringwald // TODO: emit error event 15349d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1535277abc2cSmatthias.ringwald if (service) { 1536*be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1537*be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1538277abc2cSmatthias.ringwald } 15399d9bbc01Smatthias.ringwald 15404bb582b6Smatthias.ringwald // alloc structure 15414bb582b6Smatthias.ringwald // TODO: emit error event 1542bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1543277abc2cSmatthias.ringwald if (!service) { 1544*be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1545*be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1546277abc2cSmatthias.ringwald } 15479d9bbc01Smatthias.ringwald 15489d9bbc01Smatthias.ringwald // fill in 15499d9bbc01Smatthias.ringwald service->psm = psm; 15509d9bbc01Smatthias.ringwald service->mtu = mtu; 155105ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1552df3354fcS[email protected] service->required_security_level = security_level; 15539d9bbc01Smatthias.ringwald 15549d9bbc01Smatthias.ringwald // add to services list 15559d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 1556c0e866bfSmatthias.ringwald 1557c0e866bfSmatthias.ringwald // enable page scan 1558c0e866bfSmatthias.ringwald hci_connectable_control(1); 15595842b6d9Smatthias.ringwald 1560*be2053a6SMatthias Ringwald return 0; 15619d9bbc01Smatthias.ringwald } 15629d9bbc01Smatthias.ringwald 156336944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 1564e0abb8e7S[email protected] 1565e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1566e0abb8e7S[email protected] 15679d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1568037d6e48Smatthias.ringwald if (!service) return; 15699d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 1570d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1571c0e866bfSmatthias.ringwald 1572c0e866bfSmatthias.ringwald // disable page scan when no services registered 1573c0e866bfSmatthias.ringwald if (!linked_list_empty(&l2cap_services)) return; 1574c0e866bfSmatthias.ringwald hci_connectable_control(0); 15759d9bbc01Smatthias.ringwald } 15769d9bbc01Smatthias.ringwald 15775652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 157805ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 15795652b5ffS[email protected] switch(channel_id){ 15805652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 158105ae8de3SMatthias Ringwald attribute_protocol_packet_handler = the_packet_handler; 15825652b5ffS[email protected] break; 15835652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 158405ae8de3SMatthias Ringwald security_protocol_packet_handler = the_packet_handler; 15855652b5ffS[email protected] break; 1586462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 158705ae8de3SMatthias Ringwald connectionless_channel_packet_handler = the_packet_handler; 1588462e630dS[email protected] break; 15895652b5ffS[email protected] } 15905652b5ffS[email protected] } 15915652b5ffS[email protected] 15927192e786SMatthias Ringwald #ifdef HAVE_BLE 15937192e786SMatthias Ringwald 15947f02f414SMatthias Ringwald 15957f02f414SMatthias Ringwald #if 0 15967192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 15977192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, psm); 15987192e786SMatthias Ringwald } 15997192e786SMatthias Ringwald /** 16007192e786SMatthias Ringwald * @brief Regster L2CAP LE Credit Based Flow Control Mode service 16017192e786SMatthias Ringwald * @param 16027192e786SMatthias Ringwald */ 16037192e786SMatthias Ringwald void l2cap_le_register_service_internal(void * connection, btstack_packet_handler_t packet_handler, uint16_t psm, 16047192e786SMatthias Ringwald uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 16057192e786SMatthias Ringwald 16067192e786SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 16077192e786SMatthias Ringwald 16087192e786SMatthias Ringwald // check for alread registered psm 16097192e786SMatthias Ringwald // TODO: emit error event 16107192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16117192e786SMatthias Ringwald if (service) { 16127192e786SMatthias Ringwald log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 16137192e786SMatthias Ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 16147192e786SMatthias Ringwald return; 16157192e786SMatthias Ringwald } 16167192e786SMatthias Ringwald 16177192e786SMatthias Ringwald // alloc structure 16187192e786SMatthias Ringwald // TODO: emit error event 16197192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 16207192e786SMatthias Ringwald if (!service) { 16217192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 16227192e786SMatthias Ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 16237192e786SMatthias Ringwald return; 16247192e786SMatthias Ringwald } 16257192e786SMatthias Ringwald 16267192e786SMatthias Ringwald // fill in 16277192e786SMatthias Ringwald service->psm = psm; 16287192e786SMatthias Ringwald service->mtu = mtu; 16297192e786SMatthias Ringwald service->mps = mps; 16307192e786SMatthias Ringwald service->connection = connection; 16317192e786SMatthias Ringwald service->packet_handler = packet_handler; 16327192e786SMatthias Ringwald service->required_security_level = security_level; 16337192e786SMatthias Ringwald 16347192e786SMatthias Ringwald // add to services list 16357192e786SMatthias Ringwald linked_list_add(&l2cap_le_services, (linked_item_t *) service); 16367192e786SMatthias Ringwald 16377192e786SMatthias Ringwald // done 16387192e786SMatthias Ringwald l2cap_emit_service_registered(connection, 0, psm); 16397192e786SMatthias Ringwald } 16407192e786SMatthias Ringwald 16417192e786SMatthias Ringwald void l2cap_le_unregister_service_internal(void * connection, uint16_t psm) { 16427192e786SMatthias Ringwald 16437192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 16447192e786SMatthias Ringwald 16457192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16467192e786SMatthias Ringwald if (!service) return; 16477192e786SMatthias Ringwald linked_list_remove(&l2cap_le_services, (linked_item_t *) service); 16487192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 16497192e786SMatthias Ringwald } 16507192e786SMatthias Ringwald #endif 16517f02f414SMatthias Ringwald #endif 1652