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 88e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm); 89e7d0c9aaSMatthias Ringwald #endif 9033c40538SMatthias Ringwald 915628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel { 925628cf69SMatthias Ringwald btstack_packet_handler_t callback; 932125de09SMatthias Ringwald uint8_t waiting_for_can_send_now; 945628cf69SMatthias Ringwald } l2cap_fixed_channel_t; 955628cf69SMatthias Ringwald 965628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels; 975628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services; 985628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels; 995628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services; 10039bda6d5Smatthias.ringwald 10139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 1022b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 1032b83fb7dSmatthias.ringwald static int signaling_responses_pending; 1042b83fb7dSmatthias.ringwald 10533c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp; 10633c40538SMatthias Ringwald 107fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 108fb37a842SMatthias Ringwald 10933c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler; 1105628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE]; 11139bda6d5Smatthias.ringwald 11234e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){ 11334e7e577SMatthias Ringwald switch (index){ 11434e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL: 11534e7e577SMatthias Ringwald return L2CAP_CID_ATTRIBUTE_PROTOCOL; 11634e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL: 11734e7e577SMatthias Ringwald return L2CAP_CID_SECURITY_MANAGER_PROTOCOL; 11834e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL: 11934e7e577SMatthias Ringwald return L2CAP_CID_CONNECTIONLESS_CHANNEL; 12034e7e577SMatthias Ringwald default: 12134e7e577SMatthias Ringwald return 0; 12234e7e577SMatthias Ringwald } 12334e7e577SMatthias Ringwald } 1245628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){ 1255628cf69SMatthias Ringwald switch (channel_id){ 1265628cf69SMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1275628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL; 1285628cf69SMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1295628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL; 1305628cf69SMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1315628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL; 1325628cf69SMatthias Ringwald default: 1335628cf69SMatthias Ringwald return -1; 1345628cf69SMatthias Ringwald } 1355628cf69SMatthias Ringwald } 13639bda6d5Smatthias.ringwald 13734e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){ 13834e7e577SMatthias Ringwald if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0; 13934e7e577SMatthias Ringwald return 1; 14034e7e577SMatthias Ringwald } 14134e7e577SMatthias Ringwald 14271de195eSMatthias Ringwald void l2cap_init(void){ 1432b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 144808a48abSmatthias.ringwald 145f5454fc6Smatthias.ringwald l2cap_channels = NULL; 146f5454fc6Smatthias.ringwald l2cap_services = NULL; 1477192e786SMatthias Ringwald l2cap_le_services = NULL; 1487192e786SMatthias Ringwald l2cap_le_channels = NULL; 149f5454fc6Smatthias.ringwald 15033c40538SMatthias Ringwald l2cap_event_packet_handler = NULL; 1515628cf69SMatthias Ringwald memset(fixed_channels, 0, sizeof(fixed_channels)); 152f5454fc6Smatthias.ringwald 153ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 154ac301f95S[email protected] 155fcadd0caSmatthias.ringwald // 1562718e2e7Smatthias.ringwald // register callback with HCI 157fcadd0caSmatthias.ringwald // 158d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &l2cap_hci_event_handler; 159fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 160fb37a842SMatthias Ringwald 161d9a7306aSMatthias Ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 162fb37a842SMatthias Ringwald 16315a95bd5SMatthias Ringwald gap_connectable_control(0); // no services yet 164fcadd0caSmatthias.ringwald } 165fcadd0caSmatthias.ringwald 166ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 16733c40538SMatthias Ringwald l2cap_event_packet_handler = handler; 1681e6aba47Smatthias.ringwald } 1691e6aba47Smatthias.ringwald 17017a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 17158de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 17258de5610Smatthias.ringwald } 17358de5610Smatthias.ringwald 17458de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 175c9dc710bS[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", 176fc64f94aSMatthias Ringwald status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 177c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 178c9dc710bS[email protected] uint8_t event[23]; 17958de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 18058de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 18158de5610Smatthias.ringwald event[2] = status; 182724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 183fc64f94aSMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 184f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 185f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 186f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 187f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 188f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 189f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 19058de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19117a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 19258de5610Smatthias.ringwald } 19358de5610Smatthias.ringwald 19458de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 195e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 19658de5610Smatthias.ringwald uint8_t event[4]; 19758de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 19858de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 199f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 20058de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 20117a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 20258de5610Smatthias.ringwald } 20358de5610Smatthias.ringwald 20458de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 205e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 206fc64f94aSMatthias Ringwald bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid); 20758de5610Smatthias.ringwald uint8_t event[16]; 20858de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 20958de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 210724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 211fc64f94aSMatthias Ringwald little_endian_store_16(event, 8, channel->con_handle); 212f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 213f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 214f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 21558de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21617a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 2170af41d30Smatthias.ringwald } 218808a48abSmatthias.ringwald 21934e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) { 22034e7e577SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel); 22133c40538SMatthias Ringwald uint8_t event[4]; 22233c40538SMatthias Ringwald event[0] = L2CAP_EVENT_CAN_SEND_NOW; 22333c40538SMatthias Ringwald event[1] = sizeof(event) - 2; 22434e7e577SMatthias Ringwald little_endian_store_16(event, 2, channel); 22533c40538SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 22634e7e577SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); 22733c40538SMatthias Ringwald } 22833c40538SMatthias Ringwald 229fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){ 230ccf076adS[email protected] uint8_t event[6]; 231ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 232ccf076adS[email protected] event[1] = 4; 233fc64f94aSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 234f8fbdce0SMatthias Ringwald little_endian_store_16(event, 4, result); 235ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23633c40538SMatthias Ringwald if (!l2cap_event_packet_handler) return; 23733c40538SMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 238ccf076adS[email protected] } 239ccf076adS[email protected] 2407f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 241665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 242665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 243665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 244665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 245b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 246f62db1e3Smatthias.ringwald return channel; 247f62db1e3Smatthias.ringwald } 248f62db1e3Smatthias.ringwald } 249f62db1e3Smatthias.ringwald return NULL; 250f62db1e3Smatthias.ringwald } 251f62db1e3Smatthias.ringwald 252e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 253e7d0c9aaSMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){ 254e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_t it; 255e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 256e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 257e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 258e7d0c9aaSMatthias Ringwald if ( channel->local_cid == local_cid) { 259e7d0c9aaSMatthias Ringwald return channel; 260e7d0c9aaSMatthias Ringwald } 261e7d0c9aaSMatthias Ringwald } 262e7d0c9aaSMatthias Ringwald return NULL; 263e7d0c9aaSMatthias Ringwald } 264e7d0c9aaSMatthias Ringwald #endif 265e7d0c9aaSMatthias Ringwald 2665cb87675SMatthias Ringwald static l2cap_channel_t * l2cap_get_le_channel_for_address_and_addr_type(bd_addr_t address, bd_addr_type_t address_type){ 2675cb87675SMatthias Ringwald btstack_linked_list_iterator_t it; 2685cb87675SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 2695cb87675SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2705cb87675SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2715cb87675SMatthias Ringwald if (channel->address_type != address_type) continue; 2725cb87675SMatthias Ringwald if (bd_addr_cmp(channel->address, address)) continue; 2735cb87675SMatthias Ringwald return channel; 2745cb87675SMatthias Ringwald } 2755cb87675SMatthias Ringwald return NULL; 2765cb87675SMatthias Ringwald } 2775cb87675SMatthias Ringwald 2780b9d7e78SMatthias Ringwald /// 2790b9d7e78SMatthias Ringwald 28030725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){ 28130725612SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 28230725612SMatthias Ringwald if (!channel) return; 28330725612SMatthias Ringwald channel->waiting_for_can_send_now = 1; 28430725612SMatthias Ringwald l2cap_notify_channel_can_send(); 28530725612SMatthias Ringwald } 28630725612SMatthias Ringwald 2870b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){ 2880b9d7e78SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 2890b9d7e78SMatthias Ringwald if (index < 0) return; 2900b9d7e78SMatthias Ringwald fixed_channels[index].waiting_for_can_send_now = 1; 2910b9d7e78SMatthias Ringwald l2cap_notify_channel_can_send(); 2920b9d7e78SMatthias Ringwald } 2930b9d7e78SMatthias Ringwald 2940b9d7e78SMatthias Ringwald /// 2950b9d7e78SMatthias Ringwald 2966b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2976b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2986b1fde37Smatthias.ringwald if (!channel) return 0; 2990b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(channel->con_handle); 3007856fb31S[email protected] } 3017856fb31S[email protected] 30283e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 30383e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 30483e7cdd9SMatthias Ringwald if (!channel) return 0; 3050b9d7e78SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->con_handle); 30683e7cdd9SMatthias Ringwald } 30783e7cdd9SMatthias Ringwald 308fc64f94aSMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){ 3090b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(con_handle); 3102125de09SMatthias Ringwald } 3110b9d7e78SMatthias Ringwald 3120b9d7e78SMatthias Ringwald /// 3133cab4fcaS[email protected] 31496cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 31596cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 31696cbd662Smatthias.ringwald if (channel) { 31796cbd662Smatthias.ringwald return channel->remote_mtu; 31896cbd662Smatthias.ringwald } 31996cbd662Smatthias.ringwald return 0; 32096cbd662Smatthias.ringwald } 32196cbd662Smatthias.ringwald 322ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 323665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 324665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 325665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 326665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 3275932bd7cS[email protected] if ( &channel->rtx == ts) { 3285932bd7cS[email protected] return channel; 3295932bd7cS[email protected] } 3305932bd7cS[email protected] } 3315932bd7cS[email protected] return NULL; 3325932bd7cS[email protected] } 3335932bd7cS[email protected] 334ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 3355932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 33695d2c8f4SMatthias Ringwald if (!channel) return; 3375932bd7cS[email protected] 3385932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 3395932bd7cS[email protected] 3405932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 3415932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 3425932bd7cS[email protected] // notify client 3435932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 3445932bd7cS[email protected] 3455932bd7cS[email protected] // discard channel 3469dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 347665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 3485932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 3495932bd7cS[email protected] } 3505932bd7cS[email protected] 3515932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 3525932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 353528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 3545932bd7cS[email protected] } 3555932bd7cS[email protected] 3565932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 3575932bd7cS[email protected] l2cap_stop_rtx(channel); 358cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 359528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 360528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 361528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3625932bd7cS[email protected] } 3635932bd7cS[email protected] 3645932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3655932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3665932bd7cS[email protected] l2cap_stop_rtx(channel); 367528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 368528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 369528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3705932bd7cS[email protected] } 3715932bd7cS[email protected] 37271de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 373ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 374ac301f95S[email protected] } 375ac301f95S[email protected] 376df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 377ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 378df3354fcS[email protected] } 3795932bd7cS[email protected] 3807f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 381b1d43497Smatthias.ringwald 382a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3839da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 384b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 385b1d43497Smatthias.ringwald } 386b1d43497Smatthias.ringwald 3879da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3882a373862S[email protected] hci_reserve_packet_buffer(); 389facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 39058de5610Smatthias.ringwald va_list argptr; 39158de5610Smatthias.ringwald va_start(argptr, identifier); 39270efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 39358de5610Smatthias.ringwald va_end(argptr); 3949da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 395826f7347S[email protected] return hci_send_acl_packet_buffer(len); 39658de5610Smatthias.ringwald } 39758de5610Smatthias.ringwald 398a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3997f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 40070efece1S[email protected] 401a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4029da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 40370efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 40470efece1S[email protected] } 40570efece1S[email protected] 4069da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 4072a373862S[email protected] hci_reserve_packet_buffer(); 408facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 40970efece1S[email protected] va_list argptr; 41070efece1S[email protected] va_start(argptr, identifier); 41170efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 41270efece1S[email protected] va_end(argptr); 4139da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 414826f7347S[email protected] return hci_send_acl_packet_buffer(len); 41570efece1S[email protected] } 41670efece1S[email protected] #endif 41770efece1S[email protected] 418b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 419facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 420b1d43497Smatthias.ringwald } 4216218e6f1Smatthias.ringwald 4226b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 4236b4af23dS[email protected] return hci_reserve_packet_buffer(); 4246b4af23dS[email protected] } 4256b4af23dS[email protected] 42668a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 42768a0fcf7S[email protected] hci_release_packet_buffer(); 42868a0fcf7S[email protected] } 42968a0fcf7S[email protected] 43068a0fcf7S[email protected] 431b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 432b1d43497Smatthias.ringwald 433c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 434c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 435c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 436c8b9416aS[email protected] } 437c8b9416aS[email protected] 43858de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 439b1d43497Smatthias.ringwald if (!channel) { 4409da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 441b1d43497Smatthias.ringwald return -1; // TODO: define error 4426218e6f1Smatthias.ringwald } 4436218e6f1Smatthias.ringwald 444fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){ 4459da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 446a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 447a35252c8S[email protected] } 448a35252c8S[email protected] 449fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle); 450b1d43497Smatthias.ringwald 451facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 452b1d43497Smatthias.ringwald 453e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 454e9772277S[email protected] 455e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 456fc64f94aSMatthias Ringwald little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14)); 45758de5610Smatthias.ringwald // 2 - ACL length 458f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 45958de5610Smatthias.ringwald // 4 - L2CAP packet length 460f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 46158de5610Smatthias.ringwald // 6 - L2CAP channel DEST 462f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, channel->remote_cid); 46358de5610Smatthias.ringwald // send 464826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 46591b99603Smatthias.ringwald 4666218e6f1Smatthias.ringwald return err; 46758de5610Smatthias.ringwald } 46858de5610Smatthias.ringwald 469fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){ 4702149f12eSmatthias.ringwald 471c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 472c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4732149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4742149f12eSmatthias.ringwald } 4752149f12eSmatthias.ringwald 476fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(con_handle)){ 477fc64f94aSMatthias Ringwald log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid); 478c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 479c8b9416aS[email protected] } 480c8b9416aS[email protected] 481fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid); 4822149f12eSmatthias.ringwald 483facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4842149f12eSmatthias.ringwald 485e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 486e9772277S[email protected] 487e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 488fc64f94aSMatthias Ringwald little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14)); 4892149f12eSmatthias.ringwald // 2 - ACL length 490f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 4912149f12eSmatthias.ringwald // 4 - L2CAP packet length 492f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 4932149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 494f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, cid); 4952149f12eSmatthias.ringwald // send 496826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4972149f12eSmatthias.ringwald 4982149f12eSmatthias.ringwald return err; 4992149f12eSmatthias.ringwald } 5002149f12eSmatthias.ringwald 501ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 502b1d43497Smatthias.ringwald 503a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 504a35252c8S[email protected] if (!channel) { 505ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 506a35252c8S[email protected] return -1; // TODO: define error 507a35252c8S[email protected] } 508a35252c8S[email protected] 509f0efaa57S[email protected] if (len > channel->remote_mtu){ 510ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 511f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 512f0efaa57S[email protected] } 513f0efaa57S[email protected] 514fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)){ 515ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 516b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 517b1d43497Smatthias.ringwald } 518b1d43497Smatthias.ringwald 5192a373862S[email protected] hci_reserve_packet_buffer(); 520facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 521b1d43497Smatthias.ringwald 522b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 523b1d43497Smatthias.ringwald 524b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 525b1d43497Smatthias.ringwald } 526b1d43497Smatthias.ringwald 527fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){ 5282149f12eSmatthias.ringwald 529fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(con_handle)){ 530ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 5312149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 5322149f12eSmatthias.ringwald } 5332149f12eSmatthias.ringwald 5342a373862S[email protected] hci_reserve_packet_buffer(); 535facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 5362149f12eSmatthias.ringwald 5372149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 5382149f12eSmatthias.ringwald 539fc64f94aSMatthias Ringwald return l2cap_send_prepared_connectionless(con_handle, cid, len); 5402149f12eSmatthias.ringwald } 5412149f12eSmatthias.ringwald 542fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){ 543fc64f94aSMatthias Ringwald return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data); 5440e37e417S[email protected] } 5450e37e417S[email protected] 54628ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 54728ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 54828ca2b46S[email protected] } 54928ca2b46S[email protected] 55028ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 55128ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 55228ca2b46S[email protected] } 55328ca2b46S[email protected] 55428ca2b46S[email protected] 555b1d43497Smatthias.ringwald 5568158c421Smatthias.ringwald // MARK: L2CAP_RUN 5572cd0be45Smatthias.ringwald // process outstanding signaling tasks 5587f02f414SMatthias Ringwald static void l2cap_run(void){ 5592b83fb7dSmatthias.ringwald 56022c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 56122c29ab4SMatthias Ringwald 5622b83fb7dSmatthias.ringwald // check pending signaling responses 5632b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5642b83fb7dSmatthias.ringwald 5652b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 566a35252c8S[email protected] 567a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 568a35252c8S[email protected] 5692b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5702b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 57163a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 572f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5732b83fb7dSmatthias.ringwald 574f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 575f53da564S[email protected] signaling_responses_pending--; 576f53da564S[email protected] int i; 577f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 578f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 579f53da564S[email protected] } 580f53da564S[email protected] 581f53da564S[email protected] switch (response_code){ 5822b360848Smatthias.ringwald case CONNECTION_REQUEST: 5832b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5842bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5854d816277S[email protected] if (result == 0x0003){ 5862bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5874d816277S[email protected] } 5882b360848Smatthias.ringwald break; 5892b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5902b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5912b83fb7dSmatthias.ringwald break; 5922b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5933b0484b3S[email protected] switch (infoType){ 5943b0484b3S[email protected] case 1: { // Connectionless MTU 5953b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5963b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5973b0484b3S[email protected] break; 5983b0484b3S[email protected] } 5993b0484b3S[email protected] case 2: { // Extended Features Supported 600462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 601462e630dS[email protected] uint32_t features = 0x280; 6023b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 6033b0484b3S[email protected] break; 6043b0484b3S[email protected] } 6053b0484b3S[email protected] case 3: { // Fixed Channels Supported 6063b0484b3S[email protected] uint8_t map[8]; 6073b0484b3S[email protected] memset(map, 0, 8); 608288636a2SMatthias Ringwald map[0] = 0x06; // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04) 6093b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 6103b0484b3S[email protected] break; 6113b0484b3S[email protected] } 6123b0484b3S[email protected] default: 6132b83fb7dSmatthias.ringwald // all other types are not supported 6142b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 6153b0484b3S[email protected] break; 6162b83fb7dSmatthias.ringwald } 6172b83fb7dSmatthias.ringwald break; 61863a7246aSmatthias.ringwald case COMMAND_REJECT: 6195ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 620a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 62170efece1S[email protected] case COMMAND_REJECT_LE: 62270efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 62363a7246aSmatthias.ringwald break; 62470efece1S[email protected] #endif 6252b83fb7dSmatthias.ringwald default: 6262b83fb7dSmatthias.ringwald // should not happen 6272b83fb7dSmatthias.ringwald break; 6282b83fb7dSmatthias.ringwald } 6292b83fb7dSmatthias.ringwald } 6302b83fb7dSmatthias.ringwald 631ae280e73Smatthias.ringwald uint8_t config_options[4]; 632665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 633665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 634665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 635baf94f06S[email protected] 636665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 63722c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 6382cd0be45Smatthias.ringwald switch (channel->state){ 6392cd0be45Smatthias.ringwald 640df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 641ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 642fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 643a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 644ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 645fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 646ad671560S[email protected] } 647ad671560S[email protected] break; 648ad671560S[email protected] 64902b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 650baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 65164472d52Smatthias.ringwald // send connection request - set state first 65264472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 65302b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6548f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 65502b22dc4Smatthias.ringwald break; 65602b22dc4Smatthias.ringwald 657e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 658fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 65922c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 660fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 661e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6629dcb2fb2S[email protected] l2cap_stop_rtx(channel); 663665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 664d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 665e7ff783cSmatthias.ringwald break; 666e7ff783cSmatthias.ringwald 667552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 668fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 669fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 67028ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 671fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 672552d92a1Smatthias.ringwald break; 673552d92a1Smatthias.ringwald 6746fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 675fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 6766fdcc387Smatthias.ringwald // success, start l2cap handshake 677b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6786fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 679fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6805932bd7cS[email protected] l2cap_start_rtx(channel); 6816fdcc387Smatthias.ringwald break; 6826fdcc387Smatthias.ringwald 683fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 684fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 68573cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 68663a7246aSmatthias.ringwald uint16_t flags = 0; 68728ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 68863a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 68963a7246aSmatthias.ringwald flags = 1; 69063a7246aSmatthias.ringwald } else { 69128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 69263a7246aSmatthias.ringwald } 69363a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 694fc64f94aSMatthias 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); 69563a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 69663a7246aSmatthias.ringwald config_options[0] = 1; // MTU 69763a7246aSmatthias.ringwald config_options[1] = 2; // len param 698f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 699fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 70063a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 70163a7246aSmatthias.ringwald } else { 702fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 70363a7246aSmatthias.ringwald } 70463a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 705fa8473a4Smatthias.ringwald } 70673cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 70728ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 70828ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 709b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 710ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 711ae280e73Smatthias.ringwald config_options[1] = 2; // len param 712f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 713fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 7145932bd7cS[email protected] l2cap_start_rtx(channel); 715fa8473a4Smatthias.ringwald } 716fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 717552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 718552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 719fa8473a4Smatthias.ringwald } 720552d92a1Smatthias.ringwald break; 721552d92a1Smatthias.ringwald 722e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 723fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 72422c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 725fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 7265932bd7cS[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 :) 727756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 728e7ff783cSmatthias.ringwald break; 729e7ff783cSmatthias.ringwald 730e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 731fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 732b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 7332cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 734fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 7352cd0be45Smatthias.ringwald break; 7362cd0be45Smatthias.ringwald default: 7372cd0be45Smatthias.ringwald break; 7382cd0be45Smatthias.ringwald } 7392cd0be45Smatthias.ringwald } 740da886c03S[email protected] 741a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 742efedfb4cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 743efedfb4cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 744efedfb4cSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 745efedfb4cSMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 746efedfb4cSMatthias Ringwald switch (channel->state){ 7475cb87675SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST: 7485cb87675SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 7495cb87675SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE; 7505cb87675SMatthias Ringwald // le psm, source cid, mtu, mps, initial credits 7511b8b8d05SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 7525cb87675SMatthias 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); 7535cb87675SMatthias Ringwald break; 754*23017473SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT: 755*23017473SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 756*23017473SMatthias Ringwald // TODO: support larger MPS 757*23017473SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 758*23017473SMatthias 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); 759*23017473SMatthias Ringwald break; 760e7d0c9aaSMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE: 761e7d0c9aaSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 762e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 763e7d0c9aaSMatthias 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); 764e7d0c9aaSMatthias Ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 765e7d0c9aaSMatthias Ringwald l2cap_stop_rtx(channel); 766e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 767e7d0c9aaSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 768e7d0c9aaSMatthias Ringwald break; 769efedfb4cSMatthias Ringwald default: 770efedfb4cSMatthias Ringwald break; 771efedfb4cSMatthias Ringwald } 772efedfb4cSMatthias Ringwald } 773efedfb4cSMatthias Ringwald 774da886c03S[email protected] // send l2cap con paramter update if necessary 775da886c03S[email protected] hci_connections_get_iterator(&it); 776665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 777665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 7785d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 779b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 780da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 781b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 782b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 783b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 784b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 785b68d7bc3SMatthias Ringwald break; 786da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 787b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 788b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 789da886c03S[email protected] break; 790da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 791b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 792b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 793da886c03S[email protected] break; 794da886c03S[email protected] default: 795da886c03S[email protected] break; 796da886c03S[email protected] } 797efedfb4cSMatthias Ringwald #if 0 798efedfb4cSMatthias Ringwald void l2cap_le_run(void){ 799efedfb4cSMatthias Ringwald for all channels 800efedfb4cSMatthias Ringwald switch(state): 801efedfb4cSMatthias Ringwald 802efedfb4cSMatthias Ringwald if outgoing transfer active 803efedfb4cSMatthias Ringwald send next chunk 804efedfb4cSMatthias Ringwald if done, notify app 805efedfb4cSMatthias Ringwald } 806efedfb4cSMatthias Ringwald #endif 807da886c03S[email protected] } 8084d7157c3S[email protected] #endif 809da886c03S[email protected] 81022c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 8112cd0be45Smatthias.ringwald } 8122cd0be45Smatthias.ringwald 8134aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 8144ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 815fa8c92f6Smatthias.ringwald } 816fa8c92f6Smatthias.ringwald 81771de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 8184ff786cfS[email protected] return l2cap_max_mtu(); 819e5e1518dS[email protected] } 820e5e1518dS[email protected] 821fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){ 8222df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 8235533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 8242df5dadcS[email protected] // success, start l2cap handshake 825fc64f94aSMatthias Ringwald channel->con_handle = con_handle; 8262df5dadcS[email protected] // check remote SSP feature first 8272df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 8282df5dadcS[email protected] } 8292df5dadcS[email protected] } 8302df5dadcS[email protected] 831efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE 8325cb87675SMatthias Ringwald static void l2cap_handle_le_connection_complete(l2cap_channel_t * channel, uint8_t status, hci_con_handle_t con_handle){ 833efedfb4cSMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 834efedfb4cSMatthias Ringwald log_info("l2cap_le_handle_connection_complete expected state"); 8355cb87675SMatthias Ringwald // TODO: bail if status != 0 8365cb87675SMatthias Ringwald 837efedfb4cSMatthias Ringwald // success, start l2cap handshake 838efedfb4cSMatthias Ringwald channel->con_handle = con_handle; 8395cb87675SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST; 840efedfb4cSMatthias Ringwald } 841efedfb4cSMatthias Ringwald } 842efedfb4cSMatthias Ringwald #endif 843efedfb4cSMatthias Ringwald 844efedfb4cSMatthias Ringwald 8452df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 8462df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 8472df5dadcS[email protected] 8482df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 849ac301f95S[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)); 850fc64f94aSMatthias Ringwald if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 8512df5dadcS[email protected] // request security level 2 8522df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 853fc64f94aSMatthias Ringwald gap_request_security_level(channel->con_handle, LEVEL_2); 8542df5dadcS[email protected] return; 8552df5dadcS[email protected] } 8562df5dadcS[email protected] // fine, go ahead 8572df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 8582df5dadcS[email protected] } 8592df5dadcS[email protected] 860da144af5SMatthias 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, 861da144af5SMatthias Ringwald uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){ 862da144af5SMatthias Ringwald 863da144af5SMatthias Ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 864da144af5SMatthias Ringwald if (!channel) { 865da144af5SMatthias Ringwald return NULL; 866da144af5SMatthias Ringwald } 867da144af5SMatthias Ringwald 868da144af5SMatthias Ringwald // Init memory (make valgrind happy) 869da144af5SMatthias Ringwald memset(channel, 0, sizeof(l2cap_channel_t)); 870da144af5SMatthias Ringwald 871da144af5SMatthias Ringwald // fill in 872da144af5SMatthias Ringwald channel->packet_handler = packet_handler; 873da144af5SMatthias Ringwald bd_addr_copy(channel->address, address); 874da144af5SMatthias Ringwald channel->address_type = address_type; 875da144af5SMatthias Ringwald channel->psm = psm; 876da144af5SMatthias Ringwald channel->local_mtu = local_mtu; 877da144af5SMatthias Ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 878da144af5SMatthias Ringwald channel->required_security_level = security_level; 879da144af5SMatthias Ringwald 880da144af5SMatthias Ringwald // 881da144af5SMatthias Ringwald channel->local_cid = l2cap_next_local_cid(); 882da144af5SMatthias Ringwald channel->con_handle = 0; 883da144af5SMatthias Ringwald 884da144af5SMatthias Ringwald // set initial state 885da144af5SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 886da144af5SMatthias Ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 887da144af5SMatthias Ringwald channel->remote_sig_id = L2CAP_SIG_ID_INVALID; 888da144af5SMatthias Ringwald channel->local_sig_id = L2CAP_SIG_ID_INVALID; 889da144af5SMatthias Ringwald return channel; 890da144af5SMatthias Ringwald } 891da144af5SMatthias Ringwald 8929077cb15SMatthias Ringwald /** 8939077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 8949077cb15SMatthias Ringwald * @param packet_handler 8959077cb15SMatthias Ringwald * @param address 8969077cb15SMatthias Ringwald * @param psm 8979077cb15SMatthias Ringwald * @param mtu 8989077cb15SMatthias Ringwald * @param local_cid 8999077cb15SMatthias Ringwald */ 9009077cb15SMatthias Ringwald 901da144af5SMatthias 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){ 902da144af5SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu); 903da144af5SMatthias Ringwald 904da144af5SMatthias Ringwald if (local_mtu > l2cap_max_mtu()) { 905da144af5SMatthias Ringwald local_mtu = l2cap_max_mtu(); 906da144af5SMatthias Ringwald } 907da144af5SMatthias Ringwald 908da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0); 909fc64f94aSMatthias Ringwald if (!channel) { 9109077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 9119077cb15SMatthias Ringwald } 9129077cb15SMatthias Ringwald 9139077cb15SMatthias Ringwald // add to connections list 914fc64f94aSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 9159077cb15SMatthias Ringwald 9169077cb15SMatthias Ringwald // store local_cid 9179077cb15SMatthias Ringwald if (out_local_cid){ 918fc64f94aSMatthias Ringwald *out_local_cid = channel->local_cid; 9199077cb15SMatthias Ringwald } 9209077cb15SMatthias Ringwald 9219077cb15SMatthias Ringwald // check if hci connection is already usable 9229077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 9239077cb15SMatthias Ringwald if (conn){ 924add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 925fc64f94aSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 9269077cb15SMatthias Ringwald // check if remote supported fearures are already received 9279077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 928fc64f94aSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 9299077cb15SMatthias Ringwald } 9309077cb15SMatthias Ringwald } 9319077cb15SMatthias Ringwald 9329077cb15SMatthias Ringwald l2cap_run(); 9339077cb15SMatthias Ringwald 9349077cb15SMatthias Ringwald return 0; 9359077cb15SMatthias Ringwald } 9369077cb15SMatthias Ringwald 937ce8f182eSMatthias Ringwald void 938ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 939e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 940b35f641cSmatthias.ringwald // find channel for local_cid 941b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 942f62db1e3Smatthias.ringwald if (channel) { 943e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 944f62db1e3Smatthias.ringwald } 9452cd0be45Smatthias.ringwald // process 9462cd0be45Smatthias.ringwald l2cap_run(); 94743625864Smatthias.ringwald } 9481e6aba47Smatthias.ringwald 949afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 950665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 951665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 952665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 953665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 954058e3d6bSMatthias Ringwald if ( bd_addr_cmp( channel->address, address) != 0) continue; 955c22aecc9S[email protected] // channel for this address found 956c22aecc9S[email protected] switch (channel->state){ 957c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 958c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 959afde0c52Smatthias.ringwald // failure, forward error code 960afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 961afde0c52Smatthias.ringwald // discard channel 9629dcb2fb2S[email protected] l2cap_stop_rtx(channel); 963665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 964d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 965c22aecc9S[email protected] break; 966c22aecc9S[email protected] default: 967c22aecc9S[email protected] break; 968afde0c52Smatthias.ringwald } 969afde0c52Smatthias.ringwald } 970afde0c52Smatthias.ringwald } 971afde0c52Smatthias.ringwald 972afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 973665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 974665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 975665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 976665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 977058e3d6bSMatthias Ringwald if ( ! bd_addr_cmp( channel->address, address) ){ 9782df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 979afde0c52Smatthias.ringwald } 980afde0c52Smatthias.ringwald } 9816fdcc387Smatthias.ringwald // process 9826fdcc387Smatthias.ringwald l2cap_run(); 983afde0c52Smatthias.ringwald } 984b448a0e7Smatthias.ringwald 98533c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){ 98633c40538SMatthias Ringwald btstack_linked_list_iterator_t it; 98733c40538SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 98833c40538SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 98933c40538SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 99033c40538SMatthias Ringwald if (!channel->waiting_for_can_send_now) continue; 991fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 99233c40538SMatthias Ringwald channel->waiting_for_can_send_now = 0; 99334e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 99433c40538SMatthias Ringwald } 9952125de09SMatthias Ringwald 9962125de09SMatthias Ringwald int i; 9972125de09SMatthias Ringwald for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){ 998773c4106SMatthias Ringwald if (!fixed_channels[i].callback) continue; 9992125de09SMatthias Ringwald if (!fixed_channels[i].waiting_for_can_send_now) continue; 100034e7e577SMatthias Ringwald int can_send; 100134e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 100234e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 100334e7e577SMatthias Ringwald } else { 100434e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 100534e7e577SMatthias Ringwald } 100634e7e577SMatthias Ringwald if (!can_send) continue; 100734e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 100834e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 10092125de09SMatthias Ringwald } 101033c40538SMatthias Ringwald } 101133c40538SMatthias Ringwald 1012d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 1013afde0c52Smatthias.ringwald 1014afde0c52Smatthias.ringwald bd_addr_t address; 1015afde0c52Smatthias.ringwald hci_con_handle_t handle; 1016665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 10172d00edd4Smatthias.ringwald int hci_con_used; 1018afde0c52Smatthias.ringwald 10190e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1020afde0c52Smatthias.ringwald 1021afde0c52Smatthias.ringwald // handle connection complete events 1022afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1023724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 1024afde0c52Smatthias.ringwald if (packet[2] == 0){ 1025f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1026afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 1027afde0c52Smatthias.ringwald } else { 1028afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 1029afde0c52Smatthias.ringwald } 1030afde0c52Smatthias.ringwald break; 1031afde0c52Smatthias.ringwald 1032afde0c52Smatthias.ringwald // handle successful create connection cancel command 1033afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 1034073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) { 1035afde0c52Smatthias.ringwald if (packet[5] == 0){ 1036724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 1037afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 1038afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 103903cfbabcSmatthias.ringwald } 10401e6aba47Smatthias.ringwald } 104139d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 104239d59809Smatthias.ringwald break; 104339d59809Smatthias.ringwald 104439d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 104539d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 1046afde0c52Smatthias.ringwald break; 104727a923d0Smatthias.ringwald 10481e6aba47Smatthias.ringwald // handle disconnection complete events 1049afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1050c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 1051f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1052665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1053665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1054665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1055fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 105615ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 10579dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1058665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1059d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 106027a923d0Smatthias.ringwald } 1061afde0c52Smatthias.ringwald break; 1062fcadd0caSmatthias.ringwald 106333c40538SMatthias Ringwald // Notify channel packet handler if they can send now 106463fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 10656218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 106602b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 106733c40538SMatthias Ringwald l2cap_notify_channel_can_send(); 10686218e6f1Smatthias.ringwald break; 10696218e6f1Smatthias.ringwald 1070ee091cf1Smatthias.ringwald // HCI Connection Timeouts 1071afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 1072f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1073bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 107480ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 10752d00edd4Smatthias.ringwald hci_con_used = 0; 1076665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1077665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1078665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1079fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 10802d00edd4Smatthias.ringwald hci_con_used = 1; 1081c22aecc9S[email protected] break; 1082ee091cf1Smatthias.ringwald } 10832d00edd4Smatthias.ringwald if (hci_con_used) break; 1084d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 10859edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1086afde0c52Smatthias.ringwald break; 1087ee091cf1Smatthias.ringwald 1088df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1089f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1090665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1091665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1092665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1093fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 10942df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1095df3354fcS[email protected] break; 1096df3354fcS[email protected] } 1097c22aecc9S[email protected] break; 1098df3354fcS[email protected] 10995cb87675SMatthias Ringwald #ifdef ENABLE_BLE 11005cb87675SMatthias Ringwald case HCI_EVENT_LE_META: 11015cb87675SMatthias Ringwald switch (packet[2]){ 11025cb87675SMatthias Ringwald int addr_type; 11035cb87675SMatthias Ringwald l2cap_channel_t * channel; 11045cb87675SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 11055cb87675SMatthias Ringwald // Connection management 11065cb87675SMatthias Ringwald reverse_bd_addr(&packet[8], address); 11075cb87675SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 11085cb87675SMatthias Ringwald log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address)); 11095cb87675SMatthias Ringwald // get l2cap_channel 11105cb87675SMatthias Ringwald // also pass in status 11115cb87675SMatthias Ringwald channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type); 11125cb87675SMatthias Ringwald if (!channel) break; 11135cb87675SMatthias Ringwald l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4)); 11145cb87675SMatthias Ringwald break; 11155cb87675SMatthias Ringwald default: 11165cb87675SMatthias Ringwald break; 11175cb87675SMatthias Ringwald } 11185cb87675SMatthias Ringwald break; 11195cb87675SMatthias Ringwald #endif 11205cb87675SMatthias Ringwald 11215611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1122f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1123bd63148eS[email protected] log_info("l2cap - security level update"); 1124665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1125665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1126665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1127fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11285533f01eS[email protected] 1129bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1130bd63148eS[email protected] 1131e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 11325533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 11335533f01eS[email protected] 1134df3354fcS[email protected] switch (channel->state){ 1135df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 11365533f01eS[email protected] if (actual_level >= required_level){ 1137f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1138f85a9399S[email protected] l2cap_emit_connection_request(channel); 11391eb2563eS[email protected] } else { 1140775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 11411eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 11421eb2563eS[email protected] } 1143df3354fcS[email protected] break; 1144df3354fcS[email protected] 1145df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 11465533f01eS[email protected] if (actual_level >= required_level){ 1147df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1148df3354fcS[email protected] } else { 1149df3354fcS[email protected] // disconnnect, authentication not good enough 1150df3354fcS[email protected] hci_disconnect_security_block(handle); 1151df3354fcS[email protected] } 1152df3354fcS[email protected] break; 1153df3354fcS[email protected] 1154df3354fcS[email protected] default: 1155df3354fcS[email protected] break; 1156df3354fcS[email protected] } 1157f85a9399S[email protected] } 1158f85a9399S[email protected] break; 1159f85a9399S[email protected] 1160afde0c52Smatthias.ringwald default: 1161afde0c52Smatthias.ringwald break; 1162afde0c52Smatthias.ringwald } 1163afde0c52Smatthias.ringwald 1164bd63148eS[email protected] l2cap_run(); 11651e6aba47Smatthias.ringwald } 11661e6aba47Smatthias.ringwald 1167afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1168b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1169e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1170e7ff783cSmatthias.ringwald l2cap_run(); 117184836b65Smatthias.ringwald } 117284836b65Smatthias.ringwald 11732b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 11744cf56b4aSmatthias.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." 11752b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 11762b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 11772b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 11782b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 11792b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 11802b360848Smatthias.ringwald signaling_responses_pending++; 11812b360848Smatthias.ringwald l2cap_run(); 11822b360848Smatthias.ringwald } 11832b360848Smatthias.ringwald } 11842b360848Smatthias.ringwald 1185b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1186645658c9Smatthias.ringwald 11879da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1188645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1189645658c9Smatthias.ringwald if (!service) { 1190645658c9Smatthias.ringwald // 0x0002 PSM not supported 11912b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1192645658c9Smatthias.ringwald return; 1193645658c9Smatthias.ringwald } 1194645658c9Smatthias.ringwald 11955061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1196645658c9Smatthias.ringwald if (!hci_connection) { 11972b360848Smatthias.ringwald // 11989da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1199645658c9Smatthias.ringwald return; 1200645658c9Smatthias.ringwald } 12012bd8b7e7S[email protected] 1202645658c9Smatthias.ringwald // alloc structure 12039da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1204da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC, 1205da144af5SMatthias Ringwald psm, service->mtu, service->required_security_level); 12062b360848Smatthias.ringwald if (!channel){ 12072b360848Smatthias.ringwald // 0x0004 No resources available 12082b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 12092b360848Smatthias.ringwald return; 12102b360848Smatthias.ringwald } 1211da144af5SMatthias Ringwald 1212fc64f94aSMatthias Ringwald channel->con_handle = handle; 1213b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1214b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1215645658c9Smatthias.ringwald 1216f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 12172985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 12182985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 12199775e25bSmatthias.ringwald } 12209775e25bSmatthias.ringwald 1221645658c9Smatthias.ringwald // set initial state 1222df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1223ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1224e405ae81Smatthias.ringwald 1225645658c9Smatthias.ringwald // add to connections list 1226665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1227645658c9Smatthias.ringwald 1228f85a9399S[email protected] // assert security requirements 12291eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1230e405ae81Smatthias.ringwald } 1231645658c9Smatthias.ringwald 1232ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1233e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1234b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1235e405ae81Smatthias.ringwald if (!channel) { 1236ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1237e405ae81Smatthias.ringwald return; 1238e405ae81Smatthias.ringwald } 1239e405ae81Smatthias.ringwald 1240552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1241e405ae81Smatthias.ringwald 1242552d92a1Smatthias.ringwald // process 1243552d92a1Smatthias.ringwald l2cap_run(); 1244e405ae81Smatthias.ringwald } 1245645658c9Smatthias.ringwald 12467ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){ 12477ef6a7bbSMatthias Ringwald log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid); 1248b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1249e405ae81Smatthias.ringwald if (!channel) { 1250ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1251e405ae81Smatthias.ringwald return; 1252e405ae81Smatthias.ringwald } 1253e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 12547ef6a7bbSMatthias Ringwald channel->reason = 0x04; // no resources available 1255e7ff783cSmatthias.ringwald l2cap_run(); 1256645658c9Smatthias.ringwald } 1257645658c9Smatthias.ringwald 12587f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1259b1988dceSmatthias.ringwald 1260b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1261b1988dceSmatthias.ringwald 1262f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 126363a7246aSmatthias.ringwald if (flags & 1) { 126463a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 126563a7246aSmatthias.ringwald } 126663a7246aSmatthias.ringwald 12672784b77dSmatthias.ringwald // accept the other's configuration options 1268f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 12693de7c0caSmatthias.ringwald uint16_t pos = 8; 12703de7c0caSmatthias.ringwald while (pos < end_pos){ 127163a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 127263a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 127363a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 127463a7246aSmatthias.ringwald pos++; 12751dc511deSmatthias.ringwald uint8_t length = command[pos++]; 12761dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 127763a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1278f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 12799da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 128063a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 128163a7246aSmatthias.ringwald } 12820fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 12830fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1284f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 12850fe7a9d0S[email protected] } 128663a7246aSmatthias.ringwald // check for unknown options 128763a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1288c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 128963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 12901dc511deSmatthias.ringwald } 12911dc511deSmatthias.ringwald pos += length; 12921dc511deSmatthias.ringwald } 12932784b77dSmatthias.ringwald } 12942784b77dSmatthias.ringwald 1295fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 12969da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 129773cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 129873cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1299019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1300019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1301fa8473a4Smatthias.ringwald return 1; 1302fa8473a4Smatthias.ringwald } 1303fa8473a4Smatthias.ringwald 1304fa8473a4Smatthias.ringwald 13057f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 13061e6aba47Smatthias.ringwald 130700d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 130800d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 130938e5900eSmatthias.ringwald uint16_t result = 0; 13101e6aba47Smatthias.ringwald 13119da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1312b35f641cSmatthias.ringwald 13139a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 13149a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 13159a011532Smatthias.ringwald switch (channel->state){ 1316fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 13179a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 13182b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 13199a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 13209a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 13219a011532Smatthias.ringwald break; 13229a011532Smatthias.ringwald 13239a011532Smatthias.ringwald default: 13249a011532Smatthias.ringwald // ignore in other states 13259a011532Smatthias.ringwald break; 13269a011532Smatthias.ringwald } 13279a011532Smatthias.ringwald return; 13289a011532Smatthias.ringwald } 13299a011532Smatthias.ringwald 133056081214Smatthias.ringwald // @STATEMACHINE(l2cap) 13311e6aba47Smatthias.ringwald switch (channel->state) { 13321e6aba47Smatthias.ringwald 13331e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 13341e6aba47Smatthias.ringwald switch (code){ 13351e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 13365932bd7cS[email protected] l2cap_stop_rtx(channel); 1337f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 133838e5900eSmatthias.ringwald switch (result) { 133938e5900eSmatthias.ringwald case 0: 1340169f8b28Smatthias.ringwald // successful connection 1341f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1342fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 134328ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 134438e5900eSmatthias.ringwald break; 134538e5900eSmatthias.ringwald case 1: 13465932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 13475932bd7cS[email protected] l2cap_start_ertx(channel); 134838e5900eSmatthias.ringwald break; 134938e5900eSmatthias.ringwald default: 1350eb920dbeSmatthias.ringwald // channel closed 1351eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1352f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 135338e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1354eb920dbeSmatthias.ringwald 1355eb920dbeSmatthias.ringwald // drop link key if security block 1356eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 135715a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 1358eb920dbeSmatthias.ringwald } 1359eb920dbeSmatthias.ringwald 1360eb920dbeSmatthias.ringwald // discard channel 1361665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1362d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 136338e5900eSmatthias.ringwald break; 13641e6aba47Smatthias.ringwald } 13651e6aba47Smatthias.ringwald break; 136638e5900eSmatthias.ringwald 136738e5900eSmatthias.ringwald default: 13681e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 136938e5900eSmatthias.ringwald break; 13701e6aba47Smatthias.ringwald } 13711e6aba47Smatthias.ringwald break; 13721e6aba47Smatthias.ringwald 1373fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1374f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1375ae280e73Smatthias.ringwald switch (code) { 1376ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 137728ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1378ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 137963a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 138063a7246aSmatthias.ringwald // only done if continuation not set 138163a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 138263a7246aSmatthias.ringwald } 1383ae280e73Smatthias.ringwald break; 13841e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 13855932bd7cS[email protected] l2cap_stop_rtx(channel); 13865932bd7cS[email protected] switch (result){ 13875932bd7cS[email protected] case 0: // success 13885932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 13895932bd7cS[email protected] break; 13905932bd7cS[email protected] case 4: // pending 13915932bd7cS[email protected] l2cap_start_ertx(channel); 13925932bd7cS[email protected] break; 13935932bd7cS[email protected] default: 1394fe9d8984S[email protected] // retry on negative result 1395fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1396fe9d8984S[email protected] break; 1397fe9d8984S[email protected] } 13985a67bd4aSmatthias.ringwald break; 13995a67bd4aSmatthias.ringwald default: 14005a67bd4aSmatthias.ringwald break; 14011e6aba47Smatthias.ringwald } 1402fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1403fa8473a4Smatthias.ringwald // for open: 14045a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1405fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1406c8e4258aSmatthias.ringwald } 1407c8e4258aSmatthias.ringwald break; 1408f62db1e3Smatthias.ringwald 1409f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1410f62db1e3Smatthias.ringwald switch (code) { 1411f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 141227a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 141327a923d0Smatthias.ringwald break; 14145a67bd4aSmatthias.ringwald default: 14155a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 14165a67bd4aSmatthias.ringwald break; 141727a923d0Smatthias.ringwald } 141827a923d0Smatthias.ringwald break; 141984836b65Smatthias.ringwald 142084836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 142184836b65Smatthias.ringwald // @TODO handle incoming requests 142284836b65Smatthias.ringwald break; 142384836b65Smatthias.ringwald 142484836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 142584836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 142684836b65Smatthias.ringwald break; 142710642e45Smatthias.ringwald default: 142810642e45Smatthias.ringwald break; 142927a923d0Smatthias.ringwald } 14309da54300S[email protected] // log_info("new state %u", channel->state); 143127a923d0Smatthias.ringwald } 143227a923d0Smatthias.ringwald 143300d93d79Smatthias.ringwald 14347f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 143500d93d79Smatthias.ringwald 143600d93d79Smatthias.ringwald // get code, signalind identifier and command len 143700d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 143800d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 143900d93d79Smatthias.ringwald 144000d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 144100d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 144263a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 144300d93d79Smatthias.ringwald return; 144400d93d79Smatthias.ringwald } 144500d93d79Smatthias.ringwald 144600d93d79Smatthias.ringwald // general commands without an assigned channel 144700d93d79Smatthias.ringwald switch(code) { 144800d93d79Smatthias.ringwald 144900d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1450f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1451f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 145200d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 14532b83fb7dSmatthias.ringwald return; 145400d93d79Smatthias.ringwald } 145500d93d79Smatthias.ringwald 14562b360848Smatthias.ringwald case ECHO_REQUEST: 14572b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 14582b83fb7dSmatthias.ringwald return; 145900d93d79Smatthias.ringwald 146000d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1461f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 14622b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 14632b83fb7dSmatthias.ringwald return; 146400d93d79Smatthias.ringwald } 146500d93d79Smatthias.ringwald 146600d93d79Smatthias.ringwald default: 146700d93d79Smatthias.ringwald break; 146800d93d79Smatthias.ringwald } 146900d93d79Smatthias.ringwald 147000d93d79Smatthias.ringwald 147100d93d79Smatthias.ringwald // Get potential destination CID 1472f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 147300d93d79Smatthias.ringwald 147400d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1475665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1476665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1477665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1478665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1479fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 148000d93d79Smatthias.ringwald if (code & 1) { 1481b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1482b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 148300d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 14844e32727eSmatthias.ringwald break; 148500d93d79Smatthias.ringwald } 148600d93d79Smatthias.ringwald } else { 1487b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 148800d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 148900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 14904e32727eSmatthias.ringwald break; 149100d93d79Smatthias.ringwald } 149200d93d79Smatthias.ringwald } 149300d93d79Smatthias.ringwald } 149400d93d79Smatthias.ringwald } 149500d93d79Smatthias.ringwald 1496e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1497c48b2a2cSMatthias Ringwald // @returns valid 1498c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){ 1499e7d0c9aaSMatthias Ringwald hci_connection_t * connection; 1500c48b2a2cSMatthias Ringwald uint16_t result; 1501c48b2a2cSMatthias Ringwald uint8_t event[10]; 1502e7d0c9aaSMatthias Ringwald uint16_t le_psm; 1503e7d0c9aaSMatthias Ringwald l2cap_service_t * service; 15041b8b8d05SMatthias Ringwald l2cap_channel_t * channel; 15051b8b8d05SMatthias Ringwald btstack_linked_list_iterator_t it; 150600d93d79Smatthias.ringwald 15071b8b8d05SMatthias Ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 1508*23017473SMatthias Ringwald log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id); 15091b8b8d05SMatthias Ringwald 15101b8b8d05SMatthias Ringwald switch (code){ 151100d93d79Smatthias.ringwald 1512c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_RESPONSE: 1513c48b2a2cSMatthias Ringwald result = little_endian_read_16(command, 4); 1514ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1515ccf076adS[email protected] break; 1516da886c03S[email protected] 1517c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_REQUEST: 1518e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1519da886c03S[email protected] if (connection){ 15206d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 15216d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 1522c48b2a2cSMatthias Ringwald return 0; 15236d91fb6cSMatthias Ringwald } 1524da886c03S[email protected] int update_parameter = 1; 1525a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 15264ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 1527c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(command,8); 1528c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(command,10); 1529c48b2a2cSMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(command,12); 1530c48b2a2cSMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(command,14); 1531da886c03S[email protected] 1532da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1533da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1534da886c03S[email protected] 1535da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1536da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1537da886c03S[email protected] 1538da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1539da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1540da886c03S[email protected] 1541da886c03S[email protected] if (update_parameter){ 1542da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1543da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1544da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1545da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1546da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1547da886c03S[email protected] } else { 1548da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1549da886c03S[email protected] } 1550c48b2a2cSMatthias Ringwald connection->le_con_param_update_identifier = sig_id; 1551da886c03S[email protected] } 1552da886c03S[email protected] 155333c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 1554c48b2a2cSMatthias Ringwald 1555c48b2a2cSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1556c48b2a2cSMatthias Ringwald event[1] = 8; 1557c48b2a2cSMatthias Ringwald memcpy(&event[2], &command[4], 8); 1558c48b2a2cSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 155933c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1560ccf076adS[email protected] break; 1561c48b2a2cSMatthias Ringwald 1562e7d0c9aaSMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 1563e7d0c9aaSMatthias Ringwald 1564e7d0c9aaSMatthias Ringwald // get hci connection, bail if not found (must not happen) 1565e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1566e7d0c9aaSMatthias Ringwald if (!connection) return 0; 1567e7d0c9aaSMatthias Ringwald 1568e7d0c9aaSMatthias Ringwald // check if service registered 1569e7d0c9aaSMatthias Ringwald le_psm = little_endian_read_16(command, 4); 1570e7d0c9aaSMatthias Ringwald service = l2cap_le_get_service(le_psm); 1571e7d0c9aaSMatthias Ringwald 1572e7d0c9aaSMatthias Ringwald if (service){ 1573e7d0c9aaSMatthias Ringwald 1574e7d0c9aaSMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, 6); 1575e7d0c9aaSMatthias Ringwald if (source_cid < 0x40){ 1576e7d0c9aaSMatthias Ringwald // 0x0009 Connection refused - Invalid Source CID 1577e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009); 1578e7d0c9aaSMatthias Ringwald return 1; 1579e7d0c9aaSMatthias Ringwald } 1580e7d0c9aaSMatthias Ringwald 1581e7d0c9aaSMatthias Ringwald // go through list of channels for this ACL connection and check if we get a match 1582e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1583e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 15841b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 15851b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 15861b8b8d05SMatthias Ringwald if (a_channel->remote_cid != source_cid) continue; 1587e7d0c9aaSMatthias Ringwald // 0x000a Connection refused - Source CID already allocated 1588e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a); 1589e7d0c9aaSMatthias Ringwald return 1; 1590e7d0c9aaSMatthias Ringwald } 1591e7d0c9aaSMatthias Ringwald 1592e7d0c9aaSMatthias Ringwald // TODO: deal with authentication requirements, errors 0x005 - 000x8 1593e7d0c9aaSMatthias Ringwald 1594e7d0c9aaSMatthias Ringwald // allocate channel 15951b8b8d05SMatthias Ringwald channel = l2cap_create_channel_entry(service->packet_handler, connection->address, 1596e7d0c9aaSMatthias Ringwald BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level); 1597e7d0c9aaSMatthias Ringwald if (!channel){ 1598e7d0c9aaSMatthias Ringwald // 0x0004 Connection refused – no resources available 1599e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004); 1600e7d0c9aaSMatthias Ringwald return 1; 1601e7d0c9aaSMatthias Ringwald } 1602e7d0c9aaSMatthias Ringwald 1603e7d0c9aaSMatthias Ringwald channel->con_handle = handle; 1604e7d0c9aaSMatthias Ringwald channel->remote_cid = source_cid; 1605e7d0c9aaSMatthias Ringwald channel->remote_sig_id = sig_id; 1606e7d0c9aaSMatthias Ringwald 1607e7d0c9aaSMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 1608e7d0c9aaSMatthias Ringwald if (channel->local_mtu > l2cap_max_le_mtu()) { 1609e7d0c9aaSMatthias Ringwald channel->local_mtu = l2cap_max_le_mtu(); 1610e7d0c9aaSMatthias Ringwald } 1611e7d0c9aaSMatthias Ringwald 1612e7d0c9aaSMatthias Ringwald // set initial state 1613e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1614e7d0c9aaSMatthias Ringwald 1615e7d0c9aaSMatthias Ringwald // add to connections list 1616e7d0c9aaSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1617e7d0c9aaSMatthias Ringwald 1618e7d0c9aaSMatthias Ringwald // post connection request event 1619e7d0c9aaSMatthias Ringwald l2cap_emit_connection_request(channel); 1620e7d0c9aaSMatthias Ringwald 1621e7d0c9aaSMatthias Ringwald } else { 1622e7d0c9aaSMatthias Ringwald // Connection refused – LE_PSM not supported 1623e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002); 1624e7d0c9aaSMatthias Ringwald } 1625e7d0c9aaSMatthias Ringwald break; 16261b8b8d05SMatthias Ringwald 16271b8b8d05SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_RESPONSE: 16281b8b8d05SMatthias Ringwald // Find channel for this sig_id and connection handle 16291b8b8d05SMatthias Ringwald channel = NULL; 16301b8b8d05SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 16311b8b8d05SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 16321b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 16331b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 16341b8b8d05SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 16351b8b8d05SMatthias Ringwald channel = a_channel; 16361b8b8d05SMatthias Ringwald break; 16371b8b8d05SMatthias Ringwald } 16381b8b8d05SMatthias Ringwald // TODO: send error 16391b8b8d05SMatthias Ringwald if (!channel) break; 16401b8b8d05SMatthias Ringwald 16411b8b8d05SMatthias Ringwald // cid + 0 16421b8b8d05SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8); 16431b8b8d05SMatthias Ringwald if (result){ 16441b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 16451b8b8d05SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 16461b8b8d05SMatthias Ringwald l2cap_emit_channel_opened(channel, result); 16471b8b8d05SMatthias Ringwald 16481b8b8d05SMatthias Ringwald // discard channel 16491b8b8d05SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 16501b8b8d05SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 16511b8b8d05SMatthias Ringwald break; 16521b8b8d05SMatthias Ringwald } 16531b8b8d05SMatthias Ringwald 16541b8b8d05SMatthias Ringwald // success 16551b8b8d05SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 16561b8b8d05SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 16571b8b8d05SMatthias Ringwald channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4); 16581b8b8d05SMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6); 16591b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 16601b8b8d05SMatthias Ringwald l2cap_emit_channel_opened(channel, result); 16611b8b8d05SMatthias Ringwald break; 16621b8b8d05SMatthias Ringwald 1663c48b2a2cSMatthias Ringwald default: 1664c48b2a2cSMatthias Ringwald // command unknown -> reject command 1665c48b2a2cSMatthias Ringwald return 0; 1666ccf076adS[email protected] } 1667c48b2a2cSMatthias Ringwald return 1; 1668c48b2a2cSMatthias Ringwald } 1669e7d0c9aaSMatthias Ringwald #endif 1670c48b2a2cSMatthias Ringwald 1671c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 1672c48b2a2cSMatthias Ringwald 1673c48b2a2cSMatthias Ringwald // Get Channel ID 1674c48b2a2cSMatthias Ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 1675c48b2a2cSMatthias Ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 1676c48b2a2cSMatthias Ringwald 1677c48b2a2cSMatthias Ringwald switch (channel_id) { 1678c48b2a2cSMatthias Ringwald 1679c48b2a2cSMatthias Ringwald case L2CAP_CID_SIGNALING: { 1680c48b2a2cSMatthias Ringwald uint16_t command_offset = 8; 1681c48b2a2cSMatthias Ringwald while (command_offset < size) { 1682c48b2a2cSMatthias Ringwald // handle signaling commands 1683c48b2a2cSMatthias Ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 1684c48b2a2cSMatthias Ringwald 1685c48b2a2cSMatthias Ringwald // increment command_offset 1686c48b2a2cSMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1687c48b2a2cSMatthias Ringwald } 1688c48b2a2cSMatthias Ringwald break; 1689c48b2a2cSMatthias Ringwald } 1690c48b2a2cSMatthias Ringwald 1691e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1692e7d0c9aaSMatthias Ringwald case L2CAP_CID_SIGNALING_LE: { 1693e7d0c9aaSMatthias Ringwald uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 1694e7d0c9aaSMatthias Ringwald int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id); 1695c48b2a2cSMatthias Ringwald if (!valid){ 16961bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1697ccf076adS[email protected] } 1698ccf076adS[email protected] break; 1699e7d0c9aaSMatthias Ringwald } 1700e7d0c9aaSMatthias Ringwald #endif 1701c48b2a2cSMatthias Ringwald 1702c48b2a2cSMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1703c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 1704c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1705ccf076adS[email protected] } 1706c48b2a2cSMatthias Ringwald break; 1707c48b2a2cSMatthias Ringwald 1708c48b2a2cSMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1709c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 1710c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1711c48b2a2cSMatthias Ringwald } 1712c48b2a2cSMatthias Ringwald break; 1713c48b2a2cSMatthias Ringwald 1714c48b2a2cSMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1715c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 1716c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1717c48b2a2cSMatthias Ringwald } 1718c48b2a2cSMatthias Ringwald break; 17191bbc0b23S[email protected] 17205652b5ffS[email protected] default: { 172100d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 17223d50b4baSMatthias Ringwald l2cap_channel_t * l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 1723d1fd2a88SMatthias Ringwald if (l2cap_channel) { 17243d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 172500d93d79Smatthias.ringwald } 17265652b5ffS[email protected] break; 17275652b5ffS[email protected] } 17285652b5ffS[email protected] } 172900d93d79Smatthias.ringwald 17301eb2563eS[email protected] l2cap_run(); 17312718e2e7Smatthias.ringwald } 173200d93d79Smatthias.ringwald 173315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 173427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 1735f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1736f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1737f62db1e3Smatthias.ringwald // discard channel 17389dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1739665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1740d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1741c8e4258aSmatthias.ringwald } 17421e6aba47Smatthias.ringwald 17438f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1744665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1745665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1746665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1747665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 17489d9bbc01Smatthias.ringwald if ( service->psm == psm){ 17499d9bbc01Smatthias.ringwald return service; 17509d9bbc01Smatthias.ringwald }; 17519d9bbc01Smatthias.ringwald } 17529d9bbc01Smatthias.ringwald return NULL; 17539d9bbc01Smatthias.ringwald } 17549d9bbc01Smatthias.ringwald 17557192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 17567192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 17577192e786SMatthias Ringwald } 17587192e786SMatthias Ringwald 1759e0abb8e7S[email protected] 1760be2053a6SMatthias 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){ 1761be2053a6SMatthias Ringwald 1762be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1763e0abb8e7S[email protected] 17644bb582b6Smatthias.ringwald // check for alread registered psm 17659d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1766277abc2cSmatthias.ringwald if (service) { 1767be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1768be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1769277abc2cSmatthias.ringwald } 17709d9bbc01Smatthias.ringwald 17714bb582b6Smatthias.ringwald // alloc structure 1772bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1773277abc2cSmatthias.ringwald if (!service) { 1774be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1775be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1776277abc2cSmatthias.ringwald } 17779d9bbc01Smatthias.ringwald 17789d9bbc01Smatthias.ringwald // fill in 17799d9bbc01Smatthias.ringwald service->psm = psm; 17809d9bbc01Smatthias.ringwald service->mtu = mtu; 178105ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1782df3354fcS[email protected] service->required_security_level = security_level; 17839d9bbc01Smatthias.ringwald 17849d9bbc01Smatthias.ringwald // add to services list 1785665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1786c0e866bfSmatthias.ringwald 1787c0e866bfSmatthias.ringwald // enable page scan 178815a95bd5SMatthias Ringwald gap_connectable_control(1); 17895842b6d9Smatthias.ringwald 1790be2053a6SMatthias Ringwald return 0; 17919d9bbc01Smatthias.ringwald } 17929d9bbc01Smatthias.ringwald 17937e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){ 1794e0abb8e7S[email protected] 1795e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1796e0abb8e7S[email protected] 17979d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 17987e8856ebSMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 1799665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1800d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1801c0e866bfSmatthias.ringwald 1802c0e866bfSmatthias.ringwald // disable page scan when no services registered 18037e8856ebSMatthias Ringwald if (btstack_linked_list_empty(&l2cap_services)) { 180415a95bd5SMatthias Ringwald gap_connectable_control(0); 18059d9bbc01Smatthias.ringwald } 18067e8856ebSMatthias Ringwald return 0; 18077e8856ebSMatthias Ringwald } 18089d9bbc01Smatthias.ringwald 18095652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 181005ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 18115628cf69SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 18125628cf69SMatthias Ringwald if (index < 0) return; 18135628cf69SMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 18145652b5ffS[email protected] } 18155652b5ffS[email protected] 1816a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 18177192e786SMatthias Ringwald 1818e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){ 1819e7d0c9aaSMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, le_psm); 18207192e786SMatthias Ringwald } 1821efedfb4cSMatthias Ringwald 1822da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){ 18237192e786SMatthias Ringwald 1824da144af5SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm); 18257192e786SMatthias Ringwald 18267192e786SMatthias Ringwald // check for alread registered psm 18277192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 18287192e786SMatthias Ringwald if (service) { 1829da144af5SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 18307192e786SMatthias Ringwald } 18317192e786SMatthias Ringwald 18327192e786SMatthias Ringwald // alloc structure 18337192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 18347192e786SMatthias Ringwald if (!service) { 18357192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 1836da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 18377192e786SMatthias Ringwald } 18387192e786SMatthias Ringwald 18397192e786SMatthias Ringwald // fill in 18407192e786SMatthias Ringwald service->psm = psm; 1841da144af5SMatthias Ringwald service->mtu = 0; 18427192e786SMatthias Ringwald service->packet_handler = packet_handler; 18437192e786SMatthias Ringwald service->required_security_level = security_level; 18447192e786SMatthias Ringwald 18457192e786SMatthias Ringwald // add to services list 1846665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 18477192e786SMatthias Ringwald 18487192e786SMatthias Ringwald // done 1849da144af5SMatthias Ringwald return 0; 18507192e786SMatthias Ringwald } 18517192e786SMatthias Ringwald 1852da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) { 18537192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 18547192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 18559367f9b0SMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 1856da144af5SMatthias Ringwald 1857665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 18587192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 1859da144af5SMatthias Ringwald return 0; 18607192e786SMatthias Ringwald } 1861da144af5SMatthias Ringwald 1862da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){ 1863e7d0c9aaSMatthias Ringwald // get channel 1864e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 1865e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 1866e7d0c9aaSMatthias Ringwald 1867e7d0c9aaSMatthias Ringwald // validate state 1868e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 1869e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1870e7d0c9aaSMatthias Ringwald } 1871e7d0c9aaSMatthias Ringwald 1872efedfb4cSMatthias Ringwald // set state accept connection 1873*23017473SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT; 1874*23017473SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 1875*23017473SMatthias Ringwald channel->local_mtu = mtu; 1876*23017473SMatthias Ringwald channel->credits_incoming = initial_credits; 1877e7d0c9aaSMatthias Ringwald 1878e7d0c9aaSMatthias Ringwald // go 1879e7d0c9aaSMatthias Ringwald l2cap_run(); 1880da144af5SMatthias Ringwald return 0; 1881da144af5SMatthias Ringwald } 1882da144af5SMatthias Ringwald 1883da144af5SMatthias Ringwald /** 1884da144af5SMatthias Ringwald * @brief Deny incoming LE Data Channel connection due to resource constraints 1885da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1886da144af5SMatthias Ringwald */ 1887da144af5SMatthias Ringwald 1888da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){ 1889e7d0c9aaSMatthias Ringwald // get channel 1890e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 1891e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 1892e7d0c9aaSMatthias Ringwald 1893e7d0c9aaSMatthias Ringwald // validate state 1894e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 1895e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1896e7d0c9aaSMatthias Ringwald } 1897e7d0c9aaSMatthias Ringwald 1898efedfb4cSMatthias Ringwald // set state decline connection 1899e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE; 1900e7d0c9aaSMatthias Ringwald channel->reason = 0x04; // no resources available 1901e7d0c9aaSMatthias Ringwald l2cap_run(); 1902da144af5SMatthias Ringwald return 0; 1903da144af5SMatthias Ringwald } 1904da144af5SMatthias Ringwald 1905da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type, 1906da144af5SMatthias Ringwald uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 1907efedfb4cSMatthias Ringwald uint16_t * out_local_cid) { 1908efedfb4cSMatthias Ringwald 1909da144af5SMatthias Ringwald log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 1910da144af5SMatthias Ringwald 19115cb87675SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, LEVEL_0); 1912da144af5SMatthias Ringwald if (!channel) { 1913da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1914da144af5SMatthias Ringwald } 1915e7d0c9aaSMatthias Ringwald log_info("l2cap_le_create_channel %p", channel); 1916da144af5SMatthias Ringwald 1917da144af5SMatthias Ringwald // store local_cid 1918da144af5SMatthias Ringwald if (out_local_cid){ 1919da144af5SMatthias Ringwald *out_local_cid = channel->local_cid; 1920da144af5SMatthias Ringwald } 1921da144af5SMatthias Ringwald 1922e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 1923e7d0c9aaSMatthias Ringwald 1924efedfb4cSMatthias Ringwald // add to connections list 1925efedfb4cSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1926efedfb4cSMatthias Ringwald 1927da144af5SMatthias Ringwald // check if hci connection is already usable 1928da144af5SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type); 1929efedfb4cSMatthias Ringwald 1930da144af5SMatthias Ringwald if (conn){ 1931da144af5SMatthias Ringwald log_info("l2cap_le_create_channel, hci connection already exists"); 19325cb87675SMatthias Ringwald l2cap_handle_le_connection_complete(channel, 0, conn->con_handle); 1933efedfb4cSMatthias Ringwald } else { 1934efedfb4cSMatthias Ringwald 1935efedfb4cSMatthias Ringwald // 1936efedfb4cSMatthias Ringwald // TODO: start timer 1937efedfb4cSMatthias Ringwald // .. 1938efedfb4cSMatthias Ringwald 1939efedfb4cSMatthias Ringwald // start connection 1940efedfb4cSMatthias Ringwald uint8_t res = gap_auto_connection_start(address_type, address); 194148af0c56SMatthias Ringwald if (res){ 1942efedfb4cSMatthias Ringwald // discard channel object 1943e7d0c9aaSMatthias Ringwald log_info("gap_auto_connection_start failed! 0x%02x", res); 1944e7d0c9aaSMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1945efedfb4cSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1946efedfb4cSMatthias Ringwald return res; 1947da144af5SMatthias Ringwald } 1948efedfb4cSMatthias Ringwald } 1949da144af5SMatthias Ringwald return 0; 1950da144af5SMatthias Ringwald } 1951da144af5SMatthias Ringwald 1952da144af5SMatthias Ringwald /** 1953da144af5SMatthias Ringwald * @brief Provide credtis for LE Data Channel 1954da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1955da144af5SMatthias Ringwald * @param credits Number additional credits for peer 1956da144af5SMatthias Ringwald */ 1957da144af5SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t cid, uint16_t credits){ 1958e7d0c9aaSMatthias Ringwald // get channel 1959efedfb4cSMatthias Ringwald // bail if missing 1960efedfb4cSMatthias Ringwald // check state 1961efedfb4cSMatthias Ringwald // check incoming credits + credits <= 0xffff 1962efedfb4cSMatthias Ringwald // set credits_granted 1963efedfb4cSMatthias Ringwald // l2cap_le_run() 1964da144af5SMatthias Ringwald return 0; 1965da144af5SMatthias Ringwald } 1966da144af5SMatthias Ringwald 1967da144af5SMatthias Ringwald /** 1968da144af5SMatthias Ringwald * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 1969da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1970da144af5SMatthias Ringwald */ 1971da144af5SMatthias Ringwald int l2cap_le_can_send_now(uint16_t cid){ 1972efedfb4cSMatthias Ringwald // check if data can be sent via le at all 1973da144af5SMatthias Ringwald return 0; 1974da144af5SMatthias Ringwald } 1975da144af5SMatthias Ringwald 1976da144af5SMatthias Ringwald /** 1977da144af5SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 1978da144af5SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 1979da144af5SMatthias Ringwald * so packet handler should be ready to handle it 1980da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1981da144af5SMatthias Ringwald */ 1982da144af5SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t cid){ 1983efedfb4cSMatthias Ringwald // same as for non-le 1984da144af5SMatthias Ringwald return 0; 1985da144af5SMatthias Ringwald } 1986da144af5SMatthias Ringwald 1987da144af5SMatthias Ringwald /** 1988da144af5SMatthias Ringwald * @brief Send data via LE Data Channel 1989da144af5SMatthias Ringwald * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 1990da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1991da144af5SMatthias Ringwald * @param data data to send 1992da144af5SMatthias Ringwald * @param size data size 1993da144af5SMatthias Ringwald */ 1994da144af5SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t cid, uint8_t * data, uint16_t size){ 1995efedfb4cSMatthias Ringwald // check if no outgoing data is pending 1996efedfb4cSMatthias Ringwald // store info 1997efedfb4cSMatthias Ringwald // l2cap_le_run() 1998da144af5SMatthias Ringwald return 0; 1999da144af5SMatthias Ringwald } 2000da144af5SMatthias Ringwald 2001da144af5SMatthias Ringwald /** 2002da144af5SMatthias Ringwald * @brief Disconnect from LE Data Channel 2003da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2004da144af5SMatthias Ringwald */ 2005da144af5SMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t cid) 2006da144af5SMatthias Ringwald { 2007efedfb4cSMatthias Ringwald // find channel 2008efedfb4cSMatthias Ringwald // check state 2009efedfb4cSMatthias Ringwald // set state SEND_DISCONNECT 2010da144af5SMatthias Ringwald return 0; 2011da144af5SMatthias Ringwald } 2012da144af5SMatthias Ringwald 20137f02f414SMatthias Ringwald #endif 2014