xref: /btstack/src/l2cap.c (revision 9367f9b01ebb1b4dbaf0b9703682c89f12ab84f0)
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
713efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
714efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
715efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
716efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
717efedfb4cSMatthias Ringwald         switch (channel->state){
718efedfb4cSMatthias Ringwald             default:
719efedfb4cSMatthias Ringwald                 break;
720efedfb4cSMatthias Ringwald         }
721efedfb4cSMatthias Ringwald     }
722efedfb4cSMatthias Ringwald 
723da886c03S[email protected]     // send l2cap con paramter update if necessary
724da886c03S[email protected]     hci_connections_get_iterator(&it);
725665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
726665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7275d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
728b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
729da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
730b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
731b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
732b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
733b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
734b68d7bc3SMatthias Ringwald                 break;
735da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
736b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
737b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
738da886c03S[email protected]                 break;
739da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
740b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
741b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
742da886c03S[email protected]                 break;
743da886c03S[email protected]             default:
744da886c03S[email protected]                 break;
745da886c03S[email protected]         }
746efedfb4cSMatthias Ringwald #if 0
747efedfb4cSMatthias Ringwald void l2cap_le_run(void){
748efedfb4cSMatthias Ringwald     for all channels
749efedfb4cSMatthias Ringwald         switch(state):
750efedfb4cSMatthias Ringwald 
751efedfb4cSMatthias Ringwald     if outgoing transfer active
752efedfb4cSMatthias Ringwald         send next chunk
753efedfb4cSMatthias Ringwald         if done, notify app
754efedfb4cSMatthias Ringwald }
755efedfb4cSMatthias Ringwald #endif
756da886c03S[email protected]     }
7574d7157c3S[email protected] #endif
758da886c03S[email protected] 
75922c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
7602cd0be45Smatthias.ringwald }
7612cd0be45Smatthias.ringwald 
7624aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7634ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
764fa8c92f6Smatthias.ringwald }
765fa8c92f6Smatthias.ringwald 
76671de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
7674ff786cfS[email protected]     return l2cap_max_mtu();
768e5e1518dS[email protected] }
769e5e1518dS[email protected] 
770fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
7712df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7725533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7732df5dadcS[email protected]         // success, start l2cap handshake
774fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
7752df5dadcS[email protected]         // check remote SSP feature first
7762df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7772df5dadcS[email protected]     }
7782df5dadcS[email protected] }
7792df5dadcS[email protected] 
780efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE
781efedfb4cSMatthias Ringwald static void l2cap_handle_le_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
782efedfb4cSMatthias Ringwald     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
783efedfb4cSMatthias Ringwald         log_info("l2cap_le_handle_connection_complete expected state");
784efedfb4cSMatthias Ringwald         // success, start l2cap handshake
785efedfb4cSMatthias Ringwald         channel->con_handle = con_handle;
786efedfb4cSMatthias Ringwald         // ...
787efedfb4cSMatthias Ringwald     }
788efedfb4cSMatthias Ringwald }
789efedfb4cSMatthias Ringwald #endif
790efedfb4cSMatthias Ringwald 
791efedfb4cSMatthias Ringwald 
7922df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
7932df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
7942df5dadcS[email protected] 
7952df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
796ac301f95S[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));
797fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
7982df5dadcS[email protected]         // request security level 2
7992df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
800fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
8012df5dadcS[email protected]         return;
8022df5dadcS[email protected]     }
8032df5dadcS[email protected]     // fine, go ahead
8042df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
8052df5dadcS[email protected] }
8062df5dadcS[email protected] 
807da144af5SMatthias 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,
808da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
809da144af5SMatthias Ringwald 
810da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
811da144af5SMatthias Ringwald     if (!channel) {
812da144af5SMatthias Ringwald         return NULL;
813da144af5SMatthias Ringwald     }
814da144af5SMatthias Ringwald 
815da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
816da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
817da144af5SMatthias Ringwald 
818da144af5SMatthias Ringwald     // fill in
819da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
820da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
821da144af5SMatthias Ringwald     channel->address_type = address_type;
822da144af5SMatthias Ringwald     channel->psm = psm;
823da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
824da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
825da144af5SMatthias Ringwald     channel->required_security_level = security_level;
826da144af5SMatthias Ringwald 
827da144af5SMatthias Ringwald     //
828da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
829da144af5SMatthias Ringwald     channel->con_handle = 0;
830da144af5SMatthias Ringwald 
831da144af5SMatthias Ringwald     // set initial state
832da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
833da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
834da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
835da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
836da144af5SMatthias Ringwald     return channel;
837da144af5SMatthias Ringwald }
838da144af5SMatthias Ringwald 
8399077cb15SMatthias Ringwald /**
8409077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
8419077cb15SMatthias Ringwald  * @param packet_handler
8429077cb15SMatthias Ringwald  * @param address
8439077cb15SMatthias Ringwald  * @param psm
8449077cb15SMatthias Ringwald  * @param mtu
8459077cb15SMatthias Ringwald  * @param local_cid
8469077cb15SMatthias Ringwald  */
8479077cb15SMatthias Ringwald 
848da144af5SMatthias 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){
849da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
850da144af5SMatthias Ringwald 
851da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
852da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
853da144af5SMatthias Ringwald     }
854da144af5SMatthias Ringwald 
855da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
856fc64f94aSMatthias Ringwald     if (!channel) {
8579077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
8589077cb15SMatthias Ringwald     }
8599077cb15SMatthias Ringwald 
8609077cb15SMatthias Ringwald     // add to connections list
861fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
8629077cb15SMatthias Ringwald 
8639077cb15SMatthias Ringwald     // store local_cid
8649077cb15SMatthias Ringwald     if (out_local_cid){
865fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
8669077cb15SMatthias Ringwald     }
8679077cb15SMatthias Ringwald 
8689077cb15SMatthias Ringwald     // check if hci connection is already usable
8699077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
8709077cb15SMatthias Ringwald     if (conn){
871add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
872fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
8739077cb15SMatthias Ringwald         // check if remote supported fearures are already received
8749077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
875fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
8769077cb15SMatthias Ringwald         }
8779077cb15SMatthias Ringwald     }
8789077cb15SMatthias Ringwald 
8799077cb15SMatthias Ringwald     l2cap_run();
8809077cb15SMatthias Ringwald 
8819077cb15SMatthias Ringwald     return 0;
8829077cb15SMatthias Ringwald }
8839077cb15SMatthias Ringwald 
884ce8f182eSMatthias Ringwald void
885ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
886e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
887b35f641cSmatthias.ringwald     // find channel for local_cid
888b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
889f62db1e3Smatthias.ringwald     if (channel) {
890e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
891f62db1e3Smatthias.ringwald     }
8922cd0be45Smatthias.ringwald     // process
8932cd0be45Smatthias.ringwald     l2cap_run();
89443625864Smatthias.ringwald }
8951e6aba47Smatthias.ringwald 
896afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
897665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
898665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
899665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
900665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
901058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
902c22aecc9S[email protected]         // channel for this address found
903c22aecc9S[email protected]         switch (channel->state){
904c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
905c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
906afde0c52Smatthias.ringwald                 // failure, forward error code
907afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
908afde0c52Smatthias.ringwald                 // discard channel
9099dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
910665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
911d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
912c22aecc9S[email protected]                 break;
913c22aecc9S[email protected]             default:
914c22aecc9S[email protected]                 break;
915afde0c52Smatthias.ringwald         }
916afde0c52Smatthias.ringwald     }
917afde0c52Smatthias.ringwald }
918afde0c52Smatthias.ringwald 
919afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
920665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
921665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
922665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
923665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
924058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
9252df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
926afde0c52Smatthias.ringwald         }
927afde0c52Smatthias.ringwald     }
9286fdcc387Smatthias.ringwald     // process
9296fdcc387Smatthias.ringwald     l2cap_run();
930afde0c52Smatthias.ringwald }
931b448a0e7Smatthias.ringwald 
93233c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
93333c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
93433c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
93533c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
93633c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
93733c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
938fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
93933c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
94034e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
94133c40538SMatthias Ringwald     }
9422125de09SMatthias Ringwald 
9432125de09SMatthias Ringwald     int i;
9442125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
945773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
9462125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
94734e7e577SMatthias Ringwald         int can_send;
94834e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
94934e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
95034e7e577SMatthias Ringwald         } else {
95134e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
95234e7e577SMatthias Ringwald         }
95334e7e577SMatthias Ringwald         if (!can_send) continue;
95434e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
95534e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
9562125de09SMatthias Ringwald     }
95733c40538SMatthias Ringwald }
95833c40538SMatthias Ringwald 
959d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
960afde0c52Smatthias.ringwald 
961afde0c52Smatthias.ringwald     bd_addr_t address;
962afde0c52Smatthias.ringwald     hci_con_handle_t handle;
963665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
9642d00edd4Smatthias.ringwald     int hci_con_used;
965afde0c52Smatthias.ringwald 
9660e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
967afde0c52Smatthias.ringwald 
968afde0c52Smatthias.ringwald         // handle connection complete events
969afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
970724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
971afde0c52Smatthias.ringwald             if (packet[2] == 0){
972f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
973afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
974afde0c52Smatthias.ringwald             } else {
975afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
976afde0c52Smatthias.ringwald             }
977afde0c52Smatthias.ringwald             break;
978afde0c52Smatthias.ringwald 
979afde0c52Smatthias.ringwald         // handle successful create connection cancel command
980afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
981073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
982afde0c52Smatthias.ringwald                 if (packet[5] == 0){
983724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
984afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
985afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
98603cfbabcSmatthias.ringwald                 }
9871e6aba47Smatthias.ringwald             }
98839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
98939d59809Smatthias.ringwald             break;
99039d59809Smatthias.ringwald 
99139d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
99239d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
993afde0c52Smatthias.ringwald             break;
99427a923d0Smatthias.ringwald 
9951e6aba47Smatthias.ringwald         // handle disconnection complete events
996afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
997c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
998f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
999665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1000665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1001665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1002fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
100315ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
10049dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1005665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1006d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
100727a923d0Smatthias.ringwald             }
1008afde0c52Smatthias.ringwald             break;
1009fcadd0caSmatthias.ringwald 
101033c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
101163fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
10126218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
101302b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
101433c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
10156218e6f1Smatthias.ringwald             break;
10166218e6f1Smatthias.ringwald 
1017ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
1018afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1019f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1020bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
102180ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
10222d00edd4Smatthias.ringwald             hci_con_used = 0;
1023665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1024665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1025665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1026fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10272d00edd4Smatthias.ringwald                 hci_con_used = 1;
1028c22aecc9S[email protected]                 break;
1029ee091cf1Smatthias.ringwald             }
10302d00edd4Smatthias.ringwald             if (hci_con_used) break;
1031d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
10329edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1033afde0c52Smatthias.ringwald             break;
1034ee091cf1Smatthias.ringwald 
1035df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1036f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1037665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1038665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1039665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1040fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10412df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1042df3354fcS[email protected]                 break;
1043df3354fcS[email protected]             }
1044c22aecc9S[email protected]             break;
1045df3354fcS[email protected] 
10465611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1047f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1048bd63148eS[email protected]             log_info("l2cap - security level update");
1049665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1050665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1051665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1052fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10535533f01eS[email protected] 
1054bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1055bd63148eS[email protected] 
1056e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
10575533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
10585533f01eS[email protected] 
1059df3354fcS[email protected]                 switch (channel->state){
1060df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
10615533f01eS[email protected]                         if (actual_level >= required_level){
1062f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1063f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
10641eb2563eS[email protected]                         } else {
1065775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
10661eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
10671eb2563eS[email protected]                         }
1068df3354fcS[email protected]                         break;
1069df3354fcS[email protected] 
1070df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
10715533f01eS[email protected]                         if (actual_level >= required_level){
1072df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1073df3354fcS[email protected]                         } else {
1074df3354fcS[email protected]                             // disconnnect, authentication not good enough
1075df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1076df3354fcS[email protected]                         }
1077df3354fcS[email protected]                         break;
1078df3354fcS[email protected] 
1079df3354fcS[email protected]                     default:
1080df3354fcS[email protected]                         break;
1081df3354fcS[email protected]                 }
1082f85a9399S[email protected]             }
1083f85a9399S[email protected]             break;
1084f85a9399S[email protected] 
1085afde0c52Smatthias.ringwald         default:
1086afde0c52Smatthias.ringwald             break;
1087afde0c52Smatthias.ringwald     }
1088afde0c52Smatthias.ringwald 
1089bd63148eS[email protected]     l2cap_run();
10901e6aba47Smatthias.ringwald }
10911e6aba47Smatthias.ringwald 
1092afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1093b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1094e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1095e7ff783cSmatthias.ringwald     l2cap_run();
109684836b65Smatthias.ringwald }
109784836b65Smatthias.ringwald 
10982b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
10994cf56b4aSmatthias.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."
11002b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
11012b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
11022b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
11032b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
11042b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
11052b360848Smatthias.ringwald         signaling_responses_pending++;
11062b360848Smatthias.ringwald         l2cap_run();
11072b360848Smatthias.ringwald     }
11082b360848Smatthias.ringwald }
11092b360848Smatthias.ringwald 
1110b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1111645658c9Smatthias.ringwald 
11129da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1113645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1114645658c9Smatthias.ringwald     if (!service) {
1115645658c9Smatthias.ringwald         // 0x0002 PSM not supported
11162b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1117645658c9Smatthias.ringwald         return;
1118645658c9Smatthias.ringwald     }
1119645658c9Smatthias.ringwald 
11205061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1121645658c9Smatthias.ringwald     if (!hci_connection) {
11222b360848Smatthias.ringwald         //
11239da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1124645658c9Smatthias.ringwald         return;
1125645658c9Smatthias.ringwald     }
11262bd8b7e7S[email protected] 
1127645658c9Smatthias.ringwald     // alloc structure
11289da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1129da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1130da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
11312b360848Smatthias.ringwald     if (!channel){
11322b360848Smatthias.ringwald         // 0x0004 No resources available
11332b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
11342b360848Smatthias.ringwald         return;
11352b360848Smatthias.ringwald     }
1136da144af5SMatthias Ringwald 
1137fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1138b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1139b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1140645658c9Smatthias.ringwald 
1141f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
11422985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
11432985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
11449775e25bSmatthias.ringwald     }
11459775e25bSmatthias.ringwald 
1146645658c9Smatthias.ringwald     // set initial state
1147df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1148ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1149e405ae81Smatthias.ringwald 
1150645658c9Smatthias.ringwald     // add to connections list
1151665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1152645658c9Smatthias.ringwald 
1153f85a9399S[email protected]     // assert security requirements
11541eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1155e405ae81Smatthias.ringwald }
1156645658c9Smatthias.ringwald 
1157ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1158e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1159b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1160e405ae81Smatthias.ringwald     if (!channel) {
1161ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1162e405ae81Smatthias.ringwald         return;
1163e405ae81Smatthias.ringwald     }
1164e405ae81Smatthias.ringwald 
1165552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1166e405ae81Smatthias.ringwald 
1167552d92a1Smatthias.ringwald     // process
1168552d92a1Smatthias.ringwald     l2cap_run();
1169e405ae81Smatthias.ringwald }
1170645658c9Smatthias.ringwald 
11717ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
11727ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1173b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1174e405ae81Smatthias.ringwald     if (!channel) {
1175ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1176e405ae81Smatthias.ringwald         return;
1177e405ae81Smatthias.ringwald     }
1178e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11797ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1180e7ff783cSmatthias.ringwald     l2cap_run();
1181645658c9Smatthias.ringwald }
1182645658c9Smatthias.ringwald 
11837f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1184b1988dceSmatthias.ringwald 
1185b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1186b1988dceSmatthias.ringwald 
1187f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
118863a7246aSmatthias.ringwald     if (flags & 1) {
118963a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
119063a7246aSmatthias.ringwald     }
119163a7246aSmatthias.ringwald 
11922784b77dSmatthias.ringwald     // accept the other's configuration options
1193f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
11943de7c0caSmatthias.ringwald     uint16_t pos     = 8;
11953de7c0caSmatthias.ringwald     while (pos < end_pos){
119663a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
119763a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
119863a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
119963a7246aSmatthias.ringwald         pos++;
12001dc511deSmatthias.ringwald         uint8_t length = command[pos++];
12011dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
120263a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1203f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
12049da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
120563a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
120663a7246aSmatthias.ringwald         }
12070fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
12080fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1209f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
12100fe7a9d0S[email protected]         }
121163a7246aSmatthias.ringwald         // check for unknown options
121263a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1213c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
121463a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
12151dc511deSmatthias.ringwald         }
12161dc511deSmatthias.ringwald         pos += length;
12171dc511deSmatthias.ringwald     }
12182784b77dSmatthias.ringwald }
12192784b77dSmatthias.ringwald 
1220fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
12219da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
122273cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
122373cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1224019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1225019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1226fa8473a4Smatthias.ringwald     return 1;
1227fa8473a4Smatthias.ringwald }
1228fa8473a4Smatthias.ringwald 
1229fa8473a4Smatthias.ringwald 
12307f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
12311e6aba47Smatthias.ringwald 
123200d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
123300d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
123438e5900eSmatthias.ringwald     uint16_t result = 0;
12351e6aba47Smatthias.ringwald 
12369da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1237b35f641cSmatthias.ringwald 
12389a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
12399a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
12409a011532Smatthias.ringwald         switch (channel->state){
1241fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
12429a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
12432b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
12449a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
12459a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
12469a011532Smatthias.ringwald                 break;
12479a011532Smatthias.ringwald 
12489a011532Smatthias.ringwald             default:
12499a011532Smatthias.ringwald                 // ignore in other states
12509a011532Smatthias.ringwald                 break;
12519a011532Smatthias.ringwald         }
12529a011532Smatthias.ringwald         return;
12539a011532Smatthias.ringwald     }
12549a011532Smatthias.ringwald 
125556081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
12561e6aba47Smatthias.ringwald     switch (channel->state) {
12571e6aba47Smatthias.ringwald 
12581e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
12591e6aba47Smatthias.ringwald             switch (code){
12601e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
12615932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1262f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
126338e5900eSmatthias.ringwald                     switch (result) {
126438e5900eSmatthias.ringwald                         case 0:
1265169f8b28Smatthias.ringwald                             // successful connection
1266f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1267fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
126828ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
126938e5900eSmatthias.ringwald                             break;
127038e5900eSmatthias.ringwald                         case 1:
12715932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
12725932bd7cS[email protected]                             l2cap_start_ertx(channel);
127338e5900eSmatthias.ringwald                             break;
127438e5900eSmatthias.ringwald                         default:
1275eb920dbeSmatthias.ringwald                             // channel closed
1276eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1277f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
127838e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1279eb920dbeSmatthias.ringwald 
1280eb920dbeSmatthias.ringwald                             // drop link key if security block
1281eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
128215a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1283eb920dbeSmatthias.ringwald                             }
1284eb920dbeSmatthias.ringwald 
1285eb920dbeSmatthias.ringwald                             // discard channel
1286665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1287d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
128838e5900eSmatthias.ringwald                             break;
12891e6aba47Smatthias.ringwald                     }
12901e6aba47Smatthias.ringwald                     break;
129138e5900eSmatthias.ringwald 
129238e5900eSmatthias.ringwald                 default:
12931e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
129438e5900eSmatthias.ringwald                     break;
12951e6aba47Smatthias.ringwald             }
12961e6aba47Smatthias.ringwald             break;
12971e6aba47Smatthias.ringwald 
1298fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1299f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1300ae280e73Smatthias.ringwald             switch (code) {
1301ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
130228ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1303ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
130463a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
130563a7246aSmatthias.ringwald                         // only done if continuation not set
130663a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
130763a7246aSmatthias.ringwald                     }
1308ae280e73Smatthias.ringwald                     break;
13091e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
13105932bd7cS[email protected]                     l2cap_stop_rtx(channel);
13115932bd7cS[email protected]                     switch (result){
13125932bd7cS[email protected]                         case 0: // success
13135932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
13145932bd7cS[email protected]                             break;
13155932bd7cS[email protected]                         case 4: // pending
13165932bd7cS[email protected]                             l2cap_start_ertx(channel);
13175932bd7cS[email protected]                             break;
13185932bd7cS[email protected]                         default:
1319fe9d8984S[email protected]                             // retry on negative result
1320fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1321fe9d8984S[email protected]                             break;
1322fe9d8984S[email protected]                     }
13235a67bd4aSmatthias.ringwald                     break;
13245a67bd4aSmatthias.ringwald                 default:
13255a67bd4aSmatthias.ringwald                     break;
13261e6aba47Smatthias.ringwald             }
1327fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1328fa8473a4Smatthias.ringwald                 // for open:
13295a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1330fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1331c8e4258aSmatthias.ringwald             }
1332c8e4258aSmatthias.ringwald             break;
1333f62db1e3Smatthias.ringwald 
1334f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1335f62db1e3Smatthias.ringwald             switch (code) {
1336f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
133727a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
133827a923d0Smatthias.ringwald                     break;
13395a67bd4aSmatthias.ringwald                 default:
13405a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
13415a67bd4aSmatthias.ringwald                     break;
134227a923d0Smatthias.ringwald             }
134327a923d0Smatthias.ringwald             break;
134484836b65Smatthias.ringwald 
134584836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
134684836b65Smatthias.ringwald             // @TODO handle incoming requests
134784836b65Smatthias.ringwald             break;
134884836b65Smatthias.ringwald 
134984836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
135084836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
135184836b65Smatthias.ringwald             break;
135210642e45Smatthias.ringwald         default:
135310642e45Smatthias.ringwald             break;
135427a923d0Smatthias.ringwald     }
13559da54300S[email protected]     // log_info("new state %u", channel->state);
135627a923d0Smatthias.ringwald }
135727a923d0Smatthias.ringwald 
135800d93d79Smatthias.ringwald 
13597f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
136000d93d79Smatthias.ringwald 
136100d93d79Smatthias.ringwald     // get code, signalind identifier and command len
136200d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
136300d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
136400d93d79Smatthias.ringwald 
136500d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
136600d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
136763a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
136800d93d79Smatthias.ringwald         return;
136900d93d79Smatthias.ringwald     }
137000d93d79Smatthias.ringwald 
137100d93d79Smatthias.ringwald     // general commands without an assigned channel
137200d93d79Smatthias.ringwald     switch(code) {
137300d93d79Smatthias.ringwald 
137400d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1375f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1376f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
137700d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
13782b83fb7dSmatthias.ringwald             return;
137900d93d79Smatthias.ringwald         }
138000d93d79Smatthias.ringwald 
13812b360848Smatthias.ringwald         case ECHO_REQUEST:
13822b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
13832b83fb7dSmatthias.ringwald             return;
138400d93d79Smatthias.ringwald 
138500d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1386f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
13872b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
13882b83fb7dSmatthias.ringwald             return;
138900d93d79Smatthias.ringwald         }
139000d93d79Smatthias.ringwald 
139100d93d79Smatthias.ringwald         default:
139200d93d79Smatthias.ringwald             break;
139300d93d79Smatthias.ringwald     }
139400d93d79Smatthias.ringwald 
139500d93d79Smatthias.ringwald 
139600d93d79Smatthias.ringwald     // Get potential destination CID
1397f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
139800d93d79Smatthias.ringwald 
139900d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1400665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1401665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1402665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1403665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1404fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
140500d93d79Smatthias.ringwald         if (code & 1) {
1406b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1407b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
140800d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
14094e32727eSmatthias.ringwald                 break;
141000d93d79Smatthias.ringwald             }
141100d93d79Smatthias.ringwald         } else {
1412b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
141300d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
141400d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
14154e32727eSmatthias.ringwald                 break;
141600d93d79Smatthias.ringwald             }
141700d93d79Smatthias.ringwald         }
141800d93d79Smatthias.ringwald     }
141900d93d79Smatthias.ringwald }
142000d93d79Smatthias.ringwald 
14213d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
142200d93d79Smatthias.ringwald 
142300d93d79Smatthias.ringwald     // Get Channel ID
142400d93d79Smatthias.ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
142500d93d79Smatthias.ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
142600d93d79Smatthias.ringwald 
14275652b5ffS[email protected]     switch (channel_id) {
14285652b5ffS[email protected] 
14295652b5ffS[email protected]         case L2CAP_CID_SIGNALING: {
14305652b5ffS[email protected] 
143100d93d79Smatthias.ringwald             uint16_t command_offset = 8;
143200d93d79Smatthias.ringwald             while (command_offset < size) {
143300d93d79Smatthias.ringwald 
143400d93d79Smatthias.ringwald                 // handle signaling commands
143500d93d79Smatthias.ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
143600d93d79Smatthias.ringwald 
143700d93d79Smatthias.ringwald                 // increment command_offset
1438f8fbdce0SMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
143900d93d79Smatthias.ringwald             }
14405652b5ffS[email protected]             break;
144100d93d79Smatthias.ringwald         }
144200d93d79Smatthias.ringwald 
14435652b5ffS[email protected]         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
14445628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
14455628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
14465652b5ffS[email protected]             }
14475652b5ffS[email protected]             break;
14485652b5ffS[email protected] 
14495652b5ffS[email protected]         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
14505628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
14515628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
14525652b5ffS[email protected]             }
14535652b5ffS[email protected]             break;
14545652b5ffS[email protected] 
1455462e630dS[email protected]         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
14565628cf69SMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
14575628cf69SMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1458462e630dS[email protected]             }
1459462e630dS[email protected]             break;
1460462e630dS[email protected] 
14611bbc0b23S[email protected]         case L2CAP_CID_SIGNALING_LE: {
1462ccf076adS[email protected]             switch (packet[8]){
1463ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_RESPONSE: {
1464f8fbdce0SMatthias Ringwald                     uint16_t result = little_endian_read_16(packet, 12);
1465ccf076adS[email protected]                     l2cap_emit_connection_parameter_update_response(handle, result);
1466ccf076adS[email protected]                     break;
1467ccf076adS[email protected]                 }
1468ccf076adS[email protected]                 case CONNECTION_PARAMETER_UPDATE_REQUEST: {
1469ccf076adS[email protected]                     uint8_t event[10];
1470ccf076adS[email protected]                     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1471ccf076adS[email protected]                     event[1] = 8;
1472ccf076adS[email protected]                     memcpy(&event[2], &packet[12], 8);
1473da886c03S[email protected] 
1474da886c03S[email protected]                     hci_connection_t * connection = hci_connection_for_handle(handle);
1475da886c03S[email protected]                     if (connection){
14766d91fb6cSMatthias Ringwald                         if (connection->role != HCI_ROLE_MASTER){
14776d91fb6cSMatthias Ringwald                             // reject command without notifying upper layer when not in master role
14786d91fb6cSMatthias Ringwald                             uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
14796d91fb6cSMatthias Ringwald                             l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
14806d91fb6cSMatthias Ringwald                             break;
14816d91fb6cSMatthias Ringwald                         }
1482da886c03S[email protected]                         int update_parameter = 1;
1483a4c06b28SMatthias Ringwald                         le_connection_parameter_range_t existing_range;
14844ced4e8cSMatthias Ringwald                         gap_get_connection_parameter_range(&existing_range);
1485f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_min = little_endian_read_16(packet,12);
1486f8fbdce0SMatthias Ringwald                         uint16_t le_conn_interval_max = little_endian_read_16(packet,14);
1487f8fbdce0SMatthias Ringwald                         uint16_t le_conn_latency = little_endian_read_16(packet,16);
1488f8fbdce0SMatthias Ringwald                         uint16_t le_supervision_timeout = little_endian_read_16(packet,18);
1489da886c03S[email protected] 
1490da886c03S[email protected]                         if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1491da886c03S[email protected]                         if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1492da886c03S[email protected] 
1493da886c03S[email protected]                         if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1494da886c03S[email protected]                         if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1495da886c03S[email protected] 
1496da886c03S[email protected]                         if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1497da886c03S[email protected]                         if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1498da886c03S[email protected] 
1499da886c03S[email protected]                         if (update_parameter){
1500da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1501da886c03S[email protected]                             connection->le_conn_interval_min = le_conn_interval_min;
1502da886c03S[email protected]                             connection->le_conn_interval_max = le_conn_interval_max;
1503da886c03S[email protected]                             connection->le_conn_latency = le_conn_latency;
1504da886c03S[email protected]                             connection->le_supervision_timeout = le_supervision_timeout;
1505da886c03S[email protected]                         } else {
1506da886c03S[email protected]                             connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1507da886c03S[email protected]                         }
150879f53f1dSMatthias Ringwald                         connection->le_con_param_update_identifier = packet[COMPLETE_L2CAP_HEADER + 1];
1509da886c03S[email protected]                     }
1510da886c03S[email protected] 
1511ccf076adS[email protected]                     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
151233c40538SMatthias Ringwald                     if (!l2cap_event_packet_handler) break;
151333c40538SMatthias Ringwald                     (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1514ccf076adS[email protected]                     break;
1515ccf076adS[email protected]                 }
1516ccf076adS[email protected]                 default: {
15171bbc0b23S[email protected]                     uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
15181bbc0b23S[email protected]                     l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
15191bbc0b23S[email protected]                     break;
15201bbc0b23S[email protected]                 }
1521ccf076adS[email protected]             }
1522ccf076adS[email protected]             break;
1523ccf076adS[email protected]         }
15241bbc0b23S[email protected] 
15255652b5ffS[email protected]         default: {
152600d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
15273d50b4baSMatthias Ringwald             l2cap_channel_t * l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1528d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
15293d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
153000d93d79Smatthias.ringwald             }
15315652b5ffS[email protected]             break;
15325652b5ffS[email protected]         }
15335652b5ffS[email protected]     }
153400d93d79Smatthias.ringwald 
15351eb2563eS[email protected]     l2cap_run();
15362718e2e7Smatthias.ringwald }
153700d93d79Smatthias.ringwald 
153815ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
153927a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1540f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1541f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1542f62db1e3Smatthias.ringwald     // discard channel
15439dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1544665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1545d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1546c8e4258aSmatthias.ringwald }
15471e6aba47Smatthias.ringwald 
15488f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1549665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1550665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1551665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1552665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
15539d9bbc01Smatthias.ringwald         if ( service->psm == psm){
15549d9bbc01Smatthias.ringwald             return service;
15559d9bbc01Smatthias.ringwald         };
15569d9bbc01Smatthias.ringwald     }
15579d9bbc01Smatthias.ringwald     return NULL;
15589d9bbc01Smatthias.ringwald }
15599d9bbc01Smatthias.ringwald 
15607192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
15617192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
15627192e786SMatthias Ringwald }
15637192e786SMatthias Ringwald 
1564e0abb8e7S[email protected] 
1565be2053a6SMatthias 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){
1566be2053a6SMatthias Ringwald 
1567be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1568e0abb8e7S[email protected] 
15694bb582b6Smatthias.ringwald     // check for alread registered psm
15709d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1571277abc2cSmatthias.ringwald     if (service) {
1572be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1573be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1574277abc2cSmatthias.ringwald     }
15759d9bbc01Smatthias.ringwald 
15764bb582b6Smatthias.ringwald     // alloc structure
1577bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1578277abc2cSmatthias.ringwald     if (!service) {
1579be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1580be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1581277abc2cSmatthias.ringwald     }
15829d9bbc01Smatthias.ringwald 
15839d9bbc01Smatthias.ringwald     // fill in
15849d9bbc01Smatthias.ringwald     service->psm = psm;
15859d9bbc01Smatthias.ringwald     service->mtu = mtu;
158605ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1587df3354fcS[email protected]     service->required_security_level = security_level;
15889d9bbc01Smatthias.ringwald 
15899d9bbc01Smatthias.ringwald     // add to services list
1590665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1591c0e866bfSmatthias.ringwald 
1592c0e866bfSmatthias.ringwald     // enable page scan
159315a95bd5SMatthias Ringwald     gap_connectable_control(1);
15945842b6d9Smatthias.ringwald 
1595be2053a6SMatthias Ringwald     return 0;
15969d9bbc01Smatthias.ringwald }
15979d9bbc01Smatthias.ringwald 
15987e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
1599e0abb8e7S[email protected] 
1600e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1601e0abb8e7S[email protected] 
16029d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
16037e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1604665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1605d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1606c0e866bfSmatthias.ringwald 
1607c0e866bfSmatthias.ringwald     // disable page scan when no services registered
16087e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
160915a95bd5SMatthias Ringwald         gap_connectable_control(0);
16109d9bbc01Smatthias.ringwald     }
16117e8856ebSMatthias Ringwald     return 0;
16127e8856ebSMatthias Ringwald }
16139d9bbc01Smatthias.ringwald 
16145652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
161505ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
16165628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
16175628cf69SMatthias Ringwald     if (index < 0) return;
16185628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
16195652b5ffS[email protected] }
16205652b5ffS[email protected] 
1621a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
16227192e786SMatthias Ringwald 
16237192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
16247192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
16257192e786SMatthias Ringwald }
1626efedfb4cSMatthias Ringwald 
1627da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
16287192e786SMatthias Ringwald 
1629da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
16307192e786SMatthias Ringwald 
16317192e786SMatthias Ringwald     // check for alread registered psm
16327192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
16337192e786SMatthias Ringwald     if (service) {
1634da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
16357192e786SMatthias Ringwald     }
16367192e786SMatthias Ringwald 
16377192e786SMatthias Ringwald     // alloc structure
16387192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
16397192e786SMatthias Ringwald     if (!service) {
16407192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
1641da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
16427192e786SMatthias Ringwald     }
16437192e786SMatthias Ringwald 
16447192e786SMatthias Ringwald     // fill in
16457192e786SMatthias Ringwald     service->psm = psm;
1646da144af5SMatthias Ringwald     service->mtu = 0;
16477192e786SMatthias Ringwald     service->packet_handler = packet_handler;
16487192e786SMatthias Ringwald     service->required_security_level = security_level;
16497192e786SMatthias Ringwald 
16507192e786SMatthias Ringwald     // add to services list
1651665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
16527192e786SMatthias Ringwald 
16537192e786SMatthias Ringwald     // done
1654da144af5SMatthias Ringwald     return 0;
16557192e786SMatthias Ringwald }
16567192e786SMatthias Ringwald 
1657da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
16587192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
16597192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
1660*9367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1661da144af5SMatthias Ringwald 
1662665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
16637192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
1664da144af5SMatthias Ringwald     return 0;
16657192e786SMatthias Ringwald }
1666da144af5SMatthias Ringwald 
1667da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
1668efedfb4cSMatthias Ringwald     // get connection
1669efedfb4cSMatthias Ringwald     // bail if missing
1670efedfb4cSMatthias Ringwald     // set mtu and receive buffer
1671efedfb4cSMatthias Ringwald     // check state
1672efedfb4cSMatthias Ringwald     // set state accept connection
1673efedfb4cSMatthias Ringwald     // l2cap_le_run()
1674da144af5SMatthias Ringwald     return 0;
1675da144af5SMatthias Ringwald }
1676da144af5SMatthias Ringwald 
1677da144af5SMatthias Ringwald /**
1678da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
1679da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1680da144af5SMatthias Ringwald  */
1681da144af5SMatthias Ringwald 
1682da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
1683efedfb4cSMatthias Ringwald     // get connection
1684efedfb4cSMatthias Ringwald     // bail if missing
1685efedfb4cSMatthias Ringwald     // check state
1686efedfb4cSMatthias Ringwald     // set state decline connection
1687efedfb4cSMatthias Ringwald     // l2cap_le_run()
1688da144af5SMatthias Ringwald     return 0;
1689da144af5SMatthias Ringwald }
1690da144af5SMatthias Ringwald 
1691da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
1692da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
1693efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
1694efedfb4cSMatthias Ringwald 
1695da144af5SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
1696da144af5SMatthias Ringwald 
1697da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, mtu, LEVEL_0);
1698da144af5SMatthias Ringwald     if (!channel) {
1699da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1700da144af5SMatthias Ringwald     }
1701da144af5SMatthias Ringwald 
1702da144af5SMatthias Ringwald     // store local_cid
1703da144af5SMatthias Ringwald     if (out_local_cid){
1704da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
1705da144af5SMatthias Ringwald     }
1706da144af5SMatthias Ringwald 
1707efedfb4cSMatthias Ringwald     // add to connections list
1708efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1709efedfb4cSMatthias Ringwald 
1710da144af5SMatthias Ringwald     // check if hci connection is already usable
1711da144af5SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type);
1712efedfb4cSMatthias Ringwald 
1713efedfb4cSMatthias Ringwald     channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
1714efedfb4cSMatthias Ringwald 
1715da144af5SMatthias Ringwald     if (conn){
1716da144af5SMatthias Ringwald         log_info("l2cap_le_create_channel, hci connection already exists");
1717efedfb4cSMatthias Ringwald         l2cap_handle_le_connection_complete(conn->con_handle, channel);
1718efedfb4cSMatthias Ringwald     } else {
1719efedfb4cSMatthias Ringwald 
1720efedfb4cSMatthias Ringwald         //
1721efedfb4cSMatthias Ringwald         // TODO: start timer
1722efedfb4cSMatthias Ringwald         // ..
1723efedfb4cSMatthias Ringwald 
1724efedfb4cSMatthias Ringwald         // start connection
1725efedfb4cSMatthias Ringwald         uint8_t res = gap_auto_connection_start(address_type, address);
1726efedfb4cSMatthias Ringwald         if (!res){
1727efedfb4cSMatthias Ringwald             // discard channel object
1728efedfb4cSMatthias Ringwald             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1729efedfb4cSMatthias Ringwald             btstack_memory_l2cap_channel_free(channel);
1730efedfb4cSMatthias Ringwald             return res;
1731da144af5SMatthias Ringwald         }
1732efedfb4cSMatthias Ringwald     }
1733da144af5SMatthias Ringwald     return 0;
1734da144af5SMatthias Ringwald }
1735da144af5SMatthias Ringwald 
1736da144af5SMatthias Ringwald /**
1737da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
1738da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1739da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
1740da144af5SMatthias Ringwald  */
1741da144af5SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t cid, uint16_t credits){
1742efedfb4cSMatthias Ringwald     // get connection
1743efedfb4cSMatthias Ringwald     // bail if missing
1744efedfb4cSMatthias Ringwald     // check state
1745efedfb4cSMatthias Ringwald     // check incoming credits + credits <= 0xffff
1746efedfb4cSMatthias Ringwald     // set credits_granted
1747efedfb4cSMatthias Ringwald     // l2cap_le_run()
1748da144af5SMatthias Ringwald     return 0;
1749da144af5SMatthias Ringwald }
1750da144af5SMatthias Ringwald 
1751da144af5SMatthias Ringwald /**
1752da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
1753da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1754da144af5SMatthias Ringwald  */
1755da144af5SMatthias Ringwald int l2cap_le_can_send_now(uint16_t cid){
1756efedfb4cSMatthias Ringwald     // check if data can be sent via le at all
1757da144af5SMatthias Ringwald     return 0;
1758da144af5SMatthias Ringwald }
1759da144af5SMatthias Ringwald 
1760da144af5SMatthias Ringwald /**
1761da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
1762da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
1763da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
1764da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1765da144af5SMatthias Ringwald  */
1766da144af5SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t cid){
1767efedfb4cSMatthias Ringwald     // same as for non-le
1768da144af5SMatthias Ringwald     return 0;
1769da144af5SMatthias Ringwald }
1770da144af5SMatthias Ringwald 
1771da144af5SMatthias Ringwald /**
1772da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
1773da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
1774da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1775da144af5SMatthias Ringwald  * @param data                  data to send
1776da144af5SMatthias Ringwald  * @param size                  data size
1777da144af5SMatthias Ringwald  */
1778da144af5SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t cid, uint8_t * data, uint16_t size){
1779efedfb4cSMatthias Ringwald     // check if no outgoing data is pending
1780efedfb4cSMatthias Ringwald     // store info
1781efedfb4cSMatthias Ringwald     // l2cap_le_run()
1782da144af5SMatthias Ringwald     return 0;
1783da144af5SMatthias Ringwald }
1784da144af5SMatthias Ringwald 
1785da144af5SMatthias Ringwald /**
1786da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
1787da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1788da144af5SMatthias Ringwald  */
1789da144af5SMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t cid)
1790da144af5SMatthias Ringwald {
1791efedfb4cSMatthias Ringwald     // find channel
1792efedfb4cSMatthias Ringwald     // check state
1793efedfb4cSMatthias Ringwald     // set state SEND_DISCONNECT
1794da144af5SMatthias Ringwald     return 0;
1795da144af5SMatthias Ringwald }
1796da144af5SMatthias Ringwald 
17977f02f414SMatthias Ringwald #endif
1798