143625864Smatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 381713bceaSmatthias.ringwald /* 3943625864Smatthias.ringwald * l2cap.c 4043625864Smatthias.ringwald * 4143625864Smatthias.ringwald * Logical Link Control and Adaption Protocl (L2CAP) 4243625864Smatthias.ringwald * 4343625864Smatthias.ringwald * Created by Matthias Ringwald on 5/16/09. 4443625864Smatthias.ringwald */ 4543625864Smatthias.ringwald 4643625864Smatthias.ringwald #include "l2cap.h" 47645658c9Smatthias.ringwald #include "hci.h" 482b3c6c9bSmatthias.ringwald #include "hci_dump.h" 496218e6f1Smatthias.ringwald #include "debug.h" 50d3a9df87Smatthias.ringwald #include "btstack_memory.h" 5143625864Smatthias.ringwald 5243625864Smatthias.ringwald #include <stdarg.h> 5343625864Smatthias.ringwald #include <string.h> 5443625864Smatthias.ringwald 5543625864Smatthias.ringwald #include <stdio.h> 5643625864Smatthias.ringwald 574c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 584c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 594c744e21Smatthias.ringwald 6039bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 61e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 6239bda6d5Smatthias.ringwald 6300d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6800d93d79Smatthias.ringwald 6939bda6d5Smatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 7039bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 7139bda6d5Smatthias.ringwald 7239bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 732b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 742b83fb7dSmatthias.ringwald static int signaling_responses_pending; 752b83fb7dSmatthias.ringwald 76c42e2ff2S[email protected] static linked_list_t l2cap_channels; 77c42e2ff2S[email protected] static linked_list_t l2cap_services; 787192e786SMatthias Ringwald static linked_list_t l2cap_le_channels; 797192e786SMatthias Ringwald static linked_list_t l2cap_le_services; 8036944dffSmatthias.ringwald static void (*packet_handler) (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = null_packet_handler; 811e6aba47Smatthias.ringwald 82c42e2ff2S[email protected] static btstack_packet_handler_t attribute_protocol_packet_handler; 83c42e2ff2S[email protected] static btstack_packet_handler_t security_protocol_packet_handler; 84462e630dS[email protected] static btstack_packet_handler_t connectionless_channel_packet_handler; 85ac301f95S[email protected] static uint8_t require_security_level2_for_outgoing_sdp; 865652b5ffS[email protected] 8739bda6d5Smatthias.ringwald // prototypes 88fa8473a4Smatthias.ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 899c9876e7SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 90fa8473a4Smatthias.ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 91fa8473a4Smatthias.ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 92fa8473a4Smatthias.ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 93fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 9439bda6d5Smatthias.ringwald 9539bda6d5Smatthias.ringwald 9671de195eSMatthias Ringwald void l2cap_init(void){ 972b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 98808a48abSmatthias.ringwald 99f5454fc6Smatthias.ringwald l2cap_channels = NULL; 100f5454fc6Smatthias.ringwald l2cap_services = NULL; 1017192e786SMatthias Ringwald l2cap_le_services = NULL; 1027192e786SMatthias Ringwald l2cap_le_channels = NULL; 103f5454fc6Smatthias.ringwald 104f5454fc6Smatthias.ringwald packet_handler = null_packet_handler; 105c42e2ff2S[email protected] attribute_protocol_packet_handler = NULL; 106c42e2ff2S[email protected] security_protocol_packet_handler = NULL; 107462e630dS[email protected] connectionless_channel_packet_handler = NULL; 108f5454fc6Smatthias.ringwald 109ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 110ac301f95S[email protected] 111fcadd0caSmatthias.ringwald // 1122718e2e7Smatthias.ringwald // register callback with HCI 113fcadd0caSmatthias.ringwald // 1142718e2e7Smatthias.ringwald hci_register_packet_handler(&l2cap_packet_handler); 115c0e866bfSmatthias.ringwald hci_connectable_control(0); // no services yet 116fcadd0caSmatthias.ringwald } 117fcadd0caSmatthias.ringwald 118fcadd0caSmatthias.ringwald 119fcadd0caSmatthias.ringwald /** Register L2CAP packet handlers */ 12036944dffSmatthias.ringwald static void null_packet_handler(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 121fcadd0caSmatthias.ringwald } 12236944dffSmatthias.ringwald void l2cap_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 123b502e1b0Smatthias.ringwald packet_handler = handler; 1241e6aba47Smatthias.ringwald } 1251e6aba47Smatthias.ringwald 12658de5610Smatthias.ringwald // notify client/protocol handler 127ebffa4a8SMatthias Ringwald static void l2cap_dispatch(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 12858de5610Smatthias.ringwald if (channel->packet_handler) { 12958de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 13058de5610Smatthias.ringwald } else { 13136944dffSmatthias.ringwald (*packet_handler)(channel->connection, type, channel->local_cid, data, size); 13258de5610Smatthias.ringwald } 13358de5610Smatthias.ringwald } 13458de5610Smatthias.ringwald 13558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 136c9dc710bS[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", 137e0abb8e7S[email protected] status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 138c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 139c9dc710bS[email protected] uint8_t event[23]; 14058de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 14158de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 14258de5610Smatthias.ringwald event[2] = status; 14358de5610Smatthias.ringwald bt_flip_addr(&event[3], channel->address); 14458de5610Smatthias.ringwald bt_store_16(event, 9, channel->handle); 14558de5610Smatthias.ringwald bt_store_16(event, 11, channel->psm); 14658de5610Smatthias.ringwald bt_store_16(event, 13, channel->local_cid); 14758de5610Smatthias.ringwald bt_store_16(event, 15, channel->remote_cid); 1484c98aa43Smatthias.ringwald bt_store_16(event, 17, channel->local_mtu); 1494c98aa43Smatthias.ringwald bt_store_16(event, 19, channel->remote_mtu); 15054ed2ecaS[email protected] bt_store_16(event, 21, channel->flush_timeout); 15158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 15258de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 15358de5610Smatthias.ringwald } 15458de5610Smatthias.ringwald 15558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 156e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 15758de5610Smatthias.ringwald uint8_t event[4]; 15858de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 15958de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 16058de5610Smatthias.ringwald bt_store_16(event, 2, channel->local_cid); 16158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 16258de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 16358de5610Smatthias.ringwald } 16458de5610Smatthias.ringwald 16558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 166e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 167e0abb8e7S[email protected] bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 16858de5610Smatthias.ringwald uint8_t event[16]; 16958de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 17058de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 17158de5610Smatthias.ringwald bt_flip_addr(&event[2], channel->address); 17258de5610Smatthias.ringwald bt_store_16(event, 8, channel->handle); 17358de5610Smatthias.ringwald bt_store_16(event, 10, channel->psm); 17458de5610Smatthias.ringwald bt_store_16(event, 12, channel->local_cid); 17558de5610Smatthias.ringwald bt_store_16(event, 14, channel->remote_cid); 17658de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17758de5610Smatthias.ringwald l2cap_dispatch(channel, HCI_EVENT_PACKET, event, sizeof(event)); 1780af41d30Smatthias.ringwald } 179808a48abSmatthias.ringwald 180ebffa4a8SMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){ 181ccf076adS[email protected] uint8_t event[6]; 182ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 183ccf076adS[email protected] event[1] = 4; 184ccf076adS[email protected] bt_store_16(event, 2, handle); 185ccf076adS[email protected] bt_store_16(event, 4, result); 186ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 187ccf076adS[email protected] (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); 188ccf076adS[email protected] } 189ccf076adS[email protected] 1905842b6d9Smatthias.ringwald static void l2cap_emit_service_registered(void *connection, uint8_t status, uint16_t psm){ 191e0abb8e7S[email protected] log_info("L2CAP_EVENT_SERVICE_REGISTERED status 0x%x psm 0x%x", status, psm); 1925842b6d9Smatthias.ringwald uint8_t event[5]; 1935842b6d9Smatthias.ringwald event[0] = L2CAP_EVENT_SERVICE_REGISTERED; 1945842b6d9Smatthias.ringwald event[1] = sizeof(event) - 2; 1955842b6d9Smatthias.ringwald event[2] = status; 1965842b6d9Smatthias.ringwald bt_store_16(event, 3, psm); 1975842b6d9Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 198e6f51008S[email protected] (*packet_handler)(connection, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1995842b6d9Smatthias.ringwald } 2005842b6d9Smatthias.ringwald 201ebffa4a8SMatthias Ringwald static void l2cap_emit_credits(l2cap_channel_t *channel, uint8_t credits) { 202e0abb8e7S[email protected] 203e0abb8e7S[email protected] log_info("L2CAP_EVENT_CREDITS local_cid 0x%x credits %u", channel->local_cid, 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 214ebffa4a8SMatthias Ringwald static void l2cap_hand_out_credits(void){ 215c22aecc9S[email protected] linked_list_iterator_t it; 216c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 217c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 218c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 2190e7bc007Smatthias.ringwald if (channel->state != L2CAP_STATE_OPEN) continue; 22077aee70fSMatthias Ringwald if (!hci_number_free_acl_slots_for_handle(channel->handle)) return; 2218d371091Smatthias.ringwald l2cap_emit_credits(channel, 1); 2228d371091Smatthias.ringwald } 2238d371091Smatthias.ringwald } 2248d371091Smatthias.ringwald 225ebffa4a8SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 226c22aecc9S[email protected] linked_list_iterator_t it; 227c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 228c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 229c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 230b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 231f62db1e3Smatthias.ringwald return channel; 232f62db1e3Smatthias.ringwald } 233f62db1e3Smatthias.ringwald } 234f62db1e3Smatthias.ringwald return NULL; 235f62db1e3Smatthias.ringwald } 236f62db1e3Smatthias.ringwald 2376b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2386b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2396b1fde37Smatthias.ringwald if (!channel) return 0; 240a35252c8S[email protected] return hci_can_send_acl_packet_now(channel->handle); 2417856fb31S[email protected] } 2427856fb31S[email protected] 243*83e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 244*83e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 245*83e7cdd9SMatthias Ringwald if (!channel) return 0; 246*83e7cdd9SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->handle); 247*83e7cdd9SMatthias Ringwald } 248*83e7cdd9SMatthias Ringwald 2496cd4da6bS[email protected] // @deprecated 2503cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){ 251a35252c8S[email protected] // TODO provide real handle 2526cd4da6bS[email protected] return l2cap_can_send_fixed_channel_packet_now(0x1234); 2536cd4da6bS[email protected] } 2546cd4da6bS[email protected] 2556cd4da6bS[email protected] int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 2566cd4da6bS[email protected] return hci_can_send_acl_packet_now(handle); 2573cab4fcaS[email protected] } 2583cab4fcaS[email protected] 25996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 26096cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 26196cbd662Smatthias.ringwald if (channel) { 26296cbd662Smatthias.ringwald return channel->remote_mtu; 26396cbd662Smatthias.ringwald } 26496cbd662Smatthias.ringwald return 0; 26596cbd662Smatthias.ringwald } 26696cbd662Smatthias.ringwald 2675932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 268c22aecc9S[email protected] linked_list_iterator_t it; 269c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 270c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 271c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 2725932bd7cS[email protected] if ( &channel->rtx == ts) { 2735932bd7cS[email protected] return channel; 2745932bd7cS[email protected] } 2755932bd7cS[email protected] } 2765932bd7cS[email protected] return NULL; 2775932bd7cS[email protected] } 2785932bd7cS[email protected] 2795932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){ 2805932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2815932bd7cS[email protected] if (!ts) return; 2825932bd7cS[email protected] 2835932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2845932bd7cS[email protected] 2855932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2865932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2875932bd7cS[email protected] // notify client 2885932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2895932bd7cS[email protected] 2905932bd7cS[email protected] // discard channel 2919dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 2925932bd7cS[email protected] linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 2935932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 2945932bd7cS[email protected] } 2955932bd7cS[email protected] 2965932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 2975932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 2985932bd7cS[email protected] run_loop_remove_timer(&channel->rtx); 2995932bd7cS[email protected] } 3005932bd7cS[email protected] 3015932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 3025932bd7cS[email protected] l2cap_stop_rtx(channel); 303cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 3045932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3055932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 3065932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3075932bd7cS[email protected] } 3085932bd7cS[email protected] 3095932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3105932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3115932bd7cS[email protected] l2cap_stop_rtx(channel); 3125932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 3135932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 3145932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 3155932bd7cS[email protected] } 3165932bd7cS[email protected] 31771de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 318ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 319ac301f95S[email protected] } 320ac301f95S[email protected] 321df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 322ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 323df3354fcS[email protected] } 3245932bd7cS[email protected] 325ebffa4a8SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 326b1d43497Smatthias.ringwald 327a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3289da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 329b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 330b1d43497Smatthias.ringwald } 331b1d43497Smatthias.ringwald 3329da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3332a373862S[email protected] hci_reserve_packet_buffer(); 334facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 33558de5610Smatthias.ringwald va_list argptr; 33658de5610Smatthias.ringwald va_start(argptr, identifier); 33770efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 33858de5610Smatthias.ringwald va_end(argptr); 3399da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 340826f7347S[email protected] return hci_send_acl_packet_buffer(len); 34158de5610Smatthias.ringwald } 34258de5610Smatthias.ringwald 34370efece1S[email protected] #ifdef HAVE_BLE 344ebffa4a8SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 34570efece1S[email protected] 346a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3479da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 34870efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 34970efece1S[email protected] } 35070efece1S[email protected] 3519da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3522a373862S[email protected] hci_reserve_packet_buffer(); 353facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 35470efece1S[email protected] va_list argptr; 35570efece1S[email protected] va_start(argptr, identifier); 35670efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 35770efece1S[email protected] va_end(argptr); 3589da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 359826f7347S[email protected] return hci_send_acl_packet_buffer(len); 36070efece1S[email protected] } 36170efece1S[email protected] #endif 36270efece1S[email protected] 363b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 364facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 365b1d43497Smatthias.ringwald } 3666218e6f1Smatthias.ringwald 3676b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3686b4af23dS[email protected] return hci_reserve_packet_buffer(); 3696b4af23dS[email protected] } 3706b4af23dS[email protected] 37168a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 37268a0fcf7S[email protected] hci_release_packet_buffer(); 37368a0fcf7S[email protected] } 37468a0fcf7S[email protected] 37568a0fcf7S[email protected] 376b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 377b1d43497Smatthias.ringwald 378c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 379c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 380c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 381c8b9416aS[email protected] } 382c8b9416aS[email protected] 38358de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 384b1d43497Smatthias.ringwald if (!channel) { 3859da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 386b1d43497Smatthias.ringwald return -1; // TODO: define error 3876218e6f1Smatthias.ringwald } 3886218e6f1Smatthias.ringwald 389a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 3909da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 391a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 392a35252c8S[email protected] } 393a35252c8S[email protected] 3943aff022fSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle); 395b1d43497Smatthias.ringwald 396facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 397b1d43497Smatthias.ringwald 398e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 399e9772277S[email protected] 400e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 401e9772277S[email protected] bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 40258de5610Smatthias.ringwald // 2 - ACL length 40358de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 40458de5610Smatthias.ringwald // 4 - L2CAP packet length 40558de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 40658de5610Smatthias.ringwald // 6 - L2CAP channel DEST 40758de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 40858de5610Smatthias.ringwald // send 409826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 41091b99603Smatthias.ringwald 41191b99603Smatthias.ringwald l2cap_hand_out_credits(); 41291b99603Smatthias.ringwald 4136218e6f1Smatthias.ringwald return err; 41458de5610Smatthias.ringwald } 41558de5610Smatthias.ringwald 4162149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 4172149f12eSmatthias.ringwald 418c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 419c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4202149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4212149f12eSmatthias.ringwald } 4222149f12eSmatthias.ringwald 423a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(handle)){ 4249da54300S[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 425c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 426c8b9416aS[email protected] } 427c8b9416aS[email protected] 4289da54300S[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 4292149f12eSmatthias.ringwald 430facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4312149f12eSmatthias.ringwald 432e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 433e9772277S[email protected] 434e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 435e9772277S[email protected] bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 4362149f12eSmatthias.ringwald // 2 - ACL length 4372149f12eSmatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 4382149f12eSmatthias.ringwald // 4 - L2CAP packet length 4392149f12eSmatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 4402149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 4412149f12eSmatthias.ringwald bt_store_16(acl_buffer, 6, cid); 4422149f12eSmatthias.ringwald // send 443826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4442149f12eSmatthias.ringwald 4452149f12eSmatthias.ringwald l2cap_hand_out_credits(); 4462149f12eSmatthias.ringwald 4472149f12eSmatthias.ringwald return err; 4482149f12eSmatthias.ringwald } 4492149f12eSmatthias.ringwald 450b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 451b1d43497Smatthias.ringwald 452a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 453a35252c8S[email protected] if (!channel) { 4549da54300S[email protected] log_error("l2cap_send_internal no channel for cid 0x%02x", local_cid); 455a35252c8S[email protected] return -1; // TODO: define error 456a35252c8S[email protected] } 457a35252c8S[email protected] 458f0efaa57S[email protected] if (len > channel->remote_mtu){ 459f0efaa57S[email protected] log_error("l2cap_send_internal cid 0x%02x, data length exceeds remote MTU.", local_cid); 460f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 461f0efaa57S[email protected] } 462f0efaa57S[email protected] 463a35252c8S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)){ 4649da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", local_cid); 465b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 466b1d43497Smatthias.ringwald } 467b1d43497Smatthias.ringwald 4682a373862S[email protected] hci_reserve_packet_buffer(); 469facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 470b1d43497Smatthias.ringwald 471b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 472b1d43497Smatthias.ringwald 473b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 474b1d43497Smatthias.ringwald } 475b1d43497Smatthias.ringwald 4762149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 4772149f12eSmatthias.ringwald 478a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4799da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", cid); 4802149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4812149f12eSmatthias.ringwald } 4822149f12eSmatthias.ringwald 4832a373862S[email protected] hci_reserve_packet_buffer(); 484facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4852149f12eSmatthias.ringwald 4862149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 4872149f12eSmatthias.ringwald 4882149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 4892149f12eSmatthias.ringwald } 4902149f12eSmatthias.ringwald 4910e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 4920e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 4930e37e417S[email protected] } 4940e37e417S[email protected] 49528ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 49628ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 49728ca2b46S[email protected] } 49828ca2b46S[email protected] 49928ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 50028ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 50128ca2b46S[email protected] } 50228ca2b46S[email protected] 50328ca2b46S[email protected] 504b1d43497Smatthias.ringwald 5058158c421Smatthias.ringwald // MARK: L2CAP_RUN 5062cd0be45Smatthias.ringwald // process outstanding signaling tasks 507ebffa4a8SMatthias Ringwald static void l2cap_run(void){ 5082b83fb7dSmatthias.ringwald 50922c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 51022c29ab4SMatthias Ringwald 5112b83fb7dSmatthias.ringwald // check pending signaling responses 5122b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5132b83fb7dSmatthias.ringwald 5142b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 515a35252c8S[email protected] 516a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 517a35252c8S[email protected] 5182b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5192b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 52063a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 521f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5222b83fb7dSmatthias.ringwald 523f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 524f53da564S[email protected] signaling_responses_pending--; 525f53da564S[email protected] int i; 526f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 527f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 528f53da564S[email protected] } 529f53da564S[email protected] 530f53da564S[email protected] switch (response_code){ 5312b360848Smatthias.ringwald case CONNECTION_REQUEST: 5322b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5332bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5344d816277S[email protected] if (result == 0x0003){ 5352bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5364d816277S[email protected] } 5372b360848Smatthias.ringwald break; 5382b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5392b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5402b83fb7dSmatthias.ringwald break; 5412b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5423b0484b3S[email protected] switch (infoType){ 5433b0484b3S[email protected] case 1: { // Connectionless MTU 5443b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5453b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5463b0484b3S[email protected] break; 5473b0484b3S[email protected] } 5483b0484b3S[email protected] case 2: { // Extended Features Supported 549462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 550462e630dS[email protected] uint32_t features = 0x280; 5513b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5523b0484b3S[email protected] break; 5533b0484b3S[email protected] } 5543b0484b3S[email protected] case 3: { // Fixed Channels Supported 5553b0484b3S[email protected] uint8_t map[8]; 5563b0484b3S[email protected] memset(map, 0, 8); 557462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5583b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5593b0484b3S[email protected] break; 5603b0484b3S[email protected] } 5613b0484b3S[email protected] default: 5622b83fb7dSmatthias.ringwald // all other types are not supported 5632b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5643b0484b3S[email protected] break; 5652b83fb7dSmatthias.ringwald } 5662b83fb7dSmatthias.ringwald break; 56763a7246aSmatthias.ringwald case COMMAND_REJECT: 5685ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 56970efece1S[email protected] #ifdef HAVE_BLE 57070efece1S[email protected] case COMMAND_REJECT_LE: 57170efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 57263a7246aSmatthias.ringwald break; 57370efece1S[email protected] #endif 5742b83fb7dSmatthias.ringwald default: 5752b83fb7dSmatthias.ringwald // should not happen 5762b83fb7dSmatthias.ringwald break; 5772b83fb7dSmatthias.ringwald } 5782b83fb7dSmatthias.ringwald } 5792b83fb7dSmatthias.ringwald 580ae280e73Smatthias.ringwald uint8_t config_options[4]; 581c22aecc9S[email protected] linked_list_iterator_t it; 582c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 583c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 584baf94f06S[email protected] 585c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 58622c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 5872cd0be45Smatthias.ringwald switch (channel->state){ 5882cd0be45Smatthias.ringwald 589df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 590ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 591baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 592a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 593ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 594a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 595ad671560S[email protected] } 596ad671560S[email protected] break; 597ad671560S[email protected] 59802b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 599baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 60064472d52Smatthias.ringwald // send connection request - set state first 60164472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 60202b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6038f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 60402b22dc4Smatthias.ringwald break; 60502b22dc4Smatthias.ringwald 606e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 607baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 60822c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 6091eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 610e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6119dcb2fb2S[email protected] l2cap_stop_rtx(channel); 612c22aecc9S[email protected] linked_list_iterator_remove(&it); 613d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 614e7ff783cSmatthias.ringwald break; 615e7ff783cSmatthias.ringwald 616552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 617baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 618fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 61928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 6202a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 621552d92a1Smatthias.ringwald break; 622552d92a1Smatthias.ringwald 6236fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 624baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 6256fdcc387Smatthias.ringwald // success, start l2cap handshake 626b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6276fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 6282a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6295932bd7cS[email protected] l2cap_start_rtx(channel); 6306fdcc387Smatthias.ringwald break; 6316fdcc387Smatthias.ringwald 632fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 633baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 63473cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 63563a7246aSmatthias.ringwald uint16_t flags = 0; 63628ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 63763a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 63863a7246aSmatthias.ringwald flags = 1; 63963a7246aSmatthias.ringwald } else { 64028ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 64163a7246aSmatthias.ringwald } 64263a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 64363a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 64463a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 64563a7246aSmatthias.ringwald config_options[0] = 1; // MTU 64663a7246aSmatthias.ringwald config_options[1] = 2; // len param 64763a7246aSmatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 64863a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 64963a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 65063a7246aSmatthias.ringwald } else { 65163a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 65263a7246aSmatthias.ringwald } 65363a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 654fa8473a4Smatthias.ringwald } 65573cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 65628ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 65728ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 658b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 659ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 660ae280e73Smatthias.ringwald config_options[1] = 2; // len param 661ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 662b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6635932bd7cS[email protected] l2cap_start_rtx(channel); 664fa8473a4Smatthias.ringwald } 665fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 666552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 667552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 668552d92a1Smatthias.ringwald l2cap_emit_credits(channel, 1); 669fa8473a4Smatthias.ringwald } 670552d92a1Smatthias.ringwald break; 671552d92a1Smatthias.ringwald 672e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 673baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 67422c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 675b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6765932bd7cS[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 :) 677756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 678e7ff783cSmatthias.ringwald break; 679e7ff783cSmatthias.ringwald 680e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 681baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 682b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6832cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 6842a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 6852cd0be45Smatthias.ringwald break; 6862cd0be45Smatthias.ringwald default: 6872cd0be45Smatthias.ringwald break; 6882cd0be45Smatthias.ringwald } 6892cd0be45Smatthias.ringwald } 690da886c03S[email protected] 6914d7157c3S[email protected] #ifdef HAVE_BLE 692da886c03S[email protected] // send l2cap con paramter update if necessary 693da886c03S[email protected] hci_connections_get_iterator(&it); 694da886c03S[email protected] while(linked_list_iterator_has_next(&it)){ 695da886c03S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it); 696a9df9816SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 697b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 698da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 699b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 700b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 701b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 702b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 703b68d7bc3SMatthias Ringwald break; 704da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 705b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 706b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 707da886c03S[email protected] break; 708da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 709b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 710b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 711da886c03S[email protected] break; 712da886c03S[email protected] default: 713da886c03S[email protected] break; 714da886c03S[email protected] } 715da886c03S[email protected] } 7164d7157c3S[email protected] #endif 717da886c03S[email protected] 71822c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 7192cd0be45Smatthias.ringwald } 7202cd0be45Smatthias.ringwald 7214aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 7224ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 723fa8c92f6Smatthias.ringwald } 724fa8c92f6Smatthias.ringwald 72571de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 7264ff786cfS[email protected] return l2cap_max_mtu(); 727e5e1518dS[email protected] } 728e5e1518dS[email protected] 7292df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 7302df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 7315533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 7322df5dadcS[email protected] // success, start l2cap handshake 7332df5dadcS[email protected] channel->handle = handle; 7342df5dadcS[email protected] channel->local_cid = l2cap_next_local_cid(); 7352df5dadcS[email protected] // check remote SSP feature first 7362df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 7372df5dadcS[email protected] } 7382df5dadcS[email protected] } 7392df5dadcS[email protected] 7402df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 7412df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 7422df5dadcS[email protected] 7432df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 744ac301f95S[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)); 7452df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 7462df5dadcS[email protected] // request security level 2 7472df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 7481429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 7492df5dadcS[email protected] return; 7502df5dadcS[email protected] } 7512df5dadcS[email protected] // fine, go ahead 7522df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7532df5dadcS[email protected] } 7542df5dadcS[email protected] 7551e6aba47Smatthias.ringwald // open outgoing L2CAP channel 756204e94f1SMatthias Ringwald void l2cap_create_channel_internal(void * connection, btstack_packet_handler_t channel_packet_handler, 75715470d27Smatthias.ringwald bd_addr_t address, uint16_t psm, uint16_t mtu){ 7581e6aba47Smatthias.ringwald 759e0abb8e7S[email protected] log_info("L2CAP_CREATE_CHANNEL_MTU addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 760e0abb8e7S[email protected] 7611e6aba47Smatthias.ringwald // alloc structure 762bb69aaaeS[email protected] l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 7632b360848Smatthias.ringwald if (!chan) { 7642b360848Smatthias.ringwald // emit error event 7652b360848Smatthias.ringwald l2cap_channel_t dummy_channel; 7662b360848Smatthias.ringwald BD_ADDR_COPY(dummy_channel.address, address); 7672b360848Smatthias.ringwald dummy_channel.psm = psm; 7682b360848Smatthias.ringwald l2cap_emit_channel_opened(&dummy_channel, BTSTACK_MEMORY_ALLOC_FAILED); 7692b360848Smatthias.ringwald return; 7702b360848Smatthias.ringwald } 771c523d53dS[email protected] // Init memory (make valgrind happy) 772c523d53dS[email protected] memset(chan, 0, sizeof(l2cap_channel_t)); 773f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 7742985cb84Smatthias.ringwald if (mtu > l2cap_max_mtu()) { 7752985cb84Smatthias.ringwald mtu = l2cap_max_mtu(); 7769775e25bSmatthias.ringwald } 7779775e25bSmatthias.ringwald 7781e6aba47Smatthias.ringwald // fill in 7791e6aba47Smatthias.ringwald BD_ADDR_COPY(chan->address, address); 7801e6aba47Smatthias.ringwald chan->psm = psm; 7811e6aba47Smatthias.ringwald chan->handle = 0; 7821e6aba47Smatthias.ringwald chan->connection = connection; 783204e94f1SMatthias Ringwald chan->packet_handler = channel_packet_handler; 7842784b77dSmatthias.ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 78515470d27Smatthias.ringwald chan->local_mtu = mtu; 7866218e6f1Smatthias.ringwald 7871e6aba47Smatthias.ringwald // set initial state 78802b22dc4Smatthias.ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 78973cf2b3dSmatthias.ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 790b1988dceSmatthias.ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 791b1988dceSmatthias.ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 7925533f01eS[email protected] chan->required_security_level = LEVEL_0; 7931e6aba47Smatthias.ringwald 7941e6aba47Smatthias.ringwald // add to connections list 7951e6aba47Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) chan); 7961e6aba47Smatthias.ringwald 7972df5dadcS[email protected] // check if hci connection is already usable 7982e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 7992df5dadcS[email protected] if (conn){ 8005533f01eS[email protected] log_info("l2cap_create_channel_internal, hci connection already exists"); 8012df5dadcS[email protected] l2cap_handle_connection_complete(conn->con_handle, chan); 802775ecc36SMatthias Ringwald // check if remote supported fearures are already received 8032df5dadcS[email protected] if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 8042df5dadcS[email protected] l2cap_handle_remote_supported_features_received(chan); 8052df5dadcS[email protected] } 8062df5dadcS[email protected] } 8072df5dadcS[email protected] 80802b22dc4Smatthias.ringwald l2cap_run(); 80943625864Smatthias.ringwald } 81043625864Smatthias.ringwald 811b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 812e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 813b35f641cSmatthias.ringwald // find channel for local_cid 814b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 815f62db1e3Smatthias.ringwald if (channel) { 816e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 817f62db1e3Smatthias.ringwald } 8182cd0be45Smatthias.ringwald // process 8192cd0be45Smatthias.ringwald l2cap_run(); 82043625864Smatthias.ringwald } 8211e6aba47Smatthias.ringwald 822afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 823c22aecc9S[email protected] linked_list_iterator_t it; 824c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 825c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 826c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 827c22aecc9S[email protected] if ( BD_ADDR_CMP( channel->address, address) != 0) continue; 828c22aecc9S[email protected] // channel for this address found 829c22aecc9S[email protected] switch (channel->state){ 830c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 831c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 832afde0c52Smatthias.ringwald // failure, forward error code 833afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 834afde0c52Smatthias.ringwald // discard channel 8359dcb2fb2S[email protected] l2cap_stop_rtx(channel); 836c22aecc9S[email protected] linked_list_iterator_remove(&it); 837d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 838c22aecc9S[email protected] break; 839c22aecc9S[email protected] default: 840c22aecc9S[email protected] break; 841afde0c52Smatthias.ringwald } 842afde0c52Smatthias.ringwald } 843afde0c52Smatthias.ringwald } 844afde0c52Smatthias.ringwald 845afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 846c22aecc9S[email protected] linked_list_iterator_t it; 847c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 848c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 849c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 850afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 8512df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 852afde0c52Smatthias.ringwald } 853afde0c52Smatthias.ringwald } 8546fdcc387Smatthias.ringwald // process 8556fdcc387Smatthias.ringwald l2cap_run(); 856afde0c52Smatthias.ringwald } 857b448a0e7Smatthias.ringwald 858ebffa4a8SMatthias Ringwald static void l2cap_event_handler(uint8_t *packet, uint16_t size){ 859afde0c52Smatthias.ringwald 860afde0c52Smatthias.ringwald bd_addr_t address; 861afde0c52Smatthias.ringwald hci_con_handle_t handle; 862c22aecc9S[email protected] linked_list_iterator_t it; 8632d00edd4Smatthias.ringwald int hci_con_used; 864afde0c52Smatthias.ringwald 865afde0c52Smatthias.ringwald switch(packet[0]){ 866afde0c52Smatthias.ringwald 867afde0c52Smatthias.ringwald // handle connection complete events 868afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 869afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 870afde0c52Smatthias.ringwald if (packet[2] == 0){ 871afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 872afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 873afde0c52Smatthias.ringwald } else { 874afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 875afde0c52Smatthias.ringwald } 876afde0c52Smatthias.ringwald break; 877afde0c52Smatthias.ringwald 878afde0c52Smatthias.ringwald // handle successful create connection cancel command 879afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 880afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 881afde0c52Smatthias.ringwald if (packet[5] == 0){ 882afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 883afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 884afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 88503cfbabcSmatthias.ringwald } 8861e6aba47Smatthias.ringwald } 88739d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 88839d59809Smatthias.ringwald break; 88939d59809Smatthias.ringwald 89039d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 89139d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 892afde0c52Smatthias.ringwald break; 89327a923d0Smatthias.ringwald 8941e6aba47Smatthias.ringwald // handle disconnection complete events 895afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 896c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 897afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 898c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 899c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 900c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 901c22aecc9S[email protected] if (channel->handle != handle) continue; 90215ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 9039dcb2fb2S[email protected] l2cap_stop_rtx(channel); 904c22aecc9S[email protected] linked_list_iterator_remove(&it); 905d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 90627a923d0Smatthias.ringwald } 907afde0c52Smatthias.ringwald break; 908fcadd0caSmatthias.ringwald 9096218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 91002b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 9118d371091Smatthias.ringwald l2cap_hand_out_credits(); 9126218e6f1Smatthias.ringwald break; 9136218e6f1Smatthias.ringwald 914ee091cf1Smatthias.ringwald // HCI Connection Timeouts 915afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 91636944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 917eddad2abSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 91880ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 9192d00edd4Smatthias.ringwald hci_con_used = 0; 920c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 921c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 922c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 923c22aecc9S[email protected] if (channel->handle != handle) continue; 9242d00edd4Smatthias.ringwald hci_con_used = 1; 925c22aecc9S[email protected] break; 926ee091cf1Smatthias.ringwald } 9272d00edd4Smatthias.ringwald if (hci_con_used) break; 928d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 9299edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 930afde0c52Smatthias.ringwald break; 931ee091cf1Smatthias.ringwald 9326e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 933b26c3003SMatthias Ringwald l2cap_run(); // try sending signaling packets first 934b26c3003SMatthias Ringwald l2cap_hand_out_credits(); 935b26c3003SMatthias Ringwald 936c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 937c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 938c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 939c22aecc9S[email protected] if (!channel->packet_handler) continue; 9406e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 9416e6710ebSmatthias.ringwald } 9425652b5ffS[email protected] if (attribute_protocol_packet_handler) { 9435652b5ffS[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9445652b5ffS[email protected] } 9455652b5ffS[email protected] if (security_protocol_packet_handler) { 946133efcfdSmatthias.ringwald (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9475652b5ffS[email protected] } 948462e630dS[email protected] if (connectionless_channel_packet_handler) { 949462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 950462e630dS[email protected] } 9516e6710ebSmatthias.ringwald break; 9526e6710ebSmatthias.ringwald 953df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 954df3354fcS[email protected] handle = READ_BT_16(packet, 3); 955c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 956c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 957c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 958df3354fcS[email protected] if (channel->handle != handle) continue; 9592df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 960df3354fcS[email protected] break; 961df3354fcS[email protected] } 962c22aecc9S[email protected] break; 963df3354fcS[email protected] 964a00031e2S[email protected] case GAP_SECURITY_LEVEL: 965a00031e2S[email protected] handle = READ_BT_16(packet, 2); 966bd63148eS[email protected] log_info("l2cap - security level update"); 967c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 968c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 969c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 970f85a9399S[email protected] if (channel->handle != handle) continue; 9715533f01eS[email protected] 972bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 973bd63148eS[email protected] 974e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 9755533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 9765533f01eS[email protected] 977df3354fcS[email protected] switch (channel->state){ 978df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 9795533f01eS[email protected] if (actual_level >= required_level){ 980f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 981f85a9399S[email protected] l2cap_emit_connection_request(channel); 9821eb2563eS[email protected] } else { 983775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 9841eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 9851eb2563eS[email protected] } 986df3354fcS[email protected] break; 987df3354fcS[email protected] 988df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 9895533f01eS[email protected] if (actual_level >= required_level){ 990df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 991df3354fcS[email protected] } else { 992df3354fcS[email protected] // disconnnect, authentication not good enough 993df3354fcS[email protected] hci_disconnect_security_block(handle); 994df3354fcS[email protected] } 995df3354fcS[email protected] break; 996df3354fcS[email protected] 997df3354fcS[email protected] default: 998df3354fcS[email protected] break; 999df3354fcS[email protected] } 1000f85a9399S[email protected] } 1001f85a9399S[email protected] break; 1002f85a9399S[email protected] 1003afde0c52Smatthias.ringwald default: 1004afde0c52Smatthias.ringwald break; 1005afde0c52Smatthias.ringwald } 1006afde0c52Smatthias.ringwald 1007e0707417S[email protected] // pass on: main packet handler, att and sm packet handlers 1008b502e1b0Smatthias.ringwald (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, packet, size); 1009e0707417S[email protected] if (attribute_protocol_packet_handler){ 1010e0707417S[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1011e0707417S[email protected] } 1012e0707417S[email protected] if (security_protocol_packet_handler) { 1013e0707417S[email protected] (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1014e0707417S[email protected] } 1015462e630dS[email protected] if (connectionless_channel_packet_handler) { 1016462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 1017462e630dS[email protected] } 1018bd63148eS[email protected] 1019bd63148eS[email protected] l2cap_run(); 10201e6aba47Smatthias.ringwald } 10211e6aba47Smatthias.ringwald 1022afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1023b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1024e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1025e7ff783cSmatthias.ringwald l2cap_run(); 102684836b65Smatthias.ringwald } 102784836b65Smatthias.ringwald 10282b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 10294cf56b4aSmatthias.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." 10302b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 10312b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 10322b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 10332b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 10342b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 10352b360848Smatthias.ringwald signaling_responses_pending++; 10362b360848Smatthias.ringwald l2cap_run(); 10372b360848Smatthias.ringwald } 10382b360848Smatthias.ringwald } 10392b360848Smatthias.ringwald 1040b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1041645658c9Smatthias.ringwald 10429da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1043645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1044645658c9Smatthias.ringwald if (!service) { 1045645658c9Smatthias.ringwald // 0x0002 PSM not supported 10462b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1047645658c9Smatthias.ringwald return; 1048645658c9Smatthias.ringwald } 1049645658c9Smatthias.ringwald 10505061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1051645658c9Smatthias.ringwald if (!hci_connection) { 10522b360848Smatthias.ringwald // 10539da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1054645658c9Smatthias.ringwald return; 1055645658c9Smatthias.ringwald } 10562bd8b7e7S[email protected] 1057645658c9Smatthias.ringwald // alloc structure 10589da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1059bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 10602b360848Smatthias.ringwald if (!channel){ 10612b360848Smatthias.ringwald // 0x0004 No resources available 10622b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 10632b360848Smatthias.ringwald return; 10642b360848Smatthias.ringwald } 1065c523d53dS[email protected] // Init memory (make valgrind happy) 1066c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1067645658c9Smatthias.ringwald // fill in 1068169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 1069169f8b28Smatthias.ringwald channel->psm = psm; 1070169f8b28Smatthias.ringwald channel->handle = handle; 1071169f8b28Smatthias.ringwald channel->connection = service->connection; 1072f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1073b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1074b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1075fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 10760a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1077b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1078df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1079645658c9Smatthias.ringwald 1080f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 10812985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 10822985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 10839775e25bSmatthias.ringwald } 10849775e25bSmatthias.ringwald 1085645658c9Smatthias.ringwald // set initial state 1086df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1087ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1088e405ae81Smatthias.ringwald 1089645658c9Smatthias.ringwald // add to connections list 1090169f8b28Smatthias.ringwald linked_list_add(&l2cap_channels, (linked_item_t *) channel); 1091645658c9Smatthias.ringwald 1092f85a9399S[email protected] // assert security requirements 10931eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1094e405ae81Smatthias.ringwald } 1095645658c9Smatthias.ringwald 1096b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 1097e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1098b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1099e405ae81Smatthias.ringwald if (!channel) { 11007d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 1101e405ae81Smatthias.ringwald return; 1102e405ae81Smatthias.ringwald } 1103e405ae81Smatthias.ringwald 1104552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1105e405ae81Smatthias.ringwald 1106552d92a1Smatthias.ringwald // process 1107552d92a1Smatthias.ringwald l2cap_run(); 1108e405ae81Smatthias.ringwald } 1109645658c9Smatthias.ringwald 1110b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 1111e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1112b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1113e405ae81Smatthias.ringwald if (!channel) { 11147d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 1115e405ae81Smatthias.ringwald return; 1116e405ae81Smatthias.ringwald } 1117e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1118e7ff783cSmatthias.ringwald channel->reason = reason; 1119e7ff783cSmatthias.ringwald l2cap_run(); 1120645658c9Smatthias.ringwald } 1121645658c9Smatthias.ringwald 1122ebffa4a8SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1123b1988dceSmatthias.ringwald 1124b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1125b1988dceSmatthias.ringwald 112663a7246aSmatthias.ringwald uint16_t flags = READ_BT_16(command, 6); 112763a7246aSmatthias.ringwald if (flags & 1) { 112863a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 112963a7246aSmatthias.ringwald } 113063a7246aSmatthias.ringwald 11312784b77dSmatthias.ringwald // accept the other's configuration options 11323de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 11333de7c0caSmatthias.ringwald uint16_t pos = 8; 11343de7c0caSmatthias.ringwald while (pos < end_pos){ 113563a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 113663a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 113763a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 113863a7246aSmatthias.ringwald pos++; 11391dc511deSmatthias.ringwald uint8_t length = command[pos++]; 11401dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 114163a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 11421dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 11439da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 114463a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 114563a7246aSmatthias.ringwald } 11460fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 11470fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 11480fe7a9d0S[email protected] channel->flush_timeout = READ_BT_16(command, pos); 11490fe7a9d0S[email protected] } 115063a7246aSmatthias.ringwald // check for unknown options 115163a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1152c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 115363a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 11541dc511deSmatthias.ringwald } 11551dc511deSmatthias.ringwald pos += length; 11561dc511deSmatthias.ringwald } 11572784b77dSmatthias.ringwald } 11582784b77dSmatthias.ringwald 1159fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 11609da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 116173cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 116273cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1163019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1164019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1165fa8473a4Smatthias.ringwald return 1; 1166fa8473a4Smatthias.ringwald } 1167fa8473a4Smatthias.ringwald 1168fa8473a4Smatthias.ringwald 1169ebffa4a8SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 11701e6aba47Smatthias.ringwald 117100d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 117200d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 117338e5900eSmatthias.ringwald uint16_t result = 0; 11741e6aba47Smatthias.ringwald 11759da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1176b35f641cSmatthias.ringwald 11779a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 11789a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 11799a011532Smatthias.ringwald switch (channel->state){ 1180fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 11819a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 11822b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 11839a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 11849a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 11859a011532Smatthias.ringwald break; 11869a011532Smatthias.ringwald 11879a011532Smatthias.ringwald default: 11889a011532Smatthias.ringwald // ignore in other states 11899a011532Smatthias.ringwald break; 11909a011532Smatthias.ringwald } 11919a011532Smatthias.ringwald return; 11929a011532Smatthias.ringwald } 11939a011532Smatthias.ringwald 119456081214Smatthias.ringwald // @STATEMACHINE(l2cap) 11951e6aba47Smatthias.ringwald switch (channel->state) { 11961e6aba47Smatthias.ringwald 11971e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 11981e6aba47Smatthias.ringwald switch (code){ 11991e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 12005932bd7cS[email protected] l2cap_stop_rtx(channel); 120100d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 120238e5900eSmatthias.ringwald switch (result) { 120338e5900eSmatthias.ringwald case 0: 1204169f8b28Smatthias.ringwald // successful connection 120500d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1206fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 120728ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 120838e5900eSmatthias.ringwald break; 120938e5900eSmatthias.ringwald case 1: 12105932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 12115932bd7cS[email protected] l2cap_start_ertx(channel); 121238e5900eSmatthias.ringwald break; 121338e5900eSmatthias.ringwald default: 1214eb920dbeSmatthias.ringwald // channel closed 1215eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1216f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 121738e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1218eb920dbeSmatthias.ringwald 1219eb920dbeSmatthias.ringwald // drop link key if security block 1220eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 12212e77e513S[email protected] hci_drop_link_key_for_bd_addr(channel->address); 1222eb920dbeSmatthias.ringwald } 1223eb920dbeSmatthias.ringwald 1224eb920dbeSmatthias.ringwald // discard channel 1225eb920dbeSmatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1226d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 122738e5900eSmatthias.ringwald break; 12281e6aba47Smatthias.ringwald } 12291e6aba47Smatthias.ringwald break; 123038e5900eSmatthias.ringwald 123138e5900eSmatthias.ringwald default: 12321e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 123338e5900eSmatthias.ringwald break; 12341e6aba47Smatthias.ringwald } 12351e6aba47Smatthias.ringwald break; 12361e6aba47Smatthias.ringwald 1237fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1238fe9d8984S[email protected] result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1239ae280e73Smatthias.ringwald switch (code) { 1240ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 124128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1242ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 124363a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 124463a7246aSmatthias.ringwald // only done if continuation not set 124563a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 124663a7246aSmatthias.ringwald } 1247ae280e73Smatthias.ringwald break; 12481e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 12495932bd7cS[email protected] l2cap_stop_rtx(channel); 12505932bd7cS[email protected] switch (result){ 12515932bd7cS[email protected] case 0: // success 12525932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 12535932bd7cS[email protected] break; 12545932bd7cS[email protected] case 4: // pending 12555932bd7cS[email protected] l2cap_start_ertx(channel); 12565932bd7cS[email protected] break; 12575932bd7cS[email protected] default: 1258fe9d8984S[email protected] // retry on negative result 1259fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1260fe9d8984S[email protected] break; 1261fe9d8984S[email protected] } 12625a67bd4aSmatthias.ringwald break; 12635a67bd4aSmatthias.ringwald default: 12645a67bd4aSmatthias.ringwald break; 12651e6aba47Smatthias.ringwald } 1266fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1267fa8473a4Smatthias.ringwald // for open: 12685a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1269fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 12706218e6f1Smatthias.ringwald l2cap_emit_credits(channel, 1); 1271c8e4258aSmatthias.ringwald } 1272c8e4258aSmatthias.ringwald break; 1273f62db1e3Smatthias.ringwald 1274f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1275f62db1e3Smatthias.ringwald switch (code) { 1276f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 127727a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 127827a923d0Smatthias.ringwald break; 12795a67bd4aSmatthias.ringwald default: 12805a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 12815a67bd4aSmatthias.ringwald break; 128227a923d0Smatthias.ringwald } 128327a923d0Smatthias.ringwald break; 128484836b65Smatthias.ringwald 128584836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 128684836b65Smatthias.ringwald // @TODO handle incoming requests 128784836b65Smatthias.ringwald break; 128884836b65Smatthias.ringwald 128984836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 129084836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 129184836b65Smatthias.ringwald break; 129210642e45Smatthias.ringwald default: 129310642e45Smatthias.ringwald break; 129427a923d0Smatthias.ringwald } 12959da54300S[email protected] // log_info("new state %u", channel->state); 129627a923d0Smatthias.ringwald } 129727a923d0Smatthias.ringwald 129800d93d79Smatthias.ringwald 1299ebffa4a8SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 130000d93d79Smatthias.ringwald 130100d93d79Smatthias.ringwald // get code, signalind identifier and command len 130200d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 130300d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 130400d93d79Smatthias.ringwald 130500d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 130600d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 130763a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 130800d93d79Smatthias.ringwald return; 130900d93d79Smatthias.ringwald } 131000d93d79Smatthias.ringwald 131100d93d79Smatthias.ringwald // general commands without an assigned channel 131200d93d79Smatthias.ringwald switch(code) { 131300d93d79Smatthias.ringwald 131400d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 131500d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 131600d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 131700d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 13182b83fb7dSmatthias.ringwald return; 131900d93d79Smatthias.ringwald } 132000d93d79Smatthias.ringwald 13212b360848Smatthias.ringwald case ECHO_REQUEST: 13222b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 13232b83fb7dSmatthias.ringwald return; 132400d93d79Smatthias.ringwald 132500d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 132600d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 13272b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 13282b83fb7dSmatthias.ringwald return; 132900d93d79Smatthias.ringwald } 133000d93d79Smatthias.ringwald 133100d93d79Smatthias.ringwald default: 133200d93d79Smatthias.ringwald break; 133300d93d79Smatthias.ringwald } 133400d93d79Smatthias.ringwald 133500d93d79Smatthias.ringwald 133600d93d79Smatthias.ringwald // Get potential destination CID 133700d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 133800d93d79Smatthias.ringwald 133900d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1340c22aecc9S[email protected] linked_list_iterator_t it; 1341c22aecc9S[email protected] linked_list_iterator_init(&it, &l2cap_channels); 1342c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1343c22aecc9S[email protected] l2cap_channel_t * channel = (l2cap_channel_t *) linked_list_iterator_next(&it); 1344c22aecc9S[email protected] if (channel->handle != handle) continue; 134500d93d79Smatthias.ringwald if (code & 1) { 1346b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1347b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 134800d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13494e32727eSmatthias.ringwald break; 135000d93d79Smatthias.ringwald } 135100d93d79Smatthias.ringwald } else { 1352b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 135300d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 135400d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13554e32727eSmatthias.ringwald break; 135600d93d79Smatthias.ringwald } 135700d93d79Smatthias.ringwald } 135800d93d79Smatthias.ringwald } 135900d93d79Smatthias.ringwald } 136000d93d79Smatthias.ringwald 1361ebffa4a8SMatthias Ringwald static void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 136200d93d79Smatthias.ringwald 136300d93d79Smatthias.ringwald // Get Channel ID 136400d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 136500d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 136600d93d79Smatthias.ringwald 13675652b5ffS[email protected] switch (channel_id) { 13685652b5ffS[email protected] 13695652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 13705652b5ffS[email protected] 137100d93d79Smatthias.ringwald uint16_t command_offset = 8; 137200d93d79Smatthias.ringwald while (command_offset < size) { 137300d93d79Smatthias.ringwald 137400d93d79Smatthias.ringwald // handle signaling commands 137500d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 137600d93d79Smatthias.ringwald 137700d93d79Smatthias.ringwald // increment command_offset 137800d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 137900d93d79Smatthias.ringwald } 13805652b5ffS[email protected] break; 138100d93d79Smatthias.ringwald } 138200d93d79Smatthias.ringwald 13835652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 13845652b5ffS[email protected] if (attribute_protocol_packet_handler) { 13855652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13865652b5ffS[email protected] } 13875652b5ffS[email protected] break; 13885652b5ffS[email protected] 13895652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 13905652b5ffS[email protected] if (security_protocol_packet_handler) { 13915652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13925652b5ffS[email protected] } 13935652b5ffS[email protected] break; 13945652b5ffS[email protected] 1395462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1396462e630dS[email protected] if (connectionless_channel_packet_handler) { 1397462e630dS[email protected] (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1398462e630dS[email protected] } 1399462e630dS[email protected] break; 1400462e630dS[email protected] 14011bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1402ccf076adS[email protected] switch (packet[8]){ 1403ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1404ccf076adS[email protected] uint16_t result = READ_BT_16(packet, 12); 1405ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1406ccf076adS[email protected] break; 1407ccf076adS[email protected] } 1408ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1409ccf076adS[email protected] uint8_t event[10]; 1410ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1411ccf076adS[email protected] event[1] = 8; 1412ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1413da886c03S[email protected] 1414da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1415da886c03S[email protected] if (connection){ 141623c8976eSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 141723c8976eSMatthias Ringwald // reject command without notifying upper layer when not in master role 141823c8976eSMatthias Ringwald uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 141923c8976eSMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 142023c8976eSMatthias Ringwald break; 142123c8976eSMatthias Ringwald } 1422da886c03S[email protected] int update_parameter = 1; 1423c133441bSMatthias Ringwald le_connection_parameter_range_t existing_range; 1424c133441bSMatthias Ringwald gap_le_get_connection_parameter_range(existing_range); 1425da886c03S[email protected] uint16_t le_conn_interval_min = READ_BT_16(packet,12); 1426da886c03S[email protected] uint16_t le_conn_interval_max = READ_BT_16(packet,14); 1427da886c03S[email protected] uint16_t le_conn_latency = READ_BT_16(packet,16); 1428da886c03S[email protected] uint16_t le_supervision_timeout = READ_BT_16(packet,18); 1429da886c03S[email protected] 1430da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1431da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1432da886c03S[email protected] 1433da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1434da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1435da886c03S[email protected] 1436da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1437da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1438da886c03S[email protected] 1439da886c03S[email protected] if (update_parameter){ 1440da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1441da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1442da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1443da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1444da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1445da886c03S[email protected] } else { 1446da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1447da886c03S[email protected] } 144879f53f1dSMatthias Ringwald connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1449da886c03S[email protected] } 1450da886c03S[email protected] 1451ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1452ccf076adS[email protected] (*packet_handler)(NULL, HCI_EVENT_PACKET, 0, event, sizeof(event)); 1453da886c03S[email protected] 1454ccf076adS[email protected] break; 1455ccf076adS[email protected] } 1456ccf076adS[email protected] default: { 14571bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14581bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14591bbc0b23S[email protected] break; 14601bbc0b23S[email protected] } 1461ccf076adS[email protected] } 1462ccf076adS[email protected] break; 1463ccf076adS[email protected] } 14641bbc0b23S[email protected] 14655652b5ffS[email protected] default: { 146600d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 146700d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 146800d93d79Smatthias.ringwald if (channel) { 146958de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 147000d93d79Smatthias.ringwald } 14715652b5ffS[email protected] break; 14725652b5ffS[email protected] } 14735652b5ffS[email protected] } 147400d93d79Smatthias.ringwald } 147500d93d79Smatthias.ringwald 14762718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14772718e2e7Smatthias.ringwald switch (packet_type) { 14782718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 14792718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 14802718e2e7Smatthias.ringwald break; 14812718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 14822718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 14832718e2e7Smatthias.ringwald break; 14842718e2e7Smatthias.ringwald default: 14852718e2e7Smatthias.ringwald break; 14862718e2e7Smatthias.ringwald } 14871eb2563eS[email protected] l2cap_run(); 14882718e2e7Smatthias.ringwald } 148900d93d79Smatthias.ringwald 149015ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 149127a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1492f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1493f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1494f62db1e3Smatthias.ringwald // discard channel 14959dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1496f62db1e3Smatthias.ringwald linked_list_remove(&l2cap_channels, (linked_item_t *) channel); 1497d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1498c8e4258aSmatthias.ringwald } 14991e6aba47Smatthias.ringwald 15007192e786SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(linked_list_t * services, uint16_t psm){ 1501c22aecc9S[email protected] linked_list_iterator_t it; 15027192e786SMatthias Ringwald linked_list_iterator_init(&it, services); 1503c22aecc9S[email protected] while (linked_list_iterator_has_next(&it)){ 1504c22aecc9S[email protected] l2cap_service_t * service = (l2cap_service_t *) linked_list_iterator_next(&it); 15059d9bbc01Smatthias.ringwald if ( service->psm == psm){ 15069d9bbc01Smatthias.ringwald return service; 15079d9bbc01Smatthias.ringwald }; 15089d9bbc01Smatthias.ringwald } 15099d9bbc01Smatthias.ringwald return NULL; 15109d9bbc01Smatthias.ringwald } 15119d9bbc01Smatthias.ringwald 15127192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 15137192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 15147192e786SMatthias Ringwald } 15157192e786SMatthias Ringwald 1516204e94f1SMatthias Ringwald void l2cap_register_service_internal(void *connection, btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1517e0abb8e7S[email protected] 1518e6f51008S[email protected] log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 1519e0abb8e7S[email protected] 15204bb582b6Smatthias.ringwald // check for alread registered psm 15214bb582b6Smatthias.ringwald // TODO: emit error event 15229d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1523277abc2cSmatthias.ringwald if (service) { 15249da54300S[email protected] log_error("l2cap_register_service_internal: PSM %u already registered", psm); 152581476041Smatthias.ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 1526277abc2cSmatthias.ringwald return; 1527277abc2cSmatthias.ringwald } 15289d9bbc01Smatthias.ringwald 15294bb582b6Smatthias.ringwald // alloc structure 15304bb582b6Smatthias.ringwald // TODO: emit error event 1531bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1532277abc2cSmatthias.ringwald if (!service) { 15339da54300S[email protected] log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 15345842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 1535277abc2cSmatthias.ringwald return; 1536277abc2cSmatthias.ringwald } 15379d9bbc01Smatthias.ringwald 15389d9bbc01Smatthias.ringwald // fill in 15399d9bbc01Smatthias.ringwald service->psm = psm; 15409d9bbc01Smatthias.ringwald service->mtu = mtu; 15419d9bbc01Smatthias.ringwald service->connection = connection; 1542204e94f1SMatthias Ringwald service->packet_handler = service_packet_handler; 1543df3354fcS[email protected] service->required_security_level = security_level; 15449d9bbc01Smatthias.ringwald 15459d9bbc01Smatthias.ringwald // add to services list 15469d9bbc01Smatthias.ringwald linked_list_add(&l2cap_services, (linked_item_t *) service); 1547c0e866bfSmatthias.ringwald 1548c0e866bfSmatthias.ringwald // enable page scan 1549c0e866bfSmatthias.ringwald hci_connectable_control(1); 15505842b6d9Smatthias.ringwald 15515842b6d9Smatthias.ringwald // done 15525842b6d9Smatthias.ringwald l2cap_emit_service_registered(connection, 0, psm); 15539d9bbc01Smatthias.ringwald } 15549d9bbc01Smatthias.ringwald 155536944dffSmatthias.ringwald void l2cap_unregister_service_internal(void *connection, uint16_t psm){ 1556e0abb8e7S[email protected] 1557e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1558e0abb8e7S[email protected] 15599d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1560037d6e48Smatthias.ringwald if (!service) return; 15619d9bbc01Smatthias.ringwald linked_list_remove(&l2cap_services, (linked_item_t *) service); 1562d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1563c0e866bfSmatthias.ringwald 1564c0e866bfSmatthias.ringwald // disable page scan when no services registered 1565c0e866bfSmatthias.ringwald if (!linked_list_empty(&l2cap_services)) return; 1566c0e866bfSmatthias.ringwald hci_connectable_control(0); 15679d9bbc01Smatthias.ringwald } 15689d9bbc01Smatthias.ringwald 15695652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 1570204e94f1SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 15715652b5ffS[email protected] switch(channel_id){ 15725652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1573204e94f1SMatthias Ringwald attribute_protocol_packet_handler = the_packet_handler; 15745652b5ffS[email protected] break; 15755652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1576204e94f1SMatthias Ringwald security_protocol_packet_handler = the_packet_handler; 15775652b5ffS[email protected] break; 1578462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1579204e94f1SMatthias Ringwald connectionless_channel_packet_handler = the_packet_handler; 1580462e630dS[email protected] break; 15815652b5ffS[email protected] } 15825652b5ffS[email protected] } 15835652b5ffS[email protected] 15847192e786SMatthias Ringwald #ifdef HAVE_BLE 15857192e786SMatthias Ringwald 1586ebffa4a8SMatthias Ringwald 1587ebffa4a8SMatthias Ringwald #if 0 15887192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 15897192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, psm); 15907192e786SMatthias Ringwald } 15917192e786SMatthias Ringwald /** 15927192e786SMatthias Ringwald * @brief Regster L2CAP LE Credit Based Flow Control Mode service 15937192e786SMatthias Ringwald * @param 15947192e786SMatthias Ringwald */ 15957192e786SMatthias Ringwald void l2cap_le_register_service_internal(void * connection, btstack_packet_handler_t packet_handler, uint16_t psm, 15967192e786SMatthias Ringwald uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 15977192e786SMatthias Ringwald 15987192e786SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 15997192e786SMatthias Ringwald 16007192e786SMatthias Ringwald // check for alread registered psm 16017192e786SMatthias Ringwald // TODO: emit error event 16027192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16037192e786SMatthias Ringwald if (service) { 16047192e786SMatthias Ringwald log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 16057192e786SMatthias Ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 16067192e786SMatthias Ringwald return; 16077192e786SMatthias Ringwald } 16087192e786SMatthias Ringwald 16097192e786SMatthias Ringwald // alloc structure 16107192e786SMatthias Ringwald // TODO: emit error event 16117192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 16127192e786SMatthias Ringwald if (!service) { 16137192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 16147192e786SMatthias Ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 16157192e786SMatthias Ringwald return; 16167192e786SMatthias Ringwald } 16177192e786SMatthias Ringwald 16187192e786SMatthias Ringwald // fill in 16197192e786SMatthias Ringwald service->psm = psm; 16207192e786SMatthias Ringwald service->mtu = mtu; 16217192e786SMatthias Ringwald service->mps = mps; 16227192e786SMatthias Ringwald service->connection = connection; 16237192e786SMatthias Ringwald service->packet_handler = packet_handler; 16247192e786SMatthias Ringwald service->required_security_level = security_level; 16257192e786SMatthias Ringwald 16267192e786SMatthias Ringwald // add to services list 16277192e786SMatthias Ringwald linked_list_add(&l2cap_le_services, (linked_item_t *) service); 16287192e786SMatthias Ringwald 16297192e786SMatthias Ringwald // done 16307192e786SMatthias Ringwald l2cap_emit_service_registered(connection, 0, psm); 16317192e786SMatthias Ringwald } 16327192e786SMatthias Ringwald 16337192e786SMatthias Ringwald void l2cap_le_unregister_service_internal(void * connection, uint16_t psm) { 16347192e786SMatthias Ringwald 16357192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 16367192e786SMatthias Ringwald 16377192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16387192e786SMatthias Ringwald if (!service) return; 16397192e786SMatthias Ringwald linked_list_remove(&l2cap_le_services, (linked_item_t *) service); 16407192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 16417192e786SMatthias Ringwald } 16427192e786SMatthias Ringwald #endif 1643ebffa4a8SMatthias Ringwald #endif 1644