xref: /btstack/src/l2cap.c (revision 43ec931d38cd09f2b72bdcea249f6509262d5eed)
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
9233c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
933d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
9430725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void);
9509e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
9609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
9709e9d05bSMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
9809e9d05bSMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
9909e9d05bSMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
10009e9d05bSMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
10109e9d05bSMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel);
10209e9d05bSMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
10309e9d05bSMatthias Ringwald #endif
104cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
10557be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status);
10657be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel);
10757be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid);
10844276248SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel);
109828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel);
110e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm);
111e7d0c9aaSMatthias Ringwald #endif
11233c40538SMatthias Ringwald 
1135628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
1145628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
1152125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
1165628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
1175628cf69SMatthias Ringwald 
11809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1195628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
1205628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
12109e9d05bSMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
12209e9d05bSMatthias Ringwald #endif
12357be49d6SMatthias Ringwald 
12457be49d6SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1255628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
1265628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
12757be49d6SMatthias Ringwald #endif
12839bda6d5Smatthias.ringwald 
12939bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1302b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1312b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1322b83fb7dSmatthias.ringwald 
133fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
1345628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
13539bda6d5Smatthias.ringwald 
136d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE
137d6ed9f9cSMatthias Ringwald // only used for connection parameter update events
138d6ed9f9cSMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
139d6ed9f9cSMatthias Ringwald #endif
140d6ed9f9cSMatthias Ringwald 
14134e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
14234e7e577SMatthias Ringwald     switch (index){
14334e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
14434e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
14534e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
14634e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
14734e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
14834e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
14934e7e577SMatthias Ringwald         default:
15034e7e577SMatthias Ringwald             return 0;
15134e7e577SMatthias Ringwald     }
15234e7e577SMatthias Ringwald }
1535628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1545628cf69SMatthias Ringwald     switch (channel_id){
1555628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1565628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1575628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1585628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1595628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1605628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1615628cf69SMatthias Ringwald         default:
1625628cf69SMatthias Ringwald             return -1;
1635628cf69SMatthias Ringwald         }
1645628cf69SMatthias Ringwald }
16539bda6d5Smatthias.ringwald 
16634e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
16734e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
16834e7e577SMatthias Ringwald     return 1;
16934e7e577SMatthias Ringwald }
17034e7e577SMatthias Ringwald 
17171de195eSMatthias Ringwald void l2cap_init(void){
1722b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
173808a48abSmatthias.ringwald 
17409e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
175f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
176f5454fc6Smatthias.ringwald     l2cap_services = NULL;
17709e9d05bSMatthias Ringwald     require_security_level2_for_outgoing_sdp = 0;
17809e9d05bSMatthias Ringwald #endif
179a3dc965aSMatthias Ringwald 
180a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1817192e786SMatthias Ringwald     l2cap_le_services = NULL;
1827192e786SMatthias Ringwald     l2cap_le_channels = NULL;
183a3dc965aSMatthias Ringwald #endif
184f5454fc6Smatthias.ringwald 
185d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE
18633c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
187d6ed9f9cSMatthias Ringwald #endif
1885628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
189f5454fc6Smatthias.ringwald 
190fcadd0caSmatthias.ringwald     //
1912718e2e7Smatthias.ringwald     // register callback with HCI
192fcadd0caSmatthias.ringwald     //
193d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
194fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
195fb37a842SMatthias Ringwald 
196d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
197fb37a842SMatthias Ringwald 
198be005ed6SMatthias Ringwald #ifdef ENABLE_CLASSIC
19915a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
200be005ed6SMatthias Ringwald #endif
201fcadd0caSmatthias.ringwald }
202fcadd0caSmatthias.ringwald 
203ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
204d6ed9f9cSMatthias Ringwald #ifdef ENABLE_BLE
20533c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
206d6ed9f9cSMatthias Ringwald #else
207d6ed9f9cSMatthias Ringwald     UNUSED(handler);
208d6ed9f9cSMatthias Ringwald #endif
2091e6aba47Smatthias.ringwald }
2101e6aba47Smatthias.ringwald 
21109e9d05bSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
2129ec2630cSMatthias Ringwald     UNUSED(con_handle);
2139ec2630cSMatthias Ringwald 
21409e9d05bSMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
21509e9d05bSMatthias Ringwald     if (index < 0) return;
21609e9d05bSMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
21709e9d05bSMatthias Ringwald     l2cap_notify_channel_can_send();
21809e9d05bSMatthias Ringwald }
21909e9d05bSMatthias Ringwald 
22009e9d05bSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
2219ec2630cSMatthias Ringwald     UNUSED(channel_id);
2229ec2630cSMatthias Ringwald 
22309e9d05bSMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
22409e9d05bSMatthias Ringwald }
22509e9d05bSMatthias Ringwald 
22609e9d05bSMatthias Ringwald uint8_t *l2cap_get_outgoing_buffer(void){
22709e9d05bSMatthias Ringwald     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
22809e9d05bSMatthias Ringwald }
22909e9d05bSMatthias Ringwald 
23009e9d05bSMatthias Ringwald int l2cap_reserve_packet_buffer(void){
23109e9d05bSMatthias Ringwald     return hci_reserve_packet_buffer();
23209e9d05bSMatthias Ringwald }
23309e9d05bSMatthias Ringwald 
23409e9d05bSMatthias Ringwald void l2cap_release_packet_buffer(void){
23509e9d05bSMatthias Ringwald     hci_release_packet_buffer();
23609e9d05bSMatthias Ringwald }
23709e9d05bSMatthias Ringwald 
238f511cefaSMatthias 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){
23909e9d05bSMatthias Ringwald     // 0 - Connection handle : PB=pb : BC=00
240f511cefaSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (packet_boundary << 12) | (0 << 14));
24109e9d05bSMatthias Ringwald     // 2 - ACL length
24209e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
24309e9d05bSMatthias Ringwald     // 4 - L2CAP packet length
24409e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
24509e9d05bSMatthias Ringwald     // 6 - L2CAP channel DEST
24609e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 6,  remote_cid);
24709e9d05bSMatthias Ringwald }
24809e9d05bSMatthias Ringwald 
249f511cefaSMatthias Ringwald // assumption - only on LE connections
25009e9d05bSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
25109e9d05bSMatthias Ringwald 
25209e9d05bSMatthias Ringwald     if (!hci_is_packet_buffer_reserved()){
25309e9d05bSMatthias Ringwald         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
25409e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
25509e9d05bSMatthias Ringwald     }
25609e9d05bSMatthias Ringwald 
25709e9d05bSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
25809e9d05bSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
25909e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
26009e9d05bSMatthias Ringwald     }
26109e9d05bSMatthias Ringwald 
26209e9d05bSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
26309e9d05bSMatthias Ringwald 
26409e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
265f511cefaSMatthias Ringwald     l2cap_setup_header(acl_buffer, con_handle, 0, cid, len);
26609e9d05bSMatthias Ringwald     // send
26709e9d05bSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
26809e9d05bSMatthias Ringwald }
26909e9d05bSMatthias Ringwald 
270f511cefaSMatthias Ringwald // assumption - only on LE connections
27109e9d05bSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
27209e9d05bSMatthias Ringwald 
27309e9d05bSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
27409e9d05bSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
27509e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
27609e9d05bSMatthias Ringwald     }
27709e9d05bSMatthias Ringwald 
27809e9d05bSMatthias Ringwald     hci_reserve_packet_buffer();
27909e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
28009e9d05bSMatthias Ringwald 
28109e9d05bSMatthias Ringwald     memcpy(&acl_buffer[8], data, len);
28209e9d05bSMatthias Ringwald 
28309e9d05bSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
28409e9d05bSMatthias Ringwald }
28509e9d05bSMatthias Ringwald 
28609e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
287d9d23054SMatthias Ringwald     log_debug("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
28809e9d05bSMatthias Ringwald     uint8_t event[4];
28909e9d05bSMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
29009e9d05bSMatthias Ringwald     event[1] = sizeof(event) - 2;
29109e9d05bSMatthias Ringwald     little_endian_store_16(event, 2, channel);
29209e9d05bSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
29309e9d05bSMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
29409e9d05bSMatthias Ringwald }
29509e9d05bSMatthias Ringwald 
29609e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS
29717a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
29858de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
29958de5610Smatthias.ringwald }
30058de5610Smatthias.ringwald 
30144276248SMatthias Ringwald static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){
30244276248SMatthias Ringwald     uint8_t event[4];
30344276248SMatthias Ringwald     event[0] = event_code;
30444276248SMatthias Ringwald     event[1] = sizeof(event) - 2;
30544276248SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
30644276248SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
30744276248SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
30844276248SMatthias Ringwald }
30909e9d05bSMatthias Ringwald #endif
31044276248SMatthias Ringwald 
31109e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
31258de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
313c9dc710bS[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",
314fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
315c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
316bab5f4f0SMatthias Ringwald     uint8_t event[24];
31758de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
31858de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
31958de5610Smatthias.ringwald     event[2] = status;
320724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
321fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
322f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
323f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
324f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
325f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
326f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
327f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
328bab5f4f0SMatthias Ringwald     event[23] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0;
32958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
33017a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
33158de5610Smatthias.ringwald }
33258de5610Smatthias.ringwald 
33344276248SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
334e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
33544276248SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
33658de5610Smatthias.ringwald }
33758de5610Smatthias.ringwald 
33844276248SMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) {
339e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
340fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
34158de5610Smatthias.ringwald     uint8_t event[16];
34258de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
34358de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
344724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
345fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
346f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
347f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
348f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
34958de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
35017a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
3510af41d30Smatthias.ringwald }
352808a48abSmatthias.ringwald 
3537f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
354665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
355665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
356665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
357665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
358b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
359f62db1e3Smatthias.ringwald             return channel;
360f62db1e3Smatthias.ringwald         }
361f62db1e3Smatthias.ringwald     }
362f62db1e3Smatthias.ringwald     return NULL;
363f62db1e3Smatthias.ringwald }
364f62db1e3Smatthias.ringwald 
3650b9d7e78SMatthias Ringwald ///
3660b9d7e78SMatthias Ringwald 
36730725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
36830725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
36930725612SMatthias Ringwald     if (!channel) return;
37030725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
37130725612SMatthias Ringwald     l2cap_notify_channel_can_send();
37230725612SMatthias Ringwald }
37330725612SMatthias Ringwald 
3746b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
3756b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
3766b1fde37Smatthias.ringwald     if (!channel) return 0;
3770b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
3787856fb31S[email protected] }
3797856fb31S[email protected] 
38083e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
38183e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
38283e7cdd9SMatthias Ringwald     if (!channel) return 0;
3830b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
38483e7cdd9SMatthias Ringwald }
38596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
38696cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
38796cbd662Smatthias.ringwald     if (channel) {
38896cbd662Smatthias.ringwald         return channel->remote_mtu;
38996cbd662Smatthias.ringwald     }
39096cbd662Smatthias.ringwald     return 0;
39196cbd662Smatthias.ringwald }
39296cbd662Smatthias.ringwald 
393ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
394665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
395665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
396665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
397665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3985932bd7cS[email protected]         if ( &channel->rtx == ts) {
3995932bd7cS[email protected]             return channel;
4005932bd7cS[email protected]         }
4015932bd7cS[email protected]     }
4025932bd7cS[email protected]     return NULL;
4035932bd7cS[email protected] }
4045932bd7cS[email protected] 
405ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
4065932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
40795d2c8f4SMatthias Ringwald     if (!channel) return;
4085932bd7cS[email protected] 
4095932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
4105932bd7cS[email protected] 
4115932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
4125932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
4135932bd7cS[email protected]     // notify client
4145932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
4155932bd7cS[email protected] 
4165932bd7cS[email protected]     // discard channel
4179dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
418665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
4195932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
4205932bd7cS[email protected] }
4215932bd7cS[email protected] 
4225932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
4235932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
424528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
4255932bd7cS[email protected] }
4265932bd7cS[email protected] 
4275932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
4285932bd7cS[email protected]     l2cap_stop_rtx(channel);
429cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
430528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
431528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
432528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
4335932bd7cS[email protected] }
4345932bd7cS[email protected] 
4355932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
4365932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
4375932bd7cS[email protected]     l2cap_stop_rtx(channel);
438528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
439528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
440528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
4415932bd7cS[email protected] }
4425932bd7cS[email protected] 
44371de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
444ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
445ac301f95S[email protected] }
446ac301f95S[email protected] 
447df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
448235946f1SMatthias Ringwald     return (psm == BLUETOOTH_PROTOCOL_SDP) && (!require_security_level2_for_outgoing_sdp);
449df3354fcS[email protected] }
4505932bd7cS[email protected] 
4517f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
452a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4539da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
454b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
455b1d43497Smatthias.ringwald     }
456b1d43497Smatthias.ringwald 
4579da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
4582a373862S[email protected]     hci_reserve_packet_buffer();
459facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
46058de5610Smatthias.ringwald     va_list argptr;
46158de5610Smatthias.ringwald     va_start(argptr, identifier);
46270efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
46358de5610Smatthias.ringwald     va_end(argptr);
4649da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
465826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
46658de5610Smatthias.ringwald }
46758de5610Smatthias.ringwald 
468f511cefaSMatthias Ringwald // assumption - only on Classic connections
469b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
470b1d43497Smatthias.ringwald 
471c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
472c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
473c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
474c8b9416aS[email protected]     }
475c8b9416aS[email protected] 
47658de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
477b1d43497Smatthias.ringwald     if (!channel) {
4789da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
479b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4806218e6f1Smatthias.ringwald     }
4816218e6f1Smatthias.ringwald 
482fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4839da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
484a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
485a35252c8S[email protected]     }
486a35252c8S[email protected] 
487fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
488b1d43497Smatthias.ringwald 
489f511cefaSMatthias Ringwald     // set non-flushable packet boundary flag if supported on Controller
490facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
491f511cefaSMatthias Ringwald     uint8_t packet_boundary_flag = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
492f511cefaSMatthias Ringwald     l2cap_setup_header(acl_buffer, channel->con_handle, packet_boundary_flag, channel->remote_cid, len);
49358de5610Smatthias.ringwald     // send
4947f107edaSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
49558de5610Smatthias.ringwald }
49658de5610Smatthias.ringwald 
497f511cefaSMatthias Ringwald // assumption - only on Classic connections
498ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
499b1d43497Smatthias.ringwald 
500a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
501a35252c8S[email protected]     if (!channel) {
502ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
503a35252c8S[email protected]         return -1;   // TODO: define error
504a35252c8S[email protected]     }
505a35252c8S[email protected] 
506f0efaa57S[email protected]     if (len > channel->remote_mtu){
507ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
508f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
509f0efaa57S[email protected]     }
510f0efaa57S[email protected] 
511fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
512ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
513b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
514b1d43497Smatthias.ringwald     }
515b1d43497Smatthias.ringwald 
5162a373862S[email protected]     hci_reserve_packet_buffer();
517facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
518b1d43497Smatthias.ringwald 
519b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
520b1d43497Smatthias.ringwald 
521b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
522b1d43497Smatthias.ringwald }
523b1d43497Smatthias.ringwald 
524fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
525fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5260e37e417S[email protected] }
5270e37e417S[email protected] 
52828ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
52928ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
53028ca2b46S[email protected] }
53128ca2b46S[email protected] 
53228ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
53328ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
53428ca2b46S[email protected] }
53509e9d05bSMatthias Ringwald #endif
53628ca2b46S[email protected] 
53728ca2b46S[email protected] 
53809e9d05bSMatthias Ringwald #ifdef ENABLE_BLE
53909e9d05bSMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
54009e9d05bSMatthias Ringwald 
54109e9d05bSMatthias Ringwald     if (!hci_can_send_acl_packet_now(handle)){
54209e9d05bSMatthias Ringwald         log_info("l2cap_send_le_signaling_packet, cannot send");
54309e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
54409e9d05bSMatthias Ringwald     }
54509e9d05bSMatthias Ringwald 
54609e9d05bSMatthias Ringwald     // log_info("l2cap_send_le_signaling_packet type %u", cmd);
54709e9d05bSMatthias Ringwald     hci_reserve_packet_buffer();
54809e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
54909e9d05bSMatthias Ringwald     va_list argptr;
55009e9d05bSMatthias Ringwald     va_start(argptr, identifier);
55109e9d05bSMatthias Ringwald     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
55209e9d05bSMatthias Ringwald     va_end(argptr);
55309e9d05bSMatthias Ringwald     // log_info("l2cap_send_le_signaling_packet con %u!", handle);
55409e9d05bSMatthias Ringwald     return hci_send_acl_packet_buffer(len);
55509e9d05bSMatthias Ringwald }
55609e9d05bSMatthias Ringwald #endif
55709e9d05bSMatthias Ringwald 
55809e9d05bSMatthias Ringwald uint16_t l2cap_max_mtu(void){
55909e9d05bSMatthias Ringwald     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
56009e9d05bSMatthias Ringwald }
56109e9d05bSMatthias Ringwald 
56209e9d05bSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
56309e9d05bSMatthias Ringwald     return l2cap_max_mtu();
56409e9d05bSMatthias Ringwald }
565b1d43497Smatthias.ringwald 
5668158c421Smatthias.ringwald // MARK: L2CAP_RUN
5672cd0be45Smatthias.ringwald // process outstanding signaling tasks
5687f02f414SMatthias Ringwald static void l2cap_run(void){
5692b83fb7dSmatthias.ringwald 
57022c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
57122c29ab4SMatthias Ringwald 
5722b83fb7dSmatthias.ringwald     // check pending signaling responses
5732b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5742b83fb7dSmatthias.ringwald 
5752b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
576a35252c8S[email protected] 
577a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
578a35252c8S[email protected] 
5792b83fb7dSmatthias.ringwald         uint8_t  sig_id        = signaling_responses[0].sig_id;
580e74c5f58SMatthias Ringwald         uint8_t  response_code = signaling_responses[0].code;
5812b360848Smatthias.ringwald         uint16_t infoType      = signaling_responses[0].data;  // INFORMATION_REQUEST
58263a7246aSmatthias.ringwald         uint16_t result        = signaling_responses[0].data;  // CONNECTION_REQUEST, COMMAND_REJECT
583b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC
584b3264428SMatthias Ringwald         uint16_t source_cid    = signaling_responses[0].cid;   // CONNECTION_REQUEST
585b3264428SMatthias Ringwald #endif
586f3963406SMatthias Ringwald         UNUSED(infoType);
58709e9d05bSMatthias Ringwald 
588f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
589f53da564S[email protected]         signaling_responses_pending--;
590f53da564S[email protected]         int i;
591f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
592f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
593f53da564S[email protected]         }
594f53da564S[email protected] 
595f53da564S[email protected]         switch (response_code){
59609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
5972b360848Smatthias.ringwald             case CONNECTION_REQUEST:
598e74c5f58SMatthias Ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, source_cid, 0, result, 0);
5992bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
6004d816277S[email protected]                 if (result == 0x0003){
6012bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
6024d816277S[email protected]                 }
6032b360848Smatthias.ringwald                 break;
6042b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
6052b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
6062b83fb7dSmatthias.ringwald                 break;
6072b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
6083b0484b3S[email protected]                 switch (infoType){
6093b0484b3S[email protected]                     case 1: { // Connectionless MTU
6103b0484b3S[email protected]                             uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
6113b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
6123b0484b3S[email protected]                         }
613202c8a4cSMatthias Ringwald                         break;
6143b0484b3S[email protected]                     case 2: { // Extended Features Supported
615462e630dS[email protected]                             // extended features request supported, features: fixed channels, unicast connectionless data reception
616462e630dS[email protected]                             uint32_t features = 0x280;
617a6bfadd3SMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
618a6bfadd3SMatthias Ringwald                             features |= 0x0008;
619a6bfadd3SMatthias Ringwald #endif
6203b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
6213b0484b3S[email protected]                         }
622202c8a4cSMatthias Ringwald                         break;
6233b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
6243b0484b3S[email protected]                             uint8_t map[8];
6253b0484b3S[email protected]                             memset(map, 0, 8);
626288636a2SMatthias Ringwald                             map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
6273b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
6283b0484b3S[email protected]                         }
629202c8a4cSMatthias Ringwald                         break;
6303b0484b3S[email protected]                     default:
6312b83fb7dSmatthias.ringwald                         // all other types are not supported
6322b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
6333b0484b3S[email protected]                         break;
6342b83fb7dSmatthias.ringwald                 }
6352b83fb7dSmatthias.ringwald                 break;
63663a7246aSmatthias.ringwald             case COMMAND_REJECT:
6375ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
63863f0ac45SMatthias Ringwald                 break;
63909e9d05bSMatthias Ringwald #endif
640a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
64163f0ac45SMatthias Ringwald             case LE_CREDIT_BASED_CONNECTION_REQUEST:
64263f0ac45SMatthias Ringwald                 l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result);
64363f0ac45SMatthias Ringwald                 break;
64470efece1S[email protected]             case COMMAND_REJECT_LE:
64570efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
64663a7246aSmatthias.ringwald                 break;
64770efece1S[email protected] #endif
6482b83fb7dSmatthias.ringwald             default:
6492b83fb7dSmatthias.ringwald                 // should not happen
6502b83fb7dSmatthias.ringwald                 break;
6512b83fb7dSmatthias.ringwald         }
6522b83fb7dSmatthias.ringwald     }
6532b83fb7dSmatthias.ringwald 
654665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
655f3963406SMatthias Ringwald     UNUSED(it);
65609e9d05bSMatthias Ringwald 
65709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
658*43ec931dSMatthias Ringwald     uint8_t  config_options[10];
659665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
660665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
661baf94f06S[email protected] 
662665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
66322c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6642cd0be45Smatthias.ringwald         switch (channel->state){
6652cd0be45Smatthias.ringwald 
666df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
667ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
668fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
669a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
670ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
671fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
672ad671560S[email protected]                 }
673ad671560S[email protected]                 break;
674ad671560S[email protected] 
67502b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
676baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
67764472d52Smatthias.ringwald                 // send connection request - set state first
67864472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
67902b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6808f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
68102b22dc4Smatthias.ringwald                 break;
68202b22dc4Smatthias.ringwald 
683e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
684fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
68522c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
686fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
687e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6889dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
689665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
690d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
691e7ff783cSmatthias.ringwald                 break;
692e7ff783cSmatthias.ringwald 
693552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
694fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
695fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
69628ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
697fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
698552d92a1Smatthias.ringwald                 break;
699552d92a1Smatthias.ringwald 
7006fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
701fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7026fdcc387Smatthias.ringwald                 // success, start l2cap handshake
703b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7046fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
705fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
7065932bd7cS[email protected]                 l2cap_start_rtx(channel);
7076fdcc387Smatthias.ringwald                 break;
7086fdcc387Smatthias.ringwald 
709fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
710fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
71173cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
71263a7246aSmatthias.ringwald                     uint16_t flags = 0;
71328ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
71463a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
71563a7246aSmatthias.ringwald                         flags = 1;
71663a7246aSmatthias.ringwald                     } else {
71728ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
71863a7246aSmatthias.ringwald                     }
71963a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
720fc64f94aSMatthias 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);
72163a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
72263a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
72363a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
724f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
725fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
72663a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
72763a7246aSmatthias.ringwald                     } else {
728fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
72963a7246aSmatthias.ringwald                     }
73063a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
731fa8473a4Smatthias.ringwald                 }
73273cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
73328ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
73428ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
735b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
736*43ec931dSMatthias Ringwald                     int send_retransmission_and_flow_control_option = 0;
737*43ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
738*43ec931dSMatthias Ringwald                     if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
739*43ec931dSMatthias Ringwald                         send_retransmission_and_flow_control_option = 1;
740*43ec931dSMatthias Ringwald                     }
741*43ec931dSMatthias Ringwald #endif
742*43ec931dSMatthias Ringwald                     if (send_retransmission_and_flow_control_option){
743*43ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
744*43ec931dSMatthias Ringwald                         config_options[0] = 0x04;   // RETRANSMISSION AND FLOW CONTROL OPTION
745*43ec931dSMatthias Ringwald                         config_options[1] = 9;      // length
746*43ec931dSMatthias Ringwald                         config_options[2] = (uint8_t) channel->mode;
747*43ec931dSMatthias Ringwald                         config_options[3] = 1;      // TxWindows size
748*43ec931dSMatthias Ringwald                         config_options[4] = 1;      // max retransmit
749*43ec931dSMatthias Ringwald                         little_endian_store_16( config_options, 5, 100); // Retransmission timeout: 100 ms
750*43ec931dSMatthias Ringwald                         little_endian_store_16( config_options, 5, 300); // Monitor timeout: 300 ms
751*43ec931dSMatthias Ringwald                         little_endian_store_16( config_options, 7, channel->local_mtu); // Max PDU size
752*43ec931dSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 10, &config_options);
753*43ec931dSMatthias Ringwald #endif
754*43ec931dSMatthias Ringwald                     } else {
755ae280e73Smatthias.ringwald                         config_options[0] = 1; // MTU
756ae280e73Smatthias.ringwald                         config_options[1] = 2; // len param
757f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
758fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
759*43ec931dSMatthias Ringwald                     }
7605932bd7cS[email protected]                     l2cap_start_rtx(channel);
761fa8473a4Smatthias.ringwald                 }
762fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
763552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
764552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
765fa8473a4Smatthias.ringwald                 }
766552d92a1Smatthias.ringwald                 break;
767552d92a1Smatthias.ringwald 
768e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
769fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
77022c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
771fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7725932bd7cS[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 :)
773756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
774e7ff783cSmatthias.ringwald                 break;
775e7ff783cSmatthias.ringwald 
776e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
777fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
778b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7792cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
780fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7812cd0be45Smatthias.ringwald                 break;
7822cd0be45Smatthias.ringwald             default:
7832cd0be45Smatthias.ringwald                 break;
7842cd0be45Smatthias.ringwald         }
7852cd0be45Smatthias.ringwald     }
78609e9d05bSMatthias Ringwald #endif
787da886c03S[email protected] 
788cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
789efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
790efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7917f107edaSMatthias Ringwald         uint8_t  * acl_buffer;
7927f107edaSMatthias Ringwald         uint8_t  * l2cap_payload;
7937f107edaSMatthias Ringwald         uint16_t pos;
7947f107edaSMatthias Ringwald         uint16_t payload_size;
795efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
796efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
797efedfb4cSMatthias Ringwald         switch (channel->state){
7985cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7995cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
8005cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
8015cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
8021b8b8d05SMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
80363f0ac45SMatthias Ringwald                 channel->credits_incoming =  channel->new_credits_incoming;
80463f0ac45SMatthias Ringwald                 channel->new_credits_incoming = 0;
80563f0ac45SMatthias 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);
8065cb87675SMatthias Ringwald                 break;
80723017473SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
80823017473SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
80923017473SMatthias Ringwald                 // TODO: support larger MPS
81023017473SMatthias Ringwald                 channel->state = L2CAP_STATE_OPEN;
81163f0ac45SMatthias Ringwald                 channel->credits_incoming =  channel->new_credits_incoming;
81263f0ac45SMatthias Ringwald                 channel->new_credits_incoming = 0;
81363f0ac45SMatthias 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);
81464e11ca9SMatthias Ringwald                 // notify client
81544276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, 0);
81623017473SMatthias Ringwald                 break;
817e7d0c9aaSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
818e7d0c9aaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
819e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
82063f0ac45SMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason);
821e7d0c9aaSMatthias Ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
822e7d0c9aaSMatthias Ringwald                 l2cap_stop_rtx(channel);
823e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
824e7d0c9aaSMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
825e7d0c9aaSMatthias Ringwald                 break;
8267f107edaSMatthias Ringwald             case L2CAP_STATE_OPEN:
82785aeef60SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
82885aeef60SMatthias Ringwald 
82985aeef60SMatthias Ringwald                 // send credits
83085aeef60SMatthias Ringwald                 if (channel->new_credits_incoming){
83185aeef60SMatthias Ringwald                     log_info("l2cap: sending %u credits", channel->new_credits_incoming);
83285aeef60SMatthias Ringwald                     channel->local_sig_id = l2cap_next_sig_id();
83385aeef60SMatthias Ringwald                     uint16_t new_credits = channel->new_credits_incoming;
83485aeef60SMatthias Ringwald                     channel->new_credits_incoming = 0;
83585aeef60SMatthias Ringwald                     channel->credits_incoming += new_credits;
83685aeef60SMatthias Ringwald                     l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits);
83785aeef60SMatthias Ringwald                     break;
83885aeef60SMatthias Ringwald                 }
83985aeef60SMatthias Ringwald 
84085aeef60SMatthias Ringwald                 // send data
8417f107edaSMatthias Ringwald                 if (!channel->send_sdu_buffer) break;
8427f107edaSMatthias Ringwald                 if (!channel->credits_outgoing) break;
84385aeef60SMatthias Ringwald 
8447f107edaSMatthias Ringwald                 // send part of SDU
8457f107edaSMatthias Ringwald                 hci_reserve_packet_buffer();
8467f107edaSMatthias Ringwald                 acl_buffer = hci_get_outgoing_packet_buffer();
8477f107edaSMatthias Ringwald                 l2cap_payload = acl_buffer + 8;
8487f107edaSMatthias Ringwald                 pos = 0;
8497f107edaSMatthias Ringwald                 if (!channel->send_sdu_pos){
8507f107edaSMatthias Ringwald                     // store SDU len
8517f107edaSMatthias Ringwald                     channel->send_sdu_pos += 2;
8527f107edaSMatthias Ringwald                     little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len);
8537f107edaSMatthias Ringwald                     pos += 2;
8547f107edaSMatthias Ringwald                 }
8557f107edaSMatthias Ringwald                 payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos);
85685aeef60SMatthias Ringwald                 log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing);
8577f107edaSMatthias Ringwald                 memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len
8587f107edaSMatthias Ringwald                 pos += payload_size;
8597f107edaSMatthias Ringwald                 channel->send_sdu_pos += payload_size;
860f511cefaSMatthias Ringwald                 l2cap_setup_header(acl_buffer, channel->con_handle, 0, channel->remote_cid, pos);
8617f107edaSMatthias Ringwald                 // done
8627f107edaSMatthias Ringwald 
86385aeef60SMatthias Ringwald                 channel->credits_outgoing--;
8647f107edaSMatthias Ringwald 
8657f107edaSMatthias Ringwald                 if (channel->send_sdu_pos >= channel->send_sdu_len + 2){
8667f107edaSMatthias Ringwald                     channel->send_sdu_buffer = NULL;
86744276248SMatthias Ringwald                     // send done event
86844276248SMatthias Ringwald                     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT);
86944276248SMatthias Ringwald                     // inform about can send now
87044276248SMatthias Ringwald                     l2cap_le_notify_channel_can_send(channel);
8717f107edaSMatthias Ringwald                 }
8727f107edaSMatthias Ringwald                 hci_send_acl_packet_buffer(8 + pos);
8737f107edaSMatthias Ringwald                 break;
874828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
875828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
876828a7f7aSMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
877828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
878828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
879828a7f7aSMatthias Ringwald                 break;
880828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
881828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
882828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
883828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
884828a7f7aSMatthias Ringwald                 l2cap_le_finialize_channel_close(channel);  // -- remove from list
885828a7f7aSMatthias Ringwald                 break;
886efedfb4cSMatthias Ringwald             default:
887efedfb4cSMatthias Ringwald                 break;
888efedfb4cSMatthias Ringwald         }
889efedfb4cSMatthias Ringwald     }
89009e9d05bSMatthias Ringwald #endif
891efedfb4cSMatthias Ringwald 
89209e9d05bSMatthias Ringwald #ifdef ENABLE_BLE
893da886c03S[email protected]     // send l2cap con paramter update if necessary
894da886c03S[email protected]     hci_connections_get_iterator(&it);
895665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
896665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
8975d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
898b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
899da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
900b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
901b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
902b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
903b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
904b68d7bc3SMatthias Ringwald                 break;
905da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
906b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
907b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
908da886c03S[email protected]                 break;
909da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
910b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
911b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
912da886c03S[email protected]                 break;
913da886c03S[email protected]             default:
914da886c03S[email protected]                 break;
915da886c03S[email protected]         }
916da886c03S[email protected]     }
9174d7157c3S[email protected] #endif
918da886c03S[email protected] 
91922c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
9202cd0be45Smatthias.ringwald }
9212cd0be45Smatthias.ringwald 
92209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
923fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
9242df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
9255533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
9262df5dadcS[email protected]         // success, start l2cap handshake
927fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
9282df5dadcS[email protected]         // check remote SSP feature first
9292df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
9302df5dadcS[email protected]     }
9312df5dadcS[email protected] }
9322df5dadcS[email protected] 
9332df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
9342df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
9352df5dadcS[email protected] 
9362df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
937ac301f95S[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));
938fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
9392df5dadcS[email protected]         // request security level 2
9402df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
941fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
9422df5dadcS[email protected]         return;
9432df5dadcS[email protected]     }
9442df5dadcS[email protected]     // fine, go ahead
9452df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
9462df5dadcS[email protected] }
94709e9d05bSMatthias Ringwald #endif
9482df5dadcS[email protected] 
94909e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS
950da144af5SMatthias 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,
951da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
952da144af5SMatthias Ringwald 
953da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
954da144af5SMatthias Ringwald     if (!channel) {
955da144af5SMatthias Ringwald         return NULL;
956da144af5SMatthias Ringwald     }
957da144af5SMatthias Ringwald 
958da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
959da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
960da144af5SMatthias Ringwald 
961da144af5SMatthias Ringwald     // fill in
962da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
963da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
964da144af5SMatthias Ringwald     channel->address_type = address_type;
965da144af5SMatthias Ringwald     channel->psm = psm;
966da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
967da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
968da144af5SMatthias Ringwald     channel->required_security_level = security_level;
969da144af5SMatthias Ringwald 
970da144af5SMatthias Ringwald     //
971da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
972da144af5SMatthias Ringwald     channel->con_handle = 0;
973da144af5SMatthias Ringwald 
974da144af5SMatthias Ringwald     // set initial state
975da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
976da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
977da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
978da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
979da144af5SMatthias Ringwald     return channel;
980da144af5SMatthias Ringwald }
98109e9d05bSMatthias Ringwald #endif
98209e9d05bSMatthias Ringwald 
98309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
984da144af5SMatthias Ringwald 
9859077cb15SMatthias Ringwald /**
9869077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
9879077cb15SMatthias Ringwald  * @param packet_handler
9889077cb15SMatthias Ringwald  * @param address
9899077cb15SMatthias Ringwald  * @param psm
9909077cb15SMatthias Ringwald  * @param mtu
9919077cb15SMatthias Ringwald  * @param local_cid
9929077cb15SMatthias Ringwald  */
9939077cb15SMatthias Ringwald 
9949d139fbaSMatthias 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){
9959d139fbaSMatthias Ringwald     // limit MTU to the size of our outtgoing HCI buffer
9969d139fbaSMatthias Ringwald     uint16_t local_mtu = btstack_min(mtu, l2cap_max_mtu());
997da144af5SMatthias Ringwald 
9989d139fbaSMatthias 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);
999da144af5SMatthias Ringwald 
1000da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
1001fc64f94aSMatthias Ringwald     if (!channel) {
10029077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
10039077cb15SMatthias Ringwald     }
10049077cb15SMatthias Ringwald 
10059077cb15SMatthias Ringwald     // add to connections list
1006fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
10079077cb15SMatthias Ringwald 
10089077cb15SMatthias Ringwald     // store local_cid
10099077cb15SMatthias Ringwald     if (out_local_cid){
1010fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
10119077cb15SMatthias Ringwald     }
10129077cb15SMatthias Ringwald 
10139077cb15SMatthias Ringwald     // check if hci connection is already usable
10149077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
10159077cb15SMatthias Ringwald     if (conn){
1016add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
1017fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
10189077cb15SMatthias Ringwald         // check if remote supported fearures are already received
10199077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
1020fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
10219077cb15SMatthias Ringwald         }
10229077cb15SMatthias Ringwald     }
10239077cb15SMatthias Ringwald 
10249077cb15SMatthias Ringwald     l2cap_run();
10259077cb15SMatthias Ringwald 
10269077cb15SMatthias Ringwald     return 0;
10279077cb15SMatthias Ringwald }
10289077cb15SMatthias Ringwald 
1029ce8f182eSMatthias Ringwald void
1030ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
1031e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
1032b35f641cSmatthias.ringwald     // find channel for local_cid
1033b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1034f62db1e3Smatthias.ringwald     if (channel) {
1035e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1036f62db1e3Smatthias.ringwald     }
10372cd0be45Smatthias.ringwald     // process
10382cd0be45Smatthias.ringwald     l2cap_run();
103943625864Smatthias.ringwald }
10401e6aba47Smatthias.ringwald 
1041afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
1042665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1043665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1044665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1045665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1046058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
1047c22aecc9S[email protected]         // channel for this address found
1048c22aecc9S[email protected]         switch (channel->state){
1049c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
1050c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
1051afde0c52Smatthias.ringwald                 // failure, forward error code
1052afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
1053afde0c52Smatthias.ringwald                 // discard channel
10549dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1055665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1056d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
1057c22aecc9S[email protected]                 break;
1058c22aecc9S[email protected]             default:
1059c22aecc9S[email protected]                 break;
1060afde0c52Smatthias.ringwald         }
1061afde0c52Smatthias.ringwald     }
1062afde0c52Smatthias.ringwald }
1063afde0c52Smatthias.ringwald 
1064afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
1065665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1066665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1067665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1068665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1069058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
10702df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
1071afde0c52Smatthias.ringwald         }
1072afde0c52Smatthias.ringwald     }
10736fdcc387Smatthias.ringwald     // process
10746fdcc387Smatthias.ringwald     l2cap_run();
1075afde0c52Smatthias.ringwald }
107609e9d05bSMatthias Ringwald #endif
1077b448a0e7Smatthias.ringwald 
107833c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
107909e9d05bSMatthias Ringwald 
108009e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
108133c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
108233c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
108333c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
108433c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
108533c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
1086fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
108733c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
108834e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
108933c40538SMatthias Ringwald     }
109009e9d05bSMatthias Ringwald #endif
109144276248SMatthias Ringwald 
10922125de09SMatthias Ringwald     int i;
10932125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
1094773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
10952125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
109635454696SMatthias Ringwald         int can_send = 0;
109734e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
109835454696SMatthias Ringwald #ifdef ENABLE_BLE
109934e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
110035454696SMatthias Ringwald #endif
110134e7e577SMatthias Ringwald         } else {
110235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
110334e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
110435454696SMatthias Ringwald #endif
110534e7e577SMatthias Ringwald         }
110634e7e577SMatthias Ringwald         if (!can_send) continue;
110734e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
110834e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
11092125de09SMatthias Ringwald     }
111033c40538SMatthias Ringwald }
111133c40538SMatthias Ringwald 
1112d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
1113afde0c52Smatthias.ringwald 
11149ec2630cSMatthias Ringwald     UNUSED(packet_type);
11159ec2630cSMatthias Ringwald     UNUSED(cid);
11169ec2630cSMatthias Ringwald     UNUSED(size);
11179ec2630cSMatthias Ringwald 
1118afde0c52Smatthias.ringwald     bd_addr_t address;
1119afde0c52Smatthias.ringwald     hci_con_handle_t handle;
11202d00edd4Smatthias.ringwald     int hci_con_used;
112109e9d05bSMatthias Ringwald     btstack_linked_list_iterator_t it;
112209e9d05bSMatthias Ringwald 
112309e9d05bSMatthias Ringwald     // avoid unused warnings
1124f3963406SMatthias Ringwald     UNUSED(address);
1125f3963406SMatthias Ringwald     UNUSED(hci_con_used);
1126f3963406SMatthias Ringwald     UNUSED(it);
1127f22209dfSMatthias Ringwald     UNUSED(handle);
1128afde0c52Smatthias.ringwald 
11290e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1130afde0c52Smatthias.ringwald 
113109e9d05bSMatthias Ringwald         // Notify channel packet handler if they can send now
113209e9d05bSMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
113309e9d05bSMatthias Ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
113409e9d05bSMatthias Ringwald             l2cap_run();    // try sending signaling packets first
113509e9d05bSMatthias Ringwald             l2cap_notify_channel_can_send();
113609e9d05bSMatthias Ringwald             break;
113709e9d05bSMatthias Ringwald 
113809e9d05bSMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
113909e9d05bSMatthias Ringwald             l2cap_run();    // try sending signaling packets first
114009e9d05bSMatthias Ringwald             break;
114109e9d05bSMatthias Ringwald 
114209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1143afde0c52Smatthias.ringwald         // handle connection complete events
1144afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1145724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
1146afde0c52Smatthias.ringwald             if (packet[2] == 0){
1147f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
1148afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
1149afde0c52Smatthias.ringwald             } else {
1150afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
1151afde0c52Smatthias.ringwald             }
1152afde0c52Smatthias.ringwald             break;
1153afde0c52Smatthias.ringwald 
1154afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1155afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1156073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1157afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1158724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1159afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1160afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
116103cfbabcSmatthias.ringwald                 }
11621e6aba47Smatthias.ringwald             }
116339d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
116439d59809Smatthias.ringwald             break;
116509e9d05bSMatthias Ringwald #endif
116627a923d0Smatthias.ringwald 
11671e6aba47Smatthias.ringwald         // handle disconnection complete events
1168afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1169c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
117009e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1171d0662982SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1172665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1173665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1174665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1175fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
117615ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
11779dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1178665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1179d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
118027a923d0Smatthias.ringwald             }
118109e9d05bSMatthias Ringwald #endif
1182a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1183d0662982SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1184991fea48SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1185991fea48SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1186991fea48SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1187991fea48SMatthias Ringwald                 if (channel->con_handle != handle) continue;
1188991fea48SMatthias Ringwald                 l2cap_emit_channel_closed(channel);
1189991fea48SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1190991fea48SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
1191991fea48SMatthias Ringwald             }
1192991fea48SMatthias Ringwald #endif
1193afde0c52Smatthias.ringwald             break;
1194fcadd0caSmatthias.ringwald 
1195ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
119609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1197afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1198f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1199bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
120080ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
12012d00edd4Smatthias.ringwald             hci_con_used = 0;
1202665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1203665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1204665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1205fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
12062d00edd4Smatthias.ringwald                 hci_con_used = 1;
1207c22aecc9S[email protected]                 break;
1208ee091cf1Smatthias.ringwald             }
12092d00edd4Smatthias.ringwald             if (hci_con_used) break;
1210d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
12119edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1212afde0c52Smatthias.ringwald             break;
1213ee091cf1Smatthias.ringwald 
1214df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1215f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1216665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1217665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1218665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1219fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
12202df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1221df3354fcS[email protected]                 break;
1222df3354fcS[email protected]             }
1223c22aecc9S[email protected]             break;
1224df3354fcS[email protected] 
12255611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1226f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1227bd63148eS[email protected]             log_info("l2cap - security level update");
1228665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1229665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1230665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1231fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
12325533f01eS[email protected] 
1233bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1234bd63148eS[email protected] 
1235e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
12365533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
12375533f01eS[email protected] 
1238df3354fcS[email protected]                 switch (channel->state){
1239df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
12405533f01eS[email protected]                         if (actual_level >= required_level){
1241f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
124244276248SMatthias Ringwald                             l2cap_emit_incoming_connection(channel);
12431eb2563eS[email protected]                         } else {
1244775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
12451eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
12461eb2563eS[email protected]                         }
1247df3354fcS[email protected]                         break;
1248df3354fcS[email protected] 
1249df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
12505533f01eS[email protected]                         if (actual_level >= required_level){
1251df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1252df3354fcS[email protected]                         } else {
1253df3354fcS[email protected]                             // disconnnect, authentication not good enough
1254df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1255df3354fcS[email protected]                         }
1256df3354fcS[email protected]                         break;
1257df3354fcS[email protected] 
1258df3354fcS[email protected]                     default:
1259df3354fcS[email protected]                         break;
1260df3354fcS[email protected]                 }
1261f85a9399S[email protected]             }
1262f85a9399S[email protected]             break;
126309e9d05bSMatthias Ringwald #endif
1264f85a9399S[email protected] 
1265afde0c52Smatthias.ringwald         default:
1266afde0c52Smatthias.ringwald             break;
1267afde0c52Smatthias.ringwald     }
1268afde0c52Smatthias.ringwald 
1269bd63148eS[email protected]     l2cap_run();
12701e6aba47Smatthias.ringwald }
12711e6aba47Smatthias.ringwald 
1272e74c5f58SMatthias 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){
12734cf56b4aSmatthias.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."
12742b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
12752b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
12762b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
12772b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
1278e74c5f58SMatthias Ringwald         signaling_responses[signaling_responses_pending].cid = cid;
12792b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
12802b360848Smatthias.ringwald         signaling_responses_pending++;
12812b360848Smatthias.ringwald         l2cap_run();
12822b360848Smatthias.ringwald     }
12832b360848Smatthias.ringwald }
12842b360848Smatthias.ringwald 
128509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
128609e9d05bSMatthias Ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
128709e9d05bSMatthias Ringwald     channel->remote_sig_id = identifier;
128809e9d05bSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
128909e9d05bSMatthias Ringwald     l2cap_run();
129009e9d05bSMatthias Ringwald }
129109e9d05bSMatthias Ringwald 
1292b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1293645658c9Smatthias.ringwald 
12949da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1295645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1296645658c9Smatthias.ringwald     if (!service) {
1297645658c9Smatthias.ringwald         // 0x0002 PSM not supported
1298e74c5f58SMatthias Ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0002);
1299645658c9Smatthias.ringwald         return;
1300645658c9Smatthias.ringwald     }
1301645658c9Smatthias.ringwald 
13025061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1303645658c9Smatthias.ringwald     if (!hci_connection) {
13042b360848Smatthias.ringwald         //
13059da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1306645658c9Smatthias.ringwald         return;
1307645658c9Smatthias.ringwald     }
13082bd8b7e7S[email protected] 
1309645658c9Smatthias.ringwald     // alloc structure
13109da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1311da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1312da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
13132b360848Smatthias.ringwald     if (!channel){
13142b360848Smatthias.ringwald         // 0x0004 No resources available
1315e74c5f58SMatthias Ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, 0x0004);
13162b360848Smatthias.ringwald         return;
13172b360848Smatthias.ringwald     }
1318da144af5SMatthias Ringwald 
1319fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1320b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1321b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1322645658c9Smatthias.ringwald 
1323f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
13242985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
13252985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
13269775e25bSmatthias.ringwald     }
13279775e25bSmatthias.ringwald 
1328645658c9Smatthias.ringwald     // set initial state
1329df3354fcS[email protected]     channel->state =      L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1330a24785d0SMatthias Ringwald     channel->state_var  = (L2CAP_CHANNEL_STATE_VAR) (L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND | L2CAP_CHANNEL_STATE_VAR_INCOMING);
1331e405ae81Smatthias.ringwald 
1332645658c9Smatthias.ringwald     // add to connections list
1333665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1334645658c9Smatthias.ringwald 
1335f85a9399S[email protected]     // assert security requirements
13361eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1337e405ae81Smatthias.ringwald }
1338645658c9Smatthias.ringwald 
1339ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1340e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1341b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1342e405ae81Smatthias.ringwald     if (!channel) {
1343ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1344e405ae81Smatthias.ringwald         return;
1345e405ae81Smatthias.ringwald     }
1346e405ae81Smatthias.ringwald 
1347*43ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
1348*43ec931dSMatthias Ringwald     // configure L2CAP Basic mode
1349*43ec931dSMatthias Ringwald     channel->mode  = L2CAP_CHANNEL_MODE_BASIC;
1350*43ec931dSMatthias Ringwald #endif
1351*43ec931dSMatthias Ringwald 
1352552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1353e405ae81Smatthias.ringwald 
1354552d92a1Smatthias.ringwald     // process
1355552d92a1Smatthias.ringwald     l2cap_run();
1356e405ae81Smatthias.ringwald }
1357645658c9Smatthias.ringwald 
1358*43ec931dSMatthias Ringwald 
1359*43ec931dSMatthias Ringwald #ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
1360*43ec931dSMatthias Ringwald void l2cap_accept_ertm_connection(uint16_t local_cid){
1361*43ec931dSMatthias Ringwald     log_info("L2CAP_ACCEPT_ERTM_CONNECTION local_cid 0x%x", local_cid);
1362*43ec931dSMatthias Ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1363*43ec931dSMatthias Ringwald     if (!channel) {
1364*43ec931dSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1365*43ec931dSMatthias Ringwald         return;
1366*43ec931dSMatthias Ringwald     }
1367*43ec931dSMatthias Ringwald 
1368*43ec931dSMatthias Ringwald     // configure L2CAP ERTM
1369*43ec931dSMatthias Ringwald     channel->mode  = L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION;
1370*43ec931dSMatthias Ringwald 
1371*43ec931dSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1372*43ec931dSMatthias Ringwald 
1373*43ec931dSMatthias Ringwald     // process
1374*43ec931dSMatthias Ringwald     l2cap_run();
1375*43ec931dSMatthias Ringwald }
1376*43ec931dSMatthias Ringwald #endif
1377*43ec931dSMatthias Ringwald 
1378*43ec931dSMatthias Ringwald 
13797ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
13807ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1381b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1382e405ae81Smatthias.ringwald     if (!channel) {
1383ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1384e405ae81Smatthias.ringwald         return;
1385e405ae81Smatthias.ringwald     }
1386e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
13877ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1388e7ff783cSmatthias.ringwald     l2cap_run();
1389645658c9Smatthias.ringwald }
1390645658c9Smatthias.ringwald 
13917f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1392b1988dceSmatthias.ringwald 
1393b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1394b1988dceSmatthias.ringwald 
1395f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
139663a7246aSmatthias.ringwald     if (flags & 1) {
139763a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
139863a7246aSmatthias.ringwald     }
139963a7246aSmatthias.ringwald 
14002784b77dSmatthias.ringwald     // accept the other's configuration options
1401f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
14023de7c0caSmatthias.ringwald     uint16_t pos     = 8;
14033de7c0caSmatthias.ringwald     while (pos < end_pos){
140463a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
140563a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
140663a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
140763a7246aSmatthias.ringwald         pos++;
14081dc511deSmatthias.ringwald         uint8_t length = command[pos++];
14091dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
141063a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1411f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
14129d139fbaSMatthias Ringwald             if (channel->remote_mtu > l2cap_max_mtu()){
14139d139fbaSMatthias Ringwald                 log_info("Remote MTU %u larger than outgoing buffer, only using MTU = %u", channel->remote_mtu, l2cap_max_mtu());
14149d139fbaSMatthias Ringwald                 channel->remote_mtu = l2cap_max_mtu();
14159d139fbaSMatthias Ringwald             }
141663a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
141763a7246aSmatthias.ringwald         }
14180fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
14190fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1420f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
14210fe7a9d0S[email protected]         }
142263a7246aSmatthias.ringwald         // check for unknown options
142363a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1424c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
142563a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
14261dc511deSmatthias.ringwald         }
14271dc511deSmatthias.ringwald         pos += length;
14281dc511deSmatthias.ringwald     }
14292784b77dSmatthias.ringwald }
14302784b77dSmatthias.ringwald 
1431fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
14329da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
143373cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
143473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1435019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1436019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1437fa8473a4Smatthias.ringwald     return 1;
1438fa8473a4Smatthias.ringwald }
1439fa8473a4Smatthias.ringwald 
1440fa8473a4Smatthias.ringwald 
14417f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
14421e6aba47Smatthias.ringwald 
144300d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
144400d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
144538e5900eSmatthias.ringwald     uint16_t result = 0;
14461e6aba47Smatthias.ringwald 
14479da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1448b35f641cSmatthias.ringwald 
14499a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
14509a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
14519a011532Smatthias.ringwald         switch (channel->state){
1452fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
14539a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
14542b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
14559a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
14569a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
14579a011532Smatthias.ringwald                 break;
14589a011532Smatthias.ringwald 
14599a011532Smatthias.ringwald             default:
14609a011532Smatthias.ringwald                 // ignore in other states
14619a011532Smatthias.ringwald                 break;
14629a011532Smatthias.ringwald         }
14639a011532Smatthias.ringwald         return;
14649a011532Smatthias.ringwald     }
14659a011532Smatthias.ringwald 
146656081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
14671e6aba47Smatthias.ringwald     switch (channel->state) {
14681e6aba47Smatthias.ringwald 
14691e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
14701e6aba47Smatthias.ringwald             switch (code){
14711e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
14725932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1473f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
147438e5900eSmatthias.ringwald                     switch (result) {
147538e5900eSmatthias.ringwald                         case 0:
1476169f8b28Smatthias.ringwald                             // successful connection
1477f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1478fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
147928ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
148038e5900eSmatthias.ringwald                             break;
148138e5900eSmatthias.ringwald                         case 1:
14825932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
14835932bd7cS[email protected]                             l2cap_start_ertx(channel);
148438e5900eSmatthias.ringwald                             break;
148538e5900eSmatthias.ringwald                         default:
1486eb920dbeSmatthias.ringwald                             // channel closed
1487eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1488f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
148938e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1490eb920dbeSmatthias.ringwald 
1491eb920dbeSmatthias.ringwald                             // drop link key if security block
1492eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
149315a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1494eb920dbeSmatthias.ringwald                             }
1495eb920dbeSmatthias.ringwald 
1496eb920dbeSmatthias.ringwald                             // discard channel
1497665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1498d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
149938e5900eSmatthias.ringwald                             break;
15001e6aba47Smatthias.ringwald                     }
15011e6aba47Smatthias.ringwald                     break;
150238e5900eSmatthias.ringwald 
150338e5900eSmatthias.ringwald                 default:
15041e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
150538e5900eSmatthias.ringwald                     break;
15061e6aba47Smatthias.ringwald             }
15071e6aba47Smatthias.ringwald             break;
15081e6aba47Smatthias.ringwald 
1509fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1510f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1511ae280e73Smatthias.ringwald             switch (code) {
1512ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
151328ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1514ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
151563a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
151663a7246aSmatthias.ringwald                         // only done if continuation not set
151763a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
151863a7246aSmatthias.ringwald                     }
1519ae280e73Smatthias.ringwald                     break;
15201e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
15215932bd7cS[email protected]                     l2cap_stop_rtx(channel);
15225932bd7cS[email protected]                     switch (result){
15235932bd7cS[email protected]                         case 0: // success
15245932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
15255932bd7cS[email protected]                             break;
15265932bd7cS[email protected]                         case 4: // pending
15275932bd7cS[email protected]                             l2cap_start_ertx(channel);
15285932bd7cS[email protected]                             break;
15295932bd7cS[email protected]                         default:
1530fe9d8984S[email protected]                             // retry on negative result
1531fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1532fe9d8984S[email protected]                             break;
1533fe9d8984S[email protected]                     }
15345a67bd4aSmatthias.ringwald                     break;
15355a67bd4aSmatthias.ringwald                 default:
15365a67bd4aSmatthias.ringwald                     break;
15371e6aba47Smatthias.ringwald             }
1538fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1539fa8473a4Smatthias.ringwald                 // for open:
15405a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1541fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1542c8e4258aSmatthias.ringwald             }
1543c8e4258aSmatthias.ringwald             break;
1544f62db1e3Smatthias.ringwald 
1545f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1546f62db1e3Smatthias.ringwald             switch (code) {
1547f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
154827a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
154927a923d0Smatthias.ringwald                     break;
15505a67bd4aSmatthias.ringwald                 default:
15515a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
15525a67bd4aSmatthias.ringwald                     break;
155327a923d0Smatthias.ringwald             }
155427a923d0Smatthias.ringwald             break;
155584836b65Smatthias.ringwald 
155684836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
155784836b65Smatthias.ringwald             // @TODO handle incoming requests
155884836b65Smatthias.ringwald             break;
155984836b65Smatthias.ringwald 
156084836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
156184836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
156284836b65Smatthias.ringwald             break;
156310642e45Smatthias.ringwald         default:
156410642e45Smatthias.ringwald             break;
156527a923d0Smatthias.ringwald     }
15669da54300S[email protected]     // log_info("new state %u", channel->state);
156727a923d0Smatthias.ringwald }
156827a923d0Smatthias.ringwald 
156900d93d79Smatthias.ringwald 
15707f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
157100d93d79Smatthias.ringwald 
157200d93d79Smatthias.ringwald     // get code, signalind identifier and command len
157300d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
157400d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
157500d93d79Smatthias.ringwald 
157600d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
157700d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
1578e74c5f58SMatthias Ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
157900d93d79Smatthias.ringwald         return;
158000d93d79Smatthias.ringwald     }
158100d93d79Smatthias.ringwald 
158200d93d79Smatthias.ringwald     // general commands without an assigned channel
158300d93d79Smatthias.ringwald     switch(code) {
158400d93d79Smatthias.ringwald 
158500d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1586f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1587f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
158800d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
15892b83fb7dSmatthias.ringwald             return;
159000d93d79Smatthias.ringwald         }
159100d93d79Smatthias.ringwald 
15922b360848Smatthias.ringwald         case ECHO_REQUEST:
1593e74c5f58SMatthias Ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0, 0);
15942b83fb7dSmatthias.ringwald             return;
159500d93d79Smatthias.ringwald 
159600d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1597f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1598e74c5f58SMatthias Ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0, infoType);
15992b83fb7dSmatthias.ringwald             return;
160000d93d79Smatthias.ringwald         }
160100d93d79Smatthias.ringwald 
160200d93d79Smatthias.ringwald         default:
160300d93d79Smatthias.ringwald             break;
160400d93d79Smatthias.ringwald     }
160500d93d79Smatthias.ringwald 
160600d93d79Smatthias.ringwald 
160700d93d79Smatthias.ringwald     // Get potential destination CID
1608f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
160900d93d79Smatthias.ringwald 
161000d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1611665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1612665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1613665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1614665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1615fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
161600d93d79Smatthias.ringwald         if (code & 1) {
1617b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1618b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
161900d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
16204e32727eSmatthias.ringwald                 break;
162100d93d79Smatthias.ringwald             }
162200d93d79Smatthias.ringwald         } else {
1623b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
162400d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
162500d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
16264e32727eSmatthias.ringwald                 break;
162700d93d79Smatthias.ringwald             }
162800d93d79Smatthias.ringwald         }
162900d93d79Smatthias.ringwald     }
163000d93d79Smatthias.ringwald }
163109e9d05bSMatthias Ringwald #endif
163200d93d79Smatthias.ringwald 
1633e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
163409e9d05bSMatthias Ringwald 
163509e9d05bSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
163609e9d05bSMatthias Ringwald     uint8_t event[6];
163709e9d05bSMatthias Ringwald     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
163809e9d05bSMatthias Ringwald     event[1] = 4;
163909e9d05bSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
164009e9d05bSMatthias Ringwald     little_endian_store_16(event, 4, result);
164109e9d05bSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
164209e9d05bSMatthias Ringwald     if (!l2cap_event_packet_handler) return;
164309e9d05bSMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
164409e9d05bSMatthias Ringwald }
164509e9d05bSMatthias Ringwald 
1646c48b2a2cSMatthias Ringwald // @returns valid
1647c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1648e7d0c9aaSMatthias Ringwald     hci_connection_t * connection;
1649c48b2a2cSMatthias Ringwald     uint16_t result;
1650c48b2a2cSMatthias Ringwald     uint8_t  event[10];
165183fd9c76SMatthias Ringwald 
1652cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1653a3dc965aSMatthias Ringwald     btstack_linked_list_iterator_t it;
1654a3dc965aSMatthias Ringwald     l2cap_channel_t * channel;
1655a3dc965aSMatthias Ringwald     uint16_t local_cid;
165683fd9c76SMatthias Ringwald     uint16_t le_psm;
165763f0ac45SMatthias Ringwald     uint16_t new_credits;
165863f0ac45SMatthias Ringwald     uint16_t credits_before;
1659e7d0c9aaSMatthias Ringwald     l2cap_service_t * service;
166011cae19eSMilanka Ringwald     uint16_t source_cid;
166183fd9c76SMatthias Ringwald #endif
166200d93d79Smatthias.ringwald 
16631b8b8d05SMatthias Ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
166423017473SMatthias Ringwald     log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id);
16651b8b8d05SMatthias Ringwald 
16661b8b8d05SMatthias Ringwald     switch (code){
166700d93d79Smatthias.ringwald 
1668c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1669c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1670ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1671ccf076adS[email protected]             break;
1672da886c03S[email protected] 
1673c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1674e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1675da886c03S[email protected]             if (connection){
16766d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
16776d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1678c48b2a2cSMatthias Ringwald                     return 0;
16796d91fb6cSMatthias Ringwald                 }
1680da886c03S[email protected]                 int update_parameter = 1;
1681a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
16824ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1683c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1684c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1685c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1686c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1687da886c03S[email protected] 
1688da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1689da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1690da886c03S[email protected] 
1691da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1692da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1693da886c03S[email protected] 
1694da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1695da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1696da886c03S[email protected] 
1697da886c03S[email protected]                 if (update_parameter){
1698da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1699da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1700da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1701da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1702da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1703da886c03S[email protected]                 } else {
1704da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1705da886c03S[email protected]                 }
1706c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1707da886c03S[email protected]             }
1708da886c03S[email protected] 
170933c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1710c48b2a2cSMatthias Ringwald 
1711c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1712c48b2a2cSMatthias Ringwald             event[1] = 8;
1713c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1714c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
171533c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1716ccf076adS[email protected]             break;
1717c48b2a2cSMatthias Ringwald 
1718a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1719a3dc965aSMatthias Ringwald 
172063f0ac45SMatthias Ringwald         case COMMAND_REJECT:
172163f0ac45SMatthias Ringwald             // Find channel for this sig_id and connection handle
172263f0ac45SMatthias Ringwald             channel = NULL;
172363f0ac45SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
172463f0ac45SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
172563f0ac45SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
172663f0ac45SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
172763f0ac45SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
172863f0ac45SMatthias Ringwald                 channel = a_channel;
172963f0ac45SMatthias Ringwald                 break;
173063f0ac45SMatthias Ringwald             }
173163f0ac45SMatthias Ringwald             if (!channel) break;
173263f0ac45SMatthias Ringwald 
173363f0ac45SMatthias Ringwald             // if received while waiting for le connection response, assume legacy device
173463f0ac45SMatthias Ringwald             if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){
173563f0ac45SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
173663f0ac45SMatthias Ringwald                 // no official value for this, use: Connection refused – LE_PSM not supported - 0x0002
173744276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, 0x0002);
173863f0ac45SMatthias Ringwald 
173963f0ac45SMatthias Ringwald                 // discard channel
174063f0ac45SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
174163f0ac45SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
174263f0ac45SMatthias Ringwald                 break;
174363f0ac45SMatthias Ringwald             }
174463f0ac45SMatthias Ringwald             break;
174563f0ac45SMatthias Ringwald 
1746e7d0c9aaSMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_REQUEST:
1747e7d0c9aaSMatthias Ringwald 
1748e7d0c9aaSMatthias Ringwald             // get hci connection, bail if not found (must not happen)
1749e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1750e7d0c9aaSMatthias Ringwald             if (!connection) return 0;
1751e7d0c9aaSMatthias Ringwald 
1752e7d0c9aaSMatthias Ringwald             // check if service registered
1753e7d0c9aaSMatthias Ringwald             le_psm  = little_endian_read_16(command, 4);
1754e7d0c9aaSMatthias Ringwald             service = l2cap_le_get_service(le_psm);
175511cae19eSMilanka Ringwald             source_cid = little_endian_read_16(command, 6);
1756e7d0c9aaSMatthias Ringwald 
1757e7d0c9aaSMatthias Ringwald             if (service){
1758e7d0c9aaSMatthias Ringwald                 if (source_cid < 0x40){
1759e7d0c9aaSMatthias Ringwald                     // 0x0009 Connection refused - Invalid Source CID
1760e74c5f58SMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0009);
1761e7d0c9aaSMatthias Ringwald                     return 1;
1762e7d0c9aaSMatthias Ringwald                 }
1763e7d0c9aaSMatthias Ringwald 
1764e7d0c9aaSMatthias Ringwald                 // go through list of channels for this ACL connection and check if we get a match
1765e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1766e7d0c9aaSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it)){
17671b8b8d05SMatthias Ringwald                     l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
17681b8b8d05SMatthias Ringwald                     if (a_channel->con_handle != handle) continue;
17691b8b8d05SMatthias Ringwald                     if (a_channel->remote_cid != source_cid) continue;
1770e7d0c9aaSMatthias Ringwald                     // 0x000a Connection refused - Source CID already allocated
1771e74c5f58SMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x000a);
1772e7d0c9aaSMatthias Ringwald                     return 1;
1773e7d0c9aaSMatthias Ringwald                 }
1774e7d0c9aaSMatthias Ringwald 
177583fd9c76SMatthias Ringwald                 // security: check encryption
177683fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_2){
177783fd9c76SMatthias Ringwald                     if (sm_encryption_key_size(handle) == 0){
177883fd9c76SMatthias Ringwald                         // 0x0008 Connection refused - insufficient encryption
1779e74c5f58SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0008);
178083fd9c76SMatthias Ringwald                         return 1;
178183fd9c76SMatthias Ringwald                     }
178283fd9c76SMatthias Ringwald                     // anything less than 16 byte key size is insufficient
178383fd9c76SMatthias Ringwald                     if (sm_encryption_key_size(handle) < 16){
178483fd9c76SMatthias Ringwald                         // 0x0007 Connection refused – insufficient encryption key size
1785e74c5f58SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0007);
178683fd9c76SMatthias Ringwald                         return 1;
178783fd9c76SMatthias Ringwald                     }
178883fd9c76SMatthias Ringwald                 }
178983fd9c76SMatthias Ringwald 
179083fd9c76SMatthias Ringwald                 // security: check authencation
179183fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_3){
179283fd9c76SMatthias Ringwald                     if (!sm_authenticated(handle)){
179383fd9c76SMatthias Ringwald                         // 0x0005 Connection refused – insufficient authentication
1794e74c5f58SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0005);
179583fd9c76SMatthias Ringwald                         return 1;
179683fd9c76SMatthias Ringwald                     }
179783fd9c76SMatthias Ringwald                 }
179883fd9c76SMatthias Ringwald 
179983fd9c76SMatthias Ringwald                 // security: check authorization
180083fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_4){
180183fd9c76SMatthias Ringwald                     if (sm_authorization_state(handle) != AUTHORIZATION_GRANTED){
180283fd9c76SMatthias Ringwald                         // 0x0006 Connection refused – insufficient authorization
1803e74c5f58SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0006);
180483fd9c76SMatthias Ringwald                         return 1;
180583fd9c76SMatthias Ringwald                     }
180683fd9c76SMatthias Ringwald                 }
1807e7d0c9aaSMatthias Ringwald 
1808e7d0c9aaSMatthias Ringwald                 // allocate channel
18091b8b8d05SMatthias Ringwald                 channel = l2cap_create_channel_entry(service->packet_handler, connection->address,
1810e7d0c9aaSMatthias Ringwald                     BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
1811e7d0c9aaSMatthias Ringwald                 if (!channel){
1812e7d0c9aaSMatthias Ringwald                     // 0x0004 Connection refused – no resources available
1813e74c5f58SMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0004);
1814e7d0c9aaSMatthias Ringwald                     return 1;
1815e7d0c9aaSMatthias Ringwald                 }
1816e7d0c9aaSMatthias Ringwald 
1817e7d0c9aaSMatthias Ringwald                 channel->con_handle = handle;
1818e7d0c9aaSMatthias Ringwald                 channel->remote_cid = source_cid;
1819e7d0c9aaSMatthias Ringwald                 channel->remote_sig_id = sig_id;
18207f107edaSMatthias Ringwald                 channel->remote_mtu = little_endian_read_16(command, 8);
18217f107edaSMatthias Ringwald                 channel->remote_mps = little_endian_read_16(command, 10);
18227f107edaSMatthias Ringwald                 channel->credits_outgoing = little_endian_read_16(command, 12);
1823e7d0c9aaSMatthias Ringwald 
1824e7d0c9aaSMatthias Ringwald                 // set initial state
1825e7d0c9aaSMatthias Ringwald                 channel->state      = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1826c99bb618SMatthias Ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING;
1827e7d0c9aaSMatthias Ringwald 
1828e7d0c9aaSMatthias Ringwald                 // add to connections list
1829e7d0c9aaSMatthias Ringwald                 btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1830e7d0c9aaSMatthias Ringwald 
1831e7d0c9aaSMatthias Ringwald                 // post connection request event
183244276248SMatthias Ringwald                 l2cap_emit_le_incoming_connection(channel);
1833e7d0c9aaSMatthias Ringwald 
1834e7d0c9aaSMatthias Ringwald             } else {
1835e7d0c9aaSMatthias Ringwald                 // Connection refused – LE_PSM not supported
1836e74c5f58SMatthias Ringwald                 l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, 0x0002);
1837e7d0c9aaSMatthias Ringwald             }
1838e7d0c9aaSMatthias Ringwald             break;
18391b8b8d05SMatthias Ringwald 
18401b8b8d05SMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_RESPONSE:
18411b8b8d05SMatthias Ringwald             // Find channel for this sig_id and connection handle
18421b8b8d05SMatthias Ringwald             channel = NULL;
18431b8b8d05SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
18441b8b8d05SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
18451b8b8d05SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
18461b8b8d05SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
18471b8b8d05SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
18481b8b8d05SMatthias Ringwald                 channel = a_channel;
18491b8b8d05SMatthias Ringwald                 break;
18501b8b8d05SMatthias Ringwald             }
18511b8b8d05SMatthias Ringwald             if (!channel) break;
18521b8b8d05SMatthias Ringwald 
18531b8b8d05SMatthias Ringwald             // cid + 0
18541b8b8d05SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
18551b8b8d05SMatthias Ringwald             if (result){
18561b8b8d05SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
18571b8b8d05SMatthias Ringwald                 // map l2cap connection response result to BTstack status enumeration
185844276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, result);
18591b8b8d05SMatthias Ringwald 
18601b8b8d05SMatthias Ringwald                 // discard channel
186163f0ac45SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
18621b8b8d05SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
18631b8b8d05SMatthias Ringwald                 break;
18641b8b8d05SMatthias Ringwald             }
18651b8b8d05SMatthias Ringwald 
18661b8b8d05SMatthias Ringwald             // success
18671b8b8d05SMatthias Ringwald             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
18681b8b8d05SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
18691b8b8d05SMatthias Ringwald             channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
18701b8b8d05SMatthias Ringwald             channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
18711b8b8d05SMatthias Ringwald             channel->state = L2CAP_STATE_OPEN;
187244276248SMatthias Ringwald             l2cap_emit_le_channel_opened(channel, result);
18731b8b8d05SMatthias Ringwald             break;
18741b8b8d05SMatthias Ringwald 
187585aeef60SMatthias Ringwald         case LE_FLOW_CONTROL_CREDIT:
187685aeef60SMatthias Ringwald             // find channel
187785aeef60SMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
187885aeef60SMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
187963f0ac45SMatthias Ringwald             if (!channel) {
188063f0ac45SMatthias Ringwald                 log_error("l2cap: no channel for cid 0x%02x", local_cid);
188163f0ac45SMatthias Ringwald                 break;
188263f0ac45SMatthias Ringwald             }
188363f0ac45SMatthias Ringwald             new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
188463f0ac45SMatthias Ringwald             credits_before = channel->credits_outgoing;
188563f0ac45SMatthias Ringwald             channel->credits_outgoing += new_credits;
188663f0ac45SMatthias Ringwald             // check for credit overrun
188763f0ac45SMatthias Ringwald             if (credits_before > channel->credits_outgoing){
188863f0ac45SMatthias Ringwald                 log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid);
188963f0ac45SMatthias Ringwald                 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
189063f0ac45SMatthias Ringwald                 break;
189163f0ac45SMatthias Ringwald             }
189263f0ac45SMatthias Ringwald             log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing);
189385aeef60SMatthias Ringwald             break;
189485aeef60SMatthias Ringwald 
1895828a7f7aSMatthias Ringwald         case DISCONNECTION_REQUEST:
1896828a7f7aSMatthias Ringwald             // find channel
1897828a7f7aSMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
1898828a7f7aSMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
189963f34e00SMatthias Ringwald             if (!channel) {
190063f34e00SMatthias Ringwald                 log_error("l2cap: no channel for cid 0x%02x", local_cid);
190163f34e00SMatthias Ringwald                 break;
190263f34e00SMatthias Ringwald             }
1903828a7f7aSMatthias Ringwald             channel->remote_sig_id = sig_id;
1904828a7f7aSMatthias Ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1905828a7f7aSMatthias Ringwald             break;
1906828a7f7aSMatthias Ringwald 
1907a3dc965aSMatthias Ringwald #endif
1908a3dc965aSMatthias Ringwald 
190963f0ac45SMatthias Ringwald         case DISCONNECTION_RESPONSE:
191063f0ac45SMatthias Ringwald             break;
191163f0ac45SMatthias Ringwald 
1912c48b2a2cSMatthias Ringwald         default:
1913c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1914c48b2a2cSMatthias Ringwald             return 0;
1915ccf076adS[email protected]     }
1916c48b2a2cSMatthias Ringwald     return 1;
1917c48b2a2cSMatthias Ringwald }
1918e7d0c9aaSMatthias Ringwald #endif
1919c48b2a2cSMatthias Ringwald 
1920c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
19219ec2630cSMatthias Ringwald     UNUSED(packet_type);
19229ec2630cSMatthias Ringwald     UNUSED(channel);
1923c48b2a2cSMatthias Ringwald 
192409e9d05bSMatthias Ringwald     l2cap_channel_t * l2cap_channel;
1925f3963406SMatthias Ringwald     UNUSED(l2cap_channel);
192609e9d05bSMatthias Ringwald 
1927c48b2a2cSMatthias Ringwald     // Get Channel ID
1928c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1929c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1930c48b2a2cSMatthias Ringwald 
1931c48b2a2cSMatthias Ringwald     switch (channel_id) {
1932c48b2a2cSMatthias Ringwald 
193309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1934c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1935c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1936c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1937c48b2a2cSMatthias Ringwald                 // handle signaling commands
1938c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1939c48b2a2cSMatthias Ringwald 
1940c48b2a2cSMatthias Ringwald                 // increment command_offset
1941c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1942c48b2a2cSMatthias Ringwald             }
1943c48b2a2cSMatthias Ringwald             break;
1944c48b2a2cSMatthias Ringwald         }
194509e9d05bSMatthias Ringwald #endif
1946c48b2a2cSMatthias Ringwald 
1947e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1948e7d0c9aaSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE: {
1949e7d0c9aaSMatthias Ringwald             uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1950e7d0c9aaSMatthias Ringwald             int      valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1951c48b2a2cSMatthias Ringwald             if (!valid){
1952e74c5f58SMatthias Ringwald                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
1953ccf076adS[email protected]             }
1954ccf076adS[email protected]             break;
1955e7d0c9aaSMatthias Ringwald         }
1956e7d0c9aaSMatthias Ringwald #endif
1957c48b2a2cSMatthias Ringwald 
1958c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1959c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1960c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1961ccf076adS[email protected]             }
1962c48b2a2cSMatthias Ringwald             break;
1963c48b2a2cSMatthias Ringwald 
1964c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1965c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1966c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1967c48b2a2cSMatthias Ringwald             }
1968c48b2a2cSMatthias Ringwald             break;
1969c48b2a2cSMatthias Ringwald 
1970c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1971c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1972c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1973c48b2a2cSMatthias Ringwald             }
1974c48b2a2cSMatthias Ringwald             break;
19751bbc0b23S[email protected] 
197609e9d05bSMatthias Ringwald         default:
197709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
197800d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
197964e11ca9SMatthias Ringwald             l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1980d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
19813d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
198200d93d79Smatthias.ringwald             }
198309e9d05bSMatthias Ringwald #endif
1984a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
198564e11ca9SMatthias Ringwald             l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id);
198664e11ca9SMatthias Ringwald             if (l2cap_channel) {
198785aeef60SMatthias Ringwald                 // credit counting
198885aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming == 0){
198985aeef60SMatthias Ringwald                     log_error("LE Data Channel packet received but no incoming credits");
199085aeef60SMatthias Ringwald                     l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
199185aeef60SMatthias Ringwald                     break;
199285aeef60SMatthias Ringwald                 }
199385aeef60SMatthias Ringwald                 l2cap_channel->credits_incoming--;
199485aeef60SMatthias Ringwald 
199585aeef60SMatthias Ringwald                 // automatic credits
199685aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){
199785aeef60SMatthias Ringwald                     l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT;
199885aeef60SMatthias Ringwald                 }
199985aeef60SMatthias Ringwald 
2000cd529728SMatthias Ringwald                 // first fragment
2001cd529728SMatthias Ringwald                 uint16_t pos = 0;
2002cd529728SMatthias Ringwald                 if (!l2cap_channel->receive_sdu_len){
2003cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
2004cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_pos = 0;
2005cd529728SMatthias Ringwald                     pos  += 2;
2006cd529728SMatthias Ringwald                     size -= 2;
2007cd529728SMatthias Ringwald                 }
2008cd529728SMatthias Ringwald                 memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER);
2009cd529728SMatthias Ringwald                 l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER;
2010cd529728SMatthias Ringwald                 // done?
201163f0ac45SMatthias Ringwald                 log_info("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len);
2012cd529728SMatthias Ringwald                 if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){
2013cd529728SMatthias Ringwald                     l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len);
2014cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = 0;
2015cd529728SMatthias Ringwald                 }
201663f0ac45SMatthias Ringwald             } else {
201763f0ac45SMatthias Ringwald                 log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id);
201864e11ca9SMatthias Ringwald             }
201964e11ca9SMatthias Ringwald #endif
20205652b5ffS[email protected]             break;
20215652b5ffS[email protected]     }
202200d93d79Smatthias.ringwald 
20231eb2563eS[email protected]     l2cap_run();
20242718e2e7Smatthias.ringwald }
202500d93d79Smatthias.ringwald 
202609e9d05bSMatthias Ringwald // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
202709e9d05bSMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
202809e9d05bSMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
202909e9d05bSMatthias Ringwald     if (index < 0) return;
203009e9d05bSMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
203109e9d05bSMatthias Ringwald }
203209e9d05bSMatthias Ringwald 
203309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
203415ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
203527a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
2036f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
2037f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
2038f62db1e3Smatthias.ringwald     // discard channel
20399dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
2040665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
2041d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
2042c8e4258aSmatthias.ringwald }
20431e6aba47Smatthias.ringwald 
20448f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
2045665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2046665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
2047665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2048665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
20499d9bbc01Smatthias.ringwald         if ( service->psm == psm){
20509d9bbc01Smatthias.ringwald             return service;
20519d9bbc01Smatthias.ringwald         };
20529d9bbc01Smatthias.ringwald     }
20539d9bbc01Smatthias.ringwald     return NULL;
20549d9bbc01Smatthias.ringwald }
20559d9bbc01Smatthias.ringwald 
20567192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
20577192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
20587192e786SMatthias Ringwald }
20597192e786SMatthias Ringwald 
2060e0abb8e7S[email protected] 
2061be2053a6SMatthias 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){
2062be2053a6SMatthias Ringwald 
2063be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
2064e0abb8e7S[email protected] 
20654bb582b6Smatthias.ringwald     // check for alread registered psm
20669d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
2067277abc2cSmatthias.ringwald     if (service) {
2068be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
2069be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
2070277abc2cSmatthias.ringwald     }
20719d9bbc01Smatthias.ringwald 
20724bb582b6Smatthias.ringwald     // alloc structure
2073bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
2074277abc2cSmatthias.ringwald     if (!service) {
2075be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
2076be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
2077277abc2cSmatthias.ringwald     }
20789d9bbc01Smatthias.ringwald 
20799d9bbc01Smatthias.ringwald     // fill in
20809d9bbc01Smatthias.ringwald     service->psm = psm;
20819d9bbc01Smatthias.ringwald     service->mtu = mtu;
208205ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
2083df3354fcS[email protected]     service->required_security_level = security_level;
20849d9bbc01Smatthias.ringwald 
20859d9bbc01Smatthias.ringwald     // add to services list
2086665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
2087c0e866bfSmatthias.ringwald 
2088c0e866bfSmatthias.ringwald     // enable page scan
208915a95bd5SMatthias Ringwald     gap_connectable_control(1);
20905842b6d9Smatthias.ringwald 
2091be2053a6SMatthias Ringwald     return 0;
20929d9bbc01Smatthias.ringwald }
20939d9bbc01Smatthias.ringwald 
20947e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
2095e0abb8e7S[email protected] 
2096e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
2097e0abb8e7S[email protected] 
20989d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
20997e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
2100665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
2101d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
2102c0e866bfSmatthias.ringwald 
2103c0e866bfSmatthias.ringwald     // disable page scan when no services registered
21047e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
210515a95bd5SMatthias Ringwald         gap_connectable_control(0);
21069d9bbc01Smatthias.ringwald     }
21077e8856ebSMatthias Ringwald     return 0;
21087e8856ebSMatthias Ringwald }
210909e9d05bSMatthias Ringwald #endif
21109d9bbc01Smatthias.ringwald 
21117192e786SMatthias Ringwald 
2112cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
211383fd9c76SMatthias Ringwald 
211457be49d6SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){
211557be49d6SMatthias Ringwald     if (!channel->waiting_for_can_send_now) return;
211657be49d6SMatthias Ringwald     if (channel->send_sdu_buffer) return;
211757be49d6SMatthias Ringwald     channel->waiting_for_can_send_now = 0;
211857be49d6SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid);
211957be49d6SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW);
212057be49d6SMatthias Ringwald }
212157be49d6SMatthias Ringwald 
212257be49d6SMatthias Ringwald // 1BH2222
212357be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) {
212457be49d6SMatthias 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",
212557be49d6SMatthias Ringwald              channel->address_type, bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);
212657be49d6SMatthias Ringwald     uint8_t event[19];
212757be49d6SMatthias Ringwald     event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION;
212857be49d6SMatthias Ringwald     event[1] = sizeof(event) - 2;
212957be49d6SMatthias Ringwald     event[2] = channel->address_type;
213057be49d6SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
213157be49d6SMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
213257be49d6SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
213357be49d6SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
213457be49d6SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
213557be49d6SMatthias Ringwald     little_endian_store_16(event, 17, channel->remote_mtu);
213657be49d6SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
213757be49d6SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
213857be49d6SMatthias Ringwald }
213957be49d6SMatthias Ringwald // 11BH22222
214057be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) {
214157be49d6SMatthias 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",
214257be49d6SMatthias Ringwald              status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
214357be49d6SMatthias Ringwald              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
2144c99bb618SMatthias Ringwald     uint8_t event[23];
214557be49d6SMatthias Ringwald     event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED;
214657be49d6SMatthias Ringwald     event[1] = sizeof(event) - 2;
214757be49d6SMatthias Ringwald     event[2] = status;
214857be49d6SMatthias Ringwald     event[3] = channel->address_type;
214957be49d6SMatthias Ringwald     reverse_bd_addr(channel->address, &event[4]);
215057be49d6SMatthias Ringwald     little_endian_store_16(event, 10, channel->con_handle);
2151c99bb618SMatthias Ringwald     event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0;
2152c99bb618SMatthias Ringwald     little_endian_store_16(event, 13, channel->psm);
2153c99bb618SMatthias Ringwald     little_endian_store_16(event, 15, channel->local_cid);
2154c99bb618SMatthias Ringwald     little_endian_store_16(event, 17, channel->remote_cid);
2155c99bb618SMatthias Ringwald     little_endian_store_16(event, 19, channel->local_mtu);
2156c99bb618SMatthias Ringwald     little_endian_store_16(event, 21, channel->remote_mtu);
215757be49d6SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
215857be49d6SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
215957be49d6SMatthias Ringwald }
216057be49d6SMatthias Ringwald 
216157be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){
216257be49d6SMatthias Ringwald     btstack_linked_list_iterator_t it;
216357be49d6SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
216457be49d6SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
216557be49d6SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
216657be49d6SMatthias Ringwald         if ( channel->local_cid == local_cid) {
216757be49d6SMatthias Ringwald             return channel;
216857be49d6SMatthias Ringwald         }
216957be49d6SMatthias Ringwald     }
217057be49d6SMatthias Ringwald     return NULL;
217157be49d6SMatthias Ringwald }
217257be49d6SMatthias Ringwald 
217357be49d6SMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
217457be49d6SMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){
217557be49d6SMatthias Ringwald     channel->state = L2CAP_STATE_CLOSED;
217657be49d6SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
217757be49d6SMatthias Ringwald     // discard channel
217857be49d6SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
217957be49d6SMatthias Ringwald     btstack_memory_l2cap_channel_free(channel);
218057be49d6SMatthias Ringwald }
218157be49d6SMatthias Ringwald 
2182e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
2183e7d0c9aaSMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, le_psm);
21847192e786SMatthias Ringwald }
2185efedfb4cSMatthias Ringwald 
2186da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
21877192e786SMatthias Ringwald 
2188da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
21897192e786SMatthias Ringwald 
21907192e786SMatthias Ringwald     // check for alread registered psm
21917192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
21927192e786SMatthias Ringwald     if (service) {
2193da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
21947192e786SMatthias Ringwald     }
21957192e786SMatthias Ringwald 
21967192e786SMatthias Ringwald     // alloc structure
21977192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
21987192e786SMatthias Ringwald     if (!service) {
21997192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
2200da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
22017192e786SMatthias Ringwald     }
22027192e786SMatthias Ringwald 
22037192e786SMatthias Ringwald     // fill in
22047192e786SMatthias Ringwald     service->psm = psm;
2205da144af5SMatthias Ringwald     service->mtu = 0;
22067192e786SMatthias Ringwald     service->packet_handler = packet_handler;
22077192e786SMatthias Ringwald     service->required_security_level = security_level;
22087192e786SMatthias Ringwald 
22097192e786SMatthias Ringwald     // add to services list
2210665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
22117192e786SMatthias Ringwald 
22127192e786SMatthias Ringwald     // done
2213da144af5SMatthias Ringwald     return 0;
22147192e786SMatthias Ringwald }
22157192e786SMatthias Ringwald 
2216da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
22177192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
22187192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
22199367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
2220da144af5SMatthias Ringwald 
2221665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
22227192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
2223da144af5SMatthias Ringwald     return 0;
22247192e786SMatthias Ringwald }
2225da144af5SMatthias Ringwald 
2226da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
2227e7d0c9aaSMatthias Ringwald     // get channel
2228e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2229e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2230e7d0c9aaSMatthias Ringwald 
2231e7d0c9aaSMatthias Ringwald     // validate state
2232e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
2233e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2234e7d0c9aaSMatthias Ringwald     }
2235e7d0c9aaSMatthias Ringwald 
2236efedfb4cSMatthias Ringwald     // set state accept connection
223723017473SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
223823017473SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
223923017473SMatthias Ringwald     channel->local_mtu = mtu;
224085aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
224185aeef60SMatthias Ringwald     channel->automatic_credits  = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
224285aeef60SMatthias Ringwald 
224385aeef60SMatthias Ringwald     // test
224463f0ac45SMatthias Ringwald     // channel->new_credits_incoming = 1;
2245e7d0c9aaSMatthias Ringwald 
2246e7d0c9aaSMatthias Ringwald     // go
2247e7d0c9aaSMatthias Ringwald     l2cap_run();
2248da144af5SMatthias Ringwald     return 0;
2249da144af5SMatthias Ringwald }
2250da144af5SMatthias Ringwald 
2251da144af5SMatthias Ringwald /**
2252da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
2253da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2254da144af5SMatthias Ringwald  */
2255da144af5SMatthias Ringwald 
2256da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
2257e7d0c9aaSMatthias Ringwald     // get channel
2258e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2259e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2260e7d0c9aaSMatthias Ringwald 
2261e7d0c9aaSMatthias Ringwald     // validate state
2262e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
2263e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2264e7d0c9aaSMatthias Ringwald     }
2265e7d0c9aaSMatthias Ringwald 
2266efedfb4cSMatthias Ringwald     // set state decline connection
2267e7d0c9aaSMatthias Ringwald     channel->state  = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
2268e7d0c9aaSMatthias Ringwald     channel->reason = 0x04; // no resources available
2269e7d0c9aaSMatthias Ringwald     l2cap_run();
2270da144af5SMatthias Ringwald     return 0;
2271da144af5SMatthias Ringwald }
2272da144af5SMatthias Ringwald 
22737dafa750SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
2274da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
2275efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
2276efedfb4cSMatthias Ringwald 
22777dafa750SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu);
2278da144af5SMatthias Ringwald 
22797dafa750SMatthias Ringwald 
22807dafa750SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
22817dafa750SMatthias Ringwald     if (!connection) {
22827dafa750SMatthias Ringwald         log_error("no hci_connection for handle 0x%04x", con_handle);
22837dafa750SMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
22847dafa750SMatthias Ringwald     }
22857dafa750SMatthias Ringwald 
22867dafa750SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, connection->address, connection->address_type, psm, mtu, security_level);
2287da144af5SMatthias Ringwald     if (!channel) {
2288da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
2289da144af5SMatthias Ringwald     }
2290e7d0c9aaSMatthias Ringwald     log_info("l2cap_le_create_channel %p", channel);
2291da144af5SMatthias Ringwald 
2292da144af5SMatthias Ringwald     // store local_cid
2293da144af5SMatthias Ringwald     if (out_local_cid){
2294da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
2295da144af5SMatthias Ringwald     }
2296da144af5SMatthias Ringwald 
22977dafa750SMatthias Ringwald     // provide buffer
22987dafa750SMatthias Ringwald     channel->con_handle = con_handle;
2299cd529728SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
23007dafa750SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
230185aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
230285aeef60SMatthias Ringwald     channel->automatic_credits    = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
230385aeef60SMatthias Ringwald 
2304efedfb4cSMatthias Ringwald     // add to connections list
2305efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
2306efedfb4cSMatthias Ringwald 
23077dafa750SMatthias Ringwald     // go
230863f0ac45SMatthias Ringwald     l2cap_run();
2309da144af5SMatthias Ringwald     return 0;
2310da144af5SMatthias Ringwald }
2311da144af5SMatthias Ringwald 
2312da144af5SMatthias Ringwald /**
2313da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
2314da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2315da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
2316da144af5SMatthias Ringwald  */
231764e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
231863f0ac45SMatthias Ringwald 
231963f0ac45SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
232063f0ac45SMatthias Ringwald     if (!channel) {
232163f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
232263f0ac45SMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
232363f0ac45SMatthias Ringwald     }
232463f0ac45SMatthias Ringwald 
2325efedfb4cSMatthias Ringwald     // check state
232663f0ac45SMatthias Ringwald     if (channel->state != L2CAP_STATE_OPEN){
232763f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid);
232863f0ac45SMatthias Ringwald     }
232963f0ac45SMatthias Ringwald 
233063f0ac45SMatthias Ringwald     // assert incoming credits + credits <= 0xffff
233163f0ac45SMatthias Ringwald     uint32_t total_credits = channel->credits_incoming;
233263f0ac45SMatthias Ringwald     total_credits += channel->new_credits_incoming;
233363f0ac45SMatthias Ringwald     total_credits += credits;
233463f0ac45SMatthias Ringwald     if (total_credits > 0xffff){
233563f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming,
233663f0ac45SMatthias Ringwald             channel->new_credits_incoming, credits);
233763f0ac45SMatthias Ringwald     }
233863f0ac45SMatthias Ringwald 
2339efedfb4cSMatthias Ringwald     // set credits_granted
234063f0ac45SMatthias Ringwald     channel->new_credits_incoming += credits;
234163f0ac45SMatthias Ringwald 
234263f0ac45SMatthias Ringwald     // go
234363f0ac45SMatthias Ringwald     l2cap_run();
2344da144af5SMatthias Ringwald     return 0;
2345da144af5SMatthias Ringwald }
2346da144af5SMatthias Ringwald 
2347da144af5SMatthias Ringwald /**
2348da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
2349da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2350da144af5SMatthias Ringwald  */
235164e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){
235244276248SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
235344276248SMatthias Ringwald     if (!channel) {
235444276248SMatthias Ringwald         log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
2355da144af5SMatthias Ringwald         return 0;
2356da144af5SMatthias Ringwald     }
2357da144af5SMatthias Ringwald 
235844276248SMatthias Ringwald     // check state
235944276248SMatthias Ringwald     if (channel->state != L2CAP_STATE_OPEN) return 0;
236044276248SMatthias Ringwald 
236144276248SMatthias Ringwald     // check queue
236244276248SMatthias Ringwald     if (channel->send_sdu_buffer) return 0;
236344276248SMatthias Ringwald 
236444276248SMatthias Ringwald     // fine, go ahead
236544276248SMatthias Ringwald     return 1;
236644276248SMatthias Ringwald }
236744276248SMatthias Ringwald 
2368da144af5SMatthias Ringwald /**
2369da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
2370da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
2371da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
2372da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2373da144af5SMatthias Ringwald  */
237464e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
237544276248SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
237644276248SMatthias Ringwald     if (!channel) {
237744276248SMatthias Ringwald         log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid);
237844276248SMatthias Ringwald         return 0;
237944276248SMatthias Ringwald     }
238044276248SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
238144276248SMatthias Ringwald     l2cap_le_notify_channel_can_send(channel);
2382da144af5SMatthias Ringwald     return 0;
2383da144af5SMatthias Ringwald }
2384da144af5SMatthias Ringwald 
2385da144af5SMatthias Ringwald /**
2386da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
2387da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
2388da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2389da144af5SMatthias Ringwald  * @param data                  data to send
2390da144af5SMatthias Ringwald  * @param size                  data size
2391da144af5SMatthias Ringwald  */
239264e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
239364e11ca9SMatthias Ringwald 
239464e11ca9SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
239564e11ca9SMatthias Ringwald     if (!channel) {
239664e11ca9SMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2397828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
239864e11ca9SMatthias Ringwald     }
239964e11ca9SMatthias Ringwald 
240064e11ca9SMatthias Ringwald     if (len > channel->remote_mtu){
240164e11ca9SMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
240264e11ca9SMatthias Ringwald         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
240364e11ca9SMatthias Ringwald     }
240464e11ca9SMatthias Ringwald 
24057f107edaSMatthias Ringwald     if (channel->send_sdu_buffer){
240664e11ca9SMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
240764e11ca9SMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
240864e11ca9SMatthias Ringwald     }
240964e11ca9SMatthias Ringwald 
24107f107edaSMatthias Ringwald     channel->send_sdu_buffer = data;
24117f107edaSMatthias Ringwald     channel->send_sdu_len    = len;
24127f107edaSMatthias Ringwald     channel->send_sdu_pos    = 0;
241364e11ca9SMatthias Ringwald 
24147f107edaSMatthias Ringwald     l2cap_run();
24157f107edaSMatthias Ringwald     return 0;
2416da144af5SMatthias Ringwald }
2417da144af5SMatthias Ringwald 
2418da144af5SMatthias Ringwald /**
2419da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
2420da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2421da144af5SMatthias Ringwald  */
2422828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid)
2423da144af5SMatthias Ringwald {
2424828a7f7aSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2425828a7f7aSMatthias Ringwald     if (!channel) {
2426828a7f7aSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2427828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2428828a7f7aSMatthias Ringwald     }
2429828a7f7aSMatthias Ringwald 
2430828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
2431828a7f7aSMatthias Ringwald     l2cap_run();
2432da144af5SMatthias Ringwald     return 0;
2433da144af5SMatthias Ringwald }
2434da144af5SMatthias Ringwald 
24357f02f414SMatthias Ringwald #endif
2436