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; 7836944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 79808a48abSmatthias.ringwald static int new_credits_blocked = 0; 801e6aba47Smatthias.ringwald 81c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler; 82c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler; 83462e630dS[email protected] static btstack_packet_handler_t connectionless_channel_packet_handler; 84ac301f95S[email protected] static uint8_t require_security_level2_for_outgoing_sdp; 855652b5ffS[email protected] 8639bda6d5Smatthias.ringwald // prototypes 87fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 88fa8473a4Smatthias.ringwald static l2cap_service_t * l2cap_get_service(uint16_t psm); 89fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 90fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 91fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 92fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 9339bda6d5Smatthias.ringwald 9439bda6d5Smatthias.ringwald 95*71de195eSMatthias Ringwald void l2cap_init(void){ 96808a48abSmatthias.ringwald new_credits_blocked = 0; 972b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 98808a48abSmatthias.ringwald 99f5454fc6Smatthias.ringwald l2cap_channels = NULL; 100f5454fc6Smatthias.ringwald l2cap_services = NULL; 101f5454fc6Smatthias.ringwald 102f5454fc6Smatthias.ringwald packet_handler = null_packet_handler; 103c42e2ff2S[email protected] attribute_protocol_packet_handler = NULL; 104c42e2ff2S[email protected] security_protocol_packet_handler = NULL; 105462e630dS[email protected] connectionless_channel_packet_handler = NULL; 106f5454fc6Smatthias.ringwald 107ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 108ac301f95S[email protected] 109fcadd0caSmatthias.ringwald // 1102718e2e7Smatthias.ringwald // register callback with HCI 111fcadd0caSmatthias.ringwald // 1122718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 113c0e866bfSmatthias.ringwald hci_connectable_control(0); // no services yet 114fcadd0caSmatthias.ringwald } 115fcadd0caSmatthias.ringwald 116fcadd0caSmatthias.ringwald 117fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 11836944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 119fcadd0caSmatthias.ringwald } 12036944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 121b502e1b0Smatthias.ringwald packet_handler = handler; 1221e6aba47Smatthias.ringwald } 1231e6aba47Smatthias.ringwald 12458de5610Smatthias.ringwald // notify client/protocol handler 12558de5610Smatthias.ringwald void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 12658de5610Smatthias.ringwald if (channel->packet_handler) { 12758de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 12858de5610Smatthias.ringwald } else { 12936944dffSmatthias.ringwald (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 13058de5610Smatthias.ringwald } 13158de5610Smatthias.ringwald } 13258de5610Smatthias.ringwald 13358de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 134c9dc710bS[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", 135e0abb8e7S[email protected] status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 136c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 137c9dc710bS[email protected] uint8_t event[23]; 13858de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 13958de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 14058de5610Smatthias.ringwald event[2] = status; 14158de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 14258de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 14358de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 14458de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 14558de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 1464c98aa43Smatthias.ringwald bt_store_16(event, 17, channel->local_mtu); 1474c98aa43Smatthias.ringwald bt_store_16(event, 19, channel->remote_mtu); 14854ed2ecaS[email protected] bt_store_16(event, 21, channel->flush_timeout); 14958de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 15058de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 15158de5610Smatthias.ringwald } 15258de5610Smatthias.ringwald 15358de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 154e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 15558de5610Smatthias.ringwald uint8_t event[4]; 15658de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 15758de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 15858de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 15958de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 16058de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 16158de5610Smatthias.ringwald } 16258de5610Smatthias.ringwald 16358de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 164e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 165e0abb8e7S[email protected] bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 16658de5610Smatthias.ringwald uint8_t event[16]; 16758de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 16858de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 16958de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 17058de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 17158de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 17258de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 17358de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 17458de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17558de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1760af41d30Smatthias.ringwald } 177808a48abSmatthias.ringwald 178ccf076adS[email protected] void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){ 179ccf076adS[email protected] uint8_t event[6]; 180ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 181ccf076adS[email protected] event[1] = 4; 182ccf076adS[email protected] bt_store_16(event, 2, handle); 183ccf076adS[email protected] bt_store_16(event, 4, result); 184ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 185ccf076adS[email protected] (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); 186ccf076adS[email protected] } 187ccf076adS[email protected] 1885842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ 189e0abb8e7S[email protected] log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); 1905842b6d9Smatthias.ringwald uint8_t event[5]; 1915842b6d9Smatthias.ringwald event[0] = L2CAP_EVENT_SERVICE_REGISTERED; 1925842b6d9Smatthias.ringwald event[1] = sizeof(event) - 2; 1935842b6d9Smatthias.ringwald event[2] = status; 1945842b6d9Smatthias.ringwald bt_store_16(event, 3, psm); 1955842b6d9Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 196e6f51008S[email protected] (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1975842b6d9Smatthias.ringwald } 1985842b6d9Smatthias.ringwald 1996218e6f1Smatthias.ringwald void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 200e0abb8e7S[email protected] 201e0abb8e7S[email protected] log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, credits); 2026218e6f1Smatthias.ringwald // track credits 2036218e6f1Smatthias.ringwald channel->packets_granted += credits; 2046218e6f1Smatthias.ringwald 2056218e6f1Smatthias.ringwald uint8_t event[5]; 2066218e6f1Smatthias.ringwald event[0] = L2CAP_EVENT_CREDITS; 2076218e6f1Smatthias.ringwald event[1] = sizeof(event) - 2; 2086218e6f1Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 2096218e6f1Smatthias.ringwald event[4] = credits; 2106218e6f1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 2116218e6f1Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 2126218e6f1Smatthias.ringwald } 2136218e6f1Smatthias.ringwald 214808a48abSmatthias.ringwald void l2cap_block_new_credits(uint8_t blocked){ 215808a48abSmatthias.ringwald new_credits_blocked = blocked; 216808a48abSmatthias.ringwald } 217808a48abSmatthias.ringwald 21840d1c7a4Smatthias.ringwald void l2cap_hand_out_credits(void){ 219808a48abSmatthias.ringwald 220808a48abSmatthias.ringwald if (new_credits_blocked) return; // we're told not to. used by daemon 221808a48abSmatthias.ringwald 222c22aecc9S[email protected] linked_list_iterator_t it; 223c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 224c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 225c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 226e79abdd6S[email protected] if (!hci_number_free_acl_slots_for_handle(channel->handle)) return; 2270e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 2284c744e21Smatthias.ringwald if (hci_number_outgoing_packets(channel->handle) < NR_BUFFERED_ACL_PACKETS && channel->packets_granted == 0) { 2298d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 2308d371091Smatthias.ringwald } 2318d371091Smatthias.ringwald } 2328d371091Smatthias.ringwald } 2338d371091Smatthias.ringwald 234b35f641cSmatthias.ringwald l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 235c22aecc9S[email protected] linked_list_iterator_t it; 236c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 237c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 238c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 239b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 240f62db1e3Smatthias.ringwald return channel; 241f62db1e3Smatthias.ringwald } 242f62db1e3Smatthias.ringwald } 243f62db1e3Smatthias.ringwald return NULL; 244f62db1e3Smatthias.ringwald } 245f62db1e3Smatthias.ringwald 2466b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2476b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2486b1fde37Smatthias.ringwald if (!channel) return 0; 2496b1fde37Smatthias.ringwald if (!channel->packets_granted) return 0; 250a35252c8S[email protected] return hci_can_send_acl_packet_now(channel->handle); 2517856fb31S[email protected] } 2527856fb31S[email protected] 2536cd4da6bS[email protected] // @deprecated 2543cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){ 255a35252c8S[email protected] // TODO provide real handle 2566cd4da6bS[email protected] return l2cap_can_send_fixed_channel_packet_now(0x1234); 2576cd4da6bS[email protected] } 2586cd4da6bS[email protected] 2596cd4da6bS[email protected] int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 2606cd4da6bS[email protected] return hci_can_send_acl_packet_now(handle); 2613cab4fcaS[email protected] } 2623cab4fcaS[email protected] 26396cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 26496cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 26596cbd662Smatthias.ringwald if (channel) { 26696cbd662Smatthias.ringwald return channel->remote_mtu; 26796cbd662Smatthias.ringwald } 26896cbd662Smatthias.ringwald return 0; 26996cbd662Smatthias.ringwald } 27096cbd662Smatthias.ringwald 2715932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 272c22aecc9S[email protected] linked_list_iterator_t it; 273c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 274c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 275c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 2765932bd7cS[email protected] if ( &channel->rtx == ts) { 2775932bd7cS[email protected] return channel; 2785932bd7cS[email protected] } 2795932bd7cS[email protected] } 2805932bd7cS[email protected] return NULL; 2815932bd7cS[email protected] } 2825932bd7cS[email protected] 2835932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){ 2845932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2855932bd7cS[email protected] if (!ts) return; 2865932bd7cS[email protected] 2875932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2885932bd7cS[email protected] 2895932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2905932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2915932bd7cS[email protected] // notify client 2925932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2935932bd7cS[email protected] 2945932bd7cS[email protected] // discard channel 2959dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 2965932bd7cS[email protected] linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 2975932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 2985932bd7cS[email protected] } 2995932bd7cS[email protected] 3005932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 3015932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 3025932bd7cS[email protected] run_loop_remove_timer(&channel->rtx); 3035932bd7cS[email protected] } 3045932bd7cS[email protected] 3055932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 3065932bd7cS[email protected] l2cap_stop_rtx(channel); 307cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 3085932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3095932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 3105932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3115932bd7cS[email protected] } 3125932bd7cS[email protected] 3135932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3145932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3155932bd7cS[email protected] l2cap_stop_rtx(channel); 3165932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3175932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 3185932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3195932bd7cS[email protected] } 3205932bd7cS[email protected] 321*71de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 322ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 323ac301f95S[email protected] } 324ac301f95S[email protected] 325df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 326ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 327df3354fcS[email protected] } 3285932bd7cS[email protected] 32958de5610Smatthias.ringwald int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 330b1d43497Smatthias.ringwald 331a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3329da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 333b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 334b1d43497Smatthias.ringwald } 335b1d43497Smatthias.ringwald 3369da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3372a373862S[email protected] hci_reserve_packet_buffer(); 338facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 33958de5610Smatthias.ringwald va_list argptr; 34058de5610Smatthias.ringwald va_start(argptr, identifier); 34170efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 34258de5610Smatthias.ringwald va_end(argptr); 3439da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 344826f7347S[email protected] return hci_send_acl_packet_buffer(len); 34558de5610Smatthias.ringwald } 34658de5610Smatthias.ringwald 34770efece1S[email protected] #ifdef HAVE_BLE 34870efece1S[email protected] int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 34970efece1S[email protected] 350a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3519da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 35270efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 35370efece1S[email protected] } 35470efece1S[email protected] 3559da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3562a373862S[email protected] hci_reserve_packet_buffer(); 357facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 35870efece1S[email protected] va_list argptr; 35970efece1S[email protected] va_start(argptr, identifier); 36070efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 36170efece1S[email protected] va_end(argptr); 3629da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 363826f7347S[email protected] return hci_send_acl_packet_buffer(len); 36470efece1S[email protected] } 36570efece1S[email protected] #endif 36670efece1S[email protected] 367b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 368facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 369b1d43497Smatthias.ringwald } 3706218e6f1Smatthias.ringwald 3716b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3726b4af23dS[email protected] return hci_reserve_packet_buffer(); 3736b4af23dS[email protected] } 3746b4af23dS[email protected] 37568a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 37668a0fcf7S[email protected] hci_release_packet_buffer(); 37768a0fcf7S[email protected] } 37868a0fcf7S[email protected] 37968a0fcf7S[email protected] 380b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 381b1d43497Smatthias.ringwald 382c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 383c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 384c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 385c8b9416aS[email protected] } 386c8b9416aS[email protected] 38758de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 388b1d43497Smatthias.ringwald if (!channel) { 3899da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 390b1d43497Smatthias.ringwald return -1; // TODO: define error 3916218e6f1Smatthias.ringwald } 3926218e6f1Smatthias.ringwald 393b1d43497Smatthias.ringwald if (channel->packets_granted == 0){ 3949da54300S[email protected] log_error("l2cap_send_prepared cid 0x%02x, no credits!", local_cid); 395b1d43497Smatthias.ringwald return -1; // TODO: define error 396b1d43497Smatthias.ringwald } 397b1d43497Smatthias.ringwald 398a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 3999da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 400a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 401a35252c8S[email protected] } 402a35252c8S[email protected] 403b1d43497Smatthias.ringwald --channel->packets_granted; 404b1d43497Smatthias.ringwald 4059da54300S[email protected] log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used, credits left %u;", 406b1d43497Smatthias.ringwald local_cid, channel->handle, channel->packets_granted); 407b1d43497Smatthias.ringwald 408facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 409b1d43497Smatthias.ringwald 410e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 411e9772277S[email protected] 412e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 413e9772277S[email protected] bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 41458de5610Smatthias.ringwald // 2 - ACL length 41558de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 41658de5610Smatthias.ringwald // 4 - L2CAP packet length 41758de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 41858de5610Smatthias.ringwald // 6 - L2CAP channel DEST 41958de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 42058de5610Smatthias.ringwald // send 421826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 42291b99603Smatthias.ringwald 42391b99603Smatthias.ringwald l2cap_hand_out_credits(); 42491b99603Smatthias.ringwald 4256218e6f1Smatthias.ringwald return err; 42658de5610Smatthias.ringwald } 42758de5610Smatthias.ringwald 4282149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 4292149f12eSmatthias.ringwald 430c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 431c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4322149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4332149f12eSmatthias.ringwald } 4342149f12eSmatthias.ringwald 435a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(handle)){ 4369da54300S[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 437c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 438c8b9416aS[email protected] } 439c8b9416aS[email protected] 4409da54300S[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 4412149f12eSmatthias.ringwald 442facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4432149f12eSmatthias.ringwald 444e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 445e9772277S[email protected] 446e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 447e9772277S[email protected] bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 4482149f12eSmatthias.ringwald // 2 - ACL length 4492149f12eSmatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 4502149f12eSmatthias.ringwald // 4 - L2CAP packet length 4512149f12eSmatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 4522149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 4532149f12eSmatthias.ringwald bt_store_16(acl_buffer, 6, cid); 4542149f12eSmatthias.ringwald // send 455826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4562149f12eSmatthias.ringwald 4572149f12eSmatthias.ringwald l2cap_hand_out_credits(); 4582149f12eSmatthias.ringwald 4592149f12eSmatthias.ringwald return err; 4602149f12eSmatthias.ringwald } 4612149f12eSmatthias.ringwald 462b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 463b1d43497Smatthias.ringwald 464a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 465a35252c8S[email protected] if (!channel) { 4669da54300S[email protected] log_error("l2cap_send_internal no channel for cid 0x%02x", local_cid); 467a35252c8S[email protected] return -1; // TODO: define error 468a35252c8S[email protected] } 469a35252c8S[email protected] 470f0efaa57S[email protected] if (len > channel->remote_mtu){ 471f0efaa57S[email protected] log_error("l2cap_send_internal cid 0x%02x, data length exceeds remote MTU.", local_cid); 472f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 473f0efaa57S[email protected] } 474f0efaa57S[email protected] 475a35252c8S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)){ 4769da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", local_cid); 477b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 478b1d43497Smatthias.ringwald } 479b1d43497Smatthias.ringwald 4802a373862S[email protected] hci_reserve_packet_buffer(); 481facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 482b1d43497Smatthias.ringwald 483b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 484b1d43497Smatthias.ringwald 485b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 486b1d43497Smatthias.ringwald } 487b1d43497Smatthias.ringwald 4882149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 4892149f12eSmatthias.ringwald 490a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4919da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", cid); 4922149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4932149f12eSmatthias.ringwald } 4942149f12eSmatthias.ringwald 4952a373862S[email protected] hci_reserve_packet_buffer(); 496facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4972149f12eSmatthias.ringwald 4982149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 4992149f12eSmatthias.ringwald 5002149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 5012149f12eSmatthias.ringwald } 5022149f12eSmatthias.ringwald 5030e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 5040e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 5050e37e417S[email protected] } 5060e37e417S[email protected] 50728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 50828ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 50928ca2b46S[email protected] } 51028ca2b46S[email protected] 51128ca2b46S[email protected] static inline void channelStateVarClearFlag(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] 516b1d43497Smatthias.ringwald 5178158c421Smatthias.ringwald // MARK: L2CAP_RUN 5182cd0be45Smatthias.ringwald // process outstanding signaling tasks 5192cd0be45Smatthias.ringwald void l2cap_run(void){ 5202b83fb7dSmatthias.ringwald 52122c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 52222c29ab4SMatthias Ringwald 5232b83fb7dSmatthias.ringwald // check pending signaling responses 5242b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5252b83fb7dSmatthias.ringwald 5262b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 527a35252c8S[email protected] 528a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 529a35252c8S[email protected] 5302b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5312b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 53263a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 533f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5342b83fb7dSmatthias.ringwald 535f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 536f53da564S[email protected] signaling_responses_pending--; 537f53da564S[email protected] int i; 538f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 539f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 540f53da564S[email protected] } 541f53da564S[email protected] 542f53da564S[email protected] switch (response_code){ 5432b360848Smatthias.ringwald case CONNECTION_REQUEST: 5442b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5452bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5464d816277S[email protected] if (result == 0x0003){ 5472bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5484d816277S[email protected] } 5492b360848Smatthias.ringwald break; 5502b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5512b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5522b83fb7dSmatthias.ringwald break; 5532b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5543b0484b3S[email protected] switch (infoType){ 5553b0484b3S[email protected] case 1: { // Connectionless MTU 5563b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5573b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5583b0484b3S[email protected] break; 5593b0484b3S[email protected] } 5603b0484b3S[email protected] case 2: { // Extended Features Supported 561462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 562462e630dS[email protected] uint32_t features = 0x280; 5633b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5643b0484b3S[email protected] break; 5653b0484b3S[email protected] } 5663b0484b3S[email protected] case 3: { // Fixed Channels Supported 5673b0484b3S[email protected] uint8_t map[8]; 5683b0484b3S[email protected] memset(map, 0, 8); 569462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5703b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5713b0484b3S[email protected] break; 5723b0484b3S[email protected] } 5733b0484b3S[email protected] default: 5742b83fb7dSmatthias.ringwald // all other types are not supported 5752b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5763b0484b3S[email protected] break; 5772b83fb7dSmatthias.ringwald } 5782b83fb7dSmatthias.ringwald break; 57963a7246aSmatthias.ringwald case COMMAND_REJECT: 5805ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 58170efece1S[email protected] #ifdef HAVE_BLE 58270efece1S[email protected] case COMMAND_REJECT_LE: 58370efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 58463a7246aSmatthias.ringwald break; 58570efece1S[email protected] #endif 5862b83fb7dSmatthias.ringwald default: 5872b83fb7dSmatthias.ringwald // should not happen 5882b83fb7dSmatthias.ringwald break; 5892b83fb7dSmatthias.ringwald } 5902b83fb7dSmatthias.ringwald } 5912b83fb7dSmatthias.ringwald 592ae280e73Smatthias.ringwald uint8_t config_options[4]; 593c22aecc9S[email protected] linked_list_iterator_t it; 594c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 595c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 596baf94f06S[email protected] 597c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 59822c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 5992cd0be45Smatthias.ringwald switch (channel->state){ 6002cd0be45Smatthias.ringwald 601df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 602ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 603baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 604a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 605ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 606a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 607ad671560S[email protected] } 608ad671560S[email protected] break; 609ad671560S[email protected] 61002b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 611baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 61264472d52Smatthias.ringwald // send connection request - set state first 61364472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 61402b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6158f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 61602b22dc4Smatthias.ringwald break; 61702b22dc4Smatthias.ringwald 618e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 619baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 62022c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 6211eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 622e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6239dcb2fb2S[email protected] l2cap_stop_rtx(channel); 624c22aecc9S[email protected] linked_list_iterator_remove(&it); 625d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 626e7ff783cSmatthias.ringwald break; 627e7ff783cSmatthias.ringwald 628552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 629baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 630fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 63128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 6322a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 633552d92a1Smatthias.ringwald break; 634552d92a1Smatthias.ringwald 6356fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 636baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 6376fdcc387Smatthias.ringwald // success, start l2cap handshake 638b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6396fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 6402a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6415932bd7cS[email protected] l2cap_start_rtx(channel); 6426fdcc387Smatthias.ringwald break; 6436fdcc387Smatthias.ringwald 644fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 645baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 64673cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 64763a7246aSmatthias.ringwald uint16_t flags = 0; 64828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 64963a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 65063a7246aSmatthias.ringwald flags = 1; 65163a7246aSmatthias.ringwald } else { 65228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 65363a7246aSmatthias.ringwald } 65463a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 65563a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 65663a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 65763a7246aSmatthias.ringwald config_options[0] = 1; // MTU 65863a7246aSmatthias.ringwald config_options[1] = 2; // len param 65963a7246aSmatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 66063a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 66163a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 66263a7246aSmatthias.ringwald } else { 66363a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 66463a7246aSmatthias.ringwald } 66563a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 666fa8473a4Smatthias.ringwald } 66773cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 66828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 66928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 670b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 671ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 672ae280e73Smatthias.ringwald config_options[1] = 2; // len param 673ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 674b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6755932bd7cS[email protected] l2cap_start_rtx(channel); 676fa8473a4Smatthias.ringwald } 677fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 678552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 679552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 680552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 681fa8473a4Smatthias.ringwald } 682552d92a1Smatthias.ringwald break; 683552d92a1Smatthias.ringwald 684e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 685baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 68622c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 687b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6885932bd7cS[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 :) 689756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 690e7ff783cSmatthias.ringwald break; 691e7ff783cSmatthias.ringwald 692e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 693baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 694b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6952cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 6962a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 6972cd0be45Smatthias.ringwald break; 6982cd0be45Smatthias.ringwald default: 6992cd0be45Smatthias.ringwald break; 7002cd0be45Smatthias.ringwald } 7012cd0be45Smatthias.ringwald } 702da886c03S[email protected] 7034d7157c3S[email protected] #ifdef HAVE_BLE 704da886c03S[email protected] // send l2cap con paramter update if necessary 705da886c03S[email protected] hci_connections_get_iterator(&it); 706da886c03S[email protected] while(linked_list_iterator_has_next(&it)){ 707da886c03S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it); 708da886c03S[email protected] int result; 709da886c03S[email protected] 710da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 711da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 712da886c03S[email protected] result = 0; 713da886c03S[email protected] break; 714da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 715da886c03S[email protected] result = 1; 716da886c03S[email protected] break; 717da886c03S[email protected] default: 718da886c03S[email protected] result = -1; 719da886c03S[email protected] break; 720da886c03S[email protected] } 721da886c03S[email protected] if (result < 0) break; 722da886c03S[email protected] 723da886c03S[email protected] if (!hci_can_send_acl_packet_now(connection->con_handle)) break; 724da886c03S[email protected] hci_reserve_packet_buffer(); 725da886c03S[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 726da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 727da886c03S[email protected] uint16_t len = l2cap_le_create_connection_parameter_update_response(acl_buffer, connection->con_handle, 0); 728da886c03S[email protected] hci_send_acl_packet_buffer(len); 729da886c03S[email protected] } 7304d7157c3S[email protected] #endif 731da886c03S[email protected] 73222c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 7332cd0be45Smatthias.ringwald } 7342cd0be45Smatthias.ringwald 7354aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 7364ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 737fa8c92f6Smatthias.ringwald } 738fa8c92f6Smatthias.ringwald 739*71de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 7404ff786cfS[email protected] return l2cap_max_mtu(); 741e5e1518dS[email protected] } 742e5e1518dS[email protected] 7432df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 7442df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 7455533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 7462df5dadcS[email protected] // success, start l2cap handshake 7472df5dadcS[email protected] channel->handle = handle; 7482df5dadcS[email protected] channel->local_cid = l2cap_next_local_cid(); 7492df5dadcS[email protected] // check remote SSP feature first 7502df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 7512df5dadcS[email protected] } 7522df5dadcS[email protected] } 7532df5dadcS[email protected] 7542df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 7552df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 7562df5dadcS[email protected] 7572df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 758ac301f95S[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)); 7592df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 7602df5dadcS[email protected] // request security level 2 7612df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 7621429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 7632df5dadcS[email protected] return; 7642df5dadcS[email protected] } 7652df5dadcS[email protected] // fine, go ahead 7662df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7672df5dadcS[email protected] } 7682df5dadcS[email protected] 7691e6aba47Smatthias.ringwald // open outgoing L2CAP channel 77015470d27Smatthias.ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t packet_handler, 77115470d27Smatthias.ringwald bd_addr_t address, uint16_t psm, uint16_t mtu){ 7721e6aba47Smatthias.ringwald 773e0abb8e7S[email protected] log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 774e0abb8e7S[email protected] 7751e6aba47Smatthias.ringwald // alloc structure 776bb69aaaeS[email protected] l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 7772b360848Smatthias.ringwald if (!chan) { 7782b360848Smatthias.ringwald // emit error event 7792b360848Smatthias.ringwald l2cap_channel_t dummy_channel; 7802b360848Smatthias.ringwald BD_ADDR_COPY(dummy_channel.address, address); 7812b360848Smatthias.ringwald dummy_channel.psm = psm; 7822b360848Smatthias.ringwald l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 7832b360848Smatthias.ringwald return; 7842b360848Smatthias.ringwald } 785c523d53dS[email protected] // Init memory (make valgrind happy) 786c523d53dS[email protected] memset(chan, 0, sizeof(l2cap_channel_t)); 787f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 7882985cb84Smatthias.ringwald if (mtu > l2cap_max_mtu()) { 7892985cb84Smatthias.ringwald mtu = l2cap_max_mtu(); 7909775e25bSmatthias.ringwald } 7919775e25bSmatthias.ringwald 7921e6aba47Smatthias.ringwald // fill in 7931e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 7941e6aba47Smatthias.ringwald chan->psm = psm; 7951e6aba47Smatthias.ringwald chan->handle = 0; 7961e6aba47Smatthias.ringwald chan->connection = connection; 7976b296a27Smatthias.ringwald chan->packet_handler = packet_handler; 7982784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 79915470d27Smatthias.ringwald chan->local_mtu = mtu; 8006218e6f1Smatthias.ringwald chan->packets_granted = 0; 8016218e6f1Smatthias.ringwald 8021e6aba47Smatthias.ringwald // set initial state 80302b22dc4Smatthias.ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 80473cf2b3dSmatthias.ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 805b1988dceSmatthias.ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 806b1988dceSmatthias.ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 8075533f01eS[email protected] chan->required_security_level = LEVEL_0; 8081e6aba47Smatthias.ringwald 8091e6aba47Smatthias.ringwald // add to connections list 8101e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 8111e6aba47Smatthias.ringwald 8122df5dadcS[email protected] // check if hci connection is already usable 8132e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 8142df5dadcS[email protected] if (conn){ 8155533f01eS[email protected] log_info("l2cap_create_channel_internal, hci connection already exists"); 8162df5dadcS[email protected] l2cap_handle_connection_complete(conn->con_handle, chan); 8172df5dadcS[email protected] // check ir remote supported fearures are already received 8182df5dadcS[email protected] if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 8192df5dadcS[email protected] l2cap_handle_remote_supported_features_received(chan); 8202df5dadcS[email protected] } 8212df5dadcS[email protected] } 8222df5dadcS[email protected] 82302b22dc4Smatthias.ringwald l2cap_run(); 82443625864Smatthias.ringwald } 82543625864Smatthias.ringwald 826b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 827e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 828b35f641cSmatthias.ringwald // find channel for local_cid 829b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 830f62db1e3Smatthias.ringwald if (channel) { 831e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 832f62db1e3Smatthias.ringwald } 8332cd0be45Smatthias.ringwald // process 8342cd0be45Smatthias.ringwald l2cap_run(); 83543625864Smatthias.ringwald } 8361e6aba47Smatthias.ringwald 837afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 838c22aecc9S[email protected] linked_list_iterator_t it; 839c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 840c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 841c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 842c22aecc9S[email protected] if ( BD_ADDR_CMP( channel->address, address) != 0) continue; 843c22aecc9S[email protected] // channel for this address found 844c22aecc9S[email protected] switch (channel->state){ 845c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 846c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 847afde0c52Smatthias.ringwald // failure, forward error code 848afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 849afde0c52Smatthias.ringwald // discard channel 8509dcb2fb2S[email protected] l2cap_stop_rtx(channel); 851c22aecc9S[email protected] linked_list_iterator_remove(&it); 852d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 853c22aecc9S[email protected] break; 854c22aecc9S[email protected] default: 855c22aecc9S[email protected] break; 856afde0c52Smatthias.ringwald } 857afde0c52Smatthias.ringwald } 858afde0c52Smatthias.ringwald } 859afde0c52Smatthias.ringwald 860afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 861c22aecc9S[email protected] linked_list_iterator_t it; 862c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 863c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 864c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 865afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 8662df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 867afde0c52Smatthias.ringwald } 868afde0c52Smatthias.ringwald } 8696fdcc387Smatthias.ringwald // process 8706fdcc387Smatthias.ringwald l2cap_run(); 871afde0c52Smatthias.ringwald } 872b448a0e7Smatthias.ringwald 873afde0c52Smatthias.ringwald void l2cap_event_handler(uint8_t *packet, uint16_t size){ 874afde0c52Smatthias.ringwald 875afde0c52Smatthias.ringwald bd_addr_t address; 876afde0c52Smatthias.ringwald hci_con_handle_t handle; 877c22aecc9S[email protected] linked_list_iterator_t it; 8782d00edd4Smatthias.ringwald int hci_con_used; 879afde0c52Smatthias.ringwald 880afde0c52Smatthias.ringwald switch(packet[0]){ 881afde0c52Smatthias.ringwald 882afde0c52Smatthias.ringwald // handle connection complete events 883afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 884afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 885afde0c52Smatthias.ringwald if (packet[2] == 0){ 886afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 887afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 888afde0c52Smatthias.ringwald } else { 889afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 890afde0c52Smatthias.ringwald } 891afde0c52Smatthias.ringwald break; 892afde0c52Smatthias.ringwald 893afde0c52Smatthias.ringwald // handle successful create connection cancel command 894afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 895afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 896afde0c52Smatthias.ringwald if (packet[5] == 0){ 897afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 898afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 899afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 90003cfbabcSmatthias.ringwald } 9011e6aba47Smatthias.ringwald } 90239d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 90339d59809Smatthias.ringwald break; 90439d59809Smatthias.ringwald 90539d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 90639d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 907afde0c52Smatthias.ringwald break; 90827a923d0Smatthias.ringwald 9091e6aba47Smatthias.ringwald // handle disconnection complete events 910afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 911c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 912afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 913c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 914c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 915c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 916c22aecc9S[email protected] if (channel->handle != handle) continue; 91715ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 9189dcb2fb2S[email protected] l2cap_stop_rtx(channel); 919c22aecc9S[email protected] linked_list_iterator_remove(&it); 920d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 92127a923d0Smatthias.ringwald } 922afde0c52Smatthias.ringwald break; 923fcadd0caSmatthias.ringwald 9246218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 92502b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 9268d371091Smatthias.ringwald l2cap_hand_out_credits(); 9276218e6f1Smatthias.ringwald break; 9286218e6f1Smatthias.ringwald 929ee091cf1Smatthias.ringwald // HCI Connection Timeouts 930afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 93136944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 93280ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 9332d00edd4Smatthias.ringwald hci_con_used = 0; 934c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 935c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 936c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 937c22aecc9S[email protected] if (channel->handle != handle) continue; 9382d00edd4Smatthias.ringwald hci_con_used = 1; 939c22aecc9S[email protected] break; 940ee091cf1Smatthias.ringwald } 9412d00edd4Smatthias.ringwald if (hci_con_used) break; 942d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 9439edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 944afde0c52Smatthias.ringwald break; 945ee091cf1Smatthias.ringwald 9466e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 947c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 948c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 949c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 950c22aecc9S[email protected] if (!channel->packet_handler) continue; 9516e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 9526e6710ebSmatthias.ringwald } 9535652b5ffS[email protected] if (attribute_protocol_packet_handler) { 9545652b5ffS[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9555652b5ffS[email protected] } 9565652b5ffS[email protected] if (security_protocol_packet_handler) { 957133efcfdSmatthias.ringwald (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9585652b5ffS[email protected] } 959462e630dS[email protected] if (connectionless_channel_packet_handler) { 960462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 961462e630dS[email protected] } 9626e6710ebSmatthias.ringwald break; 9636e6710ebSmatthias.ringwald 964df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 965df3354fcS[email protected] handle = READ_BT_16(packet, 3); 966c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 967c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 968c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 969df3354fcS[email protected] if (channel->handle != handle) continue; 9702df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 971df3354fcS[email protected] break; 972df3354fcS[email protected] } 973c22aecc9S[email protected] break; 974df3354fcS[email protected] 975a00031e2S[email protected] case GAP_SECURITY_LEVEL: 976a00031e2S[email protected] handle = READ_BT_16(packet, 2); 977bd63148eS[email protected] log_info("l2cap - security level update"); 978c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 979c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 980c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 981f85a9399S[email protected] if (channel->handle != handle) continue; 9825533f01eS[email protected] 983bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 984bd63148eS[email protected] 9855533f01eS[email protected] gap_security_level_t actual_level = packet[4]; 9865533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 9875533f01eS[email protected] 988df3354fcS[email protected] switch (channel->state){ 989df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 9905533f01eS[email protected] if (actual_level >= required_level){ 991f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 992f85a9399S[email protected] l2cap_emit_connection_request(channel); 9931eb2563eS[email protected] } else { 9941eb2563eS[email protected] channel->reason = 0x03; // security block 9951eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 9961eb2563eS[email protected] } 997df3354fcS[email protected] break; 998df3354fcS[email protected] 999df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 10005533f01eS[email protected] if (actual_level >= required_level){ 1001df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1002df3354fcS[email protected] } else { 1003df3354fcS[email protected] // disconnnect, authentication not good enough 1004df3354fcS[email protected] hci_disconnect_security_block(handle); 1005df3354fcS[email protected] } 1006df3354fcS[email protected] break; 1007df3354fcS[email protected] 1008df3354fcS[email protected] default: 1009df3354fcS[email protected] break; 1010df3354fcS[email protected] } 1011f85a9399S[email protected] } 1012f85a9399S[email protected] break; 1013f85a9399S[email protected] 1014afde0c52Smatthias.ringwald default: 1015afde0c52Smatthias.ringwald break; 1016afde0c52Smatthias.ringwald } 1017afde0c52Smatthias.ringwald 1018e0707417S[email protected] // pass on: main packet handler, att and sm packet handlers 1019b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 1020e0707417S[email protected] if (attribute_protocol_packet_handler){ 1021e0707417S[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1022e0707417S[email protected] } 1023e0707417S[email protected] if (security_protocol_packet_handler) { 1024e0707417S[email protected] (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1025e0707417S[email protected] } 1026462e630dS[email protected] if (connectionless_channel_packet_handler) { 1027462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1028462e630dS[email protected] } 1029bd63148eS[email protected] 1030bd63148eS[email protected] l2cap_run(); 10311e6aba47Smatthias.ringwald } 10321e6aba47Smatthias.ringwald 1033afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1034b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1035e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1036e7ff783cSmatthias.ringwald l2cap_run(); 103784836b65Smatthias.ringwald } 103884836b65Smatthias.ringwald 10392b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 10404cf56b4aSmatthias.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." 10412b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 10422b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 10432b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 10442b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 10452b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 10462b360848Smatthias.ringwald signaling_responses_pending++; 10472b360848Smatthias.ringwald l2cap_run(); 10482b360848Smatthias.ringwald } 10492b360848Smatthias.ringwald } 10502b360848Smatthias.ringwald 1051b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1052645658c9Smatthias.ringwald 10539da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1054645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1055645658c9Smatthias.ringwald if (!service) { 1056645658c9Smatthias.ringwald // 0x0002 PSM not supported 10572b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1058645658c9Smatthias.ringwald return; 1059645658c9Smatthias.ringwald } 1060645658c9Smatthias.ringwald 10615061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1062645658c9Smatthias.ringwald if (!hci_connection) { 10632b360848Smatthias.ringwald // 10649da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1065645658c9Smatthias.ringwald return; 1066645658c9Smatthias.ringwald } 10672bd8b7e7S[email protected] 10682bd8b7e7S[email protected] // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP 1069b087afb5S[email protected] if ( hci_ssp_supported_on_both_sides(handle) 1070b087afb5S[email protected] && gap_security_level(handle) == LEVEL_0 1071b087afb5S[email protected] && !l2cap_security_level_0_allowed_for_PSM(psm)){ 10722bd8b7e7S[email protected] 10732bd8b7e7S[email protected] // 0x0003 Security Block 10742bd8b7e7S[email protected] l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003); 10752bd8b7e7S[email protected] return; 10762bd8b7e7S[email protected] } 10772bd8b7e7S[email protected] 10782bd8b7e7S[email protected] 1079645658c9Smatthias.ringwald // alloc structure 10809da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1081bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 10822b360848Smatthias.ringwald if (!channel){ 10832b360848Smatthias.ringwald // 0x0004 No resources available 10842b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 10852b360848Smatthias.ringwald return; 10862b360848Smatthias.ringwald } 1087c523d53dS[email protected] // Init memory (make valgrind happy) 1088c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1089645658c9Smatthias.ringwald // fill in 1090169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 1091169f8b28Smatthias.ringwald channel->psm = psm; 1092169f8b28Smatthias.ringwald channel->handle = handle; 1093169f8b28Smatthias.ringwald channel->connection = service->connection; 1094f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1095b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1096b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1097fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 10980a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1099761b0451Smatthias.ringwald channel->packets_granted = 0; 1100b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1101df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1102645658c9Smatthias.ringwald 1103f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 11042985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 11052985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 11069775e25bSmatthias.ringwald } 11079775e25bSmatthias.ringwald 1108645658c9Smatthias.ringwald // set initial state 1109df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1110ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1111e405ae81Smatthias.ringwald 1112645658c9Smatthias.ringwald // add to connections list 1113169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 1114645658c9Smatthias.ringwald 1115f85a9399S[email protected] // assert security requirements 11161eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1117e405ae81Smatthias.ringwald } 1118645658c9Smatthias.ringwald 1119b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 1120e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1121b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1122e405ae81Smatthias.ringwald if (!channel) { 11237d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 1124e405ae81Smatthias.ringwald return; 1125e405ae81Smatthias.ringwald } 1126e405ae81Smatthias.ringwald 1127552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1128e405ae81Smatthias.ringwald 1129552d92a1Smatthias.ringwald // process 1130552d92a1Smatthias.ringwald l2cap_run(); 1131e405ae81Smatthias.ringwald } 1132645658c9Smatthias.ringwald 1133b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 1134e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1135b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1136e405ae81Smatthias.ringwald if (!channel) { 11377d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 1138e405ae81Smatthias.ringwald return; 1139e405ae81Smatthias.ringwald } 1140e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1141e7ff783cSmatthias.ringwald channel->reason = reason; 1142e7ff783cSmatthias.ringwald l2cap_run(); 1143645658c9Smatthias.ringwald } 1144645658c9Smatthias.ringwald 11452784b77dSmatthias.ringwald void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1146b1988dceSmatthias.ringwald 1147b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1148b1988dceSmatthias.ringwald 114963a7246aSmatthias.ringwald uint16_t flags = READ_BT_16(command, 6); 115063a7246aSmatthias.ringwald if (flags & 1) { 115163a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 115263a7246aSmatthias.ringwald } 115363a7246aSmatthias.ringwald 11542784b77dSmatthias.ringwald // accept the other's configuration options 11553de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 11563de7c0caSmatthias.ringwald uint16_t pos = 8; 11573de7c0caSmatthias.ringwald while (pos < end_pos){ 115863a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 115963a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 116063a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 116163a7246aSmatthias.ringwald pos++; 11621dc511deSmatthias.ringwald uint8_t length = command[pos++]; 11631dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 116463a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 11651dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 11669da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 116763a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 116863a7246aSmatthias.ringwald } 11690fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 11700fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 11710fe7a9d0S[email protected] channel->flush_timeout = READ_BT_16(command, pos); 11720fe7a9d0S[email protected] } 117363a7246aSmatthias.ringwald // check for unknown options 117463a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1175c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 117663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 11771dc511deSmatthias.ringwald } 11781dc511deSmatthias.ringwald pos += length; 11791dc511deSmatthias.ringwald } 11802784b77dSmatthias.ringwald } 11812784b77dSmatthias.ringwald 1182fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 11839da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 118473cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 118573cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1186fa8473a4Smatthias.ringwald return 1; 1187fa8473a4Smatthias.ringwald } 1188fa8473a4Smatthias.ringwald 1189fa8473a4Smatthias.ringwald 119000d93d79Smatthias.ringwald void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 11911e6aba47Smatthias.ringwald 119200d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 119300d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 119438e5900eSmatthias.ringwald uint16_t result = 0; 11951e6aba47Smatthias.ringwald 11969da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1197b35f641cSmatthias.ringwald 11989a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 11999a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 12009a011532Smatthias.ringwald switch (channel->state){ 1201fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 12029a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 12032b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 12049a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 12059a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 12069a011532Smatthias.ringwald break; 12079a011532Smatthias.ringwald 12089a011532Smatthias.ringwald default: 12099a011532Smatthias.ringwald // ignore in other states 12109a011532Smatthias.ringwald break; 12119a011532Smatthias.ringwald } 12129a011532Smatthias.ringwald return; 12139a011532Smatthias.ringwald } 12149a011532Smatthias.ringwald 121556081214Smatthias.ringwald // @STATEMACHINE(l2cap) 12161e6aba47Smatthias.ringwald switch (channel->state) { 12171e6aba47Smatthias.ringwald 12181e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 12191e6aba47Smatthias.ringwald switch (code){ 12201e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 12215932bd7cS[email protected] l2cap_stop_rtx(channel); 122200d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 122338e5900eSmatthias.ringwald switch (result) { 122438e5900eSmatthias.ringwald case 0: 1225169f8b28Smatthias.ringwald // successful connection 122600d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1227fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 122828ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 122938e5900eSmatthias.ringwald break; 123038e5900eSmatthias.ringwald case 1: 12315932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 12325932bd7cS[email protected] l2cap_start_ertx(channel); 123338e5900eSmatthias.ringwald break; 123438e5900eSmatthias.ringwald default: 1235eb920dbeSmatthias.ringwald // channel closed 1236eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1237f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 123838e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1239eb920dbeSmatthias.ringwald 1240eb920dbeSmatthias.ringwald // drop link key if security block 1241eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 12422e77e513S[email protected] hci_drop_link_key_for_bd_addr(channel->address); 1243eb920dbeSmatthias.ringwald } 1244eb920dbeSmatthias.ringwald 1245eb920dbeSmatthias.ringwald // discard channel 1246eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1247d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 124838e5900eSmatthias.ringwald break; 12491e6aba47Smatthias.ringwald } 12501e6aba47Smatthias.ringwald break; 125138e5900eSmatthias.ringwald 125238e5900eSmatthias.ringwald default: 12531e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 125438e5900eSmatthias.ringwald break; 12551e6aba47Smatthias.ringwald } 12561e6aba47Smatthias.ringwald break; 12571e6aba47Smatthias.ringwald 1258fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1259fe9d8984S[email protected] result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1260ae280e73Smatthias.ringwald switch (code) { 1261ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 126228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1263ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 126463a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 126563a7246aSmatthias.ringwald // only done if continuation not set 126663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 126763a7246aSmatthias.ringwald } 1268ae280e73Smatthias.ringwald break; 12691e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 12705932bd7cS[email protected] l2cap_stop_rtx(channel); 12715932bd7cS[email protected] switch (result){ 12725932bd7cS[email protected] case 0: // success 12735932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 12745932bd7cS[email protected] break; 12755932bd7cS[email protected] case 4: // pending 12765932bd7cS[email protected] l2cap_start_ertx(channel); 12775932bd7cS[email protected] break; 12785932bd7cS[email protected] default: 1279fe9d8984S[email protected] // retry on negative result 1280fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1281fe9d8984S[email protected] break; 1282fe9d8984S[email protected] } 12835a67bd4aSmatthias.ringwald break; 12845a67bd4aSmatthias.ringwald default: 12855a67bd4aSmatthias.ringwald break; 12861e6aba47Smatthias.ringwald } 1287fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1288fa8473a4Smatthias.ringwald // for open: 12895a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1290fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 12916218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 1292c8e4258aSmatthias.ringwald } 1293c8e4258aSmatthias.ringwald break; 1294f62db1e3Smatthias.ringwald 1295f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1296f62db1e3Smatthias.ringwald switch (code) { 1297f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 129827a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 129927a923d0Smatthias.ringwald break; 13005a67bd4aSmatthias.ringwald default: 13015a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 13025a67bd4aSmatthias.ringwald break; 130327a923d0Smatthias.ringwald } 130427a923d0Smatthias.ringwald break; 130584836b65Smatthias.ringwald 130684836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 130784836b65Smatthias.ringwald // @TODO handle incoming requests 130884836b65Smatthias.ringwald break; 130984836b65Smatthias.ringwald 131084836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 131184836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 131284836b65Smatthias.ringwald break; 131310642e45Smatthias.ringwald default: 131410642e45Smatthias.ringwald break; 131527a923d0Smatthias.ringwald } 13169da54300S[email protected] // log_info("new state %u", channel->state); 131727a923d0Smatthias.ringwald } 131827a923d0Smatthias.ringwald 131900d93d79Smatthias.ringwald 132000d93d79Smatthias.ringwald void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 132100d93d79Smatthias.ringwald 132200d93d79Smatthias.ringwald // get code, signalind identifier and command len 132300d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 132400d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 132500d93d79Smatthias.ringwald 132600d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 132700d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 132863a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 132900d93d79Smatthias.ringwald return; 133000d93d79Smatthias.ringwald } 133100d93d79Smatthias.ringwald 133200d93d79Smatthias.ringwald // general commands without an assigned channel 133300d93d79Smatthias.ringwald switch(code) { 133400d93d79Smatthias.ringwald 133500d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 133600d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 133700d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 133800d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 13392b83fb7dSmatthias.ringwald return; 134000d93d79Smatthias.ringwald } 134100d93d79Smatthias.ringwald 13422b360848Smatthias.ringwald case ECHO_REQUEST: 13432b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 13442b83fb7dSmatthias.ringwald return; 134500d93d79Smatthias.ringwald 134600d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 134700d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 13482b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 13492b83fb7dSmatthias.ringwald return; 135000d93d79Smatthias.ringwald } 135100d93d79Smatthias.ringwald 135200d93d79Smatthias.ringwald default: 135300d93d79Smatthias.ringwald break; 135400d93d79Smatthias.ringwald } 135500d93d79Smatthias.ringwald 135600d93d79Smatthias.ringwald 135700d93d79Smatthias.ringwald // Get potential destination CID 135800d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 135900d93d79Smatthias.ringwald 136000d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1361c22aecc9S[email protected] linked_list_iterator_t it; 1362c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1363c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1364c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1365c22aecc9S[email protected] if (channel->handle != handle) continue; 136600d93d79Smatthias.ringwald if (code & 1) { 1367b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1368b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 136900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13704e32727eSmatthias.ringwald break; 137100d93d79Smatthias.ringwald } 137200d93d79Smatthias.ringwald } else { 1373b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 137400d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 137500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13764e32727eSmatthias.ringwald break; 137700d93d79Smatthias.ringwald } 137800d93d79Smatthias.ringwald } 137900d93d79Smatthias.ringwald } 138000d93d79Smatthias.ringwald } 138100d93d79Smatthias.ringwald 138200d93d79Smatthias.ringwald void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 138300d93d79Smatthias.ringwald 138400d93d79Smatthias.ringwald // Get Channel ID 138500d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 138600d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 138700d93d79Smatthias.ringwald 13885652b5ffS[email protected] switch (channel_id) { 13895652b5ffS[email protected] 13905652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 13915652b5ffS[email protected] 139200d93d79Smatthias.ringwald uint16_t command_offset = 8; 139300d93d79Smatthias.ringwald while (command_offset < size) { 139400d93d79Smatthias.ringwald 139500d93d79Smatthias.ringwald // handle signaling commands 139600d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 139700d93d79Smatthias.ringwald 139800d93d79Smatthias.ringwald // increment command_offset 139900d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 140000d93d79Smatthias.ringwald } 14015652b5ffS[email protected] break; 140200d93d79Smatthias.ringwald } 140300d93d79Smatthias.ringwald 14045652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 14055652b5ffS[email protected] if (attribute_protocol_packet_handler) { 14065652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14075652b5ffS[email protected] } 14085652b5ffS[email protected] break; 14095652b5ffS[email protected] 14105652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 14115652b5ffS[email protected] if (security_protocol_packet_handler) { 14125652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14135652b5ffS[email protected] } 14145652b5ffS[email protected] break; 14155652b5ffS[email protected] 1416462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1417462e630dS[email protected] if (connectionless_channel_packet_handler) { 1418462e630dS[email protected] (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1419462e630dS[email protected] } 1420462e630dS[email protected] break; 1421462e630dS[email protected] 14221bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1423ccf076adS[email protected] switch (packet[8]){ 1424ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1425ccf076adS[email protected] uint16_t result = READ_BT_16(packet, 12); 1426ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1427ccf076adS[email protected] break; 1428ccf076adS[email protected] } 1429ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1430ccf076adS[email protected] uint8_t event[10]; 1431ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1432ccf076adS[email protected] event[1] = 8; 1433ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1434da886c03S[email protected] 1435da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1436da886c03S[email protected] if (connection){ 1437da886c03S[email protected] int update_parameter = 1; 1438da886c03S[email protected] le_connection_parameter_range_t existing_range = gap_le_get_connection_parameter_range(); 1439da886c03S[email protected] uint16_t le_conn_interval_min = READ_BT_16(packet,12); 1440da886c03S[email protected] uint16_t le_conn_interval_max = READ_BT_16(packet,14); 1441da886c03S[email protected] uint16_t le_conn_latency = READ_BT_16(packet,16); 1442da886c03S[email protected] uint16_t le_supervision_timeout = READ_BT_16(packet,18); 1443da886c03S[email protected] 1444da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1445da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1446da886c03S[email protected] 1447da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1448da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1449da886c03S[email protected] 1450da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1451da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1452da886c03S[email protected] 1453da886c03S[email protected] if (update_parameter){ 1454da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1455da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1456da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1457da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1458da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1459da886c03S[email protected] } else { 1460da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1461da886c03S[email protected] } 1462da886c03S[email protected] } 1463da886c03S[email protected] 1464ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1465ccf076adS[email protected] (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1466da886c03S[email protected] 1467ccf076adS[email protected] break; 1468ccf076adS[email protected] } 1469ccf076adS[email protected] default: { 14701bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14711bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14721bbc0b23S[email protected] break; 14731bbc0b23S[email protected] } 1474ccf076adS[email protected] } 1475ccf076adS[email protected] break; 1476ccf076adS[email protected] } 14771bbc0b23S[email protected] 14785652b5ffS[email protected] default: { 147900d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 148000d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 148100d93d79Smatthias.ringwald if (channel) { 148258de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 148300d93d79Smatthias.ringwald } 14845652b5ffS[email protected] break; 14855652b5ffS[email protected] } 14865652b5ffS[email protected] } 148700d93d79Smatthias.ringwald } 148800d93d79Smatthias.ringwald 14892718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14902718e2e7Smatthias.ringwald switch (packet_type) { 14912718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 14922718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 14932718e2e7Smatthias.ringwald break; 14942718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 14952718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 14962718e2e7Smatthias.ringwald break; 14972718e2e7Smatthias.ringwald default: 14982718e2e7Smatthias.ringwald break; 14992718e2e7Smatthias.ringwald } 15001eb2563eS[email protected] l2cap_run(); 15012718e2e7Smatthias.ringwald } 150200d93d79Smatthias.ringwald 150315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 150427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1505f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1506f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1507f62db1e3Smatthias.ringwald // discard channel 15089dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1509f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1510d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1511c8e4258aSmatthias.ringwald } 15121e6aba47Smatthias.ringwald 15139d9bbc01Smatthias.ringwald l2cap_service_t * l2cap_get_service(uint16_t psm){ 1514c22aecc9S[email protected] linked_list_iterator_t it; 1515c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_services); 1516c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1517c22aecc9S[email protected] l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it); 15189d9bbc01Smatthias.ringwald if ( service->psm == psm){ 15199d9bbc01Smatthias.ringwald return service; 15209d9bbc01Smatthias.ringwald }; 15219d9bbc01Smatthias.ringwald } 15229d9bbc01Smatthias.ringwald return NULL; 15239d9bbc01Smatthias.ringwald } 15249d9bbc01Smatthias.ringwald 152562f901dfS[email protected] void l2cap_register_service_internal(void *connection, btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1526e0abb8e7S[email protected] 1527e6f51008S[email protected] log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 1528e0abb8e7S[email protected] 15294bb582b6Smatthias.ringwald // check for alread registered psm 15304bb582b6Smatthias.ringwald // TODO: emit error event 15319d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1532277abc2cSmatthias.ringwald if (service) { 15339da54300S[email protected] log_error("l2cap_register_service_internal: PSM %u already registered", psm); 153481476041Smatthias.ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 1535277abc2cSmatthias.ringwald return; 1536277abc2cSmatthias.ringwald } 15379d9bbc01Smatthias.ringwald 15384bb582b6Smatthias.ringwald // alloc structure 15394bb582b6Smatthias.ringwald // TODO: emit error event 1540bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1541277abc2cSmatthias.ringwald if (!service) { 15429da54300S[email protected] log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 15435842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 1544277abc2cSmatthias.ringwald return; 1545277abc2cSmatthias.ringwald } 15469d9bbc01Smatthias.ringwald 15479d9bbc01Smatthias.ringwald // fill in 15489d9bbc01Smatthias.ringwald service->psm = psm; 15499d9bbc01Smatthias.ringwald service->mtu = mtu; 15509d9bbc01Smatthias.ringwald service->connection = connection; 1551d8497f19Smatthias.ringwald service->packet_handler = packet_handler; 1552df3354fcS[email protected] service->required_security_level = security_level; 15539d9bbc01Smatthias.ringwald 15549d9bbc01Smatthias.ringwald // add to services list 15559d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 1556c0e866bfSmatthias.ringwald 1557c0e866bfSmatthias.ringwald // enable page scan 1558c0e866bfSmatthias.ringwald hci_connectable_control(1); 15595842b6d9Smatthias.ringwald 15605842b6d9Smatthias.ringwald // done 15615842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, 0, psm); 15629d9bbc01Smatthias.ringwald } 15639d9bbc01Smatthias.ringwald 156436944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 1565e0abb8e7S[email protected] 1566e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1567e0abb8e7S[email protected] 15689d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1569037d6e48Smatthias.ringwald if (!service) return; 15709d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 1571d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1572c0e866bfSmatthias.ringwald 1573c0e866bfSmatthias.ringwald // disable page scan when no services registered 1574c0e866bfSmatthias.ringwald if (!linked_list_empty(&l2cap_services)) return; 1575c0e866bfSmatthias.ringwald hci_connectable_control(0); 15769d9bbc01Smatthias.ringwald } 15779d9bbc01Smatthias.ringwald 15785652b5ffS[email protected] 15795652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 15805652b5ffS[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 15815652b5ffS[email protected] switch(channel_id){ 15825652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 15835652b5ffS[email protected] attribute_protocol_packet_handler = packet_handler; 15845652b5ffS[email protected] break; 15855652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 15865652b5ffS[email protected] security_protocol_packet_handler = packet_handler; 15875652b5ffS[email protected] break; 1588462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1589462e630dS[email protected] connectionless_channel_packet_handler = packet_handler; 1590462e630dS[email protected] break; 15915652b5ffS[email protected] } 15925652b5ffS[email protected] } 15935652b5ffS[email protected] 1594ab2b01dcS[email protected] #ifdef HAVE_BLE 1595da886c03S[email protected] 1596ab2b01dcS[email protected] // Request LE connection parameter update 1597ab2b01dcS[email protected] int l2cap_le_request_connection_parameter_update(uint16_t handle, uint16_t interval_min, uint16_t interval_max, uint16_t slave_latency, uint16_t timeout_multiplier){ 1598a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 15999da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 1600ab2b01dcS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 1601ab2b01dcS[email protected] } 16029da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 1603826f7347S[email protected] hci_reserve_packet_buffer(); 1604facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 1605ab2b01dcS[email protected] uint16_t len = l2cap_le_create_connection_parameter_update_request(acl_buffer, handle, interval_min, interval_max, slave_latency, timeout_multiplier); 1606826f7347S[email protected] return hci_send_acl_packet_buffer(len); 1607ab2b01dcS[email protected] } 1608ab2b01dcS[email protected] #endif 1609ab2b01dcS[email protected] 1610