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 190*be005ed6SMatthias Ringwald #ifdef ENABLE_CLASSIC 19115a95bd5SMatthias Ringwald gap_connectable_control(0); // no services yet 192*be005ed6SMatthias 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); 300c9dc710bS[email protected] uint8_t event[23]; 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); 31258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 31317a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 31458de5610Smatthias.ringwald } 31558de5610Smatthias.ringwald 31644276248SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 317e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 31844276248SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 31958de5610Smatthias.ringwald } 32058de5610Smatthias.ringwald 32144276248SMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) { 322e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 323fc64f94aSMatthias Ringwald bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid); 32458de5610Smatthias.ringwald uint8_t event[16]; 32558de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 32658de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 327724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 328fc64f94aSMatthias Ringwald little_endian_store_16(event, 8, channel->con_handle); 329f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 330f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 331f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 33258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 33317a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 3340af41d30Smatthias.ringwald } 335808a48abSmatthias.ringwald 3367f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 337665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 338665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 339665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 340665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 341b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 342f62db1e3Smatthias.ringwald return channel; 343f62db1e3Smatthias.ringwald } 344f62db1e3Smatthias.ringwald } 345f62db1e3Smatthias.ringwald return NULL; 346f62db1e3Smatthias.ringwald } 347f62db1e3Smatthias.ringwald 3480b9d7e78SMatthias Ringwald /// 3490b9d7e78SMatthias Ringwald 35030725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){ 35130725612SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 35230725612SMatthias Ringwald if (!channel) return; 35330725612SMatthias Ringwald channel->waiting_for_can_send_now = 1; 35430725612SMatthias Ringwald l2cap_notify_channel_can_send(); 35530725612SMatthias Ringwald } 35630725612SMatthias Ringwald 3576b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 3586b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 3596b1fde37Smatthias.ringwald if (!channel) return 0; 3600b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(channel->con_handle); 3617856fb31S[email protected] } 3627856fb31S[email protected] 36383e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 36483e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 36583e7cdd9SMatthias Ringwald if (!channel) return 0; 3660b9d7e78SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->con_handle); 36783e7cdd9SMatthias Ringwald } 36896cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 36996cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 37096cbd662Smatthias.ringwald if (channel) { 37196cbd662Smatthias.ringwald return channel->remote_mtu; 37296cbd662Smatthias.ringwald } 37396cbd662Smatthias.ringwald return 0; 37496cbd662Smatthias.ringwald } 37596cbd662Smatthias.ringwald 376ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 377665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 378665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 379665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 380665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 3815932bd7cS[email protected] if ( &channel->rtx == ts) { 3825932bd7cS[email protected] return channel; 3835932bd7cS[email protected] } 3845932bd7cS[email protected] } 3855932bd7cS[email protected] return NULL; 3865932bd7cS[email protected] } 3875932bd7cS[email protected] 388ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 3895932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 39095d2c8f4SMatthias Ringwald if (!channel) return; 3915932bd7cS[email protected] 3925932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 3935932bd7cS[email protected] 3945932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 3955932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 3965932bd7cS[email protected] // notify client 3975932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 3985932bd7cS[email protected] 3995932bd7cS[email protected] // discard channel 4009dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 401665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 4025932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 4035932bd7cS[email protected] } 4045932bd7cS[email protected] 4055932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 4065932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 407528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 4085932bd7cS[email protected] } 4095932bd7cS[email protected] 4105932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 4115932bd7cS[email protected] l2cap_stop_rtx(channel); 412cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 413528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 414528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 415528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 4165932bd7cS[email protected] } 4175932bd7cS[email protected] 4185932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 4195932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 4205932bd7cS[email protected] l2cap_stop_rtx(channel); 421528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 422528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 423528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 4245932bd7cS[email protected] } 4255932bd7cS[email protected] 42671de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 427ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 428ac301f95S[email protected] } 429ac301f95S[email protected] 430df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 431ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 432df3354fcS[email protected] } 4335932bd7cS[email protected] 4347f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 435a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4369da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 437b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 438b1d43497Smatthias.ringwald } 439b1d43497Smatthias.ringwald 4409da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 4412a373862S[email protected] hci_reserve_packet_buffer(); 442facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 44358de5610Smatthias.ringwald va_list argptr; 44458de5610Smatthias.ringwald va_start(argptr, identifier); 44570efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 44658de5610Smatthias.ringwald va_end(argptr); 4479da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 448826f7347S[email protected] return hci_send_acl_packet_buffer(len); 44958de5610Smatthias.ringwald } 45058de5610Smatthias.ringwald 451b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 452b1d43497Smatthias.ringwald 453c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 454c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 455c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 456c8b9416aS[email protected] } 457c8b9416aS[email protected] 45858de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 459b1d43497Smatthias.ringwald if (!channel) { 4609da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 461b1d43497Smatthias.ringwald return -1; // TODO: define error 4626218e6f1Smatthias.ringwald } 4636218e6f1Smatthias.ringwald 464fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){ 4659da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 466a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 467a35252c8S[email protected] } 468a35252c8S[email protected] 469fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle); 470b1d43497Smatthias.ringwald 471facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4727f107edaSMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, len); 47358de5610Smatthias.ringwald // send 4747f107edaSMatthias Ringwald return hci_send_acl_packet_buffer(len+8); 47558de5610Smatthias.ringwald } 47658de5610Smatthias.ringwald 477ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 478b1d43497Smatthias.ringwald 479a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 480a35252c8S[email protected] if (!channel) { 481ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 482a35252c8S[email protected] return -1; // TODO: define error 483a35252c8S[email protected] } 484a35252c8S[email protected] 485f0efaa57S[email protected] if (len > channel->remote_mtu){ 486ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 487f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 488f0efaa57S[email protected] } 489f0efaa57S[email protected] 490fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)){ 491ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 492b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 493b1d43497Smatthias.ringwald } 494b1d43497Smatthias.ringwald 4952a373862S[email protected] hci_reserve_packet_buffer(); 496facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 497b1d43497Smatthias.ringwald 498b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 499b1d43497Smatthias.ringwald 500b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 501b1d43497Smatthias.ringwald } 502b1d43497Smatthias.ringwald 503fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){ 504fc64f94aSMatthias Ringwald return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data); 5050e37e417S[email protected] } 5060e37e417S[email protected] 50728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 50828ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 50928ca2b46S[email protected] } 51028ca2b46S[email protected] 51128ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 51228ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 51328ca2b46S[email protected] } 51409e9d05bSMatthias Ringwald #endif 51528ca2b46S[email protected] 51628ca2b46S[email protected] 51709e9d05bSMatthias Ringwald #ifdef ENABLE_BLE 51809e9d05bSMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 51909e9d05bSMatthias Ringwald 52009e9d05bSMatthias Ringwald if (!hci_can_send_acl_packet_now(handle)){ 52109e9d05bSMatthias Ringwald log_info("l2cap_send_le_signaling_packet, cannot send"); 52209e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 52309e9d05bSMatthias Ringwald } 52409e9d05bSMatthias Ringwald 52509e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet type %u", cmd); 52609e9d05bSMatthias Ringwald hci_reserve_packet_buffer(); 52709e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 52809e9d05bSMatthias Ringwald va_list argptr; 52909e9d05bSMatthias Ringwald va_start(argptr, identifier); 53009e9d05bSMatthias Ringwald uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 53109e9d05bSMatthias Ringwald va_end(argptr); 53209e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet con %u!", handle); 53309e9d05bSMatthias Ringwald return hci_send_acl_packet_buffer(len); 53409e9d05bSMatthias Ringwald } 53509e9d05bSMatthias Ringwald #endif 53609e9d05bSMatthias Ringwald 53709e9d05bSMatthias Ringwald uint16_t l2cap_max_mtu(void){ 53809e9d05bSMatthias Ringwald return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 53909e9d05bSMatthias Ringwald } 54009e9d05bSMatthias Ringwald 54109e9d05bSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 54209e9d05bSMatthias Ringwald return l2cap_max_mtu(); 54309e9d05bSMatthias Ringwald } 544b1d43497Smatthias.ringwald 5458158c421Smatthias.ringwald // MARK: L2CAP_RUN 5462cd0be45Smatthias.ringwald // process outstanding signaling tasks 5477f02f414SMatthias Ringwald static void l2cap_run(void){ 5482b83fb7dSmatthias.ringwald 54922c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 55022c29ab4SMatthias Ringwald 5512b83fb7dSmatthias.ringwald // check pending signaling responses 5522b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5532b83fb7dSmatthias.ringwald 5542b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 555a35252c8S[email protected] 556a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 557a35252c8S[email protected] 5582b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5592b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 56063a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 561f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5622b83fb7dSmatthias.ringwald 56309e9d05bSMatthias Ringwald // avoid warnings 56409e9d05bSMatthias Ringwald (void) 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; 63009e9d05bSMatthias Ringwald (void) 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; 105234e7e577SMatthias Ringwald int can_send; 105334e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 105434e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 105534e7e577SMatthias Ringwald } else { 105634e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 105734e7e577SMatthias Ringwald } 105834e7e577SMatthias Ringwald if (!can_send) continue; 105934e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 106034e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 10612125de09SMatthias Ringwald } 106233c40538SMatthias Ringwald } 106333c40538SMatthias Ringwald 1064d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 1065afde0c52Smatthias.ringwald 1066afde0c52Smatthias.ringwald bd_addr_t address; 1067afde0c52Smatthias.ringwald hci_con_handle_t handle; 10682d00edd4Smatthias.ringwald int hci_con_used; 106909e9d05bSMatthias Ringwald btstack_linked_list_iterator_t it; 107009e9d05bSMatthias Ringwald 107109e9d05bSMatthias Ringwald // avoid unused warnings 107209e9d05bSMatthias Ringwald (void) address; 107309e9d05bSMatthias Ringwald (void) hci_con_used; 107409e9d05bSMatthias Ringwald (void) it; 1075afde0c52Smatthias.ringwald 10760e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1077afde0c52Smatthias.ringwald 107809e9d05bSMatthias Ringwald // Notify channel packet handler if they can send now 107909e9d05bSMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 108009e9d05bSMatthias Ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 108109e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 108209e9d05bSMatthias Ringwald l2cap_notify_channel_can_send(); 108309e9d05bSMatthias Ringwald break; 108409e9d05bSMatthias Ringwald 108509e9d05bSMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 108609e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 108709e9d05bSMatthias Ringwald break; 108809e9d05bSMatthias Ringwald 108909e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1090afde0c52Smatthias.ringwald // handle connection complete events 1091afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1092724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 1093afde0c52Smatthias.ringwald if (packet[2] == 0){ 1094f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1095afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 1096afde0c52Smatthias.ringwald } else { 1097afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 1098afde0c52Smatthias.ringwald } 1099afde0c52Smatthias.ringwald break; 1100afde0c52Smatthias.ringwald 1101afde0c52Smatthias.ringwald // handle successful create connection cancel command 1102afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 1103073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) { 1104afde0c52Smatthias.ringwald if (packet[5] == 0){ 1105724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 1106afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 1107afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 110803cfbabcSmatthias.ringwald } 11091e6aba47Smatthias.ringwald } 111039d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 111139d59809Smatthias.ringwald break; 111209e9d05bSMatthias Ringwald #endif 111327a923d0Smatthias.ringwald 11141e6aba47Smatthias.ringwald // handle disconnection complete events 1115afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1116c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 1117f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 111809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1119665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1120665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1121665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1122fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 112315ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 11249dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1125665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1126d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 112727a923d0Smatthias.ringwald } 112809e9d05bSMatthias Ringwald #endif 1129a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1130991fea48SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1131991fea48SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1132991fea48SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1133991fea48SMatthias Ringwald if (channel->con_handle != handle) continue; 1134991fea48SMatthias Ringwald l2cap_emit_channel_closed(channel); 1135991fea48SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1136991fea48SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1137991fea48SMatthias Ringwald } 1138991fea48SMatthias Ringwald #endif 1139afde0c52Smatthias.ringwald break; 1140fcadd0caSmatthias.ringwald 1141ee091cf1Smatthias.ringwald // HCI Connection Timeouts 114209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1143afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 1144f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1145bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 114680ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 11472d00edd4Smatthias.ringwald hci_con_used = 0; 1148665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1149665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1150665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1151fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11522d00edd4Smatthias.ringwald hci_con_used = 1; 1153c22aecc9S[email protected] break; 1154ee091cf1Smatthias.ringwald } 11552d00edd4Smatthias.ringwald if (hci_con_used) break; 1156d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 11579edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1158afde0c52Smatthias.ringwald break; 1159ee091cf1Smatthias.ringwald 1160df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1161f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1162665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1163665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1164665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1165fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11662df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1167df3354fcS[email protected] break; 1168df3354fcS[email protected] } 1169c22aecc9S[email protected] break; 1170df3354fcS[email protected] 11715611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1172f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1173bd63148eS[email protected] log_info("l2cap - security level update"); 1174665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1175665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1176665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1177fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11785533f01eS[email protected] 1179bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1180bd63148eS[email protected] 1181e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 11825533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 11835533f01eS[email protected] 1184df3354fcS[email protected] switch (channel->state){ 1185df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 11865533f01eS[email protected] if (actual_level >= required_level){ 1187f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 118844276248SMatthias Ringwald l2cap_emit_incoming_connection(channel); 11891eb2563eS[email protected] } else { 1190775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 11911eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 11921eb2563eS[email protected] } 1193df3354fcS[email protected] break; 1194df3354fcS[email protected] 1195df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 11965533f01eS[email protected] if (actual_level >= required_level){ 1197df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1198df3354fcS[email protected] } else { 1199df3354fcS[email protected] // disconnnect, authentication not good enough 1200df3354fcS[email protected] hci_disconnect_security_block(handle); 1201df3354fcS[email protected] } 1202df3354fcS[email protected] break; 1203df3354fcS[email protected] 1204df3354fcS[email protected] default: 1205df3354fcS[email protected] break; 1206df3354fcS[email protected] } 1207f85a9399S[email protected] } 1208f85a9399S[email protected] break; 120909e9d05bSMatthias Ringwald #endif 1210f85a9399S[email protected] 1211afde0c52Smatthias.ringwald default: 1212afde0c52Smatthias.ringwald break; 1213afde0c52Smatthias.ringwald } 1214afde0c52Smatthias.ringwald 1215bd63148eS[email protected] l2cap_run(); 12161e6aba47Smatthias.ringwald } 12171e6aba47Smatthias.ringwald 12182b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 12194cf56b4aSmatthias.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." 12202b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 12212b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 12222b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 12232b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 12242b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 12252b360848Smatthias.ringwald signaling_responses_pending++; 12262b360848Smatthias.ringwald l2cap_run(); 12272b360848Smatthias.ringwald } 12282b360848Smatthias.ringwald } 12292b360848Smatthias.ringwald 123009e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 123109e9d05bSMatthias Ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 123209e9d05bSMatthias Ringwald channel->remote_sig_id = identifier; 123309e9d05bSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 123409e9d05bSMatthias Ringwald l2cap_run(); 123509e9d05bSMatthias Ringwald } 123609e9d05bSMatthias Ringwald 1237b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1238645658c9Smatthias.ringwald 12399da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1240645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1241645658c9Smatthias.ringwald if (!service) { 1242645658c9Smatthias.ringwald // 0x0002 PSM not supported 12432b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1244645658c9Smatthias.ringwald return; 1245645658c9Smatthias.ringwald } 1246645658c9Smatthias.ringwald 12475061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1248645658c9Smatthias.ringwald if (!hci_connection) { 12492b360848Smatthias.ringwald // 12509da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1251645658c9Smatthias.ringwald return; 1252645658c9Smatthias.ringwald } 12532bd8b7e7S[email protected] 1254645658c9Smatthias.ringwald // alloc structure 12559da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1256da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC, 1257da144af5SMatthias Ringwald psm, service->mtu, service->required_security_level); 12582b360848Smatthias.ringwald if (!channel){ 12592b360848Smatthias.ringwald // 0x0004 No resources available 12602b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 12612b360848Smatthias.ringwald return; 12622b360848Smatthias.ringwald } 1263da144af5SMatthias Ringwald 1264fc64f94aSMatthias Ringwald channel->con_handle = handle; 1265b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1266b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1267645658c9Smatthias.ringwald 1268f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 12692985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 12702985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 12719775e25bSmatthias.ringwald } 12729775e25bSmatthias.ringwald 1273645658c9Smatthias.ringwald // set initial state 1274df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1275ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1276e405ae81Smatthias.ringwald 1277645658c9Smatthias.ringwald // add to connections list 1278665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1279645658c9Smatthias.ringwald 1280f85a9399S[email protected] // assert security requirements 12811eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1282e405ae81Smatthias.ringwald } 1283645658c9Smatthias.ringwald 1284ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1285e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1286b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1287e405ae81Smatthias.ringwald if (!channel) { 1288ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1289e405ae81Smatthias.ringwald return; 1290e405ae81Smatthias.ringwald } 1291e405ae81Smatthias.ringwald 1292552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1293e405ae81Smatthias.ringwald 1294552d92a1Smatthias.ringwald // process 1295552d92a1Smatthias.ringwald l2cap_run(); 1296e405ae81Smatthias.ringwald } 1297645658c9Smatthias.ringwald 12987ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){ 12997ef6a7bbSMatthias Ringwald log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid); 1300b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1301e405ae81Smatthias.ringwald if (!channel) { 1302ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1303e405ae81Smatthias.ringwald return; 1304e405ae81Smatthias.ringwald } 1305e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 13067ef6a7bbSMatthias Ringwald channel->reason = 0x04; // no resources available 1307e7ff783cSmatthias.ringwald l2cap_run(); 1308645658c9Smatthias.ringwald } 1309645658c9Smatthias.ringwald 13107f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1311b1988dceSmatthias.ringwald 1312b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1313b1988dceSmatthias.ringwald 1314f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 131563a7246aSmatthias.ringwald if (flags & 1) { 131663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 131763a7246aSmatthias.ringwald } 131863a7246aSmatthias.ringwald 13192784b77dSmatthias.ringwald // accept the other's configuration options 1320f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 13213de7c0caSmatthias.ringwald uint16_t pos = 8; 13223de7c0caSmatthias.ringwald while (pos < end_pos){ 132363a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 132463a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 132563a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 132663a7246aSmatthias.ringwald pos++; 13271dc511deSmatthias.ringwald uint8_t length = command[pos++]; 13281dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 132963a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1330f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 13319da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 133263a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 133363a7246aSmatthias.ringwald } 13340fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 13350fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1336f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 13370fe7a9d0S[email protected] } 133863a7246aSmatthias.ringwald // check for unknown options 133963a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1340c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 134163a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 13421dc511deSmatthias.ringwald } 13431dc511deSmatthias.ringwald pos += length; 13441dc511deSmatthias.ringwald } 13452784b77dSmatthias.ringwald } 13462784b77dSmatthias.ringwald 1347fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 13489da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 134973cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 135073cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1351019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1352019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1353fa8473a4Smatthias.ringwald return 1; 1354fa8473a4Smatthias.ringwald } 1355fa8473a4Smatthias.ringwald 1356fa8473a4Smatthias.ringwald 13577f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 13581e6aba47Smatthias.ringwald 135900d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 136000d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 136138e5900eSmatthias.ringwald uint16_t result = 0; 13621e6aba47Smatthias.ringwald 13639da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1364b35f641cSmatthias.ringwald 13659a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 13669a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 13679a011532Smatthias.ringwald switch (channel->state){ 1368fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 13699a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 13702b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 13719a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 13729a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 13739a011532Smatthias.ringwald break; 13749a011532Smatthias.ringwald 13759a011532Smatthias.ringwald default: 13769a011532Smatthias.ringwald // ignore in other states 13779a011532Smatthias.ringwald break; 13789a011532Smatthias.ringwald } 13799a011532Smatthias.ringwald return; 13809a011532Smatthias.ringwald } 13819a011532Smatthias.ringwald 138256081214Smatthias.ringwald // @STATEMACHINE(l2cap) 13831e6aba47Smatthias.ringwald switch (channel->state) { 13841e6aba47Smatthias.ringwald 13851e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 13861e6aba47Smatthias.ringwald switch (code){ 13871e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 13885932bd7cS[email protected] l2cap_stop_rtx(channel); 1389f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 139038e5900eSmatthias.ringwald switch (result) { 139138e5900eSmatthias.ringwald case 0: 1392169f8b28Smatthias.ringwald // successful connection 1393f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1394fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 139528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 139638e5900eSmatthias.ringwald break; 139738e5900eSmatthias.ringwald case 1: 13985932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 13995932bd7cS[email protected] l2cap_start_ertx(channel); 140038e5900eSmatthias.ringwald break; 140138e5900eSmatthias.ringwald default: 1402eb920dbeSmatthias.ringwald // channel closed 1403eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1404f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 140538e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1406eb920dbeSmatthias.ringwald 1407eb920dbeSmatthias.ringwald // drop link key if security block 1408eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 140915a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 1410eb920dbeSmatthias.ringwald } 1411eb920dbeSmatthias.ringwald 1412eb920dbeSmatthias.ringwald // discard channel 1413665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1414d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 141538e5900eSmatthias.ringwald break; 14161e6aba47Smatthias.ringwald } 14171e6aba47Smatthias.ringwald break; 141838e5900eSmatthias.ringwald 141938e5900eSmatthias.ringwald default: 14201e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 142138e5900eSmatthias.ringwald break; 14221e6aba47Smatthias.ringwald } 14231e6aba47Smatthias.ringwald break; 14241e6aba47Smatthias.ringwald 1425fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1426f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1427ae280e73Smatthias.ringwald switch (code) { 1428ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 142928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1430ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 143163a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 143263a7246aSmatthias.ringwald // only done if continuation not set 143363a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 143463a7246aSmatthias.ringwald } 1435ae280e73Smatthias.ringwald break; 14361e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 14375932bd7cS[email protected] l2cap_stop_rtx(channel); 14385932bd7cS[email protected] switch (result){ 14395932bd7cS[email protected] case 0: // success 14405932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 14415932bd7cS[email protected] break; 14425932bd7cS[email protected] case 4: // pending 14435932bd7cS[email protected] l2cap_start_ertx(channel); 14445932bd7cS[email protected] break; 14455932bd7cS[email protected] default: 1446fe9d8984S[email protected] // retry on negative result 1447fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1448fe9d8984S[email protected] break; 1449fe9d8984S[email protected] } 14505a67bd4aSmatthias.ringwald break; 14515a67bd4aSmatthias.ringwald default: 14525a67bd4aSmatthias.ringwald break; 14531e6aba47Smatthias.ringwald } 1454fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1455fa8473a4Smatthias.ringwald // for open: 14565a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1457fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1458c8e4258aSmatthias.ringwald } 1459c8e4258aSmatthias.ringwald break; 1460f62db1e3Smatthias.ringwald 1461f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1462f62db1e3Smatthias.ringwald switch (code) { 1463f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 146427a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 146527a923d0Smatthias.ringwald break; 14665a67bd4aSmatthias.ringwald default: 14675a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 14685a67bd4aSmatthias.ringwald break; 146927a923d0Smatthias.ringwald } 147027a923d0Smatthias.ringwald break; 147184836b65Smatthias.ringwald 147284836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 147384836b65Smatthias.ringwald // @TODO handle incoming requests 147484836b65Smatthias.ringwald break; 147584836b65Smatthias.ringwald 147684836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 147784836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 147884836b65Smatthias.ringwald break; 147910642e45Smatthias.ringwald default: 148010642e45Smatthias.ringwald break; 148127a923d0Smatthias.ringwald } 14829da54300S[email protected] // log_info("new state %u", channel->state); 148327a923d0Smatthias.ringwald } 148427a923d0Smatthias.ringwald 148500d93d79Smatthias.ringwald 14867f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 148700d93d79Smatthias.ringwald 148800d93d79Smatthias.ringwald // get code, signalind identifier and command len 148900d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 149000d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 149100d93d79Smatthias.ringwald 149200d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 149300d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 149463a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 149500d93d79Smatthias.ringwald return; 149600d93d79Smatthias.ringwald } 149700d93d79Smatthias.ringwald 149800d93d79Smatthias.ringwald // general commands without an assigned channel 149900d93d79Smatthias.ringwald switch(code) { 150000d93d79Smatthias.ringwald 150100d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1502f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1503f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 150400d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 15052b83fb7dSmatthias.ringwald return; 150600d93d79Smatthias.ringwald } 150700d93d79Smatthias.ringwald 15082b360848Smatthias.ringwald case ECHO_REQUEST: 15092b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 15102b83fb7dSmatthias.ringwald return; 151100d93d79Smatthias.ringwald 151200d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1513f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 15142b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 15152b83fb7dSmatthias.ringwald return; 151600d93d79Smatthias.ringwald } 151700d93d79Smatthias.ringwald 151800d93d79Smatthias.ringwald default: 151900d93d79Smatthias.ringwald break; 152000d93d79Smatthias.ringwald } 152100d93d79Smatthias.ringwald 152200d93d79Smatthias.ringwald 152300d93d79Smatthias.ringwald // Get potential destination CID 1524f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 152500d93d79Smatthias.ringwald 152600d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1527665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1528665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1529665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1530665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1531fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 153200d93d79Smatthias.ringwald if (code & 1) { 1533b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1534b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 153500d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 15364e32727eSmatthias.ringwald break; 153700d93d79Smatthias.ringwald } 153800d93d79Smatthias.ringwald } else { 1539b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 154000d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 154100d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 15424e32727eSmatthias.ringwald break; 154300d93d79Smatthias.ringwald } 154400d93d79Smatthias.ringwald } 154500d93d79Smatthias.ringwald } 154600d93d79Smatthias.ringwald } 154709e9d05bSMatthias Ringwald #endif 154800d93d79Smatthias.ringwald 1549e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 155009e9d05bSMatthias Ringwald 155109e9d05bSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){ 155209e9d05bSMatthias Ringwald uint8_t event[6]; 155309e9d05bSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 155409e9d05bSMatthias Ringwald event[1] = 4; 155509e9d05bSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 155609e9d05bSMatthias Ringwald little_endian_store_16(event, 4, result); 155709e9d05bSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 155809e9d05bSMatthias Ringwald if (!l2cap_event_packet_handler) return; 155909e9d05bSMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 156009e9d05bSMatthias Ringwald } 156109e9d05bSMatthias Ringwald 1562c48b2a2cSMatthias Ringwald // @returns valid 1563c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){ 1564e7d0c9aaSMatthias Ringwald hci_connection_t * connection; 1565c48b2a2cSMatthias Ringwald uint16_t result; 1566c48b2a2cSMatthias Ringwald uint8_t event[10]; 156783fd9c76SMatthias Ringwald 1568cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1569a3dc965aSMatthias Ringwald btstack_linked_list_iterator_t it; 1570a3dc965aSMatthias Ringwald l2cap_channel_t * channel; 1571a3dc965aSMatthias Ringwald uint16_t local_cid; 157283fd9c76SMatthias Ringwald uint16_t le_psm; 157363f0ac45SMatthias Ringwald uint16_t new_credits; 157463f0ac45SMatthias Ringwald uint16_t credits_before; 1575e7d0c9aaSMatthias Ringwald l2cap_service_t * service; 157683fd9c76SMatthias Ringwald #endif 157700d93d79Smatthias.ringwald 15781b8b8d05SMatthias Ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 157923017473SMatthias Ringwald log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id); 15801b8b8d05SMatthias Ringwald 15811b8b8d05SMatthias Ringwald switch (code){ 158200d93d79Smatthias.ringwald 1583c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_RESPONSE: 1584c48b2a2cSMatthias Ringwald result = little_endian_read_16(command, 4); 1585ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1586ccf076adS[email protected] break; 1587da886c03S[email protected] 1588c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_REQUEST: 1589e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1590da886c03S[email protected] if (connection){ 15916d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 15926d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 1593c48b2a2cSMatthias Ringwald return 0; 15946d91fb6cSMatthias Ringwald } 1595da886c03S[email protected] int update_parameter = 1; 1596a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 15974ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 1598c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(command,8); 1599c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(command,10); 1600c48b2a2cSMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(command,12); 1601c48b2a2cSMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(command,14); 1602da886c03S[email protected] 1603da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1604da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1605da886c03S[email protected] 1606da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1607da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1608da886c03S[email protected] 1609da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1610da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1611da886c03S[email protected] 1612da886c03S[email protected] if (update_parameter){ 1613da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1614da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1615da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1616da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1617da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1618da886c03S[email protected] } else { 1619da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1620da886c03S[email protected] } 1621c48b2a2cSMatthias Ringwald connection->le_con_param_update_identifier = sig_id; 1622da886c03S[email protected] } 1623da886c03S[email protected] 162433c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 1625c48b2a2cSMatthias Ringwald 1626c48b2a2cSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1627c48b2a2cSMatthias Ringwald event[1] = 8; 1628c48b2a2cSMatthias Ringwald memcpy(&event[2], &command[4], 8); 1629c48b2a2cSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 163033c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1631ccf076adS[email protected] break; 1632c48b2a2cSMatthias Ringwald 1633a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1634a3dc965aSMatthias Ringwald 163563f0ac45SMatthias Ringwald case COMMAND_REJECT: 163663f0ac45SMatthias Ringwald // Find channel for this sig_id and connection handle 163763f0ac45SMatthias Ringwald channel = NULL; 163863f0ac45SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 163963f0ac45SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 164063f0ac45SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 164163f0ac45SMatthias Ringwald if (a_channel->con_handle != handle) continue; 164263f0ac45SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 164363f0ac45SMatthias Ringwald channel = a_channel; 164463f0ac45SMatthias Ringwald break; 164563f0ac45SMatthias Ringwald } 164663f0ac45SMatthias Ringwald if (!channel) break; 164763f0ac45SMatthias Ringwald 164863f0ac45SMatthias Ringwald // if received while waiting for le connection response, assume legacy device 164963f0ac45SMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){ 165063f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 165163f0ac45SMatthias Ringwald // no official value for this, use: Connection refused – LE_PSM not supported - 0x0002 165244276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, 0x0002); 165363f0ac45SMatthias Ringwald 165463f0ac45SMatthias Ringwald // discard channel 165563f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 165663f0ac45SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 165763f0ac45SMatthias Ringwald break; 165863f0ac45SMatthias Ringwald } 165963f0ac45SMatthias Ringwald break; 166063f0ac45SMatthias Ringwald 1661e7d0c9aaSMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 1662e7d0c9aaSMatthias Ringwald 1663e7d0c9aaSMatthias Ringwald // get hci connection, bail if not found (must not happen) 1664e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1665e7d0c9aaSMatthias Ringwald if (!connection) return 0; 1666e7d0c9aaSMatthias Ringwald 1667e7d0c9aaSMatthias Ringwald // check if service registered 1668e7d0c9aaSMatthias Ringwald le_psm = little_endian_read_16(command, 4); 1669e7d0c9aaSMatthias Ringwald service = l2cap_le_get_service(le_psm); 1670e7d0c9aaSMatthias Ringwald 1671e7d0c9aaSMatthias Ringwald if (service){ 1672e7d0c9aaSMatthias Ringwald 1673e7d0c9aaSMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, 6); 1674e7d0c9aaSMatthias Ringwald if (source_cid < 0x40){ 1675e7d0c9aaSMatthias Ringwald // 0x0009 Connection refused - Invalid Source CID 1676e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009); 1677e7d0c9aaSMatthias Ringwald return 1; 1678e7d0c9aaSMatthias Ringwald } 1679e7d0c9aaSMatthias Ringwald 1680e7d0c9aaSMatthias Ringwald // go through list of channels for this ACL connection and check if we get a match 1681e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1682e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 16831b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 16841b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 16851b8b8d05SMatthias Ringwald if (a_channel->remote_cid != source_cid) continue; 1686e7d0c9aaSMatthias Ringwald // 0x000a Connection refused - Source CID already allocated 1687e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a); 1688e7d0c9aaSMatthias Ringwald return 1; 1689e7d0c9aaSMatthias Ringwald } 1690e7d0c9aaSMatthias Ringwald 169183fd9c76SMatthias Ringwald // security: check encryption 169283fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_2){ 169383fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) == 0){ 169483fd9c76SMatthias Ringwald // 0x0008 Connection refused - insufficient encryption 169583fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0008); 169683fd9c76SMatthias Ringwald return 1; 169783fd9c76SMatthias Ringwald } 169883fd9c76SMatthias Ringwald // anything less than 16 byte key size is insufficient 169983fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) < 16){ 170083fd9c76SMatthias Ringwald // 0x0007 Connection refused – insufficient encryption key size 170183fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0007); 170283fd9c76SMatthias Ringwald return 1; 170383fd9c76SMatthias Ringwald } 170483fd9c76SMatthias Ringwald } 170583fd9c76SMatthias Ringwald 170683fd9c76SMatthias Ringwald // security: check authencation 170783fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_3){ 170883fd9c76SMatthias Ringwald if (!sm_authenticated(handle)){ 170983fd9c76SMatthias Ringwald // 0x0005 Connection refused – insufficient authentication 171083fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0005); 171183fd9c76SMatthias Ringwald return 1; 171283fd9c76SMatthias Ringwald } 171383fd9c76SMatthias Ringwald } 171483fd9c76SMatthias Ringwald 171583fd9c76SMatthias Ringwald // security: check authorization 171683fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_4){ 171783fd9c76SMatthias Ringwald if (sm_authorization_state(handle) != AUTHORIZATION_GRANTED){ 171883fd9c76SMatthias Ringwald // 0x0006 Connection refused – insufficient authorization 171983fd9c76SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0006); 172083fd9c76SMatthias Ringwald return 1; 172183fd9c76SMatthias Ringwald } 172283fd9c76SMatthias Ringwald } 1723e7d0c9aaSMatthias Ringwald 1724e7d0c9aaSMatthias Ringwald // allocate channel 17251b8b8d05SMatthias Ringwald channel = l2cap_create_channel_entry(service->packet_handler, connection->address, 1726e7d0c9aaSMatthias Ringwald BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level); 1727e7d0c9aaSMatthias Ringwald if (!channel){ 1728e7d0c9aaSMatthias Ringwald // 0x0004 Connection refused – no resources available 1729e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004); 1730e7d0c9aaSMatthias Ringwald return 1; 1731e7d0c9aaSMatthias Ringwald } 1732e7d0c9aaSMatthias Ringwald 1733e7d0c9aaSMatthias Ringwald channel->con_handle = handle; 1734e7d0c9aaSMatthias Ringwald channel->remote_cid = source_cid; 1735e7d0c9aaSMatthias Ringwald channel->remote_sig_id = sig_id; 17367f107edaSMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, 8); 17377f107edaSMatthias Ringwald channel->remote_mps = little_endian_read_16(command, 10); 17387f107edaSMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, 12); 1739e7d0c9aaSMatthias Ringwald 1740e7d0c9aaSMatthias Ringwald // set initial state 1741e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1742c99bb618SMatthias Ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING; 1743e7d0c9aaSMatthias Ringwald 1744e7d0c9aaSMatthias Ringwald // add to connections list 1745e7d0c9aaSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1746e7d0c9aaSMatthias Ringwald 1747e7d0c9aaSMatthias Ringwald // post connection request event 174844276248SMatthias Ringwald l2cap_emit_le_incoming_connection(channel); 1749e7d0c9aaSMatthias Ringwald 1750e7d0c9aaSMatthias Ringwald } else { 1751e7d0c9aaSMatthias Ringwald // Connection refused – LE_PSM not supported 1752e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002); 1753e7d0c9aaSMatthias Ringwald } 1754e7d0c9aaSMatthias Ringwald break; 17551b8b8d05SMatthias Ringwald 17561b8b8d05SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_RESPONSE: 17571b8b8d05SMatthias Ringwald // Find channel for this sig_id and connection handle 17581b8b8d05SMatthias Ringwald channel = NULL; 17591b8b8d05SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 17601b8b8d05SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 17611b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 17621b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 17631b8b8d05SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 17641b8b8d05SMatthias Ringwald channel = a_channel; 17651b8b8d05SMatthias Ringwald break; 17661b8b8d05SMatthias Ringwald } 17671b8b8d05SMatthias Ringwald if (!channel) break; 17681b8b8d05SMatthias Ringwald 17691b8b8d05SMatthias Ringwald // cid + 0 17701b8b8d05SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8); 17711b8b8d05SMatthias Ringwald if (result){ 17721b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 17731b8b8d05SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 177444276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 17751b8b8d05SMatthias Ringwald 17761b8b8d05SMatthias Ringwald // discard channel 177763f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 17781b8b8d05SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 17791b8b8d05SMatthias Ringwald break; 17801b8b8d05SMatthias Ringwald } 17811b8b8d05SMatthias Ringwald 17821b8b8d05SMatthias Ringwald // success 17831b8b8d05SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 17841b8b8d05SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 17851b8b8d05SMatthias Ringwald channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4); 17861b8b8d05SMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6); 17871b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 178844276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 17891b8b8d05SMatthias Ringwald break; 17901b8b8d05SMatthias Ringwald 179185aeef60SMatthias Ringwald case LE_FLOW_CONTROL_CREDIT: 179285aeef60SMatthias Ringwald // find channel 179385aeef60SMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 179485aeef60SMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 179563f0ac45SMatthias Ringwald if (!channel) { 179663f0ac45SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 179763f0ac45SMatthias Ringwald break; 179863f0ac45SMatthias Ringwald } 179963f0ac45SMatthias Ringwald new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 180063f0ac45SMatthias Ringwald credits_before = channel->credits_outgoing; 180163f0ac45SMatthias Ringwald channel->credits_outgoing += new_credits; 180263f0ac45SMatthias Ringwald // check for credit overrun 180363f0ac45SMatthias Ringwald if (credits_before > channel->credits_outgoing){ 180463f0ac45SMatthias Ringwald log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid); 180563f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 180663f0ac45SMatthias Ringwald break; 180763f0ac45SMatthias Ringwald } 180863f0ac45SMatthias Ringwald log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing); 180985aeef60SMatthias Ringwald break; 181085aeef60SMatthias Ringwald 1811828a7f7aSMatthias Ringwald case DISCONNECTION_REQUEST: 1812828a7f7aSMatthias Ringwald // find channel 1813828a7f7aSMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 1814828a7f7aSMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 181563f34e00SMatthias Ringwald if (!channel) { 181663f34e00SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 181763f34e00SMatthias Ringwald break; 181863f34e00SMatthias Ringwald } 1819828a7f7aSMatthias Ringwald channel->remote_sig_id = sig_id; 1820828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1821828a7f7aSMatthias Ringwald break; 1822828a7f7aSMatthias Ringwald 1823a3dc965aSMatthias Ringwald #endif 1824a3dc965aSMatthias Ringwald 182563f0ac45SMatthias Ringwald case DISCONNECTION_RESPONSE: 182663f0ac45SMatthias Ringwald break; 182763f0ac45SMatthias Ringwald 1828c48b2a2cSMatthias Ringwald default: 1829c48b2a2cSMatthias Ringwald // command unknown -> reject command 1830c48b2a2cSMatthias Ringwald return 0; 1831ccf076adS[email protected] } 1832c48b2a2cSMatthias Ringwald return 1; 1833c48b2a2cSMatthias Ringwald } 1834e7d0c9aaSMatthias Ringwald #endif 1835c48b2a2cSMatthias Ringwald 1836c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 1837c48b2a2cSMatthias Ringwald 183809e9d05bSMatthias Ringwald l2cap_channel_t * l2cap_channel; 183909e9d05bSMatthias Ringwald (void) l2cap_channel; 184009e9d05bSMatthias Ringwald 1841c48b2a2cSMatthias Ringwald // Get Channel ID 1842c48b2a2cSMatthias Ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 1843c48b2a2cSMatthias Ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 1844c48b2a2cSMatthias Ringwald 1845c48b2a2cSMatthias Ringwald switch (channel_id) { 1846c48b2a2cSMatthias Ringwald 184709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1848c48b2a2cSMatthias Ringwald case L2CAP_CID_SIGNALING: { 1849c48b2a2cSMatthias Ringwald uint16_t command_offset = 8; 1850c48b2a2cSMatthias Ringwald while (command_offset < size) { 1851c48b2a2cSMatthias Ringwald // handle signaling commands 1852c48b2a2cSMatthias Ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 1853c48b2a2cSMatthias Ringwald 1854c48b2a2cSMatthias Ringwald // increment command_offset 1855c48b2a2cSMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1856c48b2a2cSMatthias Ringwald } 1857c48b2a2cSMatthias Ringwald break; 1858c48b2a2cSMatthias Ringwald } 185909e9d05bSMatthias Ringwald #endif 1860c48b2a2cSMatthias Ringwald 1861e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1862e7d0c9aaSMatthias Ringwald case L2CAP_CID_SIGNALING_LE: { 1863e7d0c9aaSMatthias Ringwald uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 1864e7d0c9aaSMatthias Ringwald int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id); 1865c48b2a2cSMatthias Ringwald if (!valid){ 18661bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1867ccf076adS[email protected] } 1868ccf076adS[email protected] break; 1869e7d0c9aaSMatthias Ringwald } 1870e7d0c9aaSMatthias Ringwald #endif 1871c48b2a2cSMatthias Ringwald 1872c48b2a2cSMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1873c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 1874c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1875ccf076adS[email protected] } 1876c48b2a2cSMatthias Ringwald break; 1877c48b2a2cSMatthias Ringwald 1878c48b2a2cSMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1879c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 1880c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1881c48b2a2cSMatthias Ringwald } 1882c48b2a2cSMatthias Ringwald break; 1883c48b2a2cSMatthias Ringwald 1884c48b2a2cSMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1885c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 1886c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1887c48b2a2cSMatthias Ringwald } 1888c48b2a2cSMatthias Ringwald break; 18891bbc0b23S[email protected] 189009e9d05bSMatthias Ringwald default: 189109e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 189200d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 189364e11ca9SMatthias Ringwald l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 1894d1fd2a88SMatthias Ringwald if (l2cap_channel) { 18953d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 189600d93d79Smatthias.ringwald } 189709e9d05bSMatthias Ringwald #endif 1898a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 189964e11ca9SMatthias Ringwald l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id); 190064e11ca9SMatthias Ringwald if (l2cap_channel) { 190185aeef60SMatthias Ringwald // credit counting 190285aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming == 0){ 190385aeef60SMatthias Ringwald log_error("LE Data Channel packet received but no incoming credits"); 190485aeef60SMatthias Ringwald l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 190585aeef60SMatthias Ringwald break; 190685aeef60SMatthias Ringwald } 190785aeef60SMatthias Ringwald l2cap_channel->credits_incoming--; 190885aeef60SMatthias Ringwald 190985aeef60SMatthias Ringwald // automatic credits 191085aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){ 191185aeef60SMatthias Ringwald l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT; 191285aeef60SMatthias Ringwald } 191385aeef60SMatthias Ringwald 1914cd529728SMatthias Ringwald // first fragment 1915cd529728SMatthias Ringwald uint16_t pos = 0; 1916cd529728SMatthias Ringwald if (!l2cap_channel->receive_sdu_len){ 1917cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER); 1918cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos = 0; 1919cd529728SMatthias Ringwald pos += 2; 1920cd529728SMatthias Ringwald size -= 2; 1921cd529728SMatthias Ringwald } 1922cd529728SMatthias Ringwald memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER); 1923cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER; 1924cd529728SMatthias Ringwald // done? 192563f0ac45SMatthias Ringwald log_info("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len); 1926cd529728SMatthias Ringwald if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){ 1927cd529728SMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len); 1928cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = 0; 1929cd529728SMatthias Ringwald } 193063f0ac45SMatthias Ringwald } else { 193163f0ac45SMatthias Ringwald log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id); 193264e11ca9SMatthias Ringwald } 193364e11ca9SMatthias Ringwald #endif 19345652b5ffS[email protected] break; 19355652b5ffS[email protected] } 193600d93d79Smatthias.ringwald 19371eb2563eS[email protected] l2cap_run(); 19382718e2e7Smatthias.ringwald } 193900d93d79Smatthias.ringwald 194009e9d05bSMatthias Ringwald // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 194109e9d05bSMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 194209e9d05bSMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 194309e9d05bSMatthias Ringwald if (index < 0) return; 194409e9d05bSMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 194509e9d05bSMatthias Ringwald } 194609e9d05bSMatthias Ringwald 194709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 194815ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 194927a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 1950f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1951f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1952f62db1e3Smatthias.ringwald // discard channel 19539dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1954665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1955d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1956c8e4258aSmatthias.ringwald } 19571e6aba47Smatthias.ringwald 19588f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1959665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1960665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1961665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1962665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 19639d9bbc01Smatthias.ringwald if ( service->psm == psm){ 19649d9bbc01Smatthias.ringwald return service; 19659d9bbc01Smatthias.ringwald }; 19669d9bbc01Smatthias.ringwald } 19679d9bbc01Smatthias.ringwald return NULL; 19689d9bbc01Smatthias.ringwald } 19699d9bbc01Smatthias.ringwald 19707192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 19717192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 19727192e786SMatthias Ringwald } 19737192e786SMatthias Ringwald 1974e0abb8e7S[email protected] 1975be2053a6SMatthias 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){ 1976be2053a6SMatthias Ringwald 1977be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1978e0abb8e7S[email protected] 19794bb582b6Smatthias.ringwald // check for alread registered psm 19809d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1981277abc2cSmatthias.ringwald if (service) { 1982be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1983be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1984277abc2cSmatthias.ringwald } 19859d9bbc01Smatthias.ringwald 19864bb582b6Smatthias.ringwald // alloc structure 1987bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1988277abc2cSmatthias.ringwald if (!service) { 1989be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1990be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1991277abc2cSmatthias.ringwald } 19929d9bbc01Smatthias.ringwald 19939d9bbc01Smatthias.ringwald // fill in 19949d9bbc01Smatthias.ringwald service->psm = psm; 19959d9bbc01Smatthias.ringwald service->mtu = mtu; 199605ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1997df3354fcS[email protected] service->required_security_level = security_level; 19989d9bbc01Smatthias.ringwald 19999d9bbc01Smatthias.ringwald // add to services list 2000665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 2001c0e866bfSmatthias.ringwald 2002c0e866bfSmatthias.ringwald // enable page scan 200315a95bd5SMatthias Ringwald gap_connectable_control(1); 20045842b6d9Smatthias.ringwald 2005be2053a6SMatthias Ringwald return 0; 20069d9bbc01Smatthias.ringwald } 20079d9bbc01Smatthias.ringwald 20087e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){ 2009e0abb8e7S[email protected] 2010e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 2011e0abb8e7S[email protected] 20129d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 20137e8856ebSMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 2014665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 2015d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 2016c0e866bfSmatthias.ringwald 2017c0e866bfSmatthias.ringwald // disable page scan when no services registered 20187e8856ebSMatthias Ringwald if (btstack_linked_list_empty(&l2cap_services)) { 201915a95bd5SMatthias Ringwald gap_connectable_control(0); 20209d9bbc01Smatthias.ringwald } 20217e8856ebSMatthias Ringwald return 0; 20227e8856ebSMatthias Ringwald } 202309e9d05bSMatthias Ringwald #endif 20249d9bbc01Smatthias.ringwald 20257192e786SMatthias Ringwald 2026cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 202783fd9c76SMatthias Ringwald 202857be49d6SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){ 202957be49d6SMatthias Ringwald if (!channel->waiting_for_can_send_now) return; 203057be49d6SMatthias Ringwald if (channel->send_sdu_buffer) return; 203157be49d6SMatthias Ringwald channel->waiting_for_can_send_now = 0; 203257be49d6SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid); 203357be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW); 203457be49d6SMatthias Ringwald } 203557be49d6SMatthias Ringwald 203657be49d6SMatthias Ringwald // 1BH2222 203757be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) { 203857be49d6SMatthias 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", 203957be49d6SMatthias Ringwald channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu); 204057be49d6SMatthias Ringwald uint8_t event[19]; 204157be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION; 204257be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 204357be49d6SMatthias Ringwald event[2] = channel->address_type; 204457be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 204557be49d6SMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 204657be49d6SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 204757be49d6SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 204857be49d6SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 204957be49d6SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_mtu); 205057be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 205157be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 205257be49d6SMatthias Ringwald } 205357be49d6SMatthias Ringwald // 11BH22222 205457be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) { 205557be49d6SMatthias 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", 205657be49d6SMatthias Ringwald status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 205757be49d6SMatthias Ringwald channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu); 2058c99bb618SMatthias Ringwald uint8_t event[23]; 205957be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED; 206057be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 206157be49d6SMatthias Ringwald event[2] = status; 206257be49d6SMatthias Ringwald event[3] = channel->address_type; 206357be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[4]); 206457be49d6SMatthias Ringwald little_endian_store_16(event, 10, channel->con_handle); 2065c99bb618SMatthias Ringwald event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0; 2066c99bb618SMatthias Ringwald little_endian_store_16(event, 13, channel->psm); 2067c99bb618SMatthias Ringwald little_endian_store_16(event, 15, channel->local_cid); 2068c99bb618SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_cid); 2069c99bb618SMatthias Ringwald little_endian_store_16(event, 19, channel->local_mtu); 2070c99bb618SMatthias Ringwald little_endian_store_16(event, 21, channel->remote_mtu); 207157be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 207257be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 207357be49d6SMatthias Ringwald } 207457be49d6SMatthias Ringwald 207557be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){ 207657be49d6SMatthias Ringwald btstack_linked_list_iterator_t it; 207757be49d6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 207857be49d6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 207957be49d6SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 208057be49d6SMatthias Ringwald if ( channel->local_cid == local_cid) { 208157be49d6SMatthias Ringwald return channel; 208257be49d6SMatthias Ringwald } 208357be49d6SMatthias Ringwald } 208457be49d6SMatthias Ringwald return NULL; 208557be49d6SMatthias Ringwald } 208657be49d6SMatthias Ringwald 208757be49d6SMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 208857be49d6SMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){ 208957be49d6SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 209057be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 209157be49d6SMatthias Ringwald // discard channel 209257be49d6SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 209357be49d6SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 209457be49d6SMatthias Ringwald } 209557be49d6SMatthias Ringwald 2096e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){ 2097e7d0c9aaSMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, le_psm); 20987192e786SMatthias Ringwald } 2099efedfb4cSMatthias Ringwald 2100da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){ 21017192e786SMatthias Ringwald 2102da144af5SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm); 21037192e786SMatthias Ringwald 21047192e786SMatthias Ringwald // check for alread registered psm 21057192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 21067192e786SMatthias Ringwald if (service) { 2107da144af5SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 21087192e786SMatthias Ringwald } 21097192e786SMatthias Ringwald 21107192e786SMatthias Ringwald // alloc structure 21117192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 21127192e786SMatthias Ringwald if (!service) { 21137192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 2114da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 21157192e786SMatthias Ringwald } 21167192e786SMatthias Ringwald 21177192e786SMatthias Ringwald // fill in 21187192e786SMatthias Ringwald service->psm = psm; 2119da144af5SMatthias Ringwald service->mtu = 0; 21207192e786SMatthias Ringwald service->packet_handler = packet_handler; 21217192e786SMatthias Ringwald service->required_security_level = security_level; 21227192e786SMatthias Ringwald 21237192e786SMatthias Ringwald // add to services list 2124665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 21257192e786SMatthias Ringwald 21267192e786SMatthias Ringwald // done 2127da144af5SMatthias Ringwald return 0; 21287192e786SMatthias Ringwald } 21297192e786SMatthias Ringwald 2130da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) { 21317192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 21327192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 21339367f9b0SMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 2134da144af5SMatthias Ringwald 2135665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 21367192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 2137da144af5SMatthias Ringwald return 0; 21387192e786SMatthias Ringwald } 2139da144af5SMatthias Ringwald 2140da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){ 2141e7d0c9aaSMatthias Ringwald // get channel 2142e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2143e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2144e7d0c9aaSMatthias Ringwald 2145e7d0c9aaSMatthias Ringwald // validate state 2146e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 2147e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2148e7d0c9aaSMatthias Ringwald } 2149e7d0c9aaSMatthias Ringwald 2150efedfb4cSMatthias Ringwald // set state accept connection 215123017473SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT; 215223017473SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 215323017473SMatthias Ringwald channel->local_mtu = mtu; 215485aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 215585aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 215685aeef60SMatthias Ringwald 215785aeef60SMatthias Ringwald // test 215863f0ac45SMatthias Ringwald // channel->new_credits_incoming = 1; 2159e7d0c9aaSMatthias Ringwald 2160e7d0c9aaSMatthias Ringwald // go 2161e7d0c9aaSMatthias Ringwald l2cap_run(); 2162da144af5SMatthias Ringwald return 0; 2163da144af5SMatthias Ringwald } 2164da144af5SMatthias Ringwald 2165da144af5SMatthias Ringwald /** 2166da144af5SMatthias Ringwald * @brief Deny incoming LE Data Channel connection due to resource constraints 2167da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2168da144af5SMatthias Ringwald */ 2169da144af5SMatthias Ringwald 2170da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){ 2171e7d0c9aaSMatthias Ringwald // get channel 2172e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2173e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2174e7d0c9aaSMatthias Ringwald 2175e7d0c9aaSMatthias Ringwald // validate state 2176e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 2177e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2178e7d0c9aaSMatthias Ringwald } 2179e7d0c9aaSMatthias Ringwald 2180efedfb4cSMatthias Ringwald // set state decline connection 2181e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE; 2182e7d0c9aaSMatthias Ringwald channel->reason = 0x04; // no resources available 2183e7d0c9aaSMatthias Ringwald l2cap_run(); 2184da144af5SMatthias Ringwald return 0; 2185da144af5SMatthias Ringwald } 2186da144af5SMatthias Ringwald 21877dafa750SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, 2188da144af5SMatthias Ringwald uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 2189efedfb4cSMatthias Ringwald uint16_t * out_local_cid) { 2190efedfb4cSMatthias Ringwald 21917dafa750SMatthias Ringwald log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu); 2192da144af5SMatthias Ringwald 21937dafa750SMatthias Ringwald 21947dafa750SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 21957dafa750SMatthias Ringwald if (!connection) { 21967dafa750SMatthias Ringwald log_error("no hci_connection for handle 0x%04x", con_handle); 21977dafa750SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 21987dafa750SMatthias Ringwald } 21997dafa750SMatthias Ringwald 22007dafa750SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, connection->address, connection->address_type, psm, mtu, security_level); 2201da144af5SMatthias Ringwald if (!channel) { 2202da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 2203da144af5SMatthias Ringwald } 2204e7d0c9aaSMatthias Ringwald log_info("l2cap_le_create_channel %p", channel); 2205da144af5SMatthias Ringwald 2206da144af5SMatthias Ringwald // store local_cid 2207da144af5SMatthias Ringwald if (out_local_cid){ 2208da144af5SMatthias Ringwald *out_local_cid = channel->local_cid; 2209da144af5SMatthias Ringwald } 2210da144af5SMatthias Ringwald 22117dafa750SMatthias Ringwald // provide buffer 22127dafa750SMatthias Ringwald channel->con_handle = con_handle; 2213cd529728SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 22147dafa750SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST; 221585aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 221685aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 221785aeef60SMatthias Ringwald 2218efedfb4cSMatthias Ringwald // add to connections list 2219efedfb4cSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 2220efedfb4cSMatthias Ringwald 22217dafa750SMatthias Ringwald // go 222263f0ac45SMatthias Ringwald l2cap_run(); 2223da144af5SMatthias Ringwald return 0; 2224da144af5SMatthias Ringwald } 2225da144af5SMatthias Ringwald 2226da144af5SMatthias Ringwald /** 2227da144af5SMatthias Ringwald * @brief Provide credtis for LE Data Channel 2228da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2229da144af5SMatthias Ringwald * @param credits Number additional credits for peer 2230da144af5SMatthias Ringwald */ 223164e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){ 223263f0ac45SMatthias Ringwald 223363f0ac45SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 223463f0ac45SMatthias Ringwald if (!channel) { 223563f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 223663f0ac45SMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 223763f0ac45SMatthias Ringwald } 223863f0ac45SMatthias Ringwald 2239efedfb4cSMatthias Ringwald // check state 224063f0ac45SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN){ 224163f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid); 224263f0ac45SMatthias Ringwald } 224363f0ac45SMatthias Ringwald 224463f0ac45SMatthias Ringwald // assert incoming credits + credits <= 0xffff 224563f0ac45SMatthias Ringwald uint32_t total_credits = channel->credits_incoming; 224663f0ac45SMatthias Ringwald total_credits += channel->new_credits_incoming; 224763f0ac45SMatthias Ringwald total_credits += credits; 224863f0ac45SMatthias Ringwald if (total_credits > 0xffff){ 224963f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming, 225063f0ac45SMatthias Ringwald channel->new_credits_incoming, credits); 225163f0ac45SMatthias Ringwald } 225263f0ac45SMatthias Ringwald 2253efedfb4cSMatthias Ringwald // set credits_granted 225463f0ac45SMatthias Ringwald channel->new_credits_incoming += credits; 225563f0ac45SMatthias Ringwald 225663f0ac45SMatthias Ringwald // go 225763f0ac45SMatthias Ringwald l2cap_run(); 2258da144af5SMatthias Ringwald return 0; 2259da144af5SMatthias Ringwald } 2260da144af5SMatthias Ringwald 2261da144af5SMatthias Ringwald /** 2262da144af5SMatthias Ringwald * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 2263da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2264da144af5SMatthias Ringwald */ 226564e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){ 226644276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 226744276248SMatthias Ringwald if (!channel) { 226844276248SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 2269da144af5SMatthias Ringwald return 0; 2270da144af5SMatthias Ringwald } 2271da144af5SMatthias Ringwald 227244276248SMatthias Ringwald // check state 227344276248SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN) return 0; 227444276248SMatthias Ringwald 227544276248SMatthias Ringwald // check queue 227644276248SMatthias Ringwald if (channel->send_sdu_buffer) return 0; 227744276248SMatthias Ringwald 227844276248SMatthias Ringwald // fine, go ahead 227944276248SMatthias Ringwald return 1; 228044276248SMatthias Ringwald } 228144276248SMatthias Ringwald 2282da144af5SMatthias Ringwald /** 2283da144af5SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 2284da144af5SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 2285da144af5SMatthias Ringwald * so packet handler should be ready to handle it 2286da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2287da144af5SMatthias Ringwald */ 228864e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){ 228944276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 229044276248SMatthias Ringwald if (!channel) { 229144276248SMatthias Ringwald log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid); 229244276248SMatthias Ringwald return 0; 229344276248SMatthias Ringwald } 229444276248SMatthias Ringwald channel->waiting_for_can_send_now = 1; 229544276248SMatthias Ringwald l2cap_le_notify_channel_can_send(channel); 2296da144af5SMatthias Ringwald return 0; 2297da144af5SMatthias Ringwald } 2298da144af5SMatthias Ringwald 2299da144af5SMatthias Ringwald /** 2300da144af5SMatthias Ringwald * @brief Send data via LE Data Channel 2301da144af5SMatthias Ringwald * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 2302da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2303da144af5SMatthias Ringwald * @param data data to send 2304da144af5SMatthias Ringwald * @param size data size 2305da144af5SMatthias Ringwald */ 230664e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){ 230764e11ca9SMatthias Ringwald 230864e11ca9SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 230964e11ca9SMatthias Ringwald if (!channel) { 231064e11ca9SMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 2311828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 231264e11ca9SMatthias Ringwald } 231364e11ca9SMatthias Ringwald 231464e11ca9SMatthias Ringwald if (len > channel->remote_mtu){ 231564e11ca9SMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 231664e11ca9SMatthias Ringwald return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 231764e11ca9SMatthias Ringwald } 231864e11ca9SMatthias Ringwald 23197f107edaSMatthias Ringwald if (channel->send_sdu_buffer){ 232064e11ca9SMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 232164e11ca9SMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 232264e11ca9SMatthias Ringwald } 232364e11ca9SMatthias Ringwald 23247f107edaSMatthias Ringwald channel->send_sdu_buffer = data; 23257f107edaSMatthias Ringwald channel->send_sdu_len = len; 23267f107edaSMatthias Ringwald channel->send_sdu_pos = 0; 232764e11ca9SMatthias Ringwald 23287f107edaSMatthias Ringwald l2cap_run(); 23297f107edaSMatthias Ringwald return 0; 2330da144af5SMatthias Ringwald } 2331da144af5SMatthias Ringwald 2332da144af5SMatthias Ringwald /** 2333da144af5SMatthias Ringwald * @brief Disconnect from LE Data Channel 2334da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2335da144af5SMatthias Ringwald */ 2336828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid) 2337da144af5SMatthias Ringwald { 2338828a7f7aSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2339828a7f7aSMatthias Ringwald if (!channel) { 2340828a7f7aSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 2341828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2342828a7f7aSMatthias Ringwald } 2343828a7f7aSMatthias Ringwald 2344828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 2345828a7f7aSMatthias Ringwald l2cap_run(); 2346da144af5SMatthias Ringwald return 0; 2347da144af5SMatthias Ringwald } 2348da144af5SMatthias Ringwald 23497f02f414SMatthias Ringwald #endif 2350