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 5343625864Smatthias.ringwald #include <stdarg.h> 5443625864Smatthias.ringwald #include <string.h> 5543625864Smatthias.ringwald 5643625864Smatthias.ringwald #include <stdio.h> 5743625864Smatthias.ringwald 584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 604c744e21Smatthias.ringwald 6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 62e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 6339bda6d5Smatthias.ringwald 6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6900d93d79Smatthias.ringwald 705628cf69SMatthias Ringwald // internal table 715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0 725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL 1 735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2 745628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1) 755628cf69SMatthias Ringwald 7633c40538SMatthias Ringwald // prototypes 7733c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 7833c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 7933c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 8034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel); 8133c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 8233c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 8333c40538SMatthias Ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 8433c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 853d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ); 8630725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void); 87e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 88828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel); 89e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm); 90e7d0c9aaSMatthias Ringwald #endif 9133c40538SMatthias Ringwald 925628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel { 935628cf69SMatthias Ringwald btstack_packet_handler_t callback; 942125de09SMatthias Ringwald uint8_t waiting_for_can_send_now; 955628cf69SMatthias Ringwald } l2cap_fixed_channel_t; 965628cf69SMatthias Ringwald 975628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels; 985628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services; 995628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels; 1005628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services; 10139bda6d5Smatthias.ringwald 10239bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 1032b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 1042b83fb7dSmatthias.ringwald static int signaling_responses_pending; 1052b83fb7dSmatthias.ringwald 10633c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp; 10733c40538SMatthias Ringwald 108fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 109fb37a842SMatthias Ringwald 11033c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler; 1115628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE]; 11239bda6d5Smatthias.ringwald 11334e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){ 11434e7e577SMatthias Ringwald switch (index){ 11534e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL: 11634e7e577SMatthias Ringwald return L2CAP_CID_ATTRIBUTE_PROTOCOL; 11734e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL: 11834e7e577SMatthias Ringwald return L2CAP_CID_SECURITY_MANAGER_PROTOCOL; 11934e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL: 12034e7e577SMatthias Ringwald return L2CAP_CID_CONNECTIONLESS_CHANNEL; 12134e7e577SMatthias Ringwald default: 12234e7e577SMatthias Ringwald return 0; 12334e7e577SMatthias Ringwald } 12434e7e577SMatthias Ringwald } 1255628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){ 1265628cf69SMatthias Ringwald switch (channel_id){ 1275628cf69SMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1285628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL; 1295628cf69SMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1305628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL; 1315628cf69SMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1325628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL; 1335628cf69SMatthias Ringwald default: 1345628cf69SMatthias Ringwald return -1; 1355628cf69SMatthias Ringwald } 1365628cf69SMatthias Ringwald } 13739bda6d5Smatthias.ringwald 13834e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){ 13934e7e577SMatthias Ringwald if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0; 14034e7e577SMatthias Ringwald return 1; 14134e7e577SMatthias Ringwald } 14234e7e577SMatthias Ringwald 14371de195eSMatthias Ringwald void l2cap_init(void){ 1442b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 145808a48abSmatthias.ringwald 146f5454fc6Smatthias.ringwald l2cap_channels = NULL; 147f5454fc6Smatthias.ringwald l2cap_services = NULL; 1487192e786SMatthias Ringwald l2cap_le_services = NULL; 1497192e786SMatthias Ringwald l2cap_le_channels = NULL; 150f5454fc6Smatthias.ringwald 15133c40538SMatthias Ringwald l2cap_event_packet_handler = NULL; 1525628cf69SMatthias Ringwald memset(fixed_channels, 0, sizeof(fixed_channels)); 153f5454fc6Smatthias.ringwald 154ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 155ac301f95S[email protected] 156fcadd0caSmatthias.ringwald // 1572718e2e7Smatthias.ringwald // register callback with HCI 158fcadd0caSmatthias.ringwald // 159d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &l2cap_hci_event_handler; 160fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 161fb37a842SMatthias Ringwald 162d9a7306aSMatthias Ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 163fb37a842SMatthias Ringwald 16415a95bd5SMatthias Ringwald gap_connectable_control(0); // no services yet 165fcadd0caSmatthias.ringwald } 166fcadd0caSmatthias.ringwald 167ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 16833c40538SMatthias Ringwald l2cap_event_packet_handler = handler; 1691e6aba47Smatthias.ringwald } 1701e6aba47Smatthias.ringwald 17117a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 17258de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 17358de5610Smatthias.ringwald } 17458de5610Smatthias.ringwald 17558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 176c9dc710bS[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", 177fc64f94aSMatthias Ringwald status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 178c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 179c9dc710bS[email protected] uint8_t event[23]; 18058de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 18158de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 18258de5610Smatthias.ringwald event[2] = status; 183724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 184fc64f94aSMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 185f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 186f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 187f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 188f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 189f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 190f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 19158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19217a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 19358de5610Smatthias.ringwald } 19458de5610Smatthias.ringwald 19558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 196e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 19758de5610Smatthias.ringwald uint8_t event[4]; 19858de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 19958de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 200f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 20158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 20217a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 20358de5610Smatthias.ringwald } 20458de5610Smatthias.ringwald 20558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 206e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 207fc64f94aSMatthias Ringwald bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid); 20858de5610Smatthias.ringwald uint8_t event[16]; 20958de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 21058de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 211724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 212fc64f94aSMatthias Ringwald little_endian_store_16(event, 8, channel->con_handle); 213f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 214f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 215f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 21658de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21717a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 2180af41d30Smatthias.ringwald } 219808a48abSmatthias.ringwald 22034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) { 22134e7e577SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel); 22233c40538SMatthias Ringwald uint8_t event[4]; 22333c40538SMatthias Ringwald event[0] = L2CAP_EVENT_CAN_SEND_NOW; 22433c40538SMatthias Ringwald event[1] = sizeof(event) - 2; 22534e7e577SMatthias Ringwald little_endian_store_16(event, 2, channel); 22633c40538SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 22734e7e577SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); 22833c40538SMatthias Ringwald } 22933c40538SMatthias Ringwald 230fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){ 231ccf076adS[email protected] uint8_t event[6]; 232ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 233ccf076adS[email protected] event[1] = 4; 234fc64f94aSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 235f8fbdce0SMatthias Ringwald little_endian_store_16(event, 4, result); 236ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23733c40538SMatthias Ringwald if (!l2cap_event_packet_handler) return; 23833c40538SMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 239ccf076adS[email protected] } 240ccf076adS[email protected] 2417f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 242665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 243665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 244665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 245665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 246b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 247f62db1e3Smatthias.ringwald return channel; 248f62db1e3Smatthias.ringwald } 249f62db1e3Smatthias.ringwald } 250f62db1e3Smatthias.ringwald return NULL; 251f62db1e3Smatthias.ringwald } 252f62db1e3Smatthias.ringwald 253e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 254e7d0c9aaSMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){ 255e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_t it; 256e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 257e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 258e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 259e7d0c9aaSMatthias Ringwald if ( channel->local_cid == local_cid) { 260e7d0c9aaSMatthias Ringwald return channel; 261e7d0c9aaSMatthias Ringwald } 262e7d0c9aaSMatthias Ringwald } 263e7d0c9aaSMatthias Ringwald return NULL; 264e7d0c9aaSMatthias Ringwald } 265e7d0c9aaSMatthias Ringwald #endif 266e7d0c9aaSMatthias Ringwald 2675cb87675SMatthias Ringwald static l2cap_channel_t * l2cap_get_le_channel_for_address_and_addr_type(bd_addr_t address, bd_addr_type_t address_type){ 2685cb87675SMatthias Ringwald btstack_linked_list_iterator_t it; 2695cb87675SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 2705cb87675SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2715cb87675SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2725cb87675SMatthias Ringwald if (channel->address_type != address_type) continue; 2735cb87675SMatthias Ringwald if (bd_addr_cmp(channel->address, address)) continue; 2745cb87675SMatthias Ringwald return channel; 2755cb87675SMatthias Ringwald } 2765cb87675SMatthias Ringwald return NULL; 2775cb87675SMatthias Ringwald } 2785cb87675SMatthias Ringwald 2790b9d7e78SMatthias Ringwald /// 2800b9d7e78SMatthias Ringwald 28130725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){ 28230725612SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 28330725612SMatthias Ringwald if (!channel) return; 28430725612SMatthias Ringwald channel->waiting_for_can_send_now = 1; 28530725612SMatthias Ringwald l2cap_notify_channel_can_send(); 28630725612SMatthias Ringwald } 28730725612SMatthias Ringwald 2880b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){ 2890b9d7e78SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 2900b9d7e78SMatthias Ringwald if (index < 0) return; 2910b9d7e78SMatthias Ringwald fixed_channels[index].waiting_for_can_send_now = 1; 2920b9d7e78SMatthias Ringwald l2cap_notify_channel_can_send(); 2930b9d7e78SMatthias Ringwald } 2940b9d7e78SMatthias Ringwald 2950b9d7e78SMatthias Ringwald /// 2960b9d7e78SMatthias Ringwald 2976b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2986b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2996b1fde37Smatthias.ringwald if (!channel) return 0; 3000b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(channel->con_handle); 3017856fb31S[email protected] } 3027856fb31S[email protected] 30383e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 30483e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 30583e7cdd9SMatthias Ringwald if (!channel) return 0; 3060b9d7e78SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->con_handle); 30783e7cdd9SMatthias Ringwald } 30883e7cdd9SMatthias Ringwald 309fc64f94aSMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){ 3100b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(con_handle); 3112125de09SMatthias Ringwald } 3120b9d7e78SMatthias Ringwald 3130b9d7e78SMatthias Ringwald /// 3143cab4fcaS[email protected] 31596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 31696cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 31796cbd662Smatthias.ringwald if (channel) { 31896cbd662Smatthias.ringwald return channel->remote_mtu; 31996cbd662Smatthias.ringwald } 32096cbd662Smatthias.ringwald return 0; 32196cbd662Smatthias.ringwald } 32296cbd662Smatthias.ringwald 323ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 324665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 325665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 326665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 327665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 3285932bd7cS[email protected] if ( &channel->rtx == ts) { 3295932bd7cS[email protected] return channel; 3305932bd7cS[email protected] } 3315932bd7cS[email protected] } 3325932bd7cS[email protected] return NULL; 3335932bd7cS[email protected] } 3345932bd7cS[email protected] 335ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 3365932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 33795d2c8f4SMatthias Ringwald if (!channel) return; 3385932bd7cS[email protected] 3395932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 3405932bd7cS[email protected] 3415932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 3425932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 3435932bd7cS[email protected] // notify client 3445932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 3455932bd7cS[email protected] 3465932bd7cS[email protected] // discard channel 3479dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 348665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 3495932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 3505932bd7cS[email protected] } 3515932bd7cS[email protected] 3525932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 3535932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 354528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 3555932bd7cS[email protected] } 3565932bd7cS[email protected] 3575932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 3585932bd7cS[email protected] l2cap_stop_rtx(channel); 359cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 360528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 361528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 362528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3635932bd7cS[email protected] } 3645932bd7cS[email protected] 3655932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3665932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3675932bd7cS[email protected] l2cap_stop_rtx(channel); 368528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 369528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 370528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3715932bd7cS[email protected] } 3725932bd7cS[email protected] 37371de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 374ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 375ac301f95S[email protected] } 376ac301f95S[email protected] 377df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 378ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 379df3354fcS[email protected] } 3805932bd7cS[email protected] 3817f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 382b1d43497Smatthias.ringwald 383a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3849da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 385b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 386b1d43497Smatthias.ringwald } 387b1d43497Smatthias.ringwald 3889da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3892a373862S[email protected] hci_reserve_packet_buffer(); 390facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 39158de5610Smatthias.ringwald va_list argptr; 39258de5610Smatthias.ringwald va_start(argptr, identifier); 39370efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 39458de5610Smatthias.ringwald va_end(argptr); 3959da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 396826f7347S[email protected] return hci_send_acl_packet_buffer(len); 39758de5610Smatthias.ringwald } 39858de5610Smatthias.ringwald 399a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4007f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 40170efece1S[email protected] 402a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4039da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 40470efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 40570efece1S[email protected] } 40670efece1S[email protected] 4079da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 4082a373862S[email protected] hci_reserve_packet_buffer(); 409facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 41070efece1S[email protected] va_list argptr; 41170efece1S[email protected] va_start(argptr, identifier); 41270efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 41370efece1S[email protected] va_end(argptr); 4149da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 415826f7347S[email protected] return hci_send_acl_packet_buffer(len); 41670efece1S[email protected] } 41770efece1S[email protected] #endif 41870efece1S[email protected] 419b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 420facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 421b1d43497Smatthias.ringwald } 4226218e6f1Smatthias.ringwald 4236b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 4246b4af23dS[email protected] return hci_reserve_packet_buffer(); 4256b4af23dS[email protected] } 4266b4af23dS[email protected] 42768a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 42868a0fcf7S[email protected] hci_release_packet_buffer(); 42968a0fcf7S[email protected] } 43068a0fcf7S[email protected] 431*7f107edaSMatthias Ringwald static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint16_t remote_cid, uint16_t len){ 432*7f107edaSMatthias Ringwald 433*7f107edaSMatthias Ringwald int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 434*7f107edaSMatthias Ringwald 435*7f107edaSMatthias Ringwald // 0 - Connection handle : PB=pb : BC=00 436*7f107edaSMatthias Ringwald little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14)); 437*7f107edaSMatthias Ringwald // 2 - ACL length 438*7f107edaSMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 439*7f107edaSMatthias Ringwald // 4 - L2CAP packet length 440*7f107edaSMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 441*7f107edaSMatthias Ringwald // 6 - L2CAP channel DEST 442*7f107edaSMatthias Ringwald little_endian_store_16(acl_buffer, 6, remote_cid); 443*7f107edaSMatthias Ringwald } 44468a0fcf7S[email protected] 445b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 446b1d43497Smatthias.ringwald 447c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 448c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 449c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 450c8b9416aS[email protected] } 451c8b9416aS[email protected] 45258de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 453b1d43497Smatthias.ringwald if (!channel) { 4549da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 455b1d43497Smatthias.ringwald return -1; // TODO: define error 4566218e6f1Smatthias.ringwald } 4576218e6f1Smatthias.ringwald 458fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){ 4599da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 460a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 461a35252c8S[email protected] } 462a35252c8S[email protected] 463fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle); 464b1d43497Smatthias.ringwald 465facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 466*7f107edaSMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, len); 46758de5610Smatthias.ringwald // send 468*7f107edaSMatthias Ringwald return hci_send_acl_packet_buffer(len+8); 46958de5610Smatthias.ringwald } 47058de5610Smatthias.ringwald 471fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){ 4722149f12eSmatthias.ringwald 473c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 474c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4752149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4762149f12eSmatthias.ringwald } 4772149f12eSmatthias.ringwald 478fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(con_handle)){ 479fc64f94aSMatthias Ringwald log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid); 480c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 481c8b9416aS[email protected] } 482c8b9416aS[email protected] 483fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid); 4842149f12eSmatthias.ringwald 485facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 486*7f107edaSMatthias Ringwald l2cap_setup_header(acl_buffer, con_handle, cid, len); 4872149f12eSmatthias.ringwald // send 488*7f107edaSMatthias Ringwald return hci_send_acl_packet_buffer(len+8); 4892149f12eSmatthias.ringwald } 4902149f12eSmatthias.ringwald 491ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 492b1d43497Smatthias.ringwald 493a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 494a35252c8S[email protected] if (!channel) { 495ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 496a35252c8S[email protected] return -1; // TODO: define error 497a35252c8S[email protected] } 498a35252c8S[email protected] 499f0efaa57S[email protected] if (len > channel->remote_mtu){ 500ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 501f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 502f0efaa57S[email protected] } 503f0efaa57S[email protected] 504fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)){ 505ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 506b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 507b1d43497Smatthias.ringwald } 508b1d43497Smatthias.ringwald 5092a373862S[email protected] hci_reserve_packet_buffer(); 510facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 511b1d43497Smatthias.ringwald 512b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 513b1d43497Smatthias.ringwald 514b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 515b1d43497Smatthias.ringwald } 516b1d43497Smatthias.ringwald 517fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){ 5182149f12eSmatthias.ringwald 519fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(con_handle)){ 520ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 5212149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 5222149f12eSmatthias.ringwald } 5232149f12eSmatthias.ringwald 5242a373862S[email protected] hci_reserve_packet_buffer(); 525facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 5262149f12eSmatthias.ringwald 5272149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 5282149f12eSmatthias.ringwald 529fc64f94aSMatthias Ringwald return l2cap_send_prepared_connectionless(con_handle, cid, len); 5302149f12eSmatthias.ringwald } 5312149f12eSmatthias.ringwald 532fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){ 533fc64f94aSMatthias Ringwald return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data); 5340e37e417S[email protected] } 5350e37e417S[email protected] 53628ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 53728ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 53828ca2b46S[email protected] } 53928ca2b46S[email protected] 54028ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 54128ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 54228ca2b46S[email protected] } 54328ca2b46S[email protected] 54428ca2b46S[email protected] 545b1d43497Smatthias.ringwald 5468158c421Smatthias.ringwald // MARK: L2CAP_RUN 5472cd0be45Smatthias.ringwald // process outstanding signaling tasks 5487f02f414SMatthias Ringwald static void l2cap_run(void){ 5492b83fb7dSmatthias.ringwald 55022c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 55122c29ab4SMatthias Ringwald 5522b83fb7dSmatthias.ringwald // check pending signaling responses 5532b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5542b83fb7dSmatthias.ringwald 5552b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 556a35252c8S[email protected] 557a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 558a35252c8S[email protected] 5592b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5602b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 56163a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 562f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5632b83fb7dSmatthias.ringwald 564f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 565f53da564S[email protected] signaling_responses_pending--; 566f53da564S[email protected] int i; 567f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 568f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 569f53da564S[email protected] } 570f53da564S[email protected] 571f53da564S[email protected] switch (response_code){ 5722b360848Smatthias.ringwald case CONNECTION_REQUEST: 5732b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5742bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5754d816277S[email protected] if (result == 0x0003){ 5762bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5774d816277S[email protected] } 5782b360848Smatthias.ringwald break; 5792b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5802b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5812b83fb7dSmatthias.ringwald break; 5822b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5833b0484b3S[email protected] switch (infoType){ 5843b0484b3S[email protected] case 1: { // Connectionless MTU 5853b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5863b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5873b0484b3S[email protected] break; 5883b0484b3S[email protected] } 5893b0484b3S[email protected] case 2: { // Extended Features Supported 590462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 591462e630dS[email protected] uint32_t features = 0x280; 5923b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5933b0484b3S[email protected] break; 5943b0484b3S[email protected] } 5953b0484b3S[email protected] case 3: { // Fixed Channels Supported 5963b0484b3S[email protected] uint8_t map[8]; 5973b0484b3S[email protected] memset(map, 0, 8); 598288636a2SMatthias Ringwald map[0] = 0x06; // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04) 5993b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 6003b0484b3S[email protected] break; 6013b0484b3S[email protected] } 6023b0484b3S[email protected] default: 6032b83fb7dSmatthias.ringwald // all other types are not supported 6042b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 6053b0484b3S[email protected] break; 6062b83fb7dSmatthias.ringwald } 6072b83fb7dSmatthias.ringwald break; 60863a7246aSmatthias.ringwald case COMMAND_REJECT: 6095ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 610a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 61170efece1S[email protected] case COMMAND_REJECT_LE: 61270efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 61363a7246aSmatthias.ringwald break; 61470efece1S[email protected] #endif 6152b83fb7dSmatthias.ringwald default: 6162b83fb7dSmatthias.ringwald // should not happen 6172b83fb7dSmatthias.ringwald break; 6182b83fb7dSmatthias.ringwald } 6192b83fb7dSmatthias.ringwald } 6202b83fb7dSmatthias.ringwald 621ae280e73Smatthias.ringwald uint8_t config_options[4]; 622665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 623665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 624665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 625baf94f06S[email protected] 626665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 62722c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 6282cd0be45Smatthias.ringwald switch (channel->state){ 6292cd0be45Smatthias.ringwald 630df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 631ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 632fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 633a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 634ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 635fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 636ad671560S[email protected] } 637ad671560S[email protected] break; 638ad671560S[email protected] 63902b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 640baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 64164472d52Smatthias.ringwald // send connection request - set state first 64264472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 64302b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6448f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 64502b22dc4Smatthias.ringwald break; 64602b22dc4Smatthias.ringwald 647e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 648fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 64922c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 650fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 651e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6529dcb2fb2S[email protected] l2cap_stop_rtx(channel); 653665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 654d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 655e7ff783cSmatthias.ringwald break; 656e7ff783cSmatthias.ringwald 657552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 658fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 659fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 66028ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 661fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 662552d92a1Smatthias.ringwald break; 663552d92a1Smatthias.ringwald 6646fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 665fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 6666fdcc387Smatthias.ringwald // success, start l2cap handshake 667b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6686fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 669fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6705932bd7cS[email protected] l2cap_start_rtx(channel); 6716fdcc387Smatthias.ringwald break; 6726fdcc387Smatthias.ringwald 673fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 674fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 67573cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 67663a7246aSmatthias.ringwald uint16_t flags = 0; 67728ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 67863a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 67963a7246aSmatthias.ringwald flags = 1; 68063a7246aSmatthias.ringwald } else { 68128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 68263a7246aSmatthias.ringwald } 68363a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 684fc64f94aSMatthias 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); 68563a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 68663a7246aSmatthias.ringwald config_options[0] = 1; // MTU 68763a7246aSmatthias.ringwald config_options[1] = 2; // len param 688f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 689fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 69063a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 69163a7246aSmatthias.ringwald } else { 692fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 69363a7246aSmatthias.ringwald } 69463a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 695fa8473a4Smatthias.ringwald } 69673cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 69728ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 69828ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 699b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 700ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 701ae280e73Smatthias.ringwald config_options[1] = 2; // len param 702f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 703fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 7045932bd7cS[email protected] l2cap_start_rtx(channel); 705fa8473a4Smatthias.ringwald } 706fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 707552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 708552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 709fa8473a4Smatthias.ringwald } 710552d92a1Smatthias.ringwald break; 711552d92a1Smatthias.ringwald 712e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 713fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 71422c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 715fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 7165932bd7cS[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 :) 717756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 718e7ff783cSmatthias.ringwald break; 719e7ff783cSmatthias.ringwald 720e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 721fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 722b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 7232cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 724fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 7252cd0be45Smatthias.ringwald break; 7262cd0be45Smatthias.ringwald default: 7272cd0be45Smatthias.ringwald break; 7282cd0be45Smatthias.ringwald } 7292cd0be45Smatthias.ringwald } 730da886c03S[email protected] 731a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 732efedfb4cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 733efedfb4cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 734*7f107edaSMatthias Ringwald uint8_t * acl_buffer; 735*7f107edaSMatthias Ringwald uint8_t * l2cap_payload; 736*7f107edaSMatthias Ringwald uint16_t pos; 737*7f107edaSMatthias Ringwald uint16_t payload_size; 738efedfb4cSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 739efedfb4cSMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 740efedfb4cSMatthias Ringwald switch (channel->state){ 7415cb87675SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST: 7425cb87675SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 7435cb87675SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE; 7445cb87675SMatthias Ringwald // le psm, source cid, mtu, mps, initial credits 7451b8b8d05SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 7465cb87675SMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, LE_CREDIT_BASED_CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid, 23, 23, 1); 7475cb87675SMatthias Ringwald break; 74823017473SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT: 74923017473SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 75023017473SMatthias Ringwald // TODO: support larger MPS 75123017473SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 75223017473SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, channel->local_mtu, 23, channel->credits_incoming, 0); 75364e11ca9SMatthias Ringwald // notify client 75464e11ca9SMatthias Ringwald l2cap_emit_channel_opened(channel, 0); 75523017473SMatthias Ringwald break; 756e7d0c9aaSMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE: 757e7d0c9aaSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 758e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 759e7d0c9aaSMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, channel->reason); 760e7d0c9aaSMatthias Ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 761e7d0c9aaSMatthias Ringwald l2cap_stop_rtx(channel); 762e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 763e7d0c9aaSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 764e7d0c9aaSMatthias Ringwald break; 765*7f107edaSMatthias Ringwald case L2CAP_STATE_OPEN: 766*7f107edaSMatthias Ringwald if (!channel->send_sdu_buffer) break; 767*7f107edaSMatthias Ringwald if (!channel->credits_outgoing) break; 768*7f107edaSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 769*7f107edaSMatthias Ringwald // send part of SDU 770*7f107edaSMatthias Ringwald hci_reserve_packet_buffer(); 771*7f107edaSMatthias Ringwald acl_buffer = hci_get_outgoing_packet_buffer(); 772*7f107edaSMatthias Ringwald l2cap_payload = acl_buffer + 8; 773*7f107edaSMatthias Ringwald pos = 0; 774*7f107edaSMatthias Ringwald if (!channel->send_sdu_pos){ 775*7f107edaSMatthias Ringwald // store SDU len 776*7f107edaSMatthias Ringwald channel->send_sdu_pos += 2; 777*7f107edaSMatthias Ringwald little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len); 778*7f107edaSMatthias Ringwald pos += 2; 779*7f107edaSMatthias Ringwald } 780*7f107edaSMatthias Ringwald payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos); 781*7f107edaSMatthias Ringwald log_info("len %u, pos %u => payload %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size); 782*7f107edaSMatthias Ringwald memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len 783*7f107edaSMatthias Ringwald pos += payload_size; 784*7f107edaSMatthias Ringwald channel->send_sdu_pos += payload_size; 785*7f107edaSMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, payload_size); 786*7f107edaSMatthias Ringwald // done 787*7f107edaSMatthias Ringwald 788*7f107edaSMatthias Ringwald // channel->credits_outgoing--; 789*7f107edaSMatthias Ringwald 790*7f107edaSMatthias Ringwald if (channel->send_sdu_pos >= channel->send_sdu_len + 2){ 791*7f107edaSMatthias Ringwald channel->send_sdu_buffer = NULL; 792*7f107edaSMatthias Ringwald // TODO: send done event 793*7f107edaSMatthias Ringwald } 794*7f107edaSMatthias Ringwald hci_send_acl_packet_buffer(8 + pos); 795*7f107edaSMatthias Ringwald break; 796828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 797828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 798828a7f7aSMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 799828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 800828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 801828a7f7aSMatthias Ringwald break; 802828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 803828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 804828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 805828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 806828a7f7aSMatthias Ringwald l2cap_le_finialize_channel_close(channel); // -- remove from list 807828a7f7aSMatthias Ringwald break; 808efedfb4cSMatthias Ringwald default: 809efedfb4cSMatthias Ringwald break; 810efedfb4cSMatthias Ringwald } 811efedfb4cSMatthias Ringwald } 812efedfb4cSMatthias Ringwald 813da886c03S[email protected] // send l2cap con paramter update if necessary 814da886c03S[email protected] hci_connections_get_iterator(&it); 815665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 816665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 8175d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 818b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 819da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 820b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 821b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 822b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 823b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 824b68d7bc3SMatthias Ringwald break; 825da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 826b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 827b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 828da886c03S[email protected] break; 829da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 830b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 831b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 832da886c03S[email protected] break; 833da886c03S[email protected] default: 834da886c03S[email protected] break; 835da886c03S[email protected] } 836efedfb4cSMatthias Ringwald #if 0 837efedfb4cSMatthias Ringwald void l2cap_le_run(void){ 838efedfb4cSMatthias Ringwald for all channels 839efedfb4cSMatthias Ringwald switch(state): 840efedfb4cSMatthias Ringwald 841efedfb4cSMatthias Ringwald if outgoing transfer active 842efedfb4cSMatthias Ringwald send next chunk 843efedfb4cSMatthias Ringwald if done, notify app 844efedfb4cSMatthias Ringwald } 845efedfb4cSMatthias Ringwald #endif 846da886c03S[email protected] } 8474d7157c3S[email protected] #endif 848da886c03S[email protected] 84922c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 8502cd0be45Smatthias.ringwald } 8512cd0be45Smatthias.ringwald 8524aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 8534ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 854fa8c92f6Smatthias.ringwald } 855fa8c92f6Smatthias.ringwald 85671de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 8574ff786cfS[email protected] return l2cap_max_mtu(); 858e5e1518dS[email protected] } 859e5e1518dS[email protected] 860fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){ 8612df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 8625533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 8632df5dadcS[email protected] // success, start l2cap handshake 864fc64f94aSMatthias Ringwald channel->con_handle = con_handle; 8652df5dadcS[email protected] // check remote SSP feature first 8662df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 8672df5dadcS[email protected] } 8682df5dadcS[email protected] } 8692df5dadcS[email protected] 870efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE 8715cb87675SMatthias Ringwald static void l2cap_handle_le_connection_complete(l2cap_channel_t * channel, uint8_t status, hci_con_handle_t con_handle){ 872efedfb4cSMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 873efedfb4cSMatthias Ringwald log_info("l2cap_le_handle_connection_complete expected state"); 8745cb87675SMatthias Ringwald // TODO: bail if status != 0 8755cb87675SMatthias Ringwald 876efedfb4cSMatthias Ringwald // success, start l2cap handshake 877efedfb4cSMatthias Ringwald channel->con_handle = con_handle; 8785cb87675SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST; 879efedfb4cSMatthias Ringwald } 880efedfb4cSMatthias Ringwald } 881efedfb4cSMatthias Ringwald #endif 882efedfb4cSMatthias Ringwald 883efedfb4cSMatthias Ringwald 8842df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 8852df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 8862df5dadcS[email protected] 8872df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 888ac301f95S[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)); 889fc64f94aSMatthias Ringwald if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 8902df5dadcS[email protected] // request security level 2 8912df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 892fc64f94aSMatthias Ringwald gap_request_security_level(channel->con_handle, LEVEL_2); 8932df5dadcS[email protected] return; 8942df5dadcS[email protected] } 8952df5dadcS[email protected] // fine, go ahead 8962df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 8972df5dadcS[email protected] } 8982df5dadcS[email protected] 899da144af5SMatthias 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, 900da144af5SMatthias Ringwald uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){ 901da144af5SMatthias Ringwald 902da144af5SMatthias Ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 903da144af5SMatthias Ringwald if (!channel) { 904da144af5SMatthias Ringwald return NULL; 905da144af5SMatthias Ringwald } 906da144af5SMatthias Ringwald 907da144af5SMatthias Ringwald // Init memory (make valgrind happy) 908da144af5SMatthias Ringwald memset(channel, 0, sizeof(l2cap_channel_t)); 909da144af5SMatthias Ringwald 910da144af5SMatthias Ringwald // fill in 911da144af5SMatthias Ringwald channel->packet_handler = packet_handler; 912da144af5SMatthias Ringwald bd_addr_copy(channel->address, address); 913da144af5SMatthias Ringwald channel->address_type = address_type; 914da144af5SMatthias Ringwald channel->psm = psm; 915da144af5SMatthias Ringwald channel->local_mtu = local_mtu; 916da144af5SMatthias Ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 917da144af5SMatthias Ringwald channel->required_security_level = security_level; 918da144af5SMatthias Ringwald 919da144af5SMatthias Ringwald // 920da144af5SMatthias Ringwald channel->local_cid = l2cap_next_local_cid(); 921da144af5SMatthias Ringwald channel->con_handle = 0; 922da144af5SMatthias Ringwald 923da144af5SMatthias Ringwald // set initial state 924da144af5SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 925da144af5SMatthias Ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 926da144af5SMatthias Ringwald channel->remote_sig_id = L2CAP_SIG_ID_INVALID; 927da144af5SMatthias Ringwald channel->local_sig_id = L2CAP_SIG_ID_INVALID; 928da144af5SMatthias Ringwald return channel; 929da144af5SMatthias Ringwald } 930da144af5SMatthias Ringwald 9319077cb15SMatthias Ringwald /** 9329077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 9339077cb15SMatthias Ringwald * @param packet_handler 9349077cb15SMatthias Ringwald * @param address 9359077cb15SMatthias Ringwald * @param psm 9369077cb15SMatthias Ringwald * @param mtu 9379077cb15SMatthias Ringwald * @param local_cid 9389077cb15SMatthias Ringwald */ 9399077cb15SMatthias Ringwald 940da144af5SMatthias 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){ 941da144af5SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu); 942da144af5SMatthias Ringwald 943da144af5SMatthias Ringwald if (local_mtu > l2cap_max_mtu()) { 944da144af5SMatthias Ringwald local_mtu = l2cap_max_mtu(); 945da144af5SMatthias Ringwald } 946da144af5SMatthias Ringwald 947da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0); 948fc64f94aSMatthias Ringwald if (!channel) { 9499077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 9509077cb15SMatthias Ringwald } 9519077cb15SMatthias Ringwald 9529077cb15SMatthias Ringwald // add to connections list 953fc64f94aSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 9549077cb15SMatthias Ringwald 9559077cb15SMatthias Ringwald // store local_cid 9569077cb15SMatthias Ringwald if (out_local_cid){ 957fc64f94aSMatthias Ringwald *out_local_cid = channel->local_cid; 9589077cb15SMatthias Ringwald } 9599077cb15SMatthias Ringwald 9609077cb15SMatthias Ringwald // check if hci connection is already usable 9619077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 9629077cb15SMatthias Ringwald if (conn){ 963add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 964fc64f94aSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 9659077cb15SMatthias Ringwald // check if remote supported fearures are already received 9669077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 967fc64f94aSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 9689077cb15SMatthias Ringwald } 9699077cb15SMatthias Ringwald } 9709077cb15SMatthias Ringwald 9719077cb15SMatthias Ringwald l2cap_run(); 9729077cb15SMatthias Ringwald 9739077cb15SMatthias Ringwald return 0; 9749077cb15SMatthias Ringwald } 9759077cb15SMatthias Ringwald 976ce8f182eSMatthias Ringwald void 977ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 978e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 979b35f641cSmatthias.ringwald // find channel for local_cid 980b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 981f62db1e3Smatthias.ringwald if (channel) { 982e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 983f62db1e3Smatthias.ringwald } 9842cd0be45Smatthias.ringwald // process 9852cd0be45Smatthias.ringwald l2cap_run(); 98643625864Smatthias.ringwald } 9871e6aba47Smatthias.ringwald 988afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 989665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 990665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 991665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 992665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 993058e3d6bSMatthias Ringwald if ( bd_addr_cmp( channel->address, address) != 0) continue; 994c22aecc9S[email protected] // channel for this address found 995c22aecc9S[email protected] switch (channel->state){ 996c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 997c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 998afde0c52Smatthias.ringwald // failure, forward error code 999afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 1000afde0c52Smatthias.ringwald // discard channel 10019dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1002665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1003d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1004c22aecc9S[email protected] break; 1005c22aecc9S[email protected] default: 1006c22aecc9S[email protected] break; 1007afde0c52Smatthias.ringwald } 1008afde0c52Smatthias.ringwald } 1009afde0c52Smatthias.ringwald } 1010afde0c52Smatthias.ringwald 1011afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 1012665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1013665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1014665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1015665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1016058e3d6bSMatthias Ringwald if ( ! bd_addr_cmp( channel->address, address) ){ 10172df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 1018afde0c52Smatthias.ringwald } 1019afde0c52Smatthias.ringwald } 10206fdcc387Smatthias.ringwald // process 10216fdcc387Smatthias.ringwald l2cap_run(); 1022afde0c52Smatthias.ringwald } 1023b448a0e7Smatthias.ringwald 102433c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){ 102533c40538SMatthias Ringwald btstack_linked_list_iterator_t it; 102633c40538SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 102733c40538SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 102833c40538SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 102933c40538SMatthias Ringwald if (!channel->waiting_for_can_send_now) continue; 1030fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 103133c40538SMatthias Ringwald channel->waiting_for_can_send_now = 0; 103234e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 103333c40538SMatthias Ringwald } 10342125de09SMatthias Ringwald 10352125de09SMatthias Ringwald int i; 10362125de09SMatthias Ringwald for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){ 1037773c4106SMatthias Ringwald if (!fixed_channels[i].callback) continue; 10382125de09SMatthias Ringwald if (!fixed_channels[i].waiting_for_can_send_now) continue; 103934e7e577SMatthias Ringwald int can_send; 104034e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 104134e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 104234e7e577SMatthias Ringwald } else { 104334e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 104434e7e577SMatthias Ringwald } 104534e7e577SMatthias Ringwald if (!can_send) continue; 104634e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 104734e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 10482125de09SMatthias Ringwald } 104933c40538SMatthias Ringwald } 105033c40538SMatthias Ringwald 1051d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 1052afde0c52Smatthias.ringwald 1053afde0c52Smatthias.ringwald bd_addr_t address; 1054afde0c52Smatthias.ringwald hci_con_handle_t handle; 1055665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 10562d00edd4Smatthias.ringwald int hci_con_used; 1057afde0c52Smatthias.ringwald 10580e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1059afde0c52Smatthias.ringwald 1060afde0c52Smatthias.ringwald // handle connection complete events 1061afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1062724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 1063afde0c52Smatthias.ringwald if (packet[2] == 0){ 1064f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1065afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 1066afde0c52Smatthias.ringwald } else { 1067afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 1068afde0c52Smatthias.ringwald } 1069afde0c52Smatthias.ringwald break; 1070afde0c52Smatthias.ringwald 1071afde0c52Smatthias.ringwald // handle successful create connection cancel command 1072afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 1073073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) { 1074afde0c52Smatthias.ringwald if (packet[5] == 0){ 1075724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 1076afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 1077afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 107803cfbabcSmatthias.ringwald } 10791e6aba47Smatthias.ringwald } 108039d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 108139d59809Smatthias.ringwald break; 108239d59809Smatthias.ringwald 108339d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 108439d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 1085afde0c52Smatthias.ringwald break; 108627a923d0Smatthias.ringwald 10871e6aba47Smatthias.ringwald // handle disconnection complete events 1088afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1089c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 1090f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1091665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1092665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1093665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1094fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 109515ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 10969dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1097665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1098d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 109927a923d0Smatthias.ringwald } 1100991fea48SMatthias Ringwald #ifdef ENABLE_BLE 1101991fea48SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1102991fea48SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1103991fea48SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1104991fea48SMatthias Ringwald if (channel->con_handle != handle) continue; 1105991fea48SMatthias Ringwald l2cap_emit_channel_closed(channel); 1106991fea48SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1107991fea48SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1108991fea48SMatthias Ringwald } 1109991fea48SMatthias Ringwald #endif 1110afde0c52Smatthias.ringwald break; 1111fcadd0caSmatthias.ringwald 111233c40538SMatthias Ringwald // Notify channel packet handler if they can send now 111363fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 11146218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 111502b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 111633c40538SMatthias Ringwald l2cap_notify_channel_can_send(); 11176218e6f1Smatthias.ringwald break; 11186218e6f1Smatthias.ringwald 1119ee091cf1Smatthias.ringwald // HCI Connection Timeouts 1120afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 1121f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1122bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 112380ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 11242d00edd4Smatthias.ringwald hci_con_used = 0; 1125665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1126665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1127665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1128fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11292d00edd4Smatthias.ringwald hci_con_used = 1; 1130c22aecc9S[email protected] break; 1131ee091cf1Smatthias.ringwald } 11322d00edd4Smatthias.ringwald if (hci_con_used) break; 1133d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 11349edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1135afde0c52Smatthias.ringwald break; 1136ee091cf1Smatthias.ringwald 1137df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1138f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1139665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1140665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1141665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1142fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11432df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1144df3354fcS[email protected] break; 1145df3354fcS[email protected] } 1146c22aecc9S[email protected] break; 1147df3354fcS[email protected] 11485cb87675SMatthias Ringwald #ifdef ENABLE_BLE 11495cb87675SMatthias Ringwald case HCI_EVENT_LE_META: 11505cb87675SMatthias Ringwald switch (packet[2]){ 11515cb87675SMatthias Ringwald int addr_type; 11525cb87675SMatthias Ringwald l2cap_channel_t * channel; 11535cb87675SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 11545cb87675SMatthias Ringwald // Connection management 11555cb87675SMatthias Ringwald reverse_bd_addr(&packet[8], address); 11565cb87675SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 11575cb87675SMatthias Ringwald log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address)); 11585cb87675SMatthias Ringwald // get l2cap_channel 11595cb87675SMatthias Ringwald // also pass in status 11605cb87675SMatthias Ringwald channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type); 11615cb87675SMatthias Ringwald if (!channel) break; 11625cb87675SMatthias Ringwald l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4)); 11635cb87675SMatthias Ringwald break; 11645cb87675SMatthias Ringwald default: 11655cb87675SMatthias Ringwald break; 11665cb87675SMatthias Ringwald } 11675cb87675SMatthias Ringwald break; 11685cb87675SMatthias Ringwald #endif 11695cb87675SMatthias Ringwald 11705611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1171f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1172bd63148eS[email protected] log_info("l2cap - security level update"); 1173665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1174665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1175665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1176fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11775533f01eS[email protected] 1178bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1179bd63148eS[email protected] 1180e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 11815533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 11825533f01eS[email protected] 1183df3354fcS[email protected] switch (channel->state){ 1184df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 11855533f01eS[email protected] if (actual_level >= required_level){ 1186f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1187f85a9399S[email protected] l2cap_emit_connection_request(channel); 11881eb2563eS[email protected] } else { 1189775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 11901eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 11911eb2563eS[email protected] } 1192df3354fcS[email protected] break; 1193df3354fcS[email protected] 1194df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 11955533f01eS[email protected] if (actual_level >= required_level){ 1196df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1197df3354fcS[email protected] } else { 1198df3354fcS[email protected] // disconnnect, authentication not good enough 1199df3354fcS[email protected] hci_disconnect_security_block(handle); 1200df3354fcS[email protected] } 1201df3354fcS[email protected] break; 1202df3354fcS[email protected] 1203df3354fcS[email protected] default: 1204df3354fcS[email protected] break; 1205df3354fcS[email protected] } 1206f85a9399S[email protected] } 1207f85a9399S[email protected] break; 1208f85a9399S[email protected] 1209afde0c52Smatthias.ringwald default: 1210afde0c52Smatthias.ringwald break; 1211afde0c52Smatthias.ringwald } 1212afde0c52Smatthias.ringwald 1213bd63148eS[email protected] l2cap_run(); 12141e6aba47Smatthias.ringwald } 12151e6aba47Smatthias.ringwald 1216afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1217b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1218e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1219e7ff783cSmatthias.ringwald l2cap_run(); 122084836b65Smatthias.ringwald } 122184836b65Smatthias.ringwald 12222b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 12234cf56b4aSmatthias.ringwald // Vol 3, Part A, 4.3: "The DCID and SCID fields shall be ignored when the result field indi- cates the connection was refused." 12242b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 12252b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 12262b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 12272b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 12282b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 12292b360848Smatthias.ringwald signaling_responses_pending++; 12302b360848Smatthias.ringwald l2cap_run(); 12312b360848Smatthias.ringwald } 12322b360848Smatthias.ringwald } 12332b360848Smatthias.ringwald 1234b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1235645658c9Smatthias.ringwald 12369da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1237645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1238645658c9Smatthias.ringwald if (!service) { 1239645658c9Smatthias.ringwald // 0x0002 PSM not supported 12402b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1241645658c9Smatthias.ringwald return; 1242645658c9Smatthias.ringwald } 1243645658c9Smatthias.ringwald 12445061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1245645658c9Smatthias.ringwald if (!hci_connection) { 12462b360848Smatthias.ringwald // 12479da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1248645658c9Smatthias.ringwald return; 1249645658c9Smatthias.ringwald } 12502bd8b7e7S[email protected] 1251645658c9Smatthias.ringwald // alloc structure 12529da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1253da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC, 1254da144af5SMatthias Ringwald psm, service->mtu, service->required_security_level); 12552b360848Smatthias.ringwald if (!channel){ 12562b360848Smatthias.ringwald // 0x0004 No resources available 12572b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 12582b360848Smatthias.ringwald return; 12592b360848Smatthias.ringwald } 1260da144af5SMatthias Ringwald 1261fc64f94aSMatthias Ringwald channel->con_handle = handle; 1262b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1263b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1264645658c9Smatthias.ringwald 1265f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 12662985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 12672985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 12689775e25bSmatthias.ringwald } 12699775e25bSmatthias.ringwald 1270645658c9Smatthias.ringwald // set initial state 1271df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1272ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1273e405ae81Smatthias.ringwald 1274645658c9Smatthias.ringwald // add to connections list 1275665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1276645658c9Smatthias.ringwald 1277f85a9399S[email protected] // assert security requirements 12781eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1279e405ae81Smatthias.ringwald } 1280645658c9Smatthias.ringwald 1281ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1282e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1283b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1284e405ae81Smatthias.ringwald if (!channel) { 1285ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1286e405ae81Smatthias.ringwald return; 1287e405ae81Smatthias.ringwald } 1288e405ae81Smatthias.ringwald 1289552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1290e405ae81Smatthias.ringwald 1291552d92a1Smatthias.ringwald // process 1292552d92a1Smatthias.ringwald l2cap_run(); 1293e405ae81Smatthias.ringwald } 1294645658c9Smatthias.ringwald 12957ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){ 12967ef6a7bbSMatthias Ringwald log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid); 1297b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1298e405ae81Smatthias.ringwald if (!channel) { 1299ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1300e405ae81Smatthias.ringwald return; 1301e405ae81Smatthias.ringwald } 1302e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 13037ef6a7bbSMatthias Ringwald channel->reason = 0x04; // no resources available 1304e7ff783cSmatthias.ringwald l2cap_run(); 1305645658c9Smatthias.ringwald } 1306645658c9Smatthias.ringwald 13077f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1308b1988dceSmatthias.ringwald 1309b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1310b1988dceSmatthias.ringwald 1311f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 131263a7246aSmatthias.ringwald if (flags & 1) { 131363a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 131463a7246aSmatthias.ringwald } 131563a7246aSmatthias.ringwald 13162784b77dSmatthias.ringwald // accept the other's configuration options 1317f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 13183de7c0caSmatthias.ringwald uint16_t pos = 8; 13193de7c0caSmatthias.ringwald while (pos < end_pos){ 132063a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 132163a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 132263a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 132363a7246aSmatthias.ringwald pos++; 13241dc511deSmatthias.ringwald uint8_t length = command[pos++]; 13251dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 132663a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1327f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 13289da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 132963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 133063a7246aSmatthias.ringwald } 13310fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 13320fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1333f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 13340fe7a9d0S[email protected] } 133563a7246aSmatthias.ringwald // check for unknown options 133663a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1337c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 133863a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 13391dc511deSmatthias.ringwald } 13401dc511deSmatthias.ringwald pos += length; 13411dc511deSmatthias.ringwald } 13422784b77dSmatthias.ringwald } 13432784b77dSmatthias.ringwald 1344fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 13459da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 134673cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 134773cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1348019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1349019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1350fa8473a4Smatthias.ringwald return 1; 1351fa8473a4Smatthias.ringwald } 1352fa8473a4Smatthias.ringwald 1353fa8473a4Smatthias.ringwald 13547f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 13551e6aba47Smatthias.ringwald 135600d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 135700d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 135838e5900eSmatthias.ringwald uint16_t result = 0; 13591e6aba47Smatthias.ringwald 13609da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1361b35f641cSmatthias.ringwald 13629a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 13639a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 13649a011532Smatthias.ringwald switch (channel->state){ 1365fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 13669a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 13672b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 13689a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 13699a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 13709a011532Smatthias.ringwald break; 13719a011532Smatthias.ringwald 13729a011532Smatthias.ringwald default: 13739a011532Smatthias.ringwald // ignore in other states 13749a011532Smatthias.ringwald break; 13759a011532Smatthias.ringwald } 13769a011532Smatthias.ringwald return; 13779a011532Smatthias.ringwald } 13789a011532Smatthias.ringwald 137956081214Smatthias.ringwald // @STATEMACHINE(l2cap) 13801e6aba47Smatthias.ringwald switch (channel->state) { 13811e6aba47Smatthias.ringwald 13821e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 13831e6aba47Smatthias.ringwald switch (code){ 13841e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 13855932bd7cS[email protected] l2cap_stop_rtx(channel); 1386f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 138738e5900eSmatthias.ringwald switch (result) { 138838e5900eSmatthias.ringwald case 0: 1389169f8b28Smatthias.ringwald // successful connection 1390f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1391fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 139228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 139338e5900eSmatthias.ringwald break; 139438e5900eSmatthias.ringwald case 1: 13955932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 13965932bd7cS[email protected] l2cap_start_ertx(channel); 139738e5900eSmatthias.ringwald break; 139838e5900eSmatthias.ringwald default: 1399eb920dbeSmatthias.ringwald // channel closed 1400eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1401f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 140238e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1403eb920dbeSmatthias.ringwald 1404eb920dbeSmatthias.ringwald // drop link key if security block 1405eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 140615a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 1407eb920dbeSmatthias.ringwald } 1408eb920dbeSmatthias.ringwald 1409eb920dbeSmatthias.ringwald // discard channel 1410665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1411d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 141238e5900eSmatthias.ringwald break; 14131e6aba47Smatthias.ringwald } 14141e6aba47Smatthias.ringwald break; 141538e5900eSmatthias.ringwald 141638e5900eSmatthias.ringwald default: 14171e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 141838e5900eSmatthias.ringwald break; 14191e6aba47Smatthias.ringwald } 14201e6aba47Smatthias.ringwald break; 14211e6aba47Smatthias.ringwald 1422fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1423f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1424ae280e73Smatthias.ringwald switch (code) { 1425ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 142628ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1427ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 142863a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 142963a7246aSmatthias.ringwald // only done if continuation not set 143063a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 143163a7246aSmatthias.ringwald } 1432ae280e73Smatthias.ringwald break; 14331e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 14345932bd7cS[email protected] l2cap_stop_rtx(channel); 14355932bd7cS[email protected] switch (result){ 14365932bd7cS[email protected] case 0: // success 14375932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 14385932bd7cS[email protected] break; 14395932bd7cS[email protected] case 4: // pending 14405932bd7cS[email protected] l2cap_start_ertx(channel); 14415932bd7cS[email protected] break; 14425932bd7cS[email protected] default: 1443fe9d8984S[email protected] // retry on negative result 1444fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1445fe9d8984S[email protected] break; 1446fe9d8984S[email protected] } 14475a67bd4aSmatthias.ringwald break; 14485a67bd4aSmatthias.ringwald default: 14495a67bd4aSmatthias.ringwald break; 14501e6aba47Smatthias.ringwald } 1451fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1452fa8473a4Smatthias.ringwald // for open: 14535a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1454fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1455c8e4258aSmatthias.ringwald } 1456c8e4258aSmatthias.ringwald break; 1457f62db1e3Smatthias.ringwald 1458f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1459f62db1e3Smatthias.ringwald switch (code) { 1460f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 146127a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 146227a923d0Smatthias.ringwald break; 14635a67bd4aSmatthias.ringwald default: 14645a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 14655a67bd4aSmatthias.ringwald break; 146627a923d0Smatthias.ringwald } 146727a923d0Smatthias.ringwald break; 146884836b65Smatthias.ringwald 146984836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 147084836b65Smatthias.ringwald // @TODO handle incoming requests 147184836b65Smatthias.ringwald break; 147284836b65Smatthias.ringwald 147384836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 147484836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 147584836b65Smatthias.ringwald break; 147610642e45Smatthias.ringwald default: 147710642e45Smatthias.ringwald break; 147827a923d0Smatthias.ringwald } 14799da54300S[email protected] // log_info("new state %u", channel->state); 148027a923d0Smatthias.ringwald } 148127a923d0Smatthias.ringwald 148200d93d79Smatthias.ringwald 14837f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 148400d93d79Smatthias.ringwald 148500d93d79Smatthias.ringwald // get code, signalind identifier and command len 148600d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 148700d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 148800d93d79Smatthias.ringwald 148900d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 149000d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 149163a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 149200d93d79Smatthias.ringwald return; 149300d93d79Smatthias.ringwald } 149400d93d79Smatthias.ringwald 149500d93d79Smatthias.ringwald // general commands without an assigned channel 149600d93d79Smatthias.ringwald switch(code) { 149700d93d79Smatthias.ringwald 149800d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1499f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1500f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 150100d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 15022b83fb7dSmatthias.ringwald return; 150300d93d79Smatthias.ringwald } 150400d93d79Smatthias.ringwald 15052b360848Smatthias.ringwald case ECHO_REQUEST: 15062b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 15072b83fb7dSmatthias.ringwald return; 150800d93d79Smatthias.ringwald 150900d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1510f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 15112b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 15122b83fb7dSmatthias.ringwald return; 151300d93d79Smatthias.ringwald } 151400d93d79Smatthias.ringwald 151500d93d79Smatthias.ringwald default: 151600d93d79Smatthias.ringwald break; 151700d93d79Smatthias.ringwald } 151800d93d79Smatthias.ringwald 151900d93d79Smatthias.ringwald 152000d93d79Smatthias.ringwald // Get potential destination CID 1521f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 152200d93d79Smatthias.ringwald 152300d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1524665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1525665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1526665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1527665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1528fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 152900d93d79Smatthias.ringwald if (code & 1) { 1530b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1531b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 153200d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 15334e32727eSmatthias.ringwald break; 153400d93d79Smatthias.ringwald } 153500d93d79Smatthias.ringwald } else { 1536b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 153700d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 153800d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 15394e32727eSmatthias.ringwald break; 154000d93d79Smatthias.ringwald } 154100d93d79Smatthias.ringwald } 154200d93d79Smatthias.ringwald } 154300d93d79Smatthias.ringwald } 154400d93d79Smatthias.ringwald 1545e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1546c48b2a2cSMatthias Ringwald // @returns valid 1547c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){ 1548e7d0c9aaSMatthias Ringwald hci_connection_t * connection; 1549c48b2a2cSMatthias Ringwald uint16_t result; 1550c48b2a2cSMatthias Ringwald uint8_t event[10]; 1551e7d0c9aaSMatthias Ringwald uint16_t le_psm; 1552828a7f7aSMatthias Ringwald uint16_t local_cid; 1553e7d0c9aaSMatthias Ringwald l2cap_service_t * service; 15541b8b8d05SMatthias Ringwald l2cap_channel_t * channel; 15551b8b8d05SMatthias Ringwald btstack_linked_list_iterator_t it; 155600d93d79Smatthias.ringwald 15571b8b8d05SMatthias Ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 155823017473SMatthias Ringwald log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id); 15591b8b8d05SMatthias Ringwald 15601b8b8d05SMatthias Ringwald switch (code){ 156100d93d79Smatthias.ringwald 1562c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_RESPONSE: 1563c48b2a2cSMatthias Ringwald result = little_endian_read_16(command, 4); 1564ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1565ccf076adS[email protected] break; 1566da886c03S[email protected] 1567c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_REQUEST: 1568e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1569da886c03S[email protected] if (connection){ 15706d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 15716d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 1572c48b2a2cSMatthias Ringwald return 0; 15736d91fb6cSMatthias Ringwald } 1574da886c03S[email protected] int update_parameter = 1; 1575a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 15764ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 1577c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(command,8); 1578c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(command,10); 1579c48b2a2cSMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(command,12); 1580c48b2a2cSMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(command,14); 1581da886c03S[email protected] 1582da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1583da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1584da886c03S[email protected] 1585da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1586da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1587da886c03S[email protected] 1588da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1589da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1590da886c03S[email protected] 1591da886c03S[email protected] if (update_parameter){ 1592da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1593da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1594da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1595da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1596da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1597da886c03S[email protected] } else { 1598da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1599da886c03S[email protected] } 1600c48b2a2cSMatthias Ringwald connection->le_con_param_update_identifier = sig_id; 1601da886c03S[email protected] } 1602da886c03S[email protected] 160333c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 1604c48b2a2cSMatthias Ringwald 1605c48b2a2cSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1606c48b2a2cSMatthias Ringwald event[1] = 8; 1607c48b2a2cSMatthias Ringwald memcpy(&event[2], &command[4], 8); 1608c48b2a2cSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 160933c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1610ccf076adS[email protected] break; 1611c48b2a2cSMatthias Ringwald 1612e7d0c9aaSMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 1613e7d0c9aaSMatthias Ringwald 1614e7d0c9aaSMatthias Ringwald // get hci connection, bail if not found (must not happen) 1615e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1616e7d0c9aaSMatthias Ringwald if (!connection) return 0; 1617e7d0c9aaSMatthias Ringwald 1618e7d0c9aaSMatthias Ringwald // check if service registered 1619e7d0c9aaSMatthias Ringwald le_psm = little_endian_read_16(command, 4); 1620e7d0c9aaSMatthias Ringwald service = l2cap_le_get_service(le_psm); 1621e7d0c9aaSMatthias Ringwald 1622e7d0c9aaSMatthias Ringwald if (service){ 1623e7d0c9aaSMatthias Ringwald 1624e7d0c9aaSMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, 6); 1625e7d0c9aaSMatthias Ringwald if (source_cid < 0x40){ 1626e7d0c9aaSMatthias Ringwald // 0x0009 Connection refused - Invalid Source CID 1627e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009); 1628e7d0c9aaSMatthias Ringwald return 1; 1629e7d0c9aaSMatthias Ringwald } 1630e7d0c9aaSMatthias Ringwald 1631e7d0c9aaSMatthias Ringwald // go through list of channels for this ACL connection and check if we get a match 1632e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1633e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 16341b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 16351b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 16361b8b8d05SMatthias Ringwald if (a_channel->remote_cid != source_cid) continue; 1637e7d0c9aaSMatthias Ringwald // 0x000a Connection refused - Source CID already allocated 1638e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a); 1639e7d0c9aaSMatthias Ringwald return 1; 1640e7d0c9aaSMatthias Ringwald } 1641e7d0c9aaSMatthias Ringwald 1642e7d0c9aaSMatthias Ringwald // TODO: deal with authentication requirements, errors 0x005 - 000x8 1643e7d0c9aaSMatthias Ringwald 1644e7d0c9aaSMatthias Ringwald // allocate channel 16451b8b8d05SMatthias Ringwald channel = l2cap_create_channel_entry(service->packet_handler, connection->address, 1646e7d0c9aaSMatthias Ringwald BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level); 1647e7d0c9aaSMatthias Ringwald if (!channel){ 1648e7d0c9aaSMatthias Ringwald // 0x0004 Connection refused – no resources available 1649e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004); 1650e7d0c9aaSMatthias Ringwald return 1; 1651e7d0c9aaSMatthias Ringwald } 1652e7d0c9aaSMatthias Ringwald 1653e7d0c9aaSMatthias Ringwald channel->con_handle = handle; 1654e7d0c9aaSMatthias Ringwald channel->remote_cid = source_cid; 1655e7d0c9aaSMatthias Ringwald channel->remote_sig_id = sig_id; 1656*7f107edaSMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, 8); 1657*7f107edaSMatthias Ringwald channel->remote_mps = little_endian_read_16(command, 10); 1658*7f107edaSMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, 12); 1659e7d0c9aaSMatthias Ringwald 1660e7d0c9aaSMatthias Ringwald // set initial state 1661e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1662e7d0c9aaSMatthias Ringwald 1663e7d0c9aaSMatthias Ringwald // add to connections list 1664e7d0c9aaSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1665e7d0c9aaSMatthias Ringwald 1666e7d0c9aaSMatthias Ringwald // post connection request event 1667e7d0c9aaSMatthias Ringwald l2cap_emit_connection_request(channel); 1668e7d0c9aaSMatthias Ringwald 1669e7d0c9aaSMatthias Ringwald } else { 1670e7d0c9aaSMatthias Ringwald // Connection refused – LE_PSM not supported 1671e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002); 1672e7d0c9aaSMatthias Ringwald } 1673e7d0c9aaSMatthias Ringwald break; 16741b8b8d05SMatthias Ringwald 16751b8b8d05SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_RESPONSE: 16761b8b8d05SMatthias Ringwald // Find channel for this sig_id and connection handle 16771b8b8d05SMatthias Ringwald channel = NULL; 16781b8b8d05SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 16791b8b8d05SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 16801b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 16811b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 16821b8b8d05SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 16831b8b8d05SMatthias Ringwald channel = a_channel; 16841b8b8d05SMatthias Ringwald break; 16851b8b8d05SMatthias Ringwald } 16861b8b8d05SMatthias Ringwald // TODO: send error 16871b8b8d05SMatthias Ringwald if (!channel) break; 16881b8b8d05SMatthias Ringwald 16891b8b8d05SMatthias Ringwald // cid + 0 16901b8b8d05SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8); 16911b8b8d05SMatthias Ringwald if (result){ 16921b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 16931b8b8d05SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 16941b8b8d05SMatthias Ringwald l2cap_emit_channel_opened(channel, result); 16951b8b8d05SMatthias Ringwald 16961b8b8d05SMatthias Ringwald // discard channel 16971b8b8d05SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 16981b8b8d05SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 16991b8b8d05SMatthias Ringwald break; 17001b8b8d05SMatthias Ringwald } 17011b8b8d05SMatthias Ringwald 17021b8b8d05SMatthias Ringwald // success 17031b8b8d05SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 17041b8b8d05SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 17051b8b8d05SMatthias Ringwald channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4); 17061b8b8d05SMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6); 17071b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 17081b8b8d05SMatthias Ringwald l2cap_emit_channel_opened(channel, result); 17091b8b8d05SMatthias Ringwald break; 17101b8b8d05SMatthias Ringwald 1711828a7f7aSMatthias Ringwald case DISCONNECTION_REQUEST: 1712828a7f7aSMatthias Ringwald // find channel 1713828a7f7aSMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 1714828a7f7aSMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 1715828a7f7aSMatthias Ringwald if (!channel) break; 1716828a7f7aSMatthias Ringwald channel->remote_sig_id = sig_id; 1717828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1718828a7f7aSMatthias Ringwald break; 1719828a7f7aSMatthias Ringwald 1720c48b2a2cSMatthias Ringwald default: 1721c48b2a2cSMatthias Ringwald // command unknown -> reject command 1722c48b2a2cSMatthias Ringwald return 0; 1723ccf076adS[email protected] } 1724c48b2a2cSMatthias Ringwald return 1; 1725c48b2a2cSMatthias Ringwald } 1726e7d0c9aaSMatthias Ringwald #endif 1727c48b2a2cSMatthias Ringwald 1728c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 1729c48b2a2cSMatthias Ringwald 1730c48b2a2cSMatthias Ringwald // Get Channel ID 1731c48b2a2cSMatthias Ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 1732c48b2a2cSMatthias Ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 1733c48b2a2cSMatthias Ringwald 1734c48b2a2cSMatthias Ringwald switch (channel_id) { 1735c48b2a2cSMatthias Ringwald 1736c48b2a2cSMatthias Ringwald case L2CAP_CID_SIGNALING: { 1737c48b2a2cSMatthias Ringwald uint16_t command_offset = 8; 1738c48b2a2cSMatthias Ringwald while (command_offset < size) { 1739c48b2a2cSMatthias Ringwald // handle signaling commands 1740c48b2a2cSMatthias Ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 1741c48b2a2cSMatthias Ringwald 1742c48b2a2cSMatthias Ringwald // increment command_offset 1743c48b2a2cSMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1744c48b2a2cSMatthias Ringwald } 1745c48b2a2cSMatthias Ringwald break; 1746c48b2a2cSMatthias Ringwald } 1747c48b2a2cSMatthias Ringwald 1748e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1749e7d0c9aaSMatthias Ringwald case L2CAP_CID_SIGNALING_LE: { 1750e7d0c9aaSMatthias Ringwald uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 1751e7d0c9aaSMatthias Ringwald int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id); 1752c48b2a2cSMatthias Ringwald if (!valid){ 17531bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1754ccf076adS[email protected] } 1755ccf076adS[email protected] break; 1756e7d0c9aaSMatthias Ringwald } 1757e7d0c9aaSMatthias Ringwald #endif 1758c48b2a2cSMatthias Ringwald 1759c48b2a2cSMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1760c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 1761c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1762ccf076adS[email protected] } 1763c48b2a2cSMatthias Ringwald break; 1764c48b2a2cSMatthias Ringwald 1765c48b2a2cSMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1766c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 1767c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1768c48b2a2cSMatthias Ringwald } 1769c48b2a2cSMatthias Ringwald break; 1770c48b2a2cSMatthias Ringwald 1771c48b2a2cSMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1772c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 1773c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1774c48b2a2cSMatthias Ringwald } 1775c48b2a2cSMatthias Ringwald break; 17761bbc0b23S[email protected] 17775652b5ffS[email protected] default: { 177800d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 177964e11ca9SMatthias Ringwald l2cap_channel_t * l2cap_channel; 178064e11ca9SMatthias Ringwald l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 1781d1fd2a88SMatthias Ringwald if (l2cap_channel) { 17823d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 178300d93d79Smatthias.ringwald } 178464e11ca9SMatthias Ringwald #ifdef ENABLE_BLE 178564e11ca9SMatthias Ringwald l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id); 178664e11ca9SMatthias Ringwald if (l2cap_channel) { 178764e11ca9SMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 178864e11ca9SMatthias Ringwald } 178964e11ca9SMatthias Ringwald #endif 17905652b5ffS[email protected] break; 17915652b5ffS[email protected] } 17925652b5ffS[email protected] } 179300d93d79Smatthias.ringwald 17941eb2563eS[email protected] l2cap_run(); 17952718e2e7Smatthias.ringwald } 179600d93d79Smatthias.ringwald 179715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 179827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 1799f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1800f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1801f62db1e3Smatthias.ringwald // discard channel 18029dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1803665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1804d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1805c8e4258aSmatthias.ringwald } 18061e6aba47Smatthias.ringwald 1807828a7f7aSMatthias Ringwald #ifdef ENABLE_BLE 1808828a7f7aSMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 1809828a7f7aSMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){ 1810828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 1811828a7f7aSMatthias Ringwald l2cap_emit_channel_closed(channel); 1812828a7f7aSMatthias Ringwald // discard channel 1813828a7f7aSMatthias Ringwald l2cap_stop_rtx(channel); 1814828a7f7aSMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1815828a7f7aSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1816828a7f7aSMatthias Ringwald } 1817828a7f7aSMatthias Ringwald #endif 1818828a7f7aSMatthias Ringwald 18198f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1820665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1821665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1822665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1823665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 18249d9bbc01Smatthias.ringwald if ( service->psm == psm){ 18259d9bbc01Smatthias.ringwald return service; 18269d9bbc01Smatthias.ringwald }; 18279d9bbc01Smatthias.ringwald } 18289d9bbc01Smatthias.ringwald return NULL; 18299d9bbc01Smatthias.ringwald } 18309d9bbc01Smatthias.ringwald 18317192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 18327192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 18337192e786SMatthias Ringwald } 18347192e786SMatthias Ringwald 1835e0abb8e7S[email protected] 1836be2053a6SMatthias 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){ 1837be2053a6SMatthias Ringwald 1838be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1839e0abb8e7S[email protected] 18404bb582b6Smatthias.ringwald // check for alread registered psm 18419d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1842277abc2cSmatthias.ringwald if (service) { 1843be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1844be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1845277abc2cSmatthias.ringwald } 18469d9bbc01Smatthias.ringwald 18474bb582b6Smatthias.ringwald // alloc structure 1848bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1849277abc2cSmatthias.ringwald if (!service) { 1850be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1851be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1852277abc2cSmatthias.ringwald } 18539d9bbc01Smatthias.ringwald 18549d9bbc01Smatthias.ringwald // fill in 18559d9bbc01Smatthias.ringwald service->psm = psm; 18569d9bbc01Smatthias.ringwald service->mtu = mtu; 185705ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1858df3354fcS[email protected] service->required_security_level = security_level; 18599d9bbc01Smatthias.ringwald 18609d9bbc01Smatthias.ringwald // add to services list 1861665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1862c0e866bfSmatthias.ringwald 1863c0e866bfSmatthias.ringwald // enable page scan 186415a95bd5SMatthias Ringwald gap_connectable_control(1); 18655842b6d9Smatthias.ringwald 1866be2053a6SMatthias Ringwald return 0; 18679d9bbc01Smatthias.ringwald } 18689d9bbc01Smatthias.ringwald 18697e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){ 1870e0abb8e7S[email protected] 1871e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1872e0abb8e7S[email protected] 18739d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 18747e8856ebSMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 1875665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1876d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1877c0e866bfSmatthias.ringwald 1878c0e866bfSmatthias.ringwald // disable page scan when no services registered 18797e8856ebSMatthias Ringwald if (btstack_linked_list_empty(&l2cap_services)) { 188015a95bd5SMatthias Ringwald gap_connectable_control(0); 18819d9bbc01Smatthias.ringwald } 18827e8856ebSMatthias Ringwald return 0; 18837e8856ebSMatthias Ringwald } 18849d9bbc01Smatthias.ringwald 18855652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 188605ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 18875628cf69SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 18885628cf69SMatthias Ringwald if (index < 0) return; 18895628cf69SMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 18905652b5ffS[email protected] } 18915652b5ffS[email protected] 1892a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 18937192e786SMatthias Ringwald 1894e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){ 1895e7d0c9aaSMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, le_psm); 18967192e786SMatthias Ringwald } 1897efedfb4cSMatthias Ringwald 1898da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){ 18997192e786SMatthias Ringwald 1900da144af5SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm); 19017192e786SMatthias Ringwald 19027192e786SMatthias Ringwald // check for alread registered psm 19037192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 19047192e786SMatthias Ringwald if (service) { 1905da144af5SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 19067192e786SMatthias Ringwald } 19077192e786SMatthias Ringwald 19087192e786SMatthias Ringwald // alloc structure 19097192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 19107192e786SMatthias Ringwald if (!service) { 19117192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 1912da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 19137192e786SMatthias Ringwald } 19147192e786SMatthias Ringwald 19157192e786SMatthias Ringwald // fill in 19167192e786SMatthias Ringwald service->psm = psm; 1917da144af5SMatthias Ringwald service->mtu = 0; 19187192e786SMatthias Ringwald service->packet_handler = packet_handler; 19197192e786SMatthias Ringwald service->required_security_level = security_level; 19207192e786SMatthias Ringwald 19217192e786SMatthias Ringwald // add to services list 1922665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 19237192e786SMatthias Ringwald 19247192e786SMatthias Ringwald // done 1925da144af5SMatthias Ringwald return 0; 19267192e786SMatthias Ringwald } 19277192e786SMatthias Ringwald 1928da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) { 19297192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 19307192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 19319367f9b0SMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 1932da144af5SMatthias Ringwald 1933665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 19347192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 1935da144af5SMatthias Ringwald return 0; 19367192e786SMatthias Ringwald } 1937da144af5SMatthias Ringwald 1938da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){ 1939e7d0c9aaSMatthias Ringwald // get channel 1940e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 1941e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 1942e7d0c9aaSMatthias Ringwald 1943e7d0c9aaSMatthias Ringwald // validate state 1944e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 1945e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1946e7d0c9aaSMatthias Ringwald } 1947e7d0c9aaSMatthias Ringwald 1948efedfb4cSMatthias Ringwald // set state accept connection 194923017473SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT; 195023017473SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 195123017473SMatthias Ringwald channel->local_mtu = mtu; 195223017473SMatthias Ringwald channel->credits_incoming = initial_credits; 1953e7d0c9aaSMatthias Ringwald 1954e7d0c9aaSMatthias Ringwald // go 1955e7d0c9aaSMatthias Ringwald l2cap_run(); 1956da144af5SMatthias Ringwald return 0; 1957da144af5SMatthias Ringwald } 1958da144af5SMatthias Ringwald 1959da144af5SMatthias Ringwald /** 1960da144af5SMatthias Ringwald * @brief Deny incoming LE Data Channel connection due to resource constraints 1961da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1962da144af5SMatthias Ringwald */ 1963da144af5SMatthias Ringwald 1964da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){ 1965e7d0c9aaSMatthias Ringwald // get channel 1966e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 1967e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 1968e7d0c9aaSMatthias Ringwald 1969e7d0c9aaSMatthias Ringwald // validate state 1970e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 1971e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1972e7d0c9aaSMatthias Ringwald } 1973e7d0c9aaSMatthias Ringwald 1974efedfb4cSMatthias Ringwald // set state decline connection 1975e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE; 1976e7d0c9aaSMatthias Ringwald channel->reason = 0x04; // no resources available 1977e7d0c9aaSMatthias Ringwald l2cap_run(); 1978da144af5SMatthias Ringwald return 0; 1979da144af5SMatthias Ringwald } 1980da144af5SMatthias Ringwald 1981da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type, 1982da144af5SMatthias Ringwald uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 1983efedfb4cSMatthias Ringwald uint16_t * out_local_cid) { 1984efedfb4cSMatthias Ringwald 1985da144af5SMatthias Ringwald log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 1986da144af5SMatthias Ringwald 19875cb87675SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, LEVEL_0); 1988da144af5SMatthias Ringwald if (!channel) { 1989da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1990da144af5SMatthias Ringwald } 1991e7d0c9aaSMatthias Ringwald log_info("l2cap_le_create_channel %p", channel); 1992da144af5SMatthias Ringwald 1993da144af5SMatthias Ringwald // store local_cid 1994da144af5SMatthias Ringwald if (out_local_cid){ 1995da144af5SMatthias Ringwald *out_local_cid = channel->local_cid; 1996da144af5SMatthias Ringwald } 1997da144af5SMatthias Ringwald 1998e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 1999e7d0c9aaSMatthias Ringwald 2000efedfb4cSMatthias Ringwald // add to connections list 2001efedfb4cSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 2002efedfb4cSMatthias Ringwald 2003da144af5SMatthias Ringwald // check if hci connection is already usable 2004da144af5SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type); 2005efedfb4cSMatthias Ringwald 2006da144af5SMatthias Ringwald if (conn){ 2007da144af5SMatthias Ringwald log_info("l2cap_le_create_channel, hci connection already exists"); 20085cb87675SMatthias Ringwald l2cap_handle_le_connection_complete(channel, 0, conn->con_handle); 2009efedfb4cSMatthias Ringwald } else { 2010efedfb4cSMatthias Ringwald 2011efedfb4cSMatthias Ringwald // 2012efedfb4cSMatthias Ringwald // TODO: start timer 2013efedfb4cSMatthias Ringwald // .. 2014efedfb4cSMatthias Ringwald 2015efedfb4cSMatthias Ringwald // start connection 2016efedfb4cSMatthias Ringwald uint8_t res = gap_auto_connection_start(address_type, address); 201748af0c56SMatthias Ringwald if (res){ 2018efedfb4cSMatthias Ringwald // discard channel object 2019e7d0c9aaSMatthias Ringwald log_info("gap_auto_connection_start failed! 0x%02x", res); 2020e7d0c9aaSMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 2021efedfb4cSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 2022efedfb4cSMatthias Ringwald return res; 2023da144af5SMatthias Ringwald } 2024efedfb4cSMatthias Ringwald } 2025da144af5SMatthias Ringwald return 0; 2026da144af5SMatthias Ringwald } 2027da144af5SMatthias Ringwald 2028da144af5SMatthias Ringwald /** 2029da144af5SMatthias Ringwald * @brief Provide credtis for LE Data Channel 2030da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2031da144af5SMatthias Ringwald * @param credits Number additional credits for peer 2032da144af5SMatthias Ringwald */ 203364e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){ 2034e7d0c9aaSMatthias Ringwald // get channel 2035efedfb4cSMatthias Ringwald // bail if missing 2036efedfb4cSMatthias Ringwald // check state 2037efedfb4cSMatthias Ringwald // check incoming credits + credits <= 0xffff 2038efedfb4cSMatthias Ringwald // set credits_granted 2039efedfb4cSMatthias Ringwald // l2cap_le_run() 2040da144af5SMatthias Ringwald return 0; 2041da144af5SMatthias Ringwald } 2042da144af5SMatthias Ringwald 2043da144af5SMatthias Ringwald /** 2044da144af5SMatthias Ringwald * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 2045da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2046da144af5SMatthias Ringwald */ 204764e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){ 2048efedfb4cSMatthias Ringwald // check if data can be sent via le at all 2049da144af5SMatthias Ringwald return 0; 2050da144af5SMatthias Ringwald } 2051da144af5SMatthias Ringwald 2052da144af5SMatthias Ringwald /** 2053da144af5SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 2054da144af5SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 2055da144af5SMatthias Ringwald * so packet handler should be ready to handle it 2056da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2057da144af5SMatthias Ringwald */ 205864e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){ 2059efedfb4cSMatthias Ringwald // same as for non-le 2060da144af5SMatthias Ringwald return 0; 2061da144af5SMatthias Ringwald } 2062da144af5SMatthias Ringwald 2063da144af5SMatthias Ringwald /** 2064da144af5SMatthias Ringwald * @brief Send data via LE Data Channel 2065da144af5SMatthias Ringwald * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 2066da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2067da144af5SMatthias Ringwald * @param data data to send 2068da144af5SMatthias Ringwald * @param size data size 2069da144af5SMatthias Ringwald */ 207064e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){ 207164e11ca9SMatthias Ringwald 207264e11ca9SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 207364e11ca9SMatthias Ringwald if (!channel) { 207464e11ca9SMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 2075828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 207664e11ca9SMatthias Ringwald } 207764e11ca9SMatthias Ringwald 207864e11ca9SMatthias Ringwald if (len > channel->remote_mtu){ 207964e11ca9SMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 208064e11ca9SMatthias Ringwald return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 208164e11ca9SMatthias Ringwald } 208264e11ca9SMatthias Ringwald 2083*7f107edaSMatthias Ringwald if (channel->send_sdu_buffer){ 208464e11ca9SMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 208564e11ca9SMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 208664e11ca9SMatthias Ringwald } 208764e11ca9SMatthias Ringwald 2088*7f107edaSMatthias Ringwald channel->send_sdu_buffer = data; 2089*7f107edaSMatthias Ringwald channel->send_sdu_len = len; 2090*7f107edaSMatthias Ringwald channel->send_sdu_pos = 0; 209164e11ca9SMatthias Ringwald 2092*7f107edaSMatthias Ringwald l2cap_run(); 2093*7f107edaSMatthias Ringwald return 0; 2094da144af5SMatthias Ringwald } 2095da144af5SMatthias Ringwald 2096da144af5SMatthias Ringwald /** 2097da144af5SMatthias Ringwald * @brief Disconnect from LE Data Channel 2098da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2099da144af5SMatthias Ringwald */ 2100828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid) 2101da144af5SMatthias Ringwald { 2102828a7f7aSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2103828a7f7aSMatthias Ringwald if (!channel) { 2104828a7f7aSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 2105828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2106828a7f7aSMatthias Ringwald } 2107828a7f7aSMatthias Ringwald 2108828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 2109828a7f7aSMatthias Ringwald l2cap_run(); 2110da144af5SMatthias Ringwald return 0; 2111da144af5SMatthias Ringwald } 2112da144af5SMatthias Ringwald 21137f02f414SMatthias Ringwald #endif 2114