xref: /btstack/src/l2cap.c (revision a24785d0c1c38874bcd1f2f124d52cdbf7107626)
143625864Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
3943625864Smatthias.ringwald  *  l2cap.c
4043625864Smatthias.ringwald  *
4143625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
4243625864Smatthias.ringwald  *
4343625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
4443625864Smatthias.ringwald  */
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include "l2cap.h"
47645658c9Smatthias.ringwald #include "hci.h"
482b3c6c9bSmatthias.ringwald #include "hci_dump.h"
4916ece135SMatthias Ringwald #include "btstack_debug.h"
500e2df43fSMatthias Ringwald #include "btstack_event.h"
51d3a9df87Smatthias.ringwald #include "btstack_memory.h"
5243625864Smatthias.ringwald 
53cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
5483fd9c76SMatthias Ringwald #include "ble/sm.h"
5583fd9c76SMatthias Ringwald #endif
5683fd9c76SMatthias Ringwald 
5743625864Smatthias.ringwald #include <stdarg.h>
5843625864Smatthias.ringwald #include <string.h>
5943625864Smatthias.ringwald 
6043625864Smatthias.ringwald #include <stdio.h>
6143625864Smatthias.ringwald 
624c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
634c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
644c744e21Smatthias.ringwald 
6539bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
66e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6739bda6d5Smatthias.ringwald 
6885aeef60SMatthias Ringwald // nr of credits provided to remote if credits fall below watermark
6985aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK 5
7085aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT 5
7185aeef60SMatthias Ringwald 
7200d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
7300d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
7400d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
7500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
7600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
7700d93d79Smatthias.ringwald 
785628cf69SMatthias Ringwald // internal table
795628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
805628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
815628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
825628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
835628cf69SMatthias Ringwald 
8409e9d05bSMatthias Ringwald #if defined(ENABLE_LE_DATA_CHANNELS) || defined(ENABLE_CLASSIC)
8509e9d05bSMatthias Ringwald #define L2CAP_USES_CHANNELS
8609e9d05bSMatthias Ringwald #endif
8709e9d05bSMatthias Ringwald 
8833c40538SMatthias Ringwald // prototypes
8933c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
903d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
9130725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void);
9209e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
9309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
9409e9d05bSMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
9509e9d05bSMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
9609e9d05bSMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
9709e9d05bSMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
9809e9d05bSMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel);
9909e9d05bSMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
10009e9d05bSMatthias Ringwald #endif
101cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
10257be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status);
10357be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel);
10457be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid);
10544276248SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel);
106828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel);
107e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm);
108e7d0c9aaSMatthias Ringwald #endif
10933c40538SMatthias Ringwald 
1105628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
1115628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
1122125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
1135628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
1145628cf69SMatthias Ringwald 
11509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1165628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
1175628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
11809e9d05bSMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
11909e9d05bSMatthias Ringwald #endif
12057be49d6SMatthias Ringwald 
12157be49d6SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1225628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
1235628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
12457be49d6SMatthias Ringwald #endif
12539bda6d5Smatthias.ringwald 
12639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1272b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1282b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1292b83fb7dSmatthias.ringwald 
130fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
131fb37a842SMatthias Ringwald 
13233c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1335628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
13439bda6d5Smatthias.ringwald 
13534e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
13634e7e577SMatthias Ringwald     switch (index){
13734e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
13834e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
13934e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
14034e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
14134e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
14234e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
14334e7e577SMatthias Ringwald         default:
14434e7e577SMatthias Ringwald             return 0;
14534e7e577SMatthias Ringwald     }
14634e7e577SMatthias Ringwald }
1475628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1485628cf69SMatthias Ringwald     switch (channel_id){
1495628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1505628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1515628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1525628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1535628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1545628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1555628cf69SMatthias Ringwald         default:
1565628cf69SMatthias Ringwald             return -1;
1575628cf69SMatthias Ringwald         }
1585628cf69SMatthias Ringwald }
15939bda6d5Smatthias.ringwald 
16034e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
16134e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
16234e7e577SMatthias Ringwald     return 1;
16334e7e577SMatthias Ringwald }
16434e7e577SMatthias Ringwald 
16571de195eSMatthias Ringwald void l2cap_init(void){
1662b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
167808a48abSmatthias.ringwald 
16809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
169f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
170f5454fc6Smatthias.ringwald     l2cap_services = NULL;
17109e9d05bSMatthias Ringwald     require_security_level2_for_outgoing_sdp = 0;
17209e9d05bSMatthias Ringwald #endif
173a3dc965aSMatthias Ringwald 
174a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1757192e786SMatthias Ringwald     l2cap_le_services = NULL;
1767192e786SMatthias Ringwald     l2cap_le_channels = NULL;
177a3dc965aSMatthias Ringwald #endif
178f5454fc6Smatthias.ringwald 
17933c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1805628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
181f5454fc6Smatthias.ringwald 
182fcadd0caSmatthias.ringwald     //
1832718e2e7Smatthias.ringwald     // register callback with HCI
184fcadd0caSmatthias.ringwald     //
185d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
186fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
187fb37a842SMatthias Ringwald 
188d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
189fb37a842SMatthias Ringwald 
190be005ed6SMatthias Ringwald #ifdef ENABLE_CLASSIC
19115a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
192be005ed6SMatthias Ringwald #endif
193fcadd0caSmatthias.ringwald }
194fcadd0caSmatthias.ringwald 
195ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
19633c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1971e6aba47Smatthias.ringwald }
1981e6aba47Smatthias.ringwald 
19909e9d05bSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
20009e9d05bSMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
20109e9d05bSMatthias Ringwald     if (index < 0) return;
20209e9d05bSMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
20309e9d05bSMatthias Ringwald     l2cap_notify_channel_can_send();
20409e9d05bSMatthias Ringwald }
20509e9d05bSMatthias Ringwald 
20609e9d05bSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
20709e9d05bSMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
20809e9d05bSMatthias Ringwald }
20909e9d05bSMatthias Ringwald 
21009e9d05bSMatthias Ringwald uint8_t *l2cap_get_outgoing_buffer(void){
21109e9d05bSMatthias Ringwald     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
21209e9d05bSMatthias Ringwald }
21309e9d05bSMatthias Ringwald 
21409e9d05bSMatthias Ringwald int l2cap_reserve_packet_buffer(void){
21509e9d05bSMatthias Ringwald     return hci_reserve_packet_buffer();
21609e9d05bSMatthias Ringwald }
21709e9d05bSMatthias Ringwald 
21809e9d05bSMatthias Ringwald void l2cap_release_packet_buffer(void){
21909e9d05bSMatthias Ringwald     hci_release_packet_buffer();
22009e9d05bSMatthias Ringwald }
22109e9d05bSMatthias Ringwald 
22209e9d05bSMatthias Ringwald static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint16_t remote_cid, uint16_t len){
22309e9d05bSMatthias Ringwald     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
22409e9d05bSMatthias Ringwald 
22509e9d05bSMatthias Ringwald     // 0 - Connection handle : PB=pb : BC=00
22609e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
22709e9d05bSMatthias Ringwald     // 2 - ACL length
22809e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
22909e9d05bSMatthias Ringwald     // 4 - L2CAP packet length
23009e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
23109e9d05bSMatthias Ringwald     // 6 - L2CAP channel DEST
23209e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 6,  remote_cid);
23309e9d05bSMatthias Ringwald }
23409e9d05bSMatthias Ringwald 
23509e9d05bSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
23609e9d05bSMatthias Ringwald 
23709e9d05bSMatthias Ringwald     if (!hci_is_packet_buffer_reserved()){
23809e9d05bSMatthias Ringwald         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
23909e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
24009e9d05bSMatthias Ringwald     }
24109e9d05bSMatthias Ringwald 
24209e9d05bSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
24309e9d05bSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
24409e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
24509e9d05bSMatthias Ringwald     }
24609e9d05bSMatthias Ringwald 
24709e9d05bSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
24809e9d05bSMatthias Ringwald 
24909e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
25009e9d05bSMatthias Ringwald     l2cap_setup_header(acl_buffer, con_handle, cid, len);
25109e9d05bSMatthias Ringwald     // send
25209e9d05bSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
25309e9d05bSMatthias Ringwald }
25409e9d05bSMatthias Ringwald 
25509e9d05bSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
25609e9d05bSMatthias Ringwald 
25709e9d05bSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
25809e9d05bSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
25909e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
26009e9d05bSMatthias Ringwald     }
26109e9d05bSMatthias Ringwald 
26209e9d05bSMatthias Ringwald     hci_reserve_packet_buffer();
26309e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
26409e9d05bSMatthias Ringwald 
26509e9d05bSMatthias Ringwald     memcpy(&acl_buffer[8], data, len);
26609e9d05bSMatthias Ringwald 
26709e9d05bSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
26809e9d05bSMatthias Ringwald }
26909e9d05bSMatthias Ringwald 
27009e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
27109e9d05bSMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
27209e9d05bSMatthias Ringwald     uint8_t event[4];
27309e9d05bSMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
27409e9d05bSMatthias Ringwald     event[1] = sizeof(event) - 2;
27509e9d05bSMatthias Ringwald     little_endian_store_16(event, 2, channel);
27609e9d05bSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
27709e9d05bSMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
27809e9d05bSMatthias Ringwald }
27909e9d05bSMatthias Ringwald 
28009e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS
28117a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
28258de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
28358de5610Smatthias.ringwald }
28458de5610Smatthias.ringwald 
28544276248SMatthias Ringwald static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){
28644276248SMatthias Ringwald     uint8_t event[4];
28744276248SMatthias Ringwald     event[0] = event_code;
28844276248SMatthias Ringwald     event[1] = sizeof(event) - 2;
28944276248SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
29044276248SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
29144276248SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
29244276248SMatthias Ringwald }
29309e9d05bSMatthias Ringwald #endif
29444276248SMatthias Ringwald 
29509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
29658de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
297c9dc710bS[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",
298fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
299c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
300bab5f4f0SMatthias Ringwald     uint8_t event[24];
30158de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
30258de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
30358de5610Smatthias.ringwald     event[2] = status;
304724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
305fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
306f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
307f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
308f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
309f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
310f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
311f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
312bab5f4f0SMatthias Ringwald     event[23] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0;
31358de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
31417a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
31558de5610Smatthias.ringwald }
31658de5610Smatthias.ringwald 
31744276248SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
318e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
31944276248SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
32058de5610Smatthias.ringwald }
32158de5610Smatthias.ringwald 
32244276248SMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) {
323e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
324fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
32558de5610Smatthias.ringwald     uint8_t event[16];
32658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
32758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
328724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
329fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
330f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
331f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
332f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
33358de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
33417a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
3350af41d30Smatthias.ringwald }
336808a48abSmatthias.ringwald 
3377f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
338665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
339665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
340665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
341665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
342b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
343f62db1e3Smatthias.ringwald             return channel;
344f62db1e3Smatthias.ringwald         }
345f62db1e3Smatthias.ringwald     }
346f62db1e3Smatthias.ringwald     return NULL;
347f62db1e3Smatthias.ringwald }
348f62db1e3Smatthias.ringwald 
3490b9d7e78SMatthias Ringwald ///
3500b9d7e78SMatthias Ringwald 
35130725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
35230725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
35330725612SMatthias Ringwald     if (!channel) return;
35430725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
35530725612SMatthias Ringwald     l2cap_notify_channel_can_send();
35630725612SMatthias Ringwald }
35730725612SMatthias Ringwald 
3586b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
3596b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
3606b1fde37Smatthias.ringwald     if (!channel) return 0;
3610b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
3627856fb31S[email protected] }
3637856fb31S[email protected] 
36483e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
36583e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
36683e7cdd9SMatthias Ringwald     if (!channel) return 0;
3670b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
36883e7cdd9SMatthias Ringwald }
36996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
37096cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
37196cbd662Smatthias.ringwald     if (channel) {
37296cbd662Smatthias.ringwald         return channel->remote_mtu;
37396cbd662Smatthias.ringwald     }
37496cbd662Smatthias.ringwald     return 0;
37596cbd662Smatthias.ringwald }
37696cbd662Smatthias.ringwald 
377ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
378665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
379665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
380665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
381665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3825932bd7cS[email protected]         if ( &channel->rtx == ts) {
3835932bd7cS[email protected]             return channel;
3845932bd7cS[email protected]         }
3855932bd7cS[email protected]     }
3865932bd7cS[email protected]     return NULL;
3875932bd7cS[email protected] }
3885932bd7cS[email protected] 
389ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3905932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
39195d2c8f4SMatthias Ringwald     if (!channel) return;
3925932bd7cS[email protected] 
3935932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3945932bd7cS[email protected] 
3955932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3965932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3975932bd7cS[email protected]     // notify client
3985932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3995932bd7cS[email protected] 
4005932bd7cS[email protected]     // discard channel
4019dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
402665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
4035932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
4045932bd7cS[email protected] }
4055932bd7cS[email protected] 
4065932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
4075932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
408528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
4095932bd7cS[email protected] }
4105932bd7cS[email protected] 
4115932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
4125932bd7cS[email protected]     l2cap_stop_rtx(channel);
413cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
414528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
415528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
416528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
4175932bd7cS[email protected] }
4185932bd7cS[email protected] 
4195932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
4205932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
4215932bd7cS[email protected]     l2cap_stop_rtx(channel);
422528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
423528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
424528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
4255932bd7cS[email protected] }
4265932bd7cS[email protected] 
42771de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
428ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
429ac301f95S[email protected] }
430ac301f95S[email protected] 
431df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
432ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
433df3354fcS[email protected] }
4345932bd7cS[email protected] 
4357f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
436a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4379da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
438b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
439b1d43497Smatthias.ringwald     }
440b1d43497Smatthias.ringwald 
4419da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
4422a373862S[email protected]     hci_reserve_packet_buffer();
443facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
44458de5610Smatthias.ringwald     va_list argptr;
44558de5610Smatthias.ringwald     va_start(argptr, identifier);
44670efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
44758de5610Smatthias.ringwald     va_end(argptr);
4489da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
449826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
45058de5610Smatthias.ringwald }
45158de5610Smatthias.ringwald 
452b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
453b1d43497Smatthias.ringwald 
454c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
455c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
456c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
457c8b9416aS[email protected]     }
458c8b9416aS[email protected] 
45958de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
460b1d43497Smatthias.ringwald     if (!channel) {
4619da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
462b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4636218e6f1Smatthias.ringwald     }
4646218e6f1Smatthias.ringwald 
465fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4669da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
467a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
468a35252c8S[email protected]     }
469a35252c8S[email protected] 
470fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
471b1d43497Smatthias.ringwald 
472facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4737f107edaSMatthias Ringwald     l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, len);
47458de5610Smatthias.ringwald     // send
4757f107edaSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
47658de5610Smatthias.ringwald }
47758de5610Smatthias.ringwald 
478ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
479b1d43497Smatthias.ringwald 
480a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
481a35252c8S[email protected]     if (!channel) {
482ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
483a35252c8S[email protected]         return -1;   // TODO: define error
484a35252c8S[email protected]     }
485a35252c8S[email protected] 
486f0efaa57S[email protected]     if (len > channel->remote_mtu){
487ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
488f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
489f0efaa57S[email protected]     }
490f0efaa57S[email protected] 
491fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
492ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
493b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
494b1d43497Smatthias.ringwald     }
495b1d43497Smatthias.ringwald 
4962a373862S[email protected]     hci_reserve_packet_buffer();
497facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
498b1d43497Smatthias.ringwald 
499b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
500b1d43497Smatthias.ringwald 
501b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
502b1d43497Smatthias.ringwald }
503b1d43497Smatthias.ringwald 
504fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
505fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5060e37e417S[email protected] }
5070e37e417S[email protected] 
50828ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
50928ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
51028ca2b46S[email protected] }
51128ca2b46S[email protected] 
51228ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
51328ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
51428ca2b46S[email protected] }
51509e9d05bSMatthias Ringwald #endif
51628ca2b46S[email protected] 
51728ca2b46S[email protected] 
51809e9d05bSMatthias Ringwald #ifdef ENABLE_BLE
51909e9d05bSMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
52009e9d05bSMatthias Ringwald 
52109e9d05bSMatthias Ringwald     if (!hci_can_send_acl_packet_now(handle)){
52209e9d05bSMatthias Ringwald         log_info("l2cap_send_le_signaling_packet, cannot send");
52309e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
52409e9d05bSMatthias Ringwald     }
52509e9d05bSMatthias Ringwald 
52609e9d05bSMatthias Ringwald     // log_info("l2cap_send_le_signaling_packet type %u", cmd);
52709e9d05bSMatthias Ringwald     hci_reserve_packet_buffer();
52809e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
52909e9d05bSMatthias Ringwald     va_list argptr;
53009e9d05bSMatthias Ringwald     va_start(argptr, identifier);
53109e9d05bSMatthias Ringwald     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
53209e9d05bSMatthias Ringwald     va_end(argptr);
53309e9d05bSMatthias Ringwald     // log_info("l2cap_send_le_signaling_packet con %u!", handle);
53409e9d05bSMatthias Ringwald     return hci_send_acl_packet_buffer(len);
53509e9d05bSMatthias Ringwald }
53609e9d05bSMatthias Ringwald #endif
53709e9d05bSMatthias Ringwald 
53809e9d05bSMatthias Ringwald uint16_t l2cap_max_mtu(void){
53909e9d05bSMatthias Ringwald     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
54009e9d05bSMatthias Ringwald }
54109e9d05bSMatthias Ringwald 
54209e9d05bSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
54309e9d05bSMatthias Ringwald     return l2cap_max_mtu();
54409e9d05bSMatthias Ringwald }
545b1d43497Smatthias.ringwald 
5468158c421Smatthias.ringwald // MARK: L2CAP_RUN
5472cd0be45Smatthias.ringwald // process outstanding signaling tasks
5487f02f414SMatthias Ringwald static void l2cap_run(void){
5492b83fb7dSmatthias.ringwald 
55022c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
55122c29ab4SMatthias Ringwald 
5522b83fb7dSmatthias.ringwald     // check pending signaling responses
5532b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5542b83fb7dSmatthias.ringwald 
5552b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
556a35252c8S[email protected] 
557a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
558a35252c8S[email protected] 
5592b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5602b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
56163a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
562f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5632b83fb7dSmatthias.ringwald 
56409e9d05bSMatthias Ringwald         // avoid warnings
56509e9d05bSMatthias Ringwald         (void) infoType;
56609e9d05bSMatthias Ringwald 
567f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
568f53da564S[email protected]         signaling_responses_pending--;
569f53da564S[email protected]         int i;
570f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
571f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
572f53da564S[email protected]         }
573f53da564S[email protected] 
574f53da564S[email protected]         switch (response_code){
57509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
5762b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5772b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5782bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5794d816277S[email protected]                 if (result == 0x0003){
5802bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5814d816277S[email protected]                 }
5822b360848Smatthias.ringwald                 break;
5832b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5842b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5852b83fb7dSmatthias.ringwald                 break;
5862b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5873b0484b3S[email protected]                 switch (infoType){
5883b0484b3S[email protected]                     case 1: { // Connectionless MTU
5893b0484b3S[email protected]                             uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5903b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5913b0484b3S[email protected]                         }
592202c8a4cSMatthias Ringwald                         break;
5933b0484b3S[email protected]                     case 2: { // Extended Features Supported
594462e630dS[email protected]                             // extended features request supported, features: fixed channels, unicast connectionless data reception
595462e630dS[email protected]                             uint32_t features = 0x280;
5963b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5973b0484b3S[email protected]                         }
598202c8a4cSMatthias Ringwald                         break;
5993b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
6003b0484b3S[email protected]                             uint8_t map[8];
6013b0484b3S[email protected]                             memset(map, 0, 8);
602288636a2SMatthias Ringwald                             map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
6033b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
6043b0484b3S[email protected]                         }
605202c8a4cSMatthias Ringwald                         break;
6063b0484b3S[email protected]                     default:
6072b83fb7dSmatthias.ringwald                         // all other types are not supported
6082b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
6093b0484b3S[email protected]                         break;
6102b83fb7dSmatthias.ringwald                 }
6112b83fb7dSmatthias.ringwald                 break;
61263a7246aSmatthias.ringwald             case COMMAND_REJECT:
6135ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
61463f0ac45SMatthias Ringwald                 break;
61509e9d05bSMatthias Ringwald #endif
616a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
61763f0ac45SMatthias Ringwald             case LE_CREDIT_BASED_CONNECTION_REQUEST:
61863f0ac45SMatthias Ringwald                 l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result);
61963f0ac45SMatthias Ringwald                 break;
62070efece1S[email protected]             case COMMAND_REJECT_LE:
62170efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
62263a7246aSmatthias.ringwald                 break;
62370efece1S[email protected] #endif
6242b83fb7dSmatthias.ringwald             default:
6252b83fb7dSmatthias.ringwald                 // should not happen
6262b83fb7dSmatthias.ringwald                 break;
6272b83fb7dSmatthias.ringwald         }
6282b83fb7dSmatthias.ringwald     }
6292b83fb7dSmatthias.ringwald 
630665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
63109e9d05bSMatthias Ringwald     (void) it;
63209e9d05bSMatthias Ringwald 
63309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
63409e9d05bSMatthias Ringwald     uint8_t  config_options[4];
635665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
636665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
637baf94f06S[email protected] 
638665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
63922c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6402cd0be45Smatthias.ringwald         switch (channel->state){
6412cd0be45Smatthias.ringwald 
642df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
643ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
644fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
645a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
646ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
647fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
648ad671560S[email protected]                 }
649ad671560S[email protected]                 break;
650ad671560S[email protected] 
65102b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
652baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
65364472d52Smatthias.ringwald                 // send connection request - set state first
65464472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
65502b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6568f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
65702b22dc4Smatthias.ringwald                 break;
65802b22dc4Smatthias.ringwald 
659e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
660fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
66122c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
662fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
663e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6649dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
665665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
666d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
667e7ff783cSmatthias.ringwald                 break;
668e7ff783cSmatthias.ringwald 
669552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
670fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
671fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
67228ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
673fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
674552d92a1Smatthias.ringwald                 break;
675552d92a1Smatthias.ringwald 
6766fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
677fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6786fdcc387Smatthias.ringwald                 // success, start l2cap handshake
679b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6806fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
681fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6825932bd7cS[email protected]                 l2cap_start_rtx(channel);
6836fdcc387Smatthias.ringwald                 break;
6846fdcc387Smatthias.ringwald 
685fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
686fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
68773cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
68863a7246aSmatthias.ringwald                     uint16_t flags = 0;
68928ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
69063a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
69163a7246aSmatthias.ringwald                         flags = 1;
69263a7246aSmatthias.ringwald                     } else {
69328ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
69463a7246aSmatthias.ringwald                     }
69563a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
696fc64f94aSMatthias 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);
69763a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
69863a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
69963a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
700f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
701fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
70263a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
70363a7246aSmatthias.ringwald                     } else {
704fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
70563a7246aSmatthias.ringwald                     }
70663a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
707fa8473a4Smatthias.ringwald                 }
70873cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
70928ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
71028ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
711b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
712ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
713ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
714f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
715fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
7165932bd7cS[email protected]                     l2cap_start_rtx(channel);
717fa8473a4Smatthias.ringwald                 }
718fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
719552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
720552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
721fa8473a4Smatthias.ringwald                 }
722552d92a1Smatthias.ringwald                 break;
723552d92a1Smatthias.ringwald 
724e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
725fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
72622c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
727fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7285932bd7cS[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 :)
729756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
730e7ff783cSmatthias.ringwald                 break;
731e7ff783cSmatthias.ringwald 
732e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
733fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
734b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7352cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
736fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7372cd0be45Smatthias.ringwald                 break;
7382cd0be45Smatthias.ringwald             default:
7392cd0be45Smatthias.ringwald                 break;
7402cd0be45Smatthias.ringwald         }
7412cd0be45Smatthias.ringwald     }
74209e9d05bSMatthias Ringwald #endif
743da886c03S[email protected] 
744cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
745efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
746efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7477f107edaSMatthias Ringwald         uint8_t  * acl_buffer;
7487f107edaSMatthias Ringwald         uint8_t  * l2cap_payload;
7497f107edaSMatthias Ringwald         uint16_t pos;
7507f107edaSMatthias Ringwald         uint16_t payload_size;
751efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
752efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
753efedfb4cSMatthias Ringwald         switch (channel->state){
7545cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7555cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7565cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
7575cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
7581b8b8d05SMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
75963f0ac45SMatthias Ringwald                 channel->credits_incoming =  channel->new_credits_incoming;
76063f0ac45SMatthias Ringwald                 channel->new_credits_incoming = 0;
76163f0ac45SMatthias 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);
7625cb87675SMatthias Ringwald                 break;
76323017473SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
76423017473SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
76523017473SMatthias Ringwald                 // TODO: support larger MPS
76623017473SMatthias Ringwald                 channel->state = L2CAP_STATE_OPEN;
76763f0ac45SMatthias Ringwald                 channel->credits_incoming =  channel->new_credits_incoming;
76863f0ac45SMatthias Ringwald                 channel->new_credits_incoming = 0;
76963f0ac45SMatthias 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);
77064e11ca9SMatthias Ringwald                 // notify client
77144276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, 0);
77223017473SMatthias Ringwald                 break;
773e7d0c9aaSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
774e7d0c9aaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
775e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
77663f0ac45SMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason);
777e7d0c9aaSMatthias Ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
778e7d0c9aaSMatthias Ringwald                 l2cap_stop_rtx(channel);
779e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
780e7d0c9aaSMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
781e7d0c9aaSMatthias Ringwald                 break;
7827f107edaSMatthias Ringwald             case L2CAP_STATE_OPEN:
78385aeef60SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
78485aeef60SMatthias Ringwald 
78585aeef60SMatthias Ringwald                 // send credits
78685aeef60SMatthias Ringwald                 if (channel->new_credits_incoming){
78785aeef60SMatthias Ringwald                     log_info("l2cap: sending %u credits", channel->new_credits_incoming);
78885aeef60SMatthias Ringwald                     channel->local_sig_id = l2cap_next_sig_id();
78985aeef60SMatthias Ringwald                     uint16_t new_credits = channel->new_credits_incoming;
79085aeef60SMatthias Ringwald                     channel->new_credits_incoming = 0;
79185aeef60SMatthias Ringwald                     channel->credits_incoming += new_credits;
79285aeef60SMatthias Ringwald                     l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits);
79385aeef60SMatthias Ringwald                     break;
79485aeef60SMatthias Ringwald                 }
79585aeef60SMatthias Ringwald 
79685aeef60SMatthias Ringwald                 // send data
7977f107edaSMatthias Ringwald                 if (!channel->send_sdu_buffer) break;
7987f107edaSMatthias Ringwald                 if (!channel->credits_outgoing) break;
79985aeef60SMatthias Ringwald 
8007f107edaSMatthias Ringwald                 // send part of SDU
8017f107edaSMatthias Ringwald                 hci_reserve_packet_buffer();
8027f107edaSMatthias Ringwald                 acl_buffer = hci_get_outgoing_packet_buffer();
8037f107edaSMatthias Ringwald                 l2cap_payload = acl_buffer + 8;
8047f107edaSMatthias Ringwald                 pos = 0;
8057f107edaSMatthias Ringwald                 if (!channel->send_sdu_pos){
8067f107edaSMatthias Ringwald                     // store SDU len
8077f107edaSMatthias Ringwald                     channel->send_sdu_pos += 2;
8087f107edaSMatthias Ringwald                     little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len);
8097f107edaSMatthias Ringwald                     pos += 2;
8107f107edaSMatthias Ringwald                 }
8117f107edaSMatthias Ringwald                 payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos);
81285aeef60SMatthias Ringwald                 log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing);
8137f107edaSMatthias Ringwald                 memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len
8147f107edaSMatthias Ringwald                 pos += payload_size;
8157f107edaSMatthias Ringwald                 channel->send_sdu_pos += payload_size;
81663f0ac45SMatthias Ringwald                 l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, pos);
8177f107edaSMatthias Ringwald                 // done
8187f107edaSMatthias Ringwald 
81985aeef60SMatthias Ringwald                 channel->credits_outgoing--;
8207f107edaSMatthias Ringwald 
8217f107edaSMatthias Ringwald                 if (channel->send_sdu_pos >= channel->send_sdu_len + 2){
8227f107edaSMatthias Ringwald                     channel->send_sdu_buffer = NULL;
82344276248SMatthias Ringwald                     // send done event
82444276248SMatthias Ringwald                     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT);
82544276248SMatthias Ringwald                     // inform about can send now
82644276248SMatthias Ringwald                     l2cap_le_notify_channel_can_send(channel);
8277f107edaSMatthias Ringwald                 }
8287f107edaSMatthias Ringwald                 hci_send_acl_packet_buffer(8 + pos);
8297f107edaSMatthias Ringwald                 break;
830828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
831828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
832828a7f7aSMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
833828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
834828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
835828a7f7aSMatthias Ringwald                 break;
836828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
837828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
838828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
839828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
840828a7f7aSMatthias Ringwald                 l2cap_le_finialize_channel_close(channel);  // -- remove from list
841828a7f7aSMatthias Ringwald                 break;
842efedfb4cSMatthias Ringwald             default:
843efedfb4cSMatthias Ringwald                 break;
844efedfb4cSMatthias Ringwald         }
845efedfb4cSMatthias Ringwald     }
84609e9d05bSMatthias Ringwald #endif
847efedfb4cSMatthias Ringwald 
84809e9d05bSMatthias Ringwald #ifdef ENABLE_BLE
849da886c03S[email protected]     // send l2cap con paramter update if necessary
850da886c03S[email protected]     hci_connections_get_iterator(&it);
851665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
852665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
8535d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
854b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
855da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
856b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
857b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
858b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
859b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
860b68d7bc3SMatthias Ringwald                 break;
861da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
862b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
863b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
864da886c03S[email protected]                 break;
865da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
866b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
867b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
868da886c03S[email protected]                 break;
869da886c03S[email protected]             default:
870da886c03S[email protected]                 break;
871da886c03S[email protected]         }
872da886c03S[email protected]     }
8734d7157c3S[email protected] #endif
874da886c03S[email protected] 
87522c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
8762cd0be45Smatthias.ringwald }
8772cd0be45Smatthias.ringwald 
87809e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
879fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
8802df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
8815533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
8822df5dadcS[email protected]         // success, start l2cap handshake
883fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
8842df5dadcS[email protected]         // check remote SSP feature first
8852df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
8862df5dadcS[email protected]     }
8872df5dadcS[email protected] }
8882df5dadcS[email protected] 
8892df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
8902df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
8912df5dadcS[email protected] 
8922df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
893ac301f95S[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));
894fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
8952df5dadcS[email protected]         // request security level 2
8962df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
897fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
8982df5dadcS[email protected]         return;
8992df5dadcS[email protected]     }
9002df5dadcS[email protected]     // fine, go ahead
9012df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
9022df5dadcS[email protected] }
90309e9d05bSMatthias Ringwald #endif
9042df5dadcS[email protected] 
90509e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS
906da144af5SMatthias 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,
907da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
908da144af5SMatthias Ringwald 
909da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
910da144af5SMatthias Ringwald     if (!channel) {
911da144af5SMatthias Ringwald         return NULL;
912da144af5SMatthias Ringwald     }
913da144af5SMatthias Ringwald 
914da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
915da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
916da144af5SMatthias Ringwald 
917da144af5SMatthias Ringwald     // fill in
918da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
919da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
920da144af5SMatthias Ringwald     channel->address_type = address_type;
921da144af5SMatthias Ringwald     channel->psm = psm;
922da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
923da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
924da144af5SMatthias Ringwald     channel->required_security_level = security_level;
925da144af5SMatthias Ringwald 
926da144af5SMatthias Ringwald     //
927da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
928da144af5SMatthias Ringwald     channel->con_handle = 0;
929da144af5SMatthias Ringwald 
930da144af5SMatthias Ringwald     // set initial state
931da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
932da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
933da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
934da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
935da144af5SMatthias Ringwald     return channel;
936da144af5SMatthias Ringwald }
93709e9d05bSMatthias Ringwald #endif
93809e9d05bSMatthias Ringwald 
93909e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
940da144af5SMatthias Ringwald 
9419077cb15SMatthias Ringwald /**
9429077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
9439077cb15SMatthias Ringwald  * @param packet_handler
9449077cb15SMatthias Ringwald  * @param address
9459077cb15SMatthias Ringwald  * @param psm
9469077cb15SMatthias Ringwald  * @param mtu
9479077cb15SMatthias Ringwald  * @param local_cid
9489077cb15SMatthias Ringwald  */
9499077cb15SMatthias Ringwald 
950da144af5SMatthias Ringwald uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t local_mtu, uint16_t * out_local_cid){
951da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
952da144af5SMatthias Ringwald 
953da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
954da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
955da144af5SMatthias Ringwald     }
956da144af5SMatthias Ringwald 
957da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
958fc64f94aSMatthias Ringwald     if (!channel) {
9599077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
9609077cb15SMatthias Ringwald     }
9619077cb15SMatthias Ringwald 
9629077cb15SMatthias Ringwald     // add to connections list
963fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
9649077cb15SMatthias Ringwald 
9659077cb15SMatthias Ringwald     // store local_cid
9669077cb15SMatthias Ringwald     if (out_local_cid){
967fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
9689077cb15SMatthias Ringwald     }
9699077cb15SMatthias Ringwald 
9709077cb15SMatthias Ringwald     // check if hci connection is already usable
9719077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
9729077cb15SMatthias Ringwald     if (conn){
973add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
974fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
9759077cb15SMatthias Ringwald         // check if remote supported fearures are already received
9769077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
977fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
9789077cb15SMatthias Ringwald         }
9799077cb15SMatthias Ringwald     }
9809077cb15SMatthias Ringwald 
9819077cb15SMatthias Ringwald     l2cap_run();
9829077cb15SMatthias Ringwald 
9839077cb15SMatthias Ringwald     return 0;
9849077cb15SMatthias Ringwald }
9859077cb15SMatthias Ringwald 
986ce8f182eSMatthias Ringwald void
987ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
988e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
989b35f641cSmatthias.ringwald     // find channel for local_cid
990b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
991f62db1e3Smatthias.ringwald     if (channel) {
992e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
993f62db1e3Smatthias.ringwald     }
9942cd0be45Smatthias.ringwald     // process
9952cd0be45Smatthias.ringwald     l2cap_run();
99643625864Smatthias.ringwald }
9971e6aba47Smatthias.ringwald 
998afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
999665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1000665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1001665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1002665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1003058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
1004c22aecc9S[email protected]         // channel for this address found
1005c22aecc9S[email protected]         switch (channel->state){
1006c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
1007c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
1008afde0c52Smatthias.ringwald                 // failure, forward error code
1009afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
1010afde0c52Smatthias.ringwald                 // discard channel
10119dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1012665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1013d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
1014c22aecc9S[email protected]                 break;
1015c22aecc9S[email protected]             default:
1016c22aecc9S[email protected]                 break;
1017afde0c52Smatthias.ringwald         }
1018afde0c52Smatthias.ringwald     }
1019afde0c52Smatthias.ringwald }
1020afde0c52Smatthias.ringwald 
1021afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
1022665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1023665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1024665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1025665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1026058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
10272df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
1028afde0c52Smatthias.ringwald         }
1029afde0c52Smatthias.ringwald     }
10306fdcc387Smatthias.ringwald     // process
10316fdcc387Smatthias.ringwald     l2cap_run();
1032afde0c52Smatthias.ringwald }
103309e9d05bSMatthias Ringwald #endif
1034b448a0e7Smatthias.ringwald 
103533c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
103609e9d05bSMatthias Ringwald 
103709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
103833c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
103933c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
104033c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
104133c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
104233c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
1043fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
104433c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
104534e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
104633c40538SMatthias Ringwald     }
104709e9d05bSMatthias Ringwald #endif
104844276248SMatthias Ringwald 
10492125de09SMatthias Ringwald     int i;
10502125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
1051773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
10522125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
105335454696SMatthias Ringwald         int can_send = 0;
105434e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
105535454696SMatthias Ringwald #ifdef ENABLE_BLE
105634e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
105735454696SMatthias Ringwald #endif
105834e7e577SMatthias Ringwald         } else {
105935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC
106034e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
106135454696SMatthias Ringwald #endif
106234e7e577SMatthias Ringwald         }
106334e7e577SMatthias Ringwald         if (!can_send) continue;
106434e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
106534e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
10662125de09SMatthias Ringwald     }
106733c40538SMatthias Ringwald }
106833c40538SMatthias Ringwald 
1069d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
1070afde0c52Smatthias.ringwald 
1071afde0c52Smatthias.ringwald     bd_addr_t address;
1072afde0c52Smatthias.ringwald     hci_con_handle_t handle;
10732d00edd4Smatthias.ringwald     int hci_con_used;
107409e9d05bSMatthias Ringwald     btstack_linked_list_iterator_t it;
107509e9d05bSMatthias Ringwald 
107609e9d05bSMatthias Ringwald     // avoid unused warnings
107709e9d05bSMatthias Ringwald     (void) address;
107809e9d05bSMatthias Ringwald     (void) hci_con_used;
107909e9d05bSMatthias Ringwald     (void) it;
1080afde0c52Smatthias.ringwald 
10810e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1082afde0c52Smatthias.ringwald 
108309e9d05bSMatthias Ringwald         // Notify channel packet handler if they can send now
108409e9d05bSMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
108509e9d05bSMatthias Ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
108609e9d05bSMatthias Ringwald             l2cap_run();    // try sending signaling packets first
108709e9d05bSMatthias Ringwald             l2cap_notify_channel_can_send();
108809e9d05bSMatthias Ringwald             break;
108909e9d05bSMatthias Ringwald 
109009e9d05bSMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
109109e9d05bSMatthias Ringwald             l2cap_run();    // try sending signaling packets first
109209e9d05bSMatthias Ringwald             break;
109309e9d05bSMatthias Ringwald 
109409e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1095afde0c52Smatthias.ringwald         // handle connection complete events
1096afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1097724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
1098afde0c52Smatthias.ringwald             if (packet[2] == 0){
1099f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
1100afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
1101afde0c52Smatthias.ringwald             } else {
1102afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
1103afde0c52Smatthias.ringwald             }
1104afde0c52Smatthias.ringwald             break;
1105afde0c52Smatthias.ringwald 
1106afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1107afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1108073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1109afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1110724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1111afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1112afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
111303cfbabcSmatthias.ringwald                 }
11141e6aba47Smatthias.ringwald             }
111539d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
111639d59809Smatthias.ringwald             break;
111709e9d05bSMatthias Ringwald #endif
111827a923d0Smatthias.ringwald 
11191e6aba47Smatthias.ringwald         // handle disconnection complete events
1120afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1121c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
1122f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
112309e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1124665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1125665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1126665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1127fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
112815ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
11299dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1130665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1131d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
113227a923d0Smatthias.ringwald             }
113309e9d05bSMatthias Ringwald #endif
1134a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1135991fea48SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1136991fea48SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1137991fea48SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1138991fea48SMatthias Ringwald                 if (channel->con_handle != handle) continue;
1139991fea48SMatthias Ringwald                 l2cap_emit_channel_closed(channel);
1140991fea48SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1141991fea48SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
1142991fea48SMatthias Ringwald             }
1143991fea48SMatthias Ringwald #endif
1144afde0c52Smatthias.ringwald             break;
1145fcadd0caSmatthias.ringwald 
1146ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
114709e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1148afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1149f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1150bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
115180ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
11522d00edd4Smatthias.ringwald             hci_con_used = 0;
1153665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1154665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1155665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1156fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11572d00edd4Smatthias.ringwald                 hci_con_used = 1;
1158c22aecc9S[email protected]                 break;
1159ee091cf1Smatthias.ringwald             }
11602d00edd4Smatthias.ringwald             if (hci_con_used) break;
1161d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
11629edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1163afde0c52Smatthias.ringwald             break;
1164ee091cf1Smatthias.ringwald 
1165df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1166f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1167665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1168665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1169665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1170fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11712df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1172df3354fcS[email protected]                 break;
1173df3354fcS[email protected]             }
1174c22aecc9S[email protected]             break;
1175df3354fcS[email protected] 
11765611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1177f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1178bd63148eS[email protected]             log_info("l2cap - security level update");
1179665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1180665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1181665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1182fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11835533f01eS[email protected] 
1184bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1185bd63148eS[email protected] 
1186e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
11875533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
11885533f01eS[email protected] 
1189df3354fcS[email protected]                 switch (channel->state){
1190df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
11915533f01eS[email protected]                         if (actual_level >= required_level){
1192f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
119344276248SMatthias Ringwald                             l2cap_emit_incoming_connection(channel);
11941eb2563eS[email protected]                         } else {
1195775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
11961eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11971eb2563eS[email protected]                         }
1198df3354fcS[email protected]                         break;
1199df3354fcS[email protected] 
1200df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
12015533f01eS[email protected]                         if (actual_level >= required_level){
1202df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1203df3354fcS[email protected]                         } else {
1204df3354fcS[email protected]                             // disconnnect, authentication not good enough
1205df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1206df3354fcS[email protected]                         }
1207df3354fcS[email protected]                         break;
1208df3354fcS[email protected] 
1209df3354fcS[email protected]                     default:
1210df3354fcS[email protected]                         break;
1211df3354fcS[email protected]                 }
1212f85a9399S[email protected]             }
1213f85a9399S[email protected]             break;
121409e9d05bSMatthias Ringwald #endif
1215f85a9399S[email protected] 
1216afde0c52Smatthias.ringwald         default:
1217afde0c52Smatthias.ringwald             break;
1218afde0c52Smatthias.ringwald     }
1219afde0c52Smatthias.ringwald 
1220bd63148eS[email protected]     l2cap_run();
12211e6aba47Smatthias.ringwald }
12221e6aba47Smatthias.ringwald 
12232b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
12244cf56b4aSmatthias.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."
12252b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
12262b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
12272b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
12282b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
12292b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
12302b360848Smatthias.ringwald         signaling_responses_pending++;
12312b360848Smatthias.ringwald         l2cap_run();
12322b360848Smatthias.ringwald     }
12332b360848Smatthias.ringwald }
12342b360848Smatthias.ringwald 
123509e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
123609e9d05bSMatthias Ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
123709e9d05bSMatthias Ringwald     channel->remote_sig_id = identifier;
123809e9d05bSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
123909e9d05bSMatthias Ringwald     l2cap_run();
124009e9d05bSMatthias Ringwald }
124109e9d05bSMatthias Ringwald 
1242b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1243645658c9Smatthias.ringwald 
12449da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1245645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1246645658c9Smatthias.ringwald     if (!service) {
1247645658c9Smatthias.ringwald         // 0x0002 PSM not supported
12482b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1249645658c9Smatthias.ringwald         return;
1250645658c9Smatthias.ringwald     }
1251645658c9Smatthias.ringwald 
12525061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1253645658c9Smatthias.ringwald     if (!hci_connection) {
12542b360848Smatthias.ringwald         //
12559da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1256645658c9Smatthias.ringwald         return;
1257645658c9Smatthias.ringwald     }
12582bd8b7e7S[email protected] 
1259645658c9Smatthias.ringwald     // alloc structure
12609da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1261da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1262da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
12632b360848Smatthias.ringwald     if (!channel){
12642b360848Smatthias.ringwald         // 0x0004 No resources available
12652b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
12662b360848Smatthias.ringwald         return;
12672b360848Smatthias.ringwald     }
1268da144af5SMatthias Ringwald 
1269fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1270b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1271b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1272645658c9Smatthias.ringwald 
1273f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
12742985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
12752985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
12769775e25bSmatthias.ringwald     }
12779775e25bSmatthias.ringwald 
1278645658c9Smatthias.ringwald     // set initial state
1279df3354fcS[email protected]     channel->state =      L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1280*a24785d0SMatthias Ringwald     channel->state_var  = (L2CAP_CHANNEL_STATE_VAR) (L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND | L2CAP_CHANNEL_STATE_VAR_INCOMING);
1281e405ae81Smatthias.ringwald 
1282645658c9Smatthias.ringwald     // add to connections list
1283665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1284645658c9Smatthias.ringwald 
1285f85a9399S[email protected]     // assert security requirements
12861eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1287e405ae81Smatthias.ringwald }
1288645658c9Smatthias.ringwald 
1289ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1290e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1291b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1292e405ae81Smatthias.ringwald     if (!channel) {
1293ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1294e405ae81Smatthias.ringwald         return;
1295e405ae81Smatthias.ringwald     }
1296e405ae81Smatthias.ringwald 
1297552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1298e405ae81Smatthias.ringwald 
1299552d92a1Smatthias.ringwald     // process
1300552d92a1Smatthias.ringwald     l2cap_run();
1301e405ae81Smatthias.ringwald }
1302645658c9Smatthias.ringwald 
13037ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
13047ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1305b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1306e405ae81Smatthias.ringwald     if (!channel) {
1307ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1308e405ae81Smatthias.ringwald         return;
1309e405ae81Smatthias.ringwald     }
1310e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
13117ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1312e7ff783cSmatthias.ringwald     l2cap_run();
1313645658c9Smatthias.ringwald }
1314645658c9Smatthias.ringwald 
13157f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1316b1988dceSmatthias.ringwald 
1317b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1318b1988dceSmatthias.ringwald 
1319f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
132063a7246aSmatthias.ringwald     if (flags & 1) {
132163a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
132263a7246aSmatthias.ringwald     }
132363a7246aSmatthias.ringwald 
13242784b77dSmatthias.ringwald     // accept the other's configuration options
1325f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
13263de7c0caSmatthias.ringwald     uint16_t pos     = 8;
13273de7c0caSmatthias.ringwald     while (pos < end_pos){
132863a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
132963a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
133063a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
133163a7246aSmatthias.ringwald         pos++;
13321dc511deSmatthias.ringwald         uint8_t length = command[pos++];
13331dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
133463a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1335f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
13369da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
133763a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
133863a7246aSmatthias.ringwald         }
13390fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
13400fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1341f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
13420fe7a9d0S[email protected]         }
134363a7246aSmatthias.ringwald         // check for unknown options
134463a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1345c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
134663a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
13471dc511deSmatthias.ringwald         }
13481dc511deSmatthias.ringwald         pos += length;
13491dc511deSmatthias.ringwald     }
13502784b77dSmatthias.ringwald }
13512784b77dSmatthias.ringwald 
1352fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
13539da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
135473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
135573cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1356019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1357019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1358fa8473a4Smatthias.ringwald     return 1;
1359fa8473a4Smatthias.ringwald }
1360fa8473a4Smatthias.ringwald 
1361fa8473a4Smatthias.ringwald 
13627f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
13631e6aba47Smatthias.ringwald 
136400d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
136500d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
136638e5900eSmatthias.ringwald     uint16_t result = 0;
13671e6aba47Smatthias.ringwald 
13689da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1369b35f641cSmatthias.ringwald 
13709a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
13719a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
13729a011532Smatthias.ringwald         switch (channel->state){
1373fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
13749a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
13752b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
13769a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
13779a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
13789a011532Smatthias.ringwald                 break;
13799a011532Smatthias.ringwald 
13809a011532Smatthias.ringwald             default:
13819a011532Smatthias.ringwald                 // ignore in other states
13829a011532Smatthias.ringwald                 break;
13839a011532Smatthias.ringwald         }
13849a011532Smatthias.ringwald         return;
13859a011532Smatthias.ringwald     }
13869a011532Smatthias.ringwald 
138756081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
13881e6aba47Smatthias.ringwald     switch (channel->state) {
13891e6aba47Smatthias.ringwald 
13901e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
13911e6aba47Smatthias.ringwald             switch (code){
13921e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
13935932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1394f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
139538e5900eSmatthias.ringwald                     switch (result) {
139638e5900eSmatthias.ringwald                         case 0:
1397169f8b28Smatthias.ringwald                             // successful connection
1398f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1399fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
140028ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
140138e5900eSmatthias.ringwald                             break;
140238e5900eSmatthias.ringwald                         case 1:
14035932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
14045932bd7cS[email protected]                             l2cap_start_ertx(channel);
140538e5900eSmatthias.ringwald                             break;
140638e5900eSmatthias.ringwald                         default:
1407eb920dbeSmatthias.ringwald                             // channel closed
1408eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1409f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
141038e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1411eb920dbeSmatthias.ringwald 
1412eb920dbeSmatthias.ringwald                             // drop link key if security block
1413eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
141415a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1415eb920dbeSmatthias.ringwald                             }
1416eb920dbeSmatthias.ringwald 
1417eb920dbeSmatthias.ringwald                             // discard channel
1418665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1419d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
142038e5900eSmatthias.ringwald                             break;
14211e6aba47Smatthias.ringwald                     }
14221e6aba47Smatthias.ringwald                     break;
142338e5900eSmatthias.ringwald 
142438e5900eSmatthias.ringwald                 default:
14251e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
142638e5900eSmatthias.ringwald                     break;
14271e6aba47Smatthias.ringwald             }
14281e6aba47Smatthias.ringwald             break;
14291e6aba47Smatthias.ringwald 
1430fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1431f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1432ae280e73Smatthias.ringwald             switch (code) {
1433ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
143428ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1435ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
143663a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
143763a7246aSmatthias.ringwald                         // only done if continuation not set
143863a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
143963a7246aSmatthias.ringwald                     }
1440ae280e73Smatthias.ringwald                     break;
14411e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
14425932bd7cS[email protected]                     l2cap_stop_rtx(channel);
14435932bd7cS[email protected]                     switch (result){
14445932bd7cS[email protected]                         case 0: // success
14455932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
14465932bd7cS[email protected]                             break;
14475932bd7cS[email protected]                         case 4: // pending
14485932bd7cS[email protected]                             l2cap_start_ertx(channel);
14495932bd7cS[email protected]                             break;
14505932bd7cS[email protected]                         default:
1451fe9d8984S[email protected]                             // retry on negative result
1452fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1453fe9d8984S[email protected]                             break;
1454fe9d8984S[email protected]                     }
14555a67bd4aSmatthias.ringwald                     break;
14565a67bd4aSmatthias.ringwald                 default:
14575a67bd4aSmatthias.ringwald                     break;
14581e6aba47Smatthias.ringwald             }
1459fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1460fa8473a4Smatthias.ringwald                 // for open:
14615a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1462fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1463c8e4258aSmatthias.ringwald             }
1464c8e4258aSmatthias.ringwald             break;
1465f62db1e3Smatthias.ringwald 
1466f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1467f62db1e3Smatthias.ringwald             switch (code) {
1468f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
146927a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
147027a923d0Smatthias.ringwald                     break;
14715a67bd4aSmatthias.ringwald                 default:
14725a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
14735a67bd4aSmatthias.ringwald                     break;
147427a923d0Smatthias.ringwald             }
147527a923d0Smatthias.ringwald             break;
147684836b65Smatthias.ringwald 
147784836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
147884836b65Smatthias.ringwald             // @TODO handle incoming requests
147984836b65Smatthias.ringwald             break;
148084836b65Smatthias.ringwald 
148184836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
148284836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
148384836b65Smatthias.ringwald             break;
148410642e45Smatthias.ringwald         default:
148510642e45Smatthias.ringwald             break;
148627a923d0Smatthias.ringwald     }
14879da54300S[email protected]     // log_info("new state %u", channel->state);
148827a923d0Smatthias.ringwald }
148927a923d0Smatthias.ringwald 
149000d93d79Smatthias.ringwald 
14917f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
149200d93d79Smatthias.ringwald 
149300d93d79Smatthias.ringwald     // get code, signalind identifier and command len
149400d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
149500d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
149600d93d79Smatthias.ringwald 
149700d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
149800d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
149963a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
150000d93d79Smatthias.ringwald         return;
150100d93d79Smatthias.ringwald     }
150200d93d79Smatthias.ringwald 
150300d93d79Smatthias.ringwald     // general commands without an assigned channel
150400d93d79Smatthias.ringwald     switch(code) {
150500d93d79Smatthias.ringwald 
150600d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1507f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1508f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
150900d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
15102b83fb7dSmatthias.ringwald             return;
151100d93d79Smatthias.ringwald         }
151200d93d79Smatthias.ringwald 
15132b360848Smatthias.ringwald         case ECHO_REQUEST:
15142b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
15152b83fb7dSmatthias.ringwald             return;
151600d93d79Smatthias.ringwald 
151700d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1518f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
15192b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
15202b83fb7dSmatthias.ringwald             return;
152100d93d79Smatthias.ringwald         }
152200d93d79Smatthias.ringwald 
152300d93d79Smatthias.ringwald         default:
152400d93d79Smatthias.ringwald             break;
152500d93d79Smatthias.ringwald     }
152600d93d79Smatthias.ringwald 
152700d93d79Smatthias.ringwald 
152800d93d79Smatthias.ringwald     // Get potential destination CID
1529f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
153000d93d79Smatthias.ringwald 
153100d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1532665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1533665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1534665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1535665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1536fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
153700d93d79Smatthias.ringwald         if (code & 1) {
1538b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1539b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
154000d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15414e32727eSmatthias.ringwald                 break;
154200d93d79Smatthias.ringwald             }
154300d93d79Smatthias.ringwald         } else {
1544b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
154500d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
154600d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15474e32727eSmatthias.ringwald                 break;
154800d93d79Smatthias.ringwald             }
154900d93d79Smatthias.ringwald         }
155000d93d79Smatthias.ringwald     }
155100d93d79Smatthias.ringwald }
155209e9d05bSMatthias Ringwald #endif
155300d93d79Smatthias.ringwald 
1554e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
155509e9d05bSMatthias Ringwald 
155609e9d05bSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
155709e9d05bSMatthias Ringwald     uint8_t event[6];
155809e9d05bSMatthias Ringwald     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
155909e9d05bSMatthias Ringwald     event[1] = 4;
156009e9d05bSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
156109e9d05bSMatthias Ringwald     little_endian_store_16(event, 4, result);
156209e9d05bSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
156309e9d05bSMatthias Ringwald     if (!l2cap_event_packet_handler) return;
156409e9d05bSMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
156509e9d05bSMatthias Ringwald }
156609e9d05bSMatthias Ringwald 
1567c48b2a2cSMatthias Ringwald // @returns valid
1568c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1569e7d0c9aaSMatthias Ringwald     hci_connection_t * connection;
1570c48b2a2cSMatthias Ringwald     uint16_t result;
1571c48b2a2cSMatthias Ringwald     uint8_t  event[10];
157283fd9c76SMatthias Ringwald 
1573cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1574a3dc965aSMatthias Ringwald     btstack_linked_list_iterator_t it;
1575a3dc965aSMatthias Ringwald     l2cap_channel_t * channel;
1576a3dc965aSMatthias Ringwald     uint16_t local_cid;
157783fd9c76SMatthias Ringwald     uint16_t le_psm;
157863f0ac45SMatthias Ringwald     uint16_t new_credits;
157963f0ac45SMatthias Ringwald     uint16_t credits_before;
1580e7d0c9aaSMatthias Ringwald     l2cap_service_t * service;
158183fd9c76SMatthias Ringwald #endif
158200d93d79Smatthias.ringwald 
15831b8b8d05SMatthias Ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
158423017473SMatthias Ringwald     log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id);
15851b8b8d05SMatthias Ringwald 
15861b8b8d05SMatthias Ringwald     switch (code){
158700d93d79Smatthias.ringwald 
1588c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1589c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1590ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1591ccf076adS[email protected]             break;
1592da886c03S[email protected] 
1593c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1594e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1595da886c03S[email protected]             if (connection){
15966d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
15976d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1598c48b2a2cSMatthias Ringwald                     return 0;
15996d91fb6cSMatthias Ringwald                 }
1600da886c03S[email protected]                 int update_parameter = 1;
1601a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
16024ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1603c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1604c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1605c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1606c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1607da886c03S[email protected] 
1608da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1609da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1610da886c03S[email protected] 
1611da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1612da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1613da886c03S[email protected] 
1614da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1615da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1616da886c03S[email protected] 
1617da886c03S[email protected]                 if (update_parameter){
1618da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1619da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1620da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1621da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1622da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1623da886c03S[email protected]                 } else {
1624da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1625da886c03S[email protected]                 }
1626c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1627da886c03S[email protected]             }
1628da886c03S[email protected] 
162933c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1630c48b2a2cSMatthias Ringwald 
1631c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1632c48b2a2cSMatthias Ringwald             event[1] = 8;
1633c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1634c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
163533c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1636ccf076adS[email protected]             break;
1637c48b2a2cSMatthias Ringwald 
1638a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1639a3dc965aSMatthias Ringwald 
164063f0ac45SMatthias Ringwald         case COMMAND_REJECT:
164163f0ac45SMatthias Ringwald             // Find channel for this sig_id and connection handle
164263f0ac45SMatthias Ringwald             channel = NULL;
164363f0ac45SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
164463f0ac45SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
164563f0ac45SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
164663f0ac45SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
164763f0ac45SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
164863f0ac45SMatthias Ringwald                 channel = a_channel;
164963f0ac45SMatthias Ringwald                 break;
165063f0ac45SMatthias Ringwald             }
165163f0ac45SMatthias Ringwald             if (!channel) break;
165263f0ac45SMatthias Ringwald 
165363f0ac45SMatthias Ringwald             // if received while waiting for le connection response, assume legacy device
165463f0ac45SMatthias Ringwald             if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){
165563f0ac45SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
165663f0ac45SMatthias Ringwald                 // no official value for this, use: Connection refused – LE_PSM not supported - 0x0002
165744276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, 0x0002);
165863f0ac45SMatthias Ringwald 
165963f0ac45SMatthias Ringwald                 // discard channel
166063f0ac45SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
166163f0ac45SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
166263f0ac45SMatthias Ringwald                 break;
166363f0ac45SMatthias Ringwald             }
166463f0ac45SMatthias Ringwald             break;
166563f0ac45SMatthias Ringwald 
1666e7d0c9aaSMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_REQUEST:
1667e7d0c9aaSMatthias Ringwald 
1668e7d0c9aaSMatthias Ringwald             // get hci connection, bail if not found (must not happen)
1669e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1670e7d0c9aaSMatthias Ringwald             if (!connection) return 0;
1671e7d0c9aaSMatthias Ringwald 
1672e7d0c9aaSMatthias Ringwald             // check if service registered
1673e7d0c9aaSMatthias Ringwald             le_psm  = little_endian_read_16(command, 4);
1674e7d0c9aaSMatthias Ringwald             service = l2cap_le_get_service(le_psm);
1675e7d0c9aaSMatthias Ringwald 
1676e7d0c9aaSMatthias Ringwald             if (service){
1677e7d0c9aaSMatthias Ringwald 
1678e7d0c9aaSMatthias Ringwald                 uint16_t source_cid = little_endian_read_16(command, 6);
1679e7d0c9aaSMatthias Ringwald                 if (source_cid < 0x40){
1680e7d0c9aaSMatthias Ringwald                     // 0x0009 Connection refused - Invalid Source CID
1681e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009);
1682e7d0c9aaSMatthias Ringwald                     return 1;
1683e7d0c9aaSMatthias Ringwald                 }
1684e7d0c9aaSMatthias Ringwald 
1685e7d0c9aaSMatthias Ringwald                 // go through list of channels for this ACL connection and check if we get a match
1686e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1687e7d0c9aaSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it)){
16881b8b8d05SMatthias Ringwald                     l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16891b8b8d05SMatthias Ringwald                     if (a_channel->con_handle != handle) continue;
16901b8b8d05SMatthias Ringwald                     if (a_channel->remote_cid != source_cid) continue;
1691e7d0c9aaSMatthias Ringwald                     // 0x000a Connection refused - Source CID already allocated
1692e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a);
1693e7d0c9aaSMatthias Ringwald                     return 1;
1694e7d0c9aaSMatthias Ringwald                 }
1695e7d0c9aaSMatthias Ringwald 
169683fd9c76SMatthias Ringwald                 // security: check encryption
169783fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_2){
169883fd9c76SMatthias Ringwald                     if (sm_encryption_key_size(handle) == 0){
169983fd9c76SMatthias Ringwald                         // 0x0008 Connection refused - insufficient encryption
170083fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0008);
170183fd9c76SMatthias Ringwald                         return 1;
170283fd9c76SMatthias Ringwald                     }
170383fd9c76SMatthias Ringwald                     // anything less than 16 byte key size is insufficient
170483fd9c76SMatthias Ringwald                     if (sm_encryption_key_size(handle) < 16){
170583fd9c76SMatthias Ringwald                         // 0x0007 Connection refused – insufficient encryption key size
170683fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0007);
170783fd9c76SMatthias Ringwald                         return 1;
170883fd9c76SMatthias Ringwald                     }
170983fd9c76SMatthias Ringwald                 }
171083fd9c76SMatthias Ringwald 
171183fd9c76SMatthias Ringwald                 // security: check authencation
171283fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_3){
171383fd9c76SMatthias Ringwald                     if (!sm_authenticated(handle)){
171483fd9c76SMatthias Ringwald                         // 0x0005 Connection refused – insufficient authentication
171583fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0005);
171683fd9c76SMatthias Ringwald                         return 1;
171783fd9c76SMatthias Ringwald                     }
171883fd9c76SMatthias Ringwald                 }
171983fd9c76SMatthias Ringwald 
172083fd9c76SMatthias Ringwald                 // security: check authorization
172183fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_4){
172283fd9c76SMatthias Ringwald                     if (sm_authorization_state(handle) != AUTHORIZATION_GRANTED){
172383fd9c76SMatthias Ringwald                         // 0x0006 Connection refused – insufficient authorization
172483fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0006);
172583fd9c76SMatthias Ringwald                         return 1;
172683fd9c76SMatthias Ringwald                     }
172783fd9c76SMatthias Ringwald                 }
1728e7d0c9aaSMatthias Ringwald 
1729e7d0c9aaSMatthias Ringwald                 // allocate channel
17301b8b8d05SMatthias Ringwald                 channel = l2cap_create_channel_entry(service->packet_handler, connection->address,
1731e7d0c9aaSMatthias Ringwald                     BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
1732e7d0c9aaSMatthias Ringwald                 if (!channel){
1733e7d0c9aaSMatthias Ringwald                     // 0x0004 Connection refused – no resources available
1734e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004);
1735e7d0c9aaSMatthias Ringwald                     return 1;
1736e7d0c9aaSMatthias Ringwald                 }
1737e7d0c9aaSMatthias Ringwald 
1738e7d0c9aaSMatthias Ringwald                 channel->con_handle = handle;
1739e7d0c9aaSMatthias Ringwald                 channel->remote_cid = source_cid;
1740e7d0c9aaSMatthias Ringwald                 channel->remote_sig_id = sig_id;
17417f107edaSMatthias Ringwald                 channel->remote_mtu = little_endian_read_16(command, 8);
17427f107edaSMatthias Ringwald                 channel->remote_mps = little_endian_read_16(command, 10);
17437f107edaSMatthias Ringwald                 channel->credits_outgoing = little_endian_read_16(command, 12);
1744e7d0c9aaSMatthias Ringwald 
1745e7d0c9aaSMatthias Ringwald                 // set initial state
1746e7d0c9aaSMatthias Ringwald                 channel->state      = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1747c99bb618SMatthias Ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING;
1748e7d0c9aaSMatthias Ringwald 
1749e7d0c9aaSMatthias Ringwald                 // add to connections list
1750e7d0c9aaSMatthias Ringwald                 btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1751e7d0c9aaSMatthias Ringwald 
1752e7d0c9aaSMatthias Ringwald                 // post connection request event
175344276248SMatthias Ringwald                 l2cap_emit_le_incoming_connection(channel);
1754e7d0c9aaSMatthias Ringwald 
1755e7d0c9aaSMatthias Ringwald             } else {
1756e7d0c9aaSMatthias Ringwald                 // Connection refused – LE_PSM not supported
1757e7d0c9aaSMatthias Ringwald                 l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002);
1758e7d0c9aaSMatthias Ringwald             }
1759e7d0c9aaSMatthias Ringwald             break;
17601b8b8d05SMatthias Ringwald 
17611b8b8d05SMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_RESPONSE:
17621b8b8d05SMatthias Ringwald             // Find channel for this sig_id and connection handle
17631b8b8d05SMatthias Ringwald             channel = NULL;
17641b8b8d05SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
17651b8b8d05SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
17661b8b8d05SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
17671b8b8d05SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
17681b8b8d05SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
17691b8b8d05SMatthias Ringwald                 channel = a_channel;
17701b8b8d05SMatthias Ringwald                 break;
17711b8b8d05SMatthias Ringwald             }
17721b8b8d05SMatthias Ringwald             if (!channel) break;
17731b8b8d05SMatthias Ringwald 
17741b8b8d05SMatthias Ringwald             // cid + 0
17751b8b8d05SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
17761b8b8d05SMatthias Ringwald             if (result){
17771b8b8d05SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
17781b8b8d05SMatthias Ringwald                 // map l2cap connection response result to BTstack status enumeration
177944276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, result);
17801b8b8d05SMatthias Ringwald 
17811b8b8d05SMatthias Ringwald                 // discard channel
178263f0ac45SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
17831b8b8d05SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
17841b8b8d05SMatthias Ringwald                 break;
17851b8b8d05SMatthias Ringwald             }
17861b8b8d05SMatthias Ringwald 
17871b8b8d05SMatthias Ringwald             // success
17881b8b8d05SMatthias Ringwald             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
17891b8b8d05SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
17901b8b8d05SMatthias Ringwald             channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
17911b8b8d05SMatthias Ringwald             channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
17921b8b8d05SMatthias Ringwald             channel->state = L2CAP_STATE_OPEN;
179344276248SMatthias Ringwald             l2cap_emit_le_channel_opened(channel, result);
17941b8b8d05SMatthias Ringwald             break;
17951b8b8d05SMatthias Ringwald 
179685aeef60SMatthias Ringwald         case LE_FLOW_CONTROL_CREDIT:
179785aeef60SMatthias Ringwald             // find channel
179885aeef60SMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
179985aeef60SMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
180063f0ac45SMatthias Ringwald             if (!channel) {
180163f0ac45SMatthias Ringwald                 log_error("l2cap: no channel for cid 0x%02x", local_cid);
180263f0ac45SMatthias Ringwald                 break;
180363f0ac45SMatthias Ringwald             }
180463f0ac45SMatthias Ringwald             new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
180563f0ac45SMatthias Ringwald             credits_before = channel->credits_outgoing;
180663f0ac45SMatthias Ringwald             channel->credits_outgoing += new_credits;
180763f0ac45SMatthias Ringwald             // check for credit overrun
180863f0ac45SMatthias Ringwald             if (credits_before > channel->credits_outgoing){
180963f0ac45SMatthias Ringwald                 log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid);
181063f0ac45SMatthias Ringwald                 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
181163f0ac45SMatthias Ringwald                 break;
181263f0ac45SMatthias Ringwald             }
181363f0ac45SMatthias Ringwald             log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing);
181485aeef60SMatthias Ringwald             break;
181585aeef60SMatthias Ringwald 
1816828a7f7aSMatthias Ringwald         case DISCONNECTION_REQUEST:
1817828a7f7aSMatthias Ringwald             // find channel
1818828a7f7aSMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
1819828a7f7aSMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
182063f34e00SMatthias Ringwald             if (!channel) {
182163f34e00SMatthias Ringwald                 log_error("l2cap: no channel for cid 0x%02x", local_cid);
182263f34e00SMatthias Ringwald                 break;
182363f34e00SMatthias Ringwald             }
1824828a7f7aSMatthias Ringwald             channel->remote_sig_id = sig_id;
1825828a7f7aSMatthias Ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1826828a7f7aSMatthias Ringwald             break;
1827828a7f7aSMatthias Ringwald 
1828a3dc965aSMatthias Ringwald #endif
1829a3dc965aSMatthias Ringwald 
183063f0ac45SMatthias Ringwald         case DISCONNECTION_RESPONSE:
183163f0ac45SMatthias Ringwald             break;
183263f0ac45SMatthias Ringwald 
1833c48b2a2cSMatthias Ringwald         default:
1834c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1835c48b2a2cSMatthias Ringwald             return 0;
1836ccf076adS[email protected]     }
1837c48b2a2cSMatthias Ringwald     return 1;
1838c48b2a2cSMatthias Ringwald }
1839e7d0c9aaSMatthias Ringwald #endif
1840c48b2a2cSMatthias Ringwald 
1841c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
1842c48b2a2cSMatthias Ringwald 
184309e9d05bSMatthias Ringwald     l2cap_channel_t * l2cap_channel;
184409e9d05bSMatthias Ringwald     (void) l2cap_channel;
184509e9d05bSMatthias Ringwald 
1846c48b2a2cSMatthias Ringwald     // Get Channel ID
1847c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1848c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1849c48b2a2cSMatthias Ringwald 
1850c48b2a2cSMatthias Ringwald     switch (channel_id) {
1851c48b2a2cSMatthias Ringwald 
185209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1853c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1854c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1855c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1856c48b2a2cSMatthias Ringwald                 // handle signaling commands
1857c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1858c48b2a2cSMatthias Ringwald 
1859c48b2a2cSMatthias Ringwald                 // increment command_offset
1860c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1861c48b2a2cSMatthias Ringwald             }
1862c48b2a2cSMatthias Ringwald             break;
1863c48b2a2cSMatthias Ringwald         }
186409e9d05bSMatthias Ringwald #endif
1865c48b2a2cSMatthias Ringwald 
1866e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1867e7d0c9aaSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE: {
1868e7d0c9aaSMatthias Ringwald             uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1869e7d0c9aaSMatthias Ringwald             int      valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1870c48b2a2cSMatthias Ringwald             if (!valid){
18711bbc0b23S[email protected]                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
1872ccf076adS[email protected]             }
1873ccf076adS[email protected]             break;
1874e7d0c9aaSMatthias Ringwald         }
1875e7d0c9aaSMatthias Ringwald #endif
1876c48b2a2cSMatthias Ringwald 
1877c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1878c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1879c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1880ccf076adS[email protected]             }
1881c48b2a2cSMatthias Ringwald             break;
1882c48b2a2cSMatthias Ringwald 
1883c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1884c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1885c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1886c48b2a2cSMatthias Ringwald             }
1887c48b2a2cSMatthias Ringwald             break;
1888c48b2a2cSMatthias Ringwald 
1889c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1890c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1891c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1892c48b2a2cSMatthias Ringwald             }
1893c48b2a2cSMatthias Ringwald             break;
18941bbc0b23S[email protected] 
189509e9d05bSMatthias Ringwald         default:
189609e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
189700d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
189864e11ca9SMatthias Ringwald             l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1899d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
19003d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
190100d93d79Smatthias.ringwald             }
190209e9d05bSMatthias Ringwald #endif
1903a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
190464e11ca9SMatthias Ringwald             l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id);
190564e11ca9SMatthias Ringwald             if (l2cap_channel) {
190685aeef60SMatthias Ringwald                 // credit counting
190785aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming == 0){
190885aeef60SMatthias Ringwald                     log_error("LE Data Channel packet received but no incoming credits");
190985aeef60SMatthias Ringwald                     l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
191085aeef60SMatthias Ringwald                     break;
191185aeef60SMatthias Ringwald                 }
191285aeef60SMatthias Ringwald                 l2cap_channel->credits_incoming--;
191385aeef60SMatthias Ringwald 
191485aeef60SMatthias Ringwald                 // automatic credits
191585aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){
191685aeef60SMatthias Ringwald                     l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT;
191785aeef60SMatthias Ringwald                 }
191885aeef60SMatthias Ringwald 
1919cd529728SMatthias Ringwald                 // first fragment
1920cd529728SMatthias Ringwald                 uint16_t pos = 0;
1921cd529728SMatthias Ringwald                 if (!l2cap_channel->receive_sdu_len){
1922cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
1923cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_pos = 0;
1924cd529728SMatthias Ringwald                     pos  += 2;
1925cd529728SMatthias Ringwald                     size -= 2;
1926cd529728SMatthias Ringwald                 }
1927cd529728SMatthias Ringwald                 memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER);
1928cd529728SMatthias Ringwald                 l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER;
1929cd529728SMatthias Ringwald                 // done?
193063f0ac45SMatthias Ringwald                 log_info("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len);
1931cd529728SMatthias Ringwald                 if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){
1932cd529728SMatthias Ringwald                     l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len);
1933cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = 0;
1934cd529728SMatthias Ringwald                 }
193563f0ac45SMatthias Ringwald             } else {
193663f0ac45SMatthias Ringwald                 log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id);
193764e11ca9SMatthias Ringwald             }
193864e11ca9SMatthias Ringwald #endif
19395652b5ffS[email protected]             break;
19405652b5ffS[email protected]     }
194100d93d79Smatthias.ringwald 
19421eb2563eS[email protected]     l2cap_run();
19432718e2e7Smatthias.ringwald }
194400d93d79Smatthias.ringwald 
194509e9d05bSMatthias Ringwald // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
194609e9d05bSMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
194709e9d05bSMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
194809e9d05bSMatthias Ringwald     if (index < 0) return;
194909e9d05bSMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
195009e9d05bSMatthias Ringwald }
195109e9d05bSMatthias Ringwald 
195209e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
195315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
195427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1955f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1956f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1957f62db1e3Smatthias.ringwald     // discard channel
19589dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1959665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1960d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1961c8e4258aSmatthias.ringwald }
19621e6aba47Smatthias.ringwald 
19638f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1964665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1965665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1966665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1967665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
19689d9bbc01Smatthias.ringwald         if ( service->psm == psm){
19699d9bbc01Smatthias.ringwald             return service;
19709d9bbc01Smatthias.ringwald         };
19719d9bbc01Smatthias.ringwald     }
19729d9bbc01Smatthias.ringwald     return NULL;
19739d9bbc01Smatthias.ringwald }
19749d9bbc01Smatthias.ringwald 
19757192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
19767192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
19777192e786SMatthias Ringwald }
19787192e786SMatthias Ringwald 
1979e0abb8e7S[email protected] 
1980be2053a6SMatthias 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){
1981be2053a6SMatthias Ringwald 
1982be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1983e0abb8e7S[email protected] 
19844bb582b6Smatthias.ringwald     // check for alread registered psm
19859d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1986277abc2cSmatthias.ringwald     if (service) {
1987be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1988be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1989277abc2cSmatthias.ringwald     }
19909d9bbc01Smatthias.ringwald 
19914bb582b6Smatthias.ringwald     // alloc structure
1992bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1993277abc2cSmatthias.ringwald     if (!service) {
1994be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1995be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1996277abc2cSmatthias.ringwald     }
19979d9bbc01Smatthias.ringwald 
19989d9bbc01Smatthias.ringwald     // fill in
19999d9bbc01Smatthias.ringwald     service->psm = psm;
20009d9bbc01Smatthias.ringwald     service->mtu = mtu;
200105ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
2002df3354fcS[email protected]     service->required_security_level = security_level;
20039d9bbc01Smatthias.ringwald 
20049d9bbc01Smatthias.ringwald     // add to services list
2005665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
2006c0e866bfSmatthias.ringwald 
2007c0e866bfSmatthias.ringwald     // enable page scan
200815a95bd5SMatthias Ringwald     gap_connectable_control(1);
20095842b6d9Smatthias.ringwald 
2010be2053a6SMatthias Ringwald     return 0;
20119d9bbc01Smatthias.ringwald }
20129d9bbc01Smatthias.ringwald 
20137e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
2014e0abb8e7S[email protected] 
2015e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
2016e0abb8e7S[email protected] 
20179d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
20187e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
2019665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
2020d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
2021c0e866bfSmatthias.ringwald 
2022c0e866bfSmatthias.ringwald     // disable page scan when no services registered
20237e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
202415a95bd5SMatthias Ringwald         gap_connectable_control(0);
20259d9bbc01Smatthias.ringwald     }
20267e8856ebSMatthias Ringwald     return 0;
20277e8856ebSMatthias Ringwald }
202809e9d05bSMatthias Ringwald #endif
20299d9bbc01Smatthias.ringwald 
20307192e786SMatthias Ringwald 
2031cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
203283fd9c76SMatthias Ringwald 
203357be49d6SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){
203457be49d6SMatthias Ringwald     if (!channel->waiting_for_can_send_now) return;
203557be49d6SMatthias Ringwald     if (channel->send_sdu_buffer) return;
203657be49d6SMatthias Ringwald     channel->waiting_for_can_send_now = 0;
203757be49d6SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid);
203857be49d6SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW);
203957be49d6SMatthias Ringwald }
204057be49d6SMatthias Ringwald 
204157be49d6SMatthias Ringwald // 1BH2222
204257be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) {
204357be49d6SMatthias 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",
204457be49d6SMatthias Ringwald              channel->address_type, bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);
204557be49d6SMatthias Ringwald     uint8_t event[19];
204657be49d6SMatthias Ringwald     event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION;
204757be49d6SMatthias Ringwald     event[1] = sizeof(event) - 2;
204857be49d6SMatthias Ringwald     event[2] = channel->address_type;
204957be49d6SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
205057be49d6SMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
205157be49d6SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
205257be49d6SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
205357be49d6SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
205457be49d6SMatthias Ringwald     little_endian_store_16(event, 17, channel->remote_mtu);
205557be49d6SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
205657be49d6SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
205757be49d6SMatthias Ringwald }
205857be49d6SMatthias Ringwald // 11BH22222
205957be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) {
206057be49d6SMatthias 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",
206157be49d6SMatthias Ringwald              status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
206257be49d6SMatthias Ringwald              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
2063c99bb618SMatthias Ringwald     uint8_t event[23];
206457be49d6SMatthias Ringwald     event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED;
206557be49d6SMatthias Ringwald     event[1] = sizeof(event) - 2;
206657be49d6SMatthias Ringwald     event[2] = status;
206757be49d6SMatthias Ringwald     event[3] = channel->address_type;
206857be49d6SMatthias Ringwald     reverse_bd_addr(channel->address, &event[4]);
206957be49d6SMatthias Ringwald     little_endian_store_16(event, 10, channel->con_handle);
2070c99bb618SMatthias Ringwald     event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0;
2071c99bb618SMatthias Ringwald     little_endian_store_16(event, 13, channel->psm);
2072c99bb618SMatthias Ringwald     little_endian_store_16(event, 15, channel->local_cid);
2073c99bb618SMatthias Ringwald     little_endian_store_16(event, 17, channel->remote_cid);
2074c99bb618SMatthias Ringwald     little_endian_store_16(event, 19, channel->local_mtu);
2075c99bb618SMatthias Ringwald     little_endian_store_16(event, 21, channel->remote_mtu);
207657be49d6SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
207757be49d6SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
207857be49d6SMatthias Ringwald }
207957be49d6SMatthias Ringwald 
208057be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){
208157be49d6SMatthias Ringwald     btstack_linked_list_iterator_t it;
208257be49d6SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
208357be49d6SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
208457be49d6SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
208557be49d6SMatthias Ringwald         if ( channel->local_cid == local_cid) {
208657be49d6SMatthias Ringwald             return channel;
208757be49d6SMatthias Ringwald         }
208857be49d6SMatthias Ringwald     }
208957be49d6SMatthias Ringwald     return NULL;
209057be49d6SMatthias Ringwald }
209157be49d6SMatthias Ringwald 
209257be49d6SMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
209357be49d6SMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){
209457be49d6SMatthias Ringwald     channel->state = L2CAP_STATE_CLOSED;
209557be49d6SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
209657be49d6SMatthias Ringwald     // discard channel
209757be49d6SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
209857be49d6SMatthias Ringwald     btstack_memory_l2cap_channel_free(channel);
209957be49d6SMatthias Ringwald }
210057be49d6SMatthias Ringwald 
2101e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
2102e7d0c9aaSMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, le_psm);
21037192e786SMatthias Ringwald }
2104efedfb4cSMatthias Ringwald 
2105da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
21067192e786SMatthias Ringwald 
2107da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
21087192e786SMatthias Ringwald 
21097192e786SMatthias Ringwald     // check for alread registered psm
21107192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
21117192e786SMatthias Ringwald     if (service) {
2112da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
21137192e786SMatthias Ringwald     }
21147192e786SMatthias Ringwald 
21157192e786SMatthias Ringwald     // alloc structure
21167192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
21177192e786SMatthias Ringwald     if (!service) {
21187192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
2119da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
21207192e786SMatthias Ringwald     }
21217192e786SMatthias Ringwald 
21227192e786SMatthias Ringwald     // fill in
21237192e786SMatthias Ringwald     service->psm = psm;
2124da144af5SMatthias Ringwald     service->mtu = 0;
21257192e786SMatthias Ringwald     service->packet_handler = packet_handler;
21267192e786SMatthias Ringwald     service->required_security_level = security_level;
21277192e786SMatthias Ringwald 
21287192e786SMatthias Ringwald     // add to services list
2129665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
21307192e786SMatthias Ringwald 
21317192e786SMatthias Ringwald     // done
2132da144af5SMatthias Ringwald     return 0;
21337192e786SMatthias Ringwald }
21347192e786SMatthias Ringwald 
2135da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
21367192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
21377192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
21389367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
2139da144af5SMatthias Ringwald 
2140665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
21417192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
2142da144af5SMatthias Ringwald     return 0;
21437192e786SMatthias Ringwald }
2144da144af5SMatthias Ringwald 
2145da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
2146e7d0c9aaSMatthias Ringwald     // get channel
2147e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2148e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2149e7d0c9aaSMatthias Ringwald 
2150e7d0c9aaSMatthias Ringwald     // validate state
2151e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
2152e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2153e7d0c9aaSMatthias Ringwald     }
2154e7d0c9aaSMatthias Ringwald 
2155efedfb4cSMatthias Ringwald     // set state accept connection
215623017473SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
215723017473SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
215823017473SMatthias Ringwald     channel->local_mtu = mtu;
215985aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
216085aeef60SMatthias Ringwald     channel->automatic_credits  = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
216185aeef60SMatthias Ringwald 
216285aeef60SMatthias Ringwald     // test
216363f0ac45SMatthias Ringwald     // channel->new_credits_incoming = 1;
2164e7d0c9aaSMatthias Ringwald 
2165e7d0c9aaSMatthias Ringwald     // go
2166e7d0c9aaSMatthias Ringwald     l2cap_run();
2167da144af5SMatthias Ringwald     return 0;
2168da144af5SMatthias Ringwald }
2169da144af5SMatthias Ringwald 
2170da144af5SMatthias Ringwald /**
2171da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
2172da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2173da144af5SMatthias Ringwald  */
2174da144af5SMatthias Ringwald 
2175da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
2176e7d0c9aaSMatthias Ringwald     // get channel
2177e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2178e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2179e7d0c9aaSMatthias Ringwald 
2180e7d0c9aaSMatthias Ringwald     // validate state
2181e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
2182e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2183e7d0c9aaSMatthias Ringwald     }
2184e7d0c9aaSMatthias Ringwald 
2185efedfb4cSMatthias Ringwald     // set state decline connection
2186e7d0c9aaSMatthias Ringwald     channel->state  = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
2187e7d0c9aaSMatthias Ringwald     channel->reason = 0x04; // no resources available
2188e7d0c9aaSMatthias Ringwald     l2cap_run();
2189da144af5SMatthias Ringwald     return 0;
2190da144af5SMatthias Ringwald }
2191da144af5SMatthias Ringwald 
21927dafa750SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
2193da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
2194efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
2195efedfb4cSMatthias Ringwald 
21967dafa750SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu);
2197da144af5SMatthias Ringwald 
21987dafa750SMatthias Ringwald 
21997dafa750SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
22007dafa750SMatthias Ringwald     if (!connection) {
22017dafa750SMatthias Ringwald         log_error("no hci_connection for handle 0x%04x", con_handle);
22027dafa750SMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
22037dafa750SMatthias Ringwald     }
22047dafa750SMatthias Ringwald 
22057dafa750SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, connection->address, connection->address_type, psm, mtu, security_level);
2206da144af5SMatthias Ringwald     if (!channel) {
2207da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
2208da144af5SMatthias Ringwald     }
2209e7d0c9aaSMatthias Ringwald     log_info("l2cap_le_create_channel %p", channel);
2210da144af5SMatthias Ringwald 
2211da144af5SMatthias Ringwald     // store local_cid
2212da144af5SMatthias Ringwald     if (out_local_cid){
2213da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
2214da144af5SMatthias Ringwald     }
2215da144af5SMatthias Ringwald 
22167dafa750SMatthias Ringwald     // provide buffer
22177dafa750SMatthias Ringwald     channel->con_handle = con_handle;
2218cd529728SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
22197dafa750SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
222085aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
222185aeef60SMatthias Ringwald     channel->automatic_credits    = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
222285aeef60SMatthias Ringwald 
2223efedfb4cSMatthias Ringwald     // add to connections list
2224efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
2225efedfb4cSMatthias Ringwald 
22267dafa750SMatthias Ringwald     // go
222763f0ac45SMatthias Ringwald     l2cap_run();
2228da144af5SMatthias Ringwald     return 0;
2229da144af5SMatthias Ringwald }
2230da144af5SMatthias Ringwald 
2231da144af5SMatthias Ringwald /**
2232da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
2233da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2234da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
2235da144af5SMatthias Ringwald  */
223664e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
223763f0ac45SMatthias Ringwald 
223863f0ac45SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
223963f0ac45SMatthias Ringwald     if (!channel) {
224063f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
224163f0ac45SMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
224263f0ac45SMatthias Ringwald     }
224363f0ac45SMatthias Ringwald 
2244efedfb4cSMatthias Ringwald     // check state
224563f0ac45SMatthias Ringwald     if (channel->state != L2CAP_STATE_OPEN){
224663f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid);
224763f0ac45SMatthias Ringwald     }
224863f0ac45SMatthias Ringwald 
224963f0ac45SMatthias Ringwald     // assert incoming credits + credits <= 0xffff
225063f0ac45SMatthias Ringwald     uint32_t total_credits = channel->credits_incoming;
225163f0ac45SMatthias Ringwald     total_credits += channel->new_credits_incoming;
225263f0ac45SMatthias Ringwald     total_credits += credits;
225363f0ac45SMatthias Ringwald     if (total_credits > 0xffff){
225463f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming,
225563f0ac45SMatthias Ringwald             channel->new_credits_incoming, credits);
225663f0ac45SMatthias Ringwald     }
225763f0ac45SMatthias Ringwald 
2258efedfb4cSMatthias Ringwald     // set credits_granted
225963f0ac45SMatthias Ringwald     channel->new_credits_incoming += credits;
226063f0ac45SMatthias Ringwald 
226163f0ac45SMatthias Ringwald     // go
226263f0ac45SMatthias Ringwald     l2cap_run();
2263da144af5SMatthias Ringwald     return 0;
2264da144af5SMatthias Ringwald }
2265da144af5SMatthias Ringwald 
2266da144af5SMatthias Ringwald /**
2267da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
2268da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2269da144af5SMatthias Ringwald  */
227064e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){
227144276248SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
227244276248SMatthias Ringwald     if (!channel) {
227344276248SMatthias Ringwald         log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
2274da144af5SMatthias Ringwald         return 0;
2275da144af5SMatthias Ringwald     }
2276da144af5SMatthias Ringwald 
227744276248SMatthias Ringwald     // check state
227844276248SMatthias Ringwald     if (channel->state != L2CAP_STATE_OPEN) return 0;
227944276248SMatthias Ringwald 
228044276248SMatthias Ringwald     // check queue
228144276248SMatthias Ringwald     if (channel->send_sdu_buffer) return 0;
228244276248SMatthias Ringwald 
228344276248SMatthias Ringwald     // fine, go ahead
228444276248SMatthias Ringwald     return 1;
228544276248SMatthias Ringwald }
228644276248SMatthias Ringwald 
2287da144af5SMatthias Ringwald /**
2288da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
2289da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
2290da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
2291da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2292da144af5SMatthias Ringwald  */
229364e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
229444276248SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
229544276248SMatthias Ringwald     if (!channel) {
229644276248SMatthias Ringwald         log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid);
229744276248SMatthias Ringwald         return 0;
229844276248SMatthias Ringwald     }
229944276248SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
230044276248SMatthias Ringwald     l2cap_le_notify_channel_can_send(channel);
2301da144af5SMatthias Ringwald     return 0;
2302da144af5SMatthias Ringwald }
2303da144af5SMatthias Ringwald 
2304da144af5SMatthias Ringwald /**
2305da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
2306da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
2307da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2308da144af5SMatthias Ringwald  * @param data                  data to send
2309da144af5SMatthias Ringwald  * @param size                  data size
2310da144af5SMatthias Ringwald  */
231164e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
231264e11ca9SMatthias Ringwald 
231364e11ca9SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
231464e11ca9SMatthias Ringwald     if (!channel) {
231564e11ca9SMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2316828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
231764e11ca9SMatthias Ringwald     }
231864e11ca9SMatthias Ringwald 
231964e11ca9SMatthias Ringwald     if (len > channel->remote_mtu){
232064e11ca9SMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
232164e11ca9SMatthias Ringwald         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
232264e11ca9SMatthias Ringwald     }
232364e11ca9SMatthias Ringwald 
23247f107edaSMatthias Ringwald     if (channel->send_sdu_buffer){
232564e11ca9SMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
232664e11ca9SMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
232764e11ca9SMatthias Ringwald     }
232864e11ca9SMatthias Ringwald 
23297f107edaSMatthias Ringwald     channel->send_sdu_buffer = data;
23307f107edaSMatthias Ringwald     channel->send_sdu_len    = len;
23317f107edaSMatthias Ringwald     channel->send_sdu_pos    = 0;
233264e11ca9SMatthias Ringwald 
23337f107edaSMatthias Ringwald     l2cap_run();
23347f107edaSMatthias Ringwald     return 0;
2335da144af5SMatthias Ringwald }
2336da144af5SMatthias Ringwald 
2337da144af5SMatthias Ringwald /**
2338da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
2339da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2340da144af5SMatthias Ringwald  */
2341828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid)
2342da144af5SMatthias Ringwald {
2343828a7f7aSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2344828a7f7aSMatthias Ringwald     if (!channel) {
2345828a7f7aSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2346828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2347828a7f7aSMatthias Ringwald     }
2348828a7f7aSMatthias Ringwald 
2349828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
2350828a7f7aSMatthias Ringwald     l2cap_run();
2351da144af5SMatthias Ringwald     return 0;
2352da144af5SMatthias Ringwald }
2353da144af5SMatthias Ringwald 
23547f02f414SMatthias Ringwald #endif
2355