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; 75423017473SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT: 75523017473SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 75623017473SMatthias Ringwald // TODO: support larger MPS 75723017473SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 75823017473SMatthias 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); 75923017473SMatthias 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 } 1061*991fea48SMatthias Ringwald #ifdef ENABLE_BLE 1062*991fea48SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1063*991fea48SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1064*991fea48SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1065*991fea48SMatthias Ringwald if (channel->con_handle != handle) continue; 1066*991fea48SMatthias Ringwald l2cap_emit_channel_closed(channel); 1067*991fea48SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1068*991fea48SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1069*991fea48SMatthias Ringwald } 1070*991fea48SMatthias Ringwald #endif 1071afde0c52Smatthias.ringwald break; 1072fcadd0caSmatthias.ringwald 107333c40538SMatthias Ringwald // Notify channel packet handler if they can send now 107463fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 10756218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 107602b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 107733c40538SMatthias Ringwald l2cap_notify_channel_can_send(); 10786218e6f1Smatthias.ringwald break; 10796218e6f1Smatthias.ringwald 1080ee091cf1Smatthias.ringwald // HCI Connection Timeouts 1081afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 1082f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1083bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 108480ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 10852d00edd4Smatthias.ringwald hci_con_used = 0; 1086665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1087665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1088665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1089fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 10902d00edd4Smatthias.ringwald hci_con_used = 1; 1091c22aecc9S[email protected] break; 1092ee091cf1Smatthias.ringwald } 10932d00edd4Smatthias.ringwald if (hci_con_used) break; 1094d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 10959edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1096afde0c52Smatthias.ringwald break; 1097ee091cf1Smatthias.ringwald 1098df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1099f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1100665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1101665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1102665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1103fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11042df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1105df3354fcS[email protected] break; 1106df3354fcS[email protected] } 1107c22aecc9S[email protected] break; 1108df3354fcS[email protected] 11095cb87675SMatthias Ringwald #ifdef ENABLE_BLE 11105cb87675SMatthias Ringwald case HCI_EVENT_LE_META: 11115cb87675SMatthias Ringwald switch (packet[2]){ 11125cb87675SMatthias Ringwald int addr_type; 11135cb87675SMatthias Ringwald l2cap_channel_t * channel; 11145cb87675SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 11155cb87675SMatthias Ringwald // Connection management 11165cb87675SMatthias Ringwald reverse_bd_addr(&packet[8], address); 11175cb87675SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 11185cb87675SMatthias Ringwald log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address)); 11195cb87675SMatthias Ringwald // get l2cap_channel 11205cb87675SMatthias Ringwald // also pass in status 11215cb87675SMatthias Ringwald channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type); 11225cb87675SMatthias Ringwald if (!channel) break; 11235cb87675SMatthias Ringwald l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4)); 11245cb87675SMatthias Ringwald break; 11255cb87675SMatthias Ringwald default: 11265cb87675SMatthias Ringwald break; 11275cb87675SMatthias Ringwald } 11285cb87675SMatthias Ringwald break; 11295cb87675SMatthias Ringwald #endif 11305cb87675SMatthias Ringwald 11315611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1132f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1133bd63148eS[email protected] log_info("l2cap - security level update"); 1134665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1135665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1136665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1137fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 11385533f01eS[email protected] 1139bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1140bd63148eS[email protected] 1141e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 11425533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 11435533f01eS[email protected] 1144df3354fcS[email protected] switch (channel->state){ 1145df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 11465533f01eS[email protected] if (actual_level >= required_level){ 1147f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1148f85a9399S[email protected] l2cap_emit_connection_request(channel); 11491eb2563eS[email protected] } else { 1150775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 11511eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 11521eb2563eS[email protected] } 1153df3354fcS[email protected] break; 1154df3354fcS[email protected] 1155df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 11565533f01eS[email protected] if (actual_level >= required_level){ 1157df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1158df3354fcS[email protected] } else { 1159df3354fcS[email protected] // disconnnect, authentication not good enough 1160df3354fcS[email protected] hci_disconnect_security_block(handle); 1161df3354fcS[email protected] } 1162df3354fcS[email protected] break; 1163df3354fcS[email protected] 1164df3354fcS[email protected] default: 1165df3354fcS[email protected] break; 1166df3354fcS[email protected] } 1167f85a9399S[email protected] } 1168f85a9399S[email protected] break; 1169f85a9399S[email protected] 1170afde0c52Smatthias.ringwald default: 1171afde0c52Smatthias.ringwald break; 1172afde0c52Smatthias.ringwald } 1173afde0c52Smatthias.ringwald 1174bd63148eS[email protected] l2cap_run(); 11751e6aba47Smatthias.ringwald } 11761e6aba47Smatthias.ringwald 1177afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1178b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1179e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1180e7ff783cSmatthias.ringwald l2cap_run(); 118184836b65Smatthias.ringwald } 118284836b65Smatthias.ringwald 11832b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 11844cf56b4aSmatthias.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." 11852b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 11862b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 11872b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 11882b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 11892b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 11902b360848Smatthias.ringwald signaling_responses_pending++; 11912b360848Smatthias.ringwald l2cap_run(); 11922b360848Smatthias.ringwald } 11932b360848Smatthias.ringwald } 11942b360848Smatthias.ringwald 1195b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1196645658c9Smatthias.ringwald 11979da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1198645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1199645658c9Smatthias.ringwald if (!service) { 1200645658c9Smatthias.ringwald // 0x0002 PSM not supported 12012b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1202645658c9Smatthias.ringwald return; 1203645658c9Smatthias.ringwald } 1204645658c9Smatthias.ringwald 12055061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1206645658c9Smatthias.ringwald if (!hci_connection) { 12072b360848Smatthias.ringwald // 12089da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1209645658c9Smatthias.ringwald return; 1210645658c9Smatthias.ringwald } 12112bd8b7e7S[email protected] 1212645658c9Smatthias.ringwald // alloc structure 12139da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1214da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC, 1215da144af5SMatthias Ringwald psm, service->mtu, service->required_security_level); 12162b360848Smatthias.ringwald if (!channel){ 12172b360848Smatthias.ringwald // 0x0004 No resources available 12182b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 12192b360848Smatthias.ringwald return; 12202b360848Smatthias.ringwald } 1221da144af5SMatthias Ringwald 1222fc64f94aSMatthias Ringwald channel->con_handle = handle; 1223b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1224b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1225645658c9Smatthias.ringwald 1226f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 12272985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 12282985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 12299775e25bSmatthias.ringwald } 12309775e25bSmatthias.ringwald 1231645658c9Smatthias.ringwald // set initial state 1232df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1233ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1234e405ae81Smatthias.ringwald 1235645658c9Smatthias.ringwald // add to connections list 1236665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1237645658c9Smatthias.ringwald 1238f85a9399S[email protected] // assert security requirements 12391eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1240e405ae81Smatthias.ringwald } 1241645658c9Smatthias.ringwald 1242ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1243e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1244b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1245e405ae81Smatthias.ringwald if (!channel) { 1246ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1247e405ae81Smatthias.ringwald return; 1248e405ae81Smatthias.ringwald } 1249e405ae81Smatthias.ringwald 1250552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1251e405ae81Smatthias.ringwald 1252552d92a1Smatthias.ringwald // process 1253552d92a1Smatthias.ringwald l2cap_run(); 1254e405ae81Smatthias.ringwald } 1255645658c9Smatthias.ringwald 12567ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){ 12577ef6a7bbSMatthias Ringwald log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid); 1258b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1259e405ae81Smatthias.ringwald if (!channel) { 1260ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1261e405ae81Smatthias.ringwald return; 1262e405ae81Smatthias.ringwald } 1263e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 12647ef6a7bbSMatthias Ringwald channel->reason = 0x04; // no resources available 1265e7ff783cSmatthias.ringwald l2cap_run(); 1266645658c9Smatthias.ringwald } 1267645658c9Smatthias.ringwald 12687f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1269b1988dceSmatthias.ringwald 1270b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1271b1988dceSmatthias.ringwald 1272f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 127363a7246aSmatthias.ringwald if (flags & 1) { 127463a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 127563a7246aSmatthias.ringwald } 127663a7246aSmatthias.ringwald 12772784b77dSmatthias.ringwald // accept the other's configuration options 1278f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 12793de7c0caSmatthias.ringwald uint16_t pos = 8; 12803de7c0caSmatthias.ringwald while (pos < end_pos){ 128163a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 128263a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 128363a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 128463a7246aSmatthias.ringwald pos++; 12851dc511deSmatthias.ringwald uint8_t length = command[pos++]; 12861dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 128763a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1288f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 12899da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 129063a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 129163a7246aSmatthias.ringwald } 12920fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 12930fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1294f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 12950fe7a9d0S[email protected] } 129663a7246aSmatthias.ringwald // check for unknown options 129763a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1298c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 129963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 13001dc511deSmatthias.ringwald } 13011dc511deSmatthias.ringwald pos += length; 13021dc511deSmatthias.ringwald } 13032784b77dSmatthias.ringwald } 13042784b77dSmatthias.ringwald 1305fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 13069da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 130773cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 130873cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1309019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1310019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1311fa8473a4Smatthias.ringwald return 1; 1312fa8473a4Smatthias.ringwald } 1313fa8473a4Smatthias.ringwald 1314fa8473a4Smatthias.ringwald 13157f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 13161e6aba47Smatthias.ringwald 131700d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 131800d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 131938e5900eSmatthias.ringwald uint16_t result = 0; 13201e6aba47Smatthias.ringwald 13219da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1322b35f641cSmatthias.ringwald 13239a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 13249a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 13259a011532Smatthias.ringwald switch (channel->state){ 1326fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 13279a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 13282b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 13299a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 13309a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 13319a011532Smatthias.ringwald break; 13329a011532Smatthias.ringwald 13339a011532Smatthias.ringwald default: 13349a011532Smatthias.ringwald // ignore in other states 13359a011532Smatthias.ringwald break; 13369a011532Smatthias.ringwald } 13379a011532Smatthias.ringwald return; 13389a011532Smatthias.ringwald } 13399a011532Smatthias.ringwald 134056081214Smatthias.ringwald // @STATEMACHINE(l2cap) 13411e6aba47Smatthias.ringwald switch (channel->state) { 13421e6aba47Smatthias.ringwald 13431e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 13441e6aba47Smatthias.ringwald switch (code){ 13451e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 13465932bd7cS[email protected] l2cap_stop_rtx(channel); 1347f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 134838e5900eSmatthias.ringwald switch (result) { 134938e5900eSmatthias.ringwald case 0: 1350169f8b28Smatthias.ringwald // successful connection 1351f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1352fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 135328ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 135438e5900eSmatthias.ringwald break; 135538e5900eSmatthias.ringwald case 1: 13565932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 13575932bd7cS[email protected] l2cap_start_ertx(channel); 135838e5900eSmatthias.ringwald break; 135938e5900eSmatthias.ringwald default: 1360eb920dbeSmatthias.ringwald // channel closed 1361eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1362f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 136338e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1364eb920dbeSmatthias.ringwald 1365eb920dbeSmatthias.ringwald // drop link key if security block 1366eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 136715a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 1368eb920dbeSmatthias.ringwald } 1369eb920dbeSmatthias.ringwald 1370eb920dbeSmatthias.ringwald // discard channel 1371665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1372d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 137338e5900eSmatthias.ringwald break; 13741e6aba47Smatthias.ringwald } 13751e6aba47Smatthias.ringwald break; 137638e5900eSmatthias.ringwald 137738e5900eSmatthias.ringwald default: 13781e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 137938e5900eSmatthias.ringwald break; 13801e6aba47Smatthias.ringwald } 13811e6aba47Smatthias.ringwald break; 13821e6aba47Smatthias.ringwald 1383fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1384f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1385ae280e73Smatthias.ringwald switch (code) { 1386ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 138728ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1388ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 138963a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 139063a7246aSmatthias.ringwald // only done if continuation not set 139163a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 139263a7246aSmatthias.ringwald } 1393ae280e73Smatthias.ringwald break; 13941e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 13955932bd7cS[email protected] l2cap_stop_rtx(channel); 13965932bd7cS[email protected] switch (result){ 13975932bd7cS[email protected] case 0: // success 13985932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 13995932bd7cS[email protected] break; 14005932bd7cS[email protected] case 4: // pending 14015932bd7cS[email protected] l2cap_start_ertx(channel); 14025932bd7cS[email protected] break; 14035932bd7cS[email protected] default: 1404fe9d8984S[email protected] // retry on negative result 1405fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1406fe9d8984S[email protected] break; 1407fe9d8984S[email protected] } 14085a67bd4aSmatthias.ringwald break; 14095a67bd4aSmatthias.ringwald default: 14105a67bd4aSmatthias.ringwald break; 14111e6aba47Smatthias.ringwald } 1412fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1413fa8473a4Smatthias.ringwald // for open: 14145a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1415fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1416c8e4258aSmatthias.ringwald } 1417c8e4258aSmatthias.ringwald break; 1418f62db1e3Smatthias.ringwald 1419f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1420f62db1e3Smatthias.ringwald switch (code) { 1421f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 142227a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 142327a923d0Smatthias.ringwald break; 14245a67bd4aSmatthias.ringwald default: 14255a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 14265a67bd4aSmatthias.ringwald break; 142727a923d0Smatthias.ringwald } 142827a923d0Smatthias.ringwald break; 142984836b65Smatthias.ringwald 143084836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 143184836b65Smatthias.ringwald // @TODO handle incoming requests 143284836b65Smatthias.ringwald break; 143384836b65Smatthias.ringwald 143484836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 143584836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 143684836b65Smatthias.ringwald break; 143710642e45Smatthias.ringwald default: 143810642e45Smatthias.ringwald break; 143927a923d0Smatthias.ringwald } 14409da54300S[email protected] // log_info("new state %u", channel->state); 144127a923d0Smatthias.ringwald } 144227a923d0Smatthias.ringwald 144300d93d79Smatthias.ringwald 14447f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 144500d93d79Smatthias.ringwald 144600d93d79Smatthias.ringwald // get code, signalind identifier and command len 144700d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 144800d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 144900d93d79Smatthias.ringwald 145000d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 145100d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 145263a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 145300d93d79Smatthias.ringwald return; 145400d93d79Smatthias.ringwald } 145500d93d79Smatthias.ringwald 145600d93d79Smatthias.ringwald // general commands without an assigned channel 145700d93d79Smatthias.ringwald switch(code) { 145800d93d79Smatthias.ringwald 145900d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1460f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1461f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 146200d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 14632b83fb7dSmatthias.ringwald return; 146400d93d79Smatthias.ringwald } 146500d93d79Smatthias.ringwald 14662b360848Smatthias.ringwald case ECHO_REQUEST: 14672b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 14682b83fb7dSmatthias.ringwald return; 146900d93d79Smatthias.ringwald 147000d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1471f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 14722b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 14732b83fb7dSmatthias.ringwald return; 147400d93d79Smatthias.ringwald } 147500d93d79Smatthias.ringwald 147600d93d79Smatthias.ringwald default: 147700d93d79Smatthias.ringwald break; 147800d93d79Smatthias.ringwald } 147900d93d79Smatthias.ringwald 148000d93d79Smatthias.ringwald 148100d93d79Smatthias.ringwald // Get potential destination CID 1482f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 148300d93d79Smatthias.ringwald 148400d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1485665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1486665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1487665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1488665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1489fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 149000d93d79Smatthias.ringwald if (code & 1) { 1491b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1492b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 149300d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 14944e32727eSmatthias.ringwald break; 149500d93d79Smatthias.ringwald } 149600d93d79Smatthias.ringwald } else { 1497b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 149800d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 149900d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 15004e32727eSmatthias.ringwald break; 150100d93d79Smatthias.ringwald } 150200d93d79Smatthias.ringwald } 150300d93d79Smatthias.ringwald } 150400d93d79Smatthias.ringwald } 150500d93d79Smatthias.ringwald 1506e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1507c48b2a2cSMatthias Ringwald // @returns valid 1508c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){ 1509e7d0c9aaSMatthias Ringwald hci_connection_t * connection; 1510c48b2a2cSMatthias Ringwald uint16_t result; 1511c48b2a2cSMatthias Ringwald uint8_t event[10]; 1512e7d0c9aaSMatthias Ringwald uint16_t le_psm; 1513e7d0c9aaSMatthias Ringwald l2cap_service_t * service; 15141b8b8d05SMatthias Ringwald l2cap_channel_t * channel; 15151b8b8d05SMatthias Ringwald btstack_linked_list_iterator_t it; 151600d93d79Smatthias.ringwald 15171b8b8d05SMatthias Ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 151823017473SMatthias Ringwald log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id); 15191b8b8d05SMatthias Ringwald 15201b8b8d05SMatthias Ringwald switch (code){ 152100d93d79Smatthias.ringwald 1522c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_RESPONSE: 1523c48b2a2cSMatthias Ringwald result = little_endian_read_16(command, 4); 1524ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1525ccf076adS[email protected] break; 1526da886c03S[email protected] 1527c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_REQUEST: 1528e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1529da886c03S[email protected] if (connection){ 15306d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 15316d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 1532c48b2a2cSMatthias Ringwald return 0; 15336d91fb6cSMatthias Ringwald } 1534da886c03S[email protected] int update_parameter = 1; 1535a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 15364ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 1537c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(command,8); 1538c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(command,10); 1539c48b2a2cSMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(command,12); 1540c48b2a2cSMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(command,14); 1541da886c03S[email protected] 1542da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1543da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1544da886c03S[email protected] 1545da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1546da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1547da886c03S[email protected] 1548da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1549da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1550da886c03S[email protected] 1551da886c03S[email protected] if (update_parameter){ 1552da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1553da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1554da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1555da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1556da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1557da886c03S[email protected] } else { 1558da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1559da886c03S[email protected] } 1560c48b2a2cSMatthias Ringwald connection->le_con_param_update_identifier = sig_id; 1561da886c03S[email protected] } 1562da886c03S[email protected] 156333c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 1564c48b2a2cSMatthias Ringwald 1565c48b2a2cSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1566c48b2a2cSMatthias Ringwald event[1] = 8; 1567c48b2a2cSMatthias Ringwald memcpy(&event[2], &command[4], 8); 1568c48b2a2cSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 156933c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1570ccf076adS[email protected] break; 1571c48b2a2cSMatthias Ringwald 1572e7d0c9aaSMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 1573e7d0c9aaSMatthias Ringwald 1574e7d0c9aaSMatthias Ringwald // get hci connection, bail if not found (must not happen) 1575e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 1576e7d0c9aaSMatthias Ringwald if (!connection) return 0; 1577e7d0c9aaSMatthias Ringwald 1578e7d0c9aaSMatthias Ringwald // check if service registered 1579e7d0c9aaSMatthias Ringwald le_psm = little_endian_read_16(command, 4); 1580e7d0c9aaSMatthias Ringwald service = l2cap_le_get_service(le_psm); 1581e7d0c9aaSMatthias Ringwald 1582e7d0c9aaSMatthias Ringwald if (service){ 1583e7d0c9aaSMatthias Ringwald 1584e7d0c9aaSMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, 6); 1585e7d0c9aaSMatthias Ringwald if (source_cid < 0x40){ 1586e7d0c9aaSMatthias Ringwald // 0x0009 Connection refused - Invalid Source CID 1587e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009); 1588e7d0c9aaSMatthias Ringwald return 1; 1589e7d0c9aaSMatthias Ringwald } 1590e7d0c9aaSMatthias Ringwald 1591e7d0c9aaSMatthias Ringwald // go through list of channels for this ACL connection and check if we get a match 1592e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1593e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 15941b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 15951b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 15961b8b8d05SMatthias Ringwald if (a_channel->remote_cid != source_cid) continue; 1597e7d0c9aaSMatthias Ringwald // 0x000a Connection refused - Source CID already allocated 1598e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a); 1599e7d0c9aaSMatthias Ringwald return 1; 1600e7d0c9aaSMatthias Ringwald } 1601e7d0c9aaSMatthias Ringwald 1602e7d0c9aaSMatthias Ringwald // TODO: deal with authentication requirements, errors 0x005 - 000x8 1603e7d0c9aaSMatthias Ringwald 1604e7d0c9aaSMatthias Ringwald // allocate channel 16051b8b8d05SMatthias Ringwald channel = l2cap_create_channel_entry(service->packet_handler, connection->address, 1606e7d0c9aaSMatthias Ringwald BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level); 1607e7d0c9aaSMatthias Ringwald if (!channel){ 1608e7d0c9aaSMatthias Ringwald // 0x0004 Connection refused – no resources available 1609e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004); 1610e7d0c9aaSMatthias Ringwald return 1; 1611e7d0c9aaSMatthias Ringwald } 1612e7d0c9aaSMatthias Ringwald 1613e7d0c9aaSMatthias Ringwald channel->con_handle = handle; 1614e7d0c9aaSMatthias Ringwald channel->remote_cid = source_cid; 1615e7d0c9aaSMatthias Ringwald channel->remote_sig_id = sig_id; 1616e7d0c9aaSMatthias Ringwald 1617e7d0c9aaSMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 1618e7d0c9aaSMatthias Ringwald if (channel->local_mtu > l2cap_max_le_mtu()) { 1619e7d0c9aaSMatthias Ringwald channel->local_mtu = l2cap_max_le_mtu(); 1620e7d0c9aaSMatthias Ringwald } 1621e7d0c9aaSMatthias Ringwald 1622e7d0c9aaSMatthias Ringwald // set initial state 1623e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1624e7d0c9aaSMatthias Ringwald 1625e7d0c9aaSMatthias Ringwald // add to connections list 1626e7d0c9aaSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1627e7d0c9aaSMatthias Ringwald 1628e7d0c9aaSMatthias Ringwald // post connection request event 1629e7d0c9aaSMatthias Ringwald l2cap_emit_connection_request(channel); 1630e7d0c9aaSMatthias Ringwald 1631e7d0c9aaSMatthias Ringwald } else { 1632e7d0c9aaSMatthias Ringwald // Connection refused – LE_PSM not supported 1633e7d0c9aaSMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002); 1634e7d0c9aaSMatthias Ringwald } 1635e7d0c9aaSMatthias Ringwald break; 16361b8b8d05SMatthias Ringwald 16371b8b8d05SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_RESPONSE: 16381b8b8d05SMatthias Ringwald // Find channel for this sig_id and connection handle 16391b8b8d05SMatthias Ringwald channel = NULL; 16401b8b8d05SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 16411b8b8d05SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 16421b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 16431b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 16441b8b8d05SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 16451b8b8d05SMatthias Ringwald channel = a_channel; 16461b8b8d05SMatthias Ringwald break; 16471b8b8d05SMatthias Ringwald } 16481b8b8d05SMatthias Ringwald // TODO: send error 16491b8b8d05SMatthias Ringwald if (!channel) break; 16501b8b8d05SMatthias Ringwald 16511b8b8d05SMatthias Ringwald // cid + 0 16521b8b8d05SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8); 16531b8b8d05SMatthias Ringwald if (result){ 16541b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 16551b8b8d05SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 16561b8b8d05SMatthias Ringwald l2cap_emit_channel_opened(channel, result); 16571b8b8d05SMatthias Ringwald 16581b8b8d05SMatthias Ringwald // discard channel 16591b8b8d05SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 16601b8b8d05SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 16611b8b8d05SMatthias Ringwald break; 16621b8b8d05SMatthias Ringwald } 16631b8b8d05SMatthias Ringwald 16641b8b8d05SMatthias Ringwald // success 16651b8b8d05SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 16661b8b8d05SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 16671b8b8d05SMatthias Ringwald channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4); 16681b8b8d05SMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6); 16691b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 16701b8b8d05SMatthias Ringwald l2cap_emit_channel_opened(channel, result); 16711b8b8d05SMatthias Ringwald break; 16721b8b8d05SMatthias Ringwald 1673c48b2a2cSMatthias Ringwald default: 1674c48b2a2cSMatthias Ringwald // command unknown -> reject command 1675c48b2a2cSMatthias Ringwald return 0; 1676ccf076adS[email protected] } 1677c48b2a2cSMatthias Ringwald return 1; 1678c48b2a2cSMatthias Ringwald } 1679e7d0c9aaSMatthias Ringwald #endif 1680c48b2a2cSMatthias Ringwald 1681c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 1682c48b2a2cSMatthias Ringwald 1683c48b2a2cSMatthias Ringwald // Get Channel ID 1684c48b2a2cSMatthias Ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 1685c48b2a2cSMatthias Ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 1686c48b2a2cSMatthias Ringwald 1687c48b2a2cSMatthias Ringwald switch (channel_id) { 1688c48b2a2cSMatthias Ringwald 1689c48b2a2cSMatthias Ringwald case L2CAP_CID_SIGNALING: { 1690c48b2a2cSMatthias Ringwald uint16_t command_offset = 8; 1691c48b2a2cSMatthias Ringwald while (command_offset < size) { 1692c48b2a2cSMatthias Ringwald // handle signaling commands 1693c48b2a2cSMatthias Ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 1694c48b2a2cSMatthias Ringwald 1695c48b2a2cSMatthias Ringwald // increment command_offset 1696c48b2a2cSMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1697c48b2a2cSMatthias Ringwald } 1698c48b2a2cSMatthias Ringwald break; 1699c48b2a2cSMatthias Ringwald } 1700c48b2a2cSMatthias Ringwald 1701e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 1702e7d0c9aaSMatthias Ringwald case L2CAP_CID_SIGNALING_LE: { 1703e7d0c9aaSMatthias Ringwald uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 1704e7d0c9aaSMatthias Ringwald int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id); 1705c48b2a2cSMatthias Ringwald if (!valid){ 17061bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 1707ccf076adS[email protected] } 1708ccf076adS[email protected] break; 1709e7d0c9aaSMatthias Ringwald } 1710e7d0c9aaSMatthias Ringwald #endif 1711c48b2a2cSMatthias Ringwald 1712c48b2a2cSMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1713c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 1714c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1715ccf076adS[email protected] } 1716c48b2a2cSMatthias Ringwald break; 1717c48b2a2cSMatthias Ringwald 1718c48b2a2cSMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1719c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 1720c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1721c48b2a2cSMatthias Ringwald } 1722c48b2a2cSMatthias Ringwald break; 1723c48b2a2cSMatthias Ringwald 1724c48b2a2cSMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1725c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 1726c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1727c48b2a2cSMatthias Ringwald } 1728c48b2a2cSMatthias Ringwald break; 17291bbc0b23S[email protected] 17305652b5ffS[email protected] default: { 173100d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 17323d50b4baSMatthias Ringwald l2cap_channel_t * l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 1733d1fd2a88SMatthias Ringwald if (l2cap_channel) { 17343d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 173500d93d79Smatthias.ringwald } 17365652b5ffS[email protected] break; 17375652b5ffS[email protected] } 17385652b5ffS[email protected] } 173900d93d79Smatthias.ringwald 17401eb2563eS[email protected] l2cap_run(); 17412718e2e7Smatthias.ringwald } 174200d93d79Smatthias.ringwald 174315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 174427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 1745f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1746f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1747f62db1e3Smatthias.ringwald // discard channel 17489dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1749665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1750d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1751c8e4258aSmatthias.ringwald } 17521e6aba47Smatthias.ringwald 17538f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1754665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1755665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1756665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1757665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 17589d9bbc01Smatthias.ringwald if ( service->psm == psm){ 17599d9bbc01Smatthias.ringwald return service; 17609d9bbc01Smatthias.ringwald }; 17619d9bbc01Smatthias.ringwald } 17629d9bbc01Smatthias.ringwald return NULL; 17639d9bbc01Smatthias.ringwald } 17649d9bbc01Smatthias.ringwald 17657192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 17667192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 17677192e786SMatthias Ringwald } 17687192e786SMatthias Ringwald 1769e0abb8e7S[email protected] 1770be2053a6SMatthias 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){ 1771be2053a6SMatthias Ringwald 1772be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1773e0abb8e7S[email protected] 17744bb582b6Smatthias.ringwald // check for alread registered psm 17759d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1776277abc2cSmatthias.ringwald if (service) { 1777be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1778be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1779277abc2cSmatthias.ringwald } 17809d9bbc01Smatthias.ringwald 17814bb582b6Smatthias.ringwald // alloc structure 1782bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1783277abc2cSmatthias.ringwald if (!service) { 1784be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1785be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1786277abc2cSmatthias.ringwald } 17879d9bbc01Smatthias.ringwald 17889d9bbc01Smatthias.ringwald // fill in 17899d9bbc01Smatthias.ringwald service->psm = psm; 17909d9bbc01Smatthias.ringwald service->mtu = mtu; 179105ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1792df3354fcS[email protected] service->required_security_level = security_level; 17939d9bbc01Smatthias.ringwald 17949d9bbc01Smatthias.ringwald // add to services list 1795665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1796c0e866bfSmatthias.ringwald 1797c0e866bfSmatthias.ringwald // enable page scan 179815a95bd5SMatthias Ringwald gap_connectable_control(1); 17995842b6d9Smatthias.ringwald 1800be2053a6SMatthias Ringwald return 0; 18019d9bbc01Smatthias.ringwald } 18029d9bbc01Smatthias.ringwald 18037e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){ 1804e0abb8e7S[email protected] 1805e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1806e0abb8e7S[email protected] 18079d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 18087e8856ebSMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 1809665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1810d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1811c0e866bfSmatthias.ringwald 1812c0e866bfSmatthias.ringwald // disable page scan when no services registered 18137e8856ebSMatthias Ringwald if (btstack_linked_list_empty(&l2cap_services)) { 181415a95bd5SMatthias Ringwald gap_connectable_control(0); 18159d9bbc01Smatthias.ringwald } 18167e8856ebSMatthias Ringwald return 0; 18177e8856ebSMatthias Ringwald } 18189d9bbc01Smatthias.ringwald 18195652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 182005ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 18215628cf69SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 18225628cf69SMatthias Ringwald if (index < 0) return; 18235628cf69SMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 18245652b5ffS[email protected] } 18255652b5ffS[email protected] 1826a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 18277192e786SMatthias Ringwald 1828e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){ 1829e7d0c9aaSMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, le_psm); 18307192e786SMatthias Ringwald } 1831efedfb4cSMatthias Ringwald 1832da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){ 18337192e786SMatthias Ringwald 1834da144af5SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm); 18357192e786SMatthias Ringwald 18367192e786SMatthias Ringwald // check for alread registered psm 18377192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 18387192e786SMatthias Ringwald if (service) { 1839da144af5SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 18407192e786SMatthias Ringwald } 18417192e786SMatthias Ringwald 18427192e786SMatthias Ringwald // alloc structure 18437192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 18447192e786SMatthias Ringwald if (!service) { 18457192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 1846da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 18477192e786SMatthias Ringwald } 18487192e786SMatthias Ringwald 18497192e786SMatthias Ringwald // fill in 18507192e786SMatthias Ringwald service->psm = psm; 1851da144af5SMatthias Ringwald service->mtu = 0; 18527192e786SMatthias Ringwald service->packet_handler = packet_handler; 18537192e786SMatthias Ringwald service->required_security_level = security_level; 18547192e786SMatthias Ringwald 18557192e786SMatthias Ringwald // add to services list 1856665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 18577192e786SMatthias Ringwald 18587192e786SMatthias Ringwald // done 1859da144af5SMatthias Ringwald return 0; 18607192e786SMatthias Ringwald } 18617192e786SMatthias Ringwald 1862da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) { 18637192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 18647192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 18659367f9b0SMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 1866da144af5SMatthias Ringwald 1867665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 18687192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 1869da144af5SMatthias Ringwald return 0; 18707192e786SMatthias Ringwald } 1871da144af5SMatthias Ringwald 1872da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){ 1873e7d0c9aaSMatthias Ringwald // get channel 1874e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 1875e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 1876e7d0c9aaSMatthias Ringwald 1877e7d0c9aaSMatthias Ringwald // validate state 1878e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 1879e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1880e7d0c9aaSMatthias Ringwald } 1881e7d0c9aaSMatthias Ringwald 1882efedfb4cSMatthias Ringwald // set state accept connection 188323017473SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT; 188423017473SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 188523017473SMatthias Ringwald channel->local_mtu = mtu; 188623017473SMatthias Ringwald channel->credits_incoming = initial_credits; 1887e7d0c9aaSMatthias Ringwald 1888e7d0c9aaSMatthias Ringwald // go 1889e7d0c9aaSMatthias Ringwald l2cap_run(); 1890da144af5SMatthias Ringwald return 0; 1891da144af5SMatthias Ringwald } 1892da144af5SMatthias Ringwald 1893da144af5SMatthias Ringwald /** 1894da144af5SMatthias Ringwald * @brief Deny incoming LE Data Channel connection due to resource constraints 1895da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1896da144af5SMatthias Ringwald */ 1897da144af5SMatthias Ringwald 1898da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){ 1899e7d0c9aaSMatthias Ringwald // get channel 1900e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 1901e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 1902e7d0c9aaSMatthias Ringwald 1903e7d0c9aaSMatthias Ringwald // validate state 1904e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 1905e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1906e7d0c9aaSMatthias Ringwald } 1907e7d0c9aaSMatthias Ringwald 1908efedfb4cSMatthias Ringwald // set state decline connection 1909e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE; 1910e7d0c9aaSMatthias Ringwald channel->reason = 0x04; // no resources available 1911e7d0c9aaSMatthias Ringwald l2cap_run(); 1912da144af5SMatthias Ringwald return 0; 1913da144af5SMatthias Ringwald } 1914da144af5SMatthias Ringwald 1915da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type, 1916da144af5SMatthias Ringwald uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 1917efedfb4cSMatthias Ringwald uint16_t * out_local_cid) { 1918efedfb4cSMatthias Ringwald 1919da144af5SMatthias Ringwald log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 1920da144af5SMatthias Ringwald 19215cb87675SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, LEVEL_0); 1922da144af5SMatthias Ringwald if (!channel) { 1923da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1924da144af5SMatthias Ringwald } 1925e7d0c9aaSMatthias Ringwald log_info("l2cap_le_create_channel %p", channel); 1926da144af5SMatthias Ringwald 1927da144af5SMatthias Ringwald // store local_cid 1928da144af5SMatthias Ringwald if (out_local_cid){ 1929da144af5SMatthias Ringwald *out_local_cid = channel->local_cid; 1930da144af5SMatthias Ringwald } 1931da144af5SMatthias Ringwald 1932e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 1933e7d0c9aaSMatthias Ringwald 1934efedfb4cSMatthias Ringwald // add to connections list 1935efedfb4cSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1936efedfb4cSMatthias Ringwald 1937da144af5SMatthias Ringwald // check if hci connection is already usable 1938da144af5SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type); 1939efedfb4cSMatthias Ringwald 1940da144af5SMatthias Ringwald if (conn){ 1941da144af5SMatthias Ringwald log_info("l2cap_le_create_channel, hci connection already exists"); 19425cb87675SMatthias Ringwald l2cap_handle_le_connection_complete(channel, 0, conn->con_handle); 1943efedfb4cSMatthias Ringwald } else { 1944efedfb4cSMatthias Ringwald 1945efedfb4cSMatthias Ringwald // 1946efedfb4cSMatthias Ringwald // TODO: start timer 1947efedfb4cSMatthias Ringwald // .. 1948efedfb4cSMatthias Ringwald 1949efedfb4cSMatthias Ringwald // start connection 1950efedfb4cSMatthias Ringwald uint8_t res = gap_auto_connection_start(address_type, address); 195148af0c56SMatthias Ringwald if (res){ 1952efedfb4cSMatthias Ringwald // discard channel object 1953e7d0c9aaSMatthias Ringwald log_info("gap_auto_connection_start failed! 0x%02x", res); 1954e7d0c9aaSMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 1955efedfb4cSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1956efedfb4cSMatthias Ringwald return res; 1957da144af5SMatthias Ringwald } 1958efedfb4cSMatthias Ringwald } 1959da144af5SMatthias Ringwald return 0; 1960da144af5SMatthias Ringwald } 1961da144af5SMatthias Ringwald 1962da144af5SMatthias Ringwald /** 1963da144af5SMatthias Ringwald * @brief Provide credtis for LE Data Channel 1964da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1965da144af5SMatthias Ringwald * @param credits Number additional credits for peer 1966da144af5SMatthias Ringwald */ 1967da144af5SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t cid, uint16_t credits){ 1968e7d0c9aaSMatthias Ringwald // get channel 1969efedfb4cSMatthias Ringwald // bail if missing 1970efedfb4cSMatthias Ringwald // check state 1971efedfb4cSMatthias Ringwald // check incoming credits + credits <= 0xffff 1972efedfb4cSMatthias Ringwald // set credits_granted 1973efedfb4cSMatthias Ringwald // l2cap_le_run() 1974da144af5SMatthias Ringwald return 0; 1975da144af5SMatthias Ringwald } 1976da144af5SMatthias Ringwald 1977da144af5SMatthias Ringwald /** 1978da144af5SMatthias Ringwald * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 1979da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1980da144af5SMatthias Ringwald */ 1981da144af5SMatthias Ringwald int l2cap_le_can_send_now(uint16_t cid){ 1982efedfb4cSMatthias Ringwald // check if data can be sent via le at all 1983da144af5SMatthias Ringwald return 0; 1984da144af5SMatthias Ringwald } 1985da144af5SMatthias Ringwald 1986da144af5SMatthias Ringwald /** 1987da144af5SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 1988da144af5SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 1989da144af5SMatthias Ringwald * so packet handler should be ready to handle it 1990da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 1991da144af5SMatthias Ringwald */ 1992da144af5SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t cid){ 1993efedfb4cSMatthias Ringwald // same as for non-le 1994da144af5SMatthias Ringwald return 0; 1995da144af5SMatthias Ringwald } 1996da144af5SMatthias Ringwald 1997da144af5SMatthias Ringwald /** 1998da144af5SMatthias Ringwald * @brief Send data via LE Data Channel 1999da144af5SMatthias Ringwald * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 2000da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2001da144af5SMatthias Ringwald * @param data data to send 2002da144af5SMatthias Ringwald * @param size data size 2003da144af5SMatthias Ringwald */ 2004da144af5SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t cid, uint8_t * data, uint16_t size){ 2005efedfb4cSMatthias Ringwald // check if no outgoing data is pending 2006efedfb4cSMatthias Ringwald // store info 2007efedfb4cSMatthias Ringwald // l2cap_le_run() 2008da144af5SMatthias Ringwald return 0; 2009da144af5SMatthias Ringwald } 2010da144af5SMatthias Ringwald 2011da144af5SMatthias Ringwald /** 2012da144af5SMatthias Ringwald * @brief Disconnect from LE Data Channel 2013da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2014da144af5SMatthias Ringwald */ 2015da144af5SMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t cid) 2016da144af5SMatthias Ringwald { 2017efedfb4cSMatthias Ringwald // find channel 2018efedfb4cSMatthias Ringwald // check state 2019efedfb4cSMatthias Ringwald // set state SEND_DISCONNECT 2020da144af5SMatthias Ringwald return 0; 2021da144af5SMatthias Ringwald } 2022da144af5SMatthias Ringwald 20237f02f414SMatthias Ringwald #endif 2024