xref: /btstack/src/l2cap.c (revision 828a7f7ad0f6db2a8fd031f4a83261ab3d037a75)
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);
87e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
88*828a7f7aSMatthias Ringwald static void l2cap_le_finialize_channel_close(l2cap_channel_t *channel);
89e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm);
90e7d0c9aaSMatthias Ringwald #endif
9133c40538SMatthias Ringwald 
925628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
935628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
942125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
955628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
965628cf69SMatthias Ringwald 
975628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
985628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
995628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
1005628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
10139bda6d5Smatthias.ringwald 
10239bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
1032b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1042b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1052b83fb7dSmatthias.ringwald 
10633c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
10733c40538SMatthias Ringwald 
108fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
109fb37a842SMatthias Ringwald 
11033c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1115628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
11239bda6d5Smatthias.ringwald 
11334e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
11434e7e577SMatthias Ringwald     switch (index){
11534e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
11634e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
11734e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
11834e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
11934e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
12034e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
12134e7e577SMatthias Ringwald         default:
12234e7e577SMatthias Ringwald             return 0;
12334e7e577SMatthias Ringwald     }
12434e7e577SMatthias Ringwald }
1255628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1265628cf69SMatthias Ringwald     switch (channel_id){
1275628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1285628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1295628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1305628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1315628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1325628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1335628cf69SMatthias Ringwald         default:
1345628cf69SMatthias Ringwald             return -1;
1355628cf69SMatthias Ringwald         }
1365628cf69SMatthias Ringwald }
13739bda6d5Smatthias.ringwald 
13834e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
13934e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
14034e7e577SMatthias Ringwald     return 1;
14134e7e577SMatthias Ringwald }
14234e7e577SMatthias Ringwald 
14371de195eSMatthias Ringwald void l2cap_init(void){
1442b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
145808a48abSmatthias.ringwald 
146f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
147f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1487192e786SMatthias Ringwald     l2cap_le_services = NULL;
1497192e786SMatthias Ringwald     l2cap_le_channels = NULL;
150f5454fc6Smatthias.ringwald 
15133c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1525628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
153f5454fc6Smatthias.ringwald 
154ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
155ac301f95S[email protected] 
156fcadd0caSmatthias.ringwald     //
1572718e2e7Smatthias.ringwald     // register callback with HCI
158fcadd0caSmatthias.ringwald     //
159d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
160fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
161fb37a842SMatthias Ringwald 
162d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
163fb37a842SMatthias Ringwald 
16415a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
165fcadd0caSmatthias.ringwald }
166fcadd0caSmatthias.ringwald 
167ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
16833c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1691e6aba47Smatthias.ringwald }
1701e6aba47Smatthias.ringwald 
17117a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
17258de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
17358de5610Smatthias.ringwald }
17458de5610Smatthias.ringwald 
17558de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
176c9dc710bS[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",
177fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
178c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
179c9dc710bS[email protected]     uint8_t event[23];
18058de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
18158de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
18258de5610Smatthias.ringwald     event[2] = status;
183724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
184fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
185f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
186f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
187f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
188f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
189f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
190f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
19158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19217a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
19358de5610Smatthias.ringwald }
19458de5610Smatthias.ringwald 
19558de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
196e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
19758de5610Smatthias.ringwald     uint8_t event[4];
19858de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
19958de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
200f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
20158de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
20217a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
20358de5610Smatthias.ringwald }
20458de5610Smatthias.ringwald 
20558de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
206e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
207fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
20858de5610Smatthias.ringwald     uint8_t event[16];
20958de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
21058de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
211724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
212fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
213f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
214f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
215f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
21658de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21717a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2180af41d30Smatthias.ringwald }
219808a48abSmatthias.ringwald 
22034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
22134e7e577SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
22233c40538SMatthias Ringwald     uint8_t event[4];
22333c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
22433c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
22534e7e577SMatthias Ringwald     little_endian_store_16(event, 2, channel);
22633c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22734e7e577SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
22833c40538SMatthias Ringwald }
22933c40538SMatthias Ringwald 
230fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
231ccf076adS[email protected]     uint8_t event[6];
232ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
233ccf076adS[email protected]     event[1] = 4;
234fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
235f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
236ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23733c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
23833c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
239ccf076adS[email protected] }
240ccf076adS[email protected] 
2417f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
242665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
243665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
244665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
245665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
246b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
247f62db1e3Smatthias.ringwald             return channel;
248f62db1e3Smatthias.ringwald         }
249f62db1e3Smatthias.ringwald     }
250f62db1e3Smatthias.ringwald     return NULL;
251f62db1e3Smatthias.ringwald }
252f62db1e3Smatthias.ringwald 
253e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
254e7d0c9aaSMatthias Ringwald static l2cap_channel_t * l2cap_le_get_channel_for_local_cid(uint16_t local_cid){
255e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_t it;
256e7d0c9aaSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
257e7d0c9aaSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
258e7d0c9aaSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
259e7d0c9aaSMatthias Ringwald         if ( channel->local_cid == local_cid) {
260e7d0c9aaSMatthias Ringwald             return channel;
261e7d0c9aaSMatthias Ringwald         }
262e7d0c9aaSMatthias Ringwald     }
263e7d0c9aaSMatthias Ringwald     return NULL;
264e7d0c9aaSMatthias Ringwald }
265e7d0c9aaSMatthias Ringwald #endif
266e7d0c9aaSMatthias Ringwald 
2675cb87675SMatthias Ringwald static l2cap_channel_t * l2cap_get_le_channel_for_address_and_addr_type(bd_addr_t address, bd_addr_type_t address_type){
2685cb87675SMatthias Ringwald     btstack_linked_list_iterator_t it;
2695cb87675SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
2705cb87675SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2715cb87675SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2725cb87675SMatthias Ringwald         if (channel->address_type != address_type)  continue;
2735cb87675SMatthias Ringwald         if (bd_addr_cmp(channel->address, address)) continue;
2745cb87675SMatthias Ringwald         return channel;
2755cb87675SMatthias Ringwald     }
2765cb87675SMatthias Ringwald     return NULL;
2775cb87675SMatthias Ringwald }
2785cb87675SMatthias Ringwald 
2790b9d7e78SMatthias Ringwald ///
2800b9d7e78SMatthias Ringwald 
28130725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
28230725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
28330725612SMatthias Ringwald     if (!channel) return;
28430725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
28530725612SMatthias Ringwald     l2cap_notify_channel_can_send();
28630725612SMatthias Ringwald }
28730725612SMatthias Ringwald 
2880b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
2890b9d7e78SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
2900b9d7e78SMatthias Ringwald     if (index < 0) return;
2910b9d7e78SMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
2920b9d7e78SMatthias Ringwald     l2cap_notify_channel_can_send();
2930b9d7e78SMatthias Ringwald }
2940b9d7e78SMatthias Ringwald 
2950b9d7e78SMatthias Ringwald ///
2960b9d7e78SMatthias Ringwald 
2976b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2986b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2996b1fde37Smatthias.ringwald     if (!channel) return 0;
3000b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
3017856fb31S[email protected] }
3027856fb31S[email protected] 
30383e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
30483e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
30583e7cdd9SMatthias Ringwald     if (!channel) return 0;
3060b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
30783e7cdd9SMatthias Ringwald }
30883e7cdd9SMatthias Ringwald 
309fc64f94aSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
3100b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
3112125de09SMatthias Ringwald }
3120b9d7e78SMatthias Ringwald 
3130b9d7e78SMatthias Ringwald ///
3143cab4fcaS[email protected] 
31596cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
31696cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
31796cbd662Smatthias.ringwald     if (channel) {
31896cbd662Smatthias.ringwald         return channel->remote_mtu;
31996cbd662Smatthias.ringwald     }
32096cbd662Smatthias.ringwald     return 0;
32196cbd662Smatthias.ringwald }
32296cbd662Smatthias.ringwald 
323ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
324665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
325665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
326665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
327665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3285932bd7cS[email protected]         if ( &channel->rtx == ts) {
3295932bd7cS[email protected]             return channel;
3305932bd7cS[email protected]         }
3315932bd7cS[email protected]     }
3325932bd7cS[email protected]     return NULL;
3335932bd7cS[email protected] }
3345932bd7cS[email protected] 
335ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3365932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
33795d2c8f4SMatthias Ringwald     if (!channel) return;
3385932bd7cS[email protected] 
3395932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3405932bd7cS[email protected] 
3415932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3425932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3435932bd7cS[email protected]     // notify client
3445932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3455932bd7cS[email protected] 
3465932bd7cS[email protected]     // discard channel
3479dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
348665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3495932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3505932bd7cS[email protected] }
3515932bd7cS[email protected] 
3525932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3535932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
354528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3555932bd7cS[email protected] }
3565932bd7cS[email protected] 
3575932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3585932bd7cS[email protected]     l2cap_stop_rtx(channel);
359cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
360528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
361528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
362528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3635932bd7cS[email protected] }
3645932bd7cS[email protected] 
3655932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3665932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3675932bd7cS[email protected]     l2cap_stop_rtx(channel);
368528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
369528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
370528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3715932bd7cS[email protected] }
3725932bd7cS[email protected] 
37371de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
374ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
375ac301f95S[email protected] }
376ac301f95S[email protected] 
377df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
378ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
379df3354fcS[email protected] }
3805932bd7cS[email protected] 
3817f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
382b1d43497Smatthias.ringwald 
383a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3849da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
385b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
386b1d43497Smatthias.ringwald     }
387b1d43497Smatthias.ringwald 
3889da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3892a373862S[email protected]     hci_reserve_packet_buffer();
390facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
39158de5610Smatthias.ringwald     va_list argptr;
39258de5610Smatthias.ringwald     va_start(argptr, identifier);
39370efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
39458de5610Smatthias.ringwald     va_end(argptr);
3959da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
396826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
39758de5610Smatthias.ringwald }
39858de5610Smatthias.ringwald 
399a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
4007f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
40170efece1S[email protected] 
402a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
4039da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
40470efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
40570efece1S[email protected]     }
40670efece1S[email protected] 
4079da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
4082a373862S[email protected]     hci_reserve_packet_buffer();
409facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
41070efece1S[email protected]     va_list argptr;
41170efece1S[email protected]     va_start(argptr, identifier);
41270efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
41370efece1S[email protected]     va_end(argptr);
4149da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
415826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
41670efece1S[email protected] }
41770efece1S[email protected] #endif
41870efece1S[email protected] 
419b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
420facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
421b1d43497Smatthias.ringwald }
4226218e6f1Smatthias.ringwald 
4236b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
4246b4af23dS[email protected]     return hci_reserve_packet_buffer();
4256b4af23dS[email protected] }
4266b4af23dS[email protected] 
42768a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
42868a0fcf7S[email protected]     hci_release_packet_buffer();
42968a0fcf7S[email protected] }
43068a0fcf7S[email protected] 
43168a0fcf7S[email protected] 
432b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
433b1d43497Smatthias.ringwald 
434c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
435c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
436c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
437c8b9416aS[email protected]     }
438c8b9416aS[email protected] 
43958de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
440b1d43497Smatthias.ringwald     if (!channel) {
4419da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
442b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4436218e6f1Smatthias.ringwald     }
4446218e6f1Smatthias.ringwald 
445fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4469da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
447a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
448a35252c8S[email protected]     }
449a35252c8S[email protected] 
450fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
451b1d43497Smatthias.ringwald 
452facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
453b1d43497Smatthias.ringwald 
454e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
455e9772277S[email protected] 
456e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
457fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14));
45858de5610Smatthias.ringwald     // 2 - ACL length
459f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
46058de5610Smatthias.ringwald     // 4 - L2CAP packet length
461f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
46258de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
463f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
46458de5610Smatthias.ringwald     // send
465826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
46691b99603Smatthias.ringwald 
4676218e6f1Smatthias.ringwald     return err;
46858de5610Smatthias.ringwald }
46958de5610Smatthias.ringwald 
470fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
4712149f12eSmatthias.ringwald 
472c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
473c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4742149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4752149f12eSmatthias.ringwald     }
4762149f12eSmatthias.ringwald 
477fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
478fc64f94aSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
479c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
480c8b9416aS[email protected]     }
481c8b9416aS[email protected] 
482fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
4832149f12eSmatthias.ringwald 
484facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4852149f12eSmatthias.ringwald 
486e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
487e9772277S[email protected] 
488e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
489fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
4902149f12eSmatthias.ringwald     // 2 - ACL length
491f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4922149f12eSmatthias.ringwald     // 4 - L2CAP packet length
493f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4942149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
495f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4962149f12eSmatthias.ringwald     // send
497826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4982149f12eSmatthias.ringwald 
4992149f12eSmatthias.ringwald     return err;
5002149f12eSmatthias.ringwald }
5012149f12eSmatthias.ringwald 
502ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
503b1d43497Smatthias.ringwald 
504a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
505a35252c8S[email protected]     if (!channel) {
506ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
507a35252c8S[email protected]         return -1;   // TODO: define error
508a35252c8S[email protected]     }
509a35252c8S[email protected] 
510f0efaa57S[email protected]     if (len > channel->remote_mtu){
511ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
512f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
513f0efaa57S[email protected]     }
514f0efaa57S[email protected] 
515fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
516ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
517b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
518b1d43497Smatthias.ringwald     }
519b1d43497Smatthias.ringwald 
5202a373862S[email protected]     hci_reserve_packet_buffer();
521facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
522b1d43497Smatthias.ringwald 
523b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
524b1d43497Smatthias.ringwald 
525b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
526b1d43497Smatthias.ringwald }
527b1d43497Smatthias.ringwald 
528fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
5292149f12eSmatthias.ringwald 
530fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
531ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
5322149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
5332149f12eSmatthias.ringwald     }
5342149f12eSmatthias.ringwald 
5352a373862S[email protected]     hci_reserve_packet_buffer();
536facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
5372149f12eSmatthias.ringwald 
5382149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
5392149f12eSmatthias.ringwald 
540fc64f94aSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
5412149f12eSmatthias.ringwald }
5422149f12eSmatthias.ringwald 
543fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
544fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5450e37e417S[email protected] }
5460e37e417S[email protected] 
54728ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
54828ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
54928ca2b46S[email protected] }
55028ca2b46S[email protected] 
55128ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
55228ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
55328ca2b46S[email protected] }
55428ca2b46S[email protected] 
55528ca2b46S[email protected] 
556b1d43497Smatthias.ringwald 
5578158c421Smatthias.ringwald // MARK: L2CAP_RUN
5582cd0be45Smatthias.ringwald // process outstanding signaling tasks
5597f02f414SMatthias Ringwald static void l2cap_run(void){
5602b83fb7dSmatthias.ringwald 
56122c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
56222c29ab4SMatthias Ringwald 
5632b83fb7dSmatthias.ringwald     // check pending signaling responses
5642b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5652b83fb7dSmatthias.ringwald 
5662b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
567a35252c8S[email protected] 
568a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
569a35252c8S[email protected] 
5702b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5712b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
57263a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
573f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5742b83fb7dSmatthias.ringwald 
575f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
576f53da564S[email protected]         signaling_responses_pending--;
577f53da564S[email protected]         int i;
578f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
579f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
580f53da564S[email protected]         }
581f53da564S[email protected] 
582f53da564S[email protected]         switch (response_code){
5832b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5842b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5852bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5864d816277S[email protected]                 if (result == 0x0003){
5872bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5884d816277S[email protected]                 }
5892b360848Smatthias.ringwald                 break;
5902b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5912b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5922b83fb7dSmatthias.ringwald                 break;
5932b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5943b0484b3S[email protected]                 switch (infoType){
5953b0484b3S[email protected]                     case 1: { // Connectionless MTU
5963b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5973b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5983b0484b3S[email protected]                         break;
5993b0484b3S[email protected]                     }
6003b0484b3S[email protected]                     case 2: { // Extended Features Supported
601462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
602462e630dS[email protected]                         uint32_t features = 0x280;
6033b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
6043b0484b3S[email protected]                         break;
6053b0484b3S[email protected]                     }
6063b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
6073b0484b3S[email protected]                         uint8_t map[8];
6083b0484b3S[email protected]                         memset(map, 0, 8);
609288636a2SMatthias Ringwald                         map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
6103b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
6113b0484b3S[email protected]                         break;
6123b0484b3S[email protected]                     }
6133b0484b3S[email protected]                     default:
6142b83fb7dSmatthias.ringwald                         // all other types are not supported
6152b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
6163b0484b3S[email protected]                         break;
6172b83fb7dSmatthias.ringwald                 }
6182b83fb7dSmatthias.ringwald                 break;
61963a7246aSmatthias.ringwald             case COMMAND_REJECT:
6205ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
621a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
62270efece1S[email protected]             case COMMAND_REJECT_LE:
62370efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
62463a7246aSmatthias.ringwald                 break;
62570efece1S[email protected] #endif
6262b83fb7dSmatthias.ringwald             default:
6272b83fb7dSmatthias.ringwald                 // should not happen
6282b83fb7dSmatthias.ringwald                 break;
6292b83fb7dSmatthias.ringwald         }
6302b83fb7dSmatthias.ringwald     }
6312b83fb7dSmatthias.ringwald 
632ae280e73Smatthias.ringwald     uint8_t  config_options[4];
633665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
634665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
635665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
636baf94f06S[email protected] 
637665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
63822c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6392cd0be45Smatthias.ringwald         switch (channel->state){
6402cd0be45Smatthias.ringwald 
641df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
642ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
643fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
644a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
645ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
646fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
647ad671560S[email protected]                 }
648ad671560S[email protected]                 break;
649ad671560S[email protected] 
65002b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
651baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
65264472d52Smatthias.ringwald                 // send connection request - set state first
65364472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
65402b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6558f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
65602b22dc4Smatthias.ringwald                 break;
65702b22dc4Smatthias.ringwald 
658e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
659fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
66022c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
661fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
662e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6639dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
664665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
665d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
666e7ff783cSmatthias.ringwald                 break;
667e7ff783cSmatthias.ringwald 
668552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
669fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
670fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
67128ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
672fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
673552d92a1Smatthias.ringwald                 break;
674552d92a1Smatthias.ringwald 
6756fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
676fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6776fdcc387Smatthias.ringwald                 // success, start l2cap handshake
678b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6796fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
680fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6815932bd7cS[email protected]                 l2cap_start_rtx(channel);
6826fdcc387Smatthias.ringwald                 break;
6836fdcc387Smatthias.ringwald 
684fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
685fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
68673cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
68763a7246aSmatthias.ringwald                     uint16_t flags = 0;
68828ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
68963a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
69063a7246aSmatthias.ringwald                         flags = 1;
69163a7246aSmatthias.ringwald                     } else {
69228ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
69363a7246aSmatthias.ringwald                     }
69463a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
695fc64f94aSMatthias 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);
69663a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
69763a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
69863a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
699f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
700fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
70163a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
70263a7246aSmatthias.ringwald                     } else {
703fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
70463a7246aSmatthias.ringwald                     }
70563a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
706fa8473a4Smatthias.ringwald                 }
70773cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
70828ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
70928ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
710b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
711ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
712ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
713f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
714fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
7155932bd7cS[email protected]                     l2cap_start_rtx(channel);
716fa8473a4Smatthias.ringwald                 }
717fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
718552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
719552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
720fa8473a4Smatthias.ringwald                 }
721552d92a1Smatthias.ringwald                 break;
722552d92a1Smatthias.ringwald 
723e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
724fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
72522c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
726fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7275932bd7cS[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 :)
728756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
729e7ff783cSmatthias.ringwald                 break;
730e7ff783cSmatthias.ringwald 
731e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
732fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
733b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7342cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
735fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7362cd0be45Smatthias.ringwald                 break;
7372cd0be45Smatthias.ringwald             default:
7382cd0be45Smatthias.ringwald                 break;
7392cd0be45Smatthias.ringwald         }
7402cd0be45Smatthias.ringwald     }
741da886c03S[email protected] 
742a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
743efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
744efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
745efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
746efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
747efedfb4cSMatthias Ringwald         switch (channel->state){
7485cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7495cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7505cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
7515cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
7521b8b8d05SMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7535cb87675SMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, LE_CREDIT_BASED_CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid, 23, 23, 1);
7545cb87675SMatthias Ringwald                 break;
75523017473SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
75623017473SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
75723017473SMatthias Ringwald                 // TODO: support larger MPS
75823017473SMatthias Ringwald                 channel->state = L2CAP_STATE_OPEN;
75923017473SMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, channel->local_mtu, 23, channel->credits_incoming, 0);
76064e11ca9SMatthias Ringwald                 // notify client
76164e11ca9SMatthias Ringwald                 l2cap_emit_channel_opened(channel, 0);
76223017473SMatthias Ringwald                 break;
763e7d0c9aaSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
764e7d0c9aaSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
765e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
766e7d0c9aaSMatthias Ringwald                 l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->remote_cid, 0, 0, 0, channel->reason);
767e7d0c9aaSMatthias Ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
768e7d0c9aaSMatthias Ringwald                 l2cap_stop_rtx(channel);
769e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
770e7d0c9aaSMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
771e7d0c9aaSMatthias Ringwald                 break;
772*828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
773*828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
774*828a7f7aSMatthias Ringwald                 channel->local_sig_id = l2cap_next_sig_id();
775*828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
776*828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
777*828a7f7aSMatthias Ringwald                 break;
778*828a7f7aSMatthias Ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
779*828a7f7aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
780*828a7f7aSMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
781*828a7f7aSMatthias Ringwald                 l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
782*828a7f7aSMatthias Ringwald                 l2cap_le_finialize_channel_close(channel);  // -- remove from list
783*828a7f7aSMatthias Ringwald                 break;
784efedfb4cSMatthias Ringwald             default:
785efedfb4cSMatthias Ringwald                 break;
786efedfb4cSMatthias Ringwald         }
787efedfb4cSMatthias Ringwald     }
788efedfb4cSMatthias Ringwald 
789da886c03S[email protected]     // send l2cap con paramter update if necessary
790da886c03S[email protected]     hci_connections_get_iterator(&it);
791665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
792665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7935d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
794b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
795da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
796b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
797b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
798b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
799b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
800b68d7bc3SMatthias Ringwald                 break;
801da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
802b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
803b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
804da886c03S[email protected]                 break;
805da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
806b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
807b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
808da886c03S[email protected]                 break;
809da886c03S[email protected]             default:
810da886c03S[email protected]                 break;
811da886c03S[email protected]         }
812efedfb4cSMatthias Ringwald #if 0
813efedfb4cSMatthias Ringwald void l2cap_le_run(void){
814efedfb4cSMatthias Ringwald     for all channels
815efedfb4cSMatthias Ringwald         switch(state):
816efedfb4cSMatthias Ringwald 
817efedfb4cSMatthias Ringwald     if outgoing transfer active
818efedfb4cSMatthias Ringwald         send next chunk
819efedfb4cSMatthias Ringwald         if done, notify app
820efedfb4cSMatthias Ringwald }
821efedfb4cSMatthias Ringwald #endif
822da886c03S[email protected]     }
8234d7157c3S[email protected] #endif
824da886c03S[email protected] 
82522c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
8262cd0be45Smatthias.ringwald }
8272cd0be45Smatthias.ringwald 
8284aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
8294ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
830fa8c92f6Smatthias.ringwald }
831fa8c92f6Smatthias.ringwald 
83271de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
8334ff786cfS[email protected]     return l2cap_max_mtu();
834e5e1518dS[email protected] }
835e5e1518dS[email protected] 
836fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
8372df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
8385533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
8392df5dadcS[email protected]         // success, start l2cap handshake
840fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
8412df5dadcS[email protected]         // check remote SSP feature first
8422df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
8432df5dadcS[email protected]     }
8442df5dadcS[email protected] }
8452df5dadcS[email protected] 
846efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE
8475cb87675SMatthias Ringwald static void l2cap_handle_le_connection_complete(l2cap_channel_t * channel, uint8_t status, hci_con_handle_t con_handle){
848efedfb4cSMatthias Ringwald     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
849efedfb4cSMatthias Ringwald         log_info("l2cap_le_handle_connection_complete expected state");
8505cb87675SMatthias Ringwald         // TODO: bail if status != 0
8515cb87675SMatthias Ringwald 
852efedfb4cSMatthias Ringwald         // success, start l2cap handshake
853efedfb4cSMatthias Ringwald         channel->con_handle = con_handle;
8545cb87675SMatthias Ringwald         channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
855efedfb4cSMatthias Ringwald     }
856efedfb4cSMatthias Ringwald }
857efedfb4cSMatthias Ringwald #endif
858efedfb4cSMatthias Ringwald 
859efedfb4cSMatthias Ringwald 
8602df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
8612df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
8622df5dadcS[email protected] 
8632df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
864ac301f95S[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));
865fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
8662df5dadcS[email protected]         // request security level 2
8672df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
868fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
8692df5dadcS[email protected]         return;
8702df5dadcS[email protected]     }
8712df5dadcS[email protected]     // fine, go ahead
8722df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
8732df5dadcS[email protected] }
8742df5dadcS[email protected] 
875da144af5SMatthias 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,
876da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
877da144af5SMatthias Ringwald 
878da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
879da144af5SMatthias Ringwald     if (!channel) {
880da144af5SMatthias Ringwald         return NULL;
881da144af5SMatthias Ringwald     }
882da144af5SMatthias Ringwald 
883da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
884da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
885da144af5SMatthias Ringwald 
886da144af5SMatthias Ringwald     // fill in
887da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
888da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
889da144af5SMatthias Ringwald     channel->address_type = address_type;
890da144af5SMatthias Ringwald     channel->psm = psm;
891da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
892da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
893da144af5SMatthias Ringwald     channel->required_security_level = security_level;
894da144af5SMatthias Ringwald 
895da144af5SMatthias Ringwald     //
896da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
897da144af5SMatthias Ringwald     channel->con_handle = 0;
898da144af5SMatthias Ringwald 
899da144af5SMatthias Ringwald     // set initial state
900da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
901da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
902da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
903da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
904da144af5SMatthias Ringwald     return channel;
905da144af5SMatthias Ringwald }
906da144af5SMatthias Ringwald 
9079077cb15SMatthias Ringwald /**
9089077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
9099077cb15SMatthias Ringwald  * @param packet_handler
9109077cb15SMatthias Ringwald  * @param address
9119077cb15SMatthias Ringwald  * @param psm
9129077cb15SMatthias Ringwald  * @param mtu
9139077cb15SMatthias Ringwald  * @param local_cid
9149077cb15SMatthias Ringwald  */
9159077cb15SMatthias Ringwald 
916da144af5SMatthias 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){
917da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
918da144af5SMatthias Ringwald 
919da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
920da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
921da144af5SMatthias Ringwald     }
922da144af5SMatthias Ringwald 
923da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
924fc64f94aSMatthias Ringwald     if (!channel) {
9259077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
9269077cb15SMatthias Ringwald     }
9279077cb15SMatthias Ringwald 
9289077cb15SMatthias Ringwald     // add to connections list
929fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
9309077cb15SMatthias Ringwald 
9319077cb15SMatthias Ringwald     // store local_cid
9329077cb15SMatthias Ringwald     if (out_local_cid){
933fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
9349077cb15SMatthias Ringwald     }
9359077cb15SMatthias Ringwald 
9369077cb15SMatthias Ringwald     // check if hci connection is already usable
9379077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
9389077cb15SMatthias Ringwald     if (conn){
939add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
940fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
9419077cb15SMatthias Ringwald         // check if remote supported fearures are already received
9429077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
943fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
9449077cb15SMatthias Ringwald         }
9459077cb15SMatthias Ringwald     }
9469077cb15SMatthias Ringwald 
9479077cb15SMatthias Ringwald     l2cap_run();
9489077cb15SMatthias Ringwald 
9499077cb15SMatthias Ringwald     return 0;
9509077cb15SMatthias Ringwald }
9519077cb15SMatthias Ringwald 
952ce8f182eSMatthias Ringwald void
953ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
954e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
955b35f641cSmatthias.ringwald     // find channel for local_cid
956b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
957f62db1e3Smatthias.ringwald     if (channel) {
958e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
959f62db1e3Smatthias.ringwald     }
9602cd0be45Smatthias.ringwald     // process
9612cd0be45Smatthias.ringwald     l2cap_run();
96243625864Smatthias.ringwald }
9631e6aba47Smatthias.ringwald 
964afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
965665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
966665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
967665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
968665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
969058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
970c22aecc9S[email protected]         // channel for this address found
971c22aecc9S[email protected]         switch (channel->state){
972c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
973c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
974afde0c52Smatthias.ringwald                 // failure, forward error code
975afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
976afde0c52Smatthias.ringwald                 // discard channel
9779dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
978665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
979d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
980c22aecc9S[email protected]                 break;
981c22aecc9S[email protected]             default:
982c22aecc9S[email protected]                 break;
983afde0c52Smatthias.ringwald         }
984afde0c52Smatthias.ringwald     }
985afde0c52Smatthias.ringwald }
986afde0c52Smatthias.ringwald 
987afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
988665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
989665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
990665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
991665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
992058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
9932df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
994afde0c52Smatthias.ringwald         }
995afde0c52Smatthias.ringwald     }
9966fdcc387Smatthias.ringwald     // process
9976fdcc387Smatthias.ringwald     l2cap_run();
998afde0c52Smatthias.ringwald }
999b448a0e7Smatthias.ringwald 
100033c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
100133c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
100233c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
100333c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
100433c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
100533c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
1006fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
100733c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
100834e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
100933c40538SMatthias Ringwald     }
10102125de09SMatthias Ringwald 
10112125de09SMatthias Ringwald     int i;
10122125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
1013773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
10142125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
101534e7e577SMatthias Ringwald         int can_send;
101634e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
101734e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
101834e7e577SMatthias Ringwald         } else {
101934e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
102034e7e577SMatthias Ringwald         }
102134e7e577SMatthias Ringwald         if (!can_send) continue;
102234e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
102334e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
10242125de09SMatthias Ringwald     }
102533c40538SMatthias Ringwald }
102633c40538SMatthias Ringwald 
1027d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
1028afde0c52Smatthias.ringwald 
1029afde0c52Smatthias.ringwald     bd_addr_t address;
1030afde0c52Smatthias.ringwald     hci_con_handle_t handle;
1031665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
10322d00edd4Smatthias.ringwald     int hci_con_used;
1033afde0c52Smatthias.ringwald 
10340e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
1035afde0c52Smatthias.ringwald 
1036afde0c52Smatthias.ringwald         // handle connection complete events
1037afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
1038724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
1039afde0c52Smatthias.ringwald             if (packet[2] == 0){
1040f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
1041afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
1042afde0c52Smatthias.ringwald             } else {
1043afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
1044afde0c52Smatthias.ringwald             }
1045afde0c52Smatthias.ringwald             break;
1046afde0c52Smatthias.ringwald 
1047afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1048afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1049073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1050afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1051724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1052afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1053afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
105403cfbabcSmatthias.ringwald                 }
10551e6aba47Smatthias.ringwald             }
105639d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
105739d59809Smatthias.ringwald             break;
105839d59809Smatthias.ringwald 
105939d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
106039d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
1061afde0c52Smatthias.ringwald             break;
106227a923d0Smatthias.ringwald 
10631e6aba47Smatthias.ringwald         // handle disconnection complete events
1064afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1065c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
1066f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1067665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1068665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1069665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1070fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
107115ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
10729dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1073665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1074d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
107527a923d0Smatthias.ringwald             }
1076991fea48SMatthias Ringwald #ifdef ENABLE_BLE
1077991fea48SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1078991fea48SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1079991fea48SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1080991fea48SMatthias Ringwald                 if (channel->con_handle != handle) continue;
1081991fea48SMatthias Ringwald                 l2cap_emit_channel_closed(channel);
1082991fea48SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1083991fea48SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
1084991fea48SMatthias Ringwald             }
1085991fea48SMatthias Ringwald #endif
1086afde0c52Smatthias.ringwald             break;
1087fcadd0caSmatthias.ringwald 
108833c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
108963fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
10906218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
109102b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
109233c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
10936218e6f1Smatthias.ringwald             break;
10946218e6f1Smatthias.ringwald 
1095ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
1096afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1097f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1098bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
109980ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
11002d00edd4Smatthias.ringwald             hci_con_used = 0;
1101665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1102665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1103665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1104fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11052d00edd4Smatthias.ringwald                 hci_con_used = 1;
1106c22aecc9S[email protected]                 break;
1107ee091cf1Smatthias.ringwald             }
11082d00edd4Smatthias.ringwald             if (hci_con_used) break;
1109d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
11109edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1111afde0c52Smatthias.ringwald             break;
1112ee091cf1Smatthias.ringwald 
1113df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1114f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1115665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1116665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1117665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1118fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11192df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1120df3354fcS[email protected]                 break;
1121df3354fcS[email protected]             }
1122c22aecc9S[email protected]             break;
1123df3354fcS[email protected] 
11245cb87675SMatthias Ringwald #ifdef ENABLE_BLE
11255cb87675SMatthias Ringwald         case HCI_EVENT_LE_META:
11265cb87675SMatthias Ringwald             switch (packet[2]){
11275cb87675SMatthias Ringwald                 int addr_type;
11285cb87675SMatthias Ringwald                 l2cap_channel_t * channel;
11295cb87675SMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
11305cb87675SMatthias Ringwald                     // Connection management
11315cb87675SMatthias Ringwald                     reverse_bd_addr(&packet[8], address);
11325cb87675SMatthias Ringwald                     addr_type = (bd_addr_type_t)packet[7];
11335cb87675SMatthias Ringwald                     log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address));
11345cb87675SMatthias Ringwald                     // get l2cap_channel
11355cb87675SMatthias Ringwald                     // also pass in status
11365cb87675SMatthias Ringwald                     channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type);
11375cb87675SMatthias Ringwald                     if (!channel) break;
11385cb87675SMatthias Ringwald                     l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4));
11395cb87675SMatthias Ringwald                     break;
11405cb87675SMatthias Ringwald                 default:
11415cb87675SMatthias Ringwald                     break;
11425cb87675SMatthias Ringwald             }
11435cb87675SMatthias Ringwald             break;
11445cb87675SMatthias Ringwald #endif
11455cb87675SMatthias Ringwald 
11465611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1147f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1148bd63148eS[email protected]             log_info("l2cap - security level update");
1149665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1150665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1151665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1152fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
11535533f01eS[email protected] 
1154bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1155bd63148eS[email protected] 
1156e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
11575533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
11585533f01eS[email protected] 
1159df3354fcS[email protected]                 switch (channel->state){
1160df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
11615533f01eS[email protected]                         if (actual_level >= required_level){
1162f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1163f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
11641eb2563eS[email protected]                         } else {
1165775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
11661eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11671eb2563eS[email protected]                         }
1168df3354fcS[email protected]                         break;
1169df3354fcS[email protected] 
1170df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
11715533f01eS[email protected]                         if (actual_level >= required_level){
1172df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1173df3354fcS[email protected]                         } else {
1174df3354fcS[email protected]                             // disconnnect, authentication not good enough
1175df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1176df3354fcS[email protected]                         }
1177df3354fcS[email protected]                         break;
1178df3354fcS[email protected] 
1179df3354fcS[email protected]                     default:
1180df3354fcS[email protected]                         break;
1181df3354fcS[email protected]                 }
1182f85a9399S[email protected]             }
1183f85a9399S[email protected]             break;
1184f85a9399S[email protected] 
1185afde0c52Smatthias.ringwald         default:
1186afde0c52Smatthias.ringwald             break;
1187afde0c52Smatthias.ringwald     }
1188afde0c52Smatthias.ringwald 
1189bd63148eS[email protected]     l2cap_run();
11901e6aba47Smatthias.ringwald }
11911e6aba47Smatthias.ringwald 
1192afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1193b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1194e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1195e7ff783cSmatthias.ringwald     l2cap_run();
119684836b65Smatthias.ringwald }
119784836b65Smatthias.ringwald 
11982b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
11994cf56b4aSmatthias.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."
12002b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
12012b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
12022b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
12032b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
12042b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
12052b360848Smatthias.ringwald         signaling_responses_pending++;
12062b360848Smatthias.ringwald         l2cap_run();
12072b360848Smatthias.ringwald     }
12082b360848Smatthias.ringwald }
12092b360848Smatthias.ringwald 
1210b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1211645658c9Smatthias.ringwald 
12129da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1213645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1214645658c9Smatthias.ringwald     if (!service) {
1215645658c9Smatthias.ringwald         // 0x0002 PSM not supported
12162b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1217645658c9Smatthias.ringwald         return;
1218645658c9Smatthias.ringwald     }
1219645658c9Smatthias.ringwald 
12205061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1221645658c9Smatthias.ringwald     if (!hci_connection) {
12222b360848Smatthias.ringwald         //
12239da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1224645658c9Smatthias.ringwald         return;
1225645658c9Smatthias.ringwald     }
12262bd8b7e7S[email protected] 
1227645658c9Smatthias.ringwald     // alloc structure
12289da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1229da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1230da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
12312b360848Smatthias.ringwald     if (!channel){
12322b360848Smatthias.ringwald         // 0x0004 No resources available
12332b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
12342b360848Smatthias.ringwald         return;
12352b360848Smatthias.ringwald     }
1236da144af5SMatthias Ringwald 
1237fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1238b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1239b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1240645658c9Smatthias.ringwald 
1241f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
12422985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
12432985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
12449775e25bSmatthias.ringwald     }
12459775e25bSmatthias.ringwald 
1246645658c9Smatthias.ringwald     // set initial state
1247df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1248ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1249e405ae81Smatthias.ringwald 
1250645658c9Smatthias.ringwald     // add to connections list
1251665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1252645658c9Smatthias.ringwald 
1253f85a9399S[email protected]     // assert security requirements
12541eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1255e405ae81Smatthias.ringwald }
1256645658c9Smatthias.ringwald 
1257ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1258e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1259b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1260e405ae81Smatthias.ringwald     if (!channel) {
1261ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1262e405ae81Smatthias.ringwald         return;
1263e405ae81Smatthias.ringwald     }
1264e405ae81Smatthias.ringwald 
1265552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1266e405ae81Smatthias.ringwald 
1267552d92a1Smatthias.ringwald     // process
1268552d92a1Smatthias.ringwald     l2cap_run();
1269e405ae81Smatthias.ringwald }
1270645658c9Smatthias.ringwald 
12717ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
12727ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1273b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1274e405ae81Smatthias.ringwald     if (!channel) {
1275ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1276e405ae81Smatthias.ringwald         return;
1277e405ae81Smatthias.ringwald     }
1278e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
12797ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1280e7ff783cSmatthias.ringwald     l2cap_run();
1281645658c9Smatthias.ringwald }
1282645658c9Smatthias.ringwald 
12837f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1284b1988dceSmatthias.ringwald 
1285b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1286b1988dceSmatthias.ringwald 
1287f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
128863a7246aSmatthias.ringwald     if (flags & 1) {
128963a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
129063a7246aSmatthias.ringwald     }
129163a7246aSmatthias.ringwald 
12922784b77dSmatthias.ringwald     // accept the other's configuration options
1293f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
12943de7c0caSmatthias.ringwald     uint16_t pos     = 8;
12953de7c0caSmatthias.ringwald     while (pos < end_pos){
129663a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
129763a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
129863a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
129963a7246aSmatthias.ringwald         pos++;
13001dc511deSmatthias.ringwald         uint8_t length = command[pos++];
13011dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
130263a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1303f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
13049da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
130563a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
130663a7246aSmatthias.ringwald         }
13070fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
13080fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1309f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
13100fe7a9d0S[email protected]         }
131163a7246aSmatthias.ringwald         // check for unknown options
131263a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1313c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
131463a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
13151dc511deSmatthias.ringwald         }
13161dc511deSmatthias.ringwald         pos += length;
13171dc511deSmatthias.ringwald     }
13182784b77dSmatthias.ringwald }
13192784b77dSmatthias.ringwald 
1320fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
13219da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
132273cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
132373cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1324019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1325019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1326fa8473a4Smatthias.ringwald     return 1;
1327fa8473a4Smatthias.ringwald }
1328fa8473a4Smatthias.ringwald 
1329fa8473a4Smatthias.ringwald 
13307f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
13311e6aba47Smatthias.ringwald 
133200d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
133300d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
133438e5900eSmatthias.ringwald     uint16_t result = 0;
13351e6aba47Smatthias.ringwald 
13369da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1337b35f641cSmatthias.ringwald 
13389a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
13399a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
13409a011532Smatthias.ringwald         switch (channel->state){
1341fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
13429a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
13432b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
13449a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
13459a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
13469a011532Smatthias.ringwald                 break;
13479a011532Smatthias.ringwald 
13489a011532Smatthias.ringwald             default:
13499a011532Smatthias.ringwald                 // ignore in other states
13509a011532Smatthias.ringwald                 break;
13519a011532Smatthias.ringwald         }
13529a011532Smatthias.ringwald         return;
13539a011532Smatthias.ringwald     }
13549a011532Smatthias.ringwald 
135556081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
13561e6aba47Smatthias.ringwald     switch (channel->state) {
13571e6aba47Smatthias.ringwald 
13581e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
13591e6aba47Smatthias.ringwald             switch (code){
13601e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
13615932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1362f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
136338e5900eSmatthias.ringwald                     switch (result) {
136438e5900eSmatthias.ringwald                         case 0:
1365169f8b28Smatthias.ringwald                             // successful connection
1366f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1367fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
136828ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
136938e5900eSmatthias.ringwald                             break;
137038e5900eSmatthias.ringwald                         case 1:
13715932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
13725932bd7cS[email protected]                             l2cap_start_ertx(channel);
137338e5900eSmatthias.ringwald                             break;
137438e5900eSmatthias.ringwald                         default:
1375eb920dbeSmatthias.ringwald                             // channel closed
1376eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1377f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
137838e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1379eb920dbeSmatthias.ringwald 
1380eb920dbeSmatthias.ringwald                             // drop link key if security block
1381eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
138215a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1383eb920dbeSmatthias.ringwald                             }
1384eb920dbeSmatthias.ringwald 
1385eb920dbeSmatthias.ringwald                             // discard channel
1386665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1387d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
138838e5900eSmatthias.ringwald                             break;
13891e6aba47Smatthias.ringwald                     }
13901e6aba47Smatthias.ringwald                     break;
139138e5900eSmatthias.ringwald 
139238e5900eSmatthias.ringwald                 default:
13931e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
139438e5900eSmatthias.ringwald                     break;
13951e6aba47Smatthias.ringwald             }
13961e6aba47Smatthias.ringwald             break;
13971e6aba47Smatthias.ringwald 
1398fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1399f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1400ae280e73Smatthias.ringwald             switch (code) {
1401ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
140228ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1403ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
140463a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
140563a7246aSmatthias.ringwald                         // only done if continuation not set
140663a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
140763a7246aSmatthias.ringwald                     }
1408ae280e73Smatthias.ringwald                     break;
14091e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
14105932bd7cS[email protected]                     l2cap_stop_rtx(channel);
14115932bd7cS[email protected]                     switch (result){
14125932bd7cS[email protected]                         case 0: // success
14135932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
14145932bd7cS[email protected]                             break;
14155932bd7cS[email protected]                         case 4: // pending
14165932bd7cS[email protected]                             l2cap_start_ertx(channel);
14175932bd7cS[email protected]                             break;
14185932bd7cS[email protected]                         default:
1419fe9d8984S[email protected]                             // retry on negative result
1420fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1421fe9d8984S[email protected]                             break;
1422fe9d8984S[email protected]                     }
14235a67bd4aSmatthias.ringwald                     break;
14245a67bd4aSmatthias.ringwald                 default:
14255a67bd4aSmatthias.ringwald                     break;
14261e6aba47Smatthias.ringwald             }
1427fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1428fa8473a4Smatthias.ringwald                 // for open:
14295a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1430fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1431c8e4258aSmatthias.ringwald             }
1432c8e4258aSmatthias.ringwald             break;
1433f62db1e3Smatthias.ringwald 
1434f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1435f62db1e3Smatthias.ringwald             switch (code) {
1436f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
143727a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
143827a923d0Smatthias.ringwald                     break;
14395a67bd4aSmatthias.ringwald                 default:
14405a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
14415a67bd4aSmatthias.ringwald                     break;
144227a923d0Smatthias.ringwald             }
144327a923d0Smatthias.ringwald             break;
144484836b65Smatthias.ringwald 
144584836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
144684836b65Smatthias.ringwald             // @TODO handle incoming requests
144784836b65Smatthias.ringwald             break;
144884836b65Smatthias.ringwald 
144984836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
145084836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
145184836b65Smatthias.ringwald             break;
145210642e45Smatthias.ringwald         default:
145310642e45Smatthias.ringwald             break;
145427a923d0Smatthias.ringwald     }
14559da54300S[email protected]     // log_info("new state %u", channel->state);
145627a923d0Smatthias.ringwald }
145727a923d0Smatthias.ringwald 
145800d93d79Smatthias.ringwald 
14597f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
146000d93d79Smatthias.ringwald 
146100d93d79Smatthias.ringwald     // get code, signalind identifier and command len
146200d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
146300d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
146400d93d79Smatthias.ringwald 
146500d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
146600d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
146763a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
146800d93d79Smatthias.ringwald         return;
146900d93d79Smatthias.ringwald     }
147000d93d79Smatthias.ringwald 
147100d93d79Smatthias.ringwald     // general commands without an assigned channel
147200d93d79Smatthias.ringwald     switch(code) {
147300d93d79Smatthias.ringwald 
147400d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1475f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1476f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
147700d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
14782b83fb7dSmatthias.ringwald             return;
147900d93d79Smatthias.ringwald         }
148000d93d79Smatthias.ringwald 
14812b360848Smatthias.ringwald         case ECHO_REQUEST:
14822b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
14832b83fb7dSmatthias.ringwald             return;
148400d93d79Smatthias.ringwald 
148500d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1486f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
14872b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
14882b83fb7dSmatthias.ringwald             return;
148900d93d79Smatthias.ringwald         }
149000d93d79Smatthias.ringwald 
149100d93d79Smatthias.ringwald         default:
149200d93d79Smatthias.ringwald             break;
149300d93d79Smatthias.ringwald     }
149400d93d79Smatthias.ringwald 
149500d93d79Smatthias.ringwald 
149600d93d79Smatthias.ringwald     // Get potential destination CID
1497f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
149800d93d79Smatthias.ringwald 
149900d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1500665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1501665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1502665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1503665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1504fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
150500d93d79Smatthias.ringwald         if (code & 1) {
1506b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1507b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
150800d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15094e32727eSmatthias.ringwald                 break;
151000d93d79Smatthias.ringwald             }
151100d93d79Smatthias.ringwald         } else {
1512b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
151300d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
151400d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
15154e32727eSmatthias.ringwald                 break;
151600d93d79Smatthias.ringwald             }
151700d93d79Smatthias.ringwald         }
151800d93d79Smatthias.ringwald     }
151900d93d79Smatthias.ringwald }
152000d93d79Smatthias.ringwald 
1521e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1522c48b2a2cSMatthias Ringwald // @returns valid
1523c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1524e7d0c9aaSMatthias Ringwald     hci_connection_t * connection;
1525c48b2a2cSMatthias Ringwald     uint16_t result;
1526c48b2a2cSMatthias Ringwald     uint8_t  event[10];
1527e7d0c9aaSMatthias Ringwald     uint16_t le_psm;
1528*828a7f7aSMatthias Ringwald     uint16_t local_cid;
1529e7d0c9aaSMatthias Ringwald     l2cap_service_t * service;
15301b8b8d05SMatthias Ringwald     l2cap_channel_t * channel;
15311b8b8d05SMatthias Ringwald     btstack_linked_list_iterator_t it;
153200d93d79Smatthias.ringwald 
15331b8b8d05SMatthias Ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
153423017473SMatthias Ringwald     log_info("l2cap_le_signaling_handler_dispatch: command 0x%02x, sig id %u", code, sig_id);
15351b8b8d05SMatthias Ringwald 
15361b8b8d05SMatthias Ringwald     switch (code){
153700d93d79Smatthias.ringwald 
1538c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1539c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1540ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1541ccf076adS[email protected]             break;
1542da886c03S[email protected] 
1543c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1544e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1545da886c03S[email protected]             if (connection){
15466d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
15476d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1548c48b2a2cSMatthias Ringwald                     return 0;
15496d91fb6cSMatthias Ringwald                 }
1550da886c03S[email protected]                 int update_parameter = 1;
1551a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
15524ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1553c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1554c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1555c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1556c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1557da886c03S[email protected] 
1558da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1559da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1560da886c03S[email protected] 
1561da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1562da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1563da886c03S[email protected] 
1564da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1565da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1566da886c03S[email protected] 
1567da886c03S[email protected]                 if (update_parameter){
1568da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1569da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1570da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1571da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1572da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1573da886c03S[email protected]                 } else {
1574da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1575da886c03S[email protected]                 }
1576c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1577da886c03S[email protected]             }
1578da886c03S[email protected] 
157933c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1580c48b2a2cSMatthias Ringwald 
1581c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1582c48b2a2cSMatthias Ringwald             event[1] = 8;
1583c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1584c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
158533c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1586ccf076adS[email protected]             break;
1587c48b2a2cSMatthias Ringwald 
1588e7d0c9aaSMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_REQUEST:
1589e7d0c9aaSMatthias Ringwald 
1590e7d0c9aaSMatthias Ringwald             // get hci connection, bail if not found (must not happen)
1591e7d0c9aaSMatthias Ringwald             connection = hci_connection_for_handle(handle);
1592e7d0c9aaSMatthias Ringwald             if (!connection) return 0;
1593e7d0c9aaSMatthias Ringwald 
1594e7d0c9aaSMatthias Ringwald             // check if service registered
1595e7d0c9aaSMatthias Ringwald             le_psm  = little_endian_read_16(command, 4);
1596e7d0c9aaSMatthias Ringwald             service = l2cap_le_get_service(le_psm);
1597e7d0c9aaSMatthias Ringwald 
1598e7d0c9aaSMatthias Ringwald             if (service){
1599e7d0c9aaSMatthias Ringwald 
1600e7d0c9aaSMatthias Ringwald                 uint16_t source_cid = little_endian_read_16(command, 6);
1601e7d0c9aaSMatthias Ringwald                 if (source_cid < 0x40){
1602e7d0c9aaSMatthias Ringwald                     // 0x0009 Connection refused - Invalid Source CID
1603e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0009);
1604e7d0c9aaSMatthias Ringwald                     return 1;
1605e7d0c9aaSMatthias Ringwald                 }
1606e7d0c9aaSMatthias Ringwald 
1607e7d0c9aaSMatthias Ringwald                 // go through list of channels for this ACL connection and check if we get a match
1608e7d0c9aaSMatthias Ringwald                 btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
1609e7d0c9aaSMatthias Ringwald                 while (btstack_linked_list_iterator_has_next(&it)){
16101b8b8d05SMatthias Ringwald                     l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16111b8b8d05SMatthias Ringwald                     if (a_channel->con_handle != handle) continue;
16121b8b8d05SMatthias Ringwald                     if (a_channel->remote_cid != source_cid) continue;
1613e7d0c9aaSMatthias Ringwald                     // 0x000a Connection refused - Source CID already allocated
1614e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x000a);
1615e7d0c9aaSMatthias Ringwald                     return 1;
1616e7d0c9aaSMatthias Ringwald                 }
1617e7d0c9aaSMatthias Ringwald 
1618e7d0c9aaSMatthias Ringwald                 // TODO: deal with authentication requirements, errors 0x005 - 000x8
1619e7d0c9aaSMatthias Ringwald 
1620e7d0c9aaSMatthias Ringwald                 // allocate channel
16211b8b8d05SMatthias Ringwald                 channel = l2cap_create_channel_entry(service->packet_handler, connection->address,
1622e7d0c9aaSMatthias Ringwald                     BD_ADDR_TYPE_LE_RANDOM, le_psm, service->mtu, service->required_security_level);
1623e7d0c9aaSMatthias Ringwald                 if (!channel){
1624e7d0c9aaSMatthias Ringwald                     // 0x0004 Connection refused – no resources available
1625e7d0c9aaSMatthias Ringwald                     l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0004);
1626e7d0c9aaSMatthias Ringwald                     return 1;
1627e7d0c9aaSMatthias Ringwald                 }
1628e7d0c9aaSMatthias Ringwald 
1629e7d0c9aaSMatthias Ringwald                 channel->con_handle = handle;
1630e7d0c9aaSMatthias Ringwald                 channel->remote_cid = source_cid;
1631e7d0c9aaSMatthias Ringwald                 channel->remote_sig_id = sig_id;
1632e7d0c9aaSMatthias Ringwald 
1633e7d0c9aaSMatthias Ringwald                 // limit local mtu to max acl packet length - l2cap header
1634e7d0c9aaSMatthias Ringwald                 if (channel->local_mtu > l2cap_max_le_mtu()) {
1635e7d0c9aaSMatthias Ringwald                     channel->local_mtu = l2cap_max_le_mtu();
1636e7d0c9aaSMatthias Ringwald                 }
1637e7d0c9aaSMatthias Ringwald 
1638e7d0c9aaSMatthias Ringwald                 // set initial state
1639e7d0c9aaSMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1640e7d0c9aaSMatthias Ringwald 
1641e7d0c9aaSMatthias Ringwald                 // add to connections list
1642e7d0c9aaSMatthias Ringwald                 btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1643e7d0c9aaSMatthias Ringwald 
1644e7d0c9aaSMatthias Ringwald                 // post connection request event
1645e7d0c9aaSMatthias Ringwald                 l2cap_emit_connection_request(channel);
1646e7d0c9aaSMatthias Ringwald 
1647e7d0c9aaSMatthias Ringwald             } else {
1648e7d0c9aaSMatthias Ringwald                 // Connection refused – LE_PSM not supported
1649e7d0c9aaSMatthias Ringwald                 l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, 0x0002);
1650e7d0c9aaSMatthias Ringwald             }
1651e7d0c9aaSMatthias Ringwald             break;
16521b8b8d05SMatthias Ringwald 
16531b8b8d05SMatthias Ringwald         case LE_CREDIT_BASED_CONNECTION_RESPONSE:
16541b8b8d05SMatthias Ringwald             // Find channel for this sig_id and connection handle
16551b8b8d05SMatthias Ringwald             channel = NULL;
16561b8b8d05SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
16571b8b8d05SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
16581b8b8d05SMatthias Ringwald                 l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
16591b8b8d05SMatthias Ringwald                 if (a_channel->con_handle   != handle) continue;
16601b8b8d05SMatthias Ringwald                 if (a_channel->local_sig_id != sig_id) continue;
16611b8b8d05SMatthias Ringwald                 channel = a_channel;
16621b8b8d05SMatthias Ringwald                 break;
16631b8b8d05SMatthias Ringwald             }
16641b8b8d05SMatthias Ringwald             // TODO: send error
16651b8b8d05SMatthias Ringwald             if (!channel) break;
16661b8b8d05SMatthias Ringwald 
16671b8b8d05SMatthias Ringwald             // cid + 0
16681b8b8d05SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
16691b8b8d05SMatthias Ringwald             if (result){
16701b8b8d05SMatthias Ringwald                 channel->state = L2CAP_STATE_CLOSED;
16711b8b8d05SMatthias Ringwald                 // map l2cap connection response result to BTstack status enumeration
16721b8b8d05SMatthias Ringwald                 l2cap_emit_channel_opened(channel, result);
16731b8b8d05SMatthias Ringwald 
16741b8b8d05SMatthias Ringwald                 // discard channel
16751b8b8d05SMatthias Ringwald                 btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
16761b8b8d05SMatthias Ringwald                 btstack_memory_l2cap_channel_free(channel);
16771b8b8d05SMatthias Ringwald                 break;
16781b8b8d05SMatthias Ringwald             }
16791b8b8d05SMatthias Ringwald 
16801b8b8d05SMatthias Ringwald             // success
16811b8b8d05SMatthias Ringwald             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
16821b8b8d05SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
16831b8b8d05SMatthias Ringwald             channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
16841b8b8d05SMatthias Ringwald             channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
16851b8b8d05SMatthias Ringwald             channel->state = L2CAP_STATE_OPEN;
16861b8b8d05SMatthias Ringwald             l2cap_emit_channel_opened(channel, result);
16871b8b8d05SMatthias Ringwald             break;
16881b8b8d05SMatthias Ringwald 
1689*828a7f7aSMatthias Ringwald         case DISCONNECTION_REQUEST:
1690*828a7f7aSMatthias Ringwald             // find channel
1691*828a7f7aSMatthias Ringwald             local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
1692*828a7f7aSMatthias Ringwald             channel = l2cap_le_get_channel_for_local_cid(local_cid);
1693*828a7f7aSMatthias Ringwald             if (!channel) break;
1694*828a7f7aSMatthias Ringwald             channel->remote_sig_id = sig_id;
1695*828a7f7aSMatthias Ringwald             channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1696*828a7f7aSMatthias Ringwald             break;
1697*828a7f7aSMatthias Ringwald 
1698c48b2a2cSMatthias Ringwald         default:
1699c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1700c48b2a2cSMatthias Ringwald             return 0;
1701ccf076adS[email protected]     }
1702c48b2a2cSMatthias Ringwald     return 1;
1703c48b2a2cSMatthias Ringwald }
1704e7d0c9aaSMatthias Ringwald #endif
1705c48b2a2cSMatthias Ringwald 
1706c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
1707c48b2a2cSMatthias Ringwald 
1708c48b2a2cSMatthias Ringwald     // Get Channel ID
1709c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1710c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1711c48b2a2cSMatthias Ringwald 
1712c48b2a2cSMatthias Ringwald     switch (channel_id) {
1713c48b2a2cSMatthias Ringwald 
1714c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1715c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1716c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1717c48b2a2cSMatthias Ringwald                 // handle signaling commands
1718c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1719c48b2a2cSMatthias Ringwald 
1720c48b2a2cSMatthias Ringwald                 // increment command_offset
1721c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1722c48b2a2cSMatthias Ringwald             }
1723c48b2a2cSMatthias Ringwald             break;
1724c48b2a2cSMatthias Ringwald         }
1725c48b2a2cSMatthias Ringwald 
1726e7d0c9aaSMatthias Ringwald #ifdef ENABLE_BLE
1727e7d0c9aaSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE: {
1728e7d0c9aaSMatthias Ringwald             uint16_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1729e7d0c9aaSMatthias Ringwald             int      valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1730c48b2a2cSMatthias Ringwald             if (!valid){
17311bbc0b23S[email protected]                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
1732ccf076adS[email protected]             }
1733ccf076adS[email protected]             break;
1734e7d0c9aaSMatthias Ringwald         }
1735e7d0c9aaSMatthias Ringwald #endif
1736c48b2a2cSMatthias Ringwald 
1737c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1738c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1739c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1740ccf076adS[email protected]             }
1741c48b2a2cSMatthias Ringwald             break;
1742c48b2a2cSMatthias Ringwald 
1743c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1744c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1745c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1746c48b2a2cSMatthias Ringwald             }
1747c48b2a2cSMatthias Ringwald             break;
1748c48b2a2cSMatthias Ringwald 
1749c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1750c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1751c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1752c48b2a2cSMatthias Ringwald             }
1753c48b2a2cSMatthias Ringwald             break;
17541bbc0b23S[email protected] 
17555652b5ffS[email protected]         default: {
175600d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
175764e11ca9SMatthias Ringwald             l2cap_channel_t * l2cap_channel;
175864e11ca9SMatthias Ringwald             l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1759d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
17603d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
176100d93d79Smatthias.ringwald             }
176264e11ca9SMatthias Ringwald #ifdef ENABLE_BLE
176364e11ca9SMatthias Ringwald             l2cap_channel = l2cap_le_get_channel_for_local_cid(channel_id);
176464e11ca9SMatthias Ringwald             if (l2cap_channel) {
176564e11ca9SMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
176664e11ca9SMatthias Ringwald             }
176764e11ca9SMatthias Ringwald #endif
17685652b5ffS[email protected]             break;
17695652b5ffS[email protected]         }
17705652b5ffS[email protected]     }
177100d93d79Smatthias.ringwald 
17721eb2563eS[email protected]     l2cap_run();
17732718e2e7Smatthias.ringwald }
177400d93d79Smatthias.ringwald 
177515ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
177627a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1777f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1778f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1779f62db1e3Smatthias.ringwald     // discard channel
17809dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1781665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1782d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1783c8e4258aSmatthias.ringwald }
17841e6aba47Smatthias.ringwald 
1785*828a7f7aSMatthias Ringwald #ifdef ENABLE_BLE
1786*828a7f7aSMatthias Ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
1787*828a7f7aSMatthias Ringwald void l2cap_le_finialize_channel_close(l2cap_channel_t * channel){
1788*828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_CLOSED;
1789*828a7f7aSMatthias Ringwald     l2cap_emit_channel_closed(channel);
1790*828a7f7aSMatthias Ringwald     // discard channel
1791*828a7f7aSMatthias Ringwald     l2cap_stop_rtx(channel);
1792*828a7f7aSMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1793*828a7f7aSMatthias Ringwald     btstack_memory_l2cap_channel_free(channel);
1794*828a7f7aSMatthias Ringwald }
1795*828a7f7aSMatthias Ringwald #endif
1796*828a7f7aSMatthias Ringwald 
17978f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1798665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1799665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1800665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1801665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
18029d9bbc01Smatthias.ringwald         if ( service->psm == psm){
18039d9bbc01Smatthias.ringwald             return service;
18049d9bbc01Smatthias.ringwald         };
18059d9bbc01Smatthias.ringwald     }
18069d9bbc01Smatthias.ringwald     return NULL;
18079d9bbc01Smatthias.ringwald }
18089d9bbc01Smatthias.ringwald 
18097192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
18107192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
18117192e786SMatthias Ringwald }
18127192e786SMatthias Ringwald 
1813e0abb8e7S[email protected] 
1814be2053a6SMatthias 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){
1815be2053a6SMatthias Ringwald 
1816be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1817e0abb8e7S[email protected] 
18184bb582b6Smatthias.ringwald     // check for alread registered psm
18199d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1820277abc2cSmatthias.ringwald     if (service) {
1821be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1822be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1823277abc2cSmatthias.ringwald     }
18249d9bbc01Smatthias.ringwald 
18254bb582b6Smatthias.ringwald     // alloc structure
1826bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1827277abc2cSmatthias.ringwald     if (!service) {
1828be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1829be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1830277abc2cSmatthias.ringwald     }
18319d9bbc01Smatthias.ringwald 
18329d9bbc01Smatthias.ringwald     // fill in
18339d9bbc01Smatthias.ringwald     service->psm = psm;
18349d9bbc01Smatthias.ringwald     service->mtu = mtu;
183505ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1836df3354fcS[email protected]     service->required_security_level = security_level;
18379d9bbc01Smatthias.ringwald 
18389d9bbc01Smatthias.ringwald     // add to services list
1839665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1840c0e866bfSmatthias.ringwald 
1841c0e866bfSmatthias.ringwald     // enable page scan
184215a95bd5SMatthias Ringwald     gap_connectable_control(1);
18435842b6d9Smatthias.ringwald 
1844be2053a6SMatthias Ringwald     return 0;
18459d9bbc01Smatthias.ringwald }
18469d9bbc01Smatthias.ringwald 
18477e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
1848e0abb8e7S[email protected] 
1849e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1850e0abb8e7S[email protected] 
18519d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
18527e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1853665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1854d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1855c0e866bfSmatthias.ringwald 
1856c0e866bfSmatthias.ringwald     // disable page scan when no services registered
18577e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
185815a95bd5SMatthias Ringwald         gap_connectable_control(0);
18599d9bbc01Smatthias.ringwald     }
18607e8856ebSMatthias Ringwald     return 0;
18617e8856ebSMatthias Ringwald }
18629d9bbc01Smatthias.ringwald 
18635652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
186405ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
18655628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
18665628cf69SMatthias Ringwald     if (index < 0) return;
18675628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
18685652b5ffS[email protected] }
18695652b5ffS[email protected] 
1870a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
18717192e786SMatthias Ringwald 
1872e7d0c9aaSMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t le_psm){
1873e7d0c9aaSMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, le_psm);
18747192e786SMatthias Ringwald }
1875efedfb4cSMatthias Ringwald 
1876da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
18777192e786SMatthias Ringwald 
1878da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
18797192e786SMatthias Ringwald 
18807192e786SMatthias Ringwald     // check for alread registered psm
18817192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
18827192e786SMatthias Ringwald     if (service) {
1883da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
18847192e786SMatthias Ringwald     }
18857192e786SMatthias Ringwald 
18867192e786SMatthias Ringwald     // alloc structure
18877192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
18887192e786SMatthias Ringwald     if (!service) {
18897192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
1890da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
18917192e786SMatthias Ringwald     }
18927192e786SMatthias Ringwald 
18937192e786SMatthias Ringwald     // fill in
18947192e786SMatthias Ringwald     service->psm = psm;
1895da144af5SMatthias Ringwald     service->mtu = 0;
18967192e786SMatthias Ringwald     service->packet_handler = packet_handler;
18977192e786SMatthias Ringwald     service->required_security_level = security_level;
18987192e786SMatthias Ringwald 
18997192e786SMatthias Ringwald     // add to services list
1900665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
19017192e786SMatthias Ringwald 
19027192e786SMatthias Ringwald     // done
1903da144af5SMatthias Ringwald     return 0;
19047192e786SMatthias Ringwald }
19057192e786SMatthias Ringwald 
1906da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
19077192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
19087192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
19099367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1910da144af5SMatthias Ringwald 
1911665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
19127192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
1913da144af5SMatthias Ringwald     return 0;
19147192e786SMatthias Ringwald }
1915da144af5SMatthias Ringwald 
1916da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
1917e7d0c9aaSMatthias Ringwald     // get channel
1918e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
1919e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
1920e7d0c9aaSMatthias Ringwald 
1921e7d0c9aaSMatthias Ringwald     // validate state
1922e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
1923e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1924e7d0c9aaSMatthias Ringwald     }
1925e7d0c9aaSMatthias Ringwald 
1926efedfb4cSMatthias Ringwald     // set state accept connection
192723017473SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
192823017473SMatthias Ringwald     channel->receive_sdu_buffer = receive_sdu_buffer;
192923017473SMatthias Ringwald     channel->local_mtu = mtu;
193023017473SMatthias Ringwald     channel->credits_incoming = initial_credits;
1931e7d0c9aaSMatthias Ringwald 
1932e7d0c9aaSMatthias Ringwald     // go
1933e7d0c9aaSMatthias Ringwald     l2cap_run();
1934da144af5SMatthias Ringwald     return 0;
1935da144af5SMatthias Ringwald }
1936da144af5SMatthias Ringwald 
1937da144af5SMatthias Ringwald /**
1938da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
1939da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1940da144af5SMatthias Ringwald  */
1941da144af5SMatthias Ringwald 
1942da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
1943e7d0c9aaSMatthias Ringwald     // get channel
1944e7d0c9aaSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
1945e7d0c9aaSMatthias Ringwald     if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
1946e7d0c9aaSMatthias Ringwald 
1947e7d0c9aaSMatthias Ringwald     // validate state
1948e7d0c9aaSMatthias Ringwald     if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
1949e7d0c9aaSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1950e7d0c9aaSMatthias Ringwald     }
1951e7d0c9aaSMatthias Ringwald 
1952efedfb4cSMatthias Ringwald     // set state decline connection
1953e7d0c9aaSMatthias Ringwald     channel->state  = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
1954e7d0c9aaSMatthias Ringwald     channel->reason = 0x04; // no resources available
1955e7d0c9aaSMatthias Ringwald     l2cap_run();
1956da144af5SMatthias Ringwald     return 0;
1957da144af5SMatthias Ringwald }
1958da144af5SMatthias Ringwald 
1959da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
1960da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
1961efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
1962efedfb4cSMatthias Ringwald 
1963da144af5SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
1964da144af5SMatthias Ringwald 
19655cb87675SMatthias Ringwald l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, LEVEL_0);
1966da144af5SMatthias Ringwald     if (!channel) {
1967da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1968da144af5SMatthias Ringwald     }
1969e7d0c9aaSMatthias Ringwald     log_info("l2cap_le_create_channel %p", channel);
1970da144af5SMatthias Ringwald 
1971da144af5SMatthias Ringwald     // store local_cid
1972da144af5SMatthias Ringwald     if (out_local_cid){
1973da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
1974da144af5SMatthias Ringwald     }
1975da144af5SMatthias Ringwald 
1976e7d0c9aaSMatthias Ringwald     channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
1977e7d0c9aaSMatthias Ringwald 
1978efedfb4cSMatthias Ringwald     // add to connections list
1979efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1980efedfb4cSMatthias Ringwald 
1981da144af5SMatthias Ringwald     // check if hci connection is already usable
1982da144af5SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type);
1983efedfb4cSMatthias Ringwald 
1984da144af5SMatthias Ringwald     if (conn){
1985da144af5SMatthias Ringwald         log_info("l2cap_le_create_channel, hci connection already exists");
19865cb87675SMatthias Ringwald         l2cap_handle_le_connection_complete(channel, 0, conn->con_handle);
1987efedfb4cSMatthias Ringwald     } else {
1988efedfb4cSMatthias Ringwald 
1989efedfb4cSMatthias Ringwald         //
1990efedfb4cSMatthias Ringwald         // TODO: start timer
1991efedfb4cSMatthias Ringwald         // ..
1992efedfb4cSMatthias Ringwald 
1993efedfb4cSMatthias Ringwald         // start connection
1994efedfb4cSMatthias Ringwald         uint8_t res = gap_auto_connection_start(address_type, address);
199548af0c56SMatthias Ringwald         if (res){
1996efedfb4cSMatthias Ringwald             // discard channel object
1997e7d0c9aaSMatthias Ringwald             log_info("gap_auto_connection_start failed! 0x%02x", res);
1998e7d0c9aaSMatthias Ringwald             btstack_linked_list_remove(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1999efedfb4cSMatthias Ringwald             btstack_memory_l2cap_channel_free(channel);
2000efedfb4cSMatthias Ringwald             return res;
2001da144af5SMatthias Ringwald         }
2002efedfb4cSMatthias Ringwald     }
2003da144af5SMatthias Ringwald     return 0;
2004da144af5SMatthias Ringwald }
2005da144af5SMatthias Ringwald 
2006da144af5SMatthias Ringwald /**
2007da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
2008da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2009da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
2010da144af5SMatthias Ringwald  */
201164e11ca9SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
2012e7d0c9aaSMatthias Ringwald     // get channel
2013efedfb4cSMatthias Ringwald     // bail if missing
2014efedfb4cSMatthias Ringwald     // check state
2015efedfb4cSMatthias Ringwald     // check incoming credits + credits <= 0xffff
2016efedfb4cSMatthias Ringwald     // set credits_granted
2017efedfb4cSMatthias Ringwald     // l2cap_le_run()
2018da144af5SMatthias Ringwald     return 0;
2019da144af5SMatthias Ringwald }
2020da144af5SMatthias Ringwald 
2021da144af5SMatthias Ringwald /**
2022da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
2023da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2024da144af5SMatthias Ringwald  */
202564e11ca9SMatthias Ringwald int l2cap_le_can_send_now(uint16_t local_cid){
2026efedfb4cSMatthias Ringwald     // check if data can be sent via le at all
2027da144af5SMatthias Ringwald     return 0;
2028da144af5SMatthias Ringwald }
2029da144af5SMatthias Ringwald 
2030da144af5SMatthias Ringwald /**
2031da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
2032da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
2033da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
2034da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2035da144af5SMatthias Ringwald  */
203664e11ca9SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
2037efedfb4cSMatthias Ringwald     // same as for non-le
2038da144af5SMatthias Ringwald     return 0;
2039da144af5SMatthias Ringwald }
2040da144af5SMatthias Ringwald 
2041da144af5SMatthias Ringwald /**
2042da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
2043da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
2044da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2045da144af5SMatthias Ringwald  * @param data                  data to send
2046da144af5SMatthias Ringwald  * @param size                  data size
2047da144af5SMatthias Ringwald  */
204864e11ca9SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t local_cid, uint8_t * data, uint16_t len){
204964e11ca9SMatthias Ringwald 
205064e11ca9SMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
205164e11ca9SMatthias Ringwald     if (!channel) {
205264e11ca9SMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2053*828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
205464e11ca9SMatthias Ringwald     }
205564e11ca9SMatthias Ringwald 
205664e11ca9SMatthias Ringwald     if (len > channel->remote_mtu){
205764e11ca9SMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
205864e11ca9SMatthias Ringwald         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
205964e11ca9SMatthias Ringwald     }
206064e11ca9SMatthias Ringwald 
206164e11ca9SMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
206264e11ca9SMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
206364e11ca9SMatthias Ringwald         return BTSTACK_ACL_BUFFERS_FULL;
206464e11ca9SMatthias Ringwald     }
206564e11ca9SMatthias Ringwald 
206664e11ca9SMatthias Ringwald     hci_reserve_packet_buffer();
206764e11ca9SMatthias Ringwald     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
206864e11ca9SMatthias Ringwald 
206964e11ca9SMatthias Ringwald     memcpy(&acl_buffer[8], data, len);
207064e11ca9SMatthias Ringwald 
207164e11ca9SMatthias Ringwald     return l2cap_send_prepared_connectionless(channel->con_handle, local_cid, len);
2072da144af5SMatthias Ringwald }
2073da144af5SMatthias Ringwald 
2074da144af5SMatthias Ringwald /**
2075da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
2076da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
2077da144af5SMatthias Ringwald  */
2078*828a7f7aSMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t local_cid)
2079da144af5SMatthias Ringwald {
2080*828a7f7aSMatthias Ringwald     l2cap_channel_t * channel = l2cap_le_get_channel_for_local_cid(local_cid);
2081*828a7f7aSMatthias Ringwald     if (!channel) {
2082*828a7f7aSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
2083*828a7f7aSMatthias Ringwald         return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
2084*828a7f7aSMatthias Ringwald     }
2085*828a7f7aSMatthias Ringwald 
2086*828a7f7aSMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
2087*828a7f7aSMatthias Ringwald     l2cap_run();
2088da144af5SMatthias Ringwald     return 0;
2089da144af5SMatthias Ringwald }
2090da144af5SMatthias Ringwald 
20917f02f414SMatthias Ringwald #endif
2092