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] 1925842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ 193e0abb8e7S[email protected] log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); 1945842b6d9Smatthias.ringwald uint8_t event[5]; 1955842b6d9Smatthias.ringwald event[0] = L2CAP_EVENT_SERVICE_REGISTERED; 1965842b6d9Smatthias.ringwald event[1] = sizeof(event) - 2; 1975842b6d9Smatthias.ringwald event[2] = status; 1985842b6d9Smatthias.ringwald bt_store_16(event, 3, psm); 1995842b6d9Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 200e6f51008S[email protected] (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 2015842b6d9Smatthias.ringwald } 2025842b6d9Smatthias.ringwald 2037f02f414SMatthias Ringwald static void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 204e0abb8e7S[email protected] 205e0abb8e7S[email protected] log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits); 2066218e6f1Smatthias.ringwald // track credits 2076218e6f1Smatthias.ringwald channel->packets_granted += credits; 2086218e6f1Smatthias.ringwald 2096218e6f1Smatthias.ringwald uint8_t event[5]; 2106218e6f1Smatthias.ringwald event[0] = L2CAP_EVENT_CREDITS; 2116218e6f1Smatthias.ringwald event[1] = sizeof(event) - 2; 2126218e6f1Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 2136218e6f1Smatthias.ringwald event[4] = credits; 2146218e6f1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 2156218e6f1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 2166218e6f1Smatthias.ringwald } 2176218e6f1Smatthias.ringwald 218808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){ 219808a48abSmatthias.ringwald new_credits_blocked = blocked; 220808a48abSmatthias.ringwald } 221808a48abSmatthias.ringwald 2227f02f414SMatthias Ringwald static void l2cap_hand_out_credits(void){ 223808a48abSmatthias.ringwald 224808a48abSmatthias.ringwald if (new_credits_blocked) return; // we're told not to. used by daemon 225808a48abSmatthias.ringwald 226c22aecc9S[email protected] linked_list_iterator_t it; 227c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 228c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 229c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 2300e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 23177aee70fSMatthias Ringwald if (!hci_number_free_acl_slots_for_handle(channel->handle)) return; 2324c744e21Smatthias.ringwald if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 2338d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 2348d371091Smatthias.ringwald } 2358d371091Smatthias.ringwald } 2368d371091Smatthias.ringwald } 2378d371091Smatthias.ringwald 2387f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 239c22aecc9S[email protected] linked_list_iterator_t it; 240c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 241c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 242c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 243b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 244f62db1e3Smatthias.ringwald return channel; 245f62db1e3Smatthias.ringwald } 246f62db1e3Smatthias.ringwald } 247f62db1e3Smatthias.ringwald return NULL; 248f62db1e3Smatthias.ringwald } 249f62db1e3Smatthias.ringwald 2506b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2516b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2526b1fde37Smatthias.ringwald if (!channel) return 0; 2536b1fde37Smatthias.ringwald if (!channel->packets_granted) return 0; 254a35252c8S[email protected] return hci_can_send_acl_packet_now(channel->handle); 2557856fb31S[email protected] } 2567856fb31S[email protected] 2576cd4da6bS[email protected] // @deprecated 2583cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){ 259a35252c8S[email protected] // TODO provide real handle 2606cd4da6bS[email protected] return l2cap_can_send_fixed_channel_packet_now(0x1234); 2616cd4da6bS[email protected] } 2626cd4da6bS[email protected] 2636cd4da6bS[email protected] int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 2646cd4da6bS[email protected] return hci_can_send_acl_packet_now(handle); 2653cab4fcaS[email protected] } 2663cab4fcaS[email protected] 26796cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 26896cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 26996cbd662Smatthias.ringwald if (channel) { 27096cbd662Smatthias.ringwald return channel->remote_mtu; 27196cbd662Smatthias.ringwald } 27296cbd662Smatthias.ringwald return 0; 27396cbd662Smatthias.ringwald } 27496cbd662Smatthias.ringwald 2755932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 276c22aecc9S[email protected] linked_list_iterator_t it; 277c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 278c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 279c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 2805932bd7cS[email protected] if ( &channel->rtx == ts) { 2815932bd7cS[email protected] return channel; 2825932bd7cS[email protected] } 2835932bd7cS[email protected] } 2845932bd7cS[email protected] return NULL; 2855932bd7cS[email protected] } 2865932bd7cS[email protected] 2875932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){ 2885932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2895932bd7cS[email protected] if (!ts) return; 2905932bd7cS[email protected] 2915932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2925932bd7cS[email protected] 2935932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2945932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2955932bd7cS[email protected] // notify client 2965932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2975932bd7cS[email protected] 2985932bd7cS[email protected] // discard channel 2999dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 3005932bd7cS[email protected] linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 3015932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 3025932bd7cS[email protected] } 3035932bd7cS[email protected] 3045932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 3055932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 3065932bd7cS[email protected] run_loop_remove_timer(&channel->rtx); 3075932bd7cS[email protected] } 3085932bd7cS[email protected] 3095932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 3105932bd7cS[email protected] l2cap_stop_rtx(channel); 311cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 3125932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3135932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 3145932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3155932bd7cS[email protected] } 3165932bd7cS[email protected] 3175932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3185932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3195932bd7cS[email protected] l2cap_stop_rtx(channel); 3205932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3215932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 3225932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3235932bd7cS[email protected] } 3245932bd7cS[email protected] 32571de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 326ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 327ac301f95S[email protected] } 328ac301f95S[email protected] 329df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 330ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 331df3354fcS[email protected] } 3325932bd7cS[email protected] 3337f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 334b1d43497Smatthias.ringwald 335a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3369da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 337b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 338b1d43497Smatthias.ringwald } 339b1d43497Smatthias.ringwald 3409da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3412a373862S[email protected] hci_reserve_packet_buffer(); 342facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 34358de5610Smatthias.ringwald va_list argptr; 34458de5610Smatthias.ringwald va_start(argptr, identifier); 34570efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 34658de5610Smatthias.ringwald va_end(argptr); 3479da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 348826f7347S[email protected] return hci_send_acl_packet_buffer(len); 34958de5610Smatthias.ringwald } 35058de5610Smatthias.ringwald 35170efece1S[email protected] #ifdef HAVE_BLE 3527f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 35370efece1S[email protected] 354a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3559da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 35670efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 35770efece1S[email protected] } 35870efece1S[email protected] 3599da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3602a373862S[email protected] hci_reserve_packet_buffer(); 361facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 36270efece1S[email protected] va_list argptr; 36370efece1S[email protected] va_start(argptr, identifier); 36470efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 36570efece1S[email protected] va_end(argptr); 3669da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 367826f7347S[email protected] return hci_send_acl_packet_buffer(len); 36870efece1S[email protected] } 36970efece1S[email protected] #endif 37070efece1S[email protected] 371b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 372facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 373b1d43497Smatthias.ringwald } 3746218e6f1Smatthias.ringwald 3756b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3766b4af23dS[email protected] return hci_reserve_packet_buffer(); 3776b4af23dS[email protected] } 3786b4af23dS[email protected] 37968a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 38068a0fcf7S[email protected] hci_release_packet_buffer(); 38168a0fcf7S[email protected] } 38268a0fcf7S[email protected] 38368a0fcf7S[email protected] 384b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 385b1d43497Smatthias.ringwald 386c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 387c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 388c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 389c8b9416aS[email protected] } 390c8b9416aS[email protected] 39158de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 392b1d43497Smatthias.ringwald if (!channel) { 3939da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 394b1d43497Smatthias.ringwald return -1; // TODO: define error 3956218e6f1Smatthias.ringwald } 3966218e6f1Smatthias.ringwald 397b1d43497Smatthias.ringwald if (channel->packets_granted == 0){ 3989da54300S[email protected] log_error("l2cap_send_prepared cid 0x%02x, no credits!", local_cid); 399b1d43497Smatthias.ringwald return -1; // TODO: define error 400b1d43497Smatthias.ringwald } 401b1d43497Smatthias.ringwald 402a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 4039da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 404a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 405a35252c8S[email protected] } 406a35252c8S[email protected] 407b1d43497Smatthias.ringwald --channel->packets_granted; 408b1d43497Smatthias.ringwald 4099da54300S[email protected] log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used, credits left %u;", 410b1d43497Smatthias.ringwald local_cid, channel->handle, channel->packets_granted); 411b1d43497Smatthias.ringwald 412facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 413b1d43497Smatthias.ringwald 414e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 415e9772277S[email protected] 416e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 417e9772277S[email protected] bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 41858de5610Smatthias.ringwald // 2 - ACL length 41958de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 42058de5610Smatthias.ringwald // 4 - L2CAP packet length 42158de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 42258de5610Smatthias.ringwald // 6 - L2CAP channel DEST 42358de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 42458de5610Smatthias.ringwald // send 425826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 42691b99603Smatthias.ringwald 42791b99603Smatthias.ringwald l2cap_hand_out_credits(); 42891b99603Smatthias.ringwald 4296218e6f1Smatthias.ringwald return err; 43058de5610Smatthias.ringwald } 43158de5610Smatthias.ringwald 4322149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 4332149f12eSmatthias.ringwald 434c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 435c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4362149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4372149f12eSmatthias.ringwald } 4382149f12eSmatthias.ringwald 439a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(handle)){ 4409da54300S[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 441c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 442c8b9416aS[email protected] } 443c8b9416aS[email protected] 4449da54300S[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 4452149f12eSmatthias.ringwald 446facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4472149f12eSmatthias.ringwald 448e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 449e9772277S[email protected] 450e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 451e9772277S[email protected] bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 4522149f12eSmatthias.ringwald // 2 - ACL length 4532149f12eSmatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 4542149f12eSmatthias.ringwald // 4 - L2CAP packet length 4552149f12eSmatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 4562149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 4572149f12eSmatthias.ringwald bt_store_16(acl_buffer, 6, cid); 4582149f12eSmatthias.ringwald // send 459826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4602149f12eSmatthias.ringwald 4612149f12eSmatthias.ringwald l2cap_hand_out_credits(); 4622149f12eSmatthias.ringwald 4632149f12eSmatthias.ringwald return err; 4642149f12eSmatthias.ringwald } 4652149f12eSmatthias.ringwald 466b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 467b1d43497Smatthias.ringwald 468a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 469a35252c8S[email protected] if (!channel) { 4709da54300S[email protected] log_error("l2cap_send_internal no channel for cid 0x%02x", local_cid); 471a35252c8S[email protected] return -1; // TODO: define error 472a35252c8S[email protected] } 473a35252c8S[email protected] 474f0efaa57S[email protected] if (len > channel->remote_mtu){ 475f0efaa57S[email protected] log_error("l2cap_send_internal cid 0x%02x, data length exceeds remote MTU.", local_cid); 476f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 477f0efaa57S[email protected] } 478f0efaa57S[email protected] 479a35252c8S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)){ 4809da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", local_cid); 481b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 482b1d43497Smatthias.ringwald } 483b1d43497Smatthias.ringwald 4842a373862S[email protected] hci_reserve_packet_buffer(); 485facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 486b1d43497Smatthias.ringwald 487b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 488b1d43497Smatthias.ringwald 489b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 490b1d43497Smatthias.ringwald } 491b1d43497Smatthias.ringwald 4922149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 4932149f12eSmatthias.ringwald 494a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4959da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", cid); 4962149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4972149f12eSmatthias.ringwald } 4982149f12eSmatthias.ringwald 4992a373862S[email protected] hci_reserve_packet_buffer(); 500facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 5012149f12eSmatthias.ringwald 5022149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 5032149f12eSmatthias.ringwald 5042149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 5052149f12eSmatthias.ringwald } 5062149f12eSmatthias.ringwald 5070e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 5080e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 5090e37e417S[email protected] } 5100e37e417S[email protected] 51128ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 51228ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 51328ca2b46S[email protected] } 51428ca2b46S[email protected] 51528ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 51628ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 51728ca2b46S[email protected] } 51828ca2b46S[email protected] 51928ca2b46S[email protected] 520b1d43497Smatthias.ringwald 5218158c421Smatthias.ringwald // MARK: L2CAP_RUN 5222cd0be45Smatthias.ringwald // process outstanding signaling tasks 5237f02f414SMatthias Ringwald static void l2cap_run(void){ 5242b83fb7dSmatthias.ringwald 52522c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 52622c29ab4SMatthias Ringwald 5272b83fb7dSmatthias.ringwald // check pending signaling responses 5282b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5292b83fb7dSmatthias.ringwald 5302b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 531a35252c8S[email protected] 532a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 533a35252c8S[email protected] 5342b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5352b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 53663a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 537f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5382b83fb7dSmatthias.ringwald 539f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 540f53da564S[email protected] signaling_responses_pending--; 541f53da564S[email protected] int i; 542f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 543f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 544f53da564S[email protected] } 545f53da564S[email protected] 546f53da564S[email protected] switch (response_code){ 5472b360848Smatthias.ringwald case CONNECTION_REQUEST: 5482b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5492bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5504d816277S[email protected] if (result == 0x0003){ 5512bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5524d816277S[email protected] } 5532b360848Smatthias.ringwald break; 5542b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5552b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5562b83fb7dSmatthias.ringwald break; 5572b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5583b0484b3S[email protected] switch (infoType){ 5593b0484b3S[email protected] case 1: { // Connectionless MTU 5603b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5613b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5623b0484b3S[email protected] break; 5633b0484b3S[email protected] } 5643b0484b3S[email protected] case 2: { // Extended Features Supported 565462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 566462e630dS[email protected] uint32_t features = 0x280; 5673b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5683b0484b3S[email protected] break; 5693b0484b3S[email protected] } 5703b0484b3S[email protected] case 3: { // Fixed Channels Supported 5713b0484b3S[email protected] uint8_t map[8]; 5723b0484b3S[email protected] memset(map, 0, 8); 573462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5743b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5753b0484b3S[email protected] break; 5763b0484b3S[email protected] } 5773b0484b3S[email protected] default: 5782b83fb7dSmatthias.ringwald // all other types are not supported 5792b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5803b0484b3S[email protected] break; 5812b83fb7dSmatthias.ringwald } 5822b83fb7dSmatthias.ringwald break; 58363a7246aSmatthias.ringwald case COMMAND_REJECT: 5845ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 58570efece1S[email protected] #ifdef HAVE_BLE 58670efece1S[email protected] case COMMAND_REJECT_LE: 58770efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 58863a7246aSmatthias.ringwald break; 58970efece1S[email protected] #endif 5902b83fb7dSmatthias.ringwald default: 5912b83fb7dSmatthias.ringwald // should not happen 5922b83fb7dSmatthias.ringwald break; 5932b83fb7dSmatthias.ringwald } 5942b83fb7dSmatthias.ringwald } 5952b83fb7dSmatthias.ringwald 596ae280e73Smatthias.ringwald uint8_t config_options[4]; 597c22aecc9S[email protected] linked_list_iterator_t it; 598c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 599c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 600baf94f06S[email protected] 601c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 60222c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 6032cd0be45Smatthias.ringwald switch (channel->state){ 6042cd0be45Smatthias.ringwald 605df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 606ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 607baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 608a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 609ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 610a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 611ad671560S[email protected] } 612ad671560S[email protected] break; 613ad671560S[email protected] 61402b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 615baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 61664472d52Smatthias.ringwald // send connection request - set state first 61764472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 61802b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6198f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 62002b22dc4Smatthias.ringwald break; 62102b22dc4Smatthias.ringwald 622e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 623baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 62422c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 6251eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 626e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6279dcb2fb2S[email protected] l2cap_stop_rtx(channel); 628c22aecc9S[email protected] linked_list_iterator_remove(&it); 629d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 630e7ff783cSmatthias.ringwald break; 631e7ff783cSmatthias.ringwald 632552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 633baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 634fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 63528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 6362a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 637552d92a1Smatthias.ringwald break; 638552d92a1Smatthias.ringwald 6396fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 640baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 6416fdcc387Smatthias.ringwald // success, start l2cap handshake 642b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6436fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 6442a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6455932bd7cS[email protected] l2cap_start_rtx(channel); 6466fdcc387Smatthias.ringwald break; 6476fdcc387Smatthias.ringwald 648fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 649baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 65073cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 65163a7246aSmatthias.ringwald uint16_t flags = 0; 65228ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 65363a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 65463a7246aSmatthias.ringwald flags = 1; 65563a7246aSmatthias.ringwald } else { 65628ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 65763a7246aSmatthias.ringwald } 65863a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 65963a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 66063a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 66163a7246aSmatthias.ringwald config_options[0] = 1; // MTU 66263a7246aSmatthias.ringwald config_options[1] = 2; // len param 66363a7246aSmatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 66463a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 66563a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 66663a7246aSmatthias.ringwald } else { 66763a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 66863a7246aSmatthias.ringwald } 66963a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 670fa8473a4Smatthias.ringwald } 67173cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 67228ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 67328ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 674b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 675ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 676ae280e73Smatthias.ringwald config_options[1] = 2; // len param 677ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 678b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6795932bd7cS[email protected] l2cap_start_rtx(channel); 680fa8473a4Smatthias.ringwald } 681fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 682552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 683552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 684552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 685fa8473a4Smatthias.ringwald } 686552d92a1Smatthias.ringwald break; 687552d92a1Smatthias.ringwald 688e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 689baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 69022c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 691b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6925932bd7cS[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 :) 693756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 694e7ff783cSmatthias.ringwald break; 695e7ff783cSmatthias.ringwald 696e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 697baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 698b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6992cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 7002a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 7012cd0be45Smatthias.ringwald break; 7022cd0be45Smatthias.ringwald default: 7032cd0be45Smatthias.ringwald break; 7042cd0be45Smatthias.ringwald } 7052cd0be45Smatthias.ringwald } 706da886c03S[email protected] 7074d7157c3S[email protected] #ifdef HAVE_BLE 708da886c03S[email protected] // send l2cap con paramter update if necessary 709da886c03S[email protected] hci_connections_get_iterator(&it); 710da886c03S[email protected] while(linked_list_iterator_has_next(&it)){ 711da886c03S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it); 7125d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 713b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 714da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 715b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 716b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 717b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 718b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 719b68d7bc3SMatthias Ringwald break; 720da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 721b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 722b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 723da886c03S[email protected] break; 724da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 725b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 726b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 727da886c03S[email protected] break; 728da886c03S[email protected] default: 729da886c03S[email protected] break; 730da886c03S[email protected] } 731da886c03S[email protected] } 7324d7157c3S[email protected] #endif 733da886c03S[email protected] 73422c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 7352cd0be45Smatthias.ringwald } 7362cd0be45Smatthias.ringwald 7374aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 7384ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 739fa8c92f6Smatthias.ringwald } 740fa8c92f6Smatthias.ringwald 74171de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 7424ff786cfS[email protected] return l2cap_max_mtu(); 743e5e1518dS[email protected] } 744e5e1518dS[email protected] 7452df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 7462df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 7475533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 7482df5dadcS[email protected] // success, start l2cap handshake 7492df5dadcS[email protected] channel->handle = handle; 7502df5dadcS[email protected] // check remote SSP feature first 7512df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 7522df5dadcS[email protected] } 7532df5dadcS[email protected] } 7542df5dadcS[email protected] 7552df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 7562df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 7572df5dadcS[email protected] 7582df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 759ac301f95S[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)); 7602df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 7612df5dadcS[email protected] // request security level 2 7622df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 7631429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 7642df5dadcS[email protected] return; 7652df5dadcS[email protected] } 7662df5dadcS[email protected] // fine, go ahead 7672df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7682df5dadcS[email protected] } 7692df5dadcS[email protected] 770*9077cb15SMatthias Ringwald /** 771*9077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 772*9077cb15SMatthias Ringwald * @param packet_handler 773*9077cb15SMatthias Ringwald * @param address 774*9077cb15SMatthias Ringwald * @param psm 775*9077cb15SMatthias Ringwald * @param mtu 776*9077cb15SMatthias Ringwald * @param local_cid 777*9077cb15SMatthias Ringwald */ 778*9077cb15SMatthias 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){ 779*9077cb15SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 780*9077cb15SMatthias Ringwald 781*9077cb15SMatthias Ringwald // alloc structure 782*9077cb15SMatthias Ringwald l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 783*9077cb15SMatthias Ringwald if (!chan) { 784*9077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 785*9077cb15SMatthias Ringwald } 786*9077cb15SMatthias Ringwald 787*9077cb15SMatthias Ringwald // Init memory (make valgrind happy) 788*9077cb15SMatthias Ringwald memset(chan, 0, sizeof(l2cap_channel_t)); 789*9077cb15SMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 790*9077cb15SMatthias Ringwald if (mtu > l2cap_max_mtu()) { 791*9077cb15SMatthias Ringwald mtu = l2cap_max_mtu(); 792*9077cb15SMatthias Ringwald } 793*9077cb15SMatthias Ringwald 794*9077cb15SMatthias Ringwald // fill in 795*9077cb15SMatthias Ringwald BD_ADDR_COPY(chan->address, address); 796*9077cb15SMatthias Ringwald chan->psm = psm; 797*9077cb15SMatthias Ringwald chan->handle = 0; 798*9077cb15SMatthias Ringwald chan->packet_handler = channel_packet_handler; 799*9077cb15SMatthias Ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 800*9077cb15SMatthias Ringwald chan->local_mtu = mtu; 801*9077cb15SMatthias Ringwald chan->packets_granted = 0; 802*9077cb15SMatthias Ringwald chan->local_cid = l2cap_next_local_cid(); 803*9077cb15SMatthias Ringwald 804*9077cb15SMatthias Ringwald // set initial state 805*9077cb15SMatthias Ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 806*9077cb15SMatthias Ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 807*9077cb15SMatthias Ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 808*9077cb15SMatthias Ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 809*9077cb15SMatthias Ringwald chan->required_security_level = LEVEL_0; 810*9077cb15SMatthias Ringwald 811*9077cb15SMatthias Ringwald // add to connections list 812*9077cb15SMatthias Ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 813*9077cb15SMatthias Ringwald 814*9077cb15SMatthias Ringwald // store local_cid 815*9077cb15SMatthias Ringwald if (out_local_cid){ 816*9077cb15SMatthias Ringwald *out_local_cid = chan->local_cid; 817*9077cb15SMatthias Ringwald } 818*9077cb15SMatthias Ringwald 819*9077cb15SMatthias Ringwald // check if hci connection is already usable 820*9077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 821*9077cb15SMatthias Ringwald if (conn){ 822*9077cb15SMatthias Ringwald log_info("l2cap_create_channel_internal, hci connection already exists"); 823*9077cb15SMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, chan); 824*9077cb15SMatthias Ringwald // check if remote supported fearures are already received 825*9077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 826*9077cb15SMatthias Ringwald l2cap_handle_remote_supported_features_received(chan); 827*9077cb15SMatthias Ringwald } 828*9077cb15SMatthias Ringwald } 829*9077cb15SMatthias Ringwald 830*9077cb15SMatthias Ringwald l2cap_run(); 831*9077cb15SMatthias Ringwald 832*9077cb15SMatthias Ringwald return 0; 833*9077cb15SMatthias Ringwald } 834*9077cb15SMatthias Ringwald 8351e6aba47Smatthias.ringwald // open outgoing L2CAP channel 83605ae8de3SMatthias Ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t channel_packet_handler, 83715470d27Smatthias.ringwald bd_addr_t address, uint16_t psm, uint16_t mtu){ 8381e6aba47Smatthias.ringwald 839e0abb8e7S[email protected] log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 840e0abb8e7S[email protected] 8411e6aba47Smatthias.ringwald // alloc structure 842bb69aaaeS[email protected] l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 8432b360848Smatthias.ringwald if (!chan) { 8442b360848Smatthias.ringwald // emit error event 8452b360848Smatthias.ringwald l2cap_channel_t dummy_channel; 8462b360848Smatthias.ringwald BD_ADDR_COPY(dummy_channel.address, address); 8472b360848Smatthias.ringwald dummy_channel.psm = psm; 8482b360848Smatthias.ringwald l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 8492b360848Smatthias.ringwald return; 8502b360848Smatthias.ringwald } 851*9077cb15SMatthias Ringwald 852c523d53dS[email protected] // Init memory (make valgrind happy) 853c523d53dS[email protected] memset(chan, 0, sizeof(l2cap_channel_t)); 854f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 8552985cb84Smatthias.ringwald if (mtu > l2cap_max_mtu()) { 8562985cb84Smatthias.ringwald mtu = l2cap_max_mtu(); 8579775e25bSmatthias.ringwald } 8589775e25bSmatthias.ringwald 8591e6aba47Smatthias.ringwald // fill in 8601e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 8611e6aba47Smatthias.ringwald chan->psm = psm; 8621e6aba47Smatthias.ringwald chan->handle = 0; 8631e6aba47Smatthias.ringwald chan->connection = connection; 86405ae8de3SMatthias Ringwald chan->packet_handler = channel_packet_handler; 8652784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 86615470d27Smatthias.ringwald chan->local_mtu = mtu; 8676218e6f1Smatthias.ringwald chan->packets_granted = 0; 868*9077cb15SMatthias Ringwald chan->local_cid = l2cap_next_local_cid(); 8696218e6f1Smatthias.ringwald 8701e6aba47Smatthias.ringwald // set initial state 87102b22dc4Smatthias.ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 87273cf2b3dSmatthias.ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 873b1988dceSmatthias.ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 874b1988dceSmatthias.ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 8755533f01eS[email protected] chan->required_security_level = LEVEL_0; 8761e6aba47Smatthias.ringwald 8771e6aba47Smatthias.ringwald // add to connections list 8781e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 8791e6aba47Smatthias.ringwald 8802df5dadcS[email protected] // check if hci connection is already usable 8812e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 8822df5dadcS[email protected] if (conn){ 8835533f01eS[email protected] log_info("l2cap_create_channel_internal, hci connection already exists"); 8842df5dadcS[email protected] l2cap_handle_connection_complete(conn->con_handle, chan); 885775ecc36SMatthias Ringwald // check if remote supported fearures are already received 8862df5dadcS[email protected] if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 8872df5dadcS[email protected] l2cap_handle_remote_supported_features_received(chan); 8882df5dadcS[email protected] } 8892df5dadcS[email protected] } 8902df5dadcS[email protected] 89102b22dc4Smatthias.ringwald l2cap_run(); 89243625864Smatthias.ringwald } 89343625864Smatthias.ringwald 894b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 895e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 896b35f641cSmatthias.ringwald // find channel for local_cid 897b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 898f62db1e3Smatthias.ringwald if (channel) { 899e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 900f62db1e3Smatthias.ringwald } 9012cd0be45Smatthias.ringwald // process 9022cd0be45Smatthias.ringwald l2cap_run(); 90343625864Smatthias.ringwald } 9041e6aba47Smatthias.ringwald 905afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 906c22aecc9S[email protected] linked_list_iterator_t it; 907c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 908c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 909c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 910c22aecc9S[email protected] if ( BD_ADDR_CMP( channel->address, address) != 0) continue; 911c22aecc9S[email protected] // channel for this address found 912c22aecc9S[email protected] switch (channel->state){ 913c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 914c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 915afde0c52Smatthias.ringwald // failure, forward error code 916afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 917afde0c52Smatthias.ringwald // discard channel 9189dcb2fb2S[email protected] l2cap_stop_rtx(channel); 919c22aecc9S[email protected] linked_list_iterator_remove(&it); 920d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 921c22aecc9S[email protected] break; 922c22aecc9S[email protected] default: 923c22aecc9S[email protected] break; 924afde0c52Smatthias.ringwald } 925afde0c52Smatthias.ringwald } 926afde0c52Smatthias.ringwald } 927afde0c52Smatthias.ringwald 928afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 929c22aecc9S[email protected] linked_list_iterator_t it; 930c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 931c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 932c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 933afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 9342df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 935afde0c52Smatthias.ringwald } 936afde0c52Smatthias.ringwald } 9376fdcc387Smatthias.ringwald // process 9386fdcc387Smatthias.ringwald l2cap_run(); 939afde0c52Smatthias.ringwald } 940b448a0e7Smatthias.ringwald 9417f02f414SMatthias Ringwald static void l2cap_event_handler(uint8_t *packet, uint16_t size){ 942afde0c52Smatthias.ringwald 943afde0c52Smatthias.ringwald bd_addr_t address; 944afde0c52Smatthias.ringwald hci_con_handle_t handle; 945c22aecc9S[email protected] linked_list_iterator_t it; 9462d00edd4Smatthias.ringwald int hci_con_used; 947afde0c52Smatthias.ringwald 948afde0c52Smatthias.ringwald switch(packet[0]){ 949afde0c52Smatthias.ringwald 950afde0c52Smatthias.ringwald // handle connection complete events 951afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 952afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 953afde0c52Smatthias.ringwald if (packet[2] == 0){ 954afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 955afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 956afde0c52Smatthias.ringwald } else { 957afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 958afde0c52Smatthias.ringwald } 959afde0c52Smatthias.ringwald break; 960afde0c52Smatthias.ringwald 961afde0c52Smatthias.ringwald // handle successful create connection cancel command 962afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 963afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 964afde0c52Smatthias.ringwald if (packet[5] == 0){ 965afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 966afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 967afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 96803cfbabcSmatthias.ringwald } 9691e6aba47Smatthias.ringwald } 97039d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 97139d59809Smatthias.ringwald break; 97239d59809Smatthias.ringwald 97339d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 97439d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 975afde0c52Smatthias.ringwald break; 97627a923d0Smatthias.ringwald 9771e6aba47Smatthias.ringwald // handle disconnection complete events 978afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 979c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 980afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 981c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 982c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 983c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 984c22aecc9S[email protected] if (channel->handle != handle) continue; 98515ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 9869dcb2fb2S[email protected] l2cap_stop_rtx(channel); 987c22aecc9S[email protected] linked_list_iterator_remove(&it); 988d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 98927a923d0Smatthias.ringwald } 990afde0c52Smatthias.ringwald break; 991fcadd0caSmatthias.ringwald 9926218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 99302b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 9948d371091Smatthias.ringwald l2cap_hand_out_credits(); 9956218e6f1Smatthias.ringwald break; 9966218e6f1Smatthias.ringwald 997ee091cf1Smatthias.ringwald // HCI Connection Timeouts 998afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 99936944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 1000bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 100180ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 10022d00edd4Smatthias.ringwald hci_con_used = 0; 1003c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1004c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1005c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1006c22aecc9S[email protected] if (channel->handle != handle) continue; 10072d00edd4Smatthias.ringwald hci_con_used = 1; 1008c22aecc9S[email protected] break; 1009ee091cf1Smatthias.ringwald } 10102d00edd4Smatthias.ringwald if (hci_con_used) break; 1011d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 10129edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1013afde0c52Smatthias.ringwald break; 1014ee091cf1Smatthias.ringwald 10156e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 1016c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1017c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1018c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1019c22aecc9S[email protected] if (!channel->packet_handler) continue; 10206e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 10216e6710ebSmatthias.ringwald } 10225652b5ffS[email protected] if (attribute_protocol_packet_handler) { 10235652b5ffS[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 10245652b5ffS[email protected] } 10255652b5ffS[email protected] if (security_protocol_packet_handler) { 1026133efcfdSmatthias.ringwald (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 10275652b5ffS[email protected] } 1028462e630dS[email protected] if (connectionless_channel_packet_handler) { 1029462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1030462e630dS[email protected] } 10316e6710ebSmatthias.ringwald break; 10326e6710ebSmatthias.ringwald 1033df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1034df3354fcS[email protected] handle = READ_BT_16(packet, 3); 1035c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1036c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1037c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1038df3354fcS[email protected] if (channel->handle != handle) continue; 10392df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1040df3354fcS[email protected] break; 1041df3354fcS[email protected] } 1042c22aecc9S[email protected] break; 1043df3354fcS[email protected] 1044a00031e2S[email protected] case GAP_SECURITY_LEVEL: 1045a00031e2S[email protected] handle = READ_BT_16(packet, 2); 1046bd63148eS[email protected] log_info("l2cap - security level update"); 1047c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1048c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1049c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1050f85a9399S[email protected] if (channel->handle != handle) continue; 10515533f01eS[email protected] 1052bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1053bd63148eS[email protected] 1054e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 10555533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 10565533f01eS[email protected] 1057df3354fcS[email protected] switch (channel->state){ 1058df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 10595533f01eS[email protected] if (actual_level >= required_level){ 1060f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1061f85a9399S[email protected] l2cap_emit_connection_request(channel); 10621eb2563eS[email protected] } else { 1063775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 10641eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 10651eb2563eS[email protected] } 1066df3354fcS[email protected] break; 1067df3354fcS[email protected] 1068df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 10695533f01eS[email protected] if (actual_level >= required_level){ 1070df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1071df3354fcS[email protected] } else { 1072df3354fcS[email protected] // disconnnect, authentication not good enough 1073df3354fcS[email protected] hci_disconnect_security_block(handle); 1074df3354fcS[email protected] } 1075df3354fcS[email protected] break; 1076df3354fcS[email protected] 1077df3354fcS[email protected] default: 1078df3354fcS[email protected] break; 1079df3354fcS[email protected] } 1080f85a9399S[email protected] } 1081f85a9399S[email protected] break; 1082f85a9399S[email protected] 1083afde0c52Smatthias.ringwald default: 1084afde0c52Smatthias.ringwald break; 1085afde0c52Smatthias.ringwald } 1086afde0c52Smatthias.ringwald 1087e0707417S[email protected] // pass on: main packet handler, att and sm packet handlers 1088b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 1089e0707417S[email protected] if (attribute_protocol_packet_handler){ 1090e0707417S[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1091e0707417S[email protected] } 1092e0707417S[email protected] if (security_protocol_packet_handler) { 1093e0707417S[email protected] (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1094e0707417S[email protected] } 1095462e630dS[email protected] if (connectionless_channel_packet_handler) { 1096462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1097462e630dS[email protected] } 1098bd63148eS[email protected] 1099bd63148eS[email protected] l2cap_run(); 11001e6aba47Smatthias.ringwald } 11011e6aba47Smatthias.ringwald 1102afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1103b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1104e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1105e7ff783cSmatthias.ringwald l2cap_run(); 110684836b65Smatthias.ringwald } 110784836b65Smatthias.ringwald 11082b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 11094cf56b4aSmatthias.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." 11102b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 11112b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 11122b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 11132b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 11142b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 11152b360848Smatthias.ringwald signaling_responses_pending++; 11162b360848Smatthias.ringwald l2cap_run(); 11172b360848Smatthias.ringwald } 11182b360848Smatthias.ringwald } 11192b360848Smatthias.ringwald 1120b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1121645658c9Smatthias.ringwald 11229da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1123645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1124645658c9Smatthias.ringwald if (!service) { 1125645658c9Smatthias.ringwald // 0x0002 PSM not supported 11262b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1127645658c9Smatthias.ringwald return; 1128645658c9Smatthias.ringwald } 1129645658c9Smatthias.ringwald 11305061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1131645658c9Smatthias.ringwald if (!hci_connection) { 11322b360848Smatthias.ringwald // 11339da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1134645658c9Smatthias.ringwald return; 1135645658c9Smatthias.ringwald } 11362bd8b7e7S[email protected] 1137645658c9Smatthias.ringwald // alloc structure 11389da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1139bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 11402b360848Smatthias.ringwald if (!channel){ 11412b360848Smatthias.ringwald // 0x0004 No resources available 11422b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 11432b360848Smatthias.ringwald return; 11442b360848Smatthias.ringwald } 1145c523d53dS[email protected] // Init memory (make valgrind happy) 1146c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1147645658c9Smatthias.ringwald // fill in 1148169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 1149169f8b28Smatthias.ringwald channel->psm = psm; 1150169f8b28Smatthias.ringwald channel->handle = handle; 1151169f8b28Smatthias.ringwald channel->connection = service->connection; 1152f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1153b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1154b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1155fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 11560a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1157761b0451Smatthias.ringwald channel->packets_granted = 0; 1158b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1159df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1160645658c9Smatthias.ringwald 1161f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 11622985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 11632985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 11649775e25bSmatthias.ringwald } 11659775e25bSmatthias.ringwald 1166645658c9Smatthias.ringwald // set initial state 1167df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1168ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1169e405ae81Smatthias.ringwald 1170645658c9Smatthias.ringwald // add to connections list 1171169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 1172645658c9Smatthias.ringwald 1173f85a9399S[email protected] // assert security requirements 11741eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1175e405ae81Smatthias.ringwald } 1176645658c9Smatthias.ringwald 1177b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 1178e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1179b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1180e405ae81Smatthias.ringwald if (!channel) { 11817d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 1182e405ae81Smatthias.ringwald return; 1183e405ae81Smatthias.ringwald } 1184e405ae81Smatthias.ringwald 1185552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1186e405ae81Smatthias.ringwald 1187552d92a1Smatthias.ringwald // process 1188552d92a1Smatthias.ringwald l2cap_run(); 1189e405ae81Smatthias.ringwald } 1190645658c9Smatthias.ringwald 1191b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 1192e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1193b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1194e405ae81Smatthias.ringwald if (!channel) { 11957d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 1196e405ae81Smatthias.ringwald return; 1197e405ae81Smatthias.ringwald } 1198e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1199e7ff783cSmatthias.ringwald channel->reason = reason; 1200e7ff783cSmatthias.ringwald l2cap_run(); 1201645658c9Smatthias.ringwald } 1202645658c9Smatthias.ringwald 12037f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1204b1988dceSmatthias.ringwald 1205b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1206b1988dceSmatthias.ringwald 120763a7246aSmatthias.ringwald uint16_t flags = READ_BT_16(command, 6); 120863a7246aSmatthias.ringwald if (flags & 1) { 120963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 121063a7246aSmatthias.ringwald } 121163a7246aSmatthias.ringwald 12122784b77dSmatthias.ringwald // accept the other's configuration options 12133de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 12143de7c0caSmatthias.ringwald uint16_t pos = 8; 12153de7c0caSmatthias.ringwald while (pos < end_pos){ 121663a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 121763a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 121863a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 121963a7246aSmatthias.ringwald pos++; 12201dc511deSmatthias.ringwald uint8_t length = command[pos++]; 12211dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 122263a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 12231dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 12249da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 122563a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 122663a7246aSmatthias.ringwald } 12270fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 12280fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 12290fe7a9d0S[email protected] channel->flush_timeout = READ_BT_16(command, pos); 12300fe7a9d0S[email protected] } 123163a7246aSmatthias.ringwald // check for unknown options 123263a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1233c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 123463a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 12351dc511deSmatthias.ringwald } 12361dc511deSmatthias.ringwald pos += length; 12371dc511deSmatthias.ringwald } 12382784b77dSmatthias.ringwald } 12392784b77dSmatthias.ringwald 1240fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 12419da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 124273cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 124373cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1244019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1245019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1246fa8473a4Smatthias.ringwald return 1; 1247fa8473a4Smatthias.ringwald } 1248fa8473a4Smatthias.ringwald 1249fa8473a4Smatthias.ringwald 12507f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 12511e6aba47Smatthias.ringwald 125200d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 125300d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 125438e5900eSmatthias.ringwald uint16_t result = 0; 12551e6aba47Smatthias.ringwald 12569da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1257b35f641cSmatthias.ringwald 12589a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 12599a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 12609a011532Smatthias.ringwald switch (channel->state){ 1261fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 12629a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 12632b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 12649a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 12659a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 12669a011532Smatthias.ringwald break; 12679a011532Smatthias.ringwald 12689a011532Smatthias.ringwald default: 12699a011532Smatthias.ringwald // ignore in other states 12709a011532Smatthias.ringwald break; 12719a011532Smatthias.ringwald } 12729a011532Smatthias.ringwald return; 12739a011532Smatthias.ringwald } 12749a011532Smatthias.ringwald 127556081214Smatthias.ringwald // @STATEMACHINE(l2cap) 12761e6aba47Smatthias.ringwald switch (channel->state) { 12771e6aba47Smatthias.ringwald 12781e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 12791e6aba47Smatthias.ringwald switch (code){ 12801e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 12815932bd7cS[email protected] l2cap_stop_rtx(channel); 128200d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 128338e5900eSmatthias.ringwald switch (result) { 128438e5900eSmatthias.ringwald case 0: 1285169f8b28Smatthias.ringwald // successful connection 128600d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1287fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 128828ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 128938e5900eSmatthias.ringwald break; 129038e5900eSmatthias.ringwald case 1: 12915932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 12925932bd7cS[email protected] l2cap_start_ertx(channel); 129338e5900eSmatthias.ringwald break; 129438e5900eSmatthias.ringwald default: 1295eb920dbeSmatthias.ringwald // channel closed 1296eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1297f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 129838e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1299eb920dbeSmatthias.ringwald 1300eb920dbeSmatthias.ringwald // drop link key if security block 1301eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 13022e77e513S[email protected] hci_drop_link_key_for_bd_addr(channel->address); 1303eb920dbeSmatthias.ringwald } 1304eb920dbeSmatthias.ringwald 1305eb920dbeSmatthias.ringwald // discard channel 1306eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1307d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 130838e5900eSmatthias.ringwald break; 13091e6aba47Smatthias.ringwald } 13101e6aba47Smatthias.ringwald break; 131138e5900eSmatthias.ringwald 131238e5900eSmatthias.ringwald default: 13131e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 131438e5900eSmatthias.ringwald break; 13151e6aba47Smatthias.ringwald } 13161e6aba47Smatthias.ringwald break; 13171e6aba47Smatthias.ringwald 1318fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1319fe9d8984S[email protected] result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1320ae280e73Smatthias.ringwald switch (code) { 1321ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 132228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1323ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 132463a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 132563a7246aSmatthias.ringwald // only done if continuation not set 132663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 132763a7246aSmatthias.ringwald } 1328ae280e73Smatthias.ringwald break; 13291e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 13305932bd7cS[email protected] l2cap_stop_rtx(channel); 13315932bd7cS[email protected] switch (result){ 13325932bd7cS[email protected] case 0: // success 13335932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 13345932bd7cS[email protected] break; 13355932bd7cS[email protected] case 4: // pending 13365932bd7cS[email protected] l2cap_start_ertx(channel); 13375932bd7cS[email protected] break; 13385932bd7cS[email protected] default: 1339fe9d8984S[email protected] // retry on negative result 1340fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1341fe9d8984S[email protected] break; 1342fe9d8984S[email protected] } 13435a67bd4aSmatthias.ringwald break; 13445a67bd4aSmatthias.ringwald default: 13455a67bd4aSmatthias.ringwald break; 13461e6aba47Smatthias.ringwald } 1347fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1348fa8473a4Smatthias.ringwald // for open: 13495a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1350fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 13516218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 1352c8e4258aSmatthias.ringwald } 1353c8e4258aSmatthias.ringwald break; 1354f62db1e3Smatthias.ringwald 1355f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1356f62db1e3Smatthias.ringwald switch (code) { 1357f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 135827a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 135927a923d0Smatthias.ringwald break; 13605a67bd4aSmatthias.ringwald default: 13615a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 13625a67bd4aSmatthias.ringwald break; 136327a923d0Smatthias.ringwald } 136427a923d0Smatthias.ringwald break; 136584836b65Smatthias.ringwald 136684836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 136784836b65Smatthias.ringwald // @TODO handle incoming requests 136884836b65Smatthias.ringwald break; 136984836b65Smatthias.ringwald 137084836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 137184836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 137284836b65Smatthias.ringwald break; 137310642e45Smatthias.ringwald default: 137410642e45Smatthias.ringwald break; 137527a923d0Smatthias.ringwald } 13769da54300S[email protected] // log_info("new state %u", channel->state); 137727a923d0Smatthias.ringwald } 137827a923d0Smatthias.ringwald 137900d93d79Smatthias.ringwald 13807f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 138100d93d79Smatthias.ringwald 138200d93d79Smatthias.ringwald // get code, signalind identifier and command len 138300d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 138400d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 138500d93d79Smatthias.ringwald 138600d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 138700d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 138863a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 138900d93d79Smatthias.ringwald return; 139000d93d79Smatthias.ringwald } 139100d93d79Smatthias.ringwald 139200d93d79Smatthias.ringwald // general commands without an assigned channel 139300d93d79Smatthias.ringwald switch(code) { 139400d93d79Smatthias.ringwald 139500d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 139600d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 139700d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 139800d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 13992b83fb7dSmatthias.ringwald return; 140000d93d79Smatthias.ringwald } 140100d93d79Smatthias.ringwald 14022b360848Smatthias.ringwald case ECHO_REQUEST: 14032b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 14042b83fb7dSmatthias.ringwald return; 140500d93d79Smatthias.ringwald 140600d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 140700d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 14082b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 14092b83fb7dSmatthias.ringwald return; 141000d93d79Smatthias.ringwald } 141100d93d79Smatthias.ringwald 141200d93d79Smatthias.ringwald default: 141300d93d79Smatthias.ringwald break; 141400d93d79Smatthias.ringwald } 141500d93d79Smatthias.ringwald 141600d93d79Smatthias.ringwald 141700d93d79Smatthias.ringwald // Get potential destination CID 141800d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 141900d93d79Smatthias.ringwald 142000d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1421c22aecc9S[email protected] linked_list_iterator_t it; 1422c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1423c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1424c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1425c22aecc9S[email protected] if (channel->handle != handle) continue; 142600d93d79Smatthias.ringwald if (code & 1) { 1427b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1428b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 142900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 14304e32727eSmatthias.ringwald break; 143100d93d79Smatthias.ringwald } 143200d93d79Smatthias.ringwald } else { 1433b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 143400d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 143500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 14364e32727eSmatthias.ringwald break; 143700d93d79Smatthias.ringwald } 143800d93d79Smatthias.ringwald } 143900d93d79Smatthias.ringwald } 144000d93d79Smatthias.ringwald } 144100d93d79Smatthias.ringwald 14427f02f414SMatthias Ringwald static void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 144300d93d79Smatthias.ringwald 144400d93d79Smatthias.ringwald // Get Channel ID 144500d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 144600d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 144700d93d79Smatthias.ringwald 14485652b5ffS[email protected] switch (channel_id) { 14495652b5ffS[email protected] 14505652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 14515652b5ffS[email protected] 145200d93d79Smatthias.ringwald uint16_t command_offset = 8; 145300d93d79Smatthias.ringwald while (command_offset < size) { 145400d93d79Smatthias.ringwald 145500d93d79Smatthias.ringwald // handle signaling commands 145600d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 145700d93d79Smatthias.ringwald 145800d93d79Smatthias.ringwald // increment command_offset 145900d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 146000d93d79Smatthias.ringwald } 14615652b5ffS[email protected] break; 146200d93d79Smatthias.ringwald } 146300d93d79Smatthias.ringwald 14645652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 14655652b5ffS[email protected] if (attribute_protocol_packet_handler) { 14665652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14675652b5ffS[email protected] } 14685652b5ffS[email protected] break; 14695652b5ffS[email protected] 14705652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 14715652b5ffS[email protected] if (security_protocol_packet_handler) { 14725652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14735652b5ffS[email protected] } 14745652b5ffS[email protected] break; 14755652b5ffS[email protected] 1476462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1477462e630dS[email protected] if (connectionless_channel_packet_handler) { 1478462e630dS[email protected] (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1479462e630dS[email protected] } 1480462e630dS[email protected] break; 1481462e630dS[email protected] 14821bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1483ccf076adS[email protected] switch (packet[8]){ 1484ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1485ccf076adS[email protected] uint16_t result = READ_BT_16(packet, 12); 1486ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1487ccf076adS[email protected] break; 1488ccf076adS[email protected] } 1489ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1490ccf076adS[email protected] uint8_t event[10]; 1491ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1492ccf076adS[email protected] event[1] = 8; 1493ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1494da886c03S[email protected] 1495da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1496da886c03S[email protected] if (connection){ 14976d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 14986d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 14996d91fb6cSMatthias Ringwald uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 15006d91fb6cSMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 15016d91fb6cSMatthias Ringwald break; 15026d91fb6cSMatthias Ringwald } 1503da886c03S[email protected] int update_parameter = 1; 1504a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 1505a4c06b28SMatthias Ringwald gap_le_get_connection_parameter_range(existing_range); 1506da886c03S[email protected] uint16_t le_conn_interval_min = READ_BT_16(packet,12); 1507da886c03S[email protected] uint16_t le_conn_interval_max = READ_BT_16(packet,14); 1508da886c03S[email protected] uint16_t le_conn_latency = READ_BT_16(packet,16); 1509da886c03S[email protected] uint16_t le_supervision_timeout = READ_BT_16(packet,18); 1510da886c03S[email protected] 1511da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1512da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1513da886c03S[email protected] 1514da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1515da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1516da886c03S[email protected] 1517da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1518da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1519da886c03S[email protected] 1520da886c03S[email protected] if (update_parameter){ 1521da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1522da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1523da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1524da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1525da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1526da886c03S[email protected] } else { 1527da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1528da886c03S[email protected] } 152979f53f1dSMatthias Ringwald connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1530da886c03S[email protected] } 1531da886c03S[email protected] 1532ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1533ccf076adS[email protected] (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1534da886c03S[email protected] 1535ccf076adS[email protected] break; 1536ccf076adS[email protected] } 1537ccf076adS[email protected] default: { 15381bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 15391bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 15401bbc0b23S[email protected] break; 15411bbc0b23S[email protected] } 1542ccf076adS[email protected] } 1543ccf076adS[email protected] break; 1544ccf076adS[email protected] } 15451bbc0b23S[email protected] 15465652b5ffS[email protected] default: { 154700d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 154800d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 154900d93d79Smatthias.ringwald if (channel) { 155058de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 155100d93d79Smatthias.ringwald } 15525652b5ffS[email protected] break; 15535652b5ffS[email protected] } 15545652b5ffS[email protected] } 155500d93d79Smatthias.ringwald } 155600d93d79Smatthias.ringwald 15572718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 15582718e2e7Smatthias.ringwald switch (packet_type) { 15592718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 15602718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 15612718e2e7Smatthias.ringwald break; 15622718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 15632718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 15642718e2e7Smatthias.ringwald break; 15652718e2e7Smatthias.ringwald default: 15662718e2e7Smatthias.ringwald break; 15672718e2e7Smatthias.ringwald } 15681eb2563eS[email protected] l2cap_run(); 15692718e2e7Smatthias.ringwald } 157000d93d79Smatthias.ringwald 157115ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 157227a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1573f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1574f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1575f62db1e3Smatthias.ringwald // discard channel 15769dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1577f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1578d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1579c8e4258aSmatthias.ringwald } 15801e6aba47Smatthias.ringwald 15817192e786SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(linked_list_t * services, uint16_t psm){ 1582c22aecc9S[email protected] linked_list_iterator_t it; 15837192e786SMatthias Ringwald linked_list_iterator_init(&it, services); 1584c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1585c22aecc9S[email protected] l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it); 15869d9bbc01Smatthias.ringwald if ( service->psm == psm){ 15879d9bbc01Smatthias.ringwald return service; 15889d9bbc01Smatthias.ringwald }; 15899d9bbc01Smatthias.ringwald } 15909d9bbc01Smatthias.ringwald return NULL; 15919d9bbc01Smatthias.ringwald } 15929d9bbc01Smatthias.ringwald 15937192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 15947192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 15957192e786SMatthias Ringwald } 15967192e786SMatthias Ringwald 159705ae8de3SMatthias Ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1598e0abb8e7S[email protected] 1599e6f51008S[email protected] log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 1600e0abb8e7S[email protected] 16014bb582b6Smatthias.ringwald // check for alread registered psm 16024bb582b6Smatthias.ringwald // TODO: emit error event 16039d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1604277abc2cSmatthias.ringwald if (service) { 16059da54300S[email protected] log_error("l2cap_register_service_internal: PSM %u already registered", psm); 160681476041Smatthias.ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 1607277abc2cSmatthias.ringwald return; 1608277abc2cSmatthias.ringwald } 16099d9bbc01Smatthias.ringwald 16104bb582b6Smatthias.ringwald // alloc structure 16114bb582b6Smatthias.ringwald // TODO: emit error event 1612bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1613277abc2cSmatthias.ringwald if (!service) { 16149da54300S[email protected] log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 16155842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 1616277abc2cSmatthias.ringwald return; 1617277abc2cSmatthias.ringwald } 16189d9bbc01Smatthias.ringwald 16199d9bbc01Smatthias.ringwald // fill in 16209d9bbc01Smatthias.ringwald service->psm = psm; 16219d9bbc01Smatthias.ringwald service->mtu = mtu; 16229d9bbc01Smatthias.ringwald service->connection = connection; 162305ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1624df3354fcS[email protected] service->required_security_level = security_level; 16259d9bbc01Smatthias.ringwald 16269d9bbc01Smatthias.ringwald // add to services list 16279d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 1628c0e866bfSmatthias.ringwald 1629c0e866bfSmatthias.ringwald // enable page scan 1630c0e866bfSmatthias.ringwald hci_connectable_control(1); 16315842b6d9Smatthias.ringwald 16325842b6d9Smatthias.ringwald // done 16335842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, 0, psm); 16349d9bbc01Smatthias.ringwald } 16359d9bbc01Smatthias.ringwald 163636944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 1637e0abb8e7S[email protected] 1638e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1639e0abb8e7S[email protected] 16409d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1641037d6e48Smatthias.ringwald if (!service) return; 16429d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 1643d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1644c0e866bfSmatthias.ringwald 1645c0e866bfSmatthias.ringwald // disable page scan when no services registered 1646c0e866bfSmatthias.ringwald if (!linked_list_empty(&l2cap_services)) return; 1647c0e866bfSmatthias.ringwald hci_connectable_control(0); 16489d9bbc01Smatthias.ringwald } 16499d9bbc01Smatthias.ringwald 16505652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 165105ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 16525652b5ffS[email protected] switch(channel_id){ 16535652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 165405ae8de3SMatthias Ringwald attribute_protocol_packet_handler = the_packet_handler; 16555652b5ffS[email protected] break; 16565652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 165705ae8de3SMatthias Ringwald security_protocol_packet_handler = the_packet_handler; 16585652b5ffS[email protected] break; 1659462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 166005ae8de3SMatthias Ringwald connectionless_channel_packet_handler = the_packet_handler; 1661462e630dS[email protected] break; 16625652b5ffS[email protected] } 16635652b5ffS[email protected] } 16645652b5ffS[email protected] 16657192e786SMatthias Ringwald #ifdef HAVE_BLE 16667192e786SMatthias Ringwald 16677f02f414SMatthias Ringwald 16687f02f414SMatthias Ringwald #if 0 16697192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 16707192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, psm); 16717192e786SMatthias Ringwald } 16727192e786SMatthias Ringwald /** 16737192e786SMatthias Ringwald * @brief Regster L2CAP LE Credit Based Flow Control Mode service 16747192e786SMatthias Ringwald * @param 16757192e786SMatthias Ringwald */ 16767192e786SMatthias Ringwald void l2cap_le_register_service_internal(void * connection, btstack_packet_handler_t packet_handler, uint16_t psm, 16777192e786SMatthias Ringwald uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 16787192e786SMatthias Ringwald 16797192e786SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 16807192e786SMatthias Ringwald 16817192e786SMatthias Ringwald // check for alread registered psm 16827192e786SMatthias Ringwald // TODO: emit error event 16837192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16847192e786SMatthias Ringwald if (service) { 16857192e786SMatthias Ringwald log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 16867192e786SMatthias Ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 16877192e786SMatthias Ringwald return; 16887192e786SMatthias Ringwald } 16897192e786SMatthias Ringwald 16907192e786SMatthias Ringwald // alloc structure 16917192e786SMatthias Ringwald // TODO: emit error event 16927192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 16937192e786SMatthias Ringwald if (!service) { 16947192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 16957192e786SMatthias Ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 16967192e786SMatthias Ringwald return; 16977192e786SMatthias Ringwald } 16987192e786SMatthias Ringwald 16997192e786SMatthias Ringwald // fill in 17007192e786SMatthias Ringwald service->psm = psm; 17017192e786SMatthias Ringwald service->mtu = mtu; 17027192e786SMatthias Ringwald service->mps = mps; 17037192e786SMatthias Ringwald service->connection = connection; 17047192e786SMatthias Ringwald service->packet_handler = packet_handler; 17057192e786SMatthias Ringwald service->required_security_level = security_level; 17067192e786SMatthias Ringwald 17077192e786SMatthias Ringwald // add to services list 17087192e786SMatthias Ringwald linked_list_add(&l2cap_le_services, (linked_item_t *) service); 17097192e786SMatthias Ringwald 17107192e786SMatthias Ringwald // done 17117192e786SMatthias Ringwald l2cap_emit_service_registered(connection, 0, psm); 17127192e786SMatthias Ringwald } 17137192e786SMatthias Ringwald 17147192e786SMatthias Ringwald void l2cap_le_unregister_service_internal(void * connection, uint16_t psm) { 17157192e786SMatthias Ringwald 17167192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 17177192e786SMatthias Ringwald 17187192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 17197192e786SMatthias Ringwald if (!service) return; 17207192e786SMatthias Ringwald linked_list_remove(&l2cap_le_services, (linked_item_t *) service); 17217192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 17227192e786SMatthias Ringwald } 17237192e786SMatthias Ringwald #endif 17247f02f414SMatthias Ringwald #endif 1725