xref: /btstack/src/l2cap.c (revision 09e9d05bab154bbd5dd313e0a6d9a7d1650597fc)
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 
84*09e9d05bSMatthias Ringwald #if defined(ENABLE_LE_DATA_CHANNELS) || defined(ENABLE_CLASSIC)
85*09e9d05bSMatthias Ringwald #define L2CAP_USES_CHANNELS
86*09e9d05bSMatthias Ringwald #endif
87*09e9d05bSMatthias 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);
92*09e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
93*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
94*09e9d05bSMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
95*09e9d05bSMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
96*09e9d05bSMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
97*09e9d05bSMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
98*09e9d05bSMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel);
99*09e9d05bSMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
100*09e9d05bSMatthias 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 
115*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1165628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
1175628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
118*09e9d05bSMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
119*09e9d05bSMatthias 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 
168*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
169f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
170f5454fc6Smatthias.ringwald     l2cap_services = NULL;
171*09e9d05bSMatthias Ringwald     require_security_level2_for_outgoing_sdp = 0;
172*09e9d05bSMatthias 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 
19015a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
191fcadd0caSmatthias.ringwald }
192fcadd0caSmatthias.ringwald 
193ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
19433c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1951e6aba47Smatthias.ringwald }
1961e6aba47Smatthias.ringwald 
197*09e9d05bSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
198*09e9d05bSMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
199*09e9d05bSMatthias Ringwald     if (index < 0) return;
200*09e9d05bSMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
201*09e9d05bSMatthias Ringwald     l2cap_notify_channel_can_send();
202*09e9d05bSMatthias Ringwald }
203*09e9d05bSMatthias Ringwald 
204*09e9d05bSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
205*09e9d05bSMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
206*09e9d05bSMatthias Ringwald }
207*09e9d05bSMatthias Ringwald 
208*09e9d05bSMatthias Ringwald uint8_t *l2cap_get_outgoing_buffer(void){
209*09e9d05bSMatthias Ringwald     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
210*09e9d05bSMatthias Ringwald }
211*09e9d05bSMatthias Ringwald 
212*09e9d05bSMatthias Ringwald int l2cap_reserve_packet_buffer(void){
213*09e9d05bSMatthias Ringwald     return hci_reserve_packet_buffer();
214*09e9d05bSMatthias Ringwald }
215*09e9d05bSMatthias Ringwald 
216*09e9d05bSMatthias Ringwald void l2cap_release_packet_buffer(void){
217*09e9d05bSMatthias Ringwald     hci_release_packet_buffer();
218*09e9d05bSMatthias Ringwald }
219*09e9d05bSMatthias Ringwald 
220*09e9d05bSMatthias Ringwald static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint16_t remote_cid, uint16_t len){
221*09e9d05bSMatthias Ringwald     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
222*09e9d05bSMatthias Ringwald 
223*09e9d05bSMatthias Ringwald     // 0 - Connection handle : PB=pb : BC=00
224*09e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
225*09e9d05bSMatthias Ringwald     // 2 - ACL length
226*09e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
227*09e9d05bSMatthias Ringwald     // 4 - L2CAP packet length
228*09e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
229*09e9d05bSMatthias Ringwald     // 6 - L2CAP channel DEST
230*09e9d05bSMatthias Ringwald     little_endian_store_16(acl_buffer, 6,  remote_cid);
231*09e9d05bSMatthias Ringwald }
232*09e9d05bSMatthias Ringwald 
233*09e9d05bSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
234*09e9d05bSMatthias Ringwald 
235*09e9d05bSMatthias Ringwald     if (!hci_is_packet_buffer_reserved()){
236*09e9d05bSMatthias Ringwald         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
237*09e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
238*09e9d05bSMatthias Ringwald     }
239*09e9d05bSMatthias Ringwald 
240*09e9d05bSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
241*09e9d05bSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
242*09e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
243*09e9d05bSMatthias Ringwald     }
244*09e9d05bSMatthias Ringwald 
245*09e9d05bSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
246*09e9d05bSMatthias Ringwald 
247*09e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
248*09e9d05bSMatthias Ringwald     l2cap_setup_header(acl_buffer, con_handle, cid, len);
249*09e9d05bSMatthias Ringwald     // send
250*09e9d05bSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
251*09e9d05bSMatthias Ringwald }
252*09e9d05bSMatthias Ringwald 
253*09e9d05bSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
254*09e9d05bSMatthias Ringwald 
255*09e9d05bSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
256*09e9d05bSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
257*09e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
258*09e9d05bSMatthias Ringwald     }
259*09e9d05bSMatthias Ringwald 
260*09e9d05bSMatthias Ringwald     hci_reserve_packet_buffer();
261*09e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
262*09e9d05bSMatthias Ringwald 
263*09e9d05bSMatthias Ringwald     memcpy(&acl_buffer[8], data, len);
264*09e9d05bSMatthias Ringwald 
265*09e9d05bSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
266*09e9d05bSMatthias Ringwald }
267*09e9d05bSMatthias Ringwald 
268*09e9d05bSMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
269*09e9d05bSMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
270*09e9d05bSMatthias Ringwald     uint8_t event[4];
271*09e9d05bSMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
272*09e9d05bSMatthias Ringwald     event[1] = sizeof(event) - 2;
273*09e9d05bSMatthias Ringwald     little_endian_store_16(event, 2, channel);
274*09e9d05bSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
275*09e9d05bSMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
276*09e9d05bSMatthias Ringwald }
277*09e9d05bSMatthias Ringwald 
278*09e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS
27917a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
28058de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
28158de5610Smatthias.ringwald }
28258de5610Smatthias.ringwald 
28344276248SMatthias Ringwald static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){
28444276248SMatthias Ringwald     uint8_t event[4];
28544276248SMatthias Ringwald     event[0] = event_code;
28644276248SMatthias Ringwald     event[1] = sizeof(event) - 2;
28744276248SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
28844276248SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
28944276248SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
29044276248SMatthias Ringwald }
291*09e9d05bSMatthias Ringwald #endif
29244276248SMatthias Ringwald 
293*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
29458de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
295c9dc710bS[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",
296fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
297c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
298c9dc710bS[email protected]     uint8_t event[23];
29958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
30058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
30158de5610Smatthias.ringwald     event[2] = status;
302724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
303fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
304f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
305f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
306f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
307f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
308f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
309f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
31058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
31117a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
31258de5610Smatthias.ringwald }
31358de5610Smatthias.ringwald 
31444276248SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
315e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
31644276248SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
31758de5610Smatthias.ringwald }
31858de5610Smatthias.ringwald 
31944276248SMatthias Ringwald static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) {
320e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
321fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
32258de5610Smatthias.ringwald     uint8_t event[16];
32358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
32458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
325724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
326fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
327f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
328f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
329f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
33058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
33117a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
3320af41d30Smatthias.ringwald }
333808a48abSmatthias.ringwald 
3347f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
335665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
336665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
337665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
338665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
339b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
340f62db1e3Smatthias.ringwald             return channel;
341f62db1e3Smatthias.ringwald         }
342f62db1e3Smatthias.ringwald     }
343f62db1e3Smatthias.ringwald     return NULL;
344f62db1e3Smatthias.ringwald }
345f62db1e3Smatthias.ringwald 
3460b9d7e78SMatthias Ringwald ///
3470b9d7e78SMatthias Ringwald 
34830725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
34930725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
35030725612SMatthias Ringwald     if (!channel) return;
35130725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
35230725612SMatthias Ringwald     l2cap_notify_channel_can_send();
35330725612SMatthias Ringwald }
35430725612SMatthias Ringwald 
3556b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
3566b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
3576b1fde37Smatthias.ringwald     if (!channel) return 0;
3580b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
3597856fb31S[email protected] }
3607856fb31S[email protected] 
36183e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
36283e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
36383e7cdd9SMatthias Ringwald     if (!channel) return 0;
3640b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
36583e7cdd9SMatthias Ringwald }
36696cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
36796cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
36896cbd662Smatthias.ringwald     if (channel) {
36996cbd662Smatthias.ringwald         return channel->remote_mtu;
37096cbd662Smatthias.ringwald     }
37196cbd662Smatthias.ringwald     return 0;
37296cbd662Smatthias.ringwald }
37396cbd662Smatthias.ringwald 
374ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
375665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
376665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
377665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
378665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3795932bd7cS[email protected]         if ( &channel->rtx == ts) {
3805932bd7cS[email protected]             return channel;
3815932bd7cS[email protected]         }
3825932bd7cS[email protected]     }
3835932bd7cS[email protected]     return NULL;
3845932bd7cS[email protected] }
3855932bd7cS[email protected] 
386ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3875932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
38895d2c8f4SMatthias Ringwald     if (!channel) return;
3895932bd7cS[email protected] 
3905932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3915932bd7cS[email protected] 
3925932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3935932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3945932bd7cS[email protected]     // notify client
3955932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3965932bd7cS[email protected] 
3975932bd7cS[email protected]     // discard channel
3989dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
399665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
4005932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
4015932bd7cS[email protected] }
4025932bd7cS[email protected] 
4035932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
4045932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
405528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
4065932bd7cS[email protected] }
4075932bd7cS[email protected] 
4085932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
4095932bd7cS[email protected]     l2cap_stop_rtx(channel);
410cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
411528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
412528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
413528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
4145932bd7cS[email protected] }
4155932bd7cS[email protected] 
4165932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
4175932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
4185932bd7cS[email protected]     l2cap_stop_rtx(channel);
419528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
420528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
421528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
4225932bd7cS[email protected] }
4235932bd7cS[email protected] 
42471de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
425ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
426ac301f95S[email protected] }
427ac301f95S[email protected] 
428df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
429ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
430df3354fcS[email protected] }
4315932bd7cS[email protected] 
4327f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
433a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4349da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
435b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
436b1d43497Smatthias.ringwald     }
437b1d43497Smatthias.ringwald 
4389da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
4392a373862S[email protected]     hci_reserve_packet_buffer();
440facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
44158de5610Smatthias.ringwald     va_list argptr;
44258de5610Smatthias.ringwald     va_start(argptr, identifier);
44370efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
44458de5610Smatthias.ringwald     va_end(argptr);
4459da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
446826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
44758de5610Smatthias.ringwald }
44858de5610Smatthias.ringwald 
449b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
450b1d43497Smatthias.ringwald 
451c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
452c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
453c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
454c8b9416aS[email protected]     }
455c8b9416aS[email protected] 
45658de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
457b1d43497Smatthias.ringwald     if (!channel) {
4589da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
459b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4606218e6f1Smatthias.ringwald     }
4616218e6f1Smatthias.ringwald 
462fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4639da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
464a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
465a35252c8S[email protected]     }
466a35252c8S[email protected] 
467fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
468b1d43497Smatthias.ringwald 
469facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4707f107edaSMatthias Ringwald     l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, len);
47158de5610Smatthias.ringwald     // send
4727f107edaSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
47358de5610Smatthias.ringwald }
47458de5610Smatthias.ringwald 
475ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
476b1d43497Smatthias.ringwald 
477a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
478a35252c8S[email protected]     if (!channel) {
479ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
480a35252c8S[email protected]         return -1;   // TODO: define error
481a35252c8S[email protected]     }
482a35252c8S[email protected] 
483f0efaa57S[email protected]     if (len > channel->remote_mtu){
484ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
485f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
486f0efaa57S[email protected]     }
487f0efaa57S[email protected] 
488fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
489ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
490b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
491b1d43497Smatthias.ringwald     }
492b1d43497Smatthias.ringwald 
4932a373862S[email protected]     hci_reserve_packet_buffer();
494facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
495b1d43497Smatthias.ringwald 
496b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
497b1d43497Smatthias.ringwald 
498b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
499b1d43497Smatthias.ringwald }
500b1d43497Smatthias.ringwald 
501fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
502fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5030e37e417S[email protected] }
5040e37e417S[email protected] 
50528ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
50628ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
50728ca2b46S[email protected] }
50828ca2b46S[email protected] 
50928ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
51028ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
51128ca2b46S[email protected] }
512*09e9d05bSMatthias Ringwald #endif
51328ca2b46S[email protected] 
51428ca2b46S[email protected] 
515*09e9d05bSMatthias Ringwald #ifdef ENABLE_BLE
516*09e9d05bSMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
517*09e9d05bSMatthias Ringwald 
518*09e9d05bSMatthias Ringwald     if (!hci_can_send_acl_packet_now(handle)){
519*09e9d05bSMatthias Ringwald         log_info("l2cap_send_le_signaling_packet, cannot send");
520*09e9d05bSMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
521*09e9d05bSMatthias Ringwald     }
522*09e9d05bSMatthias Ringwald 
523*09e9d05bSMatthias Ringwald     // log_info("l2cap_send_le_signaling_packet type %u", cmd);
524*09e9d05bSMatthias Ringwald     hci_reserve_packet_buffer();
525*09e9d05bSMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
526*09e9d05bSMatthias Ringwald     va_list argptr;
527*09e9d05bSMatthias Ringwald     va_start(argptr, identifier);
528*09e9d05bSMatthias Ringwald     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
529*09e9d05bSMatthias Ringwald     va_end(argptr);
530*09e9d05bSMatthias Ringwald     // log_info("l2cap_send_le_signaling_packet con %u!", handle);
531*09e9d05bSMatthias Ringwald     return hci_send_acl_packet_buffer(len);
532*09e9d05bSMatthias Ringwald }
533*09e9d05bSMatthias Ringwald #endif
534*09e9d05bSMatthias Ringwald 
535*09e9d05bSMatthias Ringwald uint16_t l2cap_max_mtu(void){
536*09e9d05bSMatthias Ringwald     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
537*09e9d05bSMatthias Ringwald }
538*09e9d05bSMatthias Ringwald 
539*09e9d05bSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
540*09e9d05bSMatthias Ringwald     return l2cap_max_mtu();
541*09e9d05bSMatthias Ringwald }
542b1d43497Smatthias.ringwald 
5438158c421Smatthias.ringwald // MARK: L2CAP_RUN
5442cd0be45Smatthias.ringwald // process outstanding signaling tasks
5457f02f414SMatthias Ringwald static void l2cap_run(void){
5462b83fb7dSmatthias.ringwald 
54722c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
54822c29ab4SMatthias Ringwald 
5492b83fb7dSmatthias.ringwald     // check pending signaling responses
5502b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5512b83fb7dSmatthias.ringwald 
5522b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
553a35252c8S[email protected] 
554a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
555a35252c8S[email protected] 
5562b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5572b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
55863a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
559f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5602b83fb7dSmatthias.ringwald 
561*09e9d05bSMatthias Ringwald         // avoid warnings
562*09e9d05bSMatthias Ringwald         (void) infoType;
563*09e9d05bSMatthias Ringwald 
564f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
565f53da564S[email protected]         signaling_responses_pending--;
566f53da564S[email protected]         int i;
567f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
568f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
569f53da564S[email protected]         }
570f53da564S[email protected] 
571f53da564S[email protected]         switch (response_code){
572*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
5732b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5742b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5752bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5764d816277S[email protected]                 if (result == 0x0003){
5772bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5784d816277S[email protected]                 }
5792b360848Smatthias.ringwald                 break;
5802b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5812b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5822b83fb7dSmatthias.ringwald                 break;
5832b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5843b0484b3S[email protected]                 switch (infoType){
5853b0484b3S[email protected]                     case 1: { // Connectionless MTU
5863b0484b3S[email protected]                             uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5873b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5883b0484b3S[email protected]                         }
589202c8a4cSMatthias Ringwald                         break;
5903b0484b3S[email protected]                     case 2: { // Extended Features Supported
591462e630dS[email protected]                             // extended features request supported, features: fixed channels, unicast connectionless data reception
592462e630dS[email protected]                             uint32_t features = 0x280;
5933b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5943b0484b3S[email protected]                         }
595202c8a4cSMatthias Ringwald                         break;
5963b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5973b0484b3S[email protected]                             uint8_t map[8];
5983b0484b3S[email protected]                             memset(map, 0, 8);
599288636a2SMatthias Ringwald                             map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
6003b0484b3S[email protected]                             l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
6013b0484b3S[email protected]                         }
602202c8a4cSMatthias Ringwald                         break;
6033b0484b3S[email protected]                     default:
6042b83fb7dSmatthias.ringwald                         // all other types are not supported
6052b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
6063b0484b3S[email protected]                         break;
6072b83fb7dSmatthias.ringwald                 }
6082b83fb7dSmatthias.ringwald                 break;
60963a7246aSmatthias.ringwald             case COMMAND_REJECT:
6105ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
61163f0ac45SMatthias Ringwald                 break;
612*09e9d05bSMatthias Ringwald #endif
613a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
61463f0ac45SMatthias Ringwald             case LE_CREDIT_BASED_CONNECTION_REQUEST:
61563f0ac45SMatthias Ringwald                 l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result);
61663f0ac45SMatthias Ringwald                 break;
61770efece1S[email protected]             case COMMAND_REJECT_LE:
61870efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
61963a7246aSmatthias.ringwald                 break;
62070efece1S[email protected] #endif
6212b83fb7dSmatthias.ringwald             default:
6222b83fb7dSmatthias.ringwald                 // should not happen
6232b83fb7dSmatthias.ringwald                 break;
6242b83fb7dSmatthias.ringwald         }
6252b83fb7dSmatthias.ringwald     }
6262b83fb7dSmatthias.ringwald 
627665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
628*09e9d05bSMatthias Ringwald     (void) it;
629*09e9d05bSMatthias Ringwald 
630*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
631*09e9d05bSMatthias Ringwald     uint8_t  config_options[4];
632665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
633665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
634baf94f06S[email protected] 
635665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
63622c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6372cd0be45Smatthias.ringwald         switch (channel->state){
6382cd0be45Smatthias.ringwald 
639df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
640ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
641fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
642a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
643ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
644fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
645ad671560S[email protected]                 }
646ad671560S[email protected]                 break;
647ad671560S[email protected] 
64802b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
649baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
65064472d52Smatthias.ringwald                 // send connection request - set state first
65164472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
65202b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6538f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
65402b22dc4Smatthias.ringwald                 break;
65502b22dc4Smatthias.ringwald 
656e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
657fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
65822c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
659fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
660e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6619dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
662665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
663d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
664e7ff783cSmatthias.ringwald                 break;
665e7ff783cSmatthias.ringwald 
666552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
667fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
668fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
66928ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
670fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
671552d92a1Smatthias.ringwald                 break;
672552d92a1Smatthias.ringwald 
6736fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
674fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6756fdcc387Smatthias.ringwald                 // success, start l2cap handshake
676b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6776fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
678fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6795932bd7cS[email protected]                 l2cap_start_rtx(channel);
6806fdcc387Smatthias.ringwald                 break;
6816fdcc387Smatthias.ringwald 
682fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
683fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
68473cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
68563a7246aSmatthias.ringwald                     uint16_t flags = 0;
68628ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
68763a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
68863a7246aSmatthias.ringwald                         flags = 1;
68963a7246aSmatthias.ringwald                     } else {
69028ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
69163a7246aSmatthias.ringwald                     }
69263a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
693fc64f94aSMatthias 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);
69463a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
69563a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
69663a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
697f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
698fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
69963a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
70063a7246aSmatthias.ringwald                     } else {
701fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
70263a7246aSmatthias.ringwald                     }
70363a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
704fa8473a4Smatthias.ringwald                 }
70573cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
70628ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
70728ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
708b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
709ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
710ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
711f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
712fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
7135932bd7cS[email protected]                     l2cap_start_rtx(channel);
714fa8473a4Smatthias.ringwald                 }
715fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
716552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
717552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
718fa8473a4Smatthias.ringwald                 }
719552d92a1Smatthias.ringwald                 break;
720552d92a1Smatthias.ringwald 
721e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
722fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
72322c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
724fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7255932bd7cS[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 :)
726756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
727e7ff783cSmatthias.ringwald                 break;
728e7ff783cSmatthias.ringwald 
729e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
730fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
731b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7322cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
733fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7342cd0be45Smatthias.ringwald                 break;
7352cd0be45Smatthias.ringwald             default:
7362cd0be45Smatthias.ringwald                 break;
7372cd0be45Smatthias.ringwald         }
7382cd0be45Smatthias.ringwald     }
739*09e9d05bSMatthias Ringwald #endif
740da886c03S[email protected] 
741cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
742efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
743efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7447f107edaSMatthias Ringwald         uint8_t  * acl_buffer;
7457f107edaSMatthias Ringwald         uint8_t  * l2cap_payload;
7467f107edaSMatthias Ringwald         uint16_t pos;
7477f107edaSMatthias Ringwald         uint16_t payload_size;
748efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
749efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
750efedfb4cSMatthias Ringwald         switch (channel->state){
7515cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7525cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7535cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
7545cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
7551b8b8d05SMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
75663f0ac45SMatthias Ringwald                 channel->credits_incoming =  channel->new_credits_incoming;
75763f0ac45SMatthias Ringwald                 channel->new_credits_incoming = 0;
75863f0ac45SMatthias 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);
7595cb87675SMatthias Ringwald                 break;
76023017473SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
76123017473SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
76223017473SMatthias Ringwald                 // TODO: support larger MPS
76323017473SMatthias Ringwald                 channel->state = L2CAP_STATE_OPEN;
76463f0ac45SMatthias Ringwald                 channel->credits_incoming =  channel->new_credits_incoming;
76563f0ac45SMatthias Ringwald                 channel->new_credits_incoming = 0;
76663f0ac45SMatthias 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);
76764e11ca9SMatthias Ringwald                 // notify client
76844276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, 0);
76923017473SMatthias Ringwald                 break;
770e7d0c9aaSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
771e7d0c9aaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
772e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
77363f0ac45SMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason);
774e7d0c9aaSMatthias Ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
775e7d0c9aaSMatthias Ringwald                 l2cap_stop_rtx(channel);
776e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
777e7d0c9aaSMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
778e7d0c9aaSMatthias Ringwald                 break;
7797f107edaSMatthias Ringwald             case L2CAP_STATE_OPEN:
78085aeef60SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
78185aeef60SMatthias Ringwald 
78285aeef60SMatthias Ringwald                 // send credits
78385aeef60SMatthias Ringwald                 if (channel->new_credits_incoming){
78485aeef60SMatthias Ringwald                     log_info("l2cap: sending %u credits", channel->new_credits_incoming);
78585aeef60SMatthias Ringwald                     channel->local_sig_id = l2cap_next_sig_id();
78685aeef60SMatthias Ringwald                     uint16_t new_credits = channel->new_credits_incoming;
78785aeef60SMatthias Ringwald                     channel->new_credits_incoming = 0;
78885aeef60SMatthias Ringwald                     channel->credits_incoming += new_credits;
78985aeef60SMatthias Ringwald                     l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits);
79085aeef60SMatthias Ringwald                     break;
79185aeef60SMatthias Ringwald                 }
79285aeef60SMatthias Ringwald 
79385aeef60SMatthias Ringwald                 // send data
7947f107edaSMatthias Ringwald                 if (!channel->send_sdu_buffer) break;
7957f107edaSMatthias Ringwald                 if (!channel->credits_outgoing) break;
79685aeef60SMatthias Ringwald 
7977f107edaSMatthias Ringwald                 // send part of SDU
7987f107edaSMatthias Ringwald                 hci_reserve_packet_buffer();
7997f107edaSMatthias Ringwald                 acl_buffer = hci_get_outgoing_packet_buffer();
8007f107edaSMatthias Ringwald                 l2cap_payload = acl_buffer + 8;
8017f107edaSMatthias Ringwald                 pos = 0;
8027f107edaSMatthias Ringwald                 if (!channel->send_sdu_pos){
8037f107edaSMatthias Ringwald                     // store SDU len
8047f107edaSMatthias Ringwald                     channel->send_sdu_pos += 2;
8057f107edaSMatthias Ringwald                     little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len);
8067f107edaSMatthias Ringwald                     pos += 2;
8077f107edaSMatthias Ringwald                 }
8087f107edaSMatthias Ringwald                 payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos);
80985aeef60SMatthias Ringwald                 log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing);
8107f107edaSMatthias Ringwald                 memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len
8117f107edaSMatthias Ringwald                 pos += payload_size;
8127f107edaSMatthias Ringwald                 channel->send_sdu_pos += payload_size;
81363f0ac45SMatthias Ringwald                 l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, pos);
8147f107edaSMatthias Ringwald                 // done
8157f107edaSMatthias Ringwald 
81685aeef60SMatthias Ringwald                 channel->credits_outgoing--;
8177f107edaSMatthias Ringwald 
8187f107edaSMatthias Ringwald                 if (channel->send_sdu_pos >= channel->send_sdu_len + 2){
8197f107edaSMatthias Ringwald                     channel->send_sdu_buffer = NULL;
82044276248SMatthias Ringwald                     // send done event
82144276248SMatthias Ringwald                     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_PACKET_SENT);
82244276248SMatthias Ringwald                     // inform about can send now
82344276248SMatthias Ringwald                     l2cap_le_notify_channel_can_send(channel);
8247f107edaSMatthias Ringwald                 }
8257f107edaSMatthias Ringwald                 hci_send_acl_packet_buffer(8 + pos);
8267f107edaSMatthias Ringwald                 break;
827828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
828828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
829828a7f7aSMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
830828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
831828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
832828a7f7aSMatthias Ringwald                 break;
833828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
834828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
835828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
836828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
837828a7f7aSMatthias Ringwald                 l2cap_le_finialize_channel_close(channel);  // -- remove from list
838828a7f7aSMatthias Ringwald                 break;
839efedfb4cSMatthias Ringwald             default:
840efedfb4cSMatthias Ringwald                 break;
841efedfb4cSMatthias Ringwald         }
842efedfb4cSMatthias Ringwald     }
843*09e9d05bSMatthias Ringwald #endif
844efedfb4cSMatthias Ringwald 
845*09e9d05bSMatthias Ringwald #ifdef ENABLE_BLE
846da886c03S[email protected]     // send l2cap con paramter update if necessary
847da886c03S[email protected]     hci_connections_get_iterator(&it);
848665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
849665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
8505d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
851b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
852da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
853b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
854b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
855b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
856b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
857b68d7bc3SMatthias Ringwald                 break;
858da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
859b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
860b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
861da886c03S[email protected]                 break;
862da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
863b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
864b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
865da886c03S[email protected]                 break;
866da886c03S[email protected]             default:
867da886c03S[email protected]                 break;
868da886c03S[email protected]         }
869da886c03S[email protected]     }
8704d7157c3S[email protected] #endif
871da886c03S[email protected] 
87222c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
8732cd0be45Smatthias.ringwald }
8742cd0be45Smatthias.ringwald 
875*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
876fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
8772df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
8785533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
8792df5dadcS[email protected]         // success, start l2cap handshake
880fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
8812df5dadcS[email protected]         // check remote SSP feature first
8822df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
8832df5dadcS[email protected]     }
8842df5dadcS[email protected] }
8852df5dadcS[email protected] 
8862df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
8872df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
8882df5dadcS[email protected] 
8892df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
890ac301f95S[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));
891fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
8922df5dadcS[email protected]         // request security level 2
8932df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
894fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
8952df5dadcS[email protected]         return;
8962df5dadcS[email protected]     }
8972df5dadcS[email protected]     // fine, go ahead
8982df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
8992df5dadcS[email protected] }
900*09e9d05bSMatthias Ringwald #endif
9012df5dadcS[email protected] 
902*09e9d05bSMatthias Ringwald #ifdef L2CAP_USES_CHANNELS
903da144af5SMatthias 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,
904da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
905da144af5SMatthias Ringwald 
906da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
907da144af5SMatthias Ringwald     if (!channel) {
908da144af5SMatthias Ringwald         return NULL;
909da144af5SMatthias Ringwald     }
910da144af5SMatthias Ringwald 
911da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
912da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
913da144af5SMatthias Ringwald 
914da144af5SMatthias Ringwald     // fill in
915da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
916da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
917da144af5SMatthias Ringwald     channel->address_type = address_type;
918da144af5SMatthias Ringwald     channel->psm = psm;
919da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
920da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
921da144af5SMatthias Ringwald     channel->required_security_level = security_level;
922da144af5SMatthias Ringwald 
923da144af5SMatthias Ringwald     //
924da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
925da144af5SMatthias Ringwald     channel->con_handle = 0;
926da144af5SMatthias Ringwald 
927da144af5SMatthias Ringwald     // set initial state
928da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
929da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
930da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
931da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
932da144af5SMatthias Ringwald     return channel;
933da144af5SMatthias Ringwald }
934*09e9d05bSMatthias Ringwald #endif
935*09e9d05bSMatthias Ringwald 
936*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
937da144af5SMatthias Ringwald 
9389077cb15SMatthias Ringwald /**
9399077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
9409077cb15SMatthias Ringwald  * @param packet_handler
9419077cb15SMatthias Ringwald  * @param address
9429077cb15SMatthias Ringwald  * @param psm
9439077cb15SMatthias Ringwald  * @param mtu
9449077cb15SMatthias Ringwald  * @param local_cid
9459077cb15SMatthias Ringwald  */
9469077cb15SMatthias Ringwald 
947da144af5SMatthias 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){
948da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
949da144af5SMatthias Ringwald 
950da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
951da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
952da144af5SMatthias Ringwald     }
953da144af5SMatthias Ringwald 
954da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
955fc64f94aSMatthias Ringwald     if (!channel) {
9569077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
9579077cb15SMatthias Ringwald     }
9589077cb15SMatthias Ringwald 
9599077cb15SMatthias Ringwald     // add to connections list
960fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
9619077cb15SMatthias Ringwald 
9629077cb15SMatthias Ringwald     // store local_cid
9639077cb15SMatthias Ringwald     if (out_local_cid){
964fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
9659077cb15SMatthias Ringwald     }
9669077cb15SMatthias Ringwald 
9679077cb15SMatthias Ringwald     // check if hci connection is already usable
9689077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
9699077cb15SMatthias Ringwald     if (conn){
970add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
971fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
9729077cb15SMatthias Ringwald         // check if remote supported fearures are already received
9739077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
974fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
9759077cb15SMatthias Ringwald         }
9769077cb15SMatthias Ringwald     }
9779077cb15SMatthias Ringwald 
9789077cb15SMatthias Ringwald     l2cap_run();
9799077cb15SMatthias Ringwald 
9809077cb15SMatthias Ringwald     return 0;
9819077cb15SMatthias Ringwald }
9829077cb15SMatthias Ringwald 
983ce8f182eSMatthias Ringwald void
984ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
985e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
986b35f641cSmatthias.ringwald     // find channel for local_cid
987b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
988f62db1e3Smatthias.ringwald     if (channel) {
989e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
990f62db1e3Smatthias.ringwald     }
9912cd0be45Smatthias.ringwald     // process
9922cd0be45Smatthias.ringwald     l2cap_run();
99343625864Smatthias.ringwald }
9941e6aba47Smatthias.ringwald 
995afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
996665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
997665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
998665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
999665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1000058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
1001c22aecc9S[email protected]         // channel for this address found
1002c22aecc9S[email protected]         switch (channel->state){
1003c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
1004c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
1005afde0c52Smatthias.ringwald                 // failure, forward error code
1006afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
1007afde0c52Smatthias.ringwald                 // discard channel
10089dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1009665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1010d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
1011c22aecc9S[email protected]                 break;
1012c22aecc9S[email protected]             default:
1013c22aecc9S[email protected]                 break;
1014afde0c52Smatthias.ringwald         }
1015afde0c52Smatthias.ringwald     }
1016afde0c52Smatthias.ringwald }
1017afde0c52Smatthias.ringwald 
1018afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
1019665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1020665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1021665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1022665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1023058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
10242df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
1025afde0c52Smatthias.ringwald         }
1026afde0c52Smatthias.ringwald     }
10276fdcc387Smatthias.ringwald     // process
10286fdcc387Smatthias.ringwald     l2cap_run();
1029afde0c52Smatthias.ringwald }
1030*09e9d05bSMatthias Ringwald #endif
1031b448a0e7Smatthias.ringwald 
103233c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
1033*09e9d05bSMatthias Ringwald 
1034*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
103533c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
103633c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
103733c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
103833c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
103933c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
1040fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
104133c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
104234e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
104333c40538SMatthias Ringwald     }
1044*09e9d05bSMatthias Ringwald #endif
104544276248SMatthias Ringwald 
10462125de09SMatthias Ringwald     int i;
10472125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
1048773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
10492125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
105034e7e577SMatthias Ringwald         int can_send;
105134e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
105234e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
105334e7e577SMatthias Ringwald         } else {
105434e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
105534e7e577SMatthias Ringwald         }
105634e7e577SMatthias Ringwald         if (!can_send) continue;
105734e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
105834e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
10592125de09SMatthias Ringwald     }
106033c40538SMatthias Ringwald }
106133c40538SMatthias Ringwald 
1062d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
1063afde0c52Smatthias.ringwald 
1064afde0c52Smatthias.ringwald     bd_addr_t address;
1065afde0c52Smatthias.ringwald     hci_con_handle_t handle;
10662d00edd4Smatthias.ringwald     int hci_con_used;
1067*09e9d05bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1068*09e9d05bSMatthias Ringwald 
1069*09e9d05bSMatthias Ringwald     // avoid unused warnings
1070*09e9d05bSMatthias Ringwald     (void) address;
1071*09e9d05bSMatthias Ringwald     (void) hci_con_used;
1072*09e9d05bSMatthias Ringwald     (void) it;
1073afde0c52Smatthias.ringwald 
10740e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1075afde0c52Smatthias.ringwald 
1076*09e9d05bSMatthias Ringwald         // Notify channel packet handler if they can send now
1077*09e9d05bSMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
1078*09e9d05bSMatthias Ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
1079*09e9d05bSMatthias Ringwald             l2cap_run();    // try sending signaling packets first
1080*09e9d05bSMatthias Ringwald             l2cap_notify_channel_can_send();
1081*09e9d05bSMatthias Ringwald             break;
1082*09e9d05bSMatthias Ringwald 
1083*09e9d05bSMatthias Ringwald         case HCI_EVENT_COMMAND_STATUS:
1084*09e9d05bSMatthias Ringwald             l2cap_run();    // try sending signaling packets first
1085*09e9d05bSMatthias Ringwald             break;
1086*09e9d05bSMatthias Ringwald 
1087*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1088afde0c52Smatthias.ringwald         // handle connection complete events
1089afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1090724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
1091afde0c52Smatthias.ringwald             if (packet[2] == 0){
1092f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
1093afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
1094afde0c52Smatthias.ringwald             } else {
1095afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
1096afde0c52Smatthias.ringwald             }
1097afde0c52Smatthias.ringwald             break;
1098afde0c52Smatthias.ringwald 
1099afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1100afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1101073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1102afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1103724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1104afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1105afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
110603cfbabcSmatthias.ringwald                 }
11071e6aba47Smatthias.ringwald             }
110839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
110939d59809Smatthias.ringwald             break;
1110*09e9d05bSMatthias Ringwald #endif
111127a923d0Smatthias.ringwald 
11121e6aba47Smatthias.ringwald         // handle disconnection complete events
1113afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1114c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
1115f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1116*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1117665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1118665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1119665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1120fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
112115ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
11229dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1123665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1124d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
112527a923d0Smatthias.ringwald             }
1126*09e9d05bSMatthias Ringwald #endif
1127a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1128991fea48SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1129991fea48SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1130991fea48SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1131991fea48SMatthias Ringwald                 if (channel->con_handle != handle) continue;
1132991fea48SMatthias Ringwald                 l2cap_emit_channel_closed(channel);
1133991fea48SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1134991fea48SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
1135991fea48SMatthias Ringwald             }
1136991fea48SMatthias Ringwald #endif
1137afde0c52Smatthias.ringwald             break;
1138fcadd0caSmatthias.ringwald 
1139ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
1140*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1141afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1142f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1143bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
114480ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
11452d00edd4Smatthias.ringwald             hci_con_used = 0;
1146665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1147665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1148665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1149fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11502d00edd4Smatthias.ringwald                 hci_con_used = 1;
1151c22aecc9S[email protected]                 break;
1152ee091cf1Smatthias.ringwald             }
11532d00edd4Smatthias.ringwald             if (hci_con_used) break;
1154d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
11559edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1156afde0c52Smatthias.ringwald             break;
1157ee091cf1Smatthias.ringwald 
1158df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1159f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1160665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1161665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1162665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1163fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11642df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1165df3354fcS[email protected]                 break;
1166df3354fcS[email protected]             }
1167c22aecc9S[email protected]             break;
1168df3354fcS[email protected] 
11695611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1170f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1171bd63148eS[email protected]             log_info("l2cap - security level update");
1172665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1173665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1174665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1175fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11765533f01eS[email protected] 
1177bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1178bd63148eS[email protected] 
1179e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
11805533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
11815533f01eS[email protected] 
1182df3354fcS[email protected]                 switch (channel->state){
1183df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
11845533f01eS[email protected]                         if (actual_level >= required_level){
1185f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
118644276248SMatthias Ringwald                             l2cap_emit_incoming_connection(channel);
11871eb2563eS[email protected]                         } else {
1188775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
11891eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11901eb2563eS[email protected]                         }
1191df3354fcS[email protected]                         break;
1192df3354fcS[email protected] 
1193df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
11945533f01eS[email protected]                         if (actual_level >= required_level){
1195df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1196df3354fcS[email protected]                         } else {
1197df3354fcS[email protected]                             // disconnnect, authentication not good enough
1198df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1199df3354fcS[email protected]                         }
1200df3354fcS[email protected]                         break;
1201df3354fcS[email protected] 
1202df3354fcS[email protected]                     default:
1203df3354fcS[email protected]                         break;
1204df3354fcS[email protected]                 }
1205f85a9399S[email protected]             }
1206f85a9399S[email protected]             break;
1207*09e9d05bSMatthias Ringwald #endif
1208f85a9399S[email protected] 
1209afde0c52Smatthias.ringwald         default:
1210afde0c52Smatthias.ringwald             break;
1211afde0c52Smatthias.ringwald     }
1212afde0c52Smatthias.ringwald 
1213bd63148eS[email protected]     l2cap_run();
12141e6aba47Smatthias.ringwald }
12151e6aba47Smatthias.ringwald 
12162b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
12174cf56b4aSmatthias.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."
12182b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
12192b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
12202b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
12212b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
12222b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
12232b360848Smatthias.ringwald         signaling_responses_pending++;
12242b360848Smatthias.ringwald         l2cap_run();
12252b360848Smatthias.ringwald     }
12262b360848Smatthias.ringwald }
12272b360848Smatthias.ringwald 
1228*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1229*09e9d05bSMatthias Ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1230*09e9d05bSMatthias Ringwald     channel->remote_sig_id = identifier;
1231*09e9d05bSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1232*09e9d05bSMatthias Ringwald     l2cap_run();
1233*09e9d05bSMatthias Ringwald }
1234*09e9d05bSMatthias Ringwald 
1235b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1236645658c9Smatthias.ringwald 
12379da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1238645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1239645658c9Smatthias.ringwald     if (!service) {
1240645658c9Smatthias.ringwald         // 0x0002 PSM not supported
12412b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1242645658c9Smatthias.ringwald         return;
1243645658c9Smatthias.ringwald     }
1244645658c9Smatthias.ringwald 
12455061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1246645658c9Smatthias.ringwald     if (!hci_connection) {
12472b360848Smatthias.ringwald         //
12489da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1249645658c9Smatthias.ringwald         return;
1250645658c9Smatthias.ringwald     }
12512bd8b7e7S[email protected] 
1252645658c9Smatthias.ringwald     // alloc structure
12539da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1254da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1255da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
12562b360848Smatthias.ringwald     if (!channel){
12572b360848Smatthias.ringwald         // 0x0004 No resources available
12582b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
12592b360848Smatthias.ringwald         return;
12602b360848Smatthias.ringwald     }
1261da144af5SMatthias Ringwald 
1262fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1263b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1264b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1265645658c9Smatthias.ringwald 
1266f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
12672985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
12682985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
12699775e25bSmatthias.ringwald     }
12709775e25bSmatthias.ringwald 
1271645658c9Smatthias.ringwald     // set initial state
1272df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1273ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1274e405ae81Smatthias.ringwald 
1275645658c9Smatthias.ringwald     // add to connections list
1276665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1277645658c9Smatthias.ringwald 
1278f85a9399S[email protected]     // assert security requirements
12791eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1280e405ae81Smatthias.ringwald }
1281645658c9Smatthias.ringwald 
1282ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1283e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1284b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1285e405ae81Smatthias.ringwald     if (!channel) {
1286ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1287e405ae81Smatthias.ringwald         return;
1288e405ae81Smatthias.ringwald     }
1289e405ae81Smatthias.ringwald 
1290552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1291e405ae81Smatthias.ringwald 
1292552d92a1Smatthias.ringwald     // process
1293552d92a1Smatthias.ringwald     l2cap_run();
1294e405ae81Smatthias.ringwald }
1295645658c9Smatthias.ringwald 
12967ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
12977ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1298b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1299e405ae81Smatthias.ringwald     if (!channel) {
1300ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1301e405ae81Smatthias.ringwald         return;
1302e405ae81Smatthias.ringwald     }
1303e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
13047ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1305e7ff783cSmatthias.ringwald     l2cap_run();
1306645658c9Smatthias.ringwald }
1307645658c9Smatthias.ringwald 
13087f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1309b1988dceSmatthias.ringwald 
1310b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1311b1988dceSmatthias.ringwald 
1312f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
131363a7246aSmatthias.ringwald     if (flags & 1) {
131463a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
131563a7246aSmatthias.ringwald     }
131663a7246aSmatthias.ringwald 
13172784b77dSmatthias.ringwald     // accept the other's configuration options
1318f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
13193de7c0caSmatthias.ringwald     uint16_t pos     = 8;
13203de7c0caSmatthias.ringwald     while (pos < end_pos){
132163a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
132263a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
132363a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
132463a7246aSmatthias.ringwald         pos++;
13251dc511deSmatthias.ringwald         uint8_t length = command[pos++];
13261dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
132763a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1328f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
13299da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
133063a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
133163a7246aSmatthias.ringwald         }
13320fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
13330fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1334f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
13350fe7a9d0S[email protected]         }
133663a7246aSmatthias.ringwald         // check for unknown options
133763a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1338c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
133963a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
13401dc511deSmatthias.ringwald         }
13411dc511deSmatthias.ringwald         pos += length;
13421dc511deSmatthias.ringwald     }
13432784b77dSmatthias.ringwald }
13442784b77dSmatthias.ringwald 
1345fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
13469da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
134773cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
134873cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1349019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1350019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1351fa8473a4Smatthias.ringwald     return 1;
1352fa8473a4Smatthias.ringwald }
1353fa8473a4Smatthias.ringwald 
1354fa8473a4Smatthias.ringwald 
13557f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
13561e6aba47Smatthias.ringwald 
135700d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
135800d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
135938e5900eSmatthias.ringwald     uint16_t result = 0;
13601e6aba47Smatthias.ringwald 
13619da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1362b35f641cSmatthias.ringwald 
13639a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
13649a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
13659a011532Smatthias.ringwald         switch (channel->state){
1366fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
13679a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
13682b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
13699a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
13709a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
13719a011532Smatthias.ringwald                 break;
13729a011532Smatthias.ringwald 
13739a011532Smatthias.ringwald             default:
13749a011532Smatthias.ringwald                 // ignore in other states
13759a011532Smatthias.ringwald                 break;
13769a011532Smatthias.ringwald         }
13779a011532Smatthias.ringwald         return;
13789a011532Smatthias.ringwald     }
13799a011532Smatthias.ringwald 
138056081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
13811e6aba47Smatthias.ringwald     switch (channel->state) {
13821e6aba47Smatthias.ringwald 
13831e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
13841e6aba47Smatthias.ringwald             switch (code){
13851e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
13865932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1387f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
138838e5900eSmatthias.ringwald                     switch (result) {
138938e5900eSmatthias.ringwald                         case 0:
1390169f8b28Smatthias.ringwald                             // successful connection
1391f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1392fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
139328ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
139438e5900eSmatthias.ringwald                             break;
139538e5900eSmatthias.ringwald                         case 1:
13965932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
13975932bd7cS[email protected]                             l2cap_start_ertx(channel);
139838e5900eSmatthias.ringwald                             break;
139938e5900eSmatthias.ringwald                         default:
1400eb920dbeSmatthias.ringwald                             // channel closed
1401eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1402f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
140338e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1404eb920dbeSmatthias.ringwald 
1405eb920dbeSmatthias.ringwald                             // drop link key if security block
1406eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
140715a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1408eb920dbeSmatthias.ringwald                             }
1409eb920dbeSmatthias.ringwald 
1410eb920dbeSmatthias.ringwald                             // discard channel
1411665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1412d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
141338e5900eSmatthias.ringwald                             break;
14141e6aba47Smatthias.ringwald                     }
14151e6aba47Smatthias.ringwald                     break;
141638e5900eSmatthias.ringwald 
141738e5900eSmatthias.ringwald                 default:
14181e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
141938e5900eSmatthias.ringwald                     break;
14201e6aba47Smatthias.ringwald             }
14211e6aba47Smatthias.ringwald             break;
14221e6aba47Smatthias.ringwald 
1423fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1424f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1425ae280e73Smatthias.ringwald             switch (code) {
1426ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
142728ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1428ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
142963a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
143063a7246aSmatthias.ringwald                         // only done if continuation not set
143163a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
143263a7246aSmatthias.ringwald                     }
1433ae280e73Smatthias.ringwald                     break;
14341e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
14355932bd7cS[email protected]                     l2cap_stop_rtx(channel);
14365932bd7cS[email protected]                     switch (result){
14375932bd7cS[email protected]                         case 0: // success
14385932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
14395932bd7cS[email protected]                             break;
14405932bd7cS[email protected]                         case 4: // pending
14415932bd7cS[email protected]                             l2cap_start_ertx(channel);
14425932bd7cS[email protected]                             break;
14435932bd7cS[email protected]                         default:
1444fe9d8984S[email protected]                             // retry on negative result
1445fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1446fe9d8984S[email protected]                             break;
1447fe9d8984S[email protected]                     }
14485a67bd4aSmatthias.ringwald                     break;
14495a67bd4aSmatthias.ringwald                 default:
14505a67bd4aSmatthias.ringwald                     break;
14511e6aba47Smatthias.ringwald             }
1452fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1453fa8473a4Smatthias.ringwald                 // for open:
14545a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1455fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1456c8e4258aSmatthias.ringwald             }
1457c8e4258aSmatthias.ringwald             break;
1458f62db1e3Smatthias.ringwald 
1459f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1460f62db1e3Smatthias.ringwald             switch (code) {
1461f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
146227a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
146327a923d0Smatthias.ringwald                     break;
14645a67bd4aSmatthias.ringwald                 default:
14655a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
14665a67bd4aSmatthias.ringwald                     break;
146727a923d0Smatthias.ringwald             }
146827a923d0Smatthias.ringwald             break;
146984836b65Smatthias.ringwald 
147084836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
147184836b65Smatthias.ringwald             // @TODO handle incoming requests
147284836b65Smatthias.ringwald             break;
147384836b65Smatthias.ringwald 
147484836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
147584836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
147684836b65Smatthias.ringwald             break;
147710642e45Smatthias.ringwald         default:
147810642e45Smatthias.ringwald             break;
147927a923d0Smatthias.ringwald     }
14809da54300S[email protected]     // log_info("new state %u", channel->state);
148127a923d0Smatthias.ringwald }
148227a923d0Smatthias.ringwald 
148300d93d79Smatthias.ringwald 
14847f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
148500d93d79Smatthias.ringwald 
148600d93d79Smatthias.ringwald     // get code, signalind identifier and command len
148700d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
148800d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
148900d93d79Smatthias.ringwald 
149000d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
149100d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
149263a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
149300d93d79Smatthias.ringwald         return;
149400d93d79Smatthias.ringwald     }
149500d93d79Smatthias.ringwald 
149600d93d79Smatthias.ringwald     // general commands without an assigned channel
149700d93d79Smatthias.ringwald     switch(code) {
149800d93d79Smatthias.ringwald 
149900d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1500f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1501f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
150200d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
15032b83fb7dSmatthias.ringwald             return;
150400d93d79Smatthias.ringwald         }
150500d93d79Smatthias.ringwald 
15062b360848Smatthias.ringwald         case ECHO_REQUEST:
15072b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
15082b83fb7dSmatthias.ringwald             return;
150900d93d79Smatthias.ringwald 
151000d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1511f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
15122b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
15132b83fb7dSmatthias.ringwald             return;
151400d93d79Smatthias.ringwald         }
151500d93d79Smatthias.ringwald 
151600d93d79Smatthias.ringwald         default:
151700d93d79Smatthias.ringwald             break;
151800d93d79Smatthias.ringwald     }
151900d93d79Smatthias.ringwald 
152000d93d79Smatthias.ringwald 
152100d93d79Smatthias.ringwald     // Get potential destination CID
1522f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
152300d93d79Smatthias.ringwald 
152400d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1525665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1526665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1527665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1528665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1529fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
153000d93d79Smatthias.ringwald         if (code & 1) {
1531b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1532b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
153300d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15344e32727eSmatthias.ringwald                 break;
153500d93d79Smatthias.ringwald             }
153600d93d79Smatthias.ringwald         } else {
1537b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
153800d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
153900d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15404e32727eSmatthias.ringwald                 break;
154100d93d79Smatthias.ringwald             }
154200d93d79Smatthias.ringwald         }
154300d93d79Smatthias.ringwald     }
154400d93d79Smatthias.ringwald }
1545*09e9d05bSMatthias Ringwald #endif
154600d93d79Smatthias.ringwald 
1547e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1548*09e9d05bSMatthias Ringwald 
1549*09e9d05bSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
1550*09e9d05bSMatthias Ringwald     uint8_t event[6];
1551*09e9d05bSMatthias Ringwald     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
1552*09e9d05bSMatthias Ringwald     event[1] = 4;
1553*09e9d05bSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
1554*09e9d05bSMatthias Ringwald     little_endian_store_16(event, 4, result);
1555*09e9d05bSMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
1556*09e9d05bSMatthias Ringwald     if (!l2cap_event_packet_handler) return;
1557*09e9d05bSMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1558*09e9d05bSMatthias Ringwald }
1559*09e9d05bSMatthias Ringwald 
1560c48b2a2cSMatthias Ringwald // @returns valid
1561c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1562e7d0c9aaSMatthias Ringwald     hci_connection_t * connection;
1563c48b2a2cSMatthias Ringwald     uint16_t result;
1564c48b2a2cSMatthias Ringwald     uint8_t  event[10];
156583fd9c76SMatthias Ringwald 
1566cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1567a3dc965aSMatthias Ringwald     btstack_linked_list_iterator_t it;
1568a3dc965aSMatthias Ringwald     l2cap_channel_t * channel;
1569a3dc965aSMatthias Ringwald     uint16_t local_cid;
157083fd9c76SMatthias Ringwald     uint16_t le_psm;
157163f0ac45SMatthias Ringwald     uint16_t new_credits;
157263f0ac45SMatthias Ringwald     uint16_t credits_before;
1573e7d0c9aaSMatthias Ringwald     l2cap_service_t * service;
157483fd9c76SMatthias Ringwald #endif
157500d93d79Smatthias.ringwald 
15761b8b8d05SMatthias Ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
157723017473SMatthias Ringwald     log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id);
15781b8b8d05SMatthias Ringwald 
15791b8b8d05SMatthias Ringwald     switch (code){
158000d93d79Smatthias.ringwald 
1581c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1582c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1583ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1584ccf076adS[email protected]             break;
1585da886c03S[email protected] 
1586c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1587e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1588da886c03S[email protected]             if (connection){
15896d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
15906d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1591c48b2a2cSMatthias Ringwald                     return 0;
15926d91fb6cSMatthias Ringwald                 }
1593da886c03S[email protected]                 int update_parameter = 1;
1594a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
15954ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1596c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1597c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1598c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1599c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1600da886c03S[email protected] 
1601da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1602da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1603da886c03S[email protected] 
1604da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1605da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1606da886c03S[email protected] 
1607da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1608da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1609da886c03S[email protected] 
1610da886c03S[email protected]                 if (update_parameter){
1611da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1612da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1613da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1614da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1615da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1616da886c03S[email protected]                 } else {
1617da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1618da886c03S[email protected]                 }
1619c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1620da886c03S[email protected]             }
1621da886c03S[email protected] 
162233c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1623c48b2a2cSMatthias Ringwald 
1624c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1625c48b2a2cSMatthias Ringwald             event[1] = 8;
1626c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1627c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
162833c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1629ccf076adS[email protected]             break;
1630c48b2a2cSMatthias Ringwald 
1631a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
1632a3dc965aSMatthias Ringwald 
163363f0ac45SMatthias Ringwald         case COMMAND_REJECT:
163463f0ac45SMatthias Ringwald             // Find channel for this sig_id and connection handle
163563f0ac45SMatthias Ringwald             channel = NULL;
163663f0ac45SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
163763f0ac45SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
163863f0ac45SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
163963f0ac45SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
164063f0ac45SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
164163f0ac45SMatthias Ringwald                 channel = a_channel;
164263f0ac45SMatthias Ringwald                 break;
164363f0ac45SMatthias Ringwald             }
164463f0ac45SMatthias Ringwald             if (!channel) break;
164563f0ac45SMatthias Ringwald 
164663f0ac45SMatthias Ringwald             // if received while waiting for le connection response, assume legacy device
164763f0ac45SMatthias Ringwald             if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){
164863f0ac45SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
164963f0ac45SMatthias Ringwald                 // no official value for this, use: Connection refused – LE_PSM not supported - 0x0002
165044276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, 0x0002);
165163f0ac45SMatthias Ringwald 
165263f0ac45SMatthias Ringwald                 // discard channel
165363f0ac45SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
165463f0ac45SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
165563f0ac45SMatthias Ringwald                 break;
165663f0ac45SMatthias Ringwald             }
165763f0ac45SMatthias Ringwald             break;
165863f0ac45SMatthias Ringwald 
1659e7d0c9aaSMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_REQUEST:
1660e7d0c9aaSMatthias Ringwald 
1661e7d0c9aaSMatthias Ringwald             // get hci connection, bail if not found (must not happen)
1662e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1663e7d0c9aaSMatthias Ringwald             if (!connection) return 0;
1664e7d0c9aaSMatthias Ringwald 
1665e7d0c9aaSMatthias Ringwald             // check if service registered
1666e7d0c9aaSMatthias Ringwald             le_psm  = little_endian_read_16(command, 4);
1667e7d0c9aaSMatthias Ringwald             service = l2cap_le_get_service(le_psm);
1668e7d0c9aaSMatthias Ringwald 
1669e7d0c9aaSMatthias Ringwald             if (service){
1670e7d0c9aaSMatthias Ringwald 
1671e7d0c9aaSMatthias Ringwald                 uint16_t source_cid = little_endian_read_16(command, 6);
1672e7d0c9aaSMatthias Ringwald                 if (source_cid < 0x40){
1673e7d0c9aaSMatthias Ringwald                     // 0x0009 Connection refused - Invalid Source CID
1674e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009);
1675e7d0c9aaSMatthias Ringwald                     return 1;
1676e7d0c9aaSMatthias Ringwald                 }
1677e7d0c9aaSMatthias Ringwald 
1678e7d0c9aaSMatthias Ringwald                 // go through list of channels for this ACL connection and check if we get a match
1679e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1680e7d0c9aaSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it)){
16811b8b8d05SMatthias Ringwald                     l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16821b8b8d05SMatthias Ringwald                     if (a_channel->con_handle != handle) continue;
16831b8b8d05SMatthias Ringwald                     if (a_channel->remote_cid != source_cid) continue;
1684e7d0c9aaSMatthias Ringwald                     // 0x000a Connection refused - Source CID already allocated
1685e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a);
1686e7d0c9aaSMatthias Ringwald                     return 1;
1687e7d0c9aaSMatthias Ringwald                 }
1688e7d0c9aaSMatthias Ringwald 
168983fd9c76SMatthias Ringwald                 // security: check encryption
169083fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_2){
169183fd9c76SMatthias Ringwald                     if (sm_encryption_key_size(handle) == 0){
169283fd9c76SMatthias Ringwald                         // 0x0008 Connection refused - insufficient encryption
169383fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0008);
169483fd9c76SMatthias Ringwald                         return 1;
169583fd9c76SMatthias Ringwald                     }
169683fd9c76SMatthias Ringwald                     // anything less than 16 byte key size is insufficient
169783fd9c76SMatthias Ringwald                     if (sm_encryption_key_size(handle) < 16){
169883fd9c76SMatthias Ringwald                         // 0x0007 Connection refused – insufficient encryption key size
169983fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0007);
170083fd9c76SMatthias Ringwald                         return 1;
170183fd9c76SMatthias Ringwald                     }
170283fd9c76SMatthias Ringwald                 }
170383fd9c76SMatthias Ringwald 
170483fd9c76SMatthias Ringwald                 // security: check authencation
170583fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_3){
170683fd9c76SMatthias Ringwald                     if (!sm_authenticated(handle)){
170783fd9c76SMatthias Ringwald                         // 0x0005 Connection refused – insufficient authentication
170883fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0005);
170983fd9c76SMatthias Ringwald                         return 1;
171083fd9c76SMatthias Ringwald                     }
171183fd9c76SMatthias Ringwald                 }
171283fd9c76SMatthias Ringwald 
171383fd9c76SMatthias Ringwald                 // security: check authorization
171483fd9c76SMatthias Ringwald                 if (service->required_security_level >= LEVEL_4){
171583fd9c76SMatthias Ringwald                     if (sm_authorization_state(handle) != AUTHORIZATION_GRANTED){
171683fd9c76SMatthias Ringwald                         // 0x0006 Connection refused – insufficient authorization
171783fd9c76SMatthias Ringwald                         l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0006);
171883fd9c76SMatthias Ringwald                         return 1;
171983fd9c76SMatthias Ringwald                     }
172083fd9c76SMatthias Ringwald                 }
1721e7d0c9aaSMatthias Ringwald 
1722e7d0c9aaSMatthias Ringwald                 // allocate channel
17231b8b8d05SMatthias Ringwald                 channel = l2cap_create_channel_entry(service->packet_handler, connection->address,
1724e7d0c9aaSMatthias Ringwald                     BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
1725e7d0c9aaSMatthias Ringwald                 if (!channel){
1726e7d0c9aaSMatthias Ringwald                     // 0x0004 Connection refused – no resources available
1727e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004);
1728e7d0c9aaSMatthias Ringwald                     return 1;
1729e7d0c9aaSMatthias Ringwald                 }
1730e7d0c9aaSMatthias Ringwald 
1731e7d0c9aaSMatthias Ringwald                 channel->con_handle = handle;
1732e7d0c9aaSMatthias Ringwald                 channel->remote_cid = source_cid;
1733e7d0c9aaSMatthias Ringwald                 channel->remote_sig_id = sig_id;
17347f107edaSMatthias Ringwald                 channel->remote_mtu = little_endian_read_16(command, 8);
17357f107edaSMatthias Ringwald                 channel->remote_mps = little_endian_read_16(command, 10);
17367f107edaSMatthias Ringwald                 channel->credits_outgoing = little_endian_read_16(command, 12);
1737e7d0c9aaSMatthias Ringwald 
1738e7d0c9aaSMatthias Ringwald                 // set initial state
1739e7d0c9aaSMatthias Ringwald                 channel->state      = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1740c99bb618SMatthias Ringwald                 channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING;
1741e7d0c9aaSMatthias Ringwald 
1742e7d0c9aaSMatthias Ringwald                 // add to connections list
1743e7d0c9aaSMatthias Ringwald                 btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1744e7d0c9aaSMatthias Ringwald 
1745e7d0c9aaSMatthias Ringwald                 // post connection request event
174644276248SMatthias Ringwald                 l2cap_emit_le_incoming_connection(channel);
1747e7d0c9aaSMatthias Ringwald 
1748e7d0c9aaSMatthias Ringwald             } else {
1749e7d0c9aaSMatthias Ringwald                 // Connection refused – LE_PSM not supported
1750e7d0c9aaSMatthias Ringwald                 l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002);
1751e7d0c9aaSMatthias Ringwald             }
1752e7d0c9aaSMatthias Ringwald             break;
17531b8b8d05SMatthias Ringwald 
17541b8b8d05SMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_RESPONSE:
17551b8b8d05SMatthias Ringwald             // Find channel for this sig_id and connection handle
17561b8b8d05SMatthias Ringwald             channel = NULL;
17571b8b8d05SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
17581b8b8d05SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
17591b8b8d05SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
17601b8b8d05SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
17611b8b8d05SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
17621b8b8d05SMatthias Ringwald                 channel = a_channel;
17631b8b8d05SMatthias Ringwald                 break;
17641b8b8d05SMatthias Ringwald             }
17651b8b8d05SMatthias Ringwald             if (!channel) break;
17661b8b8d05SMatthias Ringwald 
17671b8b8d05SMatthias Ringwald             // cid + 0
17681b8b8d05SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
17691b8b8d05SMatthias Ringwald             if (result){
17701b8b8d05SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
17711b8b8d05SMatthias Ringwald                 // map l2cap connection response result to BTstack status enumeration
177244276248SMatthias Ringwald                 l2cap_emit_le_channel_opened(channel, result);
17731b8b8d05SMatthias Ringwald 
17741b8b8d05SMatthias Ringwald                 // discard channel
177563f0ac45SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
17761b8b8d05SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
17771b8b8d05SMatthias Ringwald                 break;
17781b8b8d05SMatthias Ringwald             }
17791b8b8d05SMatthias Ringwald 
17801b8b8d05SMatthias Ringwald             // success
17811b8b8d05SMatthias Ringwald             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
17821b8b8d05SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
17831b8b8d05SMatthias Ringwald             channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
17841b8b8d05SMatthias Ringwald             channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
17851b8b8d05SMatthias Ringwald             channel->state = L2CAP_STATE_OPEN;
178644276248SMatthias Ringwald             l2cap_emit_le_channel_opened(channel, result);
17871b8b8d05SMatthias Ringwald             break;
17881b8b8d05SMatthias Ringwald 
178985aeef60SMatthias Ringwald         case LE_FLOW_CONTROL_CREDIT:
179085aeef60SMatthias Ringwald             // find channel
179185aeef60SMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
179285aeef60SMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
179363f0ac45SMatthias Ringwald             if (!channel) {
179463f0ac45SMatthias Ringwald                 log_error("l2cap: no channel for cid 0x%02x", local_cid);
179563f0ac45SMatthias Ringwald                 break;
179663f0ac45SMatthias Ringwald             }
179763f0ac45SMatthias Ringwald             new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
179863f0ac45SMatthias Ringwald             credits_before = channel->credits_outgoing;
179963f0ac45SMatthias Ringwald             channel->credits_outgoing += new_credits;
180063f0ac45SMatthias Ringwald             // check for credit overrun
180163f0ac45SMatthias Ringwald             if (credits_before > channel->credits_outgoing){
180263f0ac45SMatthias Ringwald                 log_error("l2cap: new credits caused overrrun for cid 0x%02x, disconnecting", local_cid);
180363f0ac45SMatthias Ringwald                 channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
180463f0ac45SMatthias Ringwald                 break;
180563f0ac45SMatthias Ringwald             }
180663f0ac45SMatthias Ringwald             log_info("l2cap: %u credits for 0x%02x, now %u", new_credits, local_cid, channel->credits_outgoing);
180785aeef60SMatthias Ringwald             break;
180885aeef60SMatthias Ringwald 
1809828a7f7aSMatthias Ringwald         case DISCONNECTION_REQUEST:
1810828a7f7aSMatthias Ringwald             // find channel
1811828a7f7aSMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
1812828a7f7aSMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
181363f34e00SMatthias Ringwald             if (!channel) {
181463f34e00SMatthias Ringwald                 log_error("l2cap: no channel for cid 0x%02x", local_cid);
181563f34e00SMatthias Ringwald                 break;
181663f34e00SMatthias Ringwald             }
1817828a7f7aSMatthias Ringwald             channel->remote_sig_id = sig_id;
1818828a7f7aSMatthias Ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1819828a7f7aSMatthias Ringwald             break;
1820828a7f7aSMatthias Ringwald 
1821a3dc965aSMatthias Ringwald #endif
1822a3dc965aSMatthias Ringwald 
182363f0ac45SMatthias Ringwald         case DISCONNECTION_RESPONSE:
182463f0ac45SMatthias Ringwald             break;
182563f0ac45SMatthias Ringwald 
1826c48b2a2cSMatthias Ringwald         default:
1827c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1828c48b2a2cSMatthias Ringwald             return 0;
1829ccf076adS[email protected]     }
1830c48b2a2cSMatthias Ringwald     return 1;
1831c48b2a2cSMatthias Ringwald }
1832e7d0c9aaSMatthias Ringwald #endif
1833c48b2a2cSMatthias Ringwald 
1834c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
1835c48b2a2cSMatthias Ringwald 
1836*09e9d05bSMatthias Ringwald     l2cap_channel_t * l2cap_channel;
1837*09e9d05bSMatthias Ringwald     (void) l2cap_channel;
1838*09e9d05bSMatthias Ringwald 
1839c48b2a2cSMatthias Ringwald     // Get Channel ID
1840c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1841c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1842c48b2a2cSMatthias Ringwald 
1843c48b2a2cSMatthias Ringwald     switch (channel_id) {
1844c48b2a2cSMatthias Ringwald 
1845*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
1846c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1847c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1848c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1849c48b2a2cSMatthias Ringwald                 // handle signaling commands
1850c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1851c48b2a2cSMatthias Ringwald 
1852c48b2a2cSMatthias Ringwald                 // increment command_offset
1853c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1854c48b2a2cSMatthias Ringwald             }
1855c48b2a2cSMatthias Ringwald             break;
1856c48b2a2cSMatthias Ringwald         }
1857*09e9d05bSMatthias Ringwald #endif
1858c48b2a2cSMatthias Ringwald 
1859e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1860e7d0c9aaSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE: {
1861e7d0c9aaSMatthias Ringwald             uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1862e7d0c9aaSMatthias Ringwald             int      valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1863c48b2a2cSMatthias Ringwald             if (!valid){
18641bbc0b23S[email protected]                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
1865ccf076adS[email protected]             }
1866ccf076adS[email protected]             break;
1867e7d0c9aaSMatthias Ringwald         }
1868e7d0c9aaSMatthias Ringwald #endif
1869c48b2a2cSMatthias Ringwald 
1870c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1871c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1872c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1873ccf076adS[email protected]             }
1874c48b2a2cSMatthias Ringwald             break;
1875c48b2a2cSMatthias Ringwald 
1876c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1877c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1878c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1879c48b2a2cSMatthias Ringwald             }
1880c48b2a2cSMatthias Ringwald             break;
1881c48b2a2cSMatthias Ringwald 
1882c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1883c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1884c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1885c48b2a2cSMatthias Ringwald             }
1886c48b2a2cSMatthias Ringwald             break;
18871bbc0b23S[email protected] 
1888*09e9d05bSMatthias Ringwald         default:
1889*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
189000d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
189164e11ca9SMatthias Ringwald             l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1892d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
18933d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
189400d93d79Smatthias.ringwald             }
1895*09e9d05bSMatthias Ringwald #endif
1896a3dc965aSMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
189764e11ca9SMatthias Ringwald             l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id);
189864e11ca9SMatthias Ringwald             if (l2cap_channel) {
189985aeef60SMatthias Ringwald                 // credit counting
190085aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming == 0){
190185aeef60SMatthias Ringwald                     log_error("LE Data Channel packet received but no incoming credits");
190285aeef60SMatthias Ringwald                     l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
190385aeef60SMatthias Ringwald                     break;
190485aeef60SMatthias Ringwald                 }
190585aeef60SMatthias Ringwald                 l2cap_channel->credits_incoming--;
190685aeef60SMatthias Ringwald 
190785aeef60SMatthias Ringwald                 // automatic credits
190885aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){
190985aeef60SMatthias Ringwald                     l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT;
191085aeef60SMatthias Ringwald                 }
191185aeef60SMatthias Ringwald 
1912cd529728SMatthias Ringwald                 // first fragment
1913cd529728SMatthias Ringwald                 uint16_t pos = 0;
1914cd529728SMatthias Ringwald                 if (!l2cap_channel->receive_sdu_len){
1915cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
1916cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_pos = 0;
1917cd529728SMatthias Ringwald                     pos  += 2;
1918cd529728SMatthias Ringwald                     size -= 2;
1919cd529728SMatthias Ringwald                 }
1920cd529728SMatthias Ringwald                 memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER);
1921cd529728SMatthias Ringwald                 l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER;
1922cd529728SMatthias Ringwald                 // done?
192363f0ac45SMatthias Ringwald                 log_info("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len);
1924cd529728SMatthias Ringwald                 if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){
1925cd529728SMatthias Ringwald                     l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len);
1926cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = 0;
1927cd529728SMatthias Ringwald                 }
192863f0ac45SMatthias Ringwald             } else {
192963f0ac45SMatthias Ringwald                 log_error("LE Data Channel packet received but no channel found for cid 0x%02x", channel_id);
193064e11ca9SMatthias Ringwald             }
193164e11ca9SMatthias Ringwald #endif
19325652b5ffS[email protected]             break;
19335652b5ffS[email protected]     }
193400d93d79Smatthias.ringwald 
19351eb2563eS[email protected]     l2cap_run();
19362718e2e7Smatthias.ringwald }
193700d93d79Smatthias.ringwald 
1938*09e9d05bSMatthias Ringwald // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
1939*09e9d05bSMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
1940*09e9d05bSMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
1941*09e9d05bSMatthias Ringwald     if (index < 0) return;
1942*09e9d05bSMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
1943*09e9d05bSMatthias Ringwald }
1944*09e9d05bSMatthias Ringwald 
1945*09e9d05bSMatthias Ringwald #ifdef ENABLE_CLASSIC
194615ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
194727a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1948f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1949f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1950f62db1e3Smatthias.ringwald     // discard channel
19519dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1952665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1953d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1954c8e4258aSmatthias.ringwald }
19551e6aba47Smatthias.ringwald 
19568f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1957665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1958665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1959665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1960665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
19619d9bbc01Smatthias.ringwald         if ( service->psm == psm){
19629d9bbc01Smatthias.ringwald             return service;
19639d9bbc01Smatthias.ringwald         };
19649d9bbc01Smatthias.ringwald     }
19659d9bbc01Smatthias.ringwald     return NULL;
19669d9bbc01Smatthias.ringwald }
19679d9bbc01Smatthias.ringwald 
19687192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
19697192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
19707192e786SMatthias Ringwald }
19717192e786SMatthias Ringwald 
1972e0abb8e7S[email protected] 
1973be2053a6SMatthias 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){
1974be2053a6SMatthias Ringwald 
1975be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1976e0abb8e7S[email protected] 
19774bb582b6Smatthias.ringwald     // check for alread registered psm
19789d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1979277abc2cSmatthias.ringwald     if (service) {
1980be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1981be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1982277abc2cSmatthias.ringwald     }
19839d9bbc01Smatthias.ringwald 
19844bb582b6Smatthias.ringwald     // alloc structure
1985bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1986277abc2cSmatthias.ringwald     if (!service) {
1987be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1988be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1989277abc2cSmatthias.ringwald     }
19909d9bbc01Smatthias.ringwald 
19919d9bbc01Smatthias.ringwald     // fill in
19929d9bbc01Smatthias.ringwald     service->psm = psm;
19939d9bbc01Smatthias.ringwald     service->mtu = mtu;
199405ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1995df3354fcS[email protected]     service->required_security_level = security_level;
19969d9bbc01Smatthias.ringwald 
19979d9bbc01Smatthias.ringwald     // add to services list
1998665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1999c0e866bfSmatthias.ringwald 
2000c0e866bfSmatthias.ringwald     // enable page scan
200115a95bd5SMatthias Ringwald     gap_connectable_control(1);
20025842b6d9Smatthias.ringwald 
2003be2053a6SMatthias Ringwald     return 0;
20049d9bbc01Smatthias.ringwald }
20059d9bbc01Smatthias.ringwald 
20067e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
2007e0abb8e7S[email protected] 
2008e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
2009e0abb8e7S[email protected] 
20109d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
20117e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
2012665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
2013d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
2014c0e866bfSmatthias.ringwald 
2015c0e866bfSmatthias.ringwald     // disable page scan when no services registered
20167e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
201715a95bd5SMatthias Ringwald         gap_connectable_control(0);
20189d9bbc01Smatthias.ringwald     }
20197e8856ebSMatthias Ringwald     return 0;
20207e8856ebSMatthias Ringwald }
2021*09e9d05bSMatthias Ringwald #endif
20229d9bbc01Smatthias.ringwald 
20237192e786SMatthias Ringwald 
2024cab29d48SMatthias Ringwald #ifdef ENABLE_LE_DATA_CHANNELS
202583fd9c76SMatthias Ringwald 
202657be49d6SMatthias Ringwald static void l2cap_le_notify_channel_can_send(l2cap_channel_t *channel){
202757be49d6SMatthias Ringwald     if (!channel->waiting_for_can_send_now) return;
202857be49d6SMatthias Ringwald     if (channel->send_sdu_buffer) return;
202957be49d6SMatthias Ringwald     channel->waiting_for_can_send_now = 0;
203057be49d6SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_LE_CAN_SEND_NOW local_cid 0x%x", channel->local_cid);
203157be49d6SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_LE_CAN_SEND_NOW);
203257be49d6SMatthias Ringwald }
203357be49d6SMatthias Ringwald 
203457be49d6SMatthias Ringwald // 1BH2222
203557be49d6SMatthias Ringwald static void l2cap_emit_le_incoming_connection(l2cap_channel_t *channel) {
203657be49d6SMatthias 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",
203757be49d6SMatthias Ringwald              channel->address_type, bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);
203857be49d6SMatthias Ringwald     uint8_t event[19];
203957be49d6SMatthias Ringwald     event[0] = L2CAP_EVENT_LE_INCOMING_CONNECTION;
204057be49d6SMatthias Ringwald     event[1] = sizeof(event) - 2;
204157be49d6SMatthias Ringwald     event[2] = channel->address_type;
204257be49d6SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
204357be49d6SMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
204457be49d6SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
204557be49d6SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
204657be49d6SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
204757be49d6SMatthias Ringwald     little_endian_store_16(event, 17, channel->remote_mtu);
204857be49d6SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
204957be49d6SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
205057be49d6SMatthias Ringwald }
205157be49d6SMatthias Ringwald // 11BH22222
205257be49d6SMatthias Ringwald static void l2cap_emit_le_channel_opened(l2cap_channel_t *channel, uint8_t status) {
205357be49d6SMatthias 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",
205457be49d6SMatthias Ringwald              status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
205557be49d6SMatthias Ringwald              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
2056c99bb618SMatthias Ringwald     uint8_t event[23];
205757be49d6SMatthias Ringwald     event[0] = L2CAP_EVENT_LE_CHANNEL_OPENED;
205857be49d6SMatthias Ringwald     event[1] = sizeof(event) - 2;
205957be49d6SMatthias Ringwald     event[2] = status;
206057be49d6SMatthias Ringwald     event[3] = channel->address_type;
206157be49d6SMatthias Ringwald     reverse_bd_addr(channel->address, &event[4]);
206257be49d6SMatthias Ringwald     little_endian_store_16(event, 10, channel->con_handle);
2063c99bb618SMatthias Ringwald     event[12] = channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING ? 1 : 0;
2064c99bb618SMatthias Ringwald     little_endian_store_16(event, 13, channel->psm);
2065c99bb618SMatthias Ringwald     little_endian_store_16(event, 15, channel->local_cid);
2066c99bb618SMatthias Ringwald     little_endian_store_16(event, 17, channel->remote_cid);
2067c99bb618SMatthias Ringwald     little_endian_store_16(event, 19, channel->local_mtu);
2068c99bb618SMatthias Ringwald     little_endian_store_16(event, 21, channel->remote_mtu);
206957be49d6SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
207057be49d6SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
207157be49d6SMatthias Ringwald }
207257be49d6SMatthias Ringwald 
207357be49d6SMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){
207457be49d6SMatthias Ringwald     btstack_linked_list_iterator_t it;
207557be49d6SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
207657be49d6SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
207757be49d6SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
207857be49d6SMatthias Ringwald         if ( channel->local_cid == local_cid) {
207957be49d6SMatthias Ringwald             return channel;
208057be49d6SMatthias Ringwald         }
208157be49d6SMatthias Ringwald     }
208257be49d6SMatthias Ringwald     return NULL;
208357be49d6SMatthias Ringwald }
208457be49d6SMatthias Ringwald 
208557be49d6SMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
208657be49d6SMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){
208757be49d6SMatthias Ringwald     channel->state = L2CAP_STATE_CLOSED;
208857be49d6SMatthias Ringwald     l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
208957be49d6SMatthias Ringwald     // discard channel
209057be49d6SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
209157be49d6SMatthias Ringwald     btstack_memory_l2cap_channel_free(channel);
209257be49d6SMatthias Ringwald }
209357be49d6SMatthias Ringwald 
2094e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
2095e7d0c9aaSMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, le_psm);
20967192e786SMatthias Ringwald }
2097efedfb4cSMatthias Ringwald 
2098da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
20997192e786SMatthias Ringwald 
2100da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
21017192e786SMatthias Ringwald 
21027192e786SMatthias Ringwald     // check for alread registered psm
21037192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
21047192e786SMatthias Ringwald     if (service) {
2105da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
21067192e786SMatthias Ringwald     }
21077192e786SMatthias Ringwald 
21087192e786SMatthias Ringwald     // alloc structure
21097192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
21107192e786SMatthias Ringwald     if (!service) {
21117192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
2112da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
21137192e786SMatthias Ringwald     }
21147192e786SMatthias Ringwald 
21157192e786SMatthias Ringwald     // fill in
21167192e786SMatthias Ringwald     service->psm = psm;
2117da144af5SMatthias Ringwald     service->mtu = 0;
21187192e786SMatthias Ringwald     service->packet_handler = packet_handler;
21197192e786SMatthias Ringwald     service->required_security_level = security_level;
21207192e786SMatthias Ringwald 
21217192e786SMatthias Ringwald     // add to services list
2122665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
21237192e786SMatthias Ringwald 
21247192e786SMatthias Ringwald     // done
2125da144af5SMatthias Ringwald     return 0;
21267192e786SMatthias Ringwald }
21277192e786SMatthias Ringwald 
2128da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
21297192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
21307192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
21319367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
2132da144af5SMatthias Ringwald 
2133665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
21347192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
2135da144af5SMatthias Ringwald     return 0;
21367192e786SMatthias Ringwald }
2137da144af5SMatthias Ringwald 
2138da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
2139e7d0c9aaSMatthias Ringwald     // get channel
2140e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2141e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2142e7d0c9aaSMatthias Ringwald 
2143e7d0c9aaSMatthias Ringwald     // validate state
2144e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
2145e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2146e7d0c9aaSMatthias Ringwald     }
2147e7d0c9aaSMatthias Ringwald 
2148efedfb4cSMatthias Ringwald     // set state accept connection
214923017473SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
215023017473SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
215123017473SMatthias Ringwald     channel->local_mtu = mtu;
215285aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
215385aeef60SMatthias Ringwald     channel->automatic_credits  = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
215485aeef60SMatthias Ringwald 
215585aeef60SMatthias Ringwald     // test
215663f0ac45SMatthias Ringwald     // channel->new_credits_incoming = 1;
2157e7d0c9aaSMatthias Ringwald 
2158e7d0c9aaSMatthias Ringwald     // go
2159e7d0c9aaSMatthias Ringwald     l2cap_run();
2160da144af5SMatthias Ringwald     return 0;
2161da144af5SMatthias Ringwald }
2162da144af5SMatthias Ringwald 
2163da144af5SMatthias Ringwald /**
2164da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
2165da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2166da144af5SMatthias Ringwald  */
2167da144af5SMatthias Ringwald 
2168da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
2169e7d0c9aaSMatthias Ringwald     // get channel
2170e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2171e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2172e7d0c9aaSMatthias Ringwald 
2173e7d0c9aaSMatthias Ringwald     // validate state
2174e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
2175e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2176e7d0c9aaSMatthias Ringwald     }
2177e7d0c9aaSMatthias Ringwald 
2178efedfb4cSMatthias Ringwald     // set state decline connection
2179e7d0c9aaSMatthias Ringwald     channel->state  = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
2180e7d0c9aaSMatthias Ringwald     channel->reason = 0x04; // no resources available
2181e7d0c9aaSMatthias Ringwald     l2cap_run();
2182da144af5SMatthias Ringwald     return 0;
2183da144af5SMatthias Ringwald }
2184da144af5SMatthias Ringwald 
21857dafa750SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
2186da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
2187efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
2188efedfb4cSMatthias Ringwald 
21897dafa750SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu);
2190da144af5SMatthias Ringwald 
21917dafa750SMatthias Ringwald 
21927dafa750SMatthias Ringwald     hci_connection_t * connection = hci_connection_for_handle(con_handle);
21937dafa750SMatthias Ringwald     if (!connection) {
21947dafa750SMatthias Ringwald         log_error("no hci_connection for handle 0x%04x", con_handle);
21957dafa750SMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
21967dafa750SMatthias Ringwald     }
21977dafa750SMatthias Ringwald 
21987dafa750SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, connection->address, connection->address_type, psm, mtu, security_level);
2199da144af5SMatthias Ringwald     if (!channel) {
2200da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
2201da144af5SMatthias Ringwald     }
2202e7d0c9aaSMatthias Ringwald     log_info("l2cap_le_create_channel %p", channel);
2203da144af5SMatthias Ringwald 
2204da144af5SMatthias Ringwald     // store local_cid
2205da144af5SMatthias Ringwald     if (out_local_cid){
2206da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
2207da144af5SMatthias Ringwald     }
2208da144af5SMatthias Ringwald 
22097dafa750SMatthias Ringwald     // provide buffer
22107dafa750SMatthias Ringwald     channel->con_handle = con_handle;
2211cd529728SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
22127dafa750SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
221385aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
221485aeef60SMatthias Ringwald     channel->automatic_credits    = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
221585aeef60SMatthias Ringwald 
2216efedfb4cSMatthias Ringwald     // add to connections list
2217efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
2218efedfb4cSMatthias Ringwald 
22197dafa750SMatthias Ringwald     // go
222063f0ac45SMatthias Ringwald     l2cap_run();
2221da144af5SMatthias Ringwald     return 0;
2222da144af5SMatthias Ringwald }
2223da144af5SMatthias Ringwald 
2224da144af5SMatthias Ringwald /**
2225da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
2226da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2227da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
2228da144af5SMatthias Ringwald  */
222964e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
223063f0ac45SMatthias Ringwald 
223163f0ac45SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
223263f0ac45SMatthias Ringwald     if (!channel) {
223363f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
223463f0ac45SMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
223563f0ac45SMatthias Ringwald     }
223663f0ac45SMatthias Ringwald 
2237efedfb4cSMatthias Ringwald     // check state
223863f0ac45SMatthias Ringwald     if (channel->state != L2CAP_STATE_OPEN){
223963f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits but channel 0x%02x not open yet", local_cid);
224063f0ac45SMatthias Ringwald     }
224163f0ac45SMatthias Ringwald 
224263f0ac45SMatthias Ringwald     // assert incoming credits + credits <= 0xffff
224363f0ac45SMatthias Ringwald     uint32_t total_credits = channel->credits_incoming;
224463f0ac45SMatthias Ringwald     total_credits += channel->new_credits_incoming;
224563f0ac45SMatthias Ringwald     total_credits += credits;
224663f0ac45SMatthias Ringwald     if (total_credits > 0xffff){
224763f0ac45SMatthias Ringwald         log_error("l2cap_le_provide_credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming,
224863f0ac45SMatthias Ringwald             channel->new_credits_incoming, credits);
224963f0ac45SMatthias Ringwald     }
225063f0ac45SMatthias Ringwald 
2251efedfb4cSMatthias Ringwald     // set credits_granted
225263f0ac45SMatthias Ringwald     channel->new_credits_incoming += credits;
225363f0ac45SMatthias Ringwald 
225463f0ac45SMatthias Ringwald     // go
225563f0ac45SMatthias Ringwald     l2cap_run();
2256da144af5SMatthias Ringwald     return 0;
2257da144af5SMatthias Ringwald }
2258da144af5SMatthias Ringwald 
2259da144af5SMatthias Ringwald /**
2260da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
2261da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2262da144af5SMatthias Ringwald  */
226364e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){
226444276248SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
226544276248SMatthias Ringwald     if (!channel) {
226644276248SMatthias Ringwald         log_error("l2cap_le_provide_credits no channel for cid 0x%02x", local_cid);
2267da144af5SMatthias Ringwald         return 0;
2268da144af5SMatthias Ringwald     }
2269da144af5SMatthias Ringwald 
227044276248SMatthias Ringwald     // check state
227144276248SMatthias Ringwald     if (channel->state != L2CAP_STATE_OPEN) return 0;
227244276248SMatthias Ringwald 
227344276248SMatthias Ringwald     // check queue
227444276248SMatthias Ringwald     if (channel->send_sdu_buffer) return 0;
227544276248SMatthias Ringwald 
227644276248SMatthias Ringwald     // fine, go ahead
227744276248SMatthias Ringwald     return 1;
227844276248SMatthias Ringwald }
227944276248SMatthias Ringwald 
2280da144af5SMatthias Ringwald /**
2281da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
2282da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
2283da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
2284da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2285da144af5SMatthias Ringwald  */
228664e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
228744276248SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
228844276248SMatthias Ringwald     if (!channel) {
228944276248SMatthias Ringwald         log_error("l2cap_le_request_can_send_now_event no channel for cid 0x%02x", local_cid);
229044276248SMatthias Ringwald         return 0;
229144276248SMatthias Ringwald     }
229244276248SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
229344276248SMatthias Ringwald     l2cap_le_notify_channel_can_send(channel);
2294da144af5SMatthias Ringwald     return 0;
2295da144af5SMatthias Ringwald }
2296da144af5SMatthias Ringwald 
2297da144af5SMatthias Ringwald /**
2298da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
2299da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
2300da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2301da144af5SMatthias Ringwald  * @param data                  data to send
2302da144af5SMatthias Ringwald  * @param size                  data size
2303da144af5SMatthias Ringwald  */
230464e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
230564e11ca9SMatthias Ringwald 
230664e11ca9SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
230764e11ca9SMatthias Ringwald     if (!channel) {
230864e11ca9SMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2309828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
231064e11ca9SMatthias Ringwald     }
231164e11ca9SMatthias Ringwald 
231264e11ca9SMatthias Ringwald     if (len > channel->remote_mtu){
231364e11ca9SMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
231464e11ca9SMatthias Ringwald         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
231564e11ca9SMatthias Ringwald     }
231664e11ca9SMatthias Ringwald 
23177f107edaSMatthias Ringwald     if (channel->send_sdu_buffer){
231864e11ca9SMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
231964e11ca9SMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
232064e11ca9SMatthias Ringwald     }
232164e11ca9SMatthias Ringwald 
23227f107edaSMatthias Ringwald     channel->send_sdu_buffer = data;
23237f107edaSMatthias Ringwald     channel->send_sdu_len    = len;
23247f107edaSMatthias Ringwald     channel->send_sdu_pos    = 0;
232564e11ca9SMatthias Ringwald 
23267f107edaSMatthias Ringwald     l2cap_run();
23277f107edaSMatthias Ringwald     return 0;
2328da144af5SMatthias Ringwald }
2329da144af5SMatthias Ringwald 
2330da144af5SMatthias Ringwald /**
2331da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
2332da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2333da144af5SMatthias Ringwald  */
2334828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid)
2335da144af5SMatthias Ringwald {
2336828a7f7aSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2337828a7f7aSMatthias Ringwald     if (!channel) {
2338828a7f7aSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2339828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2340828a7f7aSMatthias Ringwald     }
2341828a7f7aSMatthias Ringwald 
2342828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
2343828a7f7aSMatthias Ringwald     l2cap_run();
2344da144af5SMatthias Ringwald     return 0;
2345da144af5SMatthias Ringwald }
2346da144af5SMatthias Ringwald 
23477f02f414SMatthias Ringwald #endif
2348