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" 50d3a9df87Smatthias.ringwald #include "btstack_memory.h" 5143625864Smatthias.ringwald 5243625864Smatthias.ringwald #include <stdarg.h> 5343625864Smatthias.ringwald #include <string.h> 5443625864Smatthias.ringwald 5543625864Smatthias.ringwald #include <stdio.h> 5643625864Smatthias.ringwald 574c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 584c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 594c744e21Smatthias.ringwald 6039bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 61e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 6239bda6d5Smatthias.ringwald 6300d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 6400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6800d93d79Smatthias.ringwald 695628cf69SMatthias Ringwald // internal table 705628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0 715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL 1 725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2 735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1) 745628cf69SMatthias Ringwald 7533c40538SMatthias Ringwald // prototypes 7633c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 7733c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 7833c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 7934e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel); 8033c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 8133c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 8233c40538SMatthias Ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 8333c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 8433c40538SMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ); 8533c40538SMatthias Ringwald 865628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel { 875628cf69SMatthias Ringwald btstack_packet_handler_t callback; 882125de09SMatthias Ringwald uint8_t waiting_for_can_send_now; 895628cf69SMatthias Ringwald } l2cap_fixed_channel_t; 905628cf69SMatthias Ringwald 915628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels; 925628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services; 935628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels; 945628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services; 9539bda6d5Smatthias.ringwald 9639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 972b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 982b83fb7dSmatthias.ringwald static int signaling_responses_pending; 992b83fb7dSmatthias.ringwald 10033c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp; 10133c40538SMatthias Ringwald 102fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 103fb37a842SMatthias Ringwald 10433c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler; 1055628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE]; 10639bda6d5Smatthias.ringwald 10734e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){ 10834e7e577SMatthias Ringwald switch (index){ 10934e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL: 11034e7e577SMatthias Ringwald return L2CAP_CID_ATTRIBUTE_PROTOCOL; 11134e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL: 11234e7e577SMatthias Ringwald return L2CAP_CID_SECURITY_MANAGER_PROTOCOL; 11334e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL: 11434e7e577SMatthias Ringwald return L2CAP_CID_CONNECTIONLESS_CHANNEL; 11534e7e577SMatthias Ringwald default: 11634e7e577SMatthias Ringwald return 0; 11734e7e577SMatthias Ringwald } 11834e7e577SMatthias Ringwald } 1195628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){ 1205628cf69SMatthias Ringwald switch (channel_id){ 1215628cf69SMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1225628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL; 1235628cf69SMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1245628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL; 1255628cf69SMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1265628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL; 1275628cf69SMatthias Ringwald default: 1285628cf69SMatthias Ringwald return -1; 1295628cf69SMatthias Ringwald } 1305628cf69SMatthias Ringwald } 13139bda6d5Smatthias.ringwald 13234e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){ 13334e7e577SMatthias Ringwald if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0; 13434e7e577SMatthias Ringwald return 1; 13534e7e577SMatthias Ringwald } 13634e7e577SMatthias Ringwald 13771de195eSMatthias Ringwald void l2cap_init(void){ 1382b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 139808a48abSmatthias.ringwald 140f5454fc6Smatthias.ringwald l2cap_channels = NULL; 141f5454fc6Smatthias.ringwald l2cap_services = NULL; 1427192e786SMatthias Ringwald l2cap_le_services = NULL; 1437192e786SMatthias Ringwald l2cap_le_channels = NULL; 144f5454fc6Smatthias.ringwald 14533c40538SMatthias Ringwald l2cap_event_packet_handler = NULL; 1465628cf69SMatthias Ringwald memset(fixed_channels, 0, sizeof(fixed_channels)); 147f5454fc6Smatthias.ringwald 148ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 149ac301f95S[email protected] 150fcadd0caSmatthias.ringwald // 1512718e2e7Smatthias.ringwald // register callback with HCI 152fcadd0caSmatthias.ringwald // 153d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &l2cap_hci_event_handler; 154fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 155fb37a842SMatthias Ringwald 156d9a7306aSMatthias Ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 157fb37a842SMatthias Ringwald 158c0e866bfSmatthias.ringwald hci_connectable_control(0); // no services yet 159fcadd0caSmatthias.ringwald } 160fcadd0caSmatthias.ringwald 161ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 16233c40538SMatthias Ringwald l2cap_event_packet_handler = handler; 1631e6aba47Smatthias.ringwald } 1641e6aba47Smatthias.ringwald 16517a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 16658de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 16758de5610Smatthias.ringwald } 16858de5610Smatthias.ringwald 16958de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 170c9dc710bS[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", 171e0abb8e7S[email protected] status, bd_addr_to_str(channel->address), channel->handle, channel->psm, 172c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 173c9dc710bS[email protected] uint8_t event[23]; 17458de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 17558de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 17658de5610Smatthias.ringwald event[2] = status; 177*724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 178f8fbdce0SMatthias Ringwald little_endian_store_16(event, 9, channel->handle); 179f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 180f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 181f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 182f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 183f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 184f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 18558de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18617a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 18733c40538SMatthias Ringwald 18833c40538SMatthias Ringwald // if channel opened successfully, also send can send now if possible 18933c40538SMatthias Ringwald if (status) return; 19033c40538SMatthias Ringwald if (hci_can_send_acl_packet_now(channel->handle)){ 19134e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 19233c40538SMatthias Ringwald } else { 19333c40538SMatthias Ringwald channel->waiting_for_can_send_now = 1; 19433c40538SMatthias Ringwald } 19558de5610Smatthias.ringwald } 19658de5610Smatthias.ringwald 19758de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 198e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 19958de5610Smatthias.ringwald uint8_t event[4]; 20058de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 20158de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 202f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 20358de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 20417a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 20558de5610Smatthias.ringwald } 20658de5610Smatthias.ringwald 20758de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 208e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 209e0abb8e7S[email protected] bd_addr_to_str(channel->address), channel->handle, channel->psm, channel->local_cid, channel->remote_cid); 21058de5610Smatthias.ringwald uint8_t event[16]; 21158de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 21258de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 213*724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 214f8fbdce0SMatthias Ringwald little_endian_store_16(event, 8, channel->handle); 215f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 216f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 217f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 21858de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21917a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 2200af41d30Smatthias.ringwald } 221808a48abSmatthias.ringwald 22234e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) { 22334e7e577SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel); 22433c40538SMatthias Ringwald uint8_t event[4]; 22533c40538SMatthias Ringwald event[0] = L2CAP_EVENT_CAN_SEND_NOW; 22633c40538SMatthias Ringwald event[1] = sizeof(event) - 2; 22734e7e577SMatthias Ringwald little_endian_store_16(event, 2, channel); 22833c40538SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 22934e7e577SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); 23033c40538SMatthias Ringwald } 23133c40538SMatthias Ringwald 2327f02f414SMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(uint16_t handle, uint16_t result){ 233ccf076adS[email protected] uint8_t event[6]; 234ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 235ccf076adS[email protected] event[1] = 4; 236f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, handle); 237f8fbdce0SMatthias Ringwald little_endian_store_16(event, 4, result); 238ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23933c40538SMatthias Ringwald if (!l2cap_event_packet_handler) return; 24033c40538SMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 241ccf076adS[email protected] } 242ccf076adS[email protected] 2437f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 244665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 245665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 246665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 247665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 248b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 249f62db1e3Smatthias.ringwald return channel; 250f62db1e3Smatthias.ringwald } 251f62db1e3Smatthias.ringwald } 252f62db1e3Smatthias.ringwald return NULL; 253f62db1e3Smatthias.ringwald } 254f62db1e3Smatthias.ringwald 2556b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2566b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2576b1fde37Smatthias.ringwald if (!channel) return 0; 25833c40538SMatthias Ringwald int can_send = hci_can_send_acl_packet_now(channel->handle); 25933c40538SMatthias Ringwald if (!can_send){ 26033c40538SMatthias Ringwald channel->waiting_for_can_send_now = 1; 26133c40538SMatthias Ringwald } 26233c40538SMatthias Ringwald return can_send; 2637856fb31S[email protected] } 2647856fb31S[email protected] 26583e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 26683e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 26783e7cdd9SMatthias Ringwald if (!channel) return 0; 26833c40538SMatthias Ringwald int can_send = hci_can_send_prepared_acl_packet_now(channel->handle); 26933c40538SMatthias Ringwald if (!can_send){ 27033c40538SMatthias Ringwald channel->waiting_for_can_send_now = 1; 27133c40538SMatthias Ringwald } 27233c40538SMatthias Ringwald return can_send; 27383e7cdd9SMatthias Ringwald } 27483e7cdd9SMatthias Ringwald 275adbe29e8SMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ 2762125de09SMatthias Ringwald int can_send = hci_can_send_acl_packet_now(handle); 2772125de09SMatthias Ringwald if (!can_send){ 2782125de09SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 2792125de09SMatthias Ringwald if (index >= 0){ 2802125de09SMatthias Ringwald fixed_channels[index].waiting_for_can_send_now = 1; 2812125de09SMatthias Ringwald } 2822125de09SMatthias Ringwald } 2832125de09SMatthias Ringwald return can_send; 2843cab4fcaS[email protected] } 2853cab4fcaS[email protected] 28696cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 28796cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 28896cbd662Smatthias.ringwald if (channel) { 28996cbd662Smatthias.ringwald return channel->remote_mtu; 29096cbd662Smatthias.ringwald } 29196cbd662Smatthias.ringwald return 0; 29296cbd662Smatthias.ringwald } 29396cbd662Smatthias.ringwald 294ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 295665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 296665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 297665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 298665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2995932bd7cS[email protected] if ( &channel->rtx == ts) { 3005932bd7cS[email protected] return channel; 3015932bd7cS[email protected] } 3025932bd7cS[email protected] } 3035932bd7cS[email protected] return NULL; 3045932bd7cS[email protected] } 3055932bd7cS[email protected] 306ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 3075932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 3085932bd7cS[email protected] if (!ts) return; 3095932bd7cS[email protected] 3105932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 3115932bd7cS[email protected] 3125932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 3135932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 3145932bd7cS[email protected] // notify client 3155932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 3165932bd7cS[email protected] 3175932bd7cS[email protected] // discard channel 3189dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 319665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 3205932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 3215932bd7cS[email protected] } 3225932bd7cS[email protected] 3235932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 3245932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 325528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 3265932bd7cS[email protected] } 3275932bd7cS[email protected] 3285932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 3295932bd7cS[email protected] l2cap_stop_rtx(channel); 330cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 331528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 332528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 333528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3345932bd7cS[email protected] } 3355932bd7cS[email protected] 3365932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3375932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3385932bd7cS[email protected] l2cap_stop_rtx(channel); 339528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 340528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 341528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3425932bd7cS[email protected] } 3435932bd7cS[email protected] 34471de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 345ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 346ac301f95S[email protected] } 347ac301f95S[email protected] 348df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 349ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 350df3354fcS[email protected] } 3515932bd7cS[email protected] 3527f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 353b1d43497Smatthias.ringwald 354a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3559da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 356b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 357b1d43497Smatthias.ringwald } 358b1d43497Smatthias.ringwald 3599da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3602a373862S[email protected] hci_reserve_packet_buffer(); 361facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 36258de5610Smatthias.ringwald va_list argptr; 36358de5610Smatthias.ringwald va_start(argptr, identifier); 36470efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 36558de5610Smatthias.ringwald va_end(argptr); 3669da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 367826f7347S[email protected] return hci_send_acl_packet_buffer(len); 36858de5610Smatthias.ringwald } 36958de5610Smatthias.ringwald 370a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3717f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 37270efece1S[email protected] 373a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3749da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 37570efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 37670efece1S[email protected] } 37770efece1S[email protected] 3789da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3792a373862S[email protected] hci_reserve_packet_buffer(); 380facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 38170efece1S[email protected] va_list argptr; 38270efece1S[email protected] va_start(argptr, identifier); 38370efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 38470efece1S[email protected] va_end(argptr); 3859da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 386826f7347S[email protected] return hci_send_acl_packet_buffer(len); 38770efece1S[email protected] } 38870efece1S[email protected] #endif 38970efece1S[email protected] 390b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 391facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 392b1d43497Smatthias.ringwald } 3936218e6f1Smatthias.ringwald 3946b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3956b4af23dS[email protected] return hci_reserve_packet_buffer(); 3966b4af23dS[email protected] } 3976b4af23dS[email protected] 39868a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 39968a0fcf7S[email protected] hci_release_packet_buffer(); 40068a0fcf7S[email protected] } 40168a0fcf7S[email protected] 40268a0fcf7S[email protected] 403b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 404b1d43497Smatthias.ringwald 405c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 406c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 407c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 408c8b9416aS[email protected] } 409c8b9416aS[email protected] 41058de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 411b1d43497Smatthias.ringwald if (!channel) { 4129da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 413b1d43497Smatthias.ringwald return -1; // TODO: define error 4146218e6f1Smatthias.ringwald } 4156218e6f1Smatthias.ringwald 416a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(channel->handle)){ 4179da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 418a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 419a35252c8S[email protected] } 420a35252c8S[email protected] 4213aff022fSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->handle); 422b1d43497Smatthias.ringwald 423facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 424b1d43497Smatthias.ringwald 425e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 426e9772277S[email protected] 427e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 428f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 0, channel->handle | (pb << 12) | (0 << 14)); 42958de5610Smatthias.ringwald // 2 - ACL length 430f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 43158de5610Smatthias.ringwald // 4 - L2CAP packet length 432f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 43358de5610Smatthias.ringwald // 6 - L2CAP channel DEST 434f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, channel->remote_cid); 43558de5610Smatthias.ringwald // send 436826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 43791b99603Smatthias.ringwald 4386218e6f1Smatthias.ringwald return err; 43958de5610Smatthias.ringwald } 44058de5610Smatthias.ringwald 4412149f12eSmatthias.ringwald int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 4422149f12eSmatthias.ringwald 443c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 444c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4452149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4462149f12eSmatthias.ringwald } 4472149f12eSmatthias.ringwald 448a35252c8S[email protected] if (!hci_can_send_prepared_acl_packet_now(handle)){ 4499da54300S[email protected] log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", handle, cid); 450c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 451c8b9416aS[email protected] } 452c8b9416aS[email protected] 4539da54300S[email protected] log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", handle, cid); 4542149f12eSmatthias.ringwald 455facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4562149f12eSmatthias.ringwald 457e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 458e9772277S[email protected] 459e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 460f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14)); 4612149f12eSmatthias.ringwald // 2 - ACL length 462f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 4632149f12eSmatthias.ringwald // 4 - L2CAP packet length 464f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 4652149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 466f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, cid); 4672149f12eSmatthias.ringwald // send 468826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4692149f12eSmatthias.ringwald 4702149f12eSmatthias.ringwald return err; 4712149f12eSmatthias.ringwald } 4722149f12eSmatthias.ringwald 473ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 474b1d43497Smatthias.ringwald 475a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 476a35252c8S[email protected] if (!channel) { 477ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 478a35252c8S[email protected] return -1; // TODO: define error 479a35252c8S[email protected] } 480a35252c8S[email protected] 481f0efaa57S[email protected] if (len > channel->remote_mtu){ 482ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 483f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 484f0efaa57S[email protected] } 485f0efaa57S[email protected] 486a35252c8S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)){ 487ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 488b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 489b1d43497Smatthias.ringwald } 490b1d43497Smatthias.ringwald 4912a373862S[email protected] hci_reserve_packet_buffer(); 492facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 493b1d43497Smatthias.ringwald 494b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 495b1d43497Smatthias.ringwald 496b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 497b1d43497Smatthias.ringwald } 498b1d43497Smatthias.ringwald 4992149f12eSmatthias.ringwald int l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t *data, uint16_t len){ 5002149f12eSmatthias.ringwald 501a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 502ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 5032149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 5042149f12eSmatthias.ringwald } 5052149f12eSmatthias.ringwald 5062a373862S[email protected] hci_reserve_packet_buffer(); 507facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 5082149f12eSmatthias.ringwald 5092149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 5102149f12eSmatthias.ringwald 5112149f12eSmatthias.ringwald return l2cap_send_prepared_connectionless(handle, cid, len); 5122149f12eSmatthias.ringwald } 5132149f12eSmatthias.ringwald 5140e37e417S[email protected] int l2cap_send_echo_request(uint16_t handle, uint8_t *data, uint16_t len){ 5150e37e417S[email protected] return l2cap_send_signaling_packet(handle, ECHO_REQUEST, 0x77, len, data); 5160e37e417S[email protected] } 5170e37e417S[email protected] 51828ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 51928ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 52028ca2b46S[email protected] } 52128ca2b46S[email protected] 52228ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 52328ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 52428ca2b46S[email protected] } 52528ca2b46S[email protected] 52628ca2b46S[email protected] 527b1d43497Smatthias.ringwald 5288158c421Smatthias.ringwald // MARK: L2CAP_RUN 5292cd0be45Smatthias.ringwald // process outstanding signaling tasks 5307f02f414SMatthias Ringwald static void l2cap_run(void){ 5312b83fb7dSmatthias.ringwald 53222c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 53322c29ab4SMatthias Ringwald 5342b83fb7dSmatthias.ringwald // check pending signaling responses 5352b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5362b83fb7dSmatthias.ringwald 5372b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 538a35252c8S[email protected] 539a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 540a35252c8S[email protected] 5412b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5422b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 54363a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 544f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5452b83fb7dSmatthias.ringwald 546f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 547f53da564S[email protected] signaling_responses_pending--; 548f53da564S[email protected] int i; 549f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 550f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 551f53da564S[email protected] } 552f53da564S[email protected] 553f53da564S[email protected] switch (response_code){ 5542b360848Smatthias.ringwald case CONNECTION_REQUEST: 5552b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5562bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5574d816277S[email protected] if (result == 0x0003){ 5582bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5594d816277S[email protected] } 5602b360848Smatthias.ringwald break; 5612b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5622b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5632b83fb7dSmatthias.ringwald break; 5642b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5653b0484b3S[email protected] switch (infoType){ 5663b0484b3S[email protected] case 1: { // Connectionless MTU 5673b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5683b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5693b0484b3S[email protected] break; 5703b0484b3S[email protected] } 5713b0484b3S[email protected] case 2: { // Extended Features Supported 572462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 573462e630dS[email protected] uint32_t features = 0x280; 5743b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5753b0484b3S[email protected] break; 5763b0484b3S[email protected] } 5773b0484b3S[email protected] case 3: { // Fixed Channels Supported 5783b0484b3S[email protected] uint8_t map[8]; 5793b0484b3S[email protected] memset(map, 0, 8); 580462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5813b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5823b0484b3S[email protected] break; 5833b0484b3S[email protected] } 5843b0484b3S[email protected] default: 5852b83fb7dSmatthias.ringwald // all other types are not supported 5862b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5873b0484b3S[email protected] break; 5882b83fb7dSmatthias.ringwald } 5892b83fb7dSmatthias.ringwald break; 59063a7246aSmatthias.ringwald case COMMAND_REJECT: 5915ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 592a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 59370efece1S[email protected] case COMMAND_REJECT_LE: 59470efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 59563a7246aSmatthias.ringwald break; 59670efece1S[email protected] #endif 5972b83fb7dSmatthias.ringwald default: 5982b83fb7dSmatthias.ringwald // should not happen 5992b83fb7dSmatthias.ringwald break; 6002b83fb7dSmatthias.ringwald } 6012b83fb7dSmatthias.ringwald } 6022b83fb7dSmatthias.ringwald 603ae280e73Smatthias.ringwald uint8_t config_options[4]; 604665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 605665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 606665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 607baf94f06S[email protected] 608665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 60922c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 6102cd0be45Smatthias.ringwald switch (channel->state){ 6112cd0be45Smatthias.ringwald 612df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 613ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 614baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 615a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 616ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 617a00031e2S[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 618ad671560S[email protected] } 619ad671560S[email protected] break; 620ad671560S[email protected] 62102b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 622baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 62364472d52Smatthias.ringwald // send connection request - set state first 62464472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 62502b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6268f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 62702b22dc4Smatthias.ringwald break; 62802b22dc4Smatthias.ringwald 629e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 630baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 63122c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 6321eb2563eS[email protected] l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 633e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6349dcb2fb2S[email protected] l2cap_stop_rtx(channel); 635665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 636d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 637e7ff783cSmatthias.ringwald break; 638e7ff783cSmatthias.ringwald 639552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 640baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 641fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 64228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 6432a544672Smatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 644552d92a1Smatthias.ringwald break; 645552d92a1Smatthias.ringwald 6466fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 647baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 6486fdcc387Smatthias.ringwald // success, start l2cap handshake 649b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6506fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 6512a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6525932bd7cS[email protected] l2cap_start_rtx(channel); 6536fdcc387Smatthias.ringwald break; 6546fdcc387Smatthias.ringwald 655fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 656baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 65773cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 65863a7246aSmatthias.ringwald uint16_t flags = 0; 65928ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 66063a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 66163a7246aSmatthias.ringwald flags = 1; 66263a7246aSmatthias.ringwald } else { 66328ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 66463a7246aSmatthias.ringwald } 66563a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 66663a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 66763a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 66863a7246aSmatthias.ringwald config_options[0] = 1; // MTU 66963a7246aSmatthias.ringwald config_options[1] = 2; // len param 670f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 67163a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 67263a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 67363a7246aSmatthias.ringwald } else { 67463a7246aSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 67563a7246aSmatthias.ringwald } 67663a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 677fa8473a4Smatthias.ringwald } 67873cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 67928ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 68028ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 681b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 682ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 683ae280e73Smatthias.ringwald config_options[1] = 2; // len param 684f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 685b1988dceSmatthias.ringwald l2cap_send_signaling_packet(channel->handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6865932bd7cS[email protected] l2cap_start_rtx(channel); 687fa8473a4Smatthias.ringwald } 688fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 689552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 690552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 691fa8473a4Smatthias.ringwald } 692552d92a1Smatthias.ringwald break; 693552d92a1Smatthias.ringwald 694e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 695baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 69622c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 697b1988dceSmatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6985932bd7cS[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 :) 699756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 700e7ff783cSmatthias.ringwald break; 701e7ff783cSmatthias.ringwald 702e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 703baf94f06S[email protected] if (!hci_can_send_acl_packet_now(channel->handle)) break; 704b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 7052cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 7062a544672Smatthias.ringwald l2cap_send_signaling_packet( channel->handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 7072cd0be45Smatthias.ringwald break; 7082cd0be45Smatthias.ringwald default: 7092cd0be45Smatthias.ringwald break; 7102cd0be45Smatthias.ringwald } 7112cd0be45Smatthias.ringwald } 712da886c03S[email protected] 713a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 714da886c03S[email protected] // send l2cap con paramter update if necessary 715da886c03S[email protected] hci_connections_get_iterator(&it); 716665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 717665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 7185d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 719b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 720da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 721b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 722b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 723b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 724b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 725b68d7bc3SMatthias Ringwald break; 726da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 727b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 728b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 729da886c03S[email protected] break; 730da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 731b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 732b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 733da886c03S[email protected] break; 734da886c03S[email protected] default: 735da886c03S[email protected] break; 736da886c03S[email protected] } 737da886c03S[email protected] } 7384d7157c3S[email protected] #endif 739da886c03S[email protected] 74022c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 7412cd0be45Smatthias.ringwald } 7422cd0be45Smatthias.ringwald 7434aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 7444ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 745fa8c92f6Smatthias.ringwald } 746fa8c92f6Smatthias.ringwald 74771de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 7484ff786cfS[email protected] return l2cap_max_mtu(); 749e5e1518dS[email protected] } 750e5e1518dS[email protected] 7512df5dadcS[email protected] static void l2cap_handle_connection_complete(uint16_t handle, l2cap_channel_t * channel){ 7522df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 7535533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 7542df5dadcS[email protected] // success, start l2cap handshake 7552df5dadcS[email protected] channel->handle = handle; 7562df5dadcS[email protected] // check remote SSP feature first 7572df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 7582df5dadcS[email protected] } 7592df5dadcS[email protected] } 7602df5dadcS[email protected] 7612df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 7622df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 7632df5dadcS[email protected] 7642df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 765ac301f95S[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)); 7662df5dadcS[email protected] if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 7672df5dadcS[email protected] // request security level 2 7682df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 7691429b2d6S[email protected] gap_request_security_level(channel->handle, LEVEL_2); 7702df5dadcS[email protected] return; 7712df5dadcS[email protected] } 7722df5dadcS[email protected] // fine, go ahead 7732df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7742df5dadcS[email protected] } 7752df5dadcS[email protected] 7769077cb15SMatthias Ringwald /** 7779077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 7789077cb15SMatthias Ringwald * @param packet_handler 7799077cb15SMatthias Ringwald * @param address 7809077cb15SMatthias Ringwald * @param psm 7819077cb15SMatthias Ringwald * @param mtu 7829077cb15SMatthias Ringwald * @param local_cid 7839077cb15SMatthias Ringwald */ 7849077cb15SMatthias Ringwald uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid){ 7859077cb15SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 7869077cb15SMatthias Ringwald 7879077cb15SMatthias Ringwald // alloc structure 7889077cb15SMatthias Ringwald l2cap_channel_t * chan = btstack_memory_l2cap_channel_get(); 7899077cb15SMatthias Ringwald if (!chan) { 7909077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 7919077cb15SMatthias Ringwald } 7929077cb15SMatthias Ringwald 7939077cb15SMatthias Ringwald // Init memory (make valgrind happy) 7949077cb15SMatthias Ringwald memset(chan, 0, sizeof(l2cap_channel_t)); 7959077cb15SMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 7969077cb15SMatthias Ringwald if (mtu > l2cap_max_mtu()) { 7979077cb15SMatthias Ringwald mtu = l2cap_max_mtu(); 7989077cb15SMatthias Ringwald } 7999077cb15SMatthias Ringwald 8009077cb15SMatthias Ringwald // fill in 801058e3d6bSMatthias Ringwald bd_addr_copy(chan->address, address); 8029077cb15SMatthias Ringwald chan->psm = psm; 8039077cb15SMatthias Ringwald chan->handle = 0; 8049077cb15SMatthias Ringwald chan->packet_handler = channel_packet_handler; 8059077cb15SMatthias Ringwald chan->remote_mtu = L2CAP_MINIMAL_MTU; 8069077cb15SMatthias Ringwald chan->local_mtu = mtu; 8079077cb15SMatthias Ringwald chan->local_cid = l2cap_next_local_cid(); 8089077cb15SMatthias Ringwald 8099077cb15SMatthias Ringwald // set initial state 8109077cb15SMatthias Ringwald chan->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 8119077cb15SMatthias Ringwald chan->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 8129077cb15SMatthias Ringwald chan->remote_sig_id = L2CAP_SIG_ID_INVALID; 8139077cb15SMatthias Ringwald chan->local_sig_id = L2CAP_SIG_ID_INVALID; 8149077cb15SMatthias Ringwald chan->required_security_level = LEVEL_0; 8159077cb15SMatthias Ringwald 8169077cb15SMatthias Ringwald // add to connections list 817665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) chan); 8189077cb15SMatthias Ringwald 8199077cb15SMatthias Ringwald // store local_cid 8209077cb15SMatthias Ringwald if (out_local_cid){ 8219077cb15SMatthias Ringwald *out_local_cid = chan->local_cid; 8229077cb15SMatthias Ringwald } 8239077cb15SMatthias Ringwald 8249077cb15SMatthias Ringwald // check if hci connection is already usable 8259077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 8269077cb15SMatthias Ringwald if (conn){ 827add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 8289077cb15SMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, chan); 8299077cb15SMatthias Ringwald // check if remote supported fearures are already received 8309077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 8319077cb15SMatthias Ringwald l2cap_handle_remote_supported_features_received(chan); 8329077cb15SMatthias Ringwald } 8339077cb15SMatthias Ringwald } 8349077cb15SMatthias Ringwald 8359077cb15SMatthias Ringwald l2cap_run(); 8369077cb15SMatthias Ringwald 8379077cb15SMatthias Ringwald return 0; 8389077cb15SMatthias Ringwald } 8399077cb15SMatthias Ringwald 840ce8f182eSMatthias Ringwald void 841ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 842e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 843b35f641cSmatthias.ringwald // find channel for local_cid 844b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 845f62db1e3Smatthias.ringwald if (channel) { 846e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 847f62db1e3Smatthias.ringwald } 8482cd0be45Smatthias.ringwald // process 8492cd0be45Smatthias.ringwald l2cap_run(); 85043625864Smatthias.ringwald } 8511e6aba47Smatthias.ringwald 852afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 853665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 854665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 855665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 856665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 857058e3d6bSMatthias Ringwald if ( bd_addr_cmp( channel->address, address) != 0) continue; 858c22aecc9S[email protected] // channel for this address found 859c22aecc9S[email protected] switch (channel->state){ 860c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 861c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 862afde0c52Smatthias.ringwald // failure, forward error code 863afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 864afde0c52Smatthias.ringwald // discard channel 8659dcb2fb2S[email protected] l2cap_stop_rtx(channel); 866665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 867d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 868c22aecc9S[email protected] break; 869c22aecc9S[email protected] default: 870c22aecc9S[email protected] break; 871afde0c52Smatthias.ringwald } 872afde0c52Smatthias.ringwald } 873afde0c52Smatthias.ringwald } 874afde0c52Smatthias.ringwald 875afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 876665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 877665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 878665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 879665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 880058e3d6bSMatthias Ringwald if ( ! bd_addr_cmp( channel->address, address) ){ 8812df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 882afde0c52Smatthias.ringwald } 883afde0c52Smatthias.ringwald } 8846fdcc387Smatthias.ringwald // process 8856fdcc387Smatthias.ringwald l2cap_run(); 886afde0c52Smatthias.ringwald } 887b448a0e7Smatthias.ringwald 88833c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){ 88933c40538SMatthias Ringwald btstack_linked_list_iterator_t it; 89033c40538SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 89133c40538SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 89233c40538SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 89333c40538SMatthias Ringwald if (!channel->waiting_for_can_send_now) continue; 89433c40538SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->handle)) continue; 89533c40538SMatthias Ringwald channel->waiting_for_can_send_now = 0; 89634e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 89733c40538SMatthias Ringwald } 8982125de09SMatthias Ringwald 8992125de09SMatthias Ringwald int i; 9002125de09SMatthias Ringwald for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){ 901773c4106SMatthias Ringwald if (!fixed_channels[i].callback) continue; 9022125de09SMatthias Ringwald if (!fixed_channels[i].waiting_for_can_send_now) continue; 90334e7e577SMatthias Ringwald int can_send; 90434e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 90534e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 90634e7e577SMatthias Ringwald } else { 90734e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 90834e7e577SMatthias Ringwald } 90934e7e577SMatthias Ringwald if (!can_send) continue; 91034e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 91134e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 9122125de09SMatthias Ringwald } 91333c40538SMatthias Ringwald } 91433c40538SMatthias Ringwald 915d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 916afde0c52Smatthias.ringwald 917afde0c52Smatthias.ringwald bd_addr_t address; 918afde0c52Smatthias.ringwald hci_con_handle_t handle; 919665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 9202d00edd4Smatthias.ringwald int hci_con_used; 921afde0c52Smatthias.ringwald 922afde0c52Smatthias.ringwald switch(packet[0]){ 923afde0c52Smatthias.ringwald 924afde0c52Smatthias.ringwald // handle connection complete events 925afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 926*724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 927afde0c52Smatthias.ringwald if (packet[2] == 0){ 928f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 929afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 930afde0c52Smatthias.ringwald } else { 931afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 932afde0c52Smatthias.ringwald } 933afde0c52Smatthias.ringwald break; 934afde0c52Smatthias.ringwald 935afde0c52Smatthias.ringwald // handle successful create connection cancel command 936afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 937afde0c52Smatthias.ringwald if ( COMMAND_COMPLETE_EVENT(packet, hci_create_connection_cancel) ) { 938afde0c52Smatthias.ringwald if (packet[5] == 0){ 939*724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 940afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 941afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 94203cfbabcSmatthias.ringwald } 9431e6aba47Smatthias.ringwald } 94439d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 94539d59809Smatthias.ringwald break; 94639d59809Smatthias.ringwald 94739d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 94839d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 949afde0c52Smatthias.ringwald break; 95027a923d0Smatthias.ringwald 9511e6aba47Smatthias.ringwald // handle disconnection complete events 952afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 953c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 954f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 955665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 956665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 957665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 958c22aecc9S[email protected] if (channel->handle != handle) continue; 95915ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 9609dcb2fb2S[email protected] l2cap_stop_rtx(channel); 961665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 962d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 96327a923d0Smatthias.ringwald } 964afde0c52Smatthias.ringwald break; 965fcadd0caSmatthias.ringwald 96633c40538SMatthias Ringwald // Notify channel packet handler if they can send now 96733c40538SMatthias Ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 9686218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 96902b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 97033c40538SMatthias Ringwald l2cap_notify_channel_can_send(); 9716218e6f1Smatthias.ringwald break; 9726218e6f1Smatthias.ringwald 973ee091cf1Smatthias.ringwald // HCI Connection Timeouts 974afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 975f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 976bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 97780ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 9782d00edd4Smatthias.ringwald hci_con_used = 0; 979665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 980665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 981665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 982c22aecc9S[email protected] if (channel->handle != handle) continue; 9832d00edd4Smatthias.ringwald hci_con_used = 1; 984c22aecc9S[email protected] break; 985ee091cf1Smatthias.ringwald } 9862d00edd4Smatthias.ringwald if (hci_con_used) break; 987d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 9889edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 989afde0c52Smatthias.ringwald break; 990ee091cf1Smatthias.ringwald 991df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 992f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 993665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 994665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 995665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 996df3354fcS[email protected] if (channel->handle != handle) continue; 9972df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 998df3354fcS[email protected] break; 999df3354fcS[email protected] } 1000c22aecc9S[email protected] break; 1001df3354fcS[email protected] 10025611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1003f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1004bd63148eS[email protected] log_info("l2cap - security level update"); 1005665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1006665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1007665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1008f85a9399S[email protected] if (channel->handle != handle) continue; 10095533f01eS[email protected] 1010bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1011bd63148eS[email protected] 1012e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 10135533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 10145533f01eS[email protected] 1015df3354fcS[email protected] switch (channel->state){ 1016df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 10175533f01eS[email protected] if (actual_level >= required_level){ 1018f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1019f85a9399S[email protected] l2cap_emit_connection_request(channel); 10201eb2563eS[email protected] } else { 1021775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 10221eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 10231eb2563eS[email protected] } 1024df3354fcS[email protected] break; 1025df3354fcS[email protected] 1026df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 10275533f01eS[email protected] if (actual_level >= required_level){ 1028df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1029df3354fcS[email protected] } else { 1030df3354fcS[email protected] // disconnnect, authentication not good enough 1031df3354fcS[email protected] hci_disconnect_security_block(handle); 1032df3354fcS[email protected] } 1033df3354fcS[email protected] break; 1034df3354fcS[email protected] 1035df3354fcS[email protected] default: 1036df3354fcS[email protected] break; 1037df3354fcS[email protected] } 1038f85a9399S[email protected] } 1039f85a9399S[email protected] break; 1040f85a9399S[email protected] 1041afde0c52Smatthias.ringwald default: 1042afde0c52Smatthias.ringwald break; 1043afde0c52Smatthias.ringwald } 1044afde0c52Smatthias.ringwald 1045bd63148eS[email protected] l2cap_run(); 10461e6aba47Smatthias.ringwald } 10471e6aba47Smatthias.ringwald 1048afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1049b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1050e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1051e7ff783cSmatthias.ringwald l2cap_run(); 105284836b65Smatthias.ringwald } 105384836b65Smatthias.ringwald 10542b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 10554cf56b4aSmatthias.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." 10562b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 10572b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 10582b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 10592b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 10602b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 10612b360848Smatthias.ringwald signaling_responses_pending++; 10622b360848Smatthias.ringwald l2cap_run(); 10632b360848Smatthias.ringwald } 10642b360848Smatthias.ringwald } 10652b360848Smatthias.ringwald 1066b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1067645658c9Smatthias.ringwald 10689da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1069645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1070645658c9Smatthias.ringwald if (!service) { 1071645658c9Smatthias.ringwald // 0x0002 PSM not supported 10722b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1073645658c9Smatthias.ringwald return; 1074645658c9Smatthias.ringwald } 1075645658c9Smatthias.ringwald 10765061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1077645658c9Smatthias.ringwald if (!hci_connection) { 10782b360848Smatthias.ringwald // 10799da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1080645658c9Smatthias.ringwald return; 1081645658c9Smatthias.ringwald } 10822bd8b7e7S[email protected] 1083645658c9Smatthias.ringwald // alloc structure 10849da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1085bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 10862b360848Smatthias.ringwald if (!channel){ 10872b360848Smatthias.ringwald // 0x0004 No resources available 10882b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 10892b360848Smatthias.ringwald return; 10902b360848Smatthias.ringwald } 1091c523d53dS[email protected] // Init memory (make valgrind happy) 1092c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1093645658c9Smatthias.ringwald // fill in 1094058e3d6bSMatthias Ringwald bd_addr_copy(channel->address, hci_connection->address); 1095169f8b28Smatthias.ringwald channel->psm = psm; 1096169f8b28Smatthias.ringwald channel->handle = handle; 1097f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1098b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1099b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1100fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 11010a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1102b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1103df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1104645658c9Smatthias.ringwald 1105f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 11062985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 11072985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 11089775e25bSmatthias.ringwald } 11099775e25bSmatthias.ringwald 1110645658c9Smatthias.ringwald // set initial state 1111df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1112ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1113e405ae81Smatthias.ringwald 1114645658c9Smatthias.ringwald // add to connections list 1115665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1116645658c9Smatthias.ringwald 1117f85a9399S[email protected] // assert security requirements 11181eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1119e405ae81Smatthias.ringwald } 1120645658c9Smatthias.ringwald 1121ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1122e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1123b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1124e405ae81Smatthias.ringwald if (!channel) { 1125ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1126e405ae81Smatthias.ringwald return; 1127e405ae81Smatthias.ringwald } 1128e405ae81Smatthias.ringwald 1129552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1130e405ae81Smatthias.ringwald 1131552d92a1Smatthias.ringwald // process 1132552d92a1Smatthias.ringwald l2cap_run(); 1133e405ae81Smatthias.ringwald } 1134645658c9Smatthias.ringwald 1135ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){ 1136e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1137b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1138e405ae81Smatthias.ringwald if (!channel) { 1139ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1140e405ae81Smatthias.ringwald return; 1141e405ae81Smatthias.ringwald } 1142e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1143e7ff783cSmatthias.ringwald channel->reason = reason; 1144e7ff783cSmatthias.ringwald l2cap_run(); 1145645658c9Smatthias.ringwald } 1146645658c9Smatthias.ringwald 11477f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1148b1988dceSmatthias.ringwald 1149b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1150b1988dceSmatthias.ringwald 1151f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 115263a7246aSmatthias.ringwald if (flags & 1) { 115363a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 115463a7246aSmatthias.ringwald } 115563a7246aSmatthias.ringwald 11562784b77dSmatthias.ringwald // accept the other's configuration options 1157f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 11583de7c0caSmatthias.ringwald uint16_t pos = 8; 11593de7c0caSmatthias.ringwald while (pos < end_pos){ 116063a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 116163a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 116263a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 116363a7246aSmatthias.ringwald pos++; 11641dc511deSmatthias.ringwald uint8_t length = command[pos++]; 11651dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 116663a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1167f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 11689da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 116963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 117063a7246aSmatthias.ringwald } 11710fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 11720fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1173f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 11740fe7a9d0S[email protected] } 117563a7246aSmatthias.ringwald // check for unknown options 117663a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1177c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 117863a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 11791dc511deSmatthias.ringwald } 11801dc511deSmatthias.ringwald pos += length; 11811dc511deSmatthias.ringwald } 11822784b77dSmatthias.ringwald } 11832784b77dSmatthias.ringwald 1184fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 11859da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 118673cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 118773cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1188019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1189019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1190fa8473a4Smatthias.ringwald return 1; 1191fa8473a4Smatthias.ringwald } 1192fa8473a4Smatthias.ringwald 1193fa8473a4Smatthias.ringwald 11947f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 11951e6aba47Smatthias.ringwald 119600d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 119700d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 119838e5900eSmatthias.ringwald uint16_t result = 0; 11991e6aba47Smatthias.ringwald 12009da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1201b35f641cSmatthias.ringwald 12029a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 12039a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 12049a011532Smatthias.ringwald switch (channel->state){ 1205fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 12069a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 12072b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 12089a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 12099a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 12109a011532Smatthias.ringwald break; 12119a011532Smatthias.ringwald 12129a011532Smatthias.ringwald default: 12139a011532Smatthias.ringwald // ignore in other states 12149a011532Smatthias.ringwald break; 12159a011532Smatthias.ringwald } 12169a011532Smatthias.ringwald return; 12179a011532Smatthias.ringwald } 12189a011532Smatthias.ringwald 121956081214Smatthias.ringwald // @STATEMACHINE(l2cap) 12201e6aba47Smatthias.ringwald switch (channel->state) { 12211e6aba47Smatthias.ringwald 12221e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 12231e6aba47Smatthias.ringwald switch (code){ 12241e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 12255932bd7cS[email protected] l2cap_stop_rtx(channel); 1226f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 122738e5900eSmatthias.ringwald switch (result) { 122838e5900eSmatthias.ringwald case 0: 1229169f8b28Smatthias.ringwald // successful connection 1230f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1231fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 123228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 123338e5900eSmatthias.ringwald break; 123438e5900eSmatthias.ringwald case 1: 12355932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 12365932bd7cS[email protected] l2cap_start_ertx(channel); 123738e5900eSmatthias.ringwald break; 123838e5900eSmatthias.ringwald default: 1239eb920dbeSmatthias.ringwald // channel closed 1240eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1241f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 124238e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1243eb920dbeSmatthias.ringwald 1244eb920dbeSmatthias.ringwald // drop link key if security block 1245eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 12462e77e513S[email protected] hci_drop_link_key_for_bd_addr(channel->address); 1247eb920dbeSmatthias.ringwald } 1248eb920dbeSmatthias.ringwald 1249eb920dbeSmatthias.ringwald // discard channel 1250665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1251d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 125238e5900eSmatthias.ringwald break; 12531e6aba47Smatthias.ringwald } 12541e6aba47Smatthias.ringwald break; 125538e5900eSmatthias.ringwald 125638e5900eSmatthias.ringwald default: 12571e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 125838e5900eSmatthias.ringwald break; 12591e6aba47Smatthias.ringwald } 12601e6aba47Smatthias.ringwald break; 12611e6aba47Smatthias.ringwald 1262fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1263f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1264ae280e73Smatthias.ringwald switch (code) { 1265ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 126628ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1267ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 126863a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 126963a7246aSmatthias.ringwald // only done if continuation not set 127063a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 127163a7246aSmatthias.ringwald } 1272ae280e73Smatthias.ringwald break; 12731e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 12745932bd7cS[email protected] l2cap_stop_rtx(channel); 12755932bd7cS[email protected] switch (result){ 12765932bd7cS[email protected] case 0: // success 12775932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 12785932bd7cS[email protected] break; 12795932bd7cS[email protected] case 4: // pending 12805932bd7cS[email protected] l2cap_start_ertx(channel); 12815932bd7cS[email protected] break; 12825932bd7cS[email protected] default: 1283fe9d8984S[email protected] // retry on negative result 1284fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1285fe9d8984S[email protected] break; 1286fe9d8984S[email protected] } 12875a67bd4aSmatthias.ringwald break; 12885a67bd4aSmatthias.ringwald default: 12895a67bd4aSmatthias.ringwald break; 12901e6aba47Smatthias.ringwald } 1291fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1292fa8473a4Smatthias.ringwald // for open: 12935a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1294fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1295c8e4258aSmatthias.ringwald } 1296c8e4258aSmatthias.ringwald break; 1297f62db1e3Smatthias.ringwald 1298f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1299f62db1e3Smatthias.ringwald switch (code) { 1300f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 130127a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 130227a923d0Smatthias.ringwald break; 13035a67bd4aSmatthias.ringwald default: 13045a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 13055a67bd4aSmatthias.ringwald break; 130627a923d0Smatthias.ringwald } 130727a923d0Smatthias.ringwald break; 130884836b65Smatthias.ringwald 130984836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 131084836b65Smatthias.ringwald // @TODO handle incoming requests 131184836b65Smatthias.ringwald break; 131284836b65Smatthias.ringwald 131384836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 131484836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 131584836b65Smatthias.ringwald break; 131610642e45Smatthias.ringwald default: 131710642e45Smatthias.ringwald break; 131827a923d0Smatthias.ringwald } 13199da54300S[email protected] // log_info("new state %u", channel->state); 132027a923d0Smatthias.ringwald } 132127a923d0Smatthias.ringwald 132200d93d79Smatthias.ringwald 13237f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 132400d93d79Smatthias.ringwald 132500d93d79Smatthias.ringwald // get code, signalind identifier and command len 132600d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 132700d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 132800d93d79Smatthias.ringwald 132900d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 133000d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 133163a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 133200d93d79Smatthias.ringwald return; 133300d93d79Smatthias.ringwald } 133400d93d79Smatthias.ringwald 133500d93d79Smatthias.ringwald // general commands without an assigned channel 133600d93d79Smatthias.ringwald switch(code) { 133700d93d79Smatthias.ringwald 133800d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1339f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1340f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 134100d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 13422b83fb7dSmatthias.ringwald return; 134300d93d79Smatthias.ringwald } 134400d93d79Smatthias.ringwald 13452b360848Smatthias.ringwald case ECHO_REQUEST: 13462b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 13472b83fb7dSmatthias.ringwald return; 134800d93d79Smatthias.ringwald 134900d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1350f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 13512b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 13522b83fb7dSmatthias.ringwald return; 135300d93d79Smatthias.ringwald } 135400d93d79Smatthias.ringwald 135500d93d79Smatthias.ringwald default: 135600d93d79Smatthias.ringwald break; 135700d93d79Smatthias.ringwald } 135800d93d79Smatthias.ringwald 135900d93d79Smatthias.ringwald 136000d93d79Smatthias.ringwald // Get potential destination CID 1361f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 136200d93d79Smatthias.ringwald 136300d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1364665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1365665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1366665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1367665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1368c22aecc9S[email protected] if (channel->handle != handle) continue; 136900d93d79Smatthias.ringwald if (code & 1) { 1370b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1371b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 137200d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13734e32727eSmatthias.ringwald break; 137400d93d79Smatthias.ringwald } 137500d93d79Smatthias.ringwald } else { 1376b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 137700d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 137800d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13794e32727eSmatthias.ringwald break; 138000d93d79Smatthias.ringwald } 138100d93d79Smatthias.ringwald } 138200d93d79Smatthias.ringwald } 138300d93d79Smatthias.ringwald } 138400d93d79Smatthias.ringwald 1385d9a7306aSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint8_t *packet, uint16_t size ){ 138600d93d79Smatthias.ringwald 138700d93d79Smatthias.ringwald // Get Channel ID 138800d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 138900d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 139000d93d79Smatthias.ringwald 13915652b5ffS[email protected] switch (channel_id) { 13925652b5ffS[email protected] 13935652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 13945652b5ffS[email protected] 139500d93d79Smatthias.ringwald uint16_t command_offset = 8; 139600d93d79Smatthias.ringwald while (command_offset < size) { 139700d93d79Smatthias.ringwald 139800d93d79Smatthias.ringwald // handle signaling commands 139900d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 140000d93d79Smatthias.ringwald 140100d93d79Smatthias.ringwald // increment command_offset 1402f8fbdce0SMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 140300d93d79Smatthias.ringwald } 14045652b5ffS[email protected] break; 140500d93d79Smatthias.ringwald } 140600d93d79Smatthias.ringwald 14075652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 14085628cf69SMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 14095628cf69SMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14105652b5ffS[email protected] } 14115652b5ffS[email protected] break; 14125652b5ffS[email protected] 14135652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 14145628cf69SMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 14155628cf69SMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14165652b5ffS[email protected] } 14175652b5ffS[email protected] break; 14185652b5ffS[email protected] 1419462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 14205628cf69SMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 14215628cf69SMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1422462e630dS[email protected] } 1423462e630dS[email protected] break; 1424462e630dS[email protected] 14251bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1426ccf076adS[email protected] switch (packet[8]){ 1427ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1428f8fbdce0SMatthias Ringwald uint16_t result = little_endian_read_16(packet, 12); 1429ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1430ccf076adS[email protected] break; 1431ccf076adS[email protected] } 1432ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1433ccf076adS[email protected] uint8_t event[10]; 1434ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1435ccf076adS[email protected] event[1] = 8; 1436ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1437da886c03S[email protected] 1438da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1439da886c03S[email protected] if (connection){ 14406d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 14416d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 14426d91fb6cSMatthias Ringwald uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14436d91fb6cSMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14446d91fb6cSMatthias Ringwald break; 14456d91fb6cSMatthias Ringwald } 1446da886c03S[email protected] int update_parameter = 1; 1447a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 1448a4c06b28SMatthias Ringwald gap_le_get_connection_parameter_range(existing_range); 1449f8fbdce0SMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(packet,12); 1450f8fbdce0SMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(packet,14); 1451f8fbdce0SMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(packet,16); 1452f8fbdce0SMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(packet,18); 1453da886c03S[email protected] 1454da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1455da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1456da886c03S[email protected] 1457da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1458da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1459da886c03S[email protected] 1460da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1461da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1462da886c03S[email protected] 1463da886c03S[email protected] if (update_parameter){ 1464da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1465da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1466da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1467da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1468da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1469da886c03S[email protected] } else { 1470da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1471da886c03S[email protected] } 147279f53f1dSMatthias Ringwald connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1473da886c03S[email protected] } 1474da886c03S[email protected] 1475ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 147633c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 147733c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1478ccf076adS[email protected] break; 1479ccf076adS[email protected] } 1480ccf076adS[email protected] default: { 14811bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14821bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14831bbc0b23S[email protected] break; 14841bbc0b23S[email protected] } 1485ccf076adS[email protected] } 1486ccf076adS[email protected] break; 1487ccf076adS[email protected] } 14881bbc0b23S[email protected] 14895652b5ffS[email protected] default: { 149000d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 149100d93d79Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(channel_id); 149200d93d79Smatthias.ringwald if (channel) { 149317a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 149400d93d79Smatthias.ringwald } 14955652b5ffS[email protected] break; 14965652b5ffS[email protected] } 14975652b5ffS[email protected] } 149800d93d79Smatthias.ringwald 14991eb2563eS[email protected] l2cap_run(); 15002718e2e7Smatthias.ringwald } 150100d93d79Smatthias.ringwald 150215ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 150327a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t *channel){ 1504f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1505f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1506f62db1e3Smatthias.ringwald // discard channel 15079dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1508665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1509d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1510c8e4258aSmatthias.ringwald } 15111e6aba47Smatthias.ringwald 15128f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1513665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1514665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1515665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1516665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 15179d9bbc01Smatthias.ringwald if ( service->psm == psm){ 15189d9bbc01Smatthias.ringwald return service; 15199d9bbc01Smatthias.ringwald }; 15209d9bbc01Smatthias.ringwald } 15219d9bbc01Smatthias.ringwald return NULL; 15229d9bbc01Smatthias.ringwald } 15239d9bbc01Smatthias.ringwald 15247192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 15257192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 15267192e786SMatthias Ringwald } 15277192e786SMatthias Ringwald 1528e0abb8e7S[email protected] 1529be2053a6SMatthias 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){ 1530be2053a6SMatthias Ringwald 1531be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1532e0abb8e7S[email protected] 15334bb582b6Smatthias.ringwald // check for alread registered psm 15344bb582b6Smatthias.ringwald // TODO: emit error event 15359d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1536277abc2cSmatthias.ringwald if (service) { 1537be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1538be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1539277abc2cSmatthias.ringwald } 15409d9bbc01Smatthias.ringwald 15414bb582b6Smatthias.ringwald // alloc structure 15424bb582b6Smatthias.ringwald // TODO: emit error event 1543bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1544277abc2cSmatthias.ringwald if (!service) { 1545be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1546be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1547277abc2cSmatthias.ringwald } 15489d9bbc01Smatthias.ringwald 15499d9bbc01Smatthias.ringwald // fill in 15509d9bbc01Smatthias.ringwald service->psm = psm; 15519d9bbc01Smatthias.ringwald service->mtu = mtu; 155205ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1553df3354fcS[email protected] service->required_security_level = security_level; 15549d9bbc01Smatthias.ringwald 15559d9bbc01Smatthias.ringwald // add to services list 1556665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1557c0e866bfSmatthias.ringwald 1558c0e866bfSmatthias.ringwald // enable page scan 1559c0e866bfSmatthias.ringwald hci_connectable_control(1); 15605842b6d9Smatthias.ringwald 1561be2053a6SMatthias Ringwald return 0; 15629d9bbc01Smatthias.ringwald } 15639d9bbc01Smatthias.ringwald 156402f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){ 1565e0abb8e7S[email protected] 1566e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1567e0abb8e7S[email protected] 15689d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1569037d6e48Smatthias.ringwald if (!service) return; 1570665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1571d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1572c0e866bfSmatthias.ringwald 1573c0e866bfSmatthias.ringwald // disable page scan when no services registered 1574665d90f2SMatthias Ringwald if (!btstack_linked_list_empty(&l2cap_services)) return; 1575c0e866bfSmatthias.ringwald hci_connectable_control(0); 15769d9bbc01Smatthias.ringwald } 15779d9bbc01Smatthias.ringwald 15785652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 157905ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 15805628cf69SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 15815628cf69SMatthias Ringwald if (index < 0) return; 15825628cf69SMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 15835652b5ffS[email protected] } 15845652b5ffS[email protected] 1585a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 15867192e786SMatthias Ringwald 15877f02f414SMatthias Ringwald 15887f02f414SMatthias Ringwald #if 0 15897192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 15907192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, psm); 15917192e786SMatthias Ringwald } 15927192e786SMatthias Ringwald /** 15937192e786SMatthias Ringwald * @brief Regster L2CAP LE Credit Based Flow Control Mode service 15947192e786SMatthias Ringwald * @param 15957192e786SMatthias Ringwald */ 1596ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, 15977192e786SMatthias Ringwald uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 15987192e786SMatthias Ringwald 15997192e786SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 16007192e786SMatthias Ringwald 16017192e786SMatthias Ringwald // check for alread registered psm 16027192e786SMatthias Ringwald // TODO: emit error event 16037192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16047192e786SMatthias Ringwald if (service) { 16057192e786SMatthias Ringwald log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 16067192e786SMatthias Ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 16077192e786SMatthias Ringwald return; 16087192e786SMatthias Ringwald } 16097192e786SMatthias Ringwald 16107192e786SMatthias Ringwald // alloc structure 16117192e786SMatthias Ringwald // TODO: emit error event 16127192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 16137192e786SMatthias Ringwald if (!service) { 16147192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 16157192e786SMatthias Ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 16167192e786SMatthias Ringwald return; 16177192e786SMatthias Ringwald } 16187192e786SMatthias Ringwald 16197192e786SMatthias Ringwald // fill in 16207192e786SMatthias Ringwald service->psm = psm; 16217192e786SMatthias Ringwald service->mtu = mtu; 16227192e786SMatthias Ringwald service->mps = mps; 16237192e786SMatthias Ringwald service->packet_handler = packet_handler; 16247192e786SMatthias Ringwald service->required_security_level = security_level; 16257192e786SMatthias Ringwald 16267192e786SMatthias Ringwald // add to services list 1627665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 16287192e786SMatthias Ringwald 16297192e786SMatthias Ringwald // done 16307192e786SMatthias Ringwald l2cap_emit_service_registered(connection, 0, psm); 16317192e786SMatthias Ringwald } 16327192e786SMatthias Ringwald 1633ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) { 16347192e786SMatthias Ringwald 16357192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 16367192e786SMatthias Ringwald 16377192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16387192e786SMatthias Ringwald if (!service) return; 1639665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 16407192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 16417192e786SMatthias Ringwald } 16427192e786SMatthias Ringwald #endif 16437f02f414SMatthias Ringwald #endif 1644