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 113*212b6be2SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 114*212b6be2SMatthias Ringwald static int l2cap_ertm_num_unacknowledged_tx_packets(l2cap_channel_t * channel); 115*212b6be2SMatthias 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 21385ddcd84SMatthias Ringwald uint8_t test[] = {0x0E, 0x00, 0x40, 0x00, 0x02, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }; 21485ddcd84SMatthias Ringwald printf("crc16: %04x\n", crc16_calc(test, sizeof(test))); 21585ddcd84SMatthias Ringwald 21609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 217f5454fc6Smatthias.ringwald l2cap_channels = NULL; 218f5454fc6Smatthias.ringwald l2cap_services = NULL; 21909e9d05bSMatthias Ringwald require_security_level2_for_outgoing_sdp = 0; 22009e9d05bSMatthias Ringwald #endif 221a3dc965aSMatthias Ringwald 222a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 2237192e786SMatthias Ringwald l2cap_le_services = NULL; 2247192e786SMatthias Ringwald l2cap_le_channels = NULL; 225a3dc965aSMatthias Ringwald #endif 226f5454fc6Smatthias.ringwald 227d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE 22833c40538SMatthias Ringwald l2cap_event_packet_handler = NULL; 229d6ed9f9cSMatthias Ringwald #endif 2305628cf69SMatthias Ringwald memset(fixed_channels, 0, sizeof(fixed_channels)); 231f5454fc6Smatthias.ringwald 232fcadd0caSmatthias.ringwald // 2332718e2e7Smatthias.ringwald // register callback with HCI 234fcadd0caSmatthias.ringwald // 235d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &l2cap_hci_event_handler; 236fb37a842SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 237fb37a842SMatthias Ringwald 238d9a7306aSMatthias Ringwald hci_register_acl_packet_handler(&l2cap_acl_handler); 239fb37a842SMatthias Ringwald 240be005ed6SMatthias Ringwald #ifdef ENABLE_CLASSIC 24115a95bd5SMatthias Ringwald gap_connectable_control(0); // no services yet 242be005ed6SMatthias Ringwald #endif 243fcadd0caSmatthias.ringwald } 244fcadd0caSmatthias.ringwald 245ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 246d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE 24733c40538SMatthias Ringwald l2cap_event_packet_handler = handler; 248d6ed9f9cSMatthias Ringwald #else 249d6ed9f9cSMatthias Ringwald UNUSED(handler); 250d6ed9f9cSMatthias Ringwald #endif 2511e6aba47Smatthias.ringwald } 2521e6aba47Smatthias.ringwald 25309e9d05bSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){ 2549ec2630cSMatthias Ringwald UNUSED(con_handle); 2559ec2630cSMatthias Ringwald 25609e9d05bSMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 25709e9d05bSMatthias Ringwald if (index < 0) return; 25809e9d05bSMatthias Ringwald fixed_channels[index].waiting_for_can_send_now = 1; 25909e9d05bSMatthias Ringwald l2cap_notify_channel_can_send(); 26009e9d05bSMatthias Ringwald } 26109e9d05bSMatthias Ringwald 26209e9d05bSMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){ 2639ec2630cSMatthias Ringwald UNUSED(channel_id); 2649ec2630cSMatthias Ringwald 26509e9d05bSMatthias Ringwald return hci_can_send_acl_packet_now(con_handle); 26609e9d05bSMatthias Ringwald } 26709e9d05bSMatthias Ringwald 26809e9d05bSMatthias Ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 26909e9d05bSMatthias Ringwald return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes 27009e9d05bSMatthias Ringwald } 27109e9d05bSMatthias Ringwald 27209e9d05bSMatthias Ringwald int l2cap_reserve_packet_buffer(void){ 27309e9d05bSMatthias Ringwald return hci_reserve_packet_buffer(); 27409e9d05bSMatthias Ringwald } 27509e9d05bSMatthias Ringwald 27609e9d05bSMatthias Ringwald void l2cap_release_packet_buffer(void){ 27709e9d05bSMatthias Ringwald hci_release_packet_buffer(); 27809e9d05bSMatthias Ringwald } 27909e9d05bSMatthias Ringwald 280f511cefaSMatthias 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){ 28109e9d05bSMatthias Ringwald // 0 - Connection handle : PB=pb : BC=00 282f511cefaSMatthias Ringwald little_endian_store_16(acl_buffer, 0, con_handle | (packet_boundary << 12) | (0 << 14)); 28309e9d05bSMatthias Ringwald // 2 - ACL length 28409e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 2, len + 4); 28509e9d05bSMatthias Ringwald // 4 - L2CAP packet length 28609e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 4, len + 0); 28709e9d05bSMatthias Ringwald // 6 - L2CAP channel DEST 28809e9d05bSMatthias Ringwald little_endian_store_16(acl_buffer, 6, remote_cid); 28909e9d05bSMatthias Ringwald } 29009e9d05bSMatthias Ringwald 291f511cefaSMatthias Ringwald // assumption - only on LE connections 29209e9d05bSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){ 29309e9d05bSMatthias Ringwald 29409e9d05bSMatthias Ringwald if (!hci_is_packet_buffer_reserved()){ 29509e9d05bSMatthias Ringwald log_error("l2cap_send_prepared_connectionless called without reserving packet first"); 29609e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 29709e9d05bSMatthias Ringwald } 29809e9d05bSMatthias Ringwald 29909e9d05bSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(con_handle)){ 30009e9d05bSMatthias Ringwald log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid); 30109e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 30209e9d05bSMatthias Ringwald } 30309e9d05bSMatthias Ringwald 30409e9d05bSMatthias Ringwald log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid); 30509e9d05bSMatthias Ringwald 30609e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 307f511cefaSMatthias Ringwald l2cap_setup_header(acl_buffer, con_handle, 0, cid, len); 30809e9d05bSMatthias Ringwald // send 30909e9d05bSMatthias Ringwald return hci_send_acl_packet_buffer(len+8); 31009e9d05bSMatthias Ringwald } 31109e9d05bSMatthias Ringwald 312f511cefaSMatthias Ringwald // assumption - only on LE connections 31309e9d05bSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){ 31409e9d05bSMatthias Ringwald 31509e9d05bSMatthias Ringwald if (!hci_can_send_acl_packet_now(con_handle)){ 31609e9d05bSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", cid); 31709e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 31809e9d05bSMatthias Ringwald } 31909e9d05bSMatthias Ringwald 32009e9d05bSMatthias Ringwald hci_reserve_packet_buffer(); 32109e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 32209e9d05bSMatthias Ringwald 32309e9d05bSMatthias Ringwald memcpy(&acl_buffer[8], data, len); 32409e9d05bSMatthias Ringwald 32509e9d05bSMatthias Ringwald return l2cap_send_prepared_connectionless(con_handle, cid, len); 32609e9d05bSMatthias Ringwald } 32709e9d05bSMatthias Ringwald 32809e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) { 329d9d23054SMatthias Ringwald log_debug("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel); 33009e9d05bSMatthias Ringwald uint8_t event[4]; 33109e9d05bSMatthias Ringwald event[0] = L2CAP_EVENT_CAN_SEND_NOW; 33209e9d05bSMatthias Ringwald event[1] = sizeof(event) - 2; 33309e9d05bSMatthias Ringwald little_endian_store_16(event, 2, channel); 33409e9d05bSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 33509e9d05bSMatthias Ringwald packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event)); 33609e9d05bSMatthias Ringwald } 33709e9d05bSMatthias Ringwald 33809e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS 33917a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){ 34058de5610Smatthias.ringwald (* (channel->packet_handler))(type, channel->local_cid, data, size); 34158de5610Smatthias.ringwald } 34258de5610Smatthias.ringwald 34344276248SMatthias Ringwald static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){ 34444276248SMatthias Ringwald uint8_t event[4]; 34544276248SMatthias Ringwald event[0] = event_code; 34644276248SMatthias Ringwald event[1] = sizeof(event) - 2; 34744276248SMatthias Ringwald little_endian_store_16(event, 2, channel->local_cid); 34844276248SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 34944276248SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 35044276248SMatthias Ringwald } 35109e9d05bSMatthias Ringwald #endif 35244276248SMatthias Ringwald 35309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 35458de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) { 355c9dc710bS[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", 356fc64f94aSMatthias Ringwald status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 357c9dc710bS[email protected] channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout); 358bab5f4f0SMatthias Ringwald uint8_t event[24]; 35958de5610Smatthias.ringwald event[0] = L2CAP_EVENT_CHANNEL_OPENED; 36058de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 36158de5610Smatthias.ringwald event[2] = status; 362724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 363fc64f94aSMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 364f8fbdce0SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 365f8fbdce0SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 366f8fbdce0SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 367f8fbdce0SMatthias Ringwald little_endian_store_16(event, 17, channel->local_mtu); 368f8fbdce0SMatthias Ringwald little_endian_store_16(event, 19, channel->remote_mtu); 369f8fbdce0SMatthias Ringwald little_endian_store_16(event, 21, channel->flush_timeout); 370bab5f4f0SMatthias Ringwald event[23] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0; 37158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 37217a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 37358de5610Smatthias.ringwald } 37458de5610Smatthias.ringwald 37544276248SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel) { 376e0abb8e7S[email protected] log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid); 37744276248SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 37858de5610Smatthias.ringwald } 37958de5610Smatthias.ringwald 38044276248SMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) { 381e0abb8e7S[email protected] log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x", 382fc64f94aSMatthias Ringwald bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid); 38358de5610Smatthias.ringwald uint8_t event[16]; 38458de5610Smatthias.ringwald event[0] = L2CAP_EVENT_INCOMING_CONNECTION; 38558de5610Smatthias.ringwald event[1] = sizeof(event) - 2; 386724d70a2SMatthias Ringwald reverse_bd_addr(channel->address, &event[2]); 387fc64f94aSMatthias Ringwald little_endian_store_16(event, 8, channel->con_handle); 388f8fbdce0SMatthias Ringwald little_endian_store_16(event, 10, channel->psm); 389f8fbdce0SMatthias Ringwald little_endian_store_16(event, 12, channel->local_cid); 390f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, channel->remote_cid); 39158de5610Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 39217a9b554SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 3930af41d30Smatthias.ringwald } 394808a48abSmatthias.ringwald 3957f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){ 396665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 397665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 398665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 399665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 400b35f641cSmatthias.ringwald if ( channel->local_cid == local_cid) { 401f62db1e3Smatthias.ringwald return channel; 402f62db1e3Smatthias.ringwald } 403f62db1e3Smatthias.ringwald } 404f62db1e3Smatthias.ringwald return NULL; 405f62db1e3Smatthias.ringwald } 406f62db1e3Smatthias.ringwald 4070b9d7e78SMatthias Ringwald /// 4080b9d7e78SMatthias Ringwald 40930725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){ 41030725612SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 41130725612SMatthias Ringwald if (!channel) return; 41230725612SMatthias Ringwald channel->waiting_for_can_send_now = 1; 41330725612SMatthias Ringwald l2cap_notify_channel_can_send(); 41430725612SMatthias Ringwald } 41530725612SMatthias Ringwald 4166b1fde37Smatthias.ringwald int l2cap_can_send_packet_now(uint16_t local_cid){ 4176b1fde37Smatthias.ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 4186b1fde37Smatthias.ringwald if (!channel) return 0; 4190b9d7e78SMatthias Ringwald return hci_can_send_acl_packet_now(channel->con_handle); 4207856fb31S[email protected] } 4217856fb31S[email protected] 42283e7cdd9SMatthias Ringwald int l2cap_can_send_prepared_packet_now(uint16_t local_cid){ 42383e7cdd9SMatthias Ringwald l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid); 42483e7cdd9SMatthias Ringwald if (!channel) return 0; 4250b9d7e78SMatthias Ringwald return hci_can_send_prepared_acl_packet_now(channel->con_handle); 42683e7cdd9SMatthias Ringwald } 42796cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){ 42896cbd662Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 42996cbd662Smatthias.ringwald if (channel) { 43096cbd662Smatthias.ringwald return channel->remote_mtu; 43196cbd662Smatthias.ringwald } 43296cbd662Smatthias.ringwald return 0; 43396cbd662Smatthias.ringwald } 43496cbd662Smatthias.ringwald 435ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){ 436665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 437665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 438665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 439665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 4405932bd7cS[email protected] if ( &channel->rtx == ts) { 4415932bd7cS[email protected] return channel; 4425932bd7cS[email protected] } 4435932bd7cS[email protected] } 4445932bd7cS[email protected] return NULL; 4455932bd7cS[email protected] } 4465932bd7cS[email protected] 447ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){ 4485932bd7cS[email protected] l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts); 44995d2c8f4SMatthias Ringwald if (!channel) return; 4505932bd7cS[email protected] 4515932bd7cS[email protected] log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid); 4525932bd7cS[email protected] 4535932bd7cS[email protected] // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq 4545932bd7cS[email protected] // and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state." 4555932bd7cS[email protected] // notify client 4565932bd7cS[email protected] l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT); 4575932bd7cS[email protected] 4585932bd7cS[email protected] // discard channel 4599dcb2fb2S[email protected] // no need to stop timer here, it is removed from list during timer callback 460665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 4615932bd7cS[email protected] btstack_memory_l2cap_channel_free(channel); 4625932bd7cS[email protected] } 4635932bd7cS[email protected] 4645932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){ 4655932bd7cS[email protected] log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid); 466528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&channel->rtx); 4675932bd7cS[email protected] } 4685932bd7cS[email protected] 4695932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){ 4705932bd7cS[email protected] l2cap_stop_rtx(channel); 471cb0ff06bS[email protected] log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid); 472528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 473528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS); 474528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 4755932bd7cS[email protected] } 4765932bd7cS[email protected] 4775932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){ 4785932bd7cS[email protected] log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 4795932bd7cS[email protected] l2cap_stop_rtx(channel); 480528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 481528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 482528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&channel->rtx); 4835932bd7cS[email protected] } 4845932bd7cS[email protected] 48571de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){ 486ac301f95S[email protected] require_security_level2_for_outgoing_sdp = 1; 487ac301f95S[email protected] } 488ac301f95S[email protected] 489df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 490235946f1SMatthias Ringwald return (psm == BLUETOOTH_PROTOCOL_SDP) && (!require_security_level2_for_outgoing_sdp); 491df3354fcS[email protected] } 4925932bd7cS[email protected] 4937f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 494a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)){ 4959da54300S[email protected] log_info("l2cap_send_signaling_packet, cannot send"); 496b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 497b1d43497Smatthias.ringwald } 498b1d43497Smatthias.ringwald 4999da54300S[email protected] // log_info("l2cap_send_signaling_packet type %u", cmd); 5002a373862S[email protected] hci_reserve_packet_buffer(); 501facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 50258de5610Smatthias.ringwald va_list argptr; 50358de5610Smatthias.ringwald va_start(argptr, identifier); 50470efece1S[email protected] uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr); 50558de5610Smatthias.ringwald va_end(argptr); 5069da54300S[email protected] // log_info("l2cap_send_signaling_packet con %u!", handle); 507826f7347S[email protected] return hci_send_acl_packet_buffer(len); 50858de5610Smatthias.ringwald } 50958de5610Smatthias.ringwald 510f511cefaSMatthias Ringwald // assumption - only on Classic connections 511b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){ 512b1d43497Smatthias.ringwald 513c8b9416aS[email protected] if (!hci_is_packet_buffer_reserved()){ 514c8b9416aS[email protected] log_error("l2cap_send_prepared called without reserving packet first"); 515c8b9416aS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 516c8b9416aS[email protected] } 517c8b9416aS[email protected] 51858de5610Smatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 519b1d43497Smatthias.ringwald if (!channel) { 5209da54300S[email protected] log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid); 521b1d43497Smatthias.ringwald return -1; // TODO: define error 5226218e6f1Smatthias.ringwald } 5236218e6f1Smatthias.ringwald 524fc64f94aSMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){ 5259da54300S[email protected] log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid); 526a35252c8S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 527a35252c8S[email protected] } 528a35252c8S[email protected] 529fc64f94aSMatthias Ringwald log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle); 530b1d43497Smatthias.ringwald 53185ddcd84SMatthias Ringwald int fcs_size = 0; 53285ddcd84SMatthias Ringwald 53385ddcd84SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 53485ddcd84SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 53585ddcd84SMatthias Ringwald fcs_size = 2; 53685ddcd84SMatthias Ringwald } 53785ddcd84SMatthias Ringwald #endif 53885ddcd84SMatthias Ringwald 539f511cefaSMatthias Ringwald // set non-flushable packet boundary flag if supported on Controller 540facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 541f511cefaSMatthias Ringwald uint8_t packet_boundary_flag = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 54285ddcd84SMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, packet_boundary_flag, channel->remote_cid, len + fcs_size); 54385ddcd84SMatthias Ringwald 54485ddcd84SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 54585ddcd84SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 54685ddcd84SMatthias Ringwald // calculate FCS over l2cap data 54785ddcd84SMatthias Ringwald uint16_t fcs = crc16_calc(acl_buffer + 4, 4 + len); 54885ddcd84SMatthias Ringwald log_info("I-Frame: fcs 0x%04x", fcs); 54985ddcd84SMatthias Ringwald little_endian_store_16(acl_buffer, 8 + len, fcs); 55058de5610Smatthias.ringwald } 55185ddcd84SMatthias Ringwald #endif 55285ddcd84SMatthias Ringwald 55385ddcd84SMatthias Ringwald // send 55485ddcd84SMatthias Ringwald return hci_send_acl_packet_buffer(len+8+fcs_size); 55585ddcd84SMatthias Ringwald } 55685ddcd84SMatthias Ringwald 55785ddcd84SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 55885ddcd84SMatthias 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){ 55985ddcd84SMatthias Ringwald return (((uint16_t) sar) << 14) | (req_seq << 8) | (final << 7) | (tx_seq << 1) | 0; 56085ddcd84SMatthias Ringwald } 56185ddcd84SMatthias 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){ 56238f62777SMatthias Ringwald return (req_seq << 8) | (final << 7) | (poll << 4) | (((int) supervisory_function) << 2) | 1; 56385ddcd84SMatthias Ringwald } 56485ddcd84SMatthias Ringwald static int l2cap_next_ertm_seq_nr(int seq_nr){ 56585ddcd84SMatthias Ringwald return (seq_nr + 1) & 0x3f; 56685ddcd84SMatthias Ringwald } 567f0fb4cd7SMatthias Ringwald static void l2cap_ertm_next_tx_write_index(l2cap_channel_t * channel){ 568f0fb4cd7SMatthias Ringwald channel->tx_write_index++; 569f0fb4cd7SMatthias Ringwald if (channel->tx_write_index < channel->num_tx_buffers) return; 570f0fb4cd7SMatthias Ringwald channel->tx_write_index = 0; 571f0fb4cd7SMatthias Ringwald } 572f0fb4cd7SMatthias Ringwald static int l2cap_ertm_send_information_frame(l2cap_channel_t * channel, int index){ 573f0fb4cd7SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index]; 574f0fb4cd7SMatthias Ringwald hci_reserve_packet_buffer(); 575f0fb4cd7SMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 576f0fb4cd7SMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_information_frame(tx_state->tx_seq, 0, 0, L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU); 577f0fb4cd7SMatthias Ringwald log_info("I-Frame: control 0x%04x", control); 578f0fb4cd7SMatthias Ringwald little_endian_store_16(acl_buffer, 8, control); 579f0fb4cd7SMatthias Ringwald memcpy(&acl_buffer[8+2], &channel->tx_packets_data[index * channel->local_mtu], tx_state->len); 580f0fb4cd7SMatthias Ringwald // send 581f0fb4cd7SMatthias Ringwald return l2cap_send_prepared(channel->local_cid, 2 + tx_state->len); 582f0fb4cd7SMatthias Ringwald } 583f0fb4cd7SMatthias Ringwald static int l2cap_ertm_send(l2cap_channel_t * channel, uint8_t * data, uint16_t len){ 584f0fb4cd7SMatthias Ringwald if (len > channel->remote_mtu){ 585f0fb4cd7SMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", channel->local_cid); 586f0fb4cd7SMatthias Ringwald return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 587f0fb4cd7SMatthias Ringwald } 588f0fb4cd7SMatthias Ringwald // TODO: check tx_transmit 589f0fb4cd7SMatthias Ringwald // store int tx packet buffer 590f0fb4cd7SMatthias Ringwald int index = channel->tx_write_index; 591f0fb4cd7SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index]; 592f0fb4cd7SMatthias Ringwald tx_state->tx_seq = channel->next_tx_seq; 593f0fb4cd7SMatthias Ringwald tx_state->len = len; 594f0fb4cd7SMatthias Ringwald memcpy(&channel->tx_packets_data[index * channel->local_mtu], data, len); 595f0fb4cd7SMatthias Ringwald // update 596f0fb4cd7SMatthias Ringwald channel->next_tx_seq = l2cap_next_ertm_seq_nr(channel->next_tx_seq); 597f0fb4cd7SMatthias Ringwald l2cap_ertm_next_tx_write_index(channel); 598675e6881SMatthias Ringwald // try to send 599675e6881SMatthias Ringwald l2cap_run(); 600f0fb4cd7SMatthias Ringwald return 0; 601f0fb4cd7SMatthias Ringwald } 60285ddcd84SMatthias Ringwald #endif 60358de5610Smatthias.ringwald 604f511cefaSMatthias Ringwald // assumption - only on Classic connections 605ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){ 606a35252c8S[email protected] l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 607a35252c8S[email protected] if (!channel) { 608ce8f182eSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 609a35252c8S[email protected] return -1; // TODO: define error 610a35252c8S[email protected] } 611a35252c8S[email protected] 612f0fb4cd7SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 613f0fb4cd7SMatthias Ringwald // send in ERTM 614f0fb4cd7SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 615f0fb4cd7SMatthias Ringwald return l2cap_ertm_send(channel, data, len); 616f0fb4cd7SMatthias Ringwald } 617f0fb4cd7SMatthias Ringwald #endif 618f0fb4cd7SMatthias Ringwald 619f0efaa57S[email protected] if (len > channel->remote_mtu){ 620ce8f182eSMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 621f0efaa57S[email protected] return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 622f0efaa57S[email protected] } 623f0efaa57S[email protected] 624fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)){ 625ce8f182eSMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 626b1d43497Smatthias.ringwald return BTSTACK_ACL_BUFFERS_FULL; 627b1d43497Smatthias.ringwald } 628b1d43497Smatthias.ringwald 6292a373862S[email protected] hci_reserve_packet_buffer(); 630facf93fdS[email protected] uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 631f0fb4cd7SMatthias Ringwald memcpy(&acl_buffer[8], data, len); 632f0fb4cd7SMatthias Ringwald return l2cap_send_prepared(local_cid, len); 633b1d43497Smatthias.ringwald } 634b1d43497Smatthias.ringwald 635fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){ 636fc64f94aSMatthias Ringwald return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data); 6370e37e417S[email protected] } 6380e37e417S[email protected] 63928ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 64028ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag); 64128ca2b46S[email protected] } 64228ca2b46S[email protected] 64328ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){ 64428ca2b46S[email protected] channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag); 64528ca2b46S[email protected] } 64609e9d05bSMatthias Ringwald #endif 64728ca2b46S[email protected] 64828ca2b46S[email protected] 64909e9d05bSMatthias Ringwald #ifdef ENABLE_BLE 65009e9d05bSMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 65109e9d05bSMatthias Ringwald 65209e9d05bSMatthias Ringwald if (!hci_can_send_acl_packet_now(handle)){ 65309e9d05bSMatthias Ringwald log_info("l2cap_send_le_signaling_packet, cannot send"); 65409e9d05bSMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 65509e9d05bSMatthias Ringwald } 65609e9d05bSMatthias Ringwald 65709e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet type %u", cmd); 65809e9d05bSMatthias Ringwald hci_reserve_packet_buffer(); 65909e9d05bSMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 66009e9d05bSMatthias Ringwald va_list argptr; 66109e9d05bSMatthias Ringwald va_start(argptr, identifier); 66209e9d05bSMatthias Ringwald uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr); 66309e9d05bSMatthias Ringwald va_end(argptr); 66409e9d05bSMatthias Ringwald // log_info("l2cap_send_le_signaling_packet con %u!", handle); 66509e9d05bSMatthias Ringwald return hci_send_acl_packet_buffer(len); 66609e9d05bSMatthias Ringwald } 66709e9d05bSMatthias Ringwald #endif 66809e9d05bSMatthias Ringwald 66909e9d05bSMatthias Ringwald uint16_t l2cap_max_mtu(void){ 67009e9d05bSMatthias Ringwald return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE; 67109e9d05bSMatthias Ringwald } 67209e9d05bSMatthias Ringwald 67309e9d05bSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 67409e9d05bSMatthias Ringwald return l2cap_max_mtu(); 67509e9d05bSMatthias Ringwald } 676b1d43497Smatthias.ringwald 677450ad7ecSMatthias Ringwald static uint16_t l2cap_setup_options_mtu(l2cap_channel_t * channel, uint8_t * config_options){ 678450ad7ecSMatthias Ringwald config_options[0] = 1; // MTU 679450ad7ecSMatthias Ringwald config_options[1] = 2; // len param 680450ad7ecSMatthias Ringwald little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu); 681450ad7ecSMatthias Ringwald return 4; 682450ad7ecSMatthias Ringwald } 683450ad7ecSMatthias Ringwald 6846dca2a0cSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 685450ad7ecSMatthias Ringwald static uint16_t l2cap_setup_options_ertm(l2cap_channel_t * channel, uint8_t * config_options){ 6866dca2a0cSMatthias Ringwald config_options[0] = 0x04; // RETRANSMISSION AND FLOW CONTROL OPTION 6876dca2a0cSMatthias Ringwald config_options[1] = 9; // length 6886dca2a0cSMatthias Ringwald config_options[2] = (uint8_t) channel->mode; 6897b181629SMatthias Ringwald config_options[3] = channel->num_rx_buffers; // == TxWindows size 690bbc0a9e7SMatthias Ringwald config_options[4] = channel->local_max_transmit; 691bbc0a9e7SMatthias Ringwald little_endian_store_16( config_options, 5, channel->local_retransmission_timeout_ms); 692bbc0a9e7SMatthias Ringwald little_endian_store_16( config_options, 7, channel->local_monitor_timeout_ms); 6937b181629SMatthias Ringwald little_endian_store_16( config_options, 9, channel->local_mtu); 694450ad7ecSMatthias Ringwald return 11; 6956dca2a0cSMatthias Ringwald } 69638f62777SMatthias Ringwald static int l2cap_ertm_send_supervisor_frame(l2cap_channel_t * channel, uint16_t control){ 69738f62777SMatthias Ringwald hci_reserve_packet_buffer(); 69838f62777SMatthias Ringwald uint8_t *acl_buffer = hci_get_outgoing_packet_buffer(); 69938f62777SMatthias Ringwald log_info("S-Frame: control 0x%04x", control); 70038f62777SMatthias Ringwald little_endian_store_16(acl_buffer, 8, control); 70138f62777SMatthias Ringwald return l2cap_send_prepared(channel->local_cid, 2); 70238f62777SMatthias Ringwald } 703*212b6be2SMatthias Ringwald static int l2cap_ertm_num_unacknowledged_tx_packets(l2cap_channel_t * channel){ 704*212b6be2SMatthias Ringwald int unacknowledged_packets = channel->tx_send_index - channel->tx_read_index; 705*212b6be2SMatthias Ringwald if (unacknowledged_packets < 0){ 706*212b6be2SMatthias Ringwald unacknowledged_packets += channel->num_tx_buffers; 707*212b6be2SMatthias Ringwald } 708*212b6be2SMatthias Ringwald return unacknowledged_packets; 709*212b6be2SMatthias Ringwald } 710450ad7ecSMatthias Ringwald #endif 711450ad7ecSMatthias Ringwald 712450ad7ecSMatthias Ringwald static uint16_t l2cap_setup_options(l2cap_channel_t * channel, uint8_t * config_options){ 713450ad7ecSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 714450ad7ecSMatthias Ringwald // use ERTM options if supported 715450ad7ecSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(channel->con_handle); 716450ad7ecSMatthias Ringwald if ((connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_DONE) && (connection->l2cap_state.extended_feature_mask & 0x08)){ 717450ad7ecSMatthias Ringwald return l2cap_setup_options_ertm(channel, config_options); 718450ad7ecSMatthias Ringwald 719450ad7ecSMatthias Ringwald } 720450ad7ecSMatthias Ringwald #endif 721450ad7ecSMatthias Ringwald return l2cap_setup_options_mtu(channel, config_options); 7226dca2a0cSMatthias Ringwald } 7236dca2a0cSMatthias Ringwald 7241b9cb13dSMatthias Ringwald static uint32_t l2cap_extended_features_mask(void){ 7251b9cb13dSMatthias Ringwald // extended features request supported, features: fixed channels, unicast connectionless data reception 7261b9cb13dSMatthias Ringwald uint32_t features = 0x280; 7271b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 7281b9cb13dSMatthias Ringwald features |= 0x0008; 7291b9cb13dSMatthias Ringwald #endif 7301b9cb13dSMatthias Ringwald return features; 7311b9cb13dSMatthias Ringwald } 7321b9cb13dSMatthias Ringwald 7338158c421Smatthias.ringwald // MARK: L2CAP_RUN 7342cd0be45Smatthias.ringwald // process outstanding signaling tasks 7357f02f414SMatthias Ringwald static void l2cap_run(void){ 7362b83fb7dSmatthias.ringwald 73722c29ab4SMatthias Ringwald // log_info("l2cap_run: entered"); 73822c29ab4SMatthias Ringwald 7392b83fb7dSmatthias.ringwald // check pending signaling responses 7402b83fb7dSmatthias.ringwald while (signaling_responses_pending){ 7412b83fb7dSmatthias.ringwald 7422b83fb7dSmatthias.ringwald hci_con_handle_t handle = signaling_responses[0].handle; 743a35252c8S[email protected] 744a35252c8S[email protected] if (!hci_can_send_acl_packet_now(handle)) break; 745a35252c8S[email protected] 7462b83fb7dSmatthias.ringwald uint8_t sig_id = signaling_responses[0].sig_id; 747e74c5f58SMatthias Ringwald uint8_t response_code = signaling_responses[0].code; 7482b360848Smatthias.ringwald uint16_t infoType = signaling_responses[0].data; // INFORMATION_REQUEST 74963a7246aSmatthias.ringwald uint16_t result = signaling_responses[0].data; // CONNECTION_REQUEST, COMMAND_REJECT 750b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 751b3264428SMatthias Ringwald uint16_t source_cid = signaling_responses[0].cid; // CONNECTION_REQUEST 752b3264428SMatthias Ringwald #endif 753f3963406SMatthias Ringwald UNUSED(infoType); 75409e9d05bSMatthias Ringwald 755f53da564S[email protected] // remove first item before sending (to avoid sending response mutliple times) 756f53da564S[email protected] signaling_responses_pending--; 757f53da564S[email protected] int i; 758f53da564S[email protected] for (i=0; i < signaling_responses_pending; i++){ 759f53da564S[email protected] memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t)); 760f53da564S[email protected] } 761f53da564S[email protected] 762f53da564S[email protected] switch (response_code){ 76309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 7642b360848Smatthias.ringwald case CONNECTION_REQUEST: 765e74c5f58SMatthias Ringwald l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, source_cid, 0, result, 0); 7662bd8b7e7S[email protected] // also disconnect if result is 0x0003 - security blocked 7674d816277S[email protected] if (result == 0x0003){ 7682bd8b7e7S[email protected] hci_disconnect_security_block(handle); 7694d816277S[email protected] } 7702b360848Smatthias.ringwald break; 7712b83fb7dSmatthias.ringwald case ECHO_REQUEST: 7722b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL); 7732b83fb7dSmatthias.ringwald break; 7742b83fb7dSmatthias.ringwald case INFORMATION_REQUEST: 7753b0484b3S[email protected] switch (infoType){ 7763b0484b3S[email protected] case 1: { // Connectionless MTU 7773b0484b3S[email protected] uint16_t connectionless_mtu = hci_max_acl_data_packet_length(); 7783b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu); 7793b0484b3S[email protected] } 780202c8a4cSMatthias Ringwald break; 7813b0484b3S[email protected] case 2: { // Extended Features Supported 7821b9cb13dSMatthias Ringwald uint32_t features = l2cap_extended_features_mask(); 7833b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features); 7843b0484b3S[email protected] } 785202c8a4cSMatthias Ringwald break; 7863b0484b3S[email protected] case 3: { // Fixed Channels Supported 7873b0484b3S[email protected] uint8_t map[8]; 7883b0484b3S[email protected] memset(map, 0, 8); 789288636a2SMatthias Ringwald map[0] = 0x06; // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04) 7903b0484b3S[email protected] l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map); 7913b0484b3S[email protected] } 792202c8a4cSMatthias Ringwald break; 7933b0484b3S[email protected] default: 7942b83fb7dSmatthias.ringwald // all other types are not supported 7952b83fb7dSmatthias.ringwald l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL); 7963b0484b3S[email protected] break; 7972b83fb7dSmatthias.ringwald } 7982b83fb7dSmatthias.ringwald break; 79963a7246aSmatthias.ringwald case COMMAND_REJECT: 8005ca8d57bS[email protected] l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 80163f0ac45SMatthias Ringwald break; 80209e9d05bSMatthias Ringwald #endif 803a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 80463f0ac45SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 80563f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result); 80663f0ac45SMatthias Ringwald break; 80770efece1S[email protected] case COMMAND_REJECT_LE: 80870efece1S[email protected] l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL); 80963a7246aSmatthias.ringwald break; 81070efece1S[email protected] #endif 8112b83fb7dSmatthias.ringwald default: 8122b83fb7dSmatthias.ringwald // should not happen 8132b83fb7dSmatthias.ringwald break; 8142b83fb7dSmatthias.ringwald } 8152b83fb7dSmatthias.ringwald } 8162b83fb7dSmatthias.ringwald 817665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 818f3963406SMatthias Ringwald UNUSED(it); 81909e9d05bSMatthias Ringwald 8201b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 8211b9cb13dSMatthias Ringwald // send l2cap information request if neccessary 8221b9cb13dSMatthias Ringwald hci_connections_get_iterator(&it); 8231b9cb13dSMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 8241b9cb13dSMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 8251b9cb13dSMatthias Ringwald if (connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST){ 8261b9cb13dSMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W4_EXTENDED_FEATURE_RESPONSE; 8271b9cb13dSMatthias Ringwald // send information request for extended features 8281b9cb13dSMatthias Ringwald uint8_t sig_id = l2cap_next_sig_id(); 8291b9cb13dSMatthias Ringwald uint8_t info_type = 2; 8301b9cb13dSMatthias Ringwald l2cap_send_signaling_packet(connection->con_handle, INFORMATION_REQUEST, sig_id, info_type); 8311b9cb13dSMatthias Ringwald return; 8321b9cb13dSMatthias Ringwald } 8331b9cb13dSMatthias Ringwald } 8341b9cb13dSMatthias Ringwald #endif 8351b9cb13dSMatthias Ringwald 83609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 83743ec931dSMatthias Ringwald uint8_t config_options[10]; 838665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 839665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 840baf94f06S[email protected] 841665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 84222c29ab4SMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 8432cd0be45Smatthias.ringwald switch (channel->state){ 8442cd0be45Smatthias.ringwald 845df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 846ad671560S[email protected] case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 847fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 848a00031e2S[email protected] if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 849ad671560S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 850fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 851ad671560S[email protected] } 852ad671560S[email protected] break; 853ad671560S[email protected] 85402b22dc4Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 855baf94f06S[email protected] if (!hci_can_send_command_packet_now()) break; 85664472d52Smatthias.ringwald // send connection request - set state first 85764472d52Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE; 85802b22dc4Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 8598f8108aaSmatthias.ringwald hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 86002b22dc4Smatthias.ringwald break; 86102b22dc4Smatthias.ringwald 862e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE: 863fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 86422c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 865fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0); 866e7ff783cSmatthias.ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 8679dcb2fb2S[email protected] l2cap_stop_rtx(channel); 868665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 869d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 870e7ff783cSmatthias.ringwald break; 871e7ff783cSmatthias.ringwald 872552d92a1Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT: 873fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 874fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 87528ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 876fc64f94aSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0); 877552d92a1Smatthias.ringwald break; 878552d92a1Smatthias.ringwald 8796fdcc387Smatthias.ringwald case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST: 880fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 8816fdcc387Smatthias.ringwald // success, start l2cap handshake 882b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 8836fdcc387Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_CONNECT_RSP; 884fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid); 8855932bd7cS[email protected] l2cap_start_rtx(channel); 8866fdcc387Smatthias.ringwald break; 8876fdcc387Smatthias.ringwald 888fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 889fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 89073cf2b3dSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){ 89163a7246aSmatthias.ringwald uint16_t flags = 0; 89228ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 89363a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { 89463a7246aSmatthias.ringwald flags = 1; 89563a7246aSmatthias.ringwald } else { 89628ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 89763a7246aSmatthias.ringwald } 89863a7246aSmatthias.ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){ 899ac8f1300SMatthias Ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 900fc64f94aSMatthias 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); 901ac8f1300SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 902ac8f1300SMatthias Ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED){ 903ac8f1300SMatthias Ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED); 904ac8f1300SMatthias Ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP); 9056dca2a0cSMatthias Ringwald uint16_t options_size = l2cap_setup_options(channel, config_options); 906ac8f1300SMatthias 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); 907ac8f1300SMatthias Ringwald #endif 908ac8f1300SMatthias Ringwald } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){ 90963a7246aSmatthias.ringwald channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 910ac8f1300SMatthias Ringwald uint16_t options_size = l2cap_setup_options(channel, config_options); 911ac8f1300SMatthias 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); 91263a7246aSmatthias.ringwald } else { 913ac8f1300SMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS, 0, NULL); 91463a7246aSmatthias.ringwald } 91563a7246aSmatthias.ringwald channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 916fa8473a4Smatthias.ringwald } 91773cf2b3dSmatthias.ringwald else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){ 91828ca2b46S[email protected] channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 91928ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ); 920b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 9216dca2a0cSMatthias Ringwald uint16_t options_size = l2cap_setup_options(channel, config_options); 9226dca2a0cSMatthias Ringwald l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, options_size, &config_options); 9235932bd7cS[email protected] l2cap_start_rtx(channel); 924fa8473a4Smatthias.ringwald } 925fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 926552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_OPEN; 927552d92a1Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); // success 928fa8473a4Smatthias.ringwald } 929552d92a1Smatthias.ringwald break; 930552d92a1Smatthias.ringwald 931e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 932fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 93322c29ab4SMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 934fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 9355932bd7cS[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 :) 936756102d3Smatthias.ringwald l2cap_finialize_channel_close(channel); // -- remove from list 937e7ff783cSmatthias.ringwald break; 938e7ff783cSmatthias.ringwald 939e7ff783cSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 940fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 941b1988dceSmatthias.ringwald channel->local_sig_id = l2cap_next_sig_id(); 9422cd0be45Smatthias.ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 943fc64f94aSMatthias Ringwald l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 9442cd0be45Smatthias.ringwald break; 9452cd0be45Smatthias.ringwald default: 9462cd0be45Smatthias.ringwald break; 9472cd0be45Smatthias.ringwald } 94838f62777SMatthias Ringwald 94938f62777SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 95038f62777SMatthias Ringwald // send s-frame to acknowledge received packets 95138f62777SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 952675e6881SMatthias Ringwald 953675e6881SMatthias Ringwald if (channel->tx_send_index != channel->tx_write_index){ 954*212b6be2SMatthias Ringwald int unacknowledged_packets = l2cap_ertm_num_unacknowledged_tx_packets(channel); 955*212b6be2SMatthias Ringwald // check remote tx window 956*212b6be2SMatthias Ringwald log_info("unacknowledged_packets %u, remote tx window size %u", unacknowledged_packets, channel->remote_tx_window_size); 957*212b6be2SMatthias Ringwald if (unacknowledged_packets < channel->remote_tx_window_size){ 958675e6881SMatthias Ringwald int index = channel->tx_send_index; 959675e6881SMatthias Ringwald channel->tx_send_index++; 960675e6881SMatthias Ringwald if (channel->tx_send_index >= channel->num_tx_buffers){ 961675e6881SMatthias Ringwald channel->tx_send_index = 0; 962675e6881SMatthias Ringwald } 963675e6881SMatthias Ringwald l2cap_ertm_send_information_frame(channel, index); 964675e6881SMatthias Ringwald continue; 965675e6881SMatthias Ringwald } 966*212b6be2SMatthias Ringwald } 967675e6881SMatthias Ringwald 968d84e866fSMatthias Ringwald if (channel->send_supervisor_frame_receiver_ready){ 969d84e866fSMatthias Ringwald channel->send_supervisor_frame_receiver_ready = 0;; 970d84e866fSMatthias Ringwald log_info("Send S-Frame: RR %u", channel->req_seq); 97138f62777SMatthias Ringwald uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY, 0, 0, channel->req_seq); 97238f62777SMatthias Ringwald l2cap_ertm_send_supervisor_frame(channel, control); 973675e6881SMatthias Ringwald continue; 97438f62777SMatthias Ringwald } 97538f62777SMatthias Ringwald #endif 97638f62777SMatthias Ringwald 9772cd0be45Smatthias.ringwald } 97809e9d05bSMatthias Ringwald #endif 979da886c03S[email protected] 980cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 981efedfb4cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 982efedfb4cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9837f107edaSMatthias Ringwald uint8_t * acl_buffer; 9847f107edaSMatthias Ringwald uint8_t * l2cap_payload; 9857f107edaSMatthias Ringwald uint16_t pos; 9867f107edaSMatthias Ringwald uint16_t payload_size; 987efedfb4cSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 988efedfb4cSMatthias Ringwald // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var); 989efedfb4cSMatthias Ringwald switch (channel->state){ 9905cb87675SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST: 9915cb87675SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 9925cb87675SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE; 9935cb87675SMatthias Ringwald // le psm, source cid, mtu, mps, initial credits 9941b8b8d05SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 99563f0ac45SMatthias Ringwald channel->credits_incoming = channel->new_credits_incoming; 99663f0ac45SMatthias Ringwald channel->new_credits_incoming = 0; 99763f0ac45SMatthias 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); 9985cb87675SMatthias Ringwald break; 99923017473SMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT: 100023017473SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 100123017473SMatthias Ringwald // TODO: support larger MPS 100223017473SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 100363f0ac45SMatthias Ringwald channel->credits_incoming = channel->new_credits_incoming; 100463f0ac45SMatthias Ringwald channel->new_credits_incoming = 0; 100563f0ac45SMatthias 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); 100664e11ca9SMatthias Ringwald // notify client 100744276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, 0); 100823017473SMatthias Ringwald break; 1009e7d0c9aaSMatthias Ringwald case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE: 1010e7d0c9aaSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1011e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 101263f0ac45SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason); 1013e7d0c9aaSMatthias Ringwald // discard channel - l2cap_finialize_channel_close without sending l2cap close event 1014e7d0c9aaSMatthias Ringwald l2cap_stop_rtx(channel); 1015e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1016e7d0c9aaSMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1017e7d0c9aaSMatthias Ringwald break; 10187f107edaSMatthias Ringwald case L2CAP_STATE_OPEN: 101985aeef60SMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 102085aeef60SMatthias Ringwald 102185aeef60SMatthias Ringwald // send credits 102285aeef60SMatthias Ringwald if (channel->new_credits_incoming){ 102385aeef60SMatthias Ringwald log_info("l2cap: sending %u credits", channel->new_credits_incoming); 102485aeef60SMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 102585aeef60SMatthias Ringwald uint16_t new_credits = channel->new_credits_incoming; 102685aeef60SMatthias Ringwald channel->new_credits_incoming = 0; 102785aeef60SMatthias Ringwald channel->credits_incoming += new_credits; 102885aeef60SMatthias Ringwald l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits); 102985aeef60SMatthias Ringwald break; 103085aeef60SMatthias Ringwald } 103185aeef60SMatthias Ringwald 103285aeef60SMatthias Ringwald // send data 10337f107edaSMatthias Ringwald if (!channel->send_sdu_buffer) break; 10347f107edaSMatthias Ringwald if (!channel->credits_outgoing) break; 103585aeef60SMatthias Ringwald 10367f107edaSMatthias Ringwald // send part of SDU 10377f107edaSMatthias Ringwald hci_reserve_packet_buffer(); 10387f107edaSMatthias Ringwald acl_buffer = hci_get_outgoing_packet_buffer(); 10397f107edaSMatthias Ringwald l2cap_payload = acl_buffer + 8; 10407f107edaSMatthias Ringwald pos = 0; 10417f107edaSMatthias Ringwald if (!channel->send_sdu_pos){ 10427f107edaSMatthias Ringwald // store SDU len 10437f107edaSMatthias Ringwald channel->send_sdu_pos += 2; 10447f107edaSMatthias Ringwald little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len); 10457f107edaSMatthias Ringwald pos += 2; 10467f107edaSMatthias Ringwald } 10477f107edaSMatthias Ringwald payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos); 104885aeef60SMatthias Ringwald log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing); 10497f107edaSMatthias Ringwald memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len 10507f107edaSMatthias Ringwald pos += payload_size; 10517f107edaSMatthias Ringwald channel->send_sdu_pos += payload_size; 1052f511cefaSMatthias Ringwald l2cap_setup_header(acl_buffer, channel->con_handle, 0, channel->remote_cid, pos); 10537f107edaSMatthias Ringwald // done 10547f107edaSMatthias Ringwald 105585aeef60SMatthias Ringwald channel->credits_outgoing--; 10567f107edaSMatthias Ringwald 10577f107edaSMatthias Ringwald if (channel->send_sdu_pos >= channel->send_sdu_len + 2){ 10587f107edaSMatthias Ringwald channel->send_sdu_buffer = NULL; 105944276248SMatthias Ringwald // send done event 106044276248SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT); 106144276248SMatthias Ringwald // inform about can send now 106244276248SMatthias Ringwald l2cap_le_notify_channel_can_send(channel); 10637f107edaSMatthias Ringwald } 10647f107edaSMatthias Ringwald hci_send_acl_packet_buffer(8 + pos); 10657f107edaSMatthias Ringwald break; 1066828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 1067828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1068828a7f7aSMatthias Ringwald channel->local_sig_id = l2cap_next_sig_id(); 1069828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_DISCONNECT; 1070828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid); 1071828a7f7aSMatthias Ringwald break; 1072828a7f7aSMatthias Ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE: 1073828a7f7aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) break; 1074828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_INVALID; 1075828a7f7aSMatthias Ringwald l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid); 1076828a7f7aSMatthias Ringwald l2cap_le_finialize_channel_close(channel); // -- remove from list 1077828a7f7aSMatthias Ringwald break; 1078efedfb4cSMatthias Ringwald default: 1079efedfb4cSMatthias Ringwald break; 1080efedfb4cSMatthias Ringwald } 1081efedfb4cSMatthias Ringwald } 108209e9d05bSMatthias Ringwald #endif 1083efedfb4cSMatthias Ringwald 108409e9d05bSMatthias Ringwald #ifdef ENABLE_BLE 1085da886c03S[email protected] // send l2cap con paramter update if necessary 1086da886c03S[email protected] hci_connections_get_iterator(&it); 1087665d90f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 1088665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 10895d14fa8fSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue; 1090b68d7bc3SMatthias Ringwald if (!hci_can_send_acl_packet_now(connection->con_handle)) continue; 1091da886c03S[email protected] switch (connection->le_con_parameter_update_state){ 1092b68d7bc3SMatthias Ringwald case CON_PARAMETER_UPDATE_SEND_REQUEST: 1093b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 1094b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier, 1095b68d7bc3SMatthias Ringwald connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout); 1096b68d7bc3SMatthias Ringwald break; 1097da886c03S[email protected] case CON_PARAMETER_UPDATE_SEND_RESPONSE: 1098b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 1099b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0); 1100da886c03S[email protected] break; 1101da886c03S[email protected] case CON_PARAMETER_UPDATE_DENY: 1102b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 1103b68d7bc3SMatthias Ringwald l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1); 1104da886c03S[email protected] break; 1105da886c03S[email protected] default: 1106da886c03S[email protected] break; 1107da886c03S[email protected] } 1108da886c03S[email protected] } 11094d7157c3S[email protected] #endif 1110da886c03S[email protected] 111122c29ab4SMatthias Ringwald // log_info("l2cap_run: exit"); 11122cd0be45Smatthias.ringwald } 11132cd0be45Smatthias.ringwald 111409e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1115fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){ 11162df5dadcS[email protected] if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 11175533f01eS[email protected] log_info("l2cap_handle_connection_complete expected state"); 11182df5dadcS[email protected] // success, start l2cap handshake 1119fc64f94aSMatthias Ringwald channel->con_handle = con_handle; 11202df5dadcS[email protected] // check remote SSP feature first 11212df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; 11222df5dadcS[email protected] } 11232df5dadcS[email protected] } 11242df5dadcS[email protected] 11251b9cb13dSMatthias Ringwald static void l2cap_ready_to_connect(l2cap_channel_t * channel){ 11261b9cb13dSMatthias Ringwald 11271b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 112827e0774aSMatthias Ringwald // assumption: outgoing connection 11291b9cb13dSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(channel->con_handle); 11301b9cb13dSMatthias Ringwald if (connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_IDLE){ 11311b9cb13dSMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST; 11321b9cb13dSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES; 11331b9cb13dSMatthias Ringwald return; 11341b9cb13dSMatthias Ringwald } 11351b9cb13dSMatthias Ringwald #endif 11361b9cb13dSMatthias Ringwald 11371b9cb13dSMatthias Ringwald // fine, go ahead 11381b9cb13dSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 11391b9cb13dSMatthias Ringwald } 11401b9cb13dSMatthias Ringwald 11412df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){ 11422df5dadcS[email protected] if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return; 11432df5dadcS[email protected] 11442df5dadcS[email protected] // we have been waiting for remote supported features, if both support SSP, 1145ac301f95S[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)); 1146fc64f94aSMatthias Ringwald if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 11472df5dadcS[email protected] // request security level 2 11482df5dadcS[email protected] channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 1149fc64f94aSMatthias Ringwald gap_request_security_level(channel->con_handle, LEVEL_2); 11502df5dadcS[email protected] return; 11512df5dadcS[email protected] } 11521b9cb13dSMatthias Ringwald 11531b9cb13dSMatthias Ringwald l2cap_ready_to_connect(channel); 11542df5dadcS[email protected] } 115509e9d05bSMatthias Ringwald #endif 11562df5dadcS[email protected] 115709e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS 1158da144af5SMatthias 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, 1159da144af5SMatthias Ringwald uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){ 1160da144af5SMatthias Ringwald 1161da144af5SMatthias Ringwald l2cap_channel_t * channel = btstack_memory_l2cap_channel_get(); 1162da144af5SMatthias Ringwald if (!channel) { 1163da144af5SMatthias Ringwald return NULL; 1164da144af5SMatthias Ringwald } 1165da144af5SMatthias Ringwald 1166da144af5SMatthias Ringwald // Init memory (make valgrind happy) 1167da144af5SMatthias Ringwald memset(channel, 0, sizeof(l2cap_channel_t)); 1168da144af5SMatthias Ringwald 1169da144af5SMatthias Ringwald // fill in 1170da144af5SMatthias Ringwald channel->packet_handler = packet_handler; 1171da144af5SMatthias Ringwald bd_addr_copy(channel->address, address); 1172da144af5SMatthias Ringwald channel->address_type = address_type; 1173da144af5SMatthias Ringwald channel->psm = psm; 1174da144af5SMatthias Ringwald channel->local_mtu = local_mtu; 1175da144af5SMatthias Ringwald channel->remote_mtu = L2CAP_MINIMAL_MTU; 1176da144af5SMatthias Ringwald channel->required_security_level = security_level; 1177da144af5SMatthias Ringwald 1178da144af5SMatthias Ringwald // 1179da144af5SMatthias Ringwald channel->local_cid = l2cap_next_local_cid(); 1180da144af5SMatthias Ringwald channel->con_handle = 0; 1181da144af5SMatthias Ringwald 1182da144af5SMatthias Ringwald // set initial state 1183da144af5SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION; 1184da144af5SMatthias Ringwald channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE; 1185da144af5SMatthias Ringwald channel->remote_sig_id = L2CAP_SIG_ID_INVALID; 1186da144af5SMatthias Ringwald channel->local_sig_id = L2CAP_SIG_ID_INVALID; 118738f62777SMatthias Ringwald 1188da144af5SMatthias Ringwald return channel; 1189da144af5SMatthias Ringwald } 119009e9d05bSMatthias Ringwald #endif 119109e9d05bSMatthias Ringwald 119209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1193da144af5SMatthias Ringwald 11949077cb15SMatthias Ringwald /** 11959077cb15SMatthias Ringwald * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary. 11969077cb15SMatthias Ringwald * @param packet_handler 11979077cb15SMatthias Ringwald * @param address 11989077cb15SMatthias Ringwald * @param psm 11999077cb15SMatthias Ringwald * @param mtu 12009077cb15SMatthias Ringwald * @param local_cid 12019077cb15SMatthias Ringwald */ 12029077cb15SMatthias Ringwald 12039d139fbaSMatthias 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){ 12049d139fbaSMatthias Ringwald // limit MTU to the size of our outtgoing HCI buffer 12059d139fbaSMatthias Ringwald uint16_t local_mtu = btstack_min(mtu, l2cap_max_mtu()); 1206da144af5SMatthias Ringwald 12079d139fbaSMatthias 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); 1208da144af5SMatthias Ringwald 1209da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0); 1210fc64f94aSMatthias Ringwald if (!channel) { 12119077cb15SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 12129077cb15SMatthias Ringwald } 12139077cb15SMatthias Ringwald 12141b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 12151b9cb13dSMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 12161b9cb13dSMatthias Ringwald #endif 12171b9cb13dSMatthias Ringwald 12189077cb15SMatthias Ringwald // add to connections list 1219fc64f94aSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 12209077cb15SMatthias Ringwald 12219077cb15SMatthias Ringwald // store local_cid 12229077cb15SMatthias Ringwald if (out_local_cid){ 1223fc64f94aSMatthias Ringwald *out_local_cid = channel->local_cid; 12249077cb15SMatthias Ringwald } 12259077cb15SMatthias Ringwald 12269077cb15SMatthias Ringwald // check if hci connection is already usable 12279077cb15SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 12289077cb15SMatthias Ringwald if (conn){ 1229add0254bSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 1230fc64f94aSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 12319077cb15SMatthias Ringwald // check if remote supported fearures are already received 12329077cb15SMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 1233fc64f94aSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 12349077cb15SMatthias Ringwald } 12359077cb15SMatthias Ringwald } 12369077cb15SMatthias Ringwald 12379077cb15SMatthias Ringwald l2cap_run(); 12389077cb15SMatthias Ringwald 12399077cb15SMatthias Ringwald return 0; 12409077cb15SMatthias Ringwald } 12419077cb15SMatthias Ringwald 12421b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 12437b181629SMatthias Ringwald 12442b70d705SMatthias Ringwald static uint8_t l2cap_ertm_validate_local_config(uint8_t max_transmit, uint16_t retransmission_timeout_ms, 12452b70d705SMatthias Ringwald uint16_t monitor_timeout_ms, uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size){ 12462b70d705SMatthias Ringwald UNUSED(buffer); 12472b70d705SMatthias Ringwald UNUSED(size); 12482b70d705SMatthias Ringwald 12492b70d705SMatthias Ringwald uint8_t result = ERROR_CODE_SUCCESS; 12502b70d705SMatthias Ringwald if (max_transmit < 1){ 12512b70d705SMatthias Ringwald log_error("max_transmit must be >= 1"); 12522b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 12532b70d705SMatthias Ringwald } 12542b70d705SMatthias Ringwald if (retransmission_timeout_ms < 2000){ 12552b70d705SMatthias Ringwald log_error("retransmission_timeout_ms must be >= 2000 ms"); 12562b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 12572b70d705SMatthias Ringwald } 12582b70d705SMatthias Ringwald if (monitor_timeout_ms < 12000){ 12592b70d705SMatthias Ringwald log_error("monitor_timeout_ms must be >= 12000 ms"); 12602b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 12612b70d705SMatthias Ringwald } 12622b70d705SMatthias Ringwald if (num_rx_buffers < 1){ 12632b70d705SMatthias Ringwald log_error("num_rx_buffers must be >= 1"); 12642b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 12652b70d705SMatthias Ringwald } 12662b70d705SMatthias Ringwald if (num_tx_buffers < 1){ 12672b70d705SMatthias Ringwald log_error("num_rx_buffers must be >= 1"); 12682b70d705SMatthias Ringwald result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 12692b70d705SMatthias Ringwald } 12702b70d705SMatthias Ringwald return result; 12712b70d705SMatthias Ringwald } 12722b70d705SMatthias Ringwald 12737b181629SMatthias Ringwald static void l2cap_ertm_configure_channel(l2cap_channel_t * channel, int ertm_mandatory, uint8_t max_transmit, 12747b181629SMatthias Ringwald uint16_t retransmission_timeout_ms, uint16_t monitor_timeout_ms, uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size){ 12757b181629SMatthias Ringwald 12767b181629SMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION; 12777b181629SMatthias Ringwald channel->ertm_mandatory = ertm_mandatory; 1278bbc0a9e7SMatthias Ringwald channel->local_max_transmit = max_transmit; 1279bbc0a9e7SMatthias Ringwald channel->local_retransmission_timeout_ms = retransmission_timeout_ms; 1280bbc0a9e7SMatthias Ringwald channel->local_monitor_timeout_ms = monitor_timeout_ms; 12817b181629SMatthias Ringwald channel->num_rx_buffers = num_rx_buffers; 12827b181629SMatthias Ringwald channel->num_tx_buffers = num_tx_buffers; 12837b181629SMatthias Ringwald 12847b181629SMatthias Ringwald // TODO: align buffer pointer 12857b181629SMatthias Ringwald uint32_t pos = 0; 12867b181629SMatthias Ringwald channel->rx_packets_state = (l2cap_ertm_rx_packet_state_t *) &buffer[pos]; 12877b181629SMatthias Ringwald pos += num_rx_buffers * sizeof(l2cap_ertm_rx_packet_state_t); 12887b181629SMatthias Ringwald channel->tx_packets_state = (l2cap_ertm_tx_packet_state_t *) &buffer[pos]; 12897b181629SMatthias Ringwald pos += num_tx_buffers * sizeof(l2cap_ertm_tx_packet_state_t); 12907b181629SMatthias Ringwald // calculate MTU 12917b181629SMatthias Ringwald channel->local_mtu = (size - pos) / (num_rx_buffers + num_tx_buffers); 12927b181629SMatthias Ringwald log_info("Local ERTM MTU: %u", channel->local_mtu); 12937b181629SMatthias Ringwald channel->rx_packets_data = &buffer[pos]; 12947b181629SMatthias Ringwald pos += num_rx_buffers * channel->local_mtu; 12957b181629SMatthias Ringwald channel->tx_packets_data = &buffer[pos]; 12967b181629SMatthias Ringwald log_info("RX packets %p, TX packets %p", channel->rx_packets_data, channel->tx_packets_data); 12977b181629SMatthias Ringwald } 12987b181629SMatthias Ringwald 1299501329faSMatthias Ringwald uint8_t l2cap_create_ertm_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, 1300501329faSMatthias Ringwald int ertm_mandatory, uint8_t max_transmit, uint16_t retransmission_timeout_ms, uint16_t monitor_timeout_ms, 1301501329faSMatthias Ringwald uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid){ 1302501329faSMatthias Ringwald 13031b9cb13dSMatthias Ringwald // limit MTU to the size of our outtgoing HCI buffer 1304501329faSMatthias Ringwald uint16_t local_mtu = l2cap_max_mtu(); 13051b9cb13dSMatthias Ringwald 1306501329faSMatthias Ringwald log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x -> local mtu %u", bd_addr_to_str(address), psm, local_mtu); 13071b9cb13dSMatthias Ringwald 13082b70d705SMatthias Ringwald // validate local config 13092b70d705SMatthias Ringwald uint8_t result = l2cap_ertm_validate_local_config(max_transmit, retransmission_timeout_ms, monitor_timeout_ms, num_tx_buffers, num_rx_buffers, buffer, size); 13102b70d705SMatthias Ringwald if (result) return result; 13112b70d705SMatthias Ringwald 1312501329faSMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0); 13131b9cb13dSMatthias Ringwald if (!channel) { 13141b9cb13dSMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 13151b9cb13dSMatthias Ringwald } 13161b9cb13dSMatthias Ringwald 13177b181629SMatthias Ringwald // configure ERTM 13187b181629SMatthias Ringwald l2cap_ertm_configure_channel(channel, ertm_mandatory, max_transmit, retransmission_timeout_ms, 13197b181629SMatthias Ringwald monitor_timeout_ms, num_tx_buffers, num_rx_buffers, buffer, size); 13201b9cb13dSMatthias Ringwald 13211b9cb13dSMatthias Ringwald // add to connections list 13221b9cb13dSMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 13231b9cb13dSMatthias Ringwald 13241b9cb13dSMatthias Ringwald // store local_cid 13251b9cb13dSMatthias Ringwald if (out_local_cid){ 13261b9cb13dSMatthias Ringwald *out_local_cid = channel->local_cid; 13271b9cb13dSMatthias Ringwald } 13281b9cb13dSMatthias Ringwald 13291b9cb13dSMatthias Ringwald // check if hci connection is already usable 13301b9cb13dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC); 13311b9cb13dSMatthias Ringwald if (conn){ 13321b9cb13dSMatthias Ringwald log_info("l2cap_create_channel, hci connection already exists"); 13331b9cb13dSMatthias Ringwald l2cap_handle_connection_complete(conn->con_handle, channel); 13341b9cb13dSMatthias Ringwald // check if remote supported fearures are already received 13351b9cb13dSMatthias Ringwald if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) { 13361b9cb13dSMatthias Ringwald l2cap_handle_remote_supported_features_received(channel); 13371b9cb13dSMatthias Ringwald } 13381b9cb13dSMatthias Ringwald } 13391b9cb13dSMatthias Ringwald 13401b9cb13dSMatthias Ringwald l2cap_run(); 13411b9cb13dSMatthias Ringwald 13421b9cb13dSMatthias Ringwald return 0; 13431b9cb13dSMatthias Ringwald } 13441b9cb13dSMatthias Ringwald #endif 13451b9cb13dSMatthias Ringwald 1346ce8f182eSMatthias Ringwald void 1347ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){ 1348e0abb8e7S[email protected] log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason); 1349b35f641cSmatthias.ringwald // find channel for local_cid 1350b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1351f62db1e3Smatthias.ringwald if (channel) { 1352e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1353f62db1e3Smatthias.ringwald } 13542cd0be45Smatthias.ringwald // process 13552cd0be45Smatthias.ringwald l2cap_run(); 135643625864Smatthias.ringwald } 13571e6aba47Smatthias.ringwald 1358afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){ 1359665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1360665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1361665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1362665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1363058e3d6bSMatthias Ringwald if ( bd_addr_cmp( channel->address, address) != 0) continue; 1364c22aecc9S[email protected] // channel for this address found 1365c22aecc9S[email protected] switch (channel->state){ 1366c22aecc9S[email protected] case L2CAP_STATE_WAIT_CONNECTION_COMPLETE: 1367c22aecc9S[email protected] case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: 1368afde0c52Smatthias.ringwald // failure, forward error code 1369afde0c52Smatthias.ringwald l2cap_emit_channel_opened(channel, status); 1370afde0c52Smatthias.ringwald // discard channel 13719dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1372665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1373d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 1374c22aecc9S[email protected] break; 1375c22aecc9S[email protected] default: 1376c22aecc9S[email protected] break; 1377afde0c52Smatthias.ringwald } 1378afde0c52Smatthias.ringwald } 1379afde0c52Smatthias.ringwald } 1380afde0c52Smatthias.ringwald 1381afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 1382665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1383665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1384665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1385665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1386058e3d6bSMatthias Ringwald if ( ! bd_addr_cmp( channel->address, address) ){ 13872df5dadcS[email protected] l2cap_handle_connection_complete(handle, channel); 1388afde0c52Smatthias.ringwald } 1389afde0c52Smatthias.ringwald } 13906fdcc387Smatthias.ringwald // process 13916fdcc387Smatthias.ringwald l2cap_run(); 1392afde0c52Smatthias.ringwald } 139309e9d05bSMatthias Ringwald #endif 1394b448a0e7Smatthias.ringwald 139533c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){ 139609e9d05bSMatthias Ringwald 139709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 139833c40538SMatthias Ringwald btstack_linked_list_iterator_t it; 139933c40538SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 140033c40538SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 140133c40538SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 140233c40538SMatthias Ringwald if (!channel->waiting_for_can_send_now) continue; 1403fc64f94aSMatthias Ringwald if (!hci_can_send_acl_packet_now(channel->con_handle)) continue; 140433c40538SMatthias Ringwald channel->waiting_for_can_send_now = 0; 140534e7e577SMatthias Ringwald l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid); 140633c40538SMatthias Ringwald } 140709e9d05bSMatthias Ringwald #endif 140844276248SMatthias Ringwald 14092125de09SMatthias Ringwald int i; 14102125de09SMatthias Ringwald for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){ 1411773c4106SMatthias Ringwald if (!fixed_channels[i].callback) continue; 14122125de09SMatthias Ringwald if (!fixed_channels[i].waiting_for_can_send_now) continue; 141335454696SMatthias Ringwald int can_send = 0; 141434e7e577SMatthias Ringwald if (l2cap_fixed_channel_table_index_is_le(i)){ 141535454696SMatthias Ringwald #ifdef ENABLE_BLE 141634e7e577SMatthias Ringwald can_send = hci_can_send_acl_le_packet_now(); 141735454696SMatthias Ringwald #endif 141834e7e577SMatthias Ringwald } else { 141935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 142034e7e577SMatthias Ringwald can_send = hci_can_send_acl_classic_packet_now(); 142135454696SMatthias Ringwald #endif 142234e7e577SMatthias Ringwald } 142334e7e577SMatthias Ringwald if (!can_send) continue; 142434e7e577SMatthias Ringwald fixed_channels[i].waiting_for_can_send_now = 0; 142534e7e577SMatthias Ringwald l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i)); 14262125de09SMatthias Ringwald } 142733c40538SMatthias Ringwald } 142833c40538SMatthias Ringwald 1429d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){ 1430afde0c52Smatthias.ringwald 14319ec2630cSMatthias Ringwald UNUSED(packet_type); 14329ec2630cSMatthias Ringwald UNUSED(cid); 14339ec2630cSMatthias Ringwald UNUSED(size); 14349ec2630cSMatthias Ringwald 1435afde0c52Smatthias.ringwald bd_addr_t address; 1436afde0c52Smatthias.ringwald hci_con_handle_t handle; 14372d00edd4Smatthias.ringwald int hci_con_used; 143809e9d05bSMatthias Ringwald btstack_linked_list_iterator_t it; 143909e9d05bSMatthias Ringwald 144009e9d05bSMatthias Ringwald // avoid unused warnings 1441f3963406SMatthias Ringwald UNUSED(address); 1442f3963406SMatthias Ringwald UNUSED(hci_con_used); 1443f3963406SMatthias Ringwald UNUSED(it); 1444f22209dfSMatthias Ringwald UNUSED(handle); 1445afde0c52Smatthias.ringwald 14460e2df43fSMatthias Ringwald switch(hci_event_packet_get_type(packet)){ 1447afde0c52Smatthias.ringwald 144809e9d05bSMatthias Ringwald // Notify channel packet handler if they can send now 144909e9d05bSMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 145009e9d05bSMatthias Ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 145109e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 145209e9d05bSMatthias Ringwald l2cap_notify_channel_can_send(); 145309e9d05bSMatthias Ringwald break; 145409e9d05bSMatthias Ringwald 145509e9d05bSMatthias Ringwald case HCI_EVENT_COMMAND_STATUS: 145609e9d05bSMatthias Ringwald l2cap_run(); // try sending signaling packets first 145709e9d05bSMatthias Ringwald break; 145809e9d05bSMatthias Ringwald 145909e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1460afde0c52Smatthias.ringwald // handle connection complete events 1461afde0c52Smatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1462724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], address); 1463afde0c52Smatthias.ringwald if (packet[2] == 0){ 1464f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1465afde0c52Smatthias.ringwald l2cap_handle_connection_success_for_addr(address, handle); 1466afde0c52Smatthias.ringwald } else { 1467afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, packet[2]); 1468afde0c52Smatthias.ringwald } 1469afde0c52Smatthias.ringwald break; 1470afde0c52Smatthias.ringwald 1471afde0c52Smatthias.ringwald // handle successful create connection cancel command 1472afde0c52Smatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 1473073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) { 1474afde0c52Smatthias.ringwald if (packet[5] == 0){ 1475724d70a2SMatthias Ringwald reverse_bd_addr(&packet[6], address); 1476afde0c52Smatthias.ringwald // CONNECTION TERMINATED BY LOCAL HOST (0X16) 1477afde0c52Smatthias.ringwald l2cap_handle_connection_failed_for_addr(address, 0x16); 147803cfbabcSmatthias.ringwald } 14791e6aba47Smatthias.ringwald } 148039d59809Smatthias.ringwald l2cap_run(); // try sending signaling packets first 148139d59809Smatthias.ringwald break; 148209e9d05bSMatthias Ringwald #endif 148327a923d0Smatthias.ringwald 14841e6aba47Smatthias.ringwald // handle disconnection complete events 1485afde0c52Smatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1486c22aecc9S[email protected] // send l2cap disconnect events for all channels on this handle and free them 148709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1488d0662982SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1489665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1490665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1491665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1492fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 149315ec09bbSmatthias.ringwald l2cap_emit_channel_closed(channel); 14949dcb2fb2S[email protected] l2cap_stop_rtx(channel); 1495665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1496d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 149727a923d0Smatthias.ringwald } 149809e9d05bSMatthias Ringwald #endif 1499a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 1500d0662982SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1501991fea48SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 1502991fea48SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1503991fea48SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1504991fea48SMatthias Ringwald if (channel->con_handle != handle) continue; 1505991fea48SMatthias Ringwald l2cap_emit_channel_closed(channel); 1506991fea48SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 1507991fea48SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 1508991fea48SMatthias Ringwald } 1509991fea48SMatthias Ringwald #endif 1510afde0c52Smatthias.ringwald break; 1511fcadd0caSmatthias.ringwald 1512ee091cf1Smatthias.ringwald // HCI Connection Timeouts 151309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 1514afde0c52Smatthias.ringwald case L2CAP_EVENT_TIMEOUT_CHECK: 1515f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1516bd04d84aSMatthias Ringwald if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break; 151780ca58a0Smatthias.ringwald if (hci_authentication_active_for_handle(handle)) break; 15182d00edd4Smatthias.ringwald hci_con_used = 0; 1519665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1520665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1521665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1522fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 15232d00edd4Smatthias.ringwald hci_con_used = 1; 1524c22aecc9S[email protected] break; 1525ee091cf1Smatthias.ringwald } 15262d00edd4Smatthias.ringwald if (hci_con_used) break; 1527d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) break; 15289edc8742Smatthias.ringwald hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection 1529afde0c52Smatthias.ringwald break; 1530ee091cf1Smatthias.ringwald 1531df3354fcS[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1532f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1533665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1534665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1535665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1536fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 15372df5dadcS[email protected] l2cap_handle_remote_supported_features_received(channel); 1538df3354fcS[email protected] break; 1539df3354fcS[email protected] } 1540c22aecc9S[email protected] break; 1541df3354fcS[email protected] 15425611a760SMatthias Ringwald case GAP_EVENT_SECURITY_LEVEL: 1543f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 2); 1544bd63148eS[email protected] log_info("l2cap - security level update"); 1545665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 1546665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1547665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 1548fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 15495533f01eS[email protected] 1550bd63148eS[email protected] log_info("l2cap - state %u", channel->state); 1551bd63148eS[email protected] 1552e569dfd9SMatthias Ringwald gap_security_level_t actual_level = (gap_security_level_t) packet[4]; 15535533f01eS[email protected] gap_security_level_t required_level = channel->required_security_level; 15545533f01eS[email protected] 1555df3354fcS[email protected] switch (channel->state){ 1556df3354fcS[email protected] case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 15575533f01eS[email protected] if (actual_level >= required_level){ 155852606043SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 155952606043SMatthias Ringwald // we need to know if ERTM is supported before sending a config response 156052606043SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(channel->con_handle); 156152606043SMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST; 156252606043SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES; 156352606043SMatthias Ringwald #else 1564f85a9399S[email protected] channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 156544276248SMatthias Ringwald l2cap_emit_incoming_connection(channel); 156652606043SMatthias Ringwald #endif 15671eb2563eS[email protected] } else { 1568775ecc36SMatthias Ringwald channel->reason = 0x0003; // security block 15691eb2563eS[email protected] channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 15701eb2563eS[email protected] } 1571df3354fcS[email protected] break; 1572df3354fcS[email protected] 1573df3354fcS[email protected] case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 15745533f01eS[email protected] if (actual_level >= required_level){ 15751b9cb13dSMatthias Ringwald l2cap_ready_to_connect(channel); 1576df3354fcS[email protected] } else { 1577df3354fcS[email protected] // disconnnect, authentication not good enough 1578df3354fcS[email protected] hci_disconnect_security_block(handle); 1579df3354fcS[email protected] } 1580df3354fcS[email protected] break; 1581df3354fcS[email protected] 1582df3354fcS[email protected] default: 1583df3354fcS[email protected] break; 1584df3354fcS[email protected] } 1585f85a9399S[email protected] } 1586f85a9399S[email protected] break; 158709e9d05bSMatthias Ringwald #endif 1588f85a9399S[email protected] 1589afde0c52Smatthias.ringwald default: 1590afde0c52Smatthias.ringwald break; 1591afde0c52Smatthias.ringwald } 1592afde0c52Smatthias.ringwald 1593bd63148eS[email protected] l2cap_run(); 15941e6aba47Smatthias.ringwald } 15951e6aba47Smatthias.ringwald 1596e74c5f58SMatthias 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){ 15974cf56b4aSmatthias.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." 15982b360848Smatthias.ringwald if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { 15992b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].handle = handle; 16002b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].code = code; 16012b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].sig_id = sig_id; 1602e74c5f58SMatthias Ringwald signaling_responses[signaling_responses_pending].cid = cid; 16032b360848Smatthias.ringwald signaling_responses[signaling_responses_pending].data = data; 16042b360848Smatthias.ringwald signaling_responses_pending++; 16052b360848Smatthias.ringwald l2cap_run(); 16062b360848Smatthias.ringwald } 16072b360848Smatthias.ringwald } 16082b360848Smatthias.ringwald 160909e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 161009e9d05bSMatthias Ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){ 161109e9d05bSMatthias Ringwald channel->remote_sig_id = identifier; 161209e9d05bSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 161309e9d05bSMatthias Ringwald l2cap_run(); 161409e9d05bSMatthias Ringwald } 161509e9d05bSMatthias Ringwald 1616b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){ 1617645658c9Smatthias.ringwald 16189da54300S[email protected] // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid); 1619645658c9Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 1620645658c9Smatthias.ringwald if (!service) { 1621645658c9Smatthias.ringwald // 0x0002 PSM not supported 1622e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0002); 1623645658c9Smatthias.ringwald return; 1624645658c9Smatthias.ringwald } 1625645658c9Smatthias.ringwald 16265061f3afS[email protected] hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 1627645658c9Smatthias.ringwald if (!hci_connection) { 16282b360848Smatthias.ringwald // 16299da54300S[email protected] log_error("no hci_connection for handle %u", handle); 1630645658c9Smatthias.ringwald return; 1631645658c9Smatthias.ringwald } 16322bd8b7e7S[email protected] 1633645658c9Smatthias.ringwald // alloc structure 16349da54300S[email protected] // log_info("l2cap_handle_connection_request register channel"); 1635da144af5SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC, 1636da144af5SMatthias Ringwald psm, service->mtu, service->required_security_level); 16372b360848Smatthias.ringwald if (!channel){ 16382b360848Smatthias.ringwald // 0x0004 No resources available 1639e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0004); 16402b360848Smatthias.ringwald return; 16412b360848Smatthias.ringwald } 1642da144af5SMatthias Ringwald 1643fc64f94aSMatthias Ringwald channel->con_handle = handle; 1644b35f641cSmatthias.ringwald channel->remote_cid = source_cid; 1645b1988dceSmatthias.ringwald channel->remote_sig_id = sig_id; 1646645658c9Smatthias.ringwald 1647f53da564S[email protected] // limit local mtu to max acl packet length - l2cap header 16482985cb84Smatthias.ringwald if (channel->local_mtu > l2cap_max_mtu()) { 16492985cb84Smatthias.ringwald channel->local_mtu = l2cap_max_mtu(); 16509775e25bSmatthias.ringwald } 16519775e25bSmatthias.ringwald 1652645658c9Smatthias.ringwald // set initial state 1653df3354fcS[email protected] channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; 1654a24785d0SMatthias Ringwald channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND | L2CAP_CHANNEL_STATE_VAR_INCOMING); 1655e405ae81Smatthias.ringwald 1656645658c9Smatthias.ringwald // add to connections list 1657665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel); 1658645658c9Smatthias.ringwald 1659f85a9399S[email protected] // assert security requirements 16601eb2563eS[email protected] gap_request_security_level(handle, channel->required_security_level); 1661e405ae81Smatthias.ringwald } 1662645658c9Smatthias.ringwald 1663ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){ 1664e0abb8e7S[email protected] log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid); 1665b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 1666e405ae81Smatthias.ringwald if (!channel) { 1667ce8f182eSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 1668e405ae81Smatthias.ringwald return; 1669e405ae81Smatthias.ringwald } 1670e405ae81Smatthias.ringwald 167143ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 167243ec931dSMatthias Ringwald // configure L2CAP Basic mode 167343ec931dSMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 167443ec931dSMatthias Ringwald #endif 167543ec931dSMatthias Ringwald 1676552d92a1Smatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 1677e405ae81Smatthias.ringwald 1678552d92a1Smatthias.ringwald // process 1679552d92a1Smatthias.ringwald l2cap_run(); 1680e405ae81Smatthias.ringwald } 1681645658c9Smatthias.ringwald 168243ec931dSMatthias Ringwald 168343ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 16842b70d705SMatthias Ringwald uint8_t l2cap_accept_ertm_connection(uint16_t local_cid, int ertm_mandatory, uint8_t max_transmit, 1685501329faSMatthias Ringwald uint16_t retransmission_timeout_ms, uint16_t monitor_timeout_ms, uint8_t num_tx_buffers, uint8_t num_rx_buffers, uint8_t * buffer, uint32_t size){ 1686501329faSMatthias Ringwald 168743ec931dSMatthias Ringwald log_info("L2CAP_ACCEPT_ERTM_CONNECTION local_cid 0x%x", local_cid); 168843ec931dSMatthias Ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid); 168943ec931dSMatthias Ringwald if (!channel) { 169043ec931dSMatthias Ringwald log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid); 16912b70d705SMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 169243ec931dSMatthias Ringwald } 169343ec931dSMatthias Ringwald 16942b70d705SMatthias Ringwald // validate local config 16952b70d705SMatthias Ringwald uint8_t result = l2cap_ertm_validate_local_config(max_transmit, retransmission_timeout_ms, monitor_timeout_ms, num_tx_buffers, num_rx_buffers, buffer, size); 16962b70d705SMatthias Ringwald if (result) return result; 16972b70d705SMatthias Ringwald 169843ec931dSMatthias Ringwald // configure L2CAP ERTM 16997b181629SMatthias Ringwald l2cap_ertm_configure_channel(channel, ertm_mandatory, max_transmit, retransmission_timeout_ms, monitor_timeout_ms, num_tx_buffers, num_rx_buffers, buffer, size); 170043ec931dSMatthias Ringwald 17017b181629SMatthias Ringwald // continue 170243ec931dSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT; 170343ec931dSMatthias Ringwald 170443ec931dSMatthias Ringwald // process 170543ec931dSMatthias Ringwald l2cap_run(); 17062b70d705SMatthias Ringwald 17072b70d705SMatthias Ringwald return ERROR_CODE_SUCCESS; 170843ec931dSMatthias Ringwald } 170943ec931dSMatthias Ringwald #endif 171043ec931dSMatthias Ringwald 171143ec931dSMatthias Ringwald 17127ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){ 17137ef6a7bbSMatthias Ringwald log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid); 1714b35f641cSmatthias.ringwald l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid); 1715e405ae81Smatthias.ringwald if (!channel) { 1716ce8f182eSMatthias Ringwald log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid); 1717e405ae81Smatthias.ringwald return; 1718e405ae81Smatthias.ringwald } 1719e7ff783cSmatthias.ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 17207ef6a7bbSMatthias Ringwald channel->reason = 0x04; // no resources available 1721e7ff783cSmatthias.ringwald l2cap_run(); 1722645658c9Smatthias.ringwald } 1723645658c9Smatthias.ringwald 17247f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){ 1725b1988dceSmatthias.ringwald 1726b1988dceSmatthias.ringwald channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 1727b1988dceSmatthias.ringwald 1728f8fbdce0SMatthias Ringwald uint16_t flags = little_endian_read_16(command, 6); 172963a7246aSmatthias.ringwald if (flags & 1) { 173063a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT); 173163a7246aSmatthias.ringwald } 173263a7246aSmatthias.ringwald 17332784b77dSmatthias.ringwald // accept the other's configuration options 1734f8fbdce0SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 17353de7c0caSmatthias.ringwald uint16_t pos = 8; 17363de7c0caSmatthias.ringwald while (pos < end_pos){ 173763a7246aSmatthias.ringwald uint8_t option_hint = command[pos] >> 7; 173863a7246aSmatthias.ringwald uint8_t option_type = command[pos] & 0x7f; 173963a7246aSmatthias.ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 174063a7246aSmatthias.ringwald pos++; 17411dc511deSmatthias.ringwald uint8_t length = command[pos++]; 17421dc511deSmatthias.ringwald // MTU { type(8): 1, len(8):2, MTU(16) } 174363a7246aSmatthias.ringwald if (option_type == 1 && length == 2){ 1744f8fbdce0SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos); 17459d139fbaSMatthias Ringwald if (channel->remote_mtu > l2cap_max_mtu()){ 17469d139fbaSMatthias Ringwald log_info("Remote MTU %u larger than outgoing buffer, only using MTU = %u", channel->remote_mtu, l2cap_max_mtu()); 17479d139fbaSMatthias Ringwald channel->remote_mtu = l2cap_max_mtu(); 17489d139fbaSMatthias Ringwald } 174963a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 175063a7246aSmatthias.ringwald } 17510fe7a9d0S[email protected] // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)} 17520fe7a9d0S[email protected] if (option_type == 2 && length == 2){ 1753f8fbdce0SMatthias Ringwald channel->flush_timeout = little_endian_read_16(command, pos); 17540fe7a9d0S[email protected] } 1755f2fa388dSMatthias Ringwald 17566dca2a0cSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 17576dca2a0cSMatthias Ringwald // Retransmission and Flow Control Option 17586dca2a0cSMatthias Ringwald if (option_type == 4 && length == 9){ 17593232a1c6SMatthias Ringwald l2cap_channel_mode_t mode = (l2cap_channel_mode_t) command[pos]; 1760ac8f1300SMatthias Ringwald switch(channel->mode){ 1761ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION: 176225cd60d3SMatthias Ringwald // Store remote config 1763bbc0a9e7SMatthias Ringwald channel->remote_tx_window_size = command[pos+1]; 1764bbc0a9e7SMatthias Ringwald channel->remote_max_transmit = command[pos+2]; 1765bbc0a9e7SMatthias Ringwald channel->remote_retransmission_timeout_ms = little_endian_read_16(command, pos + 3); 1766bbc0a9e7SMatthias Ringwald channel->remote_monitor_timeout_ms = little_endian_read_16(command, pos + 5); 1767bbc0a9e7SMatthias Ringwald log_info("FC&C config: tx window: %u, max transmit %u, retrans timeout %u, monitor timeout %u", 1768bbc0a9e7SMatthias Ringwald channel->remote_tx_window_size, 1769bbc0a9e7SMatthias Ringwald channel->remote_max_transmit, 1770bbc0a9e7SMatthias Ringwald channel->remote_retransmission_timeout_ms, 1771bbc0a9e7SMatthias Ringwald channel->remote_monitor_timeout_ms); 1772bbc0a9e7SMatthias Ringwald // we store remote MPS in remote_mtu, might need to get re-evaluated 1773bbc0a9e7SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, pos + 7); 177425cd60d3SMatthias Ringwald // If ERTM mandatory, but remote doens't offer ERTM -> disconnect 177525cd60d3SMatthias Ringwald if (channel->ertm_mandatory && mode != L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 177625cd60d3SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1777ac8f1300SMatthias Ringwald } else { 1778ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 1779ac8f1300SMatthias Ringwald } 1780ac8f1300SMatthias Ringwald break; 1781ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_BASIC: 1782ac8f1300SMatthias Ringwald switch (mode){ 1783ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION: 1784ac8f1300SMatthias Ringwald // remote asks for ERTM, but we want basic mode. disconnect if this happens a second time 1785ac8f1300SMatthias Ringwald if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED){ 1786ac8f1300SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1787ac8f1300SMatthias Ringwald } 1788ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED); 1789ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED); 1790ac8f1300SMatthias Ringwald break; 1791ac8f1300SMatthias Ringwald default: // case L2CAP_CHANNEL_MODE_BASIC: 1792ac8f1300SMatthias Ringwald // TODO store and evaluate configuration 1793ac8f1300SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU); 1794ac8f1300SMatthias Ringwald break; 1795ac8f1300SMatthias Ringwald } 1796ac8f1300SMatthias Ringwald break; 1797ac8f1300SMatthias Ringwald default: 1798ac8f1300SMatthias Ringwald break; 1799ac8f1300SMatthias Ringwald } 18003232a1c6SMatthias Ringwald } 18016dca2a0cSMatthias Ringwald #endif 180263a7246aSmatthias.ringwald // check for unknown options 180363a7246aSmatthias.ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 1804c177a91cS[email protected] log_info("l2cap cid %u, unknown options", channel->local_cid); 180563a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 18061dc511deSmatthias.ringwald } 18071dc511deSmatthias.ringwald pos += length; 18081dc511deSmatthias.ringwald } 18092784b77dSmatthias.ringwald } 18102784b77dSmatthias.ringwald 1811f2fa388dSMatthias Ringwald static void l2cap_signaling_handle_configure_response(l2cap_channel_t *channel, uint8_t result, uint8_t *command){ 1812f2fa388dSMatthias Ringwald log_info("l2cap_signaling_handle_configure_response"); 18133232a1c6SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 18143232a1c6SMatthias Ringwald uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 1815f2fa388dSMatthias Ringwald uint16_t pos = 10; 18163232a1c6SMatthias Ringwald while (pos < end_pos){ 18173232a1c6SMatthias Ringwald uint8_t option_hint = command[pos] >> 7; 18183232a1c6SMatthias Ringwald uint8_t option_type = command[pos] & 0x7f; 18193232a1c6SMatthias Ringwald log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type); 18203232a1c6SMatthias Ringwald pos++; 18213232a1c6SMatthias Ringwald uint8_t length = command[pos++]; 18223232a1c6SMatthias Ringwald 18233232a1c6SMatthias Ringwald // Retransmission and Flow Control Option 18243232a1c6SMatthias Ringwald if (option_type == 4 && length == 9){ 1825ac8f1300SMatthias Ringwald switch (channel->mode){ 1826ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION: 1827f2fa388dSMatthias Ringwald if (channel->ertm_mandatory){ 1828ac8f1300SMatthias Ringwald // ?? 1829f2fa388dSMatthias Ringwald } else { 1830ac8f1300SMatthias Ringwald // On 'Reject - Unacceptable Parameters' to our optional ERTM request, fall back to BASIC mode 1831ac8f1300SMatthias Ringwald if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){ 1832f2fa388dSMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 18333232a1c6SMatthias Ringwald } 18343232a1c6SMatthias Ringwald } 1835ac8f1300SMatthias Ringwald break; 1836ac8f1300SMatthias Ringwald case L2CAP_CHANNEL_MODE_BASIC: 1837ac8f1300SMatthias Ringwald if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){ 1838ac8f1300SMatthias Ringwald // On 'Reject - Unacceptable Parameters' to our Basic mode request, disconnect 1839ac8f1300SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1840ac8f1300SMatthias Ringwald } 1841ac8f1300SMatthias Ringwald break; 1842ac8f1300SMatthias Ringwald default: 1843ac8f1300SMatthias Ringwald break; 18443232a1c6SMatthias Ringwald } 1845f2fa388dSMatthias Ringwald } 18463232a1c6SMatthias Ringwald 18473232a1c6SMatthias Ringwald // check for unknown options 18483232a1c6SMatthias Ringwald if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){ 18493232a1c6SMatthias Ringwald log_info("l2cap cid %u, unknown options", channel->local_cid); 18503232a1c6SMatthias Ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID); 18513232a1c6SMatthias Ringwald } 18523232a1c6SMatthias Ringwald 18533232a1c6SMatthias Ringwald pos += length; 18543232a1c6SMatthias Ringwald } 18553232a1c6SMatthias Ringwald #endif 18563232a1c6SMatthias Ringwald } 18573232a1c6SMatthias Ringwald 1858fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){ 18599da54300S[email protected] // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var); 186073cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0; 186173cf2b3dSmatthias.ringwald if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0; 1862019f9b43SMatthias Ringwald // addition check that fixes re-entrance issue causing l2cap event channel opened twice 1863019f9b43SMatthias Ringwald if (channel->state == L2CAP_STATE_OPEN) return 0; 1864fa8473a4Smatthias.ringwald return 1; 1865fa8473a4Smatthias.ringwald } 1866fa8473a4Smatthias.ringwald 1867fa8473a4Smatthias.ringwald 18687f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){ 18691e6aba47Smatthias.ringwald 187000d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 187100d93d79Smatthias.ringwald uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 187238e5900eSmatthias.ringwald uint16_t result = 0; 18731e6aba47Smatthias.ringwald 18749da54300S[email protected] log_info("L2CAP signaling handler code %u, state %u", code, channel->state); 1875b35f641cSmatthias.ringwald 18769a011532Smatthias.ringwald // handle DISCONNECT REQUESTS seperately 18779a011532Smatthias.ringwald if (code == DISCONNECTION_REQUEST){ 18789a011532Smatthias.ringwald switch (channel->state){ 1879fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 18809a011532Smatthias.ringwald case L2CAP_STATE_OPEN: 18812b83fb7dSmatthias.ringwald case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST: 18829a011532Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 18839a011532Smatthias.ringwald l2cap_handle_disconnect_request(channel, identifier); 18849a011532Smatthias.ringwald break; 18859a011532Smatthias.ringwald 18869a011532Smatthias.ringwald default: 18879a011532Smatthias.ringwald // ignore in other states 18889a011532Smatthias.ringwald break; 18899a011532Smatthias.ringwald } 18909a011532Smatthias.ringwald return; 18919a011532Smatthias.ringwald } 18929a011532Smatthias.ringwald 189356081214Smatthias.ringwald // @STATEMACHINE(l2cap) 18941e6aba47Smatthias.ringwald switch (channel->state) { 18951e6aba47Smatthias.ringwald 18961e6aba47Smatthias.ringwald case L2CAP_STATE_WAIT_CONNECT_RSP: 18971e6aba47Smatthias.ringwald switch (code){ 18981e6aba47Smatthias.ringwald case CONNECTION_RESPONSE: 18995932bd7cS[email protected] l2cap_stop_rtx(channel); 1900f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 190138e5900eSmatthias.ringwald switch (result) { 190238e5900eSmatthias.ringwald case 0: 1903169f8b28Smatthias.ringwald // successful connection 1904f8fbdce0SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 1905fa8473a4Smatthias.ringwald channel->state = L2CAP_STATE_CONFIG; 190628ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 190738e5900eSmatthias.ringwald break; 190838e5900eSmatthias.ringwald case 1: 19095932bd7cS[email protected] // connection pending. get some coffee, but start the ERTX 19105932bd7cS[email protected] l2cap_start_ertx(channel); 191138e5900eSmatthias.ringwald break; 191238e5900eSmatthias.ringwald default: 1913eb920dbeSmatthias.ringwald // channel closed 1914eb920dbeSmatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 1915f32b992eSmatthias.ringwald // map l2cap connection response result to BTstack status enumeration 191638e5900eSmatthias.ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result); 1917eb920dbeSmatthias.ringwald 1918eb920dbeSmatthias.ringwald // drop link key if security block 1919eb920dbeSmatthias.ringwald if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){ 192015a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(channel->address); 1921eb920dbeSmatthias.ringwald } 1922eb920dbeSmatthias.ringwald 1923eb920dbeSmatthias.ringwald // discard channel 1924665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 1925d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 192638e5900eSmatthias.ringwald break; 19271e6aba47Smatthias.ringwald } 19281e6aba47Smatthias.ringwald break; 192938e5900eSmatthias.ringwald 193038e5900eSmatthias.ringwald default: 19311e6aba47Smatthias.ringwald //@TODO: implement other signaling packets 193238e5900eSmatthias.ringwald break; 19331e6aba47Smatthias.ringwald } 19341e6aba47Smatthias.ringwald break; 19351e6aba47Smatthias.ringwald 1936fa8473a4Smatthias.ringwald case L2CAP_STATE_CONFIG: 1937f8fbdce0SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 1938ae280e73Smatthias.ringwald switch (code) { 1939ae280e73Smatthias.ringwald case CONFIGURE_REQUEST: 194028ca2b46S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP); 1941ae280e73Smatthias.ringwald l2cap_signaling_handle_configure_request(channel, command); 194263a7246aSmatthias.ringwald if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){ 194363a7246aSmatthias.ringwald // only done if continuation not set 194463a7246aSmatthias.ringwald channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ); 194563a7246aSmatthias.ringwald } 1946ae280e73Smatthias.ringwald break; 19471e6aba47Smatthias.ringwald case CONFIGURE_RESPONSE: 19485932bd7cS[email protected] l2cap_stop_rtx(channel); 1949f2fa388dSMatthias Ringwald l2cap_signaling_handle_configure_response(channel, result, command); 19505932bd7cS[email protected] switch (result){ 19515932bd7cS[email protected] case 0: // success 19525932bd7cS[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP); 19535932bd7cS[email protected] break; 19545932bd7cS[email protected] case 4: // pending 19555932bd7cS[email protected] l2cap_start_ertx(channel); 19565932bd7cS[email protected] break; 19575932bd7cS[email protected] default: 1958a32d6a03SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 1959a32d6a03SMatthias Ringwald if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->ertm_mandatory){ 1960a32d6a03SMatthias Ringwald // remote does not offer ertm but it's required 1961a32d6a03SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 1962a32d6a03SMatthias Ringwald break; 1963a32d6a03SMatthias Ringwald } 1964a32d6a03SMatthias Ringwald #endif 1965fe9d8984S[email protected] // retry on negative result 1966fe9d8984S[email protected] channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ); 1967fe9d8984S[email protected] break; 1968fe9d8984S[email protected] } 19695a67bd4aSmatthias.ringwald break; 19705a67bd4aSmatthias.ringwald default: 19715a67bd4aSmatthias.ringwald break; 19721e6aba47Smatthias.ringwald } 1973fa8473a4Smatthias.ringwald if (l2cap_channel_ready_for_open(channel)){ 1974fa8473a4Smatthias.ringwald // for open: 19755a67bd4aSmatthias.ringwald channel->state = L2CAP_STATE_OPEN; 1976fa8473a4Smatthias.ringwald l2cap_emit_channel_opened(channel, 0); 1977c8e4258aSmatthias.ringwald } 1978c8e4258aSmatthias.ringwald break; 1979f62db1e3Smatthias.ringwald 1980f62db1e3Smatthias.ringwald case L2CAP_STATE_WAIT_DISCONNECT: 1981f62db1e3Smatthias.ringwald switch (code) { 1982f62db1e3Smatthias.ringwald case DISCONNECTION_RESPONSE: 198327a923d0Smatthias.ringwald l2cap_finialize_channel_close(channel); 198427a923d0Smatthias.ringwald break; 19855a67bd4aSmatthias.ringwald default: 19865a67bd4aSmatthias.ringwald //@TODO: implement other signaling packets 19875a67bd4aSmatthias.ringwald break; 198827a923d0Smatthias.ringwald } 198927a923d0Smatthias.ringwald break; 199084836b65Smatthias.ringwald 199184836b65Smatthias.ringwald case L2CAP_STATE_CLOSED: 199284836b65Smatthias.ringwald // @TODO handle incoming requests 199384836b65Smatthias.ringwald break; 199484836b65Smatthias.ringwald 199584836b65Smatthias.ringwald case L2CAP_STATE_OPEN: 199684836b65Smatthias.ringwald //@TODO: implement other signaling packets, e.g. re-configure 199784836b65Smatthias.ringwald break; 199810642e45Smatthias.ringwald default: 199910642e45Smatthias.ringwald break; 200027a923d0Smatthias.ringwald } 20019da54300S[email protected] // log_info("new state %u", channel->state); 200227a923d0Smatthias.ringwald } 200327a923d0Smatthias.ringwald 200400d93d79Smatthias.ringwald 20057f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){ 200600d93d79Smatthias.ringwald 20071b9cb13dSMatthias Ringwald btstack_linked_list_iterator_t it; 20081b9cb13dSMatthias Ringwald 200900d93d79Smatthias.ringwald // get code, signalind identifier and command len 201000d93d79Smatthias.ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 201100d93d79Smatthias.ringwald uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET]; 201200d93d79Smatthias.ringwald 20131b9cb13dSMatthias Ringwald // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_RESPONSE 20141b9cb13dSMatthias Ringwald if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_RESPONSE){ 2015e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN); 201600d93d79Smatthias.ringwald return; 201700d93d79Smatthias.ringwald } 201800d93d79Smatthias.ringwald 201900d93d79Smatthias.ringwald // general commands without an assigned channel 202000d93d79Smatthias.ringwald switch(code) { 202100d93d79Smatthias.ringwald 202200d93d79Smatthias.ringwald case CONNECTION_REQUEST: { 2023f8fbdce0SMatthias Ringwald uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 2024f8fbdce0SMatthias Ringwald uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 202500d93d79Smatthias.ringwald l2cap_handle_connection_request(handle, sig_id, psm, source_cid); 20262b83fb7dSmatthias.ringwald return; 202700d93d79Smatthias.ringwald } 202800d93d79Smatthias.ringwald 20292b360848Smatthias.ringwald case ECHO_REQUEST: 2030e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, code, sig_id, 0, 0); 20312b83fb7dSmatthias.ringwald return; 203200d93d79Smatthias.ringwald 203300d93d79Smatthias.ringwald case INFORMATION_REQUEST: { 2034f8fbdce0SMatthias Ringwald uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 2035e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, code, sig_id, 0, infoType); 20362b83fb7dSmatthias.ringwald return; 203700d93d79Smatthias.ringwald } 203800d93d79Smatthias.ringwald 20391b9cb13dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 20401b9cb13dSMatthias Ringwald case INFORMATION_RESPONSE: { 20411b9cb13dSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 20421b9cb13dSMatthias Ringwald if (!connection) return; 20431b9cb13dSMatthias Ringwald uint16_t info_type = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 20441b9cb13dSMatthias Ringwald uint16_t result = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2); 20451b9cb13dSMatthias Ringwald if (result != 0) return; 2046543b84e4SMatthias Ringwald if (info_type != 0x02) return; 20471b9cb13dSMatthias Ringwald connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_DONE; 20481b9cb13dSMatthias Ringwald connection->l2cap_state.extended_feature_mask = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4); 2049543b84e4SMatthias Ringwald log_info("extended features mask 0x%02x", connection->l2cap_state.extended_feature_mask); 20501b9cb13dSMatthias Ringwald // trigger connection request 20511b9cb13dSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 20521b9cb13dSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 20531b9cb13dSMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2054543b84e4SMatthias Ringwald if (channel->con_handle != handle) continue; 2055543b84e4SMatthias Ringwald // bail if ERTM was requested but is not supported 2056543b84e4SMatthias Ringwald if ((channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) && ((connection->l2cap_state.extended_feature_mask & 0x08) == 0)){ 2057f073f086SMatthias Ringwald if (channel->ertm_mandatory){ 2058543b84e4SMatthias Ringwald // channel closed 2059543b84e4SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 2060543b84e4SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 2061543b84e4SMatthias Ringwald l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_ERTM_NOT_SUPPORTD); 2062543b84e4SMatthias Ringwald // discard channel 2063543b84e4SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 2064543b84e4SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 2065543b84e4SMatthias Ringwald continue; 2066f073f086SMatthias Ringwald } else { 2067f073f086SMatthias Ringwald // fallback to Basic mode 2068f073f086SMatthias Ringwald channel->mode = L2CAP_CHANNEL_MODE_BASIC; 2069f073f086SMatthias Ringwald } 2070543b84e4SMatthias Ringwald } 2071543b84e4SMatthias Ringwald // start connecting 20721b9cb13dSMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES){ 20731b9cb13dSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 20741b9cb13dSMatthias Ringwald } 207552606043SMatthias Ringwald // respond to connection request 207652606043SMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES){ 207752606043SMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 207852606043SMatthias Ringwald l2cap_emit_incoming_connection(channel); 207952606043SMatthias Ringwald } 20801b9cb13dSMatthias Ringwald } 20811b9cb13dSMatthias Ringwald return; 20821b9cb13dSMatthias Ringwald } 20831b9cb13dSMatthias Ringwald #endif 20841b9cb13dSMatthias Ringwald 208500d93d79Smatthias.ringwald default: 208600d93d79Smatthias.ringwald break; 208700d93d79Smatthias.ringwald } 208800d93d79Smatthias.ringwald 208900d93d79Smatthias.ringwald 209000d93d79Smatthias.ringwald // Get potential destination CID 2091f8fbdce0SMatthias Ringwald uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET); 209200d93d79Smatthias.ringwald 209300d93d79Smatthias.ringwald // Find channel for this sig_id and connection handle 2094665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_channels); 2095665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2096665d90f2SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 2097fc64f94aSMatthias Ringwald if (channel->con_handle != handle) continue; 209800d93d79Smatthias.ringwald if (code & 1) { 2099b1988dceSmatthias.ringwald // match odd commands (responses) by previous signaling identifier 2100b1988dceSmatthias.ringwald if (channel->local_sig_id == sig_id) { 210100d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 21024e32727eSmatthias.ringwald break; 210300d93d79Smatthias.ringwald } 210400d93d79Smatthias.ringwald } else { 2105b1988dceSmatthias.ringwald // match even commands (requests) by local channel id 210600d93d79Smatthias.ringwald if (channel->local_cid == dest_cid) { 210700d93d79Smatthias.ringwald l2cap_signaling_handler_channel(channel, command); 21084e32727eSmatthias.ringwald break; 210900d93d79Smatthias.ringwald } 211000d93d79Smatthias.ringwald } 211100d93d79Smatthias.ringwald } 211200d93d79Smatthias.ringwald } 211309e9d05bSMatthias Ringwald #endif 211400d93d79Smatthias.ringwald 2115e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 211609e9d05bSMatthias Ringwald 211709e9d05bSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){ 211809e9d05bSMatthias Ringwald uint8_t event[6]; 211909e9d05bSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE; 212009e9d05bSMatthias Ringwald event[1] = 4; 212109e9d05bSMatthias Ringwald little_endian_store_16(event, 2, con_handle); 212209e9d05bSMatthias Ringwald little_endian_store_16(event, 4, result); 212309e9d05bSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 212409e9d05bSMatthias Ringwald if (!l2cap_event_packet_handler) return; 212509e9d05bSMatthias Ringwald (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 212609e9d05bSMatthias Ringwald } 212709e9d05bSMatthias Ringwald 2128c48b2a2cSMatthias Ringwald // @returns valid 2129c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){ 2130e7d0c9aaSMatthias Ringwald hci_connection_t * connection; 2131c48b2a2cSMatthias Ringwald uint16_t result; 2132c48b2a2cSMatthias Ringwald uint8_t event[10]; 213383fd9c76SMatthias Ringwald 2134cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 2135a3dc965aSMatthias Ringwald btstack_linked_list_iterator_t it; 2136a3dc965aSMatthias Ringwald l2cap_channel_t * channel; 2137a3dc965aSMatthias Ringwald uint16_t local_cid; 213883fd9c76SMatthias Ringwald uint16_t le_psm; 213963f0ac45SMatthias Ringwald uint16_t new_credits; 214063f0ac45SMatthias Ringwald uint16_t credits_before; 2141e7d0c9aaSMatthias Ringwald l2cap_service_t * service; 214211cae19eSMilanka Ringwald uint16_t source_cid; 214383fd9c76SMatthias Ringwald #endif 214400d93d79Smatthias.ringwald 21451b8b8d05SMatthias Ringwald uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET]; 214623017473SMatthias Ringwald log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id); 21471b8b8d05SMatthias Ringwald 21481b8b8d05SMatthias Ringwald switch (code){ 214900d93d79Smatthias.ringwald 2150c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_RESPONSE: 2151c48b2a2cSMatthias Ringwald result = little_endian_read_16(command, 4); 2152ccf076adS[email protected] l2cap_emit_connection_parameter_update_response(handle, result); 2153ccf076adS[email protected] break; 2154da886c03S[email protected] 2155c48b2a2cSMatthias Ringwald case CONNECTION_PARAMETER_UPDATE_REQUEST: 2156e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 2157da886c03S[email protected] if (connection){ 21586d91fb6cSMatthias Ringwald if (connection->role != HCI_ROLE_MASTER){ 21596d91fb6cSMatthias Ringwald // reject command without notifying upper layer when not in master role 2160c48b2a2cSMatthias Ringwald return 0; 21616d91fb6cSMatthias Ringwald } 2162da886c03S[email protected] int update_parameter = 1; 2163a4c06b28SMatthias Ringwald le_connection_parameter_range_t existing_range; 21644ced4e8cSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 2165c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_min = little_endian_read_16(command,8); 2166c48b2a2cSMatthias Ringwald uint16_t le_conn_interval_max = little_endian_read_16(command,10); 2167c48b2a2cSMatthias Ringwald uint16_t le_conn_latency = little_endian_read_16(command,12); 2168c48b2a2cSMatthias Ringwald uint16_t le_supervision_timeout = little_endian_read_16(command,14); 2169da886c03S[email protected] 2170da886c03S[email protected] if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0; 2171da886c03S[email protected] if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0; 2172da886c03S[email protected] 2173da886c03S[email protected] if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0; 2174da886c03S[email protected] if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0; 2175da886c03S[email protected] 2176da886c03S[email protected] if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0; 2177da886c03S[email protected] if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0; 2178da886c03S[email protected] 2179da886c03S[email protected] if (update_parameter){ 2180da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE; 2181da886c03S[email protected] connection->le_conn_interval_min = le_conn_interval_min; 2182da886c03S[email protected] connection->le_conn_interval_max = le_conn_interval_max; 2183da886c03S[email protected] connection->le_conn_latency = le_conn_latency; 2184da886c03S[email protected] connection->le_supervision_timeout = le_supervision_timeout; 2185da886c03S[email protected] } else { 2186da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 2187da886c03S[email protected] } 2188c48b2a2cSMatthias Ringwald connection->le_con_param_update_identifier = sig_id; 2189da886c03S[email protected] } 2190da886c03S[email protected] 219133c40538SMatthias Ringwald if (!l2cap_event_packet_handler) break; 2192c48b2a2cSMatthias Ringwald 2193c48b2a2cSMatthias Ringwald event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST; 2194c48b2a2cSMatthias Ringwald event[1] = 8; 2195c48b2a2cSMatthias Ringwald memcpy(&event[2], &command[4], 8); 2196c48b2a2cSMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 219733c40538SMatthias Ringwald (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event)); 2198ccf076adS[email protected] break; 2199c48b2a2cSMatthias Ringwald 2200a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 2201a3dc965aSMatthias Ringwald 220263f0ac45SMatthias Ringwald case COMMAND_REJECT: 220363f0ac45SMatthias Ringwald // Find channel for this sig_id and connection handle 220463f0ac45SMatthias Ringwald channel = NULL; 220563f0ac45SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 220663f0ac45SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 220763f0ac45SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 220863f0ac45SMatthias Ringwald if (a_channel->con_handle != handle) continue; 220963f0ac45SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 221063f0ac45SMatthias Ringwald channel = a_channel; 221163f0ac45SMatthias Ringwald break; 221263f0ac45SMatthias Ringwald } 221363f0ac45SMatthias Ringwald if (!channel) break; 221463f0ac45SMatthias Ringwald 221563f0ac45SMatthias Ringwald // if received while waiting for le connection response, assume legacy device 221663f0ac45SMatthias Ringwald if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){ 221763f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 221863f0ac45SMatthias Ringwald // no official value for this, use: Connection refused – LE_PSM not supported - 0x0002 221944276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, 0x0002); 222063f0ac45SMatthias Ringwald 222163f0ac45SMatthias Ringwald // discard channel 222263f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 222363f0ac45SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 222463f0ac45SMatthias Ringwald break; 222563f0ac45SMatthias Ringwald } 222663f0ac45SMatthias Ringwald break; 222763f0ac45SMatthias Ringwald 2228e7d0c9aaSMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_REQUEST: 2229e7d0c9aaSMatthias Ringwald 2230e7d0c9aaSMatthias Ringwald // get hci connection, bail if not found (must not happen) 2231e7d0c9aaSMatthias Ringwald connection = hci_connection_for_handle(handle); 2232e7d0c9aaSMatthias Ringwald if (!connection) return 0; 2233e7d0c9aaSMatthias Ringwald 2234e7d0c9aaSMatthias Ringwald // check if service registered 2235e7d0c9aaSMatthias Ringwald le_psm = little_endian_read_16(command, 4); 2236e7d0c9aaSMatthias Ringwald service = l2cap_le_get_service(le_psm); 223711cae19eSMilanka Ringwald source_cid = little_endian_read_16(command, 6); 2238e7d0c9aaSMatthias Ringwald 2239e7d0c9aaSMatthias Ringwald if (service){ 2240e7d0c9aaSMatthias Ringwald if (source_cid < 0x40){ 2241e7d0c9aaSMatthias Ringwald // 0x0009 Connection refused - Invalid Source CID 2242e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0009); 2243e7d0c9aaSMatthias Ringwald return 1; 2244e7d0c9aaSMatthias Ringwald } 2245e7d0c9aaSMatthias Ringwald 2246e7d0c9aaSMatthias Ringwald // go through list of channels for this ACL connection and check if we get a match 2247e7d0c9aaSMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 2248e7d0c9aaSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 22491b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 22501b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 22511b8b8d05SMatthias Ringwald if (a_channel->remote_cid != source_cid) continue; 2252e7d0c9aaSMatthias Ringwald // 0x000a Connection refused - Source CID already allocated 2253e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x000a); 2254e7d0c9aaSMatthias Ringwald return 1; 2255e7d0c9aaSMatthias Ringwald } 2256e7d0c9aaSMatthias Ringwald 225783fd9c76SMatthias Ringwald // security: check encryption 225883fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_2){ 225983fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) == 0){ 226083fd9c76SMatthias Ringwald // 0x0008 Connection refused - insufficient encryption 2261e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0008); 226283fd9c76SMatthias Ringwald return 1; 226383fd9c76SMatthias Ringwald } 226483fd9c76SMatthias Ringwald // anything less than 16 byte key size is insufficient 226583fd9c76SMatthias Ringwald if (sm_encryption_key_size(handle) < 16){ 226683fd9c76SMatthias Ringwald // 0x0007 Connection refused – insufficient encryption key size 2267e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0007); 226883fd9c76SMatthias Ringwald return 1; 226983fd9c76SMatthias Ringwald } 227083fd9c76SMatthias Ringwald } 227183fd9c76SMatthias Ringwald 227283fd9c76SMatthias Ringwald // security: check authencation 227383fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_3){ 227483fd9c76SMatthias Ringwald if (!sm_authenticated(handle)){ 227583fd9c76SMatthias Ringwald // 0x0005 Connection refused – insufficient authentication 2276e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0005); 227783fd9c76SMatthias Ringwald return 1; 227883fd9c76SMatthias Ringwald } 227983fd9c76SMatthias Ringwald } 228083fd9c76SMatthias Ringwald 228183fd9c76SMatthias Ringwald // security: check authorization 228283fd9c76SMatthias Ringwald if (service->required_security_level >= LEVEL_4){ 228383fd9c76SMatthias Ringwald if (sm_authorization_state(handle) != AUTHORIZATION_GRANTED){ 228483fd9c76SMatthias Ringwald // 0x0006 Connection refused – insufficient authorization 2285e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0006); 228683fd9c76SMatthias Ringwald return 1; 228783fd9c76SMatthias Ringwald } 228883fd9c76SMatthias Ringwald } 2289e7d0c9aaSMatthias Ringwald 2290e7d0c9aaSMatthias Ringwald // allocate channel 22911b8b8d05SMatthias Ringwald channel = l2cap_create_channel_entry(service->packet_handler, connection->address, 2292e7d0c9aaSMatthias Ringwald BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level); 2293e7d0c9aaSMatthias Ringwald if (!channel){ 2294e7d0c9aaSMatthias Ringwald // 0x0004 Connection refused – no resources available 2295e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0004); 2296e7d0c9aaSMatthias Ringwald return 1; 2297e7d0c9aaSMatthias Ringwald } 2298e7d0c9aaSMatthias Ringwald 2299e7d0c9aaSMatthias Ringwald channel->con_handle = handle; 2300e7d0c9aaSMatthias Ringwald channel->remote_cid = source_cid; 2301e7d0c9aaSMatthias Ringwald channel->remote_sig_id = sig_id; 23027f107edaSMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, 8); 23037f107edaSMatthias Ringwald channel->remote_mps = little_endian_read_16(command, 10); 23047f107edaSMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, 12); 2305e7d0c9aaSMatthias Ringwald 2306e7d0c9aaSMatthias Ringwald // set initial state 2307e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 2308c99bb618SMatthias Ringwald channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING; 2309e7d0c9aaSMatthias Ringwald 2310e7d0c9aaSMatthias Ringwald // add to connections list 2311e7d0c9aaSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 2312e7d0c9aaSMatthias Ringwald 2313e7d0c9aaSMatthias Ringwald // post connection request event 231444276248SMatthias Ringwald l2cap_emit_le_incoming_connection(channel); 2315e7d0c9aaSMatthias Ringwald 2316e7d0c9aaSMatthias Ringwald } else { 2317e7d0c9aaSMatthias Ringwald // Connection refused – LE_PSM not supported 2318e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0002); 2319e7d0c9aaSMatthias Ringwald } 2320e7d0c9aaSMatthias Ringwald break; 23211b8b8d05SMatthias Ringwald 23221b8b8d05SMatthias Ringwald case LE_CREDIT_BASED_CONNECTION_RESPONSE: 23231b8b8d05SMatthias Ringwald // Find channel for this sig_id and connection handle 23241b8b8d05SMatthias Ringwald channel = NULL; 23251b8b8d05SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 23261b8b8d05SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 23271b8b8d05SMatthias Ringwald l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 23281b8b8d05SMatthias Ringwald if (a_channel->con_handle != handle) continue; 23291b8b8d05SMatthias Ringwald if (a_channel->local_sig_id != sig_id) continue; 23301b8b8d05SMatthias Ringwald channel = a_channel; 23311b8b8d05SMatthias Ringwald break; 23321b8b8d05SMatthias Ringwald } 23331b8b8d05SMatthias Ringwald if (!channel) break; 23341b8b8d05SMatthias Ringwald 23351b8b8d05SMatthias Ringwald // cid + 0 23361b8b8d05SMatthias Ringwald result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8); 23371b8b8d05SMatthias Ringwald if (result){ 23381b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 23391b8b8d05SMatthias Ringwald // map l2cap connection response result to BTstack status enumeration 234044276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 23411b8b8d05SMatthias Ringwald 23421b8b8d05SMatthias Ringwald // discard channel 234363f0ac45SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 23441b8b8d05SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 23451b8b8d05SMatthias Ringwald break; 23461b8b8d05SMatthias Ringwald } 23471b8b8d05SMatthias Ringwald 23481b8b8d05SMatthias Ringwald // success 23491b8b8d05SMatthias Ringwald channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 23501b8b8d05SMatthias Ringwald channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 23511b8b8d05SMatthias Ringwald channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4); 23521b8b8d05SMatthias Ringwald channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6); 23531b8b8d05SMatthias Ringwald channel->state = L2CAP_STATE_OPEN; 235444276248SMatthias Ringwald l2cap_emit_le_channel_opened(channel, result); 23551b8b8d05SMatthias Ringwald break; 23561b8b8d05SMatthias Ringwald 235785aeef60SMatthias Ringwald case LE_FLOW_CONTROL_CREDIT: 235885aeef60SMatthias Ringwald // find channel 235985aeef60SMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 236085aeef60SMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 236163f0ac45SMatthias Ringwald if (!channel) { 236263f0ac45SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 236363f0ac45SMatthias Ringwald break; 236463f0ac45SMatthias Ringwald } 236563f0ac45SMatthias Ringwald new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2); 236663f0ac45SMatthias Ringwald credits_before = channel->credits_outgoing; 236763f0ac45SMatthias Ringwald channel->credits_outgoing += new_credits; 236863f0ac45SMatthias Ringwald // check for credit overrun 236963f0ac45SMatthias Ringwald if (credits_before > channel->credits_outgoing){ 237063f0ac45SMatthias Ringwald log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid); 237163f0ac45SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 237263f0ac45SMatthias Ringwald break; 237363f0ac45SMatthias Ringwald } 237463f0ac45SMatthias Ringwald log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing); 237585aeef60SMatthias Ringwald break; 237685aeef60SMatthias Ringwald 2377828a7f7aSMatthias Ringwald case DISCONNECTION_REQUEST: 2378828a7f7aSMatthias Ringwald // find channel 2379828a7f7aSMatthias Ringwald local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0); 2380828a7f7aSMatthias Ringwald channel = l2cap_le_get_channel_for_local_cid(local_cid); 238163f34e00SMatthias Ringwald if (!channel) { 238263f34e00SMatthias Ringwald log_error("l2cap: no channel for cid 0x%02x", local_cid); 238363f34e00SMatthias Ringwald break; 238463f34e00SMatthias Ringwald } 2385828a7f7aSMatthias Ringwald channel->remote_sig_id = sig_id; 2386828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE; 2387828a7f7aSMatthias Ringwald break; 2388828a7f7aSMatthias Ringwald 2389a3dc965aSMatthias Ringwald #endif 2390a3dc965aSMatthias Ringwald 239163f0ac45SMatthias Ringwald case DISCONNECTION_RESPONSE: 239263f0ac45SMatthias Ringwald break; 239363f0ac45SMatthias Ringwald 2394c48b2a2cSMatthias Ringwald default: 2395c48b2a2cSMatthias Ringwald // command unknown -> reject command 2396c48b2a2cSMatthias Ringwald return 0; 2397ccf076adS[email protected] } 2398c48b2a2cSMatthias Ringwald return 1; 2399c48b2a2cSMatthias Ringwald } 2400e7d0c9aaSMatthias Ringwald #endif 2401c48b2a2cSMatthias Ringwald 2402c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){ 24039ec2630cSMatthias Ringwald UNUSED(packet_type); 24049ec2630cSMatthias Ringwald UNUSED(channel); 2405c48b2a2cSMatthias Ringwald 240609e9d05bSMatthias Ringwald l2cap_channel_t * l2cap_channel; 2407f3963406SMatthias Ringwald UNUSED(l2cap_channel); 240809e9d05bSMatthias Ringwald 2409c48b2a2cSMatthias Ringwald // Get Channel ID 2410c48b2a2cSMatthias Ringwald uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet); 2411c48b2a2cSMatthias Ringwald hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet); 2412c48b2a2cSMatthias Ringwald 2413c48b2a2cSMatthias Ringwald switch (channel_id) { 2414c48b2a2cSMatthias Ringwald 241509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 2416c48b2a2cSMatthias Ringwald case L2CAP_CID_SIGNALING: { 2417c48b2a2cSMatthias Ringwald uint16_t command_offset = 8; 2418c48b2a2cSMatthias Ringwald while (command_offset < size) { 2419c48b2a2cSMatthias Ringwald // handle signaling commands 2420c48b2a2cSMatthias Ringwald l2cap_signaling_handler_dispatch(handle, &packet[command_offset]); 2421c48b2a2cSMatthias Ringwald 2422c48b2a2cSMatthias Ringwald // increment command_offset 2423c48b2a2cSMatthias Ringwald command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET); 2424c48b2a2cSMatthias Ringwald } 2425c48b2a2cSMatthias Ringwald break; 2426c48b2a2cSMatthias Ringwald } 242709e9d05bSMatthias Ringwald #endif 2428c48b2a2cSMatthias Ringwald 2429e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE 2430e7d0c9aaSMatthias Ringwald case L2CAP_CID_SIGNALING_LE: { 2431e7d0c9aaSMatthias Ringwald uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1]; 2432e7d0c9aaSMatthias Ringwald int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id); 2433c48b2a2cSMatthias Ringwald if (!valid){ 2434e74c5f58SMatthias Ringwald l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN); 2435ccf076adS[email protected] } 2436ccf076adS[email protected] break; 2437e7d0c9aaSMatthias Ringwald } 2438e7d0c9aaSMatthias Ringwald #endif 2439c48b2a2cSMatthias Ringwald 2440c48b2a2cSMatthias Ringwald case L2CAP_CID_ATTRIBUTE_PROTOCOL: 2441c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) { 2442c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 2443ccf076adS[email protected] } 2444c48b2a2cSMatthias Ringwald break; 2445c48b2a2cSMatthias Ringwald 2446c48b2a2cSMatthias Ringwald case L2CAP_CID_SECURITY_MANAGER_PROTOCOL: 2447c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) { 2448c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 2449c48b2a2cSMatthias Ringwald } 2450c48b2a2cSMatthias Ringwald break; 2451c48b2a2cSMatthias Ringwald 2452c48b2a2cSMatthias Ringwald case L2CAP_CID_CONNECTIONLESS_CHANNEL: 2453c48b2a2cSMatthias Ringwald if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) { 2454c48b2a2cSMatthias Ringwald (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 2455c48b2a2cSMatthias Ringwald } 2456c48b2a2cSMatthias Ringwald break; 24571bbc0b23S[email protected] 245809e9d05bSMatthias Ringwald default: 245909e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 246000d93d79Smatthias.ringwald // Find channel for this channel_id and connection handle 246164e11ca9SMatthias Ringwald l2cap_channel = l2cap_get_channel_for_local_cid(channel_id); 2462d1fd2a88SMatthias Ringwald if (l2cap_channel) { 246327e0774aSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 246427e0774aSMatthias Ringwald if (l2cap_channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){ 246527e0774aSMatthias Ringwald 246627e0774aSMatthias Ringwald // verify FCS 246727e0774aSMatthias Ringwald uint16_t fcs_calculated = crc16_calc(&packet[4], size - (4+2)); 246827e0774aSMatthias Ringwald uint16_t fcs_packet = little_endian_read_16(packet, size-2); 246927e0774aSMatthias Ringwald log_info("Packet FCS 0x%04x, calculated FCS 0x%04x", fcs_packet, fcs_calculated); 247027e0774aSMatthias Ringwald if (fcs_calculated != fcs_packet){ 247127e0774aSMatthias Ringwald log_error("FCS mismatch! Packet 0x%04x, calculated 0x%04x", fcs_packet, fcs_calculated); 247227e0774aSMatthias Ringwald // TODO: trigger retransmission or something like that 247327e0774aSMatthias Ringwald break; 247427e0774aSMatthias Ringwald } 247527e0774aSMatthias Ringwald 247627e0774aSMatthias Ringwald // switch on packet type 247727e0774aSMatthias Ringwald uint16_t control = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER); 247838f62777SMatthias Ringwald uint8_t req_seq = (control >> 8) & 0x3f; 247927e0774aSMatthias Ringwald if (control & 1){ 248027e0774aSMatthias Ringwald // S-Frame 2481bdbe2e49SMatthias Ringwald // int poll = (control >> 7) & 0x01; 2482bdbe2e49SMatthias Ringwald l2cap_supervisory_function_t s = (l2cap_supervisory_function_t) ((control >> 2) & 0x03); 2483bdbe2e49SMatthias Ringwald l2cap_ertm_tx_packet_state_t * tx_state; 2484bdbe2e49SMatthias Ringwald switch (s){ 2485bdbe2e49SMatthias Ringwald case L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY: 2486bdbe2e49SMatthias Ringwald tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index]; 2487bdbe2e49SMatthias Ringwald if ( ((req_seq - 1) & 0x3f) == tx_state->tx_seq){ 2488bdbe2e49SMatthias Ringwald log_info("RR seq %u == seq of oldest tx packet -> packet done", req_seq); 2489bdbe2e49SMatthias Ringwald l2cap_channel->tx_read_index++; 2490bdbe2e49SMatthias Ringwald if (l2cap_channel->tx_read_index >= l2cap_channel->num_rx_buffers){ 2491bdbe2e49SMatthias Ringwald l2cap_channel->tx_read_index = 0; 2492bdbe2e49SMatthias Ringwald } 2493bdbe2e49SMatthias Ringwald } else { 2494bdbe2e49SMatthias Ringwald log_info("RR seq %u != seq of oldest tx packet %u ???", req_seq, tx_state->tx_seq); 2495bdbe2e49SMatthias Ringwald } 2496bdbe2e49SMatthias Ringwald break; 2497bdbe2e49SMatthias Ringwald default: 2498bdbe2e49SMatthias Ringwald break; 2499bdbe2e49SMatthias Ringwald } 250027e0774aSMatthias Ringwald break; 250127e0774aSMatthias Ringwald } else { 250227e0774aSMatthias Ringwald // I-Frame 250327e0774aSMatthias Ringwald // get control 250427e0774aSMatthias Ringwald l2cap_segmentation_and_reassembly_t sar = (l2cap_segmentation_and_reassembly_t) (control >> 14); 250538f62777SMatthias Ringwald uint8_t tx_seq = (control >> 1) & 0x3f; 250638f62777SMatthias Ringwald log_info("Control: 0x%04x => SAR %u, ReqSeq %02u, R?, TxSeq %02u", control, (int) sar, req_seq, tx_seq); 250738f62777SMatthias Ringwald log_info("SAR: pos %u", l2cap_channel->rx_packets_state->pos); 250838f62777SMatthias Ringwald log_info("State: expected_tx_seq %02u, req_seq %02u", l2cap_channel->expected_tx_seq, l2cap_channel->req_seq); 250938f62777SMatthias Ringwald // check ordering 251038f62777SMatthias Ringwald if (l2cap_channel->expected_tx_seq == tx_seq){ 251138f62777SMatthias Ringwald log_info("Received expected frame with TxSeq == ExpectedTxSeq == %02u", tx_seq); 251238f62777SMatthias Ringwald l2cap_channel->req_seq = tx_seq; 2513d84e866fSMatthias Ringwald l2cap_channel->send_supervisor_frame_receiver_ready = 1; 251438f62777SMatthias Ringwald l2cap_channel->expected_tx_seq = l2cap_next_ertm_seq_nr(l2cap_channel->expected_tx_seq); 2515ed971c5aSMatthias Ringwald uint16_t sdu_length; 2516ed971c5aSMatthias Ringwald uint16_t segment_length; 2517ed971c5aSMatthias Ringwald uint16_t payload_offset; 251827e0774aSMatthias Ringwald switch (sar){ 251927e0774aSMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU: 2520ed971c5aSMatthias Ringwald payload_offset = COMPLETE_L2CAP_HEADER+2; 2521ed971c5aSMatthias Ringwald segment_length = payload_offset-2; 2522ed971c5aSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER+2], segment_length); 252327e0774aSMatthias Ringwald break; 252427e0774aSMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU: 2525ed971c5aSMatthias Ringwald // TODO: use current packet 2526ed971c5aSMatthias Ringwald // TODO: check if reassembly started 2527ed971c5aSMatthias Ringwald // TODO: check len against local mtu 2528ed971c5aSMatthias Ringwald sdu_length = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER+2); 2529ed971c5aSMatthias Ringwald payload_offset = COMPLETE_L2CAP_HEADER+4; 2530ed971c5aSMatthias Ringwald segment_length = size - payload_offset-2; 2531ed971c5aSMatthias Ringwald memcpy(&l2cap_channel->rx_packets_data[0], &packet[payload_offset], segment_length); 2532ed971c5aSMatthias Ringwald l2cap_channel->rx_packets_state->sdu_length = sdu_length; 2533ed971c5aSMatthias Ringwald l2cap_channel->rx_packets_state->pos = segment_length; 2534ed971c5aSMatthias Ringwald break; 253527e0774aSMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU: 2536ed971c5aSMatthias Ringwald payload_offset = COMPLETE_L2CAP_HEADER+2; 2537ed971c5aSMatthias Ringwald segment_length = size - payload_offset-2; 2538ed971c5aSMatthias Ringwald memcpy(&l2cap_channel->rx_packets_data[l2cap_channel->rx_packets_state->pos], &packet[payload_offset], segment_length); 2539ed971c5aSMatthias Ringwald l2cap_channel->rx_packets_state->pos += segment_length; 2540ed971c5aSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->rx_packets_data, l2cap_channel->rx_packets_state[0].pos); 2541ed971c5aSMatthias Ringwald break; 254227e0774aSMatthias Ringwald case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU: 2543ed971c5aSMatthias Ringwald payload_offset = COMPLETE_L2CAP_HEADER+2; 2544ed971c5aSMatthias Ringwald segment_length = size - payload_offset-2; 2545ed971c5aSMatthias Ringwald memcpy(&l2cap_channel->rx_packets_data[l2cap_channel->rx_packets_state->pos], &packet[payload_offset], segment_length); 2546ed971c5aSMatthias Ringwald l2cap_channel->rx_packets_state->pos += segment_length; 254727e0774aSMatthias Ringwald break; 254827e0774aSMatthias Ringwald } 254927e0774aSMatthias Ringwald } 255038f62777SMatthias Ringwald } 255127e0774aSMatthias Ringwald break; 255227e0774aSMatthias Ringwald } 255327e0774aSMatthias Ringwald #endif 25543d50b4baSMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER); 255500d93d79Smatthias.ringwald } 255609e9d05bSMatthias Ringwald #endif 2557a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 255864e11ca9SMatthias Ringwald l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id); 255964e11ca9SMatthias Ringwald if (l2cap_channel) { 256085aeef60SMatthias Ringwald // credit counting 256185aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming == 0){ 256285aeef60SMatthias Ringwald log_error("LE Data Channel packet received but no incoming credits"); 256385aeef60SMatthias Ringwald l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 256485aeef60SMatthias Ringwald break; 256585aeef60SMatthias Ringwald } 256685aeef60SMatthias Ringwald l2cap_channel->credits_incoming--; 256785aeef60SMatthias Ringwald 256885aeef60SMatthias Ringwald // automatic credits 256985aeef60SMatthias Ringwald if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){ 257085aeef60SMatthias Ringwald l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT; 257185aeef60SMatthias Ringwald } 257285aeef60SMatthias Ringwald 2573cd529728SMatthias Ringwald // first fragment 2574cd529728SMatthias Ringwald uint16_t pos = 0; 2575cd529728SMatthias Ringwald if (!l2cap_channel->receive_sdu_len){ 2576cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER); 2577cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos = 0; 2578cd529728SMatthias Ringwald pos += 2; 2579cd529728SMatthias Ringwald size -= 2; 2580cd529728SMatthias Ringwald } 2581cd529728SMatthias Ringwald memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER); 2582cd529728SMatthias Ringwald l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER; 2583cd529728SMatthias Ringwald // done? 258463f0ac45SMatthias Ringwald log_info("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len); 2585cd529728SMatthias Ringwald if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){ 2586cd529728SMatthias Ringwald l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len); 2587cd529728SMatthias Ringwald l2cap_channel->receive_sdu_len = 0; 2588cd529728SMatthias Ringwald } 258963f0ac45SMatthias Ringwald } else { 259063f0ac45SMatthias Ringwald log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id); 259164e11ca9SMatthias Ringwald } 259264e11ca9SMatthias Ringwald #endif 25935652b5ffS[email protected] break; 25945652b5ffS[email protected] } 259500d93d79Smatthias.ringwald 25961eb2563eS[email protected] l2cap_run(); 25972718e2e7Smatthias.ringwald } 259800d93d79Smatthias.ringwald 259909e9d05bSMatthias Ringwald // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol 260009e9d05bSMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) { 260109e9d05bSMatthias Ringwald int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id); 260209e9d05bSMatthias Ringwald if (index < 0) return; 260309e9d05bSMatthias Ringwald fixed_channels[index].callback = the_packet_handler; 260409e9d05bSMatthias Ringwald } 260509e9d05bSMatthias Ringwald 260609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC 260715ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 260827a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){ 2609f62db1e3Smatthias.ringwald channel->state = L2CAP_STATE_CLOSED; 2610f62db1e3Smatthias.ringwald l2cap_emit_channel_closed(channel); 2611f62db1e3Smatthias.ringwald // discard channel 26129dcb2fb2S[email protected] l2cap_stop_rtx(channel); 2613665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel); 2614d3a9df87Smatthias.ringwald btstack_memory_l2cap_channel_free(channel); 2615c8e4258aSmatthias.ringwald } 26161e6aba47Smatthias.ringwald 26178f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){ 2618665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 2619665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, services); 2620665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2621665d90f2SMatthias Ringwald l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it); 26229d9bbc01Smatthias.ringwald if ( service->psm == psm){ 26239d9bbc01Smatthias.ringwald return service; 26249d9bbc01Smatthias.ringwald }; 26259d9bbc01Smatthias.ringwald } 26269d9bbc01Smatthias.ringwald return NULL; 26279d9bbc01Smatthias.ringwald } 26289d9bbc01Smatthias.ringwald 26297192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){ 26307192e786SMatthias Ringwald return l2cap_get_service_internal(&l2cap_services, psm); 26317192e786SMatthias Ringwald } 26327192e786SMatthias Ringwald 2633e0abb8e7S[email protected] 2634be2053a6SMatthias 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){ 2635be2053a6SMatthias Ringwald 2636be2053a6SMatthias Ringwald log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu); 2637e0abb8e7S[email protected] 26384bb582b6Smatthias.ringwald // check for alread registered psm 26399d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 2640277abc2cSmatthias.ringwald if (service) { 2641be2053a6SMatthias Ringwald log_error("l2cap_register_service: PSM %u already registered", psm); 2642be2053a6SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 2643277abc2cSmatthias.ringwald } 26449d9bbc01Smatthias.ringwald 26454bb582b6Smatthias.ringwald // alloc structure 2646bb69aaaeS[email protected] service = btstack_memory_l2cap_service_get(); 2647277abc2cSmatthias.ringwald if (!service) { 2648be2053a6SMatthias Ringwald log_error("l2cap_register_service: no memory for l2cap_service_t"); 2649be2053a6SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 2650277abc2cSmatthias.ringwald } 26519d9bbc01Smatthias.ringwald 26529d9bbc01Smatthias.ringwald // fill in 26539d9bbc01Smatthias.ringwald service->psm = psm; 26549d9bbc01Smatthias.ringwald service->mtu = mtu; 265505ae8de3SMatthias Ringwald service->packet_handler = service_packet_handler; 2656df3354fcS[email protected] service->required_security_level = security_level; 26579d9bbc01Smatthias.ringwald 26589d9bbc01Smatthias.ringwald // add to services list 2659665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service); 2660c0e866bfSmatthias.ringwald 2661c0e866bfSmatthias.ringwald // enable page scan 266215a95bd5SMatthias Ringwald gap_connectable_control(1); 26635842b6d9Smatthias.ringwald 2664be2053a6SMatthias Ringwald return 0; 26659d9bbc01Smatthias.ringwald } 26669d9bbc01Smatthias.ringwald 26677e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){ 2668e0abb8e7S[email protected] 2669e0abb8e7S[email protected] log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm); 2670e0abb8e7S[email protected] 26719d9bbc01Smatthias.ringwald l2cap_service_t *service = l2cap_get_service(psm); 26727e8856ebSMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 2673665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service); 2674d3a9df87Smatthias.ringwald btstack_memory_l2cap_service_free(service); 2675c0e866bfSmatthias.ringwald 2676c0e866bfSmatthias.ringwald // disable page scan when no services registered 26777e8856ebSMatthias Ringwald if (btstack_linked_list_empty(&l2cap_services)) { 267815a95bd5SMatthias Ringwald gap_connectable_control(0); 26799d9bbc01Smatthias.ringwald } 26807e8856ebSMatthias Ringwald return 0; 26817e8856ebSMatthias Ringwald } 268209e9d05bSMatthias Ringwald #endif 26839d9bbc01Smatthias.ringwald 26847192e786SMatthias Ringwald 2685cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS 268683fd9c76SMatthias Ringwald 268757be49d6SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){ 268857be49d6SMatthias Ringwald if (!channel->waiting_for_can_send_now) return; 268957be49d6SMatthias Ringwald if (channel->send_sdu_buffer) return; 269057be49d6SMatthias Ringwald channel->waiting_for_can_send_now = 0; 269157be49d6SMatthias Ringwald log_info("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid); 269257be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW); 269357be49d6SMatthias Ringwald } 269457be49d6SMatthias Ringwald 269557be49d6SMatthias Ringwald // 1BH2222 269657be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) { 269757be49d6SMatthias 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", 269857be49d6SMatthias Ringwald channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu); 269957be49d6SMatthias Ringwald uint8_t event[19]; 270057be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION; 270157be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 270257be49d6SMatthias Ringwald event[2] = channel->address_type; 270357be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[3]); 270457be49d6SMatthias Ringwald little_endian_store_16(event, 9, channel->con_handle); 270557be49d6SMatthias Ringwald little_endian_store_16(event, 11, channel->psm); 270657be49d6SMatthias Ringwald little_endian_store_16(event, 13, channel->local_cid); 270757be49d6SMatthias Ringwald little_endian_store_16(event, 15, channel->remote_cid); 270857be49d6SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_mtu); 270957be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 271057be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 271157be49d6SMatthias Ringwald } 271257be49d6SMatthias Ringwald // 11BH22222 271357be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) { 271457be49d6SMatthias 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", 271557be49d6SMatthias Ringwald status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, 271657be49d6SMatthias Ringwald channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu); 2717c99bb618SMatthias Ringwald uint8_t event[23]; 271857be49d6SMatthias Ringwald event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED; 271957be49d6SMatthias Ringwald event[1] = sizeof(event) - 2; 272057be49d6SMatthias Ringwald event[2] = status; 272157be49d6SMatthias Ringwald event[3] = channel->address_type; 272257be49d6SMatthias Ringwald reverse_bd_addr(channel->address, &event[4]); 272357be49d6SMatthias Ringwald little_endian_store_16(event, 10, channel->con_handle); 2724c99bb618SMatthias Ringwald event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0; 2725c99bb618SMatthias Ringwald little_endian_store_16(event, 13, channel->psm); 2726c99bb618SMatthias Ringwald little_endian_store_16(event, 15, channel->local_cid); 2727c99bb618SMatthias Ringwald little_endian_store_16(event, 17, channel->remote_cid); 2728c99bb618SMatthias Ringwald little_endian_store_16(event, 19, channel->local_mtu); 2729c99bb618SMatthias Ringwald little_endian_store_16(event, 21, channel->remote_mtu); 273057be49d6SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 273157be49d6SMatthias Ringwald l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event)); 273257be49d6SMatthias Ringwald } 273357be49d6SMatthias Ringwald 273457be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){ 273557be49d6SMatthias Ringwald btstack_linked_list_iterator_t it; 273657be49d6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &l2cap_le_channels); 273757be49d6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 273857be49d6SMatthias Ringwald l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it); 273957be49d6SMatthias Ringwald if ( channel->local_cid == local_cid) { 274057be49d6SMatthias Ringwald return channel; 274157be49d6SMatthias Ringwald } 274257be49d6SMatthias Ringwald } 274357be49d6SMatthias Ringwald return NULL; 274457be49d6SMatthias Ringwald } 274557be49d6SMatthias Ringwald 274657be49d6SMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE 274757be49d6SMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){ 274857be49d6SMatthias Ringwald channel->state = L2CAP_STATE_CLOSED; 274957be49d6SMatthias Ringwald l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED); 275057be49d6SMatthias Ringwald // discard channel 275157be49d6SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel); 275257be49d6SMatthias Ringwald btstack_memory_l2cap_channel_free(channel); 275357be49d6SMatthias Ringwald } 275457be49d6SMatthias Ringwald 2755e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){ 2756e7d0c9aaSMatthias Ringwald return l2cap_get_service_internal(&l2cap_le_services, le_psm); 27577192e786SMatthias Ringwald } 2758efedfb4cSMatthias Ringwald 2759da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){ 27607192e786SMatthias Ringwald 2761da144af5SMatthias Ringwald log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm); 27627192e786SMatthias Ringwald 27637192e786SMatthias Ringwald // check for alread registered psm 27647192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 27657192e786SMatthias Ringwald if (service) { 2766da144af5SMatthias Ringwald return L2CAP_SERVICE_ALREADY_REGISTERED; 27677192e786SMatthias Ringwald } 27687192e786SMatthias Ringwald 27697192e786SMatthias Ringwald // alloc structure 27707192e786SMatthias Ringwald service = btstack_memory_l2cap_service_get(); 27717192e786SMatthias Ringwald if (!service) { 27727192e786SMatthias Ringwald log_error("l2cap_register_service_internal: no memory for l2cap_service_t"); 2773da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 27747192e786SMatthias Ringwald } 27757192e786SMatthias Ringwald 27767192e786SMatthias Ringwald // fill in 27777192e786SMatthias Ringwald service->psm = psm; 2778da144af5SMatthias Ringwald service->mtu = 0; 27797192e786SMatthias Ringwald service->packet_handler = packet_handler; 27807192e786SMatthias Ringwald service->required_security_level = security_level; 27817192e786SMatthias Ringwald 27827192e786SMatthias Ringwald // add to services list 2783665d90f2SMatthias Ringwald btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service); 27847192e786SMatthias Ringwald 27857192e786SMatthias Ringwald // done 2786da144af5SMatthias Ringwald return 0; 27877192e786SMatthias Ringwald } 27887192e786SMatthias Ringwald 2789da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) { 27907192e786SMatthias Ringwald log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm); 27917192e786SMatthias Ringwald l2cap_service_t *service = l2cap_le_get_service(psm); 27929367f9b0SMatthias Ringwald if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST; 2793da144af5SMatthias Ringwald 2794665d90f2SMatthias Ringwald btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service); 27957192e786SMatthias Ringwald btstack_memory_l2cap_service_free(service); 2796da144af5SMatthias Ringwald return 0; 27977192e786SMatthias Ringwald } 2798da144af5SMatthias Ringwald 2799da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){ 2800e7d0c9aaSMatthias Ringwald // get channel 2801e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2802e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2803e7d0c9aaSMatthias Ringwald 2804e7d0c9aaSMatthias Ringwald // validate state 2805e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 2806e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2807e7d0c9aaSMatthias Ringwald } 2808e7d0c9aaSMatthias Ringwald 2809efedfb4cSMatthias Ringwald // set state accept connection 281023017473SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT; 281123017473SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 281223017473SMatthias Ringwald channel->local_mtu = mtu; 281385aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 281485aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 281585aeef60SMatthias Ringwald 281685aeef60SMatthias Ringwald // test 281763f0ac45SMatthias Ringwald // channel->new_credits_incoming = 1; 2818e7d0c9aaSMatthias Ringwald 2819e7d0c9aaSMatthias Ringwald // go 2820e7d0c9aaSMatthias Ringwald l2cap_run(); 2821da144af5SMatthias Ringwald return 0; 2822da144af5SMatthias Ringwald } 2823da144af5SMatthias Ringwald 2824da144af5SMatthias Ringwald /** 2825da144af5SMatthias Ringwald * @brief Deny incoming LE Data Channel connection due to resource constraints 2826da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2827da144af5SMatthias Ringwald */ 2828da144af5SMatthias Ringwald 2829da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){ 2830e7d0c9aaSMatthias Ringwald // get channel 2831e7d0c9aaSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2832e7d0c9aaSMatthias Ringwald if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 2833e7d0c9aaSMatthias Ringwald 2834e7d0c9aaSMatthias Ringwald // validate state 2835e7d0c9aaSMatthias Ringwald if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){ 2836e7d0c9aaSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 2837e7d0c9aaSMatthias Ringwald } 2838e7d0c9aaSMatthias Ringwald 2839efedfb4cSMatthias Ringwald // set state decline connection 2840e7d0c9aaSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE; 2841e7d0c9aaSMatthias Ringwald channel->reason = 0x04; // no resources available 2842e7d0c9aaSMatthias Ringwald l2cap_run(); 2843da144af5SMatthias Ringwald return 0; 2844da144af5SMatthias Ringwald } 2845da144af5SMatthias Ringwald 28467dafa750SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, 2847da144af5SMatthias Ringwald uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, 2848efedfb4cSMatthias Ringwald uint16_t * out_local_cid) { 2849efedfb4cSMatthias Ringwald 28507dafa750SMatthias Ringwald log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu); 2851da144af5SMatthias Ringwald 28527dafa750SMatthias Ringwald 28537dafa750SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 28547dafa750SMatthias Ringwald if (!connection) { 28557dafa750SMatthias Ringwald log_error("no hci_connection for handle 0x%04x", con_handle); 28567dafa750SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 28577dafa750SMatthias Ringwald } 28587dafa750SMatthias Ringwald 28597dafa750SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, connection->address, connection->address_type, psm, mtu, security_level); 2860da144af5SMatthias Ringwald if (!channel) { 2861da144af5SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; 2862da144af5SMatthias Ringwald } 2863e7d0c9aaSMatthias Ringwald log_info("l2cap_le_create_channel %p", channel); 2864da144af5SMatthias Ringwald 2865da144af5SMatthias Ringwald // store local_cid 2866da144af5SMatthias Ringwald if (out_local_cid){ 2867da144af5SMatthias Ringwald *out_local_cid = channel->local_cid; 2868da144af5SMatthias Ringwald } 2869da144af5SMatthias Ringwald 28707dafa750SMatthias Ringwald // provide buffer 28717dafa750SMatthias Ringwald channel->con_handle = con_handle; 2872cd529728SMatthias Ringwald channel->receive_sdu_buffer = receive_sdu_buffer; 28737dafa750SMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST; 287485aeef60SMatthias Ringwald channel->new_credits_incoming = initial_credits; 287585aeef60SMatthias Ringwald channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS; 287685aeef60SMatthias Ringwald 2877efedfb4cSMatthias Ringwald // add to connections list 2878efedfb4cSMatthias Ringwald btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel); 2879efedfb4cSMatthias Ringwald 28807dafa750SMatthias Ringwald // go 288163f0ac45SMatthias Ringwald l2cap_run(); 2882da144af5SMatthias Ringwald return 0; 2883da144af5SMatthias Ringwald } 2884da144af5SMatthias Ringwald 2885da144af5SMatthias Ringwald /** 2886da144af5SMatthias Ringwald * @brief Provide credtis for LE Data Channel 2887da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2888da144af5SMatthias Ringwald * @param credits Number additional credits for peer 2889da144af5SMatthias Ringwald */ 289064e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){ 289163f0ac45SMatthias Ringwald 289263f0ac45SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 289363f0ac45SMatthias Ringwald if (!channel) { 289463f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 289563f0ac45SMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 289663f0ac45SMatthias Ringwald } 289763f0ac45SMatthias Ringwald 2898efedfb4cSMatthias Ringwald // check state 289963f0ac45SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN){ 290063f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid); 290163f0ac45SMatthias Ringwald } 290263f0ac45SMatthias Ringwald 290363f0ac45SMatthias Ringwald // assert incoming credits + credits <= 0xffff 290463f0ac45SMatthias Ringwald uint32_t total_credits = channel->credits_incoming; 290563f0ac45SMatthias Ringwald total_credits += channel->new_credits_incoming; 290663f0ac45SMatthias Ringwald total_credits += credits; 290763f0ac45SMatthias Ringwald if (total_credits > 0xffff){ 290863f0ac45SMatthias Ringwald log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming, 290963f0ac45SMatthias Ringwald channel->new_credits_incoming, credits); 291063f0ac45SMatthias Ringwald } 291163f0ac45SMatthias Ringwald 2912efedfb4cSMatthias Ringwald // set credits_granted 291363f0ac45SMatthias Ringwald channel->new_credits_incoming += credits; 291463f0ac45SMatthias Ringwald 291563f0ac45SMatthias Ringwald // go 291663f0ac45SMatthias Ringwald l2cap_run(); 2917da144af5SMatthias Ringwald return 0; 2918da144af5SMatthias Ringwald } 2919da144af5SMatthias Ringwald 2920da144af5SMatthias Ringwald /** 2921da144af5SMatthias Ringwald * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module 2922da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2923da144af5SMatthias Ringwald */ 292464e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){ 292544276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 292644276248SMatthias Ringwald if (!channel) { 292744276248SMatthias Ringwald log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid); 2928da144af5SMatthias Ringwald return 0; 2929da144af5SMatthias Ringwald } 2930da144af5SMatthias Ringwald 293144276248SMatthias Ringwald // check state 293244276248SMatthias Ringwald if (channel->state != L2CAP_STATE_OPEN) return 0; 293344276248SMatthias Ringwald 293444276248SMatthias Ringwald // check queue 293544276248SMatthias Ringwald if (channel->send_sdu_buffer) return 0; 293644276248SMatthias Ringwald 293744276248SMatthias Ringwald // fine, go ahead 293844276248SMatthias Ringwald return 1; 293944276248SMatthias Ringwald } 294044276248SMatthias Ringwald 2941da144af5SMatthias Ringwald /** 2942da144af5SMatthias Ringwald * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible 2943da144af5SMatthias Ringwald * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function 2944da144af5SMatthias Ringwald * so packet handler should be ready to handle it 2945da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2946da144af5SMatthias Ringwald */ 294764e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){ 294844276248SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 294944276248SMatthias Ringwald if (!channel) { 295044276248SMatthias Ringwald log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid); 295144276248SMatthias Ringwald return 0; 295244276248SMatthias Ringwald } 295344276248SMatthias Ringwald channel->waiting_for_can_send_now = 1; 295444276248SMatthias Ringwald l2cap_le_notify_channel_can_send(channel); 2955da144af5SMatthias Ringwald return 0; 2956da144af5SMatthias Ringwald } 2957da144af5SMatthias Ringwald 2958da144af5SMatthias Ringwald /** 2959da144af5SMatthias Ringwald * @brief Send data via LE Data Channel 2960da144af5SMatthias Ringwald * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event 2961da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2962da144af5SMatthias Ringwald * @param data data to send 2963da144af5SMatthias Ringwald * @param size data size 2964da144af5SMatthias Ringwald */ 296564e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){ 296664e11ca9SMatthias Ringwald 296764e11ca9SMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 296864e11ca9SMatthias Ringwald if (!channel) { 296964e11ca9SMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 2970828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 297164e11ca9SMatthias Ringwald } 297264e11ca9SMatthias Ringwald 297364e11ca9SMatthias Ringwald if (len > channel->remote_mtu){ 297464e11ca9SMatthias Ringwald log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid); 297564e11ca9SMatthias Ringwald return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU; 297664e11ca9SMatthias Ringwald } 297764e11ca9SMatthias Ringwald 29787f107edaSMatthias Ringwald if (channel->send_sdu_buffer){ 297964e11ca9SMatthias Ringwald log_info("l2cap_send cid 0x%02x, cannot send", local_cid); 298064e11ca9SMatthias Ringwald return BTSTACK_ACL_BUFFERS_FULL; 298164e11ca9SMatthias Ringwald } 298264e11ca9SMatthias Ringwald 29837f107edaSMatthias Ringwald channel->send_sdu_buffer = data; 29847f107edaSMatthias Ringwald channel->send_sdu_len = len; 29857f107edaSMatthias Ringwald channel->send_sdu_pos = 0; 298664e11ca9SMatthias Ringwald 29877f107edaSMatthias Ringwald l2cap_run(); 29887f107edaSMatthias Ringwald return 0; 2989da144af5SMatthias Ringwald } 2990da144af5SMatthias Ringwald 2991da144af5SMatthias Ringwald /** 2992da144af5SMatthias Ringwald * @brief Disconnect from LE Data Channel 2993da144af5SMatthias Ringwald * @param local_cid L2CAP LE Data Channel Identifier 2994da144af5SMatthias Ringwald */ 2995828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid) 2996da144af5SMatthias Ringwald { 2997828a7f7aSMatthias Ringwald l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid); 2998828a7f7aSMatthias Ringwald if (!channel) { 2999828a7f7aSMatthias Ringwald log_error("l2cap_send no channel for cid 0x%02x", local_cid); 3000828a7f7aSMatthias Ringwald return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 3001828a7f7aSMatthias Ringwald } 3002828a7f7aSMatthias Ringwald 3003828a7f7aSMatthias Ringwald channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST; 3004828a7f7aSMatthias Ringwald l2cap_run(); 3005da144af5SMatthias Ringwald return 0; 3006da144af5SMatthias Ringwald } 3007da144af5SMatthias Ringwald 30087f02f414SMatthias Ringwald #endif 3009