xref: /btstack/src/l2cap.c (revision c48b2a2cf14d777d2d8da7ec3a45007a1ec0b275)
143625864Smatthias.ringwald /*
2a0c35809S[email protected]  * Copyright (C) 2014 BlueKitchen GmbH
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
20a0c35809S[email protected]  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
33a0c35809S[email protected]  * Please inquire about commercial licensing options at
34a0c35809S[email protected]  * [email protected]
356b64433eSmatthias.ringwald  *
361713bceaSmatthias.ringwald  */
371713bceaSmatthias.ringwald 
381713bceaSmatthias.ringwald /*
3943625864Smatthias.ringwald  *  l2cap.c
4043625864Smatthias.ringwald  *
4143625864Smatthias.ringwald  *  Logical Link Control and Adaption Protocl (L2CAP)
4243625864Smatthias.ringwald  *
4343625864Smatthias.ringwald  *  Created by Matthias Ringwald on 5/16/09.
4443625864Smatthias.ringwald  */
4543625864Smatthias.ringwald 
4643625864Smatthias.ringwald #include "l2cap.h"
47645658c9Smatthias.ringwald #include "hci.h"
482b3c6c9bSmatthias.ringwald #include "hci_dump.h"
4916ece135SMatthias Ringwald #include "btstack_debug.h"
500e2df43fSMatthias Ringwald #include "btstack_event.h"
51d3a9df87Smatthias.ringwald #include "btstack_memory.h"
5243625864Smatthias.ringwald 
5343625864Smatthias.ringwald #include <stdarg.h>
5443625864Smatthias.ringwald #include <string.h>
5543625864Smatthias.ringwald 
5643625864Smatthias.ringwald #include <stdio.h>
5743625864Smatthias.ringwald 
584c744e21Smatthias.ringwald // nr of buffered acl packets in outgoing queue to get max performance
594c744e21Smatthias.ringwald #define NR_BUFFERED_ACL_PACKETS 3
604c744e21Smatthias.ringwald 
6139bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
62e16a9cacSmatthias.ringwald #define NR_PENDING_SIGNALING_RESPONSES 3
6339bda6d5Smatthias.ringwald 
6400d93d79Smatthias.ringwald // offsets for L2CAP SIGNALING COMMANDS
6500d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_CODE_OFFSET   0
6600d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET  1
6700d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
6800d93d79Smatthias.ringwald #define L2CAP_SIGNALING_COMMAND_DATA_OFFSET   4
6900d93d79Smatthias.ringwald 
705628cf69SMatthias Ringwald // internal table
715628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL 0
725628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL  1
735628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL 2
745628cf69SMatthias Ringwald #define L2CAP_FIXED_CHANNEL_TABLE_SIZE (L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL+1)
755628cf69SMatthias Ringwald 
7633c40538SMatthias Ringwald // prototypes
7733c40538SMatthias Ringwald static void l2cap_finialize_channel_close(l2cap_channel_t *channel);
7833c40538SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
7933c40538SMatthias Ringwald static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
8034e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
8133c40538SMatthias Ringwald static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
8233c40538SMatthias Ringwald static void l2cap_emit_connection_request(l2cap_channel_t *channel);
8333c40538SMatthias Ringwald static int  l2cap_channel_ready_for_open(l2cap_channel_t *channel);
8433c40538SMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
853d50b4baSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
8630725612SMatthias Ringwald static void l2cap_notify_channel_can_send(void);
8733c40538SMatthias Ringwald 
885628cf69SMatthias Ringwald typedef struct l2cap_fixed_channel {
895628cf69SMatthias Ringwald     btstack_packet_handler_t callback;
902125de09SMatthias Ringwald     uint8_t waiting_for_can_send_now;
915628cf69SMatthias Ringwald } l2cap_fixed_channel_t;
925628cf69SMatthias Ringwald 
935628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_channels;
945628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_services;
955628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_channels;
965628cf69SMatthias Ringwald static btstack_linked_list_t l2cap_le_services;
9739bda6d5Smatthias.ringwald 
9839bda6d5Smatthias.ringwald // used to cache l2cap rejects, echo, and informational requests
992b83fb7dSmatthias.ringwald static l2cap_signaling_response_t signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
1002b83fb7dSmatthias.ringwald static int signaling_responses_pending;
1012b83fb7dSmatthias.ringwald 
10233c40538SMatthias Ringwald static uint8_t require_security_level2_for_outgoing_sdp;
10333c40538SMatthias Ringwald 
104fb37a842SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
105fb37a842SMatthias Ringwald 
10633c40538SMatthias Ringwald static btstack_packet_handler_t l2cap_event_packet_handler;
1075628cf69SMatthias Ringwald static l2cap_fixed_channel_t fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_SIZE];
10839bda6d5Smatthias.ringwald 
10934e7e577SMatthias Ringwald static uint16_t l2cap_fixed_channel_table_channel_id_for_index(int index){
11034e7e577SMatthias Ringwald     switch (index){
11134e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL:
11234e7e577SMatthias Ringwald             return L2CAP_CID_ATTRIBUTE_PROTOCOL;
11334e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL:
11434e7e577SMatthias Ringwald             return L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
11534e7e577SMatthias Ringwald         case L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL:
11634e7e577SMatthias Ringwald             return L2CAP_CID_CONNECTIONLESS_CHANNEL;
11734e7e577SMatthias Ringwald         default:
11834e7e577SMatthias Ringwald             return 0;
11934e7e577SMatthias Ringwald     }
12034e7e577SMatthias Ringwald }
1215628cf69SMatthias Ringwald static int l2cap_fixed_channel_table_index_for_channel_id(uint16_t channel_id){
1225628cf69SMatthias Ringwald     switch (channel_id){
1235628cf69SMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1245628cf69SMatthias Ringwald             return L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL;
1255628cf69SMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1265628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL;
1275628cf69SMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1285628cf69SMatthias Ringwald             return  L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL;
1295628cf69SMatthias Ringwald         default:
1305628cf69SMatthias Ringwald             return -1;
1315628cf69SMatthias Ringwald         }
1325628cf69SMatthias Ringwald }
13339bda6d5Smatthias.ringwald 
13434e7e577SMatthias Ringwald static int l2cap_fixed_channel_table_index_is_le(int index){
13534e7e577SMatthias Ringwald     if (index == L2CAP_CID_CONNECTIONLESS_CHANNEL) return 0;
13634e7e577SMatthias Ringwald     return 1;
13734e7e577SMatthias Ringwald }
13834e7e577SMatthias Ringwald 
13971de195eSMatthias Ringwald void l2cap_init(void){
1402b83fb7dSmatthias.ringwald     signaling_responses_pending = 0;
141808a48abSmatthias.ringwald 
142f5454fc6Smatthias.ringwald     l2cap_channels = NULL;
143f5454fc6Smatthias.ringwald     l2cap_services = NULL;
1447192e786SMatthias Ringwald     l2cap_le_services = NULL;
1457192e786SMatthias Ringwald     l2cap_le_channels = NULL;
146f5454fc6Smatthias.ringwald 
14733c40538SMatthias Ringwald     l2cap_event_packet_handler = NULL;
1485628cf69SMatthias Ringwald     memset(fixed_channels, 0, sizeof(fixed_channels));
149f5454fc6Smatthias.ringwald 
150ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 0;
151ac301f95S[email protected] 
152fcadd0caSmatthias.ringwald     //
1532718e2e7Smatthias.ringwald     // register callback with HCI
154fcadd0caSmatthias.ringwald     //
155d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &l2cap_hci_event_handler;
156fb37a842SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
157fb37a842SMatthias Ringwald 
158d9a7306aSMatthias Ringwald     hci_register_acl_packet_handler(&l2cap_acl_handler);
159fb37a842SMatthias Ringwald 
16015a95bd5SMatthias Ringwald     gap_connectable_control(0); // no services yet
161fcadd0caSmatthias.ringwald }
162fcadd0caSmatthias.ringwald 
163ffbf8201SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
16433c40538SMatthias Ringwald     l2cap_event_packet_handler = handler;
1651e6aba47Smatthias.ringwald }
1661e6aba47Smatthias.ringwald 
16717a9b554SMatthias Ringwald static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
16858de5610Smatthias.ringwald     (* (channel->packet_handler))(type, channel->local_cid, data, size);
16958de5610Smatthias.ringwald }
17058de5610Smatthias.ringwald 
17158de5610Smatthias.ringwald void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
172c9dc710bS[email protected]     log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u",
173fc64f94aSMatthias Ringwald              status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
174c9dc710bS[email protected]              channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
175c9dc710bS[email protected]     uint8_t event[23];
17658de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_OPENED;
17758de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
17858de5610Smatthias.ringwald     event[2] = status;
179724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[3]);
180fc64f94aSMatthias Ringwald     little_endian_store_16(event,  9, channel->con_handle);
181f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 11, channel->psm);
182f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 13, channel->local_cid);
183f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 15, channel->remote_cid);
184f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 17, channel->local_mtu);
185f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 19, channel->remote_mtu);
186f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 21, channel->flush_timeout);
18758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18817a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
18958de5610Smatthias.ringwald }
19058de5610Smatthias.ringwald 
19158de5610Smatthias.ringwald void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
192e0abb8e7S[email protected]     log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
19358de5610Smatthias.ringwald     uint8_t event[4];
19458de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_CHANNEL_CLOSED;
19558de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
196f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 2, channel->local_cid);
19758de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19817a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
19958de5610Smatthias.ringwald }
20058de5610Smatthias.ringwald 
20158de5610Smatthias.ringwald void l2cap_emit_connection_request(l2cap_channel_t *channel) {
202e0abb8e7S[email protected]     log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
203fc64f94aSMatthias Ringwald              bd_addr_to_str(channel->address), channel->con_handle,  channel->psm, channel->local_cid, channel->remote_cid);
20458de5610Smatthias.ringwald     uint8_t event[16];
20558de5610Smatthias.ringwald     event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
20658de5610Smatthias.ringwald     event[1] = sizeof(event) - 2;
207724d70a2SMatthias Ringwald     reverse_bd_addr(channel->address, &event[2]);
208fc64f94aSMatthias Ringwald     little_endian_store_16(event,  8, channel->con_handle);
209f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 10, channel->psm);
210f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 12, channel->local_cid);
211f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 14, channel->remote_cid);
21258de5610Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
21317a9b554SMatthias Ringwald     l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
2140af41d30Smatthias.ringwald }
215808a48abSmatthias.ringwald 
21634e7e577SMatthias Ringwald static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
21734e7e577SMatthias Ringwald     log_info("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
21833c40538SMatthias Ringwald     uint8_t event[4];
21933c40538SMatthias Ringwald     event[0] = L2CAP_EVENT_CAN_SEND_NOW;
22033c40538SMatthias Ringwald     event[1] = sizeof(event) - 2;
22134e7e577SMatthias Ringwald     little_endian_store_16(event, 2, channel);
22233c40538SMatthias Ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
22334e7e577SMatthias Ringwald     packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
22433c40538SMatthias Ringwald }
22533c40538SMatthias Ringwald 
226fc64f94aSMatthias Ringwald static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
227ccf076adS[email protected]     uint8_t event[6];
228ccf076adS[email protected]     event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
229ccf076adS[email protected]     event[1] = 4;
230fc64f94aSMatthias Ringwald     little_endian_store_16(event, 2, con_handle);
231f8fbdce0SMatthias Ringwald     little_endian_store_16(event, 4, result);
232ccf076adS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
23333c40538SMatthias Ringwald     if (!l2cap_event_packet_handler) return;
23433c40538SMatthias Ringwald     (*l2cap_event_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
235ccf076adS[email protected] }
236ccf076adS[email protected] 
2377f02f414SMatthias Ringwald static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
238665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
239665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
240665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
241665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
242b35f641cSmatthias.ringwald         if ( channel->local_cid == local_cid) {
243f62db1e3Smatthias.ringwald             return channel;
244f62db1e3Smatthias.ringwald         }
245f62db1e3Smatthias.ringwald     }
246f62db1e3Smatthias.ringwald     return NULL;
247f62db1e3Smatthias.ringwald }
248f62db1e3Smatthias.ringwald 
2495cb87675SMatthias Ringwald static l2cap_channel_t * l2cap_get_le_channel_for_address_and_addr_type(bd_addr_t address, bd_addr_type_t address_type){
2505cb87675SMatthias Ringwald     btstack_linked_list_iterator_t it;
2515cb87675SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
2525cb87675SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2535cb87675SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
2545cb87675SMatthias Ringwald         if (channel->address_type != address_type)  continue;
2555cb87675SMatthias Ringwald         if (bd_addr_cmp(channel->address, address)) continue;
2565cb87675SMatthias Ringwald         return channel;
2575cb87675SMatthias Ringwald     }
2585cb87675SMatthias Ringwald     return NULL;
2595cb87675SMatthias Ringwald }
2605cb87675SMatthias Ringwald 
2610b9d7e78SMatthias Ringwald ///
2620b9d7e78SMatthias Ringwald 
26330725612SMatthias Ringwald void l2cap_request_can_send_now_event(uint16_t local_cid){
26430725612SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
26530725612SMatthias Ringwald     if (!channel) return;
26630725612SMatthias Ringwald     channel->waiting_for_can_send_now = 1;
26730725612SMatthias Ringwald     l2cap_notify_channel_can_send();
26830725612SMatthias Ringwald }
26930725612SMatthias Ringwald 
2700b9d7e78SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
2710b9d7e78SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
2720b9d7e78SMatthias Ringwald     if (index < 0) return;
2730b9d7e78SMatthias Ringwald     fixed_channels[index].waiting_for_can_send_now = 1;
2740b9d7e78SMatthias Ringwald     l2cap_notify_channel_can_send();
2750b9d7e78SMatthias Ringwald }
2760b9d7e78SMatthias Ringwald 
2770b9d7e78SMatthias Ringwald ///
2780b9d7e78SMatthias Ringwald 
2796b1fde37Smatthias.ringwald int  l2cap_can_send_packet_now(uint16_t local_cid){
2806b1fde37Smatthias.ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
2816b1fde37Smatthias.ringwald     if (!channel) return 0;
2820b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(channel->con_handle);
2837856fb31S[email protected] }
2847856fb31S[email protected] 
28583e7cdd9SMatthias Ringwald int  l2cap_can_send_prepared_packet_now(uint16_t local_cid){
28683e7cdd9SMatthias Ringwald     l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
28783e7cdd9SMatthias Ringwald     if (!channel) return 0;
2880b9d7e78SMatthias Ringwald     return hci_can_send_prepared_acl_packet_now(channel->con_handle);
28983e7cdd9SMatthias Ringwald }
29083e7cdd9SMatthias Ringwald 
291fc64f94aSMatthias Ringwald int  l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
2920b9d7e78SMatthias Ringwald     return hci_can_send_acl_packet_now(con_handle);
2932125de09SMatthias Ringwald }
2940b9d7e78SMatthias Ringwald 
2950b9d7e78SMatthias Ringwald ///
2963cab4fcaS[email protected] 
29796cbd662Smatthias.ringwald uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
29896cbd662Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
29996cbd662Smatthias.ringwald     if (channel) {
30096cbd662Smatthias.ringwald         return channel->remote_mtu;
30196cbd662Smatthias.ringwald     }
30296cbd662Smatthias.ringwald     return 0;
30396cbd662Smatthias.ringwald }
30496cbd662Smatthias.ringwald 
305ec820d77SMatthias Ringwald static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
306665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
307665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
308665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
309665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
3105932bd7cS[email protected]         if ( &channel->rtx == ts) {
3115932bd7cS[email protected]             return channel;
3125932bd7cS[email protected]         }
3135932bd7cS[email protected]     }
3145932bd7cS[email protected]     return NULL;
3155932bd7cS[email protected] }
3165932bd7cS[email protected] 
317ec820d77SMatthias Ringwald static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
3185932bd7cS[email protected]     l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
31995d2c8f4SMatthias Ringwald     if (!channel) return;
3205932bd7cS[email protected] 
3215932bd7cS[email protected]     log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
3225932bd7cS[email protected] 
3235932bd7cS[email protected]     // "When terminating the channel, it is not necessary to send a L2CAP_DisconnectReq
3245932bd7cS[email protected]     //  and enter WAIT_DISCONNECT state. Channels can be transitioned directly to the CLOSED state."
3255932bd7cS[email protected]     // notify client
3265932bd7cS[email protected]     l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
3275932bd7cS[email protected] 
3285932bd7cS[email protected]     // discard channel
3299dcb2fb2S[email protected]     // no need to stop timer here, it is removed from list during timer callback
330665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
3315932bd7cS[email protected]     btstack_memory_l2cap_channel_free(channel);
3325932bd7cS[email protected] }
3335932bd7cS[email protected] 
3345932bd7cS[email protected] static void l2cap_stop_rtx(l2cap_channel_t * channel){
3355932bd7cS[email protected]     log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
336528a4a3bSMatthias Ringwald     btstack_run_loop_remove_timer(&channel->rtx);
3375932bd7cS[email protected] }
3385932bd7cS[email protected] 
3395932bd7cS[email protected] static void l2cap_start_rtx(l2cap_channel_t * channel){
3405932bd7cS[email protected]     l2cap_stop_rtx(channel);
341cb0ff06bS[email protected]     log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
342528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
343528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
344528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3455932bd7cS[email protected] }
3465932bd7cS[email protected] 
3475932bd7cS[email protected] static void l2cap_start_ertx(l2cap_channel_t * channel){
3485932bd7cS[email protected]     log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
3495932bd7cS[email protected]     l2cap_stop_rtx(channel);
350528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
351528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
352528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&channel->rtx);
3535932bd7cS[email protected] }
3545932bd7cS[email protected] 
35571de195eSMatthias Ringwald void l2cap_require_security_level_2_for_outgoing_sdp(void){
356ac301f95S[email protected]     require_security_level2_for_outgoing_sdp = 1;
357ac301f95S[email protected] }
358ac301f95S[email protected] 
359df3354fcS[email protected] static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
360ac301f95S[email protected]     return (psm == PSM_SDP) && (!require_security_level2_for_outgoing_sdp);
361df3354fcS[email protected] }
3625932bd7cS[email protected] 
3637f02f414SMatthias Ringwald static int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
364b1d43497Smatthias.ringwald 
365a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3669da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
367b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
368b1d43497Smatthias.ringwald     }
369b1d43497Smatthias.ringwald 
3709da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3712a373862S[email protected]     hci_reserve_packet_buffer();
372facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
37358de5610Smatthias.ringwald     va_list argptr;
37458de5610Smatthias.ringwald     va_start(argptr, identifier);
37570efece1S[email protected]     uint16_t len = l2cap_create_signaling_classic(acl_buffer, handle, cmd, identifier, argptr);
37658de5610Smatthias.ringwald     va_end(argptr);
3779da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
378826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
37958de5610Smatthias.ringwald }
38058de5610Smatthias.ringwald 
381a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
3827f02f414SMatthias Ringwald static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){
38370efece1S[email protected] 
384a35252c8S[email protected]     if (!hci_can_send_acl_packet_now(handle)){
3859da54300S[email protected]         log_info("l2cap_send_signaling_packet, cannot send");
38670efece1S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
38770efece1S[email protected]     }
38870efece1S[email protected] 
3899da54300S[email protected]     // log_info("l2cap_send_signaling_packet type %u", cmd);
3902a373862S[email protected]     hci_reserve_packet_buffer();
391facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
39270efece1S[email protected]     va_list argptr;
39370efece1S[email protected]     va_start(argptr, identifier);
39470efece1S[email protected]     uint16_t len = l2cap_create_signaling_le(acl_buffer, handle, cmd, identifier, argptr);
39570efece1S[email protected]     va_end(argptr);
3969da54300S[email protected]     // log_info("l2cap_send_signaling_packet con %u!", handle);
397826f7347S[email protected]     return hci_send_acl_packet_buffer(len);
39870efece1S[email protected] }
39970efece1S[email protected] #endif
40070efece1S[email protected] 
401b1d43497Smatthias.ringwald uint8_t *l2cap_get_outgoing_buffer(void){
402facf93fdS[email protected]     return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER; // 8 bytes
403b1d43497Smatthias.ringwald }
4046218e6f1Smatthias.ringwald 
4056b4af23dS[email protected] int l2cap_reserve_packet_buffer(void){
4066b4af23dS[email protected]     return hci_reserve_packet_buffer();
4076b4af23dS[email protected] }
4086b4af23dS[email protected] 
40968a0fcf7S[email protected] void l2cap_release_packet_buffer(void){
41068a0fcf7S[email protected]     hci_release_packet_buffer();
41168a0fcf7S[email protected] }
41268a0fcf7S[email protected] 
41368a0fcf7S[email protected] 
414b1d43497Smatthias.ringwald int l2cap_send_prepared(uint16_t local_cid, uint16_t len){
415b1d43497Smatthias.ringwald 
416c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
417c8b9416aS[email protected]         log_error("l2cap_send_prepared called without reserving packet first");
418c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
419c8b9416aS[email protected]     }
420c8b9416aS[email protected] 
42158de5610Smatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
422b1d43497Smatthias.ringwald     if (!channel) {
4239da54300S[email protected]         log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
424b1d43497Smatthias.ringwald         return -1;   // TODO: define error
4256218e6f1Smatthias.ringwald     }
4266218e6f1Smatthias.ringwald 
427fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
4289da54300S[email protected]         log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
429a35252c8S[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
430a35252c8S[email protected]     }
431a35252c8S[email protected] 
432fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
433b1d43497Smatthias.ringwald 
434facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
435b1d43497Smatthias.ringwald 
436e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
437e9772277S[email protected] 
438e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
439fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, channel->con_handle | (pb << 12) | (0 << 14));
44058de5610Smatthias.ringwald     // 2 - ACL length
441f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
44258de5610Smatthias.ringwald     // 4 - L2CAP packet length
443f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
44458de5610Smatthias.ringwald     // 6 - L2CAP channel DEST
445f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, channel->remote_cid);
44658de5610Smatthias.ringwald     // send
447826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
44891b99603Smatthias.ringwald 
4496218e6f1Smatthias.ringwald     return err;
45058de5610Smatthias.ringwald }
45158de5610Smatthias.ringwald 
452fc64f94aSMatthias Ringwald int l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
4532149f12eSmatthias.ringwald 
454c8b9416aS[email protected]     if (!hci_is_packet_buffer_reserved()){
455c8b9416aS[email protected]         log_error("l2cap_send_prepared_connectionless called without reserving packet first");
4562149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
4572149f12eSmatthias.ringwald     }
4582149f12eSmatthias.ringwald 
459fc64f94aSMatthias Ringwald     if (!hci_can_send_prepared_acl_packet_now(con_handle)){
460fc64f94aSMatthias Ringwald         log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
461c8b9416aS[email protected]         return BTSTACK_ACL_BUFFERS_FULL;
462c8b9416aS[email protected]     }
463c8b9416aS[email protected] 
464fc64f94aSMatthias Ringwald     log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
4652149f12eSmatthias.ringwald 
466facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
4672149f12eSmatthias.ringwald 
468e9772277S[email protected]     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
469e9772277S[email protected] 
470e9772277S[email protected]     // 0 - Connection handle : PB=pb : BC=00
471fc64f94aSMatthias Ringwald     little_endian_store_16(acl_buffer, 0, con_handle | (pb << 12) | (0 << 14));
4722149f12eSmatthias.ringwald     // 2 - ACL length
473f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 2,  len + 4);
4742149f12eSmatthias.ringwald     // 4 - L2CAP packet length
475f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 4,  len + 0);
4762149f12eSmatthias.ringwald     // 6 - L2CAP channel DEST
477f8fbdce0SMatthias Ringwald     little_endian_store_16(acl_buffer, 6, cid);
4782149f12eSmatthias.ringwald     // send
479826f7347S[email protected]     int err = hci_send_acl_packet_buffer(len+8);
4802149f12eSmatthias.ringwald 
4812149f12eSmatthias.ringwald     return err;
4822149f12eSmatthias.ringwald }
4832149f12eSmatthias.ringwald 
484ce8f182eSMatthias Ringwald int l2cap_send(uint16_t local_cid, uint8_t *data, uint16_t len){
485b1d43497Smatthias.ringwald 
486a35252c8S[email protected]     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
487a35252c8S[email protected]     if (!channel) {
488ce8f182eSMatthias Ringwald         log_error("l2cap_send no channel for cid 0x%02x", local_cid);
489a35252c8S[email protected]         return -1;   // TODO: define error
490a35252c8S[email protected]     }
491a35252c8S[email protected] 
492f0efaa57S[email protected]     if (len > channel->remote_mtu){
493ce8f182eSMatthias Ringwald         log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", local_cid);
494f0efaa57S[email protected]         return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
495f0efaa57S[email protected]     }
496f0efaa57S[email protected] 
497fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(channel->con_handle)){
498ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", local_cid);
499b1d43497Smatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
500b1d43497Smatthias.ringwald     }
501b1d43497Smatthias.ringwald 
5022a373862S[email protected]     hci_reserve_packet_buffer();
503facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
504b1d43497Smatthias.ringwald 
505b1d43497Smatthias.ringwald     memcpy(&acl_buffer[8], data, len);
506b1d43497Smatthias.ringwald 
507b1d43497Smatthias.ringwald     return l2cap_send_prepared(local_cid, len);
508b1d43497Smatthias.ringwald }
509b1d43497Smatthias.ringwald 
510fc64f94aSMatthias Ringwald int l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
5112149f12eSmatthias.ringwald 
512fc64f94aSMatthias Ringwald     if (!hci_can_send_acl_packet_now(con_handle)){
513ce8f182eSMatthias Ringwald         log_info("l2cap_send cid 0x%02x, cannot send", cid);
5142149f12eSmatthias.ringwald         return BTSTACK_ACL_BUFFERS_FULL;
5152149f12eSmatthias.ringwald     }
5162149f12eSmatthias.ringwald 
5172a373862S[email protected]     hci_reserve_packet_buffer();
518facf93fdS[email protected]     uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
5192149f12eSmatthias.ringwald 
5202149f12eSmatthias.ringwald     memcpy(&acl_buffer[8], data, len);
5212149f12eSmatthias.ringwald 
522fc64f94aSMatthias Ringwald     return l2cap_send_prepared_connectionless(con_handle, cid, len);
5232149f12eSmatthias.ringwald }
5242149f12eSmatthias.ringwald 
525fc64f94aSMatthias Ringwald int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
526fc64f94aSMatthias Ringwald     return l2cap_send_signaling_packet(con_handle, ECHO_REQUEST, 0x77, len, data);
5270e37e417S[email protected] }
5280e37e417S[email protected] 
52928ca2b46S[email protected] static inline void channelStateVarSetFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
53028ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var | flag);
53128ca2b46S[email protected] }
53228ca2b46S[email protected] 
53328ca2b46S[email protected] static inline void channelStateVarClearFlag(l2cap_channel_t *channel, L2CAP_CHANNEL_STATE_VAR flag){
53428ca2b46S[email protected]     channel->state_var = (L2CAP_CHANNEL_STATE_VAR) (channel->state_var & ~flag);
53528ca2b46S[email protected] }
53628ca2b46S[email protected] 
53728ca2b46S[email protected] 
538b1d43497Smatthias.ringwald 
5398158c421Smatthias.ringwald // MARK: L2CAP_RUN
5402cd0be45Smatthias.ringwald // process outstanding signaling tasks
5417f02f414SMatthias Ringwald static void l2cap_run(void){
5422b83fb7dSmatthias.ringwald 
54322c29ab4SMatthias Ringwald     // log_info("l2cap_run: entered");
54422c29ab4SMatthias Ringwald 
5452b83fb7dSmatthias.ringwald     // check pending signaling responses
5462b83fb7dSmatthias.ringwald     while (signaling_responses_pending){
5472b83fb7dSmatthias.ringwald 
5482b83fb7dSmatthias.ringwald         hci_con_handle_t handle = signaling_responses[0].handle;
549a35252c8S[email protected] 
550a35252c8S[email protected]         if (!hci_can_send_acl_packet_now(handle)) break;
551a35252c8S[email protected] 
5522b83fb7dSmatthias.ringwald         uint8_t  sig_id = signaling_responses[0].sig_id;
5532b360848Smatthias.ringwald         uint16_t infoType = signaling_responses[0].data;    // INFORMATION_REQUEST
55463a7246aSmatthias.ringwald         uint16_t result   = signaling_responses[0].data;    // CONNECTION_REQUEST, COMMAND_REJECT
555f53da564S[email protected]         uint8_t  response_code = signaling_responses[0].code;
5562b83fb7dSmatthias.ringwald 
557f53da564S[email protected]         // remove first item before sending (to avoid sending response mutliple times)
558f53da564S[email protected]         signaling_responses_pending--;
559f53da564S[email protected]         int i;
560f53da564S[email protected]         for (i=0; i < signaling_responses_pending; i++){
561f53da564S[email protected]             memcpy(&signaling_responses[i], &signaling_responses[i+1], sizeof(l2cap_signaling_response_t));
562f53da564S[email protected]         }
563f53da564S[email protected] 
564f53da564S[email protected]         switch (response_code){
5652b360848Smatthias.ringwald             case CONNECTION_REQUEST:
5662b360848Smatthias.ringwald                 l2cap_send_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, 0, 0, result, 0);
5672bd8b7e7S[email protected]                 // also disconnect if result is 0x0003 - security blocked
5684d816277S[email protected]                 if (result == 0x0003){
5692bd8b7e7S[email protected]                     hci_disconnect_security_block(handle);
5704d816277S[email protected]                 }
5712b360848Smatthias.ringwald                 break;
5722b83fb7dSmatthias.ringwald             case ECHO_REQUEST:
5732b83fb7dSmatthias.ringwald                 l2cap_send_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
5742b83fb7dSmatthias.ringwald                 break;
5752b83fb7dSmatthias.ringwald             case INFORMATION_REQUEST:
5763b0484b3S[email protected]                 switch (infoType){
5773b0484b3S[email protected]                     case 1: { // Connectionless MTU
5783b0484b3S[email protected]                         uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
5793b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(connectionless_mtu), &connectionless_mtu);
5803b0484b3S[email protected]                         break;
5813b0484b3S[email protected]                     }
5823b0484b3S[email protected]                     case 2: { // Extended Features Supported
583462e630dS[email protected]                         // extended features request supported, features: fixed channels, unicast connectionless data reception
584462e630dS[email protected]                         uint32_t features = 0x280;
5853b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(features), &features);
5863b0484b3S[email protected]                         break;
5873b0484b3S[email protected]                     }
5883b0484b3S[email protected]                     case 3: { // Fixed Channels Supported
5893b0484b3S[email protected]                         uint8_t map[8];
5903b0484b3S[email protected]                         memset(map, 0, 8);
591288636a2SMatthias Ringwald                         map[0] = 0x06;  // L2CAP Signaling Channel (0x02) + Connectionless reception (0x04)
5923b0484b3S[email protected]                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 0, sizeof(map), &map);
5933b0484b3S[email protected]                         break;
5943b0484b3S[email protected]                     }
5953b0484b3S[email protected]                     default:
5962b83fb7dSmatthias.ringwald                         // all other types are not supported
5972b83fb7dSmatthias.ringwald                         l2cap_send_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, infoType, 1, 0, NULL);
5983b0484b3S[email protected]                         break;
5992b83fb7dSmatthias.ringwald                 }
6002b83fb7dSmatthias.ringwald                 break;
60163a7246aSmatthias.ringwald             case COMMAND_REJECT:
6025ca8d57bS[email protected]                 l2cap_send_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
603a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
60470efece1S[email protected]             case COMMAND_REJECT_LE:
60570efece1S[email protected]                 l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
60663a7246aSmatthias.ringwald                 break;
60770efece1S[email protected] #endif
6082b83fb7dSmatthias.ringwald             default:
6092b83fb7dSmatthias.ringwald                 // should not happen
6102b83fb7dSmatthias.ringwald                 break;
6112b83fb7dSmatthias.ringwald         }
6122b83fb7dSmatthias.ringwald     }
6132b83fb7dSmatthias.ringwald 
614ae280e73Smatthias.ringwald     uint8_t  config_options[4];
615665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
616665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
617665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
618baf94f06S[email protected] 
619665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
62022c29ab4SMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
6212cd0be45Smatthias.ringwald         switch (channel->state){
6222cd0be45Smatthias.ringwald 
623df3354fcS[email protected]             case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
624ad671560S[email protected]             case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
625fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
626a00031e2S[email protected]                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
627ad671560S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
628fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0);
629ad671560S[email protected]                 }
630ad671560S[email protected]                 break;
631ad671560S[email protected] 
63202b22dc4Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
633baf94f06S[email protected]                 if (!hci_can_send_command_packet_now()) break;
63464472d52Smatthias.ringwald                 // send connection request - set state first
63564472d52Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
63602b22dc4Smatthias.ringwald                 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
6378f8108aaSmatthias.ringwald                 hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
63802b22dc4Smatthias.ringwald                 break;
63902b22dc4Smatthias.ringwald 
640e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
641fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
64222c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
643fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, channel->reason, 0);
644e7ff783cSmatthias.ringwald                 // discard channel - l2cap_finialize_channel_close without sending l2cap close event
6459dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
646665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
647d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
648e7ff783cSmatthias.ringwald                 break;
649e7ff783cSmatthias.ringwald 
650552d92a1Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
651fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
652fa8473a4Smatthias.ringwald                 channel->state = L2CAP_STATE_CONFIG;
65328ca2b46S[email protected]                 channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
654fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 0, 0);
655552d92a1Smatthias.ringwald                 break;
656552d92a1Smatthias.ringwald 
6576fdcc387Smatthias.ringwald             case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
658fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
6596fdcc387Smatthias.ringwald                 // success, start l2cap handshake
660b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
6616fdcc387Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
662fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id, channel->psm, channel->local_cid);
6635932bd7cS[email protected]                 l2cap_start_rtx(channel);
6646fdcc387Smatthias.ringwald                 break;
6656fdcc387Smatthias.ringwald 
666fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
667fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
66873cf2b3dSmatthias.ringwald                 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
66963a7246aSmatthias.ringwald                     uint16_t flags = 0;
67028ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
67163a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
67263a7246aSmatthias.ringwald                         flags = 1;
67363a7246aSmatthias.ringwald                     } else {
67428ca2b46S[email protected]                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
67563a7246aSmatthias.ringwald                     }
67663a7246aSmatthias.ringwald                     if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
677fc64f94aSMatthias 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);
67863a7246aSmatthias.ringwald                     } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
67963a7246aSmatthias.ringwald                         config_options[0] = 1; // MTU
68063a7246aSmatthias.ringwald                         config_options[1] = 2; // len param
681f8fbdce0SMatthias Ringwald                         little_endian_store_16( (uint8_t*)&config_options, 2, channel->remote_mtu);
682fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 4, &config_options);
68363a7246aSmatthias.ringwald                         channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
68463a7246aSmatthias.ringwald                     } else {
685fc64f94aSMatthias Ringwald                         l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id, channel->remote_cid, flags, 0, 0, NULL);
68663a7246aSmatthias.ringwald                     }
68763a7246aSmatthias.ringwald                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
688fa8473a4Smatthias.ringwald                 }
68973cf2b3dSmatthias.ringwald                 else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
69028ca2b46S[email protected]                     channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
69128ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
692b1988dceSmatthias.ringwald                     channel->local_sig_id = l2cap_next_sig_id();
693ae280e73Smatthias.ringwald                     config_options[0] = 1; // MTU
694ae280e73Smatthias.ringwald                     config_options[1] = 2; // len param
695f8fbdce0SMatthias Ringwald                     little_endian_store_16( (uint8_t*)&config_options, 2, channel->local_mtu);
696fc64f94aSMatthias Ringwald                     l2cap_send_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id, channel->remote_cid, 0, 4, &config_options);
6975932bd7cS[email protected]                     l2cap_start_rtx(channel);
698fa8473a4Smatthias.ringwald                 }
699fa8473a4Smatthias.ringwald                 if (l2cap_channel_ready_for_open(channel)){
700552d92a1Smatthias.ringwald                     channel->state = L2CAP_STATE_OPEN;
701552d92a1Smatthias.ringwald                     l2cap_emit_channel_opened(channel, 0);  // success
702fa8473a4Smatthias.ringwald                 }
703552d92a1Smatthias.ringwald                 break;
704552d92a1Smatthias.ringwald 
705e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
706fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
70722c29ab4SMatthias Ringwald                 channel->state = L2CAP_STATE_INVALID;
708fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
7095932bd7cS[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 :)
710756102d3Smatthias.ringwald                 l2cap_finialize_channel_close(channel);  // -- remove from list
711e7ff783cSmatthias.ringwald                 break;
712e7ff783cSmatthias.ringwald 
713e7ff783cSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
714fc64f94aSMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
715b1988dceSmatthias.ringwald                 channel->local_sig_id = l2cap_next_sig_id();
7162cd0be45Smatthias.ringwald                 channel->state = L2CAP_STATE_WAIT_DISCONNECT;
717fc64f94aSMatthias Ringwald                 l2cap_send_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
7182cd0be45Smatthias.ringwald                 break;
7192cd0be45Smatthias.ringwald             default:
7202cd0be45Smatthias.ringwald                 break;
7212cd0be45Smatthias.ringwald         }
7222cd0be45Smatthias.ringwald     }
723da886c03S[email protected] 
724a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
725efedfb4cSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_le_channels);
726efedfb4cSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
727efedfb4cSMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
728efedfb4cSMatthias Ringwald         // log_info("l2cap_run: channel %p, state %u, var 0x%02x", channel, channel->state, channel->state_var);
729efedfb4cSMatthias Ringwald         switch (channel->state){
7305cb87675SMatthias Ringwald             case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
7315cb87675SMatthias Ringwald                 if (!hci_can_send_acl_packet_now(channel->con_handle)) break;
7325cb87675SMatthias Ringwald                 channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
7335cb87675SMatthias Ringwald                 // le psm, source cid, mtu, mps, initial credits
7345cb87675SMatthias 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);
7355cb87675SMatthias Ringwald                 break;
736efedfb4cSMatthias Ringwald             default:
737efedfb4cSMatthias Ringwald                 break;
738efedfb4cSMatthias Ringwald         }
739efedfb4cSMatthias Ringwald     }
740efedfb4cSMatthias Ringwald 
741da886c03S[email protected]     // send l2cap con paramter update if necessary
742da886c03S[email protected]     hci_connections_get_iterator(&it);
743665d90f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
744665d90f2SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
7455d14fa8fSMatthias Ringwald         if (connection->address_type != BD_ADDR_TYPE_LE_PUBLIC && connection->address_type != BD_ADDR_TYPE_LE_RANDOM) continue;
746b68d7bc3SMatthias Ringwald         if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
747da886c03S[email protected]         switch (connection->le_con_parameter_update_state){
748b68d7bc3SMatthias Ringwald             case CON_PARAMETER_UPDATE_SEND_REQUEST:
749b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
750b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, connection->le_con_param_update_identifier,
751b68d7bc3SMatthias Ringwald                                                connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
752b68d7bc3SMatthias Ringwald                 break;
753da886c03S[email protected]             case CON_PARAMETER_UPDATE_SEND_RESPONSE:
754b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
755b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
756da886c03S[email protected]                 break;
757da886c03S[email protected]             case CON_PARAMETER_UPDATE_DENY:
758b68d7bc3SMatthias Ringwald                 connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
759b68d7bc3SMatthias Ringwald                 l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
760da886c03S[email protected]                 break;
761da886c03S[email protected]             default:
762da886c03S[email protected]                 break;
763da886c03S[email protected]         }
764efedfb4cSMatthias Ringwald #if 0
765efedfb4cSMatthias Ringwald void l2cap_le_run(void){
766efedfb4cSMatthias Ringwald     for all channels
767efedfb4cSMatthias Ringwald         switch(state):
768efedfb4cSMatthias Ringwald 
769efedfb4cSMatthias Ringwald     if outgoing transfer active
770efedfb4cSMatthias Ringwald         send next chunk
771efedfb4cSMatthias Ringwald         if done, notify app
772efedfb4cSMatthias Ringwald }
773efedfb4cSMatthias Ringwald #endif
774da886c03S[email protected]     }
7754d7157c3S[email protected] #endif
776da886c03S[email protected] 
77722c29ab4SMatthias Ringwald     // log_info("l2cap_run: exit");
7782cd0be45Smatthias.ringwald }
7792cd0be45Smatthias.ringwald 
7804aa9e837Smatthias.ringwald uint16_t l2cap_max_mtu(void){
7814ff786cfS[email protected]     return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
782fa8c92f6Smatthias.ringwald }
783fa8c92f6Smatthias.ringwald 
78471de195eSMatthias Ringwald uint16_t l2cap_max_le_mtu(void){
7854ff786cfS[email protected]     return l2cap_max_mtu();
786e5e1518dS[email protected] }
787e5e1518dS[email protected] 
788fc64f94aSMatthias Ringwald static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
7892df5dadcS[email protected]     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
7905533f01eS[email protected]         log_info("l2cap_handle_connection_complete expected state");
7912df5dadcS[email protected]         // success, start l2cap handshake
792fc64f94aSMatthias Ringwald         channel->con_handle = con_handle;
7932df5dadcS[email protected]         // check remote SSP feature first
7942df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
7952df5dadcS[email protected]     }
7962df5dadcS[email protected] }
7972df5dadcS[email protected] 
798efedfb4cSMatthias Ringwald #ifdef ENABLE_BLE
7995cb87675SMatthias Ringwald static void l2cap_handle_le_connection_complete(l2cap_channel_t * channel, uint8_t status, hci_con_handle_t con_handle){
800efedfb4cSMatthias Ringwald     if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) {
801efedfb4cSMatthias Ringwald         log_info("l2cap_le_handle_connection_complete expected state");
8025cb87675SMatthias Ringwald         // TODO: bail if status != 0
8035cb87675SMatthias Ringwald 
804efedfb4cSMatthias Ringwald         // success, start l2cap handshake
805efedfb4cSMatthias Ringwald         channel->con_handle = con_handle;
8065cb87675SMatthias Ringwald         channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
807efedfb4cSMatthias Ringwald     }
808efedfb4cSMatthias Ringwald }
809efedfb4cSMatthias Ringwald #endif
810efedfb4cSMatthias Ringwald 
811efedfb4cSMatthias Ringwald 
8122df5dadcS[email protected] static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
8132df5dadcS[email protected]     if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
8142df5dadcS[email protected] 
8152df5dadcS[email protected]     // we have been waiting for remote supported features, if both support SSP,
816ac301f95S[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));
817fc64f94aSMatthias Ringwald     if (gap_ssp_supported_on_both_sides(channel->con_handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){
8182df5dadcS[email protected]         // request security level 2
8192df5dadcS[email protected]         channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
820fc64f94aSMatthias Ringwald         gap_request_security_level(channel->con_handle, LEVEL_2);
8212df5dadcS[email protected]         return;
8222df5dadcS[email protected]     }
8232df5dadcS[email protected]     // fine, go ahead
8242df5dadcS[email protected]     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
8252df5dadcS[email protected] }
8262df5dadcS[email protected] 
827da144af5SMatthias 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,
828da144af5SMatthias Ringwald     uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
829da144af5SMatthias Ringwald 
830da144af5SMatthias Ringwald     l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
831da144af5SMatthias Ringwald     if (!channel) {
832da144af5SMatthias Ringwald         return NULL;
833da144af5SMatthias Ringwald     }
834da144af5SMatthias Ringwald 
835da144af5SMatthias Ringwald      // Init memory (make valgrind happy)
836da144af5SMatthias Ringwald     memset(channel, 0, sizeof(l2cap_channel_t));
837da144af5SMatthias Ringwald 
838da144af5SMatthias Ringwald     // fill in
839da144af5SMatthias Ringwald     channel->packet_handler = packet_handler;
840da144af5SMatthias Ringwald     bd_addr_copy(channel->address, address);
841da144af5SMatthias Ringwald     channel->address_type = address_type;
842da144af5SMatthias Ringwald     channel->psm = psm;
843da144af5SMatthias Ringwald     channel->local_mtu  = local_mtu;
844da144af5SMatthias Ringwald     channel->remote_mtu = L2CAP_MINIMAL_MTU;
845da144af5SMatthias Ringwald     channel->required_security_level = security_level;
846da144af5SMatthias Ringwald 
847da144af5SMatthias Ringwald     //
848da144af5SMatthias Ringwald     channel->local_cid = l2cap_next_local_cid();
849da144af5SMatthias Ringwald     channel->con_handle = 0;
850da144af5SMatthias Ringwald 
851da144af5SMatthias Ringwald     // set initial state
852da144af5SMatthias Ringwald     channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
853da144af5SMatthias Ringwald     channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
854da144af5SMatthias Ringwald     channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
855da144af5SMatthias Ringwald     channel->local_sig_id = L2CAP_SIG_ID_INVALID;
856da144af5SMatthias Ringwald     return channel;
857da144af5SMatthias Ringwald }
858da144af5SMatthias Ringwald 
8599077cb15SMatthias Ringwald /**
8609077cb15SMatthias Ringwald  * @brief Creates L2CAP channel to the PSM of a remote device with baseband address. A new baseband connection will be initiated if necessary.
8619077cb15SMatthias Ringwald  * @param packet_handler
8629077cb15SMatthias Ringwald  * @param address
8639077cb15SMatthias Ringwald  * @param psm
8649077cb15SMatthias Ringwald  * @param mtu
8659077cb15SMatthias Ringwald  * @param local_cid
8669077cb15SMatthias Ringwald  */
8679077cb15SMatthias Ringwald 
868da144af5SMatthias 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){
869da144af5SMatthias Ringwald     log_info("L2CAP_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, local_mtu);
870da144af5SMatthias Ringwald 
871da144af5SMatthias Ringwald     if (local_mtu > l2cap_max_mtu()) {
872da144af5SMatthias Ringwald         local_mtu = l2cap_max_mtu();
873da144af5SMatthias Ringwald     }
874da144af5SMatthias Ringwald 
875da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, address, BD_ADDR_TYPE_CLASSIC, psm, local_mtu, LEVEL_0);
876fc64f94aSMatthias Ringwald     if (!channel) {
8779077cb15SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
8789077cb15SMatthias Ringwald     }
8799077cb15SMatthias Ringwald 
8809077cb15SMatthias Ringwald     // add to connections list
881fc64f94aSMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
8829077cb15SMatthias Ringwald 
8839077cb15SMatthias Ringwald     // store local_cid
8849077cb15SMatthias Ringwald     if (out_local_cid){
885fc64f94aSMatthias Ringwald        *out_local_cid = channel->local_cid;
8869077cb15SMatthias Ringwald     }
8879077cb15SMatthias Ringwald 
8889077cb15SMatthias Ringwald     // check if hci connection is already usable
8899077cb15SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_CLASSIC);
8909077cb15SMatthias Ringwald     if (conn){
891add0254bSMatthias Ringwald         log_info("l2cap_create_channel, hci connection already exists");
892fc64f94aSMatthias Ringwald         l2cap_handle_connection_complete(conn->con_handle, channel);
8939077cb15SMatthias Ringwald         // check if remote supported fearures are already received
8949077cb15SMatthias Ringwald         if (conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) {
895fc64f94aSMatthias Ringwald             l2cap_handle_remote_supported_features_received(channel);
8969077cb15SMatthias Ringwald         }
8979077cb15SMatthias Ringwald     }
8989077cb15SMatthias Ringwald 
8999077cb15SMatthias Ringwald     l2cap_run();
9009077cb15SMatthias Ringwald 
9019077cb15SMatthias Ringwald     return 0;
9029077cb15SMatthias Ringwald }
9039077cb15SMatthias Ringwald 
904ce8f182eSMatthias Ringwald void
905ce8f182eSMatthias Ringwald l2cap_disconnect(uint16_t local_cid, uint8_t reason){
906e0abb8e7S[email protected]     log_info("L2CAP_DISCONNECT local_cid 0x%x reason 0x%x", local_cid, reason);
907b35f641cSmatthias.ringwald     // find channel for local_cid
908b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
909f62db1e3Smatthias.ringwald     if (channel) {
910e7ff783cSmatthias.ringwald         channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
911f62db1e3Smatthias.ringwald     }
9122cd0be45Smatthias.ringwald     // process
9132cd0be45Smatthias.ringwald     l2cap_run();
91443625864Smatthias.ringwald }
9151e6aba47Smatthias.ringwald 
916afde0c52Smatthias.ringwald static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
917665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
918665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
919665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
920665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
921058e3d6bSMatthias Ringwald         if ( bd_addr_cmp( channel->address, address) != 0) continue;
922c22aecc9S[email protected]         // channel for this address found
923c22aecc9S[email protected]         switch (channel->state){
924c22aecc9S[email protected]             case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
925c22aecc9S[email protected]             case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
926afde0c52Smatthias.ringwald                 // failure, forward error code
927afde0c52Smatthias.ringwald                 l2cap_emit_channel_opened(channel, status);
928afde0c52Smatthias.ringwald                 // discard channel
9299dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
930665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
931d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
932c22aecc9S[email protected]                 break;
933c22aecc9S[email protected]             default:
934c22aecc9S[email protected]                 break;
935afde0c52Smatthias.ringwald         }
936afde0c52Smatthias.ringwald     }
937afde0c52Smatthias.ringwald }
938afde0c52Smatthias.ringwald 
939afde0c52Smatthias.ringwald static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
940665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
941665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
942665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
943665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
944058e3d6bSMatthias Ringwald         if ( ! bd_addr_cmp( channel->address, address) ){
9452df5dadcS[email protected]             l2cap_handle_connection_complete(handle, channel);
946afde0c52Smatthias.ringwald         }
947afde0c52Smatthias.ringwald     }
9486fdcc387Smatthias.ringwald     // process
9496fdcc387Smatthias.ringwald     l2cap_run();
950afde0c52Smatthias.ringwald }
951b448a0e7Smatthias.ringwald 
95233c40538SMatthias Ringwald static void l2cap_notify_channel_can_send(void){
95333c40538SMatthias Ringwald     btstack_linked_list_iterator_t it;
95433c40538SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
95533c40538SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
95633c40538SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
95733c40538SMatthias Ringwald         if (!channel->waiting_for_can_send_now) continue;
958fc64f94aSMatthias Ringwald         if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
95933c40538SMatthias Ringwald         channel->waiting_for_can_send_now = 0;
96034e7e577SMatthias Ringwald         l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
96133c40538SMatthias Ringwald     }
9622125de09SMatthias Ringwald 
9632125de09SMatthias Ringwald     int i;
9642125de09SMatthias Ringwald     for (i=0;i<L2CAP_FIXED_CHANNEL_TABLE_SIZE;i++){
965773c4106SMatthias Ringwald         if (!fixed_channels[i].callback) continue;
9662125de09SMatthias Ringwald         if (!fixed_channels[i].waiting_for_can_send_now) continue;
96734e7e577SMatthias Ringwald         int can_send;
96834e7e577SMatthias Ringwald         if (l2cap_fixed_channel_table_index_is_le(i)){
96934e7e577SMatthias Ringwald             can_send = hci_can_send_acl_le_packet_now();
97034e7e577SMatthias Ringwald         } else {
97134e7e577SMatthias Ringwald             can_send = hci_can_send_acl_classic_packet_now();
97234e7e577SMatthias Ringwald         }
97334e7e577SMatthias Ringwald         if (!can_send) continue;
97434e7e577SMatthias Ringwald         fixed_channels[i].waiting_for_can_send_now = 0;
97534e7e577SMatthias Ringwald         l2cap_emit_can_send_now(fixed_channels[i].callback, l2cap_fixed_channel_table_channel_id_for_index(i));
9762125de09SMatthias Ringwald     }
97733c40538SMatthias Ringwald }
97833c40538SMatthias Ringwald 
979d9a7306aSMatthias Ringwald static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
980afde0c52Smatthias.ringwald 
981afde0c52Smatthias.ringwald     bd_addr_t address;
982afde0c52Smatthias.ringwald     hci_con_handle_t handle;
983665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
9842d00edd4Smatthias.ringwald     int hci_con_used;
985afde0c52Smatthias.ringwald 
9860e2df43fSMatthias Ringwald     switch(hci_event_packet_get_type(packet)){
987afde0c52Smatthias.ringwald 
988afde0c52Smatthias.ringwald         // handle connection complete events
989afde0c52Smatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
990724d70a2SMatthias Ringwald             reverse_bd_addr(&packet[5], address);
991afde0c52Smatthias.ringwald             if (packet[2] == 0){
992f8fbdce0SMatthias Ringwald                 handle = little_endian_read_16(packet, 3);
993afde0c52Smatthias.ringwald                 l2cap_handle_connection_success_for_addr(address, handle);
994afde0c52Smatthias.ringwald             } else {
995afde0c52Smatthias.ringwald                 l2cap_handle_connection_failed_for_addr(address, packet[2]);
996afde0c52Smatthias.ringwald             }
997afde0c52Smatthias.ringwald             break;
998afde0c52Smatthias.ringwald 
999afde0c52Smatthias.ringwald         // handle successful create connection cancel command
1000afde0c52Smatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
1001073bd0faSMatthias Ringwald             if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_create_connection_cancel)) {
1002afde0c52Smatthias.ringwald                 if (packet[5] == 0){
1003724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[6], address);
1004afde0c52Smatthias.ringwald                     // CONNECTION TERMINATED BY LOCAL HOST (0X16)
1005afde0c52Smatthias.ringwald                     l2cap_handle_connection_failed_for_addr(address, 0x16);
100603cfbabcSmatthias.ringwald                 }
10071e6aba47Smatthias.ringwald             }
100839d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
100939d59809Smatthias.ringwald             break;
101039d59809Smatthias.ringwald 
101139d59809Smatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
101239d59809Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
1013afde0c52Smatthias.ringwald             break;
101427a923d0Smatthias.ringwald 
10151e6aba47Smatthias.ringwald         // handle disconnection complete events
1016afde0c52Smatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
1017c22aecc9S[email protected]             // send l2cap disconnect events for all channels on this handle and free them
1018f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1019665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1020665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1021665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1022fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
102315ec09bbSmatthias.ringwald                 l2cap_emit_channel_closed(channel);
10249dcb2fb2S[email protected]                 l2cap_stop_rtx(channel);
1025665d90f2SMatthias Ringwald                 btstack_linked_list_iterator_remove(&it);
1026d3a9df87Smatthias.ringwald                 btstack_memory_l2cap_channel_free(channel);
102727a923d0Smatthias.ringwald             }
1028afde0c52Smatthias.ringwald             break;
1029fcadd0caSmatthias.ringwald 
103033c40538SMatthias Ringwald         // Notify channel packet handler if they can send now
103163fa3374SMatthias Ringwald         case HCI_EVENT_TRANSPORT_PACKET_SENT:
10326218e6f1Smatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
103302b22dc4Smatthias.ringwald             l2cap_run();    // try sending signaling packets first
103433c40538SMatthias Ringwald             l2cap_notify_channel_can_send();
10356218e6f1Smatthias.ringwald             break;
10366218e6f1Smatthias.ringwald 
1037ee091cf1Smatthias.ringwald         // HCI Connection Timeouts
1038afde0c52Smatthias.ringwald         case L2CAP_EVENT_TIMEOUT_CHECK:
1039f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1040bd04d84aSMatthias Ringwald             if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) break;
104180ca58a0Smatthias.ringwald             if (hci_authentication_active_for_handle(handle)) break;
10422d00edd4Smatthias.ringwald             hci_con_used = 0;
1043665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1044665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1045665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1046fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10472d00edd4Smatthias.ringwald                 hci_con_used = 1;
1048c22aecc9S[email protected]                 break;
1049ee091cf1Smatthias.ringwald             }
10502d00edd4Smatthias.ringwald             if (hci_con_used) break;
1051d94d3cafS[email protected]             if (!hci_can_send_command_packet_now()) break;
10529edc8742Smatthias.ringwald             hci_send_cmd(&hci_disconnect, handle, 0x13); // remote closed connection
1053afde0c52Smatthias.ringwald             break;
1054ee091cf1Smatthias.ringwald 
1055df3354fcS[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
1056f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 3);
1057665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1058665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1059665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1060fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10612df5dadcS[email protected]                 l2cap_handle_remote_supported_features_received(channel);
1062df3354fcS[email protected]                 break;
1063df3354fcS[email protected]             }
1064c22aecc9S[email protected]             break;
1065df3354fcS[email protected] 
10665cb87675SMatthias Ringwald #ifdef ENABLE_BLE
10675cb87675SMatthias Ringwald         case HCI_EVENT_LE_META:
10685cb87675SMatthias Ringwald             switch (packet[2]){
10695cb87675SMatthias Ringwald                 int addr_type;
10705cb87675SMatthias Ringwald                 l2cap_channel_t * channel;
10715cb87675SMatthias Ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
10725cb87675SMatthias Ringwald                     // Connection management
10735cb87675SMatthias Ringwald                     reverse_bd_addr(&packet[8], address);
10745cb87675SMatthias Ringwald                     addr_type = (bd_addr_type_t)packet[7];
10755cb87675SMatthias Ringwald                     log_info("L2CAP - LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(address));
10765cb87675SMatthias Ringwald                     // get l2cap_channel
10775cb87675SMatthias Ringwald                     // also pass in status
10785cb87675SMatthias Ringwald                     channel = l2cap_get_le_channel_for_address_and_addr_type(address, addr_type);
10795cb87675SMatthias Ringwald                     if (!channel) break;
10805cb87675SMatthias Ringwald                     l2cap_handle_le_connection_complete(channel, packet[3], little_endian_read_16(packet, 4));
10815cb87675SMatthias Ringwald                     break;
10825cb87675SMatthias Ringwald                 default:
10835cb87675SMatthias Ringwald                     break;
10845cb87675SMatthias Ringwald             }
10855cb87675SMatthias Ringwald             break;
10865cb87675SMatthias Ringwald #endif
10875cb87675SMatthias Ringwald 
10885611a760SMatthias Ringwald         case GAP_EVENT_SECURITY_LEVEL:
1089f8fbdce0SMatthias Ringwald             handle = little_endian_read_16(packet, 2);
1090bd63148eS[email protected]             log_info("l2cap - security level update");
1091665d90f2SMatthias Ringwald             btstack_linked_list_iterator_init(&it, &l2cap_channels);
1092665d90f2SMatthias Ringwald             while (btstack_linked_list_iterator_has_next(&it)){
1093665d90f2SMatthias Ringwald                 l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1094fc64f94aSMatthias Ringwald                 if (channel->con_handle != handle) continue;
10955533f01eS[email protected] 
1096bd63148eS[email protected]                 log_info("l2cap - state %u", channel->state);
1097bd63148eS[email protected] 
1098e569dfd9SMatthias Ringwald                 gap_security_level_t actual_level = (gap_security_level_t) packet[4];
10995533f01eS[email protected]                 gap_security_level_t required_level = channel->required_security_level;
11005533f01eS[email protected] 
1101df3354fcS[email protected]                 switch (channel->state){
1102df3354fcS[email protected]                     case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
11035533f01eS[email protected]                         if (actual_level >= required_level){
1104f85a9399S[email protected]                             channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
1105f85a9399S[email protected]                             l2cap_emit_connection_request(channel);
11061eb2563eS[email protected]                         } else {
1107775ecc36SMatthias Ringwald                             channel->reason = 0x0003; // security block
11081eb2563eS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
11091eb2563eS[email protected]                         }
1110df3354fcS[email protected]                         break;
1111df3354fcS[email protected] 
1112df3354fcS[email protected]                     case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
11135533f01eS[email protected]                         if (actual_level >= required_level){
1114df3354fcS[email protected]                             channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
1115df3354fcS[email protected]                         } else {
1116df3354fcS[email protected]                             // disconnnect, authentication not good enough
1117df3354fcS[email protected]                             hci_disconnect_security_block(handle);
1118df3354fcS[email protected]                         }
1119df3354fcS[email protected]                         break;
1120df3354fcS[email protected] 
1121df3354fcS[email protected]                     default:
1122df3354fcS[email protected]                         break;
1123df3354fcS[email protected]                 }
1124f85a9399S[email protected]             }
1125f85a9399S[email protected]             break;
1126f85a9399S[email protected] 
1127afde0c52Smatthias.ringwald         default:
1128afde0c52Smatthias.ringwald             break;
1129afde0c52Smatthias.ringwald     }
1130afde0c52Smatthias.ringwald 
1131bd63148eS[email protected]     l2cap_run();
11321e6aba47Smatthias.ringwald }
11331e6aba47Smatthias.ringwald 
1134afde0c52Smatthias.ringwald static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint16_t identifier){
1135b1988dceSmatthias.ringwald     channel->remote_sig_id = identifier;
1136e7ff783cSmatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
1137e7ff783cSmatthias.ringwald     l2cap_run();
113884836b65Smatthias.ringwald }
113984836b65Smatthias.ringwald 
11402b360848Smatthias.ringwald static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t data){
11414cf56b4aSmatthias.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."
11422b360848Smatthias.ringwald     if (signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
11432b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].handle = handle;
11442b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].code = code;
11452b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].sig_id = sig_id;
11462b360848Smatthias.ringwald         signaling_responses[signaling_responses_pending].data = data;
11472b360848Smatthias.ringwald         signaling_responses_pending++;
11482b360848Smatthias.ringwald         l2cap_run();
11492b360848Smatthias.ringwald     }
11502b360848Smatthias.ringwald }
11512b360848Smatthias.ringwald 
1152b35f641cSmatthias.ringwald static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
1153645658c9Smatthias.ringwald 
11549da54300S[email protected]     // log_info("l2cap_handle_connection_request for handle %u, psm %u cid 0x%02x", handle, psm, source_cid);
1155645658c9Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1156645658c9Smatthias.ringwald     if (!service) {
1157645658c9Smatthias.ringwald         // 0x0002 PSM not supported
11582b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0002);
1159645658c9Smatthias.ringwald         return;
1160645658c9Smatthias.ringwald     }
1161645658c9Smatthias.ringwald 
11625061f3afS[email protected]     hci_connection_t * hci_connection = hci_connection_for_handle( handle );
1163645658c9Smatthias.ringwald     if (!hci_connection) {
11642b360848Smatthias.ringwald         //
11659da54300S[email protected]         log_error("no hci_connection for handle %u", handle);
1166645658c9Smatthias.ringwald         return;
1167645658c9Smatthias.ringwald     }
11682bd8b7e7S[email protected] 
1169645658c9Smatthias.ringwald     // alloc structure
11709da54300S[email protected]     // log_info("l2cap_handle_connection_request register channel");
1171da144af5SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, hci_connection->address, BD_ADDR_TYPE_CLASSIC,
1172da144af5SMatthias Ringwald     psm, service->mtu, service->required_security_level);
11732b360848Smatthias.ringwald     if (!channel){
11742b360848Smatthias.ringwald         // 0x0004 No resources available
11752b360848Smatthias.ringwald         l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0004);
11762b360848Smatthias.ringwald         return;
11772b360848Smatthias.ringwald     }
1178da144af5SMatthias Ringwald 
1179fc64f94aSMatthias Ringwald     channel->con_handle = handle;
1180b35f641cSmatthias.ringwald     channel->remote_cid = source_cid;
1181b1988dceSmatthias.ringwald     channel->remote_sig_id = sig_id;
1182645658c9Smatthias.ringwald 
1183f53da564S[email protected]     // limit local mtu to max acl packet length - l2cap header
11842985cb84Smatthias.ringwald     if (channel->local_mtu > l2cap_max_mtu()) {
11852985cb84Smatthias.ringwald         channel->local_mtu = l2cap_max_mtu();
11869775e25bSmatthias.ringwald     }
11879775e25bSmatthias.ringwald 
1188645658c9Smatthias.ringwald     // set initial state
1189df3354fcS[email protected]     channel->state =     L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
1190ad671560S[email protected]     channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
1191e405ae81Smatthias.ringwald 
1192645658c9Smatthias.ringwald     // add to connections list
1193665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) channel);
1194645658c9Smatthias.ringwald 
1195f85a9399S[email protected]     // assert security requirements
11961eb2563eS[email protected]     gap_request_security_level(handle, channel->required_security_level);
1197e405ae81Smatthias.ringwald }
1198645658c9Smatthias.ringwald 
1199ce8f182eSMatthias Ringwald void l2cap_accept_connection(uint16_t local_cid){
1200e0abb8e7S[email protected]     log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
1201b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
1202e405ae81Smatthias.ringwald     if (!channel) {
1203ce8f182eSMatthias Ringwald         log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
1204e405ae81Smatthias.ringwald         return;
1205e405ae81Smatthias.ringwald     }
1206e405ae81Smatthias.ringwald 
1207552d92a1Smatthias.ringwald     channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
1208e405ae81Smatthias.ringwald 
1209552d92a1Smatthias.ringwald     // process
1210552d92a1Smatthias.ringwald     l2cap_run();
1211e405ae81Smatthias.ringwald }
1212645658c9Smatthias.ringwald 
12137ef6a7bbSMatthias Ringwald void l2cap_decline_connection(uint16_t local_cid){
12147ef6a7bbSMatthias Ringwald     log_info("L2CAP_DECLINE_CONNECTION local_cid 0x%x", local_cid);
1215b35f641cSmatthias.ringwald     l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
1216e405ae81Smatthias.ringwald     if (!channel) {
1217ce8f182eSMatthias Ringwald         log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
1218e405ae81Smatthias.ringwald         return;
1219e405ae81Smatthias.ringwald     }
1220e7ff783cSmatthias.ringwald     channel->state  = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
12217ef6a7bbSMatthias Ringwald     channel->reason = 0x04; // no resources available
1222e7ff783cSmatthias.ringwald     l2cap_run();
1223645658c9Smatthias.ringwald }
1224645658c9Smatthias.ringwald 
12257f02f414SMatthias Ringwald static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
1226b1988dceSmatthias.ringwald 
1227b1988dceSmatthias.ringwald     channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
1228b1988dceSmatthias.ringwald 
1229f8fbdce0SMatthias Ringwald     uint16_t flags = little_endian_read_16(command, 6);
123063a7246aSmatthias.ringwald     if (flags & 1) {
123163a7246aSmatthias.ringwald         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
123263a7246aSmatthias.ringwald     }
123363a7246aSmatthias.ringwald 
12342784b77dSmatthias.ringwald     // accept the other's configuration options
1235f8fbdce0SMatthias Ringwald     uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
12363de7c0caSmatthias.ringwald     uint16_t pos     = 8;
12373de7c0caSmatthias.ringwald     while (pos < end_pos){
123863a7246aSmatthias.ringwald         uint8_t option_hint = command[pos] >> 7;
123963a7246aSmatthias.ringwald         uint8_t option_type = command[pos] & 0x7f;
124063a7246aSmatthias.ringwald         log_info("l2cap cid %u, hint %u, type %u", channel->local_cid, option_hint, option_type);
124163a7246aSmatthias.ringwald         pos++;
12421dc511deSmatthias.ringwald         uint8_t length = command[pos++];
12431dc511deSmatthias.ringwald         // MTU { type(8): 1, len(8):2, MTU(16) }
124463a7246aSmatthias.ringwald         if (option_type == 1 && length == 2){
1245f8fbdce0SMatthias Ringwald             channel->remote_mtu = little_endian_read_16(command, pos);
12469da54300S[email protected]             // log_info("l2cap cid 0x%02x, remote mtu %u", channel->local_cid, channel->remote_mtu);
124763a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
124863a7246aSmatthias.ringwald         }
12490fe7a9d0S[email protected]         // Flush timeout { type(8):2, len(8): 2, Flush Timeout(16)}
12500fe7a9d0S[email protected]         if (option_type == 2 && length == 2){
1251f8fbdce0SMatthias Ringwald             channel->flush_timeout = little_endian_read_16(command, pos);
12520fe7a9d0S[email protected]         }
125363a7246aSmatthias.ringwald         // check for unknown options
125463a7246aSmatthias.ringwald         if (option_hint == 0 && (option_type == 0 || option_type >= 0x07)){
1255c177a91cS[email protected]             log_info("l2cap cid %u, unknown options", channel->local_cid);
125663a7246aSmatthias.ringwald             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
12571dc511deSmatthias.ringwald         }
12581dc511deSmatthias.ringwald         pos += length;
12591dc511deSmatthias.ringwald     }
12602784b77dSmatthias.ringwald }
12612784b77dSmatthias.ringwald 
1262fa8473a4Smatthias.ringwald static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
12639da54300S[email protected]     // log_info("l2cap_channel_ready_for_open 0x%02x", channel->state_var);
126473cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
126573cf2b3dSmatthias.ringwald     if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
1266019f9b43SMatthias Ringwald     // addition check that fixes re-entrance issue causing l2cap event channel opened twice
1267019f9b43SMatthias Ringwald     if (channel->state == L2CAP_STATE_OPEN) return 0;
1268fa8473a4Smatthias.ringwald     return 1;
1269fa8473a4Smatthias.ringwald }
1270fa8473a4Smatthias.ringwald 
1271fa8473a4Smatthias.ringwald 
12727f02f414SMatthias Ringwald static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
12731e6aba47Smatthias.ringwald 
127400d93d79Smatthias.ringwald     uint8_t  code       = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
127500d93d79Smatthias.ringwald     uint8_t  identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
127638e5900eSmatthias.ringwald     uint16_t result = 0;
12771e6aba47Smatthias.ringwald 
12789da54300S[email protected]     log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
1279b35f641cSmatthias.ringwald 
12809a011532Smatthias.ringwald     // handle DISCONNECT REQUESTS seperately
12819a011532Smatthias.ringwald     if (code == DISCONNECTION_REQUEST){
12829a011532Smatthias.ringwald         switch (channel->state){
1283fa8473a4Smatthias.ringwald             case L2CAP_STATE_CONFIG:
12849a011532Smatthias.ringwald             case L2CAP_STATE_OPEN:
12852b83fb7dSmatthias.ringwald             case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
12869a011532Smatthias.ringwald             case L2CAP_STATE_WAIT_DISCONNECT:
12879a011532Smatthias.ringwald                 l2cap_handle_disconnect_request(channel, identifier);
12889a011532Smatthias.ringwald                 break;
12899a011532Smatthias.ringwald 
12909a011532Smatthias.ringwald             default:
12919a011532Smatthias.ringwald                 // ignore in other states
12929a011532Smatthias.ringwald                 break;
12939a011532Smatthias.ringwald         }
12949a011532Smatthias.ringwald         return;
12959a011532Smatthias.ringwald     }
12969a011532Smatthias.ringwald 
129756081214Smatthias.ringwald     // @STATEMACHINE(l2cap)
12981e6aba47Smatthias.ringwald     switch (channel->state) {
12991e6aba47Smatthias.ringwald 
13001e6aba47Smatthias.ringwald         case L2CAP_STATE_WAIT_CONNECT_RSP:
13011e6aba47Smatthias.ringwald             switch (code){
13021e6aba47Smatthias.ringwald                 case CONNECTION_RESPONSE:
13035932bd7cS[email protected]                     l2cap_stop_rtx(channel);
1304f8fbdce0SMatthias Ringwald                     result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
130538e5900eSmatthias.ringwald                     switch (result) {
130638e5900eSmatthias.ringwald                         case 0:
1307169f8b28Smatthias.ringwald                             // successful connection
1308f8fbdce0SMatthias Ringwald                             channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1309fa8473a4Smatthias.ringwald                             channel->state = L2CAP_STATE_CONFIG;
131028ca2b46S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
131138e5900eSmatthias.ringwald                             break;
131238e5900eSmatthias.ringwald                         case 1:
13135932bd7cS[email protected]                             // connection pending. get some coffee, but start the ERTX
13145932bd7cS[email protected]                             l2cap_start_ertx(channel);
131538e5900eSmatthias.ringwald                             break;
131638e5900eSmatthias.ringwald                         default:
1317eb920dbeSmatthias.ringwald                             // channel closed
1318eb920dbeSmatthias.ringwald                             channel->state = L2CAP_STATE_CLOSED;
1319f32b992eSmatthias.ringwald                             // map l2cap connection response result to BTstack status enumeration
132038e5900eSmatthias.ringwald                             l2cap_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
1321eb920dbeSmatthias.ringwald 
1322eb920dbeSmatthias.ringwald                             // drop link key if security block
1323eb920dbeSmatthias.ringwald                             if (L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
132415a95bd5SMatthias Ringwald                                 gap_drop_link_key_for_bd_addr(channel->address);
1325eb920dbeSmatthias.ringwald                             }
1326eb920dbeSmatthias.ringwald 
1327eb920dbeSmatthias.ringwald                             // discard channel
1328665d90f2SMatthias Ringwald                             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1329d3a9df87Smatthias.ringwald                             btstack_memory_l2cap_channel_free(channel);
133038e5900eSmatthias.ringwald                             break;
13311e6aba47Smatthias.ringwald                     }
13321e6aba47Smatthias.ringwald                     break;
133338e5900eSmatthias.ringwald 
133438e5900eSmatthias.ringwald                 default:
13351e6aba47Smatthias.ringwald                     //@TODO: implement other signaling packets
133638e5900eSmatthias.ringwald                     break;
13371e6aba47Smatthias.ringwald             }
13381e6aba47Smatthias.ringwald             break;
13391e6aba47Smatthias.ringwald 
1340fa8473a4Smatthias.ringwald         case L2CAP_STATE_CONFIG:
1341f8fbdce0SMatthias Ringwald             result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
1342ae280e73Smatthias.ringwald             switch (code) {
1343ae280e73Smatthias.ringwald                 case CONFIGURE_REQUEST:
134428ca2b46S[email protected]                     channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
1345ae280e73Smatthias.ringwald                     l2cap_signaling_handle_configure_request(channel, command);
134663a7246aSmatthias.ringwald                     if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
134763a7246aSmatthias.ringwald                         // only done if continuation not set
134863a7246aSmatthias.ringwald                         channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
134963a7246aSmatthias.ringwald                     }
1350ae280e73Smatthias.ringwald                     break;
13511e6aba47Smatthias.ringwald                 case CONFIGURE_RESPONSE:
13525932bd7cS[email protected]                     l2cap_stop_rtx(channel);
13535932bd7cS[email protected]                     switch (result){
13545932bd7cS[email protected]                         case 0: // success
13555932bd7cS[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
13565932bd7cS[email protected]                             break;
13575932bd7cS[email protected]                         case 4: // pending
13585932bd7cS[email protected]                             l2cap_start_ertx(channel);
13595932bd7cS[email protected]                             break;
13605932bd7cS[email protected]                         default:
1361fe9d8984S[email protected]                             // retry on negative result
1362fe9d8984S[email protected]                             channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
1363fe9d8984S[email protected]                             break;
1364fe9d8984S[email protected]                     }
13655a67bd4aSmatthias.ringwald                     break;
13665a67bd4aSmatthias.ringwald                 default:
13675a67bd4aSmatthias.ringwald                     break;
13681e6aba47Smatthias.ringwald             }
1369fa8473a4Smatthias.ringwald             if (l2cap_channel_ready_for_open(channel)){
1370fa8473a4Smatthias.ringwald                 // for open:
13715a67bd4aSmatthias.ringwald                 channel->state = L2CAP_STATE_OPEN;
1372fa8473a4Smatthias.ringwald                 l2cap_emit_channel_opened(channel, 0);
1373c8e4258aSmatthias.ringwald             }
1374c8e4258aSmatthias.ringwald             break;
1375f62db1e3Smatthias.ringwald 
1376f62db1e3Smatthias.ringwald         case L2CAP_STATE_WAIT_DISCONNECT:
1377f62db1e3Smatthias.ringwald             switch (code) {
1378f62db1e3Smatthias.ringwald                 case DISCONNECTION_RESPONSE:
137927a923d0Smatthias.ringwald                     l2cap_finialize_channel_close(channel);
138027a923d0Smatthias.ringwald                     break;
13815a67bd4aSmatthias.ringwald                 default:
13825a67bd4aSmatthias.ringwald                     //@TODO: implement other signaling packets
13835a67bd4aSmatthias.ringwald                     break;
138427a923d0Smatthias.ringwald             }
138527a923d0Smatthias.ringwald             break;
138684836b65Smatthias.ringwald 
138784836b65Smatthias.ringwald         case L2CAP_STATE_CLOSED:
138884836b65Smatthias.ringwald             // @TODO handle incoming requests
138984836b65Smatthias.ringwald             break;
139084836b65Smatthias.ringwald 
139184836b65Smatthias.ringwald         case L2CAP_STATE_OPEN:
139284836b65Smatthias.ringwald             //@TODO: implement other signaling packets, e.g. re-configure
139384836b65Smatthias.ringwald             break;
139410642e45Smatthias.ringwald         default:
139510642e45Smatthias.ringwald             break;
139627a923d0Smatthias.ringwald     }
13979da54300S[email protected]     // log_info("new state %u", channel->state);
139827a923d0Smatthias.ringwald }
139927a923d0Smatthias.ringwald 
140000d93d79Smatthias.ringwald 
14017f02f414SMatthias Ringwald static void l2cap_signaling_handler_dispatch( hci_con_handle_t handle, uint8_t * command){
140200d93d79Smatthias.ringwald 
140300d93d79Smatthias.ringwald     // get code, signalind identifier and command len
140400d93d79Smatthias.ringwald     uint8_t code   = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
140500d93d79Smatthias.ringwald     uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
140600d93d79Smatthias.ringwald 
140700d93d79Smatthias.ringwald     // not for a particular channel, and not CONNECTION_REQUEST, ECHO_[REQUEST|RESPONSE], INFORMATION_REQUEST
140800d93d79Smatthias.ringwald     if (code < 1 || code == ECHO_RESPONSE || code > INFORMATION_REQUEST){
140963a7246aSmatthias.ringwald         l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, L2CAP_REJ_CMD_UNKNOWN);
141000d93d79Smatthias.ringwald         return;
141100d93d79Smatthias.ringwald     }
141200d93d79Smatthias.ringwald 
141300d93d79Smatthias.ringwald     // general commands without an assigned channel
141400d93d79Smatthias.ringwald     switch(code) {
141500d93d79Smatthias.ringwald 
141600d93d79Smatthias.ringwald         case CONNECTION_REQUEST: {
1417f8fbdce0SMatthias Ringwald             uint16_t psm =        little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
1418f8fbdce0SMatthias Ringwald             uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
141900d93d79Smatthias.ringwald             l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
14202b83fb7dSmatthias.ringwald             return;
142100d93d79Smatthias.ringwald         }
142200d93d79Smatthias.ringwald 
14232b360848Smatthias.ringwald         case ECHO_REQUEST:
14242b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, 0);
14252b83fb7dSmatthias.ringwald             return;
142600d93d79Smatthias.ringwald 
142700d93d79Smatthias.ringwald         case INFORMATION_REQUEST: {
1428f8fbdce0SMatthias Ringwald             uint16_t infoType = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
14292b360848Smatthias.ringwald             l2cap_register_signaling_response(handle, code, sig_id, infoType);
14302b83fb7dSmatthias.ringwald             return;
143100d93d79Smatthias.ringwald         }
143200d93d79Smatthias.ringwald 
143300d93d79Smatthias.ringwald         default:
143400d93d79Smatthias.ringwald             break;
143500d93d79Smatthias.ringwald     }
143600d93d79Smatthias.ringwald 
143700d93d79Smatthias.ringwald 
143800d93d79Smatthias.ringwald     // Get potential destination CID
1439f8fbdce0SMatthias Ringwald     uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
144000d93d79Smatthias.ringwald 
144100d93d79Smatthias.ringwald     // Find channel for this sig_id and connection handle
1442665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1443665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &l2cap_channels);
1444665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1445665d90f2SMatthias Ringwald         l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
1446fc64f94aSMatthias Ringwald         if (channel->con_handle != handle) continue;
144700d93d79Smatthias.ringwald         if (code & 1) {
1448b1988dceSmatthias.ringwald             // match odd commands (responses) by previous signaling identifier
1449b1988dceSmatthias.ringwald             if (channel->local_sig_id == sig_id) {
145000d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
14514e32727eSmatthias.ringwald                 break;
145200d93d79Smatthias.ringwald             }
145300d93d79Smatthias.ringwald         } else {
1454b1988dceSmatthias.ringwald             // match even commands (requests) by local channel id
145500d93d79Smatthias.ringwald             if (channel->local_cid == dest_cid) {
145600d93d79Smatthias.ringwald                 l2cap_signaling_handler_channel(channel, command);
14574e32727eSmatthias.ringwald                 break;
145800d93d79Smatthias.ringwald             }
145900d93d79Smatthias.ringwald         }
146000d93d79Smatthias.ringwald     }
146100d93d79Smatthias.ringwald }
146200d93d79Smatthias.ringwald 
1463*c48b2a2cSMatthias Ringwald // @returns valid
1464*c48b2a2cSMatthias Ringwald static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
1465*c48b2a2cSMatthias Ringwald     uint16_t result;
1466*c48b2a2cSMatthias Ringwald     uint8_t event[10];
146700d93d79Smatthias.ringwald 
1468*c48b2a2cSMatthias Ringwald     switch (command[0]){
146900d93d79Smatthias.ringwald 
1470*c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_RESPONSE:
1471*c48b2a2cSMatthias Ringwald             result = little_endian_read_16(command, 4);
1472ccf076adS[email protected]             l2cap_emit_connection_parameter_update_response(handle, result);
1473ccf076adS[email protected]             break;
1474da886c03S[email protected] 
1475*c48b2a2cSMatthias Ringwald         case CONNECTION_PARAMETER_UPDATE_REQUEST:
1476da886c03S[email protected]             hci_connection_t * connection = hci_connection_for_handle(handle);
1477da886c03S[email protected]             if (connection){
14786d91fb6cSMatthias Ringwald                 if (connection->role != HCI_ROLE_MASTER){
14796d91fb6cSMatthias Ringwald                     // reject command without notifying upper layer when not in master role
1480*c48b2a2cSMatthias Ringwald                     return 0;
14816d91fb6cSMatthias Ringwald                 }
1482da886c03S[email protected]                 int update_parameter = 1;
1483a4c06b28SMatthias Ringwald                 le_connection_parameter_range_t existing_range;
14844ced4e8cSMatthias Ringwald                 gap_get_connection_parameter_range(&existing_range);
1485*c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_min = little_endian_read_16(command,8);
1486*c48b2a2cSMatthias Ringwald                 uint16_t le_conn_interval_max = little_endian_read_16(command,10);
1487*c48b2a2cSMatthias Ringwald                 uint16_t le_conn_latency = little_endian_read_16(command,12);
1488*c48b2a2cSMatthias Ringwald                 uint16_t le_supervision_timeout = little_endian_read_16(command,14);
1489da886c03S[email protected] 
1490da886c03S[email protected]                 if (le_conn_interval_min < existing_range.le_conn_interval_min) update_parameter = 0;
1491da886c03S[email protected]                 if (le_conn_interval_max > existing_range.le_conn_interval_max) update_parameter = 0;
1492da886c03S[email protected] 
1493da886c03S[email protected]                 if (le_conn_latency < existing_range.le_conn_latency_min) update_parameter = 0;
1494da886c03S[email protected]                 if (le_conn_latency > existing_range.le_conn_latency_max) update_parameter = 0;
1495da886c03S[email protected] 
1496da886c03S[email protected]                 if (le_supervision_timeout < existing_range.le_supervision_timeout_min) update_parameter = 0;
1497da886c03S[email protected]                 if (le_supervision_timeout > existing_range.le_supervision_timeout_max) update_parameter = 0;
1498da886c03S[email protected] 
1499da886c03S[email protected]                 if (update_parameter){
1500da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
1501da886c03S[email protected]                     connection->le_conn_interval_min = le_conn_interval_min;
1502da886c03S[email protected]                     connection->le_conn_interval_max = le_conn_interval_max;
1503da886c03S[email protected]                     connection->le_conn_latency = le_conn_latency;
1504da886c03S[email protected]                     connection->le_supervision_timeout = le_supervision_timeout;
1505da886c03S[email protected]                 } else {
1506da886c03S[email protected]                     connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
1507da886c03S[email protected]                 }
1508*c48b2a2cSMatthias Ringwald                 connection->le_con_param_update_identifier = sig_id;
1509da886c03S[email protected]             }
1510da886c03S[email protected] 
151133c40538SMatthias Ringwald             if (!l2cap_event_packet_handler) break;
1512*c48b2a2cSMatthias Ringwald 
1513*c48b2a2cSMatthias Ringwald             event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
1514*c48b2a2cSMatthias Ringwald             event[1] = 8;
1515*c48b2a2cSMatthias Ringwald             memcpy(&event[2], &command[4], 8);
1516*c48b2a2cSMatthias Ringwald             hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
151733c40538SMatthias Ringwald             (*l2cap_event_packet_handler)( HCI_EVENT_PACKET, 0, event, sizeof(event));
1518ccf076adS[email protected]             break;
1519*c48b2a2cSMatthias Ringwald 
1520*c48b2a2cSMatthias Ringwald         default:
1521*c48b2a2cSMatthias Ringwald             // command unknown -> reject command
1522*c48b2a2cSMatthias Ringwald             return 0;
1523ccf076adS[email protected]     }
1524*c48b2a2cSMatthias Ringwald     return 1;
1525*c48b2a2cSMatthias Ringwald }
1526*c48b2a2cSMatthias Ringwald 
1527*c48b2a2cSMatthias Ringwald static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size ){
1528*c48b2a2cSMatthias Ringwald 
1529*c48b2a2cSMatthias Ringwald     // Get Channel ID
1530*c48b2a2cSMatthias Ringwald     uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
1531*c48b2a2cSMatthias Ringwald     uint8_t sig_id;
1532*c48b2a2cSMatthias Ringwald     hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
1533*c48b2a2cSMatthias Ringwald     int valid;
1534*c48b2a2cSMatthias Ringwald 
1535*c48b2a2cSMatthias Ringwald     switch (channel_id) {
1536*c48b2a2cSMatthias Ringwald 
1537*c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING: {
1538*c48b2a2cSMatthias Ringwald             uint16_t command_offset = 8;
1539*c48b2a2cSMatthias Ringwald             while (command_offset < size) {
1540*c48b2a2cSMatthias Ringwald 
1541*c48b2a2cSMatthias Ringwald                 // handle signaling commands
1542*c48b2a2cSMatthias Ringwald                 l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
1543*c48b2a2cSMatthias Ringwald 
1544*c48b2a2cSMatthias Ringwald                 // increment command_offset
1545*c48b2a2cSMatthias Ringwald                 command_offset += L2CAP_SIGNALING_COMMAND_DATA_OFFSET + little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
1546*c48b2a2cSMatthias Ringwald             }
1547*c48b2a2cSMatthias Ringwald             break;
1548*c48b2a2cSMatthias Ringwald         }
1549*c48b2a2cSMatthias Ringwald 
1550*c48b2a2cSMatthias Ringwald         case L2CAP_CID_SIGNALING_LE:
1551*c48b2a2cSMatthias Ringwald             sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
1552*c48b2a2cSMatthias Ringwald             valid  = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
1553*c48b2a2cSMatthias Ringwald             if (!valid){
15541bbc0b23S[email protected]                 l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, L2CAP_REJ_CMD_UNKNOWN);
1555ccf076adS[email protected]             }
1556ccf076adS[email protected]             break;
1557*c48b2a2cSMatthias Ringwald 
1558*c48b2a2cSMatthias Ringwald         case L2CAP_CID_ATTRIBUTE_PROTOCOL:
1559*c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback) {
1560*c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_ATTRIBUTE_PROTOCOL].callback)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1561ccf076adS[email protected]             }
1562*c48b2a2cSMatthias Ringwald             break;
1563*c48b2a2cSMatthias Ringwald 
1564*c48b2a2cSMatthias Ringwald         case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
1565*c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback) {
1566*c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_SECURITY_MANAGER_PROTOCOL].callback)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1567*c48b2a2cSMatthias Ringwald             }
1568*c48b2a2cSMatthias Ringwald             break;
1569*c48b2a2cSMatthias Ringwald 
1570*c48b2a2cSMatthias Ringwald         case L2CAP_CID_CONNECTIONLESS_CHANNEL:
1571*c48b2a2cSMatthias Ringwald             if (fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback) {
1572*c48b2a2cSMatthias Ringwald                 (*fixed_channels[L2CAP_FIXED_CHANNEL_TABLE_INDEX_CONNECTIONLESS_CHANNEL].callback)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
1573*c48b2a2cSMatthias Ringwald             }
1574*c48b2a2cSMatthias Ringwald             break;
15751bbc0b23S[email protected] 
15765652b5ffS[email protected]         default: {
157700d93d79Smatthias.ringwald             // Find channel for this channel_id and connection handle
15783d50b4baSMatthias Ringwald             l2cap_channel_t * l2cap_channel = l2cap_get_channel_for_local_cid(channel_id);
1579d1fd2a88SMatthias Ringwald             if (l2cap_channel) {
15803d50b4baSMatthias Ringwald                 l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
158100d93d79Smatthias.ringwald             }
15825652b5ffS[email protected]             break;
15835652b5ffS[email protected]         }
15845652b5ffS[email protected]     }
158500d93d79Smatthias.ringwald 
15861eb2563eS[email protected]     l2cap_run();
15872718e2e7Smatthias.ringwald }
158800d93d79Smatthias.ringwald 
158915ec09bbSmatthias.ringwald // finalize closed channel - l2cap_handle_disconnect_request & DISCONNECTION_RESPONSE
159027a923d0Smatthias.ringwald void l2cap_finialize_channel_close(l2cap_channel_t * channel){
1591f62db1e3Smatthias.ringwald     channel->state = L2CAP_STATE_CLOSED;
1592f62db1e3Smatthias.ringwald     l2cap_emit_channel_closed(channel);
1593f62db1e3Smatthias.ringwald     // discard channel
15949dcb2fb2S[email protected]     l2cap_stop_rtx(channel);
1595665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1596d3a9df87Smatthias.ringwald     btstack_memory_l2cap_channel_free(channel);
1597c8e4258aSmatthias.ringwald }
15981e6aba47Smatthias.ringwald 
15998f2a52f4SMatthias Ringwald static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
1600665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1601665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, services);
1602665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1603665d90f2SMatthias Ringwald         l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
16049d9bbc01Smatthias.ringwald         if ( service->psm == psm){
16059d9bbc01Smatthias.ringwald             return service;
16069d9bbc01Smatthias.ringwald         };
16079d9bbc01Smatthias.ringwald     }
16089d9bbc01Smatthias.ringwald     return NULL;
16099d9bbc01Smatthias.ringwald }
16109d9bbc01Smatthias.ringwald 
16117192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
16127192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_services, psm);
16137192e786SMatthias Ringwald }
16147192e786SMatthias Ringwald 
1615e0abb8e7S[email protected] 
1616be2053a6SMatthias 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){
1617be2053a6SMatthias Ringwald 
1618be2053a6SMatthias Ringwald     log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
1619e0abb8e7S[email protected] 
16204bb582b6Smatthias.ringwald     // check for alread registered psm
16219d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
1622277abc2cSmatthias.ringwald     if (service) {
1623be2053a6SMatthias Ringwald         log_error("l2cap_register_service: PSM %u already registered", psm);
1624be2053a6SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
1625277abc2cSmatthias.ringwald     }
16269d9bbc01Smatthias.ringwald 
16274bb582b6Smatthias.ringwald     // alloc structure
1628bb69aaaeS[email protected]     service = btstack_memory_l2cap_service_get();
1629277abc2cSmatthias.ringwald     if (!service) {
1630be2053a6SMatthias Ringwald         log_error("l2cap_register_service: no memory for l2cap_service_t");
1631be2053a6SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1632277abc2cSmatthias.ringwald     }
16339d9bbc01Smatthias.ringwald 
16349d9bbc01Smatthias.ringwald     // fill in
16359d9bbc01Smatthias.ringwald     service->psm = psm;
16369d9bbc01Smatthias.ringwald     service->mtu = mtu;
163705ae8de3SMatthias Ringwald     service->packet_handler = service_packet_handler;
1638df3354fcS[email protected]     service->required_security_level = security_level;
16399d9bbc01Smatthias.ringwald 
16409d9bbc01Smatthias.ringwald     // add to services list
1641665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
1642c0e866bfSmatthias.ringwald 
1643c0e866bfSmatthias.ringwald     // enable page scan
164415a95bd5SMatthias Ringwald     gap_connectable_control(1);
16455842b6d9Smatthias.ringwald 
1646be2053a6SMatthias Ringwald     return 0;
16479d9bbc01Smatthias.ringwald }
16489d9bbc01Smatthias.ringwald 
16497e8856ebSMatthias Ringwald uint8_t l2cap_unregister_service(uint16_t psm){
1650e0abb8e7S[email protected] 
1651e0abb8e7S[email protected]     log_info("L2CAP_UNREGISTER_SERVICE psm 0x%x", psm);
1652e0abb8e7S[email protected] 
16539d9bbc01Smatthias.ringwald     l2cap_service_t *service = l2cap_get_service(psm);
16547e8856ebSMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1655665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
1656d3a9df87Smatthias.ringwald     btstack_memory_l2cap_service_free(service);
1657c0e866bfSmatthias.ringwald 
1658c0e866bfSmatthias.ringwald     // disable page scan when no services registered
16597e8856ebSMatthias Ringwald     if (btstack_linked_list_empty(&l2cap_services)) {
166015a95bd5SMatthias Ringwald         gap_connectable_control(0);
16619d9bbc01Smatthias.ringwald     }
16627e8856ebSMatthias Ringwald     return 0;
16637e8856ebSMatthias Ringwald }
16649d9bbc01Smatthias.ringwald 
16655652b5ffS[email protected] // Bluetooth 4.0 - allows to register handler for Attribute Protocol and Security Manager Protocol
166605ae8de3SMatthias Ringwald void l2cap_register_fixed_channel(btstack_packet_handler_t the_packet_handler, uint16_t channel_id) {
16675628cf69SMatthias Ringwald     int index = l2cap_fixed_channel_table_index_for_channel_id(channel_id);
16685628cf69SMatthias Ringwald     if (index < 0) return;
16695628cf69SMatthias Ringwald     fixed_channels[index].callback = the_packet_handler;
16705652b5ffS[email protected] }
16715652b5ffS[email protected] 
1672a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE
16737192e786SMatthias Ringwald 
16747192e786SMatthias Ringwald static inline l2cap_service_t * l2cap_le_get_service(uint16_t psm){
16757192e786SMatthias Ringwald     return l2cap_get_service_internal(&l2cap_le_services, psm);
16767192e786SMatthias Ringwald }
1677efedfb4cSMatthias Ringwald 
1678da144af5SMatthias Ringwald uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
16797192e786SMatthias Ringwald 
1680da144af5SMatthias Ringwald     log_info("L2CAP_LE_REGISTER_SERVICE psm 0x%x", psm);
16817192e786SMatthias Ringwald 
16827192e786SMatthias Ringwald     // check for alread registered psm
16837192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
16847192e786SMatthias Ringwald     if (service) {
1685da144af5SMatthias Ringwald         return L2CAP_SERVICE_ALREADY_REGISTERED;
16867192e786SMatthias Ringwald     }
16877192e786SMatthias Ringwald 
16887192e786SMatthias Ringwald     // alloc structure
16897192e786SMatthias Ringwald     service = btstack_memory_l2cap_service_get();
16907192e786SMatthias Ringwald     if (!service) {
16917192e786SMatthias Ringwald         log_error("l2cap_register_service_internal: no memory for l2cap_service_t");
1692da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
16937192e786SMatthias Ringwald     }
16947192e786SMatthias Ringwald 
16957192e786SMatthias Ringwald     // fill in
16967192e786SMatthias Ringwald     service->psm = psm;
1697da144af5SMatthias Ringwald     service->mtu = 0;
16987192e786SMatthias Ringwald     service->packet_handler = packet_handler;
16997192e786SMatthias Ringwald     service->required_security_level = security_level;
17007192e786SMatthias Ringwald 
17017192e786SMatthias Ringwald     // add to services list
1702665d90f2SMatthias Ringwald     btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
17037192e786SMatthias Ringwald 
17047192e786SMatthias Ringwald     // done
1705da144af5SMatthias Ringwald     return 0;
17067192e786SMatthias Ringwald }
17077192e786SMatthias Ringwald 
1708da144af5SMatthias Ringwald uint8_t l2cap_le_unregister_service(uint16_t psm) {
17097192e786SMatthias Ringwald     log_info("L2CAP_LE_UNREGISTER_SERVICE psm 0x%x", psm);
17107192e786SMatthias Ringwald     l2cap_service_t *service = l2cap_le_get_service(psm);
17119367f9b0SMatthias Ringwald     if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
1712da144af5SMatthias Ringwald 
1713665d90f2SMatthias Ringwald     btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
17147192e786SMatthias Ringwald     btstack_memory_l2cap_service_free(service);
1715da144af5SMatthias Ringwald     return 0;
17167192e786SMatthias Ringwald }
1717da144af5SMatthias Ringwald 
1718da144af5SMatthias Ringwald uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
1719efedfb4cSMatthias Ringwald     // get connection
1720efedfb4cSMatthias Ringwald     // bail if missing
1721efedfb4cSMatthias Ringwald     // set mtu and receive buffer
1722efedfb4cSMatthias Ringwald     // check state
1723efedfb4cSMatthias Ringwald     // set state accept connection
1724efedfb4cSMatthias Ringwald     // l2cap_le_run()
1725da144af5SMatthias Ringwald     return 0;
1726da144af5SMatthias Ringwald }
1727da144af5SMatthias Ringwald 
1728da144af5SMatthias Ringwald /**
1729da144af5SMatthias Ringwald  * @brief Deny incoming LE Data Channel connection due to resource constraints
1730da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1731da144af5SMatthias Ringwald  */
1732da144af5SMatthias Ringwald 
1733da144af5SMatthias Ringwald uint8_t l2cap_le_decline_connection(uint16_t local_cid){
1734efedfb4cSMatthias Ringwald     // get connection
1735efedfb4cSMatthias Ringwald     // bail if missing
1736efedfb4cSMatthias Ringwald     // check state
1737efedfb4cSMatthias Ringwald     // set state decline connection
1738efedfb4cSMatthias Ringwald     // l2cap_le_run()
1739da144af5SMatthias Ringwald     return 0;
1740da144af5SMatthias Ringwald }
1741da144af5SMatthias Ringwald 
1742da144af5SMatthias Ringwald uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, bd_addr_type_t address_type,
1743da144af5SMatthias Ringwald     uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
1744efedfb4cSMatthias Ringwald     uint16_t * out_local_cid) {
1745efedfb4cSMatthias Ringwald 
1746da144af5SMatthias Ringwald     log_info("L2CAP_LE_CREATE_CHANNEL addr %s psm 0x%x mtu %u", bd_addr_to_str(address), psm, mtu);
1747da144af5SMatthias Ringwald 
17485cb87675SMatthias Ringwald     l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, address, address_type, psm, mtu, LEVEL_0);
1749da144af5SMatthias Ringwald     if (!channel) {
1750da144af5SMatthias Ringwald         return BTSTACK_MEMORY_ALLOC_FAILED;
1751da144af5SMatthias Ringwald     }
1752da144af5SMatthias Ringwald 
1753da144af5SMatthias Ringwald     // store local_cid
1754da144af5SMatthias Ringwald     if (out_local_cid){
1755da144af5SMatthias Ringwald        *out_local_cid = channel->local_cid;
1756da144af5SMatthias Ringwald     }
1757da144af5SMatthias Ringwald 
1758efedfb4cSMatthias Ringwald     // add to connections list
1759efedfb4cSMatthias Ringwald     btstack_linked_list_add(&l2cap_le_channels, (btstack_linked_item_t *) channel);
1760efedfb4cSMatthias Ringwald 
1761da144af5SMatthias Ringwald     // check if hci connection is already usable
1762da144af5SMatthias Ringwald     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, address_type);
1763efedfb4cSMatthias Ringwald 
1764efedfb4cSMatthias Ringwald     channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
1765efedfb4cSMatthias Ringwald 
1766da144af5SMatthias Ringwald     if (conn){
1767da144af5SMatthias Ringwald         log_info("l2cap_le_create_channel, hci connection already exists");
17685cb87675SMatthias Ringwald         l2cap_handle_le_connection_complete(channel, 0, conn->con_handle);
1769efedfb4cSMatthias Ringwald     } else {
1770efedfb4cSMatthias Ringwald 
1771efedfb4cSMatthias Ringwald         //
1772efedfb4cSMatthias Ringwald         // TODO: start timer
1773efedfb4cSMatthias Ringwald         // ..
1774efedfb4cSMatthias Ringwald 
1775efedfb4cSMatthias Ringwald         // start connection
1776efedfb4cSMatthias Ringwald         uint8_t res = gap_auto_connection_start(address_type, address);
177748af0c56SMatthias Ringwald         if (res){
1778efedfb4cSMatthias Ringwald             // discard channel object
1779efedfb4cSMatthias Ringwald             btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
1780efedfb4cSMatthias Ringwald             btstack_memory_l2cap_channel_free(channel);
1781efedfb4cSMatthias Ringwald             return res;
1782da144af5SMatthias Ringwald         }
1783efedfb4cSMatthias Ringwald     }
1784da144af5SMatthias Ringwald     return 0;
1785da144af5SMatthias Ringwald }
1786da144af5SMatthias Ringwald 
1787da144af5SMatthias Ringwald /**
1788da144af5SMatthias Ringwald  * @brief Provide credtis for LE Data Channel
1789da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1790da144af5SMatthias Ringwald  * @param credits               Number additional credits for peer
1791da144af5SMatthias Ringwald  */
1792da144af5SMatthias Ringwald uint8_t l2cap_le_provide_credits(uint16_t cid, uint16_t credits){
1793efedfb4cSMatthias Ringwald     // get connection
1794efedfb4cSMatthias Ringwald     // bail if missing
1795efedfb4cSMatthias Ringwald     // check state
1796efedfb4cSMatthias Ringwald     // check incoming credits + credits <= 0xffff
1797efedfb4cSMatthias Ringwald     // set credits_granted
1798efedfb4cSMatthias Ringwald     // l2cap_le_run()
1799da144af5SMatthias Ringwald     return 0;
1800da144af5SMatthias Ringwald }
1801da144af5SMatthias Ringwald 
1802da144af5SMatthias Ringwald /**
1803da144af5SMatthias Ringwald  * @brief Check if outgoing buffer is available and that there's space on the Bluetooth module
1804da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1805da144af5SMatthias Ringwald  */
1806da144af5SMatthias Ringwald int l2cap_le_can_send_now(uint16_t cid){
1807efedfb4cSMatthias Ringwald     // check if data can be sent via le at all
1808da144af5SMatthias Ringwald     return 0;
1809da144af5SMatthias Ringwald }
1810da144af5SMatthias Ringwald 
1811da144af5SMatthias Ringwald /**
1812da144af5SMatthias Ringwald  * @brief Request emission of L2CAP_EVENT_CAN_SEND_NOW as soon as possible
1813da144af5SMatthias Ringwald  * @note L2CAP_EVENT_CAN_SEND_NOW might be emitted during call to this function
1814da144af5SMatthias Ringwald  *       so packet handler should be ready to handle it
1815da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1816da144af5SMatthias Ringwald  */
1817da144af5SMatthias Ringwald uint8_t l2cap_le_request_can_send_now_event(uint16_t cid){
1818efedfb4cSMatthias Ringwald     // same as for non-le
1819da144af5SMatthias Ringwald     return 0;
1820da144af5SMatthias Ringwald }
1821da144af5SMatthias Ringwald 
1822da144af5SMatthias Ringwald /**
1823da144af5SMatthias Ringwald  * @brief Send data via LE Data Channel
1824da144af5SMatthias Ringwald  * @note Since data larger then the maximum PDU needs to be segmented into multiple PDUs, data needs to stay valid until ... event
1825da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1826da144af5SMatthias Ringwald  * @param data                  data to send
1827da144af5SMatthias Ringwald  * @param size                  data size
1828da144af5SMatthias Ringwald  */
1829da144af5SMatthias Ringwald uint8_t l2cap_le_send_data(uint16_t cid, uint8_t * data, uint16_t size){
1830efedfb4cSMatthias Ringwald     // check if no outgoing data is pending
1831efedfb4cSMatthias Ringwald     // store info
1832efedfb4cSMatthias Ringwald     // l2cap_le_run()
1833da144af5SMatthias Ringwald     return 0;
1834da144af5SMatthias Ringwald }
1835da144af5SMatthias Ringwald 
1836da144af5SMatthias Ringwald /**
1837da144af5SMatthias Ringwald  * @brief Disconnect from LE Data Channel
1838da144af5SMatthias Ringwald  * @param local_cid             L2CAP LE Data Channel Identifier
1839da144af5SMatthias Ringwald  */
1840da144af5SMatthias Ringwald uint8_t l2cap_le_disconnect(uint16_t cid)
1841da144af5SMatthias Ringwald {
1842efedfb4cSMatthias Ringwald     // find channel
1843efedfb4cSMatthias Ringwald     // check state
1844efedfb4cSMatthias Ringwald     // set state SEND_DISCONNECT
1845da144af5SMatthias Ringwald     return 0;
1846da144af5SMatthias Ringwald }
1847da144af5SMatthias Ringwald 
18487f02f414SMatthias Ringwald #endif
1849