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" 49*16ece135SMatthias Ringwald #include "btstack_debug.h" 50d3a9df87Smatthias.ringwald #include "btstack_memory.h" 5143625864Smatthias.ringwald 5243625864Smatthias.ringwald #include <stdarg.h> 5343625864Smatthias.ringwald #include <string.h> 5443625864Smatthias.ringwald 5543625864Smatthias.ringwald #include <stdio.h> 5643625864Smatthias.ringwald 574c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 584c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 594c744e21Smatthias.ringwald 6039bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 61e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 6239bda6d5Smatthias.ringwald 6300d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6800d93d79Smatthias.ringwald 69ffbf8201SMatthias Ringwald static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 7039bda6d5Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size); 7139bda6d5Smatthias.ringwald 7239bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 732b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 742b83fb7dSmatthias.ringwald static int signaling_responses_pending; 752b83fb7dSmatthias.ringwald 76665d90f2SMatthias Ringwald static btstack_btstack_linked_list_t l2cap_channels; 77665d90f2SMatthias Ringwald static btstack_btstack_linked_list_t l2cap_services; 78665d90f2SMatthias Ringwald static btstack_btstack_linked_list_t l2cap_le_channels; 79665d90f2SMatthias Ringwald static btstack_btstack_linked_list_t l2cap_le_services; 80ffbf8201SMatthias Ringwald static void (*packet_handler) (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 */ 120ffbf8201SMatthias Ringwald static void null_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 121fcadd0caSmatthias.ringwald } 122ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(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 1277f02f414SMatthias 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 { 131ffbf8201SMatthias Ringwald (*packet_handler)(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 1807f02f414SMatthias 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)); 187ffbf8201SMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 188ccf076adS[email protected] } 189ccf076adS[email protected] 1907f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 191665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 192665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 193665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 194665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 195b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 196f62db1e3Smatthias.ringwald return channel; 197f62db1e3Smatthias.ringwald } 198f62db1e3Smatthias.ringwald } 199f62db1e3Smatthias.ringwald return NULL; 200f62db1e3Smatthias.ringwald } 201f62db1e3Smatthias.ringwald 2026b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2036b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2046b1fde37Smatthias.ringwald if (!channel) return 0; 205a35252c8S[email protected] return hci_can_send_acl_packet_now(channel->handle); 2067856fb31S[email protected] } 2077856fb31S[email protected] 2086cd4da6bS[email protected] // @deprecated 2093cab4fcaS[email protected] int l2cap_can_send_connectionless_packet_now(void){ 210a35252c8S[email protected] // TODO provide real handle 2116cd4da6bS[email protected] return l2cap_can_send_fixed_channel_packet_now(0x1234); 2126cd4da6bS[email protected] } 2136cd4da6bS[email protected] 2146cd4da6bS[email protected] int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 2156cd4da6bS[email protected] return hci_can_send_acl_packet_now(handle); 2163cab4fcaS[email protected] } 2173cab4fcaS[email protected] 21896cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 21996cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 22096cbd662Smatthias.ringwald if (channel) { 22196cbd662Smatthias.ringwald return channel->remote_mtu; 22296cbd662Smatthias.ringwald } 22396cbd662Smatthias.ringwald return 0; 22496cbd662Smatthias.ringwald } 22596cbd662Smatthias.ringwald 2265932bd7cS[email protected] static l2cap_channel_t * l2cap_channel_for_rtx_timer(timer_source_t * ts){ 227665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 228665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 229665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 230665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2315932bd7cS[email protected] if ( &channel->rtx == ts) { 2325932bd7cS[email protected] return channel; 2335932bd7cS[email protected] } 2345932bd7cS[email protected] } 2355932bd7cS[email protected] return NULL; 2365932bd7cS[email protected] } 2375932bd7cS[email protected] 2385932bd7cS[email protected] static void l2cap_rtx_timeout(timer_source_t * ts){ 2395932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 2405932bd7cS[email protected] if (!ts) return; 2415932bd7cS[email protected] 2425932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 2435932bd7cS[email protected] 2445932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 2455932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 2465932bd7cS[email protected] // notify client 2475932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 2485932bd7cS[email protected] 2495932bd7cS[email protected] // discard channel 2509dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 251665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 2525932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 2535932bd7cS[email protected] } 2545932bd7cS[email protected] 2555932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 2565932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 2575932bd7cS[email protected] run_loop_remove_timer(&channel->rtx); 2585932bd7cS[email protected] } 2595932bd7cS[email protected] 2605932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 2615932bd7cS[email protected] l2cap_stop_rtx(channel); 262cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 2635932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 2645932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 2655932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 2665932bd7cS[email protected] } 2675932bd7cS[email protected] 2685932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 2695932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 2705932bd7cS[email protected] l2cap_stop_rtx(channel); 2715932bd7cS[email protected] run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 2725932bd7cS[email protected] run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 2735932bd7cS[email protected] run_loop_add_timer(&channel->rtx); 2745932bd7cS[email protected] } 2755932bd7cS[email protected] 27671de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 277ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 278ac301f95S[email protected] } 279ac301f95S[email protected] 280df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 281ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 282df3354fcS[email protected] } 2835932bd7cS[email protected] 2847f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 285b1d43497Smatthias.ringwald 286a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 2879da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 288b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 289b1d43497Smatthias.ringwald } 290b1d43497Smatthias.ringwald 2919da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 2922a373862S[email protected] hci_reserve_packet_buffer(); 293facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 29458de5610Smatthias.ringwald va_list argptr; 29558de5610Smatthias.ringwald va_start(argptr, identifier); 29670efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 29758de5610Smatthias.ringwald va_end(argptr); 2989da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 299826f7347S[email protected] return hci_send_acl_packet_buffer(len); 30058de5610Smatthias.ringwald } 30158de5610Smatthias.ringwald 30270efece1S[email protected] #ifdef HAVE_BLE 3037f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 30470efece1S[email protected] 305a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3069da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 30770efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 30870efece1S[email protected] } 30970efece1S[email protected] 3109da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3112a373862S[email protected] hci_reserve_packet_buffer(); 312facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 31370efece1S[email protected] va_list argptr; 31470efece1S[email protected] va_start(argptr, identifier); 31570efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 31670efece1S[email protected] va_end(argptr); 3179da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 318826f7347S[email protected] return hci_send_acl_packet_buffer(len); 31970efece1S[email protected] } 32070efece1S[email protected] #endif 32170efece1S[email protected] 322b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 323facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 324b1d43497Smatthias.ringwald } 3256218e6f1Smatthias.ringwald 3266b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3276b4af23dS[email protected] return hci_reserve_packet_buffer(); 3286b4af23dS[email protected] } 3296b4af23dS[email protected] 33068a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 33168a0fcf7S[email protected] hci_release_packet_buffer(); 33268a0fcf7S[email protected] } 33368a0fcf7S[email protected] 33468a0fcf7S[email protected] 335b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 336b1d43497Smatthias.ringwald 337c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 338c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 339c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 340c8b9416aS[email protected] } 341c8b9416aS[email protected] 34258de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 343b1d43497Smatthias.ringwald if (!channel) { 3449da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 345b1d43497Smatthias.ringwald return -1; // TODO: define error 3466218e6f1Smatthias.ringwald } 3476218e6f1Smatthias.ringwald 348a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 3499da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 350a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 351a35252c8S[email protected] } 352a35252c8S[email protected] 3533aff022fSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle); 354b1d43497Smatthias.ringwald 355facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 356b1d43497Smatthias.ringwald 357e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 358e9772277S[email protected] 359e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 360e9772277S[email protected] bt_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 36158de5610Smatthias.ringwald // 2 - ACL length 36258de5610Smatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 36358de5610Smatthias.ringwald // 4 - L2CAP packet length 36458de5610Smatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 36558de5610Smatthias.ringwald // 6 - L2CAP channel DEST 36658de5610Smatthias.ringwald bt_store_16(acl_buffer, 6, channel->remote_cid); 36758de5610Smatthias.ringwald // send 368826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 36991b99603Smatthias.ringwald 3706218e6f1Smatthias.ringwald return err; 37158de5610Smatthias.ringwald } 37258de5610Smatthias.ringwald 3732149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 3742149f12eSmatthias.ringwald 375c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 376c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 3772149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 3782149f12eSmatthias.ringwald } 3792149f12eSmatthias.ringwald 380a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(handle)){ 3819da54300S[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 382c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 383c8b9416aS[email protected] } 384c8b9416aS[email protected] 3859da54300S[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 3862149f12eSmatthias.ringwald 387facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 3882149f12eSmatthias.ringwald 389e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 390e9772277S[email protected] 391e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 392e9772277S[email protected] bt_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 3932149f12eSmatthias.ringwald // 2 - ACL length 3942149f12eSmatthias.ringwald bt_store_16(acl_buffer, 2, len + 4); 3952149f12eSmatthias.ringwald // 4 - L2CAP packet length 3962149f12eSmatthias.ringwald bt_store_16(acl_buffer, 4, len + 0); 3972149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 3982149f12eSmatthias.ringwald bt_store_16(acl_buffer, 6, cid); 3992149f12eSmatthias.ringwald // send 400826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4012149f12eSmatthias.ringwald 4022149f12eSmatthias.ringwald return err; 4032149f12eSmatthias.ringwald } 4042149f12eSmatthias.ringwald 405b1d43497Smatthias.ringwald int l2cap_send_internal(uint16_t local_cid, uint8_t *data, uint16_t len){ 406b1d43497Smatthias.ringwald 407a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 408a35252c8S[email protected] if (!channel) { 4099da54300S[email protected] log_error("l2cap_send_internal no channel for cid 0x%02x", local_cid); 410a35252c8S[email protected] return -1; // TODO: define error 411a35252c8S[email protected] } 412a35252c8S[email protected] 413f0efaa57S[email protected] if (len > channel->remote_mtu){ 414f0efaa57S[email protected] log_error("l2cap_send_internal cid 0x%02x, data length exceeds remote MTU.", local_cid); 415f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 416f0efaa57S[email protected] } 417f0efaa57S[email protected] 418a35252c8S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)){ 4199da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", local_cid); 420b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 421b1d43497Smatthias.ringwald } 422b1d43497Smatthias.ringwald 4232a373862S[email protected] hci_reserve_packet_buffer(); 424facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 425b1d43497Smatthias.ringwald 426b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 427b1d43497Smatthias.ringwald 428b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 429b1d43497Smatthias.ringwald } 430b1d43497Smatthias.ringwald 4312149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 4322149f12eSmatthias.ringwald 433a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4349da54300S[email protected] log_info("l2cap_send_internal cid 0x%02x, cannot send", cid); 4352149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4362149f12eSmatthias.ringwald } 4372149f12eSmatthias.ringwald 4382a373862S[email protected] hci_reserve_packet_buffer(); 439facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4402149f12eSmatthias.ringwald 4412149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 4422149f12eSmatthias.ringwald 4432149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 4442149f12eSmatthias.ringwald } 4452149f12eSmatthias.ringwald 4460e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 4470e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 4480e37e417S[email protected] } 4490e37e417S[email protected] 45028ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 45128ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 45228ca2b46S[email protected] } 45328ca2b46S[email protected] 45428ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 45528ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 45628ca2b46S[email protected] } 45728ca2b46S[email protected] 45828ca2b46S[email protected] 459b1d43497Smatthias.ringwald 4608158c421Smatthias.ringwald // MARK: L2CAP_RUN 4612cd0be45Smatthias.ringwald // process outstanding signaling tasks 4627f02f414SMatthias Ringwald static void l2cap_run(void){ 4632b83fb7dSmatthias.ringwald 46422c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 46522c29ab4SMatthias Ringwald 4662b83fb7dSmatthias.ringwald // check pending signaling responses 4672b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 4682b83fb7dSmatthias.ringwald 4692b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 470a35252c8S[email protected] 471a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 472a35252c8S[email protected] 4732b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 4742b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 47563a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 476f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 4772b83fb7dSmatthias.ringwald 478f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 479f53da564S[email protected] signaling_responses_pending--; 480f53da564S[email protected] int i; 481f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 482f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 483f53da564S[email protected] } 484f53da564S[email protected] 485f53da564S[email protected] switch (response_code){ 4862b360848Smatthias.ringwald case CONNECTION_REQUEST: 4872b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 4882bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 4894d816277S[email protected] if (result == 0x0003){ 4902bd8b7e7S[email protected] hci_disconnect_security_block(handle); 4914d816277S[email protected] } 4922b360848Smatthias.ringwald break; 4932b83fb7dSmatthias.ringwald case ECHO_REQUEST: 4942b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 4952b83fb7dSmatthias.ringwald break; 4962b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 4973b0484b3S[email protected] switch (infoType){ 4983b0484b3S[email protected] case 1: { // Connectionless MTU 4993b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5003b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5013b0484b3S[email protected] break; 5023b0484b3S[email protected] } 5033b0484b3S[email protected] case 2: { // Extended Features Supported 504462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 505462e630dS[email protected] uint32_t features = 0x280; 5063b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5073b0484b3S[email protected] break; 5083b0484b3S[email protected] } 5093b0484b3S[email protected] case 3: { // Fixed Channels Supported 5103b0484b3S[email protected] uint8_t map[8]; 5113b0484b3S[email protected] memset(map, 0, 8); 512462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5133b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5143b0484b3S[email protected] break; 5153b0484b3S[email protected] } 5163b0484b3S[email protected] default: 5172b83fb7dSmatthias.ringwald // all other types are not supported 5182b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5193b0484b3S[email protected] break; 5202b83fb7dSmatthias.ringwald } 5212b83fb7dSmatthias.ringwald break; 52263a7246aSmatthias.ringwald case COMMAND_REJECT: 5235ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 52470efece1S[email protected] #ifdef HAVE_BLE 52570efece1S[email protected] case COMMAND_REJECT_LE: 52670efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 52763a7246aSmatthias.ringwald break; 52870efece1S[email protected] #endif 5292b83fb7dSmatthias.ringwald default: 5302b83fb7dSmatthias.ringwald // should not happen 5312b83fb7dSmatthias.ringwald break; 5322b83fb7dSmatthias.ringwald } 5332b83fb7dSmatthias.ringwald } 5342b83fb7dSmatthias.ringwald 535ae280e73Smatthias.ringwald uint8_t config_options[4]; 536665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 537665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 538665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 539baf94f06S[email protected] 540665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 54122c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 5422cd0be45Smatthias.ringwald switch (channel->state){ 5432cd0be45Smatthias.ringwald 544df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 545ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 546baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 547a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 548ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 549a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 550ad671560S[email protected] } 551ad671560S[email protected] break; 552ad671560S[email protected] 55302b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 554baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 55564472d52Smatthias.ringwald // send connection request - set state first 55664472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 55702b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 5588f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 55902b22dc4Smatthias.ringwald break; 56002b22dc4Smatthias.ringwald 561e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 562baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 56322c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 5641eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 565e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 5669dcb2fb2S[email protected] l2cap_stop_rtx(channel); 567665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 568d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 569e7ff783cSmatthias.ringwald break; 570e7ff783cSmatthias.ringwald 571552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 572baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 573fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 57428ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 5752a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 576552d92a1Smatthias.ringwald break; 577552d92a1Smatthias.ringwald 5786fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 579baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 5806fdcc387Smatthias.ringwald // success, start l2cap handshake 581b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 5826fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 5832a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 5845932bd7cS[email protected] l2cap_start_rtx(channel); 5856fdcc387Smatthias.ringwald break; 5866fdcc387Smatthias.ringwald 587fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 588baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 58973cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 59063a7246aSmatthias.ringwald uint16_t flags = 0; 59128ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 59263a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 59363a7246aSmatthias.ringwald flags = 1; 59463a7246aSmatthias.ringwald } else { 59528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 59663a7246aSmatthias.ringwald } 59763a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 59863a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 59963a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 60063a7246aSmatthias.ringwald config_options[0] = 1; // MTU 60163a7246aSmatthias.ringwald config_options[1] = 2; // len param 60263a7246aSmatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 60363a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 60463a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 60563a7246aSmatthias.ringwald } else { 60663a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 60763a7246aSmatthias.ringwald } 60863a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 609fa8473a4Smatthias.ringwald } 61073cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 61128ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 61228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 613b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 614ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 615ae280e73Smatthias.ringwald config_options[1] = 2; // len param 616ae280e73Smatthias.ringwald bt_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 617b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6185932bd7cS[email protected] l2cap_start_rtx(channel); 619fa8473a4Smatthias.ringwald } 620fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 621552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 622552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 623fa8473a4Smatthias.ringwald } 624552d92a1Smatthias.ringwald break; 625552d92a1Smatthias.ringwald 626e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 627baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 62822c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 629b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6305932bd7cS[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 :) 631756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 632e7ff783cSmatthias.ringwald break; 633e7ff783cSmatthias.ringwald 634e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 635baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 636b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6372cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 6382a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 6392cd0be45Smatthias.ringwald break; 6402cd0be45Smatthias.ringwald default: 6412cd0be45Smatthias.ringwald break; 6422cd0be45Smatthias.ringwald } 6432cd0be45Smatthias.ringwald } 644da886c03S[email protected] 6454d7157c3S[email protected] #ifdef HAVE_BLE 646da886c03S[email protected] // send l2cap con paramter update if necessary 647da886c03S[email protected] hci_connections_get_iterator(&it); 648665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 649665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 6505d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 651b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 652da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 653b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 654b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 655b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 656b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 657b68d7bc3SMatthias Ringwald break; 658da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 659b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 660b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 661da886c03S[email protected] break; 662da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 663b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 664b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 665da886c03S[email protected] break; 666da886c03S[email protected] default: 667da886c03S[email protected] break; 668da886c03S[email protected] } 669da886c03S[email protected] } 6704d7157c3S[email protected] #endif 671da886c03S[email protected] 67222c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 6732cd0be45Smatthias.ringwald } 6742cd0be45Smatthias.ringwald 6754aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 6764ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 677fa8c92f6Smatthias.ringwald } 678fa8c92f6Smatthias.ringwald 67971de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 6804ff786cfS[email protected] return l2cap_max_mtu(); 681e5e1518dS[email protected] } 682e5e1518dS[email protected] 6832df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 6842df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 6855533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 6862df5dadcS[email protected] // success, start l2cap handshake 6872df5dadcS[email protected] channel->handle = handle; 6882df5dadcS[email protected] // check remote SSP feature first 6892df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 6902df5dadcS[email protected] } 6912df5dadcS[email protected] } 6922df5dadcS[email protected] 6932df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 6942df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 6952df5dadcS[email protected] 6962df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 697ac301f95S[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)); 6982df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 6992df5dadcS[email protected] // request security level 2 7002df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 7011429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 7022df5dadcS[email protected] return; 7032df5dadcS[email protected] } 7042df5dadcS[email protected] // fine, go ahead 7052df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7062df5dadcS[email protected] } 7072df5dadcS[email protected] 7089077cb15SMatthias Ringwald /** 7099077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 7109077cb15SMatthias Ringwald * @param packet_handler 7119077cb15SMatthias Ringwald * @param address 7129077cb15SMatthias Ringwald * @param psm 7139077cb15SMatthias Ringwald * @param mtu 7149077cb15SMatthias Ringwald * @param local_cid 7159077cb15SMatthias Ringwald */ 7169077cb15SMatthias Ringwald uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid){ 7179077cb15SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 7189077cb15SMatthias Ringwald 7199077cb15SMatthias Ringwald // alloc structure 7209077cb15SMatthias Ringwald l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 7219077cb15SMatthias Ringwald if (!chan) { 7229077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 7239077cb15SMatthias Ringwald } 7249077cb15SMatthias Ringwald 7259077cb15SMatthias Ringwald // Init memory (make valgrind happy) 7269077cb15SMatthias Ringwald memset(chan, 0, sizeof(l2cap_channel_t)); 7279077cb15SMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 7289077cb15SMatthias Ringwald if (mtu > l2cap_max_mtu()) { 7299077cb15SMatthias Ringwald mtu = l2cap_max_mtu(); 7309077cb15SMatthias Ringwald } 7319077cb15SMatthias Ringwald 7329077cb15SMatthias Ringwald // fill in 7339077cb15SMatthias Ringwald BD_ADDR_COPY(chan->address, address); 7349077cb15SMatthias Ringwald chan->psm = psm; 7359077cb15SMatthias Ringwald chan->handle = 0; 7369077cb15SMatthias Ringwald chan->packet_handler = channel_packet_handler; 7379077cb15SMatthias Ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 7389077cb15SMatthias Ringwald chan->local_mtu = mtu; 7399077cb15SMatthias Ringwald chan->local_cid = l2cap_next_local_cid(); 7409077cb15SMatthias Ringwald 7419077cb15SMatthias Ringwald // set initial state 7429077cb15SMatthias Ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 7439077cb15SMatthias Ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 7449077cb15SMatthias Ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 7459077cb15SMatthias Ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 7469077cb15SMatthias Ringwald chan->required_security_level = LEVEL_0; 7479077cb15SMatthias Ringwald 7489077cb15SMatthias Ringwald // add to connections list 749665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan); 7509077cb15SMatthias Ringwald 7519077cb15SMatthias Ringwald // store local_cid 7529077cb15SMatthias Ringwald if (out_local_cid){ 7539077cb15SMatthias Ringwald *out_local_cid = chan->local_cid; 7549077cb15SMatthias Ringwald } 7559077cb15SMatthias Ringwald 7569077cb15SMatthias Ringwald // check if hci connection is already usable 7579077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 7589077cb15SMatthias Ringwald if (conn){ 7599077cb15SMatthias Ringwald log_info("l2cap_create_channel_internal, hci connection already exists"); 7609077cb15SMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, chan); 7619077cb15SMatthias Ringwald // check if remote supported fearures are already received 7629077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 7639077cb15SMatthias Ringwald l2cap_handle_remote_supported_features_received(chan); 7649077cb15SMatthias Ringwald } 7659077cb15SMatthias Ringwald } 7669077cb15SMatthias Ringwald 7679077cb15SMatthias Ringwald l2cap_run(); 7689077cb15SMatthias Ringwald 7699077cb15SMatthias Ringwald return 0; 7709077cb15SMatthias Ringwald } 7719077cb15SMatthias Ringwald 772b35f641cSmatthias.ringwald void l2cap_disconnect_internal(uint16_t local_cid, uint8_t reason){ 773e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 774b35f641cSmatthias.ringwald // find channel for local_cid 775b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 776f62db1e3Smatthias.ringwald if (channel) { 777e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 778f62db1e3Smatthias.ringwald } 7792cd0be45Smatthias.ringwald // process 7802cd0be45Smatthias.ringwald l2cap_run(); 78143625864Smatthias.ringwald } 7821e6aba47Smatthias.ringwald 783afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 784665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 785665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 786665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 787665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 788c22aecc9S[email protected] if ( BD_ADDR_CMP( channel->address, address) != 0) continue; 789c22aecc9S[email protected] // channel for this address found 790c22aecc9S[email protected] switch (channel->state){ 791c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 792c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 793afde0c52Smatthias.ringwald // failure, forward error code 794afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 795afde0c52Smatthias.ringwald // discard channel 7969dcb2fb2S[email protected] l2cap_stop_rtx(channel); 797665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 798d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 799c22aecc9S[email protected] break; 800c22aecc9S[email protected] default: 801c22aecc9S[email protected] break; 802afde0c52Smatthias.ringwald } 803afde0c52Smatthias.ringwald } 804afde0c52Smatthias.ringwald } 805afde0c52Smatthias.ringwald 806afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 807665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 808665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 809665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 810665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 811afde0c52Smatthias.ringwald if ( ! BD_ADDR_CMP( channel->address, address) ){ 8122df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 813afde0c52Smatthias.ringwald } 814afde0c52Smatthias.ringwald } 8156fdcc387Smatthias.ringwald // process 8166fdcc387Smatthias.ringwald l2cap_run(); 817afde0c52Smatthias.ringwald } 818b448a0e7Smatthias.ringwald 8197f02f414SMatthias Ringwald static void l2cap_event_handler(uint8_t *packet, uint16_t size){ 820afde0c52Smatthias.ringwald 821afde0c52Smatthias.ringwald bd_addr_t address; 822afde0c52Smatthias.ringwald hci_con_handle_t handle; 823665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8242d00edd4Smatthias.ringwald int hci_con_used; 825afde0c52Smatthias.ringwald 826afde0c52Smatthias.ringwald switch(packet[0]){ 827afde0c52Smatthias.ringwald 828afde0c52Smatthias.ringwald // handle connection complete events 829afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 830afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[5]); 831afde0c52Smatthias.ringwald if (packet[2] == 0){ 832afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 833afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 834afde0c52Smatthias.ringwald } else { 835afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 836afde0c52Smatthias.ringwald } 837afde0c52Smatthias.ringwald break; 838afde0c52Smatthias.ringwald 839afde0c52Smatthias.ringwald // handle successful create connection cancel command 840afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 841afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 842afde0c52Smatthias.ringwald if (packet[5] == 0){ 843afde0c52Smatthias.ringwald bt_flip_addr(address, &packet[6]); 844afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 845afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 84603cfbabcSmatthias.ringwald } 8471e6aba47Smatthias.ringwald } 84839d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 84939d59809Smatthias.ringwald break; 85039d59809Smatthias.ringwald 85139d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 85239d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 853afde0c52Smatthias.ringwald break; 85427a923d0Smatthias.ringwald 8551e6aba47Smatthias.ringwald // handle disconnection complete events 856afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 857c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 858afde0c52Smatthias.ringwald handle = READ_BT_16(packet, 3); 859665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 860665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 861665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 862c22aecc9S[email protected] if (channel->handle != handle) continue; 86315ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 8649dcb2fb2S[email protected] l2cap_stop_rtx(channel); 865665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 866d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 86727a923d0Smatthias.ringwald } 868afde0c52Smatthias.ringwald break; 869fcadd0caSmatthias.ringwald 8706218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 87102b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 8726218e6f1Smatthias.ringwald break; 8736218e6f1Smatthias.ringwald 874ee091cf1Smatthias.ringwald // HCI Connection Timeouts 875afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 87636944dffSmatthias.ringwald handle = READ_BT_16(packet, 2); 877bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 87880ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 8792d00edd4Smatthias.ringwald hci_con_used = 0; 880665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 881665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 882665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 883c22aecc9S[email protected] if (channel->handle != handle) continue; 8842d00edd4Smatthias.ringwald hci_con_used = 1; 885c22aecc9S[email protected] break; 886ee091cf1Smatthias.ringwald } 8872d00edd4Smatthias.ringwald if (hci_con_used) break; 888d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 8899edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 890afde0c52Smatthias.ringwald break; 891ee091cf1Smatthias.ringwald 8926e6710ebSmatthias.ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 893b26c3003SMatthias Ringwald l2cap_run(); // try sending signaling packets first 894b26c3003SMatthias Ringwald 895665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 896665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 897665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 898c22aecc9S[email protected] if (!channel->packet_handler) continue; 8996e6710ebSmatthias.ringwald (* (channel->packet_handler))(HCI_EVENT_PACKET, channel->local_cid, packet, size); 9006e6710ebSmatthias.ringwald } 9015652b5ffS[email protected] if (attribute_protocol_packet_handler) { 9025652b5ffS[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9035652b5ffS[email protected] } 9045652b5ffS[email protected] if (security_protocol_packet_handler) { 905133efcfdSmatthias.ringwald (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 9065652b5ffS[email protected] } 907462e630dS[email protected] if (connectionless_channel_packet_handler) { 908462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 909462e630dS[email protected] } 9106e6710ebSmatthias.ringwald break; 9116e6710ebSmatthias.ringwald 912df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 913df3354fcS[email protected] handle = READ_BT_16(packet, 3); 914665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 915665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 916665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 917df3354fcS[email protected] if (channel->handle != handle) continue; 9182df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 919df3354fcS[email protected] break; 920df3354fcS[email protected] } 921c22aecc9S[email protected] break; 922df3354fcS[email protected] 923a00031e2S[email protected] case GAP_SECURITY_LEVEL: 924a00031e2S[email protected] handle = READ_BT_16(packet, 2); 925bd63148eS[email protected] log_info("l2cap - security level update"); 926665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 927665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 928665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 929f85a9399S[email protected] if (channel->handle != handle) continue; 9305533f01eS[email protected] 931bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 932bd63148eS[email protected] 933e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 9345533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 9355533f01eS[email protected] 936df3354fcS[email protected] switch (channel->state){ 937df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 9385533f01eS[email protected] if (actual_level >= required_level){ 939f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 940f85a9399S[email protected] l2cap_emit_connection_request(channel); 9411eb2563eS[email protected] } else { 942775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 9431eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 9441eb2563eS[email protected] } 945df3354fcS[email protected] break; 946df3354fcS[email protected] 947df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 9485533f01eS[email protected] if (actual_level >= required_level){ 949df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 950df3354fcS[email protected] } else { 951df3354fcS[email protected] // disconnnect, authentication not good enough 952df3354fcS[email protected] hci_disconnect_security_block(handle); 953df3354fcS[email protected] } 954df3354fcS[email protected] break; 955df3354fcS[email protected] 956df3354fcS[email protected] default: 957df3354fcS[email protected] break; 958df3354fcS[email protected] } 959f85a9399S[email protected] } 960f85a9399S[email protected] break; 961f85a9399S[email protected] 962afde0c52Smatthias.ringwald default: 963afde0c52Smatthias.ringwald break; 964afde0c52Smatthias.ringwald } 965afde0c52Smatthias.ringwald 966e0707417S[email protected] // pass on: main packet handler, att and sm packet handlers 967ffbf8201SMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 968e0707417S[email protected] if (attribute_protocol_packet_handler){ 969e0707417S[email protected] (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 970e0707417S[email protected] } 971e0707417S[email protected] if (security_protocol_packet_handler) { 972e0707417S[email protected] (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 973e0707417S[email protected] } 974462e630dS[email protected] if (connectionless_channel_packet_handler) { 975462e630dS[email protected] (*connectionless_channel_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 976462e630dS[email protected] } 977bd63148eS[email protected] 978bd63148eS[email protected] l2cap_run(); 9791e6aba47Smatthias.ringwald } 9801e6aba47Smatthias.ringwald 981afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 982b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 983e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 984e7ff783cSmatthias.ringwald l2cap_run(); 98584836b65Smatthias.ringwald } 98684836b65Smatthias.ringwald 9872b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 9884cf56b4aSmatthias.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." 9892b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 9902b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 9912b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 9922b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 9932b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 9942b360848Smatthias.ringwald signaling_responses_pending++; 9952b360848Smatthias.ringwald l2cap_run(); 9962b360848Smatthias.ringwald } 9972b360848Smatthias.ringwald } 9982b360848Smatthias.ringwald 999b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1000645658c9Smatthias.ringwald 10019da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1002645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1003645658c9Smatthias.ringwald if (!service) { 1004645658c9Smatthias.ringwald // 0x0002 PSM not supported 10052b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1006645658c9Smatthias.ringwald return; 1007645658c9Smatthias.ringwald } 1008645658c9Smatthias.ringwald 10095061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1010645658c9Smatthias.ringwald if (!hci_connection) { 10112b360848Smatthias.ringwald // 10129da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1013645658c9Smatthias.ringwald return; 1014645658c9Smatthias.ringwald } 10152bd8b7e7S[email protected] 1016645658c9Smatthias.ringwald // alloc structure 10179da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1018bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 10192b360848Smatthias.ringwald if (!channel){ 10202b360848Smatthias.ringwald // 0x0004 No resources available 10212b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 10222b360848Smatthias.ringwald return; 10232b360848Smatthias.ringwald } 1024c523d53dS[email protected] // Init memory (make valgrind happy) 1025c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1026645658c9Smatthias.ringwald // fill in 1027169f8b28Smatthias.ringwald BD_ADDR_COPY(channel->address, hci_connection->address); 1028169f8b28Smatthias.ringwald channel->psm = psm; 1029169f8b28Smatthias.ringwald channel->handle = handle; 1030f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1031b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1032b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1033fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 10340a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1035b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1036df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1037645658c9Smatthias.ringwald 1038f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 10392985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 10402985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 10419775e25bSmatthias.ringwald } 10429775e25bSmatthias.ringwald 1043645658c9Smatthias.ringwald // set initial state 1044df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1045ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1046e405ae81Smatthias.ringwald 1047645658c9Smatthias.ringwald // add to connections list 1048665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1049645658c9Smatthias.ringwald 1050f85a9399S[email protected] // assert security requirements 10511eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1052e405ae81Smatthias.ringwald } 1053645658c9Smatthias.ringwald 1054b35f641cSmatthias.ringwald void l2cap_accept_connection_internal(uint16_t local_cid){ 1055e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1056b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1057e405ae81Smatthias.ringwald if (!channel) { 10587d67539fSmatthias.ringwald log_error("l2cap_accept_connection_internal called but local_cid 0x%x not found", local_cid); 1059e405ae81Smatthias.ringwald return; 1060e405ae81Smatthias.ringwald } 1061e405ae81Smatthias.ringwald 1062552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1063e405ae81Smatthias.ringwald 1064552d92a1Smatthias.ringwald // process 1065552d92a1Smatthias.ringwald l2cap_run(); 1066e405ae81Smatthias.ringwald } 1067645658c9Smatthias.ringwald 1068b35f641cSmatthias.ringwald void l2cap_decline_connection_internal(uint16_t local_cid, uint8_t reason){ 1069e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1070b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1071e405ae81Smatthias.ringwald if (!channel) { 10727d67539fSmatthias.ringwald log_error( "l2cap_decline_connection_internal called but local_cid 0x%x not found", local_cid); 1073e405ae81Smatthias.ringwald return; 1074e405ae81Smatthias.ringwald } 1075e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1076e7ff783cSmatthias.ringwald channel->reason = reason; 1077e7ff783cSmatthias.ringwald l2cap_run(); 1078645658c9Smatthias.ringwald } 1079645658c9Smatthias.ringwald 10807f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1081b1988dceSmatthias.ringwald 1082b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1083b1988dceSmatthias.ringwald 108463a7246aSmatthias.ringwald uint16_t flags = READ_BT_16(command, 6); 108563a7246aSmatthias.ringwald if (flags & 1) { 108663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 108763a7246aSmatthias.ringwald } 108863a7246aSmatthias.ringwald 10892784b77dSmatthias.ringwald // accept the other's configuration options 10903de7c0caSmatthias.ringwald uint16_t end_pos = 4 + READ_BT_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 10913de7c0caSmatthias.ringwald uint16_t pos = 8; 10923de7c0caSmatthias.ringwald while (pos < end_pos){ 109363a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 109463a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 109563a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 109663a7246aSmatthias.ringwald pos++; 10971dc511deSmatthias.ringwald uint8_t length = command[pos++]; 10981dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 109963a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 11001dc511deSmatthias.ringwald channel->remote_mtu = READ_BT_16(command, pos); 11019da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 110263a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 110363a7246aSmatthias.ringwald } 11040fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 11050fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 11060fe7a9d0S[email protected] channel->flush_timeout = READ_BT_16(command, pos); 11070fe7a9d0S[email protected] } 110863a7246aSmatthias.ringwald // check for unknown options 110963a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1110c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 111163a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 11121dc511deSmatthias.ringwald } 11131dc511deSmatthias.ringwald pos += length; 11141dc511deSmatthias.ringwald } 11152784b77dSmatthias.ringwald } 11162784b77dSmatthias.ringwald 1117fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 11189da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 111973cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 112073cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1121019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1122019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1123fa8473a4Smatthias.ringwald return 1; 1124fa8473a4Smatthias.ringwald } 1125fa8473a4Smatthias.ringwald 1126fa8473a4Smatthias.ringwald 11277f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 11281e6aba47Smatthias.ringwald 112900d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 113000d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 113138e5900eSmatthias.ringwald uint16_t result = 0; 11321e6aba47Smatthias.ringwald 11339da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1134b35f641cSmatthias.ringwald 11359a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 11369a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 11379a011532Smatthias.ringwald switch (channel->state){ 1138fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 11399a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 11402b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 11419a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 11429a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 11439a011532Smatthias.ringwald break; 11449a011532Smatthias.ringwald 11459a011532Smatthias.ringwald default: 11469a011532Smatthias.ringwald // ignore in other states 11479a011532Smatthias.ringwald break; 11489a011532Smatthias.ringwald } 11499a011532Smatthias.ringwald return; 11509a011532Smatthias.ringwald } 11519a011532Smatthias.ringwald 115256081214Smatthias.ringwald // @STATEMACHINE(l2cap) 11531e6aba47Smatthias.ringwald switch (channel->state) { 11541e6aba47Smatthias.ringwald 11551e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 11561e6aba47Smatthias.ringwald switch (code){ 11571e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 11585932bd7cS[email protected] l2cap_stop_rtx(channel); 115900d93d79Smatthias.ringwald result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 116038e5900eSmatthias.ringwald switch (result) { 116138e5900eSmatthias.ringwald case 0: 1162169f8b28Smatthias.ringwald // successful connection 116300d93d79Smatthias.ringwald channel->remote_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1164fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 116528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 116638e5900eSmatthias.ringwald break; 116738e5900eSmatthias.ringwald case 1: 11685932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 11695932bd7cS[email protected] l2cap_start_ertx(channel); 117038e5900eSmatthias.ringwald break; 117138e5900eSmatthias.ringwald default: 1172eb920dbeSmatthias.ringwald // channel closed 1173eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1174f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 117538e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1176eb920dbeSmatthias.ringwald 1177eb920dbeSmatthias.ringwald // drop link key if security block 1178eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 11792e77e513S[email protected] hci_drop_link_key_for_bd_addr(channel->address); 1180eb920dbeSmatthias.ringwald } 1181eb920dbeSmatthias.ringwald 1182eb920dbeSmatthias.ringwald // discard channel 1183665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1184d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 118538e5900eSmatthias.ringwald break; 11861e6aba47Smatthias.ringwald } 11871e6aba47Smatthias.ringwald break; 118838e5900eSmatthias.ringwald 118938e5900eSmatthias.ringwald default: 11901e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 119138e5900eSmatthias.ringwald break; 11921e6aba47Smatthias.ringwald } 11931e6aba47Smatthias.ringwald break; 11941e6aba47Smatthias.ringwald 1195fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1196fe9d8984S[email protected] result = READ_BT_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1197ae280e73Smatthias.ringwald switch (code) { 1198ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 119928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1200ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 120163a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 120263a7246aSmatthias.ringwald // only done if continuation not set 120363a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 120463a7246aSmatthias.ringwald } 1205ae280e73Smatthias.ringwald break; 12061e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 12075932bd7cS[email protected] l2cap_stop_rtx(channel); 12085932bd7cS[email protected] switch (result){ 12095932bd7cS[email protected] case 0: // success 12105932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 12115932bd7cS[email protected] break; 12125932bd7cS[email protected] case 4: // pending 12135932bd7cS[email protected] l2cap_start_ertx(channel); 12145932bd7cS[email protected] break; 12155932bd7cS[email protected] default: 1216fe9d8984S[email protected] // retry on negative result 1217fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1218fe9d8984S[email protected] break; 1219fe9d8984S[email protected] } 12205a67bd4aSmatthias.ringwald break; 12215a67bd4aSmatthias.ringwald default: 12225a67bd4aSmatthias.ringwald break; 12231e6aba47Smatthias.ringwald } 1224fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1225fa8473a4Smatthias.ringwald // for open: 12265a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1227fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1228c8e4258aSmatthias.ringwald } 1229c8e4258aSmatthias.ringwald break; 1230f62db1e3Smatthias.ringwald 1231f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1232f62db1e3Smatthias.ringwald switch (code) { 1233f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 123427a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 123527a923d0Smatthias.ringwald break; 12365a67bd4aSmatthias.ringwald default: 12375a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 12385a67bd4aSmatthias.ringwald break; 123927a923d0Smatthias.ringwald } 124027a923d0Smatthias.ringwald break; 124184836b65Smatthias.ringwald 124284836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 124384836b65Smatthias.ringwald // @TODO handle incoming requests 124484836b65Smatthias.ringwald break; 124584836b65Smatthias.ringwald 124684836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 124784836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 124884836b65Smatthias.ringwald break; 124910642e45Smatthias.ringwald default: 125010642e45Smatthias.ringwald break; 125127a923d0Smatthias.ringwald } 12529da54300S[email protected] // log_info("new state %u", channel->state); 125327a923d0Smatthias.ringwald } 125427a923d0Smatthias.ringwald 125500d93d79Smatthias.ringwald 12567f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 125700d93d79Smatthias.ringwald 125800d93d79Smatthias.ringwald // get code, signalind identifier and command len 125900d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 126000d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 126100d93d79Smatthias.ringwald 126200d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 126300d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 126463a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 126500d93d79Smatthias.ringwald return; 126600d93d79Smatthias.ringwald } 126700d93d79Smatthias.ringwald 126800d93d79Smatthias.ringwald // general commands without an assigned channel 126900d93d79Smatthias.ringwald switch(code) { 127000d93d79Smatthias.ringwald 127100d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 127200d93d79Smatthias.ringwald uint16_t psm = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 127300d93d79Smatthias.ringwald uint16_t source_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 127400d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 12752b83fb7dSmatthias.ringwald return; 127600d93d79Smatthias.ringwald } 127700d93d79Smatthias.ringwald 12782b360848Smatthias.ringwald case ECHO_REQUEST: 12792b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 12802b83fb7dSmatthias.ringwald return; 128100d93d79Smatthias.ringwald 128200d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 128300d93d79Smatthias.ringwald uint16_t infoType = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 12842b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 12852b83fb7dSmatthias.ringwald return; 128600d93d79Smatthias.ringwald } 128700d93d79Smatthias.ringwald 128800d93d79Smatthias.ringwald default: 128900d93d79Smatthias.ringwald break; 129000d93d79Smatthias.ringwald } 129100d93d79Smatthias.ringwald 129200d93d79Smatthias.ringwald 129300d93d79Smatthias.ringwald // Get potential destination CID 129400d93d79Smatthias.ringwald uint16_t dest_cid = READ_BT_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 129500d93d79Smatthias.ringwald 129600d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1297665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1298665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1299665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1300665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1301c22aecc9S[email protected] if (channel->handle != handle) continue; 130200d93d79Smatthias.ringwald if (code & 1) { 1303b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1304b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 130500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13064e32727eSmatthias.ringwald break; 130700d93d79Smatthias.ringwald } 130800d93d79Smatthias.ringwald } else { 1309b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 131000d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 131100d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13124e32727eSmatthias.ringwald break; 131300d93d79Smatthias.ringwald } 131400d93d79Smatthias.ringwald } 131500d93d79Smatthias.ringwald } 131600d93d79Smatthias.ringwald } 131700d93d79Smatthias.ringwald 13187f02f414SMatthias Ringwald static void l2cap_acl_handler( uint8_t *packet, uint16_t size ){ 131900d93d79Smatthias.ringwald 132000d93d79Smatthias.ringwald // Get Channel ID 132100d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 132200d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 132300d93d79Smatthias.ringwald 13245652b5ffS[email protected] switch (channel_id) { 13255652b5ffS[email protected] 13265652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 13275652b5ffS[email protected] 132800d93d79Smatthias.ringwald uint16_t command_offset = 8; 132900d93d79Smatthias.ringwald while (command_offset < size) { 133000d93d79Smatthias.ringwald 133100d93d79Smatthias.ringwald // handle signaling commands 133200d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 133300d93d79Smatthias.ringwald 133400d93d79Smatthias.ringwald // increment command_offset 133500d93d79Smatthias.ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + READ_BT_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 133600d93d79Smatthias.ringwald } 13375652b5ffS[email protected] break; 133800d93d79Smatthias.ringwald } 133900d93d79Smatthias.ringwald 13405652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 13415652b5ffS[email protected] if (attribute_protocol_packet_handler) { 13425652b5ffS[email protected] (*attribute_protocol_packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13435652b5ffS[email protected] } 13445652b5ffS[email protected] break; 13455652b5ffS[email protected] 13465652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 13475652b5ffS[email protected] if (security_protocol_packet_handler) { 13485652b5ffS[email protected] (*security_protocol_packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 13495652b5ffS[email protected] } 13505652b5ffS[email protected] break; 13515652b5ffS[email protected] 1352462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1353462e630dS[email protected] if (connectionless_channel_packet_handler) { 1354462e630dS[email protected] (*connectionless_channel_packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1355462e630dS[email protected] } 1356462e630dS[email protected] break; 1357462e630dS[email protected] 13581bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1359ccf076adS[email protected] switch (packet[8]){ 1360ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1361ccf076adS[email protected] uint16_t result = READ_BT_16(packet, 12); 1362ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1363ccf076adS[email protected] break; 1364ccf076adS[email protected] } 1365ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1366ccf076adS[email protected] uint8_t event[10]; 1367ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1368ccf076adS[email protected] event[1] = 8; 1369ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1370da886c03S[email protected] 1371da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1372da886c03S[email protected] if (connection){ 13736d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 13746d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 13756d91fb6cSMatthias Ringwald uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 13766d91fb6cSMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 13776d91fb6cSMatthias Ringwald break; 13786d91fb6cSMatthias Ringwald } 1379da886c03S[email protected] int update_parameter = 1; 1380a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 1381a4c06b28SMatthias Ringwald gap_le_get_connection_parameter_range(existing_range); 1382da886c03S[email protected] uint16_t le_conn_interval_min = READ_BT_16(packet,12); 1383da886c03S[email protected] uint16_t le_conn_interval_max = READ_BT_16(packet,14); 1384da886c03S[email protected] uint16_t le_conn_latency = READ_BT_16(packet,16); 1385da886c03S[email protected] uint16_t le_supervision_timeout = READ_BT_16(packet,18); 1386da886c03S[email protected] 1387da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1388da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1389da886c03S[email protected] 1390da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1391da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1392da886c03S[email protected] 1393da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1394da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1395da886c03S[email protected] 1396da886c03S[email protected] if (update_parameter){ 1397da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1398da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1399da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1400da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1401da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1402da886c03S[email protected] } else { 1403da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1404da886c03S[email protected] } 140579f53f1dSMatthias Ringwald connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1406da886c03S[email protected] } 1407da886c03S[email protected] 1408ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1409ffbf8201SMatthias Ringwald (*packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1410da886c03S[email protected] 1411ccf076adS[email protected] break; 1412ccf076adS[email protected] } 1413ccf076adS[email protected] default: { 14141bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14151bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14161bbc0b23S[email protected] break; 14171bbc0b23S[email protected] } 1418ccf076adS[email protected] } 1419ccf076adS[email protected] break; 1420ccf076adS[email protected] } 14211bbc0b23S[email protected] 14225652b5ffS[email protected] default: { 142300d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 142400d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 142500d93d79Smatthias.ringwald if (channel) { 142658de5610Smatthias.ringwald l2cap_dispatch(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 142700d93d79Smatthias.ringwald } 14285652b5ffS[email protected] break; 14295652b5ffS[email protected] } 14305652b5ffS[email protected] } 143100d93d79Smatthias.ringwald } 143200d93d79Smatthias.ringwald 14332718e2e7Smatthias.ringwald static void l2cap_packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14342718e2e7Smatthias.ringwald switch (packet_type) { 14352718e2e7Smatthias.ringwald case HCI_EVENT_PACKET: 14362718e2e7Smatthias.ringwald l2cap_event_handler(packet, size); 14372718e2e7Smatthias.ringwald break; 14382718e2e7Smatthias.ringwald case HCI_ACL_DATA_PACKET: 14392718e2e7Smatthias.ringwald l2cap_acl_handler(packet, size); 14402718e2e7Smatthias.ringwald break; 14412718e2e7Smatthias.ringwald default: 14422718e2e7Smatthias.ringwald break; 14432718e2e7Smatthias.ringwald } 14441eb2563eS[email protected] l2cap_run(); 14452718e2e7Smatthias.ringwald } 144600d93d79Smatthias.ringwald 144715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 144827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1449f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1450f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1451f62db1e3Smatthias.ringwald // discard channel 14529dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1453665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1454d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1455c8e4258aSmatthias.ringwald } 14561e6aba47Smatthias.ringwald 1457665d90f2SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_btstack_linked_list_t * services, uint16_t psm){ 1458665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1459665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1460665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1461665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 14629d9bbc01Smatthias.ringwald if ( service->psm == psm){ 14639d9bbc01Smatthias.ringwald return service; 14649d9bbc01Smatthias.ringwald }; 14659d9bbc01Smatthias.ringwald } 14669d9bbc01Smatthias.ringwald return NULL; 14679d9bbc01Smatthias.ringwald } 14689d9bbc01Smatthias.ringwald 14697192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 14707192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 14717192e786SMatthias Ringwald } 14727192e786SMatthias Ringwald 1473e0abb8e7S[email protected] 1474be2053a6SMatthias Ringwald uint8_t l2cap_register_service(btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){ 1475be2053a6SMatthias Ringwald 1476be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1477e0abb8e7S[email protected] 14784bb582b6Smatthias.ringwald // check for alread registered psm 14794bb582b6Smatthias.ringwald // TODO: emit error event 14809d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1481277abc2cSmatthias.ringwald if (service) { 1482be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1483be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1484277abc2cSmatthias.ringwald } 14859d9bbc01Smatthias.ringwald 14864bb582b6Smatthias.ringwald // alloc structure 14874bb582b6Smatthias.ringwald // TODO: emit error event 1488bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1489277abc2cSmatthias.ringwald if (!service) { 1490be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1491be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1492277abc2cSmatthias.ringwald } 14939d9bbc01Smatthias.ringwald 14949d9bbc01Smatthias.ringwald // fill in 14959d9bbc01Smatthias.ringwald service->psm = psm; 14969d9bbc01Smatthias.ringwald service->mtu = mtu; 149705ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1498df3354fcS[email protected] service->required_security_level = security_level; 14999d9bbc01Smatthias.ringwald 15009d9bbc01Smatthias.ringwald // add to services list 1501665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1502c0e866bfSmatthias.ringwald 1503c0e866bfSmatthias.ringwald // enable page scan 1504c0e866bfSmatthias.ringwald hci_connectable_control(1); 15055842b6d9Smatthias.ringwald 1506be2053a6SMatthias Ringwald return 0; 15079d9bbc01Smatthias.ringwald } 15089d9bbc01Smatthias.ringwald 150902f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){ 1510e0abb8e7S[email protected] 1511e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1512e0abb8e7S[email protected] 15139d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1514037d6e48Smatthias.ringwald if (!service) return; 1515665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1516d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1517c0e866bfSmatthias.ringwald 1518c0e866bfSmatthias.ringwald // disable page scan when no services registered 1519665d90f2SMatthias Ringwald if (!btstack_linked_list_empty(&l2cap_services)) return; 1520c0e866bfSmatthias.ringwald hci_connectable_control(0); 15219d9bbc01Smatthias.ringwald } 15229d9bbc01Smatthias.ringwald 15235652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 152405ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 15255652b5ffS[email protected] switch(channel_id){ 15265652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 152705ae8de3SMatthias Ringwald attribute_protocol_packet_handler = the_packet_handler; 15285652b5ffS[email protected] break; 15295652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 153005ae8de3SMatthias Ringwald security_protocol_packet_handler = the_packet_handler; 15315652b5ffS[email protected] break; 1532462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 153305ae8de3SMatthias Ringwald connectionless_channel_packet_handler = the_packet_handler; 1534462e630dS[email protected] break; 15355652b5ffS[email protected] } 15365652b5ffS[email protected] } 15375652b5ffS[email protected] 15387192e786SMatthias Ringwald #ifdef HAVE_BLE 15397192e786SMatthias Ringwald 15407f02f414SMatthias Ringwald 15417f02f414SMatthias Ringwald #if 0 15427192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 15437192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, psm); 15447192e786SMatthias Ringwald } 15457192e786SMatthias Ringwald /** 15467192e786SMatthias Ringwald * @brief Regster L2CAP LE Credit Based Flow Control Mode service 15477192e786SMatthias Ringwald * @param 15487192e786SMatthias Ringwald */ 1549ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, 15507192e786SMatthias Ringwald uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 15517192e786SMatthias Ringwald 15527192e786SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 15537192e786SMatthias Ringwald 15547192e786SMatthias Ringwald // check for alread registered psm 15557192e786SMatthias Ringwald // TODO: emit error event 15567192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 15577192e786SMatthias Ringwald if (service) { 15587192e786SMatthias Ringwald log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 15597192e786SMatthias Ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 15607192e786SMatthias Ringwald return; 15617192e786SMatthias Ringwald } 15627192e786SMatthias Ringwald 15637192e786SMatthias Ringwald // alloc structure 15647192e786SMatthias Ringwald // TODO: emit error event 15657192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 15667192e786SMatthias Ringwald if (!service) { 15677192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 15687192e786SMatthias Ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 15697192e786SMatthias Ringwald return; 15707192e786SMatthias Ringwald } 15717192e786SMatthias Ringwald 15727192e786SMatthias Ringwald // fill in 15737192e786SMatthias Ringwald service->psm = psm; 15747192e786SMatthias Ringwald service->mtu = mtu; 15757192e786SMatthias Ringwald service->mps = mps; 15767192e786SMatthias Ringwald service->packet_handler = packet_handler; 15777192e786SMatthias Ringwald service->required_security_level = security_level; 15787192e786SMatthias Ringwald 15797192e786SMatthias Ringwald // add to services list 1580665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 15817192e786SMatthias Ringwald 15827192e786SMatthias Ringwald // done 15837192e786SMatthias Ringwald l2cap_emit_service_registered(connection, 0, psm); 15847192e786SMatthias Ringwald } 15857192e786SMatthias Ringwald 1586ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) { 15877192e786SMatthias Ringwald 15887192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 15897192e786SMatthias Ringwald 15907192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 15917192e786SMatthias Ringwald if (!service) return; 1592665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 15937192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 15947192e786SMatthias Ringwald } 15957192e786SMatthias Ringwald #endif 15967f02f414SMatthias Ringwald #endif 1597