xref: /btstack/src/l2cap.c (revision 85aeef606dcc66e7601f2002401127f983410d7e)
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 
5343625864Smatthias.ringwald #include <stdarg.h>
5443625864Smatthias.ringwald #include <string.h>
5543625864Smatthias.ringwald 
5643625864Smatthias.ringwald #include <stdio.h>
5743625864Smatthias.ringwald 
584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
604c744e21Smatthias.ringwald 
6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
62e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6339bda6d5Smatthias.ringwald 
64*85aeef60SMatthias Ringwald // nr of credits provided to remote if credits fall below watermark
65*85aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK 5
66*85aeef60SMatthias Ringwald #define L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT 5
67*85aeef60SMatthias Ringwald 
6800d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6900d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
7000d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
7100d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
7200d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
7300d93d79Smatthias.ringwald 
745628cf69SMatthias Ringwald // internal table
755628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
765628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
775628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
785628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
795628cf69SMatthias Ringwald 
8033c40538SMatthias Ringwald // prototypes
8133c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
8233c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
8333c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
8434e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
8533c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
8633c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
8733c40538SMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8833c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
893d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
9030725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void);
91e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
92828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel);
93e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm);
94e7d0c9aaSMatthias Ringwald #endif
9533c40538SMatthias Ringwald 
965628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
975628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
982125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
995628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
1005628cf69SMatthias Ringwald 
1015628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
1025628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
1035628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
1045628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
10539bda6d5Smatthias.ringwald 
10639bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1072b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1082b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1092b83fb7dSmatthias.ringwald 
11033c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
11133c40538SMatthias Ringwald 
112fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
113fb37a842SMatthias Ringwald 
11433c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1155628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
11639bda6d5Smatthias.ringwald 
11734e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
11834e7e577SMatthias Ringwald     switch (index){
11934e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
12034e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
12134e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
12234e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
12334e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
12434e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
12534e7e577SMatthias Ringwald         default:
12634e7e577SMatthias Ringwald             return 0;
12734e7e577SMatthias Ringwald     }
12834e7e577SMatthias Ringwald }
1295628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1305628cf69SMatthias Ringwald     switch (channel_id){
1315628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1325628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1335628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1345628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1355628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1365628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1375628cf69SMatthias Ringwald         default:
1385628cf69SMatthias Ringwald             return -1;
1395628cf69SMatthias Ringwald         }
1405628cf69SMatthias Ringwald }
14139bda6d5Smatthias.ringwald 
14234e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
14334e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
14434e7e577SMatthias Ringwald     return 1;
14534e7e577SMatthias Ringwald }
14634e7e577SMatthias Ringwald 
14771de195eSMatthias Ringwald void l2cap_init(void){
1482b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
149808a48abSmatthias.ringwald 
150f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
151f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1527192e786SMatthias Ringwald     l2cap_le_services = NULL;
1537192e786SMatthias Ringwald     l2cap_le_channels = NULL;
154f5454fc6Smatthias.ringwald 
15533c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1565628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
157f5454fc6Smatthias.ringwald 
158ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
159ac301f95S[email protected] 
160fcadd0caSmatthias.ringwald     //
1612718e2e7Smatthias.ringwald     // register callback with HCI
162fcadd0caSmatthias.ringwald     //
163d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
164fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
165fb37a842SMatthias Ringwald 
166d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
167fb37a842SMatthias Ringwald 
16815a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
169fcadd0caSmatthias.ringwald }
170fcadd0caSmatthias.ringwald 
171ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
17233c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1731e6aba47Smatthias.ringwald }
1741e6aba47Smatthias.ringwald 
17517a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
17658de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
17758de5610Smatthias.ringwald }
17858de5610Smatthias.ringwald 
17958de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
180c9dc710bS[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",
181fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
182c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
183c9dc710bS[email protected]     uint8_t event[23];
18458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
18558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
18658de5610Smatthias.ringwald     event[2] = status;
187724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
188fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
189f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
190f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
191f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
192f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
193f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
194f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
19558de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19617a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
19758de5610Smatthias.ringwald }
19858de5610Smatthias.ringwald 
19958de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
200e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
20158de5610Smatthias.ringwald     uint8_t event[4];
20258de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
20358de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
204f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
20558de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20617a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
20758de5610Smatthias.ringwald }
20858de5610Smatthias.ringwald 
20958de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
210e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
211fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
21258de5610Smatthias.ringwald     uint8_t event[16];
21358de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
21458de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
215724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
216fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
217f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
218f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
219f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
22058de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22117a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2220af41d30Smatthias.ringwald }
223808a48abSmatthias.ringwald 
22434e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
22534e7e577SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
22633c40538SMatthias Ringwald     uint8_t event[4];
22733c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
22833c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
22934e7e577SMatthias Ringwald     little_endian_store_16(event, 2, channel);
23033c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23134e7e577SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
23233c40538SMatthias Ringwald }
23333c40538SMatthias Ringwald 
234fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
235ccf076adS[email protected]     uint8_t event[6];
236ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
237ccf076adS[email protected]     event[1] = 4;
238fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
239f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
240ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
24133c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
24233c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
243ccf076adS[email protected] }
244ccf076adS[email protected] 
2457f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
246665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
247665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
248665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
249665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
250b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
251f62db1e3Smatthias.ringwald             return channel;
252f62db1e3Smatthias.ringwald         }
253f62db1e3Smatthias.ringwald     }
254f62db1e3Smatthias.ringwald     return NULL;
255f62db1e3Smatthias.ringwald }
256f62db1e3Smatthias.ringwald 
257e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
258e7d0c9aaSMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){
259e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_t it;
260e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
261e7d0c9aaSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
262e7d0c9aaSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
263e7d0c9aaSMatthias Ringwald         if ( channel->local_cid == local_cid) {
264e7d0c9aaSMatthias Ringwald             return channel;
265e7d0c9aaSMatthias Ringwald         }
266e7d0c9aaSMatthias Ringwald     }
267e7d0c9aaSMatthias Ringwald     return NULL;
268e7d0c9aaSMatthias Ringwald }
269e7d0c9aaSMatthias Ringwald #endif
270e7d0c9aaSMatthias Ringwald 
2715cb87675SMatthias Ringwald static l2cap_channel_t * l2cap_get_le_channel_for_address_and_addr_type(bd_addr_t address, bd_addr_type_t address_type){
2725cb87675SMatthias Ringwald     btstack_linked_list_iterator_t it;
2735cb87675SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
2745cb87675SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2755cb87675SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2765cb87675SMatthias Ringwald         if (channel->address_type != address_type)  continue;
2775cb87675SMatthias Ringwald         if (bd_addr_cmp(channel->address, address)) continue;
2785cb87675SMatthias Ringwald         return channel;
2795cb87675SMatthias Ringwald     }
2805cb87675SMatthias Ringwald     return NULL;
2815cb87675SMatthias Ringwald }
2825cb87675SMatthias Ringwald 
2830b9d7e78SMatthias Ringwald ///
2840b9d7e78SMatthias Ringwald 
28530725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
28630725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
28730725612SMatthias Ringwald     if (!channel) return;
28830725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
28930725612SMatthias Ringwald     l2cap_notify_channel_can_send();
29030725612SMatthias Ringwald }
29130725612SMatthias Ringwald 
2920b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
2930b9d7e78SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
2940b9d7e78SMatthias Ringwald     if (index < 0) return;
2950b9d7e78SMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
2960b9d7e78SMatthias Ringwald     l2cap_notify_channel_can_send();
2970b9d7e78SMatthias Ringwald }
2980b9d7e78SMatthias Ringwald 
2990b9d7e78SMatthias Ringwald ///
3000b9d7e78SMatthias Ringwald 
3016b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
3026b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
3036b1fde37Smatthias.ringwald     if (!channel) return 0;
3040b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
3057856fb31S[email protected] }
3067856fb31S[email protected] 
30783e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
30883e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
30983e7cdd9SMatthias Ringwald     if (!channel) return 0;
3100b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
31183e7cdd9SMatthias Ringwald }
31283e7cdd9SMatthias Ringwald 
313fc64f94aSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
3140b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
3152125de09SMatthias Ringwald }
3160b9d7e78SMatthias Ringwald 
3170b9d7e78SMatthias Ringwald ///
3183cab4fcaS[email protected] 
31996cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
32096cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
32196cbd662Smatthias.ringwald     if (channel) {
32296cbd662Smatthias.ringwald         return channel->remote_mtu;
32396cbd662Smatthias.ringwald     }
32496cbd662Smatthias.ringwald     return 0;
32596cbd662Smatthias.ringwald }
32696cbd662Smatthias.ringwald 
327ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
328665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
329665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
330665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
331665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3325932bd7cS[email protected]         if ( &channel->rtx == ts) {
3335932bd7cS[email protected]             return channel;
3345932bd7cS[email protected]         }
3355932bd7cS[email protected]     }
3365932bd7cS[email protected]     return NULL;
3375932bd7cS[email protected] }
3385932bd7cS[email protected] 
339ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3405932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
34195d2c8f4SMatthias Ringwald     if (!channel) return;
3425932bd7cS[email protected] 
3435932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3445932bd7cS[email protected] 
3455932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3465932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3475932bd7cS[email protected]     // notify client
3485932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3495932bd7cS[email protected] 
3505932bd7cS[email protected]     // discard channel
3519dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
352665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3535932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3545932bd7cS[email protected] }
3555932bd7cS[email protected] 
3565932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3575932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
358528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3595932bd7cS[email protected] }
3605932bd7cS[email protected] 
3615932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3625932bd7cS[email protected]     l2cap_stop_rtx(channel);
363cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
364528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
365528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
366528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3675932bd7cS[email protected] }
3685932bd7cS[email protected] 
3695932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3705932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3715932bd7cS[email protected]     l2cap_stop_rtx(channel);
372528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
373528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
374528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3755932bd7cS[email protected] }
3765932bd7cS[email protected] 
37771de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
378ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
379ac301f95S[email protected] }
380ac301f95S[email protected] 
381df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
382ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
383df3354fcS[email protected] }
3845932bd7cS[email protected] 
3857f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
386b1d43497Smatthias.ringwald 
387a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3889da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
389b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
390b1d43497Smatthias.ringwald     }
391b1d43497Smatthias.ringwald 
3929da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3932a373862S[email protected]     hci_reserve_packet_buffer();
394facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
39558de5610Smatthias.ringwald     va_list argptr;
39658de5610Smatthias.ringwald     va_start(argptr, identifier);
39770efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
39858de5610Smatthias.ringwald     va_end(argptr);
3999da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
400826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
40158de5610Smatthias.ringwald }
40258de5610Smatthias.ringwald 
403a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
4047f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
40570efece1S[email protected] 
406a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4079da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
40870efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
40970efece1S[email protected]     }
41070efece1S[email protected] 
4119da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
4122a373862S[email protected]     hci_reserve_packet_buffer();
413facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
41470efece1S[email protected]     va_list argptr;
41570efece1S[email protected]     va_start(argptr, identifier);
41670efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
41770efece1S[email protected]     va_end(argptr);
4189da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
419826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
42070efece1S[email protected] }
42170efece1S[email protected] #endif
42270efece1S[email protected] 
423b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
424facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
425b1d43497Smatthias.ringwald }
4266218e6f1Smatthias.ringwald 
4276b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
4286b4af23dS[email protected]     return hci_reserve_packet_buffer();
4296b4af23dS[email protected] }
4306b4af23dS[email protected] 
43168a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
43268a0fcf7S[email protected]     hci_release_packet_buffer();
43368a0fcf7S[email protected] }
43468a0fcf7S[email protected] 
4357f107edaSMatthias Ringwald static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint16_t remote_cid, uint16_t len){
4367f107edaSMatthias Ringwald 
4377f107edaSMatthias Ringwald     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
4387f107edaSMatthias Ringwald 
4397f107edaSMatthias Ringwald     // 0 - Connection handle : PB=pb : BC=00
4407f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
4417f107edaSMatthias Ringwald     // 2 - ACL length
4427f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4437f107edaSMatthias Ringwald     // 4 - L2CAP packet length
4447f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4457f107edaSMatthias Ringwald     // 6 - L2CAP channel DEST
4467f107edaSMatthias Ringwald     little_endian_store_16(acl_buffer, 6,  remote_cid);
4477f107edaSMatthias Ringwald }
44868a0fcf7S[email protected] 
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 
475fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
4762149f12eSmatthias.ringwald 
477c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
478c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4792149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4802149f12eSmatthias.ringwald     }
4812149f12eSmatthias.ringwald 
482fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
483fc64f94aSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
484c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
485c8b9416aS[email protected]     }
486c8b9416aS[email protected] 
487fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
4882149f12eSmatthias.ringwald 
489facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4907f107edaSMatthias Ringwald     l2cap_setup_header(acl_buffer, con_handle, cid, len);
4912149f12eSmatthias.ringwald     // send
4927f107edaSMatthias Ringwald     return hci_send_acl_packet_buffer(len+8);
4932149f12eSmatthias.ringwald }
4942149f12eSmatthias.ringwald 
495ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
496b1d43497Smatthias.ringwald 
497a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
498a35252c8S[email protected]     if (!channel) {
499ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
500a35252c8S[email protected]         return -1;   // TODO: define error
501a35252c8S[email protected]     }
502a35252c8S[email protected] 
503f0efaa57S[email protected]     if (len > channel->remote_mtu){
504ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
505f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
506f0efaa57S[email protected]     }
507f0efaa57S[email protected] 
508fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
509ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
510b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
511b1d43497Smatthias.ringwald     }
512b1d43497Smatthias.ringwald 
5132a373862S[email protected]     hci_reserve_packet_buffer();
514facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
515b1d43497Smatthias.ringwald 
516b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
517b1d43497Smatthias.ringwald 
518b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
519b1d43497Smatthias.ringwald }
520b1d43497Smatthias.ringwald 
521fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
5222149f12eSmatthias.ringwald 
523fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
524ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
5252149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
5262149f12eSmatthias.ringwald     }
5272149f12eSmatthias.ringwald 
5282a373862S[email protected]     hci_reserve_packet_buffer();
529facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
5302149f12eSmatthias.ringwald 
5312149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
5322149f12eSmatthias.ringwald 
533fc64f94aSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
5342149f12eSmatthias.ringwald }
5352149f12eSmatthias.ringwald 
536fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
537fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5380e37e417S[email protected] }
5390e37e417S[email protected] 
54028ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
54128ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
54228ca2b46S[email protected] }
54328ca2b46S[email protected] 
54428ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
54528ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
54628ca2b46S[email protected] }
54728ca2b46S[email protected] 
54828ca2b46S[email protected] 
549b1d43497Smatthias.ringwald 
5508158c421Smatthias.ringwald // MARK: L2CAP_RUN
5512cd0be45Smatthias.ringwald // process outstanding signaling tasks
5527f02f414SMatthias Ringwald static void l2cap_run(void){
5532b83fb7dSmatthias.ringwald 
55422c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
55522c29ab4SMatthias Ringwald 
5562b83fb7dSmatthias.ringwald     // check pending signaling responses
5572b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5582b83fb7dSmatthias.ringwald 
5592b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
560a35252c8S[email protected] 
561a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
562a35252c8S[email protected] 
5632b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5642b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
56563a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
566f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5672b83fb7dSmatthias.ringwald 
568f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
569f53da564S[email protected]         signaling_responses_pending--;
570f53da564S[email protected]         int i;
571f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
572f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
573f53da564S[email protected]         }
574f53da564S[email protected] 
575f53da564S[email protected]         switch (response_code){
5762b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5772b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5782bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5794d816277S[email protected]                 if (result == 0x0003){
5802bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5814d816277S[email protected]                 }
5822b360848Smatthias.ringwald                 break;
5832b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5842b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5852b83fb7dSmatthias.ringwald                 break;
5862b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5873b0484b3S[email protected]                 switch (infoType){
5883b0484b3S[email protected]                     case 1: { // Connectionless MTU
5893b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5903b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5913b0484b3S[email protected]                         break;
5923b0484b3S[email protected]                     }
5933b0484b3S[email protected]                     case 2: { // Extended Features Supported
594462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
595462e630dS[email protected]                         uint32_t features = 0x280;
5963b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5973b0484b3S[email protected]                         break;
5983b0484b3S[email protected]                     }
5993b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
6003b0484b3S[email protected]                         uint8_t map[8];
6013b0484b3S[email protected]                         memset(map, 0, 8);
602288636a2SMatthias Ringwald                         map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
6033b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
6043b0484b3S[email protected]                         break;
6053b0484b3S[email protected]                     }
6063b0484b3S[email protected]                     default:
6072b83fb7dSmatthias.ringwald                         // all other types are not supported
6082b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
6093b0484b3S[email protected]                         break;
6102b83fb7dSmatthias.ringwald                 }
6112b83fb7dSmatthias.ringwald                 break;
61263a7246aSmatthias.ringwald             case COMMAND_REJECT:
6135ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
614a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
61570efece1S[email protected]             case COMMAND_REJECT_LE:
61670efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
61763a7246aSmatthias.ringwald                 break;
61870efece1S[email protected] #endif
6192b83fb7dSmatthias.ringwald             default:
6202b83fb7dSmatthias.ringwald                 // should not happen
6212b83fb7dSmatthias.ringwald                 break;
6222b83fb7dSmatthias.ringwald         }
6232b83fb7dSmatthias.ringwald     }
6242b83fb7dSmatthias.ringwald 
625ae280e73Smatthias.ringwald     uint8_t  config_options[4];
626665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
627665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
628665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
629baf94f06S[email protected] 
630665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
63122c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6322cd0be45Smatthias.ringwald         switch (channel->state){
6332cd0be45Smatthias.ringwald 
634df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
635ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
636fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
637a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
638ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
639fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
640ad671560S[email protected]                 }
641ad671560S[email protected]                 break;
642ad671560S[email protected] 
64302b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
644baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
64564472d52Smatthias.ringwald                 // send connection request - set state first
64664472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
64702b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6488f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
64902b22dc4Smatthias.ringwald                 break;
65002b22dc4Smatthias.ringwald 
651e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
652fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
65322c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
654fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
655e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6569dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
657665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
658d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
659e7ff783cSmatthias.ringwald                 break;
660e7ff783cSmatthias.ringwald 
661552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
662fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
663fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
66428ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
665fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
666552d92a1Smatthias.ringwald                 break;
667552d92a1Smatthias.ringwald 
6686fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
669fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6706fdcc387Smatthias.ringwald                 // success, start l2cap handshake
671b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6726fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
673fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6745932bd7cS[email protected]                 l2cap_start_rtx(channel);
6756fdcc387Smatthias.ringwald                 break;
6766fdcc387Smatthias.ringwald 
677fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
678fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
67973cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
68063a7246aSmatthias.ringwald                     uint16_t flags = 0;
68128ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
68263a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
68363a7246aSmatthias.ringwald                         flags = 1;
68463a7246aSmatthias.ringwald                     } else {
68528ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
68663a7246aSmatthias.ringwald                     }
68763a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
688fc64f94aSMatthias 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);
68963a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
69063a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
69163a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
692f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
693fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
69463a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
69563a7246aSmatthias.ringwald                     } else {
696fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
69763a7246aSmatthias.ringwald                     }
69863a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
699fa8473a4Smatthias.ringwald                 }
70073cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
70128ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
70228ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
703b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
704ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
705ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
706f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
707fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
7085932bd7cS[email protected]                     l2cap_start_rtx(channel);
709fa8473a4Smatthias.ringwald                 }
710fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
711552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
712552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
713fa8473a4Smatthias.ringwald                 }
714552d92a1Smatthias.ringwald                 break;
715552d92a1Smatthias.ringwald 
716e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
717fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
71822c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
719fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7205932bd7cS[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 :)
721756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
722e7ff783cSmatthias.ringwald                 break;
723e7ff783cSmatthias.ringwald 
724e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
725fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
726b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7272cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
728fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7292cd0be45Smatthias.ringwald                 break;
7302cd0be45Smatthias.ringwald             default:
7312cd0be45Smatthias.ringwald                 break;
7322cd0be45Smatthias.ringwald         }
7332cd0be45Smatthias.ringwald     }
734da886c03S[email protected] 
735a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
736efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
737efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7387f107edaSMatthias Ringwald         uint8_t  * acl_buffer;
7397f107edaSMatthias Ringwald         uint8_t  * l2cap_payload;
7407f107edaSMatthias Ringwald         uint16_t pos;
7417f107edaSMatthias Ringwald         uint16_t payload_size;
742efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
743efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
744efedfb4cSMatthias Ringwald         switch (channel->state){
7455cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7465cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7475cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
7485cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
7491b8b8d05SMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
750cd529728SMatthias 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, 1);
7515cb87675SMatthias Ringwald                 break;
75223017473SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
75323017473SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
75423017473SMatthias Ringwald                 // TODO: support larger MPS
75523017473SMatthias Ringwald                 channel->state = L2CAP_STATE_OPEN;
75623017473SMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, channel->local_mtu, 23, channel->credits_incoming, 0);
75764e11ca9SMatthias Ringwald                 // notify client
75864e11ca9SMatthias Ringwald                 l2cap_emit_channel_opened(channel, 0);
75923017473SMatthias Ringwald                 break;
760e7d0c9aaSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
761e7d0c9aaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
762e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
763e7d0c9aaSMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, channel->reason);
764e7d0c9aaSMatthias Ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
765e7d0c9aaSMatthias Ringwald                 l2cap_stop_rtx(channel);
766e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
767e7d0c9aaSMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
768e7d0c9aaSMatthias Ringwald                 break;
7697f107edaSMatthias Ringwald             case L2CAP_STATE_OPEN:
770*85aeef60SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
771*85aeef60SMatthias Ringwald 
772*85aeef60SMatthias Ringwald                 // send credits
773*85aeef60SMatthias Ringwald                 if (channel->new_credits_incoming){
774*85aeef60SMatthias Ringwald                     log_info("l2cap: sending %u credits", channel->new_credits_incoming);
775*85aeef60SMatthias Ringwald                     channel->local_sig_id = l2cap_next_sig_id();
776*85aeef60SMatthias Ringwald                     uint16_t new_credits = channel->new_credits_incoming;
777*85aeef60SMatthias Ringwald                     channel->new_credits_incoming = 0;
778*85aeef60SMatthias Ringwald                     channel->credits_incoming += new_credits;
779*85aeef60SMatthias Ringwald                     l2cap_send_le_signaling_packet(channel->con_handle, LE_FLOW_CONTROL_CREDIT, channel->local_sig_id, channel->remote_cid, new_credits);
780*85aeef60SMatthias Ringwald                     break;
781*85aeef60SMatthias Ringwald                 }
782*85aeef60SMatthias Ringwald 
783*85aeef60SMatthias Ringwald                 // send data
7847f107edaSMatthias Ringwald                 if (!channel->send_sdu_buffer) break;
7857f107edaSMatthias Ringwald                 if (!channel->credits_outgoing) break;
786*85aeef60SMatthias Ringwald 
7877f107edaSMatthias Ringwald                 // send part of SDU
7887f107edaSMatthias Ringwald                 hci_reserve_packet_buffer();
7897f107edaSMatthias Ringwald                 acl_buffer = hci_get_outgoing_packet_buffer();
7907f107edaSMatthias Ringwald                 l2cap_payload = acl_buffer + 8;
7917f107edaSMatthias Ringwald                 pos = 0;
7927f107edaSMatthias Ringwald                 if (!channel->send_sdu_pos){
7937f107edaSMatthias Ringwald                     // store SDU len
7947f107edaSMatthias Ringwald                     channel->send_sdu_pos += 2;
7957f107edaSMatthias Ringwald                     little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len);
7967f107edaSMatthias Ringwald                     pos += 2;
7977f107edaSMatthias Ringwald                 }
7987f107edaSMatthias Ringwald                 payload_size = btstack_min(channel->send_sdu_len + 2 - channel->send_sdu_pos, channel->remote_mps - pos);
799*85aeef60SMatthias Ringwald                 log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size, channel->credits_outgoing);
8007f107edaSMatthias Ringwald                 memcpy(&l2cap_payload[pos], &channel->send_sdu_buffer[channel->send_sdu_pos-2], payload_size); // -2 for virtual SDU len
8017f107edaSMatthias Ringwald                 pos += payload_size;
8027f107edaSMatthias Ringwald                 channel->send_sdu_pos += payload_size;
8037f107edaSMatthias Ringwald                 l2cap_setup_header(acl_buffer, channel->con_handle, channel->remote_cid, payload_size);
8047f107edaSMatthias Ringwald                 // done
8057f107edaSMatthias Ringwald 
806*85aeef60SMatthias Ringwald                 channel->credits_outgoing--;
8077f107edaSMatthias Ringwald 
8087f107edaSMatthias Ringwald                 if (channel->send_sdu_pos >= channel->send_sdu_len + 2){
8097f107edaSMatthias Ringwald                     channel->send_sdu_buffer = NULL;
8107f107edaSMatthias Ringwald                     // TODO: send done event
8117f107edaSMatthias Ringwald                 }
8127f107edaSMatthias Ringwald                 hci_send_acl_packet_buffer(8 + pos);
8137f107edaSMatthias Ringwald                 break;
814828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
815828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
816828a7f7aSMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
817828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
818828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
819828a7f7aSMatthias Ringwald                 break;
820828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
821828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
822828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
823828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
824828a7f7aSMatthias Ringwald                 l2cap_le_finialize_channel_close(channel);  // -- remove from list
825828a7f7aSMatthias Ringwald                 break;
826efedfb4cSMatthias Ringwald             default:
827efedfb4cSMatthias Ringwald                 break;
828efedfb4cSMatthias Ringwald         }
829efedfb4cSMatthias Ringwald     }
830efedfb4cSMatthias Ringwald 
831da886c03S[email protected]     // send l2cap con paramter update if necessary
832da886c03S[email protected]     hci_connections_get_iterator(&it);
833665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
834665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
8355d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
836b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
837da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
838b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
839b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
840b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
841b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
842b68d7bc3SMatthias Ringwald                 break;
843da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
844b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
845b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
846da886c03S[email protected]                 break;
847da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
848b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
849b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
850da886c03S[email protected]                 break;
851da886c03S[email protected]             default:
852da886c03S[email protected]                 break;
853da886c03S[email protected]         }
854da886c03S[email protected]     }
8554d7157c3S[email protected] #endif
856da886c03S[email protected] 
85722c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
8582cd0be45Smatthias.ringwald }
8592cd0be45Smatthias.ringwald 
8604aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
8614ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
862fa8c92f6Smatthias.ringwald }
863fa8c92f6Smatthias.ringwald 
86471de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
8654ff786cfS[email protected]     return l2cap_max_mtu();
866e5e1518dS[email protected] }
867e5e1518dS[email protected] 
868fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
8692df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
8705533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
8712df5dadcS[email protected]         // success, start l2cap handshake
872fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
8732df5dadcS[email protected]         // check remote SSP feature first
8742df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
8752df5dadcS[email protected]     }
8762df5dadcS[email protected] }
8772df5dadcS[email protected] 
878efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE
8795cb87675SMatthias Ringwald static void l2cap_handle_le_connection_complete(l2cap_channel_t * channel, uint8_t status, hci_con_handle_t con_handle){
880efedfb4cSMatthias Ringwald     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
881efedfb4cSMatthias Ringwald         log_info("l2cap_le_handle_connection_complete expected state");
8825cb87675SMatthias Ringwald         // TODO: bail if status != 0
8835cb87675SMatthias Ringwald 
884efedfb4cSMatthias Ringwald         // success, start l2cap handshake
885efedfb4cSMatthias Ringwald         channel->con_handle = con_handle;
8865cb87675SMatthias Ringwald         channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
887efedfb4cSMatthias Ringwald     }
888efedfb4cSMatthias Ringwald }
889efedfb4cSMatthias Ringwald #endif
890efedfb4cSMatthias Ringwald 
891efedfb4cSMatthias Ringwald 
8922df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
8932df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
8942df5dadcS[email protected] 
8952df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
896ac301f95S[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));
897fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
8982df5dadcS[email protected]         // request security level 2
8992df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
900fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
9012df5dadcS[email protected]         return;
9022df5dadcS[email protected]     }
9032df5dadcS[email protected]     // fine, go ahead
9042df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
9052df5dadcS[email protected] }
9062df5dadcS[email protected] 
907da144af5SMatthias 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,
908da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
909da144af5SMatthias Ringwald 
910da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
911da144af5SMatthias Ringwald     if (!channel) {
912da144af5SMatthias Ringwald         return NULL;
913da144af5SMatthias Ringwald     }
914da144af5SMatthias Ringwald 
915da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
916da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
917da144af5SMatthias Ringwald 
918da144af5SMatthias Ringwald     // fill in
919da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
920da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
921da144af5SMatthias Ringwald     channel->address_type = address_type;
922da144af5SMatthias Ringwald     channel->psm = psm;
923da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
924da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
925da144af5SMatthias Ringwald     channel->required_security_level = security_level;
926da144af5SMatthias Ringwald 
927da144af5SMatthias Ringwald     //
928da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
929da144af5SMatthias Ringwald     channel->con_handle = 0;
930da144af5SMatthias Ringwald 
931da144af5SMatthias Ringwald     // set initial state
932da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
933da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
934da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
935da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
936da144af5SMatthias Ringwald     return channel;
937da144af5SMatthias Ringwald }
938da144af5SMatthias Ringwald 
9399077cb15SMatthias Ringwald /**
9409077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
9419077cb15SMatthias Ringwald  * @param packet_handler
9429077cb15SMatthias Ringwald  * @param address
9439077cb15SMatthias Ringwald  * @param psm
9449077cb15SMatthias Ringwald  * @param mtu
9459077cb15SMatthias Ringwald  * @param local_cid
9469077cb15SMatthias Ringwald  */
9479077cb15SMatthias Ringwald 
948da144af5SMatthias 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){
949da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
950da144af5SMatthias Ringwald 
951da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
952da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
953da144af5SMatthias Ringwald     }
954da144af5SMatthias Ringwald 
955da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
956fc64f94aSMatthias Ringwald     if (!channel) {
9579077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
9589077cb15SMatthias Ringwald     }
9599077cb15SMatthias Ringwald 
9609077cb15SMatthias Ringwald     // add to connections list
961fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
9629077cb15SMatthias Ringwald 
9639077cb15SMatthias Ringwald     // store local_cid
9649077cb15SMatthias Ringwald     if (out_local_cid){
965fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
9669077cb15SMatthias Ringwald     }
9679077cb15SMatthias Ringwald 
9689077cb15SMatthias Ringwald     // check if hci connection is already usable
9699077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
9709077cb15SMatthias Ringwald     if (conn){
971add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
972fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
9739077cb15SMatthias Ringwald         // check if remote supported fearures are already received
9749077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
975fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
9769077cb15SMatthias Ringwald         }
9779077cb15SMatthias Ringwald     }
9789077cb15SMatthias Ringwald 
9799077cb15SMatthias Ringwald     l2cap_run();
9809077cb15SMatthias Ringwald 
9819077cb15SMatthias Ringwald     return 0;
9829077cb15SMatthias Ringwald }
9839077cb15SMatthias Ringwald 
984ce8f182eSMatthias Ringwald void
985ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
986e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
987b35f641cSmatthias.ringwald     // find channel for local_cid
988b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
989f62db1e3Smatthias.ringwald     if (channel) {
990e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
991f62db1e3Smatthias.ringwald     }
9922cd0be45Smatthias.ringwald     // process
9932cd0be45Smatthias.ringwald     l2cap_run();
99443625864Smatthias.ringwald }
9951e6aba47Smatthias.ringwald 
996afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
997665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
998665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
999665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1000665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1001058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
1002c22aecc9S[email protected]         // channel for this address found
1003c22aecc9S[email protected]         switch (channel->state){
1004c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
1005c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
1006afde0c52Smatthias.ringwald                 // failure, forward error code
1007afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
1008afde0c52Smatthias.ringwald                 // discard channel
10099dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1010665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1011d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
1012c22aecc9S[email protected]                 break;
1013c22aecc9S[email protected]             default:
1014c22aecc9S[email protected]                 break;
1015afde0c52Smatthias.ringwald         }
1016afde0c52Smatthias.ringwald     }
1017afde0c52Smatthias.ringwald }
1018afde0c52Smatthias.ringwald 
1019afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
1020665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1021665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1022665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1023665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1024058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
10252df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
1026afde0c52Smatthias.ringwald         }
1027afde0c52Smatthias.ringwald     }
10286fdcc387Smatthias.ringwald     // process
10296fdcc387Smatthias.ringwald     l2cap_run();
1030afde0c52Smatthias.ringwald }
1031b448a0e7Smatthias.ringwald 
103233c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
103333c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
103433c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
103533c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
103633c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
103733c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
1038fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
103933c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
104034e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
104133c40538SMatthias Ringwald     }
10422125de09SMatthias Ringwald 
10432125de09SMatthias Ringwald     int i;
10442125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
1045773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
10462125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
104734e7e577SMatthias Ringwald         int can_send;
104834e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
104934e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
105034e7e577SMatthias Ringwald         } else {
105134e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
105234e7e577SMatthias Ringwald         }
105334e7e577SMatthias Ringwald         if (!can_send) continue;
105434e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
105534e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
10562125de09SMatthias Ringwald     }
105733c40538SMatthias Ringwald }
105833c40538SMatthias Ringwald 
1059d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
1060afde0c52Smatthias.ringwald 
1061afde0c52Smatthias.ringwald     bd_addr_t address;
1062afde0c52Smatthias.ringwald     hci_con_handle_t handle;
1063665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
10642d00edd4Smatthias.ringwald     int hci_con_used;
1065afde0c52Smatthias.ringwald 
10660e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1067afde0c52Smatthias.ringwald 
1068afde0c52Smatthias.ringwald         // handle connection complete events
1069afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1070724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
1071afde0c52Smatthias.ringwald             if (packet[2] == 0){
1072f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
1073afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
1074afde0c52Smatthias.ringwald             } else {
1075afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
1076afde0c52Smatthias.ringwald             }
1077afde0c52Smatthias.ringwald             break;
1078afde0c52Smatthias.ringwald 
1079afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1080afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1081073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1082afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1083724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1084afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1085afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
108603cfbabcSmatthias.ringwald                 }
10871e6aba47Smatthias.ringwald             }
108839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
108939d59809Smatthias.ringwald             break;
109039d59809Smatthias.ringwald 
109139d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
109239d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
1093afde0c52Smatthias.ringwald             break;
109427a923d0Smatthias.ringwald 
10951e6aba47Smatthias.ringwald         // handle disconnection complete events
1096afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1097c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
1098f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1099665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1100665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1101665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1102fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
110315ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
11049dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1105665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1106d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
110727a923d0Smatthias.ringwald             }
1108991fea48SMatthias Ringwald #ifdef ENABLE_BLE
1109991fea48SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1110991fea48SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1111991fea48SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1112991fea48SMatthias Ringwald                 if (channel->con_handle != handle) continue;
1113991fea48SMatthias Ringwald                 l2cap_emit_channel_closed(channel);
1114991fea48SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1115991fea48SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
1116991fea48SMatthias Ringwald             }
1117991fea48SMatthias Ringwald #endif
1118afde0c52Smatthias.ringwald             break;
1119fcadd0caSmatthias.ringwald 
112033c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
112163fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
11226218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
112302b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
112433c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
11256218e6f1Smatthias.ringwald             break;
11266218e6f1Smatthias.ringwald 
1127ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
1128afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1129f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1130bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
113180ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
11322d00edd4Smatthias.ringwald             hci_con_used = 0;
1133665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1134665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1135665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1136fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11372d00edd4Smatthias.ringwald                 hci_con_used = 1;
1138c22aecc9S[email protected]                 break;
1139ee091cf1Smatthias.ringwald             }
11402d00edd4Smatthias.ringwald             if (hci_con_used) break;
1141d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
11429edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1143afde0c52Smatthias.ringwald             break;
1144ee091cf1Smatthias.ringwald 
1145df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1146f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1147665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1148665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1149665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1150fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11512df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1152df3354fcS[email protected]                 break;
1153df3354fcS[email protected]             }
1154c22aecc9S[email protected]             break;
1155df3354fcS[email protected] 
11565cb87675SMatthias Ringwald #ifdef ENABLE_BLE
11575cb87675SMatthias Ringwald         case HCI_EVENT_LE_META:
11585cb87675SMatthias Ringwald             switch (packet[2]){
11595cb87675SMatthias Ringwald                 int addr_type;
11605cb87675SMatthias Ringwald                 l2cap_channel_t * channel;
11615cb87675SMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
11625cb87675SMatthias Ringwald                     // Connection management
11635cb87675SMatthias Ringwald                     reverse_bd_addr(&packet[8], address);
11645cb87675SMatthias Ringwald                     addr_type = (bd_addr_type_t)packet[7];
11655cb87675SMatthias Ringwald                     log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address));
11665cb87675SMatthias Ringwald                     // get l2cap_channel
11675cb87675SMatthias Ringwald                     // also pass in status
11685cb87675SMatthias Ringwald                     channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type);
11695cb87675SMatthias Ringwald                     if (!channel) break;
11705cb87675SMatthias Ringwald                     l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4));
11715cb87675SMatthias Ringwald                     break;
11725cb87675SMatthias Ringwald                 default:
11735cb87675SMatthias Ringwald                     break;
11745cb87675SMatthias Ringwald             }
11755cb87675SMatthias Ringwald             break;
11765cb87675SMatthias Ringwald #endif
11775cb87675SMatthias Ringwald 
11785611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1179f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1180bd63148eS[email protected]             log_info("l2cap - security level update");
1181665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1182665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1183665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1184fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11855533f01eS[email protected] 
1186bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1187bd63148eS[email protected] 
1188e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
11895533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
11905533f01eS[email protected] 
1191df3354fcS[email protected]                 switch (channel->state){
1192df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
11935533f01eS[email protected]                         if (actual_level >= required_level){
1194f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1195f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
11961eb2563eS[email protected]                         } else {
1197775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
11981eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11991eb2563eS[email protected]                         }
1200df3354fcS[email protected]                         break;
1201df3354fcS[email protected] 
1202df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
12035533f01eS[email protected]                         if (actual_level >= required_level){
1204df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1205df3354fcS[email protected]                         } else {
1206df3354fcS[email protected]                             // disconnnect, authentication not good enough
1207df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1208df3354fcS[email protected]                         }
1209df3354fcS[email protected]                         break;
1210df3354fcS[email protected] 
1211df3354fcS[email protected]                     default:
1212df3354fcS[email protected]                         break;
1213df3354fcS[email protected]                 }
1214f85a9399S[email protected]             }
1215f85a9399S[email protected]             break;
1216f85a9399S[email protected] 
1217afde0c52Smatthias.ringwald         default:
1218afde0c52Smatthias.ringwald             break;
1219afde0c52Smatthias.ringwald     }
1220afde0c52Smatthias.ringwald 
1221bd63148eS[email protected]     l2cap_run();
12221e6aba47Smatthias.ringwald }
12231e6aba47Smatthias.ringwald 
1224afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1225b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1226e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1227e7ff783cSmatthias.ringwald     l2cap_run();
122884836b65Smatthias.ringwald }
122984836b65Smatthias.ringwald 
12302b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
12314cf56b4aSmatthias.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."
12322b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
12332b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
12342b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
12352b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
12362b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
12372b360848Smatthias.ringwald         signaling_responses_pending++;
12382b360848Smatthias.ringwald         l2cap_run();
12392b360848Smatthias.ringwald     }
12402b360848Smatthias.ringwald }
12412b360848Smatthias.ringwald 
1242b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1243645658c9Smatthias.ringwald 
12449da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1245645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1246645658c9Smatthias.ringwald     if (!service) {
1247645658c9Smatthias.ringwald         // 0x0002 PSM not supported
12482b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1249645658c9Smatthias.ringwald         return;
1250645658c9Smatthias.ringwald     }
1251645658c9Smatthias.ringwald 
12525061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1253645658c9Smatthias.ringwald     if (!hci_connection) {
12542b360848Smatthias.ringwald         //
12559da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1256645658c9Smatthias.ringwald         return;
1257645658c9Smatthias.ringwald     }
12582bd8b7e7S[email protected] 
1259645658c9Smatthias.ringwald     // alloc structure
12609da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1261da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1262da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
12632b360848Smatthias.ringwald     if (!channel){
12642b360848Smatthias.ringwald         // 0x0004 No resources available
12652b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
12662b360848Smatthias.ringwald         return;
12672b360848Smatthias.ringwald     }
1268da144af5SMatthias Ringwald 
1269fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1270b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1271b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1272645658c9Smatthias.ringwald 
1273f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
12742985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
12752985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
12769775e25bSmatthias.ringwald     }
12779775e25bSmatthias.ringwald 
1278645658c9Smatthias.ringwald     // set initial state
1279df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1280ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1281e405ae81Smatthias.ringwald 
1282645658c9Smatthias.ringwald     // add to connections list
1283665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1284645658c9Smatthias.ringwald 
1285f85a9399S[email protected]     // assert security requirements
12861eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1287e405ae81Smatthias.ringwald }
1288645658c9Smatthias.ringwald 
1289ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1290e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1291b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1292e405ae81Smatthias.ringwald     if (!channel) {
1293ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1294e405ae81Smatthias.ringwald         return;
1295e405ae81Smatthias.ringwald     }
1296e405ae81Smatthias.ringwald 
1297552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1298e405ae81Smatthias.ringwald 
1299552d92a1Smatthias.ringwald     // process
1300552d92a1Smatthias.ringwald     l2cap_run();
1301e405ae81Smatthias.ringwald }
1302645658c9Smatthias.ringwald 
13037ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
13047ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1305b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1306e405ae81Smatthias.ringwald     if (!channel) {
1307ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1308e405ae81Smatthias.ringwald         return;
1309e405ae81Smatthias.ringwald     }
1310e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
13117ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1312e7ff783cSmatthias.ringwald     l2cap_run();
1313645658c9Smatthias.ringwald }
1314645658c9Smatthias.ringwald 
13157f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1316b1988dceSmatthias.ringwald 
1317b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1318b1988dceSmatthias.ringwald 
1319f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
132063a7246aSmatthias.ringwald     if (flags & 1) {
132163a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
132263a7246aSmatthias.ringwald     }
132363a7246aSmatthias.ringwald 
13242784b77dSmatthias.ringwald     // accept the other's configuration options
1325f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
13263de7c0caSmatthias.ringwald     uint16_t pos     = 8;
13273de7c0caSmatthias.ringwald     while (pos < end_pos){
132863a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
132963a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
133063a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
133163a7246aSmatthias.ringwald         pos++;
13321dc511deSmatthias.ringwald         uint8_t length = command[pos++];
13331dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
133463a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1335f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
13369da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
133763a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
133863a7246aSmatthias.ringwald         }
13390fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
13400fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1341f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
13420fe7a9d0S[email protected]         }
134363a7246aSmatthias.ringwald         // check for unknown options
134463a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1345c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
134663a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
13471dc511deSmatthias.ringwald         }
13481dc511deSmatthias.ringwald         pos += length;
13491dc511deSmatthias.ringwald     }
13502784b77dSmatthias.ringwald }
13512784b77dSmatthias.ringwald 
1352fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
13539da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
135473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
135573cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1356019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1357019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1358fa8473a4Smatthias.ringwald     return 1;
1359fa8473a4Smatthias.ringwald }
1360fa8473a4Smatthias.ringwald 
1361fa8473a4Smatthias.ringwald 
13627f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
13631e6aba47Smatthias.ringwald 
136400d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
136500d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
136638e5900eSmatthias.ringwald     uint16_t result = 0;
13671e6aba47Smatthias.ringwald 
13689da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1369b35f641cSmatthias.ringwald 
13709a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
13719a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
13729a011532Smatthias.ringwald         switch (channel->state){
1373fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
13749a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
13752b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
13769a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
13779a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
13789a011532Smatthias.ringwald                 break;
13799a011532Smatthias.ringwald 
13809a011532Smatthias.ringwald             default:
13819a011532Smatthias.ringwald                 // ignore in other states
13829a011532Smatthias.ringwald                 break;
13839a011532Smatthias.ringwald         }
13849a011532Smatthias.ringwald         return;
13859a011532Smatthias.ringwald     }
13869a011532Smatthias.ringwald 
138756081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
13881e6aba47Smatthias.ringwald     switch (channel->state) {
13891e6aba47Smatthias.ringwald 
13901e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
13911e6aba47Smatthias.ringwald             switch (code){
13921e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
13935932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1394f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
139538e5900eSmatthias.ringwald                     switch (result) {
139638e5900eSmatthias.ringwald                         case 0:
1397169f8b28Smatthias.ringwald                             // successful connection
1398f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1399fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
140028ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
140138e5900eSmatthias.ringwald                             break;
140238e5900eSmatthias.ringwald                         case 1:
14035932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
14045932bd7cS[email protected]                             l2cap_start_ertx(channel);
140538e5900eSmatthias.ringwald                             break;
140638e5900eSmatthias.ringwald                         default:
1407eb920dbeSmatthias.ringwald                             // channel closed
1408eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1409f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
141038e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1411eb920dbeSmatthias.ringwald 
1412eb920dbeSmatthias.ringwald                             // drop link key if security block
1413eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
141415a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1415eb920dbeSmatthias.ringwald                             }
1416eb920dbeSmatthias.ringwald 
1417eb920dbeSmatthias.ringwald                             // discard channel
1418665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1419d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
142038e5900eSmatthias.ringwald                             break;
14211e6aba47Smatthias.ringwald                     }
14221e6aba47Smatthias.ringwald                     break;
142338e5900eSmatthias.ringwald 
142438e5900eSmatthias.ringwald                 default:
14251e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
142638e5900eSmatthias.ringwald                     break;
14271e6aba47Smatthias.ringwald             }
14281e6aba47Smatthias.ringwald             break;
14291e6aba47Smatthias.ringwald 
1430fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1431f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1432ae280e73Smatthias.ringwald             switch (code) {
1433ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
143428ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1435ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
143663a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
143763a7246aSmatthias.ringwald                         // only done if continuation not set
143863a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
143963a7246aSmatthias.ringwald                     }
1440ae280e73Smatthias.ringwald                     break;
14411e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
14425932bd7cS[email protected]                     l2cap_stop_rtx(channel);
14435932bd7cS[email protected]                     switch (result){
14445932bd7cS[email protected]                         case 0: // success
14455932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
14465932bd7cS[email protected]                             break;
14475932bd7cS[email protected]                         case 4: // pending
14485932bd7cS[email protected]                             l2cap_start_ertx(channel);
14495932bd7cS[email protected]                             break;
14505932bd7cS[email protected]                         default:
1451fe9d8984S[email protected]                             // retry on negative result
1452fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1453fe9d8984S[email protected]                             break;
1454fe9d8984S[email protected]                     }
14555a67bd4aSmatthias.ringwald                     break;
14565a67bd4aSmatthias.ringwald                 default:
14575a67bd4aSmatthias.ringwald                     break;
14581e6aba47Smatthias.ringwald             }
1459fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1460fa8473a4Smatthias.ringwald                 // for open:
14615a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1462fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1463c8e4258aSmatthias.ringwald             }
1464c8e4258aSmatthias.ringwald             break;
1465f62db1e3Smatthias.ringwald 
1466f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1467f62db1e3Smatthias.ringwald             switch (code) {
1468f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
146927a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
147027a923d0Smatthias.ringwald                     break;
14715a67bd4aSmatthias.ringwald                 default:
14725a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
14735a67bd4aSmatthias.ringwald                     break;
147427a923d0Smatthias.ringwald             }
147527a923d0Smatthias.ringwald             break;
147684836b65Smatthias.ringwald 
147784836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
147884836b65Smatthias.ringwald             // @TODO handle incoming requests
147984836b65Smatthias.ringwald             break;
148084836b65Smatthias.ringwald 
148184836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
148284836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
148384836b65Smatthias.ringwald             break;
148410642e45Smatthias.ringwald         default:
148510642e45Smatthias.ringwald             break;
148627a923d0Smatthias.ringwald     }
14879da54300S[email protected]     // log_info("new state %u", channel->state);
148827a923d0Smatthias.ringwald }
148927a923d0Smatthias.ringwald 
149000d93d79Smatthias.ringwald 
14917f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
149200d93d79Smatthias.ringwald 
149300d93d79Smatthias.ringwald     // get code, signalind identifier and command len
149400d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
149500d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
149600d93d79Smatthias.ringwald 
149700d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
149800d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
149963a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
150000d93d79Smatthias.ringwald         return;
150100d93d79Smatthias.ringwald     }
150200d93d79Smatthias.ringwald 
150300d93d79Smatthias.ringwald     // general commands without an assigned channel
150400d93d79Smatthias.ringwald     switch(code) {
150500d93d79Smatthias.ringwald 
150600d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1507f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1508f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
150900d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
15102b83fb7dSmatthias.ringwald             return;
151100d93d79Smatthias.ringwald         }
151200d93d79Smatthias.ringwald 
15132b360848Smatthias.ringwald         case ECHO_REQUEST:
15142b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
15152b83fb7dSmatthias.ringwald             return;
151600d93d79Smatthias.ringwald 
151700d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1518f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
15192b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
15202b83fb7dSmatthias.ringwald             return;
152100d93d79Smatthias.ringwald         }
152200d93d79Smatthias.ringwald 
152300d93d79Smatthias.ringwald         default:
152400d93d79Smatthias.ringwald             break;
152500d93d79Smatthias.ringwald     }
152600d93d79Smatthias.ringwald 
152700d93d79Smatthias.ringwald 
152800d93d79Smatthias.ringwald     // Get potential destination CID
1529f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
153000d93d79Smatthias.ringwald 
153100d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1532665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1533665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1534665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1535665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1536fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
153700d93d79Smatthias.ringwald         if (code & 1) {
1538b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1539b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
154000d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15414e32727eSmatthias.ringwald                 break;
154200d93d79Smatthias.ringwald             }
154300d93d79Smatthias.ringwald         } else {
1544b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
154500d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
154600d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15474e32727eSmatthias.ringwald                 break;
154800d93d79Smatthias.ringwald             }
154900d93d79Smatthias.ringwald         }
155000d93d79Smatthias.ringwald     }
155100d93d79Smatthias.ringwald }
155200d93d79Smatthias.ringwald 
1553e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1554c48b2a2cSMatthias Ringwald // @returns valid
1555c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1556e7d0c9aaSMatthias Ringwald     hci_connection_t * connection;
1557c48b2a2cSMatthias Ringwald     uint16_t result;
1558c48b2a2cSMatthias Ringwald     uint8_t  event[10];
1559e7d0c9aaSMatthias Ringwald     uint16_t le_psm;
1560828a7f7aSMatthias Ringwald     uint16_t local_cid;
1561*85aeef60SMatthias Ringwald     uint16_t credits;
1562e7d0c9aaSMatthias Ringwald     l2cap_service_t * service;
15631b8b8d05SMatthias Ringwald     l2cap_channel_t * channel;
15641b8b8d05SMatthias Ringwald     btstack_linked_list_iterator_t it;
156500d93d79Smatthias.ringwald 
15661b8b8d05SMatthias Ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
156723017473SMatthias Ringwald     log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id);
15681b8b8d05SMatthias Ringwald 
15691b8b8d05SMatthias Ringwald     switch (code){
157000d93d79Smatthias.ringwald 
1571c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1572c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1573ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1574ccf076adS[email protected]             break;
1575da886c03S[email protected] 
1576c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1577e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1578da886c03S[email protected]             if (connection){
15796d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
15806d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1581c48b2a2cSMatthias Ringwald                     return 0;
15826d91fb6cSMatthias Ringwald                 }
1583da886c03S[email protected]                 int update_parameter = 1;
1584a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
15854ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1586c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1587c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1588c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1589c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1590da886c03S[email protected] 
1591da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1592da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1593da886c03S[email protected] 
1594da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1595da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1596da886c03S[email protected] 
1597da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1598da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1599da886c03S[email protected] 
1600da886c03S[email protected]                 if (update_parameter){
1601da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1602da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1603da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1604da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1605da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1606da886c03S[email protected]                 } else {
1607da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1608da886c03S[email protected]                 }
1609c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1610da886c03S[email protected]             }
1611da886c03S[email protected] 
161233c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1613c48b2a2cSMatthias Ringwald 
1614c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1615c48b2a2cSMatthias Ringwald             event[1] = 8;
1616c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1617c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
161833c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1619ccf076adS[email protected]             break;
1620c48b2a2cSMatthias Ringwald 
1621e7d0c9aaSMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_REQUEST:
1622e7d0c9aaSMatthias Ringwald 
1623e7d0c9aaSMatthias Ringwald             // get hci connection, bail if not found (must not happen)
1624e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1625e7d0c9aaSMatthias Ringwald             if (!connection) return 0;
1626e7d0c9aaSMatthias Ringwald 
1627e7d0c9aaSMatthias Ringwald             // check if service registered
1628e7d0c9aaSMatthias Ringwald             le_psm  = little_endian_read_16(command, 4);
1629e7d0c9aaSMatthias Ringwald             service = l2cap_le_get_service(le_psm);
1630e7d0c9aaSMatthias Ringwald 
1631e7d0c9aaSMatthias Ringwald             if (service){
1632e7d0c9aaSMatthias Ringwald 
1633e7d0c9aaSMatthias Ringwald                 uint16_t source_cid = little_endian_read_16(command, 6);
1634e7d0c9aaSMatthias Ringwald                 if (source_cid < 0x40){
1635e7d0c9aaSMatthias Ringwald                     // 0x0009 Connection refused - Invalid Source CID
1636e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009);
1637e7d0c9aaSMatthias Ringwald                     return 1;
1638e7d0c9aaSMatthias Ringwald                 }
1639e7d0c9aaSMatthias Ringwald 
1640e7d0c9aaSMatthias Ringwald                 // go through list of channels for this ACL connection and check if we get a match
1641e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1642e7d0c9aaSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it)){
16431b8b8d05SMatthias Ringwald                     l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16441b8b8d05SMatthias Ringwald                     if (a_channel->con_handle != handle) continue;
16451b8b8d05SMatthias Ringwald                     if (a_channel->remote_cid != source_cid) continue;
1646e7d0c9aaSMatthias Ringwald                     // 0x000a Connection refused - Source CID already allocated
1647e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a);
1648e7d0c9aaSMatthias Ringwald                     return 1;
1649e7d0c9aaSMatthias Ringwald                 }
1650e7d0c9aaSMatthias Ringwald 
1651e7d0c9aaSMatthias Ringwald                 // TODO: deal with authentication requirements, errors 0x005 - 000x8
1652e7d0c9aaSMatthias Ringwald 
1653e7d0c9aaSMatthias Ringwald                 // allocate channel
16541b8b8d05SMatthias Ringwald                 channel = l2cap_create_channel_entry(service->packet_handler, connection->address,
1655e7d0c9aaSMatthias Ringwald                     BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
1656e7d0c9aaSMatthias Ringwald                 if (!channel){
1657e7d0c9aaSMatthias Ringwald                     // 0x0004 Connection refused – no resources available
1658e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004);
1659e7d0c9aaSMatthias Ringwald                     return 1;
1660e7d0c9aaSMatthias Ringwald                 }
1661e7d0c9aaSMatthias Ringwald 
1662e7d0c9aaSMatthias Ringwald                 channel->con_handle = handle;
1663e7d0c9aaSMatthias Ringwald                 channel->remote_cid = source_cid;
1664e7d0c9aaSMatthias Ringwald                 channel->remote_sig_id = sig_id;
16657f107edaSMatthias Ringwald                 channel->remote_mtu = little_endian_read_16(command, 8);
16667f107edaSMatthias Ringwald                 channel->remote_mps = little_endian_read_16(command, 10);
16677f107edaSMatthias Ringwald                 channel->credits_outgoing = little_endian_read_16(command, 12);
1668e7d0c9aaSMatthias Ringwald 
1669e7d0c9aaSMatthias Ringwald                 // set initial state
1670e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1671e7d0c9aaSMatthias Ringwald 
1672e7d0c9aaSMatthias Ringwald                 // add to connections list
1673e7d0c9aaSMatthias Ringwald                 btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1674e7d0c9aaSMatthias Ringwald 
1675e7d0c9aaSMatthias Ringwald                 // post connection request event
1676e7d0c9aaSMatthias Ringwald                 l2cap_emit_connection_request(channel);
1677e7d0c9aaSMatthias Ringwald 
1678e7d0c9aaSMatthias Ringwald             } else {
1679e7d0c9aaSMatthias Ringwald                 // Connection refused – LE_PSM not supported
1680e7d0c9aaSMatthias Ringwald                 l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002);
1681e7d0c9aaSMatthias Ringwald             }
1682e7d0c9aaSMatthias Ringwald             break;
16831b8b8d05SMatthias Ringwald 
16841b8b8d05SMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_RESPONSE:
16851b8b8d05SMatthias Ringwald             // Find channel for this sig_id and connection handle
16861b8b8d05SMatthias Ringwald             channel = NULL;
16871b8b8d05SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
16881b8b8d05SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
16891b8b8d05SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16901b8b8d05SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
16911b8b8d05SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
16921b8b8d05SMatthias Ringwald                 channel = a_channel;
16931b8b8d05SMatthias Ringwald                 break;
16941b8b8d05SMatthias Ringwald             }
16951b8b8d05SMatthias Ringwald             if (!channel) break;
16961b8b8d05SMatthias Ringwald 
16971b8b8d05SMatthias Ringwald             // cid + 0
16981b8b8d05SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
16991b8b8d05SMatthias Ringwald             if (result){
17001b8b8d05SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
17011b8b8d05SMatthias Ringwald                 // map l2cap connection response result to BTstack status enumeration
17021b8b8d05SMatthias Ringwald                 l2cap_emit_channel_opened(channel, result);
17031b8b8d05SMatthias Ringwald 
17041b8b8d05SMatthias Ringwald                 // discard channel
17051b8b8d05SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
17061b8b8d05SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
17071b8b8d05SMatthias Ringwald                 break;
17081b8b8d05SMatthias Ringwald             }
17091b8b8d05SMatthias Ringwald 
17101b8b8d05SMatthias Ringwald             // success
17111b8b8d05SMatthias Ringwald             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
17121b8b8d05SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
17131b8b8d05SMatthias Ringwald             channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
17141b8b8d05SMatthias Ringwald             channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
17151b8b8d05SMatthias Ringwald             channel->state = L2CAP_STATE_OPEN;
17161b8b8d05SMatthias Ringwald             l2cap_emit_channel_opened(channel, result);
17171b8b8d05SMatthias Ringwald             break;
17181b8b8d05SMatthias Ringwald 
1719*85aeef60SMatthias Ringwald         case LE_FLOW_CONTROL_CREDIT:
1720*85aeef60SMatthias Ringwald             // find channel
1721*85aeef60SMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
1722*85aeef60SMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
1723*85aeef60SMatthias Ringwald             if (!channel) break;
1724*85aeef60SMatthias Ringwald             credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
1725*85aeef60SMatthias Ringwald             channel->credits_outgoing += credits;
1726*85aeef60SMatthias Ringwald             log_info("l2cap: %u credits for 0x%02x", credits, local_cid);
1727*85aeef60SMatthias Ringwald             break;
1728*85aeef60SMatthias Ringwald 
1729828a7f7aSMatthias Ringwald         case DISCONNECTION_REQUEST:
1730828a7f7aSMatthias Ringwald             // find channel
1731828a7f7aSMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
1732828a7f7aSMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
1733828a7f7aSMatthias Ringwald             if (!channel) break;
1734828a7f7aSMatthias Ringwald             channel->remote_sig_id = sig_id;
1735828a7f7aSMatthias Ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1736828a7f7aSMatthias Ringwald             break;
1737828a7f7aSMatthias Ringwald 
1738c48b2a2cSMatthias Ringwald         default:
1739c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1740c48b2a2cSMatthias Ringwald             return 0;
1741ccf076adS[email protected]     }
1742c48b2a2cSMatthias Ringwald     return 1;
1743c48b2a2cSMatthias Ringwald }
1744e7d0c9aaSMatthias Ringwald #endif
1745c48b2a2cSMatthias Ringwald 
1746c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
1747c48b2a2cSMatthias Ringwald 
1748c48b2a2cSMatthias Ringwald     // Get Channel ID
1749c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1750c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1751c48b2a2cSMatthias Ringwald 
1752c48b2a2cSMatthias Ringwald     switch (channel_id) {
1753c48b2a2cSMatthias Ringwald 
1754c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1755c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1756c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1757c48b2a2cSMatthias Ringwald                 // handle signaling commands
1758c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1759c48b2a2cSMatthias Ringwald 
1760c48b2a2cSMatthias Ringwald                 // increment command_offset
1761c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1762c48b2a2cSMatthias Ringwald             }
1763c48b2a2cSMatthias Ringwald             break;
1764c48b2a2cSMatthias Ringwald         }
1765c48b2a2cSMatthias Ringwald 
1766e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1767e7d0c9aaSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE: {
1768e7d0c9aaSMatthias Ringwald             uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1769e7d0c9aaSMatthias Ringwald             int      valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1770c48b2a2cSMatthias Ringwald             if (!valid){
17711bbc0b23S[email protected]                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
1772ccf076adS[email protected]             }
1773ccf076adS[email protected]             break;
1774e7d0c9aaSMatthias Ringwald         }
1775e7d0c9aaSMatthias Ringwald #endif
1776c48b2a2cSMatthias Ringwald 
1777c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1778c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1779c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1780ccf076adS[email protected]             }
1781c48b2a2cSMatthias Ringwald             break;
1782c48b2a2cSMatthias Ringwald 
1783c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1784c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1785c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1786c48b2a2cSMatthias Ringwald             }
1787c48b2a2cSMatthias Ringwald             break;
1788c48b2a2cSMatthias Ringwald 
1789c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1790c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1791c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1792c48b2a2cSMatthias Ringwald             }
1793c48b2a2cSMatthias Ringwald             break;
17941bbc0b23S[email protected] 
17955652b5ffS[email protected]         default: {
179600d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
179764e11ca9SMatthias Ringwald             l2cap_channel_t * l2cap_channel;
179864e11ca9SMatthias Ringwald             l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1799d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
18003d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
180100d93d79Smatthias.ringwald             }
180264e11ca9SMatthias Ringwald #ifdef ENABLE_BLE
180364e11ca9SMatthias Ringwald             l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id);
180464e11ca9SMatthias Ringwald             if (l2cap_channel) {
1805*85aeef60SMatthias Ringwald                 // credit counting
1806*85aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming == 0){
1807*85aeef60SMatthias Ringwald                     log_error("LE Data Channel packet received but no incoming credits");
1808*85aeef60SMatthias Ringwald                     l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
1809*85aeef60SMatthias Ringwald                     break;
1810*85aeef60SMatthias Ringwald                 }
1811*85aeef60SMatthias Ringwald                 l2cap_channel->credits_incoming--;
1812*85aeef60SMatthias Ringwald 
1813*85aeef60SMatthias Ringwald                 // automatic credits
1814*85aeef60SMatthias Ringwald                 if (l2cap_channel->credits_incoming < L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_WATERMARK && l2cap_channel->automatic_credits){
1815*85aeef60SMatthias Ringwald                     l2cap_channel->new_credits_incoming = L2CAP_LE_DATA_CHANNELS_AUTOMATIC_CREDITS_INCREMENT;
1816*85aeef60SMatthias Ringwald                 }
1817*85aeef60SMatthias Ringwald 
1818cd529728SMatthias Ringwald                 // first fragment
1819cd529728SMatthias Ringwald                 uint16_t pos = 0;
1820cd529728SMatthias Ringwald                 if (!l2cap_channel->receive_sdu_len){
1821cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
1822cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_pos = 0;
1823cd529728SMatthias Ringwald                     pos  += 2;
1824cd529728SMatthias Ringwald                     size -= 2;
1825cd529728SMatthias Ringwald                 }
1826cd529728SMatthias Ringwald                 memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos], &packet[COMPLETE_L2CAP_HEADER+pos], size-COMPLETE_L2CAP_HEADER);
1827cd529728SMatthias Ringwald                 l2cap_channel->receive_sdu_pos += size - COMPLETE_L2CAP_HEADER;
1828cd529728SMatthias Ringwald                 // done?
1829cd529728SMatthias Ringwald                 log_debug("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len);
1830cd529728SMatthias Ringwald                 if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){
1831cd529728SMatthias Ringwald                     l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len);
1832cd529728SMatthias Ringwald                     l2cap_channel->receive_sdu_len = 0;
1833cd529728SMatthias Ringwald                 }
183464e11ca9SMatthias Ringwald             }
183564e11ca9SMatthias Ringwald #endif
18365652b5ffS[email protected]             break;
18375652b5ffS[email protected]         }
18385652b5ffS[email protected]     }
183900d93d79Smatthias.ringwald 
18401eb2563eS[email protected]     l2cap_run();
18412718e2e7Smatthias.ringwald }
184200d93d79Smatthias.ringwald 
184315ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
184427a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1845f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1846f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1847f62db1e3Smatthias.ringwald     // discard channel
18489dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1849665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1850d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1851c8e4258aSmatthias.ringwald }
18521e6aba47Smatthias.ringwald 
1853828a7f7aSMatthias Ringwald #ifdef ENABLE_BLE
1854828a7f7aSMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
1855828a7f7aSMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){
1856828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_CLOSED;
1857828a7f7aSMatthias Ringwald     l2cap_emit_channel_closed(channel);
1858828a7f7aSMatthias Ringwald     // discard channel
1859828a7f7aSMatthias Ringwald     l2cap_stop_rtx(channel);
1860828a7f7aSMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1861828a7f7aSMatthias Ringwald     btstack_memory_l2cap_channel_free(channel);
1862828a7f7aSMatthias Ringwald }
1863828a7f7aSMatthias Ringwald #endif
1864828a7f7aSMatthias Ringwald 
18658f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1866665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1867665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1868665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1869665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
18709d9bbc01Smatthias.ringwald         if ( service->psm == psm){
18719d9bbc01Smatthias.ringwald             return service;
18729d9bbc01Smatthias.ringwald         };
18739d9bbc01Smatthias.ringwald     }
18749d9bbc01Smatthias.ringwald     return NULL;
18759d9bbc01Smatthias.ringwald }
18769d9bbc01Smatthias.ringwald 
18777192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
18787192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
18797192e786SMatthias Ringwald }
18807192e786SMatthias Ringwald 
1881e0abb8e7S[email protected] 
1882be2053a6SMatthias 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){
1883be2053a6SMatthias Ringwald 
1884be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1885e0abb8e7S[email protected] 
18864bb582b6Smatthias.ringwald     // check for alread registered psm
18879d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1888277abc2cSmatthias.ringwald     if (service) {
1889be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1890be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1891277abc2cSmatthias.ringwald     }
18929d9bbc01Smatthias.ringwald 
18934bb582b6Smatthias.ringwald     // alloc structure
1894bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1895277abc2cSmatthias.ringwald     if (!service) {
1896be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1897be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1898277abc2cSmatthias.ringwald     }
18999d9bbc01Smatthias.ringwald 
19009d9bbc01Smatthias.ringwald     // fill in
19019d9bbc01Smatthias.ringwald     service->psm = psm;
19029d9bbc01Smatthias.ringwald     service->mtu = mtu;
190305ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1904df3354fcS[email protected]     service->required_security_level = security_level;
19059d9bbc01Smatthias.ringwald 
19069d9bbc01Smatthias.ringwald     // add to services list
1907665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1908c0e866bfSmatthias.ringwald 
1909c0e866bfSmatthias.ringwald     // enable page scan
191015a95bd5SMatthias Ringwald     gap_connectable_control(1);
19115842b6d9Smatthias.ringwald 
1912be2053a6SMatthias Ringwald     return 0;
19139d9bbc01Smatthias.ringwald }
19149d9bbc01Smatthias.ringwald 
19157e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
1916e0abb8e7S[email protected] 
1917e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1918e0abb8e7S[email protected] 
19199d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
19207e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1921665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1922d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1923c0e866bfSmatthias.ringwald 
1924c0e866bfSmatthias.ringwald     // disable page scan when no services registered
19257e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
192615a95bd5SMatthias Ringwald         gap_connectable_control(0);
19279d9bbc01Smatthias.ringwald     }
19287e8856ebSMatthias Ringwald     return 0;
19297e8856ebSMatthias Ringwald }
19309d9bbc01Smatthias.ringwald 
19315652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
193205ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
19335628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
19345628cf69SMatthias Ringwald     if (index < 0) return;
19355628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
19365652b5ffS[email protected] }
19375652b5ffS[email protected] 
1938a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
19397192e786SMatthias Ringwald 
1940e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
1941e7d0c9aaSMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, le_psm);
19427192e786SMatthias Ringwald }
1943efedfb4cSMatthias Ringwald 
1944da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
19457192e786SMatthias Ringwald 
1946da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
19477192e786SMatthias Ringwald 
19487192e786SMatthias Ringwald     // check for alread registered psm
19497192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
19507192e786SMatthias Ringwald     if (service) {
1951da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
19527192e786SMatthias Ringwald     }
19537192e786SMatthias Ringwald 
19547192e786SMatthias Ringwald     // alloc structure
19557192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
19567192e786SMatthias Ringwald     if (!service) {
19577192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
1958da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
19597192e786SMatthias Ringwald     }
19607192e786SMatthias Ringwald 
19617192e786SMatthias Ringwald     // fill in
19627192e786SMatthias Ringwald     service->psm = psm;
1963da144af5SMatthias Ringwald     service->mtu = 0;
19647192e786SMatthias Ringwald     service->packet_handler = packet_handler;
19657192e786SMatthias Ringwald     service->required_security_level = security_level;
19667192e786SMatthias Ringwald 
19677192e786SMatthias Ringwald     // add to services list
1968665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
19697192e786SMatthias Ringwald 
19707192e786SMatthias Ringwald     // done
1971da144af5SMatthias Ringwald     return 0;
19727192e786SMatthias Ringwald }
19737192e786SMatthias Ringwald 
1974da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
19757192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
19767192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
19779367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1978da144af5SMatthias Ringwald 
1979665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
19807192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
1981da144af5SMatthias Ringwald     return 0;
19827192e786SMatthias Ringwald }
1983da144af5SMatthias Ringwald 
1984da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
1985e7d0c9aaSMatthias Ringwald     // get channel
1986e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
1987e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
1988e7d0c9aaSMatthias Ringwald 
1989e7d0c9aaSMatthias Ringwald     // validate state
1990e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
1991e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1992e7d0c9aaSMatthias Ringwald     }
1993e7d0c9aaSMatthias Ringwald 
1994efedfb4cSMatthias Ringwald     // set state accept connection
199523017473SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
199623017473SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
199723017473SMatthias Ringwald     channel->local_mtu = mtu;
1998*85aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
1999*85aeef60SMatthias Ringwald     channel->automatic_credits  = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
2000*85aeef60SMatthias Ringwald 
2001*85aeef60SMatthias Ringwald     // test
2002*85aeef60SMatthias Ringwald     channel->new_credits_incoming = 1;
2003e7d0c9aaSMatthias Ringwald 
2004e7d0c9aaSMatthias Ringwald     // go
2005e7d0c9aaSMatthias Ringwald     l2cap_run();
2006da144af5SMatthias Ringwald     return 0;
2007da144af5SMatthias Ringwald }
2008da144af5SMatthias Ringwald 
2009da144af5SMatthias Ringwald /**
2010da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
2011da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2012da144af5SMatthias Ringwald  */
2013da144af5SMatthias Ringwald 
2014da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
2015e7d0c9aaSMatthias Ringwald     // get channel
2016e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2017e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2018e7d0c9aaSMatthias Ringwald 
2019e7d0c9aaSMatthias Ringwald     // validate state
2020e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
2021e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2022e7d0c9aaSMatthias Ringwald     }
2023e7d0c9aaSMatthias Ringwald 
2024efedfb4cSMatthias Ringwald     // set state decline connection
2025e7d0c9aaSMatthias Ringwald     channel->state  = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
2026e7d0c9aaSMatthias Ringwald     channel->reason = 0x04; // no resources available
2027e7d0c9aaSMatthias Ringwald     l2cap_run();
2028da144af5SMatthias Ringwald     return 0;
2029da144af5SMatthias Ringwald }
2030da144af5SMatthias Ringwald 
2031da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
2032da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
2033efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
2034efedfb4cSMatthias Ringwald 
2035da144af5SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
2036da144af5SMatthias Ringwald 
2037cd529728SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, security_level);
2038da144af5SMatthias Ringwald     if (!channel) {
2039da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
2040da144af5SMatthias Ringwald     }
2041e7d0c9aaSMatthias Ringwald     log_info("l2cap_le_create_channel %p", channel);
2042da144af5SMatthias Ringwald 
2043da144af5SMatthias Ringwald     // store local_cid
2044da144af5SMatthias Ringwald     if (out_local_cid){
2045da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
2046da144af5SMatthias Ringwald     }
2047da144af5SMatthias Ringwald 
2048cd529728SMatthias Ringwald     // provdide buffer
2049cd529728SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
2050e7d0c9aaSMatthias Ringwald     channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
2051*85aeef60SMatthias Ringwald     channel->new_credits_incoming = initial_credits;
2052*85aeef60SMatthias Ringwald     channel->automatic_credits    = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
2053*85aeef60SMatthias Ringwald 
2054*85aeef60SMatthias Ringwald     // test
2055*85aeef60SMatthias Ringwald     channel->new_credits_incoming = 1;
2056e7d0c9aaSMatthias Ringwald 
2057efedfb4cSMatthias Ringwald     // add to connections list
2058efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
2059efedfb4cSMatthias Ringwald 
2060da144af5SMatthias Ringwald     // check if hci connection is already usable
2061da144af5SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type);
2062efedfb4cSMatthias Ringwald 
2063da144af5SMatthias Ringwald     if (conn){
2064da144af5SMatthias Ringwald         log_info("l2cap_le_create_channel, hci connection already exists");
20655cb87675SMatthias Ringwald         l2cap_handle_le_connection_complete(channel, 0, conn->con_handle);
2066efedfb4cSMatthias Ringwald     } else {
2067efedfb4cSMatthias Ringwald 
2068efedfb4cSMatthias Ringwald         //
2069efedfb4cSMatthias Ringwald         // TODO: start timer
2070efedfb4cSMatthias Ringwald         // ..
2071efedfb4cSMatthias Ringwald 
2072efedfb4cSMatthias Ringwald         // start connection
2073efedfb4cSMatthias Ringwald         uint8_t res = gap_auto_connection_start(address_type, address);
207448af0c56SMatthias Ringwald         if (res){
2075efedfb4cSMatthias Ringwald             // discard channel object
2076e7d0c9aaSMatthias Ringwald             log_info("gap_auto_connection_start failed! 0x%02x", res);
2077e7d0c9aaSMatthias Ringwald             btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
2078efedfb4cSMatthias Ringwald             btstack_memory_l2cap_channel_free(channel);
2079efedfb4cSMatthias Ringwald             return res;
2080da144af5SMatthias Ringwald         }
2081efedfb4cSMatthias Ringwald     }
2082da144af5SMatthias Ringwald     return 0;
2083da144af5SMatthias Ringwald }
2084da144af5SMatthias Ringwald 
2085da144af5SMatthias Ringwald /**
2086da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
2087da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2088da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
2089da144af5SMatthias Ringwald  */
209064e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
2091e7d0c9aaSMatthias Ringwald     // get channel
2092efedfb4cSMatthias Ringwald     // bail if missing
2093efedfb4cSMatthias Ringwald     // check state
2094efedfb4cSMatthias Ringwald     // check incoming credits + credits <= 0xffff
2095efedfb4cSMatthias Ringwald     // set credits_granted
2096efedfb4cSMatthias Ringwald     // l2cap_le_run()
2097da144af5SMatthias Ringwald     return 0;
2098da144af5SMatthias Ringwald }
2099da144af5SMatthias Ringwald 
2100da144af5SMatthias Ringwald /**
2101da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
2102da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2103da144af5SMatthias Ringwald  */
210464e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){
2105efedfb4cSMatthias Ringwald     // check if data can be sent via le at all
2106da144af5SMatthias Ringwald     return 0;
2107da144af5SMatthias Ringwald }
2108da144af5SMatthias Ringwald 
2109da144af5SMatthias Ringwald /**
2110da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
2111da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
2112da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
2113da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2114da144af5SMatthias Ringwald  */
211564e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
2116efedfb4cSMatthias Ringwald     // same as for non-le
2117da144af5SMatthias Ringwald     return 0;
2118da144af5SMatthias Ringwald }
2119da144af5SMatthias Ringwald 
2120da144af5SMatthias Ringwald /**
2121da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
2122da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
2123da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2124da144af5SMatthias Ringwald  * @param data                  data to send
2125da144af5SMatthias Ringwald  * @param size                  data size
2126da144af5SMatthias Ringwald  */
212764e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
212864e11ca9SMatthias Ringwald 
212964e11ca9SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
213064e11ca9SMatthias Ringwald     if (!channel) {
213164e11ca9SMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2132828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
213364e11ca9SMatthias Ringwald     }
213464e11ca9SMatthias Ringwald 
213564e11ca9SMatthias Ringwald     if (len > channel->remote_mtu){
213664e11ca9SMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
213764e11ca9SMatthias Ringwald         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
213864e11ca9SMatthias Ringwald     }
213964e11ca9SMatthias Ringwald 
21407f107edaSMatthias Ringwald     if (channel->send_sdu_buffer){
214164e11ca9SMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
214264e11ca9SMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
214364e11ca9SMatthias Ringwald     }
214464e11ca9SMatthias Ringwald 
21457f107edaSMatthias Ringwald     channel->send_sdu_buffer = data;
21467f107edaSMatthias Ringwald     channel->send_sdu_len    = len;
21477f107edaSMatthias Ringwald     channel->send_sdu_pos    = 0;
214864e11ca9SMatthias Ringwald 
21497f107edaSMatthias Ringwald     l2cap_run();
21507f107edaSMatthias Ringwald     return 0;
2151da144af5SMatthias Ringwald }
2152da144af5SMatthias Ringwald 
2153da144af5SMatthias Ringwald /**
2154da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
2155da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2156da144af5SMatthias Ringwald  */
2157828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid)
2158da144af5SMatthias Ringwald {
2159828a7f7aSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2160828a7f7aSMatthias Ringwald     if (!channel) {
2161828a7f7aSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2162828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2163828a7f7aSMatthias Ringwald     }
2164828a7f7aSMatthias Ringwald 
2165828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
2166828a7f7aSMatthias Ringwald     l2cap_run();
2167da144af5SMatthias Ringwald     return 0;
2168da144af5SMatthias Ringwald }
2169da144af5SMatthias Ringwald 
21707f02f414SMatthias Ringwald #endif
2171