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" 4916ece135SMatthias Ringwald #include "btstack_debug.h" 500e2df43fSMatthias Ringwald #include "btstack_event.h" 51d3a9df87Smatthias.ringwald #include "btstack_memory.h" 5243625864Smatthias.ringwald 53cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 5483fd9c76SMatthias Ringwald #include "ble/sm.h" 5583fd9c76SMatthias Ringwald #endif 5683fd9c76SMatthias Ringwald 5743625864Smatthias.ringwald #include <stdarg.h> 5843625864Smatthias.ringwald #include <string.h> 5943625864Smatthias.ringwald 6043625864Smatthias.ringwald #include <stdio.h> 6143625864Smatthias.ringwald 624c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 634c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 644c744e21Smatthias.ringwald 6539bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 66e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 6739bda6d5Smatthias.ringwald 6885aeef60SMatthias Ringwald // nr of credits provided to remote if credits fall below watermark 6985aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK 5 7085aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT 5 7185aeef60SMatthias Ringwald 7200d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 7300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 7400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 7500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 7600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 7700d93d79Smatthias.ringwald 785628cf69SMatthias Ringwald // internal table 795628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0 805628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL 1 815628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2 825628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1) 835628cf69SMatthias Ringwald 8409e9d05bSMatthias Ringwald #if defined(ENABLE_LE_DATA_CHANNELS) || defined(ENABLE_CLASSIC) 8509e9d05bSMatthias Ringwald #define L2CAP_USES_CHANNELS 8609e9d05bSMatthias Ringwald #endif 8709e9d05bSMatthias Ringwald 8833c40538SMatthias Ringwald // prototypes 8933c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 903d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ); 9130725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void); 9209e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel); 9309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 9409e9d05bSMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 9509e9d05bSMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 9609e9d05bSMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 9709e9d05bSMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 9809e9d05bSMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel); 9909e9d05bSMatthias Ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 10009e9d05bSMatthias Ringwald #endif 101cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 10257be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status); 10357be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel); 10457be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid); 10544276248SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel); 106828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel); 107e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm); 108e7d0c9aaSMatthias Ringwald #endif 10933c40538SMatthias Ringwald 1105628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel { 1115628cf69SMatthias Ringwald btstack_packet_handler_t callback; 1122125de09SMatthias Ringwald uint8_t waiting_for_can_send_now; 1135628cf69SMatthias Ringwald } l2cap_fixed_channel_t; 1145628cf69SMatthias Ringwald 11509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1165628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels; 1175628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services; 11809e9d05bSMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp; 11909e9d05bSMatthias Ringwald #endif 12057be49d6SMatthias Ringwald 12157be49d6SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1225628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels; 1235628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services; 12457be49d6SMatthias Ringwald #endif 12539bda6d5Smatthias.ringwald 12639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 1272b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 1282b83fb7dSmatthias.ringwald static int signaling_responses_pending; 1292b83fb7dSmatthias.ringwald 130fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 131fb37a842SMatthias Ringwald 13233c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler; 1335628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE]; 13439bda6d5Smatthias.ringwald 13534e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){ 13634e7e577SMatthias Ringwald switch (index){ 13734e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL: 13834e7e577SMatthias Ringwald return L2CAP_CID_ATTRIBUTE_PROTOCOL; 13934e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL: 14034e7e577SMatthias Ringwald return L2CAP_CID_SECURITY_MANAGER_PROTOCOL; 14134e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL: 14234e7e577SMatthias Ringwald return L2CAP_CID_CONNECTIONLESS_CHANNEL; 14334e7e577SMatthias Ringwald default: 14434e7e577SMatthias Ringwald return 0; 14534e7e577SMatthias Ringwald } 14634e7e577SMatthias Ringwald } 1475628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){ 1485628cf69SMatthias Ringwald switch (channel_id){ 1495628cf69SMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1505628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL; 1515628cf69SMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1525628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL; 1535628cf69SMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1545628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL; 1555628cf69SMatthias Ringwald default: 1565628cf69SMatthias Ringwald return -1; 1575628cf69SMatthias Ringwald } 1585628cf69SMatthias Ringwald } 15939bda6d5Smatthias.ringwald 16034e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){ 16134e7e577SMatthias Ringwald if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0; 16234e7e577SMatthias Ringwald return 1; 16334e7e577SMatthias Ringwald } 16434e7e577SMatthias Ringwald 16571de195eSMatthias Ringwald void l2cap_init(void){ 1662b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 167808a48abSmatthias.ringwald 16809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 169f5454fc6Smatthias.ringwald l2cap_channels = NULL; 170f5454fc6Smatthias.ringwald l2cap_services = NULL; 17109e9d05bSMatthias Ringwald require_security_level2_for_outgoing_sdp = 0; 17209e9d05bSMatthias Ringwald #endif 173a3dc965aSMatthias Ringwald 174a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1757192e786SMatthias Ringwald l2cap_le_services = NULL; 1767192e786SMatthias Ringwald l2cap_le_channels = NULL; 177a3dc965aSMatthias Ringwald #endif 178f5454fc6Smatthias.ringwald 17933c40538SMatthias Ringwald l2cap_event_packet_handler = NULL; 1805628cf69SMatthias Ringwald memset(fixed_channels, 0, sizeof(fixed_channels)); 181f5454fc6Smatthias.ringwald 182fcadd0caSmatthias.ringwald // 1832718e2e7Smatthias.ringwald // register callback with HCI 184fcadd0caSmatthias.ringwald // 185d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &l2cap_hci_event_handler; 186fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 187fb37a842SMatthias Ringwald 188d9a7306aSMatthias Ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 189fb37a842SMatthias Ringwald 190be005ed6SMatthias Ringwald #ifdef ENABLE_CLASSIC 19115a95bd5SMatthias Ringwald gap_connectable_control(0); // no services yet 192be005ed6SMatthias Ringwald #endif 193fcadd0caSmatthias.ringwald } 194fcadd0caSmatthias.ringwald 195ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 19633c40538SMatthias Ringwald l2cap_event_packet_handler = handler; 1971e6aba47Smatthias.ringwald } 1981e6aba47Smatthias.ringwald 19909e9d05bSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){ 20009e9d05bSMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 20109e9d05bSMatthias Ringwald if (index < 0) return; 20209e9d05bSMatthias Ringwald fixed_channels[index].waiting_for_can_send_now = 1; 20309e9d05bSMatthias Ringwald l2cap_notify_channel_can_send(); 20409e9d05bSMatthias Ringwald } 20509e9d05bSMatthias Ringwald 20609e9d05bSMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){ 20709e9d05bSMatthias Ringwald return hci_can_send_acl_packet_now(con_handle); 20809e9d05bSMatthias Ringwald } 20909e9d05bSMatthias Ringwald 21009e9d05bSMatthias Ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 21109e9d05bSMatthias Ringwald return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 21209e9d05bSMatthias Ringwald } 21309e9d05bSMatthias Ringwald 21409e9d05bSMatthias Ringwald int l2cap_reserve_packet_buffer(void){ 21509e9d05bSMatthias Ringwald return hci_reserve_packet_buffer(); 21609e9d05bSMatthias Ringwald } 21709e9d05bSMatthias Ringwald 21809e9d05bSMatthias Ringwald void l2cap_release_packet_buffer(void){ 21909e9d05bSMatthias Ringwald hci_release_packet_buffer(); 22009e9d05bSMatthias Ringwald } 22109e9d05bSMatthias Ringwald 22209e9d05bSMatthias Ringwald static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint16_t remote_cid, uint16_t len){ 22309e9d05bSMatthias Ringwald int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 22409e9d05bSMatthias Ringwald 22509e9d05bSMatthias Ringwald // 0 - Connection handle : PB=pb : BC=00 22609e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14)); 22709e9d05bSMatthias Ringwald // 2 - ACL length 22809e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 22909e9d05bSMatthias Ringwald // 4 - L2CAP packet length 23009e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 23109e9d05bSMatthias Ringwald // 6 - L2CAP channel DEST 23209e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 6, remote_cid); 23309e9d05bSMatthias Ringwald } 23409e9d05bSMatthias Ringwald 23509e9d05bSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){ 23609e9d05bSMatthias Ringwald 23709e9d05bSMatthias Ringwald if (!hci_is_packet_buffer_reserved()){ 23809e9d05bSMatthias Ringwald log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 23909e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 24009e9d05bSMatthias Ringwald } 24109e9d05bSMatthias Ringwald 24209e9d05bSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(con_handle)){ 24309e9d05bSMatthias Ringwald log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid); 24409e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 24509e9d05bSMatthias Ringwald } 24609e9d05bSMatthias Ringwald 24709e9d05bSMatthias Ringwald log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid); 24809e9d05bSMatthias Ringwald 24909e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 25009e9d05bSMatthias Ringwald l2cap_setup_header(acl_buffer, con_handle, cid, len); 25109e9d05bSMatthias Ringwald // send 25209e9d05bSMatthias Ringwald return hci_send_acl_packet_buffer(len+8); 25309e9d05bSMatthias Ringwald } 25409e9d05bSMatthias Ringwald 25509e9d05bSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){ 25609e9d05bSMatthias Ringwald 25709e9d05bSMatthias Ringwald if (!hci_can_send_acl_packet_now(con_handle)){ 25809e9d05bSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 25909e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 26009e9d05bSMatthias Ringwald } 26109e9d05bSMatthias Ringwald 26209e9d05bSMatthias Ringwald hci_reserve_packet_buffer(); 26309e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 26409e9d05bSMatthias Ringwald 26509e9d05bSMatthias Ringwald memcpy(&acl_buffer[8], data, len); 26609e9d05bSMatthias Ringwald 26709e9d05bSMatthias Ringwald return l2cap_send_prepared_connectionless(con_handle, cid, len); 26809e9d05bSMatthias Ringwald } 26909e9d05bSMatthias Ringwald 27009e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) { 27109e9d05bSMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel); 27209e9d05bSMatthias Ringwald uint8_t event[4]; 27309e9d05bSMatthias Ringwald event[0] = L2CAP_EVENT_CAN_SEND_NOW; 27409e9d05bSMatthias Ringwald event[1] = sizeof(event) - 2; 27509e9d05bSMatthias Ringwald little_endian_store_16(event, 2, channel); 27609e9d05bSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 27709e9d05bSMatthias Ringwald packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); 27809e9d05bSMatthias Ringwald } 27909e9d05bSMatthias Ringwald 28009e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS 28117a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 28258de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 28358de5610Smatthias.ringwald } 28458de5610Smatthias.ringwald 28544276248SMatthias Ringwald static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){ 28644276248SMatthias Ringwald uint8_t event[4]; 28744276248SMatthias Ringwald event[0] = event_code; 28844276248SMatthias Ringwald event[1] = sizeof(event) - 2; 28944276248SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 29044276248SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 29144276248SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 29244276248SMatthias Ringwald } 29309e9d05bSMatthias Ringwald #endif 29444276248SMatthias Ringwald 29509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 29658de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 297c9dc710bS[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", 298fc64f94aSMatthias Ringwald status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 299c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 300bab5f4f0SMatthias Ringwald uint8_t event[24]; 30158de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 30258de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 30358de5610Smatthias.ringwald event[2] = status; 304724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 305fc64f94aSMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 306f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 307f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 308f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 309f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 310f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 311f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 312bab5f4f0SMatthias Ringwald event[23] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0; 31358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 31417a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 31558de5610Smatthias.ringwald } 31658de5610Smatthias.ringwald 31744276248SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 318e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 31944276248SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 32058de5610Smatthias.ringwald } 32158de5610Smatthias.ringwald 32244276248SMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) { 323e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 324fc64f94aSMatthias Ringwald bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid); 32558de5610Smatthias.ringwald uint8_t event[16]; 32658de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 32758de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 328724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 329fc64f94aSMatthias Ringwald little_endian_store_16(event, 8, channel->con_handle); 330f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 331f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 332f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 33358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 33417a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 3350af41d30Smatthias.ringwald } 336808a48abSmatthias.ringwald 3377f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 338665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 339665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 340665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 341665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 342b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 343f62db1e3Smatthias.ringwald return channel; 344f62db1e3Smatthias.ringwald } 345f62db1e3Smatthias.ringwald } 346f62db1e3Smatthias.ringwald return NULL; 347f62db1e3Smatthias.ringwald } 348f62db1e3Smatthias.ringwald 3490b9d7e78SMatthias Ringwald /// 3500b9d7e78SMatthias Ringwald 35130725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){ 35230725612SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 35330725612SMatthias Ringwald if (!channel) return; 35430725612SMatthias Ringwald channel->waiting_for_can_send_now = 1; 35530725612SMatthias Ringwald l2cap_notify_channel_can_send(); 35630725612SMatthias Ringwald } 35730725612SMatthias Ringwald 3586b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 3596b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 3606b1fde37Smatthias.ringwald if (!channel) return 0; 3610b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(channel->con_handle); 3627856fb31S[email protected] } 3637856fb31S[email protected] 36483e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 36583e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 36683e7cdd9SMatthias Ringwald if (!channel) return 0; 3670b9d7e78SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->con_handle); 36883e7cdd9SMatthias Ringwald } 36996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 37096cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 37196cbd662Smatthias.ringwald if (channel) { 37296cbd662Smatthias.ringwald return channel->remote_mtu; 37396cbd662Smatthias.ringwald } 37496cbd662Smatthias.ringwald return 0; 37596cbd662Smatthias.ringwald } 37696cbd662Smatthias.ringwald 377ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 378665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 379665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 380665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 381665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 3825932bd7cS[email protected] if ( &channel->rtx == ts) { 3835932bd7cS[email protected] return channel; 3845932bd7cS[email protected] } 3855932bd7cS[email protected] } 3865932bd7cS[email protected] return NULL; 3875932bd7cS[email protected] } 3885932bd7cS[email protected] 389ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 3905932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 39195d2c8f4SMatthias Ringwald if (!channel) return; 3925932bd7cS[email protected] 3935932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 3945932bd7cS[email protected] 3955932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 3965932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 3975932bd7cS[email protected] // notify client 3985932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 3995932bd7cS[email protected] 4005932bd7cS[email protected] // discard channel 4019dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 402665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 4035932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 4045932bd7cS[email protected] } 4055932bd7cS[email protected] 4065932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 4075932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 408528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 4095932bd7cS[email protected] } 4105932bd7cS[email protected] 4115932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 4125932bd7cS[email protected] l2cap_stop_rtx(channel); 413cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 414528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 415528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 416528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 4175932bd7cS[email protected] } 4185932bd7cS[email protected] 4195932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 4205932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 4215932bd7cS[email protected] l2cap_stop_rtx(channel); 422528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 423528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 424528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 4255932bd7cS[email protected] } 4265932bd7cS[email protected] 42771de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 428ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 429ac301f95S[email protected] } 430ac301f95S[email protected] 431df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 432ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 433df3354fcS[email protected] } 4345932bd7cS[email protected] 4357f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 436a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4379da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 438b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 439b1d43497Smatthias.ringwald } 440b1d43497Smatthias.ringwald 4419da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 4422a373862S[email protected] hci_reserve_packet_buffer(); 443facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 44458de5610Smatthias.ringwald va_list argptr; 44558de5610Smatthias.ringwald va_start(argptr, identifier); 44670efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 44758de5610Smatthias.ringwald va_end(argptr); 4489da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 449826f7347S[email protected] return hci_send_acl_packet_buffer(len); 45058de5610Smatthias.ringwald } 45158de5610Smatthias.ringwald 452b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 453b1d43497Smatthias.ringwald 454c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 455c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 456c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 457c8b9416aS[email protected] } 458c8b9416aS[email protected] 45958de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 460b1d43497Smatthias.ringwald if (!channel) { 4619da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 462b1d43497Smatthias.ringwald return -1; // TODO: define error 4636218e6f1Smatthias.ringwald } 4646218e6f1Smatthias.ringwald 465fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){ 4669da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 467a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 468a35252c8S[email protected] } 469a35252c8S[email protected] 470fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle); 471b1d43497Smatthias.ringwald 472facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4737f107edaSMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, len); 47458de5610Smatthias.ringwald // send 4757f107edaSMatthias Ringwald return hci_send_acl_packet_buffer(len+8); 47658de5610Smatthias.ringwald } 47758de5610Smatthias.ringwald 478ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 479b1d43497Smatthias.ringwald 480a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 481a35252c8S[email protected] if (!channel) { 482ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 483a35252c8S[email protected] return -1; // TODO: define error 484a35252c8S[email protected] } 485a35252c8S[email protected] 486f0efaa57S[email protected] if (len > channel->remote_mtu){ 487ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 488f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 489f0efaa57S[email protected] } 490f0efaa57S[email protected] 491fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)){ 492ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 493b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 494b1d43497Smatthias.ringwald } 495b1d43497Smatthias.ringwald 4962a373862S[email protected] hci_reserve_packet_buffer(); 497facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 498b1d43497Smatthias.ringwald 499b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 500b1d43497Smatthias.ringwald 501b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 502b1d43497Smatthias.ringwald } 503b1d43497Smatthias.ringwald 504fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){ 505fc64f94aSMatthias Ringwald return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data); 5060e37e417S[email protected] } 5070e37e417S[email protected] 50828ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 50928ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 51028ca2b46S[email protected] } 51128ca2b46S[email protected] 51228ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 51328ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 51428ca2b46S[email protected] } 51509e9d05bSMatthias Ringwald #endif 51628ca2b46S[email protected] 51728ca2b46S[email protected] 51809e9d05bSMatthias Ringwald #ifdef ENABLE_BLE 51909e9d05bSMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 52009e9d05bSMatthias Ringwald 52109e9d05bSMatthias Ringwald if (!hci_can_send_acl_packet_now(handle)){ 52209e9d05bSMatthias Ringwald log_info("l2cap_send_le_signaling_packet, cannot send"); 52309e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 52409e9d05bSMatthias Ringwald } 52509e9d05bSMatthias Ringwald 52609e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet type %u", cmd); 52709e9d05bSMatthias Ringwald hci_reserve_packet_buffer(); 52809e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 52909e9d05bSMatthias Ringwald va_list argptr; 53009e9d05bSMatthias Ringwald va_start(argptr, identifier); 53109e9d05bSMatthias Ringwald uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 53209e9d05bSMatthias Ringwald va_end(argptr); 53309e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet con %u!", handle); 53409e9d05bSMatthias Ringwald return hci_send_acl_packet_buffer(len); 53509e9d05bSMatthias Ringwald } 53609e9d05bSMatthias Ringwald #endif 53709e9d05bSMatthias Ringwald 53809e9d05bSMatthias Ringwald uint16_t l2cap_max_mtu(void){ 53909e9d05bSMatthias Ringwald return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 54009e9d05bSMatthias Ringwald } 54109e9d05bSMatthias Ringwald 54209e9d05bSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 54309e9d05bSMatthias Ringwald return l2cap_max_mtu(); 54409e9d05bSMatthias Ringwald } 545b1d43497Smatthias.ringwald 5468158c421Smatthias.ringwald // MARK: L2CAP_RUN 5472cd0be45Smatthias.ringwald // process outstanding signaling tasks 5487f02f414SMatthias Ringwald static void l2cap_run(void){ 5492b83fb7dSmatthias.ringwald 55022c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 55122c29ab4SMatthias Ringwald 5522b83fb7dSmatthias.ringwald // check pending signaling responses 5532b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5542b83fb7dSmatthias.ringwald 5552b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 556a35252c8S[email protected] 557a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 558a35252c8S[email protected] 5592b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5602b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 56163a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 562f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5632b83fb7dSmatthias.ringwald 564*f3963406SMatthias Ringwald UNUSED(infoType); 56509e9d05bSMatthias Ringwald 566f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 567f53da564S[email protected] signaling_responses_pending--; 568f53da564S[email protected] int i; 569f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 570f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 571f53da564S[email protected] } 572f53da564S[email protected] 573f53da564S[email protected] switch (response_code){ 57409e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 5752b360848Smatthias.ringwald case CONNECTION_REQUEST: 5762b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5772bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5784d816277S[email protected] if (result == 0x0003){ 5792bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5804d816277S[email protected] } 5812b360848Smatthias.ringwald break; 5822b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5832b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5842b83fb7dSmatthias.ringwald break; 5852b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5863b0484b3S[email protected] switch (infoType){ 5873b0484b3S[email protected] case 1: { // Connectionless MTU 5883b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5893b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5903b0484b3S[email protected] } 591202c8a4cSMatthias Ringwald break; 5923b0484b3S[email protected] case 2: { // Extended Features Supported 593462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 594462e630dS[email protected] uint32_t features = 0x280; 5953b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5963b0484b3S[email protected] } 597202c8a4cSMatthias Ringwald break; 5983b0484b3S[email protected] case 3: { // Fixed Channels Supported 5993b0484b3S[email protected] uint8_t map[8]; 6003b0484b3S[email protected] memset(map, 0, 8); 601288636a2SMatthias Ringwald map[0] = 0x06; // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04) 6023b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 6033b0484b3S[email protected] } 604202c8a4cSMatthias Ringwald break; 6053b0484b3S[email protected] default: 6062b83fb7dSmatthias.ringwald // all other types are not supported 6072b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 6083b0484b3S[email protected] break; 6092b83fb7dSmatthias.ringwald } 6102b83fb7dSmatthias.ringwald break; 61163a7246aSmatthias.ringwald case COMMAND_REJECT: 6125ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 61363f0ac45SMatthias Ringwald break; 61409e9d05bSMatthias Ringwald #endif 615a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 61663f0ac45SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 61763f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result); 61863f0ac45SMatthias Ringwald break; 61970efece1S[email protected] case COMMAND_REJECT_LE: 62070efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 62163a7246aSmatthias.ringwald break; 62270efece1S[email protected] #endif 6232b83fb7dSmatthias.ringwald default: 6242b83fb7dSmatthias.ringwald // should not happen 6252b83fb7dSmatthias.ringwald break; 6262b83fb7dSmatthias.ringwald } 6272b83fb7dSmatthias.ringwald } 6282b83fb7dSmatthias.ringwald 629665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 630*f3963406SMatthias Ringwald UNUSED(it); 63109e9d05bSMatthias Ringwald 63209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 63309e9d05bSMatthias Ringwald uint8_t config_options[4]; 634665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 635665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 636baf94f06S[email protected] 637665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 63822c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 6392cd0be45Smatthias.ringwald switch (channel->state){ 6402cd0be45Smatthias.ringwald 641df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 642ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 643fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 644a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 645ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 646fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 647ad671560S[email protected] } 648ad671560S[email protected] break; 649ad671560S[email protected] 65002b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 651baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 65264472d52Smatthias.ringwald // send connection request - set state first 65364472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 65402b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6558f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 65602b22dc4Smatthias.ringwald break; 65702b22dc4Smatthias.ringwald 658e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 659fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 66022c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 661fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 662e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6639dcb2fb2S[email protected] l2cap_stop_rtx(channel); 664665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 665d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 666e7ff783cSmatthias.ringwald break; 667e7ff783cSmatthias.ringwald 668552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 669fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 670fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 67128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 672fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 673552d92a1Smatthias.ringwald break; 674552d92a1Smatthias.ringwald 6756fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 676fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 6776fdcc387Smatthias.ringwald // success, start l2cap handshake 678b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6796fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 680fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6815932bd7cS[email protected] l2cap_start_rtx(channel); 6826fdcc387Smatthias.ringwald break; 6836fdcc387Smatthias.ringwald 684fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 685fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 68673cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 68763a7246aSmatthias.ringwald uint16_t flags = 0; 68828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 68963a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 69063a7246aSmatthias.ringwald flags = 1; 69163a7246aSmatthias.ringwald } else { 69228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 69363a7246aSmatthias.ringwald } 69463a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 695fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 69663a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 69763a7246aSmatthias.ringwald config_options[0] = 1; // MTU 69863a7246aSmatthias.ringwald config_options[1] = 2; // len param 699f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 700fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 70163a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 70263a7246aSmatthias.ringwald } else { 703fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 70463a7246aSmatthias.ringwald } 70563a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 706fa8473a4Smatthias.ringwald } 70773cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 70828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 70928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 710b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 711ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 712ae280e73Smatthias.ringwald config_options[1] = 2; // len param 713f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 714fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 7155932bd7cS[email protected] l2cap_start_rtx(channel); 716fa8473a4Smatthias.ringwald } 717fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 718552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 719552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 720fa8473a4Smatthias.ringwald } 721552d92a1Smatthias.ringwald break; 722552d92a1Smatthias.ringwald 723e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 724fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 72522c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 726fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 7275932bd7cS[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 :) 728756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 729e7ff783cSmatthias.ringwald break; 730e7ff783cSmatthias.ringwald 731e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 732fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 733b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 7342cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 735fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 7362cd0be45Smatthias.ringwald break; 7372cd0be45Smatthias.ringwald default: 7382cd0be45Smatthias.ringwald break; 7392cd0be45Smatthias.ringwald } 7402cd0be45Smatthias.ringwald } 74109e9d05bSMatthias Ringwald #endif 742da886c03S[email protected] 743cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 744efedfb4cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 745efedfb4cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7467f107edaSMatthias Ringwald uint8_t * acl_buffer; 7477f107edaSMatthias Ringwald uint8_t * l2cap_payload; 7487f107edaSMatthias Ringwald uint16_t pos; 7497f107edaSMatthias Ringwald uint16_t payload_size; 750efedfb4cSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 751efedfb4cSMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 752efedfb4cSMatthias Ringwald switch (channel->state){ 7535cb87675SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST: 7545cb87675SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 7555cb87675SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE; 7565cb87675SMatthias Ringwald // le psm, source cid, mtu, mps, initial credits 7571b8b8d05SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 75863f0ac45SMatthias Ringwald channel->credits_incoming = channel->new_credits_incoming; 75963f0ac45SMatthias Ringwald channel->new_credits_incoming = 0; 76063f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, LE_CREDIT_BASED_CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid, channel->local_mtu, 23, channel->credits_incoming); 7615cb87675SMatthias Ringwald break; 76223017473SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT: 76323017473SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 76423017473SMatthias Ringwald // TODO: support larger MPS 76523017473SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 76663f0ac45SMatthias Ringwald channel->credits_incoming = channel->new_credits_incoming; 76763f0ac45SMatthias Ringwald channel->new_credits_incoming = 0; 76863f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->local_mtu, 23, channel->credits_incoming, 0); 76964e11ca9SMatthias Ringwald // notify client 77044276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, 0); 77123017473SMatthias Ringwald break; 772e7d0c9aaSMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE: 773e7d0c9aaSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 774e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 77563f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason); 776e7d0c9aaSMatthias Ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 777e7d0c9aaSMatthias Ringwald l2cap_stop_rtx(channel); 778e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 779e7d0c9aaSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 780e7d0c9aaSMatthias Ringwald break; 7817f107edaSMatthias Ringwald case L2CAP_STATE_OPEN: 78285aeef60SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 78385aeef60SMatthias Ringwald 78485aeef60SMatthias Ringwald // send credits 78585aeef60SMatthias Ringwald if (channel->new_credits_incoming){ 78685aeef60SMatthias Ringwald log_info("l2cap: sending %u credits", channel->new_credits_incoming); 78785aeef60SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 78885aeef60SMatthias Ringwald uint16_t new_credits = channel->new_credits_incoming; 78985aeef60SMatthias Ringwald channel->new_credits_incoming = 0; 79085aeef60SMatthias Ringwald channel->credits_incoming += new_credits; 79185aeef60SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits); 79285aeef60SMatthias Ringwald break; 79385aeef60SMatthias Ringwald } 79485aeef60SMatthias Ringwald 79585aeef60SMatthias Ringwald // send data 7967f107edaSMatthias Ringwald if (!channel->send_sdu_buffer) break; 7977f107edaSMatthias Ringwald if (!channel->credits_outgoing) break; 79885aeef60SMatthias Ringwald 7997f107edaSMatthias Ringwald // send part of SDU 8007f107edaSMatthias Ringwald hci_reserve_packet_buffer(); 8017f107edaSMatthias Ringwald acl_buffer = hci_get_outgoing_packet_buffer(); 8027f107edaSMatthias Ringwald l2cap_payload = acl_buffer + 8; 8037f107edaSMatthias Ringwald pos = 0; 8047f107edaSMatthias Ringwald if (!channel->send_sdu_pos){ 8057f107edaSMatthias Ringwald // store SDU len 8067f107edaSMatthias Ringwald channel->send_sdu_pos += 2; 8077f107edaSMatthias Ringwald little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len); 8087f107edaSMatthias Ringwald pos += 2; 8097f107edaSMatthias Ringwald } 8107f107edaSMatthias Ringwald payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos); 81185aeef60SMatthias Ringwald log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing); 8127f107edaSMatthias Ringwald memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len 8137f107edaSMatthias Ringwald pos += payload_size; 8147f107edaSMatthias Ringwald channel->send_sdu_pos += payload_size; 81563f0ac45SMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, pos); 8167f107edaSMatthias Ringwald // done 8177f107edaSMatthias Ringwald 81885aeef60SMatthias Ringwald channel->credits_outgoing--; 8197f107edaSMatthias Ringwald 8207f107edaSMatthias Ringwald if (channel->send_sdu_pos >= channel->send_sdu_len + 2){ 8217f107edaSMatthias Ringwald channel->send_sdu_buffer = NULL; 82244276248SMatthias Ringwald // send done event 82344276248SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT); 82444276248SMatthias Ringwald // inform about can send now 82544276248SMatthias Ringwald l2cap_le_notify_channel_can_send(channel); 8267f107edaSMatthias Ringwald } 8277f107edaSMatthias Ringwald hci_send_acl_packet_buffer(8 + pos); 8287f107edaSMatthias Ringwald break; 829828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 830828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 831828a7f7aSMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 832828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 833828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 834828a7f7aSMatthias Ringwald break; 835828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 836828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 837828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 838828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 839828a7f7aSMatthias Ringwald l2cap_le_finialize_channel_close(channel); // -- remove from list 840828a7f7aSMatthias Ringwald break; 841efedfb4cSMatthias Ringwald default: 842efedfb4cSMatthias Ringwald break; 843efedfb4cSMatthias Ringwald } 844efedfb4cSMatthias Ringwald } 84509e9d05bSMatthias Ringwald #endif 846efedfb4cSMatthias Ringwald 84709e9d05bSMatthias Ringwald #ifdef ENABLE_BLE 848da886c03S[email protected] // send l2cap con paramter update if necessary 849da886c03S[email protected] hci_connections_get_iterator(&it); 850665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 851665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 8525d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 853b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 854da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 855b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 856b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 857b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 858b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 859b68d7bc3SMatthias Ringwald break; 860da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 861b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 862b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 863da886c03S[email protected] break; 864da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 865b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 866b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 867da886c03S[email protected] break; 868da886c03S[email protected] default: 869da886c03S[email protected] break; 870da886c03S[email protected] } 871da886c03S[email protected] } 8724d7157c3S[email protected] #endif 873da886c03S[email protected] 87422c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 8752cd0be45Smatthias.ringwald } 8762cd0be45Smatthias.ringwald 87709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 878fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){ 8792df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 8805533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 8812df5dadcS[email protected] // success, start l2cap handshake 882fc64f94aSMatthias Ringwald channel->con_handle = con_handle; 8832df5dadcS[email protected] // check remote SSP feature first 8842df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 8852df5dadcS[email protected] } 8862df5dadcS[email protected] } 8872df5dadcS[email protected] 8882df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 8892df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 8902df5dadcS[email protected] 8912df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 892ac301f95S[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)); 893fc64f94aSMatthias Ringwald if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 8942df5dadcS[email protected] // request security level 2 8952df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 896fc64f94aSMatthias Ringwald gap_request_security_level(channel->con_handle, LEVEL_2); 8972df5dadcS[email protected] return; 8982df5dadcS[email protected] } 8992df5dadcS[email protected] // fine, go ahead 9002df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 9012df5dadcS[email protected] } 90209e9d05bSMatthias Ringwald #endif 9032df5dadcS[email protected] 90409e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS 905da144af5SMatthias Ringwald static l2cap_channel_t * l2cap_create_channel_entry(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type, 906da144af5SMatthias Ringwald uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){ 907da144af5SMatthias Ringwald 908da144af5SMatthias Ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 909da144af5SMatthias Ringwald if (!channel) { 910da144af5SMatthias Ringwald return NULL; 911da144af5SMatthias Ringwald } 912da144af5SMatthias Ringwald 913da144af5SMatthias Ringwald // Init memory (make valgrind happy) 914da144af5SMatthias Ringwald memset(channel, 0, sizeof(l2cap_channel_t)); 915da144af5SMatthias Ringwald 916da144af5SMatthias Ringwald // fill in 917da144af5SMatthias Ringwald channel->packet_handler = packet_handler; 918da144af5SMatthias Ringwald bd_addr_copy(channel->address, address); 919da144af5SMatthias Ringwald channel->address_type = address_type; 920da144af5SMatthias Ringwald channel->psm = psm; 921da144af5SMatthias Ringwald channel->local_mtu = local_mtu; 922da144af5SMatthias Ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 923da144af5SMatthias Ringwald channel->required_security_level = security_level; 924da144af5SMatthias Ringwald 925da144af5SMatthias Ringwald // 926da144af5SMatthias Ringwald channel->local_cid = l2cap_next_local_cid(); 927da144af5SMatthias Ringwald channel->con_handle = 0; 928da144af5SMatthias Ringwald 929da144af5SMatthias Ringwald // set initial state 930da144af5SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 931da144af5SMatthias Ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 932da144af5SMatthias Ringwald channel->remote_sig_id = L2CAP_SIG_ID_INVALID; 933da144af5SMatthias Ringwald channel->local_sig_id = L2CAP_SIG_ID_INVALID; 934da144af5SMatthias Ringwald return channel; 935da144af5SMatthias Ringwald } 93609e9d05bSMatthias Ringwald #endif 93709e9d05bSMatthias Ringwald 93809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 939da144af5SMatthias Ringwald 9409077cb15SMatthias Ringwald /** 9419077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 9429077cb15SMatthias Ringwald * @param packet_handler 9439077cb15SMatthias Ringwald * @param address 9449077cb15SMatthias Ringwald * @param psm 9459077cb15SMatthias Ringwald * @param mtu 9469077cb15SMatthias Ringwald * @param local_cid 9479077cb15SMatthias Ringwald */ 9489077cb15SMatthias Ringwald 949da144af5SMatthias Ringwald uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t local_mtu, uint16_t * out_local_cid){ 950da144af5SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu); 951da144af5SMatthias Ringwald 952da144af5SMatthias Ringwald if (local_mtu > l2cap_max_mtu()) { 953da144af5SMatthias Ringwald local_mtu = l2cap_max_mtu(); 954da144af5SMatthias Ringwald } 955da144af5SMatthias Ringwald 956da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0); 957fc64f94aSMatthias Ringwald if (!channel) { 9589077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 9599077cb15SMatthias Ringwald } 9609077cb15SMatthias Ringwald 9619077cb15SMatthias Ringwald // add to connections list 962fc64f94aSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 9639077cb15SMatthias Ringwald 9649077cb15SMatthias Ringwald // store local_cid 9659077cb15SMatthias Ringwald if (out_local_cid){ 966fc64f94aSMatthias Ringwald *out_local_cid = channel->local_cid; 9679077cb15SMatthias Ringwald } 9689077cb15SMatthias Ringwald 9699077cb15SMatthias Ringwald // check if hci connection is already usable 9709077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 9719077cb15SMatthias Ringwald if (conn){ 972add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 973fc64f94aSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 9749077cb15SMatthias Ringwald // check if remote supported fearures are already received 9759077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 976fc64f94aSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 9779077cb15SMatthias Ringwald } 9789077cb15SMatthias Ringwald } 9799077cb15SMatthias Ringwald 9809077cb15SMatthias Ringwald l2cap_run(); 9819077cb15SMatthias Ringwald 9829077cb15SMatthias Ringwald return 0; 9839077cb15SMatthias Ringwald } 9849077cb15SMatthias Ringwald 985ce8f182eSMatthias Ringwald void 986ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 987e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 988b35f641cSmatthias.ringwald // find channel for local_cid 989b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 990f62db1e3Smatthias.ringwald if (channel) { 991e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 992f62db1e3Smatthias.ringwald } 9932cd0be45Smatthias.ringwald // process 9942cd0be45Smatthias.ringwald l2cap_run(); 99543625864Smatthias.ringwald } 9961e6aba47Smatthias.ringwald 997afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 998665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 999665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1000665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1001665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1002058e3d6bSMatthias Ringwald if ( bd_addr_cmp( channel->address, address) != 0) continue; 1003c22aecc9S[email protected] // channel for this address found 1004c22aecc9S[email protected] switch (channel->state){ 1005c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 1006c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 1007afde0c52Smatthias.ringwald // failure, forward error code 1008afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 1009afde0c52Smatthias.ringwald // discard channel 10109dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1011665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1012d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1013c22aecc9S[email protected] break; 1014c22aecc9S[email protected] default: 1015c22aecc9S[email protected] break; 1016afde0c52Smatthias.ringwald } 1017afde0c52Smatthias.ringwald } 1018afde0c52Smatthias.ringwald } 1019afde0c52Smatthias.ringwald 1020afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 1021665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1022665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1023665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1024665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1025058e3d6bSMatthias Ringwald if ( ! bd_addr_cmp( channel->address, address) ){ 10262df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 1027afde0c52Smatthias.ringwald } 1028afde0c52Smatthias.ringwald } 10296fdcc387Smatthias.ringwald // process 10306fdcc387Smatthias.ringwald l2cap_run(); 1031afde0c52Smatthias.ringwald } 103209e9d05bSMatthias Ringwald #endif 1033b448a0e7Smatthias.ringwald 103433c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){ 103509e9d05bSMatthias Ringwald 103609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 103733c40538SMatthias Ringwald btstack_linked_list_iterator_t it; 103833c40538SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 103933c40538SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 104033c40538SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 104133c40538SMatthias Ringwald if (!channel->waiting_for_can_send_now) continue; 1042fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 104333c40538SMatthias Ringwald channel->waiting_for_can_send_now = 0; 104434e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 104533c40538SMatthias Ringwald } 104609e9d05bSMatthias Ringwald #endif 104744276248SMatthias Ringwald 10482125de09SMatthias Ringwald int i; 10492125de09SMatthias Ringwald for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){ 1050773c4106SMatthias Ringwald if (!fixed_channels[i].callback) continue; 10512125de09SMatthias Ringwald if (!fixed_channels[i].waiting_for_can_send_now) continue; 105235454696SMatthias Ringwald int can_send = 0; 105334e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 105435454696SMatthias Ringwald #ifdef ENABLE_BLE 105534e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 105635454696SMatthias Ringwald #endif 105734e7e577SMatthias Ringwald } else { 105835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 105934e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 106035454696SMatthias Ringwald #endif 106134e7e577SMatthias Ringwald } 106234e7e577SMatthias Ringwald if (!can_send) continue; 106334e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 106434e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 10652125de09SMatthias Ringwald } 106633c40538SMatthias Ringwald } 106733c40538SMatthias Ringwald 1068d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 1069afde0c52Smatthias.ringwald 1070afde0c52Smatthias.ringwald bd_addr_t address; 1071afde0c52Smatthias.ringwald hci_con_handle_t handle; 10722d00edd4Smatthias.ringwald int hci_con_used; 107309e9d05bSMatthias Ringwald btstack_linked_list_iterator_t it; 107409e9d05bSMatthias Ringwald 107509e9d05bSMatthias Ringwald // avoid unused warnings 1076*f3963406SMatthias Ringwald UNUSED(address); 1077*f3963406SMatthias Ringwald UNUSED(hci_con_used); 1078*f3963406SMatthias Ringwald UNUSED(it); 1079afde0c52Smatthias.ringwald 10800e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1081afde0c52Smatthias.ringwald 108209e9d05bSMatthias Ringwald // Notify channel packet handler if they can send now 108309e9d05bSMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 108409e9d05bSMatthias Ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 108509e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 108609e9d05bSMatthias Ringwald l2cap_notify_channel_can_send(); 108709e9d05bSMatthias Ringwald break; 108809e9d05bSMatthias Ringwald 108909e9d05bSMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 109009e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 109109e9d05bSMatthias Ringwald break; 109209e9d05bSMatthias Ringwald 109309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1094afde0c52Smatthias.ringwald // handle connection complete events 1095afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1096724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 1097afde0c52Smatthias.ringwald if (packet[2] == 0){ 1098f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1099afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 1100afde0c52Smatthias.ringwald } else { 1101afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 1102afde0c52Smatthias.ringwald } 1103afde0c52Smatthias.ringwald break; 1104afde0c52Smatthias.ringwald 1105afde0c52Smatthias.ringwald // handle successful create connection cancel command 1106afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 1107073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) { 1108afde0c52Smatthias.ringwald if (packet[5] == 0){ 1109724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 1110afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 1111afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 111203cfbabcSmatthias.ringwald } 11131e6aba47Smatthias.ringwald } 111439d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 111539d59809Smatthias.ringwald break; 111609e9d05bSMatthias Ringwald #endif 111727a923d0Smatthias.ringwald 11181e6aba47Smatthias.ringwald // handle disconnection complete events 1119afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1120c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 1121f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 112209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1123665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1124665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1125665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1126fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 112715ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 11289dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1129665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1130d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 113127a923d0Smatthias.ringwald } 113209e9d05bSMatthias Ringwald #endif 1133a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1134991fea48SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1135991fea48SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1136991fea48SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1137991fea48SMatthias Ringwald if (channel->con_handle != handle) continue; 1138991fea48SMatthias Ringwald l2cap_emit_channel_closed(channel); 1139991fea48SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1140991fea48SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1141991fea48SMatthias Ringwald } 1142991fea48SMatthias Ringwald #endif 1143afde0c52Smatthias.ringwald break; 1144fcadd0caSmatthias.ringwald 1145ee091cf1Smatthias.ringwald // HCI Connection Timeouts 114609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1147afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 1148f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1149bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 115080ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 11512d00edd4Smatthias.ringwald hci_con_used = 0; 1152665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1153665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1154665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1155fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11562d00edd4Smatthias.ringwald hci_con_used = 1; 1157c22aecc9S[email protected] break; 1158ee091cf1Smatthias.ringwald } 11592d00edd4Smatthias.ringwald if (hci_con_used) break; 1160d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 11619edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1162afde0c52Smatthias.ringwald break; 1163ee091cf1Smatthias.ringwald 1164df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1165f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1166665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1167665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1168665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1169fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11702df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1171df3354fcS[email protected] break; 1172df3354fcS[email protected] } 1173c22aecc9S[email protected] break; 1174df3354fcS[email protected] 11755611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1176f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1177bd63148eS[email protected] log_info("l2cap - security level update"); 1178665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1179665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1180665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1181fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11825533f01eS[email protected] 1183bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1184bd63148eS[email protected] 1185e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 11865533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 11875533f01eS[email protected] 1188df3354fcS[email protected] switch (channel->state){ 1189df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 11905533f01eS[email protected] if (actual_level >= required_level){ 1191f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 119244276248SMatthias Ringwald l2cap_emit_incoming_connection(channel); 11931eb2563eS[email protected] } else { 1194775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 11951eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 11961eb2563eS[email protected] } 1197df3354fcS[email protected] break; 1198df3354fcS[email protected] 1199df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 12005533f01eS[email protected] if (actual_level >= required_level){ 1201df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1202df3354fcS[email protected] } else { 1203df3354fcS[email protected] // disconnnect, authentication not good enough 1204df3354fcS[email protected] hci_disconnect_security_block(handle); 1205df3354fcS[email protected] } 1206df3354fcS[email protected] break; 1207df3354fcS[email protected] 1208df3354fcS[email protected] default: 1209df3354fcS[email protected] break; 1210df3354fcS[email protected] } 1211f85a9399S[email protected] } 1212f85a9399S[email protected] break; 121309e9d05bSMatthias Ringwald #endif 1214f85a9399S[email protected] 1215afde0c52Smatthias.ringwald default: 1216afde0c52Smatthias.ringwald break; 1217afde0c52Smatthias.ringwald } 1218afde0c52Smatthias.ringwald 1219bd63148eS[email protected] l2cap_run(); 12201e6aba47Smatthias.ringwald } 12211e6aba47Smatthias.ringwald 12222b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 12234cf56b4aSmatthias.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." 12242b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 12252b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 12262b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 12272b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 12282b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 12292b360848Smatthias.ringwald signaling_responses_pending++; 12302b360848Smatthias.ringwald l2cap_run(); 12312b360848Smatthias.ringwald } 12322b360848Smatthias.ringwald } 12332b360848Smatthias.ringwald 123409e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 123509e9d05bSMatthias Ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 123609e9d05bSMatthias Ringwald channel->remote_sig_id = identifier; 123709e9d05bSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 123809e9d05bSMatthias Ringwald l2cap_run(); 123909e9d05bSMatthias Ringwald } 124009e9d05bSMatthias Ringwald 1241b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1242645658c9Smatthias.ringwald 12439da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1244645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1245645658c9Smatthias.ringwald if (!service) { 1246645658c9Smatthias.ringwald // 0x0002 PSM not supported 12472b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1248645658c9Smatthias.ringwald return; 1249645658c9Smatthias.ringwald } 1250645658c9Smatthias.ringwald 12515061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1252645658c9Smatthias.ringwald if (!hci_connection) { 12532b360848Smatthias.ringwald // 12549da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1255645658c9Smatthias.ringwald return; 1256645658c9Smatthias.ringwald } 12572bd8b7e7S[email protected] 1258645658c9Smatthias.ringwald // alloc structure 12599da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1260da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC, 1261da144af5SMatthias Ringwald psm, service->mtu, service->required_security_level); 12622b360848Smatthias.ringwald if (!channel){ 12632b360848Smatthias.ringwald // 0x0004 No resources available 12642b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 12652b360848Smatthias.ringwald return; 12662b360848Smatthias.ringwald } 1267da144af5SMatthias Ringwald 1268fc64f94aSMatthias Ringwald channel->con_handle = handle; 1269b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1270b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1271645658c9Smatthias.ringwald 1272f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 12732985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 12742985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 12759775e25bSmatthias.ringwald } 12769775e25bSmatthias.ringwald 1277645658c9Smatthias.ringwald // set initial state 1278df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1279a24785d0SMatthias Ringwald channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND | L2CAP_CHANNEL_STATE_VAR_INCOMING); 1280e405ae81Smatthias.ringwald 1281645658c9Smatthias.ringwald // add to connections list 1282665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1283645658c9Smatthias.ringwald 1284f85a9399S[email protected] // assert security requirements 12851eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1286e405ae81Smatthias.ringwald } 1287645658c9Smatthias.ringwald 1288ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1289e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1290b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1291e405ae81Smatthias.ringwald if (!channel) { 1292ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1293e405ae81Smatthias.ringwald return; 1294e405ae81Smatthias.ringwald } 1295e405ae81Smatthias.ringwald 1296552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1297e405ae81Smatthias.ringwald 1298552d92a1Smatthias.ringwald // process 1299552d92a1Smatthias.ringwald l2cap_run(); 1300e405ae81Smatthias.ringwald } 1301645658c9Smatthias.ringwald 13027ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){ 13037ef6a7bbSMatthias Ringwald log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid); 1304b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1305e405ae81Smatthias.ringwald if (!channel) { 1306ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1307e405ae81Smatthias.ringwald return; 1308e405ae81Smatthias.ringwald } 1309e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 13107ef6a7bbSMatthias Ringwald channel->reason = 0x04; // no resources available 1311e7ff783cSmatthias.ringwald l2cap_run(); 1312645658c9Smatthias.ringwald } 1313645658c9Smatthias.ringwald 13147f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1315b1988dceSmatthias.ringwald 1316b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1317b1988dceSmatthias.ringwald 1318f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 131963a7246aSmatthias.ringwald if (flags & 1) { 132063a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 132163a7246aSmatthias.ringwald } 132263a7246aSmatthias.ringwald 13232784b77dSmatthias.ringwald // accept the other's configuration options 1324f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 13253de7c0caSmatthias.ringwald uint16_t pos = 8; 13263de7c0caSmatthias.ringwald while (pos < end_pos){ 132763a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 132863a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 132963a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 133063a7246aSmatthias.ringwald pos++; 13311dc511deSmatthias.ringwald uint8_t length = command[pos++]; 13321dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 133363a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1334f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 13359da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 133663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 133763a7246aSmatthias.ringwald } 13380fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 13390fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1340f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 13410fe7a9d0S[email protected] } 134263a7246aSmatthias.ringwald // check for unknown options 134363a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1344c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 134563a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 13461dc511deSmatthias.ringwald } 13471dc511deSmatthias.ringwald pos += length; 13481dc511deSmatthias.ringwald } 13492784b77dSmatthias.ringwald } 13502784b77dSmatthias.ringwald 1351fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 13529da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 135373cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 135473cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1355019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1356019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1357fa8473a4Smatthias.ringwald return 1; 1358fa8473a4Smatthias.ringwald } 1359fa8473a4Smatthias.ringwald 1360fa8473a4Smatthias.ringwald 13617f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 13621e6aba47Smatthias.ringwald 136300d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 136400d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 136538e5900eSmatthias.ringwald uint16_t result = 0; 13661e6aba47Smatthias.ringwald 13679da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1368b35f641cSmatthias.ringwald 13699a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 13709a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 13719a011532Smatthias.ringwald switch (channel->state){ 1372fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 13739a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 13742b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 13759a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 13769a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 13779a011532Smatthias.ringwald break; 13789a011532Smatthias.ringwald 13799a011532Smatthias.ringwald default: 13809a011532Smatthias.ringwald // ignore in other states 13819a011532Smatthias.ringwald break; 13829a011532Smatthias.ringwald } 13839a011532Smatthias.ringwald return; 13849a011532Smatthias.ringwald } 13859a011532Smatthias.ringwald 138656081214Smatthias.ringwald // @STATEMACHINE(l2cap) 13871e6aba47Smatthias.ringwald switch (channel->state) { 13881e6aba47Smatthias.ringwald 13891e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 13901e6aba47Smatthias.ringwald switch (code){ 13911e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 13925932bd7cS[email protected] l2cap_stop_rtx(channel); 1393f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 139438e5900eSmatthias.ringwald switch (result) { 139538e5900eSmatthias.ringwald case 0: 1396169f8b28Smatthias.ringwald // successful connection 1397f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1398fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 139928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 140038e5900eSmatthias.ringwald break; 140138e5900eSmatthias.ringwald case 1: 14025932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 14035932bd7cS[email protected] l2cap_start_ertx(channel); 140438e5900eSmatthias.ringwald break; 140538e5900eSmatthias.ringwald default: 1406eb920dbeSmatthias.ringwald // channel closed 1407eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1408f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 140938e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1410eb920dbeSmatthias.ringwald 1411eb920dbeSmatthias.ringwald // drop link key if security block 1412eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 141315a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 1414eb920dbeSmatthias.ringwald } 1415eb920dbeSmatthias.ringwald 1416eb920dbeSmatthias.ringwald // discard channel 1417665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1418d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 141938e5900eSmatthias.ringwald break; 14201e6aba47Smatthias.ringwald } 14211e6aba47Smatthias.ringwald break; 142238e5900eSmatthias.ringwald 142338e5900eSmatthias.ringwald default: 14241e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 142538e5900eSmatthias.ringwald break; 14261e6aba47Smatthias.ringwald } 14271e6aba47Smatthias.ringwald break; 14281e6aba47Smatthias.ringwald 1429fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1430f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1431ae280e73Smatthias.ringwald switch (code) { 1432ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 143328ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1434ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 143563a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 143663a7246aSmatthias.ringwald // only done if continuation not set 143763a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 143863a7246aSmatthias.ringwald } 1439ae280e73Smatthias.ringwald break; 14401e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 14415932bd7cS[email protected] l2cap_stop_rtx(channel); 14425932bd7cS[email protected] switch (result){ 14435932bd7cS[email protected] case 0: // success 14445932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 14455932bd7cS[email protected] break; 14465932bd7cS[email protected] case 4: // pending 14475932bd7cS[email protected] l2cap_start_ertx(channel); 14485932bd7cS[email protected] break; 14495932bd7cS[email protected] default: 1450fe9d8984S[email protected] // retry on negative result 1451fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1452fe9d8984S[email protected] break; 1453fe9d8984S[email protected] } 14545a67bd4aSmatthias.ringwald break; 14555a67bd4aSmatthias.ringwald default: 14565a67bd4aSmatthias.ringwald break; 14571e6aba47Smatthias.ringwald } 1458fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1459fa8473a4Smatthias.ringwald // for open: 14605a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1461fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1462c8e4258aSmatthias.ringwald } 1463c8e4258aSmatthias.ringwald break; 1464f62db1e3Smatthias.ringwald 1465f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1466f62db1e3Smatthias.ringwald switch (code) { 1467f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 146827a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 146927a923d0Smatthias.ringwald break; 14705a67bd4aSmatthias.ringwald default: 14715a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 14725a67bd4aSmatthias.ringwald break; 147327a923d0Smatthias.ringwald } 147427a923d0Smatthias.ringwald break; 147584836b65Smatthias.ringwald 147684836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 147784836b65Smatthias.ringwald // @TODO handle incoming requests 147884836b65Smatthias.ringwald break; 147984836b65Smatthias.ringwald 148084836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 148184836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 148284836b65Smatthias.ringwald break; 148310642e45Smatthias.ringwald default: 148410642e45Smatthias.ringwald break; 148527a923d0Smatthias.ringwald } 14869da54300S[email protected] // log_info("new state %u", channel->state); 148727a923d0Smatthias.ringwald } 148827a923d0Smatthias.ringwald 148900d93d79Smatthias.ringwald 14907f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 149100d93d79Smatthias.ringwald 149200d93d79Smatthias.ringwald // get code, signalind identifier and command len 149300d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 149400d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 149500d93d79Smatthias.ringwald 149600d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 149700d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 149863a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 149900d93d79Smatthias.ringwald return; 150000d93d79Smatthias.ringwald } 150100d93d79Smatthias.ringwald 150200d93d79Smatthias.ringwald // general commands without an assigned channel 150300d93d79Smatthias.ringwald switch(code) { 150400d93d79Smatthias.ringwald 150500d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1506f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1507f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 150800d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 15092b83fb7dSmatthias.ringwald return; 151000d93d79Smatthias.ringwald } 151100d93d79Smatthias.ringwald 15122b360848Smatthias.ringwald case ECHO_REQUEST: 15132b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 15142b83fb7dSmatthias.ringwald return; 151500d93d79Smatthias.ringwald 151600d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1517f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 15182b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 15192b83fb7dSmatthias.ringwald return; 152000d93d79Smatthias.ringwald } 152100d93d79Smatthias.ringwald 152200d93d79Smatthias.ringwald default: 152300d93d79Smatthias.ringwald break; 152400d93d79Smatthias.ringwald } 152500d93d79Smatthias.ringwald 152600d93d79Smatthias.ringwald 152700d93d79Smatthias.ringwald // Get potential destination CID 1528f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 152900d93d79Smatthias.ringwald 153000d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1531665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1532665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1533665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1534665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1535fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 153600d93d79Smatthias.ringwald if (code & 1) { 1537b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1538b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 153900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 15404e32727eSmatthias.ringwald break; 154100d93d79Smatthias.ringwald } 154200d93d79Smatthias.ringwald } else { 1543b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 154400d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 154500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 15464e32727eSmatthias.ringwald break; 154700d93d79Smatthias.ringwald } 154800d93d79Smatthias.ringwald } 154900d93d79Smatthias.ringwald } 155000d93d79Smatthias.ringwald } 155109e9d05bSMatthias Ringwald #endif 155200d93d79Smatthias.ringwald 1553e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 155409e9d05bSMatthias Ringwald 155509e9d05bSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){ 155609e9d05bSMatthias Ringwald uint8_t event[6]; 155709e9d05bSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 155809e9d05bSMatthias Ringwald event[1] = 4; 155909e9d05bSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 156009e9d05bSMatthias Ringwald little_endian_store_16(event, 4, result); 156109e9d05bSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 156209e9d05bSMatthias Ringwald if (!l2cap_event_packet_handler) return; 156309e9d05bSMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 156409e9d05bSMatthias Ringwald } 156509e9d05bSMatthias Ringwald 1566c48b2a2cSMatthias Ringwald // @returns valid 1567c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){ 1568e7d0c9aaSMatthias Ringwald hci_connection_t * connection; 1569c48b2a2cSMatthias Ringwald uint16_t result; 1570c48b2a2cSMatthias Ringwald uint8_t event[10]; 157183fd9c76SMatthias Ringwald 1572cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1573a3dc965aSMatthias Ringwald btstack_linked_list_iterator_t it; 1574a3dc965aSMatthias Ringwald l2cap_channel_t * channel; 1575a3dc965aSMatthias Ringwald uint16_t local_cid; 157683fd9c76SMatthias Ringwald uint16_t le_psm; 157763f0ac45SMatthias Ringwald uint16_t new_credits; 157863f0ac45SMatthias Ringwald uint16_t credits_before; 1579e7d0c9aaSMatthias Ringwald l2cap_service_t * service; 158083fd9c76SMatthias Ringwald #endif 158100d93d79Smatthias.ringwald 15821b8b8d05SMatthias Ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 158323017473SMatthias Ringwald log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id); 15841b8b8d05SMatthias Ringwald 15851b8b8d05SMatthias Ringwald switch (code){ 158600d93d79Smatthias.ringwald 1587c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_RESPONSE: 1588c48b2a2cSMatthias Ringwald result = little_endian_read_16(command, 4); 1589ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1590ccf076adS[email protected] break; 1591da886c03S[email protected] 1592c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_REQUEST: 1593e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1594da886c03S[email protected] if (connection){ 15956d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 15966d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 1597c48b2a2cSMatthias Ringwald return 0; 15986d91fb6cSMatthias Ringwald } 1599da886c03S[email protected] int update_parameter = 1; 1600a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 16014ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 1602c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(command,8); 1603c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(command,10); 1604c48b2a2cSMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(command,12); 1605c48b2a2cSMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(command,14); 1606da886c03S[email protected] 1607da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1608da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1609da886c03S[email protected] 1610da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1611da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1612da886c03S[email protected] 1613da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1614da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1615da886c03S[email protected] 1616da886c03S[email protected] if (update_parameter){ 1617da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1618da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1619da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1620da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1621da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1622da886c03S[email protected] } else { 1623da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1624da886c03S[email protected] } 1625c48b2a2cSMatthias Ringwald connection->le_con_param_update_identifier = sig_id; 1626da886c03S[email protected] } 1627da886c03S[email protected] 162833c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 1629c48b2a2cSMatthias Ringwald 1630c48b2a2cSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1631c48b2a2cSMatthias Ringwald event[1] = 8; 1632c48b2a2cSMatthias Ringwald memcpy(&event[2], &command[4], 8); 1633c48b2a2cSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 163433c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1635ccf076adS[email protected] break; 1636c48b2a2cSMatthias Ringwald 1637a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1638a3dc965aSMatthias Ringwald 163963f0ac45SMatthias Ringwald case COMMAND_REJECT: 164063f0ac45SMatthias Ringwald // Find channel for this sig_id and connection handle 164163f0ac45SMatthias Ringwald channel = NULL; 164263f0ac45SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 164363f0ac45SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 164463f0ac45SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 164563f0ac45SMatthias Ringwald if (a_channel->con_handle != handle) continue; 164663f0ac45SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 164763f0ac45SMatthias Ringwald channel = a_channel; 164863f0ac45SMatthias Ringwald break; 164963f0ac45SMatthias Ringwald } 165063f0ac45SMatthias Ringwald if (!channel) break; 165163f0ac45SMatthias Ringwald 165263f0ac45SMatthias Ringwald // if received while waiting for le connection response, assume legacy device 165363f0ac45SMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){ 165463f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 165563f0ac45SMatthias Ringwald // no official value for this, use: Connection refused – LE_PSM not supported - 0x0002 165644276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, 0x0002); 165763f0ac45SMatthias Ringwald 165863f0ac45SMatthias Ringwald // discard channel 165963f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 166063f0ac45SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 166163f0ac45SMatthias Ringwald break; 166263f0ac45SMatthias Ringwald } 166363f0ac45SMatthias Ringwald break; 166463f0ac45SMatthias Ringwald 1665e7d0c9aaSMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 1666e7d0c9aaSMatthias Ringwald 1667e7d0c9aaSMatthias Ringwald // get hci connection, bail if not found (must not happen) 1668e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1669e7d0c9aaSMatthias Ringwald if (!connection) return 0; 1670e7d0c9aaSMatthias Ringwald 1671e7d0c9aaSMatthias Ringwald // check if service registered 1672e7d0c9aaSMatthias Ringwald le_psm = little_endian_read_16(command, 4); 1673e7d0c9aaSMatthias Ringwald service = l2cap_le_get_service(le_psm); 1674e7d0c9aaSMatthias Ringwald 1675e7d0c9aaSMatthias Ringwald if (service){ 1676e7d0c9aaSMatthias Ringwald 1677e7d0c9aaSMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, 6); 1678e7d0c9aaSMatthias Ringwald if (source_cid < 0x40){ 1679e7d0c9aaSMatthias Ringwald // 0x0009 Connection refused - Invalid Source CID 1680e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009); 1681e7d0c9aaSMatthias Ringwald return 1; 1682e7d0c9aaSMatthias Ringwald } 1683e7d0c9aaSMatthias Ringwald 1684e7d0c9aaSMatthias Ringwald // go through list of channels for this ACL connection and check if we get a match 1685e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1686e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 16871b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 16881b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 16891b8b8d05SMatthias Ringwald if (a_channel->remote_cid != source_cid) continue; 1690e7d0c9aaSMatthias Ringwald // 0x000a Connection refused - Source CID already allocated 1691e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a); 1692e7d0c9aaSMatthias Ringwald return 1; 1693e7d0c9aaSMatthias Ringwald } 1694e7d0c9aaSMatthias Ringwald 169583fd9c76SMatthias Ringwald // security: check encryption 169683fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_2){ 169783fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) == 0){ 169883fd9c76SMatthias Ringwald // 0x0008 Connection refused - insufficient encryption 169983fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0008); 170083fd9c76SMatthias Ringwald return 1; 170183fd9c76SMatthias Ringwald } 170283fd9c76SMatthias Ringwald // anything less than 16 byte key size is insufficient 170383fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) < 16){ 170483fd9c76SMatthias Ringwald // 0x0007 Connection refused – insufficient encryption key size 170583fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0007); 170683fd9c76SMatthias Ringwald return 1; 170783fd9c76SMatthias Ringwald } 170883fd9c76SMatthias Ringwald } 170983fd9c76SMatthias Ringwald 171083fd9c76SMatthias Ringwald // security: check authencation 171183fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_3){ 171283fd9c76SMatthias Ringwald if (!sm_authenticated(handle)){ 171383fd9c76SMatthias Ringwald // 0x0005 Connection refused – insufficient authentication 171483fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0005); 171583fd9c76SMatthias Ringwald return 1; 171683fd9c76SMatthias Ringwald } 171783fd9c76SMatthias Ringwald } 171883fd9c76SMatthias Ringwald 171983fd9c76SMatthias Ringwald // security: check authorization 172083fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_4){ 172183fd9c76SMatthias Ringwald if (sm_authorization_state(handle) != AUTHORIZATION_GRANTED){ 172283fd9c76SMatthias Ringwald // 0x0006 Connection refused – insufficient authorization 172383fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0006); 172483fd9c76SMatthias Ringwald return 1; 172583fd9c76SMatthias Ringwald } 172683fd9c76SMatthias Ringwald } 1727e7d0c9aaSMatthias Ringwald 1728e7d0c9aaSMatthias Ringwald // allocate channel 17291b8b8d05SMatthias Ringwald channel = l2cap_create_channel_entry(service->packet_handler, connection->address, 1730e7d0c9aaSMatthias Ringwald BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level); 1731e7d0c9aaSMatthias Ringwald if (!channel){ 1732e7d0c9aaSMatthias Ringwald // 0x0004 Connection refused – no resources available 1733e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004); 1734e7d0c9aaSMatthias Ringwald return 1; 1735e7d0c9aaSMatthias Ringwald } 1736e7d0c9aaSMatthias Ringwald 1737e7d0c9aaSMatthias Ringwald channel->con_handle = handle; 1738e7d0c9aaSMatthias Ringwald channel->remote_cid = source_cid; 1739e7d0c9aaSMatthias Ringwald channel->remote_sig_id = sig_id; 17407f107edaSMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, 8); 17417f107edaSMatthias Ringwald channel->remote_mps = little_endian_read_16(command, 10); 17427f107edaSMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, 12); 1743e7d0c9aaSMatthias Ringwald 1744e7d0c9aaSMatthias Ringwald // set initial state 1745e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1746c99bb618SMatthias Ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING; 1747e7d0c9aaSMatthias Ringwald 1748e7d0c9aaSMatthias Ringwald // add to connections list 1749e7d0c9aaSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1750e7d0c9aaSMatthias Ringwald 1751e7d0c9aaSMatthias Ringwald // post connection request event 175244276248SMatthias Ringwald l2cap_emit_le_incoming_connection(channel); 1753e7d0c9aaSMatthias Ringwald 1754e7d0c9aaSMatthias Ringwald } else { 1755e7d0c9aaSMatthias Ringwald // Connection refused – LE_PSM not supported 1756e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002); 1757e7d0c9aaSMatthias Ringwald } 1758e7d0c9aaSMatthias Ringwald break; 17591b8b8d05SMatthias Ringwald 17601b8b8d05SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_RESPONSE: 17611b8b8d05SMatthias Ringwald // Find channel for this sig_id and connection handle 17621b8b8d05SMatthias Ringwald channel = NULL; 17631b8b8d05SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 17641b8b8d05SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 17651b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 17661b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 17671b8b8d05SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 17681b8b8d05SMatthias Ringwald channel = a_channel; 17691b8b8d05SMatthias Ringwald break; 17701b8b8d05SMatthias Ringwald } 17711b8b8d05SMatthias Ringwald if (!channel) break; 17721b8b8d05SMatthias Ringwald 17731b8b8d05SMatthias Ringwald // cid + 0 17741b8b8d05SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8); 17751b8b8d05SMatthias Ringwald if (result){ 17761b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 17771b8b8d05SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 177844276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 17791b8b8d05SMatthias Ringwald 17801b8b8d05SMatthias Ringwald // discard channel 178163f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 17821b8b8d05SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 17831b8b8d05SMatthias Ringwald break; 17841b8b8d05SMatthias Ringwald } 17851b8b8d05SMatthias Ringwald 17861b8b8d05SMatthias Ringwald // success 17871b8b8d05SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 17881b8b8d05SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 17891b8b8d05SMatthias Ringwald channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4); 17901b8b8d05SMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6); 17911b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 179244276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 17931b8b8d05SMatthias Ringwald break; 17941b8b8d05SMatthias Ringwald 179585aeef60SMatthias Ringwald case LE_FLOW_CONTROL_CREDIT: 179685aeef60SMatthias Ringwald // find channel 179785aeef60SMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 179885aeef60SMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 179963f0ac45SMatthias Ringwald if (!channel) { 180063f0ac45SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 180163f0ac45SMatthias Ringwald break; 180263f0ac45SMatthias Ringwald } 180363f0ac45SMatthias Ringwald new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 180463f0ac45SMatthias Ringwald credits_before = channel->credits_outgoing; 180563f0ac45SMatthias Ringwald channel->credits_outgoing += new_credits; 180663f0ac45SMatthias Ringwald // check for credit overrun 180763f0ac45SMatthias Ringwald if (credits_before > channel->credits_outgoing){ 180863f0ac45SMatthias Ringwald log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid); 180963f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 181063f0ac45SMatthias Ringwald break; 181163f0ac45SMatthias Ringwald } 181263f0ac45SMatthias Ringwald log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing); 181385aeef60SMatthias Ringwald break; 181485aeef60SMatthias Ringwald 1815828a7f7aSMatthias Ringwald case DISCONNECTION_REQUEST: 1816828a7f7aSMatthias Ringwald // find channel 1817828a7f7aSMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 1818828a7f7aSMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 181963f34e00SMatthias Ringwald if (!channel) { 182063f34e00SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 182163f34e00SMatthias Ringwald break; 182263f34e00SMatthias Ringwald } 1823828a7f7aSMatthias Ringwald channel->remote_sig_id = sig_id; 1824828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1825828a7f7aSMatthias Ringwald break; 1826828a7f7aSMatthias Ringwald 1827a3dc965aSMatthias Ringwald #endif 1828a3dc965aSMatthias Ringwald 182963f0ac45SMatthias Ringwald case DISCONNECTION_RESPONSE: 183063f0ac45SMatthias Ringwald break; 183163f0ac45SMatthias Ringwald 1832c48b2a2cSMatthias Ringwald default: 1833c48b2a2cSMatthias Ringwald // command unknown -> reject command 1834c48b2a2cSMatthias Ringwald return 0; 1835ccf076adS[email protected] } 1836c48b2a2cSMatthias Ringwald return 1; 1837c48b2a2cSMatthias Ringwald } 1838e7d0c9aaSMatthias Ringwald #endif 1839c48b2a2cSMatthias Ringwald 1840c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 1841c48b2a2cSMatthias Ringwald 184209e9d05bSMatthias Ringwald l2cap_channel_t * l2cap_channel; 1843*f3963406SMatthias Ringwald UNUSED(l2cap_channel); 184409e9d05bSMatthias Ringwald 1845c48b2a2cSMatthias Ringwald // Get Channel ID 1846c48b2a2cSMatthias Ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 1847c48b2a2cSMatthias Ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 1848c48b2a2cSMatthias Ringwald 1849c48b2a2cSMatthias Ringwald switch (channel_id) { 1850c48b2a2cSMatthias Ringwald 185109e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1852c48b2a2cSMatthias Ringwald case L2CAP_CID_SIGNALING: { 1853c48b2a2cSMatthias Ringwald uint16_t command_offset = 8; 1854c48b2a2cSMatthias Ringwald while (command_offset < size) { 1855c48b2a2cSMatthias Ringwald // handle signaling commands 1856c48b2a2cSMatthias Ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 1857c48b2a2cSMatthias Ringwald 1858c48b2a2cSMatthias Ringwald // increment command_offset 1859c48b2a2cSMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1860c48b2a2cSMatthias Ringwald } 1861c48b2a2cSMatthias Ringwald break; 1862c48b2a2cSMatthias Ringwald } 186309e9d05bSMatthias Ringwald #endif 1864c48b2a2cSMatthias Ringwald 1865e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1866e7d0c9aaSMatthias Ringwald case L2CAP_CID_SIGNALING_LE: { 1867e7d0c9aaSMatthias Ringwald uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 1868e7d0c9aaSMatthias Ringwald int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id); 1869c48b2a2cSMatthias Ringwald if (!valid){ 18701bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1871ccf076adS[email protected] } 1872ccf076adS[email protected] break; 1873e7d0c9aaSMatthias Ringwald } 1874e7d0c9aaSMatthias Ringwald #endif 1875c48b2a2cSMatthias Ringwald 1876c48b2a2cSMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1877c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 1878c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1879ccf076adS[email protected] } 1880c48b2a2cSMatthias Ringwald break; 1881c48b2a2cSMatthias Ringwald 1882c48b2a2cSMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1883c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 1884c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1885c48b2a2cSMatthias Ringwald } 1886c48b2a2cSMatthias Ringwald break; 1887c48b2a2cSMatthias Ringwald 1888c48b2a2cSMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1889c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 1890c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1891c48b2a2cSMatthias Ringwald } 1892c48b2a2cSMatthias Ringwald break; 18931bbc0b23S[email protected] 189409e9d05bSMatthias Ringwald default: 189509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 189600d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 189764e11ca9SMatthias Ringwald l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 1898d1fd2a88SMatthias Ringwald if (l2cap_channel) { 18993d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 190000d93d79Smatthias.ringwald } 190109e9d05bSMatthias Ringwald #endif 1902a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 190364e11ca9SMatthias Ringwald l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id); 190464e11ca9SMatthias Ringwald if (l2cap_channel) { 190585aeef60SMatthias Ringwald // credit counting 190685aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming == 0){ 190785aeef60SMatthias Ringwald log_error("LE Data Channel packet received but no incoming credits"); 190885aeef60SMatthias Ringwald l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 190985aeef60SMatthias Ringwald break; 191085aeef60SMatthias Ringwald } 191185aeef60SMatthias Ringwald l2cap_channel->credits_incoming--; 191285aeef60SMatthias Ringwald 191385aeef60SMatthias Ringwald // automatic credits 191485aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){ 191585aeef60SMatthias Ringwald l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT; 191685aeef60SMatthias Ringwald } 191785aeef60SMatthias Ringwald 1918cd529728SMatthias Ringwald // first fragment 1919cd529728SMatthias Ringwald uint16_t pos = 0; 1920cd529728SMatthias Ringwald if (!l2cap_channel->receive_sdu_len){ 1921cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER); 1922cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos = 0; 1923cd529728SMatthias Ringwald pos += 2; 1924cd529728SMatthias Ringwald size -= 2; 1925cd529728SMatthias Ringwald } 1926cd529728SMatthias Ringwald memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER); 1927cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER; 1928cd529728SMatthias Ringwald // done? 192963f0ac45SMatthias Ringwald log_info("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len); 1930cd529728SMatthias Ringwald if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){ 1931cd529728SMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len); 1932cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = 0; 1933cd529728SMatthias Ringwald } 193463f0ac45SMatthias Ringwald } else { 193563f0ac45SMatthias Ringwald log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id); 193664e11ca9SMatthias Ringwald } 193764e11ca9SMatthias Ringwald #endif 19385652b5ffS[email protected] break; 19395652b5ffS[email protected] } 194000d93d79Smatthias.ringwald 19411eb2563eS[email protected] l2cap_run(); 19422718e2e7Smatthias.ringwald } 194300d93d79Smatthias.ringwald 194409e9d05bSMatthias Ringwald // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 194509e9d05bSMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 194609e9d05bSMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 194709e9d05bSMatthias Ringwald if (index < 0) return; 194809e9d05bSMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 194909e9d05bSMatthias Ringwald } 195009e9d05bSMatthias Ringwald 195109e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 195215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 195327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 1954f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1955f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1956f62db1e3Smatthias.ringwald // discard channel 19579dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1958665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1959d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1960c8e4258aSmatthias.ringwald } 19611e6aba47Smatthias.ringwald 19628f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1963665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1964665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1965665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1966665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 19679d9bbc01Smatthias.ringwald if ( service->psm == psm){ 19689d9bbc01Smatthias.ringwald return service; 19699d9bbc01Smatthias.ringwald }; 19709d9bbc01Smatthias.ringwald } 19719d9bbc01Smatthias.ringwald return NULL; 19729d9bbc01Smatthias.ringwald } 19739d9bbc01Smatthias.ringwald 19747192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 19757192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 19767192e786SMatthias Ringwald } 19777192e786SMatthias Ringwald 1978e0abb8e7S[email protected] 1979be2053a6SMatthias 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){ 1980be2053a6SMatthias Ringwald 1981be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1982e0abb8e7S[email protected] 19834bb582b6Smatthias.ringwald // check for alread registered psm 19849d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1985277abc2cSmatthias.ringwald if (service) { 1986be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1987be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1988277abc2cSmatthias.ringwald } 19899d9bbc01Smatthias.ringwald 19904bb582b6Smatthias.ringwald // alloc structure 1991bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1992277abc2cSmatthias.ringwald if (!service) { 1993be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1994be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1995277abc2cSmatthias.ringwald } 19969d9bbc01Smatthias.ringwald 19979d9bbc01Smatthias.ringwald // fill in 19989d9bbc01Smatthias.ringwald service->psm = psm; 19999d9bbc01Smatthias.ringwald service->mtu = mtu; 200005ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 2001df3354fcS[email protected] service->required_security_level = security_level; 20029d9bbc01Smatthias.ringwald 20039d9bbc01Smatthias.ringwald // add to services list 2004665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 2005c0e866bfSmatthias.ringwald 2006c0e866bfSmatthias.ringwald // enable page scan 200715a95bd5SMatthias Ringwald gap_connectable_control(1); 20085842b6d9Smatthias.ringwald 2009be2053a6SMatthias Ringwald return 0; 20109d9bbc01Smatthias.ringwald } 20119d9bbc01Smatthias.ringwald 20127e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){ 2013e0abb8e7S[email protected] 2014e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 2015e0abb8e7S[email protected] 20169d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 20177e8856ebSMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 2018665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 2019d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 2020c0e866bfSmatthias.ringwald 2021c0e866bfSmatthias.ringwald // disable page scan when no services registered 20227e8856ebSMatthias Ringwald if (btstack_linked_list_empty(&l2cap_services)) { 202315a95bd5SMatthias Ringwald gap_connectable_control(0); 20249d9bbc01Smatthias.ringwald } 20257e8856ebSMatthias Ringwald return 0; 20267e8856ebSMatthias Ringwald } 202709e9d05bSMatthias Ringwald #endif 20289d9bbc01Smatthias.ringwald 20297192e786SMatthias Ringwald 2030cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 203183fd9c76SMatthias Ringwald 203257be49d6SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){ 203357be49d6SMatthias Ringwald if (!channel->waiting_for_can_send_now) return; 203457be49d6SMatthias Ringwald if (channel->send_sdu_buffer) return; 203557be49d6SMatthias Ringwald channel->waiting_for_can_send_now = 0; 203657be49d6SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid); 203757be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW); 203857be49d6SMatthias Ringwald } 203957be49d6SMatthias Ringwald 204057be49d6SMatthias Ringwald // 1BH2222 204157be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) { 204257be49d6SMatthias Ringwald log_info("L2CAP_EVENT_LE_INCOMING_CONNECTION addr_type %u, addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x, remote_mtu %u", 204357be49d6SMatthias Ringwald channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu); 204457be49d6SMatthias Ringwald uint8_t event[19]; 204557be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION; 204657be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 204757be49d6SMatthias Ringwald event[2] = channel->address_type; 204857be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 204957be49d6SMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 205057be49d6SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 205157be49d6SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 205257be49d6SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 205357be49d6SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_mtu); 205457be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 205557be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 205657be49d6SMatthias Ringwald } 205757be49d6SMatthias Ringwald // 11BH22222 205857be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) { 205957be49d6SMatthias Ringwald log_info("L2CAP_EVENT_LE_CHANNEL_OPENED status 0x%x addr_type %u addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u", 206057be49d6SMatthias Ringwald status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 206157be49d6SMatthias Ringwald channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu); 2062c99bb618SMatthias Ringwald uint8_t event[23]; 206357be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED; 206457be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 206557be49d6SMatthias Ringwald event[2] = status; 206657be49d6SMatthias Ringwald event[3] = channel->address_type; 206757be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[4]); 206857be49d6SMatthias Ringwald little_endian_store_16(event, 10, channel->con_handle); 2069c99bb618SMatthias Ringwald event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0; 2070c99bb618SMatthias Ringwald little_endian_store_16(event, 13, channel->psm); 2071c99bb618SMatthias Ringwald little_endian_store_16(event, 15, channel->local_cid); 2072c99bb618SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_cid); 2073c99bb618SMatthias Ringwald little_endian_store_16(event, 19, channel->local_mtu); 2074c99bb618SMatthias Ringwald little_endian_store_16(event, 21, channel->remote_mtu); 207557be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 207657be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 207757be49d6SMatthias Ringwald } 207857be49d6SMatthias Ringwald 207957be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){ 208057be49d6SMatthias Ringwald btstack_linked_list_iterator_t it; 208157be49d6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 208257be49d6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 208357be49d6SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 208457be49d6SMatthias Ringwald if ( channel->local_cid == local_cid) { 208557be49d6SMatthias Ringwald return channel; 208657be49d6SMatthias Ringwald } 208757be49d6SMatthias Ringwald } 208857be49d6SMatthias Ringwald return NULL; 208957be49d6SMatthias Ringwald } 209057be49d6SMatthias Ringwald 209157be49d6SMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 209257be49d6SMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){ 209357be49d6SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 209457be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 209557be49d6SMatthias Ringwald // discard channel 209657be49d6SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 209757be49d6SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 209857be49d6SMatthias Ringwald } 209957be49d6SMatthias Ringwald 2100e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){ 2101e7d0c9aaSMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, le_psm); 21027192e786SMatthias Ringwald } 2103efedfb4cSMatthias Ringwald 2104da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){ 21057192e786SMatthias Ringwald 2106da144af5SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm); 21077192e786SMatthias Ringwald 21087192e786SMatthias Ringwald // check for alread registered psm 21097192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 21107192e786SMatthias Ringwald if (service) { 2111da144af5SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 21127192e786SMatthias Ringwald } 21137192e786SMatthias Ringwald 21147192e786SMatthias Ringwald // alloc structure 21157192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 21167192e786SMatthias Ringwald if (!service) { 21177192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 2118da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 21197192e786SMatthias Ringwald } 21207192e786SMatthias Ringwald 21217192e786SMatthias Ringwald // fill in 21227192e786SMatthias Ringwald service->psm = psm; 2123da144af5SMatthias Ringwald service->mtu = 0; 21247192e786SMatthias Ringwald service->packet_handler = packet_handler; 21257192e786SMatthias Ringwald service->required_security_level = security_level; 21267192e786SMatthias Ringwald 21277192e786SMatthias Ringwald // add to services list 2128665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 21297192e786SMatthias Ringwald 21307192e786SMatthias Ringwald // done 2131da144af5SMatthias Ringwald return 0; 21327192e786SMatthias Ringwald } 21337192e786SMatthias Ringwald 2134da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) { 21357192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 21367192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 21379367f9b0SMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 2138da144af5SMatthias Ringwald 2139665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 21407192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 2141da144af5SMatthias Ringwald return 0; 21427192e786SMatthias Ringwald } 2143da144af5SMatthias Ringwald 2144da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){ 2145e7d0c9aaSMatthias Ringwald // get channel 2146e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2147e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2148e7d0c9aaSMatthias Ringwald 2149e7d0c9aaSMatthias Ringwald // validate state 2150e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 2151e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2152e7d0c9aaSMatthias Ringwald } 2153e7d0c9aaSMatthias Ringwald 2154efedfb4cSMatthias Ringwald // set state accept connection 215523017473SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT; 215623017473SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 215723017473SMatthias Ringwald channel->local_mtu = mtu; 215885aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 215985aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 216085aeef60SMatthias Ringwald 216185aeef60SMatthias Ringwald // test 216263f0ac45SMatthias Ringwald // channel->new_credits_incoming = 1; 2163e7d0c9aaSMatthias Ringwald 2164e7d0c9aaSMatthias Ringwald // go 2165e7d0c9aaSMatthias Ringwald l2cap_run(); 2166da144af5SMatthias Ringwald return 0; 2167da144af5SMatthias Ringwald } 2168da144af5SMatthias Ringwald 2169da144af5SMatthias Ringwald /** 2170da144af5SMatthias Ringwald * @brief Deny incoming LE Data Channel connection due to resource constraints 2171da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2172da144af5SMatthias Ringwald */ 2173da144af5SMatthias Ringwald 2174da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){ 2175e7d0c9aaSMatthias Ringwald // get channel 2176e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2177e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2178e7d0c9aaSMatthias Ringwald 2179e7d0c9aaSMatthias Ringwald // validate state 2180e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 2181e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2182e7d0c9aaSMatthias Ringwald } 2183e7d0c9aaSMatthias Ringwald 2184efedfb4cSMatthias Ringwald // set state decline connection 2185e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE; 2186e7d0c9aaSMatthias Ringwald channel->reason = 0x04; // no resources available 2187e7d0c9aaSMatthias Ringwald l2cap_run(); 2188da144af5SMatthias Ringwald return 0; 2189da144af5SMatthias Ringwald } 2190da144af5SMatthias Ringwald 21917dafa750SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, 2192da144af5SMatthias Ringwald uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 2193efedfb4cSMatthias Ringwald uint16_t * out_local_cid) { 2194efedfb4cSMatthias Ringwald 21957dafa750SMatthias Ringwald log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu); 2196da144af5SMatthias Ringwald 21977dafa750SMatthias Ringwald 21987dafa750SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 21997dafa750SMatthias Ringwald if (!connection) { 22007dafa750SMatthias Ringwald log_error("no hci_connection for handle 0x%04x", con_handle); 22017dafa750SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 22027dafa750SMatthias Ringwald } 22037dafa750SMatthias Ringwald 22047dafa750SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, connection->address, connection->address_type, psm, mtu, security_level); 2205da144af5SMatthias Ringwald if (!channel) { 2206da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 2207da144af5SMatthias Ringwald } 2208e7d0c9aaSMatthias Ringwald log_info("l2cap_le_create_channel %p", channel); 2209da144af5SMatthias Ringwald 2210da144af5SMatthias Ringwald // store local_cid 2211da144af5SMatthias Ringwald if (out_local_cid){ 2212da144af5SMatthias Ringwald *out_local_cid = channel->local_cid; 2213da144af5SMatthias Ringwald } 2214da144af5SMatthias Ringwald 22157dafa750SMatthias Ringwald // provide buffer 22167dafa750SMatthias Ringwald channel->con_handle = con_handle; 2217cd529728SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 22187dafa750SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST; 221985aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 222085aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 222185aeef60SMatthias Ringwald 2222efedfb4cSMatthias Ringwald // add to connections list 2223efedfb4cSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 2224efedfb4cSMatthias Ringwald 22257dafa750SMatthias Ringwald // go 222663f0ac45SMatthias Ringwald l2cap_run(); 2227da144af5SMatthias Ringwald return 0; 2228da144af5SMatthias Ringwald } 2229da144af5SMatthias Ringwald 2230da144af5SMatthias Ringwald /** 2231da144af5SMatthias Ringwald * @brief Provide credtis for LE Data Channel 2232da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2233da144af5SMatthias Ringwald * @param credits Number additional credits for peer 2234da144af5SMatthias Ringwald */ 223564e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){ 223663f0ac45SMatthias Ringwald 223763f0ac45SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 223863f0ac45SMatthias Ringwald if (!channel) { 223963f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 224063f0ac45SMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 224163f0ac45SMatthias Ringwald } 224263f0ac45SMatthias Ringwald 2243efedfb4cSMatthias Ringwald // check state 224463f0ac45SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN){ 224563f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid); 224663f0ac45SMatthias Ringwald } 224763f0ac45SMatthias Ringwald 224863f0ac45SMatthias Ringwald // assert incoming credits + credits <= 0xffff 224963f0ac45SMatthias Ringwald uint32_t total_credits = channel->credits_incoming; 225063f0ac45SMatthias Ringwald total_credits += channel->new_credits_incoming; 225163f0ac45SMatthias Ringwald total_credits += credits; 225263f0ac45SMatthias Ringwald if (total_credits > 0xffff){ 225363f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming, 225463f0ac45SMatthias Ringwald channel->new_credits_incoming, credits); 225563f0ac45SMatthias Ringwald } 225663f0ac45SMatthias Ringwald 2257efedfb4cSMatthias Ringwald // set credits_granted 225863f0ac45SMatthias Ringwald channel->new_credits_incoming += credits; 225963f0ac45SMatthias Ringwald 226063f0ac45SMatthias Ringwald // go 226163f0ac45SMatthias Ringwald l2cap_run(); 2262da144af5SMatthias Ringwald return 0; 2263da144af5SMatthias Ringwald } 2264da144af5SMatthias Ringwald 2265da144af5SMatthias Ringwald /** 2266da144af5SMatthias Ringwald * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 2267da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2268da144af5SMatthias Ringwald */ 226964e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){ 227044276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 227144276248SMatthias Ringwald if (!channel) { 227244276248SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 2273da144af5SMatthias Ringwald return 0; 2274da144af5SMatthias Ringwald } 2275da144af5SMatthias Ringwald 227644276248SMatthias Ringwald // check state 227744276248SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN) return 0; 227844276248SMatthias Ringwald 227944276248SMatthias Ringwald // check queue 228044276248SMatthias Ringwald if (channel->send_sdu_buffer) return 0; 228144276248SMatthias Ringwald 228244276248SMatthias Ringwald // fine, go ahead 228344276248SMatthias Ringwald return 1; 228444276248SMatthias Ringwald } 228544276248SMatthias Ringwald 2286da144af5SMatthias Ringwald /** 2287da144af5SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 2288da144af5SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 2289da144af5SMatthias Ringwald * so packet handler should be ready to handle it 2290da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2291da144af5SMatthias Ringwald */ 229264e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){ 229344276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 229444276248SMatthias Ringwald if (!channel) { 229544276248SMatthias Ringwald log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid); 229644276248SMatthias Ringwald return 0; 229744276248SMatthias Ringwald } 229844276248SMatthias Ringwald channel->waiting_for_can_send_now = 1; 229944276248SMatthias Ringwald l2cap_le_notify_channel_can_send(channel); 2300da144af5SMatthias Ringwald return 0; 2301da144af5SMatthias Ringwald } 2302da144af5SMatthias Ringwald 2303da144af5SMatthias Ringwald /** 2304da144af5SMatthias Ringwald * @brief Send data via LE Data Channel 2305da144af5SMatthias Ringwald * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 2306da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2307da144af5SMatthias Ringwald * @param data data to send 2308da144af5SMatthias Ringwald * @param size data size 2309da144af5SMatthias Ringwald */ 231064e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){ 231164e11ca9SMatthias Ringwald 231264e11ca9SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 231364e11ca9SMatthias Ringwald if (!channel) { 231464e11ca9SMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 2315828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 231664e11ca9SMatthias Ringwald } 231764e11ca9SMatthias Ringwald 231864e11ca9SMatthias Ringwald if (len > channel->remote_mtu){ 231964e11ca9SMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 232064e11ca9SMatthias Ringwald return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 232164e11ca9SMatthias Ringwald } 232264e11ca9SMatthias Ringwald 23237f107edaSMatthias Ringwald if (channel->send_sdu_buffer){ 232464e11ca9SMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 232564e11ca9SMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 232664e11ca9SMatthias Ringwald } 232764e11ca9SMatthias Ringwald 23287f107edaSMatthias Ringwald channel->send_sdu_buffer = data; 23297f107edaSMatthias Ringwald channel->send_sdu_len = len; 23307f107edaSMatthias Ringwald channel->send_sdu_pos = 0; 233164e11ca9SMatthias Ringwald 23327f107edaSMatthias Ringwald l2cap_run(); 23337f107edaSMatthias Ringwald return 0; 2334da144af5SMatthias Ringwald } 2335da144af5SMatthias Ringwald 2336da144af5SMatthias Ringwald /** 2337da144af5SMatthias Ringwald * @brief Disconnect from LE Data Channel 2338da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2339da144af5SMatthias Ringwald */ 2340828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid) 2341da144af5SMatthias Ringwald { 2342828a7f7aSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2343828a7f7aSMatthias Ringwald if (!channel) { 2344828a7f7aSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 2345828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2346828a7f7aSMatthias Ringwald } 2347828a7f7aSMatthias Ringwald 2348828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 2349828a7f7aSMatthias Ringwald l2cap_run(); 2350da144af5SMatthias Ringwald return 0; 2351da144af5SMatthias Ringwald } 2352da144af5SMatthias Ringwald 23537f02f414SMatthias Ringwald #endif 2354