xref: /btstack/src/l2cap.c (revision da144af509d14092d51a6d5e48b2e051fc9b6ea0)
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 
6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6900d93d79Smatthias.ringwald 
705628cf69SMatthias Ringwald // internal table
715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
745628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
755628cf69SMatthias Ringwald 
7633c40538SMatthias Ringwald // prototypes
7733c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
7833c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
7933c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
8034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
8133c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
8233c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
8333c40538SMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8433c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
853d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
8630725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void);
8733c40538SMatthias Ringwald 
885628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
895628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
902125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
915628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
925628cf69SMatthias Ringwald 
935628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
945628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
955628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
965628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
9739bda6d5Smatthias.ringwald 
9839bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
992b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1002b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1012b83fb7dSmatthias.ringwald 
10233c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
10333c40538SMatthias Ringwald 
104fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
105fb37a842SMatthias Ringwald 
10633c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1075628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
10839bda6d5Smatthias.ringwald 
10934e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
11034e7e577SMatthias Ringwald     switch (index){
11134e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
11234e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
11334e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
11434e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
11534e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
11634e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
11734e7e577SMatthias Ringwald         default:
11834e7e577SMatthias Ringwald             return 0;
11934e7e577SMatthias Ringwald     }
12034e7e577SMatthias Ringwald }
1215628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1225628cf69SMatthias Ringwald     switch (channel_id){
1235628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1245628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1255628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1265628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1275628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1285628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1295628cf69SMatthias Ringwald         default:
1305628cf69SMatthias Ringwald             return -1;
1315628cf69SMatthias Ringwald         }
1325628cf69SMatthias Ringwald }
13339bda6d5Smatthias.ringwald 
13434e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
13534e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
13634e7e577SMatthias Ringwald     return 1;
13734e7e577SMatthias Ringwald }
13834e7e577SMatthias Ringwald 
13971de195eSMatthias Ringwald void l2cap_init(void){
1402b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
141808a48abSmatthias.ringwald 
142f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
143f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1447192e786SMatthias Ringwald     l2cap_le_services = NULL;
1457192e786SMatthias Ringwald     l2cap_le_channels = NULL;
146f5454fc6Smatthias.ringwald 
14733c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1485628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
149f5454fc6Smatthias.ringwald 
150ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
151ac301f95S[email protected] 
152fcadd0caSmatthias.ringwald     //
1532718e2e7Smatthias.ringwald     // register callback with HCI
154fcadd0caSmatthias.ringwald     //
155d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
156fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
157fb37a842SMatthias Ringwald 
158d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
159fb37a842SMatthias Ringwald 
16015a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
161fcadd0caSmatthias.ringwald }
162fcadd0caSmatthias.ringwald 
163ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
16433c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1651e6aba47Smatthias.ringwald }
1661e6aba47Smatthias.ringwald 
16717a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
16858de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
16958de5610Smatthias.ringwald }
17058de5610Smatthias.ringwald 
17158de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
172c9dc710bS[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",
173fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
174c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
175c9dc710bS[email protected]     uint8_t event[23];
17658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
17758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
17858de5610Smatthias.ringwald     event[2] = status;
179724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
180fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
181f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
182f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
183f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
184f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
185f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
186f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
18758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18817a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
18958de5610Smatthias.ringwald }
19058de5610Smatthias.ringwald 
19158de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
192e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
19358de5610Smatthias.ringwald     uint8_t event[4];
19458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
19558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
196f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
19758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19817a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
19958de5610Smatthias.ringwald }
20058de5610Smatthias.ringwald 
20158de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
202e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
203fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
20458de5610Smatthias.ringwald     uint8_t event[16];
20558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
20658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
207724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
208fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
209f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
210f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
211f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
21258de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21317a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2140af41d30Smatthias.ringwald }
215808a48abSmatthias.ringwald 
21634e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
21734e7e577SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
21833c40538SMatthias Ringwald     uint8_t event[4];
21933c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
22033c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
22134e7e577SMatthias Ringwald     little_endian_store_16(event, 2, channel);
22233c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22334e7e577SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
22433c40538SMatthias Ringwald }
22533c40538SMatthias Ringwald 
226fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
227ccf076adS[email protected]     uint8_t event[6];
228ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
229ccf076adS[email protected]     event[1] = 4;
230fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
231f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
232ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23333c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
23433c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
235ccf076adS[email protected] }
236ccf076adS[email protected] 
2377f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
238665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
239665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
240665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
241665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
242b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
243f62db1e3Smatthias.ringwald             return channel;
244f62db1e3Smatthias.ringwald         }
245f62db1e3Smatthias.ringwald     }
246f62db1e3Smatthias.ringwald     return NULL;
247f62db1e3Smatthias.ringwald }
248f62db1e3Smatthias.ringwald 
2490b9d7e78SMatthias Ringwald ///
2500b9d7e78SMatthias Ringwald 
25130725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
25230725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
25330725612SMatthias Ringwald     if (!channel) return;
25430725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
25530725612SMatthias Ringwald     l2cap_notify_channel_can_send();
25630725612SMatthias Ringwald }
25730725612SMatthias Ringwald 
2580b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
2590b9d7e78SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
2600b9d7e78SMatthias Ringwald     if (index < 0) return;
2610b9d7e78SMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
2620b9d7e78SMatthias Ringwald     l2cap_notify_channel_can_send();
2630b9d7e78SMatthias Ringwald }
2640b9d7e78SMatthias Ringwald 
2650b9d7e78SMatthias Ringwald ///
2660b9d7e78SMatthias Ringwald 
2676b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2686b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2696b1fde37Smatthias.ringwald     if (!channel) return 0;
2700b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
2717856fb31S[email protected] }
2727856fb31S[email protected] 
27383e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
27483e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
27583e7cdd9SMatthias Ringwald     if (!channel) return 0;
2760b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
27783e7cdd9SMatthias Ringwald }
27883e7cdd9SMatthias Ringwald 
279fc64f94aSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
2800b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
2812125de09SMatthias Ringwald }
2820b9d7e78SMatthias Ringwald 
2830b9d7e78SMatthias Ringwald ///
2843cab4fcaS[email protected] 
28596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
28696cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
28796cbd662Smatthias.ringwald     if (channel) {
28896cbd662Smatthias.ringwald         return channel->remote_mtu;
28996cbd662Smatthias.ringwald     }
29096cbd662Smatthias.ringwald     return 0;
29196cbd662Smatthias.ringwald }
29296cbd662Smatthias.ringwald 
293ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
294665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
295665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
296665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
297665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2985932bd7cS[email protected]         if ( &channel->rtx == ts) {
2995932bd7cS[email protected]             return channel;
3005932bd7cS[email protected]         }
3015932bd7cS[email protected]     }
3025932bd7cS[email protected]     return NULL;
3035932bd7cS[email protected] }
3045932bd7cS[email protected] 
305ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3065932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
30795d2c8f4SMatthias Ringwald     if (!channel) return;
3085932bd7cS[email protected] 
3095932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3105932bd7cS[email protected] 
3115932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3125932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3135932bd7cS[email protected]     // notify client
3145932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3155932bd7cS[email protected] 
3165932bd7cS[email protected]     // discard channel
3179dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
318665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3195932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3205932bd7cS[email protected] }
3215932bd7cS[email protected] 
3225932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3235932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
324528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3255932bd7cS[email protected] }
3265932bd7cS[email protected] 
3275932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3285932bd7cS[email protected]     l2cap_stop_rtx(channel);
329cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
330528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
331528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
332528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3335932bd7cS[email protected] }
3345932bd7cS[email protected] 
3355932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3365932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3375932bd7cS[email protected]     l2cap_stop_rtx(channel);
338528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
339528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
340528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3415932bd7cS[email protected] }
3425932bd7cS[email protected] 
34371de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
344ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
345ac301f95S[email protected] }
346ac301f95S[email protected] 
347df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
348ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
349df3354fcS[email protected] }
3505932bd7cS[email protected] 
3517f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
352b1d43497Smatthias.ringwald 
353a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3549da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
355b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
356b1d43497Smatthias.ringwald     }
357b1d43497Smatthias.ringwald 
3589da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3592a373862S[email protected]     hci_reserve_packet_buffer();
360facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
36158de5610Smatthias.ringwald     va_list argptr;
36258de5610Smatthias.ringwald     va_start(argptr, identifier);
36370efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
36458de5610Smatthias.ringwald     va_end(argptr);
3659da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
366826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
36758de5610Smatthias.ringwald }
36858de5610Smatthias.ringwald 
369a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3707f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
37170efece1S[email protected] 
372a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3739da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
37470efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
37570efece1S[email protected]     }
37670efece1S[email protected] 
3779da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3782a373862S[email protected]     hci_reserve_packet_buffer();
379facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
38070efece1S[email protected]     va_list argptr;
38170efece1S[email protected]     va_start(argptr, identifier);
38270efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
38370efece1S[email protected]     va_end(argptr);
3849da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
385826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
38670efece1S[email protected] }
38770efece1S[email protected] #endif
38870efece1S[email protected] 
389b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
390facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
391b1d43497Smatthias.ringwald }
3926218e6f1Smatthias.ringwald 
3936b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
3946b4af23dS[email protected]     return hci_reserve_packet_buffer();
3956b4af23dS[email protected] }
3966b4af23dS[email protected] 
39768a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
39868a0fcf7S[email protected]     hci_release_packet_buffer();
39968a0fcf7S[email protected] }
40068a0fcf7S[email protected] 
40168a0fcf7S[email protected] 
402b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
403b1d43497Smatthias.ringwald 
404c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
405c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
406c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
407c8b9416aS[email protected]     }
408c8b9416aS[email protected] 
40958de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
410b1d43497Smatthias.ringwald     if (!channel) {
4119da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
412b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4136218e6f1Smatthias.ringwald     }
4146218e6f1Smatthias.ringwald 
415fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4169da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
417a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
418a35252c8S[email protected]     }
419a35252c8S[email protected] 
420fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
421b1d43497Smatthias.ringwald 
422facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
423b1d43497Smatthias.ringwald 
424e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
425e9772277S[email protected] 
426e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
427fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14));
42858de5610Smatthias.ringwald     // 2 - ACL length
429f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
43058de5610Smatthias.ringwald     // 4 - L2CAP packet length
431f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
43258de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
433f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
43458de5610Smatthias.ringwald     // send
435826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
43691b99603Smatthias.ringwald 
4376218e6f1Smatthias.ringwald     return err;
43858de5610Smatthias.ringwald }
43958de5610Smatthias.ringwald 
440fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
4412149f12eSmatthias.ringwald 
442c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
443c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4442149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4452149f12eSmatthias.ringwald     }
4462149f12eSmatthias.ringwald 
447fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
448fc64f94aSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
449c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
450c8b9416aS[email protected]     }
451c8b9416aS[email protected] 
452fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
4532149f12eSmatthias.ringwald 
454facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4552149f12eSmatthias.ringwald 
456e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
457e9772277S[email protected] 
458e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
459fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
4602149f12eSmatthias.ringwald     // 2 - ACL length
461f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4622149f12eSmatthias.ringwald     // 4 - L2CAP packet length
463f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4642149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
465f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4662149f12eSmatthias.ringwald     // send
467826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4682149f12eSmatthias.ringwald 
4692149f12eSmatthias.ringwald     return err;
4702149f12eSmatthias.ringwald }
4712149f12eSmatthias.ringwald 
472ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
473b1d43497Smatthias.ringwald 
474a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
475a35252c8S[email protected]     if (!channel) {
476ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
477a35252c8S[email protected]         return -1;   // TODO: define error
478a35252c8S[email protected]     }
479a35252c8S[email protected] 
480f0efaa57S[email protected]     if (len > channel->remote_mtu){
481ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
482f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
483f0efaa57S[email protected]     }
484f0efaa57S[email protected] 
485fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
486ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
487b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
488b1d43497Smatthias.ringwald     }
489b1d43497Smatthias.ringwald 
4902a373862S[email protected]     hci_reserve_packet_buffer();
491facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
492b1d43497Smatthias.ringwald 
493b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
494b1d43497Smatthias.ringwald 
495b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
496b1d43497Smatthias.ringwald }
497b1d43497Smatthias.ringwald 
498fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
4992149f12eSmatthias.ringwald 
500fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
501ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
5022149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
5032149f12eSmatthias.ringwald     }
5042149f12eSmatthias.ringwald 
5052a373862S[email protected]     hci_reserve_packet_buffer();
506facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
5072149f12eSmatthias.ringwald 
5082149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
5092149f12eSmatthias.ringwald 
510fc64f94aSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
5112149f12eSmatthias.ringwald }
5122149f12eSmatthias.ringwald 
513fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
514fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5150e37e417S[email protected] }
5160e37e417S[email protected] 
51728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
51828ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
51928ca2b46S[email protected] }
52028ca2b46S[email protected] 
52128ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
52228ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
52328ca2b46S[email protected] }
52428ca2b46S[email protected] 
52528ca2b46S[email protected] 
526b1d43497Smatthias.ringwald 
5278158c421Smatthias.ringwald // MARK: L2CAP_RUN
5282cd0be45Smatthias.ringwald // process outstanding signaling tasks
5297f02f414SMatthias Ringwald static void l2cap_run(void){
5302b83fb7dSmatthias.ringwald 
53122c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
53222c29ab4SMatthias Ringwald 
5332b83fb7dSmatthias.ringwald     // check pending signaling responses
5342b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5352b83fb7dSmatthias.ringwald 
5362b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
537a35252c8S[email protected] 
538a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
539a35252c8S[email protected] 
5402b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5412b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
54263a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
543f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5442b83fb7dSmatthias.ringwald 
545f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
546f53da564S[email protected]         signaling_responses_pending--;
547f53da564S[email protected]         int i;
548f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
549f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
550f53da564S[email protected]         }
551f53da564S[email protected] 
552f53da564S[email protected]         switch (response_code){
5532b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5542b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5552bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5564d816277S[email protected]                 if (result == 0x0003){
5572bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5584d816277S[email protected]                 }
5592b360848Smatthias.ringwald                 break;
5602b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5612b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5622b83fb7dSmatthias.ringwald                 break;
5632b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5643b0484b3S[email protected]                 switch (infoType){
5653b0484b3S[email protected]                     case 1: { // Connectionless MTU
5663b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5673b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5683b0484b3S[email protected]                         break;
5693b0484b3S[email protected]                     }
5703b0484b3S[email protected]                     case 2: { // Extended Features Supported
571462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
572462e630dS[email protected]                         uint32_t features = 0x280;
5733b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5743b0484b3S[email protected]                         break;
5753b0484b3S[email protected]                     }
5763b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5773b0484b3S[email protected]                         uint8_t map[8];
5783b0484b3S[email protected]                         memset(map, 0, 8);
579288636a2SMatthias Ringwald                         map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
5803b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5813b0484b3S[email protected]                         break;
5823b0484b3S[email protected]                     }
5833b0484b3S[email protected]                     default:
5842b83fb7dSmatthias.ringwald                         // all other types are not supported
5852b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5863b0484b3S[email protected]                         break;
5872b83fb7dSmatthias.ringwald                 }
5882b83fb7dSmatthias.ringwald                 break;
58963a7246aSmatthias.ringwald             case COMMAND_REJECT:
5905ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
591a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
59270efece1S[email protected]             case COMMAND_REJECT_LE:
59370efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
59463a7246aSmatthias.ringwald                 break;
59570efece1S[email protected] #endif
5962b83fb7dSmatthias.ringwald             default:
5972b83fb7dSmatthias.ringwald                 // should not happen
5982b83fb7dSmatthias.ringwald                 break;
5992b83fb7dSmatthias.ringwald         }
6002b83fb7dSmatthias.ringwald     }
6012b83fb7dSmatthias.ringwald 
602ae280e73Smatthias.ringwald     uint8_t  config_options[4];
603665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
604665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
605665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
606baf94f06S[email protected] 
607665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
60822c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6092cd0be45Smatthias.ringwald         switch (channel->state){
6102cd0be45Smatthias.ringwald 
611df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
612ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
613fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
614a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
615ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
616fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
617ad671560S[email protected]                 }
618ad671560S[email protected]                 break;
619ad671560S[email protected] 
62002b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
621baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
62264472d52Smatthias.ringwald                 // send connection request - set state first
62364472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
62402b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6258f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
62602b22dc4Smatthias.ringwald                 break;
62702b22dc4Smatthias.ringwald 
628e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
629fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
63022c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
631fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
632e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6339dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
634665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
635d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
636e7ff783cSmatthias.ringwald                 break;
637e7ff783cSmatthias.ringwald 
638552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
639fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
640fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
64128ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
642fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
643552d92a1Smatthias.ringwald                 break;
644552d92a1Smatthias.ringwald 
6456fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
646fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6476fdcc387Smatthias.ringwald                 // success, start l2cap handshake
648b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6496fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
650fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6515932bd7cS[email protected]                 l2cap_start_rtx(channel);
6526fdcc387Smatthias.ringwald                 break;
6536fdcc387Smatthias.ringwald 
654fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
655fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
65673cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
65763a7246aSmatthias.ringwald                     uint16_t flags = 0;
65828ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
65963a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
66063a7246aSmatthias.ringwald                         flags = 1;
66163a7246aSmatthias.ringwald                     } else {
66228ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
66363a7246aSmatthias.ringwald                     }
66463a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
665fc64f94aSMatthias 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);
66663a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
66763a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
66863a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
669f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
670fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
67163a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
67263a7246aSmatthias.ringwald                     } else {
673fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
67463a7246aSmatthias.ringwald                     }
67563a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
676fa8473a4Smatthias.ringwald                 }
67773cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
67828ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
67928ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
680b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
681ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
682ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
683f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
684fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6855932bd7cS[email protected]                     l2cap_start_rtx(channel);
686fa8473a4Smatthias.ringwald                 }
687fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
688552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
689552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
690fa8473a4Smatthias.ringwald                 }
691552d92a1Smatthias.ringwald                 break;
692552d92a1Smatthias.ringwald 
693e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
694fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
69522c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
696fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
6975932bd7cS[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 :)
698756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
699e7ff783cSmatthias.ringwald                 break;
700e7ff783cSmatthias.ringwald 
701e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
702fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
703b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7042cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
705fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7062cd0be45Smatthias.ringwald                 break;
7072cd0be45Smatthias.ringwald             default:
7082cd0be45Smatthias.ringwald                 break;
7092cd0be45Smatthias.ringwald         }
7102cd0be45Smatthias.ringwald     }
711da886c03S[email protected] 
712a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
713da886c03S[email protected]     // send l2cap con paramter update if necessary
714da886c03S[email protected]     hci_connections_get_iterator(&it);
715665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
716665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7175d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
718b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
719da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
720b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
721b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
722b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
723b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
724b68d7bc3SMatthias Ringwald                 break;
725da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
726b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
727b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
728da886c03S[email protected]                 break;
729da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
730b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
731b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
732da886c03S[email protected]                 break;
733da886c03S[email protected]             default:
734da886c03S[email protected]                 break;
735da886c03S[email protected]         }
736da886c03S[email protected]     }
7374d7157c3S[email protected] #endif
738da886c03S[email protected] 
73922c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
7402cd0be45Smatthias.ringwald }
7412cd0be45Smatthias.ringwald 
7424aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7434ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
744fa8c92f6Smatthias.ringwald }
745fa8c92f6Smatthias.ringwald 
74671de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
7474ff786cfS[email protected]     return l2cap_max_mtu();
748e5e1518dS[email protected] }
749e5e1518dS[email protected] 
750fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
7512df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7525533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7532df5dadcS[email protected]         // success, start l2cap handshake
754fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
7552df5dadcS[email protected]         // check remote SSP feature first
7562df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7572df5dadcS[email protected]     }
7582df5dadcS[email protected] }
7592df5dadcS[email protected] 
7602df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7612df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7622df5dadcS[email protected] 
7632df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
764ac301f95S[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));
765fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7662df5dadcS[email protected]         // request security level 2
7672df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
768fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
7692df5dadcS[email protected]         return;
7702df5dadcS[email protected]     }
7712df5dadcS[email protected]     // fine, go ahead
7722df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
7732df5dadcS[email protected] }
7742df5dadcS[email protected] 
775*da144af5SMatthias 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,
776*da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
777*da144af5SMatthias Ringwald 
778*da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
779*da144af5SMatthias Ringwald     if (!channel) {
780*da144af5SMatthias Ringwald         return NULL;
781*da144af5SMatthias Ringwald     }
782*da144af5SMatthias Ringwald 
783*da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
784*da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
785*da144af5SMatthias Ringwald 
786*da144af5SMatthias Ringwald     // fill in
787*da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
788*da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
789*da144af5SMatthias Ringwald     channel->address_type = address_type;
790*da144af5SMatthias Ringwald     channel->psm = psm;
791*da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
792*da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
793*da144af5SMatthias Ringwald     channel->required_security_level = security_level;
794*da144af5SMatthias Ringwald 
795*da144af5SMatthias Ringwald     //
796*da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
797*da144af5SMatthias Ringwald     channel->con_handle = 0;
798*da144af5SMatthias Ringwald 
799*da144af5SMatthias Ringwald     // set initial state
800*da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
801*da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
802*da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
803*da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
804*da144af5SMatthias Ringwald     return channel;
805*da144af5SMatthias Ringwald }
806*da144af5SMatthias Ringwald 
8079077cb15SMatthias Ringwald /**
8089077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
8099077cb15SMatthias Ringwald  * @param packet_handler
8109077cb15SMatthias Ringwald  * @param address
8119077cb15SMatthias Ringwald  * @param psm
8129077cb15SMatthias Ringwald  * @param mtu
8139077cb15SMatthias Ringwald  * @param local_cid
8149077cb15SMatthias Ringwald  */
8159077cb15SMatthias Ringwald 
816*da144af5SMatthias 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){
817*da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
818*da144af5SMatthias Ringwald 
819*da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
820*da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
821*da144af5SMatthias Ringwald     }
822*da144af5SMatthias Ringwald 
823*da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
824fc64f94aSMatthias Ringwald     if (!channel) {
8259077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
8269077cb15SMatthias Ringwald     }
8279077cb15SMatthias Ringwald 
8289077cb15SMatthias Ringwald     // add to connections list
829fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
8309077cb15SMatthias Ringwald 
8319077cb15SMatthias Ringwald     // store local_cid
8329077cb15SMatthias Ringwald     if (out_local_cid){
833fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
8349077cb15SMatthias Ringwald     }
8359077cb15SMatthias Ringwald 
8369077cb15SMatthias Ringwald     // check if hci connection is already usable
8379077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
8389077cb15SMatthias Ringwald     if (conn){
839add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
840fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
8419077cb15SMatthias Ringwald         // check if remote supported fearures are already received
8429077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
843fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
8449077cb15SMatthias Ringwald         }
8459077cb15SMatthias Ringwald     }
8469077cb15SMatthias Ringwald 
8479077cb15SMatthias Ringwald     l2cap_run();
8489077cb15SMatthias Ringwald 
8499077cb15SMatthias Ringwald     return 0;
8509077cb15SMatthias Ringwald }
8519077cb15SMatthias Ringwald 
852ce8f182eSMatthias Ringwald void
853ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
854e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
855b35f641cSmatthias.ringwald     // find channel for local_cid
856b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
857f62db1e3Smatthias.ringwald     if (channel) {
858e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
859f62db1e3Smatthias.ringwald     }
8602cd0be45Smatthias.ringwald     // process
8612cd0be45Smatthias.ringwald     l2cap_run();
86243625864Smatthias.ringwald }
8631e6aba47Smatthias.ringwald 
864afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
865665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
866665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
867665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
868665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
869058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
870c22aecc9S[email protected]         // channel for this address found
871c22aecc9S[email protected]         switch (channel->state){
872c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
873c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
874afde0c52Smatthias.ringwald                 // failure, forward error code
875afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
876afde0c52Smatthias.ringwald                 // discard channel
8779dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
878665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
879d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
880c22aecc9S[email protected]                 break;
881c22aecc9S[email protected]             default:
882c22aecc9S[email protected]                 break;
883afde0c52Smatthias.ringwald         }
884afde0c52Smatthias.ringwald     }
885afde0c52Smatthias.ringwald }
886afde0c52Smatthias.ringwald 
887afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
888665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
889665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
890665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
891665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
892058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
8932df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
894afde0c52Smatthias.ringwald         }
895afde0c52Smatthias.ringwald     }
8966fdcc387Smatthias.ringwald     // process
8976fdcc387Smatthias.ringwald     l2cap_run();
898afde0c52Smatthias.ringwald }
899b448a0e7Smatthias.ringwald 
90033c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
90133c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
90233c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
90333c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
90433c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
90533c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
906fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
90733c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
90834e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
90933c40538SMatthias Ringwald     }
9102125de09SMatthias Ringwald 
9112125de09SMatthias Ringwald     int i;
9122125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
913773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
9142125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
91534e7e577SMatthias Ringwald         int can_send;
91634e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
91734e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
91834e7e577SMatthias Ringwald         } else {
91934e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
92034e7e577SMatthias Ringwald         }
92134e7e577SMatthias Ringwald         if (!can_send) continue;
92234e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
92334e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
9242125de09SMatthias Ringwald     }
92533c40538SMatthias Ringwald }
92633c40538SMatthias Ringwald 
927d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
928afde0c52Smatthias.ringwald 
929afde0c52Smatthias.ringwald     bd_addr_t address;
930afde0c52Smatthias.ringwald     hci_con_handle_t handle;
931665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
9322d00edd4Smatthias.ringwald     int hci_con_used;
933afde0c52Smatthias.ringwald 
9340e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
935afde0c52Smatthias.ringwald 
936afde0c52Smatthias.ringwald         // handle connection complete events
937afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
938724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
939afde0c52Smatthias.ringwald             if (packet[2] == 0){
940f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
941afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
942afde0c52Smatthias.ringwald             } else {
943afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
944afde0c52Smatthias.ringwald             }
945afde0c52Smatthias.ringwald             break;
946afde0c52Smatthias.ringwald 
947afde0c52Smatthias.ringwald         // handle successful create connection cancel command
948afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
949073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
950afde0c52Smatthias.ringwald                 if (packet[5] == 0){
951724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
952afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
953afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
95403cfbabcSmatthias.ringwald                 }
9551e6aba47Smatthias.ringwald             }
95639d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
95739d59809Smatthias.ringwald             break;
95839d59809Smatthias.ringwald 
95939d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
96039d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
961afde0c52Smatthias.ringwald             break;
96227a923d0Smatthias.ringwald 
9631e6aba47Smatthias.ringwald         // handle disconnection complete events
964afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
965c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
966f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
967665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
968665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
969665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
970fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
97115ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
9729dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
973665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
974d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
97527a923d0Smatthias.ringwald             }
976afde0c52Smatthias.ringwald             break;
977fcadd0caSmatthias.ringwald 
97833c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
97963fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
9806218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
98102b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
98233c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
9836218e6f1Smatthias.ringwald             break;
9846218e6f1Smatthias.ringwald 
985ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
986afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
987f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
988bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
98980ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
9902d00edd4Smatthias.ringwald             hci_con_used = 0;
991665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
992665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
993665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
994fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
9952d00edd4Smatthias.ringwald                 hci_con_used = 1;
996c22aecc9S[email protected]                 break;
997ee091cf1Smatthias.ringwald             }
9982d00edd4Smatthias.ringwald             if (hci_con_used) break;
999d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
10009edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1001afde0c52Smatthias.ringwald             break;
1002ee091cf1Smatthias.ringwald 
1003df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1004f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1005665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1006665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1007665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1008fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10092df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1010df3354fcS[email protected]                 break;
1011df3354fcS[email protected]             }
1012c22aecc9S[email protected]             break;
1013df3354fcS[email protected] 
10145611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1015f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1016bd63148eS[email protected]             log_info("l2cap - security level update");
1017665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1018665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1019665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1020fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10215533f01eS[email protected] 
1022bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1023bd63148eS[email protected] 
1024e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
10255533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
10265533f01eS[email protected] 
1027df3354fcS[email protected]                 switch (channel->state){
1028df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
10295533f01eS[email protected]                         if (actual_level >= required_level){
1030f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1031f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
10321eb2563eS[email protected]                         } else {
1033775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
10341eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
10351eb2563eS[email protected]                         }
1036df3354fcS[email protected]                         break;
1037df3354fcS[email protected] 
1038df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
10395533f01eS[email protected]                         if (actual_level >= required_level){
1040df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1041df3354fcS[email protected]                         } else {
1042df3354fcS[email protected]                             // disconnnect, authentication not good enough
1043df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1044df3354fcS[email protected]                         }
1045df3354fcS[email protected]                         break;
1046df3354fcS[email protected] 
1047df3354fcS[email protected]                     default:
1048df3354fcS[email protected]                         break;
1049df3354fcS[email protected]                 }
1050f85a9399S[email protected]             }
1051f85a9399S[email protected]             break;
1052f85a9399S[email protected] 
1053afde0c52Smatthias.ringwald         default:
1054afde0c52Smatthias.ringwald             break;
1055afde0c52Smatthias.ringwald     }
1056afde0c52Smatthias.ringwald 
1057bd63148eS[email protected]     l2cap_run();
10581e6aba47Smatthias.ringwald }
10591e6aba47Smatthias.ringwald 
1060afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1061b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1062e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1063e7ff783cSmatthias.ringwald     l2cap_run();
106484836b65Smatthias.ringwald }
106584836b65Smatthias.ringwald 
10662b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
10674cf56b4aSmatthias.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."
10682b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
10692b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
10702b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
10712b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
10722b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
10732b360848Smatthias.ringwald         signaling_responses_pending++;
10742b360848Smatthias.ringwald         l2cap_run();
10752b360848Smatthias.ringwald     }
10762b360848Smatthias.ringwald }
10772b360848Smatthias.ringwald 
1078b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1079645658c9Smatthias.ringwald 
10809da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1081645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1082645658c9Smatthias.ringwald     if (!service) {
1083645658c9Smatthias.ringwald         // 0x0002 PSM not supported
10842b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1085645658c9Smatthias.ringwald         return;
1086645658c9Smatthias.ringwald     }
1087645658c9Smatthias.ringwald 
10885061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1089645658c9Smatthias.ringwald     if (!hci_connection) {
10902b360848Smatthias.ringwald         //
10919da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1092645658c9Smatthias.ringwald         return;
1093645658c9Smatthias.ringwald     }
10942bd8b7e7S[email protected] 
1095645658c9Smatthias.ringwald     // alloc structure
10969da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1097*da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1098*da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
10992b360848Smatthias.ringwald     if (!channel){
11002b360848Smatthias.ringwald         // 0x0004 No resources available
11012b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
11022b360848Smatthias.ringwald         return;
11032b360848Smatthias.ringwald     }
1104*da144af5SMatthias Ringwald 
1105fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1106b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1107b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1108645658c9Smatthias.ringwald 
1109f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
11102985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
11112985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
11129775e25bSmatthias.ringwald     }
11139775e25bSmatthias.ringwald 
1114645658c9Smatthias.ringwald     // set initial state
1115df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1116ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1117e405ae81Smatthias.ringwald 
1118645658c9Smatthias.ringwald     // add to connections list
1119665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1120645658c9Smatthias.ringwald 
1121f85a9399S[email protected]     // assert security requirements
11221eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1123e405ae81Smatthias.ringwald }
1124645658c9Smatthias.ringwald 
1125ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1126e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1127b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1128e405ae81Smatthias.ringwald     if (!channel) {
1129ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1130e405ae81Smatthias.ringwald         return;
1131e405ae81Smatthias.ringwald     }
1132e405ae81Smatthias.ringwald 
1133552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1134e405ae81Smatthias.ringwald 
1135552d92a1Smatthias.ringwald     // process
1136552d92a1Smatthias.ringwald     l2cap_run();
1137e405ae81Smatthias.ringwald }
1138645658c9Smatthias.ringwald 
1139ce8f182eSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid, uint8_t reason){
1140e0abb8e7S[email protected]     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x, reason %x", local_cid, reason);
1141b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1142e405ae81Smatthias.ringwald     if (!channel) {
1143ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1144e405ae81Smatthias.ringwald         return;
1145e405ae81Smatthias.ringwald     }
1146e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
1147e7ff783cSmatthias.ringwald     channel->reason = reason;
1148e7ff783cSmatthias.ringwald     l2cap_run();
1149645658c9Smatthias.ringwald }
1150645658c9Smatthias.ringwald 
11517f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1152b1988dceSmatthias.ringwald 
1153b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1154b1988dceSmatthias.ringwald 
1155f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
115663a7246aSmatthias.ringwald     if (flags & 1) {
115763a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
115863a7246aSmatthias.ringwald     }
115963a7246aSmatthias.ringwald 
11602784b77dSmatthias.ringwald     // accept the other's configuration options
1161f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
11623de7c0caSmatthias.ringwald     uint16_t pos     = 8;
11633de7c0caSmatthias.ringwald     while (pos < end_pos){
116463a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
116563a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
116663a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
116763a7246aSmatthias.ringwald         pos++;
11681dc511deSmatthias.ringwald         uint8_t length = command[pos++];
11691dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
117063a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1171f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
11729da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
117363a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
117463a7246aSmatthias.ringwald         }
11750fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
11760fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1177f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
11780fe7a9d0S[email protected]         }
117963a7246aSmatthias.ringwald         // check for unknown options
118063a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1181c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
118263a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
11831dc511deSmatthias.ringwald         }
11841dc511deSmatthias.ringwald         pos += length;
11851dc511deSmatthias.ringwald     }
11862784b77dSmatthias.ringwald }
11872784b77dSmatthias.ringwald 
1188fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
11899da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
119073cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
119173cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1192019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1193019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1194fa8473a4Smatthias.ringwald     return 1;
1195fa8473a4Smatthias.ringwald }
1196fa8473a4Smatthias.ringwald 
1197fa8473a4Smatthias.ringwald 
11987f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
11991e6aba47Smatthias.ringwald 
120000d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
120100d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
120238e5900eSmatthias.ringwald     uint16_t result = 0;
12031e6aba47Smatthias.ringwald 
12049da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1205b35f641cSmatthias.ringwald 
12069a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
12079a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
12089a011532Smatthias.ringwald         switch (channel->state){
1209fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
12109a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
12112b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
12129a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
12139a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
12149a011532Smatthias.ringwald                 break;
12159a011532Smatthias.ringwald 
12169a011532Smatthias.ringwald             default:
12179a011532Smatthias.ringwald                 // ignore in other states
12189a011532Smatthias.ringwald                 break;
12199a011532Smatthias.ringwald         }
12209a011532Smatthias.ringwald         return;
12219a011532Smatthias.ringwald     }
12229a011532Smatthias.ringwald 
122356081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
12241e6aba47Smatthias.ringwald     switch (channel->state) {
12251e6aba47Smatthias.ringwald 
12261e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
12271e6aba47Smatthias.ringwald             switch (code){
12281e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
12295932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1230f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
123138e5900eSmatthias.ringwald                     switch (result) {
123238e5900eSmatthias.ringwald                         case 0:
1233169f8b28Smatthias.ringwald                             // successful connection
1234f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1235fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
123628ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
123738e5900eSmatthias.ringwald                             break;
123838e5900eSmatthias.ringwald                         case 1:
12395932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
12405932bd7cS[email protected]                             l2cap_start_ertx(channel);
124138e5900eSmatthias.ringwald                             break;
124238e5900eSmatthias.ringwald                         default:
1243eb920dbeSmatthias.ringwald                             // channel closed
1244eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1245f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
124638e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1247eb920dbeSmatthias.ringwald 
1248eb920dbeSmatthias.ringwald                             // drop link key if security block
1249eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
125015a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1251eb920dbeSmatthias.ringwald                             }
1252eb920dbeSmatthias.ringwald 
1253eb920dbeSmatthias.ringwald                             // discard channel
1254665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1255d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
125638e5900eSmatthias.ringwald                             break;
12571e6aba47Smatthias.ringwald                     }
12581e6aba47Smatthias.ringwald                     break;
125938e5900eSmatthias.ringwald 
126038e5900eSmatthias.ringwald                 default:
12611e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
126238e5900eSmatthias.ringwald                     break;
12631e6aba47Smatthias.ringwald             }
12641e6aba47Smatthias.ringwald             break;
12651e6aba47Smatthias.ringwald 
1266fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1267f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1268ae280e73Smatthias.ringwald             switch (code) {
1269ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
127028ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1271ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
127263a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
127363a7246aSmatthias.ringwald                         // only done if continuation not set
127463a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
127563a7246aSmatthias.ringwald                     }
1276ae280e73Smatthias.ringwald                     break;
12771e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
12785932bd7cS[email protected]                     l2cap_stop_rtx(channel);
12795932bd7cS[email protected]                     switch (result){
12805932bd7cS[email protected]                         case 0: // success
12815932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
12825932bd7cS[email protected]                             break;
12835932bd7cS[email protected]                         case 4: // pending
12845932bd7cS[email protected]                             l2cap_start_ertx(channel);
12855932bd7cS[email protected]                             break;
12865932bd7cS[email protected]                         default:
1287fe9d8984S[email protected]                             // retry on negative result
1288fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1289fe9d8984S[email protected]                             break;
1290fe9d8984S[email protected]                     }
12915a67bd4aSmatthias.ringwald                     break;
12925a67bd4aSmatthias.ringwald                 default:
12935a67bd4aSmatthias.ringwald                     break;
12941e6aba47Smatthias.ringwald             }
1295fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1296fa8473a4Smatthias.ringwald                 // for open:
12975a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1298fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1299c8e4258aSmatthias.ringwald             }
1300c8e4258aSmatthias.ringwald             break;
1301f62db1e3Smatthias.ringwald 
1302f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1303f62db1e3Smatthias.ringwald             switch (code) {
1304f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
130527a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
130627a923d0Smatthias.ringwald                     break;
13075a67bd4aSmatthias.ringwald                 default:
13085a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
13095a67bd4aSmatthias.ringwald                     break;
131027a923d0Smatthias.ringwald             }
131127a923d0Smatthias.ringwald             break;
131284836b65Smatthias.ringwald 
131384836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
131484836b65Smatthias.ringwald             // @TODO handle incoming requests
131584836b65Smatthias.ringwald             break;
131684836b65Smatthias.ringwald 
131784836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
131884836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
131984836b65Smatthias.ringwald             break;
132010642e45Smatthias.ringwald         default:
132110642e45Smatthias.ringwald             break;
132227a923d0Smatthias.ringwald     }
13239da54300S[email protected]     // log_info("new state %u", channel->state);
132427a923d0Smatthias.ringwald }
132527a923d0Smatthias.ringwald 
132600d93d79Smatthias.ringwald 
13277f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
132800d93d79Smatthias.ringwald 
132900d93d79Smatthias.ringwald     // get code, signalind identifier and command len
133000d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
133100d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
133200d93d79Smatthias.ringwald 
133300d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
133400d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
133563a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
133600d93d79Smatthias.ringwald         return;
133700d93d79Smatthias.ringwald     }
133800d93d79Smatthias.ringwald 
133900d93d79Smatthias.ringwald     // general commands without an assigned channel
134000d93d79Smatthias.ringwald     switch(code) {
134100d93d79Smatthias.ringwald 
134200d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1343f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1344f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
134500d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
13462b83fb7dSmatthias.ringwald             return;
134700d93d79Smatthias.ringwald         }
134800d93d79Smatthias.ringwald 
13492b360848Smatthias.ringwald         case ECHO_REQUEST:
13502b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
13512b83fb7dSmatthias.ringwald             return;
135200d93d79Smatthias.ringwald 
135300d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1354f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
13552b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
13562b83fb7dSmatthias.ringwald             return;
135700d93d79Smatthias.ringwald         }
135800d93d79Smatthias.ringwald 
135900d93d79Smatthias.ringwald         default:
136000d93d79Smatthias.ringwald             break;
136100d93d79Smatthias.ringwald     }
136200d93d79Smatthias.ringwald 
136300d93d79Smatthias.ringwald 
136400d93d79Smatthias.ringwald     // Get potential destination CID
1365f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
136600d93d79Smatthias.ringwald 
136700d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1368665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1369665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1370665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1371665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1372fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
137300d93d79Smatthias.ringwald         if (code & 1) {
1374b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1375b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
137600d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13774e32727eSmatthias.ringwald                 break;
137800d93d79Smatthias.ringwald             }
137900d93d79Smatthias.ringwald         } else {
1380b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
138100d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
138200d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
13834e32727eSmatthias.ringwald                 break;
138400d93d79Smatthias.ringwald             }
138500d93d79Smatthias.ringwald         }
138600d93d79Smatthias.ringwald     }
138700d93d79Smatthias.ringwald }
138800d93d79Smatthias.ringwald 
13893d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
139000d93d79Smatthias.ringwald 
139100d93d79Smatthias.ringwald     // Get Channel ID
139200d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
139300d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
139400d93d79Smatthias.ringwald 
13955652b5ffS[email protected]     switch (channel_id) {
13965652b5ffS[email protected] 
13975652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
13985652b5ffS[email protected] 
139900d93d79Smatthias.ringwald             uint16_t command_offset = 8;
140000d93d79Smatthias.ringwald             while (command_offset < size) {
140100d93d79Smatthias.ringwald 
140200d93d79Smatthias.ringwald                 // handle signaling commands
140300d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
140400d93d79Smatthias.ringwald 
140500d93d79Smatthias.ringwald                 // increment command_offset
1406f8fbdce0SMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
140700d93d79Smatthias.ringwald             }
14085652b5ffS[email protected]             break;
140900d93d79Smatthias.ringwald         }
141000d93d79Smatthias.ringwald 
14115652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
14125628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
14135628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
14145652b5ffS[email protected]             }
14155652b5ffS[email protected]             break;
14165652b5ffS[email protected] 
14175652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
14185628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
14195628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
14205652b5ffS[email protected]             }
14215652b5ffS[email protected]             break;
14225652b5ffS[email protected] 
1423462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
14245628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
14255628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1426462e630dS[email protected]             }
1427462e630dS[email protected]             break;
1428462e630dS[email protected] 
14291bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1430ccf076adS[email protected]             switch (packet[8]){
1431ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1432f8fbdce0SMatthias Ringwald                     uint16_t result = little_endian_read_16(packet, 12);
1433ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1434ccf076adS[email protected]                     break;
1435ccf076adS[email protected]                 }
1436ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1437ccf076adS[email protected]                     uint8_t event[10];
1438ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1439ccf076adS[email protected]                     event[1] = 8;
1440ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1441da886c03S[email protected] 
1442da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1443da886c03S[email protected]                     if (connection){
14446d91fb6cSMatthias Ringwald                         if (connection->role != HCI_ROLE_MASTER){
14456d91fb6cSMatthias Ringwald                             // reject command without notifying upper layer when not in master role
14466d91fb6cSMatthias Ringwald                             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14476d91fb6cSMatthias Ringwald                             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14486d91fb6cSMatthias Ringwald                             break;
14496d91fb6cSMatthias Ringwald                         }
1450da886c03S[email protected]                         int update_parameter = 1;
1451a4c06b28SMatthias Ringwald                         le_connection_parameter_range_t existing_range;
14524ced4e8cSMatthias Ringwald                         gap_get_connection_parameter_range(&existing_range);
1453f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_min = little_endian_read_16(packet,12);
1454f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_max = little_endian_read_16(packet,14);
1455f8fbdce0SMatthias Ringwald                         uint16_t le_conn_latency = little_endian_read_16(packet,16);
1456f8fbdce0SMatthias Ringwald                         uint16_t le_supervision_timeout = little_endian_read_16(packet,18);
1457da886c03S[email protected] 
1458da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1459da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1460da886c03S[email protected] 
1461da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1462da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1463da886c03S[email protected] 
1464da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1465da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1466da886c03S[email protected] 
1467da886c03S[email protected]                         if (update_parameter){
1468da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1469da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1470da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1471da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1472da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1473da886c03S[email protected]                         } else {
1474da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1475da886c03S[email protected]                         }
147679f53f1dSMatthias Ringwald                         connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1];
1477da886c03S[email protected]                     }
1478da886c03S[email protected] 
1479ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
148033c40538SMatthias Ringwald                     if (!l2cap_event_packet_handler) break;
148133c40538SMatthias Ringwald                     (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1482ccf076adS[email protected]                     break;
1483ccf076adS[email protected]                 }
1484ccf076adS[email protected]                 default: {
14851bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14861bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14871bbc0b23S[email protected]                     break;
14881bbc0b23S[email protected]                 }
1489ccf076adS[email protected]             }
1490ccf076adS[email protected]             break;
1491ccf076adS[email protected]         }
14921bbc0b23S[email protected] 
14935652b5ffS[email protected]         default: {
149400d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
14953d50b4baSMatthias Ringwald             l2cap_channel_t * l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1496d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
14973d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
149800d93d79Smatthias.ringwald             }
14995652b5ffS[email protected]             break;
15005652b5ffS[email protected]         }
15015652b5ffS[email protected]     }
150200d93d79Smatthias.ringwald 
15031eb2563eS[email protected]     l2cap_run();
15042718e2e7Smatthias.ringwald }
150500d93d79Smatthias.ringwald 
150615ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
150727a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1508f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1509f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1510f62db1e3Smatthias.ringwald     // discard channel
15119dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1512665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1513d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1514c8e4258aSmatthias.ringwald }
15151e6aba47Smatthias.ringwald 
15168f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1517665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1518665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1519665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1520665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
15219d9bbc01Smatthias.ringwald         if ( service->psm == psm){
15229d9bbc01Smatthias.ringwald             return service;
15239d9bbc01Smatthias.ringwald         };
15249d9bbc01Smatthias.ringwald     }
15259d9bbc01Smatthias.ringwald     return NULL;
15269d9bbc01Smatthias.ringwald }
15279d9bbc01Smatthias.ringwald 
15287192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
15297192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
15307192e786SMatthias Ringwald }
15317192e786SMatthias Ringwald 
1532e0abb8e7S[email protected] 
1533be2053a6SMatthias 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){
1534be2053a6SMatthias Ringwald 
1535be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1536e0abb8e7S[email protected] 
15374bb582b6Smatthias.ringwald     // check for alread registered psm
15389d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1539277abc2cSmatthias.ringwald     if (service) {
1540be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1541be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1542277abc2cSmatthias.ringwald     }
15439d9bbc01Smatthias.ringwald 
15444bb582b6Smatthias.ringwald     // alloc structure
1545bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1546277abc2cSmatthias.ringwald     if (!service) {
1547be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1548be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1549277abc2cSmatthias.ringwald     }
15509d9bbc01Smatthias.ringwald 
15519d9bbc01Smatthias.ringwald     // fill in
15529d9bbc01Smatthias.ringwald     service->psm = psm;
15539d9bbc01Smatthias.ringwald     service->mtu = mtu;
155405ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1555df3354fcS[email protected]     service->required_security_level = security_level;
15569d9bbc01Smatthias.ringwald 
15579d9bbc01Smatthias.ringwald     // add to services list
1558665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1559c0e866bfSmatthias.ringwald 
1560c0e866bfSmatthias.ringwald     // enable page scan
156115a95bd5SMatthias Ringwald     gap_connectable_control(1);
15625842b6d9Smatthias.ringwald 
1563be2053a6SMatthias Ringwald     return 0;
15649d9bbc01Smatthias.ringwald }
15659d9bbc01Smatthias.ringwald 
156602f83142SMatthias Ringwald void l2cap_unregister_service(uint16_t psm){
1567e0abb8e7S[email protected] 
1568e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1569e0abb8e7S[email protected] 
15709d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1571037d6e48Smatthias.ringwald     if (!service) return;
1572665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1573d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1574c0e866bfSmatthias.ringwald 
1575c0e866bfSmatthias.ringwald     // disable page scan when no services registered
1576665d90f2SMatthias Ringwald     if (!btstack_linked_list_empty(&l2cap_services)) return;
157715a95bd5SMatthias Ringwald     gap_connectable_control(0);
15789d9bbc01Smatthias.ringwald }
15799d9bbc01Smatthias.ringwald 
15805652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
158105ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
15825628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
15835628cf69SMatthias Ringwald     if (index < 0) return;
15845628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
15855652b5ffS[email protected] }
15865652b5ffS[email protected] 
1587a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
15887192e786SMatthias Ringwald 
15897192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
15907192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
15917192e786SMatthias Ringwald }
15927192e786SMatthias Ringwald /**
15937192e786SMatthias Ringwald  * @brief Regster L2CAP LE Credit Based Flow Control Mode service
15947192e786SMatthias Ringwald  * @param
15957192e786SMatthias Ringwald  */
1596*da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
15977192e786SMatthias Ringwald 
1598*da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
15997192e786SMatthias Ringwald 
16007192e786SMatthias Ringwald     // check for alread registered psm
16017192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
16027192e786SMatthias Ringwald     if (service) {
1603*da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
16047192e786SMatthias Ringwald     }
16057192e786SMatthias Ringwald 
16067192e786SMatthias Ringwald     // alloc structure
16077192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
16087192e786SMatthias Ringwald     if (!service) {
16097192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
1610*da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
16117192e786SMatthias Ringwald     }
16127192e786SMatthias Ringwald 
16137192e786SMatthias Ringwald     // fill in
16147192e786SMatthias Ringwald     service->psm = psm;
1615*da144af5SMatthias Ringwald     service->mtu = 0;
16167192e786SMatthias Ringwald     service->packet_handler = packet_handler;
16177192e786SMatthias Ringwald     service->required_security_level = security_level;
16187192e786SMatthias Ringwald 
16197192e786SMatthias Ringwald     // add to services list
1620665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
16217192e786SMatthias Ringwald 
16227192e786SMatthias Ringwald     // done
1623*da144af5SMatthias Ringwald     return 0;
16247192e786SMatthias Ringwald }
16257192e786SMatthias Ringwald 
1626*da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
16277192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
16287192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
1629*da144af5SMatthias Ringwald     if (!service) return L2CAP_SERVICE_NOT_FOUND;
1630*da144af5SMatthias Ringwald 
1631665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
16327192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
1633*da144af5SMatthias Ringwald     return 0;
16347192e786SMatthias Ringwald }
1635*da144af5SMatthias Ringwald 
1636*da144af5SMatthias Ringwald /*
1637*da144af5SMatthias Ringwald  * @brief Accept incoming LE Data Channel connection
1638*da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1639*da144af5SMatthias Ringwald  * @param receive_buffer        buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU
1640*da144af5SMatthias Ringwald  * @param receive_buffer_size   buffer size equals MTU
1641*da144af5SMatthias Ringwald  * @param initial_credits       Number of initial credits provided to peer
1642*da144af5SMatthias Ringwald  */
1643*da144af5SMatthias Ringwald 
1644*da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
1645*da144af5SMatthias Ringwald     return 0;
1646*da144af5SMatthias Ringwald }
1647*da144af5SMatthias Ringwald 
1648*da144af5SMatthias Ringwald /**
1649*da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
1650*da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1651*da144af5SMatthias Ringwald  */
1652*da144af5SMatthias Ringwald 
1653*da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
1654*da144af5SMatthias Ringwald     return 0;
1655*da144af5SMatthias Ringwald }
1656*da144af5SMatthias Ringwald 
1657*da144af5SMatthias Ringwald /**
1658*da144af5SMatthias Ringwald  * @brief Create LE Data Channel
1659*da144af5SMatthias Ringwald  * @param packet_handler        Packet handler for this connection
1660*da144af5SMatthias Ringwald  * @param address               Peer address
1661*da144af5SMatthias Ringwald  * @param address_type          Peer address type
1662*da144af5SMatthias Ringwald  * @param psm                   Service PSM to connect to
1663*da144af5SMatthias Ringwald  * @param receive_buffer        buffer used for reassembly of L2CAP LE Information Frames into service data unit (SDU) with given MTU
1664*da144af5SMatthias Ringwald  * @param receive_buffer_size   buffer size equals MTU
1665*da144af5SMatthias Ringwald  * @param initial_credits       Number of initial credits provided to peer
1666*da144af5SMatthias Ringwald  * @param security_level        Minimum required security level
1667*da144af5SMatthias Ringwald  * @param out_local_cid         L2CAP LE Channel Identifier is stored here
1668*da144af5SMatthias Ringwald  */
1669*da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
1670*da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
1671*da144af5SMatthias Ringwald     uint16_t * out_local_cid)
1672*da144af5SMatthias Ringwald {
1673*da144af5SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
1674*da144af5SMatthias Ringwald 
1675*da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, mtu, LEVEL_0);
1676*da144af5SMatthias Ringwald     if (!channel) {
1677*da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1678*da144af5SMatthias Ringwald     }
1679*da144af5SMatthias Ringwald 
1680*da144af5SMatthias Ringwald     // add to connections list
1681*da144af5SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1682*da144af5SMatthias Ringwald 
1683*da144af5SMatthias Ringwald     // store local_cid
1684*da144af5SMatthias Ringwald     if (out_local_cid){
1685*da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
1686*da144af5SMatthias Ringwald     }
1687*da144af5SMatthias Ringwald 
1688*da144af5SMatthias Ringwald     // check if hci connection is already usable
1689*da144af5SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type);
1690*da144af5SMatthias Ringwald     if (conn){
1691*da144af5SMatthias Ringwald         log_info("l2cap_le_create_channel, hci connection already exists");
1692*da144af5SMatthias Ringwald         // l2cap_handle_connection_complete(conn->con_handle, channel);
1693*da144af5SMatthias Ringwald         // check if remote supported fearures are already received
1694*da144af5SMatthias Ringwald         // if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
1695*da144af5SMatthias Ringwald         //     l2cap_handle_remote_supported_features_received(channel);
1696*da144af5SMatthias Ringwald         // }
1697*da144af5SMatthias Ringwald     }
1698*da144af5SMatthias Ringwald 
1699*da144af5SMatthias Ringwald     l2cap_run();
1700*da144af5SMatthias Ringwald 
1701*da144af5SMatthias Ringwald     return 0;
1702*da144af5SMatthias Ringwald }
1703*da144af5SMatthias Ringwald 
1704*da144af5SMatthias Ringwald /**
1705*da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
1706*da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1707*da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
1708*da144af5SMatthias Ringwald  */
1709*da144af5SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t cid, uint16_t credits){
1710*da144af5SMatthias Ringwald     return 0;
1711*da144af5SMatthias Ringwald }
1712*da144af5SMatthias Ringwald 
1713*da144af5SMatthias Ringwald /**
1714*da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
1715*da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1716*da144af5SMatthias Ringwald  */
1717*da144af5SMatthias Ringwald int l2cap_le_can_send_now(uint16_t cid){
1718*da144af5SMatthias Ringwald     return 0;
1719*da144af5SMatthias Ringwald }
1720*da144af5SMatthias Ringwald 
1721*da144af5SMatthias Ringwald /**
1722*da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
1723*da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
1724*da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
1725*da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1726*da144af5SMatthias Ringwald  */
1727*da144af5SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t cid){
1728*da144af5SMatthias Ringwald     return 0;
1729*da144af5SMatthias Ringwald }
1730*da144af5SMatthias Ringwald 
1731*da144af5SMatthias Ringwald /**
1732*da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
1733*da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
1734*da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1735*da144af5SMatthias Ringwald  * @param data                  data to send
1736*da144af5SMatthias Ringwald  * @param size                  data size
1737*da144af5SMatthias Ringwald  */
1738*da144af5SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t cid, uint8_t * data, uint16_t size){
1739*da144af5SMatthias Ringwald     return 0;
1740*da144af5SMatthias Ringwald }
1741*da144af5SMatthias Ringwald 
1742*da144af5SMatthias Ringwald /**
1743*da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
1744*da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1745*da144af5SMatthias Ringwald  */
1746*da144af5SMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t cid)
1747*da144af5SMatthias Ringwald {
1748*da144af5SMatthias Ringwald     return 0;
1749*da144af5SMatthias Ringwald }
1750*da144af5SMatthias Ringwald 
17517f02f414SMatthias Ringwald #endif
1752