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 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "l2cap.c" 39ab2c6ae4SMatthias Ringwald 401713bceaSmatthias.ringwald /* 4143625864Smatthias.ringwald * l2cap.c 4243625864Smatthias.ringwald * 4343625864Smatthias.ringwald * Logical Link Control and Adaption Protocl (L2CAP) 4443625864Smatthias.ringwald * 4543625864Smatthias.ringwald * Created by Matthias Ringwald on 5/16/09. 4643625864Smatthias.ringwald */ 4743625864Smatthias.ringwald 4843625864Smatthias.ringwald #include "l2cap.h" 49645658c9Smatthias.ringwald #include "hci.h" 502b3c6c9bSmatthias.ringwald #include "hci_dump.h" 51235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 5216ece135SMatthias Ringwald #include "btstack_debug.h" 530e2df43fSMatthias Ringwald #include "btstack_event.h" 54d3a9df87Smatthias.ringwald #include "btstack_memory.h" 5543625864Smatthias.ringwald 56cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 5783fd9c76SMatthias Ringwald #include "ble/sm.h" 5883fd9c76SMatthias Ringwald #endif 5983fd9c76SMatthias Ringwald 6043625864Smatthias.ringwald #include <stdarg.h> 6143625864Smatthias.ringwald #include <string.h> 6243625864Smatthias.ringwald 6343625864Smatthias.ringwald #include <stdio.h> 6443625864Smatthias.ringwald 654c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance 664c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3 674c744e21Smatthias.ringwald 6839bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 69e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3 7039bda6d5Smatthias.ringwald 7185aeef60SMatthias Ringwald // nr of credits provided to remote if credits fall below watermark 7285aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK 5 7385aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT 5 7485aeef60SMatthias Ringwald 7500d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS 7600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0 7700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1 7800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2 7900d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4 8000d93d79Smatthias.ringwald 815628cf69SMatthias Ringwald // internal table 825628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0 835628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL 1 845628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2 855628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1) 865628cf69SMatthias Ringwald 8709e9d05bSMatthias Ringwald #if defined(ENABLE_LE_DATA_CHANNELS) || defined(ENABLE_CLASSIC) 8809e9d05bSMatthias Ringwald #define L2CAP_USES_CHANNELS 8909e9d05bSMatthias Ringwald #endif 9009e9d05bSMatthias Ringwald 9133c40538SMatthias Ringwald // prototypes 92675e6881SMatthias Ringwald static void l2cap_run(void); 9333c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 943d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ); 9530725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void); 9609e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel); 9709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 9809e9d05bSMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel); 9909e9d05bSMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm); 10009e9d05bSMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status); 10109e9d05bSMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel); 10209e9d05bSMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel); 10309e9d05bSMatthias Ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel); 10409e9d05bSMatthias Ringwald #endif 105cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 10657be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status); 10757be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel); 10857be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid); 10944276248SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel); 110828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel); 111e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm); 112e7d0c9aaSMatthias Ringwald #endif 113212b6be2SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 114212b6be2SMatthias Ringwald static int l2cap_ertm_num_unacknowledged_tx_packets(l2cap_channel_t * channel); 115212b6be2SMatthias Ringwald #endif 11633c40538SMatthias Ringwald 1175628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel { 1185628cf69SMatthias Ringwald btstack_packet_handler_t callback; 1192125de09SMatthias Ringwald uint8_t waiting_for_can_send_now; 1205628cf69SMatthias Ringwald } l2cap_fixed_channel_t; 1215628cf69SMatthias Ringwald 12209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1235628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels; 1245628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services; 12509e9d05bSMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp; 12609e9d05bSMatthias Ringwald #endif 12757be49d6SMatthias Ringwald 12857be49d6SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1295628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels; 1305628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services; 13157be49d6SMatthias Ringwald #endif 13239bda6d5Smatthias.ringwald 13339bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests 1342b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES]; 1352b83fb7dSmatthias.ringwald static int signaling_responses_pending; 1362b83fb7dSmatthias.ringwald 137fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 1385628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE]; 13939bda6d5Smatthias.ringwald 140d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE 141d6ed9f9cSMatthias Ringwald // only used for connection parameter update events 142d6ed9f9cSMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler; 143d6ed9f9cSMatthias Ringwald #endif 144d6ed9f9cSMatthias Ringwald 14585ddcd84SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 14685ddcd84SMatthias Ringwald 14785ddcd84SMatthias Ringwald /* 14885ddcd84SMatthias Ringwald * CRC lookup table for generator polynom D^16 + D^15 + D^2 + 1 14985ddcd84SMatthias Ringwald */ 15085ddcd84SMatthias Ringwald static const uint16_t crc16_table[256] = { 15185ddcd84SMatthias Ringwald 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440, 15285ddcd84SMatthias Ringwald 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40, 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841, 15385ddcd84SMatthias Ringwald 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40, 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41, 15485ddcd84SMatthias Ringwald 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040, 15585ddcd84SMatthias Ringwald 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240, 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441, 15685ddcd84SMatthias Ringwald 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41, 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840, 15785ddcd84SMatthias Ringwald 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41, 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40, 15885ddcd84SMatthias Ringwald 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640, 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041, 15985ddcd84SMatthias Ringwald 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240, 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441, 16085ddcd84SMatthias Ringwald 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840, 16185ddcd84SMatthias Ringwald 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40, 16285ddcd84SMatthias Ringwald 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640, 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041, 16385ddcd84SMatthias Ringwald 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440, 16485ddcd84SMatthias Ringwald 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841, 16585ddcd84SMatthias Ringwald 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41, 16685ddcd84SMatthias Ringwald 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040, 16785ddcd84SMatthias Ringwald }; 16885ddcd84SMatthias Ringwald 16985ddcd84SMatthias Ringwald static uint16_t crc16_calc(uint8_t * data, uint16_t len){ 17085ddcd84SMatthias Ringwald uint16_t crc = 0; // initial value = 0 17185ddcd84SMatthias Ringwald while (len--){ 17285ddcd84SMatthias Ringwald crc = (crc >> 8) ^ crc16_table[ (crc ^ ((uint16_t) *data++)) & 0x00FF ]; 17385ddcd84SMatthias Ringwald } 17485ddcd84SMatthias Ringwald return crc; 17585ddcd84SMatthias Ringwald } 17685ddcd84SMatthias Ringwald 17785ddcd84SMatthias Ringwald #endif 17885ddcd84SMatthias Ringwald 17985ddcd84SMatthias Ringwald 18034e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){ 18134e7e577SMatthias Ringwald switch (index){ 18234e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL: 18334e7e577SMatthias Ringwald return L2CAP_CID_ATTRIBUTE_PROTOCOL; 18434e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL: 18534e7e577SMatthias Ringwald return L2CAP_CID_SECURITY_MANAGER_PROTOCOL; 18634e7e577SMatthias Ringwald case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL: 18734e7e577SMatthias Ringwald return L2CAP_CID_CONNECTIONLESS_CHANNEL; 18834e7e577SMatthias Ringwald default: 18934e7e577SMatthias Ringwald return 0; 19034e7e577SMatthias Ringwald } 19134e7e577SMatthias Ringwald } 1925628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){ 1935628cf69SMatthias Ringwald switch (channel_id){ 1945628cf69SMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 1955628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL; 1965628cf69SMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 1975628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL; 1985628cf69SMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 1995628cf69SMatthias Ringwald return L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL; 2005628cf69SMatthias Ringwald default: 2015628cf69SMatthias Ringwald return -1; 2025628cf69SMatthias Ringwald } 2035628cf69SMatthias Ringwald } 20439bda6d5Smatthias.ringwald 20534e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){ 20634e7e577SMatthias Ringwald if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0; 20734e7e577SMatthias Ringwald return 1; 20834e7e577SMatthias Ringwald } 20934e7e577SMatthias Ringwald 21071de195eSMatthias Ringwald void l2cap_init(void){ 2112b83fb7dSmatthias.ringwald signaling_responses_pending = 0; 212808a48abSmatthias.ringwald 21309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 214f5454fc6Smatthias.ringwald l2cap_channels = NULL; 215f5454fc6Smatthias.ringwald l2cap_services = NULL; 21609e9d05bSMatthias Ringwald require_security_level2_for_outgoing_sdp = 0; 21709e9d05bSMatthias Ringwald #endif 218a3dc965aSMatthias Ringwald 219a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 2207192e786SMatthias Ringwald l2cap_le_services = NULL; 2217192e786SMatthias Ringwald l2cap_le_channels = NULL; 222a3dc965aSMatthias Ringwald #endif 223f5454fc6Smatthias.ringwald 224d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE 22533c40538SMatthias Ringwald l2cap_event_packet_handler = NULL; 226d6ed9f9cSMatthias Ringwald #endif 2275628cf69SMatthias Ringwald memset(fixed_channels, 0, sizeof(fixed_channels)); 228f5454fc6Smatthias.ringwald 229fcadd0caSmatthias.ringwald // 2302718e2e7Smatthias.ringwald // register callback with HCI 231fcadd0caSmatthias.ringwald // 232d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &l2cap_hci_event_handler; 233fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 234fb37a842SMatthias Ringwald 235d9a7306aSMatthias Ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 236fb37a842SMatthias Ringwald 237be005ed6SMatthias Ringwald #ifdef ENABLE_CLASSIC 23815a95bd5SMatthias Ringwald gap_connectable_control(0); // no services yet 239be005ed6SMatthias Ringwald #endif 240fcadd0caSmatthias.ringwald } 241fcadd0caSmatthias.ringwald 242ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 243d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE 24433c40538SMatthias Ringwald l2cap_event_packet_handler = handler; 245d6ed9f9cSMatthias Ringwald #else 246d6ed9f9cSMatthias Ringwald UNUSED(handler); 247d6ed9f9cSMatthias Ringwald #endif 2481e6aba47Smatthias.ringwald } 2491e6aba47Smatthias.ringwald 25009e9d05bSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){ 2519ec2630cSMatthias Ringwald UNUSED(con_handle); 2529ec2630cSMatthias Ringwald 25309e9d05bSMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 25409e9d05bSMatthias Ringwald if (index < 0) return; 25509e9d05bSMatthias Ringwald fixed_channels[index].waiting_for_can_send_now = 1; 25609e9d05bSMatthias Ringwald l2cap_notify_channel_can_send(); 25709e9d05bSMatthias Ringwald } 25809e9d05bSMatthias Ringwald 25909e9d05bSMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){ 2609ec2630cSMatthias Ringwald UNUSED(channel_id); 2619ec2630cSMatthias Ringwald 26209e9d05bSMatthias Ringwald return hci_can_send_acl_packet_now(con_handle); 26309e9d05bSMatthias Ringwald } 26409e9d05bSMatthias Ringwald 26509e9d05bSMatthias Ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 26609e9d05bSMatthias Ringwald return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 26709e9d05bSMatthias Ringwald } 26809e9d05bSMatthias Ringwald 26909e9d05bSMatthias Ringwald int l2cap_reserve_packet_buffer(void){ 27009e9d05bSMatthias Ringwald return hci_reserve_packet_buffer(); 27109e9d05bSMatthias Ringwald } 27209e9d05bSMatthias Ringwald 27309e9d05bSMatthias Ringwald void l2cap_release_packet_buffer(void){ 27409e9d05bSMatthias Ringwald hci_release_packet_buffer(); 27509e9d05bSMatthias Ringwald } 27609e9d05bSMatthias Ringwald 277f511cefaSMatthias Ringwald static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint8_t packet_boundary, uint16_t remote_cid, uint16_t len){ 27809e9d05bSMatthias Ringwald // 0 - Connection handle : PB=pb : BC=00 279f511cefaSMatthias Ringwald little_endian_store_16(acl_buffer, 0, con_handle | (packet_boundary << 12) | (0 << 14)); 28009e9d05bSMatthias Ringwald // 2 - ACL length 28109e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 28209e9d05bSMatthias Ringwald // 4 - L2CAP packet length 28309e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 28409e9d05bSMatthias Ringwald // 6 - L2CAP channel DEST 28509e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 6, remote_cid); 28609e9d05bSMatthias Ringwald } 28709e9d05bSMatthias Ringwald 288f511cefaSMatthias Ringwald // assumption - only on LE connections 28909e9d05bSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){ 29009e9d05bSMatthias Ringwald 29109e9d05bSMatthias Ringwald if (!hci_is_packet_buffer_reserved()){ 29209e9d05bSMatthias Ringwald log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 29309e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 29409e9d05bSMatthias Ringwald } 29509e9d05bSMatthias Ringwald 29609e9d05bSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(con_handle)){ 29709e9d05bSMatthias Ringwald log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid); 29809e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 29909e9d05bSMatthias Ringwald } 30009e9d05bSMatthias Ringwald 30109e9d05bSMatthias Ringwald log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid); 30209e9d05bSMatthias Ringwald 30309e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 304f511cefaSMatthias Ringwald l2cap_setup_header(acl_buffer, con_handle, 0, cid, len); 30509e9d05bSMatthias Ringwald // send 30609e9d05bSMatthias Ringwald return hci_send_acl_packet_buffer(len+8); 30709e9d05bSMatthias Ringwald } 30809e9d05bSMatthias Ringwald 309f511cefaSMatthias Ringwald // assumption - only on LE connections 31009e9d05bSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){ 31109e9d05bSMatthias Ringwald 31209e9d05bSMatthias Ringwald if (!hci_can_send_acl_packet_now(con_handle)){ 31309e9d05bSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 31409e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 31509e9d05bSMatthias Ringwald } 31609e9d05bSMatthias Ringwald 31709e9d05bSMatthias Ringwald hci_reserve_packet_buffer(); 31809e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 31909e9d05bSMatthias Ringwald 32009e9d05bSMatthias Ringwald memcpy(&acl_buffer[8], data, len); 32109e9d05bSMatthias Ringwald 32209e9d05bSMatthias Ringwald return l2cap_send_prepared_connectionless(con_handle, cid, len); 32309e9d05bSMatthias Ringwald } 32409e9d05bSMatthias Ringwald 32509e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) { 326d9d23054SMatthias Ringwald log_debug("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel); 32709e9d05bSMatthias Ringwald uint8_t event[4]; 32809e9d05bSMatthias Ringwald event[0] = L2CAP_EVENT_CAN_SEND_NOW; 32909e9d05bSMatthias Ringwald event[1] = sizeof(event) - 2; 33009e9d05bSMatthias Ringwald little_endian_store_16(event, 2, channel); 33109e9d05bSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 33209e9d05bSMatthias Ringwald packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); 33309e9d05bSMatthias Ringwald } 33409e9d05bSMatthias Ringwald 33509e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS 33617a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 33758de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 33858de5610Smatthias.ringwald } 33958de5610Smatthias.ringwald 34044276248SMatthias Ringwald static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){ 34144276248SMatthias Ringwald uint8_t event[4]; 34244276248SMatthias Ringwald event[0] = event_code; 34344276248SMatthias Ringwald event[1] = sizeof(event) - 2; 34444276248SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 34544276248SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 34644276248SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 34744276248SMatthias Ringwald } 34809e9d05bSMatthias Ringwald #endif 34944276248SMatthias Ringwald 35009e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 35158de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 352c9dc710bS[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", 353fc64f94aSMatthias Ringwald status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 354c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 355bab5f4f0SMatthias Ringwald uint8_t event[24]; 35658de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 35758de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 35858de5610Smatthias.ringwald event[2] = status; 359724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 360fc64f94aSMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 361f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 362f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 363f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 364f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 365f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 366f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 367bab5f4f0SMatthias Ringwald event[23] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0; 36858de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 36917a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 37058de5610Smatthias.ringwald } 37158de5610Smatthias.ringwald 37244276248SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 373e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 37444276248SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 37558de5610Smatthias.ringwald } 37658de5610Smatthias.ringwald 37744276248SMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) { 378e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 379fc64f94aSMatthias Ringwald bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid); 38058de5610Smatthias.ringwald uint8_t event[16]; 38158de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 38258de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 383724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 384fc64f94aSMatthias Ringwald little_endian_store_16(event, 8, channel->con_handle); 385f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 386f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 387f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 38858de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 38917a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 3900af41d30Smatthias.ringwald } 391808a48abSmatthias.ringwald 3927f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 393665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 394665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 395665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 396665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 397b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 398f62db1e3Smatthias.ringwald return channel; 399f62db1e3Smatthias.ringwald } 400f62db1e3Smatthias.ringwald } 401f62db1e3Smatthias.ringwald return NULL; 402f62db1e3Smatthias.ringwald } 403f62db1e3Smatthias.ringwald 4040b9d7e78SMatthias Ringwald /// 4050b9d7e78SMatthias Ringwald 40630725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){ 40730725612SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 40830725612SMatthias Ringwald if (!channel) return; 40930725612SMatthias Ringwald channel->waiting_for_can_send_now = 1; 41030725612SMatthias Ringwald l2cap_notify_channel_can_send(); 41130725612SMatthias Ringwald } 41230725612SMatthias Ringwald 4136b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 4146b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 4156b1fde37Smatthias.ringwald if (!channel) return 0; 416*0b20b13bSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 417*0b20b13bSMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 418*0b20b13bSMatthias Ringwald // get num free tx buffers 419*0b20b13bSMatthias Ringwald int num_tx_buffers_used = channel->tx_write_index - channel->tx_read_index; 420*0b20b13bSMatthias Ringwald if (num_tx_buffers_used < 0){ 421*0b20b13bSMatthias Ringwald num_tx_buffers_used += channel->num_tx_buffers; 422*0b20b13bSMatthias Ringwald } 423*0b20b13bSMatthias Ringwald int num_free_tx_buffers = channel->num_tx_buffers - num_tx_buffers_used; 424*0b20b13bSMatthias Ringwald // calculate num tx buffers for remote MTU 425*0b20b13bSMatthias Ringwald int num_tx_buffers_for_max_remote_mtu; 426*0b20b13bSMatthias Ringwald if (channel->remote_mtu <= channel->remote_mps){ 427*0b20b13bSMatthias Ringwald // MTU fits into single packet 428*0b20b13bSMatthias Ringwald num_tx_buffers_for_max_remote_mtu = 1; 429*0b20b13bSMatthias Ringwald } else { 430*0b20b13bSMatthias Ringwald // include SDU Length 431*0b20b13bSMatthias Ringwald num_tx_buffers_for_max_remote_mtu = (channel->remote_mtu + 2 + (channel->remote_mps - 1)) / channel->remote_mps; 432*0b20b13bSMatthias Ringwald } 433*0b20b13bSMatthias Ringwald return num_tx_buffers_for_max_remote_mtu <= num_free_tx_buffers; 434*0b20b13bSMatthias Ringwald } 435*0b20b13bSMatthias Ringwald #endif 4360b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(channel->con_handle); 4377856fb31S[email protected] } 4387856fb31S[email protected] 43983e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 44083e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 44183e7cdd9SMatthias Ringwald if (!channel) return 0; 442*0b20b13bSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 443*0b20b13bSMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 444*0b20b13bSMatthias Ringwald return 0; 445*0b20b13bSMatthias Ringwald } 446*0b20b13bSMatthias Ringwald #endif 4470b9d7e78SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->con_handle); 44883e7cdd9SMatthias Ringwald } 449*0b20b13bSMatthias Ringwald 45096cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 45196cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 45296cbd662Smatthias.ringwald if (channel) { 45396cbd662Smatthias.ringwald return channel->remote_mtu; 45496cbd662Smatthias.ringwald } 45596cbd662Smatthias.ringwald return 0; 45696cbd662Smatthias.ringwald } 45796cbd662Smatthias.ringwald 458ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 459665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 460665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 461665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 462665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 4635932bd7cS[email protected] if ( &channel->rtx == ts) { 4645932bd7cS[email protected] return channel; 4655932bd7cS[email protected] } 4665932bd7cS[email protected] } 4675932bd7cS[email protected] return NULL; 4685932bd7cS[email protected] } 4695932bd7cS[email protected] 470ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 4715932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 47295d2c8f4SMatthias Ringwald if (!channel) return; 4735932bd7cS[email protected] 4745932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 4755932bd7cS[email protected] 4765932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 4775932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 4785932bd7cS[email protected] // notify client 4795932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 4805932bd7cS[email protected] 4815932bd7cS[email protected] // discard channel 4829dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 483665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 4845932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 4855932bd7cS[email protected] } 4865932bd7cS[email protected] 4875932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 4885932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 489528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 4905932bd7cS[email protected] } 4915932bd7cS[email protected] 4925932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 4935932bd7cS[email protected] l2cap_stop_rtx(channel); 494cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 495528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 496528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 497528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 4985932bd7cS[email protected] } 4995932bd7cS[email protected] 5005932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 5015932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 5025932bd7cS[email protected] l2cap_stop_rtx(channel); 503528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 504528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 505528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 5065932bd7cS[email protected] } 5075932bd7cS[email protected] 50871de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 509ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 510ac301f95S[email protected] } 511ac301f95S[email protected] 512df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 513235946f1SMatthias Ringwald return (psm == BLUETOOTH_PROTOCOL_SDP) && (!require_security_level2_for_outgoing_sdp); 514df3354fcS[email protected] } 5155932bd7cS[email protected] 5167f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 517a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 5189da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 519b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 520b1d43497Smatthias.ringwald } 521b1d43497Smatthias.ringwald 5229da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 5232a373862S[email protected] hci_reserve_packet_buffer(); 524facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 52558de5610Smatthias.ringwald va_list argptr; 52658de5610Smatthias.ringwald va_start(argptr, identifier); 52770efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 52858de5610Smatthias.ringwald va_end(argptr); 5299da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 530826f7347S[email protected] return hci_send_acl_packet_buffer(len); 53158de5610Smatthias.ringwald } 53258de5610Smatthias.ringwald 533f511cefaSMatthias Ringwald // assumption - only on Classic connections 534b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 535b1d43497Smatthias.ringwald 536c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 537c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 538c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 539c8b9416aS[email protected] } 540c8b9416aS[email protected] 54158de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 542b1d43497Smatthias.ringwald if (!channel) { 5439da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 544b1d43497Smatthias.ringwald return -1; // TODO: define error 5456218e6f1Smatthias.ringwald } 5466218e6f1Smatthias.ringwald 547fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){ 5489da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 549a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 550a35252c8S[email protected] } 551a35252c8S[email protected] 552fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle); 553b1d43497Smatthias.ringwald 55485ddcd84SMatthias Ringwald int fcs_size = 0; 55585ddcd84SMatthias Ringwald 55685ddcd84SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 55785ddcd84SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 55885ddcd84SMatthias Ringwald fcs_size = 2; 55985ddcd84SMatthias Ringwald } 56085ddcd84SMatthias Ringwald #endif 56185ddcd84SMatthias Ringwald 562f511cefaSMatthias Ringwald // set non-flushable packet boundary flag if supported on Controller 563facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 564f511cefaSMatthias Ringwald uint8_t packet_boundary_flag = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 56585ddcd84SMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, packet_boundary_flag, channel->remote_cid, len + fcs_size); 56685ddcd84SMatthias Ringwald 56785ddcd84SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 56885ddcd84SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 56985ddcd84SMatthias Ringwald // calculate FCS over l2cap data 57085ddcd84SMatthias Ringwald uint16_t fcs = crc16_calc(acl_buffer + 4, 4 + len); 57185ddcd84SMatthias Ringwald log_info("I-Frame: fcs 0x%04x", fcs); 57285ddcd84SMatthias Ringwald little_endian_store_16(acl_buffer, 8 + len, fcs); 57358de5610Smatthias.ringwald } 57485ddcd84SMatthias Ringwald #endif 57585ddcd84SMatthias Ringwald 57685ddcd84SMatthias Ringwald // send 57785ddcd84SMatthias Ringwald return hci_send_acl_packet_buffer(len+8+fcs_size); 57885ddcd84SMatthias Ringwald } 57985ddcd84SMatthias Ringwald 58085ddcd84SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 58185ddcd84SMatthias Ringwald static inline uint16_t l2cap_encanced_control_field_for_information_frame(uint8_t tx_seq, int final, uint8_t req_seq, l2cap_segmentation_and_reassembly_t sar){ 58285ddcd84SMatthias Ringwald return (((uint16_t) sar) << 14) | (req_seq << 8) | (final << 7) | (tx_seq << 1) | 0; 58385ddcd84SMatthias Ringwald } 58485ddcd84SMatthias Ringwald static inline uint16_t l2cap_encanced_control_field_for_supevisor_frame(l2cap_supervisory_function_t supervisory_function, int poll, int final, uint8_t req_seq){ 58538f62777SMatthias Ringwald return (req_seq << 8) | (final << 7) | (poll << 4) | (((int) supervisory_function) << 2) | 1; 58685ddcd84SMatthias Ringwald } 58785ddcd84SMatthias Ringwald static int l2cap_next_ertm_seq_nr(int seq_nr){ 58885ddcd84SMatthias Ringwald return (seq_nr + 1) & 0x3f; 58985ddcd84SMatthias Ringwald } 590f0fb4cd7SMatthias Ringwald static void l2cap_ertm_next_tx_write_index(l2cap_channel_t * channel){ 591f0fb4cd7SMatthias Ringwald channel->tx_write_index++; 592f0fb4cd7SMatthias Ringwald if (channel->tx_write_index < channel->num_tx_buffers) return; 593f0fb4cd7SMatthias Ringwald channel->tx_write_index = 0; 594f0fb4cd7SMatthias Ringwald } 59520fa474dSMatthias Ringwald static void l2cap_ertm_monitor_timeout_callback(btstack_timer_source_t * ts){ 59620fa474dSMatthias Ringwald log_info("l2cap_ertm_monitor_timeout_callback"); 597db55d2e9SMatthias Ringwald l2cap_channel_t * l2cap_channel = (l2cap_channel_t *) btstack_run_loop_get_timer_context(ts); 598db55d2e9SMatthias Ringwald 599db55d2e9SMatthias Ringwald // TODO: we assume that it's the oldest packet 600db55d2e9SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state; 601db55d2e9SMatthias Ringwald tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index]; 602db55d2e9SMatthias Ringwald 603db55d2e9SMatthias Ringwald // check retry count 604db55d2e9SMatthias Ringwald if (tx_state->retry_count < l2cap_channel->remote_max_transmit){ 605db55d2e9SMatthias Ringwald // increment retry count 606db55d2e9SMatthias Ringwald tx_state->retry_count++; 607db55d2e9SMatthias Ringwald 608db55d2e9SMatthias Ringwald // start monitor timer 609db55d2e9SMatthias Ringwald btstack_run_loop_set_timer_handler(&tx_state->monitor_timer, &l2cap_ertm_monitor_timeout_callback); 610db55d2e9SMatthias Ringwald btstack_run_loop_set_timer_context(&tx_state->monitor_timer, l2cap_channel); 611db55d2e9SMatthias Ringwald btstack_run_loop_set_timer(&tx_state->monitor_timer, l2cap_channel->local_monitor_timeout_ms); 612db55d2e9SMatthias Ringwald btstack_run_loop_add_timer(&tx_state->monitor_timer); 613db55d2e9SMatthias Ringwald 614db55d2e9SMatthias Ringwald // send RR/P=1 615db55d2e9SMatthias Ringwald l2cap_channel->send_supervisor_frame_receiver_ready_poll = 1; 616db55d2e9SMatthias Ringwald } else { 617db55d2e9SMatthias Ringwald log_info("Monitor timer expired & retry count >= max transmit -> disconnect"); 618db55d2e9SMatthias Ringwald l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 619db55d2e9SMatthias Ringwald } 620db55d2e9SMatthias Ringwald l2cap_run(); 621db55d2e9SMatthias Ringwald } 622db55d2e9SMatthias Ringwald static void l2cap_ertm_retransmission_timeout_callback(btstack_timer_source_t * ts){ 623db55d2e9SMatthias Ringwald log_info("l2cap_ertm_retransmission_timeout_callback"); 624db55d2e9SMatthias Ringwald l2cap_channel_t * l2cap_channel = (l2cap_channel_t *) btstack_run_loop_get_timer_context(ts); 625db55d2e9SMatthias Ringwald 626db55d2e9SMatthias Ringwald // TODO: we assume that it's the oldest packet 627db55d2e9SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state; 628db55d2e9SMatthias Ringwald tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index]; 629db55d2e9SMatthias Ringwald 630db55d2e9SMatthias Ringwald // set retry count = 1 631db55d2e9SMatthias Ringwald tx_state->retry_count = 1; 632db55d2e9SMatthias Ringwald 633db55d2e9SMatthias Ringwald // start monitor timer 634db55d2e9SMatthias Ringwald btstack_run_loop_set_timer_handler(&tx_state->monitor_timer, &l2cap_ertm_monitor_timeout_callback); 635db55d2e9SMatthias Ringwald btstack_run_loop_set_timer_context(&tx_state->monitor_timer, l2cap_channel); 636db55d2e9SMatthias Ringwald btstack_run_loop_set_timer(&tx_state->monitor_timer, l2cap_channel->local_monitor_timeout_ms); 637db55d2e9SMatthias Ringwald btstack_run_loop_add_timer(&tx_state->monitor_timer); 638db55d2e9SMatthias Ringwald 639db55d2e9SMatthias Ringwald // send RR/P=1 640db55d2e9SMatthias Ringwald l2cap_channel->send_supervisor_frame_receiver_ready_poll = 1; 64120fa474dSMatthias Ringwald l2cap_run(); 64220fa474dSMatthias Ringwald } 6437b7901d8SMatthias Ringwald static int l2cap_ertm_send_information_frame(l2cap_channel_t * channel, int index, int final){ 644f0fb4cd7SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index]; 645f0fb4cd7SMatthias Ringwald hci_reserve_packet_buffer(); 646f0fb4cd7SMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 64782bb0e22SMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_information_frame(tx_state->tx_seq, final, channel->req_seq, tx_state->sar); 648f0fb4cd7SMatthias Ringwald log_info("I-Frame: control 0x%04x", control); 649f0fb4cd7SMatthias Ringwald little_endian_store_16(acl_buffer, 8, control); 650f0fb4cd7SMatthias Ringwald memcpy(&acl_buffer[8+2], &channel->tx_packets_data[index * channel->local_mtu], tx_state->len); 651f0fb4cd7SMatthias Ringwald // send 652f0fb4cd7SMatthias Ringwald return l2cap_send_prepared(channel->local_cid, 2 + tx_state->len); 653f0fb4cd7SMatthias Ringwald } 65482bb0e22SMatthias Ringwald 6557bebc11cSMatthias Ringwald static void l2cap_ertm_store_fragment(l2cap_channel_t * channel, l2cap_segmentation_and_reassembly_t sar, uint16_t sdu_length, uint8_t * data, uint16_t len){ 6567bebc11cSMatthias Ringwald // get next index for storing packets 657f0fb4cd7SMatthias Ringwald int index = channel->tx_write_index; 65882bb0e22SMatthias Ringwald 659f0fb4cd7SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index]; 660f0fb4cd7SMatthias Ringwald tx_state->tx_seq = channel->next_tx_seq; 661f0fb4cd7SMatthias Ringwald tx_state->len = len; 6627bebc11cSMatthias Ringwald tx_state->sar = sar; 663db55d2e9SMatthias Ringwald tx_state->retry_count = 0; 66482bb0e22SMatthias Ringwald 66582bb0e22SMatthias Ringwald uint8_t * tx_packet = &channel->tx_packets_data[index * channel->local_mtu]; 6667bebc11cSMatthias Ringwald int pos = 0; 6677bebc11cSMatthias Ringwald if (sar == L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU){ 6687bebc11cSMatthias Ringwald little_endian_store_16(tx_packet, 0, sdu_length); 6697bebc11cSMatthias Ringwald pos += 2; 6707bebc11cSMatthias Ringwald } 6717bebc11cSMatthias Ringwald memcpy(&tx_packet[pos], data, len); 67282bb0e22SMatthias Ringwald 673f0fb4cd7SMatthias Ringwald // update 674f0fb4cd7SMatthias Ringwald channel->next_tx_seq = l2cap_next_ertm_seq_nr(channel->next_tx_seq); 675f0fb4cd7SMatthias Ringwald l2cap_ertm_next_tx_write_index(channel); 67682bb0e22SMatthias Ringwald 67762041d70SMatthias Ringwald // set retransmission timer 67862041d70SMatthias Ringwald btstack_run_loop_set_timer_handler(&tx_state->retransmission_timer, &l2cap_ertm_retransmission_timeout_callback); 67962041d70SMatthias Ringwald btstack_run_loop_set_timer_context(&tx_state->retransmission_timer, channel); 68062041d70SMatthias Ringwald btstack_run_loop_set_timer(&tx_state->retransmission_timer, channel->local_retransmission_timeout_ms); 68162041d70SMatthias Ringwald btstack_run_loop_add_timer(&tx_state->retransmission_timer); 6827bebc11cSMatthias Ringwald } 6837bebc11cSMatthias Ringwald 6847bebc11cSMatthias Ringwald static int l2cap_ertm_send(l2cap_channel_t * channel, uint8_t * data, uint16_t len){ 6857bebc11cSMatthias Ringwald if (len > channel->remote_mtu){ 6867bebc11cSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", channel->local_cid); 6877bebc11cSMatthias Ringwald return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 6887bebc11cSMatthias Ringwald } 6897bebc11cSMatthias Ringwald 6907bebc11cSMatthias Ringwald // check if it needs to get fragmented 6917bebc11cSMatthias Ringwald if (len > channel->remote_mps){ 6927bebc11cSMatthias Ringwald // fragmentation needed. 6937bebc11cSMatthias Ringwald l2cap_segmentation_and_reassembly_t sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU; 6947bebc11cSMatthias Ringwald int chunk_len; 6957bebc11cSMatthias Ringwald while (len){ 6967bebc11cSMatthias Ringwald switch (sar){ 6977bebc11cSMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU: 6987bebc11cSMatthias Ringwald chunk_len = channel->remote_mps - 2; // sdu_length 6997bebc11cSMatthias Ringwald l2cap_ertm_store_fragment(channel, sar, len, data, chunk_len); 7007bebc11cSMatthias Ringwald len -= chunk_len; 7017bebc11cSMatthias Ringwald sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU; 7027bebc11cSMatthias Ringwald break; 7037bebc11cSMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU: 7047bebc11cSMatthias Ringwald chunk_len = channel->remote_mps; 7057bebc11cSMatthias Ringwald if (chunk_len >= len){ 7067bebc11cSMatthias Ringwald sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU; 7077bebc11cSMatthias Ringwald chunk_len = len; 7087bebc11cSMatthias Ringwald } 7097bebc11cSMatthias Ringwald l2cap_ertm_store_fragment(channel, sar, len, data, chunk_len); 7107bebc11cSMatthias Ringwald len -= chunk_len; 7117bebc11cSMatthias Ringwald break; 7127bebc11cSMatthias Ringwald default: 7137bebc11cSMatthias Ringwald break; 7147bebc11cSMatthias Ringwald } 7157bebc11cSMatthias Ringwald } 7167bebc11cSMatthias Ringwald 7177bebc11cSMatthias Ringwald } else { 7187bebc11cSMatthias Ringwald l2cap_ertm_store_fragment(channel, L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU, 0, data, len); 7197bebc11cSMatthias Ringwald } 72082bb0e22SMatthias Ringwald 721675e6881SMatthias Ringwald // try to send 722675e6881SMatthias Ringwald l2cap_run(); 723f0fb4cd7SMatthias Ringwald return 0; 724f0fb4cd7SMatthias Ringwald } 72585ddcd84SMatthias Ringwald #endif 72658de5610Smatthias.ringwald 727f511cefaSMatthias Ringwald // assumption - only on Classic connections 728ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 729a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 730a35252c8S[email protected] if (!channel) { 731ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 732a35252c8S[email protected] return -1; // TODO: define error 733a35252c8S[email protected] } 734a35252c8S[email protected] 735f0fb4cd7SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 736f0fb4cd7SMatthias Ringwald // send in ERTM 737f0fb4cd7SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 738f0fb4cd7SMatthias Ringwald return l2cap_ertm_send(channel, data, len); 739f0fb4cd7SMatthias Ringwald } 740f0fb4cd7SMatthias Ringwald #endif 741f0fb4cd7SMatthias Ringwald 742f0efaa57S[email protected] if (len > channel->remote_mtu){ 743ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 744f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 745f0efaa57S[email protected] } 746f0efaa57S[email protected] 747fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)){ 748ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 749b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 750b1d43497Smatthias.ringwald } 751b1d43497Smatthias.ringwald 7522a373862S[email protected] hci_reserve_packet_buffer(); 753facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 754f0fb4cd7SMatthias Ringwald memcpy(&acl_buffer[8], data, len); 755f0fb4cd7SMatthias Ringwald return l2cap_send_prepared(local_cid, len); 756b1d43497Smatthias.ringwald } 757b1d43497Smatthias.ringwald 758fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){ 759fc64f94aSMatthias Ringwald return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data); 7600e37e417S[email protected] } 7610e37e417S[email protected] 76228ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 76328ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 76428ca2b46S[email protected] } 76528ca2b46S[email protected] 76628ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 76728ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 76828ca2b46S[email protected] } 76909e9d05bSMatthias Ringwald #endif 77028ca2b46S[email protected] 77128ca2b46S[email protected] 77209e9d05bSMatthias Ringwald #ifdef ENABLE_BLE 77309e9d05bSMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 77409e9d05bSMatthias Ringwald 77509e9d05bSMatthias Ringwald if (!hci_can_send_acl_packet_now(handle)){ 77609e9d05bSMatthias Ringwald log_info("l2cap_send_le_signaling_packet, cannot send"); 77709e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 77809e9d05bSMatthias Ringwald } 77909e9d05bSMatthias Ringwald 78009e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet type %u", cmd); 78109e9d05bSMatthias Ringwald hci_reserve_packet_buffer(); 78209e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 78309e9d05bSMatthias Ringwald va_list argptr; 78409e9d05bSMatthias Ringwald va_start(argptr, identifier); 78509e9d05bSMatthias Ringwald uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 78609e9d05bSMatthias Ringwald va_end(argptr); 78709e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet con %u!", handle); 78809e9d05bSMatthias Ringwald return hci_send_acl_packet_buffer(len); 78909e9d05bSMatthias Ringwald } 79009e9d05bSMatthias Ringwald #endif 79109e9d05bSMatthias Ringwald 79209e9d05bSMatthias Ringwald uint16_t l2cap_max_mtu(void){ 79309e9d05bSMatthias Ringwald return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 79409e9d05bSMatthias Ringwald } 79509e9d05bSMatthias Ringwald 79609e9d05bSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 79709e9d05bSMatthias Ringwald return l2cap_max_mtu(); 79809e9d05bSMatthias Ringwald } 799b1d43497Smatthias.ringwald 8006dca2a0cSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 801450ad7ecSMatthias Ringwald static uint16_t l2cap_setup_options_ertm(l2cap_channel_t * channel, uint8_t * config_options){ 8026dca2a0cSMatthias Ringwald config_options[0] = 0x04; // RETRANSMISSION AND FLOW CONTROL OPTION 8036dca2a0cSMatthias Ringwald config_options[1] = 9; // length 8046dca2a0cSMatthias Ringwald config_options[2] = (uint8_t) channel->mode; 8057b181629SMatthias Ringwald config_options[3] = channel->num_rx_buffers; // == TxWindows size 806bbc0a9e7SMatthias Ringwald config_options[4] = channel->local_max_transmit; 807bbc0a9e7SMatthias Ringwald little_endian_store_16( config_options, 5, channel->local_retransmission_timeout_ms); 808bbc0a9e7SMatthias Ringwald little_endian_store_16( config_options, 7, channel->local_monitor_timeout_ms); 809843bae5dSMatthias Ringwald little_endian_store_16( config_options, 9, channel->local_mps); 810450ad7ecSMatthias Ringwald return 11; 8116dca2a0cSMatthias Ringwald } 81238f62777SMatthias Ringwald static int l2cap_ertm_send_supervisor_frame(l2cap_channel_t * channel, uint16_t control){ 81338f62777SMatthias Ringwald hci_reserve_packet_buffer(); 81438f62777SMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 81538f62777SMatthias Ringwald log_info("S-Frame: control 0x%04x", control); 81638f62777SMatthias Ringwald little_endian_store_16(acl_buffer, 8, control); 81738f62777SMatthias Ringwald return l2cap_send_prepared(channel->local_cid, 2); 81838f62777SMatthias Ringwald } 819212b6be2SMatthias Ringwald static int l2cap_ertm_num_unacknowledged_tx_packets(l2cap_channel_t * channel){ 820212b6be2SMatthias Ringwald int unacknowledged_packets = channel->tx_send_index - channel->tx_read_index; 821212b6be2SMatthias Ringwald if (unacknowledged_packets < 0){ 822212b6be2SMatthias Ringwald unacknowledged_packets += channel->num_tx_buffers; 823212b6be2SMatthias Ringwald } 824212b6be2SMatthias Ringwald return unacknowledged_packets; 825212b6be2SMatthias Ringwald } 826450ad7ecSMatthias Ringwald #endif 827450ad7ecSMatthias Ringwald 828d2afdd38SMatthias Ringwald #ifdef ENABLE_CLASSIC 829d2afdd38SMatthias Ringwald 830d2afdd38SMatthias Ringwald static uint16_t l2cap_setup_options_mtu(l2cap_channel_t * channel, uint8_t * config_options){ 831d2afdd38SMatthias Ringwald config_options[0] = 1; // MTU 832d2afdd38SMatthias Ringwald config_options[1] = 2; // len param 833d2afdd38SMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 834d2afdd38SMatthias Ringwald return 4; 835d2afdd38SMatthias Ringwald } 836d2afdd38SMatthias Ringwald 837450ad7ecSMatthias Ringwald static uint16_t l2cap_setup_options(l2cap_channel_t * channel, uint8_t * config_options){ 838450ad7ecSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 839450ad7ecSMatthias Ringwald // use ERTM options if supported 840450ad7ecSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(channel->con_handle); 841450ad7ecSMatthias Ringwald if ((connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_DONE) && (connection->l2cap_state.extended_feature_mask & 0x08)){ 842450ad7ecSMatthias Ringwald return l2cap_setup_options_ertm(channel, config_options); 843450ad7ecSMatthias Ringwald 844450ad7ecSMatthias Ringwald } 845450ad7ecSMatthias Ringwald #endif 846450ad7ecSMatthias Ringwald return l2cap_setup_options_mtu(channel, config_options); 8476dca2a0cSMatthias Ringwald } 8486dca2a0cSMatthias Ringwald 8491b9cb13dSMatthias Ringwald static uint32_t l2cap_extended_features_mask(void){ 8501b9cb13dSMatthias Ringwald // extended features request supported, features: fixed channels, unicast connectionless data reception 8511b9cb13dSMatthias Ringwald uint32_t features = 0x280; 8521b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 8531b9cb13dSMatthias Ringwald features |= 0x0008; 8541b9cb13dSMatthias Ringwald #endif 8551b9cb13dSMatthias Ringwald return features; 8561b9cb13dSMatthias Ringwald } 857d2afdd38SMatthias Ringwald #endif 8581b9cb13dSMatthias Ringwald 8598158c421Smatthias.ringwald // MARK: L2CAP_RUN 8602cd0be45Smatthias.ringwald // process outstanding signaling tasks 8617f02f414SMatthias Ringwald static void l2cap_run(void){ 8622b83fb7dSmatthias.ringwald 86322c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 86422c29ab4SMatthias Ringwald 8652b83fb7dSmatthias.ringwald // check pending signaling responses 8662b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 8672b83fb7dSmatthias.ringwald 8682b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 869a35252c8S[email protected] 870a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 871a35252c8S[email protected] 8722b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 873e74c5f58SMatthias Ringwald uint8_t response_code = signaling_responses[0].code; 8742b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 87563a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 876b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 877b3264428SMatthias Ringwald uint16_t source_cid = signaling_responses[0].cid; // CONNECTION_REQUEST 878b3264428SMatthias Ringwald #endif 879f3963406SMatthias Ringwald UNUSED(infoType); 88009e9d05bSMatthias Ringwald 881f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 882f53da564S[email protected] signaling_responses_pending--; 883f53da564S[email protected] int i; 884f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 885f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 886f53da564S[email protected] } 887f53da564S[email protected] 888f53da564S[email protected] switch (response_code){ 88909e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 8902b360848Smatthias.ringwald case CONNECTION_REQUEST: 891e74c5f58SMatthias Ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, source_cid, 0, result, 0); 8922bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 8934d816277S[email protected] if (result == 0x0003){ 8942bd8b7e7S[email protected] hci_disconnect_security_block(handle); 8954d816277S[email protected] } 8962b360848Smatthias.ringwald break; 8972b83fb7dSmatthias.ringwald case ECHO_REQUEST: 8982b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 8992b83fb7dSmatthias.ringwald break; 9002b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 9013b0484b3S[email protected] switch (infoType){ 9023b0484b3S[email protected] case 1: { // Connectionless MTU 9033b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 9043b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 9053b0484b3S[email protected] } 906202c8a4cSMatthias Ringwald break; 9073b0484b3S[email protected] case 2: { // Extended Features Supported 9081b9cb13dSMatthias Ringwald uint32_t features = l2cap_extended_features_mask(); 9093b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 9103b0484b3S[email protected] } 911202c8a4cSMatthias Ringwald break; 9123b0484b3S[email protected] case 3: { // Fixed Channels Supported 9133b0484b3S[email protected] uint8_t map[8]; 9143b0484b3S[email protected] memset(map, 0, 8); 915288636a2SMatthias Ringwald map[0] = 0x06; // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04) 9163b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 9173b0484b3S[email protected] } 918202c8a4cSMatthias Ringwald break; 9193b0484b3S[email protected] default: 9202b83fb7dSmatthias.ringwald // all other types are not supported 9212b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 9223b0484b3S[email protected] break; 9232b83fb7dSmatthias.ringwald } 9242b83fb7dSmatthias.ringwald break; 92563a7246aSmatthias.ringwald case COMMAND_REJECT: 9265ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 92763f0ac45SMatthias Ringwald break; 92809e9d05bSMatthias Ringwald #endif 929a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 93063f0ac45SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 93163f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result); 93263f0ac45SMatthias Ringwald break; 93370efece1S[email protected] case COMMAND_REJECT_LE: 93470efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 93563a7246aSmatthias.ringwald break; 93670efece1S[email protected] #endif 9372b83fb7dSmatthias.ringwald default: 9382b83fb7dSmatthias.ringwald // should not happen 9392b83fb7dSmatthias.ringwald break; 9402b83fb7dSmatthias.ringwald } 9412b83fb7dSmatthias.ringwald } 9422b83fb7dSmatthias.ringwald 943665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 944f3963406SMatthias Ringwald UNUSED(it); 94509e9d05bSMatthias Ringwald 9461b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 9471b9cb13dSMatthias Ringwald // send l2cap information request if neccessary 9481b9cb13dSMatthias Ringwald hci_connections_get_iterator(&it); 9491b9cb13dSMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 9501b9cb13dSMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 9511b9cb13dSMatthias Ringwald if (connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST){ 9521b9cb13dSMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W4_EXTENDED_FEATURE_RESPONSE; 9531b9cb13dSMatthias Ringwald // send information request for extended features 9541b9cb13dSMatthias Ringwald uint8_t sig_id = l2cap_next_sig_id(); 9551b9cb13dSMatthias Ringwald uint8_t info_type = 2; 9561b9cb13dSMatthias Ringwald l2cap_send_signaling_packet(connection->con_handle, INFORMATION_REQUEST, sig_id, info_type); 9571b9cb13dSMatthias Ringwald return; 9581b9cb13dSMatthias Ringwald } 9591b9cb13dSMatthias Ringwald } 9601b9cb13dSMatthias Ringwald #endif 9611b9cb13dSMatthias Ringwald 96209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 96343ec931dSMatthias Ringwald uint8_t config_options[10]; 964665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 965665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 966baf94f06S[email protected] 967665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 96822c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 9692cd0be45Smatthias.ringwald switch (channel->state){ 9702cd0be45Smatthias.ringwald 971df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 972ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 973fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 974a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 975ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 976fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 977ad671560S[email protected] } 978ad671560S[email protected] break; 979ad671560S[email protected] 98002b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 981baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 98264472d52Smatthias.ringwald // send connection request - set state first 98364472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 98402b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 9858f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 98602b22dc4Smatthias.ringwald break; 98702b22dc4Smatthias.ringwald 988e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 989fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 99022c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 991fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 992e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 9939dcb2fb2S[email protected] l2cap_stop_rtx(channel); 994665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 995d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 996e7ff783cSmatthias.ringwald break; 997e7ff783cSmatthias.ringwald 998552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 999fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1000fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 100128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1002fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 1003552d92a1Smatthias.ringwald break; 1004552d92a1Smatthias.ringwald 10056fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 1006fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 10076fdcc387Smatthias.ringwald // success, start l2cap handshake 1008b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 10096fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 1010fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 10115932bd7cS[email protected] l2cap_start_rtx(channel); 10126fdcc387Smatthias.ringwald break; 10136fdcc387Smatthias.ringwald 1014fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1015fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 101673cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 101763a7246aSmatthias.ringwald uint16_t flags = 0; 101828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 101963a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 102063a7246aSmatthias.ringwald flags = 1; 102163a7246aSmatthias.ringwald } else { 102228ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 102363a7246aSmatthias.ringwald } 102463a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 1025ac8f1300SMatthias Ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 1026fc64f94aSMatthias 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); 1027ac8f1300SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 1028ac8f1300SMatthias Ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED){ 1029ac8f1300SMatthias Ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED); 1030ac8f1300SMatthias Ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 10316dca2a0cSMatthias Ringwald uint16_t options_size = l2cap_setup_options(channel, config_options); 1032ac8f1300SMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS, options_size, &config_options); 1033ac8f1300SMatthias Ringwald #endif 1034ac8f1300SMatthias Ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 103563a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 1036ac8f1300SMatthias Ringwald uint16_t options_size = l2cap_setup_options(channel, config_options); 1037ac8f1300SMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS, options_size, &config_options); 103863a7246aSmatthias.ringwald } else { 1039ac8f1300SMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS, 0, NULL); 104063a7246aSmatthias.ringwald } 104163a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 1042fa8473a4Smatthias.ringwald } 104373cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 104428ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 104528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 1046b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 10476dca2a0cSMatthias Ringwald uint16_t options_size = l2cap_setup_options(channel, config_options); 10486dca2a0cSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, options_size, &config_options); 10495932bd7cS[email protected] l2cap_start_rtx(channel); 1050fa8473a4Smatthias.ringwald } 1051fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1052552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1053552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 1054fa8473a4Smatthias.ringwald } 1055552d92a1Smatthias.ringwald break; 1056552d92a1Smatthias.ringwald 1057e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 1058fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 105922c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 1060fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 10615932bd7cS[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 :) 1062756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 1063e7ff783cSmatthias.ringwald break; 1064e7ff783cSmatthias.ringwald 1065e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 1066fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1067b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 10682cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 1069fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 10702cd0be45Smatthias.ringwald break; 10712cd0be45Smatthias.ringwald default: 10722cd0be45Smatthias.ringwald break; 10732cd0be45Smatthias.ringwald } 107438f62777SMatthias Ringwald 107538f62777SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 107638f62777SMatthias Ringwald // send s-frame to acknowledge received packets 107738f62777SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 1078675e6881SMatthias Ringwald 1079675e6881SMatthias Ringwald if (channel->tx_send_index != channel->tx_write_index){ 1080212b6be2SMatthias Ringwald int unacknowledged_packets = l2cap_ertm_num_unacknowledged_tx_packets(channel); 1081212b6be2SMatthias Ringwald // check remote tx window 1082212b6be2SMatthias Ringwald log_info("unacknowledged_packets %u, remote tx window size %u", unacknowledged_packets, channel->remote_tx_window_size); 1083212b6be2SMatthias Ringwald if (unacknowledged_packets < channel->remote_tx_window_size){ 1084675e6881SMatthias Ringwald int index = channel->tx_send_index; 1085675e6881SMatthias Ringwald channel->tx_send_index++; 1086675e6881SMatthias Ringwald if (channel->tx_send_index >= channel->num_tx_buffers){ 1087675e6881SMatthias Ringwald channel->tx_send_index = 0; 1088675e6881SMatthias Ringwald } 10897b7901d8SMatthias Ringwald l2cap_ertm_send_information_frame(channel, index, 0); // final = 0 1090675e6881SMatthias Ringwald continue; 1091675e6881SMatthias Ringwald } 1092212b6be2SMatthias Ringwald } 1093675e6881SMatthias Ringwald 1094d84e866fSMatthias Ringwald if (channel->send_supervisor_frame_receiver_ready){ 109578cd8a22SMatthias Ringwald channel->send_supervisor_frame_receiver_ready = 0; 1096d2afdd38SMatthias Ringwald log_info("Send S-Frame: RR %u, final %u", channel->req_seq, channel->set_final_bit_after_packet_with_poll_bit_set); 1097d2afdd38SMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY, 0, channel->set_final_bit_after_packet_with_poll_bit_set, channel->req_seq); 1098d2afdd38SMatthias Ringwald channel->set_final_bit_after_packet_with_poll_bit_set = 0; 109938f62777SMatthias Ringwald l2cap_ertm_send_supervisor_frame(channel, control); 1100675e6881SMatthias Ringwald continue; 110138f62777SMatthias Ringwald } 110262041d70SMatthias Ringwald if (channel->send_supervisor_frame_receiver_ready_poll){ 110378cd8a22SMatthias Ringwald channel->send_supervisor_frame_receiver_ready_poll = 0; 110462041d70SMatthias Ringwald log_info("Send S-Frame: RR %u with poll=1 ", channel->req_seq); 110562041d70SMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY, 1, 0, channel->req_seq); 110662041d70SMatthias Ringwald l2cap_ertm_send_supervisor_frame(channel, control); 110762041d70SMatthias Ringwald continue; 110862041d70SMatthias Ringwald } 11098f1c9639SMatthias Ringwald if (channel->send_supervisor_frame_receiver_not_ready){ 111078cd8a22SMatthias Ringwald channel->send_supervisor_frame_receiver_not_ready = 0; 11118f1c9639SMatthias Ringwald log_info("Send S-Frame: RNR %u", channel->req_seq); 11128f1c9639SMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY, 0, 0, channel->req_seq); 11138f1c9639SMatthias Ringwald l2cap_ertm_send_supervisor_frame(channel, control); 11148f1c9639SMatthias Ringwald continue; 11158f1c9639SMatthias Ringwald } 1116c7309e8dSMatthias Ringwald if (channel->send_supervisor_frame_reject){ 1117c7309e8dSMatthias Ringwald channel->send_supervisor_frame_reject = 0; 1118c7309e8dSMatthias Ringwald log_info("Send S-Frame: REJ %u", channel->req_seq); 1119c7309e8dSMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT, 0, 0, channel->req_seq); 1120c7309e8dSMatthias Ringwald l2cap_ertm_send_supervisor_frame(channel, control); 1121c7309e8dSMatthias Ringwald continue; 1122c7309e8dSMatthias Ringwald } 1123df2191a7SMatthias Ringwald if (channel->send_supervisor_frame_selective_reject){ 1124df2191a7SMatthias Ringwald channel->send_supervisor_frame_selective_reject = 0; 1125df2191a7SMatthias Ringwald log_info("Send S-Frame: SREJ %u", channel->expected_tx_seq); 1126f85ade6bSMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT, 0, channel->set_final_bit_after_packet_with_poll_bit_set, channel->expected_tx_seq); 1127f85ade6bSMatthias Ringwald channel->set_final_bit_after_packet_with_poll_bit_set = 0; 1128df2191a7SMatthias Ringwald l2cap_ertm_send_supervisor_frame(channel, control); 1129df2191a7SMatthias Ringwald continue; 1130df2191a7SMatthias Ringwald } 11317b7901d8SMatthias Ringwald 11327b7901d8SMatthias Ringwald if (channel->srej_active){ 11337b7901d8SMatthias Ringwald int i; 11347b7901d8SMatthias Ringwald for (i=0;i<channel->num_tx_buffers;i++){ 11357b7901d8SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[i]; 11367b7901d8SMatthias Ringwald if (tx_state->retransmission_requested) { 11377b7901d8SMatthias Ringwald tx_state->retransmission_requested = 0; 1138d2afdd38SMatthias Ringwald uint8_t final = channel->set_final_bit_after_packet_with_poll_bit_set; 1139d2afdd38SMatthias Ringwald channel->set_final_bit_after_packet_with_poll_bit_set = 0; 1140d2afdd38SMatthias Ringwald l2cap_ertm_send_information_frame(channel, i, final); 11417b7901d8SMatthias Ringwald break; 11427b7901d8SMatthias Ringwald } 11437b7901d8SMatthias Ringwald } 11447b7901d8SMatthias Ringwald if (i == channel->num_tx_buffers){ 11457b7901d8SMatthias Ringwald // no retransmission request found 11467b7901d8SMatthias Ringwald channel->srej_active = 0; 11477b7901d8SMatthias Ringwald } else { 11487b7901d8SMatthias Ringwald // packet was sent 11497b7901d8SMatthias Ringwald continue; 11507b7901d8SMatthias Ringwald } 11517b7901d8SMatthias Ringwald } 115238f62777SMatthias Ringwald #endif 115338f62777SMatthias Ringwald 11542cd0be45Smatthias.ringwald } 115509e9d05bSMatthias Ringwald #endif 1156da886c03S[email protected] 1157cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1158efedfb4cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1159efedfb4cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11607f107edaSMatthias Ringwald uint8_t * acl_buffer; 11617f107edaSMatthias Ringwald uint8_t * l2cap_payload; 11627f107edaSMatthias Ringwald uint16_t pos; 11637f107edaSMatthias Ringwald uint16_t payload_size; 1164efedfb4cSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1165efedfb4cSMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 1166efedfb4cSMatthias Ringwald switch (channel->state){ 11675cb87675SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST: 11685cb87675SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 11695cb87675SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE; 11705cb87675SMatthias Ringwald // le psm, source cid, mtu, mps, initial credits 11711b8b8d05SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 117263f0ac45SMatthias Ringwald channel->credits_incoming = channel->new_credits_incoming; 117363f0ac45SMatthias Ringwald channel->new_credits_incoming = 0; 117463f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, LE_CREDIT_BASED_CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid, channel->local_mtu, 23, channel->credits_incoming); 11755cb87675SMatthias Ringwald break; 117623017473SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT: 117723017473SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 117823017473SMatthias Ringwald // TODO: support larger MPS 117923017473SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 118063f0ac45SMatthias Ringwald channel->credits_incoming = channel->new_credits_incoming; 118163f0ac45SMatthias Ringwald channel->new_credits_incoming = 0; 118263f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->local_mtu, 23, channel->credits_incoming, 0); 118364e11ca9SMatthias Ringwald // notify client 118444276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, 0); 118523017473SMatthias Ringwald break; 1186e7d0c9aaSMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE: 1187e7d0c9aaSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1188e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 118963f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason); 1190e7d0c9aaSMatthias Ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 1191e7d0c9aaSMatthias Ringwald l2cap_stop_rtx(channel); 1192e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1193e7d0c9aaSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1194e7d0c9aaSMatthias Ringwald break; 11957f107edaSMatthias Ringwald case L2CAP_STATE_OPEN: 119685aeef60SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 119785aeef60SMatthias Ringwald 119885aeef60SMatthias Ringwald // send credits 119985aeef60SMatthias Ringwald if (channel->new_credits_incoming){ 120085aeef60SMatthias Ringwald log_info("l2cap: sending %u credits", channel->new_credits_incoming); 120185aeef60SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 120285aeef60SMatthias Ringwald uint16_t new_credits = channel->new_credits_incoming; 120385aeef60SMatthias Ringwald channel->new_credits_incoming = 0; 120485aeef60SMatthias Ringwald channel->credits_incoming += new_credits; 120585aeef60SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits); 120685aeef60SMatthias Ringwald break; 120785aeef60SMatthias Ringwald } 120885aeef60SMatthias Ringwald 120985aeef60SMatthias Ringwald // send data 12107f107edaSMatthias Ringwald if (!channel->send_sdu_buffer) break; 12117f107edaSMatthias Ringwald if (!channel->credits_outgoing) break; 121285aeef60SMatthias Ringwald 12137f107edaSMatthias Ringwald // send part of SDU 12147f107edaSMatthias Ringwald hci_reserve_packet_buffer(); 12157f107edaSMatthias Ringwald acl_buffer = hci_get_outgoing_packet_buffer(); 12167f107edaSMatthias Ringwald l2cap_payload = acl_buffer + 8; 12177f107edaSMatthias Ringwald pos = 0; 12187f107edaSMatthias Ringwald if (!channel->send_sdu_pos){ 12197f107edaSMatthias Ringwald // store SDU len 12207f107edaSMatthias Ringwald channel->send_sdu_pos += 2; 12217f107edaSMatthias Ringwald little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len); 12227f107edaSMatthias Ringwald pos += 2; 12237f107edaSMatthias Ringwald } 12247f107edaSMatthias Ringwald payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos); 122585aeef60SMatthias Ringwald log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing); 12267f107edaSMatthias Ringwald memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len 12277f107edaSMatthias Ringwald pos += payload_size; 12287f107edaSMatthias Ringwald channel->send_sdu_pos += payload_size; 1229f511cefaSMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, 0, channel->remote_cid, pos); 12307f107edaSMatthias Ringwald // done 12317f107edaSMatthias Ringwald 123285aeef60SMatthias Ringwald channel->credits_outgoing--; 12337f107edaSMatthias Ringwald 12347f107edaSMatthias Ringwald if (channel->send_sdu_pos >= channel->send_sdu_len + 2){ 12357f107edaSMatthias Ringwald channel->send_sdu_buffer = NULL; 123644276248SMatthias Ringwald // send done event 123744276248SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT); 123844276248SMatthias Ringwald // inform about can send now 123944276248SMatthias Ringwald l2cap_le_notify_channel_can_send(channel); 12407f107edaSMatthias Ringwald } 12417f107edaSMatthias Ringwald hci_send_acl_packet_buffer(8 + pos); 12427f107edaSMatthias Ringwald break; 1243828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 1244828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1245828a7f7aSMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 1246828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 1247828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 1248828a7f7aSMatthias Ringwald break; 1249828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 1250828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1251828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 1252828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 1253828a7f7aSMatthias Ringwald l2cap_le_finialize_channel_close(channel); // -- remove from list 1254828a7f7aSMatthias Ringwald break; 1255efedfb4cSMatthias Ringwald default: 1256efedfb4cSMatthias Ringwald break; 1257efedfb4cSMatthias Ringwald } 1258efedfb4cSMatthias Ringwald } 125909e9d05bSMatthias Ringwald #endif 1260efedfb4cSMatthias Ringwald 126109e9d05bSMatthias Ringwald #ifdef ENABLE_BLE 1262da886c03S[email protected] // send l2cap con paramter update if necessary 1263da886c03S[email protected] hci_connections_get_iterator(&it); 1264665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 1265665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 12665d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 1267b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 1268da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 1269b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 1270b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 1271b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 1272b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 1273b68d7bc3SMatthias Ringwald break; 1274da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 1275b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 1276b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 1277da886c03S[email protected] break; 1278da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 1279b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 1280b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 1281da886c03S[email protected] break; 1282da886c03S[email protected] default: 1283da886c03S[email protected] break; 1284da886c03S[email protected] } 1285da886c03S[email protected] } 12864d7157c3S[email protected] #endif 1287da886c03S[email protected] 128822c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 12892cd0be45Smatthias.ringwald } 12902cd0be45Smatthias.ringwald 129109e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1292fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){ 12932df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 12945533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 12952df5dadcS[email protected] // success, start l2cap handshake 1296fc64f94aSMatthias Ringwald channel->con_handle = con_handle; 12972df5dadcS[email protected] // check remote SSP feature first 12982df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 12992df5dadcS[email protected] } 13002df5dadcS[email protected] } 13012df5dadcS[email protected] 13021b9cb13dSMatthias Ringwald static void l2cap_ready_to_connect(l2cap_channel_t * channel){ 13031b9cb13dSMatthias Ringwald 13041b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 130527e0774aSMatthias Ringwald // assumption: outgoing connection 13061b9cb13dSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(channel->con_handle); 13071b9cb13dSMatthias Ringwald if (connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_IDLE){ 13081b9cb13dSMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST; 13091b9cb13dSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES; 13101b9cb13dSMatthias Ringwald return; 13111b9cb13dSMatthias Ringwald } 13121b9cb13dSMatthias Ringwald #endif 13131b9cb13dSMatthias Ringwald 13141b9cb13dSMatthias Ringwald // fine, go ahead 13151b9cb13dSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 13161b9cb13dSMatthias Ringwald } 13171b9cb13dSMatthias Ringwald 13182df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 13192df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 13202df5dadcS[email protected] 13212df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 1322ac301f95S[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)); 1323fc64f94aSMatthias Ringwald if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 13242df5dadcS[email protected] // request security level 2 13252df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 1326fc64f94aSMatthias Ringwald gap_request_security_level(channel->con_handle, LEVEL_2); 13272df5dadcS[email protected] return; 13282df5dadcS[email protected] } 13291b9cb13dSMatthias Ringwald 13301b9cb13dSMatthias Ringwald l2cap_ready_to_connect(channel); 13312df5dadcS[email protected] } 133209e9d05bSMatthias Ringwald #endif 13332df5dadcS[email protected] 133409e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS 1335da144af5SMatthias Ringwald static l2cap_channel_t * l2cap_create_channel_entry(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type, 1336da144af5SMatthias Ringwald uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){ 1337da144af5SMatthias Ringwald 1338da144af5SMatthias Ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 1339da144af5SMatthias Ringwald if (!channel) { 1340da144af5SMatthias Ringwald return NULL; 1341da144af5SMatthias Ringwald } 1342da144af5SMatthias Ringwald 1343da144af5SMatthias Ringwald // Init memory (make valgrind happy) 1344da144af5SMatthias Ringwald memset(channel, 0, sizeof(l2cap_channel_t)); 1345da144af5SMatthias Ringwald 1346da144af5SMatthias Ringwald // fill in 1347da144af5SMatthias Ringwald channel->packet_handler = packet_handler; 1348da144af5SMatthias Ringwald bd_addr_copy(channel->address, address); 1349da144af5SMatthias Ringwald channel->address_type = address_type; 1350da144af5SMatthias Ringwald channel->psm = psm; 1351da144af5SMatthias Ringwald channel->local_mtu = local_mtu; 1352da144af5SMatthias Ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 1353da144af5SMatthias Ringwald channel->required_security_level = security_level; 1354da144af5SMatthias Ringwald 1355da144af5SMatthias Ringwald // 1356da144af5SMatthias Ringwald channel->local_cid = l2cap_next_local_cid(); 1357da144af5SMatthias Ringwald channel->con_handle = 0; 1358da144af5SMatthias Ringwald 1359da144af5SMatthias Ringwald // set initial state 1360da144af5SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 1361da144af5SMatthias Ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 1362da144af5SMatthias Ringwald channel->remote_sig_id = L2CAP_SIG_ID_INVALID; 1363da144af5SMatthias Ringwald channel->local_sig_id = L2CAP_SIG_ID_INVALID; 136438f62777SMatthias Ringwald 1365da144af5SMatthias Ringwald return channel; 1366da144af5SMatthias Ringwald } 136709e9d05bSMatthias Ringwald #endif 136809e9d05bSMatthias Ringwald 136909e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1370da144af5SMatthias Ringwald 13719077cb15SMatthias Ringwald /** 13729077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 13739077cb15SMatthias Ringwald * @param packet_handler 13749077cb15SMatthias Ringwald * @param address 13759077cb15SMatthias Ringwald * @param psm 13769077cb15SMatthias Ringwald * @param mtu 13779077cb15SMatthias Ringwald * @param local_cid 13789077cb15SMatthias Ringwald */ 13799077cb15SMatthias Ringwald 13809d139fbaSMatthias 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){ 13819d139fbaSMatthias Ringwald // limit MTU to the size of our outtgoing HCI buffer 13829d139fbaSMatthias Ringwald uint16_t local_mtu = btstack_min(mtu, l2cap_max_mtu()); 1383da144af5SMatthias Ringwald 13849d139fbaSMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u -> local mtu %u", bd_addr_to_str(address), psm, mtu, local_mtu); 1385da144af5SMatthias Ringwald 1386da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0); 1387fc64f94aSMatthias Ringwald if (!channel) { 13889077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 13899077cb15SMatthias Ringwald } 13909077cb15SMatthias Ringwald 13911b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 13921b9cb13dSMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 13931b9cb13dSMatthias Ringwald #endif 13941b9cb13dSMatthias Ringwald 13959077cb15SMatthias Ringwald // add to connections list 1396fc64f94aSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 13979077cb15SMatthias Ringwald 13989077cb15SMatthias Ringwald // store local_cid 13999077cb15SMatthias Ringwald if (out_local_cid){ 1400fc64f94aSMatthias Ringwald *out_local_cid = channel->local_cid; 14019077cb15SMatthias Ringwald } 14029077cb15SMatthias Ringwald 14039077cb15SMatthias Ringwald // check if hci connection is already usable 14049077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 14059077cb15SMatthias Ringwald if (conn){ 1406add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 1407fc64f94aSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 14089077cb15SMatthias Ringwald // check if remote supported fearures are already received 14099077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 1410fc64f94aSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 14119077cb15SMatthias Ringwald } 14129077cb15SMatthias Ringwald } 14139077cb15SMatthias Ringwald 14149077cb15SMatthias Ringwald l2cap_run(); 14159077cb15SMatthias Ringwald 14169077cb15SMatthias Ringwald return 0; 14179077cb15SMatthias Ringwald } 14189077cb15SMatthias Ringwald 14191b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 14207b181629SMatthias Ringwald 1421843bae5dSMatthias Ringwald static uint8_t l2cap_ertm_validate_local_config(uint8_t max_transmit, uint16_t retransmission_timeout_ms, uint16_t monitor_timeout_ms, 1422843bae5dSMatthias Ringwald uint16_t local_mtu, uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size){ 1423843bae5dSMatthias Ringwald 14242b70d705SMatthias Ringwald UNUSED(buffer); 14252b70d705SMatthias Ringwald UNUSED(size); 14262b70d705SMatthias Ringwald 14272b70d705SMatthias Ringwald uint8_t result = ERROR_CODE_SUCCESS; 14282b70d705SMatthias Ringwald if (max_transmit < 1){ 14292b70d705SMatthias Ringwald log_error("max_transmit must be >= 1"); 14302b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 14312b70d705SMatthias Ringwald } 14322b70d705SMatthias Ringwald if (retransmission_timeout_ms < 2000){ 14332b70d705SMatthias Ringwald log_error("retransmission_timeout_ms must be >= 2000 ms"); 14342b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 14352b70d705SMatthias Ringwald } 14362b70d705SMatthias Ringwald if (monitor_timeout_ms < 12000){ 14372b70d705SMatthias Ringwald log_error("monitor_timeout_ms must be >= 12000 ms"); 14382b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 14392b70d705SMatthias Ringwald } 1440843bae5dSMatthias Ringwald if (local_mtu < 48){ 1441843bae5dSMatthias Ringwald log_error("local_mtu must be >= 48"); 1442843bae5dSMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 1443843bae5dSMatthias Ringwald } 14442b70d705SMatthias Ringwald if (num_rx_buffers < 1){ 14452b70d705SMatthias Ringwald log_error("num_rx_buffers must be >= 1"); 14462b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 14472b70d705SMatthias Ringwald } 14482b70d705SMatthias Ringwald if (num_tx_buffers < 1){ 14492b70d705SMatthias Ringwald log_error("num_rx_buffers must be >= 1"); 14502b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 14512b70d705SMatthias Ringwald } 14522b70d705SMatthias Ringwald return result; 14532b70d705SMatthias Ringwald } 14542b70d705SMatthias Ringwald 1455843bae5dSMatthias Ringwald static void l2cap_ertm_configure_channel(l2cap_channel_t * channel, int ertm_mandatory, uint8_t max_transmit, uint16_t retransmission_timeout_ms, 1456843bae5dSMatthias Ringwald uint16_t monitor_timeout_ms, uint16_t local_mtu, uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size){ 14577b181629SMatthias Ringwald 14587b181629SMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION; 14597b181629SMatthias Ringwald channel->ertm_mandatory = ertm_mandatory; 1460bbc0a9e7SMatthias Ringwald channel->local_max_transmit = max_transmit; 1461bbc0a9e7SMatthias Ringwald channel->local_retransmission_timeout_ms = retransmission_timeout_ms; 1462bbc0a9e7SMatthias Ringwald channel->local_monitor_timeout_ms = monitor_timeout_ms; 1463843bae5dSMatthias Ringwald channel->local_mtu = local_mtu; 14647b181629SMatthias Ringwald channel->num_rx_buffers = num_rx_buffers; 14657b181629SMatthias Ringwald channel->num_tx_buffers = num_tx_buffers; 14667b181629SMatthias Ringwald 1467843bae5dSMatthias Ringwald // align buffer to 16-byte boundary, just in case 1468843bae5dSMatthias Ringwald int bytes_till_alignment = 16 - (((uintptr_t) buffer) & 0x0f); 1469843bae5dSMatthias Ringwald buffer += bytes_till_alignment; 1470843bae5dSMatthias Ringwald size -= bytes_till_alignment; 1471843bae5dSMatthias Ringwald 1472843bae5dSMatthias Ringwald // setup state buffers 14737b181629SMatthias Ringwald uint32_t pos = 0; 14747b181629SMatthias Ringwald channel->rx_packets_state = (l2cap_ertm_rx_packet_state_t *) &buffer[pos]; 14757b181629SMatthias Ringwald pos += num_rx_buffers * sizeof(l2cap_ertm_rx_packet_state_t); 14767b181629SMatthias Ringwald channel->tx_packets_state = (l2cap_ertm_tx_packet_state_t *) &buffer[pos]; 14777b181629SMatthias Ringwald pos += num_tx_buffers * sizeof(l2cap_ertm_tx_packet_state_t); 1478843bae5dSMatthias Ringwald 1479843bae5dSMatthias Ringwald // setup reassembly buffer 1480843bae5dSMatthias Ringwald channel->reassembly_buffer = &buffer[pos]; 1481843bae5dSMatthias Ringwald pos += local_mtu; 1482843bae5dSMatthias Ringwald 1483843bae5dSMatthias Ringwald // divide rest of data equally 1484843bae5dSMatthias Ringwald channel->local_mps = (size - pos) / (num_rx_buffers + num_tx_buffers); 1485843bae5dSMatthias Ringwald log_info("Local MPS: %u", channel->local_mtu); 14867b181629SMatthias Ringwald channel->rx_packets_data = &buffer[pos]; 14877b181629SMatthias Ringwald pos += num_rx_buffers * channel->local_mtu; 14887b181629SMatthias Ringwald channel->tx_packets_data = &buffer[pos]; 14897b181629SMatthias Ringwald } 14907b181629SMatthias Ringwald 1491501329faSMatthias Ringwald uint8_t l2cap_create_ertm_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, 1492501329faSMatthias Ringwald int ertm_mandatory, uint8_t max_transmit, uint16_t retransmission_timeout_ms, uint16_t monitor_timeout_ms, 1493843bae5dSMatthias Ringwald uint16_t local_mtu, uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid){ 1494501329faSMatthias Ringwald 1495843bae5dSMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s, psm 0x%x, local mtu %u", bd_addr_to_str(address), psm, local_mtu); 14961b9cb13dSMatthias Ringwald 14972b70d705SMatthias Ringwald // validate local config 1498843bae5dSMatthias Ringwald uint8_t result = l2cap_ertm_validate_local_config(max_transmit, retransmission_timeout_ms, monitor_timeout_ms, local_mtu, num_tx_buffers, num_rx_buffers, buffer, size); 14992b70d705SMatthias Ringwald if (result) return result; 15002b70d705SMatthias Ringwald 1501501329faSMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0); 15021b9cb13dSMatthias Ringwald if (!channel) { 15031b9cb13dSMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 15041b9cb13dSMatthias Ringwald } 15051b9cb13dSMatthias Ringwald 15067b181629SMatthias Ringwald // configure ERTM 15077b181629SMatthias Ringwald l2cap_ertm_configure_channel(channel, ertm_mandatory, max_transmit, retransmission_timeout_ms, 1508843bae5dSMatthias Ringwald local_mtu, monitor_timeout_ms, num_tx_buffers, num_rx_buffers, buffer, size); 15091b9cb13dSMatthias Ringwald 15101b9cb13dSMatthias Ringwald // add to connections list 15111b9cb13dSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 15121b9cb13dSMatthias Ringwald 15131b9cb13dSMatthias Ringwald // store local_cid 15141b9cb13dSMatthias Ringwald if (out_local_cid){ 15151b9cb13dSMatthias Ringwald *out_local_cid = channel->local_cid; 15161b9cb13dSMatthias Ringwald } 15171b9cb13dSMatthias Ringwald 15181b9cb13dSMatthias Ringwald // check if hci connection is already usable 15191b9cb13dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 15201b9cb13dSMatthias Ringwald if (conn){ 15211b9cb13dSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 15221b9cb13dSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 15231b9cb13dSMatthias Ringwald // check if remote supported fearures are already received 15241b9cb13dSMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 15251b9cb13dSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 15261b9cb13dSMatthias Ringwald } 15271b9cb13dSMatthias Ringwald } 15281b9cb13dSMatthias Ringwald 15291b9cb13dSMatthias Ringwald l2cap_run(); 15301b9cb13dSMatthias Ringwald 15311b9cb13dSMatthias Ringwald return 0; 15321b9cb13dSMatthias Ringwald } 15331b9cb13dSMatthias Ringwald #endif 15341b9cb13dSMatthias Ringwald 1535ce8f182eSMatthias Ringwald void 1536ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 1537e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 1538b35f641cSmatthias.ringwald // find channel for local_cid 1539b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1540f62db1e3Smatthias.ringwald if (channel) { 1541e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1542f62db1e3Smatthias.ringwald } 15432cd0be45Smatthias.ringwald // process 15442cd0be45Smatthias.ringwald l2cap_run(); 154543625864Smatthias.ringwald } 15461e6aba47Smatthias.ringwald 1547afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 1548665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1549665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1550665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1551665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1552058e3d6bSMatthias Ringwald if ( bd_addr_cmp( channel->address, address) != 0) continue; 1553c22aecc9S[email protected] // channel for this address found 1554c22aecc9S[email protected] switch (channel->state){ 1555c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 1556c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 1557afde0c52Smatthias.ringwald // failure, forward error code 1558afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 1559afde0c52Smatthias.ringwald // discard channel 15609dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1561665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1562d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1563c22aecc9S[email protected] break; 1564c22aecc9S[email protected] default: 1565c22aecc9S[email protected] break; 1566afde0c52Smatthias.ringwald } 1567afde0c52Smatthias.ringwald } 1568afde0c52Smatthias.ringwald } 1569afde0c52Smatthias.ringwald 1570afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 1571665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1572665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1573665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1574665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1575058e3d6bSMatthias Ringwald if ( ! bd_addr_cmp( channel->address, address) ){ 15762df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 1577afde0c52Smatthias.ringwald } 1578afde0c52Smatthias.ringwald } 15796fdcc387Smatthias.ringwald // process 15806fdcc387Smatthias.ringwald l2cap_run(); 1581afde0c52Smatthias.ringwald } 158209e9d05bSMatthias Ringwald #endif 1583b448a0e7Smatthias.ringwald 158433c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){ 158509e9d05bSMatthias Ringwald 158609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 158733c40538SMatthias Ringwald btstack_linked_list_iterator_t it; 158833c40538SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 158933c40538SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 159033c40538SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 159133c40538SMatthias Ringwald if (!channel->waiting_for_can_send_now) continue; 1592fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 159333c40538SMatthias Ringwald channel->waiting_for_can_send_now = 0; 159434e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 159533c40538SMatthias Ringwald } 159609e9d05bSMatthias Ringwald #endif 159744276248SMatthias Ringwald 15982125de09SMatthias Ringwald int i; 15992125de09SMatthias Ringwald for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){ 1600773c4106SMatthias Ringwald if (!fixed_channels[i].callback) continue; 16012125de09SMatthias Ringwald if (!fixed_channels[i].waiting_for_can_send_now) continue; 160235454696SMatthias Ringwald int can_send = 0; 160334e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 160435454696SMatthias Ringwald #ifdef ENABLE_BLE 160534e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 160635454696SMatthias Ringwald #endif 160734e7e577SMatthias Ringwald } else { 160835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 160934e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 161035454696SMatthias Ringwald #endif 161134e7e577SMatthias Ringwald } 161234e7e577SMatthias Ringwald if (!can_send) continue; 161334e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 161434e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 16152125de09SMatthias Ringwald } 161633c40538SMatthias Ringwald } 161733c40538SMatthias Ringwald 1618d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 1619afde0c52Smatthias.ringwald 16209ec2630cSMatthias Ringwald UNUSED(packet_type); 16219ec2630cSMatthias Ringwald UNUSED(cid); 16229ec2630cSMatthias Ringwald UNUSED(size); 16239ec2630cSMatthias Ringwald 1624afde0c52Smatthias.ringwald bd_addr_t address; 1625afde0c52Smatthias.ringwald hci_con_handle_t handle; 16262d00edd4Smatthias.ringwald int hci_con_used; 162709e9d05bSMatthias Ringwald btstack_linked_list_iterator_t it; 162809e9d05bSMatthias Ringwald 162909e9d05bSMatthias Ringwald // avoid unused warnings 1630f3963406SMatthias Ringwald UNUSED(address); 1631f3963406SMatthias Ringwald UNUSED(hci_con_used); 1632f3963406SMatthias Ringwald UNUSED(it); 1633f22209dfSMatthias Ringwald UNUSED(handle); 1634afde0c52Smatthias.ringwald 16350e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1636afde0c52Smatthias.ringwald 163709e9d05bSMatthias Ringwald // Notify channel packet handler if they can send now 163809e9d05bSMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 163909e9d05bSMatthias Ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 164009e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 164109e9d05bSMatthias Ringwald l2cap_notify_channel_can_send(); 164209e9d05bSMatthias Ringwald break; 164309e9d05bSMatthias Ringwald 164409e9d05bSMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 164509e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 164609e9d05bSMatthias Ringwald break; 164709e9d05bSMatthias Ringwald 164809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1649afde0c52Smatthias.ringwald // handle connection complete events 1650afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1651724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 1652afde0c52Smatthias.ringwald if (packet[2] == 0){ 1653f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1654afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 1655afde0c52Smatthias.ringwald } else { 1656afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 1657afde0c52Smatthias.ringwald } 1658afde0c52Smatthias.ringwald break; 1659afde0c52Smatthias.ringwald 1660afde0c52Smatthias.ringwald // handle successful create connection cancel command 1661afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 1662073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) { 1663afde0c52Smatthias.ringwald if (packet[5] == 0){ 1664724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 1665afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 1666afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 166703cfbabcSmatthias.ringwald } 16681e6aba47Smatthias.ringwald } 166939d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 167039d59809Smatthias.ringwald break; 167109e9d05bSMatthias Ringwald #endif 167227a923d0Smatthias.ringwald 16731e6aba47Smatthias.ringwald // handle disconnection complete events 1674afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1675c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 167609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1677d0662982SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1678665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1679665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1680665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1681fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 168215ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 16839dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1684665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1685d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 168627a923d0Smatthias.ringwald } 168709e9d05bSMatthias Ringwald #endif 1688a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1689d0662982SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1690991fea48SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1691991fea48SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1692991fea48SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1693991fea48SMatthias Ringwald if (channel->con_handle != handle) continue; 1694991fea48SMatthias Ringwald l2cap_emit_channel_closed(channel); 1695991fea48SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1696991fea48SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1697991fea48SMatthias Ringwald } 1698991fea48SMatthias Ringwald #endif 1699afde0c52Smatthias.ringwald break; 1700fcadd0caSmatthias.ringwald 1701ee091cf1Smatthias.ringwald // HCI Connection Timeouts 170209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1703afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 1704f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1705bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 170680ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 17072d00edd4Smatthias.ringwald hci_con_used = 0; 1708665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1709665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1710665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1711fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 17122d00edd4Smatthias.ringwald hci_con_used = 1; 1713c22aecc9S[email protected] break; 1714ee091cf1Smatthias.ringwald } 17152d00edd4Smatthias.ringwald if (hci_con_used) break; 1716d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 17179edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1718afde0c52Smatthias.ringwald break; 1719ee091cf1Smatthias.ringwald 1720df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1721f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1722665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1723665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1724665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1725fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 17262df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1727df3354fcS[email protected] break; 1728df3354fcS[email protected] } 1729c22aecc9S[email protected] break; 1730df3354fcS[email protected] 17315611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1732f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1733bd63148eS[email protected] log_info("l2cap - security level update"); 1734665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1735665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1736665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1737fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 17385533f01eS[email protected] 1739bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1740bd63148eS[email protected] 1741e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 17425533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 17435533f01eS[email protected] 1744df3354fcS[email protected] switch (channel->state){ 1745df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 17465533f01eS[email protected] if (actual_level >= required_level){ 174752606043SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 174852606043SMatthias Ringwald // we need to know if ERTM is supported before sending a config response 174952606043SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(channel->con_handle); 175052606043SMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST; 175152606043SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES; 175252606043SMatthias Ringwald #else 1753f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 175444276248SMatthias Ringwald l2cap_emit_incoming_connection(channel); 175552606043SMatthias Ringwald #endif 17561eb2563eS[email protected] } else { 1757775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 17581eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 17591eb2563eS[email protected] } 1760df3354fcS[email protected] break; 1761df3354fcS[email protected] 1762df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 17635533f01eS[email protected] if (actual_level >= required_level){ 17641b9cb13dSMatthias Ringwald l2cap_ready_to_connect(channel); 1765df3354fcS[email protected] } else { 1766df3354fcS[email protected] // disconnnect, authentication not good enough 1767df3354fcS[email protected] hci_disconnect_security_block(handle); 1768df3354fcS[email protected] } 1769df3354fcS[email protected] break; 1770df3354fcS[email protected] 1771df3354fcS[email protected] default: 1772df3354fcS[email protected] break; 1773df3354fcS[email protected] } 1774f85a9399S[email protected] } 1775f85a9399S[email protected] break; 177609e9d05bSMatthias Ringwald #endif 1777f85a9399S[email protected] 1778afde0c52Smatthias.ringwald default: 1779afde0c52Smatthias.ringwald break; 1780afde0c52Smatthias.ringwald } 1781afde0c52Smatthias.ringwald 1782bd63148eS[email protected] l2cap_run(); 17831e6aba47Smatthias.ringwald } 17841e6aba47Smatthias.ringwald 1785e74c5f58SMatthias Ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t cid, uint16_t data){ 17864cf56b4aSmatthias.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." 17872b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 17882b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 17892b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 17902b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 1791e74c5f58SMatthias Ringwald signaling_responses[signaling_responses_pending].cid = cid; 17922b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 17932b360848Smatthias.ringwald signaling_responses_pending++; 17942b360848Smatthias.ringwald l2cap_run(); 17952b360848Smatthias.ringwald } 17962b360848Smatthias.ringwald } 17972b360848Smatthias.ringwald 179809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 179909e9d05bSMatthias Ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 180009e9d05bSMatthias Ringwald channel->remote_sig_id = identifier; 180109e9d05bSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 180209e9d05bSMatthias Ringwald l2cap_run(); 180309e9d05bSMatthias Ringwald } 180409e9d05bSMatthias Ringwald 1805b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1806645658c9Smatthias.ringwald 18079da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1808645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1809645658c9Smatthias.ringwald if (!service) { 1810645658c9Smatthias.ringwald // 0x0002 PSM not supported 1811e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0002); 1812645658c9Smatthias.ringwald return; 1813645658c9Smatthias.ringwald } 1814645658c9Smatthias.ringwald 18155061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1816645658c9Smatthias.ringwald if (!hci_connection) { 18172b360848Smatthias.ringwald // 18189da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1819645658c9Smatthias.ringwald return; 1820645658c9Smatthias.ringwald } 18212bd8b7e7S[email protected] 1822645658c9Smatthias.ringwald // alloc structure 18239da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1824da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC, 1825da144af5SMatthias Ringwald psm, service->mtu, service->required_security_level); 18262b360848Smatthias.ringwald if (!channel){ 18272b360848Smatthias.ringwald // 0x0004 No resources available 1828e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0004); 18292b360848Smatthias.ringwald return; 18302b360848Smatthias.ringwald } 1831da144af5SMatthias Ringwald 1832fc64f94aSMatthias Ringwald channel->con_handle = handle; 1833b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1834b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1835645658c9Smatthias.ringwald 1836f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 18372985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 18382985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 18399775e25bSmatthias.ringwald } 18409775e25bSmatthias.ringwald 1841645658c9Smatthias.ringwald // set initial state 1842df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1843a24785d0SMatthias Ringwald channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND | L2CAP_CHANNEL_STATE_VAR_INCOMING); 1844e405ae81Smatthias.ringwald 1845645658c9Smatthias.ringwald // add to connections list 1846665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1847645658c9Smatthias.ringwald 1848f85a9399S[email protected] // assert security requirements 18491eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1850e405ae81Smatthias.ringwald } 1851645658c9Smatthias.ringwald 1852ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1853e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1854b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1855e405ae81Smatthias.ringwald if (!channel) { 1856ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1857e405ae81Smatthias.ringwald return; 1858e405ae81Smatthias.ringwald } 1859e405ae81Smatthias.ringwald 186043ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 186143ec931dSMatthias Ringwald // configure L2CAP Basic mode 186243ec931dSMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 186343ec931dSMatthias Ringwald #endif 186443ec931dSMatthias Ringwald 1865552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1866e405ae81Smatthias.ringwald 1867552d92a1Smatthias.ringwald // process 1868552d92a1Smatthias.ringwald l2cap_run(); 1869e405ae81Smatthias.ringwald } 1870645658c9Smatthias.ringwald 187143ec931dSMatthias Ringwald 187243ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 1873843bae5dSMatthias Ringwald uint8_t l2cap_accept_ertm_connection(uint16_t local_cid, int ertm_mandatory, uint8_t max_transmit, uint16_t retransmission_timeout_ms, 1874843bae5dSMatthias Ringwald uint16_t monitor_timeout_ms, uint16_t local_mtu, uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size){ 1875501329faSMatthias Ringwald 187643ec931dSMatthias Ringwald log_info("L2CAP_ACCEPT_ERTM_CONNECTION local_cid 0x%x", local_cid); 187743ec931dSMatthias Ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 187843ec931dSMatthias Ringwald if (!channel) { 187943ec931dSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 18802b70d705SMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 188143ec931dSMatthias Ringwald } 188243ec931dSMatthias Ringwald 18832b70d705SMatthias Ringwald // validate local config 1884843bae5dSMatthias Ringwald uint8_t result = l2cap_ertm_validate_local_config(max_transmit, retransmission_timeout_ms, monitor_timeout_ms, local_mtu, num_tx_buffers, num_rx_buffers, buffer, size); 18852b70d705SMatthias Ringwald if (result) return result; 18862b70d705SMatthias Ringwald 188743ec931dSMatthias Ringwald // configure L2CAP ERTM 1888843bae5dSMatthias Ringwald l2cap_ertm_configure_channel(channel, ertm_mandatory, max_transmit, retransmission_timeout_ms, monitor_timeout_ms, local_mtu, num_tx_buffers, num_rx_buffers, buffer, size); 188943ec931dSMatthias Ringwald 18907b181629SMatthias Ringwald // continue 189143ec931dSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 189243ec931dSMatthias Ringwald 189343ec931dSMatthias Ringwald // process 189443ec931dSMatthias Ringwald l2cap_run(); 18952b70d705SMatthias Ringwald 18962b70d705SMatthias Ringwald return ERROR_CODE_SUCCESS; 189743ec931dSMatthias Ringwald } 18982a424812SMatthias Ringwald 189967a3a5b7SMatthias Ringwald uint8_t l2cap_ertm_set_busy(uint16_t local_cid){ 19008f1c9639SMatthias Ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 19018f1c9639SMatthias Ringwald if (!channel) { 19028f1c9639SMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 19038f1c9639SMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 19048f1c9639SMatthias Ringwald } 19052bea1b8dSMatthias Ringwald if (!channel->local_busy){ 19062bea1b8dSMatthias Ringwald channel->local_busy = 1; 19078f1c9639SMatthias Ringwald channel->send_supervisor_frame_receiver_not_ready = 1; 19088f1c9639SMatthias Ringwald l2cap_run(); 19092bea1b8dSMatthias Ringwald } 191067a3a5b7SMatthias Ringwald return ERROR_CODE_SUCCESS; 191167a3a5b7SMatthias Ringwald } 191267a3a5b7SMatthias Ringwald 191367a3a5b7SMatthias Ringwald uint8_t l2cap_ertm_set_ready(uint16_t local_cid){ 19142bea1b8dSMatthias Ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 19152bea1b8dSMatthias Ringwald if (!channel) { 19162bea1b8dSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 19172bea1b8dSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 19182bea1b8dSMatthias Ringwald } 19192bea1b8dSMatthias Ringwald if (channel->local_busy){ 19202bea1b8dSMatthias Ringwald channel->local_busy = 0; 19212bea1b8dSMatthias Ringwald channel->send_supervisor_frame_receiver_ready_poll = 1; 19222bea1b8dSMatthias Ringwald l2cap_run(); 19232bea1b8dSMatthias Ringwald } 192467a3a5b7SMatthias Ringwald return ERROR_CODE_SUCCESS; 192567a3a5b7SMatthias Ringwald } 192667a3a5b7SMatthias Ringwald 19272a424812SMatthias Ringwald static void l2cap_ertm_handle_req_seq(l2cap_channel_t * l2cap_channel, uint8_t req_seq){ 19282a424812SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state; 1929bc4521b7SMatthias Ringwald while (1){ 19302a424812SMatthias Ringwald tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index]; 1931bc4521b7SMatthias Ringwald // calc delta 1932bc4521b7SMatthias Ringwald int delta = (req_seq - tx_state->tx_seq) & 0x03f; 1933bc4521b7SMatthias Ringwald if (delta == 0) break; // all packets acknowledged 1934bc4521b7SMatthias Ringwald if (delta > l2cap_channel->remote_tx_window_size) break; 1935bc4521b7SMatthias Ringwald 1936bc4521b7SMatthias Ringwald log_info("RR seq %u => packet with tx_seq %u done", req_seq, tx_state->tx_seq); 1937bc4521b7SMatthias Ringwald 193862041d70SMatthias Ringwald // stop retransmission timer 193962041d70SMatthias Ringwald btstack_run_loop_remove_timer(&tx_state->retransmission_timer); 19402a424812SMatthias Ringwald l2cap_channel->tx_read_index++; 19412a424812SMatthias Ringwald if (l2cap_channel->tx_read_index >= l2cap_channel->num_rx_buffers){ 19422a424812SMatthias Ringwald l2cap_channel->tx_read_index = 0; 19432a424812SMatthias Ringwald } 1944bc4521b7SMatthias Ringwald 1945bc4521b7SMatthias Ringwald // no unack packages 1946bc4521b7SMatthias Ringwald if (l2cap_channel->tx_read_index == l2cap_channel->tx_write_index) break; 19472a424812SMatthias Ringwald } 19482a424812SMatthias Ringwald } 194967a3a5b7SMatthias Ringwald 19507b7901d8SMatthias Ringwald static l2cap_ertm_tx_packet_state_t * l2cap_ertm_get_tx_state(l2cap_channel_t * l2cap_channel, uint8_t tx_seq){ 19517b7901d8SMatthias Ringwald int i; 19527b7901d8SMatthias Ringwald for (i=0;i<l2cap_channel->num_tx_buffers;i++){ 19537b7901d8SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state = &l2cap_channel->tx_packets_state[i]; 19547b7901d8SMatthias Ringwald if (tx_state->tx_seq == tx_seq) return tx_state; 19557b7901d8SMatthias Ringwald } 19567b7901d8SMatthias Ringwald return NULL; 19577b7901d8SMatthias Ringwald } 195843ec931dSMatthias Ringwald #endif 195943ec931dSMatthias Ringwald 196043ec931dSMatthias Ringwald 19617ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){ 19627ef6a7bbSMatthias Ringwald log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid); 1963b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1964e405ae81Smatthias.ringwald if (!channel) { 1965ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1966e405ae81Smatthias.ringwald return; 1967e405ae81Smatthias.ringwald } 1968e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 19697ef6a7bbSMatthias Ringwald channel->reason = 0x04; // no resources available 1970e7ff783cSmatthias.ringwald l2cap_run(); 1971645658c9Smatthias.ringwald } 1972645658c9Smatthias.ringwald 19737f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1974b1988dceSmatthias.ringwald 1975b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1976b1988dceSmatthias.ringwald 1977f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 197863a7246aSmatthias.ringwald if (flags & 1) { 197963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 198063a7246aSmatthias.ringwald } 198163a7246aSmatthias.ringwald 19822784b77dSmatthias.ringwald // accept the other's configuration options 1983f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 19843de7c0caSmatthias.ringwald uint16_t pos = 8; 19853de7c0caSmatthias.ringwald while (pos < end_pos){ 198663a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 198763a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 19883844aeadSMatthias Ringwald // log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 198963a7246aSmatthias.ringwald pos++; 19901dc511deSmatthias.ringwald uint8_t length = command[pos++]; 19911dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 199263a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1993f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 19943844aeadSMatthias Ringwald log_info("Remote MTU %u", channel->remote_mtu); 19959d139fbaSMatthias Ringwald if (channel->remote_mtu > l2cap_max_mtu()){ 19969d139fbaSMatthias Ringwald log_info("Remote MTU %u larger than outgoing buffer, only using MTU = %u", channel->remote_mtu, l2cap_max_mtu()); 19979d139fbaSMatthias Ringwald channel->remote_mtu = l2cap_max_mtu(); 19989d139fbaSMatthias Ringwald } 199963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 200063a7246aSmatthias.ringwald } 20010fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 20020fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 2003f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 20043844aeadSMatthias Ringwald log_info("Flush timeout: %u ms", channel->flush_timeout); 20050fe7a9d0S[email protected] } 2006f2fa388dSMatthias Ringwald 20076dca2a0cSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 20086dca2a0cSMatthias Ringwald // Retransmission and Flow Control Option 20096dca2a0cSMatthias Ringwald if (option_type == 4 && length == 9){ 20103232a1c6SMatthias Ringwald l2cap_channel_mode_t mode = (l2cap_channel_mode_t) command[pos]; 2011ac8f1300SMatthias Ringwald switch(channel->mode){ 2012ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION: 201325cd60d3SMatthias Ringwald // Store remote config 2014bbc0a9e7SMatthias Ringwald channel->remote_tx_window_size = command[pos+1]; 2015bbc0a9e7SMatthias Ringwald channel->remote_max_transmit = command[pos+2]; 2016bbc0a9e7SMatthias Ringwald channel->remote_retransmission_timeout_ms = little_endian_read_16(command, pos + 3); 2017bbc0a9e7SMatthias Ringwald channel->remote_monitor_timeout_ms = little_endian_read_16(command, pos + 5); 20183844aeadSMatthias Ringwald channel->remote_mps = little_endian_read_16(command, pos + 7); 20193844aeadSMatthias Ringwald log_info("FC&C config: tx window: %u, max transmit %u, retrans timeout %u, monitor timeout %u, mps %u", 2020bbc0a9e7SMatthias Ringwald channel->remote_tx_window_size, 2021bbc0a9e7SMatthias Ringwald channel->remote_max_transmit, 2022bbc0a9e7SMatthias Ringwald channel->remote_retransmission_timeout_ms, 20233844aeadSMatthias Ringwald channel->remote_monitor_timeout_ms, 20243844aeadSMatthias Ringwald channel->remote_mps); 202525cd60d3SMatthias Ringwald // If ERTM mandatory, but remote doens't offer ERTM -> disconnect 202625cd60d3SMatthias Ringwald if (channel->ertm_mandatory && mode != L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 202725cd60d3SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 2028ac8f1300SMatthias Ringwald } else { 2029ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 2030ac8f1300SMatthias Ringwald } 2031ac8f1300SMatthias Ringwald break; 2032ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_BASIC: 2033ac8f1300SMatthias Ringwald switch (mode){ 2034ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION: 2035ac8f1300SMatthias Ringwald // remote asks for ERTM, but we want basic mode. disconnect if this happens a second time 2036ac8f1300SMatthias Ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED){ 2037ac8f1300SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 2038ac8f1300SMatthias Ringwald } 2039ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED); 2040ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED); 2041ac8f1300SMatthias Ringwald break; 2042ac8f1300SMatthias Ringwald default: // case L2CAP_CHANNEL_MODE_BASIC: 2043ac8f1300SMatthias Ringwald // TODO store and evaluate configuration 2044ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 2045ac8f1300SMatthias Ringwald break; 2046ac8f1300SMatthias Ringwald } 2047ac8f1300SMatthias Ringwald break; 2048ac8f1300SMatthias Ringwald default: 2049ac8f1300SMatthias Ringwald break; 2050ac8f1300SMatthias Ringwald } 20513232a1c6SMatthias Ringwald } 20526dca2a0cSMatthias Ringwald #endif 205363a7246aSmatthias.ringwald // check for unknown options 205463a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 2055c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 205663a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 20571dc511deSmatthias.ringwald } 20581dc511deSmatthias.ringwald pos += length; 20591dc511deSmatthias.ringwald } 20602784b77dSmatthias.ringwald } 20612784b77dSmatthias.ringwald 2062f2fa388dSMatthias Ringwald static void l2cap_signaling_handle_configure_response(l2cap_channel_t *channel, uint8_t result, uint8_t *command){ 2063f2fa388dSMatthias Ringwald log_info("l2cap_signaling_handle_configure_response"); 20643232a1c6SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 20653232a1c6SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 2066f2fa388dSMatthias Ringwald uint16_t pos = 10; 20673232a1c6SMatthias Ringwald while (pos < end_pos){ 20683232a1c6SMatthias Ringwald uint8_t option_hint = command[pos] >> 7; 20693232a1c6SMatthias Ringwald uint8_t option_type = command[pos] & 0x7f; 20703232a1c6SMatthias Ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 20713232a1c6SMatthias Ringwald pos++; 20723232a1c6SMatthias Ringwald uint8_t length = command[pos++]; 20733232a1c6SMatthias Ringwald 20743232a1c6SMatthias Ringwald // Retransmission and Flow Control Option 20753232a1c6SMatthias Ringwald if (option_type == 4 && length == 9){ 2076ac8f1300SMatthias Ringwald switch (channel->mode){ 2077ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION: 2078f2fa388dSMatthias Ringwald if (channel->ertm_mandatory){ 2079ac8f1300SMatthias Ringwald // ?? 2080f2fa388dSMatthias Ringwald } else { 2081ac8f1300SMatthias Ringwald // On 'Reject - Unacceptable Parameters' to our optional ERTM request, fall back to BASIC mode 2082ac8f1300SMatthias Ringwald if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){ 2083f2fa388dSMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 20843232a1c6SMatthias Ringwald } 20853232a1c6SMatthias Ringwald } 2086ac8f1300SMatthias Ringwald break; 2087ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_BASIC: 2088ac8f1300SMatthias Ringwald if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){ 2089ac8f1300SMatthias Ringwald // On 'Reject - Unacceptable Parameters' to our Basic mode request, disconnect 2090ac8f1300SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 2091ac8f1300SMatthias Ringwald } 2092ac8f1300SMatthias Ringwald break; 2093ac8f1300SMatthias Ringwald default: 2094ac8f1300SMatthias Ringwald break; 20953232a1c6SMatthias Ringwald } 2096f2fa388dSMatthias Ringwald } 20973232a1c6SMatthias Ringwald 20983232a1c6SMatthias Ringwald // check for unknown options 20993232a1c6SMatthias Ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 21003232a1c6SMatthias Ringwald log_info("l2cap cid %u, unknown options", channel->local_cid); 21013232a1c6SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 21023232a1c6SMatthias Ringwald } 21033232a1c6SMatthias Ringwald 21043232a1c6SMatthias Ringwald pos += length; 21053232a1c6SMatthias Ringwald } 21063232a1c6SMatthias Ringwald #endif 21073232a1c6SMatthias Ringwald } 21083232a1c6SMatthias Ringwald 2109fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 21109da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 211173cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 211273cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 2113019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 2114019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 2115fa8473a4Smatthias.ringwald return 1; 2116fa8473a4Smatthias.ringwald } 2117fa8473a4Smatthias.ringwald 2118fa8473a4Smatthias.ringwald 21197f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 21201e6aba47Smatthias.ringwald 212100d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 212200d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 212338e5900eSmatthias.ringwald uint16_t result = 0; 21241e6aba47Smatthias.ringwald 21259da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 2126b35f641cSmatthias.ringwald 21279a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 21289a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 21299a011532Smatthias.ringwald switch (channel->state){ 2130fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 21319a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 21322b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 21339a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 21349a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 21359a011532Smatthias.ringwald break; 21369a011532Smatthias.ringwald 21379a011532Smatthias.ringwald default: 21389a011532Smatthias.ringwald // ignore in other states 21399a011532Smatthias.ringwald break; 21409a011532Smatthias.ringwald } 21419a011532Smatthias.ringwald return; 21429a011532Smatthias.ringwald } 21439a011532Smatthias.ringwald 214456081214Smatthias.ringwald // @STATEMACHINE(l2cap) 21451e6aba47Smatthias.ringwald switch (channel->state) { 21461e6aba47Smatthias.ringwald 21471e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 21481e6aba47Smatthias.ringwald switch (code){ 21491e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 21505932bd7cS[email protected] l2cap_stop_rtx(channel); 2151f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 215238e5900eSmatthias.ringwald switch (result) { 215338e5900eSmatthias.ringwald case 0: 2154169f8b28Smatthias.ringwald // successful connection 2155f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 2156fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 215728ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 215838e5900eSmatthias.ringwald break; 215938e5900eSmatthias.ringwald case 1: 21605932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 21615932bd7cS[email protected] l2cap_start_ertx(channel); 216238e5900eSmatthias.ringwald break; 216338e5900eSmatthias.ringwald default: 2164eb920dbeSmatthias.ringwald // channel closed 2165eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 2166f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 216738e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 2168eb920dbeSmatthias.ringwald 2169eb920dbeSmatthias.ringwald // drop link key if security block 2170eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 217115a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 2172eb920dbeSmatthias.ringwald } 2173eb920dbeSmatthias.ringwald 2174eb920dbeSmatthias.ringwald // discard channel 2175665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 2176d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 217738e5900eSmatthias.ringwald break; 21781e6aba47Smatthias.ringwald } 21791e6aba47Smatthias.ringwald break; 218038e5900eSmatthias.ringwald 218138e5900eSmatthias.ringwald default: 21821e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 218338e5900eSmatthias.ringwald break; 21841e6aba47Smatthias.ringwald } 21851e6aba47Smatthias.ringwald break; 21861e6aba47Smatthias.ringwald 2187fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 2188f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 2189ae280e73Smatthias.ringwald switch (code) { 2190ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 219128ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 2192ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 219363a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 219463a7246aSmatthias.ringwald // only done if continuation not set 219563a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 219663a7246aSmatthias.ringwald } 2197ae280e73Smatthias.ringwald break; 21981e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 21995932bd7cS[email protected] l2cap_stop_rtx(channel); 2200f2fa388dSMatthias Ringwald l2cap_signaling_handle_configure_response(channel, result, command); 22015932bd7cS[email protected] switch (result){ 22025932bd7cS[email protected] case 0: // success 22035932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 22045932bd7cS[email protected] break; 22055932bd7cS[email protected] case 4: // pending 22065932bd7cS[email protected] l2cap_start_ertx(channel); 22075932bd7cS[email protected] break; 22085932bd7cS[email protected] default: 2209a32d6a03SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 2210a32d6a03SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->ertm_mandatory){ 2211a32d6a03SMatthias Ringwald // remote does not offer ertm but it's required 2212a32d6a03SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 2213a32d6a03SMatthias Ringwald break; 2214a32d6a03SMatthias Ringwald } 2215a32d6a03SMatthias Ringwald #endif 2216fe9d8984S[email protected] // retry on negative result 2217fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 2218fe9d8984S[email protected] break; 2219fe9d8984S[email protected] } 22205a67bd4aSmatthias.ringwald break; 22215a67bd4aSmatthias.ringwald default: 22225a67bd4aSmatthias.ringwald break; 22231e6aba47Smatthias.ringwald } 2224fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 2225fa8473a4Smatthias.ringwald // for open: 22265a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 2227fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 2228c8e4258aSmatthias.ringwald } 2229c8e4258aSmatthias.ringwald break; 2230f62db1e3Smatthias.ringwald 2231f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 2232f62db1e3Smatthias.ringwald switch (code) { 2233f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 223427a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 223527a923d0Smatthias.ringwald break; 22365a67bd4aSmatthias.ringwald default: 22375a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 22385a67bd4aSmatthias.ringwald break; 223927a923d0Smatthias.ringwald } 224027a923d0Smatthias.ringwald break; 224184836b65Smatthias.ringwald 224284836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 224384836b65Smatthias.ringwald // @TODO handle incoming requests 224484836b65Smatthias.ringwald break; 224584836b65Smatthias.ringwald 224684836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 224784836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 224884836b65Smatthias.ringwald break; 224910642e45Smatthias.ringwald default: 225010642e45Smatthias.ringwald break; 225127a923d0Smatthias.ringwald } 22529da54300S[email protected] // log_info("new state %u", channel->state); 225327a923d0Smatthias.ringwald } 225427a923d0Smatthias.ringwald 225500d93d79Smatthias.ringwald 22567f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 225700d93d79Smatthias.ringwald 22581b9cb13dSMatthias Ringwald btstack_linked_list_iterator_t it; 22591b9cb13dSMatthias Ringwald 226000d93d79Smatthias.ringwald // get code, signalind identifier and command len 226100d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 226200d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 226300d93d79Smatthias.ringwald 22641b9cb13dSMatthias Ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_RESPONSE 22651b9cb13dSMatthias Ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_RESPONSE){ 2266e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN); 226700d93d79Smatthias.ringwald return; 226800d93d79Smatthias.ringwald } 226900d93d79Smatthias.ringwald 227000d93d79Smatthias.ringwald // general commands without an assigned channel 227100d93d79Smatthias.ringwald switch(code) { 227200d93d79Smatthias.ringwald 227300d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 2274f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 2275f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 227600d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 22772b83fb7dSmatthias.ringwald return; 227800d93d79Smatthias.ringwald } 227900d93d79Smatthias.ringwald 22802b360848Smatthias.ringwald case ECHO_REQUEST: 2281e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, code, sig_id, 0, 0); 22822b83fb7dSmatthias.ringwald return; 228300d93d79Smatthias.ringwald 228400d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 2285f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 2286e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, code, sig_id, 0, infoType); 22872b83fb7dSmatthias.ringwald return; 228800d93d79Smatthias.ringwald } 228900d93d79Smatthias.ringwald 22901b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 22911b9cb13dSMatthias Ringwald case INFORMATION_RESPONSE: { 22921b9cb13dSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 22931b9cb13dSMatthias Ringwald if (!connection) return; 22941b9cb13dSMatthias Ringwald uint16_t info_type = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 22951b9cb13dSMatthias Ringwald uint16_t result = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 22961b9cb13dSMatthias Ringwald if (result != 0) return; 2297543b84e4SMatthias Ringwald if (info_type != 0x02) return; 22981b9cb13dSMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_DONE; 22991b9cb13dSMatthias Ringwald connection->l2cap_state.extended_feature_mask = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 2300543b84e4SMatthias Ringwald log_info("extended features mask 0x%02x", connection->l2cap_state.extended_feature_mask); 23011b9cb13dSMatthias Ringwald // trigger connection request 23021b9cb13dSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 23031b9cb13dSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 23041b9cb13dSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2305543b84e4SMatthias Ringwald if (channel->con_handle != handle) continue; 2306543b84e4SMatthias Ringwald // bail if ERTM was requested but is not supported 2307543b84e4SMatthias Ringwald if ((channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) && ((connection->l2cap_state.extended_feature_mask & 0x08) == 0)){ 2308f073f086SMatthias Ringwald if (channel->ertm_mandatory){ 2309543b84e4SMatthias Ringwald // channel closed 2310543b84e4SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 2311543b84e4SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 2312543b84e4SMatthias Ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_ERTM_NOT_SUPPORTD); 2313543b84e4SMatthias Ringwald // discard channel 2314543b84e4SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 2315543b84e4SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 2316543b84e4SMatthias Ringwald continue; 2317f073f086SMatthias Ringwald } else { 2318f073f086SMatthias Ringwald // fallback to Basic mode 2319f073f086SMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 2320f073f086SMatthias Ringwald } 2321543b84e4SMatthias Ringwald } 2322543b84e4SMatthias Ringwald // start connecting 23231b9cb13dSMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES){ 23241b9cb13dSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 23251b9cb13dSMatthias Ringwald } 232652606043SMatthias Ringwald // respond to connection request 232752606043SMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES){ 232852606043SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 232952606043SMatthias Ringwald l2cap_emit_incoming_connection(channel); 233052606043SMatthias Ringwald } 23311b9cb13dSMatthias Ringwald } 23321b9cb13dSMatthias Ringwald return; 23331b9cb13dSMatthias Ringwald } 23341b9cb13dSMatthias Ringwald #endif 23351b9cb13dSMatthias Ringwald 233600d93d79Smatthias.ringwald default: 233700d93d79Smatthias.ringwald break; 233800d93d79Smatthias.ringwald } 233900d93d79Smatthias.ringwald 234000d93d79Smatthias.ringwald 234100d93d79Smatthias.ringwald // Get potential destination CID 2342f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 234300d93d79Smatthias.ringwald 234400d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 2345665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 2346665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2347665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2348fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 234900d93d79Smatthias.ringwald if (code & 1) { 2350b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 2351b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 235200d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 23534e32727eSmatthias.ringwald break; 235400d93d79Smatthias.ringwald } 235500d93d79Smatthias.ringwald } else { 2356b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 235700d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 235800d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 23594e32727eSmatthias.ringwald break; 236000d93d79Smatthias.ringwald } 236100d93d79Smatthias.ringwald } 236200d93d79Smatthias.ringwald } 236300d93d79Smatthias.ringwald } 236409e9d05bSMatthias Ringwald #endif 236500d93d79Smatthias.ringwald 2366e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 236709e9d05bSMatthias Ringwald 236809e9d05bSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){ 236909e9d05bSMatthias Ringwald uint8_t event[6]; 237009e9d05bSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 237109e9d05bSMatthias Ringwald event[1] = 4; 237209e9d05bSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 237309e9d05bSMatthias Ringwald little_endian_store_16(event, 4, result); 237409e9d05bSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 237509e9d05bSMatthias Ringwald if (!l2cap_event_packet_handler) return; 237609e9d05bSMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 237709e9d05bSMatthias Ringwald } 237809e9d05bSMatthias Ringwald 2379c48b2a2cSMatthias Ringwald // @returns valid 2380c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){ 2381e7d0c9aaSMatthias Ringwald hci_connection_t * connection; 2382c48b2a2cSMatthias Ringwald uint16_t result; 2383c48b2a2cSMatthias Ringwald uint8_t event[10]; 238483fd9c76SMatthias Ringwald 2385cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 2386a3dc965aSMatthias Ringwald btstack_linked_list_iterator_t it; 2387a3dc965aSMatthias Ringwald l2cap_channel_t * channel; 2388a3dc965aSMatthias Ringwald uint16_t local_cid; 238983fd9c76SMatthias Ringwald uint16_t le_psm; 239063f0ac45SMatthias Ringwald uint16_t new_credits; 239163f0ac45SMatthias Ringwald uint16_t credits_before; 2392e7d0c9aaSMatthias Ringwald l2cap_service_t * service; 239311cae19eSMilanka Ringwald uint16_t source_cid; 239483fd9c76SMatthias Ringwald #endif 239500d93d79Smatthias.ringwald 23961b8b8d05SMatthias Ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 239723017473SMatthias Ringwald log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id); 23981b8b8d05SMatthias Ringwald 23991b8b8d05SMatthias Ringwald switch (code){ 240000d93d79Smatthias.ringwald 2401c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_RESPONSE: 2402c48b2a2cSMatthias Ringwald result = little_endian_read_16(command, 4); 2403ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 2404ccf076adS[email protected] break; 2405da886c03S[email protected] 2406c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_REQUEST: 2407e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 2408da886c03S[email protected] if (connection){ 24096d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 24106d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 2411c48b2a2cSMatthias Ringwald return 0; 24126d91fb6cSMatthias Ringwald } 2413da886c03S[email protected] int update_parameter = 1; 2414a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 24154ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 2416c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(command,8); 2417c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(command,10); 2418c48b2a2cSMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(command,12); 2419c48b2a2cSMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(command,14); 2420da886c03S[email protected] 2421da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 2422da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 2423da886c03S[email protected] 2424da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 2425da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 2426da886c03S[email protected] 2427da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 2428da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 2429da886c03S[email protected] 2430da886c03S[email protected] if (update_parameter){ 2431da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 2432da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 2433da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 2434da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 2435da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 2436da886c03S[email protected] } else { 2437da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 2438da886c03S[email protected] } 2439c48b2a2cSMatthias Ringwald connection->le_con_param_update_identifier = sig_id; 2440da886c03S[email protected] } 2441da886c03S[email protected] 244233c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 2443c48b2a2cSMatthias Ringwald 2444c48b2a2cSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 2445c48b2a2cSMatthias Ringwald event[1] = 8; 2446c48b2a2cSMatthias Ringwald memcpy(&event[2], &command[4], 8); 2447c48b2a2cSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 244833c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 2449ccf076adS[email protected] break; 2450c48b2a2cSMatthias Ringwald 2451a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 2452a3dc965aSMatthias Ringwald 245363f0ac45SMatthias Ringwald case COMMAND_REJECT: 245463f0ac45SMatthias Ringwald // Find channel for this sig_id and connection handle 245563f0ac45SMatthias Ringwald channel = NULL; 245663f0ac45SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 245763f0ac45SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 245863f0ac45SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 245963f0ac45SMatthias Ringwald if (a_channel->con_handle != handle) continue; 246063f0ac45SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 246163f0ac45SMatthias Ringwald channel = a_channel; 246263f0ac45SMatthias Ringwald break; 246363f0ac45SMatthias Ringwald } 246463f0ac45SMatthias Ringwald if (!channel) break; 246563f0ac45SMatthias Ringwald 246663f0ac45SMatthias Ringwald // if received while waiting for le connection response, assume legacy device 246763f0ac45SMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){ 246863f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 246963f0ac45SMatthias Ringwald // no official value for this, use: Connection refused – LE_PSM not supported - 0x0002 247044276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, 0x0002); 247163f0ac45SMatthias Ringwald 247263f0ac45SMatthias Ringwald // discard channel 247363f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 247463f0ac45SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 247563f0ac45SMatthias Ringwald break; 247663f0ac45SMatthias Ringwald } 247763f0ac45SMatthias Ringwald break; 247863f0ac45SMatthias Ringwald 2479e7d0c9aaSMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 2480e7d0c9aaSMatthias Ringwald 2481e7d0c9aaSMatthias Ringwald // get hci connection, bail if not found (must not happen) 2482e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 2483e7d0c9aaSMatthias Ringwald if (!connection) return 0; 2484e7d0c9aaSMatthias Ringwald 2485e7d0c9aaSMatthias Ringwald // check if service registered 2486e7d0c9aaSMatthias Ringwald le_psm = little_endian_read_16(command, 4); 2487e7d0c9aaSMatthias Ringwald service = l2cap_le_get_service(le_psm); 248811cae19eSMilanka Ringwald source_cid = little_endian_read_16(command, 6); 2489e7d0c9aaSMatthias Ringwald 2490e7d0c9aaSMatthias Ringwald if (service){ 2491e7d0c9aaSMatthias Ringwald if (source_cid < 0x40){ 2492e7d0c9aaSMatthias Ringwald // 0x0009 Connection refused - Invalid Source CID 2493e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0009); 2494e7d0c9aaSMatthias Ringwald return 1; 2495e7d0c9aaSMatthias Ringwald } 2496e7d0c9aaSMatthias Ringwald 2497e7d0c9aaSMatthias Ringwald // go through list of channels for this ACL connection and check if we get a match 2498e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 2499e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 25001b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 25011b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 25021b8b8d05SMatthias Ringwald if (a_channel->remote_cid != source_cid) continue; 2503e7d0c9aaSMatthias Ringwald // 0x000a Connection refused - Source CID already allocated 2504e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x000a); 2505e7d0c9aaSMatthias Ringwald return 1; 2506e7d0c9aaSMatthias Ringwald } 2507e7d0c9aaSMatthias Ringwald 250883fd9c76SMatthias Ringwald // security: check encryption 250983fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_2){ 251083fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) == 0){ 251183fd9c76SMatthias Ringwald // 0x0008 Connection refused - insufficient encryption 2512e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0008); 251383fd9c76SMatthias Ringwald return 1; 251483fd9c76SMatthias Ringwald } 251583fd9c76SMatthias Ringwald // anything less than 16 byte key size is insufficient 251683fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) < 16){ 251783fd9c76SMatthias Ringwald // 0x0007 Connection refused – insufficient encryption key size 2518e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0007); 251983fd9c76SMatthias Ringwald return 1; 252083fd9c76SMatthias Ringwald } 252183fd9c76SMatthias Ringwald } 252283fd9c76SMatthias Ringwald 252383fd9c76SMatthias Ringwald // security: check authencation 252483fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_3){ 252583fd9c76SMatthias Ringwald if (!sm_authenticated(handle)){ 252683fd9c76SMatthias Ringwald // 0x0005 Connection refused – insufficient authentication 2527e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0005); 252883fd9c76SMatthias Ringwald return 1; 252983fd9c76SMatthias Ringwald } 253083fd9c76SMatthias Ringwald } 253183fd9c76SMatthias Ringwald 253283fd9c76SMatthias Ringwald // security: check authorization 253383fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_4){ 253483fd9c76SMatthias Ringwald if (sm_authorization_state(handle) != AUTHORIZATION_GRANTED){ 253583fd9c76SMatthias Ringwald // 0x0006 Connection refused – insufficient authorization 2536e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0006); 253783fd9c76SMatthias Ringwald return 1; 253883fd9c76SMatthias Ringwald } 253983fd9c76SMatthias Ringwald } 2540e7d0c9aaSMatthias Ringwald 2541e7d0c9aaSMatthias Ringwald // allocate channel 25421b8b8d05SMatthias Ringwald channel = l2cap_create_channel_entry(service->packet_handler, connection->address, 2543e7d0c9aaSMatthias Ringwald BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level); 2544e7d0c9aaSMatthias Ringwald if (!channel){ 2545e7d0c9aaSMatthias Ringwald // 0x0004 Connection refused – no resources available 2546e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0004); 2547e7d0c9aaSMatthias Ringwald return 1; 2548e7d0c9aaSMatthias Ringwald } 2549e7d0c9aaSMatthias Ringwald 2550e7d0c9aaSMatthias Ringwald channel->con_handle = handle; 2551e7d0c9aaSMatthias Ringwald channel->remote_cid = source_cid; 2552e7d0c9aaSMatthias Ringwald channel->remote_sig_id = sig_id; 25537f107edaSMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, 8); 25547f107edaSMatthias Ringwald channel->remote_mps = little_endian_read_16(command, 10); 25557f107edaSMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, 12); 2556e7d0c9aaSMatthias Ringwald 2557e7d0c9aaSMatthias Ringwald // set initial state 2558e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 2559c99bb618SMatthias Ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING; 2560e7d0c9aaSMatthias Ringwald 2561e7d0c9aaSMatthias Ringwald // add to connections list 2562e7d0c9aaSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 2563e7d0c9aaSMatthias Ringwald 2564e7d0c9aaSMatthias Ringwald // post connection request event 256544276248SMatthias Ringwald l2cap_emit_le_incoming_connection(channel); 2566e7d0c9aaSMatthias Ringwald 2567e7d0c9aaSMatthias Ringwald } else { 2568e7d0c9aaSMatthias Ringwald // Connection refused – LE_PSM not supported 2569e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0002); 2570e7d0c9aaSMatthias Ringwald } 2571e7d0c9aaSMatthias Ringwald break; 25721b8b8d05SMatthias Ringwald 25731b8b8d05SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_RESPONSE: 25741b8b8d05SMatthias Ringwald // Find channel for this sig_id and connection handle 25751b8b8d05SMatthias Ringwald channel = NULL; 25761b8b8d05SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 25771b8b8d05SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 25781b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 25791b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 25801b8b8d05SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 25811b8b8d05SMatthias Ringwald channel = a_channel; 25821b8b8d05SMatthias Ringwald break; 25831b8b8d05SMatthias Ringwald } 25841b8b8d05SMatthias Ringwald if (!channel) break; 25851b8b8d05SMatthias Ringwald 25861b8b8d05SMatthias Ringwald // cid + 0 25871b8b8d05SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8); 25881b8b8d05SMatthias Ringwald if (result){ 25891b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 25901b8b8d05SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 259144276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 25921b8b8d05SMatthias Ringwald 25931b8b8d05SMatthias Ringwald // discard channel 259463f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 25951b8b8d05SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 25961b8b8d05SMatthias Ringwald break; 25971b8b8d05SMatthias Ringwald } 25981b8b8d05SMatthias Ringwald 25991b8b8d05SMatthias Ringwald // success 26001b8b8d05SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 26011b8b8d05SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 26021b8b8d05SMatthias Ringwald channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4); 26031b8b8d05SMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6); 26041b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 260544276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 26061b8b8d05SMatthias Ringwald break; 26071b8b8d05SMatthias Ringwald 260885aeef60SMatthias Ringwald case LE_FLOW_CONTROL_CREDIT: 260985aeef60SMatthias Ringwald // find channel 261085aeef60SMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 261185aeef60SMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 261263f0ac45SMatthias Ringwald if (!channel) { 261363f0ac45SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 261463f0ac45SMatthias Ringwald break; 261563f0ac45SMatthias Ringwald } 261663f0ac45SMatthias Ringwald new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 261763f0ac45SMatthias Ringwald credits_before = channel->credits_outgoing; 261863f0ac45SMatthias Ringwald channel->credits_outgoing += new_credits; 261963f0ac45SMatthias Ringwald // check for credit overrun 262063f0ac45SMatthias Ringwald if (credits_before > channel->credits_outgoing){ 262163f0ac45SMatthias Ringwald log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid); 262263f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 262363f0ac45SMatthias Ringwald break; 262463f0ac45SMatthias Ringwald } 262563f0ac45SMatthias Ringwald log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing); 262685aeef60SMatthias Ringwald break; 262785aeef60SMatthias Ringwald 2628828a7f7aSMatthias Ringwald case DISCONNECTION_REQUEST: 2629828a7f7aSMatthias Ringwald // find channel 2630828a7f7aSMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 2631828a7f7aSMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 263263f34e00SMatthias Ringwald if (!channel) { 263363f34e00SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 263463f34e00SMatthias Ringwald break; 263563f34e00SMatthias Ringwald } 2636828a7f7aSMatthias Ringwald channel->remote_sig_id = sig_id; 2637828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 2638828a7f7aSMatthias Ringwald break; 2639828a7f7aSMatthias Ringwald 2640a3dc965aSMatthias Ringwald #endif 2641a3dc965aSMatthias Ringwald 264263f0ac45SMatthias Ringwald case DISCONNECTION_RESPONSE: 264363f0ac45SMatthias Ringwald break; 264463f0ac45SMatthias Ringwald 2645c48b2a2cSMatthias Ringwald default: 2646c48b2a2cSMatthias Ringwald // command unknown -> reject command 2647c48b2a2cSMatthias Ringwald return 0; 2648ccf076adS[email protected] } 2649c48b2a2cSMatthias Ringwald return 1; 2650c48b2a2cSMatthias Ringwald } 2651e7d0c9aaSMatthias Ringwald #endif 2652c48b2a2cSMatthias Ringwald 2653d48432d4SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 265470734707SMatthias Ringwald 265570734707SMatthias Ringwald // @param delta number of frames in the future, >= 1 265670734707SMatthias Ringwald static void l2cap_ertm_handle_out_of_sequence_sdu(l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, int delta, uint8_t * payload, uint16_t size){ 265770734707SMatthias Ringwald log_info("Store SDU with delta %u", delta); 265870734707SMatthias Ringwald // get rx state for packet to store 265970734707SMatthias Ringwald int index = l2cap_channel->rx_store_index + delta - 1; 266070734707SMatthias Ringwald if (index > l2cap_channel->num_rx_buffers){ 266170734707SMatthias Ringwald index -= l2cap_channel->num_rx_buffers; 266270734707SMatthias Ringwald } 266370734707SMatthias Ringwald log_info("Index of packet to store %u", index); 266470734707SMatthias Ringwald l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index]; 266570734707SMatthias Ringwald // check if buffer is free 266670734707SMatthias Ringwald if (rx_state->valid){ 266770734707SMatthias Ringwald log_error("Packet buffer already used"); 266870734707SMatthias Ringwald return; 266970734707SMatthias Ringwald } 267070734707SMatthias Ringwald rx_state->valid = 1; 267170734707SMatthias Ringwald rx_state->sar = sar; 267270734707SMatthias Ringwald rx_state->len = size; 267370734707SMatthias Ringwald uint8_t * rx_buffer = &l2cap_channel->rx_packets_data[index]; 267470734707SMatthias Ringwald memcpy(rx_buffer, payload, size); 267570734707SMatthias Ringwald } 267670734707SMatthias Ringwald 2677e32be409SMatthias Ringwald static void l2cap_ertm_handle_in_sequence_sdu(l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, uint8_t * payload, uint16_t size){ 2678d48432d4SMatthias Ringwald switch (sar){ 2679d48432d4SMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU: 2680e32be409SMatthias Ringwald // packet complete -> disapatch 2681e32be409SMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, payload, size); 2682d48432d4SMatthias Ringwald break; 2683d48432d4SMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU: 2684d48432d4SMatthias Ringwald // TODO: check if reassembly started 2685e8e9809fSMatthias Ringwald // TODO: check sdu_len against local mtu 2686e32be409SMatthias Ringwald l2cap_channel->reassembly_sdu_length = little_endian_read_16(payload, 0); 2687e32be409SMatthias Ringwald payload += 2; 2688e32be409SMatthias Ringwald size -= 2; 2689e32be409SMatthias Ringwald memcpy(&l2cap_channel->reassembly_buffer[0], payload, size); 2690e32be409SMatthias Ringwald l2cap_channel->reassembly_pos = size; 2691d48432d4SMatthias Ringwald break; 2692d48432d4SMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU: 2693e32be409SMatthias Ringwald memcpy(&l2cap_channel->reassembly_buffer[l2cap_channel->reassembly_pos], payload, size); 2694e32be409SMatthias Ringwald l2cap_channel->reassembly_pos += size; 2695e32be409SMatthias Ringwald break; 2696e32be409SMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU: 2697e32be409SMatthias Ringwald memcpy(&l2cap_channel->reassembly_buffer[l2cap_channel->reassembly_pos], payload, size); 2698e32be409SMatthias Ringwald l2cap_channel->reassembly_pos += size; 2699e32be409SMatthias Ringwald // packet complete -> disapatch 2700e32be409SMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->reassembly_buffer, l2cap_channel->reassembly_pos); 2701e32be409SMatthias Ringwald l2cap_channel->reassembly_pos = 0; 2702d48432d4SMatthias Ringwald break; 2703d48432d4SMatthias Ringwald } 2704d48432d4SMatthias Ringwald } 2705d48432d4SMatthias Ringwald #endif 2706d48432d4SMatthias Ringwald 2707c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 27089ec2630cSMatthias Ringwald UNUSED(packet_type); 27099ec2630cSMatthias Ringwald UNUSED(channel); 2710c48b2a2cSMatthias Ringwald 271109e9d05bSMatthias Ringwald l2cap_channel_t * l2cap_channel; 2712f3963406SMatthias Ringwald UNUSED(l2cap_channel); 271309e9d05bSMatthias Ringwald 2714c48b2a2cSMatthias Ringwald // Get Channel ID 2715c48b2a2cSMatthias Ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 2716c48b2a2cSMatthias Ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 2717c48b2a2cSMatthias Ringwald 2718c48b2a2cSMatthias Ringwald switch (channel_id) { 2719c48b2a2cSMatthias Ringwald 272009e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 2721c48b2a2cSMatthias Ringwald case L2CAP_CID_SIGNALING: { 2722c48b2a2cSMatthias Ringwald uint16_t command_offset = 8; 2723c48b2a2cSMatthias Ringwald while (command_offset < size) { 2724c48b2a2cSMatthias Ringwald // handle signaling commands 2725c48b2a2cSMatthias Ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 2726c48b2a2cSMatthias Ringwald 2727c48b2a2cSMatthias Ringwald // increment command_offset 2728c48b2a2cSMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 2729c48b2a2cSMatthias Ringwald } 2730c48b2a2cSMatthias Ringwald break; 2731c48b2a2cSMatthias Ringwald } 273209e9d05bSMatthias Ringwald #endif 2733c48b2a2cSMatthias Ringwald 2734e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 2735e7d0c9aaSMatthias Ringwald case L2CAP_CID_SIGNALING_LE: { 2736e7d0c9aaSMatthias Ringwald uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 2737e7d0c9aaSMatthias Ringwald int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id); 2738c48b2a2cSMatthias Ringwald if (!valid){ 2739e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN); 2740ccf076adS[email protected] } 2741ccf076adS[email protected] break; 2742e7d0c9aaSMatthias Ringwald } 2743e7d0c9aaSMatthias Ringwald #endif 2744c48b2a2cSMatthias Ringwald 2745c48b2a2cSMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 2746c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 2747c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 2748ccf076adS[email protected] } 2749c48b2a2cSMatthias Ringwald break; 2750c48b2a2cSMatthias Ringwald 2751c48b2a2cSMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 2752c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 2753c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 2754c48b2a2cSMatthias Ringwald } 2755c48b2a2cSMatthias Ringwald break; 2756c48b2a2cSMatthias Ringwald 2757c48b2a2cSMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 2758c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 2759c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 2760c48b2a2cSMatthias Ringwald } 2761c48b2a2cSMatthias Ringwald break; 27621bbc0b23S[email protected] 276309e9d05bSMatthias Ringwald default: 276409e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 276500d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 276664e11ca9SMatthias Ringwald l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 2767d1fd2a88SMatthias Ringwald if (l2cap_channel) { 276827e0774aSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 276927e0774aSMatthias Ringwald if (l2cap_channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 277027e0774aSMatthias Ringwald 277127e0774aSMatthias Ringwald // verify FCS 277227e0774aSMatthias Ringwald uint16_t fcs_calculated = crc16_calc(&packet[4], size - (4+2)); 277327e0774aSMatthias Ringwald uint16_t fcs_packet = little_endian_read_16(packet, size-2); 277427e0774aSMatthias Ringwald log_info("Packet FCS 0x%04x, calculated FCS 0x%04x", fcs_packet, fcs_calculated); 277527e0774aSMatthias Ringwald if (fcs_calculated != fcs_packet){ 277627e0774aSMatthias Ringwald log_error("FCS mismatch! Packet 0x%04x, calculated 0x%04x", fcs_packet, fcs_calculated); 277727e0774aSMatthias Ringwald // TODO: trigger retransmission or something like that 277827e0774aSMatthias Ringwald break; 277927e0774aSMatthias Ringwald } 278027e0774aSMatthias Ringwald 278127e0774aSMatthias Ringwald // switch on packet type 278227e0774aSMatthias Ringwald uint16_t control = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER); 278338f62777SMatthias Ringwald uint8_t req_seq = (control >> 8) & 0x3f; 27842bea1b8dSMatthias Ringwald int final = (control >> 7) & 0x01; 278527e0774aSMatthias Ringwald if (control & 1){ 278627e0774aSMatthias Ringwald // S-Frame 278778cd8a22SMatthias Ringwald int poll = (control >> 4) & 0x01; 2788bdbe2e49SMatthias Ringwald l2cap_supervisory_function_t s = (l2cap_supervisory_function_t) ((control >> 2) & 0x03); 27899ffcbce4SMatthias Ringwald log_info("Control: 0x%04x => Supervisory function %u, ReqSeq %02u", control, (int) s, req_seq); 27907b7901d8SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state; 2791bdbe2e49SMatthias Ringwald switch (s){ 2792bdbe2e49SMatthias Ringwald case L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY: 27939ffcbce4SMatthias Ringwald log_info("L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY"); 27942a424812SMatthias Ringwald l2cap_ertm_handle_req_seq(l2cap_channel, req_seq); 27953233d8abSMatthias Ringwald if (poll && final){ 27963233d8abSMatthias Ringwald // S-frames shall not be transmitted with both the F-bit and the P-bit set to 1 at the same time. 27973233d8abSMatthias Ringwald log_error("P=F=1 in S-Frame"); 27983233d8abSMatthias Ringwald break; 27993233d8abSMatthias Ringwald } 280078cd8a22SMatthias Ringwald if (poll){ 2801f85ade6bSMatthias Ringwald // check if we did request selective retransmission before <==> we have stored SDU segments 2802f85ade6bSMatthias Ringwald int i; 2803f85ade6bSMatthias Ringwald int num_stored_out_of_order_packets = 0; 2804f85ade6bSMatthias Ringwald for (i=0;i<l2cap_channel->num_rx_buffers;i++){ 2805f85ade6bSMatthias Ringwald int index = l2cap_channel->rx_store_index + i; 2806f85ade6bSMatthias Ringwald if (index >= l2cap_channel->num_rx_buffers){ 2807f85ade6bSMatthias Ringwald index -= l2cap_channel->num_rx_buffers; 2808f85ade6bSMatthias Ringwald } 2809f85ade6bSMatthias Ringwald l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index]; 2810f85ade6bSMatthias Ringwald if (!rx_state->valid) continue; 2811f85ade6bSMatthias Ringwald num_stored_out_of_order_packets++; 2812f85ade6bSMatthias Ringwald } 2813f85ade6bSMatthias Ringwald if (num_stored_out_of_order_packets){ 2814f85ade6bSMatthias Ringwald l2cap_channel->send_supervisor_frame_selective_reject = 1; 2815f85ade6bSMatthias Ringwald } else { 2816d2afdd38SMatthias Ringwald l2cap_channel->send_supervisor_frame_receiver_ready = 1; 281778cd8a22SMatthias Ringwald } 2818f85ade6bSMatthias Ringwald l2cap_channel->set_final_bit_after_packet_with_poll_bit_set = 1; 2819f85ade6bSMatthias Ringwald } 28203233d8abSMatthias Ringwald if (final){ 28213233d8abSMatthias Ringwald // final bit set <- response to RR with poll bit set. All not acknowledged packets need to be retransmitted 28223233d8abSMatthias Ringwald l2cap_channel->tx_send_index = l2cap_channel->tx_read_index; 28233233d8abSMatthias Ringwald } 2824bdbe2e49SMatthias Ringwald break; 28259ffcbce4SMatthias Ringwald case L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT: 28269ffcbce4SMatthias Ringwald log_info("L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT"); 28279ffcbce4SMatthias Ringwald l2cap_ertm_handle_req_seq(l2cap_channel, req_seq); 28289ffcbce4SMatthias Ringwald // rsetart transmittion from last unacknowledted packet (earlier packets already freed in l2cap_ertm_handle_req_seq) 28299ffcbce4SMatthias Ringwald l2cap_channel->tx_send_index = l2cap_channel->tx_read_index; 28309ffcbce4SMatthias Ringwald break; 28319ffcbce4SMatthias Ringwald case L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY: 28329ffcbce4SMatthias Ringwald log_error("L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY"); 28339ffcbce4SMatthias Ringwald break; 28349ffcbce4SMatthias Ringwald case L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT: 28357b7901d8SMatthias Ringwald log_info("L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT"); 28367b7901d8SMatthias Ringwald if (poll){ 28377b7901d8SMatthias Ringwald l2cap_ertm_handle_req_seq(l2cap_channel, req_seq); 28387b7901d8SMatthias Ringwald } 28397b7901d8SMatthias Ringwald // find requested i-frame 28407b7901d8SMatthias Ringwald tx_state = l2cap_ertm_get_tx_state(l2cap_channel, req_seq); 28417b7901d8SMatthias Ringwald if (tx_state){ 28427b7901d8SMatthias Ringwald log_info("Retransmission for tx_seq %u requested", req_seq); 2843d2afdd38SMatthias Ringwald l2cap_channel->set_final_bit_after_packet_with_poll_bit_set = poll; 28447b7901d8SMatthias Ringwald tx_state->retransmission_requested = 1; 28457b7901d8SMatthias Ringwald l2cap_channel->srej_active = 1; 28467b7901d8SMatthias Ringwald } 28479ffcbce4SMatthias Ringwald break; 2848bdbe2e49SMatthias Ringwald default: 2849bdbe2e49SMatthias Ringwald break; 2850bdbe2e49SMatthias Ringwald } 285127e0774aSMatthias Ringwald break; 285227e0774aSMatthias Ringwald } else { 285327e0774aSMatthias Ringwald // I-Frame 285427e0774aSMatthias Ringwald // get control 285527e0774aSMatthias Ringwald l2cap_segmentation_and_reassembly_t sar = (l2cap_segmentation_and_reassembly_t) (control >> 14); 285638f62777SMatthias Ringwald uint8_t tx_seq = (control >> 1) & 0x3f; 285738f62777SMatthias Ringwald log_info("Control: 0x%04x => SAR %u, ReqSeq %02u, R?, TxSeq %02u", control, (int) sar, req_seq, tx_seq); 2858e8e9809fSMatthias Ringwald log_info("SAR: pos %u", l2cap_channel->reassembly_pos); 285938f62777SMatthias Ringwald log_info("State: expected_tx_seq %02u, req_seq %02u", l2cap_channel->expected_tx_seq, l2cap_channel->req_seq); 28602a424812SMatthias Ringwald l2cap_ertm_handle_req_seq(l2cap_channel, req_seq); 286111b576c5SMatthias Ringwald if (final){ 286211b576c5SMatthias Ringwald // final bit set <- response to RR with poll bit set. All not acknowledged packets need to be retransmitted 286311b576c5SMatthias Ringwald l2cap_channel->tx_send_index = l2cap_channel->tx_read_index; 286411b576c5SMatthias Ringwald } 286538f62777SMatthias Ringwald // check ordering 286638f62777SMatthias Ringwald if (l2cap_channel->expected_tx_seq == tx_seq){ 286738f62777SMatthias Ringwald log_info("Received expected frame with TxSeq == ExpectedTxSeq == %02u", tx_seq); 286838f62777SMatthias Ringwald l2cap_channel->expected_tx_seq = l2cap_next_ertm_seq_nr(l2cap_channel->expected_tx_seq); 2869f85ade6bSMatthias Ringwald l2cap_channel->req_seq = l2cap_channel->expected_tx_seq; 2870d48432d4SMatthias Ringwald 2871e32be409SMatthias Ringwald // process SDU 2872e32be409SMatthias Ringwald l2cap_ertm_handle_in_sequence_sdu(l2cap_channel, sar, &packet[COMPLETE_L2CAP_HEADER+2], size-(COMPLETE_L2CAP_HEADER+2)); 2873d48432d4SMatthias Ringwald 287470734707SMatthias Ringwald // process stored segments 287570734707SMatthias Ringwald while (1){ 287670734707SMatthias Ringwald int index = l2cap_channel->rx_store_index; 287770734707SMatthias Ringwald l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index]; 287870734707SMatthias Ringwald if (!rx_state->valid) break; 2879f85ade6bSMatthias Ringwald 2880f85ade6bSMatthias Ringwald log_info("Processing stored frame with TxSeq == ExpectedTxSeq == %02u", l2cap_channel->expected_tx_seq); 2881f85ade6bSMatthias Ringwald l2cap_channel->expected_tx_seq = l2cap_next_ertm_seq_nr(l2cap_channel->expected_tx_seq); 2882f85ade6bSMatthias Ringwald l2cap_channel->req_seq = l2cap_channel->expected_tx_seq; 2883f85ade6bSMatthias Ringwald 288470734707SMatthias Ringwald rx_state->valid = 0; 288570734707SMatthias Ringwald l2cap_ertm_handle_in_sequence_sdu(l2cap_channel, rx_state->sar, &l2cap_channel->rx_packets_data[index], rx_state->len); 2886f85ade6bSMatthias Ringwald 2887f85ade6bSMatthias Ringwald // update rx store index 288870734707SMatthias Ringwald index++; 288970734707SMatthias Ringwald if (index >= l2cap_channel->num_rx_buffers){ 289070734707SMatthias Ringwald index = 0; 289170734707SMatthias Ringwald } 289270734707SMatthias Ringwald l2cap_channel->rx_store_index = index; 289370734707SMatthias Ringwald } 289470734707SMatthias Ringwald 2895f85ade6bSMatthias Ringwald // 2896f85ade6bSMatthias Ringwald l2cap_channel->send_supervisor_frame_receiver_ready = 1; 2897f85ade6bSMatthias Ringwald 2898c7309e8dSMatthias Ringwald } else { 2899df2191a7SMatthias Ringwald int delta = (tx_seq - l2cap_channel->expected_tx_seq) & 0x3f; 2900df2191a7SMatthias Ringwald if (delta < 2){ 290170734707SMatthias Ringwald // store segment 290270734707SMatthias Ringwald l2cap_ertm_handle_out_of_sequence_sdu(l2cap_channel, sar, delta, &packet[COMPLETE_L2CAP_HEADER+2], size-(COMPLETE_L2CAP_HEADER+2)); 290370734707SMatthias Ringwald 2904df2191a7SMatthias Ringwald log_info("Received unexpected frame TxSeq %u but expected %u -> send S-SREJ", tx_seq, l2cap_channel->expected_tx_seq); 2905df2191a7SMatthias Ringwald l2cap_channel->send_supervisor_frame_selective_reject = 1; 2906df2191a7SMatthias Ringwald } else { 2907df2191a7SMatthias Ringwald log_info("Received unexpected frame TxSeq %u but expected %u -> send S-REJ", tx_seq, l2cap_channel->expected_tx_seq); 2908c7309e8dSMatthias Ringwald l2cap_channel->send_supervisor_frame_reject = 1; 290927e0774aSMatthias Ringwald } 291038f62777SMatthias Ringwald } 2911df2191a7SMatthias Ringwald } 291227e0774aSMatthias Ringwald break; 291327e0774aSMatthias Ringwald } 291427e0774aSMatthias Ringwald #endif 29153d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 291600d93d79Smatthias.ringwald } 291709e9d05bSMatthias Ringwald #endif 2918a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 291964e11ca9SMatthias Ringwald l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id); 292064e11ca9SMatthias Ringwald if (l2cap_channel) { 292185aeef60SMatthias Ringwald // credit counting 292285aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming == 0){ 292385aeef60SMatthias Ringwald log_error("LE Data Channel packet received but no incoming credits"); 292485aeef60SMatthias Ringwald l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 292585aeef60SMatthias Ringwald break; 292685aeef60SMatthias Ringwald } 292785aeef60SMatthias Ringwald l2cap_channel->credits_incoming--; 292885aeef60SMatthias Ringwald 292985aeef60SMatthias Ringwald // automatic credits 293085aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){ 293185aeef60SMatthias Ringwald l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT; 293285aeef60SMatthias Ringwald } 293385aeef60SMatthias Ringwald 2934cd529728SMatthias Ringwald // first fragment 2935cd529728SMatthias Ringwald uint16_t pos = 0; 2936cd529728SMatthias Ringwald if (!l2cap_channel->receive_sdu_len){ 2937cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER); 2938cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos = 0; 2939cd529728SMatthias Ringwald pos += 2; 2940cd529728SMatthias Ringwald size -= 2; 2941cd529728SMatthias Ringwald } 2942cd529728SMatthias Ringwald memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER); 2943cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER; 2944cd529728SMatthias Ringwald // done? 294563f0ac45SMatthias Ringwald log_info("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len); 2946cd529728SMatthias Ringwald if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){ 2947cd529728SMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len); 2948cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = 0; 2949cd529728SMatthias Ringwald } 295063f0ac45SMatthias Ringwald } else { 295163f0ac45SMatthias Ringwald log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id); 295264e11ca9SMatthias Ringwald } 295364e11ca9SMatthias Ringwald #endif 29545652b5ffS[email protected] break; 29555652b5ffS[email protected] } 295600d93d79Smatthias.ringwald 29571eb2563eS[email protected] l2cap_run(); 29582718e2e7Smatthias.ringwald } 295900d93d79Smatthias.ringwald 296009e9d05bSMatthias Ringwald // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 296109e9d05bSMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 296209e9d05bSMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 296309e9d05bSMatthias Ringwald if (index < 0) return; 296409e9d05bSMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 296509e9d05bSMatthias Ringwald } 296609e9d05bSMatthias Ringwald 296709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 296815ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 296927a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 2970f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 2971f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 2972f62db1e3Smatthias.ringwald // discard channel 29739dcb2fb2S[email protected] l2cap_stop_rtx(channel); 2974665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 2975d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 2976c8e4258aSmatthias.ringwald } 29771e6aba47Smatthias.ringwald 29788f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 2979665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2980665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 2981665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2982665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 29839d9bbc01Smatthias.ringwald if ( service->psm == psm){ 29849d9bbc01Smatthias.ringwald return service; 29859d9bbc01Smatthias.ringwald }; 29869d9bbc01Smatthias.ringwald } 29879d9bbc01Smatthias.ringwald return NULL; 29889d9bbc01Smatthias.ringwald } 29899d9bbc01Smatthias.ringwald 29907192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 29917192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 29927192e786SMatthias Ringwald } 29937192e786SMatthias Ringwald 2994e0abb8e7S[email protected] 2995be2053a6SMatthias 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){ 2996be2053a6SMatthias Ringwald 2997be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 2998e0abb8e7S[email protected] 29994bb582b6Smatthias.ringwald // check for alread registered psm 30009d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 3001277abc2cSmatthias.ringwald if (service) { 3002be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 3003be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 3004277abc2cSmatthias.ringwald } 30059d9bbc01Smatthias.ringwald 30064bb582b6Smatthias.ringwald // alloc structure 3007bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 3008277abc2cSmatthias.ringwald if (!service) { 3009be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 3010be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 3011277abc2cSmatthias.ringwald } 30129d9bbc01Smatthias.ringwald 30139d9bbc01Smatthias.ringwald // fill in 30149d9bbc01Smatthias.ringwald service->psm = psm; 30159d9bbc01Smatthias.ringwald service->mtu = mtu; 301605ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 3017df3354fcS[email protected] service->required_security_level = security_level; 30189d9bbc01Smatthias.ringwald 30199d9bbc01Smatthias.ringwald // add to services list 3020665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 3021c0e866bfSmatthias.ringwald 3022c0e866bfSmatthias.ringwald // enable page scan 302315a95bd5SMatthias Ringwald gap_connectable_control(1); 30245842b6d9Smatthias.ringwald 3025be2053a6SMatthias Ringwald return 0; 30269d9bbc01Smatthias.ringwald } 30279d9bbc01Smatthias.ringwald 30287e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){ 3029e0abb8e7S[email protected] 3030e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 3031e0abb8e7S[email protected] 30329d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 30337e8856ebSMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 3034665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 3035d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 3036c0e866bfSmatthias.ringwald 3037c0e866bfSmatthias.ringwald // disable page scan when no services registered 30387e8856ebSMatthias Ringwald if (btstack_linked_list_empty(&l2cap_services)) { 303915a95bd5SMatthias Ringwald gap_connectable_control(0); 30409d9bbc01Smatthias.ringwald } 30417e8856ebSMatthias Ringwald return 0; 30427e8856ebSMatthias Ringwald } 304309e9d05bSMatthias Ringwald #endif 30449d9bbc01Smatthias.ringwald 30457192e786SMatthias Ringwald 3046cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 304783fd9c76SMatthias Ringwald 304857be49d6SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){ 304957be49d6SMatthias Ringwald if (!channel->waiting_for_can_send_now) return; 305057be49d6SMatthias Ringwald if (channel->send_sdu_buffer) return; 305157be49d6SMatthias Ringwald channel->waiting_for_can_send_now = 0; 305257be49d6SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid); 305357be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW); 305457be49d6SMatthias Ringwald } 305557be49d6SMatthias Ringwald 305657be49d6SMatthias Ringwald // 1BH2222 305757be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) { 305857be49d6SMatthias Ringwald log_info("L2CAP_EVENT_LE_INCOMING_CONNECTION addr_type %u, addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x, remote_mtu %u", 305957be49d6SMatthias Ringwald channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu); 306057be49d6SMatthias Ringwald uint8_t event[19]; 306157be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION; 306257be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 306357be49d6SMatthias Ringwald event[2] = channel->address_type; 306457be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 306557be49d6SMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 306657be49d6SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 306757be49d6SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 306857be49d6SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 306957be49d6SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_mtu); 307057be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 307157be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 307257be49d6SMatthias Ringwald } 307357be49d6SMatthias Ringwald // 11BH22222 307457be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) { 307557be49d6SMatthias Ringwald log_info("L2CAP_EVENT_LE_CHANNEL_OPENED status 0x%x addr_type %u addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u", 307657be49d6SMatthias Ringwald status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 307757be49d6SMatthias Ringwald channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu); 3078c99bb618SMatthias Ringwald uint8_t event[23]; 307957be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED; 308057be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 308157be49d6SMatthias Ringwald event[2] = status; 308257be49d6SMatthias Ringwald event[3] = channel->address_type; 308357be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[4]); 308457be49d6SMatthias Ringwald little_endian_store_16(event, 10, channel->con_handle); 3085c99bb618SMatthias Ringwald event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0; 3086c99bb618SMatthias Ringwald little_endian_store_16(event, 13, channel->psm); 3087c99bb618SMatthias Ringwald little_endian_store_16(event, 15, channel->local_cid); 3088c99bb618SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_cid); 3089c99bb618SMatthias Ringwald little_endian_store_16(event, 19, channel->local_mtu); 3090c99bb618SMatthias Ringwald little_endian_store_16(event, 21, channel->remote_mtu); 309157be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 309257be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 309357be49d6SMatthias Ringwald } 309457be49d6SMatthias Ringwald 309557be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){ 309657be49d6SMatthias Ringwald btstack_linked_list_iterator_t it; 309757be49d6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 309857be49d6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 309957be49d6SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 310057be49d6SMatthias Ringwald if ( channel->local_cid == local_cid) { 310157be49d6SMatthias Ringwald return channel; 310257be49d6SMatthias Ringwald } 310357be49d6SMatthias Ringwald } 310457be49d6SMatthias Ringwald return NULL; 310557be49d6SMatthias Ringwald } 310657be49d6SMatthias Ringwald 310757be49d6SMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 310857be49d6SMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){ 310957be49d6SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 311057be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 311157be49d6SMatthias Ringwald // discard channel 311257be49d6SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 311357be49d6SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 311457be49d6SMatthias Ringwald } 311557be49d6SMatthias Ringwald 3116e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){ 3117e7d0c9aaSMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, le_psm); 31187192e786SMatthias Ringwald } 3119efedfb4cSMatthias Ringwald 3120da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){ 31217192e786SMatthias Ringwald 3122da144af5SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm); 31237192e786SMatthias Ringwald 31247192e786SMatthias Ringwald // check for alread registered psm 31257192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 31267192e786SMatthias Ringwald if (service) { 3127da144af5SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 31287192e786SMatthias Ringwald } 31297192e786SMatthias Ringwald 31307192e786SMatthias Ringwald // alloc structure 31317192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 31327192e786SMatthias Ringwald if (!service) { 31337192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 3134da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 31357192e786SMatthias Ringwald } 31367192e786SMatthias Ringwald 31377192e786SMatthias Ringwald // fill in 31387192e786SMatthias Ringwald service->psm = psm; 3139da144af5SMatthias Ringwald service->mtu = 0; 31407192e786SMatthias Ringwald service->packet_handler = packet_handler; 31417192e786SMatthias Ringwald service->required_security_level = security_level; 31427192e786SMatthias Ringwald 31437192e786SMatthias Ringwald // add to services list 3144665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 31457192e786SMatthias Ringwald 31467192e786SMatthias Ringwald // done 3147da144af5SMatthias Ringwald return 0; 31487192e786SMatthias Ringwald } 31497192e786SMatthias Ringwald 3150da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) { 31517192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 31527192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 31539367f9b0SMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 3154da144af5SMatthias Ringwald 3155665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 31567192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 3157da144af5SMatthias Ringwald return 0; 31587192e786SMatthias Ringwald } 3159da144af5SMatthias Ringwald 3160da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){ 3161e7d0c9aaSMatthias Ringwald // get channel 3162e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 3163e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 3164e7d0c9aaSMatthias Ringwald 3165e7d0c9aaSMatthias Ringwald // validate state 3166e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 3167e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3168e7d0c9aaSMatthias Ringwald } 3169e7d0c9aaSMatthias Ringwald 3170efedfb4cSMatthias Ringwald // set state accept connection 317123017473SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT; 317223017473SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 317323017473SMatthias Ringwald channel->local_mtu = mtu; 317485aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 317585aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 317685aeef60SMatthias Ringwald 317785aeef60SMatthias Ringwald // test 317863f0ac45SMatthias Ringwald // channel->new_credits_incoming = 1; 3179e7d0c9aaSMatthias Ringwald 3180e7d0c9aaSMatthias Ringwald // go 3181e7d0c9aaSMatthias Ringwald l2cap_run(); 3182da144af5SMatthias Ringwald return 0; 3183da144af5SMatthias Ringwald } 3184da144af5SMatthias Ringwald 3185da144af5SMatthias Ringwald /** 3186da144af5SMatthias Ringwald * @brief Deny incoming LE Data Channel connection due to resource constraints 3187da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 3188da144af5SMatthias Ringwald */ 3189da144af5SMatthias Ringwald 3190da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){ 3191e7d0c9aaSMatthias Ringwald // get channel 3192e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 3193e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 3194e7d0c9aaSMatthias Ringwald 3195e7d0c9aaSMatthias Ringwald // validate state 3196e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 3197e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 3198e7d0c9aaSMatthias Ringwald } 3199e7d0c9aaSMatthias Ringwald 3200efedfb4cSMatthias Ringwald // set state decline connection 3201e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE; 3202e7d0c9aaSMatthias Ringwald channel->reason = 0x04; // no resources available 3203e7d0c9aaSMatthias Ringwald l2cap_run(); 3204da144af5SMatthias Ringwald return 0; 3205da144af5SMatthias Ringwald } 3206da144af5SMatthias Ringwald 32077dafa750SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, 3208da144af5SMatthias Ringwald uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 3209efedfb4cSMatthias Ringwald uint16_t * out_local_cid) { 3210efedfb4cSMatthias Ringwald 32117dafa750SMatthias Ringwald log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu); 3212da144af5SMatthias Ringwald 32137dafa750SMatthias Ringwald 32147dafa750SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 32157dafa750SMatthias Ringwald if (!connection) { 32167dafa750SMatthias Ringwald log_error("no hci_connection for handle 0x%04x", con_handle); 32177dafa750SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 32187dafa750SMatthias Ringwald } 32197dafa750SMatthias Ringwald 32207dafa750SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, connection->address, connection->address_type, psm, mtu, security_level); 3221da144af5SMatthias Ringwald if (!channel) { 3222da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 3223da144af5SMatthias Ringwald } 3224e7d0c9aaSMatthias Ringwald log_info("l2cap_le_create_channel %p", channel); 3225da144af5SMatthias Ringwald 3226da144af5SMatthias Ringwald // store local_cid 3227da144af5SMatthias Ringwald if (out_local_cid){ 3228da144af5SMatthias Ringwald *out_local_cid = channel->local_cid; 3229da144af5SMatthias Ringwald } 3230da144af5SMatthias Ringwald 32317dafa750SMatthias Ringwald // provide buffer 32327dafa750SMatthias Ringwald channel->con_handle = con_handle; 3233cd529728SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 32347dafa750SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST; 323585aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 323685aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 323785aeef60SMatthias Ringwald 3238efedfb4cSMatthias Ringwald // add to connections list 3239efedfb4cSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 3240efedfb4cSMatthias Ringwald 32417dafa750SMatthias Ringwald // go 324263f0ac45SMatthias Ringwald l2cap_run(); 3243da144af5SMatthias Ringwald return 0; 3244da144af5SMatthias Ringwald } 3245da144af5SMatthias Ringwald 3246da144af5SMatthias Ringwald /** 3247da144af5SMatthias Ringwald * @brief Provide credtis for LE Data Channel 3248da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 3249da144af5SMatthias Ringwald * @param credits Number additional credits for peer 3250da144af5SMatthias Ringwald */ 325164e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){ 325263f0ac45SMatthias Ringwald 325363f0ac45SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 325463f0ac45SMatthias Ringwald if (!channel) { 325563f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 325663f0ac45SMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 325763f0ac45SMatthias Ringwald } 325863f0ac45SMatthias Ringwald 3259efedfb4cSMatthias Ringwald // check state 326063f0ac45SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN){ 326163f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid); 326263f0ac45SMatthias Ringwald } 326363f0ac45SMatthias Ringwald 326463f0ac45SMatthias Ringwald // assert incoming credits + credits <= 0xffff 326563f0ac45SMatthias Ringwald uint32_t total_credits = channel->credits_incoming; 326663f0ac45SMatthias Ringwald total_credits += channel->new_credits_incoming; 326763f0ac45SMatthias Ringwald total_credits += credits; 326863f0ac45SMatthias Ringwald if (total_credits > 0xffff){ 326963f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming, 327063f0ac45SMatthias Ringwald channel->new_credits_incoming, credits); 327163f0ac45SMatthias Ringwald } 327263f0ac45SMatthias Ringwald 3273efedfb4cSMatthias Ringwald // set credits_granted 327463f0ac45SMatthias Ringwald channel->new_credits_incoming += credits; 327563f0ac45SMatthias Ringwald 327663f0ac45SMatthias Ringwald // go 327763f0ac45SMatthias Ringwald l2cap_run(); 3278da144af5SMatthias Ringwald return 0; 3279da144af5SMatthias Ringwald } 3280da144af5SMatthias Ringwald 3281da144af5SMatthias Ringwald /** 3282da144af5SMatthias Ringwald * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 3283da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 3284da144af5SMatthias Ringwald */ 328564e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){ 328644276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 328744276248SMatthias Ringwald if (!channel) { 328844276248SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 3289da144af5SMatthias Ringwald return 0; 3290da144af5SMatthias Ringwald } 3291da144af5SMatthias Ringwald 329244276248SMatthias Ringwald // check state 329344276248SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN) return 0; 329444276248SMatthias Ringwald 329544276248SMatthias Ringwald // check queue 329644276248SMatthias Ringwald if (channel->send_sdu_buffer) return 0; 329744276248SMatthias Ringwald 329844276248SMatthias Ringwald // fine, go ahead 329944276248SMatthias Ringwald return 1; 330044276248SMatthias Ringwald } 330144276248SMatthias Ringwald 3302da144af5SMatthias Ringwald /** 3303da144af5SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 3304da144af5SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 3305da144af5SMatthias Ringwald * so packet handler should be ready to handle it 3306da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 3307da144af5SMatthias Ringwald */ 330864e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){ 330944276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 331044276248SMatthias Ringwald if (!channel) { 331144276248SMatthias Ringwald log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid); 331244276248SMatthias Ringwald return 0; 331344276248SMatthias Ringwald } 331444276248SMatthias Ringwald channel->waiting_for_can_send_now = 1; 331544276248SMatthias Ringwald l2cap_le_notify_channel_can_send(channel); 3316da144af5SMatthias Ringwald return 0; 3317da144af5SMatthias Ringwald } 3318da144af5SMatthias Ringwald 3319da144af5SMatthias Ringwald /** 3320da144af5SMatthias Ringwald * @brief Send data via LE Data Channel 3321da144af5SMatthias Ringwald * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 3322da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 3323da144af5SMatthias Ringwald * @param data data to send 3324da144af5SMatthias Ringwald * @param size data size 3325da144af5SMatthias Ringwald */ 332664e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){ 332764e11ca9SMatthias Ringwald 332864e11ca9SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 332964e11ca9SMatthias Ringwald if (!channel) { 333064e11ca9SMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 3331828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 333264e11ca9SMatthias Ringwald } 333364e11ca9SMatthias Ringwald 333464e11ca9SMatthias Ringwald if (len > channel->remote_mtu){ 333564e11ca9SMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 333664e11ca9SMatthias Ringwald return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 333764e11ca9SMatthias Ringwald } 333864e11ca9SMatthias Ringwald 33397f107edaSMatthias Ringwald if (channel->send_sdu_buffer){ 334064e11ca9SMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 334164e11ca9SMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 334264e11ca9SMatthias Ringwald } 334364e11ca9SMatthias Ringwald 33447f107edaSMatthias Ringwald channel->send_sdu_buffer = data; 33457f107edaSMatthias Ringwald channel->send_sdu_len = len; 33467f107edaSMatthias Ringwald channel->send_sdu_pos = 0; 334764e11ca9SMatthias Ringwald 33487f107edaSMatthias Ringwald l2cap_run(); 33497f107edaSMatthias Ringwald return 0; 3350da144af5SMatthias Ringwald } 3351da144af5SMatthias Ringwald 3352da144af5SMatthias Ringwald /** 3353da144af5SMatthias Ringwald * @brief Disconnect from LE Data Channel 3354da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 3355da144af5SMatthias Ringwald */ 3356828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid) 3357da144af5SMatthias Ringwald { 3358828a7f7aSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 3359828a7f7aSMatthias Ringwald if (!channel) { 3360828a7f7aSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 3361828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 3362828a7f7aSMatthias Ringwald } 3363828a7f7aSMatthias Ringwald 3364828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 3365828a7f7aSMatthias Ringwald l2cap_run(); 3366da144af5SMatthias Ringwald return 0; 3367da144af5SMatthias Ringwald } 3368da144af5SMatthias Ringwald 33697f02f414SMatthias Ringwald #endif 3370