143625864Smatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 381713bceaSmatthias.ringwald /* 3943625864Smatthias.ringwald * l2cap.c 4043625864Smatthias.ringwald * 4143625864Smatthias.ringwald * Logical Link Control and Adaption Protocl (L2CAP) 4243625864Smatthias.ringwald * 4343625864Smatthias.ringwald * Created by Matthias Ringwald on 5/16/09. 4443625864Smatthias.ringwald */ 4543625864Smatthias.ringwald 4643625864Smatthias.ringwald #include "l2cap.h" 47645658c9Smatthias.ringwald #include "hci.h" 482b3c6c9bSmatthias.ringwald #include "hci_dump.h" 4916ece135SMatthias Ringwald #include "btstack_debug.h" 500e2df43fSMatthias Ringwald #include "btstack_event.h" 51d3a9df87Smatthias.ringwald #include "btstack_memory.h" 5243625864Smatthias.ringwald 5343625864Smatthias.ringwald #include <stdarg.h> 5443625864Smatthias.ringwald #include <string.h> 5543625864Smatthias.ringwald 5643625864Smatthias.ringwald #include <stdio.h> 5743625864Smatthias.ringwald 584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 604c744e21Smatthias.ringwald 6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 62e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 6339bda6d5Smatthias.ringwald 6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 6900d93d79Smatthias.ringwald 705628cf69SMatthias Ringwald // internal table 715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0 725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL 1 735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2 745628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1) 755628cf69SMatthias Ringwald 7633c40538SMatthias Ringwald // prototypes 7733c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 7833c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 7933c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 8034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel); 8133c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 8233c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel); 8333c40538SMatthias Ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 8433c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 85*3d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ); 8630725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void); 8733c40538SMatthias Ringwald 885628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel { 895628cf69SMatthias Ringwald btstack_packet_handler_t callback; 902125de09SMatthias Ringwald uint8_t waiting_for_can_send_now; 915628cf69SMatthias Ringwald } l2cap_fixed_channel_t; 925628cf69SMatthias Ringwald 935628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels; 945628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services; 955628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels; 965628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services; 9739bda6d5Smatthias.ringwald 9839bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 992b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 1002b83fb7dSmatthias.ringwald static int signaling_responses_pending; 1012b83fb7dSmatthias.ringwald 10233c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp; 10333c40538SMatthias Ringwald 104fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 105fb37a842SMatthias Ringwald 10633c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler; 1075628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE]; 10839bda6d5Smatthias.ringwald 10934e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){ 11034e7e577SMatthias Ringwald switch (index){ 11134e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL: 11234e7e577SMatthias Ringwald return L2CAP_CID_ATTRIBUTE_PROTOCOL; 11334e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL: 11434e7e577SMatthias Ringwald return L2CAP_CID_SECURITY_MANAGER_PROTOCOL; 11534e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL: 11634e7e577SMatthias Ringwald return L2CAP_CID_CONNECTIONLESS_CHANNEL; 11734e7e577SMatthias Ringwald default: 11834e7e577SMatthias Ringwald return 0; 11934e7e577SMatthias Ringwald } 12034e7e577SMatthias Ringwald } 1215628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){ 1225628cf69SMatthias Ringwald switch (channel_id){ 1235628cf69SMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1245628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL; 1255628cf69SMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1265628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL; 1275628cf69SMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1285628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL; 1295628cf69SMatthias Ringwald default: 1305628cf69SMatthias Ringwald return -1; 1315628cf69SMatthias Ringwald } 1325628cf69SMatthias Ringwald } 13339bda6d5Smatthias.ringwald 13434e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){ 13534e7e577SMatthias Ringwald if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0; 13634e7e577SMatthias Ringwald return 1; 13734e7e577SMatthias Ringwald } 13834e7e577SMatthias Ringwald 13971de195eSMatthias Ringwald void l2cap_init(void){ 1402b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 141808a48abSmatthias.ringwald 142f5454fc6Smatthias.ringwald l2cap_channels = NULL; 143f5454fc6Smatthias.ringwald l2cap_services = NULL; 1447192e786SMatthias Ringwald l2cap_le_services = NULL; 1457192e786SMatthias Ringwald l2cap_le_channels = NULL; 146f5454fc6Smatthias.ringwald 14733c40538SMatthias Ringwald l2cap_event_packet_handler = NULL; 1485628cf69SMatthias Ringwald memset(fixed_channels, 0, sizeof(fixed_channels)); 149f5454fc6Smatthias.ringwald 150ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 0; 151ac301f95S[email protected] 152fcadd0caSmatthias.ringwald // 1532718e2e7Smatthias.ringwald // register callback with HCI 154fcadd0caSmatthias.ringwald // 155d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &l2cap_hci_event_handler; 156fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 157fb37a842SMatthias Ringwald 158d9a7306aSMatthias Ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 159fb37a842SMatthias Ringwald 16015a95bd5SMatthias Ringwald gap_connectable_control(0); // no services yet 161fcadd0caSmatthias.ringwald } 162fcadd0caSmatthias.ringwald 163ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 16433c40538SMatthias Ringwald l2cap_event_packet_handler = handler; 1651e6aba47Smatthias.ringwald } 1661e6aba47Smatthias.ringwald 16717a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 16858de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 16958de5610Smatthias.ringwald } 17058de5610Smatthias.ringwald 17158de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 172c9dc710bS[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", 173fc64f94aSMatthias Ringwald status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 174c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 175c9dc710bS[email protected] uint8_t event[23]; 17658de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 17758de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 17858de5610Smatthias.ringwald event[2] = status; 179724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 180fc64f94aSMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 181f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 182f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 183f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 184f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 185f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 186f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 18758de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18817a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 18958de5610Smatthias.ringwald } 19058de5610Smatthias.ringwald 19158de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 192e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 19358de5610Smatthias.ringwald uint8_t event[4]; 19458de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_CLOSED; 19558de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 196f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 19758de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19817a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 19958de5610Smatthias.ringwald } 20058de5610Smatthias.ringwald 20158de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) { 202e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 203fc64f94aSMatthias Ringwald bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid); 20458de5610Smatthias.ringwald uint8_t event[16]; 20558de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 20658de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 207724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 208fc64f94aSMatthias Ringwald little_endian_store_16(event, 8, channel->con_handle); 209f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 210f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 211f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 21258de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21317a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 2140af41d30Smatthias.ringwald } 215808a48abSmatthias.ringwald 21634e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) { 21734e7e577SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel); 21833c40538SMatthias Ringwald uint8_t event[4]; 21933c40538SMatthias Ringwald event[0] = L2CAP_EVENT_CAN_SEND_NOW; 22033c40538SMatthias Ringwald event[1] = sizeof(event) - 2; 22134e7e577SMatthias Ringwald little_endian_store_16(event, 2, channel); 22233c40538SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 22334e7e577SMatthias Ringwald packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); 22433c40538SMatthias Ringwald } 22533c40538SMatthias Ringwald 226fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){ 227ccf076adS[email protected] uint8_t event[6]; 228ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 229ccf076adS[email protected] event[1] = 4; 230fc64f94aSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 231f8fbdce0SMatthias Ringwald little_endian_store_16(event, 4, result); 232ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23333c40538SMatthias Ringwald if (!l2cap_event_packet_handler) return; 23433c40538SMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 235ccf076adS[email protected] } 236ccf076adS[email protected] 2377f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 238665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 239665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 240665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 241665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 242b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 243f62db1e3Smatthias.ringwald return channel; 244f62db1e3Smatthias.ringwald } 245f62db1e3Smatthias.ringwald } 246f62db1e3Smatthias.ringwald return NULL; 247f62db1e3Smatthias.ringwald } 248f62db1e3Smatthias.ringwald 2490b9d7e78SMatthias Ringwald /// 2500b9d7e78SMatthias Ringwald 25130725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){ 25230725612SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 25330725612SMatthias Ringwald if (!channel) return; 25430725612SMatthias Ringwald channel->waiting_for_can_send_now = 1; 25530725612SMatthias Ringwald l2cap_notify_channel_can_send(); 25630725612SMatthias Ringwald } 25730725612SMatthias Ringwald 2580b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){ 2590b9d7e78SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 2600b9d7e78SMatthias Ringwald if (index < 0) return; 2610b9d7e78SMatthias Ringwald fixed_channels[index].waiting_for_can_send_now = 1; 2620b9d7e78SMatthias Ringwald l2cap_notify_channel_can_send(); 2630b9d7e78SMatthias Ringwald } 2640b9d7e78SMatthias Ringwald 2650b9d7e78SMatthias Ringwald /// 2660b9d7e78SMatthias Ringwald 2676b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 2686b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 2696b1fde37Smatthias.ringwald if (!channel) return 0; 2700b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(channel->con_handle); 2717856fb31S[email protected] } 2727856fb31S[email protected] 27383e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 27483e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 27583e7cdd9SMatthias Ringwald if (!channel) return 0; 2760b9d7e78SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->con_handle); 27783e7cdd9SMatthias Ringwald } 27883e7cdd9SMatthias Ringwald 279fc64f94aSMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){ 2800b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(con_handle); 2812125de09SMatthias Ringwald } 2820b9d7e78SMatthias Ringwald 2830b9d7e78SMatthias Ringwald /// 2843cab4fcaS[email protected] 28596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 28696cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 28796cbd662Smatthias.ringwald if (channel) { 28896cbd662Smatthias.ringwald return channel->remote_mtu; 28996cbd662Smatthias.ringwald } 29096cbd662Smatthias.ringwald return 0; 29196cbd662Smatthias.ringwald } 29296cbd662Smatthias.ringwald 293ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 294665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 295665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 296665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 297665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2985932bd7cS[email protected] if ( &channel->rtx == ts) { 2995932bd7cS[email protected] return channel; 3005932bd7cS[email protected] } 3015932bd7cS[email protected] } 3025932bd7cS[email protected] return NULL; 3035932bd7cS[email protected] } 3045932bd7cS[email protected] 305ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 3065932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 3075932bd7cS[email protected] if (!ts) return; 3085932bd7cS[email protected] 3095932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 3105932bd7cS[email protected] 3115932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 3125932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 3135932bd7cS[email protected] // notify client 3145932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 3155932bd7cS[email protected] 3165932bd7cS[email protected] // discard channel 3179dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 318665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 3195932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 3205932bd7cS[email protected] } 3215932bd7cS[email protected] 3225932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 3235932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 324528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 3255932bd7cS[email protected] } 3265932bd7cS[email protected] 3275932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 3285932bd7cS[email protected] l2cap_stop_rtx(channel); 329cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 330528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 331528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 332528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3335932bd7cS[email protected] } 3345932bd7cS[email protected] 3355932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 3365932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 3375932bd7cS[email protected] l2cap_stop_rtx(channel); 338528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 339528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 340528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 3415932bd7cS[email protected] } 3425932bd7cS[email protected] 34371de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 344ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 345ac301f95S[email protected] } 346ac301f95S[email protected] 347df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 348ac301f95S[email protected] return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp); 349df3354fcS[email protected] } 3505932bd7cS[email protected] 3517f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 352b1d43497Smatthias.ringwald 353a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3549da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 355b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 356b1d43497Smatthias.ringwald } 357b1d43497Smatthias.ringwald 3589da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3592a373862S[email protected] hci_reserve_packet_buffer(); 360facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 36158de5610Smatthias.ringwald va_list argptr; 36258de5610Smatthias.ringwald va_start(argptr, identifier); 36370efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 36458de5610Smatthias.ringwald va_end(argptr); 3659da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 366826f7347S[email protected] return hci_send_acl_packet_buffer(len); 36758de5610Smatthias.ringwald } 36858de5610Smatthias.ringwald 369a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3707f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 37170efece1S[email protected] 372a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 3739da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 37470efece1S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 37570efece1S[email protected] } 37670efece1S[email protected] 3779da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 3782a373862S[email protected] hci_reserve_packet_buffer(); 379facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 38070efece1S[email protected] va_list argptr; 38170efece1S[email protected] va_start(argptr, identifier); 38270efece1S[email protected] uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 38370efece1S[email protected] va_end(argptr); 3849da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 385826f7347S[email protected] return hci_send_acl_packet_buffer(len); 38670efece1S[email protected] } 38770efece1S[email protected] #endif 38870efece1S[email protected] 389b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 390facf93fdS[email protected] return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 391b1d43497Smatthias.ringwald } 3926218e6f1Smatthias.ringwald 3936b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){ 3946b4af23dS[email protected] return hci_reserve_packet_buffer(); 3956b4af23dS[email protected] } 3966b4af23dS[email protected] 39768a0fcf7S[email protected] void l2cap_release_packet_buffer(void){ 39868a0fcf7S[email protected] hci_release_packet_buffer(); 39968a0fcf7S[email protected] } 40068a0fcf7S[email protected] 40168a0fcf7S[email protected] 402b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 403b1d43497Smatthias.ringwald 404c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 405c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 406c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 407c8b9416aS[email protected] } 408c8b9416aS[email protected] 40958de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 410b1d43497Smatthias.ringwald if (!channel) { 4119da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 412b1d43497Smatthias.ringwald return -1; // TODO: define error 4136218e6f1Smatthias.ringwald } 4146218e6f1Smatthias.ringwald 415fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){ 4169da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 417a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 418a35252c8S[email protected] } 419a35252c8S[email protected] 420fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle); 421b1d43497Smatthias.ringwald 422facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 423b1d43497Smatthias.ringwald 424e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 425e9772277S[email protected] 426e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 427fc64f94aSMatthias Ringwald little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14)); 42858de5610Smatthias.ringwald // 2 - ACL length 429f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 43058de5610Smatthias.ringwald // 4 - L2CAP packet length 431f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 43258de5610Smatthias.ringwald // 6 - L2CAP channel DEST 433f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, channel->remote_cid); 43458de5610Smatthias.ringwald // send 435826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 43691b99603Smatthias.ringwald 4376218e6f1Smatthias.ringwald return err; 43858de5610Smatthias.ringwald } 43958de5610Smatthias.ringwald 440fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){ 4412149f12eSmatthias.ringwald 442c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 443c8b9416aS[email protected] log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 4442149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 4452149f12eSmatthias.ringwald } 4462149f12eSmatthias.ringwald 447fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(con_handle)){ 448fc64f94aSMatthias Ringwald log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid); 449c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 450c8b9416aS[email protected] } 451c8b9416aS[email protected] 452fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid); 4532149f12eSmatthias.ringwald 454facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 4552149f12eSmatthias.ringwald 456e9772277S[email protected] int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 457e9772277S[email protected] 458e9772277S[email protected] // 0 - Connection handle : PB=pb : BC=00 459fc64f94aSMatthias Ringwald little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14)); 4602149f12eSmatthias.ringwald // 2 - ACL length 461f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 4622149f12eSmatthias.ringwald // 4 - L2CAP packet length 463f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 4642149f12eSmatthias.ringwald // 6 - L2CAP channel DEST 465f8fbdce0SMatthias Ringwald little_endian_store_16(acl_buffer, 6, cid); 4662149f12eSmatthias.ringwald // send 467826f7347S[email protected] int err = hci_send_acl_packet_buffer(len+8); 4682149f12eSmatthias.ringwald 4692149f12eSmatthias.ringwald return err; 4702149f12eSmatthias.ringwald } 4712149f12eSmatthias.ringwald 472ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 473b1d43497Smatthias.ringwald 474a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 475a35252c8S[email protected] if (!channel) { 476ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 477a35252c8S[email protected] return -1; // TODO: define error 478a35252c8S[email protected] } 479a35252c8S[email protected] 480f0efaa57S[email protected] if (len > channel->remote_mtu){ 481ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 482f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 483f0efaa57S[email protected] } 484f0efaa57S[email protected] 485fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)){ 486ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 487b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 488b1d43497Smatthias.ringwald } 489b1d43497Smatthias.ringwald 4902a373862S[email protected] hci_reserve_packet_buffer(); 491facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 492b1d43497Smatthias.ringwald 493b1d43497Smatthias.ringwald memcpy(&acl_buffer[8], data, len); 494b1d43497Smatthias.ringwald 495b1d43497Smatthias.ringwald return l2cap_send_prepared(local_cid, len); 496b1d43497Smatthias.ringwald } 497b1d43497Smatthias.ringwald 498fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){ 4992149f12eSmatthias.ringwald 500fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(con_handle)){ 501ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 5022149f12eSmatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 5032149f12eSmatthias.ringwald } 5042149f12eSmatthias.ringwald 5052a373862S[email protected] hci_reserve_packet_buffer(); 506facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 5072149f12eSmatthias.ringwald 5082149f12eSmatthias.ringwald memcpy(&acl_buffer[8], data, len); 5092149f12eSmatthias.ringwald 510fc64f94aSMatthias Ringwald return l2cap_send_prepared_connectionless(con_handle, cid, len); 5112149f12eSmatthias.ringwald } 5122149f12eSmatthias.ringwald 513fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){ 514fc64f94aSMatthias Ringwald return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data); 5150e37e417S[email protected] } 5160e37e417S[email protected] 51728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 51828ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 51928ca2b46S[email protected] } 52028ca2b46S[email protected] 52128ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 52228ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 52328ca2b46S[email protected] } 52428ca2b46S[email protected] 52528ca2b46S[email protected] 526b1d43497Smatthias.ringwald 5278158c421Smatthias.ringwald // MARK: L2CAP_RUN 5282cd0be45Smatthias.ringwald // process outstanding signaling tasks 5297f02f414SMatthias Ringwald static void l2cap_run(void){ 5302b83fb7dSmatthias.ringwald 53122c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 53222c29ab4SMatthias Ringwald 5332b83fb7dSmatthias.ringwald // check pending signaling responses 5342b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 5352b83fb7dSmatthias.ringwald 5362b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 537a35252c8S[email protected] 538a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 539a35252c8S[email protected] 5402b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 5412b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 54263a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 543f53da564S[email protected] uint8_t response_code = signaling_responses[0].code; 5442b83fb7dSmatthias.ringwald 545f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 546f53da564S[email protected] signaling_responses_pending--; 547f53da564S[email protected] int i; 548f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 549f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 550f53da564S[email protected] } 551f53da564S[email protected] 552f53da564S[email protected] switch (response_code){ 5532b360848Smatthias.ringwald case CONNECTION_REQUEST: 5542b360848Smatthias.ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0); 5552bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 5564d816277S[email protected] if (result == 0x0003){ 5572bd8b7e7S[email protected] hci_disconnect_security_block(handle); 5584d816277S[email protected] } 5592b360848Smatthias.ringwald break; 5602b83fb7dSmatthias.ringwald case ECHO_REQUEST: 5612b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 5622b83fb7dSmatthias.ringwald break; 5632b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 5643b0484b3S[email protected] switch (infoType){ 5653b0484b3S[email protected] case 1: { // Connectionless MTU 5663b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 5673b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 5683b0484b3S[email protected] break; 5693b0484b3S[email protected] } 5703b0484b3S[email protected] case 2: { // Extended Features Supported 571462e630dS[email protected] // extended features request supported, features: fixed channels, unicast connectionless data reception 572462e630dS[email protected] uint32_t features = 0x280; 5733b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 5743b0484b3S[email protected] break; 5753b0484b3S[email protected] } 5763b0484b3S[email protected] case 3: { // Fixed Channels Supported 5773b0484b3S[email protected] uint8_t map[8]; 5783b0484b3S[email protected] memset(map, 0, 8); 579462e630dS[email protected] map[0] = 0x01; // L2CAP Signaling Channel (0x01) + Connectionless reception (0x02) 5803b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 5813b0484b3S[email protected] break; 5823b0484b3S[email protected] } 5833b0484b3S[email protected] default: 5842b83fb7dSmatthias.ringwald // all other types are not supported 5852b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 5863b0484b3S[email protected] break; 5872b83fb7dSmatthias.ringwald } 5882b83fb7dSmatthias.ringwald break; 58963a7246aSmatthias.ringwald case COMMAND_REJECT: 5905ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 591a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 59270efece1S[email protected] case COMMAND_REJECT_LE: 59370efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 59463a7246aSmatthias.ringwald break; 59570efece1S[email protected] #endif 5962b83fb7dSmatthias.ringwald default: 5972b83fb7dSmatthias.ringwald // should not happen 5982b83fb7dSmatthias.ringwald break; 5992b83fb7dSmatthias.ringwald } 6002b83fb7dSmatthias.ringwald } 6012b83fb7dSmatthias.ringwald 602ae280e73Smatthias.ringwald uint8_t config_options[4]; 603665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 604665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 605665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 606baf94f06S[email protected] 607665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 60822c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 6092cd0be45Smatthias.ringwald switch (channel->state){ 6102cd0be45Smatthias.ringwald 611df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 612ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 613fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 614a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 615ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 616fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 617ad671560S[email protected] } 618ad671560S[email protected] break; 619ad671560S[email protected] 62002b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 621baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 62264472d52Smatthias.ringwald // send connection request - set state first 62364472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 62402b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 6258f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 62602b22dc4Smatthias.ringwald break; 62702b22dc4Smatthias.ringwald 628e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 629fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 63022c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 631fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 632e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 6339dcb2fb2S[email protected] l2cap_stop_rtx(channel); 634665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 635d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 636e7ff783cSmatthias.ringwald break; 637e7ff783cSmatthias.ringwald 638552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 639fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 640fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 64128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 642fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 643552d92a1Smatthias.ringwald break; 644552d92a1Smatthias.ringwald 6456fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 646fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 6476fdcc387Smatthias.ringwald // success, start l2cap handshake 648b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 6496fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 650fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 6515932bd7cS[email protected] l2cap_start_rtx(channel); 6526fdcc387Smatthias.ringwald break; 6536fdcc387Smatthias.ringwald 654fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 655fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 65673cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 65763a7246aSmatthias.ringwald uint16_t flags = 0; 65828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 65963a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 66063a7246aSmatthias.ringwald flags = 1; 66163a7246aSmatthias.ringwald } else { 66228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 66363a7246aSmatthias.ringwald } 66463a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 665fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS, 0, NULL); 66663a7246aSmatthias.ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 66763a7246aSmatthias.ringwald config_options[0] = 1; // MTU 66863a7246aSmatthias.ringwald config_options[1] = 2; // len param 669f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu); 670fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options); 67163a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 67263a7246aSmatthias.ringwald } else { 673fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL); 67463a7246aSmatthias.ringwald } 67563a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 676fa8473a4Smatthias.ringwald } 67773cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 67828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 67928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 680b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 681ae280e73Smatthias.ringwald config_options[0] = 1; // MTU 682ae280e73Smatthias.ringwald config_options[1] = 2; // len param 683f8fbdce0SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 684fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options); 6855932bd7cS[email protected] l2cap_start_rtx(channel); 686fa8473a4Smatthias.ringwald } 687fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 688552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 689552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 690fa8473a4Smatthias.ringwald } 691552d92a1Smatthias.ringwald break; 692552d92a1Smatthias.ringwald 693e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 694fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 69522c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 696fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 6975932bd7cS[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 :) 698756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 699e7ff783cSmatthias.ringwald break; 700e7ff783cSmatthias.ringwald 701e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 702fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 703b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 7042cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 705fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 7062cd0be45Smatthias.ringwald break; 7072cd0be45Smatthias.ringwald default: 7082cd0be45Smatthias.ringwald break; 7092cd0be45Smatthias.ringwald } 7102cd0be45Smatthias.ringwald } 711da886c03S[email protected] 712a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 713da886c03S[email protected] // send l2cap con paramter update if necessary 714da886c03S[email protected] hci_connections_get_iterator(&it); 715665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 716665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 7175d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 718b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 719da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 720b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 721b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 722b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 723b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 724b68d7bc3SMatthias Ringwald break; 725da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 726b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 727b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 728da886c03S[email protected] break; 729da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 730b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 731b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 732da886c03S[email protected] break; 733da886c03S[email protected] default: 734da886c03S[email protected] break; 735da886c03S[email protected] } 736da886c03S[email protected] } 7374d7157c3S[email protected] #endif 738da886c03S[email protected] 73922c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 7402cd0be45Smatthias.ringwald } 7412cd0be45Smatthias.ringwald 7424aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){ 7434ff786cfS[email protected] return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 744fa8c92f6Smatthias.ringwald } 745fa8c92f6Smatthias.ringwald 74671de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 7474ff786cfS[email protected] return l2cap_max_mtu(); 748e5e1518dS[email protected] } 749e5e1518dS[email protected] 750fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){ 7512df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 7525533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 7532df5dadcS[email protected] // success, start l2cap handshake 754fc64f94aSMatthias Ringwald channel->con_handle = con_handle; 7552df5dadcS[email protected] // check remote SSP feature first 7562df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 7572df5dadcS[email protected] } 7582df5dadcS[email protected] } 7592df5dadcS[email protected] 7602df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 7612df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 7622df5dadcS[email protected] 7632df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 764ac301f95S[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)); 765fc64f94aSMatthias Ringwald if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 7662df5dadcS[email protected] // request security level 2 7672df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 768fc64f94aSMatthias Ringwald gap_request_security_level(channel->con_handle, LEVEL_2); 7692df5dadcS[email protected] return; 7702df5dadcS[email protected] } 7712df5dadcS[email protected] // fine, go ahead 7722df5dadcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 7732df5dadcS[email protected] } 7742df5dadcS[email protected] 7759077cb15SMatthias Ringwald /** 7769077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 7779077cb15SMatthias Ringwald * @param packet_handler 7789077cb15SMatthias Ringwald * @param address 7799077cb15SMatthias Ringwald * @param psm 7809077cb15SMatthias Ringwald * @param mtu 7819077cb15SMatthias Ringwald * @param local_cid 7829077cb15SMatthias Ringwald */ 7839077cb15SMatthias 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){ 7849077cb15SMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu); 7859077cb15SMatthias Ringwald 7869077cb15SMatthias Ringwald // alloc structure 787fc64f94aSMatthias Ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 788fc64f94aSMatthias Ringwald if (!channel) { 7899077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 7909077cb15SMatthias Ringwald } 7919077cb15SMatthias Ringwald 7929077cb15SMatthias Ringwald // Init memory (make valgrind happy) 793fc64f94aSMatthias Ringwald memset(channel, 0, sizeof(l2cap_channel_t)); 7949077cb15SMatthias Ringwald // limit local mtu to max acl packet length - l2cap header 7959077cb15SMatthias Ringwald if (mtu > l2cap_max_mtu()) { 7969077cb15SMatthias Ringwald mtu = l2cap_max_mtu(); 7979077cb15SMatthias Ringwald } 7989077cb15SMatthias Ringwald 7999077cb15SMatthias Ringwald // fill in 800fc64f94aSMatthias Ringwald bd_addr_copy(channel->address, address); 801fc64f94aSMatthias Ringwald channel->psm = psm; 802fc64f94aSMatthias Ringwald channel->con_handle = 0; 803fc64f94aSMatthias Ringwald channel->packet_handler = channel_packet_handler; 804fc64f94aSMatthias Ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 805fc64f94aSMatthias Ringwald channel->local_mtu = mtu; 806fc64f94aSMatthias Ringwald channel->local_cid = l2cap_next_local_cid(); 8079077cb15SMatthias Ringwald 8089077cb15SMatthias Ringwald // set initial state 809fc64f94aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 810fc64f94aSMatthias Ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 811fc64f94aSMatthias Ringwald channel->remote_sig_id = L2CAP_SIG_ID_INVALID; 812fc64f94aSMatthias Ringwald channel->local_sig_id = L2CAP_SIG_ID_INVALID; 813fc64f94aSMatthias Ringwald channel->required_security_level = LEVEL_0; 8149077cb15SMatthias Ringwald 8159077cb15SMatthias Ringwald // add to connections list 816fc64f94aSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 8179077cb15SMatthias Ringwald 8189077cb15SMatthias Ringwald // store local_cid 8199077cb15SMatthias Ringwald if (out_local_cid){ 820fc64f94aSMatthias Ringwald *out_local_cid = channel->local_cid; 8219077cb15SMatthias Ringwald } 8229077cb15SMatthias Ringwald 8239077cb15SMatthias Ringwald // check if hci connection is already usable 8249077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 8259077cb15SMatthias Ringwald if (conn){ 826add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 827fc64f94aSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 8289077cb15SMatthias Ringwald // check if remote supported fearures are already received 8299077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 830fc64f94aSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 8319077cb15SMatthias Ringwald } 8329077cb15SMatthias Ringwald } 8339077cb15SMatthias Ringwald 8349077cb15SMatthias Ringwald l2cap_run(); 8359077cb15SMatthias Ringwald 8369077cb15SMatthias Ringwald return 0; 8379077cb15SMatthias Ringwald } 8389077cb15SMatthias Ringwald 839ce8f182eSMatthias Ringwald void 840ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 841e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 842b35f641cSmatthias.ringwald // find channel for local_cid 843b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 844f62db1e3Smatthias.ringwald if (channel) { 845e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 846f62db1e3Smatthias.ringwald } 8472cd0be45Smatthias.ringwald // process 8482cd0be45Smatthias.ringwald l2cap_run(); 84943625864Smatthias.ringwald } 8501e6aba47Smatthias.ringwald 851afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 852665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 853665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 854665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 855665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 856058e3d6bSMatthias Ringwald if ( bd_addr_cmp( channel->address, address) != 0) continue; 857c22aecc9S[email protected] // channel for this address found 858c22aecc9S[email protected] switch (channel->state){ 859c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 860c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 861afde0c52Smatthias.ringwald // failure, forward error code 862afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 863afde0c52Smatthias.ringwald // discard channel 8649dcb2fb2S[email protected] l2cap_stop_rtx(channel); 865665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 866d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 867c22aecc9S[email protected] break; 868c22aecc9S[email protected] default: 869c22aecc9S[email protected] break; 870afde0c52Smatthias.ringwald } 871afde0c52Smatthias.ringwald } 872afde0c52Smatthias.ringwald } 873afde0c52Smatthias.ringwald 874afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 875665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 876665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 877665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 878665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 879058e3d6bSMatthias Ringwald if ( ! bd_addr_cmp( channel->address, address) ){ 8802df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 881afde0c52Smatthias.ringwald } 882afde0c52Smatthias.ringwald } 8836fdcc387Smatthias.ringwald // process 8846fdcc387Smatthias.ringwald l2cap_run(); 885afde0c52Smatthias.ringwald } 886b448a0e7Smatthias.ringwald 88733c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){ 88833c40538SMatthias Ringwald btstack_linked_list_iterator_t it; 88933c40538SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 89033c40538SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 89133c40538SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 89233c40538SMatthias Ringwald if (!channel->waiting_for_can_send_now) continue; 893fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 89433c40538SMatthias Ringwald channel->waiting_for_can_send_now = 0; 89534e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 89633c40538SMatthias Ringwald } 8972125de09SMatthias Ringwald 8982125de09SMatthias Ringwald int i; 8992125de09SMatthias Ringwald for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){ 900773c4106SMatthias Ringwald if (!fixed_channels[i].callback) continue; 9012125de09SMatthias Ringwald if (!fixed_channels[i].waiting_for_can_send_now) continue; 90234e7e577SMatthias Ringwald int can_send; 90334e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 90434e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 90534e7e577SMatthias Ringwald } else { 90634e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 90734e7e577SMatthias Ringwald } 90834e7e577SMatthias Ringwald if (!can_send) continue; 90934e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 91034e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 9112125de09SMatthias Ringwald } 91233c40538SMatthias Ringwald } 91333c40538SMatthias Ringwald 914d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 915afde0c52Smatthias.ringwald 916afde0c52Smatthias.ringwald bd_addr_t address; 917afde0c52Smatthias.ringwald hci_con_handle_t handle; 918665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 9192d00edd4Smatthias.ringwald int hci_con_used; 920afde0c52Smatthias.ringwald 9210e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 922afde0c52Smatthias.ringwald 923afde0c52Smatthias.ringwald // handle connection complete events 924afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 925724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 926afde0c52Smatthias.ringwald if (packet[2] == 0){ 927f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 928afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 929afde0c52Smatthias.ringwald } else { 930afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 931afde0c52Smatthias.ringwald } 932afde0c52Smatthias.ringwald break; 933afde0c52Smatthias.ringwald 934afde0c52Smatthias.ringwald // handle successful create connection cancel command 935afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 936073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) { 937afde0c52Smatthias.ringwald if (packet[5] == 0){ 938724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 939afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 940afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 94103cfbabcSmatthias.ringwald } 9421e6aba47Smatthias.ringwald } 94339d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 94439d59809Smatthias.ringwald break; 94539d59809Smatthias.ringwald 94639d59809Smatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 94739d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 948afde0c52Smatthias.ringwald break; 94927a923d0Smatthias.ringwald 9501e6aba47Smatthias.ringwald // handle disconnection complete events 951afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 952c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 953f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 954665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 955665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 956665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 957fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 95815ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 9599dcb2fb2S[email protected] l2cap_stop_rtx(channel); 960665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 961d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 96227a923d0Smatthias.ringwald } 963afde0c52Smatthias.ringwald break; 964fcadd0caSmatthias.ringwald 96533c40538SMatthias Ringwald // Notify channel packet handler if they can send now 96663fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 9676218e6f1Smatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 96802b22dc4Smatthias.ringwald l2cap_run(); // try sending signaling packets first 96933c40538SMatthias Ringwald l2cap_notify_channel_can_send(); 9706218e6f1Smatthias.ringwald break; 9716218e6f1Smatthias.ringwald 972ee091cf1Smatthias.ringwald // HCI Connection Timeouts 973afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 974f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 975bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 97680ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 9772d00edd4Smatthias.ringwald hci_con_used = 0; 978665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 979665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 980665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 981fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 9822d00edd4Smatthias.ringwald hci_con_used = 1; 983c22aecc9S[email protected] break; 984ee091cf1Smatthias.ringwald } 9852d00edd4Smatthias.ringwald if (hci_con_used) break; 986d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 9879edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 988afde0c52Smatthias.ringwald break; 989ee091cf1Smatthias.ringwald 990df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 991f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 992665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 993665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 994665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 995fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 9962df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 997df3354fcS[email protected] break; 998df3354fcS[email protected] } 999c22aecc9S[email protected] break; 1000df3354fcS[email protected] 10015611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1002f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1003bd63148eS[email protected] log_info("l2cap - security level update"); 1004665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1005665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1006665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1007fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 10085533f01eS[email protected] 1009bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1010bd63148eS[email protected] 1011e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 10125533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 10135533f01eS[email protected] 1014df3354fcS[email protected] switch (channel->state){ 1015df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 10165533f01eS[email protected] if (actual_level >= required_level){ 1017f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 1018f85a9399S[email protected] l2cap_emit_connection_request(channel); 10191eb2563eS[email protected] } else { 1020775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 10211eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 10221eb2563eS[email protected] } 1023df3354fcS[email protected] break; 1024df3354fcS[email protected] 1025df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 10265533f01eS[email protected] if (actual_level >= required_level){ 1027df3354fcS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 1028df3354fcS[email protected] } else { 1029df3354fcS[email protected] // disconnnect, authentication not good enough 1030df3354fcS[email protected] hci_disconnect_security_block(handle); 1031df3354fcS[email protected] } 1032df3354fcS[email protected] break; 1033df3354fcS[email protected] 1034df3354fcS[email protected] default: 1035df3354fcS[email protected] break; 1036df3354fcS[email protected] } 1037f85a9399S[email protected] } 1038f85a9399S[email protected] break; 1039f85a9399S[email protected] 1040afde0c52Smatthias.ringwald default: 1041afde0c52Smatthias.ringwald break; 1042afde0c52Smatthias.ringwald } 1043afde0c52Smatthias.ringwald 1044bd63148eS[email protected] l2cap_run(); 10451e6aba47Smatthias.ringwald } 10461e6aba47Smatthias.ringwald 1047afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 1048b1988dceSmatthias.ringwald channel->remote_sig_id = identifier; 1049e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 1050e7ff783cSmatthias.ringwald l2cap_run(); 105184836b65Smatthias.ringwald } 105284836b65Smatthias.ringwald 10532b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){ 10544cf56b4aSmatthias.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." 10552b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 10562b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 10572b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 10582b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 10592b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 10602b360848Smatthias.ringwald signaling_responses_pending++; 10612b360848Smatthias.ringwald l2cap_run(); 10622b360848Smatthias.ringwald } 10632b360848Smatthias.ringwald } 10642b360848Smatthias.ringwald 1065b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1066645658c9Smatthias.ringwald 10679da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1068645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1069645658c9Smatthias.ringwald if (!service) { 1070645658c9Smatthias.ringwald // 0x0002 PSM not supported 10712b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002); 1072645658c9Smatthias.ringwald return; 1073645658c9Smatthias.ringwald } 1074645658c9Smatthias.ringwald 10755061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1076645658c9Smatthias.ringwald if (!hci_connection) { 10772b360848Smatthias.ringwald // 10789da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1079645658c9Smatthias.ringwald return; 1080645658c9Smatthias.ringwald } 10812bd8b7e7S[email protected] 1082645658c9Smatthias.ringwald // alloc structure 10839da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1084bb69aaaeS[email protected] l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 10852b360848Smatthias.ringwald if (!channel){ 10862b360848Smatthias.ringwald // 0x0004 No resources available 10872b360848Smatthias.ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004); 10882b360848Smatthias.ringwald return; 10892b360848Smatthias.ringwald } 1090c523d53dS[email protected] // Init memory (make valgrind happy) 1091c523d53dS[email protected] memset(channel, 0, sizeof(l2cap_channel_t)); 1092645658c9Smatthias.ringwald // fill in 1093058e3d6bSMatthias Ringwald bd_addr_copy(channel->address, hci_connection->address); 1094169f8b28Smatthias.ringwald channel->psm = psm; 1095fc64f94aSMatthias Ringwald channel->con_handle = handle; 1096f8dd2f72Smatthias.ringwald channel->packet_handler = service->packet_handler; 1097b35f641cSmatthias.ringwald channel->local_cid = l2cap_next_local_cid(); 1098b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1099fa2b2627Smatthias.ringwald channel->local_mtu = service->mtu; 11000a18a8e9Smatthias.ringwald channel->remote_mtu = L2CAP_DEFAULT_MTU; 1101b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1102df3354fcS[email protected] channel->required_security_level = service->required_security_level; 1103645658c9Smatthias.ringwald 1104f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 11052985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 11062985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 11079775e25bSmatthias.ringwald } 11089775e25bSmatthias.ringwald 1109645658c9Smatthias.ringwald // set initial state 1110df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1111ad671560S[email protected] channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 1112e405ae81Smatthias.ringwald 1113645658c9Smatthias.ringwald // add to connections list 1114665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1115645658c9Smatthias.ringwald 1116f85a9399S[email protected] // assert security requirements 11171eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1118e405ae81Smatthias.ringwald } 1119645658c9Smatthias.ringwald 1120ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1121e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1122b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1123e405ae81Smatthias.ringwald if (!channel) { 1124ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1125e405ae81Smatthias.ringwald return; 1126e405ae81Smatthias.ringwald } 1127e405ae81Smatthias.ringwald 1128552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1129e405ae81Smatthias.ringwald 1130552d92a1Smatthias.ringwald // process 1131552d92a1Smatthias.ringwald l2cap_run(); 1132e405ae81Smatthias.ringwald } 1133645658c9Smatthias.ringwald 1134ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){ 1135e0abb8e7S[email protected] log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason); 1136b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1137e405ae81Smatthias.ringwald if (!channel) { 1138ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1139e405ae81Smatthias.ringwald return; 1140e405ae81Smatthias.ringwald } 1141e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 1142e7ff783cSmatthias.ringwald channel->reason = reason; 1143e7ff783cSmatthias.ringwald l2cap_run(); 1144645658c9Smatthias.ringwald } 1145645658c9Smatthias.ringwald 11467f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1147b1988dceSmatthias.ringwald 1148b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1149b1988dceSmatthias.ringwald 1150f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 115163a7246aSmatthias.ringwald if (flags & 1) { 115263a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 115363a7246aSmatthias.ringwald } 115463a7246aSmatthias.ringwald 11552784b77dSmatthias.ringwald // accept the other's configuration options 1156f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 11573de7c0caSmatthias.ringwald uint16_t pos = 8; 11583de7c0caSmatthias.ringwald while (pos < end_pos){ 115963a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 116063a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 116163a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 116263a7246aSmatthias.ringwald pos++; 11631dc511deSmatthias.ringwald uint8_t length = command[pos++]; 11641dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 116563a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1166f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 11679da54300S[email protected] // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu); 116863a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 116963a7246aSmatthias.ringwald } 11700fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 11710fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1172f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 11730fe7a9d0S[email protected] } 117463a7246aSmatthias.ringwald // check for unknown options 117563a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1176c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 117763a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 11781dc511deSmatthias.ringwald } 11791dc511deSmatthias.ringwald pos += length; 11801dc511deSmatthias.ringwald } 11812784b77dSmatthias.ringwald } 11822784b77dSmatthias.ringwald 1183fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 11849da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 118573cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 118673cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1187019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1188019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1189fa8473a4Smatthias.ringwald return 1; 1190fa8473a4Smatthias.ringwald } 1191fa8473a4Smatthias.ringwald 1192fa8473a4Smatthias.ringwald 11937f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 11941e6aba47Smatthias.ringwald 119500d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 119600d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 119738e5900eSmatthias.ringwald uint16_t result = 0; 11981e6aba47Smatthias.ringwald 11999da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1200b35f641cSmatthias.ringwald 12019a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 12029a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 12039a011532Smatthias.ringwald switch (channel->state){ 1204fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 12059a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 12062b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 12079a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 12089a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 12099a011532Smatthias.ringwald break; 12109a011532Smatthias.ringwald 12119a011532Smatthias.ringwald default: 12129a011532Smatthias.ringwald // ignore in other states 12139a011532Smatthias.ringwald break; 12149a011532Smatthias.ringwald } 12159a011532Smatthias.ringwald return; 12169a011532Smatthias.ringwald } 12179a011532Smatthias.ringwald 121856081214Smatthias.ringwald // @STATEMACHINE(l2cap) 12191e6aba47Smatthias.ringwald switch (channel->state) { 12201e6aba47Smatthias.ringwald 12211e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 12221e6aba47Smatthias.ringwald switch (code){ 12231e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 12245932bd7cS[email protected] l2cap_stop_rtx(channel); 1225f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 122638e5900eSmatthias.ringwald switch (result) { 122738e5900eSmatthias.ringwald case 0: 1228169f8b28Smatthias.ringwald // successful connection 1229f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1230fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 123128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 123238e5900eSmatthias.ringwald break; 123338e5900eSmatthias.ringwald case 1: 12345932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 12355932bd7cS[email protected] l2cap_start_ertx(channel); 123638e5900eSmatthias.ringwald break; 123738e5900eSmatthias.ringwald default: 1238eb920dbeSmatthias.ringwald // channel closed 1239eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1240f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 124138e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1242eb920dbeSmatthias.ringwald 1243eb920dbeSmatthias.ringwald // drop link key if security block 1244eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 124515a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 1246eb920dbeSmatthias.ringwald } 1247eb920dbeSmatthias.ringwald 1248eb920dbeSmatthias.ringwald // discard channel 1249665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1250d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 125138e5900eSmatthias.ringwald break; 12521e6aba47Smatthias.ringwald } 12531e6aba47Smatthias.ringwald break; 125438e5900eSmatthias.ringwald 125538e5900eSmatthias.ringwald default: 12561e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 125738e5900eSmatthias.ringwald break; 12581e6aba47Smatthias.ringwald } 12591e6aba47Smatthias.ringwald break; 12601e6aba47Smatthias.ringwald 1261fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1262f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1263ae280e73Smatthias.ringwald switch (code) { 1264ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 126528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1266ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 126763a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 126863a7246aSmatthias.ringwald // only done if continuation not set 126963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 127063a7246aSmatthias.ringwald } 1271ae280e73Smatthias.ringwald break; 12721e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 12735932bd7cS[email protected] l2cap_stop_rtx(channel); 12745932bd7cS[email protected] switch (result){ 12755932bd7cS[email protected] case 0: // success 12765932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 12775932bd7cS[email protected] break; 12785932bd7cS[email protected] case 4: // pending 12795932bd7cS[email protected] l2cap_start_ertx(channel); 12805932bd7cS[email protected] break; 12815932bd7cS[email protected] default: 1282fe9d8984S[email protected] // retry on negative result 1283fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1284fe9d8984S[email protected] break; 1285fe9d8984S[email protected] } 12865a67bd4aSmatthias.ringwald break; 12875a67bd4aSmatthias.ringwald default: 12885a67bd4aSmatthias.ringwald break; 12891e6aba47Smatthias.ringwald } 1290fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1291fa8473a4Smatthias.ringwald // for open: 12925a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1293fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1294c8e4258aSmatthias.ringwald } 1295c8e4258aSmatthias.ringwald break; 1296f62db1e3Smatthias.ringwald 1297f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1298f62db1e3Smatthias.ringwald switch (code) { 1299f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 130027a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 130127a923d0Smatthias.ringwald break; 13025a67bd4aSmatthias.ringwald default: 13035a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 13045a67bd4aSmatthias.ringwald break; 130527a923d0Smatthias.ringwald } 130627a923d0Smatthias.ringwald break; 130784836b65Smatthias.ringwald 130884836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 130984836b65Smatthias.ringwald // @TODO handle incoming requests 131084836b65Smatthias.ringwald break; 131184836b65Smatthias.ringwald 131284836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 131384836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 131484836b65Smatthias.ringwald break; 131510642e45Smatthias.ringwald default: 131610642e45Smatthias.ringwald break; 131727a923d0Smatthias.ringwald } 13189da54300S[email protected] // log_info("new state %u", channel->state); 131927a923d0Smatthias.ringwald } 132027a923d0Smatthias.ringwald 132100d93d79Smatthias.ringwald 13227f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 132300d93d79Smatthias.ringwald 132400d93d79Smatthias.ringwald // get code, signalind identifier and command len 132500d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 132600d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 132700d93d79Smatthias.ringwald 132800d93d79Smatthias.ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST 132900d93d79Smatthias.ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){ 133063a7246aSmatthias.ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN); 133100d93d79Smatthias.ringwald return; 133200d93d79Smatthias.ringwald } 133300d93d79Smatthias.ringwald 133400d93d79Smatthias.ringwald // general commands without an assigned channel 133500d93d79Smatthias.ringwald switch(code) { 133600d93d79Smatthias.ringwald 133700d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 1338f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1339f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 134000d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 13412b83fb7dSmatthias.ringwald return; 134200d93d79Smatthias.ringwald } 134300d93d79Smatthias.ringwald 13442b360848Smatthias.ringwald case ECHO_REQUEST: 13452b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, 0); 13462b83fb7dSmatthias.ringwald return; 134700d93d79Smatthias.ringwald 134800d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 1349f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 13502b360848Smatthias.ringwald l2cap_register_signaling_response(handle, code, sig_id, infoType); 13512b83fb7dSmatthias.ringwald return; 135200d93d79Smatthias.ringwald } 135300d93d79Smatthias.ringwald 135400d93d79Smatthias.ringwald default: 135500d93d79Smatthias.ringwald break; 135600d93d79Smatthias.ringwald } 135700d93d79Smatthias.ringwald 135800d93d79Smatthias.ringwald 135900d93d79Smatthias.ringwald // Get potential destination CID 1360f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 136100d93d79Smatthias.ringwald 136200d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 1363665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1364665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1365665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1366665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1367fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 136800d93d79Smatthias.ringwald if (code & 1) { 1369b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 1370b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 137100d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13724e32727eSmatthias.ringwald break; 137300d93d79Smatthias.ringwald } 137400d93d79Smatthias.ringwald } else { 1375b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 137600d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 137700d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 13784e32727eSmatthias.ringwald break; 137900d93d79Smatthias.ringwald } 138000d93d79Smatthias.ringwald } 138100d93d79Smatthias.ringwald } 138200d93d79Smatthias.ringwald } 138300d93d79Smatthias.ringwald 1384*3d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 138500d93d79Smatthias.ringwald 138600d93d79Smatthias.ringwald // Get Channel ID 138700d93d79Smatthias.ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 138800d93d79Smatthias.ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 138900d93d79Smatthias.ringwald 13905652b5ffS[email protected] switch (channel_id) { 13915652b5ffS[email protected] 13925652b5ffS[email protected] case L2CAP_CID_SIGNALING: { 13935652b5ffS[email protected] 139400d93d79Smatthias.ringwald uint16_t command_offset = 8; 139500d93d79Smatthias.ringwald while (command_offset < size) { 139600d93d79Smatthias.ringwald 139700d93d79Smatthias.ringwald // handle signaling commands 139800d93d79Smatthias.ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 139900d93d79Smatthias.ringwald 140000d93d79Smatthias.ringwald // increment command_offset 1401f8fbdce0SMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 140200d93d79Smatthias.ringwald } 14035652b5ffS[email protected] break; 140400d93d79Smatthias.ringwald } 140500d93d79Smatthias.ringwald 14065652b5ffS[email protected] case L2CAP_CID_ATTRIBUTE_PROTOCOL: 14075628cf69SMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 14085628cf69SMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14095652b5ffS[email protected] } 14105652b5ffS[email protected] break; 14115652b5ffS[email protected] 14125652b5ffS[email protected] case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 14135628cf69SMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 14145628cf69SMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 14155652b5ffS[email protected] } 14165652b5ffS[email protected] break; 14175652b5ffS[email protected] 1418462e630dS[email protected] case L2CAP_CID_CONNECTIONLESS_CHANNEL: 14195628cf69SMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 14205628cf69SMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 1421462e630dS[email protected] } 1422462e630dS[email protected] break; 1423462e630dS[email protected] 14241bbc0b23S[email protected] case L2CAP_CID_SIGNALING_LE: { 1425ccf076adS[email protected] switch (packet[8]){ 1426ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_RESPONSE: { 1427f8fbdce0SMatthias Ringwald uint16_t result = little_endian_read_16(packet, 12); 1428ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 1429ccf076adS[email protected] break; 1430ccf076adS[email protected] } 1431ccf076adS[email protected] case CONNECTION_PARAMETER_UPDATE_REQUEST: { 1432ccf076adS[email protected] uint8_t event[10]; 1433ccf076adS[email protected] event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 1434ccf076adS[email protected] event[1] = 8; 1435ccf076adS[email protected] memcpy(&event[2], &packet[12], 8); 1436da886c03S[email protected] 1437da886c03S[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 1438da886c03S[email protected] if (connection){ 14396d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 14406d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 14416d91fb6cSMatthias Ringwald uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14426d91fb6cSMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14436d91fb6cSMatthias Ringwald break; 14446d91fb6cSMatthias Ringwald } 1445da886c03S[email protected] int update_parameter = 1; 1446a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 14474ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 1448f8fbdce0SMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(packet,12); 1449f8fbdce0SMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(packet,14); 1450f8fbdce0SMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(packet,16); 1451f8fbdce0SMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(packet,18); 1452da886c03S[email protected] 1453da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 1454da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 1455da886c03S[email protected] 1456da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 1457da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 1458da886c03S[email protected] 1459da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 1460da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 1461da886c03S[email protected] 1462da886c03S[email protected] if (update_parameter){ 1463da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 1464da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 1465da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 1466da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 1467da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 1468da886c03S[email protected] } else { 1469da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 1470da886c03S[email protected] } 147179f53f1dSMatthias Ringwald connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1]; 1472da886c03S[email protected] } 1473da886c03S[email protected] 1474ccf076adS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 147533c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 147633c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1477ccf076adS[email protected] break; 1478ccf076adS[email protected] } 1479ccf076adS[email protected] default: { 14801bbc0b23S[email protected] uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 14811bbc0b23S[email protected] l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN); 14821bbc0b23S[email protected] break; 14831bbc0b23S[email protected] } 1484ccf076adS[email protected] } 1485ccf076adS[email protected] break; 1486ccf076adS[email protected] } 14871bbc0b23S[email protected] 14885652b5ffS[email protected] default: { 148900d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 1490*3d50b4baSMatthias Ringwald l2cap_channel_t * l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 149100d93d79Smatthias.ringwald if (channel) { 1492*3d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 149300d93d79Smatthias.ringwald } 14945652b5ffS[email protected] break; 14955652b5ffS[email protected] } 14965652b5ffS[email protected] } 149700d93d79Smatthias.ringwald 14981eb2563eS[email protected] l2cap_run(); 14992718e2e7Smatthias.ringwald } 150000d93d79Smatthias.ringwald 150115ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 150227a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 1503f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1504f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 1505f62db1e3Smatthias.ringwald // discard channel 15069dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1507665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1508d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1509c8e4258aSmatthias.ringwald } 15101e6aba47Smatthias.ringwald 15118f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 1512665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1513665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 1514665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1515665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 15169d9bbc01Smatthias.ringwald if ( service->psm == psm){ 15179d9bbc01Smatthias.ringwald return service; 15189d9bbc01Smatthias.ringwald }; 15199d9bbc01Smatthias.ringwald } 15209d9bbc01Smatthias.ringwald return NULL; 15219d9bbc01Smatthias.ringwald } 15229d9bbc01Smatthias.ringwald 15237192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 15247192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 15257192e786SMatthias Ringwald } 15267192e786SMatthias Ringwald 1527e0abb8e7S[email protected] 1528be2053a6SMatthias 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){ 1529be2053a6SMatthias Ringwald 1530be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 1531e0abb8e7S[email protected] 15324bb582b6Smatthias.ringwald // check for alread registered psm 15334bb582b6Smatthias.ringwald // TODO: emit error event 15349d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1535277abc2cSmatthias.ringwald if (service) { 1536be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 1537be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 1538277abc2cSmatthias.ringwald } 15399d9bbc01Smatthias.ringwald 15404bb582b6Smatthias.ringwald // alloc structure 15414bb582b6Smatthias.ringwald // TODO: emit error event 1542bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 1543277abc2cSmatthias.ringwald if (!service) { 1544be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 1545be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 1546277abc2cSmatthias.ringwald } 15479d9bbc01Smatthias.ringwald 15489d9bbc01Smatthias.ringwald // fill in 15499d9bbc01Smatthias.ringwald service->psm = psm; 15509d9bbc01Smatthias.ringwald service->mtu = mtu; 155105ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 1552df3354fcS[email protected] service->required_security_level = security_level; 15539d9bbc01Smatthias.ringwald 15549d9bbc01Smatthias.ringwald // add to services list 1555665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 1556c0e866bfSmatthias.ringwald 1557c0e866bfSmatthias.ringwald // enable page scan 155815a95bd5SMatthias Ringwald gap_connectable_control(1); 15595842b6d9Smatthias.ringwald 1560be2053a6SMatthias Ringwald return 0; 15619d9bbc01Smatthias.ringwald } 15629d9bbc01Smatthias.ringwald 156302f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){ 1564e0abb8e7S[email protected] 1565e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 1566e0abb8e7S[email protected] 15679d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1568037d6e48Smatthias.ringwald if (!service) return; 1569665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 1570d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 1571c0e866bfSmatthias.ringwald 1572c0e866bfSmatthias.ringwald // disable page scan when no services registered 1573665d90f2SMatthias Ringwald if (!btstack_linked_list_empty(&l2cap_services)) return; 157415a95bd5SMatthias Ringwald gap_connectable_control(0); 15759d9bbc01Smatthias.ringwald } 15769d9bbc01Smatthias.ringwald 15775652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 157805ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 15795628cf69SMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 15805628cf69SMatthias Ringwald if (index < 0) return; 15815628cf69SMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 15825652b5ffS[email protected] } 15835652b5ffS[email protected] 1584a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 15857192e786SMatthias Ringwald 15867f02f414SMatthias Ringwald 15877f02f414SMatthias Ringwald #if 0 15887192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){ 15897192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, psm); 15907192e786SMatthias Ringwald } 15917192e786SMatthias Ringwald /** 15927192e786SMatthias Ringwald * @brief Regster L2CAP LE Credit Based Flow Control Mode service 15937192e786SMatthias Ringwald * @param 15947192e786SMatthias Ringwald */ 1595ffbf8201SMatthias Ringwald void l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, 15967192e786SMatthias Ringwald uint16_t mtu, uint16_t mps, uint16_t initial_credits, gap_security_level_t security_level){ 15977192e786SMatthias Ringwald 15987192e786SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x mtu %u connection %p", psm, mtu, connection); 15997192e786SMatthias Ringwald 16007192e786SMatthias Ringwald // check for alread registered psm 16017192e786SMatthias Ringwald // TODO: emit error event 16027192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16037192e786SMatthias Ringwald if (service) { 16047192e786SMatthias Ringwald log_error("l2cap_le_register_service_internal: PSM %u already registered", psm); 16057192e786SMatthias Ringwald l2cap_emit_service_registered(connection, L2CAP_SERVICE_ALREADY_REGISTERED, psm); 16067192e786SMatthias Ringwald return; 16077192e786SMatthias Ringwald } 16087192e786SMatthias Ringwald 16097192e786SMatthias Ringwald // alloc structure 16107192e786SMatthias Ringwald // TODO: emit error event 16117192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 16127192e786SMatthias Ringwald if (!service) { 16137192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 16147192e786SMatthias Ringwald l2cap_emit_service_registered(connection, BTSTACK_MEMORY_ALLOC_FAILED, psm); 16157192e786SMatthias Ringwald return; 16167192e786SMatthias Ringwald } 16177192e786SMatthias Ringwald 16187192e786SMatthias Ringwald // fill in 16197192e786SMatthias Ringwald service->psm = psm; 16207192e786SMatthias Ringwald service->mtu = mtu; 16217192e786SMatthias Ringwald service->mps = mps; 16227192e786SMatthias Ringwald service->packet_handler = packet_handler; 16237192e786SMatthias Ringwald service->required_security_level = security_level; 16247192e786SMatthias Ringwald 16257192e786SMatthias Ringwald // add to services list 1626665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 16277192e786SMatthias Ringwald 16287192e786SMatthias Ringwald // done 16297192e786SMatthias Ringwald l2cap_emit_service_registered(connection, 0, psm); 16307192e786SMatthias Ringwald } 16317192e786SMatthias Ringwald 1632ffbf8201SMatthias Ringwald void l2cap_le_unregister_service(uint16_t psm) { 16337192e786SMatthias Ringwald 16347192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 16357192e786SMatthias Ringwald 16367192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 16377192e786SMatthias Ringwald if (!service) return; 1638665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 16397192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 16407192e786SMatthias Ringwald } 16417192e786SMatthias Ringwald #endif 16427f02f414SMatthias Ringwald #endif 1643